#!/usr/bin/env bash
# verify-x68000-golden.sh - X68000 golden-hash gate.
#
# Builds UBER with a 1-frame measurement window (the timings are meaningless at
# that setting; this run is for the HASHES), stages it on a Human68k image,
# runs it headless under the patched MAME, extracts joeylog.txt back off the
# image with xdftool, and diffs the hashes against the Apple IIgs reference.
#
# Takes roughly 70 minutes: UBER on the generic renderer is slow, and the run
# must reach jlLogFlush at the very end before any hashes exist on disk.
#
#   X68K_SCRATCH=<dir with x68mame/> bash scripts/verify-x68000-golden.sh
set -uo pipefail

repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
SP="${X68K_SCRATCH:?set X68K_SCRATCH to a work dir containing x68mame/}"
MAME="$repo/toolchains/cache/mame-mame0264/x68k"
TEMPLATE="$SP/x68mame/HUMAN302.XDF"
GOLDEN="${X68K_GOLDEN:-$repo/tests/goldens/uber/iigs.txt}"
FRAMES="${X68K_GOLDEN_FRAMES:-300000}"
WALL="${X68K_GOLDEN_WALL:-7200}"

export PATH="$repo/toolchains/x68000/m68k-xelf/bin:$PATH"
work=$(mktemp -d -t joey-x68gold.XXXXXX)
trap 'rm -rf "$work"' EXIT

# -s (strip) is REQUIRED: the unstripped binary is ~247 KB and does not fit
# alongside Human68k on a 1232 KB floppy (232 KB free).
m68k-xelf-gcc -s -O2 -m68000 -fomit-frame-pointer \
    -DJOEYLIB_PLATFORM_X68000 -DUBER_FRAMES=1u \
    -I"$repo/include" -I"$repo/src/core" -I"$repo/src/x68000" \
    -I"$repo/toolchains/audio/libxmp-lite/include" \
    "$repo/examples/uber/uber.c" \
    "$repo/build/x68000/lib/libjoey.a" "$repo/build/x68000/lib/libxmplite.a" -lm \
    -o "$work/UBER.X" || exit 1

# TWO DISKS. UBER.X is ~240 KB and Human68k needs ~90 KB, which leaves a 1232 KB
# floppy with under a cluster spare -- so joeylog.txt cannot be written and the
# run silently produces nothing after ~50 minutes. The boot disk therefore holds
# only the binary, and a blank second disk on B: takes the log. AUTOEXEC switches
# to B: before launching so the log lands there.
printf 'B:\r\nA:\\UBER.X\r\n\x1a' > "$work/AUTOEXEC.BAT"
cp "$TEMPLATE" "$work/gold.xdf"
# Blank data disk = the template with every file removed (keeps the format).
cp "$TEMPLATE" "$work/data.xdf"
for f in HUMAN.SYS CONFIG.SYS KEY.SYS USKCG.SYS BEEP.SYS STARTUP.ENV COMMAND.X AUTOEXEC.BAT; do
    python3 "$repo/tools/xdftool.py" delete "$work/data.xdf" "$f" >/dev/null 2>&1
done
# Trim the boot disk of anything not needed to boot and run one .X.
for f in USKCG.SYS BEEP.SYS KEY.SYS STARTUP.ENV; do
    python3 "$repo/tools/xdftool.py" delete "$work/gold.xdf" "$f" >/dev/null 2>&1
done
python3 "$repo/tools/xdftool.py" add "$work/gold.xdf" "$work/UBER.X" UBER.X >/dev/null || exit 1
python3 "$repo/tools/xdftool.py" add "$work/gold.xdf" "$work/AUTOEXEC.BAT" AUTOEXEC.BAT >/dev/null

# UBER ends on jlWaitForAnyKey, AFTER jlLogFlush -- post keys late so it exits
# cleanly rather than being killed mid-write.
cat > "$work/gold.lua" <<LUA
local frame = 0
emu.register_frame_done(function()
    frame = frame + 1
    if frame % 20000 == 0 then io.write("GOLD f"..frame.."\n"); io.flush() end
    if frame > ($FRAMES - 60000) and frame % 2000 == 0 then
        manager.machine.natkeyboard:post(" ")
    end
    if frame > $FRAMES then manager.machine:exit() end
end)
LUA

timeout -s KILL "$WALL" "$MAME" x68000 -bios ipl10 \
    -rompath "$SP/x68mame/roms" -flop1 "$work/gold.xdf" -flop2 "$work/data.xdf" \
    -video none -sound none -nothrottle \
    -autoboot_script "$work/gold.lua" </dev/null 2>&1 | grep '^GOLD'

python3 "$repo/tools/xdftool.py" extract "$work/data.xdf" joeylog.txt "$work/x68.txt" || {
    echo "verify-x68000-golden: FAIL - no joeylog.txt (run did not reach jlLogFlush)" >&2
    exit 1
}
tr -d '\r' < "$work/x68.txt" > "$work/x68.clean"
cp "$work/x68.clean" "$repo/build/x68000/joeylog.txt"
"$repo/tools/diff-uber-hashes" "$GOLDEN" "$work/x68.clean"
