// retroDE_ps2 — tb_ee_core_ei
//
// Ch286 — Focused TB for R5900 EI (Enable Interrupts), the EE-
// specific extension to MIPS COP0 CO at funct 0x38. The full 32-bit
// canonical encoding is 0x42000038 (opcode=COP0, rs=CO, rt/rd/sa=0).
// qbert.elf hits this exact instruction at PC 0x001000FC during
// init. Codex framing for Ch286:
//
//   "Decode exact instruction 32'h42000038 as EI. Treat as legal:
//    no GPR writeback, no memory side effect, PC advances normally,
//    no halt/trap. Do not NOP-class all COP0/CO instructions."
//
// Test sequence:
//   1. LUI/ORI sentinels into $v0, $t0  (sentinel_A, sentinel_B)
//   2. Execute EI (0x42000038) — must retire, PC += 4, no register
//      effects.
//   3. LUI/ORI sentinel into $t1 (sentinel_C). Reaching this proves
//      we got past EI without halting.
//   4. Execute DI (0x42000039) — the *companion* CO instruction.
//      Under strict mode this MUST still trap, proving that we
//      didn't widen the decode beyond exact EI.
//   5. Post-halt: assert saw_trap=1 with trap_pc at the DI slot;
//      $v0/$t0/$t1 still hold their sentinels (EI side-effect-free).
//
// Strict mode ON.

