+ end
+
+ // detect modulation signal: if modulating, there must be a falling and a rising edge
+ if (rx_mod_falling_edge_max > 6 && rx_mod_rising_edge_max > 6)
+ curbit <= 1'b1; // modulation
+ else
+ curbit <= 1'b0; // no modulation
+
+
+ // store previous samples for filtering and edge detection:
+ adc_d_4 <= adc_d_3;
+ adc_d_3 <= adc_d_2;
+ adc_d_2 <= adc_d_1;
+ adc_d_1 <= adc_d;
+
+
+ // Relevant for TAGSIM_MOD only (timing the Tag's answer. See above)
+ // When we see end of a modulation and we are emulating a Tag, start fdt_counter.
+ // Reset fdt_counter when modulation is detected.
+ if(~after_hysteresis /* && mod_sig_buf_empty */ && mod_type == `TAGSIM_LISTEN)
+ begin
+ fdt_counter <= 11'd0;
+ fdt_elapsed = 1'b0;
+ fdt_indicator <= 1'b0;
+ temp_buffer_reset = 1'b0;
+ mod_sig_ptr <= 5'b00000;
+ mod_sig = 1'b0;
+ end
+
+
+ if(negedge_cnt[3:0] == 4'd1)
+ begin
+ // What do we communicate to the ARM
+ if(mod_type == `TAGSIM_LISTEN)
+ sendbit = after_hysteresis;
+ else if(mod_type == `TAGSIM_MOD)
+ /* if(fdt_counter > 11'd772) sendbit = mod_sig_coil; // huh?
+ else */
+ sendbit = fdt_indicator;
+ else if (mod_type == `READER_LISTEN)
+ sendbit = curbit;
+ else
+ sendbit = 1'b0;
+ end
+
+
+
+ // check timing of a falling edge in reader signal
+ if (pre_after_hysteresis && ~after_hysteresis)
+ reader_falling_edge_time[3:0] <= negedge_cnt[3:0];
+
+
+
+ // sync clock to external reader's clock:
+ if (negedge_cnt[3:0] == 4'd13 && (mod_type == `SNIFFER || mod_type == `TAGSIM_MOD || mod_type == `TAGSIM_LISTEN))
+ begin
+ // adjust clock if necessary:
+ if (reader_falling_edge_time < 4'd8 && reader_falling_edge_time > 4'd1)