text
[RRG-proxmark3.git] / fpga / hi_reader.v
blobc5ffa7b85b98844adbe427aba3af5ed2d936cf25
1 //-----------------------------------------------------------------------------
2 //
3 // Jonathan Westhues, April 2006
4 //-----------------------------------------------------------------------------
6 module hi_reader(
7 ck_1356meg,
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 dbg,
12 subcarrier_frequency, minor_mode
14 input ck_1356meg;
15 output pwr_lo, pwr_hi, pwr_oe1, pwr_oe2, pwr_oe3, pwr_oe4;
16 input [7:0] adc_d;
17 output adc_clk;
18 input ssp_dout;
19 output ssp_frame, ssp_din, ssp_clk;
20 output dbg;
21 input [1:0] subcarrier_frequency;
22 input [3:0] minor_mode;
24 assign adc_clk = ck_1356meg; // sample frequency is 13,56 MHz
26 // When we're a reader, we just need to do the BPSK demod; but when we're an
27 // eavesdropper, we also need to pick out the commands sent by the reader,
28 // using AM. Do this the same way that we do it for the simulated tag.
29 reg after_hysteresis, after_hysteresis_prev, after_hysteresis_prev_prev;
30 reg [11:0] has_been_low_for;
31 always @(negedge adc_clk)
32 begin
33 if (& adc_d[7:0]) after_hysteresis <= 1'b1;
34 else if (~(| adc_d[7:0])) after_hysteresis <= 1'b0;
36 if (after_hysteresis)
37 begin
38 has_been_low_for <= 12'd0;
39 end
40 else
41 begin
42 if (has_been_low_for == 12'd4095)
43 begin
44 has_been_low_for <= 12'd0;
45 after_hysteresis <= 1'b1;
46 end
47 else
48 has_been_low_for <= has_been_low_for + 1;
49 end
50 end
53 // Let us report a correlation every 64 samples. I.e.
54 // one Q/I pair after 4 subcarrier cycles for the 848kHz subcarrier,
55 // one Q/I pair after 2 subcarrier cycles for the 424kHz subcarriers,
56 // one Q/I pair for each subcarrier cyle for the 212kHz subcarrier.
57 // We need a 6-bit counter for the timing.
58 reg [5:0] corr_i_cnt;
59 always @(negedge adc_clk)
60 begin
61 corr_i_cnt <= corr_i_cnt + 1;
62 end
65 // A couple of registers in which to accumulate the correlations. From the 64 samples
66 // we would add at most 32 times the difference between unmodulated and modulated signal. It should
67 // be safe to assume that a tag will not be able to modulate the carrier signal by more than 25%.
68 // 32 * 255 * 0,25 = 2040, which can be held in 11 bits. Add 1 bit for sign.
69 // Temporary we might need more bits. For the 212kHz subcarrier we could possible add 32 times the
70 // maximum signal value before a first subtraction would occur. 32 * 255 = 8160 can be held in 13 bits.
71 // Add one bit for sign -> need 14 bit registers but final result will fit into 12 bits.
72 reg signed [13:0] corr_i_accum;
73 reg signed [13:0] corr_q_accum;
74 // we will report maximum 8 significant bits
75 reg signed [7:0] corr_i_out;
76 reg signed [7:0] corr_q_out;
79 // the amplitude of the subcarrier is sqrt(ci^2 + cq^2).
80 // approximate by amplitude = max(|ci|,|cq|) + 1/2*min(|ci|,|cq|)
81 reg [13:0] corr_amplitude, abs_ci, abs_cq, max_ci_cq;
82 reg [12:0] min_ci_cq_2; // min_ci_cq / 2
84 always @(*)
85 begin
86 if (corr_i_accum[13] == 1'b0)
87 abs_ci <= corr_i_accum;
88 else
89 abs_ci <= -corr_i_accum;
91 if (corr_q_accum[13] == 1'b0)
92 abs_cq <= corr_q_accum;
93 else
94 abs_cq <= -corr_q_accum;
96 if (abs_ci > abs_cq)
97 begin
98 max_ci_cq <= abs_ci;
99 min_ci_cq_2 <= abs_cq / 2;
101 else
102 begin
103 max_ci_cq <= abs_cq;
104 min_ci_cq_2 <= abs_ci / 2;
107 corr_amplitude <= max_ci_cq + min_ci_cq_2;
112 // The subcarrier reference signals
113 reg subcarrier_I;
114 reg subcarrier_Q;
116 always @(*)
117 begin
118 if (subcarrier_frequency == `FPGA_HF_READER_SUBCARRIER_848_KHZ)
119 begin
120 subcarrier_I = ~corr_i_cnt[3];
121 subcarrier_Q = ~(corr_i_cnt[3] ^ corr_i_cnt[2]);
123 else if (subcarrier_frequency == `FPGA_HF_READER_SUBCARRIER_212_KHZ)
124 begin
125 subcarrier_I = ~corr_i_cnt[5];
126 subcarrier_Q = ~(corr_i_cnt[5] ^ corr_i_cnt[4]);
128 else
129 begin // 424 kHz
130 subcarrier_I = ~corr_i_cnt[4];
131 subcarrier_Q = ~(corr_i_cnt[4] ^ corr_i_cnt[3]);
136 // ADC data appears on the rising edge, so sample it on the falling edge
137 always @(negedge adc_clk)
138 begin
139 // These are the correlators: we correlate against in-phase and quadrature
140 // versions of our reference signal, and keep the (signed) results or the
141 // resulting amplitude to send out later over the SSP.
142 if (corr_i_cnt == 6'd0)
143 begin
144 if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_AMPLITUDE)
145 begin
146 // send amplitude plus 2 bits reader signal
147 corr_i_out <= corr_amplitude[13:6];
148 corr_q_out <= {corr_amplitude[5:0], after_hysteresis_prev_prev, after_hysteresis_prev};
150 else if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_IQ)
151 begin
153 // Send 7 most significant bits of in phase tag signal (signed), plus 1 bit reader signal
154 if (corr_i_accum[13:11] == 3'b000 || corr_i_accum[13:11] == 3'b111)
155 corr_i_out <= {corr_i_accum[11:5], after_hysteresis_prev_prev};
156 else // truncate to maximum value
157 if (corr_i_accum[13] == 1'b0)
158 corr_i_out <= {7'b0111111, after_hysteresis_prev_prev};
159 else
160 corr_i_out <= {7'b1000000, after_hysteresis_prev_prev};
162 // Send 7 most significant bits of quadrature phase tag signal (signed), plus 1 bit reader signal
163 if (corr_q_accum[13:11] == 3'b000 || corr_q_accum[13:11] == 3'b111)
164 corr_q_out <= {corr_q_accum[11:5], after_hysteresis_prev};
165 else // truncate to maximum value
166 if (corr_q_accum[13] == 1'b0)
167 corr_q_out <= {7'b0111111, after_hysteresis_prev};
168 else
169 corr_q_out <= {7'b1000000, after_hysteresis_prev};
171 else if (minor_mode == `FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE)
172 begin
173 // send amplitude
174 corr_i_out <= {2'b00, corr_amplitude[13:8]};
175 corr_q_out <= corr_amplitude[7:0];
177 else if (minor_mode == `FPGA_HF_READER_MODE_RECEIVE_IQ)
178 begin
180 // Send 8 bits of in phase tag signal
181 if (corr_i_accum[13:11] == 3'b000 || corr_i_accum[13:11] == 3'b111)
182 corr_i_out <= corr_i_accum[11:4];
183 else // truncate to maximum value
184 if (corr_i_accum[13] == 1'b0)
185 corr_i_out <= 8'b01111111;
186 else
187 corr_i_out <= 8'b10000000;
189 // Send 8 bits of quadrature phase tag signal
190 if (corr_q_accum[13:11] == 3'b000 || corr_q_accum[13:11] == 3'b111)
191 corr_q_out <= corr_q_accum[11:4];
192 else // truncate to maximum value
193 if (corr_q_accum[13] == 1'b0)
194 corr_q_out <= 8'b01111111;
195 else
196 corr_q_out <= 8'b10000000;
199 // for each Q/I pair report two reader signal samples when sniffing. Store the 1st.
200 after_hysteresis_prev_prev <= after_hysteresis;
202 // Initialize next correlation.
203 // Both I and Q reference signals are high when corr_i_nct == 0. Therefore need to accumulate.
204 corr_i_accum <= $signed({1'b0, adc_d});
205 corr_q_accum <= $signed({1'b0, adc_d});
207 else
208 begin
209 if (subcarrier_I)
210 corr_i_accum <= corr_i_accum + $signed({1'b0, adc_d});
211 else
212 corr_i_accum <= corr_i_accum - $signed({1'b0, adc_d});
214 if (subcarrier_Q)
215 corr_q_accum <= corr_q_accum + $signed({1'b0, adc_d});
216 else
217 corr_q_accum <= corr_q_accum - $signed({1'b0, adc_d});
220 // for each Q/I pair report two reader signal samples when sniffing. Store the 2nd.
221 if (corr_i_cnt == 6'd32)
222 after_hysteresis_prev <= after_hysteresis;
224 // Then the result from last time is serialized and send out to the ARM.
225 // We get one report each cycle, and each report is 16 bits, so the
226 // ssp_clk should be the adc_clk divided by 64/16 = 4.
227 // ssp_clk frequency = 13,56MHz / 4 = 3.39MHz
229 if (corr_i_cnt[1:0] == 2'b00)
230 begin
231 // Don't shift if we just loaded new data, obviously.
232 if (corr_i_cnt != 6'd0)
233 begin
234 corr_i_out[7:0] <= {corr_i_out[6:0], corr_q_out[7]};
235 corr_q_out[7:1] <= corr_q_out[6:0];
242 // ssp clock and frame signal for communication to and from ARM
243 // _____ _____ _____ _
244 // ssp_clk | |_____| |_____| |_____|
245 // _____
246 // ssp_frame ___| |____________________________
247 // ___________ ___________ ___________ _
248 // ssp_d_in X___________X___________X___________X_
250 // corr_i_cnt 0 1 2 3 4 5 6 7 8 9 10 11 12 ...
253 reg ssp_clk;
254 reg ssp_frame;
256 always @(negedge adc_clk)
257 begin
258 if (corr_i_cnt[1:0] == 2'b00)
259 ssp_clk <= 1'b1;
261 if (corr_i_cnt[1:0] == 2'b10)
262 ssp_clk <= 1'b0;
264 // set ssp_frame signal for corr_i_cnt = 1..3
265 // (send one frame with 16 Bits)
266 if (corr_i_cnt == 6'd1)
267 ssp_frame <= 1'b1;
269 if (corr_i_cnt == 6'd3)
270 ssp_frame <= 1'b0;
274 assign ssp_din = corr_i_out[7];
277 // a jamming signal
278 reg jam_signal;
279 reg [3:0] jam_counter;
281 always @(negedge adc_clk)
282 begin
283 if (corr_i_cnt == 6'd0)
284 begin
285 jam_counter <= jam_counter + 1;
286 jam_signal <= jam_counter[1] ^ jam_counter[3];
290 // Antenna drivers
291 reg pwr_hi, pwr_oe4;
293 always @(*)
294 begin
295 if (minor_mode == `FPGA_HF_READER_MODE_SEND_SHALLOW_MOD)
296 begin
297 pwr_hi = ck_1356meg;
298 pwr_oe4 = ssp_dout;
300 else if (minor_mode == `FPGA_HF_READER_MODE_SEND_FULL_MOD)
301 begin
302 pwr_hi = ck_1356meg & ~ssp_dout;
303 pwr_oe4 = 1'b0;
305 else if (minor_mode == `FPGA_HF_READER_MODE_SEND_JAM)
306 begin
307 pwr_hi = ck_1356meg & jam_signal;
308 pwr_oe4 = 1'b0;
310 else if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_IQ
311 || minor_mode == `FPGA_HF_READER_MODE_SNIFF_AMPLITUDE
312 || minor_mode == `FPGA_HF_READER_MODE_SNIFF_PHASE)
313 begin // all off
314 pwr_hi = 1'b0;
315 pwr_oe4 = 1'b0;
317 else // receiving from tag
318 begin
319 pwr_hi = ck_1356meg;
320 pwr_oe4 = 1'b0;
324 // always on
325 assign pwr_oe1 = 1'b0;
326 assign pwr_oe3 = 1'b0;
328 // Unused.
329 assign pwr_lo = 1'b0;
330 assign pwr_oe2 = 1'b0;
332 // Debug Output
333 assign dbg = corr_i_cnt[3];
335 endmodule