`timescale 1ns / 1ps

module tb;

    localparam period       = 20;
    localparam vector_count = 1045;
    logic [18:0] vectors [0:vector_count-1];
    logic [5:0] expected, check_mask;
    logic clk = 1'b0;

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

    logic rst        = '0;
    logic [3:0] data = '0;
    logic push       = '0;
    logic pop        = '0;
    logic [3:0] q_rtl;
    logic [3:0] q_chips;
    logic empty_rtl;
    logic empty_chips;
    logic full_rtl;
    logic full_chips;

    fifo_4x16 dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .data         (data),
        .push         (push),
        .pop          (pop),
        .q            (q_rtl),
        .empty        (empty_rtl),
        .full         (full_rtl)
    );

    fifo_4x16_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .data         (data),
        .push         (push),
        .pop          (pop),
        .q            (q_chips),
        .empty        (empty_chips),
        .full         (full_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            rst        = vectors [i] [0 +: 1];
            data       = vectors [i] [1 +: 4];
            push       = vectors [i] [5 +: 1];
            pop        = vectors [i] [6 +: 1];
            expected   = vectors [i] [7 +: 6];
            check_mask = vectors [i] [13 +: 6];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            @ (negedge clk);
            # 1;  // HC FIFO commands commit in this half-cycle.
            if (({ full_rtl, empty_rtl, q_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_4x16_rtl vector %0d got=%h expected=%h mask=%h", i, { full_rtl, empty_rtl, q_rtl }, expected, check_mask);
            if (({ full_chips, empty_chips, q_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_4x16_chips vector %0d got=%h expected=%h mask=%h", i, { full_chips, empty_chips, q_chips }, expected, check_mask);
        end
        $display ("PASS fifo_4x16: 1045 oracle vectors");
        $finish;
    end

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

endmodule
