// Testbench for exercise M3.
//
// Runs the generic design (two_bit_adder.sv) and the chip-level design
// (two_bit_adder_chips.sv) side by side and compares both with
// the sum of A1 A0 and B1 B0 as a 3-bit number.  Works with Icarus Verilog, Verilator (--binary)
// and Questa.
//
// The circuit has no clock, but like the testbenches of Basics-Graphics-Music
// this one drives and checks signals only at rising edges of its own clock:
// it changes the inputs after one edge and checks the outputs at the next.
// It tries all 16 input combinations twice: counting up, and then in
// Gray code order, where one input changes at a time, as when a student
// presses or releases one button.

// 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 a1;
    logic a0;
    logic b1;
    logic b0;

    wire  s2, s2_chips;
    wire  s1, s1_chips;
    wire  s0, s0_chips;

    two_bit_adder i_generic
    (
        .a1 ( a1 ),
        .a0 ( a0 ),
        .b1 ( b1 ),
        .b0 ( b0 ),
        .s2 ( s2 ),
        .s1 ( s1 ),
        .s0 ( s0 )
    );

    two_bit_adder_chips i_chips
    (
        .a1 ( a1       ),
        .a0 ( a0       ),
        .b1 ( b1       ),
        .b0 ( b0       ),
        .s2 ( s2_chips ),
        .s1 ( s1_chips ),
        .s0 ( s0_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the sum of A1 A0 and B1 B0 as a 3-bit number.
    // Bits, from the left: s2, s1, s0.

    function automatic logic [2:0] expected
    (
        input logic in_a1,
        input logic in_a0,
        input logic in_b1,
        input logic in_b0
    );
        logic [2:0] sum;

        sum = 3' ({ in_a1, in_a0 }) + 3' ({ in_b1, in_b0 });

        return sum;  // { s2, s1, s0 }
    endfunction

    wire [2:0] out       = { s2, s1, s0 };
    wire [2:0] out_chips = { s2_chips, s1_chips, s0_chips };
    wire [2:0] out_model = expected (a1, a0, b1, b0);

    //------------------------------------------------------------------------
    // 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 done     = 1'b0;  // Set by the stimulus before $finish

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

    logic [15:0] rows_seen = '0;  // Which input combinations were checked

    always @ (posedge clk)
        if (checking & ~ done)
        begin
            cycle     <= cycle + 1;
            n_checks  <= n_checks + 2;
            n_errors  <= n_errors + (out === out_model ? 0 : 1) + (out_chips === out_model ? 0 : 1);
            rows_seen <= rows_seen | 16'b1 << { a1, a0, b1, b0 };

            $display ("%5d  %b   %b   %b   %b   %b   %b   %b   %s  %s",
                cycle + 1, a1, a0, b1, b0, s2, s1, s0,
                out       === out_model ? "ok     " : "WRONG  ",
                out_chips === out_model ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs: all combinations, counting up, then in Gray code order

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

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

        $display ("cycle  A1  A0  B1  B0  S2  S1  S0  generic  chips");

        checking <= '1;

        for (int i = 0; i < 16; i ++)
        begin
            { a1, a0, b1, b0 } <= 4' (i);
            @ (posedge clk);
        end

        for (int i = 0; i < 16; i ++)
        begin
            { a1, a0, b1, b0 } <= 4' (i ^ (i >> 1));
            @ (posedge clk);
        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 && & rows_seen)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d of %0d input combinations seen",
                n_errors, n_checks, $countones (rows_seen), 16);
    end

endmodule
