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 //-----------------------------------------------------------------------------
18 This code demodulates and modulates signal as described in ISO/IEC 18092.
19 That includes packets used for Felica, NFC Tag 3, etc. (which do overlap)
20 simple envelope following algorithm is used (modification of fail0verflow LF one)
21 is used to combat some nasty aliasing effect with testing phone (envelope looked like sine wave)
23 Speeds supported: only 212 kbps (fc/64) for now. Todo: 414 kbps
24 though for reader, the selection has to come from ARM. modulation waits for market sprocket -doesn't really mean anything
27 bit 2 : reader drive/power on/off
28 bit 1 : speed bit, 0 : 212, 1 :424
29 bit 0 : listen or modulate
53 wire power
= mod_type
[2];
54 wire speed
= mod_type
[1];
55 wire disabl
= mod_type
[0];
57 // 512x64/fc -wait before ts0, 32768 ticks
59 assign adc_clk
= ck_1356meg
;
61 ///heuristic values for initial thresholds. seem to work OK
62 `define imin 70 // (13'd256)
63 `define imax 180 // (-13'd256)
64 `define ithrmin 91 // -13'd8
65 `define ithrmax 160 // 13'd8
67 `define min_bitdelay_212 8
68 //minimum values and corresponding thresholds
69 reg [8:0] curmin
=`imin;
70 reg [8:0] curminthres
=`ithrmin;
71 reg [8:0] curmaxthres
=`ithrmax;
72 reg [8:0] curmax
=`imax;
74 //signal state, 1-not modulated, 0 -modulated
75 reg after_hysteresis
= 1'b1;
77 //state machine for envelope tracking
78 reg [1:0] state
= 1'd0;
80 //lower edge detected, trying to detect first bit of SYNC (b24d, 1011001001001101)
83 //detected first sync bit, phase frozen
86 `define bithalf_212 32 // half-bit length for 212 kbit
87 `define bitmlen_212 63 // bit transition edge
89 `define bithalf_424 16 // half-bit length for 212 kbit
90 `define bitmlen_424 31 // bit transition edge
92 wire [7:0] bithalf
= speed ?
`bithalf_424 : `bithalf_212;
93 wire [7:0] bitmlen
= speed ?
`bitmlen_424 : `bitmlen_212;
97 reg [7:0] fccount
= 8'd0; // in-bit tick counter. Counts carrier cycles from the first lower edge detected, reset on every manchester bit detected
99 reg [7:0] tsinceedge
= 8'd0;// ticks from last edge, desync if the valye is too large
101 reg zero
= 1'b0; // Manchester first halfbit low second high corresponds to this value. It has been known to change. SYNC is used to set it
103 //ssp clock and current values
104 //ssp counter for transfer and framing
105 reg [8:0] ssp_cnt
= 9'd0;
107 always @(posedge adc_clk
)
108 ssp_cnt
<= (ssp_cnt
+ 1);
110 //maybe change it so that ARM sends preamble as well.
111 //then: ready bits sent to ARM, 8 bits sent from ARM (all ones), then preamble (all zeros, presumably) - which starts modulation
113 always @(negedge adc_clk
)
115 //count fc/64 - transfer bits to ARM at the rate they are received
116 if( ((~speed
) && (ssp_cnt
[5:0] == 6'b000000) ) ||
(speed
&& (ssp_cnt
[4:0] == 5'b00000)) )
119 //send current bit (detected in SNIFF mode or the one being modulated in MOD mode, 0 otherwise)
122 if( ( (~speed
) && (ssp_cnt
[5:0] == 6'b100000)) ||
(speed
&& ssp_cnt
[4:0] == 5'b10000))
124 //create frame pulses. TBH, I still don't know what they do exactly, but they are crucial for ARM->FPGA transfer. If the frame is in the beginning of the byte, transfer slows to a crawl for some reason
125 // took me a day to figure THAT out.
126 if(( (~speed
) && (ssp_cnt
[8:0] == 9'd31)) ||
(speed
&& ssp_cnt
[7:0] == 8'd15))
130 if(( (~speed
) && (ssp_cnt
[8:0] == 9'b1011111)) ||
(speed
&&ssp_cnt
[7:0] == 8'b101111) )
136 //previous signal value, mostly to detect SYNC
139 // for simple error correction in mod/demod detection, use maximum of modded/demodded in given interval. Maybe 1 bit is extra? but better safe than sorry.
140 reg[7:0] mid
= 8'd128;
142 // set TAGSIM__MODULATE on ARM if we want to write... (frame would get lost if done mid-frame...)
143 // start sending over 1s on ssp->arm when we start sending preamble
144 // reg sending = 1'b0; // are we actively modulating?
145 reg [11:0] bit_counts
= 12'd0; // for timeslots. only support ts=0 for now, at 212 speed -512 fullbits from end of frame. One hopes. might remove those?
147 //we need some way to flush bit_counts triggers on mod_type changes don't compile
149 always @(negedge adc_clk
) // every data ping?
151 //envelope follow code...
153 if (fccount
== bitmlen
)
155 if ((~try_sync
) && (adc_d
< curminthres
) && disabl
)
164 if (bit_counts
> 768) // should be over ts0 now, without ARM interference... stop counting...
172 bit_counts
<= bit_counts
+ 1;
176 if((~try_sync
) && (adc_d
< curminthres
) && disabl
)
182 fccount
<= fccount
+ 1;
187 if (adc_d
> curmaxthres
)
191 curmax
<= adc_d
> `imax? adc_d : `imax;
195 curminthres
<= ((curmin
>> 1) + (curmin
>> 2) + (curmin
>> 4) + (curmax
>> 3) + (curmax
>> 4)); //threshold: 0.1875 max + 0.8125 min
196 curmaxthres
<= ((curmax
>> 1) + (curmax
>> 2) + (curmax
>> 4) + (curmin
>> 3) + (curmin
>> 4));
197 curmax
<= adc_d
> 155 ? adc_d
: 155; // to hopefully prevent overflow from spikes going up to 255
208 after_hysteresis
<= 1'b1;
212 else if (adc_d
<curminthres
) //falling edge
216 curmin
<= adc_d
<`imin? adc_d :`imin;
224 curminthres
<= ( (curmin
>> 1) + (curmin
>> 2) + (curmin
>> 4) + (curmax
>> 3) + (curmax
>> 4));
225 curmaxthres
<= ( (curmax
>> 1) + (curmax
>> 2) + (curmax
>> 4) + (curmin
>> 3) + (curmin
>> 4));
226 curmin
<= adc_d
< `imin ? adc_d : `imin;
233 after_hysteresis
<= 0;
234 if (~try_sync
) //begin modulation, lower edge...
249 else //stable state, low or high
251 curminthres
<= ( (curmin
>> 1) + (curmin
>> 2) + (curmin
>> 4) + (curmax
>> 3) + (curmax
>> 4));
252 curmaxthres
<= ( (curmax
>> 1) + (curmax
>> 2) + (curmax
>> 4) + (curmin
>> 3) + (curmin
>> 4));
257 if (tsinceedge
>= (128))
259 //we might need to start counting... assuming ARM wants to reply to the frame.
260 bit_counts
<= 1;// i think? 128 is about 2 bits passed... but 1 also works
262 did_sync
<= 0;//desync
263 curmin
<= `imin; //reset envelope
265 curminthres
<= `ithrmin;
266 curmaxthres
<= `ithrmax;
269 after_hysteresis
<= 1'b1;
274 tsinceedge
<= (tsinceedge
+ 1);
278 if (try_sync
&& tsinceedge
< 128)
280 //detect bits in their middle ssp sampling is in sync, so it would sample all bits in order
281 if (fccount
== bithalf
)
283 if ((~did_sync
) && ((prv
== 1 && (mid
> 128))||
(prv
== 0 && (mid
<= 128))))
285 //sync the Zero, and set curbit roperly
287 zero
<= ~prv
;// 1-prv
291 curbit
<= (mid
> 128) ?
(~zero
) : zero
;
293 prv
<= (mid
> 128) ?
1 : 0;
295 if (adc_d
> curmaxthres
)
297 else if (adc_d
< curminthres
)
301 if (after_hysteresis
)
313 if (fccount
==bitmlen
)
316 prv
<= (mid
> 128) ?
1 : 0;
321 // minimum-maximum calc
322 if(adc_d
> curmaxthres
)
324 else if (adc_d
< curminthres
)
328 if (after_hysteresis
)
343 //put modulation here to maintain the correct clock. Seems that some readers are sensitive to that
345 wire mod
= ((fccount
>= bithalf
) ^ dlay
) & (~disabl
);
347 always @(ck_1356meg
or ssp_dout
or power
or disabl
or mod
)
351 pwr_hi
<= ck_1356meg
;
353 pwr_oe1
<= 1'b0;//mod;
354 pwr_oe2
<= 1'b0;//mod;
355 pwr_oe3
<= 1'b0;//mod;
356 pwr_oe4
<= mod
;//1'b0;