#!/usr/bin/env bash
# probeQdStartup.sh - boot qdProbe.omf under GS/OS, dump the per-phase
# markers ($80..$86) plus SHR row 0/1/2 byte signatures.
#
# Tells us WHICH startup call (QDStartUp / EMStartUp / WindStartUp)
# happened, and whether any of them wipe the pre-existing SHR markers.

set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

CADIUS="${CADIUS:-$PROJECT_ROOT/tools/cadius/cadius}"
SYSDISK="${SYSDISK:-$PROJECT_ROOT/tools/gsos/sys602.po}"
OMF="$PROJECT_ROOT/demos/qdProbe.omf"
[ -f "$OMF" ] || bash "$PROJECT_ROOT/demos/build.sh" qdProbe >/dev/null

WORK=$(mktemp -d -t qdProbe.XXXXXX)
trap 'rm -rf "$WORK"' EXIT

cp "$SYSDISK" "$WORK/disk.po"
"$CADIUS" CREATEVOLUME "$WORK/data.po" DATA 800KB >/dev/null
cp "$OMF" "$WORK/HELLO#B30000"
"$CADIUS" ADDFILE "$WORK/data.po" /DATA "$WORK/HELLO#B30000" >/dev/null

cat > "$WORK/launch.lua" <<'LUA'
local cpu = manager.machine.devices[":maincpu"]
local mem = cpu.spaces["program"]
local nat = manager.machine.natkeyboard
local frame = 0; local idx = 1
local function gf(p,n) local pp = manager.machine.ioport.ports[p]; return pp and pp.fields[n] end
local kcmd = gf(":macadb:KEY3", "Command / Open Apple")
local function press(f) if f then f:set_value(1) end end
local function release(f) if f then f:set_value(0) end end
local steps = {
    {3300, function() nat:post("D") end},
    {3540, function() press(kcmd) end},
    {3546, function() nat:post("o") end},
    {3600, function() release(kcmd) end},
    {4200, function() nat:post("H") end},
    {4500, function() press(kcmd) end},
    {4506, function() nat:post("o") end},
    {4560, function() release(kcmd) end},
    {8500, function()
        for a = 0x70, 0x87 do
            local v = mem:read_u8(a)
            if v ~= 0 then
                print(string.format("DP[0x%02x]=%02x", a, v))
            end
        end
        -- SHR row sentinel sample (bytes 0..2 of rows 0/1/2)
        local function rowSample(off)
            local s = ""
            for i=0,3 do s = s .. string.format(" %02x", mem:read_u8(0xE12000 + off + i)) end
            return s
        end
        print("SHR-row0:" .. rowSample(0))
        print("SHR-row1:" .. rowSample(160))
        print("SHR-row2:" .. rowSample(320))
        print("SHR-row5:" .. rowSample(800))
        print("SHR-row10:" .. rowSample(1600))
        -- Count any other non-zero pixels (excluding our intentional rows 0/1/2).
        local count = 0
        for o = 480, 31999 do
            if mem:read_u8(0xE12000 + o) ~= 0 then count = count + 1 end
        end
        print(string.format("SHR-rest-nonzero=%d", count))
        local scb0 = mem:read_u8(0xE19D00)
        print(string.format("SHR-SCB0=%02x", scb0))
        manager.machine:exit()
    end},
}
emu.register_frame_done(function()
    frame = frame + 1
    while idx <= #steps and frame >= steps[idx][1] do
        steps[idx][2](); idx = idx + 1
    end
end)
LUA

timeout 200 mame apple2gs -rompath "$PROJECT_ROOT/tools/mame/roms" \
    -window -nothrottle -sound none \
    -seconds_to_run 180 -flop3 "$WORK/disk.po" -flop4 "$WORK/data.po" \
    -autoboot_script "$WORK/launch.lua" </dev/null 2>&1 | \
    grep -E "DP|SHR-|MAME" || true
