// Testbench for exercise BB9.
//
// Runs the generic design (register_file.sv) and the chip-level design
// (register_file_chips.sv) side by side and compares both with a reference
// model of four words.  Works with Icarus Verilog, Verilator (--binary) and
// Questa.
//
// Like the testbenches of Basics-Graphics-Music, it drives and samples
// signals only at rising clock edges.  It plays the writes of section 2,
// reads every pair of words through the two ports, changes the data and
// the address while WRITE is released, and ends with pseudo-random writes
// and reads.  The 40108 has no reset, so a word is checked only after it
// was written.

// 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       we      = 1'b0;
    logic [1:0] waddr   = '0;
    logic [3:0] wdata   = '0;
    logic [1:0] raddr_a = 2'd0;
    logic [1:0] raddr_b = 2'd1;

    wire  [3:0] rdata_a, rdata_a_chips;
    wire  [3:0] rdata_b, rdata_b_chips;

    register_file i_generic
    (
        .clk     ( clk     ),
        .we      ( we      ),
        .waddr   ( waddr   ),
        .wdata   ( wdata   ),
        .raddr_a ( raddr_a ),
        .rdata_a ( rdata_a ),
        .raddr_b ( raddr_b ),
        .rdata_b ( rdata_b )
    );

    register_file_chips i_chips
    (
        .clk     ( clk           ),
        .we      ( we            ),
        .waddr   ( waddr         ),
        .wdata   ( wdata         ),
        .raddr_a ( raddr_a       ),
        .raddr_b ( raddr_b       ),
        .rdata_a ( rdata_a_chips ),
        .rdata_b ( rdata_b_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns.
    // On the breadboard one period is about a second.

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Initial values are fine in a testbench

    // verilator lint_off PROCASSINIT

    //------------------------------------------------------------------------
    // Reference model: four words, and which of them were written

    logic [3:0] model [0:3];
    logic [3:0] known = '0;

    always @ (posedge clk)
        if (we)
        begin
            model [waddr] <= wdata;
            known [waddr] <= 1'b1;
        end

    wire ok_a       = ! known [raddr_a] || rdata_a       === model [raddr_a];
    wire ok_b       = ! known [raddr_b] || rdata_b       === model [raddr_b];
    wire ok_a_chips = ! known [raddr_a] || rdata_a_chips === model [raddr_a];
    wire ok_b_chips = ! known [raddr_b] || rdata_b_chips === model [raddr_b];

    wire ok_generic = ok_a && ok_b;
    wire ok_chips   = ok_a_chips && ok_b_chips;

    //------------------------------------------------------------------------
    // Checking at every rising clock edge.  At the edge both designs and the
    // model still show the clock cycle that ends there, the same values that
    // a flip-flop would sample.

    logic started = 1'b0;  // There is no reset: set by the stimulus
    logic done    = 1'b0;  // Set by the stimulus before $finish

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

    logic [7:0] seen = '0;  // Word k read through port A (bit k) and B (bit 4 + k) after it was written

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

            if (known [raddr_a])
                seen [3' (raddr_a)] <= 1'b1;

            if (known [raddr_b])
                seen [3' (4 + raddr_b)] <= 1'b1;

            $display ("%5d  %b   %0d    %2d  | %0d  %2d  | %0d  %2d  | %h %h %h %h  %s  %s",
                cycle + 1, we, waddr, wdata, raddr_a, rdata_a, raddr_b, rdata_b,
                model [0], model [1], model [2], model [3],
                ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end
    end

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

    // One press of WRITE: hold it for one clock edge, then release it

    task automatic write_word (input logic [1:0] address, input logic [3:0] value,
                               input logic [1:0] read_a, input logic [1:0] read_b);
        we      <= 1'b1;
        waddr   <= address;
        wdata   <= value;
        raddr_a <= read_a;
        raddr_b <= read_b;
        @ (posedge clk);
        we      <= 1'b0;
        @ (posedge clk);
    endtask

    logic [31:0] seed = 32'd7;

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

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

        $display ("cycle  WE  W    D   | RA  A  | RB  B  | words 0-3  generic  chips");

        // Before the first write the words are unknown, as after power-up

        repeat (2) @ (posedge clk);
        started <= 1'b1;

        // The example of section 2

        write_word (0,  5, 0, 1);
        write_word (1, 10, 0, 1);
        write_word (2,  3, 2, 1);
        write_word (3, 12, 2, 3);

        // D changes, but WRITE is released: nothing is written

        wdata   <= 15;
        raddr_a <= 3;
        raddr_b <= 0;
        @ (posedge clk);
        @ (posedge clk);

        write_word (0,  9, 0, 3);

        // Every pair of read addresses

        for (int k = 0; k < 16; k ++)
        begin
            raddr_a <= 2' (k);
            raddr_b <= 2' (k >> 2);
            @ (posedge clk);
        end

        // Pseudo-random writes and reads, the same in every simulator

        for (int i = 0; i < 120; i ++)
        begin
            seed = seed * 32'd1664525 + 32'd1013904223;

            we      <= seed [31] & seed [30];
            waddr   <= seed [29:28];
            wdata   <= seed [27:24];
            raddr_a <= seed [23:22];
            raddr_b <= seed [21:20];

            @ (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 && seen == 8'hff)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, words read %b", n_errors, n_checks, seen);
    end

endmodule
