// Testbench for exercise BB4c.
//
// Runs the generic design (mux_2_triple.sv) and the chip-level design
// (mux_2_triple_chips.sv) side by side and compares both with
// for each switch X or Y as its select says, or z while INH = 1.  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 1024 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 [2:0] x_ch;
    logic [2:0] y_ch;
    logic [2:0] sel;
    logic       inh;

    wire  [2:0] com, com_chips;

    mux_2_triple i_generic
    (
        .x_ch ( x_ch ),
        .y_ch ( y_ch ),
        .sel  ( sel  ),
        .inh  ( inh  ),
        .com  ( com  )
    );

    mux_2_triple_chips i_chips
    (
        .x_ch ( x_ch      ),
        .y_ch ( y_ch      ),
        .sel  ( sel       ),
        .inh  ( inh       ),
        .com  ( com_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: for each switch X or Y as its select says, or z while INH = 1.
    // Bits, from the left: com.

    function automatic logic [2:0] expected
    (
        input logic [2:0] in_x_ch,
        input logic [2:0] in_y_ch,
        input logic [2:0] in_sel,
        input logic       in_inh
    );
        logic [2:0] y;

        for (int i = 0; i < 3; i ++)
            y [i] = in_sel [i] ? in_y_ch [i] : in_x_ch [i];

        if (in_inh)
            y = 3'bzzz;

        return y;
    endfunction

    wire [2:0] out       = { com };
    wire [2:0] out_chips = { com_chips };
    wire [2:0] out_model = expected (x_ch, y_ch, sel, inh);

    //------------------------------------------------------------------------
    // 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 [1023: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 | 1024'b1 << { x_ch, y_ch, sel, inh };

            // Print the rows with the data wires as on the breadboard, and every error

            if (x_ch == 3'b010 & y_ch == 3'b101 | out !== out_model | out_chips !== out_model)
                $display ("%5d  %b     %b     %b  %b    %b       %s  %s",
                    cycle + 1, x_ch, y_ch, sel, inh, com,
                    out       === out_model ? "ok     " : "WRONG  ",
                    out_chips === out_model ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs: all combinations, counting up from the data wires as on the
    // breadboard, 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  CX..AX  CY..AY  CBA  INH  COM C..A  generic  chips");

        checking <= '1;

        for (int i = 0; i < 1024; i ++)
        begin
            { x_ch, y_ch, inh, sel } <= 10' (i) ^ 10'h150;
            @ (posedge clk);
        end

        for (int i = 0; i < 1024; i ++)
        begin
            { x_ch, y_ch, inh, sel } <= 10' (i ^ (i >> 1)) ^ 10'h150;
            @ (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), 1024);
    end

endmodule
