// retroDE_ps2 — tb_ee_core_gif_raster_gouraud (Ch86)
//
// Verifies the Ch86 per-pixel Gouraud color interpolation:
//   - Three vertex colors (red / green / blue) are placed at the
//     three vertices of a triangle.
//   - The rasterizer interpolates R/G/B/A per pixel using
//     barycentric weights derived from the edge functions, then
//     divides by the signed area.
//   - Q (texture-coord float) passes through from the closing
//     vertex's RGBAQ — not interpolated.
//
// Triangle: A=(5,5)/red, B=(8,5)/green, C=(5,8)/blue.
//   Signed area = (8-5)*(8-5) - 0 = 9. CCW. No swap.
//   Top-left rule on edges (post-swap CCW):
//     A→B (top, dy=0/dx>0) inclusive
//     B→C (left, dy>0)     inclusive
//     C→A (right, dy<0)    exclusive
//   Bounding box [5..8]×[5..8], inside iff px>=6 AND px+py<=13.
//   → 6 pixels: (6,5), (7,5), (8,5), (6,6), (7,6), (6,7).
//
// Barycentric weights at each pixel:
//   L0 = -e1 = 3*(13 - px - py)   (weight of v0=A=red)
//   L1 = -e2 =  3*(px - 5)        (weight of v1=B=green)
//   L2 = -e0 =  3*(py - 5)        (weight of v2=C=blue)
//   sum = sa = 9.
//
// Per-channel: ch_out = (L0*c0.ch + L1*c1.ch + L2*c2.ch) / 9.
//
// With c0=red(0xFF,0,0,0), c1=green(0,0xFF,0,0), c2=blue(0,0,0xFF,0):
//   For each pixel (px, py), the interpolated RGB simplifies to:
//     R = L0 * 0xFF / 9 = (13-px-py) * 0x55
//     G = L1 * 0xFF / 9 = (px-5)     * 0x55
//     B = L2 * 0xFF / 9 = (py-5)     * 0x55
//   (Q stays at 0 since c2.q=0; A stays at 0.)
//
//   (6,5): R=2*0x55=0xAA, G=1*0x55=0x55, B=0    → 0x0000_0000_0000_55AA
//   (7,5): R=1*0x55=0x55, G=2*0x55=0xAA, B=0    → 0x0000_0000_0000_AA55
//   (8,5): R=0,            G=3*0x55=0xFF, B=0    → 0x0000_0000_0000_FF00
//   (6,6): R=1*0x55,      G=1*0x55,      B=1*0x55→ 0x0000_0000_0055_5555
//   (7,6): R=0,            G=2*0x55=0xAA, B=1*0x55=0x55 → 0x0000_0000_0055_AA00
//   (6,7): R=0,            G=1*0x55=0x55, B=2*0x55=0xAA → 0x0000_0000_00AA_5500
//
// Sanity check on the gradient: at vertex A (6,5 is closest to (5,5))
// the color is mostly red; at B (8,5) it's pure green; at C the
// gradient extends toward blue. Interpolation visibly varies across
// the 6 emitted pixels — a flat-shaded raster would not.
//
// FBP=2 / FBW=10 / PSMCT32. Expected fb_addrs:
//   (6,5) → 0x4218
//   (7,5) → 0x421C
//   (8,5) → 0x4220
//   (6,6) → 0x4C18
//   (7,6) → 0x4C1C
//   (6,7) → 0x5618
//
// Pass criteria:
//   - DMA: 1 START / 1 DONE
//   - GS:  1 EV_PRIM_DRAW
//   - pixel_emit_count == 1 (Ch82 channel)
//   - raster_pixel_emit_count == 6 (3 colors × 6 pixels)
//   - raster_overflow == 0
//   - raster_degenerate == 0
//   - per-emit captures match the expected (x, y, fb_addr, color)
//     tuples — verifies non-flat interpolation across the interior.

