#!/bin/sh
# retroDE_ps2 — Ch334 NATIVE RECTANGLE RECORD silicon proof (host command compression).
#
# A native rectangle is ONE 3-word record (color + 2 corners) that the FEEDER expands into two
# colored triangles — 6x smaller host payload than the explicit 18-word two-triangle form, same
# rendered result. The count word now carries {rect_count[31:16], tri_count[15:0]}. Two scenes:
#   NATIVE_RECT : 3 native rects -> red/green/blue filled quads {0,5,10}   (records=6, == Ch333 color_rect)
#   NATIVE_MIX  : red triangle(0) + green/blue/yellow native rects(5/10/15)  (records=7)
# Ends on NATIVE_MIX. Sim proof: tb_top_psmct32_feeder_native_demo (matches the explicit version).
#
# REQUIRES the Ch334 bitstream (feeder rect-expansion) — a re-fit from Ch333.
# Register map identical to ps2_feeder_test.sh (BASE 0x40000000).

set -u
BASE="${PS2_BRIDGE_BASE:-0x40000000}"
DEVMEM="${DEVMEM:-busybox devmem}"
OFF_STATUS=0x0D8; OFF_LO=0x0DC; OFF_HI=0x0E4; OFF_GO=0x0E8
w() { $DEVMEM $(printf "0x%X" $(( BASE + $1 ))) w "$2" >/dev/null; }
r() { $DEVMEM $(printf "0x%X" $(( BASE + $1 ))) w; }

NATIVE_RECT="0000000000030000 0000000000010000 0000000000000044 0000000000050000 0000000000000002 0000000088004060 0000000000000053 00000000ff0000ff 0000500000100010 0000500000e000e0 00000000ff00ff00 0000510001100110 0000510001e001e0 00000000ffff0000 0000520002100210 0000520002e002e0"
NATIVE_MIX="0000000000030001 0000000000010000 0000000000000044 0000000000050000 0000000000000002 0000000088004060 0000000000000053 00000000ff0000ff 0000000000000000 0000500000100010 00000000ff0000ff 0000000000000030 00005000001000e0 00000000ff0000ff 00000000000c0000 0000500000e00010 00000000ff00ff00 0000510001100110 0000510001e001e0 00000000ffff0000 0000520002100210 0000520002e002e0 00000000ff00ffff 0000530003100310 0000530003e003e0"

wait_ready() {
    i=0
    while [ $i -lt 300 ]; do st=$(r $OFF_STATUS); [ $(( st & 1 )) -eq 1 ] && return 0; i=$(( i + 1 )); sleep 0.01 2>/dev/null || true; done
    echo "  !! feeder never reported ready — is this the Ch334 bitstream?"; return 1
}
stage_and_go() { # $1 label $2 words $3 expected-records
    label="$1"; words="$2"; exp="$3"
    echo "[$label] waiting for feeder ready ..."; wait_ready || return 1
    echo "[$label] streaming the list, then GO ..."; w $OFF_STATUS 0x0; n=0
    for word in $words; do
        lo=$(printf '%s' "$word" | cut -c9-16); hi=$(printf '%s' "$word" | cut -c1-8)
        w $OFF_LO 0x$lo; w $OFF_HI 0x$hi; n=$(( n + 1 ))
    done
    echo "[$label] wrote $n words; bridge staging addr now=$(( $(r $OFF_LO) )) (expect $n)"
    wait_ready || return 1; w $OFF_GO 0x1; wait_ready || return 1
    rec=$(r $OFF_HI)
    echo "[$label] records=$(( rec )) (expect $exp)"
    [ $(( rec )) -eq $exp ] || { echo "  !! records != $exp"; return 1; }
    echo "[$label] OK — look at HDMI."; sleep 2 2>/dev/null || true
}

echo "=== Ch334 native rectangle record — NATIVE_RECT -> NATIVE_MIX (host command compression) ==="
stage_and_go "NATIVE_RECT (3 native rects: r/g/b quads, 16 words)"     "$NATIVE_RECT" 6 || exit 1
stage_and_go "NATIVE_MIX (red tri + 3 native rects, 25 words)"         "$NATIVE_MIX"  7 || exit 1
echo "=== done — ENDS ON NATIVE_MIX: red triangle (top-left) + green/blue/yellow filled squares."
echo "    Each rectangle was ONE 3-word record expanded to 2 triangles in the feeder. ==="
