// retroDE_ps2 — tb_top_psmct32_sh3_multidraw  (Ch354 Brick 1 — multi-draw accumulation into the LPDDR framebuffer)
//
// Composites N authentic SH3 draws (sharing one texture/CLUT) into ONE LPDDR PSMCT32 framebuffer through the SAME
// Ch353 LPDDR-FB path (FEEDER_AUTOSTART=0 + render-epoch EOF + frame_drained hard gate + line-buffer scanout), and
// proves Codex's Brick-1 acceptance:
//   * records_emitted == NTRIS (all draws' triangles emitted)
//   * max staging address stays < STG_WORDS (2048); FIFO/writer overflow == 0
//   * ALL batches complete before ONE ordered frame_drained
//   * scanout/preclear geometry = 256xFBH (338); exactly FBH*32 read beats/frame
//   * bounded <=1-texel oracle vs the INDEPENDENT combined refmap, with the OVERLAP region scored separately
//     (that is where multi-draw accumulation is actually proven).
// LOCAL/gitignored fixtures (dump-derived); skip-guard if absent.
`timescale 1ns/1ps

module tb_top_psmct32_sh3_multidraw;
`include "sh3_multi_params.vh"                 // FBPXW=256, FBH=338, STG_WORDS=2048, NTRIS=204, NDRAWS, TW/TH, ...
    localparam int W = FBPXW, H = FBH;
    localparam int STRIDE = W*4;               // 1024 B
    localparam int ROW_BEATS = STRIDE/32;      // 32
    localparam int FB_BYTES = STRIDE*H;
    localparam int FB_WORDS = W*H;
    localparam int BEATS_PER_FRAME = ROW_BEATS*H;   // 338*32 = 10816
    localparam int H_ACT=W, V_ACT=H, H_BP=32, H_FP=8, V_BP=16, V_FP=8;
    localparam int H_TOT=H_BP+H_ACT+H_FP, V_TOT=V_BP+V_ACT+V_FP;

    logic clk=0;       always #5 clk=~clk;         // GS/design clock
    logic emif_clk=0;  always #2 emif_clk=~emif_clk;
    logic video_clk=0; always #7 video_clk=~video_clk;
    logic rst_n; logic fb_commit=0; always #13 fb_commit=~fb_commit;
    int errors; initial errors=0;

    // ===== bram-top: multi-draw SH3 via the feeder, FB_LPDDR_ONLY, AUTOSTART off, STG_WORDS=2048 =====
    logic        core_go; logic [7:0] r,g,b; logic hsync,vsync_o,de;
    logic        core_halt,dma_done_seen,frame_seen,raster_overflow,frame_toggle,dma_done_toggle;
    logic        feeder_go_tb, feeder_ready_tb; logic [15:0] feeder_records_w; logic [31:0] feeder_waits_w;
    logic        gs_tex_rd_en_o; logic [31:0] gs_tex_rd_addr_o; logic [31:0] tex_cache_data; logic tex_cache_ready;
    logic [31:0] tex_cache_hits, tex_bram_hits;
    logic        flush_emit_w; logic [31:0] flush_addr_w; logic [31:0] flush_color32_w; logic [5:0] flush_psm_w;

    top_psmct32_raster_demo_bram #(
        .H_ACTIVE(FBPXW), .V_ACTIVE(FBH),
        .VRAM_BYTES(VRAM_BYTES_P), .VRAM_ENABLE_READ2(1'b0), .PSMCT32_SWIZZLE(1'b0),
        .COMBINED_TAZ(1'b0), .TILE_LOCAL(1'b0), .TILE_COLS(1), .TILE_ROWS(1),
        .TILE_MULTIPRIM(1'b0), .TILE_PRIM_COUNT(1), .TILE_FIFO_DEPTH(8),
        .BIN_BUFFER_ENABLE(1'b0), .HEARTBEAT_SPLICE_ENABLE(1'b0),
        .FEEDER_ENABLE(1'b1), .FEEDER_STG_WORDS(STG_WORDS), .FEEDER_AUTOSTART(1'b0),   // 2048 staging, no boot render
        .PERSPECTIVE_CORRECT(1'b1), .PERSP_RECIP_IDX_BITS(11),
        .GRAD_SEQ_DIVIDER(1'b1), .GRAD_DIV_CYCLES(4),
        .GS_LPDDR_TEX(1'b1), .TEX_VRAM_BASE(TEX_VRAM_BASE), .TEX_CACHE_BYTES(TEX_BYTES),
        .CLUT_CSM1_ENABLE(1'b1), .FB_LPDDR_ONLY(1'b1)
    ) dut (
        .clk(clk), .rst_n(rst_n), .core_go(core_go),
        .r(r), .g(g), .b(b), .hsync(hsync), .vsync(vsync_o), .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_ready_tb),
        .feeder_records_o(feeder_records_w), .feeder_waits_o(feeder_waits_w),
        .flush_emit_o(flush_emit_w), .flush_addr_o(flush_addr_w),
        .flush_pix16_o(), .flush_color32_o(flush_color32_w), .flush_psm_o(flush_psm_w),
        .gs_tex_rd_en_o(gs_tex_rd_en_o), .gs_tex_rd_addr_o(gs_tex_rd_addr_o),
        .tex_cache_data_i(tex_cache_data), .tex_cache_ready_i(tex_cache_ready),
        .tex_cache_hits_o(tex_cache_hits), .tex_bram_hits_o(tex_bram_hits)
    );

    // texture cache + behavioral texture LPDDR
    logic [29:0] araddr; logic [1:0] arburst; logic [6:0] arid; logic [7:0] arlen; logic [2:0] arsize;
    logic        arvalid, arready; logic [255:0] rdata; logic [1:0] rresp; logic rlast, rvalid, rready;
    logic        fill_start, fill_done; logic [31:0] fill_beats, fill_bytes, tex_rd_errs, fill_crc_w;
    gs_texture_cache #(.LPDDR_TEX_BASE(LPDDR_TEX_BASE), .TEX_VRAM_BASE(TEX_VRAM_BASE),
                       .TEX_BYTES(TEX_BYTES), .N_BEATS(N_BEATS)) u_cache (
        .axi_clk(clk), .axi_rst_n(rst_n), .fill_start(fill_start), .fill_done(fill_done),
        .fill_beats(fill_beats), .fill_bytes(fill_bytes), .rd_errs(tex_rd_errs), .fill_crc(fill_crc_w),
        .araddr(araddr), .arburst(arburst), .arid(arid), .arlen(arlen), .arsize(arsize), .arvalid(arvalid), .arready(arready),
        .rdata(rdata), .rresp(rresp), .rlast(rlast), .rvalid(rvalid), .rready(rready),
        .sample_clk(clk), .tex_rd_en(gs_tex_rd_en_o), .tex_rd_addr(gs_tex_rd_addr_o),
        .tex_rd_data(tex_cache_data), .tex_ready(tex_cache_ready)
    );
    logic [31:0] lpddr_mem [0:(TEX_BYTES/4)-1];
    typedef enum logic [1:0] { S_IDLE, S_WAIT, S_DATA } sstate_t; sstate_t sst; logic [3:0] tdly; int tbeat;
    always_ff @(posedge clk) begin
        if (!rst_n) begin sst<=S_IDLE; arready<=0; rvalid<=0; rlast<=0; rresp<=0; rdata<=0; tdly<=0; end
        else begin
            arready<=0; rvalid<=0; rlast<=0;
            case (sst)
                S_IDLE: if (arvalid) begin arready<=1; tbeat<=(araddr-LPDDR_TEX_BASE)>>5; tdly<=4'd2; sst<=S_WAIT; end
                S_WAIT: if (tdly==0) sst<=S_DATA; else tdly<=tdly-1'b1;
                S_DATA: if (rready) begin for (int w=0;w<8;w++) rdata[w*32 +: 32]<=lpddr_mem[tbeat*8+w];
                        rresp<=2'b00; rvalid<=1; rlast<=1; sst<=S_IDLE; end
            endcase
        end
    end

    // ===== render epoch (same as the board) + PSMCT32 writer + precleared LPDDR FB =====
    logic fb_flush=0, render_inflight=0, feeder_ready_q=0; int eof_count;
    wire feeder_ready_rise = feeder_ready_tb && !feeder_ready_q;
    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin render_inflight<=0; feeder_ready_q<=0; fb_flush<=0; eof_count<=0; end
        else begin
            feeder_ready_q<=feeder_ready_tb; fb_flush<=1'b0;
            if (feeder_go_tb && feeder_ready_tb) render_inflight<=1'b1;
            if (render_inflight && feeder_ready_rise) begin fb_flush<=1'b1; render_inflight<=1'b0; eof_count<=eof_count+1; end
        end
    end
    logic        wr_arm=0; logic [255:0] fbw_wdata; logic [31:0] fbw_wstrb, fbw_awaddr; logic fbw_awvalid, fbw_wvalid;
    logic [31:0] fbw_beats, fbw_ovf, fbw_bresp_err; logic fbw_idle, fbw_drained;
    gs_lpddr_axi_master #(.FIFO_DEPTH(64), .PIX_BYTES(4)) u_wr (
        .gs_clk(clk), .gs_rst_n(rst_n), .enable(1'b1),
        .arm(wr_arm), .canary(1'b0), .fb_base(32'h0), .ctrl_commit(fb_commit),
        .px_emit(flush_emit_w && (flush_psm_w==6'h00)), .px_addr(flush_addr_w), .px_pix32(flush_color32_w), .flush(fb_flush),
        .axi_clk(emif_clk), .axi_rst_n(rst_n),
        .awaddr(fbw_awaddr), .awlen(), .awsize(), .awburst(), .awid(), .awvalid(fbw_awvalid), .awready(1'b1),
        .wdata(fbw_wdata), .wstrb(fbw_wstrb), .wlast(), .wvalid(fbw_wvalid), .wready(1'b1),
        .bvalid(1'b1), .bready(), .bresp(2'b00),
        .beats_written(fbw_beats), .bursts_issued(), .bresp_err_count(fbw_bresp_err),
        .fifo_overflow_count(fbw_ovf), .idle(fbw_idle), .frame_drained(fbw_drained)
    );
    logic [7:0] fb [0:FB_BYTES-1]; logic [31:0] fb_awlat;
    always_ff @(posedge emif_clk) begin
        if (fbw_awvalid) fb_awlat<=fbw_awaddr;
        if (fbw_wvalid) for (int i=0;i<32;i++) if (fbw_wstrb[i]) begin
            int aa; aa=fb_awlat+i; if (aa>=0 && aa<FB_BYTES) fb[aa]<=fbw_wdata[i*8 +: 8];
        end
    end
    logic [31:0] ideal [0:FB_WORDS-1]; logic ideal_set [0:FB_WORDS-1];
    always_ff @(posedge clk) if (rst_n && flush_emit_w && (flush_psm_w==6'h00)) begin
        ideal[flush_addr_w>>2]<=flush_color32_w; ideal_set[flush_addr_w>>2]<=1'b1;
    end

    // ===== scanout reads the FB back (256x338) =====
    logic [11:0] px, py; logic vsync, in_win; logic [7:0] so_r, so_g, so_b;
    logic so_underflow; logic [31:0] so_rd_errs; logic so_line_valid;
    logic [29:0] so_araddr; logic [1:0] so_arburst; logic [6:0] so_arid; logic [7:0] so_arlen;
    logic [2:0]  so_arsize; logic so_arvalid, so_arready;
    logic [255:0] so_rdata; logic [1:0] so_rresp; logic so_rlast, so_rvalid, so_rready;
    gs_lpddr_scanout_lb #(.FB_BASE(30'd0), .STRIDE_BYTES(STRIDE), .ROW_BEATS(ROW_BEATS),
                          .N_ROWS(H), .PSMCT32(1'b1)) u_scan (
        .axi_clk(emif_clk), .axi_rst_n(rst_n), .enable(fbw_drained),
        .video_clk(video_clk), .frame_start(vsync), .pixel_x(px), .pixel_y(py), .in_window(in_win),
        .r(so_r), .g(so_g), .b(so_b), .line_valid(so_line_valid), .underflow(so_underflow), .rd_errs(so_rd_errs),
        .araddr(so_araddr), .arburst(so_arburst), .arid(so_arid), .arlen(so_arlen), .arsize(so_arsize),
        .arvalid(so_arvalid), .arready(so_arready), .rdata(so_rdata), .rresp(so_rresp),
        .rlast(so_rlast), .rvalid(so_rvalid), .rready(so_rready)
    );
    logic [7:0] rlfsr=8'h3C; always_ff @(posedge emif_clk) rlfsr<={rlfsr[6:0], rlfsr[7]^rlfsr[5]^rlfsr[4]^rlfsr[3]};
    typedef enum logic [1:0] { R_IDLE, R_WAIT, R_DATA } rst_t; rst_t rst_state;
    logic [3:0] rdly; logic [29:0] rd_addr_l; int read_beats;
    logic [2:0] vs_e; wire vs_edge_e = vs_e[1] && !vs_e[2]; int fb_reads, fb_reads_last;
    always_ff @(posedge emif_clk or negedge rst_n) begin
        if (!rst_n) begin rst_state<=R_IDLE; so_arready<=0; so_rvalid<=0; so_rlast<=0; so_rresp<=0; so_rdata<=0; rdly<=0;
                          read_beats<=0; vs_e<=0; fb_reads<=0; fb_reads_last<=0; end
        else begin
            so_arready<=0; so_rvalid<=0; so_rlast<=0; vs_e<={vs_e[1:0], vsync};
            if (vs_edge_e) begin fb_reads_last<=fb_reads; fb_reads<=0; end
            case (rst_state)
                R_IDLE: if (so_arvalid) begin so_arready<=1; rd_addr_l<=so_araddr; rdly<=rlfsr[2:0]; rst_state<=R_WAIT; end
                R_WAIT: if (rdly==0) rst_state<=R_DATA; else rdly<=rdly-1'b1;
                R_DATA: if (so_rready) begin
                    for (int w=0;w<8;w++) begin int aa; aa=rd_addr_l+w*4;
                        so_rdata[w*32 +: 32] <= (aa+3<FB_BYTES) ? {fb[aa+3],fb[aa+2],fb[aa+1],fb[aa]} : 32'd0; end
                    so_rresp<=2'b00; so_rvalid<=1; so_rlast<=1; read_beats<=read_beats+1;
                    if (!vs_edge_e) fb_reads<=fb_reads+1; rst_state<=R_IDLE;
                end
            endcase
        end
    end
    logic vid_run=0; logic [11:0] rawx, rawy;
    always_ff @(posedge video_clk) begin
        if (!vid_run) begin rawx<=0; rawy<=0; end
        else if (rawx==H_TOT-1) begin rawx<=0; rawy<=(rawy==V_TOT-1)?12'd0:rawy+1'b1; end
        else rawx<=rawx+1'b1;
    end
    wire active = (rawx>=H_BP)&&(rawx<H_BP+H_ACT)&&(rawy>=V_BP)&&(rawy<V_BP+V_ACT);
    assign px=active?(rawx-H_BP):12'd0; assign py=(rawy>=V_BP&&rawy<V_BP+V_ACT)?(rawy-V_BP):12'd0;
    assign in_win=active; assign vsync=vid_run&&(rawx==0)&&(rawy==0);
    logic [11:0] px_q, py_q; logic inwin_q, run_q;
    always_ff @(posedge video_clk) begin px_q<=px; py_q<=py; inwin_q<=in_win; run_q<=vid_run; end
    int checked; initial checked=0; logic scoring=0;
    always_ff @(posedge video_clk) if (scoring && run_q) begin
        logic [7:0] er,eg,eb; logic [31:0] w;
        if (inwin_q) begin int aa; aa=py_q*STRIDE+px_q*4; w={fb[aa+3],fb[aa+2],fb[aa+1],fb[aa]}; er=w[7:0]; eg=w[15:8]; eb=w[23:16]; end
        else begin er=0; eg=0; eb=0; end
        checked++;
        if (so_r!==er||so_g!==eg||so_b!==eb) begin
            if (errors<12) $error("[scan] px(%0d,%0d) got(%02x,%02x,%02x) exp(%02x,%02x,%02x)", px_q,py_q, so_r,so_g,so_b, er,eg,eb);
            errors++;
        end
    end

    // ===== combined INDEPENDENT oracle + overlap scoring =====
    logic [31:0] idx_words [0:(512*512/4)-1]; logic [31:0] pal [0:255]; logic [31:0] refmap [0:FB_WORDS-1];
    function automatic logic [7:0] sh3_idx(input integer u, input integer v);
        integer lin; logic [31:0] w; lin=v*TW+u; w=idx_words[lin/4]; sh3_idx=w[(8*(lin%4)) +: 8]; endfunction
    function automatic logic [23:0] exp_cell(input integer u, input integer v); exp_cell=pal[sh3_idx(u,v)][23:0]; endfunction

    // diagnostic: +TAG=<tag> selects the fixture set (multi | d548 | d761 | d974 ...); +FBDUMP=<file> dumps the RTL FB.
    string ftag; string fdump; int is_multi;
    initial begin
        errors=0; feeder_go_tb=1'b0; fill_start=1'b0; wr_arm=0;
        if (!$value$plusargs("TAG=%s", ftag)) ftag="multi";
        is_multi = (ftag=="multi");
        for (int i=0;i<FB_WORDS;i++) begin ideal_set[i]=1'b0; ideal[i]=32'd0; end
        for (int i=0;i<FB_BYTES;i++) fb[i]=8'h00;                      // preclear
        lpddr_mem[0]='x;
        $readmemh($sformatf("../../data/top_psmct32_raster_demo/feeder_sh3_%s.mem", ftag), dut.g_feeder.feeder_stg);
        $readmemh($sformatf("../../data/top_psmct32_raster_demo/sh3_%s_tex_lpddr.mem", ftag), lpddr_mem);
        $readmemh($sformatf("../../data/top_psmct32_raster_demo/sh3_%s_idx.mem", ftag), idx_words);
        $readmemh($sformatf("../../data/top_psmct32_raster_demo/sh3_%s_pal.mem", ftag), pal);
        $readmemh($sformatf("../../data/top_psmct32_raster_demo/sh3_%s_refmap.mem", ftag), refmap);
        $display("[md] TAG=%s (is_multi=%0d)", ftag, is_multi);
        if (lpddr_mem[0]===32'bx) begin
            $display("[tb_top_psmct32_sh3_multidraw] SKIP — sh3_%s_*.mem absent (run gs_make_sh3_multidraw_fixture.py --emit)", ftag);
            $finish;
        end
        rst_n=1'b0; core_go=1'b0; repeat(6) @(posedge clk); rst_n=1'b1; repeat(8) @(posedge clk);

        @(posedge clk) fill_start<=1'b1;
        begin int gd=0; while(!fill_done && gd<200000) begin @(posedge clk); gd++; end end
        if (!fill_done)               begin $error("[seq] cache fill_done never asserted"); errors++; end
        if (fill_crc_w!==32'hfbdeaa32)begin $error("[seq] cache fill_crc=%08x exp fbdeaa32", fill_crc_w); errors++; end

        @(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);
        wait (feeder_ready_tb==1'b1); repeat(200) @(posedge clk);       // C_READY, no boot render
        if (fbw_beats!==0)  begin $error("[seq] writer wrote %0d beats BEFORE GO", fbw_beats); errors++; end
        if (eof_count!==0)  begin $error("[seq] %0d EOF BEFORE GO", eof_count); errors++; end

        wr_arm<=1'b1; repeat(40) @(posedge clk);
        @(negedge clk); feeder_go_tb=1'b1; @(negedge clk); feeder_go_tb=1'b0;
        begin int d=0; while(feeder_ready_tb && d<4000) begin @(posedge clk); d++; end end
        if (feeder_ready_tb!==1'b0) begin $error("[seq] ready did not drop after GO"); errors++; end
        wait (feeder_ready_tb==1'b1);                                   // ALL batches drained -> ready rises

        begin int d=0; while(!fbw_drained && d<800000) begin @(posedge clk); d++; end end
        if (!fbw_drained)   begin $error("[seq] frame_drained never asserted"); errors++; end
        repeat(200) @(posedge clk);
        // ---- Codex acceptance ----
        if (eof_count!==1)           begin $error("[md] eof_count=%0d exp 1 (one ordered drain after ALL batches)", eof_count); errors++; end
        if (is_multi && feeder_records_w!==NTRIS) begin $error("[md] records_emitted=%0d exp %0d (NTRIS)", feeder_records_w, NTRIS); errors++; end
        if (fbw_ovf!==0)             begin $error("[md] writer FIFO overflow=%0d", fbw_ovf); errors++; end
        if (fbw_bresp_err!==0)       begin $error("[md] writer BRESP errors=%0d", fbw_bresp_err); errors++; end
        if (raster_overflow!==1'b0)  begin $error("[md] raster_overflow asserted"); errors++; end
        $display("[md] post-GO: eof=%0d records_emitted=%0d (exp %0d) writer_beats=%0d ovf=0", eof_count, feeder_records_w, NTRIS, fbw_beats);

        vid_run=1;
        begin int d=0; while(!vsync && d<200000) begin @(posedge video_clk); d++; end end
        @(posedge video_clk); while(!vsync) @(posedge video_clk);
        begin scoring=1;
            @(posedge video_clk); while(!vsync) @(posedge video_clk);
            scoring=0; repeat(60) @(posedge emif_clk);
            if (fb_reads_last!==BEATS_PER_FRAME) begin
                $error("[md] scanout beats/frame=%0d exp %0d (%0dx%0d)", fb_reads_last, BEATS_PER_FRAME, H, ROW_BEATS); errors++; end
        end
        if (so_underflow!==0) begin $error("[md] scanout underflow"); errors++; end
        if (so_rd_errs !==0)  begin $error("[md] scanout rd_errs=%0d", so_rd_errs); errors++; end
        if (checked < H_ACT*V_ACT) begin $error("[md] only %0d px checked (exp >= %0d)", checked, H_ACT*V_ACT); errors++; end

        // ---- combined oracle: ALL covered + the OVERLAP subset (refmap bit28) scored SEPARATELY ----
        // Score four cohorts. The ACCEPTANCE gate is on INTERIOR (bit30 = away from triangle edges) — the SAME
        // seam-free standard Ch353 used (its ALL was only ~96%, gated INTERIOR>=95%). ALL and the raw OVERLAP
        // region are seam-heavy (draw boundaries = sub-pixel which-draw-wins ambiguity + the Ch352 perspective/8b
        // reciprocal limit) and reported informationally. OVERLAP-INTERIOR is the real multi-draw ACCUMULATION proof:
        // pixels covered by >1 draw AND away from edges must render correctly.
        begin
            int m_tot,m_ok,mi_tot,mi_ok,o_tot,o_ok,oi_tot,oi_ok,cb;
            m_tot=0;m_ok=0;mi_tot=0;mi_ok=0;o_tot=0;o_ok=0;oi_tot=0;oi_ok=0;cb=0;
            for (int y=0;y<H;y++) for (int x=0;x<W;x++) begin
                logic [31:0] rm; logic [23:0] fbc; int tu,tv,D; rm=refmap[y*W+x];
                if (rm[31]) begin
                    tu=(rm>>9)&9'h1FF; tv=rm&9'h1FF; fbc=ideal[y*W+x][23:0]; m_tot++; D=9;
                    for (int rad=0;rad<=1;rad++) for (int du=-rad;du<=rad;du++) for (int dv=-rad;dv<=rad;dv++) begin
                        int ch; ch=(du<0?-du:du); if((dv<0?-dv:dv)>ch) ch=(dv<0?-dv:dv);
                        if (ch==rad && D==9 && (tu+du)>=0 && (tu+du)<TW && (tv+dv)>=0 && (tv+dv)<TH && fbc===exp_cell(tu+du,tv+dv)) D=rad;
                    end
                    if (D<=1) m_ok++;
                    if (rm[30]) begin mi_tot++; if (D<=1) mi_ok++; end               // interior (seam-free)
                    if (rm[28]) begin o_tot++;  if (D<=1) o_ok++; end                // overlap (accumulation, seam-incl)
                    if (rm[28]&&rm[30]) begin oi_tot++; if (D<=1) oi_ok++; end        // overlap AND interior
                    begin bit f; f=1'b0; for (int i=0;i<256;i++) if (pal[i][23:0]===fbc) f=1'b1; if (!f) cb++; end
                end
            end
            $display("[md][oracle] <=1texel  ALL=%0d/%0d (%.1f%%)  INTERIOR=%0d/%0d (%.1f%%)  OVERLAP=%0d/%0d (%.1f%%)  OVERLAP-INT=%0d/%0d (%.1f%%)  clut_bad=%0d",
                     m_ok,m_tot,(m_tot>0)?100.0*m_ok/m_tot:0.0, mi_ok,mi_tot,(mi_tot>0)?100.0*mi_ok/mi_tot:0.0,
                     o_ok,o_tot,(o_tot>0)?100.0*o_ok/o_tot:0.0, oi_ok,oi_tot,(oi_tot>0)?100.0*oi_ok/oi_tot:0.0, cb);
            // ACCEPTANCE (Codex): the Brick-1 claim is MULTI-DRAW ACCUMULATION, proven bit-exact externally
            // (combined RTL == RTL-paint-order composition of the isolated single renders, 0/86528 px diff, see
            // compose_check + docs/ch354_audit_log.md). clut_bad and the overlap-exercised count ARE gated (a valid
            // composited render). The <=1-texel fidelity above is a SEPARATE, draw-dependent metric (the existing
            // perspective-sampling fidelity limitation: 89761=97.5%, 89548=95.6%, 89974=93.2% individually) and is
            // REPORTED, NOT gated — it is not part of the accumulation acceptance.
            if (cb!==0)                 begin $error("[md][oracle] %0d covered px not a CLUT entry", cb); errors++; end
            if (is_multi && o_tot<1000) begin $error("[md][oracle] only %0d overlap px (<1000) — accumulation not exercised", o_tot); errors++; end
        end

        // diagnostic: dump the RTL framebuffer (one 32-bit PSMCT32 word per FB pixel, row-major) for Python composition
        if ($value$plusargs("FBDUMP=%s", fdump)) begin
            int fh; fh=$fopen(fdump,"w");
            for (int y=0;y<H;y++) for (int x=0;x<W;x++) begin
                int aa; aa=y*STRIDE+x*4; $fwrite(fh, "%08x\n", {fb[aa+3],fb[aa+2],fb[aa+1],fb[aa]});
            end
            $fclose(fh); $display("[md] dumped RTL FB (%0dx%0d) -> %s", W, H, fdump);
        end

        $display("[tb_top_psmct32_sh3_multidraw] checked=%0d px, beats/frame=%0d (exp %0d), records=%0d eof=%0d underflow=%0b ovf=%0d errors=%0d",
                 checked, fb_reads_last, BEATS_PER_FRAME, feeder_records_w, eof_count, so_underflow, fbw_ovf, errors);
        if (errors==0) $display("[tb_top_psmct32_sh3_multidraw] PASS");
        else           $display("[tb_top_psmct32_sh3_multidraw] FAIL");
        $finish;
    end
    initial begin #80000000; $error("[tb_top_psmct32_sh3_multidraw] TIMEOUT"); $finish; end
endmodule : tb_top_psmct32_sh3_multidraw
