// Testbench for exercise B3d.
//
// Runs the generic design (gates_8_input.sv) and the chip-level design
// (gates_8_input_chips.sv) side by side and compares both with
// a comparison of the inputs with all ones and all zeros.  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 4096 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       a;
    logic       b;
    logic       c;
    logic       d;
    logic [3:0] rest_and;
    logic [3:0] rest_or;

    wire  y_and,  y_and_chips;
    wire  y_nand, y_nand_chips;
    wire  y_or,   y_or_chips;
    wire  y_nor,  y_nor_chips;

    gates_8_input i_generic
    (
        .a        ( a        ),
        .b        ( b        ),
        .c        ( c        ),
        .d        ( d        ),
        .rest_and ( rest_and ),
        .rest_or  ( rest_or  ),
        .y_and    ( y_and    ),
        .y_nand   ( y_nand   ),
        .y_or     ( y_or     ),
        .y_nor    ( y_nor    )
    );

    gates_8_input_chips i_chips
    (
        .a        ( a            ),
        .b        ( b            ),
        .c        ( c            ),
        .d        ( d            ),
        .rest_and ( rest_and     ),
        .rest_or  ( rest_or      ),
        .y_and    ( y_and_chips  ),
        .y_nand   ( y_nand_chips ),
        .y_or     ( y_or_chips   ),
        .y_nor    ( y_nor_chips  )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: a comparison of the inputs with all ones and all zeros.
    // Bits, from the left: y_and, y_nand, y_or, y_nor.

    function automatic logic [3:0] expected
    (
        input logic       in_a,
        input logic       in_b,
        input logic       in_c,
        input logic       in_d,
        input logic [3:0] in_rest_and,
        input logic [3:0] in_rest_or
    );
        // The 4068 sees A-D and its own E-H, the 4078 A-D and its own E-H

        logic [7:0] and_inputs = { in_a, in_b, in_c, in_d, in_rest_and };
        logic [7:0] or_inputs  = { in_a, in_b, in_c, in_d, in_rest_or };

        return { and_inputs == 8'hff, and_inputs != 8'hff,
                 or_inputs  != 8'h00, or_inputs  == 8'h00 };
    endfunction

    wire [3:0] out       = { y_and, y_nand, y_or, y_nor };
    wire [3:0] out_chips = { y_and_chips, y_nand_chips, y_or_chips, y_nor_chips };
    wire [3:0] out_model = expected (a, b, c, d, rest_and, rest_or);

    //------------------------------------------------------------------------
    // 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 [4095: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 | 4096'b1 << { a, b, c, d, rest_and, rest_or };

            // Print the rows with E-H as on the breadboard, and every error

            if (rest_and == 4'hf & rest_or == 4'h0 | out !== out_model | out_chips !== out_model)
                $display ("%5d  %b  %b  %b  %b  %b      %b      %b    %b     %b   %b    %s  %s",
                    cycle + 1, a, b, c, d, rest_and, rest_or, y_and, y_nand, y_or, y_nor,
                    out       === out_model ? "ok     " : "WRONG  ",
                    out_chips === out_model ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs: all combinations, counting up from E-H as on the
    // breadboard (1 for the 4068, 0 for the 4078), 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  A  B  C  D  E-H 4068  E-H 4078  AND  NAND  OR  NOR  generic  chips");

        checking <= '1;

        for (int i = 0; i < 4096; i ++)
        begin
            { rest_and, rest_or, a, b, c, d } <= 12' (i) ^ 12'hf00;
            @ (posedge clk);
        end

        for (int i = 0; i < 4096; i ++)
        begin
            { rest_and, rest_or, a, b, c, d } <= 12' (i ^ (i >> 1)) ^ 12'hf00;
            @ (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), 4096);
    end

endmodule