`timescale 1ns/1ps

module tb_ee_core_ei;

    localparam int BIOS_BYTES    = 4 * 1024 * 1024;
    localparam int EE_RAM_BYTES  = 4 * 1024;
    localparam int EE_RAM_ADDR_W = $clog2(EE_RAM_BYTES);

    localparam logic [31:0] RESET_VECTOR   = 32'hBFC0_0000;
    localparam logic [31:0] BOOTSTRAP_VA   = 32'hBFC0_0100;
    localparam int          RESET_SLOT     = 0;
    localparam int          BOOTSTRAP_SLOT = 32'h100 >> 2;

    // Slot layout
    localparam int B_LUI_V0   = BOOTSTRAP_SLOT + 0;
    localparam int B_ORI_V0   = BOOTSTRAP_SLOT + 1;
    localparam int B_LUI_T0   = BOOTSTRAP_SLOT + 2;
    localparam int B_ORI_T0   = BOOTSTRAP_SLOT + 3;
    localparam int B_EI       = BOOTSTRAP_SLOT + 4;  // 0x42000038
    localparam int B_LUI_T1   = BOOTSTRAP_SLOT + 5;
    localparam int B_ORI_T1   = BOOTSTRAP_SLOT + 6;
    localparam int B_DI       = BOOTSTRAP_SLOT + 7;  // 0x42000039 — should trap

    localparam logic [5:0] OP_J     = 6'h02;
    localparam logic [5:0] OP_ORI   = 6'h0D;
    localparam logic [5:0] OP_LUI   = 6'h0F;

    localparam int RV0 = 2;
    localparam int RT0 = 8;
    localparam int RT1 = 9;

    localparam logic [31:0] SENTINEL_A = 32'hAAAA_5555;
    localparam logic [31:0] SENTINEL_B = 32'hBBBB_3333;
    localparam logic [31:0] SENTINEL_C = 32'hCCCC_1111;

    logic clk, rst_n;
    initial clk = 1'b0;
    always #5 clk = ~clk;

    logic        core_go, core_halt, core_trap;
    logic [31:0] core_pc, core_trap_pc, core_trap_instr;
    logic        ee_rd_en, ee_rd_valid, ee_wr_en;
    logic [31:0] ee_rd_addr, ee_rd_data, ee_wr_addr, ee_wr_data;
    logic [3:0]  ee_wr_be;

    logic                core_ev_valid;
    trace_pkg::subsys_e  core_ev_subsys;
    trace_pkg::event_e   core_ev_event;
    logic [63:0]         core_ev_arg0, core_ev_arg1, core_ev_arg2, core_ev_arg3;
    logic [31:0]         core_ev_flags;

    ee_core_stub #(.STRICT_UNSUPPORTED(1'b1)) u_core (
        .clk(clk), .rst_n(rst_n), .go_i(core_go),
        .map_rd_en(ee_rd_en), .map_rd_addr(ee_rd_addr),
        .map_rd_data(ee_rd_data), .map_rd_valid(ee_rd_valid),
        .map_wr_en(ee_wr_en), .map_wr_addr(ee_wr_addr),
        .map_wr_data(ee_wr_data), .map_wr_be(ee_wr_be),
        .cpu_irq(1'b0),
        .halt_o(core_halt), .pc_o(core_pc),
        .trap_o(core_trap), .trap_pc_o(core_trap_pc),
        .trap_instr_o(core_trap_instr),
        .ev_valid(core_ev_valid), .ev_subsys(core_ev_subsys),
        .ev_event(core_ev_event),
        .ev_arg0(core_ev_arg0), .ev_arg1(core_ev_arg1),
        .ev_arg2(core_ev_arg2), .ev_arg3(core_ev_arg3),
        .ev_flags(core_ev_flags)
    );

    logic         bios_rd_en, bios_rd_valid;
    logic [21:0]  bios_rd_addr;
    logic [31:0]  bios_rd_data;
    logic         ee_ram_rd_en, ee_ram_rd_valid, ee_ram_wr_en;
    logic [24:0]  ee_ram_rd_addr, ee_ram_wr_addr;
    logic [127:0] ee_ram_rd_data, ee_ram_wr_data;
    logic [15:0]  ee_ram_wr_be;
    logic [7:0]   ee_ram_master_id;

    ee_memory_map_stub u_ee_map (
        .clk(clk), .rst_n(rst_n),
        .ee_rd_en(ee_rd_en), .ee_rd_addr(ee_rd_addr),
        .ee_rd_data(ee_rd_data), .ee_rd_valid(ee_rd_valid),
        .ee_wr_en(ee_wr_en), .ee_wr_addr(ee_wr_addr),
        .ee_wr_data(ee_wr_data), .ee_wr_be(ee_wr_be),
        .dmac_rd_en(1'b0), .dmac_rd_addr(32'd0),
        .dmac_rd_data(), .dmac_rd_valid(),
        .bridge_wr_en(1'b0), .bridge_wr_addr(32'd0),
        .bridge_wr_data(128'd0), .bridge_wr_be(16'd0),
        .bridge_master_id(8'd0),
        .bios_rd_en(bios_rd_en), .bios_rd_addr(bios_rd_addr),
        .bios_rd_data(bios_rd_data), .bios_rd_valid(bios_rd_valid),
        .ram_rd_en(ee_ram_rd_en), .ram_rd_addr(ee_ram_rd_addr),
        .ram_rd_data(ee_ram_rd_data), .ram_rd_valid(ee_ram_rd_valid),
        .ram_wr_en(ee_ram_wr_en), .ram_wr_addr(ee_ram_wr_addr),
        .ram_wr_data(ee_ram_wr_data), .ram_wr_be(ee_ram_wr_be),
        .ram_master_id(ee_ram_master_id),
        .ee_dmac_ch2_wr_en(), .ee_dmac_ch2_wr_addr(), .ee_dmac_ch2_wr_data(),
        .ee_dmac_ch2_rd_en(), .ee_dmac_ch2_rd_addr(),
        .ee_dmac_ch2_rd_data(32'd0), .ee_dmac_ch2_rd_valid(1'b0),
        .ee_intc_wr_en(), .ee_intc_wr_addr(), .ee_intc_wr_data(),
        .ee_intc_rd_en(), .ee_intc_rd_addr(),
        .ee_intc_rd_data(32'd0), .ee_intc_rd_valid(1'b0),
        .ee_misc_mmio_wr_en(), .ee_misc_mmio_wr_addr(), .ee_misc_mmio_wr_data(), .ee_misc_mmio_wr_be(),
        .ee_misc_mmio_rd_en(), .ee_misc_mmio_rd_addr(),
        .ee_misc_mmio_rd_data(32'd0), .ee_misc_mmio_rd_valid(1'b0),
        .ee_biu_wr_en(), .ee_biu_wr_addr(), .ee_biu_wr_data(), .ee_biu_wr_be(),
        .ee_biu_rd_en(), .ee_biu_rd_addr(),
        .ee_biu_rd_data(32'd0), .ee_biu_rd_valid(1'b0),
        .ev_valid(), .ev_subsys(), .ev_event(),
        .ev_arg0(), .ev_arg1(), .ev_arg2(), .ev_arg3(), .ev_flags()
    );

    ee_ram_stub #(.SIZE_BYTES(EE_RAM_BYTES)) u_ee_ram (
        .clk(clk), .rst_n(rst_n),
        .rd_en(ee_ram_rd_en), .rd_addr(ee_ram_rd_addr[EE_RAM_ADDR_W-1:0]),
        .rd_data(ee_ram_rd_data), .rd_valid(ee_ram_rd_valid),
        .wr_en(ee_ram_wr_en), .wr_addr(ee_ram_wr_addr[EE_RAM_ADDR_W-1:0]),
        .wr_data(ee_ram_wr_data), .wr_be(ee_ram_wr_be),
        .master_id(ee_ram_master_id),
        .ev_valid(), .ev_subsys(), .ev_event(),
        .ev_arg0(), .ev_arg1(), .ev_arg2(), .ev_arg3(), .ev_flags()
    );

    bios_rom_stub #(.SIZE_BYTES(BIOS_BYTES)) u_bios (
        .clk(clk), .rst_n(rst_n),
        .rd_en(bios_rd_en), .rd_addr(bios_rd_addr),
        .rd_data(bios_rd_data), .rd_valid(bios_rd_valid),
        .ev_valid(), .ev_subsys(), .ev_event(),
        .ev_arg0(), .ev_arg1(), .ev_arg2(), .ev_arg3(), .ev_flags()
    );

    trace_sink_stub #(.FILENAME("ee_core_ei_core.trace"), .SINK_LABEL("ee_core"))
        u_trace_core (.clk(clk), .rst_n(rst_n),
            .ev_valid(core_ev_valid), .ev_subsys(core_ev_subsys),
            .ev_event(core_ev_event), .ev_arg0(core_ev_arg0),
            .ev_arg1(core_ev_arg1), .ev_arg2(core_ev_arg2),
            .ev_arg3(core_ev_arg3), .ev_flags(core_ev_flags));

    int core_retired_events, core_halt_events, core_trap_events, errors;
    // EI-retire latch: snapshot $v0/$t0 the cycle the EI instruction
    // retires (retired_pc == EI slot's PC). Confirms that the EI
    // retire fired at all *and* that $v0/$t0 still hold their
    // sentinels at that exact moment (= EI didn't clobber them).
    logic [31:0] v0_at_ei_retire, t0_at_ei_retire;
    logic        seen_ei_retire;
    initial begin
        core_retired_events = 0; core_halt_events = 0;
        core_trap_events = 0; errors = 0;
        v0_at_ei_retire = 32'hDEAD_BEEF;
        t0_at_ei_retire = 32'hDEAD_BEEF;
        seen_ei_retire  = 1'b0;
    end
    always_ff @(posedge clk) begin
        if (rst_n && core_ev_valid) begin
            core_retired_events <= core_retired_events + 1;
            if ((core_ev_flags & 32'h08) != 32'd0) core_halt_events <= core_halt_events + 1;
            if ((core_ev_flags & 32'h80) != 32'd0) core_trap_events <= core_trap_events + 1;
            if (u_core.retired_pc == BOOTSTRAP_VA + ((B_EI - BOOTSTRAP_SLOT) << 2)
                    && !seen_ei_retire) begin
                v0_at_ei_retire <= u_core.regfile[RV0];
                t0_at_ei_retire <= u_core.regfile[RT0];
                seen_ei_retire  <= 1'b1;
            end
        end
    end

    task automatic bios_word(input int slot, input logic [31:0] word);
        u_bios.mem[slot] = word;
    endtask
    function automatic logic [31:0] enc_i(input logic [5:0] op,
                                          input int rs, input int rt,
                                          input logic [15:0] imm);
        return {op, rs[4:0], rt[4:0], imm};
    endfunction
    function automatic logic [31:0] enc_j(input logic [31:0] target_addr);
        return {OP_J, target_addr[27:2]};
    endfunction

    initial begin
        rst_n = 1'b0; core_go = 1'b0;
        repeat (4) @(posedge clk);
        rst_n = 1'b1;
        repeat (2) @(posedge clk);

        bios_word(RESET_SLOT,     enc_j(BOOTSTRAP_VA));
        bios_word(RESET_SLOT + 1, 32'd0);

        // $v0 = SENTINEL_A
        bios_word(B_LUI_V0, enc_i(OP_LUI, 0, RV0, SENTINEL_A[31:16]));
        bios_word(B_ORI_V0, enc_i(OP_ORI, RV0, RV0, SENTINEL_A[15:0]));
        // $t0 = SENTINEL_B
        bios_word(B_LUI_T0, enc_i(OP_LUI, 0, RT0, SENTINEL_B[31:16]));
        bios_word(B_ORI_T0, enc_i(OP_ORI, RT0, RT0, SENTINEL_B[15:0]));
        // EI — must retire, no side effects, PC += 4.
        bios_word(B_EI,     32'h4200_0038);
        // $t1 = SENTINEL_C — reaching this proves we got past EI.
        bios_word(B_LUI_T1, enc_i(OP_LUI, 0, RT1, SENTINEL_C[31:16]));
        bios_word(B_ORI_T1, enc_i(OP_ORI, RT1, RT1, SENTINEL_C[15:0]));
        // DI — companion CO encoding, NOT decoded. Must trap under
        // strict mode (proves we didn't widen beyond exact EI).
        bios_word(B_DI,     32'h4200_0039);

        @(negedge clk); core_go = 1'b1;
        @(negedge clk); core_go = 1'b0;

        while (!core_halt) @(posedge clk);
        repeat (4) @(posedge clk);

        // EI must have retired (= the latch captured something).
        if (!seen_ei_retire) begin
            $error("[tb_ee_core_ei] EI never retired — core trapped on EI itself?");
            errors = errors + 1;
        end
        // At the EI retire, $v0/$t0 still hold their sentinels:
        // proves EI was side-effect-free at that moment (and the
        // post-EI LUI+ORI hadn't yet clobbered them).
        if (seen_ei_retire && v0_at_ei_retire !== SENTINEL_A) begin
            $error("[tb_ee_core_ei] $v0 at EI retire = 0x%08h (expected 0x%08h)",
                   v0_at_ei_retire, SENTINEL_A);
            errors = errors + 1;
        end
        if (seen_ei_retire && t0_at_ei_retire !== SENTINEL_B) begin
            $error("[tb_ee_core_ei] $t0 at EI retire = 0x%08h (expected 0x%08h)",
                   t0_at_ei_retire, SENTINEL_B);
            errors = errors + 1;
        end

        // DI must trap (strict mode rejects narrow-EI's neighbor).
        if (core_trap_events != 1) begin
            $error("[tb_ee_core_ei] expected 1 trap (on DI), got %0d",
                   core_trap_events);
            errors = errors + 1;
        end
        if (core_trap_pc !== BOOTSTRAP_VA + ((B_DI - BOOTSTRAP_SLOT) << 2)) begin
            $error("[tb_ee_core_ei] trap_pc = 0x%08h (expected DI slot 0x%08h)",
                   core_trap_pc, BOOTSTRAP_VA + ((B_DI - BOOTSTRAP_SLOT) << 2));
            errors = errors + 1;
        end
        if (core_trap_instr !== 32'h4200_0039) begin
            $error("[tb_ee_core_ei] trap_instr = 0x%08h (expected DI 0x42000039)",
                   core_trap_instr);
            errors = errors + 1;
        end

        // End-of-sim: $t1 = SENTINEL_C proves the post-EI LUI+ORI ran
        // (i.e. EI did *not* halt). $v0/$t0 still hold sentinels
        // (no spurious writes between EI and DI).
        if (u_core.regfile[RT1] !== SENTINEL_C) begin
            $error("[tb_ee_core_ei] $t1 = 0x%08h (expected 0x%08h — post-EI LUI+ORI didn't run, so EI must have halted)",
                   u_core.regfile[RT1], SENTINEL_C);
            errors = errors + 1;
        end
        if (u_core.regfile[RV0] !== SENTINEL_A) begin
            $error("[tb_ee_core_ei] $v0 (end) = 0x%08h (expected 0x%08h)",
                   u_core.regfile[RV0], SENTINEL_A);
            errors = errors + 1;
        end
        if (u_core.regfile[RT0] !== SENTINEL_B) begin
            $error("[tb_ee_core_ei] $t0 (end) = 0x%08h (expected 0x%08h)",
                   u_core.regfile[RT0], SENTINEL_B);
            errors = errors + 1;
        end

        $display("[tb_ee_core_ei] retired=%0d halt=%0d trap=%0d pc=0x%08h errors=%0d",
                 core_retired_events, core_halt_events,
                 core_trap_events, core_pc, errors);
        $display("[tb_ee_core_ei]   seen_ei_retire=%0b v0_at_ei=0x%08h t0_at_ei=0x%08h",
                 seen_ei_retire, v0_at_ei_retire, t0_at_ei_retire);
        $display("[tb_ee_core_ei]   trap_pc=0x%08h trap_instr=0x%08h",
                 core_trap_pc, core_trap_instr);
        $display("[tb_ee_core_ei]   $v0=0x%08h $t0=0x%08h $t1=0x%08h",
                 u_core.regfile[RV0], u_core.regfile[RT0], u_core.regfile[RT1]);

        if (errors == 0)
            $display("[tb_ee_core_ei] PASS");
        else
            $display("[tb_ee_core_ei] FAIL");
        $finish;
    end

    initial begin
        #2000000;
        $error("[tb_ee_core_ei] timeout");
        $finish;
    end

endmodule : tb_ee_core_ei
