-- mame_dump.lua: drive MAME's galaga as the board oracle for emu-galaga. -- -- Run as `-autoboot_script oracle/mame_dump.lua` (see docs/mame-oracle.md for -- the full command line). Writes one JSON object per line to GALAGA_OUT: -- periodic RAM snapshots (video RAM and the three sprite RAM windows) and the -- device writes seen between them, then exits MAME at the last frame. -- -- Configuration is by environment variable (all optional): -- GALAGA_FRAMES frames to run before exiting (default 600) -- GALAGA_DUMP_EVERY dump RAM every N frames; the last frame is always dumped (default 60) -- GALAGA_OUT output JSONL path (default galaga.jsonl) -- GALAGA_INPUTS "frame:control:pressed,..." e.g. "120:coin1:1,130:coin1:0,200:start1:1" -- controls: coin1 coin2 start1 start2 p1_left p1_right p1_fire -- p2_left p2_right p2_fire service -- GALAGA_SNAPS "frame,frame,..." frames at which to save a PNG screenshot -- GALAGA_SNAP_DIR directory for those PNGs (default: MAME's -snapshot_directory) -- GALAGA_EVENTS 1 to record device writes/reads as events (default 1), 0 to skip -- GALAGA_LOG 1 to print progress to stdout (default 0) -- GALAGA_FIELDS "port:field:value,..." input fields set before the run, e.g. "DSWB:Coinage:0" (free play) -- GALAGA_WATCH "addr,addr,..." (hex or decimal) RAM addresses whose every write is logged, -- with the machine time and CPU, to GALAGA_WATCH_OUT (default watch.jsonl) local cfg = { frames = tonumber(os.getenv("GALAGA_FRAMES") or "600"), dump_every = tonumber(os.getenv("GALAGA_DUMP_EVERY") or "60"), out = os.getenv("GALAGA_OUT") or "galaga.jsonl", inputs = os.getenv("GALAGA_INPUTS") or "", snaps = os.getenv("GALAGA_SNAPS") or "", snap_dir = os.getenv("GALAGA_SNAP_DIR"), events = (os.getenv("GALAGA_EVENTS") or "1") ~= "0", log = (os.getenv("GALAGA_LOG") or "0") ~= "0", fields = os.getenv("GALAGA_FIELDS") or "", watch = os.getenv("GALAGA_WATCH") or "", watch_out = os.getenv("GALAGA_WATCH_OUT") or "watch.jsonl", } -- The board's RAM as the maincpu sees it (MAME src/mame/namco/galaga.cpp). local RAM = { { name = "vram", lo = 0x8000, hi = 0x87ff }, -- tile codes 0x8000-0x83ff, attributes 0x8400-0x87ff { name = "sprite1", lo = 0x8b80, hi = 0x8bff }, -- RAM1 + 0x380: sprite code / colour { name = "sprite2", lo = 0x9380, hi = 0x93ff }, -- RAM2 + 0x380: sprite x / y { name = "sprite3", lo = 0x9b80, hi = 0x9bff }, -- RAM3 + 0x380: sprite flip / size / x msb } -- Device windows on which write taps are installed, on every Z80 that maps them. local TAPS = { { kind = "wsg", lo = 0x6800, hi = 0x681f }, -- Namco WSG (3 voices) registers { kind = "latch", lo = 0x6820, hi = 0x6827 }, -- 74LS259 misc latch: irq enables, reset, flip { kind = "bus06", lo = 0x7000, hi = 0x70ff }, -- Namco 06xx data window (51xx/54xx) { kind = "ctl06", lo = 0x7100, hi = 0x7100 }, -- Namco 06xx control { kind = "star", lo = 0xa000, hi = 0xa007 }, -- 74LS259 video latch: starfield control } -- Input controls -> port tag and field display name (from iterating ioport.ports). local CONTROLS = { coin1 = { ":IN1", "Coin 1" }, coin2 = { ":IN1", "Coin 2" }, start1 = { ":IN1", "1 Player Start" }, start2 = { ":IN1", "2 Players Start" }, p1_fire = { ":IN1", "P1 Button 1" }, p2_fire = { ":IN1", "P2 Button 1" }, service = { ":IN1", "Service 1" }, p1_left = { ":IN0", "P1 Left" }, p1_right = { ":IN0", "P1 Right" }, p2_left = { ":IN0", "P2 Left" }, p2_right = { ":IN0", "P2 Right" }, } local machine = manager.machine local screen = machine.screens[":screen"] local cpus = { maincpu = ":maincpu", sub = ":sub", sub2 = ":sub2" } local space = machine.devices[":maincpu"].spaces["program"] local function log(...) if cfg.log then print("[mame_dump]", ...) end end local out = assert(io.open(cfg.out, "w")) local function hex(s) return (s:gsub(".", function(c) return string.format("%02x", c:byte()) end)) end -- Parse the input schedule into a list sorted by frame. local schedule = {} for at, control, pressed in cfg.inputs:gmatch("(%d+):([%w_]+):(%d)") do assert(CONTROLS[control], "unknown control " .. control) schedule[#schedule + 1] = { at = tonumber(at), control = control, pressed = pressed == "1" } end table.sort(schedule, function(a, b) return a.at < b.at end) local next_input = 1 local snap_at = {} for f in cfg.snaps:gmatch("%d+") do snap_at[tonumber(f)] = true end -- Events accumulated since the last dump. local events = {} local current_frame = 0 local function add_event(cpu, kind, addr, data, is_read) events[#events + 1] = string.format('{"f":%d,"cpu":"%s","kind":"%s","addr":%d,"data":%d%s}', current_frame, cpu, kind, addr, data, is_read and ',"read":true' or "") end -- Taps: the returned handler objects MUST be kept, or the GC removes the tap. tap_handles = {} if cfg.events then for cpu, tag in pairs(cpus) do local sp = machine.devices[tag].spaces["program"] for _, t in ipairs(TAPS) do tap_handles[#tap_handles + 1] = sp:install_write_tap(t.lo, t.hi, "dump_" .. cpu .. "_" .. t.kind, function(offset, data, mask) add_event(cpu, t.kind, offset, data & 0xff, false) end) end end -- Reads of the 06xx window on the main CPU are how the 51xx's input/credit reply reaches the game. tap_handles[#tap_handles + 1] = space:install_read_tap(0x7000, 0x70ff, "dump_maincpu_bus06_r", function(offset, data, mask) add_event("maincpu", "bus06", offset, data & 0xff, true) end) -- MAME drops pass-through handlers when a space's handlers change. A -- space:add_change_notifier probe saw no change in 1500 frames on this board -- (and holding that subscription to exit segfaults 0.285), so none is used; -- do NOT call tap:reinstall() on a tap that is still installed (heap corruption). end -- Watched addresses: every write from any CPU, stamped with the machine time. watch_handles = {} local watch_file if cfg.watch ~= "" then watch_file = assert(io.open(cfg.watch_out, "w")) for a in cfg.watch:gmatch("[^,]+") do local addr = tonumber(a) for cpu, tag in pairs(cpus) do local sp = machine.devices[tag].spaces["program"] watch_handles[#watch_handles + 1] = sp:install_write_tap(addr, addr, "watch_" .. cpu .. "_" .. addr, function(offset, data, mask) watch_file:write(string.format('{"f":%d,"time":"%s","cpu":"%s","addr":%d,"data":%d}\n', current_frame, tostring(machine.time), cpu, offset, data & 0xff)) end) end end end for tag, name, value in cfg.fields:gmatch("([^:,]+):([^:,]+):([^,]+)") do local port = machine.ioport.ports[":" .. tag] assert(port, "no port :" .. tag) local field = port.fields[name] assert(field, "no field " .. name .. " on " .. tag) -- DIP switches read their configured value, not the live one set_value drives. field.user_value = tonumber(value) log("field", tag, name, "=", value, "now", field.user_value) end local function set_input(control, pressed) local port_tag, field_name = table.unpack(CONTROLS[control]) local field = machine.ioport.ports[port_tag].fields[field_name] if pressed then field:set_value(1) else field:clear_value() end end local function dump(frame) local parts = {} for _, r in ipairs(RAM) do parts[#parts + 1] = string.format('"%s":"%s"', r.name, hex(space:read_range(r.lo, r.hi, 8))) end local ports = string.format('"in0":%d,"in1":%d', machine.ioport.ports[":IN0"]:read() & 0xff, machine.ioport.ports[":IN1"]:read() & 0xff) out:write(string.format('{"frame":%d,"time":"%s",%s,%s,"events":[%s]}\n', frame, tostring(machine.time), ports, table.concat(parts, ","), table.concat(events, ","))) events = {} end -- The subscription objects MUST be kept in globals: a local is collected and -- the notifier silently stops firing (observed in 0.285). frame_sub = emu.add_machine_frame_notifier(function() local frame = screen:frame_number() -- a method in 0.285, not a property current_frame = frame + 1 -- events seen from now on belong to the next frame -- Inputs scheduled for this frame land before the next frame is emulated. while next_input <= #schedule and schedule[next_input].at <= frame do local s = schedule[next_input] set_input(s.control, s.pressed) log("frame", frame, "input", s.control, s.pressed) next_input = next_input + 1 end if snap_at[frame] then local name = string.format("frame%05d.png", frame) if cfg.snap_dir then name = cfg.snap_dir .. "/" .. name end local err = screen:snapshot(name) log("frame", frame, "snapshot", name, err or "ok") end local last = frame + 1 >= cfg.frames if frame % cfg.dump_every == 0 or last then dump(frame) end if last then out:close() if watch_file then watch_file:close() end log("done at frame", frame) machine:exit() end end)