// Testbench for exercise M4.
//
// Drives the generic design (adder8.sv) and the chip-level design
// (adder8_chips.sv) with the same inputs and compares both with integer
// addition.  Works with Icarus Verilog, Verilator (--binary) and Questa.
//
// Like the testbenches of Basics-Graphics-Music, it changes the inputs and
// checks the outputs only at rising clock edges.  It adds the examples of
// section 2 and then every pair of 8-bit numbers, 65536 in all; it prints
// only the examples and any error, and dumps the waveforms only at the
// start.

// Time units.  The design files have none: Yosys does not accept timeunit
// and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps
// in its own testbench.  This line sets the same units here and for the
// design files compiled after it, so tb.sv comes first in the file list.

`timescale 1ns / 1ps

module tb;

    logic       clk;
    logic [7:0] a, b;

    wire  [7:0] sum,   sum_chips;
    wire        carry, carry_chips;

    adder8 i_generic
    (
        .a     ( a     ),
        .b     ( b     ),
        .sum   ( sum   ),
        .carry ( carry )
    );

    adder8_chips i_chips
    (
        .a     ( a           ),
        .b     ( b           ),
        .sum   ( sum_chips   ),
        .carry ( carry_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: integer addition, the ninth bit is the carry

    wire [8:0] expected = 9' (a) + 9' (b);

    wire ok_generic = { carry,       sum       } === expected;
    wire ok_chips   = { carry_chips, sum_chips } === expected;

    //------------------------------------------------------------------------
    // Checking at every rising clock edge: the inputs still hold the values
    // the stimulus set at the previous edge, and the outputs have settled.

    // Initial values are fine in a testbench

    // verilator lint_off PROCASSINIT

    logic checking = 1'b0;  // Set by the stimulus after the first inputs
    logic printing = 1'b1;  // Cleared by the stimulus after the examples
    logic done     = 1'b0;  // Set by the stimulus before $finish

    int cycle    = 0;
    int n_checks = 0;
    int n_errors = 0;

    always @ (posedge clk)
        if (checking & ~ done)
        begin
            cycle    <= cycle + 1;
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1);

            if (printing | ~ ok_generic | ~ ok_chips)
                $display ("%5d  %3d + %3d  %b %b  %3d  %s  %s", cycle + 1, a, b, carry, sum, expected,
                    ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs

    initial
    begin
        $dumpfile ("tb.vcd");

        `ifdef __ICARUS__
            $dumpvars (0, tb);  // Without a scope Icarus also tries to dump $unit
        `else
            $dumpvars;
        `endif

        $display ("cycle    A     B    C8 S7..S0    A+B  generic  chips");

        checking <= '1;

        // The examples of section 2

        { a, b } <= { 8'd100, 8'd45  };  @ (posedge clk);
        { a, b } <= { 8'd200, 8'd45  };  @ (posedge clk);
        { a, b } <= { 8'd15,  8'd1   };  @ (posedge clk);
        { a, b } <= { 8'd255, 8'd1   };  @ (posedge clk);
        { a, b } <= { 8'd128, 8'd128 };  @ (posedge clk);
        { a, b } <= { 8'd85,  8'd170 };  @ (posedge clk);

        // Every pair, counting up: the low nibbles change fastest, so the
        // carry between the two adders flips often

        printing <= '0;

        for (int i = 0; i < 65536; i ++)
        begin
            { b, a } <= 16' (i);
            @ (posedge clk);

            if (i == 14)
                $dumpoff;
        end

        // Stop checking one clock edge before $finish, so that every
        // simulator reports the same number of checks

        done <= '1;
        @ (posedge clk);

        $finish;
    end

    //------------------------------------------------------------------------

    final
    begin
        if (n_errors == 0 && n_checks == 2 * (6 + 65536))
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks", n_errors, n_checks);
    end

endmodule
