Q142FreeSystemVerilog
Check divisibility by five on an LSB-first bit stream
Interview prompt
Question
Receive one LSB-first bit per accepted cycle and report whether each nonempty framed unsigned value is divisible by five. An abort input must discard a partial frame without producing a result. Transfers on this interface are counted only on rising edges with rst_n=1 and abort=0. A sampled abort cancels a pending result even when out_ready=1, so the consumer ignores the old out_valid on that sample. Reset asserts asynchronously; the platform releases reset safely in the local clock domain.
Candidate starting point
Implementation scaffold
module div5_stream (
input logic clk, rst_n, abort,
input logic in_valid, in_bit, in_last,
output logic in_ready,
output logic out_valid, div_by_5,
input logic out_ready
);
logic [2:0] rem_q, weight_q;
logic [3:0] sum;
always_comb begin : input_acceptance_and_sum
// TODO: qualify input readiness and form the next remainder sum.
end
always_ff @(posedge clk or negedge rst_n) begin : frame_and_result
// TODO: implement reset, sampled abort priority, result retirement and accepted-bit updates.
end
endmodule
Reviewed example
Trace one case
Input
Accept bits 1,0,1 with in_last on the third bit; later accept 1,1, abort, then start a new frame 0,1,1Expected output
First result div_by_5=1 because 101 LSB-first is 5; aborted frame has no result; final result is 0 because 011 LSB-first is 6LSB-first framing requires tracking the current power-of-two weight modulo five. Abort explicitly resets that weight as well as the accumulated remainder.
What to cover
Requirements
- Treat the first accepted bit as position zero, with remainder r=0 and current weight w=1.
- On each handshake update r=(r+in_bit*w)%5 and w=(2*w)%5; include the in_last bit in the published result.
- Hold out_valid and div_by_5 stable under output backpressure and accept no next-frame input until the result is consumed.
- Give abort priority over input: it discards partial state, clears any pending output, restores r=0/w=1, and emits no result.
