// Testbench for exercise B1.
//
// Runs the generic design (basic_gates.sv) and the chip-level design
// (basic_gates_chips.sv) side by side and compares both with the truth
// tables of the gates.  Works with Icarus Verilog, Verilator (--binary)
// and Questa.
//
// The gates have 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 A and B after one edge and checks the outputs at the next.

// 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, b;

    wire  a_and_b, a_and_b_chips;
    wire  a_or_b,  a_or_b_chips;
    wire  a_xor_b, a_xor_b_chips;
    wire  not_a,   not_a_chips;
    wire  not_b,   not_b_chips;

    basic_gates i_generic
    (
        .a       ( a       ),
        .b       ( b       ),
        .a_and_b ( a_and_b ),
        .a_or_b  ( a_or_b  ),
        .a_xor_b ( a_xor_b ),
        .not_a   ( not_a   ),
        .not_b   ( not_b   )
    );

    basic_gates_chips i_chips
    (
        .a       ( a             ),
        .b       ( b             ),
        .a_and_b ( a_and_b_chips ),
        .a_or_b  ( a_or_b_chips  ),
        .a_xor_b ( a_xor_b_chips ),
        .not_a   ( not_a_chips   ),
        .not_b   ( not_b_chips   )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the truth tables, written out row by row.
    // Bits: AND, OR, XOR, NOT A, NOT B.

    function automatic logic [4:0] expected (input logic in_a, input logic in_b);
        case ({ in_a, in_b })
        2'b00:   return 5'b000_11;
        2'b01:   return 5'b011_10;
        2'b10:   return 5'b011_01;
        2'b11:   return 5'b110_00;
        default: return 5'bxxx_xx;
        endcase
    endfunction

    wire [4:0] out       = { a_and_b,       a_or_b,       a_xor_b,       not_a,       not_b       };
    wire [4:0] out_chips = { a_and_b_chips, a_or_b_chips, a_xor_b_chips, not_a_chips, not_b_chips };

    //------------------------------------------------------------------------
    // Checking at every rising clock edge: A and B 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 [3:0] rows_seen = '0;  // Which rows of the truth table were checked

    always @ (posedge clk)
        if (checking & ~ done)
        begin
            cycle     <= cycle + 1;
            n_checks  <= n_checks + 2;
            n_errors  <= n_errors + (out === expected (a, b) ? 0 : 1) + (out_chips === expected (a, b) ? 0 : 1);
            rows_seen <= rows_seen | 4'b1 << { a, b };

            $display ("%5d  %b %b   %b   %b   %b    %b    %b     %s  %s",
                cycle + 1, a, b, a_and_b, a_or_b, a_xor_b, not_a, not_b,
                out       === expected (a, b) ? "ok     " : "WRONG  ",
                out_chips === expected (a, b) ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving A and B: every row of the truth table, twice, in two orders

    wire [15:0] order = 16'b00_01_10_11_11_10_00_01;  // { A, B } for each step

    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  AND  OR XOR NOT A NOT B  generic  chips");

        checking <= '1;

        for (int i = 0; i < 8; i ++)
        begin
            { a, b } <= order [15 - 2 * i -: 2];
            @ (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 == 4'b1111)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, truth table rows seen %b", n_errors, n_checks, rows_seen);
    end

endmodule
