`timescale 1ns / 1ps

module tb;

    localparam period       = 20;
    localparam vector_count = 65536;
    logic [21:0] vectors [0:vector_count-1];
    logic [2:0] expected, check_mask;
    logic clk = 1'b0;

    always # (period / 2) clk = ~ clk;

    logic [7:0] a = '0;
    logic [7:0] b = '0;
    logic lt_rtl;
    logic lt_chips;
    logic eq_rtl;
    logic eq_chips;
    logic gt_rtl;
    logic gt_chips;

    comparator_8bit dut_rtl
    (
        .a            (a),
        .b            (b),
        .lt           (lt_rtl),
        .eq           (eq_rtl),
        .gt           (gt_rtl)
    );

    comparator_8bit_chips dut_chips
    (
        .a            (a),
        .b            (b),
        .lt           (lt_chips),
        .eq           (eq_chips),
        .gt           (gt_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            a          = vectors [i] [0 +: 8];
            b          = vectors [i] [8 +: 8];
            expected   = vectors [i] [16 +: 3];
            check_mask = vectors [i] [19 +: 3];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ gt_rtl, eq_rtl, lt_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "comparator_8bit_rtl vector %0d got=%h expected=%h mask=%h", i, { gt_rtl, eq_rtl, lt_rtl }, expected, check_mask);
            if (({ gt_chips, eq_chips, lt_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "comparator_8bit_chips vector %0d got=%h expected=%h mask=%h", i, { gt_chips, eq_chips, lt_chips }, expected, check_mask);
        end
        $display ("PASS comparator_8bit: 65536 oracle vectors");
        $finish;
    end

    initial
    begin
        repeat (vector_count * 3 + 20) @ (posedge clk);
        $fatal (1, "Cycle watchdog");
    end

endmodule
