// Testbench for exercise B7.
//
// Runs the generic design (seven_segment.sv) and the chip-level design
// (seven_segment_chips.sv) side by side and compares both with the truth
// table of the 4511 from the datasheet, for all sixteen codes.  Works with
// Icarus Verilog, Verilator (--binary) and Questa.
//
// The decoder 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 the code after one edge and checks the segments 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 [3:0] bcd;

    wire  [6:0] abcdefg, abcdefg_chips;

    seven_segment i_generic
    (
        .bcd     ( bcd     ),
        .abcdefg ( abcdefg )
    );

    seven_segment_chips i_chips
    (
        .bcd     ( bcd           ),
        .abcdefg ( abcdefg_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the truth table of the CD4511B datasheet, segments
    // a b c d e f g, with LAMP TEST and BLANKING at 1 and LATCH ENABLE at 0

    function automatic logic [6:0] expected (input logic [3:0] code);
        case (code)
        4'd0:    return 7'b111_1110;
        4'd1:    return 7'b011_0000;
        4'd2:    return 7'b110_1101;
        4'd3:    return 7'b111_1001;
        4'd4:    return 7'b011_0011;
        4'd5:    return 7'b101_1011;
        4'd6:    return 7'b001_1111;
        4'd7:    return 7'b111_0000;
        4'd8:    return 7'b111_1111;
        4'd9:    return 7'b111_0011;
        default: return 7'b000_0000;
        endcase
    endfunction

    // The digit as text, from the segments, for the trace

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

    //------------------------------------------------------------------------
    // Checking at every rising clock edge: the code still holds the value
    // the stimulus set at the previous edge, and the segments have settled.

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

    logic [15:0] codes_seen = '0;

    always @ (posedge clk)
        if (checking & ~ done)
        begin
            n_checks   <= n_checks + 2;
            n_errors   <= n_errors + (abcdefg       === expected (bcd) ? 0 : 1)
                                   + (abcdefg_chips === expected (bcd) ? 0 : 1);
            codes_seen <= codes_seen | 16'b1 << bcd;

            $display ("%2d   %b    %b   %s     %s  %s",
                bcd, bcd, abcdefg, shown (abcdefg),
                abcdefg       === expected (bcd) ? "ok     " : "WRONG  ",
                abcdefg_chips === expected (bcd) ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the code: 0 to 15, as with the four buttons D C B A

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

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

        $display ("code DCBA  abcdefg  digit  generic  chips");

        checking <= '1;

        for (int i = 0; i < 16; i ++)
        begin
            bcd <= 4' (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 && codes_seen == 16'hffff)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, codes seen %b", n_errors, n_checks, codes_seen);
    end

endmodule