`timescale 1ns/1ps

module tb_ee_core_gif_raster_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;

    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;

    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];

    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)
    );

    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                pixel_emit;
    logic [31:0]         pixel_emit_count;
    logic [11:0]         pixel_x_q, pixel_y_q;
    logic [63:0]         pixel_color_q;
    logic [8:0]          pixel_fbp_q;
    logic [5:0]          pixel_fbw_q, pixel_psm_q;
    logic [31:0]         pixel_fb_addr_q;
    logic                raster_pixel_emit;
    logic [31:0]         raster_pixel_emit_count;
    logic [11:0]         raster_pixel_x_q, raster_pixel_y_q;
    logic [63:0]         raster_pixel_color_q;
    logic [31:0]         raster_pixel_fb_addr_q;
    logic                raster_active;
    logic                raster_overflow;
    logic                raster_degenerate;
    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),
        .pixel_emit(pixel_emit),
        .pixel_emit_count(pixel_emit_count),
        .pixel_x_q(pixel_x_q), .pixel_y_q(pixel_y_q),
        .pixel_color_q(pixel_color_q),
        .pixel_fbp_q(pixel_fbp_q),
        .pixel_fbw_q(pixel_fbw_q),
        .pixel_psm_q(pixel_psm_q),
        .pixel_fb_addr_q(pixel_fb_addr_q),
        .raster_pixel_emit(raster_pixel_emit),
        .raster_pixel_emit_count(raster_pixel_emit_count),
        .raster_pixel_x_q(raster_pixel_x_q),
        .raster_pixel_y_q(raster_pixel_y_q),
        .raster_pixel_color_q(raster_pixel_color_q),
        .raster_pixel_fb_addr_q(raster_pixel_fb_addr_q),
        .raster_active(raster_active),
        .raster_overflow(raster_overflow),
        .raster_degenerate(raster_degenerate),
        .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_raster_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_raster_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_raster_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));

    int dma_start_count, dma_done_count;
    int gs_prim_draw_count;
    int pixel_emit_pulse_count;
    int raster_pixel_pulse_count;
    int errors;

    initial begin
        dma_start_count = 0; dma_done_count = 0;
        gs_prim_draw_count = 0;
        pixel_emit_pulse_count = 0;
        raster_pixel_pulse_count = 0;
        errors = 0;
    end

    always_ff @(posedge clk) begin
        if (rst_n && dmac_ev_valid) begin
            if (dmac_ev_event == trace_pkg::EV_DMA_START) dma_start_count <= dma_start_count + 1;
            if (dmac_ev_event == trace_pkg::EV_DMA_DONE)  dma_done_count  <= dma_done_count  + 1;
        end
        if (rst_n && gs_ev_valid && gs_ev_event == trace_pkg::EV_PRIM_DRAW)
            gs_prim_draw_count <= gs_prim_draw_count + 1;
        if (rst_n && pixel_emit)        pixel_emit_pulse_count   <= pixel_emit_pulse_count   + 1;
        if (rst_n && raster_pixel_emit) raster_pixel_pulse_count <= raster_pixel_pulse_count + 1;
    end

    logic [11:0] cap_x       [0:5];
    logic [11:0] cap_y       [0:5];
    logic [63:0] cap_color   [0:5];
    logic [31:0] cap_fb_addr [0:5];
    int          cap_idx;

    initial begin
        cap_idx = 0;
        for (int i = 0; i < 6; i = i + 1) begin
            cap_x[i] = 0; cap_y[i] = 0;
            cap_color[i] = 0; cap_fb_addr[i] = 0;
        end
    end

    always_ff @(posedge clk) begin
        if (rst_n && raster_pixel_emit && cap_idx < 6) begin
            cap_x[cap_idx]       <= raster_pixel_x_q;
            cap_y[cap_idx]       <= raster_pixel_y_q;
            cap_color[cap_idx]   <= raster_pixel_color_q;
            cap_fb_addr[cap_idx] <= raster_pixel_fb_addr_q;
            cap_idx              <= cap_idx + 1;
        end
    end

    localparam logic [7:0]  CHCR_OFF = 8'h00;
    localparam logic [7:0]  MADR_OFF = 8'h10;
    localparam logic [7:0]  QWC_OFF  = 8'h20;

    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

    function automatic logic [63:0] xyz2_data(input logic [11:0] x_int,
                                              input logic [11:0] y_int);
        return {32'd0, y_int, 4'd0, x_int, 4'd0};
    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 [7:0] R_FRAME_1 = 8'h4C;

    localparam logic [63:0] PRIM_TRIANGLE = 64'd3;
    localparam logic [63:0] FRAME_1_VAL   = 64'h0000_0000_000A_0002;
    localparam logic [63:0] COL_RED       = 64'h0000_0000_0000_00FF;
    localparam logic [63:0] COL_GREEN     = 64'h0000_0000_0000_FF00;
    localparam logic [63:0] COL_BLUE      = 64'h0000_0000_00FF_0000;

    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;

            // Single TRI tag with 3 distinct vertex colors.
            // NREG=8: PRIM, FRAME_1, RGBAQ_red, XYZ2_A,
            //         RGBAQ_green, XYZ2_B, RGBAQ_blue, XYZ2_C
            addr_q = 32'h100 + 32'd0;
            preload_qword(addr_q, giftag(15'd1, 1'b1, 2'd0, 4'd8,
                                         64'h0000_0000_EEEE_EEEE));
            addr_q = 32'h100 + 32'd16;
            preload_qword(addr_q, aplusd_real(R_PRIM,    PRIM_TRIANGLE));
            addr_q = 32'h100 + 32'd32;
            preload_qword(addr_q, aplusd_real(R_FRAME_1, FRAME_1_VAL));
            addr_q = 32'h100 + 32'd48;
            preload_qword(addr_q, aplusd_real(R_RGBAQ,   COL_RED));
            addr_q = 32'h100 + 32'd64;
            preload_qword(addr_q, aplusd_real(R_XYZ2, xyz2_data(12'd5, 12'd5)));    // A red
            addr_q = 32'h100 + 32'd80;
            preload_qword(addr_q, aplusd_real(R_RGBAQ,   COL_GREEN));
            addr_q = 32'h100 + 32'd96;
            preload_qword(addr_q, aplusd_real(R_XYZ2, xyz2_data(12'd8, 12'd5)));    // B green
            addr_q = 32'h100 + 32'd112;
            preload_qword(addr_q, aplusd_real(R_RGBAQ,   COL_BLUE));
            addr_q = 32'h100 + 32'd128;
            preload_qword(addr_q, aplusd_real(R_XYZ2, xyz2_data(12'd5, 12'd8)));    // C blue closes
        end

        write_dmac(MADR_OFF, 32'h0000_0100);
        write_dmac(QWC_OFF,  32'd9);
        write_dmac(CHCR_OFF, 32'h0000_0001);

        wait (raster_active == 1'b0);
        // Even after the DMA done event the raster FSM may still be
        // mid-scan; wait a bit longer to be safe.
        repeat (80) @(posedge clk);

        $display("[tb_ee_core_gif_raster_gouraud] dma=%0d/%0d  prim_draw=%0d  pixel_emit=%0d  raster_emit=%0d  raster_overflow=%b  raster_degenerate=%b",
                 dma_start_count, dma_done_count, gs_prim_draw_count,
                 pixel_emit_pulse_count, raster_pixel_pulse_count,
                 raster_overflow, raster_degenerate);
        for (int i = 0; i < 6; i = i + 1) begin
            $display("[tb_ee_core_gif_raster_gouraud] cap[%0d] (%0d,%0d) fb=0x%h color=0x%h",
                     i, cap_x[i], cap_y[i], cap_fb_addr[i], cap_color[i]);
        end

        if (dma_start_count != 1)         begin $error("dma_start"); errors = errors + 1; end
        if (dma_done_count != 1)          begin $error("dma_done"); errors = errors + 1; end
        if (gs_prim_draw_count != 1)      begin $error("gs_prim_draw=%0d (expected 1)", gs_prim_draw_count); errors = errors + 1; end
        if (raster_pixel_emit_count != 32'd6) begin $error("raster_pixel_emit_count=%0d (expected 6)", raster_pixel_emit_count); errors = errors + 1; end
        if (raster_pixel_pulse_count != 6) begin $error("raster_emit pulses=%0d (expected 6)", raster_pixel_pulse_count); errors = errors + 1; end
        if (raster_overflow !== 1'b0)     begin $error("raster_overflow=%b (expected 0)", raster_overflow); errors = errors + 1; end
        if (raster_degenerate !== 1'b0)   begin $error("raster_degenerate=%b (expected 0)", raster_degenerate); errors = errors + 1; end

        // Per-pixel checks. fb_addr first (verifies pixel coords are
        // in the right place), then color (verifies interpolation).
        if (cap_x[0] !== 12'd6 || cap_y[0] !== 12'd5 || cap_fb_addr[0] !== 32'h0000_4218) begin
            $error("cap[0] (%0d,%0d) fb=0x%h (expected (6,5) 0x4218)", cap_x[0], cap_y[0], cap_fb_addr[0]); errors = errors + 1;
        end
        if (cap_color[0] !== 64'h0000_0000_0000_55AA) begin
            $error("cap[0].color=0x%h (expected 0x55AA — R=0xAA G=0x55 B=0)", cap_color[0]); errors = errors + 1;
        end

        if (cap_x[1] !== 12'd7 || cap_y[1] !== 12'd5 || cap_fb_addr[1] !== 32'h0000_421C) begin
            $error("cap[1] (%0d,%0d) fb=0x%h", cap_x[1], cap_y[1], cap_fb_addr[1]); errors = errors + 1;
        end
        if (cap_color[1] !== 64'h0000_0000_0000_AA55) begin
            $error("cap[1].color=0x%h (expected 0xAA55 — R=0x55 G=0xAA B=0)", cap_color[1]); errors = errors + 1;
        end

        if (cap_x[2] !== 12'd8 || cap_y[2] !== 12'd5 || cap_fb_addr[2] !== 32'h0000_4220) begin
            $error("cap[2] (%0d,%0d) fb=0x%h", cap_x[2], cap_y[2], cap_fb_addr[2]); errors = errors + 1;
        end
        if (cap_color[2] !== 64'h0000_0000_0000_FF00) begin
            $error("cap[2].color=0x%h (expected 0xFF00 — pure green at vertex B)", cap_color[2]); errors = errors + 1;
        end

        if (cap_x[3] !== 12'd6 || cap_y[3] !== 12'd6 || cap_fb_addr[3] !== 32'h0000_4C18) begin
            $error("cap[3] (%0d,%0d) fb=0x%h", cap_x[3], cap_y[3], cap_fb_addr[3]); errors = errors + 1;
        end
        if (cap_color[3] !== 64'h0000_0000_0055_5555) begin
            $error("cap[3].color=0x%h (expected 0x555555 — equal RGB at centroid)", cap_color[3]); errors = errors + 1;
        end

        if (cap_x[4] !== 12'd7 || cap_y[4] !== 12'd6 || cap_fb_addr[4] !== 32'h0000_4C1C) begin
            $error("cap[4] (%0d,%0d) fb=0x%h", cap_x[4], cap_y[4], cap_fb_addr[4]); errors = errors + 1;
        end
        if (cap_color[4] !== 64'h0000_0000_0055_AA00) begin
            $error("cap[4].color=0x%h (expected 0x55AA00)", cap_color[4]); errors = errors + 1;
        end

        if (cap_x[5] !== 12'd6 || cap_y[5] !== 12'd7 || cap_fb_addr[5] !== 32'h0000_5618) begin
            $error("cap[5] (%0d,%0d) fb=0x%h", cap_x[5], cap_y[5], cap_fb_addr[5]); errors = errors + 1;
        end
        if (cap_color[5] !== 64'h0000_0000_00AA_5500) begin
            $error("cap[5].color=0x%h (expected 0xAA5500)", cap_color[5]); errors = errors + 1;
        end

        // Sanity: all 6 colors are distinct (proves real interpolation).
        for (int i = 0; i < 6; i = i + 1) begin
            for (int j = i + 1; j < 6; j = j + 1) begin
                if (cap_color[i] === cap_color[j]) begin
                    $error("FLAT-NOT-GOURAUD: cap[%0d] and cap[%0d] same color 0x%h",
                           i, j, cap_color[i]);
                    errors = errors + 1;
                end
            end
        end

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

    initial begin
        #5000000;
        $error("[tb_ee_core_gif_raster_gouraud] timeout");
        $finish;
    end

endmodule : tb_ee_core_gif_raster_gouraud
