// tb_mlkem_kg_katN_xsim.v - ML-KEM-512 KeyGen vs NIST KAT, case selected by +CASE=n.
// Loads d, z, ek, dk from per-case hex files (no literals via text channel).
//   d/z: single 256-bit word ($readmemh, byte0 in [7:0]).
//   ek : 800 bytes (== KAT pk).  dk: 1632 bytes (== KAT sk).
`timescale 1ns/1ps
module tb_mlkem_kg_katN_xsim;
    reg clk=0, rst_n=0, start_i=0;
    reg [255:0] d_i, z_i;
    wire busy_o, done_o;
    reg [3:0] dbg_slot_i=0; reg [7:0] dbg_idx_i=0; wire [11:0] dbg_coeff_o;
    reg dbg_byte_sel_i=0; reg [9:0] dbg_byte_idx_i=0; wire [7:0] dbg_byte_o;
    reg [11:0] dbg_dk_idx_i=0; wire [7:0] dbg_dk_o;
    wire [255:0] dbg_rho_o, dbg_sigma_o;

    mlkem_top #(.K(2)) dut (
        .clk(clk), .rst_n(rst_n), .d_i(d_i), .z_i(z_i), .start_i(start_i),
        .busy_o(busy_o), .done_o(done_o),
        .dbg_slot_i(dbg_slot_i), .dbg_idx_i(dbg_idx_i), .dbg_coeff_o(dbg_coeff_o),
        .dbg_byte_sel_i(dbg_byte_sel_i), .dbg_byte_idx_i(dbg_byte_idx_i), .dbg_byte_o(dbg_byte_o),
        .dbg_dk_idx_i(dbg_dk_idx_i), .dbg_dk_o(dbg_dk_o),
        .dbg_rho_o(dbg_rho_o), .dbg_sigma_o(dbg_sigma_o)
    );
    always #5 clk = ~clk;

    reg [255:0] dmem [0:0];
    reg [255:0] zmem [0:0];
    reg [7:0]   ek_gold [0:799];
    reg [7:0]   dk_gold [0:1631];
    integer c, i, errors, casenum;
    reg [8*64-1:0] dfile, zfile, ekfile, dkfile;

    initial begin
        if (!$value$plusargs("CASE=%d", casenum)) casenum = 0;
        $sformat(dfile,  "sync_rtl/top/TB/vectors/kat_c%0d_d.hex",  casenum);
        $sformat(zfile,  "sync_rtl/top/TB/vectors/kat_c%0d_z.hex",  casenum);
        $sformat(ekfile, "sync_rtl/top/TB/vectors/kat_c%0d_ek.hex", casenum);
        $sformat(dkfile, "sync_rtl/top/TB/vectors/kat_c%0d_dk.hex", casenum);
        $readmemh(dfile,  dmem);
        $readmemh(zfile,  zmem);
        $readmemh(ekfile, ek_gold);
        $readmemh(dkfile, dk_gold);
        d_i = dmem[0];
        z_i = zmem[0];

        rst_n=0; repeat(4) @(posedge clk); rst_n=1; @(posedge clk);
        start_i=1; @(posedge clk); start_i=0;
        c=0; while(!done_o && c<600000) begin @(posedge clk); c=c+1; end
        if(!done_o) begin $display("FAIL case %0d: timeout", casenum); $finish; end
        $display("=== KAT case %0d: KeyGen done in %0d cyc ===", casenum, c);

        errors = 0;
        dbg_byte_sel_i = 1'b0;
        for (i = 0; i < 800; i = i + 1) begin
            dbg_byte_idx_i = i[9:0]; @(posedge clk); @(posedge clk);
            if (dbg_byte_o !== ek_gold[i]) begin
                if (errors < 8) $display("  EK[%0d] got=%02x exp=%02x", i, dbg_byte_o, ek_gold[i]);
                errors = errors + 1;
            end
        end
        for (i = 0; i < 1632; i = i + 1) begin
            dbg_dk_idx_i = i[11:0]; @(posedge clk); @(posedge clk);
            if (dbg_dk_o !== dk_gold[i]) begin
                if (errors < 8) $display("  DK[%0d] got=%02x exp=%02x", i, dbg_dk_o, dk_gold[i]);
                errors = errors + 1;
            end
        end
        if (errors == 0) $display("CASE %0d PASS: ek==KAT.pk (800B), dk==KAT.sk (1632B)", casenum);
        else             $display("CASE %0d FAIL: %0d byte mismatches", casenum, errors);
        $finish;
    end
    initial begin #40000000; $display("FAIL: global timeout"); $finish; end
endmodule
