#!/bin/sh
# retroDE_ps2 — Ch335 GOURAUD per-vertex color silicon proof (smooth gradients, runtime-switched).
#
# Distinct per-vertex RGBAQ -> the combined MODULATE path multiplies the texel by the INTERPOLATED
# vertex color, so a primitive shows a smooth gradient. Flat scenes (equal vertex colors) are
# unchanged. Three scenes:
#   GOURAUD_TRI  : tile0 triangle, v0=red v1=green v2=blue  -> RGB gradient (records=1)
#   GOURAUD_RECT : tile5 quad (2 tris), corners red/green/blue/white -> gradient quad (records=2)
#   GOURAUD_MIX  : flat red triangle(0) + RGB gradient triangle(10) (records=2)
# Ends on GOURAUD_MIX. Sim proof: tb_top_psmct32_feeder_gouraud_demo (per-vertex channel dominance).
#
# REQUIRES the Ch335 bitstream (interpolated MODULATE) — a re-fit from Ch334.
# 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; }

GOURAUD_TRI="0000000000000001 0000000000010000 0000000000000044 0000000000050000 0000000000000002 0000000088004060 0000000000000053 00000000ff0000ff 0000000000000000 0000500000100010 00000000ff00ff00 0000000000000030 00005000001000e0 00000000ffff0000 00000000000c0000 0000500000e00010"
GOURAUD_RECT="0000000000000002 0000000000010000 0000000000000044 0000000000050000 0000000000000002 0000000088004060 0000000000000053 00000000ff0000ff 0000000000000000 0000500001100110 00000000ff00ff00 0000000000000030 00005000011001e0 00000000ffff0000 00000000000c0000 0000500001e00110 00000000ff00ff00 0000000000000000 00005100011001e0 00000000ffff0000 0000000000000030 0000510001e00110 00000000ffffffff 00000000000c0000 0000510001e001e0"
GOURAUD_MIX="0000000000000002 0000000000010000 0000000000000044 0000000000050000 0000000000000002 0000000088004060 0000000000000053 00000000ff0000ff 0000000000000000 0000500000100010 00000000ff0000ff 0000000000000030 00005000001000e0 00000000ff0000ff 00000000000c0000 0000500000e00010 00000000ff0000ff 0000000000000000 0000510002100210 00000000ff00ff00 0000000000000030 00005100021002e0 00000000ffff0000 00000000000c0000 0000510002e00210"

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 Ch335 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 "=== Ch335 gouraud per-vertex color — GOURAUD_TRI -> GOURAUD_RECT -> GOURAUD_MIX ==="
stage_and_go "GOURAUD_TRI (RGB gradient triangle)"          "$GOURAUD_TRI"  1 || exit 1
stage_and_go "GOURAUD_RECT (RGB+white gradient quad)"       "$GOURAUD_RECT" 2 || exit 1
stage_and_go "GOURAUD_MIX (flat red tri + gradient tri)"    "$GOURAUD_MIX"  2 || exit 1
echo "=== done — ENDS ON GOURAUD_MIX: solid red triangle (top-left) + a smooth red->green->blue"
echo "    gradient triangle (center). Smooth per-vertex color from staging RGBAQ, no rebuild/reset. ==="
