Workspace with comment-preserving parser (vlog_parser), prettier-style document IR + formatting rules (vlog_formatter), and CLI (vfmt). Includes snapshot/idempotency tests and a TODO backlog of practices adopted from ruff and Verible. Co-Authored-By: Claude Fable 5 <[email protected]>
66 lines
1.4 KiB
Systemverilog
66 lines
1.4 KiB
Systemverilog
// Handshake FSM with a testbench-style checker task.
|
|
module fsm #(
|
|
parameter int TIMEOUT = 100
|
|
) (
|
|
input logic clk,
|
|
input logic rst_n,
|
|
input logic req_i,
|
|
input logic ack_i,
|
|
output logic grant_o,
|
|
output logic error_o
|
|
);
|
|
|
|
typedef enum logic [1:0] {
|
|
IDLE, // waiting for request
|
|
GRANT, // request granted
|
|
WAIT, // waiting for ack
|
|
ERROR // timeout
|
|
} state_e;
|
|
|
|
state_e state_q, state_d;
|
|
logic [$clog2(TIMEOUT)-1:0] timer_q, timer_d;
|
|
|
|
always_comb begin
|
|
state_d = state_q;
|
|
timer_d = timer_q;
|
|
unique case (state_q)
|
|
IDLE: begin
|
|
timer_d = '0;
|
|
if (req_i) state_d = GRANT;
|
|
end
|
|
GRANT: state_d = WAIT;
|
|
WAIT: begin
|
|
timer_d = timer_q + 1'b1;
|
|
if (ack_i) state_d = IDLE;
|
|
else if (timer_q == TIMEOUT[$clog2(TIMEOUT)-1:0]) state_d = ERROR;
|
|
end
|
|
default: state_d = IDLE;
|
|
endcase
|
|
end
|
|
|
|
always_ff @(posedge clk or negedge rst_n) begin
|
|
if (!rst_n) begin
|
|
state_q <= IDLE;
|
|
timer_q <= '0;
|
|
end else begin
|
|
state_q <= state_d;
|
|
timer_q <= timer_d;
|
|
end
|
|
end
|
|
|
|
assign grant_o = state_q == GRANT || state_q == WAIT;
|
|
assign error_o = state_q == ERROR;
|
|
|
|
task automatic wait_for_grant(input int max_cycles, output bit ok);
|
|
ok = 1'b0;
|
|
for (int unsigned n = 0; n < max_cycles; n++) begin
|
|
@(posedge clk);
|
|
if (grant_o) begin
|
|
ok = 1'b1;
|
|
break;
|
|
end
|
|
end
|
|
endtask
|
|
|
|
endmodule
|