#!/bin/sh
# retroDE_ps2 — copy the Ch359 shared-texture persistent-Z NATIVE-640x480 board files to the DE25.
#
# This is the C6 file set (sh3_zs640c6_epochs.txt, fbpxw 640 fbh 480): six authentic SH3 draws in two ordered,
# shared-texture epochs at native screen coordinates. It requires the sh3_lpddr_fb_z640c6 RBF so the bootlet
# preloads the matching C6 CLUT. Copies go to the home dir so the host can resolve relative texture/list paths.
#
#   ./tools/scp_zs640c6_to_board.sh                 # default terasic@192.168.50.161:~
#   ./tools/scp_zs640c6_to_board.sh user@host:dir   # override destination
#
# STEP 0 — LOAD THE FRESH CORE FIRST. This script copies the 5 host/data files ONLY, NOT the RBF.
#   Deploy output_files/retroDE_ps2.core.rbf (verify its timestamp is from the signoff-clean fit!) via the usual
#   core loader and confirm it is LIVE before trusting any host run. The C6 host dump must report
#   bounds=(84,101)..(340,301); a different bound means the wrong fixture or core is live.
#
# On the board afterward (persistent-Z needs --zbuf: preclear the LPDDR Z region + arm EARLY):
#   gcc -O2 -o ps2_sh3_sched ps2_sh3_sched.c
#   sudo ./ps2_sh3_sched --zbuf sh3_zs640c6_epochs.txt
#
# Board framebuffer readback for the Ch359 C6 board proof (PSMCT32 linear, 640x480):
#   sudo ./ps2_sh3_sched --zbuf sh3_zs640c6_epochs.txt --dump-fb sh3_zs640c6_board_fb.mem
#   scp terasic@192.168.50.161:~/sh3_zs640c6_board_fb.mem sim/data/top_psmct32_raster_demo/
#   python3 tools/gs_fb_to_png.py sim/data/top_psmct32_raster_demo/sh3_zs640c6_board_fb.mem \
#       sim/data/top_psmct32_raster_demo/sh3_zs640c6_board_fb.png 640 480 3
#   make -C sim sh3_zs640c6_board_compare
#
# Exact emitted-fragment color replay (also run by sh3_zs640c6_board_compare):
#   python3 tools/analyze_zsched_fb.py sim/data/top_psmct32_raster_demo/sh3_zs640c6_board_fb.mem \
#       --tag zs640c6 --owner replay-color --frags sim/traces/rtl/zs640c6_frags.txt
#
# Regenerate the LOCAL zs640c6 fixtures with the signoff reference contract:
#   make -C sim sh3_zs640c6_fixture
# Equivalent explicit emit:
#   python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 119471,119684,119897,120110,120323,120536 --group-size 3 --authz --fb640 --tag zs640c6 \
#       --pscale auto,auto --xy-quant round,round --ref-xy-quant --emit
set -eu

DEST="${1:-terasic@192.168.50.161:~}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DATA="$ROOT/sim/data/top_psmct32_raster_demo"

FILES="
$ROOT/tools/ps2_sh3_sched.c
$DATA/sh3_zs640c6_epochs.txt
$DATA/sh3_zs640c60_tex_lpddr.mem
$DATA/feeder_sh3_zs640c60.mem
$DATA/feeder_sh3_zs640c61.mem
"

missing=0
for f in $FILES; do
    if [ ! -f "$f" ]; then echo "MISSING: $f" >&2; missing=1; fi
done
if [ "$missing" -ne 0 ]; then
    echo "" >&2
    echo "Regenerate the LOCAL zs640c6 fixtures first (dump-derived, local-only):" >&2
    echo "  make -C sim sh3_zs640c6_fixture" >&2
    echo "  # or:" >&2
    echo "  python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 119471,119684,119897,120110,120323,120536 --group-size 3 --authz --fb640 --tag zs640c6 \\" >&2
    echo "      --pscale auto,auto --xy-quant round,round --ref-xy-quant --emit" >&2
    exit 1
fi

echo "Copying 5 files -> $DEST (one password prompt):"
for f in $FILES; do echo "  $(basename "$f")"; done
# shellcheck disable=SC2086
exec scp $FILES "$DEST"
