1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
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.
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 // ISO14443-A support for the Proxmark3
22 input [10:0] edge_detect_threshold
,
23 input [10:0] edge_detect_threshold_high
,
39 assign adc_clk
= ck_1356meg
;
41 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43 // detecting and shaping the reader's signal. Reader will modulate the carrier by 100% (signal is either on or off). Use a
44 // hysteresis (Schmitt Trigger) to avoid false triggers during slowly increasing or decreasing carrier amplitudes
46 reg [11:0] has_been_low_for
;
48 always @(negedge adc_clk
)
50 if(adc_d
>= 16) after_hysteresis
<= 1'b1; // U >= 1,14V -> after_hysteresis = 1
51 else if(adc_d
< 8) after_hysteresis
<= 1'b0; // U < 1,04V -> after_hysteresis = 0
52 // Note: was >= 3,53V and <= 1,19V. The new trigger values allow more reliable detection of the first bit
53 // (it might not reach 3,53V due to the high time constant of the high pass filter in the analogue RF part).
54 // In addition, the new values are more in line with ISO14443-2: "The PICC shall detect the ”End of Pause” after the field exceeds
55 // 5% of H_INITIAL and before it exceeds 60% of H_INITIAL." Depending on the signal strength, 60% might well be less than 3,53V.
58 // detecting a loss of reader's field (adc_d < 192 for 4096 clock cycles). If this is the case,
59 // set the detected reader signal (after_hysteresis) to '1' (unmodulated)
62 has_been_low_for
<= 12'd0;
66 if(has_been_low_for
== 12'd4095)
68 has_been_low_for
<= 12'd0;
69 after_hysteresis
<= 1'b1;
73 has_been_low_for
<= has_been_low_for
+ 1;
79 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81 // detect when a reader is active (modulating). We assume that the reader is active, if we see the carrier off for at least 8
82 // carrier cycles. We assume that the reader is inactive, if the carrier stayed high for at least 256 carrier cycles.
84 reg [2:0] deep_counter
;
85 reg [8:0] saw_deep_modulation
;
87 always @(negedge adc_clk
)
89 if(~(| adc_d
[7:0])) // if adc_d == 0 (U <= 0,94V)
91 if(deep_counter
== 3'd7) // adc_d == 0 for 8 adc_clk ticks -> deep_modulation (by reader)
93 deep_modulation
<= 1'b1;
94 saw_deep_modulation
<= 8'd0;
97 deep_counter
<= deep_counter
+ 1;
101 deep_counter
<= 3'd0;
102 if(saw_deep_modulation
== 8'd255) // adc_d != 0 for 256 adc_clk ticks -> deep_modulation is over, probably waiting for tag's response
103 deep_modulation
<= 1'b0;
105 saw_deep_modulation
<= saw_deep_modulation
+ 1;
109 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111 // filter the input for a tag's signal. The filter box needs the 4 previous input values and is a gaussian derivative filter
112 // for noise reduction and edge detection.
113 // store 4 previous samples:
114 reg [7:0] input_prev_4
, input_prev_3
, input_prev_2
, input_prev_1
;
116 always @(negedge adc_clk
)
118 input_prev_4
<= input_prev_3
;
119 input_prev_3
<= input_prev_2
;
120 input_prev_2
<= input_prev_1
;
121 input_prev_1
<= adc_d
;
124 // adc_d_filtered = 2*input_prev4 + 1*input_prev3 + 0*input_prev2 - 1*input_prev1 - 2*input
125 // = (2*input_prev4 + input_prev3) - (2*input + input_prev1)
126 wire [8:0] input_prev_4_times_2
= input_prev_4
<< 1;
127 wire [8:0] adc_d_times_2
= adc_d
<< 1;
129 wire [9:0] tmp1
= input_prev_4_times_2
+ input_prev_3
;
130 wire [9:0] tmp2
= adc_d_times_2
+ input_prev_1
;
132 // convert intermediate signals to signed and calculate the filter output
133 wire signed
[10:0] adc_d_filtered
= {1'b0, tmp1
} - {1'b0, tmp2
};
135 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136 // internal FPGA timing. Maximum required period is 128 carrier clock cycles for a full 8 Bit transfer to ARM. (i.e. we need a
137 // 7 bit counter). Adjust its frequency to external reader's clock when simulating a tag or sniffing.
138 reg pre_after_hysteresis
;
139 reg [3:0] reader_falling_edge_time
;
140 reg [6:0] negedge_cnt
;
142 always @(negedge adc_clk
)
144 // detect a reader signal's falling edge and remember its timing:
145 pre_after_hysteresis
<= after_hysteresis
;
146 if (pre_after_hysteresis
&& ~after_hysteresis
)
148 reader_falling_edge_time
[3:0] <= negedge_cnt
[3:0];
151 // adjust internal timer counter if necessary:
152 if (negedge_cnt
[3:0] == 4'd13 && (mod_type
== `FPGA_HF_ISO14443A_SNIFFER || mod_type == `FPGA_HF_ISO14443A_TAGSIM_LISTEN) && deep_modulation)
154 if (reader_falling_edge_time
== 4'd1) // reader signal changes right after sampling. Better sample earlier next time.
156 negedge_cnt
<= negedge_cnt
+ 2; // time warp
158 else if (reader_falling_edge_time
== 4'd0) // reader signal changes right before sampling. Better sample later next time.
160 negedge_cnt
<= negedge_cnt
; // freeze time
164 negedge_cnt
<= negedge_cnt
+ 1; // Continue as usual
166 reader_falling_edge_time
[3:0] <= 4'd8; // adjust only once per detected edge
168 else if (negedge_cnt
== 7'd127) // normal operation: count from 0 to 127
174 negedge_cnt
<= negedge_cnt
+ 1;
178 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180 // determine best possible time for starting/resetting the modulation detector.
181 reg [3:0] mod_detect_reset_time
;
183 always @(negedge adc_clk
)
185 if (mod_type
== `FPGA_HF_ISO14443A_READER_LISTEN)
186 // (our) reader signal changes at negedge_cnt[3:0]=9, tag response expected to start n*16+4 ticks later, further delayed by
187 // 3 ticks ADC conversion. The maximum filter output (edge detected) will be detected after subcarrier zero crossing (+7 ticks).
188 // To allow some timing variances, we want to have the maximum filter outputs well within the detection window, i.e.
189 // at mod_detect_reset_time+4 and mod_detect_reset_time+12 (-4 ticks).
190 // 9 + 4 + 3 + 7 - 4 = 19. 19 mod 16 = 3
192 mod_detect_reset_time
<= 4'd4;
195 if (mod_type
== `FPGA_HF_ISO14443A_SNIFFER)
197 // detect a rising edge of reader's signal and sync modulation detector to the tag's answer:
198 if (~pre_after_hysteresis
&& after_hysteresis
&& deep_modulation
)
199 // reader signal rising edge detected at negedge_cnt[3:0]. This signal had been delayed
200 // 9 ticks by the RF part + 3 ticks by the A/D converter + 1 tick to assign to after_hysteresis.
201 // Then the same as above.
202 // - 9 - 3 - 1 + 4 + 3 + 7 - 4 = -3
204 mod_detect_reset_time
<= negedge_cnt
[3:0] - 4'd3;
209 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211 // modulation detector. Looks for the steepest falling and rising edges within a 16 clock period. If there is both a significant
212 // falling and rising edge (in any order), a modulation is detected.
213 reg signed
[10:0] rx_mod_falling_edge_max
;
214 reg signed
[10:0] rx_mod_rising_edge_max
;
217 always @(negedge adc_clk
)
219 if(negedge_cnt
[3:0] == mod_detect_reset_time
)
221 if (mod_type
== `FPGA_HF_ISO14443A_SNIFFER)
223 // detect modulation signal: if modulating, there must have been a falling AND a rising edge
224 if ((rx_mod_falling_edge_max
> edge_detect_threshold_high
) && (rx_mod_rising_edge_max
< -edge_detect_threshold_high
))
225 curbit
<= 1'b1; // modulation
227 curbit
<= 1'b0; // no modulation
231 // detect modulation signal: if modulating, there must have been a falling AND a rising edge
232 if ((rx_mod_falling_edge_max
> edge_detect_threshold
) && (rx_mod_rising_edge_max
< -edge_detect_threshold
))
233 curbit
<= 1'b1; // modulation
235 curbit
<= 1'b0; // no modulation
237 // reset modulation detector
238 rx_mod_rising_edge_max
<= 0;
239 rx_mod_falling_edge_max
<= 0;
241 else // look for steepest edges (slopes)
243 if (adc_d_filtered
> 0)
245 if (adc_d_filtered
> rx_mod_falling_edge_max
)
246 rx_mod_falling_edge_max
<= adc_d_filtered
;
250 if (adc_d_filtered
< rx_mod_rising_edge_max
)
251 rx_mod_rising_edge_max
<= adc_d_filtered
;
257 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259 // sample 4 bits reader data and 4 bits tag data for sniffing
260 reg [3:0] reader_data
;
263 always @(negedge adc_clk
)
265 if(negedge_cnt
[3:0] == 4'd0)
267 reader_data
[3:0] <= {reader_data
[2:0], after_hysteresis
};
268 tag_data
[3:0] <= {tag_data
[2:0], curbit
};
272 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
274 // a delay line to ensure that we send the (emulated) tag's answer at the correct time according to ISO14443-3
275 reg [31:0] mod_sig_buf
;
276 reg [4:0] mod_sig_ptr
;
279 always @(negedge adc_clk
)
281 if(negedge_cnt
[3:0] == 4'd0) // sample data at rising edge of ssp_clk - ssp_dout changes at the falling edge.
283 mod_sig_buf
[31:2] <= mod_sig_buf
[30:1]; // shift
284 if (~ssp_dout
&& ~mod_sig_buf
[1])
285 mod_sig_buf
[1] <= 1'b0; // delete the correction bit (a single 1 preceded and succeeded by 0)
287 mod_sig_buf
[1] <= mod_sig_buf
[0];
288 mod_sig_buf
[0] <= ssp_dout
; // add new data to the delay line
290 mod_sig
= mod_sig_buf
[mod_sig_ptr
]; // the delayed signal.
294 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
295 // PM3 -> Reader, internal timing:
296 // a timer for the 1172 cycles fdt (Frame Delay Time). Start the timer with a rising edge of the reader's signal.
297 // set fdt_elapsed when we no longer need to delay data. Set fdt_indicator when we can start sending data.
298 // Note: the FPGA only takes care for the 1172 delay. To achieve an additional 1236-1172=64 ticks delay, the ARM must send
299 // a correction bit (before the start bit). The correction bit will be coded as 00010000, i.e. it adds 4 bits to the
300 // transmission stream, causing the required additional delay.
301 reg [10:0] fdt_counter
;
302 reg fdt_indicator
, fdt_elapsed
;
303 reg [3:0] mod_sig_flip
;
304 reg [3:0] sub_carrier_cnt
;
306 // we want to achieve a delay of 1172. The RF part already has delayed the reader signals's rising edge
307 // by 9 ticks, the ADC took 3 ticks and there is always a delay of 32 ticks by the mod_sig_buf. Therefore need to
308 // count to 1172 - 9 - 3 - 32 = 1128
309 `define FDT_COUNT 11'd1128
311 // The ARM must not send too early, otherwise the mod_sig_buf will overflow, therefore signal that we are ready
312 // with fdt_indicator. The mod_sig_buf can buffer 29 excess data bits, i.e. a maximum delay of 29 * 16 = 464 adc_clk ticks.
313 // fdt_indicator is assigned to sendbit after at least 1 tick, the transfer to ARM needs minimum 8 ticks. Response from
314 // ARM could appear at ssp_dout 8 ticks later.
315 // 1128 - 464 - 1 - 8 - 8 = 647
316 `define FDT_INDICATOR_COUNT 11'd647
317 // Note: worst case, assignment to sendbit takes 15 ticks more, and transfer to ARM needs 7*16 = 112 ticks more.
318 // When the ARM's response then appears, the fdt_count is already 647 + 15 + 112 = 774, which still allows the ARM a possible
319 // response window of 1128 - 774 = 354 ticks.
321 // reset on a pause in listen mode. I.e. the counter starts when the pause is over:
322 assign fdt_reset
= ~after_hysteresis
&& mod_type
== `FPGA_HF_ISO14443A_TAGSIM_LISTEN;
324 always @(negedge adc_clk
)
328 fdt_counter
<= 11'd0;
330 fdt_indicator
<= 1'b0;
334 if(fdt_counter
== `FDT_COUNT)
336 if(~fdt_elapsed
) // just reached fdt.
338 mod_sig_flip
<= negedge_cnt
[3:0]; // start modulation at this time
339 sub_carrier_cnt
<= 4'd0; // subcarrier phase in sync with start of modulation
344 sub_carrier_cnt
<= sub_carrier_cnt
+ 1;
349 fdt_counter
<= fdt_counter
+ 1;
353 if(fdt_counter
== `FDT_INDICATOR_COUNT) fdt_indicator <= 1'b1;
356 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
357 // PM3 -> Reader or Tag
358 // assign a modulation signal to the antenna. This signal is either a delayed signal (to achieve fdt when sending to a reader)
359 // or undelayed when sending to a tag
362 always @(negedge adc_clk
)
364 if (mod_type
== `FPGA_HF_ISO14443A_TAGSIM_MOD) // need to take care of proper fdt timing
366 if(fdt_counter
== `FDT_COUNT)
370 if(negedge_cnt
[3:0] == mod_sig_flip
) mod_sig_coil
<= mod_sig
;
374 mod_sig_coil
<= mod_sig
; // just reached fdt. Immediately assign signal to coil
378 else // other modes: don't delay
380 mod_sig_coil
<= ssp_dout
;
384 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
386 // determine the required delay in the mod_sig_buf (set mod_sig_ptr).
387 reg temp_buffer_reset
;
389 always @(negedge adc_clk
)
394 temp_buffer_reset
= 1'b0;
398 if(fdt_counter
== `FDT_COUNT && ~fdt_elapsed) // if we just reached fdt
399 if(~(| mod_sig_ptr
[4:0]))
400 mod_sig_ptr
<= 5'd8; // ... but didn't buffer a 1 yet, delay next 1 by n*128 ticks.
402 temp_buffer_reset
= 1'b1; // else no need for further delays.
404 if(negedge_cnt
[3:0] == 4'd0) // at rising edge of ssp_clk - ssp_dout changes at the falling edge.
406 if((ssp_dout ||
(| mod_sig_ptr
[4:0])) && ~fdt_elapsed
) // buffer a 1 (and all subsequent data) until fdt is reached.
407 if (mod_sig_ptr
== 5'd31)
408 mod_sig_ptr
<= 5'd0; // buffer overflow - data loss.
410 mod_sig_ptr
<= mod_sig_ptr
+ 1; // increase buffer (= increase delay by 16 adc_clk ticks). mod_sig_ptr always points ahead of first 1.
411 else if(fdt_elapsed
&& ~temp_buffer_reset
)
413 // wait for the next 1 after fdt_elapsed before fixing the delay and starting modulation. This ensures that the response can only happen
414 // at intervals of 8 * 16 = 128 adc_clk ticks (as defined in ISO14443-3)
416 temp_buffer_reset
= 1'b1;
417 if(mod_sig_ptr
== 5'd1)
418 mod_sig_ptr
<= 5'd8; // still nothing received, need to go for the next interval
420 mod_sig_ptr
<= mod_sig_ptr
- 1; // decrease buffer.
426 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
427 // FPGA -> ARM communication:
428 // buffer 8 bits data to be sent to ARM. Shift them out bit by bit.
431 always @(negedge adc_clk
)
433 if (negedge_cnt
[5:0] == 6'd63) // fill the buffer
435 if (mod_type
== `FPGA_HF_ISO14443A_SNIFFER)
437 if(deep_modulation
) // a reader is sending (or there's no field at all)
439 to_arm
<= {reader_data
[3:0], 4'b0000}; // don't send tag data
443 to_arm
<= {reader_data
[3:0], tag_data
[3:0]};
448 to_arm
[7:0] <= {mod_sig_ptr
[4:0], mod_sig_flip
[3:1]}; // feedback timing information
452 if(negedge_cnt
[2:0] == 3'b000 && mod_type
== `FPGA_HF_ISO14443A_SNIFFER) // shift at double speed
454 // Don't shift if we just loaded new data, obviously.
455 if(negedge_cnt
[5:0] != 6'd0)
457 to_arm
[7:1] <= to_arm
[6:0];
461 if(negedge_cnt
[3:0] == 4'b0000 && mod_type
!= `FPGA_HF_ISO14443A_SNIFFER)
463 // Don't shift if we just loaded new data, obviously.
464 if(negedge_cnt
[6:0] != 7'd0)
466 to_arm
[7:1] <= to_arm
[6:0];
471 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
472 // FPGA <-> ARM communication:
473 // generate a ssp clock and ssp frame signal for the synchronous transfer from/to the ARM
475 always @(negedge adc_clk
)
477 if(mod_type
== `FPGA_HF_ISO14443A_SNIFFER)
478 // FPGA_HF_ISO14443A_SNIFFER mode (ssp_clk = adc_clk / 8, ssp_frame clock = adc_clk / 64)):
480 if(negedge_cnt
[2:0] == 3'd0)
482 if(negedge_cnt
[2:0] == 3'd4)
485 if(negedge_cnt
[5:0] == 6'd0) // ssp_frame rising edge indicates start of frame
487 if(negedge_cnt
[5:0] == 6'd8)
491 // all other modes (ssp_clk = adc_clk / 16, ssp_frame clock = adc_clk / 128):
493 if(negedge_cnt
[3:0] == 4'd0)
495 if(negedge_cnt
[3:0] == 4'd8)
498 if(negedge_cnt
[6:0] == 7'd7) // ssp_frame rising edge indicates start of frame, sampled on falling edge of ssp_clk
500 if(negedge_cnt
[6:0] == 7'd23)
505 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
506 // FPGA -> ARM communication:
507 // select the data to be sent to ARM
511 always @(negedge adc_clk
)
513 if(negedge_cnt
[3:0] == 4'd0)
515 // What do we communicate to the ARM
516 if(mod_type
== `FPGA_HF_ISO14443A_TAGSIM_LISTEN)
517 sendbit
= after_hysteresis
;
518 else if(mod_type
== `FPGA_HF_ISO14443A_TAGSIM_MOD)
519 /* if(fdt_counter > 11'd772) sendbit = mod_sig_coil; // huh?
521 sendbit
= fdt_indicator
;
522 else if (mod_type
== `FPGA_HF_ISO14443A_READER_LISTEN)
528 if(mod_type
== `FPGA_HF_ISO14443A_SNIFFER)
529 // send sampled reader and tag data:
530 bit_to_arm
= to_arm
[7];
531 else if (mod_type
== `FPGA_HF_ISO14443A_TAGSIM_MOD && fdt_elapsed && temp_buffer_reset)
532 // send timing information:
533 bit_to_arm
= to_arm
[7];
535 // send data or fdt_indicator
536 bit_to_arm
= sendbit
;
539 assign ssp_din
= bit_to_arm
;
541 // Subcarrier (adc_clk/16, for FPGA_HF_ISO14443A_TAGSIM_MOD only).
543 assign sub_carrier
= ~sub_carrier_cnt
[3];
545 // in FPGA_HF_ISO14443A_READER_MOD: drop carrier for mod_sig_coil == 1 (pause);
546 // in FPGA_HF_ISO14443A_READER_LISTEN: carrier always on; in other modes: carrier always off
547 assign pwr_hi
= (ck_1356meg
& (((mod_type
== `FPGA_HF_ISO14443A_READER_MOD) & ~mod_sig_coil) || (mod_type == `FPGA_HF_ISO14443A_READER_LISTEN)));
549 // Enable HF antenna drivers:
550 assign pwr_oe1
= 1'b0;
551 assign pwr_oe3
= 1'b0;
553 // FPGA_HF_ISO14443A_TAGSIM_MOD: short circuit antenna with different resistances (modulated by sub_carrier modulated by mod_sig_coil)
554 // for pwr_oe4 = 1 (tristate): antenna load = 10k || 33 = 32,9 Ohms
555 // for pwr_oe4 = 0 (active): antenna load = 10k || 33 || 33 = 16,5 Ohms
556 assign pwr_oe4
= mod_sig_coil
& sub_carrier
& (mod_type
== `FPGA_HF_ISO14443A_TAGSIM_MOD);
558 // This is all LF, so doesn't matter.
559 assign pwr_oe2
= 1'b0;
560 assign pwr_lo
= 1'b0;
562 assign debug
= negedge_cnt
[3];