// retroDE_ps2 — tb_top_psmct32_feeder_colors_demo (Ch333)
//
// VISUAL PAYLOAD DIVERSITY: the runtime feeder now controls COLOR, not just geometry. A unity
// (0x80) texture + TEX0.TFX=MODULATE means the staging RGBAQ flows through the texel unchanged, so
// each primitive's color is host-controlled at runtime. Three scenes (all <= FIFO_DEPTH), switched
// with no rebuild/reset:
//   COLOR_TRI  : red / green / blue TRIANGLES        tiles {0,5,10}
//   COLOR_RECT : red / green / blue filled QUADS      tiles {0,5,10}
//   COLOR_MIX  : red tri(0) + green rect(5) + blue tri(10) + yellow rect(15)  (shape AND color vary)
//
// PROOF: each scene tile shows its EXACT staging RGBAQ color (not blue), empties stay green clean,
// records == prim count. The DECAL anchors (Ch330-332) stay blue — verified by their own TBs.

`timescale 1ns/1ps

module tb_top_psmct32_feeder_colors_demo;

    localparam int H_ACTIVE = 64, V_ACTIVE = 64;
    localparam real MARG = 0.12;
    localparam logic [1:0] C_RUN = 2'd1, C_READY = 2'd3;

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

    logic        core_go;
    logic [7:0]  r, g, b;
    logic        hsync, vsync, de;
    logic        core_halt, dma_done_seen, frame_seen, raster_overflow;
    logic        frame_toggle, dma_done_toggle, feeder_go_tb;

    top_psmct32_raster_demo_bram #(
        .H_ACTIVE(H_ACTIVE), .V_ACTIVE(V_ACTIVE),
        .VRAM_BYTES(32*1024), .PSMCT32_SWIZZLE(1'b0),
        .COMBINED_TAZ(1'b1), .TILE_LOCAL(1'b1), .TILE_COLS(4), .TILE_ROWS(4),
        .TILE_MULTIPRIM(1'b1), .TILE_PRIM_COUNT(4), .TILE_FIFO_DEPTH(8),
        .BIN_BUFFER_ENABLE(1'b1), .HEARTBEAT_SPLICE_ENABLE(1'b0),
        .FEEDER_ENABLE(1'b1), .FEEDER_STG_WORDS(256)
    ) dut (
        .clk(clk), .rst_n(rst_n), .core_go(core_go),
        .r(r), .g(g), .b(b), .hsync(hsync), .vsync(vsync), .de(de),
        .core_halt(core_halt), .dma_done_seen(dma_done_seen),
        .frame_seen(frame_seen), .raster_overflow(raster_overflow),
        .frame_toggle(frame_toggle), .dma_done_toggle(dma_done_toggle),
        .joy_a_pressed_i(1'b0), .joy_b_pressed_i(1'b0),
        .feeder_stg_we_i(1'b0), .feeder_stg_waddr_i(12'd0), .feeder_stg_wdata_i(64'd0),
        .feeder_go_i(feeder_go_tb),
        .feeder_ready_o(), .feeder_records_o(), .feeder_waits_o()
    );

    logic [7:0] cap_r[0:V_ACTIVE-1][0:H_ACTIVE-1], cap_g[0:V_ACTIVE-1][0:H_ACTIVE-1], cap_b[0:V_ACTIVE-1][0:H_ACTIVE-1];
    logic       cap_de[0:V_ACTIVE-1][0:H_ACTIVE-1];
    bit capture_armed;
    task automatic clear_cap;
        for (int y=0;y<V_ACTIVE;y++) for (int x=0;x<H_ACTIVE;x++) begin
            cap_r[y][x]=0; cap_g[y][x]=0; cap_b[y][x]=0; cap_de[y][x]=0; end
    endtask
    initial begin clear_cap(); capture_armed=1'b0; end
    logic [31:0] hcnt_d, vcnt_d;
    always_ff @(posedge clk) begin
        if (!rst_n) begin hcnt_d<=0; vcnt_d<=0; end
        else begin hcnt_d<=32'(dut.u_pcrtc.hcnt); vcnt_d<=32'(dut.u_pcrtc.vcnt); end
    end
    always_ff @(posedge clk)
        if (rst_n && capture_armed && de && (vcnt_d<V_ACTIVE) && (hcnt_d<H_ACTIVE)) begin
            cap_r[vcnt_d][hcnt_d]<=r; cap_g[vcnt_d][hcnt_d]<=g; cap_b[vcnt_d][hcnt_d]<=b; cap_de[vcnt_d][hcnt_d]<=1'b1; end

    // count pixels in tile-box [bx0..+15]^2 that exactly match (er,eg,eb)
    function automatic int color_in_box(input int bx0, input int by0, input logic [7:0] er, eg, eb);
        int n; begin n=0;
            for (int y=by0;y<by0+16;y++) for (int x=bx0;x<bx0+16;x++)
                if (cap_de[y][x] && cap_r[y][x]===er && cap_g[y][x]===eg && cap_b[y][x]===eb) n++;
            color_in_box = n;
        end
    endfunction

    task automatic capture_frame;
        clear_cap();
        @(posedge dut.u_pcrtc.end_of_frame); @(posedge clk); capture_armed=1'b1;
        @(posedge dut.u_pcrtc.end_of_frame); @(posedge clk); capture_armed=1'b0;
    endtask
    task automatic stage_and_run(input string memfile);
        $readmemh(memfile, dut.g_feeder.feeder_stg);
        @(negedge clk); feeder_go_tb=1'b1; @(negedge clk); feeder_go_tb=1'b0;
        wait (dut.g_feeder.cst == C_RUN); wait (dut.g_feeder.cst == C_READY);
        capture_frame();
    endtask

    int errors;
    localparam logic [7:0] RR=8'hFF, GG=8'hFF, BB=8'hFF, ZZ=8'h00;
    // assert tile t shows >=15 px of exactly (er,eg,eb)
    task automatic want_color(input string label, input int t, input logic [7:0] er, eg, eb);
        int nb; nb = color_in_box((t%4)*16, (t/4)*16, er, eg, eb);
        if (nb < 15) begin $error("[%s] tile %0d wants color (%02x,%02x,%02x) but only %0d px", label, t, er,eg,eb, nb); errors++; end
    endtask
    task automatic want_green_clear(input string label, input int t);   // empty tile = green clear (0x008000)
        int nb; nb = color_in_box((t%4)*16, (t/4)*16, 8'h00, 8'h80, 8'h00);
        if (nb < 200) begin $error("[%s] tile %0d not clean green-clear (%0d px)", label, t, nb); errors++; end
    endtask

    initial begin
        errors=0; feeder_go_tb=1'b0;
        $readmemh(`FEEDER_COLOR_TRI_FILE, dut.g_feeder.feeder_stg);   // power-up scene = COLOR_TRI

        rst_n=1'b0; core_go=1'b0;
        repeat(4) @(posedge clk); rst_n=1'b1; repeat(8) @(posedge clk);
        @(negedge clk); core_go=1'b1; @(negedge clk); core_go=1'b0;
        wait (core_halt==1'b1); repeat(4) @(posedge clk);
        wait (dma_done_seen==1'b1); repeat(10) @(posedge clk);
        if (dut.xfer_busy==1'b1) wait (dut.xfer_busy==1'b0);

        // COLOR_TRI: red(0) green(5) blue(10) triangles
        wait (dut.g_feeder.cst == C_READY); capture_frame();
        want_color("COLOR_TRI", 0, RR,ZZ,ZZ); want_color("COLOR_TRI", 5, ZZ,GG,ZZ); want_color("COLOR_TRI", 10, ZZ,ZZ,BB);
        if (int'(dut.feeder_records)!=3) begin $error("COLOR_TRI records=%0d (want 3)", int'(dut.feeder_records)); errors++; end
        $display("[COLOR_TRI] red0=%0d green5=%0d blue10=%0d records=%0d",
                 color_in_box(0,0,RR,ZZ,ZZ), color_in_box(16,16,ZZ,GG,ZZ), color_in_box(32,32,ZZ,ZZ,BB), int'(dut.feeder_records));

        // COLOR_RECT: red/green/blue filled quads
        stage_and_run(`FEEDER_COLOR_RECT_FILE);
        want_color("COLOR_RECT", 0, RR,ZZ,ZZ); want_color("COLOR_RECT", 5, ZZ,GG,ZZ); want_color("COLOR_RECT", 10, ZZ,ZZ,BB);
        if (int'(dut.feeder_records)!=6) begin $error("COLOR_RECT records=%0d (want 6)", int'(dut.feeder_records)); errors++; end
        $display("[COLOR_RECT] red0=%0d green5=%0d blue10=%0d records=%0d",
                 color_in_box(0,0,RR,ZZ,ZZ), color_in_box(16,16,ZZ,GG,ZZ), color_in_box(32,32,ZZ,ZZ,BB), int'(dut.feeder_records));

        // COLOR_MIX: red tri(0), green rect(5), blue tri(10), yellow rect(15) — shape AND color vary
        stage_and_run(`FEEDER_COLOR_MIX_FILE);
        want_color("COLOR_MIX", 0, RR,ZZ,ZZ); want_color("COLOR_MIX", 5, ZZ,GG,ZZ);
        want_color("COLOR_MIX", 10, ZZ,ZZ,BB); want_color("COLOR_MIX", 15, RR,GG,ZZ);  // yellow
        if (int'(dut.feeder_records)!=6) begin $error("COLOR_MIX records=%0d (want 6)", int'(dut.feeder_records)); errors++; end
        $display("[COLOR_MIX] red0=%0d green5=%0d blue10=%0d yellow15=%0d records=%0d",
                 color_in_box(0,0,RR,ZZ,ZZ), color_in_box(16,16,ZZ,GG,ZZ), color_in_box(32,32,ZZ,ZZ,BB), color_in_box(48,48,RR,GG,ZZ), int'(dut.feeder_records));

        if (raster_overflow) begin $error("raster_overflow set"); errors++; end
        if (!core_halt)      begin $error("core_halt low"); errors++; end
        $display("[tb_top_psmct32_feeder_colors_demo] errors=%0d", errors);
        if (errors==0) $display("[tb_top_psmct32_feeder_colors_demo] PASS");
        else           $display("[tb_top_psmct32_feeder_colors_demo] FAIL");
        $finish;
    end
    initial begin #1200000000; $error("[tb_top_psmct32_feeder_colors_demo] TIMEOUT"); $finish; end
endmodule : tb_top_psmct32_feeder_colors_demo
