// retroDE_ps2 — tb_ee_core_gif_primitive_gouraud (Ch80)
//
// Verifies the Ch80 per-vertex (Gouraud) color tracking in
// gs_stub. Real game streams that want Gouraud shading interleave
// RGBAQ writes with XYZ2 commits — each vertex captures a
// different color. The observer's parallel rolling color window
// (c_curr/c_prev/c_prev_prev/c_pivot) snapshots into
// prim_color_v0_q / prim_color_v1_q / prim_color_v2_q on every
// EV_PRIM_DRAW so a TB can verify *which color* belongs to *which
// vertex* of the closing primitive.
//
// Exercises three primitive types with distinct per-vertex colors:
//
//   Tag1 — discrete TRIANGLE (NREG=7):
//     PRIM=TRI, RGBAQ=red, XYZ2_v1, RGBAQ=green, XYZ2_v2,
//     RGBAQ=blue, XYZ2_v3 → 1 EV_PRIM_DRAW with per-vertex colors
//     {red, green, blue}.
//
//   Tag2 — TRI_STRIP, 5 vertices, 5 distinct colors (NREG=11):
//     PRIM=TRI_STRIP, then 5×(RGBAQ_cN, XYZ2_vN) →
//     anchor draw {c1,c2,c3}, then 2 saturated extensions:
//     {c2,c3,c4}, {c3,c4,c5}. Per-vertex colors roll just like
//     the vertex window.
//
//   Tag3 — TRI_FAN, 5 vertices, 5 distinct colors (NREG=11):
//     PRIM=TRI_FAN, then 5×(RGBAQ_dN, XYZ2_vN) →
//     anchor draw {d1,d2,d3}, then 2 saturated extensions:
//     {d1, d3, d4}, {d1, d4, d5}. Saturated extensions PIN v0's
//     color to d1 (the color of the pivot vertex), in lockstep
//     with vertex v0 being pinned to v_pivot. THIS is the
//     load-bearing distinction from TRI_STRIP — without per-vertex
//     color tracking a Gouraud-shaded fan would all show the same
//     color via prim_color_q (the closing-vertex color).
//
// Stream length: (1+7) + (1+11) + (1+11) = 32 qwords.
//
// Pass criteria:
//   - DMAC: 1 START / 32 BEAT / 1 DONE
//   - GIF:  3 GIFTAG, 29 EV_WRITE, 0 EV_MODE
//   - GS:   0 BGCOLOR, 22 EV_WRITE, 7 EV_PRIM_DRAW, 0 EV_MODE
//           (TRI: 6 W + 1 PD; STRIP: 8 W + 3 PD; FAN: 8 W + 3 PD)
//   - prim_complete_count == 7
//   - 21 per-vertex color assertions across the 7 events
//   - Load-bearing: events [5] and [6] (TRI_FAN saturated) have
//     prim_color_v0_q pinned to d1 — distinguishes pivot-color
//     semantics from rolling-color semantics.

