spaces
[RRG-proxmark3.git] / fpga / hi_reader.v
blob07afdc2debe3b56b6fa0c73bb2ddd6922794cf89
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // with optional support for iso15 2sc mode slected with compiler define WITH_HF_15
18 module hi_reader(
19 input ck_1356meg,
20 input [7:0] adc_d,
21 input [1:0] subcarrier_frequency,
22 input [3:0] minor_mode,
23 input ssp_dout,
25 output ssp_din,
26 output reg ssp_frame,
27 output reg ssp_clk,
28 output adc_clk,
29 output pwr_lo,
30 output reg pwr_hi,
31 output reg pwr_oe1,
32 output pwr_oe2,
33 output pwr_oe3,
34 output reg pwr_oe4,
35 output debug
38 assign adc_clk = ck_1356meg; // sample frequency is 13,56 MHz
40 // When we're a reader, we just need to do the BPSK demod; but when we're an
41 // eavesdropper, we also need to pick out the commands sent by the reader,
42 // using AM. Do this the same way that we do it for the simulated tag.
43 reg after_hysteresis, after_hysteresis_prev, after_hysteresis_prev_prev;
44 reg [11:0] has_been_low_for;
45 always @(negedge adc_clk)
46 begin
47 `ifdef WITH_HF_15_LOWSIGNAL
48 if (& adc_d[7:4]) after_hysteresis <= 1'b1;
49 else if (~(| adc_d[7:6])) after_hysteresis <= 1'b0;
50 `else
51 if (& adc_d[7:0]) after_hysteresis <= 1'b1;
52 else if (~(| adc_d[7:0])) after_hysteresis <= 1'b0;
53 `endif
55 if (after_hysteresis)
56 begin
57 has_been_low_for <= 12'd0;
58 end
59 else
60 begin
61 if (has_been_low_for == 12'd4095)
62 begin
63 has_been_low_for <= 12'd0;
64 after_hysteresis <= 1'b1;
65 end
66 else
67 has_been_low_for <= has_been_low_for + 1;
68 end
69 end
71 // Let us report a correlation every 64 samples. I.e.
72 // one Q/I pair after 4 subcarrier cycles for the 848kHz subcarrier,
73 // one Q/I pair after 2 subcarrier cycles for the 424kHz subcarriers,
74 // one Q/I pair for each subcarrier cyle for the 212kHz subcarrier.
75 // We need a 6-bit counter for the timing.
76 reg [5:0] corr_i_cnt;
77 always @(negedge adc_clk)
78 corr_i_cnt <= corr_i_cnt + 1;
80 `ifdef WITH_HF_15
81 reg [1:0] fskout = 2'd0;
82 reg last0 = 1'b0;
84 reg [7:0] avg = 8'd0;
85 reg [127:0] avg128 = 128'd0;
86 reg [7:0] diff16 = 8'd0;
87 reg [7:0] diff28 = 8'd0;
88 reg [7:0] diff32 = 8'd0;
90 reg [11:0] match16 = 12'd0;
91 reg [11:0] match32 = 12'd0;
92 reg [11:0] match28 = 12'd0;
94 always @(negedge adc_clk)
95 begin
96 if (corr_i_cnt[0] == 1'b0) // every 2 clock
97 avg = adc_d[7:1];
98 else
99 begin
100 avg = avg + adc_d[7:1];
101 if (corr_i_cnt[0] == 1'b1) // every 2 clock
102 begin
103 if (avg > avg128[63:56])
104 diff16 = avg - avg128[63:56];
105 else
106 diff16 = avg128[63:56] - avg;
108 if (avg > avg128[111:104])
109 diff28 = avg - avg128[111:104];
110 else
111 diff28 = avg128[111:104] - avg;
113 if (avg > avg128[127:120])
114 diff32 = avg - avg128[127:120];
115 else
116 diff32 = avg128[127:120] - avg;
118 avg128[127:8] = avg128[119:0];
119 avg128[7:0] = avg;
121 if (corr_i_cnt[4:1] == 4'b0000) // every 32 clock (8*4)
122 begin
123 match16 = diff16;
124 match28 = diff28;
125 match32 = diff32;
127 else
128 begin
129 match16 = match16 + diff16;
130 match28 = match28 + diff28;
131 match32 = match32 + diff32;
133 if (corr_i_cnt[4:1] == 4'b1111) // every 32 clock (8*4)
134 begin
135 last0 = (fskout == 2'b0);
136 if (match16 < 12'd64 && last0)
137 fskout = 2'b00; // not yet started
138 else if ((match16 | match28 | match32) == 12'b0)
139 fskout = 2'b00; // signal likely ended
140 else if (((match16 <= match28 + 12'd16) && (match16 <= match32+ 12'd16)) ||
141 (match28 <= 12'd16 && match32 <= 12'd16))
142 begin
143 if (!last0)
144 fskout = 2'b11; // 16 match better than 28 or 32 but already started
146 else
147 begin
148 if (match28 < match32)
149 begin
150 diff28 = match32 - match28;
151 diff16 = match16 - match28;
152 if (diff28*2 > diff16)
153 fskout = 2'b01;
154 else if (!last0)
155 begin
156 fskout = 2'b01;
159 else //if (match32 <= match28)
160 begin
161 diff32 = match28 - match32;
162 diff16 = match16 - match32;
163 if (diff32*2 > diff16)
164 fskout = 2'b10;
165 else if (!last0)
166 begin
167 fskout = 2'b10;
176 `endif
178 // A couple of registers in which to accumulate the correlations. From the 64 samples
179 // we would add at most 32 times the difference between unmodulated and modulated signal. It should
180 // be safe to assume that a tag will not be able to modulate the carrier signal by more than 25%.
181 // 32 * 255 * 0,25 = 2040, which can be held in 11 bits. Add 1 bit for sign.
182 // Temporary we might need more bits. For the 212kHz subcarrier we could possible add 32 times the
183 // maximum signal value before a first subtraction would occur. 32 * 255 = 8160 can be held in 13 bits.
184 // Add one bit for sign -> need 14 bit registers but final result will fit into 12 bits.
185 reg signed [13:0] corr_i_accum;
186 reg signed [13:0] corr_q_accum;
187 // we will report maximum 8 significant bits
188 reg signed [7:0] corr_i_out;
189 reg signed [7:0] corr_q_out;
191 // the amplitude of the subcarrier is sqrt(ci^2 + cq^2).
192 // approximate by amplitude = max(|ci|,|cq|) + 1/2*min(|ci|,|cq|)
193 reg [13:0] corr_amplitude, abs_ci, abs_cq, max_ci_cq;
194 reg [12:0] min_ci_cq_2; // min_ci_cq / 2
196 always @(*)
197 begin
198 if (corr_i_accum[13] == 1'b0)
199 abs_ci <= corr_i_accum;
200 else
201 abs_ci <= -corr_i_accum;
203 if (corr_q_accum[13] == 1'b0)
204 abs_cq <= corr_q_accum;
205 else
206 abs_cq <= -corr_q_accum;
208 if (abs_ci > abs_cq)
209 begin
210 max_ci_cq <= abs_ci;
211 min_ci_cq_2 <= abs_cq / 2;
213 else
214 begin
215 max_ci_cq <= abs_cq;
216 min_ci_cq_2 <= abs_ci / 2;
219 corr_amplitude <= max_ci_cq + min_ci_cq_2;
223 // The subcarrier reference signals
224 reg subcarrier_I;
225 reg subcarrier_Q;
227 always @(*)
228 begin
229 if (subcarrier_frequency == `FPGA_HF_READER_SUBCARRIER_848_KHZ)
230 begin
231 subcarrier_I = ~corr_i_cnt[3];
232 subcarrier_Q = ~(corr_i_cnt[3] ^ corr_i_cnt[2]);
234 else if (subcarrier_frequency == `FPGA_HF_READER_SUBCARRIER_212_KHZ)
235 begin
236 subcarrier_I = ~corr_i_cnt[5];
237 subcarrier_Q = ~(corr_i_cnt[5] ^ corr_i_cnt[4]);
239 else
240 begin // 424 kHz
241 subcarrier_I = ~corr_i_cnt[4];
242 subcarrier_Q = ~(corr_i_cnt[4] ^ corr_i_cnt[3]);
246 // ADC data appears on the rising edge, so sample it on the falling edge
247 always @(negedge adc_clk)
248 begin
249 // These are the correlators: we correlate against in-phase and quadrature
250 // versions of our reference signal, and keep the (signed) results or the
251 // resulting amplitude to send out later over the SSP.
252 if (corr_i_cnt == 6'd0)
253 begin
254 if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_AMPLITUDE)
255 begin
256 `ifdef WITH_HF_15
257 if (subcarrier_frequency == `FPGA_HF_READER_2SUBCARRIERS_424_484_KHZ)
258 begin
259 // send amplitude + 2 bits fsk (2sc) signal + 2 bits reader signal
260 corr_i_out <= corr_amplitude[13:6];
261 corr_q_out <= {corr_amplitude[5:2], fskout, after_hysteresis_prev_prev, after_hysteresis_prev};
263 else
264 `endif
265 begin
266 // send amplitude plus 2 bits reader signal
267 corr_i_out <= corr_amplitude[13:6];
268 corr_q_out <= {corr_amplitude[5:0], after_hysteresis_prev_prev, after_hysteresis_prev};
271 else if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_IQ)
272 begin
273 // Send 7 most significant bits of in phase tag signal (signed), plus 1 bit reader signal
274 if (corr_i_accum[13:11] == 3'b000 || corr_i_accum[13:11] == 3'b111)
275 corr_i_out <= {corr_i_accum[11:5], after_hysteresis_prev_prev};
276 else // truncate to maximum value
277 if (corr_i_accum[13] == 1'b0)
278 corr_i_out <= {7'b0111111, after_hysteresis_prev_prev};
279 else
280 corr_i_out <= {7'b1000000, after_hysteresis_prev_prev};
282 // Send 7 most significant bits of quadrature phase tag signal (signed), plus 1 bit reader signal
283 if (corr_q_accum[13:11] == 3'b000 || corr_q_accum[13:11] == 3'b111)
284 corr_q_out <= {corr_q_accum[11:5], after_hysteresis_prev};
285 else // truncate to maximum value
286 if (corr_q_accum[13] == 1'b0)
287 corr_q_out <= {7'b0111111, after_hysteresis_prev};
288 else
289 corr_q_out <= {7'b1000000, after_hysteresis_prev};
291 else if (minor_mode == `FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE)
292 begin
293 `ifdef WITH_HF_15
294 if (subcarrier_frequency == `FPGA_HF_READER_2SUBCARRIERS_424_484_KHZ)
295 begin
296 // send 2 bits fsk (2sc) signal + amplitude
297 corr_i_out <= {fskout, corr_amplitude[13:8]};
298 corr_q_out <= corr_amplitude[7:0];
300 else
301 `endif
302 begin
303 // send amplitude
304 corr_i_out <= {2'b00, corr_amplitude[13:8]};
305 corr_q_out <= corr_amplitude[7:0];
308 else if (minor_mode == `FPGA_HF_READER_MODE_RECEIVE_IQ)
309 begin
310 // Send 8 bits of in phase tag signal
311 if (corr_i_accum[13:11] == 3'b000 || corr_i_accum[13:11] == 3'b111)
312 corr_i_out <= corr_i_accum[11:4];
313 else // truncate to maximum value
314 if (corr_i_accum[13] == 1'b0)
315 corr_i_out <= 8'b01111111;
316 else
317 corr_i_out <= 8'b10000000;
319 // Send 8 bits of quadrature phase tag signal
320 if (corr_q_accum[13:11] == 3'b000 || corr_q_accum[13:11] == 3'b111)
321 corr_q_out <= corr_q_accum[11:4];
322 else // truncate to maximum value
323 if (corr_q_accum[13] == 1'b0)
324 corr_q_out <= 8'b01111111;
325 else
326 corr_q_out <= 8'b10000000;
329 // for each Q/I pair report two reader signal samples when sniffing. Store the 1st.
330 after_hysteresis_prev_prev <= after_hysteresis;
332 // Initialize next correlation.
333 // Both I and Q reference signals are high when corr_i_nct == 0. Therefore need to accumulate.
334 corr_i_accum <= $signed({1'b0, adc_d});
335 corr_q_accum <= $signed({1'b0, adc_d});
337 else
338 begin
339 if (subcarrier_I)
340 corr_i_accum <= corr_i_accum + $signed({1'b0, adc_d});
341 else
342 corr_i_accum <= corr_i_accum - $signed({1'b0, adc_d});
344 if (subcarrier_Q)
345 corr_q_accum <= corr_q_accum + $signed({1'b0, adc_d});
346 else
347 corr_q_accum <= corr_q_accum - $signed({1'b0, adc_d});
350 // for each Q/I pair report two reader signal samples when sniffing. Store the 2nd.
351 if (corr_i_cnt == 6'd32)
352 after_hysteresis_prev <= after_hysteresis;
354 // Then the result from last time is serialized and send out to the ARM.
355 // We get one report each cycle, and each report is 16 bits, so the
356 // ssp_clk should be the adc_clk divided by 64/16 = 4.
357 // ssp_clk frequency = 13,56MHz / 4 = 3.39MHz
359 if (corr_i_cnt[1:0] == 2'b00)
360 begin
361 // Don't shift if we just loaded new data, obviously.
362 if (corr_i_cnt != 6'd0)
363 begin
364 corr_i_out[7:0] <= {corr_i_out[6:0], corr_q_out[7]};
365 corr_q_out[7:1] <= corr_q_out[6:0];
371 // ssp clock and frame signal for communication to and from ARM
372 // _____ _____ _____ _
373 // ssp_clk | |_____| |_____| |_____|
374 // _____
375 // ssp_frame ___| |____________________________
376 // ___________ ___________ ___________ _
377 // ssp_d_in X___________X___________X___________X_
379 // corr_i_cnt 0 1 2 3 4 5 6 7 8 9 10 11 12 ...
381 always @(negedge adc_clk)
382 begin
383 if (corr_i_cnt[1:0] == 2'b00)
384 ssp_clk <= 1'b1;
386 if (corr_i_cnt[1:0] == 2'b10)
387 ssp_clk <= 1'b0;
389 // set ssp_frame signal for corr_i_cnt = 1..3
390 // (send one frame with 16 Bits)
391 if (corr_i_cnt == 6'd1)
392 ssp_frame <= 1'b1;
394 if (corr_i_cnt == 6'd3)
395 ssp_frame <= 1'b0;
398 assign ssp_din = corr_i_out[7];
400 // a jamming signal
401 reg jam_signal;
402 reg [3:0] jam_counter;
404 always @(negedge adc_clk)
405 begin
406 if (corr_i_cnt == 6'd0)
407 begin
408 jam_counter <= jam_counter + 1;
409 jam_signal <= jam_counter[1] ^ jam_counter[3];
413 always @(*)
414 begin
415 pwr_oe1 = 1'b0;
416 pwr_oe4 = 1'b0;
418 if (minor_mode == `FPGA_HF_READER_MODE_SEND_SHALLOW_MOD)
419 begin
420 pwr_hi = ck_1356meg;
421 pwr_oe4 = ssp_dout;
423 else if (minor_mode == `FPGA_HF_READER_MODE_SEND_SHALLOW_MOD_RDV4)
424 begin
425 pwr_hi = ck_1356meg;
426 pwr_oe1 = ssp_dout;
428 else if (minor_mode == `FPGA_HF_READER_MODE_SEND_FULL_MOD)
429 begin
430 pwr_hi = ck_1356meg & ~ssp_dout;
431 pwr_oe4 = 1'b0;
433 else if (minor_mode == `FPGA_HF_READER_MODE_SEND_JAM)
434 begin
435 pwr_hi = ck_1356meg & jam_signal;
436 pwr_oe4 = 1'b0;
438 else if (minor_mode == `FPGA_HF_READER_MODE_SNIFF_IQ
439 || minor_mode == `FPGA_HF_READER_MODE_SNIFF_AMPLITUDE
440 || minor_mode == `FPGA_HF_READER_MODE_SNIFF_PHASE)
441 begin // all off
442 pwr_hi = 1'b0;
443 pwr_oe4 = 1'b0;
445 else // receiving from tag
446 begin
447 pwr_hi = ck_1356meg;
448 pwr_oe4 = 1'b0;
452 // unused
453 assign pwr_oe2 = 1'b0;
454 assign pwr_oe3 = 1'b0;
455 assign pwr_lo = 1'b0;
457 // Debug Output
458 assign debug = corr_i_cnt[3];
460 endmodule