`timescale 1ns / 1ps

module tb;

    localparam period       = 20;
    localparam vector_count = 200;
    logic [22:0] vectors [0:vector_count-1];
    logic [10:0] expected, check_mask;
    logic clk = 1'b0;

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

    logic rst = '0;
    logic [3:0] binary_rtl;
    logic [3:0] binary_chips;
    logic [3:0] johnson_rtl;
    logic [3:0] johnson_chips;
    logic lt_rtl;
    logic lt_chips;
    logic eq_rtl;
    logic eq_chips;
    logic gt_rtl;
    logic gt_chips;

    counter_stream_compare dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .binary       (binary_rtl),
        .johnson      (johnson_rtl),
        .lt           (lt_rtl),
        .eq           (eq_rtl),
        .gt           (gt_rtl)
    );

    counter_stream_compare_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .binary       (binary_chips),
        .johnson      (johnson_chips),
        .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
            rst        = vectors [i] [0 +: 1];
            expected   = vectors [i] [1 +: 11];
            check_mask = vectors [i] [12 +: 11];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ gt_rtl, eq_rtl, lt_rtl, johnson_rtl, binary_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "counter_stream_compare_rtl vector %0d got=%h expected=%h mask=%h", i, { gt_rtl, eq_rtl, lt_rtl, johnson_rtl, binary_rtl }, expected, check_mask);
            if (({ gt_chips, eq_chips, lt_chips, johnson_chips, binary_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "counter_stream_compare_chips vector %0d got=%h expected=%h mask=%h", i, { gt_chips, eq_chips, lt_chips, johnson_chips, binary_chips }, expected, check_mask);
        end
        $display ("PASS counter_stream_compare: 200 oracle vectors");
        $finish;
    end

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

endmodule
