Merge branch 'master' into fpga-merge-and-opensource

# Conflicts:
#	fpga/hi_flite.v
This commit is contained in:
dxl committed 2026-10-12 00:22:24 +08:00
commit da413ac9b9
1316 files changed
+6010798 -28983

No files matched your search

+42 -6
View File
@@ -45,7 +45,7 @@ XST_OPTS_AREA += -fsm_style bram
XST_OPTS_AREA += -fsm_encoding compact
# par specific option (set determistic seed)
PAR_OPTIONS = -t 1
PAR_OPTIONS = -t 2
# Types of selective module compilation:
# WITH_LF Enables selection of LF modules (and disables all HF)
@@ -76,9 +76,13 @@ TARGET2_OPTIONS = -define \{WITH_HF0 WITH_HF1 WITH_HF2 WITH_HF3 WITH_HF5\}
# RDV40/Generic - Enable all HF modules except Felica and ISO14443, select HF_15 instead of HF
TARGET3_OPTIONS = -define \{WITH_HF0 WITH_HF1 WITH_HF3 WITH_HF5 WITH_HF_15\}
# RDV40/Generic - Enable all HF modules except ISO14443
TARGET4_OPTIONS = -define \{WITH_HF0 WITH_HF1 WITH_HF3 WITH_HF4 WITH_HF5\}
TARGET4_OPTIONS = -define \{WITH_HF0 WITH_HF1 WITH_HF3 WITH_HF4 WITH_HF5 WITH_FELICA_PROBE\}
# ICOPYX
TARGET5_OPTIONS = -define {PM3ICOPYX} -rtlview Yes
TARGET5_OPTIONS = $(TARGET1_OPTIONS)
TARGET6_OPTIONS = $(TARGET1_OPTIONS)
TARGET7_OPTIONS = $(TARGET1_OPTIONS)
TARGET8_OPTIONS = $(TARGET1_OPTIONS)
TARGET9_OPTIONS = $(TARGET1_OPTIONS)
# Here we list the target names
TARGET1_NAME = fpga_pm3_lf
@@ -86,6 +90,10 @@ TARGET2_NAME = fpga_pm3_hf
TARGET3_NAME = fpga_pm3_hf_15
TARGET4_NAME = fpga_pm3_felica
TARGET5_NAME = fpga_icopyx_hf
TARGET6_NAME = fpga_pm3_ult_lf
TARGET7_NAME = fpga_pm3_ult_hf
TARGET8_NAME = fpga_pm3_ult_hf_15
TARGET9_NAME = fpga_pm3_ult_felica
# Targets can be compiled for different FPGA flavours
TARGET1_FPGA = xc2s30-5-vq100
@@ -93,6 +101,10 @@ TARGET2_FPGA = $(TARGET1_FPGA)
TARGET3_FPGA = $(TARGET1_FPGA)
TARGET4_FPGA = $(TARGET1_FPGA)
TARGET5_FPGA = xc3s100e-4-vq100
TARGET6_FPGA = xc2s50-5-tq144
TARGET7_FPGA = $(TARGET6_FPGA)
TARGET8_FPGA = $(TARGET6_FPGA)
TARGET9_FPGA = $(TARGET6_FPGA)
# Assemble the final XST options for each target
TARGET1_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_AREA) -p $(TARGET1_FPGA) -ofn $(TARGET1_NAME) $(TARGET1_OPTIONS)
@@ -100,6 +112,10 @@ TARGET2_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_AREA) -p $(TARGET2_FPGA) -ofn $(
TARGET3_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_AREA) -p $(TARGET3_FPGA) -ofn $(TARGET3_NAME) $(TARGET3_OPTIONS)
TARGET4_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_AREA) -p $(TARGET4_FPGA) -ofn $(TARGET4_NAME) $(TARGET4_OPTIONS)
TARGET5_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_SPEED) -p $(TARGET5_FPGA) -ofn $(TARGET5_NAME) $(TARGET5_OPTIONS)
TARGET6_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_SPEED) -p $(TARGET6_FPGA) -ofn $(TARGET6_NAME) $(TARGET6_OPTIONS)
TARGET7_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_SPEED) -p $(TARGET7_FPGA) -ofn $(TARGET7_NAME) $(TARGET7_OPTIONS)
TARGET8_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_SPEED) -p $(TARGET8_FPGA) -ofn $(TARGET8_NAME) $(TARGET8_OPTIONS)
TARGET9_XST_OPTS = $(XST_OPTS_BASE) $(XST_OPTS_SPEED) -p $(TARGET9_FPGA) -ofn $(TARGET9_NAME) $(TARGET9_OPTIONS)
# these files are common for all targets
TARGET_COMMON_FILES = define.v
@@ -125,14 +141,18 @@ TARGET2_FILES = $(TARGET1_FILES)
TARGET3_FILES = $(TARGET1_FILES)
TARGET4_FILES = $(TARGET1_FILES)
TARGET5_FILES = $(TARGET_COMMON_FILES) mux2_onein.v mux2_oneout.v fpga_icopyx_hf.v fpga_icopyx_lf.v fpga_icopyx_top.v
TARGET6_FILES = $(TARGET1_FILES)
TARGET7_FILES = $(TARGET1_FILES)
TARGET8_FILES = $(TARGET1_FILES)
TARGET9_FILES = $(TARGET1_FILES)
# List of all valid target FPGA images to build
TARGETS = $(TARGET1_NAME) $(TARGET2_NAME) $(TARGET3_NAME) $(TARGET4_NAME) $(TARGET5_NAME)
TARGETS = $(TARGET1_NAME) $(TARGET2_NAME) $(TARGET3_NAME) $(TARGET4_NAME) $(TARGET5_NAME) $(TARGET6_NAME) $(TARGET7_NAME) $(TARGET8_NAME) $(TARGET9_NAME)
# Verbosity type for ISE tools ise|xflow|silent
VERBOSITY = -intstyle silent
# Echo (Q=) or not echo (Q=@) build commands to the terminal
Q=@
Q=
# Pass the custom variables to the lower make rules
$(TARGET1_NAME).bit: TARGET_FPGA = $(TARGET1_FPGA)
@@ -155,6 +175,22 @@ $(TARGET5_NAME).bit: TARGET_FPGA = $(TARGET5_FPGA)
$(TARGET5_NAME).bit: TARGET_FILES = $(TARGET5_FILES)
$(TARGET5_NAME).bit: TARGET_XST_OPTS = $(TARGET5_XST_OPTS)
$(TARGET6_NAME).bit: TARGET_FPGA = $(TARGET6_FPGA)
$(TARGET6_NAME).bit: TARGET_FILES = $(TARGET6_FILES)
$(TARGET6_NAME).bit: TARGET_XST_OPTS = $(TARGET6_XST_OPTS)
$(TARGET7_NAME).bit: TARGET_FPGA = $(TARGET7_FPGA)
$(TARGET7_NAME).bit: TARGET_FILES = $(TARGET7_FILES)
$(TARGET7_NAME).bit: TARGET_XST_OPTS = $(TARGET7_XST_OPTS)
$(TARGET8_NAME).bit: TARGET_FPGA = $(TARGET8_FPGA)
$(TARGET8_NAME).bit: TARGET_FILES = $(TARGET8_FILES)
$(TARGET8_NAME).bit: TARGET_XST_OPTS = $(TARGET8_XST_OPTS)
$(TARGET9_NAME).bit: TARGET_FPGA = $(TARGET9_FPGA)
$(TARGET9_NAME).bit: TARGET_FILES = $(TARGET9_FILES)
$(TARGET9_NAME).bit: TARGET_XST_OPTS = $(TARGET9_XST_OPTS)
$(TARGETS):
$(Q)$(MKDIR) $(PREFIX)build_$@
$(Q)$(MAKE) -C $(PREFIX)build_$@ -f ../Makefile $(notdir $@).bit
@@ -192,7 +228,7 @@ work:
$(Q)$(RM) $@ $*.drc $*.rbt
$(info [=] BITGEN $@)
$(Q)$(XILINX_TOOLS_PREFIX)bitgen $(VERBOSITY) -w $* $@
python3 ../strip_date_time_from_binary.py $@ || true
$(Q)python3 ../strip_date_time_from_binary.py $@ || true
$(Q)$(CP) $@ ..
# Build all targets
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+7
View File
@@ -28,6 +28,8 @@
//`define PM3GENERIC
// iCopy-X with XC3S100E
//`define PM3ICOPYX
// Proxmark3 Ultimate with XC2S50
//`define PM3ULTIMATE
// Pass desired defines to compiler to enable required modules
// WITH_LF enables Low Frequency mode when defined else HF is enabled
@@ -206,6 +208,10 @@ lo_edge_detect le(
.cross_lo (cross_lo),
.lf_field (conf_word[0]),
.lf_ed_toggle_mode (conf_word[1]),
.lf_weak_load (conf_word[2]),
.lf_ed_hold_tracker (conf_word[3]),
.lf_ed_sensitive (conf_word[4]),
.lf_ed_slope (conf_word[5]),
.lf_ed_threshold (lf_ed_threshold),
.ssp_dout (ssp_dout),
@@ -248,6 +254,7 @@ lo_adc la(
.adc_d (adc_d),
.divisor (divisor),
.lf_field (conf_word[0]),
.lf_weak_load (conf_word[2]),
.ssp_dout (ssp_dout),
.ssp_din (mux3_ssp_din),
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+269 -76
View File
@@ -53,17 +53,63 @@ assign debug = 0;
wire power = mod_type[2];
wire speed = mod_type[1];
wire disabl = mod_type[0];
// Signal probe: stream how much the envelope is actually moving instead of
// demodulated bits, so reading distance can be measured rather than guessed.
wire probe = mod_type[3];
// 512x64/fc -wait before ts0, 32768 ticks
// tslot: 256*64/fc
assign adc_clk = ck_1356meg;
///heuristic values for initial thresholds. seem to work OK
// Initial envelope guess, only used until the first edges are seen.
`define imin 70 // (13'd256)
`define imax 180 // (-13'd256)
`define ithrmin 91 // -13'd8
`define ithrmax 160 // 13'd8
// Narrowest hysteresis band the threshold generator will produce. An
// unmodulated carrier collapses curmin onto curmax, and without a floor the
// band would collapse with it, so every ADC noise count would look like an
// edge. This floor is also the sensitivity limit: a tag whose modulation is
// smaller than the band can never arm the edge detector, so it sets the
// reading distance.
//
// Measured on a RDV4 with the reader field on: the raw carrier ripples 4..6
// counts peak to peak, about +/- 1 after the lowpass above. +/- 4 keeps a 4x
// noise margin while halving the smallest tag modulation we can still see
// compared to sizing the floor against the unfiltered ripple.
`define minhalfband 8
// Recovery watchdog, counting samples since the demodulator was last in a
// known-good idle state (the desync in the stable branch below).
//
// Nothing else in this module can put the hysteresis band back onto the signal
// once it has drifted off it, and there are two ways to get there. The band can
// end up somewhere the signal never visits, which makes the stable branch -
// the only place thresholds are recomputed and the only place a desync can
// happen - permanently unreachable. Or try_sync can latch on with the band
// mis-positioned, where edges of one polarity keep clearing tsinceedge so the
// desync never fires either. Both freeze the slicer, and both survive the
// field being switched off between commands, because these are FPGA registers
// and only a bitstream reload clears them. The symptom is that the first
// command after the client starts works and everything after it fails on a
// signal that is plainly strong enough.
//
// 2^18 samples is 19.3 ms. The longest legal FeliCa frame, 255 bytes plus
// preamble and sync, is 9.9 ms, so this cannot fire part way through a reply.
// During quiet it fires harmlessly and keeps the band centred on the carrier.
`define stalebit 18
// Bit decisions to ignore right after the edge detector arms. try_sync is
// armed part way through a bit, so its first accumulation covers only part of
// a half-bit and the decision that follows is meaningless. Acting on it can
// latch `zero` inverted, which decodes the entire frame with the wrong
// polarity and loses the sync word. The preamble is 48 bits, so skipping the
// first couple of decisions costs nothing and lets the real preamble-to-sync
// transition be the one that locks polarity.
`define syncguard 2
`define min_bitdelay_212 8
//minimum values and corresponding thresholds
reg [8:0] curmin=`imin;
@@ -71,10 +117,45 @@ reg [8:0] curminthres=`ithrmin;
reg [8:0] curmaxthres=`ithrmax;
reg [8:0] curmax=`imax;
// Hysteresis band, derived from the tracked envelope rather than from fixed
// levels. The old code blended curmin/curmax with fixed weights, but clamped
// curmin to <= `imin and curmax to >= `imax, so the thresholds could never
// leave ~91/160 no matter where the signal actually sat. A tag whose envelope
// lives inside that window - which is the normal case for a reader field, the
// peak detector idles near 112 and a tag swings it by about +/-20 - never
// crossed a threshold at all and demodulated as a constant.
//
// The band is now centred on the tracked envelope and set to 3/16 of its span.
// The old 0.8125/0.1875 blend worked out to 5/16, which on a real card leaves
// only about 1.6x margin between the threshold and the modulation peaks and
// bit-slips often; 3/16 roughly doubles that margin.
wire [8:0] span = (curmax > curmin) ? (curmax - curmin) : 9'd0;
wire [9:0] envsum = {1'b0, curmax} + {1'b0, curmin};
wire [8:0] centre = envsum[9:1];
// 3/16 of the tracked span, floored. Keep the intermediate wide: span reaches
// 255 and 3 * 255 needs 10 bits, a 9 bit intermediate silently wraps and hands
// back a far too narrow band exactly when the envelope is widest.
wire [11:0] scaledspan = ({3'd0, span} << 1) + {3'd0, span};
wire [8:0] rawhalf = scaledspan[11:4];
wire [8:0] halfband = (rawhalf < `minhalfband) ? `minhalfband : rawhalf;
wire [8:0] lothres = (centre > halfband) ? (centre - halfband) : 9'd0;
// where to put the band when re-centring on the level actually present
wire [9:0] rc_hisum = {2'd0, adc_d} + `minhalfband;
wire [8:0] rc_hi = (rc_hisum > 10'd255) ? 9'd255 : rc_hisum[8:0];
wire [8:0] rc_lo = (adc_d > `minhalfband) ? ({1'b0, adc_d} - `minhalfband) : 9'd0;
wire [9:0] hisum = {1'b0, centre} + {1'b0, halfband};
wire [8:0] hithres = (hisum > 10'd255) ? 9'd255 : hisum[8:0];
//signal state, 1-not modulated, 0 -modulated
reg after_hysteresis = 1'b1;
//state machine for envelope tracking
// Keep this out of block RAM. The project synthesises with -fsm_style bram, and
// once this got large enough for XST to recognise it as a state machine it put
// the state ROM in a block RAM - the xc2s30 has six and the design already uses
// all of them, so the build failed to fit with nothing but a MAP error to say
// so. It is two bits; LUTs are the right home for it.
(* fsm_extract = "no" *)
reg [1:0] state = 1'd0;
//lower edge detected, trying to detect first bit of SYNC (b24d, 1011001001001101)
@@ -83,6 +164,12 @@ reg try_sync = 1'b0;
//detected first sync bit, phase frozen
reg did_sync=0;
//samples since the last known-good idle, see `stalebit
reg [`stalebit:0] stale = 0;
//decisions still to skip before did_sync may latch, see `syncguard
reg [1:0] guard = 2'd0;
`define bithalf_212 32 // half-bit length for 212 kbit
`define bitmlen_212 63 // bit transition edge
@@ -92,6 +179,14 @@ reg did_sync=0;
wire [7:0] bithalf = speed ? `bithalf_424 : `bithalf_212;
wire [7:0] bitmlen = speed ? `bitmlen_424 : `bitmlen_212;
// curbit_raw is decided in the bit-phase domain, which is aligned to the tag's
// edges by try_sync and so drifts against ssp_cnt. curbit is that decision
// re-timed into the ssp domain: the SSC latches ssp_din when ssp_clk rises at
// ssp_cnt[5:0] == 0, so updating half an ssp bit away from that keeps the ARM
// from ever sampling a bit while it is changing. Both run at 64 carrier
// periods per bit, so this is a re-time and not a resample - no bit is
// duplicated or dropped.
reg curbit_raw = 1'b0;
reg curbit = 1'b0;
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
@@ -107,6 +202,43 @@ reg [8:0] ssp_cnt = 9'd0;
always @(posedge adc_clk)
ssp_cnt <= (ssp_cnt + 1'b1);
always @(negedge adc_clk)
if (ssp_cnt[5:0] == 6'd32)
curbit <= curbit_raw;
`ifdef WITH_FELICA_PROBE
// Signal probe: min and max of the envelope over each ssp byte window (512
// carrier periods, 8 bit periods), reported in alternating bytes - min, max,
// min, max. That gives both the carrier level and the tag modulation depth,
// which are different questions: the level says whether the front end is
// running out of ADC range, the depth says whether the tag is in range at all.
// Measured on a RDV4: idle ripples 4..6 counts, an ordinary card on the
// antenna swings about 70, a strongly coupled one over 200.
reg [7:0] pmin = 8'hff;
reg [7:0] pmax = 8'd0;
reg [7:0] pout = 8'd0;
reg ptog = 1'b0;
reg [7:0] probe_sr = 8'd0;
always @(negedge adc_clk)
begin
if (ssp_cnt[8:0] == 9'd0)
begin
pout <= ptog ? pmax : pmin;
ptog <= ~ptog;
pmin <= adc_d;
pmax <= adc_d;
end
else
begin
if (adc_d < pmin) pmin <= adc_d;
if (adc_d > pmax) pmax <= adc_d;
end
end
`endif
//maybe change it so that ARM sends preamble as well.
//then: ready bits sent to ARM, 8 bits sent from ARM (all ones), then preamble (all zeros, presumably) - which starts modulation
@@ -134,7 +266,26 @@ begin
begin
ssp_clk <= 1'b1;
//send current bit (detected in SNIFF mode or the one being modulated in MOD mode, 0 otherwise)
ssp_din <= curbit;
`ifdef WITH_FELICA_PROBE
if (probe)
begin
// one 8 bit reading per ssp byte, LSB first so the ARM reads it verbatim
if (ssp_cnt[8:6] == 3'd0)
begin
ssp_din <= pout[0];
probe_sr <= {1'b0, pout[7:1]};
end
else
begin
ssp_din <= probe_sr[0];
probe_sr <= {1'b0, probe_sr[7:1]};
end
end
else
`endif
begin
ssp_din <= curbit;
end
end
// At speed 0, clk falls at 32; at speed 1, clk falls at 16.
if( ( (~speed) && (ssp_cnt[5:0] == 6'b100000)) ||(speed && ssp_cnt[4:0] == 5'b10000))
@@ -151,11 +302,24 @@ begin
end
end
//previous signal value, mostly to detect SYNC
reg prv = 1'b1;
// Matched-filter accumulators. acc integrates the envelope over the half-bit
// in progress, h1 holds the completed first half.
//
// The top 6 ADC bits are enough and the xc2s30 has no room for more: 32 samples
// of 6 bits needs 11 bits of accumulator against 13 for the full 8. Resolution
// is not the limit here - a 70 count tag swing is 17 counts at 6 bits, times 32
// samples is a difference of ~560, against a noise floor of about 6 after the
// same averaging. Two extra bits would buy nothing and cost slices the device
// does not have.
wire [5:0] samp = adc_d[7:2];
reg [11:0] acc = 12'd0;
reg [11:0] h1 = 12'd0;
wire [11:0] h2 = acc + {6'd0, samp};
wire firsthalfhigh = (h1 > h2);
// 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.
reg[7:0] mid = 8'd128;
// which half was the larger on the previous bit, ie the previous bit value
// before polarity is known. A change in it is a Manchester bit transition.
reg prv_s = 1'b0;
// set TAGSIM__MODULATE on ARM if we want to write... (frame would get lost if done mid-frame...)
// start sending over 1s on ssp->arm when we start sending preamble
@@ -166,6 +330,11 @@ reg [11:0] bit_counts = 12'd0; // for timeslots. only support ts=0 for now, at 2
reg dlay;
always @(negedge adc_clk) // every data ping?
begin
// Watchdog clock, see `stalebit. It runs first so that the three places
// that clear it below - a clean desync, a successful Manchester lock, and
// the watchdog firing itself - all override it.
stale <= stale + 1'b1;
//envelope follow code...
////////////
if (fccount == bitmlen)
@@ -206,13 +375,13 @@ begin
begin
case (state)
0: begin
curmax <= adc_d > `imax? adc_d : `imax;
curmax <= adc_d;
state <= 2;
end
1: begin
curminthres <= ((curmin >> 1) + (curmin >> 2) + (curmin >> 4) + (curmax >> 3) + (curmax >> 4)); //threshold: 0.1875 max + 0.8125 min
curmaxthres <= ((curmax >> 1) + (curmax >> 2) + (curmax >> 4) + (curmin >> 3) + (curmin >> 4));
curmax <= adc_d > 155 ? adc_d : 155; // to hopefully prevent overflow from spikes going up to 255
curminthres <= lothres;
curmaxthres <= hithres;
curmax <= adc_d;
state <= 2;
end
2: begin
@@ -231,7 +400,7 @@ begin
begin
case (state)
0: begin
curmin <= adc_d<`imin? adc_d :`imin;
curmin <= adc_d;
state <= 1;
end
1: begin
@@ -239,9 +408,9 @@ begin
curmin <= adc_d;
end
2: begin
curminthres <= ( (curmin >> 1) + (curmin >> 2) + (curmin >> 4) + (curmax >> 3) + (curmax >> 4));
curmaxthres <= ( (curmax >> 1) + (curmax >> 2) + (curmax >> 4) + (curmin >> 3) + (curmin >> 4));
curmin <= adc_d < `imin ? adc_d : `imin;
curminthres <= lothres;
curmaxthres <= hithres;
curmin <= adc_d;
state <= 1;
end
default:
@@ -254,10 +423,18 @@ begin
try_sync <= 1;
fccount <= 1;
did_sync <= 0;
curbit <= 0;
mid <= 8'd127;
guard <= `syncguard;
// A frame is starting: give it the full watchdog interval. Doing
// this only on did_sync below is not enough, because did_sync stays
// latched between frames unless a desync clears it, so during a
// long exchange - `hf felica dump` walking a card's nodes - the
// watchdog clock keeps running across frames and eventually fires
// in the middle of one. try_sync arming is the one event that
// happens at the start of every frame.
stale <= 0;
curbit_raw <= 0;
acc <= 0;
tsinceedge <= 0;
prv <= 1;
end
else
begin
@@ -266,8 +443,8 @@ begin
end
else //stable state, low or high
begin
curminthres <= ( (curmin >> 1) + (curmin >> 2) + (curmin >> 4) + (curmax >> 3) + (curmax >> 4));
curmaxthres <= ( (curmax >> 1) + (curmax >> 2) + (curmax >> 4) + (curmin >> 3) + (curmin >> 4));
curminthres <= lothres;
curmaxthres <= hithres;
state <= 0;
if (try_sync )
@@ -278,15 +455,20 @@ begin
bit_counts <= 1;// i think? 128 is about 2 bits passed... but 1 also works
try_sync <= 0;
did_sync <= 0;//desync
curmin <= `imin; //reset envelope
curmax <= `imax;
curminthres <= `ithrmin;
curmaxthres <= `ithrmax;
prv <= 1;
stale <= 0; //this is the known-good state the watchdog looks for
// Re-centre the envelope on the carrier level that is actually
// there. Resetting to the compile-time constants instead threw
// away the only measurement we had and, when the real level sat
// inside `ithrmin..`ithrmax, guaranteed the next frame could not
// produce a single edge.
curmin <= adc_d;
curmax <= adc_d;
curminthres <= lothres;
curmaxthres <= hithres;
tsinceedge <= 0;
after_hysteresis <= 1'b1;
curbit <= 0;
mid <= 8'd128;
curbit_raw <= 0;
acc <= 0;
end
else
tsinceedge <= (tsinceedge + 1'b1);
@@ -295,65 +477,76 @@ begin
if (try_sync && tsinceedge < 128)
begin
//detect bits in their middle ssp sampling is in sync, so it would sample all bits in order
// Matched-filter bit detector. Each Manchester bit is two half-bits of
// opposite level, so integrating the raw ADC over each half and taking
// the larger recovers the bit without ever consulting a threshold.
//
// The old detector counted comparator trips instead: +1 per sample
// above curmaxthres, -1 below curminthres, and inside the dead band it
// just repeated the previous crossing direction. That made every bit
// depend on where the hysteresis band happened to sit, which is what
// made a mispositioned band rail the output to a constant, a clipped
// envelope mis-slice, and a weak tag undetectable. It also threw away
// amplitude, so it gained nothing from the 32x oversampling.
//
// Integrating 32 samples per half instead averages the noise down by
// sqrt(32) and cancels any offset common to both halves, which is what
// slew and clipping asymmetry look like. Thresholds still drive the bit
// phase and the desync below, they just no longer decide bit values.
if (fccount == bithalf)
begin
if ((~did_sync) && ((prv == 1 && (mid > 128))||(prv == 0 && (mid <= 128))))
h1 <= acc; // first half: samples 0..31
acc <= {6'd0, samp}; // second half starts here, sample 32
end
else if (fccount == bitmlen)
begin
// h2 is acc plus this sample, so both halves are 32 samples and
// neither is biased by an extra sample of carrier.
if (guard != 2'd0)
guard <= guard - 2'd1;
// A bit value change flips which half is the larger. The preamble
// is 48 identical bits, so the first flip is the preamble meeting
// the sync word, whose first bit is a 1. Lock polarity there.
if ((~did_sync) && (guard == 2'd0) && (firsthalfhigh != prv_s))
begin
//sync the Zero, and set curbit roperly
did_sync <= 1'b1;
zero <= ~prv;// 1-prv
curbit <= 1;
zero <= ~firsthalfhigh;
curbit_raw <= 1;
// A Manchester lock means the demodulator is working, so hold
// the watchdog off for the frame that is now starting. Without
// this it eventually fires part way through a reply and clears
// try_sync and did_sync mid-frame, slipping every bit after
// that point. It is deterministic rather than rare: the delay
// from field-on to reply barely varies, so the watchdog phase
// lines up with the reply on attempt after attempt.
stale <= 0;
end
else
curbit <= (mid > 128) ? (~zero) : zero;
curbit_raw <= firsthalfhigh ? (~zero) : zero;
prv <= (mid > 128) ? 1 : 0;
if (adc_d > curmaxthres)
mid <= 8'd129;
else if (adc_d < curminthres)
mid <= 8'd127;
else
begin
if (after_hysteresis)
begin
mid <= 8'd129;
end
else
begin
mid <= 8'd127;
end
end
prv_s <= firsthalfhigh;
acc <= 0;
end
else
begin
if (fccount==bitmlen)
begin
// fccount <= 0;
prv <= (mid > 128) ? 1 : 0;
mid <= 128;
end
else
begin
// minimum-maximum calc
if(adc_d > curmaxthres)
mid <= mid + 1'b1;
else if (adc_d < curminthres)
mid <= mid - 1'b1;
else
begin
if (after_hysteresis)
begin
mid <= mid + 1'b1;
end
else
begin
mid <= mid - 1'b1;
end
end
end
end
acc <= acc + {6'd0, samp};
end
// Watchdog, see `stalebit. Put the band back on the signal directly, since
// nothing above is able to any more.
if (stale[`stalebit])
begin
curmin <= adc_d;
curmax <= adc_d;
curminthres <= rc_lo;
curmaxthres <= rc_hi;
try_sync <= 1'b0;
did_sync <= 1'b0;
curbit_raw <= 1'b0;
after_hysteresis <= 1'b1;
tsinceedge <= 0;
state <= 0;
stale <= 0;
end
// sending <= 0;
end
+152 -7
View File
@@ -18,6 +18,10 @@ module lf_edge_detect(
input clk,
input [7:0] adc_d,
input [7:0] lf_ed_threshold,
input freeze,
input sensitive,
input slope_mode,
input data_rdy,
output [7:0] max,
output [7:0] min,
@@ -33,6 +37,7 @@ min_max_tracker tracker(
.clk (clk),
.adc_d (adc_d),
.threshold (lf_ed_threshold),
.freeze (freeze),
.min (min),
.max (max)
);
@@ -44,13 +49,80 @@ assign lowz_threshold = (max + min) / 2 - (max - min) / 8;
assign low_threshold = (max + min) / 2 - (max - min) / 4;
// heuristic to see if it makes sense to try to detect an edge
//
// The gaps below are span/8, span/4 and span/8, so the default limits require a
// tracked span of more than 64 counts before any edge is reported. That is fine
// for a settled signal and wrong for a recovering one: right after a tag answers,
// the reader's own frame arrives on an envelope ramping from 8 back up to 135,
// where the per bit dips ride on the ramp and the honest tracked span is small.
// The detector then switches itself off exactly when it is needed, which is what
// leaves a Proxmark simulating Hitag 2 deaf across the reader's password.
//
// `sensitive` relaxes the limits so a tight span still counts as workable. It is
// opt in: on a settled signal the wider limits reject noise, and that is worth
// keeping as the default.
wire [7:0] gap_outer = sensitive ? 8'd2 : 8'd8;
wire [7:0] gap_inner = sensitive ? 8'd4 : 8'd16;
wire enabled =
(high_threshold > highz_threshold)
& (highz_threshold > lowz_threshold)
& (lowz_threshold > low_threshold)
& ((high_threshold - highz_threshold) > 8)
& ((highz_threshold - lowz_threshold) > 16)
& ((lowz_threshold - low_threshold) > 8);
& ((high_threshold - highz_threshold) > gap_outer)
& ((highz_threshold - lowz_threshold) > gap_inner)
& ((lowz_threshold - low_threshold) > gap_outer);
// Slope detection.
//
// Everything above slices against a level derived from the tracked min and max.
// That works on a settled signal and fails on a moving one: right after the tag
// answers, the reader's frame rides on an envelope ramping back up, so a fixed
// level is reached late or not at all. Per the datasheet a '0' is 18..22 T0 and
// a '1' is 26..32, and t_stop is anything over 36 - so two merged '0' bits, at
// 36..44, cannot be told from a stop by duration. The edge has to be produced
// here rather than inferred later.
//
// A gap is a sharp drop whatever the DC level is, so compare each sample against
// one from thirty two 1 MSa/s samples ago - 32 us, two thirds of the 48 us gap -
// and call the edge on the difference. lf_ed_threshold sets how big a step
// counts, so it stays tunable from the ARM.
//
// The baseline has to be a decent fraction of the gap or the step is small and
// the usable threshold band with it: at 16 us, 18 worked and 14 was already
// noise, which is too tight to trust across rigs. Widening it to 64 us, so the
// comparison spans the whole gap, was measured to gain nothing - same 29 bits of
// the reader's 32 - so 32 us stays and the delay line stays half the size.
reg [7:0] hist [0:31];
reg [4:0] hist_ptr = 0;
always @(posedge clk)
if (data_rdy)
begin
hist[hist_ptr] <= adc_d;
hist_ptr <= hist_ptr + 1;
end
wire [7:0] adc_delayed = hist[hist_ptr]; // the oldest entry, 32 samples back
// How big a step counts as an edge, in ADC counts over the 32 us baseline.
//
// Absolute. Scaling it to the tracked span was tried twice and the tracker's
// span turns out not to be a usable reference: (max - min) >> 3 chatters - 1602
// edges, no decodable frame - while span/3 goes dead, no edges at all. Those two
// bracket the working step of ~31, so the span is not stable between them; it is
// driven by our own load modulation as well as the reader's, which is exactly
// what makes it the wrong thing to normalise against. min_max_tracker also
// starts at min=255, max=0, so any such expression needs an underflow guard.
//
// Swept against a Proxmark reader: 28 and below chatters, 29 loses a bit, 30..33
// authenticates, 34 degrades, 36 stops resolving frames. The optimum does drift
// with coupling - 30, then 31..32, then 32..33 across rig changes today - so -t
// is the knob for a different setup.
wire falling_slope = (adc_delayed > adc_d) &&
(({1'b0, adc_delayed} - {1'b0, adc_d}) >= {1'b0, lf_ed_threshold});
wire rising_slope = (adc_d > adc_delayed) &&
(({1'b0, adc_d} - {1'b0, adc_delayed}) >= {1'b0, lf_ed_threshold});
// Toggle the output with hysteresis
// Set to high if the ADC value is above the threshold
@@ -62,10 +134,40 @@ reg trigger_enabled = 1;
reg output_edge = 0;
reg output_state;
// How far the signal has to travel before the state output flips.
//
// The default slices on high/low, which are +/- span/4 either side of the mean,
// so every bit has to traverse half the tracked span. A reader's BPLM '0' bit
// only gives 14 T0 of field between its gaps, and on a run of them the envelope
// never climbs back above high_threshold - so the state never returns high, no
// new falling edge is produced, and two bits merge into one interval. Measured
// against a Proxmark reader: the 32 bit password arrived as 29 bits with a single
// 38 T0 interval where two 20 T0 zeroes should have been.
//
// `sensitive` slices on the inner pair instead, +/- span/8, halving the travel
// needed. Left as an option because the wider hysteresis is what rejects noise
// on a settled signal.
// Measured: an asymmetric pair - trigger on low_threshold, re-arm on lowz - was
// tried here and came out worse, 30 bits of the reader's 32 against 31 for the
// symmetric inner pair. Keeping both slices on the inner thresholds.
// Where to slice, when `sensitive` is on.
//
// The default pair sits +/- span/4 either side of the mean, so every bit has to
// traverse half the tracked span. A reader's BPLM '0' only gives 14 T0 of field
// between its gaps, and on a run of them the envelope never climbs back over
// high_threshold - the state never returns high, no new falling edge is produced
// and two bits merge into one interval.
//
// Measured alternatives, on the reader's 32 bit password: +/- span/8 gives 31
// bits, span/16 the same but less consistently, an asymmetric strict-trigger /
// easy-rearm pair 30, and biasing the band towards the top of the envelope 29.
wire [7:0] hi_slice = sensitive ? highz_threshold : high_threshold;
wire [7:0] lo_slice = sensitive ? lowz_threshold : low_threshold;
always @(posedge clk)
begin
is_high <= (adc_d >= high_threshold);
is_low <= (adc_d <= low_threshold);
is_high <= (adc_d >= hi_slice);
is_low <= (adc_d <= lo_slice);
is_zero <= ((adc_d > lowz_threshold) & (adc_d < highz_threshold));
end
@@ -86,9 +188,52 @@ begin
trigger_enabled <= trigger_enabled | is_zero;
end
// edge states
// Measured: requiring the slope to hold across two consecutive 1 MSa/s samples
// was tried, to throw away the single sample noise that has the reader's 32 bit
// frame arriving as anything from 30 to 33 bits. It does remove the extra edges
// - nothing over 32 after it - but delays the real ones enough to lose more than
// it saves, 1 authentication in 7 against 2 without. Left undebounced.
// Return the slope output to idle high after a long flat stretch.
//
// The two slope rules below only ever move output_state on a slope, so it has no
// idle value: it keeps whatever the last edge left it at. Left low - which is
// where a tag's answer tends to leave it - the next gap drives it low again, so
// there is no transition, no falling edge and no counter reset, and only the
// rising edge at the END of that gap re-arms it. The frame's first bit is then
// never measured: `lf hitag sniff` logged the reader's first gap as a rising edge
// carrying ra=254, meaning the capture counter had not been reset for 254 T0.
//
// A gap is 4..10 T0 and a bit period at most 32, so the longest flat stretch
// inside a frame is about 28 T0; between frames it is at least t_EOF, 80 T0.
// 512 samples at 1 MSa/s is 64 T0, comfortably between the two.
reg [9:0] flat = 0;
always @(posedge clk)
if (enabled)
if (data_rdy)
begin
if (rising_slope | falling_slope)
flat <= 0;
else if (flat != 10'd1023)
flat <= flat + 1;
end
// edge states
//
// Slope mode is not gated on `enabled`: that heuristic asks whether the tracked
// min/max span is wide enough to slice against, which is exactly the judgement
// slope detection does not need.
always @(posedge clk)
if (slope_mode)
begin
if (rising_slope)
output_state <= 1'd1;
else if (falling_slope)
output_state <= 1'd0;
else if (flat == 10'd512)
output_state <= 1'd1;
end
else if (enabled)
begin
if (is_high)
output_state <= 1'd1;
+5 -2
View File
@@ -26,6 +26,7 @@ module lo_adc(
input [7:0] adc_d,
input [7:0] divisor,
input lf_field,
input lf_weak_load,
input ssp_dout,
output ssp_din,
@@ -56,8 +57,10 @@ assign pwr_hi = 1'b0;
// low frequency outputs
assign pwr_lo = reader_modulation;
assign pwr_oe2 = 1'b0; // 33 Ohms
assign pwr_oe3 = tag_modulation; // base antenna load = 33 Ohms
assign pwr_oe4 = 1'b0; // 10k Ohms
// lf_weak_load moves the modulation from the 33 Ohm leg to the 10k one, so the
// depth measured here matches what lo_edge_detect does in the same mode.
assign pwr_oe3 = tag_modulation & ~lf_weak_load; // base antenna load = 33 Ohms
assign pwr_oe4 = tag_modulation & lf_weak_load; // 10k Ohms
// Debug Output ADC clock
assign debug = adc_clk;
+79 -3
View File
@@ -32,6 +32,10 @@ module lo_edge_detect(
input cross_lo,
input lf_field,
input lf_ed_toggle_mode,
input lf_weak_load,
input lf_ed_hold_tracker,
input lf_ed_sensitive,
input lf_ed_slope,
input [7:0] lf_ed_threshold,
input ssp_dout,
@@ -52,15 +56,78 @@ wire reader_modulation = !ssp_dout & lf_field & pck_divclk;
// No logic, straight through.
assign pwr_oe1 = 1'b0; // not used in LF mode
assign pwr_oe3 = 1'b0; // base antenna load = 33 Ohms
// when modulating, add another 33 Ohms and 10k Ohms in parallel:
assign pwr_oe2 = tag_modulation;
// Modulate on the same leg the working LF tag simulator uses.
//
// This used to hold pwr_oe3 off and modulate on pwr_oe2 and pwr_oe4 instead,
// which is not what lo_adc.v does - and lo_adc.v is the path behind the LF tag
// simulation that is known to swing a reader's ADC rail to rail from this coil:
//
// lo_adc.v oe2 = 0, oe3 = mod & ~weak, oe4 = mod & weak
// lo_edge_detect.v oe2 = mod & ~weak, oe3 = 0, oe4 = mod
//
// Measured against a genuine Paxton reader with a Proxmark simulating on top of
// it: the reader beeps at a continuous coil load toggle, so it can see this
// antenna modulating, and our Hitag 2 answer is emitted perfectly - 1184 samples
// a frame, no bails, the turnaround matching a genuine fob's 227 T0 to the
// carrier period - yet the reader never advances past START_AUTH. Detectable
// but not decodable is what too shallow a load looks like on a short burst.
//
// The earlier note here claimed both legs together take our own envelope from
// ~148 counts to 0. That figure was withdrawn as unsound - it assumed a DOUT
// toggle in a mode where it was not reaching the coil - and in any case what
// matters is the perturbation at the READER's antenna, not at ours.
// Modulation depth, measured against a genuine Paxton reader.
//
// Three load configurations were tried, judged both by how many of the reader's
// START_AUTH polls this simulator could still hear after its own answer, and by
// whether the reader ever advanced past START_AUTH:
//
// oe2 + oe4 96 polls seen of 218 reader does not advance
// oe3 alone 218 polls seen of 218 reader does not advance
// oe2 + oe3 + oe4 103 polls seen of 218 reader does not advance
//
// So depth is not what the reader is rejecting - across a 2x range of load it
// behaves identically - but it matters a great deal to our own receive. A
// heavier load pins the min/max tracker this same module feeds and leaves the
// simulator deaf for a while after every answer, which loses half the reader's
// polls and would lose its follow up command too: a real reader's t_WAIT2 can be
// as short as 90 T0, and Paxton's measures about 269.
//
// oe3 alone is the lightest of the three, and the leg lo_adc.v drives - but it is
// too light to be HEARD: with it a Proxmark reader 1 cm away got nothing at all
// from this simulator, RXFAIL no_signal=127 and not one block read, where
// oe2 + oe4 reads and writes reliably. So the pair stays.
//
// That leaves our own post-answer deafness to solve the other way, by freezing
// the min/max tracker across our modulation rather than by modulating less - see
// lf_ed_hold_tracker below, which is what that input is for.
assign pwr_oe2 = tag_modulation & ~lf_weak_load;
assign pwr_oe4 = tag_modulation;
assign pwr_oe3 = 1'b0; // base antenna load = 33 Ohms
assign ssp_clk = cross_lo;
assign pwr_lo = reader_modulation;
assign pwr_hi = 1'b0;
// Hold the envelope follower across our own answer.
//
// Retriggerable: every carrier period in which we are modulating reloads the
// counter, so the follower stays held for the whole frame and is released a
// short time after the last transition. pck0 is 24 MHz, so 8192 counts is
// about 340 us - longer than a 128 us half bit, short enough to be listening
// again well inside the reader's t_WAIT2.
reg [12:0] hold_cnt = 0;
always @(posedge pck0)
begin
if (tag_modulation)
hold_cnt <= 13'h1FFF;
else if (hold_cnt != 0)
hold_cnt <= hold_cnt - 1;
end
wire tracker_freeze = lf_ed_hold_tracker & (hold_cnt != 0);
// filter the ADC values
wire data_rdy;
wire [7:0] adc_filtered;
@@ -85,10 +152,19 @@ wire [7:0] high_threshold, highz_threshold, lowz_threshold, low_threshold;
wire [7:0] max, min;
wire edge_state, edge_toggle;
// Measured: feeding the detector the unfiltered adc_d instead of adc_filtered
// was tried, on the grounds that lp20khz_1MSa_iir_filter cuts right at the 20 kHz
// a 6 T0 reader gap sits on and rounds it. It produced an edge storm - 188043
// edges and not one decoded frame - so the filter stays.
lf_edge_detect lf_ed(
.clk (pck0),
.adc_d (adc_filtered),
.lf_ed_threshold (lf_ed_threshold),
.freeze (tracker_freeze),
.sensitive (lf_ed_sensitive),
.slope_mode (lf_ed_slope),
.data_rdy (data_rdy),
.max (max),
.min (min),
.high_threshold (high_threshold),
+8
View File
@@ -27,6 +27,7 @@ module min_max_tracker(
input clk,
input [7:0] adc_d,
input [7:0] threshold,
input freeze,
output [7:0] min,
output [7:0] max
);
@@ -37,7 +38,14 @@ reg [7:0] cur_min_val = 255;
reg [7:0] cur_max_val = 0;
reg [1:0] state = 0;
// `freeze` holds every register still. A tag simulator's own load modulation
// is far deeper than the reader's, so letting it into this follower drags min and
// max to the extremes and the slicing levels derived from them no longer bracket
// the reader's much shallower signal - measured on a Proxmark simulating Hitag 2,
// that left the receiver deaf for ~1200 T0 after every answer. Holding the
// follower across our own transmission keeps it converged on the reader.
always @(posedge clk)
if (!freeze)
begin
case (state)
0: // initialize