// Self-checking testbench for accum8. // Run with Icarus Verilog: // $ iverilog -g2012 -o sim tb_accum8.sv accum8.sv && vvp sim // Run with Verilator 5: // $ verilator --binary -j 0 --top tb_accum8 tb_accum8.sv accum8.sv // $ ./obj_dir/Vtb_accum8 // Plusargs: +seed= picks the random seed, +dump writes tb_accum8.vcd. // Add --trace to the Verilator command line if you want +dump to work there. `timescale 1ns / 1ps module tb_accum8; // --------------------------------------------------------------- // DUT signals and instance // --------------------------------------------------------------- logic clk; logic rst; logic clr; logic en; logic [7:0] din; logic [7:0] acc; logic carry; accum8 dut ( .clk(clk), .rst(rst), .clr(clr), .en(en), .din(din), .acc(acc), .carry(carry) ); // --------------------------------------------------------------- // Clock: 100 MHz // --------------------------------------------------------------- initial begin clk = 1'b0; forever #5 clk = ~clk; end // --------------------------------------------------------------- // Reference model state and scoreboard counters // --------------------------------------------------------------- logic [7:0] model_acc; logic model_carry; int errors; int checks; integer seed; // updated by every $random(seed) call integer seed0; // the starting seed, for the report // Compare the DUT with the model. Called mid-cycle, after the edge. task automatic check_outputs(); checks++; assert (acc === model_acc && carry === model_carry) else begin errors++; $error("t=%0t: acc=%h carry=%b, expected acc=%h carry=%b", $time, acc, carry, model_acc, model_carry); end endtask // Hold reset for two cycles, then check that the outputs are zero. task automatic do_reset(); rst = 1'b1; clr = 1'b0; en = 1'b0; din = 8'h00; repeat (2) @(negedge clk); rst = 1'b0; model_acc = 8'h00; model_carry = 1'b0; if (acc !== 8'h00 || carry !== 1'b0) $fatal(1, "reset failed: acc=%h carry=%b", acc, carry); endtask // One clock cycle: drive inputs on the falling edge, let the DUT // and the model react to the rising edge, check on the next falling edge. task automatic step(input logic c, input logic e, input logic [7:0] d); clr = c; en = e; din = d; @(posedge clk); if (c) {model_carry, model_acc} = 9'h000; else if (e) {model_carry, model_acc} = {1'b0, model_acc} + {1'b0, d}; @(negedge clk); check_outputs(); endtask // --------------------------------------------------------------- // Concurrent assertions (SVA). Icarus Verilog 13 cannot parse // these, so they are compiled only by other simulators. // --------------------------------------------------------------- `ifndef __ICARUS__ // The cycle after clr, both outputs are zero. a_clr: assert property (@(posedge clk) disable iff (rst) clr |=> (acc == 8'h00 && !carry)) else $error("acc not cleared after clr"); // With clr and en both low, the accumulator holds its value. a_hold: assert property (@(posedge clk) disable iff (rst) (!clr && !en) |=> $stable(acc)) else $error("acc changed while en was low"); `endif // --------------------------------------------------------------- // Test sequence // --------------------------------------------------------------- initial begin errors = 0; checks = 0; if (!$value$plusargs("seed=%d", seed0)) seed0 = 1; seed = seed0; if ($test$plusargs("dump")) begin $dumpfile("tb_accum8.vcd"); $dumpvars(0, tb_accum8); end @(negedge clk); do_reset(); // Directed tests: the cases most likely to hide a bug. step(1'b0, 1'b1, 8'h01); // 0 + 1 step(1'b0, 1'b1, 8'h01); // 1 + 1 step(1'b0, 1'b0, 8'h55); // en low: hold, din ignored step(1'b0, 1'b1, 8'hFE); // 2 + FE = 100: carry out, acc = 00 step(1'b0, 1'b1, 8'h80); step(1'b0, 1'b1, 8'h80); // 80 + 80 = 100 step(1'b0, 1'b1, 8'hFF); // carry clears on the next add step(1'b1, 1'b1, 8'h12); // clr wins over en // Random tests: en high 3 cycles in 4, clr about 1 cycle in 32. repeat (5000) begin step(($random(seed) & 31) == 0, ($random(seed) & 3) != 0, 8'($random(seed))); end if (errors == 0) begin $display("PASS: %0d checks, seed %0d", checks, seed0); $finish; end else begin $fatal(1, "FAIL: %0d errors in %0d checks, seed %0d", errors, checks, seed0); end end // Watchdog: stop a hung simulation instead of running forever. initial begin #1ms; $fatal(1, "timeout"); end endmodule