#!/bin/bash # mame_ecs_trace.sh SYSTEM GAME_IMAGE FRAMES OUTDIR [PRESSES] [-- MAME ARGS...] # The ECS references (build step 9): mame_trace.sh's per-instruction trace # (OUTDIR/trace.log, same format: tools/intv_system.py mame reads it) of # the intv-only MAME, with keys held as the model holds them # (tools/intv_system.py parse_presses, "F1-F2:KEY,..."): hand-controller # keys by MAME field name on both controllers (as mame_psg.sh), "ecs:K1+K2" # ECS keyboard keys (tools/ecs.py KEYPOS: row r = MAME port ROW.r, column c # = bit c; ecs_ctrl.cpp) and "synth:N1+N2" synthesizer keys (note n = port # SYNTH.(n / 8) bit n % 8, C6 = SYNTH.6 bit 7), each set at frame notifier # F1 and released at F2. SNAPS (environment, optional): frame notifiers at # which to save a snapshot (OUTDIR/snap//NNNN.png in order). # GAME_IMAGE "none" runs with an empty slot; "sl:NAME" loads softlist entry # NAME (MAME_ROMPATH_EXTRA is added to the ROM path: WSMLB's four pieces). # MAME ARGS go to MAME (e.g. -ecs:ctrl_port synth). Traces come from game # data and the owner's ROMs: OUTDIR must be outside any repository. # # Environment: MAME, MAME_ROMPATH, MAME_HASHPATH (as mame_trace.sh). # MAME debugger commands used (from Lua): trace, tracelog, wpset, g, # traceflush (MAME docs, docs/source/debugger/; MAME is BSD-3). set -euo pipefail sys=${1:?SYSTEM}; img=${2:?IMAGE}; frames=${3:?FRAMES}; out=${4:?OUTDIR}; presses=${5-} shift 4; [ $# -gt 0 ] && [ "$1" != "--" ] && shift; [ "${1-}" = "--" ] && shift : "${MAME:?}" "${MAME_ROMPATH:?}" "${MAME_HASHPATH:?}" here=$(cd "$(dirname "$0")" && pwd) mkdir -p "$out"; out=$(cd "$out" && pwd) rm -f "$out/trace.log" if [ -n "${SNAPS:-}" ] && [ -e "$out/snap" ]; then echo "$out/snap exists: use a new OUTDIR" >&2; exit 2; fi lp=$(python3 -I - "$here/.." "$presses" <<'PY' import sys sys.path.insert(0, sys.argv[1]) import intv_system as s out = [] for f1, f2, code in s.parse_presses(sys.argv[2]): if isinstance(code, tuple) and code[0] == "ecs": bits, synth = code[1], False # synth presses are written "synth:" in the text; the bits are the same matrix for b in range(64): if bits >> b & 1: out.append('{%d,%d,"M",%d,%d}' % (f1, f2, b // 8, 1 << (b % 8))) elif isinstance(code, tuple): raise SystemExit("pocket: presses are not supported here") else: name = [k for k, v in s.KEYCODE.items() if v == code][0] out.append('{%d,%d,"H","%s"}' % (f1, f2, name)) print(",".join(out)) PY ) synth=0 case " $* " in *" synth "*) synth=1 ;; esac cat > "$out/trace.lua" <= $frames then d:command("traceflush"); m:exit() end end) LUA case "$img" in none) cart=() ;; sl:*) cart=(-cart "${img#sl:}") ;; *) cart=(-cart "$img") ;; esac rp=$MAME_ROMPATH; [ -n "${MAME_ROMPATH_EXTRA:-}" ] && rp="$rp;$MAME_ROMPATH_EXTRA" cd "$out" rc=0 SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy timeout "${MAME_TIMEOUT:-3600}" "$MAME" "$sys" \ -rompath "$rp" -hashpath "$MAME_HASHPATH" "${cart[@]}" "$@" \ -debug -debugger none -autoboot_script "$out/trace.lua" -video none -sound none -nothrottle \ -skip_gameinfo -nomouse -nojoystick -nvram_directory "$out/nvram" \ -cfg_directory "$out/cfg" -snapshot_directory "$out/snap" \ -seconds_to_run $(( frames / 30 + 10 )) > "$out/mame.log" 2>&1 || rc=$? echo "rc=$rc lines=$(wc -l < "$out/trace.log") snaps=$(find "$out/snap" -name '*.png' 2>/dev/null | wc -l)"