#!/bin/bash # mame_psg.sh SYSTEM GAME_IMAGE FRAMES OUTDIR [PRESSES] # Run the intv-only MAME on one cartridge image (or "none") for FRAMES # frames and record the PSG (AY-3-8914 at $01F0-$01FF). PRESSES (default # none) holds keys on both hand controllers: "F1-F2:KEY,..." presses KEY # (MAME's field name: 0-9, Clear, Enter, Upper, Lower-Left, Lower-Right, # or a disc direction of port DISC_DG: Up, Up-Up-Right, ..., Up-Up-Left, # MAME handctrl.cpp:37-53) at frame notifier F1 and releases it at F2 # (tools/psg_compare.py --press does the same in the model). A disc press # switches both controllers' "Controller Disc Emulation" to Digital first # (handctrl.cpp:62-65; nothing pressed reads $FF either way). SNAPS # (environment, optional) is a comma-separated list of frame notifiers at # which to save a snapshot: OUTDIR/snap/intv/NNNN.png in that order # (tools/stic_frames.py import turns them into PGMs); OUTDIR/snap must not # exist yet. Output: # OUTDIR/psg.log one line per CPU write to $01F0-$01FF, in order: # "c= f= pc= W " # (MAME debugger watchpoint, logged with its logerror # command into MAME's error.log, then filtered) # OUTDIR/ticks.bin the AY-3-8914's own three output streams at its # internal rate (clock / 8 = 223.7 kHz, one value per # generator step: MAME ay8910.cpp device_start), taken # with MAME's Lua sound hook (emu.register_sound_update, # device "hook" property): 3 bytes per step, each an id # of the float MAME output on that channel; # OUTDIR/ticks.ids lists "id float" per line # OUTDIR/audio.wav MAME's sound output, 48 kHz (-wavwrite) # tools/psg_compare.py compares both with tools/psg.py. Everything here is # derived from game data: OUTDIR must be outside any repository. # # Environment: MAME, MAME_ROMPATH, MAME_HASHPATH as for mame_trace.sh. # MAME debugger commands used (issued from Lua, since "-debugger none" does # not run a -debugscript): wpset, logerror, g (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:-} lp="" IFS=',' read -ra pp <<< "$presses" for p in "${pp[@]}"; do [ -n "$p" ] || continue f1=${p%%-*}; r=${p#*-}; f2=${r%%:*}; key=${r#*:} lp="$lp{$f1,$f2,\"$key\"}," done : "${MAME:?}" "${MAME_ROMPATH:?}" "${MAME_HASHPATH:?}" mkdir -p "$out"; out=$(cd "$out" && pwd) rm -f "$out/psg.log" "$out/error.log" "$out/audio.wav" if [ -n "${SNAPS:-}" ] && [ -e "$out/snap" ]; then echo "$out/snap exists: use a new OUTDIR" >&2; exit 2; fi cat > "$out/psg.lua" <= $frames then tf:close(); idf:close(); print(string.format("PSGEND frames=%d time=%.6f", n, m.time:as_double())); m:exit() end end) LUA cart=(-cart "$img") [ "$img" = none ] && cart=() cd "$out" rc=0 SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy timeout 900 "$MAME" "$sys" \ -rompath "$MAME_ROMPATH" -hashpath "$MAME_HASHPATH" "${cart[@]}" \ -debug -debugger none -log -autoboot_script "$out/psg.lua" -video none -sound none \ -samplerate 48000 -wavwrite "$out/audio.wav" -nothrottle \ -skip_gameinfo -nomouse -nojoystick -nvram_directory "$out/nvram" \ -cfg_directory "$out/cfg" -snapshot_directory "$out/snap" \ -seconds_to_run $(( frames / 30 + 5 )) > "$out/mame.log" 2>&1 || rc=$? grep '^PSGW ' "$out/error.log" | cut -c6- > "$out/psg.log" || true echo "rc=$rc writes=$(wc -l < "$out/psg.log") wav=$(stat -c %s "$out/audio.wav" 2>/dev/null || echo none)"