]> git.zerfleddert.de Git - proxmark3-svn/blame - fpga/hi_read_rx_xcorr.v
fixing iso14443b (issue #103):
[proxmark3-svn] / fpga / hi_read_rx_xcorr.v
CommitLineData
ba06a4b6 1//-----------------------------------------------------------------------------
2//
3// Jonathan Westhues, April 2006
4//-----------------------------------------------------------------------------
5
6module hi_read_rx_xcorr(
7 pck0, ck_1356meg, ck_1356megb,
8 pwr_lo, pwr_hi, pwr_oe1, pwr_oe2, pwr_oe3, pwr_oe4,
9 adc_d, adc_clk,
10 ssp_frame, ssp_din, ssp_dout, ssp_clk,
11 cross_hi, cross_lo,
12 dbg,
51d4f6f1 13 snoop
ba06a4b6 14);
15 input pck0, ck_1356meg, ck_1356megb;
16 output pwr_lo, pwr_hi, pwr_oe1, pwr_oe2, pwr_oe3, pwr_oe4;
17 input [7:0] adc_d;
18 output adc_clk;
19 input ssp_dout;
20 output ssp_frame, ssp_din, ssp_clk;
21 input cross_hi, cross_lo;
22 output dbg;
51d4f6f1 23 input snoop;
ba06a4b6 24
25// Carrier is steady on through this, unless we're snooping.
26assign pwr_hi = ck_1356megb & (~snoop);
27assign pwr_oe1 = 1'b0;
ba06a4b6 28assign pwr_oe3 = 1'b0;
29assign pwr_oe4 = 1'b0;
30
51d4f6f1 31wire adc_clk = ck_1356megb;
ba06a4b6 32
33// When we're a reader, we just need to do the BPSK demod; but when we're an
34// eavesdropper, we also need to pick out the commands sent by the reader,
35// using AM. Do this the same way that we do it for the simulated tag.
51d4f6f1 36reg after_hysteresis, after_hysteresis_prev, after_hysteresis_prev_prev;
ba06a4b6 37reg [11:0] has_been_low_for;
38always @(negedge adc_clk)
39begin
40 if(& adc_d[7:0]) after_hysteresis <= 1'b1;
41 else if(~(| adc_d[7:0])) after_hysteresis <= 1'b0;
42
43 if(after_hysteresis)
44 begin
45 has_been_low_for <= 7'b0;
46 end
47 else
48 begin
49 if(has_been_low_for == 12'd4095)
50 begin
51 has_been_low_for <= 12'd0;
52 after_hysteresis <= 1'b1;
53 end
54 else
55 has_been_low_for <= has_been_low_for + 1;
56 end
57end
58
59// Let us report a correlation every 4 subcarrier cycles, or 4*16 samples,
60// so we need a 6-bit counter.
61reg [5:0] corr_i_cnt;
ba06a4b6 62// And a couple of registers in which to accumulate the correlations.
5b95953d 63// we would add at most 32 times adc_d, the result can be held in 13 bits.
64// Need one additional bit because it can be negative as well
65reg signed [13:0] corr_i_accum;
66reg signed [13:0] corr_q_accum;
ba06a4b6 67reg signed [7:0] corr_i_out;
68reg signed [7:0] corr_q_out;
51d4f6f1 69// clock and frame signal for communication to ARM
70reg ssp_clk;
71reg ssp_frame;
72
73
ba06a4b6 74
75// ADC data appears on the rising edge, so sample it on the falling edge
76always @(negedge adc_clk)
77begin
51d4f6f1 78 corr_i_cnt <= corr_i_cnt + 1;
79
ba06a4b6 80 // These are the correlators: we correlate against in-phase and quadrature
81 // versions of our reference signal, and keep the (signed) result to
82 // send out later over the SSP.
51d4f6f1 83 if(corr_i_cnt == 7'd0)
ba06a4b6 84 begin
85 if(snoop)
86 begin
51d4f6f1 87 // 7 most significant bits of tag signal (signed), 1 bit reader signal:
88 corr_i_out <= {corr_i_accum[13:7], after_hysteresis_prev_prev};
89 corr_q_out <= {corr_q_accum[13:7], after_hysteresis_prev};
90 after_hysteresis_prev_prev <= after_hysteresis;
ba06a4b6 91 end
92 else
93 begin
51d4f6f1 94 // 8 most significant bits of tag signal
ba06a4b6 95 corr_i_out <= corr_i_accum[13:6];
96 corr_q_out <= corr_q_accum[13:6];
97 end
98
99 corr_i_accum <= adc_d;
100 corr_q_accum <= adc_d;
ba06a4b6 101 end
102 else
103 begin
104 if(corr_i_cnt[3])
105 corr_i_accum <= corr_i_accum - adc_d;
106 else
107 corr_i_accum <= corr_i_accum + adc_d;
108
51d4f6f1 109 if(corr_i_cnt[3] == corr_i_cnt[2]) // phase shifted by pi/2
ba06a4b6 110 corr_q_accum <= corr_q_accum + adc_d;
51d4f6f1 111 else
112 corr_q_accum <= corr_q_accum - adc_d;
ba06a4b6 113
ba06a4b6 114 end
115
116 // The logic in hi_simulate.v reports 4 samples per bit. We report two
117 // (I, Q) pairs per bit, so we should do 2 samples per pair.
118 if(corr_i_cnt == 6'd31)
119 after_hysteresis_prev <= after_hysteresis;
120
121 // Then the result from last time is serialized and send out to the ARM.
122 // We get one report each cycle, and each report is 16 bits, so the
123 // ssp_clk should be the adc_clk divided by 64/16 = 4.
124
125 if(corr_i_cnt[1:0] == 2'b10)
126 ssp_clk <= 1'b0;
127
128 if(corr_i_cnt[1:0] == 2'b00)
129 begin
130 ssp_clk <= 1'b1;
131 // Don't shift if we just loaded new data, obviously.
132 if(corr_i_cnt != 7'd0)
133 begin
134 corr_i_out[7:0] <= {corr_i_out[6:0], corr_q_out[7]};
135 corr_q_out[7:1] <= corr_q_out[6:0];
136 end
137 end
138
09c66f1f 139 // set ssp_frame signal for corr_i_cnt = 0..3 and corr_i_cnt = 32..35
51d4f6f1 140 // (send two frames with 8 Bits each)
09c66f1f 141 if(corr_i_cnt[5:2] == 4'b0000 || corr_i_cnt[5:2] == 4'b1000)
ba06a4b6 142 ssp_frame = 1'b1;
143 else
144 ssp_frame = 1'b0;
145
146end
147
148assign ssp_din = corr_i_out[7];
149
150assign dbg = corr_i_cnt[3];
151
152// Unused.
153assign pwr_lo = 1'b0;
51d4f6f1 154assign pwr_oe2 = 1'b0;
ba06a4b6 155
156endmodule
Impressum, Datenschutz