`timescale 1ns / 1ps

module tb;

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

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

    logic rst = '0;
    logic x   = '0;
    logic match_rtl;
    logic match_chips;

    custom_sequence dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .x            (x),
        .match        (match_rtl)
    );

    custom_sequence_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .x            (x),
        .match        (match_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            rst        = vectors [i] [0 +: 1];
            x          = vectors [i] [1 +: 1];
            expected   = vectors [i] [2 +: 1];
            check_mask = vectors [i] [3 +: 1];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ match_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "custom_sequence_rtl vector %0d got=%h expected=%h mask=%h", i, { match_rtl }, expected, check_mask);
            if (({ match_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "custom_sequence_chips vector %0d got=%h expected=%h mask=%h", i, { match_chips }, expected, check_mask);
        end
        $display ("PASS custom_sequence: 500 oracle vectors");
        $finish;
    end

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

endmodule
