// tb_seq.sv // Self-checking testbench for counter_mod, shift_reg, edge_detect and debounce. // Run: iverilog -g2012 -o sim tb_seq.sv counter_mod.sv shift_reg.sv \ // edge_detect.sv debounce.sv && vvp sim module tb_seq; logic clk = 1'b0; logic rst = 1'b1; int errors = 0; always #5 clk = ~clk; // ---- counter_mod, N = 6 ---- logic en = 1'b0; logic [2:0] count; logic wrap; counter_mod #(.N(6)) u_cnt (.clk(clk), .rst(rst), .en(en), .count(count), .wrap(wrap)); // ---- shift_reg ---- logic sr_en = 1'b0, sr_din = 1'b0; logic [7:0] sr_q; localparam logic [7:0] SR_PATTERN = 8'b1011_0010; shift_reg #(.WIDTH(8)) u_sr (.clk(clk), .en(sr_en), .din(sr_din), .q(sr_q)); // ---- edge_detect ---- logic sig = 1'b0, rise, fall; edge_detect u_ed (.clk(clk), .sig(sig), .rise(rise), .fall(fall)); // ---- debounce, STABLE_CYCLES = 8 ---- logic btn_raw = 1'b0, btn; debounce #(.STABLE_CYCLES(8)) u_db (.clk(clk), .rst(rst), .btn_raw(btn_raw), .btn(btn)); int rise_count = 0, fall_count = 0, btn_changes = 0; logic btn_d = 1'b0; always @(posedge clk) begin if (rise) rise_count++; if (fall) fall_count++; btn_d <= btn; if (!rst && btn !== btn_d) btn_changes++; end initial begin repeat (2) @(posedge clk); @(negedge clk) rst = 1'b0; // counter_mod: enable on every other cycle for 30 enabled cycles. for (int i = 1; i <= 30; i++) begin @(negedge clk) en = 1'b1; #1; if (wrap !== (count == 3'd5)) begin $display("FAIL counter_mod wrap at count %0d", count); errors++; end @(negedge clk) en = 1'b0; if (count !== 3'(i % 6)) begin $display("FAIL counter_mod: count %0d after %0d enables", count, i); errors++; end end // shift_reg: shift in SR_PATTERN, MSB first. for (int i = 7; i >= 0; i--) begin @(negedge clk) begin sr_en = 1'b1; sr_din = SR_PATTERN[i]; end end @(negedge clk) sr_en = 1'b0; if (sr_q !== SR_PATTERN) begin $display("FAIL shift_reg: q = %b", sr_q); errors++; end // edge_detect: a 5-cycle high pulse gives exactly one rise and one fall. @(negedge clk) sig = 1'b1; repeat (5) @(negedge clk); sig = 1'b0; repeat (3) @(negedge clk); if (rise_count != 1 || fall_count != 1) begin $display("FAIL edge_detect: %0d rises, %0d falls", rise_count, fall_count); errors++; end // debounce: bounce for a while (no run longer than 5 cycles), then hold. // The output must change exactly once on press and once on release. for (int i = 0; i < 6; i++) begin btn_raw = 1'b1; repeat (2 + i % 3) @(negedge clk); btn_raw = 1'b0; repeat (1 + i % 2) @(negedge clk); end if (btn !== 1'b0) begin $display("FAIL debounce: output followed the bounce"); errors++; end btn_raw = 1'b1; repeat (20) @(negedge clk); if (btn !== 1'b1) begin $display("FAIL debounce: press not accepted"); errors++; end for (int i = 0; i < 4; i++) begin btn_raw = 1'b0; repeat (3) @(negedge clk); btn_raw = 1'b1; repeat (2) @(negedge clk); end btn_raw = 1'b0; repeat (20) @(negedge clk); if (btn !== 1'b0 || btn_changes != 2) begin $display("FAIL debounce: btn = %b after release, %0d changes", btn, btn_changes); errors++; end if (errors == 0) $display("PASS"); else $display("FAILED with %0d errors", errors); $finish; end endmodule