// Testbench for exercise B9.
//
// Runs the generic design (adder_display.sv) and the chip-level design
// (adder_display_chips.sv) side by side for all 256 pairs of A and B and
// compares both with a reference model: the sum as an integer and the
// truth table of the 4511 for the three displays.  Works with Icarus
// Verilog, Verilator (--binary) and Questa.
//
// The design 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 sets A and B after one edge and checks the outputs at the next.  It
// prints only the pairs with B = 5; the summary counts all of them.

// 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 [3:0] a, b;

    wire  [3:0] sum,         sum_chips;
    wire        carry,       carry_chips;
    wire  [6:0] abcdefg_a,   abcdefg_a_chips;
    wire  [6:0] abcdefg_b,   abcdefg_b_chips;
    wire  [6:0] abcdefg_sum, abcdefg_sum_chips;

    adder_display i_generic
    (
        .a           ( a           ),
        .b           ( b           ),
        .sum         ( sum         ),
        .carry       ( carry       ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

    adder_display_chips i_chips
    (
        .a           ( a                 ),
        .b           ( b                 ),
        .sum         ( sum_chips         ),
        .carry       ( carry_chips       ),
        .abcdefg_a   ( abcdefg_a_chips   ),
        .abcdefg_b   ( abcdefg_b_chips   ),
        .abcdefg_sum ( abcdefg_sum_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model

    function automatic logic [6:0] segments (input int code);
        case (code)
        0:       return 7'b111_1110;
        1:       return 7'b011_0000;
        2:       return 7'b110_1101;
        3:       return 7'b111_1001;
        4:       return 7'b011_0011;
        5:       return 7'b101_1011;
        6:       return 7'b001_1111;
        7:       return 7'b111_0000;
        8:       return 7'b111_1111;
        9:       return 7'b111_0011;
        default: return 7'b000_0000;
        endcase
    endfunction

    wire [4:0] total = 5' (int' (a) + int' (b));  // 0 to 30

    wire ok_generic =    { carry, sum }       === total
                      && abcdefg_a            === segments (int' (a))
                      && abcdefg_b            === segments (int' (b))
                      && abcdefg_sum          === segments (int' (total [3:0]));

    wire ok_chips   =    { carry_chips, sum_chips } === total
                      && abcdefg_a_chips          === segments (int' (a))
                      && abcdefg_b_chips          === segments (int' (b))
                      && abcdefg_sum_chips        === segments (int' (total [3:0]));

    // The digit shown on a display, for the trace

    function automatic string shown (input logic [6:0] s);
        for (int k = 0; k < 10; k ++)
            if (s === segments (k))
                return $sformatf ("%0d", k);

        return s === 7'b0 ? " " : "?";
    endfunction

    //------------------------------------------------------------------------
    // Checking at every rising clock edge

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

    logic checking = 1'b0;
    logic done     = 1'b0;  // Set by the stimulus before $finish

    int n_checks = 0;
    int n_errors = 0;
    int n_pairs  = 0;

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

            if (b == 4'd5 || ! ok_generic || ! ok_chips)
                $display ("%2d + %2d = %2d   %b %b   %s    %s    %s     %b    %s  %s",
                    a, b, total, carry, sum, shown (abcdefg_a), shown (abcdefg_b), shown (abcdefg_sum),
                    carry, ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok   " : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving A and B: all 256 pairs

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

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

        $display ("  A +  B =  sum  C S4-S1  A    B    sum  dot   generic  chips");

        checking <= '1;

        for (int i = 0; i < 256; i ++)
        begin
            { b, a } <= 8' (i);
            @ (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 && n_pairs == 256)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d pairs", n_errors, n_checks, n_pairs);
    end

endmodule
