Q056FreeSystemVerilog
Validate nested protocol delimiters
Interview prompt
Question
Consume a framed stream of two delimiter kinds: token=00 is OPEN_A, 01 is CLOSE_A, 10 is OPEN_B, and 11 is CLOSE_B. Report balanced nesting and the first error. Use error_code 0=NONE, 1=MISMATCH, 2=UNDERFLOW, 3=OVERFLOW, and 4=UNFINISHED_OPEN.
Candidate starting point
Implementation scaffold
module delimiter_checker (
input logic clk,
input logic rst_n,
input logic token_valid,
input logic token_last,
input logic [1:0] token,
output logic token_ready,
input logic result_ready,
output logic result_valid,
output logic balanced,
output logic [2:0] error_code
);
localparam logic [2:0] ERR_NONE = 3'd0;
localparam logic [2:0] ERR_MISMATCH = 3'd1;
localparam logic [2:0] ERR_UNDERFLOW = 3'd2;
localparam logic [2:0] ERR_OVERFLOW = 3'd3;
localparam logic [2:0] ERR_OPEN = 3'd4;
logic stack_q [8];
logic stack_next [8];
logic [3:0] depth_q, depth_next;
logic [2:0] error_q, error_next, final_error;
assign token_ready = !result_valid;
always_comb begin : decode_token
// TODO: Implement decode_token using the supplied state and interface.
end
always_ff @(posedge clk) begin : capture_parser_and_result
// TODO: Implement capture_parser_and_result using the supplied state and interface.
end
endmoduleReviewed example
Trace one case
Input
Accepted tokens are OPEN_A, OPEN_B, CLOSE_B, CLOSE_A, with token_last on CLOSE_A. Insert one cycle with token_valid=0 between CLOSE_B and CLOSE_A.Expected output
At the final accepted token, balanced=1, error_code=0, and result_valid=1. These fields remain stable while result_ready=0.The valid-low gap leaves the stack at depth one. The final CLOSE_A is included before the result is classified, so the updated stack is empty. This reference does not apply input backpressure inside a frame; it blocks input while holding a completed result.
What to cover
Requirements
- Assume each frame contains one through 16 accepted tokens and ends with token_last. Enforce a nesting depth of at most eight.
- Advance parser state only when token_valid && token_ready. Keep consuming after the first error, preserving its error code.
- A close pops the most recent open if present and reports a type mismatch when needed. Opening at depth eight reports overflow without pushing; closing at depth zero reports underflow. At the final token, report unfinished-open only if no earlier error exists and the updated stack is nonempty.
- Hold result_valid, balanced, and error_code until result_ready accepts them. Block the next frame while a result is pending; producer valid-low gaps within a frame do not advance the parser.
