Hardware interview practice
Cross a burst count with a Gray counter
A fast source may produce several events between destination edges. The destination needs the modulo-16 event delta, not one pulse per source event. Design the dual-clock SystemVerilog counter crossing.
Starting point
Question code
input logic src_clk, src_rst_n, src_event;
input logic dst_clk, dst_rst_n;
output logic [3:0] dst_delta;
output logic [3:0] dst_total;Reviewed example
Work through one case
Input
The prior settled destination sample is binary 3, the new synchronized Gray sample decodes to 6, and dst_total is 9.Expected output
dst_delta=3 and the registered dst_total becomes 12 modulo 16.Four-bit subtraction computes 6-3=3, and the destination accumulates that same delta on the sampling edge.
What to cover
Requirements
- Increment a 4-bit source binary count on each src_event and continuously derive its Gray-code value.
- Synchronize each Gray bit through two destination flip-flops, convert the synchronized Gray value back to binary, and compute modulo-16 delta from the prior destination sample.
- Accumulate dst_total modulo 16 and present dst_delta for each destination cycle; a stable source count yields delta zero after synchronization settles.
- Assume fewer than 8 source events occur between settled destination samples and constrain physical Gray-bus skew below one source period; both resets are asserted together and clear all state.