`timescale 1ns/1ps

module tb_ee_core_gif_primitive_gouraud;

    localparam int RAM_SIZE_BYTES = 4 * 1024;
    localparam int RAM_ADDR_W     = $clog2(RAM_SIZE_BYTES);

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

    // ee_ram_stub
    logic                  ram_rd_en;
    logic [RAM_ADDR_W-1:0] ram_rd_addr;
    logic [127:0]          ram_rd_data;
    logic                  ram_rd_valid;
    logic                  ram_wr_en;
    logic [RAM_ADDR_W-1:0] ram_wr_addr;
    logic [127:0]          ram_wr_data;
    logic [15:0]           ram_wr_be;
    logic [7:0]            ram_master_id;

    ee_ram_stub #(.SIZE_BYTES(RAM_SIZE_BYTES)) u_ram (
        .clk(clk), .rst_n(rst_n),
        .rd_en(ram_rd_en), .rd_addr(ram_rd_addr),
        .rd_data(ram_rd_data), .rd_valid(ram_rd_valid),
        .wr_en(ram_wr_en), .wr_addr(ram_wr_addr),
        .wr_data(ram_wr_data), .wr_be(ram_wr_be),
        .master_id(ram_master_id),
        .ev_valid(), .ev_subsys(), .ev_event(),
        .ev_arg0(), .ev_arg1(), .ev_arg2(), .ev_arg3(), .ev_flags()
    );
    assign ram_master_id = ram_rd_en ? 8'd1 : 8'd0;

    // DMAC + map
    logic        dmac_reg_wr_en;
    logic [7:0]  dmac_reg_offset;
    logic [31:0] dmac_reg_wr_data;
    logic        dmac_mem_rd_en;
    logic [31:0] dmac_mem_rd_addr;
    logic         dmac_gif_valid;
    logic [127:0] dmac_gif_data;
    logic         dmac_gif_last;
    logic         dmac_gif_ready;

    logic                dmac_ev_valid;
    trace_pkg::subsys_e  dmac_ev_subsys;
    trace_pkg::event_e   dmac_ev_event;
    logic [63:0]         dmac_ev_arg0, dmac_ev_arg1, dmac_ev_arg2, dmac_ev_arg3;
    logic [31:0]         dmac_ev_flags;

    logic [127:0] map_to_dmac_rd_data;
    logic         map_to_dmac_rd_valid;

    dmac_reg_stub u_dmac (
        .clk(clk), .rst_n(rst_n),
        .reg_wr_en(dmac_reg_wr_en), .reg_offset(dmac_reg_offset),
        .reg_wr_data(dmac_reg_wr_data),
        .reg_rd_en(1'b0), .reg_rd_data(), .reg_rd_valid(),
        .mem_rd_en(dmac_mem_rd_en), .mem_rd_addr(dmac_mem_rd_addr),
        .mem_rd_data(map_to_dmac_rd_data), .mem_rd_valid(map_to_dmac_rd_valid),
        .ep_valid(dmac_gif_valid), .ep_data(dmac_gif_data),
        .ep_last(dmac_gif_last), .ep_ready(dmac_gif_ready),
        .irq_completion_o(),
        .ev_valid(dmac_ev_valid), .ev_subsys(dmac_ev_subsys),
        .ev_event(dmac_ev_event),
        .ev_arg0(dmac_ev_arg0), .ev_arg1(dmac_ev_arg1),
        .ev_arg2(dmac_ev_arg2), .ev_arg3(dmac_ev_arg3),
        .ev_flags(dmac_ev_flags)
    );

    logic        map_ram_rd_en;
    logic [24:0] map_ram_rd_addr;
    ee_memory_map_stub u_map (
        .clk(clk), .rst_n(rst_n),
        .ee_rd_en(1'b0), .ee_rd_addr(32'd0),
        .ee_rd_data(), .ee_rd_valid(),
        .ee_wr_en(1'b0), .ee_wr_addr(32'd0),
        .ee_wr_data(32'd0), .ee_wr_be(4'd0),
        .dmac_rd_en(dmac_mem_rd_en), .dmac_rd_addr(dmac_mem_rd_addr),
        .dmac_rd_data(map_to_dmac_rd_data),
        .dmac_rd_valid(map_to_dmac_rd_valid),
        .bios_rd_en(), .bios_rd_addr(),
        .bios_rd_data(32'd0), .bios_rd_valid(1'b0),
        .ram_rd_en(map_ram_rd_en), .ram_rd_addr(map_ram_rd_addr),
        .ram_rd_data(ram_rd_data), .ram_rd_valid(ram_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),
        .ram_wr_en(), .ram_wr_addr(), .ram_wr_data(),
        .ram_wr_be(), .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()
    );

    assign ram_rd_en   = map_ram_rd_en;
    assign ram_rd_addr = map_ram_rd_addr[RAM_ADDR_W-1:0];

    // gif_packed_stub (REAL_AD_REG_MAP=1)
    logic        gif_gs_wr_en;
    logic [15:0] gif_gs_wr_addr;
    logic [63:0] gif_gs_wr_data;
    logic        gif_gif_reg_wr_en;
    logic [7:0]  gif_gif_reg_num;
    logic [63:0] gif_gif_reg_data;

    logic                gif_ev_valid;
    trace_pkg::subsys_e  gif_ev_subsys;
    trace_pkg::event_e   gif_ev_event;
    logic [63:0]         gif_ev_arg0, gif_ev_arg1, gif_ev_arg2, gif_ev_arg3;
    logic [31:0]         gif_ev_flags;

    gif_packed_stub #(.REAL_AD_REG_MAP(1'b1)) u_gif (
        .clk(clk), .rst_n(rst_n),
        .in_valid(dmac_gif_valid), .in_data(dmac_gif_data),
        .in_last(dmac_gif_last), .in_ready(dmac_gif_ready),
        .image_data_ready(1'b1),
        .raster_fifo_full(1'b0),
        .gs_wr_en(gif_gs_wr_en), .gs_wr_addr(gif_gs_wr_addr),
        .gs_wr_data(gif_gs_wr_data),
        .gif_reg_wr_en(gif_gif_reg_wr_en),
        .gif_reg_num(gif_gif_reg_num),
        .gif_reg_data(gif_gif_reg_data),
        .ev_valid(gif_ev_valid), .ev_subsys(gif_ev_subsys),
        .ev_event(gif_ev_event),
        .ev_arg0(gif_ev_arg0), .ev_arg1(gif_ev_arg1),
        .ev_arg2(gif_ev_arg2), .ev_arg3(gif_ev_arg3),
        .ev_flags(gif_ev_flags)
    );

    // gs_stub
    logic [7:0]          bg_r, bg_g, bg_b;
    logic [63:0]         prim_q, rgbaq_q, xyz2_q, xyzf2_q, frame_1_q, zbuf_1_q;
    logic                prim_complete;
    logic [31:0]         prim_complete_count;
    logic [63:0]         prim_v0_q, prim_v1_q, prim_v2_q;
    logic [63:0]         prim_color_q;
    logic [63:0]         prim_color_v0_q, prim_color_v1_q, prim_color_v2_q;
    trace_pkg::vertex_t  prim_v0_decoded_q, prim_v1_decoded_q, prim_v2_decoded_q;
    trace_pkg::color_t   prim_v0_color_decoded_q, prim_v1_color_decoded_q, prim_v2_color_decoded_q;
    logic                gs_ev_valid;
    trace_pkg::subsys_e  gs_ev_subsys;
    trace_pkg::event_e   gs_ev_event;
    logic [63:0]         gs_ev_arg0, gs_ev_arg1, gs_ev_arg2, gs_ev_arg3;
    logic [31:0]         gs_ev_flags;

    gs_stub u_gs (
        .clk(clk), .rst_n(rst_n),
        .reg_wr_en(gif_gs_wr_en), .reg_wr_addr(gif_gs_wr_addr),
        .reg_wr_data(gif_gs_wr_data),
        .gif_reg_wr_en(gif_gif_reg_wr_en),
        .gif_reg_num(gif_gif_reg_num),
        .gif_reg_data(gif_gif_reg_data),
        .bg_r(bg_r), .bg_g(bg_g), .bg_b(bg_b),
        .prim_q(prim_q), .rgbaq_q(rgbaq_q),
        .xyz2_q(xyz2_q), .xyzf2_q(xyzf2_q),
        .frame_1_q(frame_1_q), .zbuf_1_q(zbuf_1_q),
        .prim_complete(prim_complete),
        .prim_complete_count(prim_complete_count),
        .prim_v0_q(prim_v0_q), .prim_v1_q(prim_v1_q), .prim_v2_q(prim_v2_q),
        .prim_color_q(prim_color_q),
        .prim_color_v0_q(prim_color_v0_q),
        .prim_color_v1_q(prim_color_v1_q),
        .prim_color_v2_q(prim_color_v2_q),
        .prim_v0_decoded_q(prim_v0_decoded_q),
        .prim_v1_decoded_q(prim_v1_decoded_q),
        .prim_v2_decoded_q(prim_v2_decoded_q),
        .prim_v0_color_decoded_q(prim_v0_color_decoded_q),
        .prim_v1_color_decoded_q(prim_v1_color_decoded_q),
        .prim_v2_color_decoded_q(prim_v2_color_decoded_q),
        .ev_valid(gs_ev_valid), .ev_subsys(gs_ev_subsys),
        .ev_event(gs_ev_event),
        .ev_arg0(gs_ev_arg0), .ev_arg1(gs_ev_arg1),
        .ev_arg2(gs_ev_arg2), .ev_arg3(gs_ev_arg3),
        .ev_flags(gs_ev_flags)
    );

    trace_sink_stub #(.FILENAME("gif_primitive_gouraud_dmac.trace"), .SINK_LABEL("dmac"))
        u_trace_dmac (.clk(clk), .rst_n(rst_n),
            .ev_valid(dmac_ev_valid), .ev_subsys(dmac_ev_subsys),
            .ev_event(dmac_ev_event),
            .ev_arg0(dmac_ev_arg0), .ev_arg1(dmac_ev_arg1),
            .ev_arg2(dmac_ev_arg2), .ev_arg3(dmac_ev_arg3),
            .ev_flags(dmac_ev_flags));
    trace_sink_stub #(.FILENAME("gif_primitive_gouraud_gif.trace"), .SINK_LABEL("gif"))
        u_trace_gif (.clk(clk), .rst_n(rst_n),
            .ev_valid(gif_ev_valid), .ev_subsys(gif_ev_subsys),
            .ev_event(gif_ev_event),
            .ev_arg0(gif_ev_arg0), .ev_arg1(gif_ev_arg1),
            .ev_arg2(gif_ev_arg2), .ev_arg3(gif_ev_arg3),
            .ev_flags(gif_ev_flags));
    trace_sink_stub #(.FILENAME("gif_primitive_gouraud_gs.trace"), .SINK_LABEL("gs"))
        u_trace_gs (.clk(clk), .rst_n(rst_n),
            .ev_valid(gs_ev_valid), .ev_subsys(gs_ev_subsys),
            .ev_event(gs_ev_event),
            .ev_arg0(gs_ev_arg0), .ev_arg1(gs_ev_arg1),
            .ev_arg2(gs_ev_arg2), .ev_arg3(gs_ev_arg3),
            .ev_flags(gs_ev_flags));

    // Counters
    int dma_start_count, dma_beat_count, dma_done_count;
    int gif_giftag_count, gif_write_count, gif_mode_count;
    int gs_bgcolor_count, gs_write_count, gs_prim_draw_count, gs_mode_count;
    int errors;
    logic dma_done_seen;

    initial begin
        dma_start_count = 0; dma_beat_count = 0; dma_done_count = 0;
        gif_giftag_count = 0; gif_write_count = 0; gif_mode_count = 0;
        gs_bgcolor_count = 0; gs_write_count = 0;
        gs_prim_draw_count = 0; gs_mode_count = 0;
        errors = 0; dma_done_seen = 1'b0;
    end

    always_ff @(posedge clk) begin
        if (rst_n && dmac_ev_valid) begin
            unique case (dmac_ev_event)
                trace_pkg::EV_DMA_START: dma_start_count <= dma_start_count + 1;
                trace_pkg::EV_DMA_BEAT:  dma_beat_count  <= dma_beat_count  + 1;
                trace_pkg::EV_DMA_DONE: begin
                    dma_done_count <= dma_done_count + 1;
                    dma_done_seen  <= 1'b1;
                end
                default: ;
            endcase
        end
        if (rst_n && gif_ev_valid) begin
            if (gif_ev_event == trace_pkg::EV_GIFTAG) gif_giftag_count <= gif_giftag_count + 1;
            if (gif_ev_event == trace_pkg::EV_WRITE)  gif_write_count  <= gif_write_count  + 1;
            if (gif_ev_event == trace_pkg::EV_MODE)   gif_mode_count   <= gif_mode_count   + 1;
        end
        if (rst_n && gs_ev_valid) begin
            if (gs_ev_event == trace_pkg::EV_BGCOLOR)   gs_bgcolor_count   <= gs_bgcolor_count   + 1;
            if (gs_ev_event == trace_pkg::EV_WRITE)     gs_write_count     <= gs_write_count     + 1;
            if (gs_ev_event == trace_pkg::EV_PRIM_DRAW) gs_prim_draw_count <= gs_prim_draw_count + 1;
            if (gs_ev_event == trace_pkg::EV_MODE)      gs_mode_count      <= gs_mode_count      + 1;
        end
    end

    // Stimulus
    localparam logic [7:0]  CHCR_OFF     = 8'h00;
    localparam logic [7:0]  MADR_OFF     = 8'h10;
    localparam logic [7:0]  QWC_OFF      = 8'h20;
    localparam logic [31:0] PAYLOAD_MADR = 32'h0000_0100;
    localparam int          NUM_QWORDS   = 32;

    task automatic write_dmac(input logic [7:0] offset, input logic [31:0] data);
        @(negedge clk); dmac_reg_wr_en = 1'b1; dmac_reg_offset = offset; dmac_reg_wr_data = data;
        @(negedge clk); dmac_reg_wr_en = 1'b0; dmac_reg_offset = 8'd0; dmac_reg_wr_data = 32'd0;
    endtask

    task automatic preload_qword(input logic [RAM_ADDR_W-1:0] addr,
                                 input logic [127:0] data);
        @(negedge clk); ram_wr_en = 1'b1; ram_wr_addr = addr; ram_wr_data = data; ram_wr_be = 16'hFFFF;
        @(negedge clk); ram_wr_en = 1'b0; ram_wr_addr = '0; ram_wr_data = 128'd0; ram_wr_be = 16'd0;
    endtask

    function automatic logic [127:0] giftag(
        input logic [14:0] nloop, input logic eop,
        input logic [1:0] flg, input logic [3:0] nreg, input logic [63:0] regs);
        logic [63:0] lower;
        lower = {nreg, flg, 42'd0, eop, nloop};
        return {regs, lower};
    endfunction

    function automatic logic [127:0] aplusd_real(input logic [7:0]  reg_num,
                                                 input logic [63:0] data64);
        return {56'd0, reg_num, data64};
    endfunction

    localparam logic [7:0] R_PRIM  = 8'h00;
    localparam logic [7:0] R_RGBAQ = 8'h01;
    localparam logic [7:0] R_XYZ2  = 8'h05;

    localparam logic [63:0] PRIM_TRIANGLE  = 64'd3;
    localparam logic [63:0] PRIM_TRI_STRIP = 64'd4;
    localparam logic [63:0] PRIM_TRI_FAN   = 64'd5;

    // Distinct color values, far from vertex values for sanity
    localparam logic [63:0] COL_T_RED   = 64'h0000_0000_0000_00A1;
    localparam logic [63:0] COL_T_GREEN = 64'h0000_0000_0000_00B2;
    localparam logic [63:0] COL_T_BLUE  = 64'h0000_0000_0000_00C3;

    localparam logic [63:0] COL_S1 = 64'h0000_0000_0000_0CC1;
    localparam logic [63:0] COL_S2 = 64'h0000_0000_0000_0CC2;
    localparam logic [63:0] COL_S3 = 64'h0000_0000_0000_0CC3;
    localparam logic [63:0] COL_S4 = 64'h0000_0000_0000_0CC4;
    localparam logic [63:0] COL_S5 = 64'h0000_0000_0000_0CC5;

    localparam logic [63:0] COL_F1 = 64'h0000_0000_0000_0DD1;
    localparam logic [63:0] COL_F2 = 64'h0000_0000_0000_0DD2;
    localparam logic [63:0] COL_F3 = 64'h0000_0000_0000_0DD3;
    localparam logic [63:0] COL_F4 = 64'h0000_0000_0000_0DD4;
    localparam logic [63:0] COL_F5 = 64'h0000_0000_0000_0DD5;

    // Per-event capture (7 EV_PRIM_DRAW)
    logic [63:0] pd_v0    [0:6];
    logic [63:0] pd_v1    [0:6];
    logic [63:0] pd_v2    [0:6];
    logic [63:0] pd_cv0   [0:6];
    logic [63:0] pd_cv1   [0:6];
    logic [63:0] pd_cv2   [0:6];
    logic [63:0] pd_color [0:6];
    trace_pkg::color_t pd_dc0 [0:6];
    trace_pkg::color_t pd_dc1 [0:6];
    trace_pkg::color_t pd_dc2 [0:6];
    int          pd_idx;

    initial begin
        pd_idx = 0;
        for (int i = 0; i < 7; i = i + 1) begin
            pd_v0[i]    = 64'd0; pd_v1[i]    = 64'd0; pd_v2[i] = 64'd0;
            pd_cv0[i]   = 64'd0; pd_cv1[i]   = 64'd0; pd_cv2[i] = 64'd0;
            pd_color[i] = 64'd0;
        end
    end

    always_ff @(posedge clk) begin
        if (rst_n && gs_ev_valid && gs_ev_event == trace_pkg::EV_PRIM_DRAW
            && pd_idx < 7) begin
            pd_v0[pd_idx]    <= prim_v0_q;
            pd_v1[pd_idx]    <= prim_v1_q;
            pd_v2[pd_idx]    <= prim_v2_q;
            pd_cv0[pd_idx]   <= prim_color_v0_q;
            pd_cv1[pd_idx]   <= prim_color_v1_q;
            pd_cv2[pd_idx]   <= prim_color_v2_q;
            pd_color[pd_idx] <= prim_color_q;
            pd_dc0[pd_idx]   <= prim_v0_color_decoded_q;
            pd_dc1[pd_idx]   <= prim_v1_color_decoded_q;
            pd_dc2[pd_idx]   <= prim_v2_color_decoded_q;
            pd_idx           <= pd_idx + 1;
        end
    end

    initial begin
        rst_n = 1'b0;
        dmac_reg_wr_en = 1'b0; dmac_reg_offset = 8'd0; dmac_reg_wr_data = 32'd0;
        ram_wr_en = 1'b0; ram_wr_addr = '0; ram_wr_data = 128'd0; ram_wr_be = 16'd0;

        repeat (4) @(posedge clk); rst_n = 1'b1; repeat (8) @(posedge clk);

        begin : preload_block
            logic [RAM_ADDR_W-1:0] addr_q;

            // ---------- Tag1: discrete TRI, NREG=7 ----------
            // PRIM, RGBAQ_red, XYZ2_v1, RGBAQ_green, XYZ2_v2, RGBAQ_blue, XYZ2_v3
            addr_q = PAYLOAD_MADR + 32'd0;
            preload_qword(addr_q, giftag(15'd1, 1'b0, 2'd0, 4'd7,
                                         64'h0000_0000_0EEE_EEEE));
            addr_q = PAYLOAD_MADR + 32'd16;
            preload_qword(addr_q, aplusd_real(R_PRIM, PRIM_TRIANGLE));
            addr_q = PAYLOAD_MADR + 32'd32;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_T_RED));
            addr_q = PAYLOAD_MADR + 32'd48;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h100));
            addr_q = PAYLOAD_MADR + 32'd64;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_T_GREEN));
            addr_q = PAYLOAD_MADR + 32'd80;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h200));
            addr_q = PAYLOAD_MADR + 32'd96;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_T_BLUE));
            addr_q = PAYLOAD_MADR + 32'd112;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h300));

            // ---------- Tag2: TRI_STRIP, NREG=11 ----------
            // PRIM, then 5×(RGBAQ, XYZ2) — anchor v3, sat v4, sat v5
            addr_q = PAYLOAD_MADR + 32'd128;
            preload_qword(addr_q, giftag(15'd1, 1'b0, 2'd0, 4'd11,
                                         64'h0000_0EEE_EEEE_EEEE));
            addr_q = PAYLOAD_MADR + 32'd144;
            preload_qword(addr_q, aplusd_real(R_PRIM, PRIM_TRI_STRIP));
            addr_q = PAYLOAD_MADR + 32'd160;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_S1));
            addr_q = PAYLOAD_MADR + 32'd176;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h1100));
            addr_q = PAYLOAD_MADR + 32'd192;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_S2));
            addr_q = PAYLOAD_MADR + 32'd208;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h1200));
            addr_q = PAYLOAD_MADR + 32'd224;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_S3));
            addr_q = PAYLOAD_MADR + 32'd240;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h1300));
            addr_q = PAYLOAD_MADR + 32'd256;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_S4));
            addr_q = PAYLOAD_MADR + 32'd272;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h1400));
            addr_q = PAYLOAD_MADR + 32'd288;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_S5));
            addr_q = PAYLOAD_MADR + 32'd304;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h1500));

            // ---------- Tag3: TRI_FAN, NREG=11, EOP=1 ----------
            addr_q = PAYLOAD_MADR + 32'd320;
            preload_qword(addr_q, giftag(15'd1, 1'b1, 2'd0, 4'd11,
                                         64'h0000_0EEE_EEEE_EEEE));
            addr_q = PAYLOAD_MADR + 32'd336;
            preload_qword(addr_q, aplusd_real(R_PRIM, PRIM_TRI_FAN));
            addr_q = PAYLOAD_MADR + 32'd352;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_F1));
            addr_q = PAYLOAD_MADR + 32'd368;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h2100));
            addr_q = PAYLOAD_MADR + 32'd384;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_F2));
            addr_q = PAYLOAD_MADR + 32'd400;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h2200));
            addr_q = PAYLOAD_MADR + 32'd416;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_F3));
            addr_q = PAYLOAD_MADR + 32'd432;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h2300));
            addr_q = PAYLOAD_MADR + 32'd448;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_F4));
            addr_q = PAYLOAD_MADR + 32'd464;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h2400));
            addr_q = PAYLOAD_MADR + 32'd480;
            preload_qword(addr_q, aplusd_real(R_RGBAQ, COL_F5));
            addr_q = PAYLOAD_MADR + 32'd496;
            preload_qword(addr_q, aplusd_real(R_XYZ2, 64'h2500));
        end

        write_dmac(MADR_OFF, PAYLOAD_MADR);
        write_dmac(QWC_OFF,  NUM_QWORDS);
        write_dmac(CHCR_OFF, 32'h0000_0001);

        wait (dma_done_seen == 1'b1);
        repeat (40) @(posedge clk);

        $display("[tb_ee_core_gif_primitive_gouraud] dma=(%0d,%0d,%0d)  gif=(tag=%0d,wr=%0d,mode=%0d)  gs=(bg=%0d,wr=%0d,prim_draw=%0d,mode=%0d)  prim_complete_count=%0d",
                 dma_start_count, dma_beat_count, dma_done_count,
                 gif_giftag_count, gif_write_count, gif_mode_count,
                 gs_bgcolor_count, gs_write_count, gs_prim_draw_count, gs_mode_count,
                 prim_complete_count);

        if (dma_start_count != 1)         begin $error("dma_start"); errors = errors + 1; end
        if (dma_beat_count != NUM_QWORDS) begin $error("dma_beat=%0d (expected %0d)", dma_beat_count, NUM_QWORDS); errors = errors + 1; end
        if (dma_done_count != 1)          begin $error("dma_done"); errors = errors + 1; end
        if (gif_giftag_count != 3)        begin $error("gif_giftag=%0d (expected 3)", gif_giftag_count); errors = errors + 1; end
        if (gif_write_count != 29)        begin $error("gif_write=%0d (expected 29: 7+11+11)", gif_write_count); errors = errors + 1; end
        if (gif_mode_count != 0)          begin $error("gif_mode=%0d", gif_mode_count); errors = errors + 1; end
        if (gs_write_count != 22)         begin $error("gs_write=%0d (expected 22: TRI 6 + STRIP 8 + FAN 8)", gs_write_count); errors = errors + 1; end
        if (gs_prim_draw_count != 7)      begin $error("gs_prim_draw=%0d (expected 7: 1+3+3)", gs_prim_draw_count); errors = errors + 1; end
        if (gs_mode_count != 0)           begin $error("gs_mode=%0d", gs_mode_count); errors = errors + 1; end
        if (prim_complete_count != 32'd7) begin $error("prim_complete_count=%0d", prim_complete_count); errors = errors + 1; end

        // Per-event vertex + per-vertex color checks. The
        // load-bearing distinction is at events [5] and [6]:
        // TRI_FAN saturated extensions pin BOTH v0 and color_v0
        // to the pivot's identity, while TRI_STRIP would have
        // rolled them.
        //
        // [0] discrete TRI: v={0x100,0x200,0x300} c={red,green,blue}
        if (pd_v0[0]  !== 64'h100)       begin $error("pd_v0[0]=0x%h (expected 0x100)", pd_v0[0]); errors = errors + 1; end
        if (pd_v1[0]  !== 64'h200)       begin $error("pd_v1[0]=0x%h", pd_v1[0]); errors = errors + 1; end
        if (pd_v2[0]  !== 64'h300)       begin $error("pd_v2[0]=0x%h", pd_v2[0]); errors = errors + 1; end
        if (pd_cv0[0] !== COL_T_RED)     begin $error("pd_cv0[0]=0x%h (expected red 0xA1 — TRI v1 color)",   pd_cv0[0]); errors = errors + 1; end
        if (pd_cv1[0] !== COL_T_GREEN)   begin $error("pd_cv1[0]=0x%h (expected green 0xB2 — TRI v2 color)", pd_cv1[0]); errors = errors + 1; end
        if (pd_cv2[0] !== COL_T_BLUE)    begin $error("pd_cv2[0]=0x%h (expected blue 0xC3 — TRI v3 color)",  pd_cv2[0]); errors = errors + 1; end
        if (pd_color[0] !== COL_T_BLUE)  begin $error("pd_color[0]=0x%h (expected closing color = blue)", pd_color[0]); errors = errors + 1; end

        // [1] STRIP anchor v3: v={0x1100,0x1200,0x1300} c={s1,s2,s3}
        if (pd_v0[1]  !== 64'h1100)      begin $error("pd_v0[1]=0x%h",  pd_v0[1]);  errors = errors + 1; end
        if (pd_v1[1]  !== 64'h1200)      begin $error("pd_v1[1]=0x%h",  pd_v1[1]);  errors = errors + 1; end
        if (pd_v2[1]  !== 64'h1300)      begin $error("pd_v2[1]=0x%h",  pd_v2[1]);  errors = errors + 1; end
        if (pd_cv0[1] !== COL_S1)        begin $error("pd_cv0[1]=0x%h (expected s1)", pd_cv0[1]); errors = errors + 1; end
        if (pd_cv1[1] !== COL_S2)        begin $error("pd_cv1[1]=0x%h (expected s2)", pd_cv1[1]); errors = errors + 1; end
        if (pd_cv2[1] !== COL_S3)        begin $error("pd_cv2[1]=0x%h (expected s3)", pd_cv2[1]); errors = errors + 1; end

        // [2] STRIP sat v4: v={0x1200,0x1300,0x1400} c={s2,s3,s4} — colors roll
        if (pd_v0[2]  !== 64'h1200)      begin $error("pd_v0[2]=0x%h",  pd_v0[2]);  errors = errors + 1; end
        if (pd_v1[2]  !== 64'h1300)      begin $error("pd_v1[2]=0x%h",  pd_v1[2]);  errors = errors + 1; end
        if (pd_v2[2]  !== 64'h1400)      begin $error("pd_v2[2]=0x%h",  pd_v2[2]);  errors = errors + 1; end
        if (pd_cv0[2] !== COL_S2)        begin $error("pd_cv0[2]=0x%h (expected s2 — color rolls with vertex)", pd_cv0[2]); errors = errors + 1; end
        if (pd_cv1[2] !== COL_S3)        begin $error("pd_cv1[2]=0x%h (expected s3)", pd_cv1[2]); errors = errors + 1; end
        if (pd_cv2[2] !== COL_S4)        begin $error("pd_cv2[2]=0x%h (expected s4)", pd_cv2[2]); errors = errors + 1; end

        // [3] STRIP sat v5: v={0x1300,0x1400,0x1500} c={s3,s4,s5}
        if (pd_v0[3]  !== 64'h1300)      begin $error("pd_v0[3]=0x%h",  pd_v0[3]);  errors = errors + 1; end
        if (pd_cv0[3] !== COL_S3)        begin $error("pd_cv0[3]=0x%h", pd_cv0[3]); errors = errors + 1; end
        if (pd_cv2[3] !== COL_S5)        begin $error("pd_cv2[3]=0x%h", pd_cv2[3]); errors = errors + 1; end

        // [4] FAN anchor v3: v={0x2100,0x2200,0x2300} c={d1,d2,d3} — pivot ≡ prev
        if (pd_v0[4]  !== 64'h2100)      begin $error("pd_v0[4]=0x%h",  pd_v0[4]);  errors = errors + 1; end
        if (pd_v1[4]  !== 64'h2200)      begin $error("pd_v1[4]=0x%h",  pd_v1[4]);  errors = errors + 1; end
        if (pd_v2[4]  !== 64'h2300)      begin $error("pd_v2[4]=0x%h",  pd_v2[4]);  errors = errors + 1; end
        if (pd_cv0[4] !== COL_F1)        begin $error("pd_cv0[4]=0x%h (expected d1)", pd_cv0[4]); errors = errors + 1; end
        if (pd_cv1[4] !== COL_F2)        begin $error("pd_cv1[4]=0x%h (expected d2)", pd_cv1[4]); errors = errors + 1; end
        if (pd_cv2[4] !== COL_F3)        begin $error("pd_cv2[4]=0x%h (expected d3)", pd_cv2[4]); errors = errors + 1; end

        // [5] FAN sat v4: v={pivot=0x2100, 0x2300, 0x2400}  c={d1, d3, d4}
        // *** LOAD-BEARING ***  v0 AND color_v0 are pinned to the pivot's
        // identity. A TRI_STRIP at the same stream would have rolled to
        // {v=0x2200, c=d2} for v0 here.
        if (pd_v0[5]  !== 64'h2100)      begin $error("pd_v0[5]=0x%h (expected 0x2100 — FAN pivot vertex PINNED)", pd_v0[5]); errors = errors + 1; end
        if (pd_v1[5]  !== 64'h2300)      begin $error("pd_v1[5]=0x%h (expected 0x2300)", pd_v1[5]); errors = errors + 1; end
        if (pd_v2[5]  !== 64'h2400)      begin $error("pd_v2[5]=0x%h (expected 0x2400)", pd_v2[5]); errors = errors + 1; end
        if (pd_cv0[5] !== COL_F1)        begin $error("pd_cv0[5]=0x%h (expected d1 — FAN pivot COLOR PINNED, not d2)", pd_cv0[5]); errors = errors + 1; end
        if (pd_cv1[5] !== COL_F3)        begin $error("pd_cv1[5]=0x%h (expected d3 — color rolls at v1)", pd_cv1[5]); errors = errors + 1; end
        if (pd_cv2[5] !== COL_F4)        begin $error("pd_cv2[5]=0x%h (expected d4 — closing color)", pd_cv2[5]); errors = errors + 1; end

        // [6] FAN sat v5: v={pivot=0x2100, 0x2400, 0x2500} c={d1, d4, d5}
        if (pd_v0[6]  !== 64'h2100)      begin $error("pd_v0[6]=0x%h (expected pivot 0x2100)", pd_v0[6]); errors = errors + 1; end
        if (pd_cv0[6] !== COL_F1)        begin $error("pd_cv0[6]=0x%h (expected d1 — pivot color still pinned)", pd_cv0[6]); errors = errors + 1; end
        if (pd_cv1[6] !== COL_F4)        begin $error("pd_cv1[6]=0x%h (expected d4)", pd_cv1[6]); errors = errors + 1; end
        if (pd_cv2[6] !== COL_F5)        begin $error("pd_cv2[6]=0x%h (expected d5 — closing)", pd_cv2[6]); errors = errors + 1; end

        // Backward-compat: prim_color_q always equals the closing-vertex color.
        if (pd_color[1] !== COL_S3)      begin $error("pd_color[1]=0x%h (expected s3 — STRIP anchor close)", pd_color[1]); errors = errors + 1; end
        if (pd_color[5] !== COL_F4)      begin $error("pd_color[5]=0x%h (expected d4 — FAN sat close)", pd_color[5]); errors = errors + 1; end
        if (pd_color[6] !== COL_F5)      begin $error("pd_color[6]=0x%h (expected d5 — final FAN close)", pd_color[6]); errors = errors + 1; end

        // Ch81 decoded-color checks. The load-bearing distinction is
        // FAN saturated [5]/[6]: pd_dc0 (vertex-0 color) carries d1's
        // decoded fields (r=0xD1) — pivot color PINNED — distinct
        // from where TRI_STRIP rolling would put d2/d3.
        // [0] discrete TRIANGLE: r decoded from each vertex color
        begin
            trace_pkg::color_t dc0, dc1, dc2;
            dc0 = pd_dc0[0]; dc1 = pd_dc1[0]; dc2 = pd_dc2[0];
            if (dc0.r !== 8'hA1) begin $error("dc0[0].r=0x%h (expected 0xA1 — TRI v1 red)",   dc0.r); errors = errors + 1; end
            if (dc1.r !== 8'hB2) begin $error("dc1[0].r=0x%h (expected 0xB2 — TRI v2 green)", dc1.r); errors = errors + 1; end
            if (dc2.r !== 8'hC3) begin $error("dc2[0].r=0x%h (expected 0xC3 — TRI v3 blue)",  dc2.r); errors = errors + 1; end
        end
        // [3] TRI_STRIP saturated v5: rolling colors {s3, s4, s5}
        begin
            trace_pkg::color_t dc0, dc1, dc2;
            dc0 = pd_dc0[3]; dc1 = pd_dc1[3]; dc2 = pd_dc2[3];
            if (dc0.r !== 8'hC3) begin $error("dc0[3].r=0x%h (expected 0xC3 — STRIP rolled v_n-2 color)", dc0.r); errors = errors + 1; end
            if (dc1.r !== 8'hC4) begin $error("dc1[3].r=0x%h (expected 0xC4)", dc1.r); errors = errors + 1; end
            if (dc2.r !== 8'hC5) begin $error("dc2[3].r=0x%h (expected 0xC5 — STRIP closing color)", dc2.r); errors = errors + 1; end
        end
        // [5] TRI_FAN saturated v4: *** pivot color pinned ***
        begin
            trace_pkg::color_t dc0, dc1, dc2;
            dc0 = pd_dc0[5]; dc1 = pd_dc1[5]; dc2 = pd_dc2[5];
            if (dc0.r !== 8'hD1) begin $error("dc0[5].r=0x%h (expected 0xD1 — FAN PIVOT color PINNED, not rolled)", dc0.r); errors = errors + 1; end
            if (dc1.r !== 8'hD3) begin $error("dc1[5].r=0x%h (expected 0xD3)", dc1.r); errors = errors + 1; end
            if (dc2.r !== 8'hD4) begin $error("dc2[5].r=0x%h (expected 0xD4 — closing color)", dc2.r); errors = errors + 1; end
        end
        // [6] TRI_FAN saturated v5: pivot color still pinned
        begin
            trace_pkg::color_t dc0, dc1, dc2;
            dc0 = pd_dc0[6]; dc1 = pd_dc1[6]; dc2 = pd_dc2[6];
            if (dc0.r !== 8'hD1) begin $error("dc0[6].r=0x%h (expected 0xD1 — pivot still pinned)", dc0.r); errors = errors + 1; end
            if (dc1.r !== 8'hD4) begin $error("dc1[6].r=0x%h (expected 0xD4)", dc1.r); errors = errors + 1; end
            if (dc2.r !== 8'hD5) begin $error("dc2[6].r=0x%h (expected 0xD5)", dc2.r); errors = errors + 1; end
        end

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

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

endmodule : tb_ee_core_gif_primitive_gouraud
