-- SPDX-License-Identifier: MIT
-- Log every access a machine's CPU makes to its YM2151, with the time in the
-- chip's own input clocks. Run headless by scripts/mame_tap.sh; see
-- docs/mame-oracle.md.
--
-- Environment:
-- YM_TAP_CPU tag of the CPU that owns the chip (default ":audiocpu")
-- YM_TAP_BASE address of the chip's A0 = 0 port, hex (default "f000")
-- YM_TAP_CHIP tag of the YM2151 device (default ":2151")
-- YM_TAP_CLOCK the chip's input clock in Hz (default 3579545)
-- YM_TAP_OUT the log file (default "ym2151-tap.txt")
--
-- Each line:
-- A write with A0 = 0 is the address, with A0 = 1 the data; a read is the status
-- MAME's own YM2151 answered, which is an emulator's opinion (tier 5).
--
-- The taps and the notifier are globals on purpose: MAME's Lua collects a tap
-- that nothing refers to, and the log silently stops.
local machine = manager.machine
local cpu_tag = os.getenv("YM_TAP_CPU") or ":audiocpu"
local chip_tag = os.getenv("YM_TAP_CHIP") or ":2151"
local base = tonumber(os.getenv("YM_TAP_BASE") or "f000", 16)
local path = os.getenv("YM_TAP_OUT") or "ym2151-tap.txt"
local cpu = machine.devices[cpu_tag]
local chip = machine.devices[chip_tag]
assert(cpu, "no CPU " .. cpu_tag)
assert(chip, "no chip " .. chip_tag)
-- MAME 0.285's Lua does not expose a device's clock (`chip.clock` is nil), so the
-- caller says it; scripts/mame_census.py lists each machine's.
local clock = tonumber(os.getenv("YM_TAP_CLOCK") or "3579545")
local space = cpu.spaces["program"]
local log = assert(io.open(path, "w"))
log:write(string.format("# machine %s chip %s clock %d cpu %s base %x\n",
emu.romname(), chip_tag, clock, cpu_tag, base))
ym_tap_writes = 0
ym_tap_reads = 0
ym_tap_write = space:install_write_tap(base, base + 1, "ym2151 write tap",
function(offset, data, mask)
ym_tap_writes = ym_tap_writes + 1
log:write(string.format("%d W %d %02X\n",
machine.time:as_ticks(clock), offset - base, data & 0xff))
end)
ym_tap_read = space:install_read_tap(base, base + 1, "ym2151 read tap",
function(offset, data, mask)
ym_tap_reads = ym_tap_reads + 1
log:write(string.format("%d R %d %02X\n",
machine.time:as_ticks(clock), offset - base, data & 0xff))
end)
-- Optional button presses, so a stream has notes in it (an attract mode often keys
-- nothing): YM_TAP_PRESS="2.0:Coin 1,3.0:1 Player Start" holds each named input
-- field for 0.2 emulated seconds from the given time.
ym_tap_presses = {}
for item in string.gmatch(os.getenv("YM_TAP_PRESS") or "", "[^,]+") do
local at, name = string.match(item, "^%s*([%d.]+):(.+)$")
local found = nil
for _, port in pairs(machine.ioport.ports) do
for field_name, field in pairs(port.fields) do
if field_name == name then found = field end
end
end
assert(found, "no input field " .. tostring(name))
table.insert(ym_tap_presses, {at = tonumber(at), field = found, state = 0})
end
ym_tap_frame = emu.add_machine_frame_notifier(function()
local now = machine.time:as_double()
for _, press in ipairs(ym_tap_presses) do
if press.state == 0 and now >= press.at then
press.field:set_value(1)
press.state = 1
elseif press.state == 1 and now >= press.at + 0.2 then
press.field:set_value(0)
press.state = 2
log:write(string.format("# pressed %s at %.2f s\n", press.field.name, press.at))
end
end
end)
ym_tap_stop = emu.add_machine_stop_notifier(function()
log:write(string.format("# end at %d clocks: %d writes, %d reads\n",
machine.time:as_ticks(clock), ym_tap_writes, ym_tap_reads))
log:close()
end)