#!/bin/sh
# retroDE_ps2 — copy the Ch358 persistent-Z NATIVE-640x480 (GS_SH3_LPDDR_FB_Z + GS_SH3_LPDDR_FB_640) board files to the DE25.
#
# This is the file set for the ZS640 profile (sh3_zs640_epochs.txt, fbpxw 640 fbh 480) — the strong-reject persistent-Z
# scene at authentic native screen coordinates, built by the GS_SH3_LPDDR_FB_Z+_640 RBF. (NOT sh3_zsched=256x210 or the
# paint-order sh3_s640; those are other profiles.) Copies to the home dir so the host runs from ~ and the epoch
# descriptor finds the tex/list files by their relative names.
#
#   ./tools/scp_zs640_to_board.sh                 # default terasic@192.168.50.161:~
#   ./tools/scp_zs640_to_board.sh user@host:dir   # override destination
#
# STEP 0 — LOAD THE FRESH CORE FIRST. This script copies the 8 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 Ch358 core scans out 640x480 (Ch357's
#   zsched core was 256x210), and the host dump must report bounds=(407,97)..(619,306).
#
# 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_zs640_epochs.txt
#
# Board framebuffer readback for the Ch358 board proof (PSMCT32 linear, 640x480 for this profile):
#   sudo ./ps2_sh3_sched --zbuf sh3_zs640_epochs.txt --dump-fb sh3_zs640_board_fb.mem
#   scp terasic@192.168.50.161:~/sh3_zs640_board_fb.mem sim/data/top_psmct32_raster_demo/
#   python3 tools/gs_fb_to_png.py sim/data/top_psmct32_raster_demo/sh3_zs640_board_fb.mem \
#       sim/data/top_psmct32_raster_demo/sh3_zs640_board_fb.png 640 480 3
#   make -C sim sh3_zs640_board_compare
#
# Exact emitted-fragment color replay (also run by sh3_zs640_board_compare):
#   python3 tools/analyze_zsched_fb.py sim/data/top_psmct32_raster_demo/sh3_zs640_board_fb.mem \
#       --tag zs640 --owner replay-color --frags sim/traces/rtl/zs640_frags.txt
#
# Regenerate the LOCAL zs640 fixtures with the signoff reference contract:
#   make -C sim sh3_zs640_fixture
# Equivalent explicit emit:
#   python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 8634,12757,145742 --authz --fb640 --tag zs640 \
#       --pscale auto,auto,384 --xy-quant round,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_zs640_epochs.txt
$DATA/sh3_zs6400_tex_lpddr.mem
$DATA/sh3_zs6401_tex_lpddr.mem
$DATA/sh3_zs6402_tex_lpddr.mem
$DATA/feeder_sh3_zs6400.mem
$DATA/feeder_sh3_zs6401.mem
$DATA/feeder_sh3_zs6402.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 zs640 fixtures first (dump-derived, local-only):" >&2
    echo "  make -C sim sh3_zs640_fixture" >&2
    echo "  # or:" >&2
    echo "  python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 8634,12757,145742 --authz --fb640 --tag zs640 \\" >&2
    echo "      --pscale auto,auto,384 --xy-quant round,round,round --ref-xy-quant --emit" >&2
    exit 1
fi

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