// Testbench for exercise BB2.
//
// Runs the generic design (serializer.sv) and the chip-level design
// (serializer_chips.sv) side by side and compares both with a reference
// model.  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.  The shift register has no reset, so
// the check starts at the first load.  The testbench shifts a whole value
// out and the zeros after it, loads in the middle of shifting, and holds
// LOAD for two edges.

// 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       load;
    logic [7:0] p;

    wire        q6, q6_chips;
    wire        q7, q7_chips;
    wire        q8, q8_chips;

    serializer i_generic
    (
        .clk  ( clk  ),
        .load ( load ),
        .p    ( p    ),
        .q6   ( q6   ),
        .q7   ( q7   ),
        .q8   ( q8   )
    );

    serializer_chips i_chips
    (
        .clk  ( clk      ),
        .load ( load     ),
        .p    ( p        ),
        .q6   ( q6_chips ),
        .q7   ( q7_chips ),
        .q8   ( q8_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

    //------------------------------------------------------------------------
    // Reference model: the eight stages as a vector, model [7] is stage 8.
    // loaded is 1 from the first load on; before it the stages are unknown.

    // Initial values are fine in a testbench
    // verilator lint_off PROCASSINIT

    logic [7:0] model;
    logic       loaded = 1'b0;

    always @ (posedge clk)
    begin
        if (load)
            model <= p;
        else
            model <= { model [6:0], 1'b0 };

        if (load)
            loaded <= 1'b1;
    end

    wire [2:0] expected  = model [7:5];
    wire [2:0] out       = { q8,       q7,       q6       };
    wire [2:0] out_chips = { q8_chips, q7_chips, q6_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 done = 1'b0;  // Set by the stimulus before $finish

    int cycle    = 0;
    int n_checks = 0;
    int n_errors = 0;
    int n_loads  = 0;
    int n_zeros  = 0;   // Cycles in which a zero shifted in after the value reached Q8

    always @ (posedge clk)
    begin
        cycle <= cycle + 1;

        if (loaded & ~ done)
        begin
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (out === expected ? 0 : 1) + (out_chips === expected ? 0 : 1);

            if (~ load & model == 8'b0)
                n_zeros <= n_zeros + 1;
        end

        if (load & ~ done)
            n_loads <= n_loads + 1;

        if (~ done)
            $display ("%5d   %b    %b   %b  %b  %b   %b    %s  %s",
                cycle + 1, load, p, q8, q7, q6, loaded ? expected : 3'bxxx,
                ~ loaded ? "-      " : out       === expected ? "ok     " : "WRONG  ",
                ~ loaded ? "-"       : out_chips === expected ? "ok   "   : "WRONG");
    end

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

    task automatic load_value (input logic [7:0] value, input int n_shifts);
        p    <= value;
        load <= 1'b1;
        @ (posedge clk);

        load <= 1'b0;
        repeat (n_shifts) @ (posedge clk);
    endtask

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

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

        $display ("cycle  LOAD  P8...P1   Q8 Q7 Q6  model  generic  chips");

        // After power-up the stages are unknown, as on the breadboard

        p    <= 8'b0100_1011;
        load <= 1'b0;
        repeat (2) @ (posedge clk);

        load_value (8'b0100_1011, 10);  // The value on the wires of the breadboard, and two zeros after it
        load_value (8'b1010_0101,  4);  // Load again in the middle of shifting
        load_value (8'b1111_1111,  3);

        // LOAD held for two edges loads twice

        p    <= 8'b0000_0001;
        load <= 1'b1;
        repeat (2) @ (posedge clk);
        load <= 1'b0;
        repeat (9) @ (posedge clk);

        // 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 && n_loads >= 5 && n_zeros >= 2)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d loads, %0d zeros", n_errors, n_checks, n_loads, n_zeros);
    end

endmodule
