]> git.zerfleddert.de Git - proxmark3-svn/blame - fpga/hi_read_rx_xcorr.v
Merge pull request #523 from Proxmark/iceman1001-patch-2
[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,
3f7aaf24 13 xcorr_is_848, snoop, xcorr_quarter_freq
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;
3f7aaf24 23 input xcorr_is_848, snoop, xcorr_quarter_freq;
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
3f7aaf24 31reg [2:0] fc_div;
da586b17 32always @(negedge ck_1356megb)
3f7aaf24 33 fc_div <= fc_div + 1;
b535053a 34
3f7aaf24 35(* clock_signal = "yes" *) reg adc_clk; // sample frequency, always 16 * fc
36always @(ck_1356megb, xcorr_is_848, xcorr_quarter_freq, fc_div)
d372569b 37 if (xcorr_is_848 & ~xcorr_quarter_freq) // fc = 847.5 kHz, standard ISO14443B
b535053a 38 adc_clk <= ck_1356megb;
d372569b 39 else if (~xcorr_is_848 & ~xcorr_quarter_freq) // fc = 423.75 kHz
3f7aaf24 40 adc_clk <= fc_div[0];
d372569b 41 else if (xcorr_is_848 & xcorr_quarter_freq) // fc = 211.875 kHz
3f7aaf24 42 adc_clk <= fc_div[1];
d372569b 43 else // fc = 105.9375 kHz
3f7aaf24 44 adc_clk <= fc_div[2];
45
ba06a4b6 46// When we're a reader, we just need to do the BPSK demod; but when we're an
47// eavesdropper, we also need to pick out the commands sent by the reader,
48// using AM. Do this the same way that we do it for the simulated tag.
51d4f6f1 49reg after_hysteresis, after_hysteresis_prev, after_hysteresis_prev_prev;
ba06a4b6 50reg [11:0] has_been_low_for;
51always @(negedge adc_clk)
52begin
53 if(& adc_d[7:0]) after_hysteresis <= 1'b1;
54 else if(~(| adc_d[7:0])) after_hysteresis <= 1'b0;
55
56 if(after_hysteresis)
57 begin
58 has_been_low_for <= 7'b0;
59 end
60 else
61 begin
62 if(has_been_low_for == 12'd4095)
63 begin
64 has_been_low_for <= 12'd0;
65 after_hysteresis <= 1'b1;
66 end
67 else
68 has_been_low_for <= has_been_low_for + 1;
69 end
70end
71
d372569b 72// Let us report a correlation every 4 subcarrier cycles, or 4*16=64 samples,
ba06a4b6 73// so we need a 6-bit counter.
74reg [5:0] corr_i_cnt;
ba06a4b6 75// And a couple of registers in which to accumulate the correlations.
d372569b 76// We would add at most 32 times the difference between unmodulated and modulated signal. It should
77// be safe to assume that a tag will not be able to modulate the carrier signal by more than 25%.
78// 32 * 255 * 0,25 = 2040, which can be held in 11 bits. Add 1 bit for sign.
79reg signed [11:0] corr_i_accum;
80reg signed [11:0] corr_q_accum;
81// we will report maximum 8 significant bits
ba06a4b6 82reg signed [7:0] corr_i_out;
83reg signed [7:0] corr_q_out;
51d4f6f1 84// clock and frame signal for communication to ARM
85reg ssp_clk;
86reg ssp_frame;
87
88
46734099 89always @(negedge adc_clk)
90begin
3f7aaf24 91 corr_i_cnt <= corr_i_cnt + 1;
46734099 92end
93
ba06a4b6 94
95// ADC data appears on the rising edge, so sample it on the falling edge
96always @(negedge adc_clk)
97begin
98 // These are the correlators: we correlate against in-phase and quadrature
99 // versions of our reference signal, and keep the (signed) result to
100 // send out later over the SSP.
705bfa10 101 if(corr_i_cnt == 6'd0)
ba06a4b6 102 begin
103 if(snoop)
104 begin
d372569b 105 // Send 7 most significant bits of tag signal (signed), plus 1 bit reader signal
106 corr_i_out <= {corr_i_accum[11:5], after_hysteresis_prev_prev};
107 corr_q_out <= {corr_q_accum[11:5], after_hysteresis_prev};
51d4f6f1 108 after_hysteresis_prev_prev <= after_hysteresis;
ba06a4b6 109 end
110 else
111 begin
d372569b 112 // 8 bits of tag signal
113 corr_i_out <= corr_i_accum[11:4];
114 corr_q_out <= corr_q_accum[11:4];
ba06a4b6 115 end
116
117 corr_i_accum <= adc_d;
118 corr_q_accum <= adc_d;
ba06a4b6 119 end
120 else
121 begin
122 if(corr_i_cnt[3])
123 corr_i_accum <= corr_i_accum - adc_d;
124 else
125 corr_i_accum <= corr_i_accum + adc_d;
126
51d4f6f1 127 if(corr_i_cnt[3] == corr_i_cnt[2]) // phase shifted by pi/2
ba06a4b6 128 corr_q_accum <= corr_q_accum + adc_d;
51d4f6f1 129 else
130 corr_q_accum <= corr_q_accum - adc_d;
ba06a4b6 131
ba06a4b6 132 end
133
134 // The logic in hi_simulate.v reports 4 samples per bit. We report two
135 // (I, Q) pairs per bit, so we should do 2 samples per pair.
705bfa10 136 if(corr_i_cnt == 6'd32)
ba06a4b6 137 after_hysteresis_prev <= after_hysteresis;
138
139 // Then the result from last time is serialized and send out to the ARM.
140 // We get one report each cycle, and each report is 16 bits, so the
141 // ssp_clk should be the adc_clk divided by 64/16 = 4.
142
143 if(corr_i_cnt[1:0] == 2'b10)
144 ssp_clk <= 1'b0;
145
146 if(corr_i_cnt[1:0] == 2'b00)
147 begin
148 ssp_clk <= 1'b1;
149 // Don't shift if we just loaded new data, obviously.
b535053a 150 if(corr_i_cnt != 6'd0)
ba06a4b6 151 begin
152 corr_i_out[7:0] <= {corr_i_out[6:0], corr_q_out[7]};
153 corr_q_out[7:1] <= corr_q_out[6:0];
154 end
155 end
156
09c66f1f 157 // set ssp_frame signal for corr_i_cnt = 0..3 and corr_i_cnt = 32..35
51d4f6f1 158 // (send two frames with 8 Bits each)
09c66f1f 159 if(corr_i_cnt[5:2] == 4'b0000 || corr_i_cnt[5:2] == 4'b1000)
ba06a4b6 160 ssp_frame = 1'b1;
161 else
162 ssp_frame = 1'b0;
163
164end
165
166assign ssp_din = corr_i_out[7];
167
168assign dbg = corr_i_cnt[3];
169
170// Unused.
171assign pwr_lo = 1'b0;
51d4f6f1 172assign pwr_oe2 = 1'b0;
ba06a4b6 173
174endmodule
Impressum, Datenschutz