#!/bin/sh
# retroDE_ps2 — copy the Ch357 persistent-Z (256x210, GS_SH3_LPDDR_FB_Z) board-side files to the DE25.
#
# This is the file set for the ZSCHED profile (sh3_zsched_epochs.txt, fbpxw 256 fbh 210) — the one built by the
# GS_SH3_LPDDR_FB_Z RBF. (NOT sh3_sched=384 or sh3_s640=640; 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_zsched_to_board.sh                 # default terasic@192.168.50.161:~
#   ./tools/scp_zsched_to_board.sh user@host:dir   # override destination
#
# 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_zsched_epochs.txt
#
# Optional board framebuffer readback (PSMCT32 linear, 256x210 for this profile):
#   sudo ./ps2_sh3_sched --zbuf sh3_zsched_epochs.txt --dump-fb sh3_zsched_board_fb.mem
#   scp terasic@192.168.50.161:~/sh3_zsched_board_fb.mem sim/data/top_psmct32_raster_demo/
#   python3 tools/gs_fb_to_png.py sim/data/top_psmct32_raster_demo/sh3_zsched_board_fb.mem \
#       sim/data/top_psmct32_raster_demo/sh3_zsched_board_fb.png 256 210 3
#   make -C sim sh3_zsched_board_compare
#
# Exact emitted-fragment color replay (also run by sh3_zsched_board_compare):
#   python3 tools/analyze_zsched_fb.py sim/data/top_psmct32_raster_demo/sh3_zsched_board_fb.mem \
#       --owner replay-color --frags sim/traces/rtl/zsched_frags.txt
#
# Optional texture/refmap oracle diagnostic maps:
#   make -C sim tb_top_psmct32_sh3_zsched
#   python3 tools/analyze_zsched_fb.py sim/data/top_psmct32_raster_demo/sh3_zsched_board_fb.mem \
#       --owner replay --frags sim/traces/rtl/zsched_frags.txt --maps
#
# Regenerate the LOCAL zsched fixtures with the signoff reference contract:
#   make -C sim sh3_zsched_fixture
# Equivalent explicit emit:
#   python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 8634,12757,145742 --authz --tag zsched \
#       --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_zsched_epochs.txt
$DATA/sh3_zsched0_tex_lpddr.mem
$DATA/sh3_zsched1_tex_lpddr.mem
$DATA/sh3_zsched2_tex_lpddr.mem
$DATA/feeder_sh3_zsched0.mem
$DATA/feeder_sh3_zsched1.mem
$DATA/feeder_sh3_zsched2.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 zsched fixtures first (dump-derived, local-only):" >&2
    echo "  make -C sim sh3_zsched_fixture" >&2
    echo "  # or:" >&2
    echo "  python3 tools/gs_make_sh3_scheduler_fixture.py --draw-list 8634,12757,145742 --authz --tag zsched \\" >&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"
