#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# profile-iigs.sh - PC-sampling profiler for a JoeyLib program on the IIgs.
#
# Boots a self-booting program volume (make-iigs-game-disk.sh's) under
# headless MAME and samples the 65816's program bank and counter every
# PROFILE_PERIOD_US of EMULATED time, from a Lua coroutine that emu.wait()s
# between samples. A per-frame sample would land at the same point of a
# frame-synced game loop every time; a period that is not a multiple of the
# frame's spreads the samples over the whole frame.
#
# The OMF carries no symbols and the GS/OS Loader puts each bank-aligned
# segment in whatever bank it likes, so the samples are tied back to the
# link map by the segments' BYTES: at the end the script reads the start of
# every bank a sample landed in and scores it against every segment image
# (the Loader's relocations change only a few bytes). A bank that matches
# no segment is GS/OS, a tool, NTP or the ROM, and is reported by bank.
#
# The map comes from building the program with JOEY_LINK_MAP=
, which
# keeps /NAME.map, NAME.seg.bin and NAME.json (clang-build.sh):
#
# rm -f build/iigs/bin/STAXI && JOEY_LINK_MAP=/some/dir make iigs
#
# The map lists static functions too (JOEY_LINK_MAP links with link816's
# --map-locals), but one the compiler inlined has no code of its own: its
# time is its caller's row.
#
# Usage: scripts/profile-iigs.sh