-- Trace Donkey Kong's sound MCU under MAME 0.285. -- -- The part is a Fujitsu MB8884 -- an 8035, i.e. a romless 64-byte-RAM MCS-48 -- -- at 6 MHz on the tag ":soundcpu". See docs/mame-oracle.md for the verified -- command line; scripts/mame/trace_dkong.sh wraps the whole thing. -- -- Environment: -- TRACE_FILE output file for the disassembly (default dkong_soundcpu.txt) -- TRACE_START_FRAME frame at which to start tracing (default 400; 0 = from reset) -- TRACE_STOP_FRAME frame at which to stop and exit (default 460) -- TRACE_IDLE set to 1 to skip the coin/start/joystick injection -- TRACE_CPU device tag to trace (default :soundcpu) -- -- Register lines go to error.log, which MAME writes into the *current working -- directory* -- not next to -homepath. Line N of error.log is the state -- *before* the instruction on line N of TRACE_FILE. local function envnum(name, default) local value = os.getenv(name) return (value and tonumber(value)) or default end local TRACE_FILE = os.getenv("TRACE_FILE") or "dkong_soundcpu.txt" local START_FRAME = envnum("TRACE_START_FRAME", 400) local STOP_FRAME = envnum("TRACE_STOP_FRAME", 460) local IDLE = os.getenv("TRACE_IDLE") == "1" local machine = manager.machine local debugger = machine.debugger local CPU_TAG = os.getenv("TRACE_CPU") or ":soundcpu" local soundcpu = machine.devices[CPU_TAG] if soundcpu == nil then print("no " .. CPU_TAG .. " device; the tags in this machine are:") for tag, device in pairs(machine.devices) do print(" " .. tag) end machine:exit() return end debugger.visible_cpu = soundcpu -- MAME 0.285 exposes no PSW register for this core. "curflags" is the whole -- PSW byte (STATE_GENFLAGS is bound to m_psw); "sp" is its low three bits. -- Naming a register that does not exist -- "psw", for instance -- makes the -- whole trace action produce nothing, silently. See docs/mame-oracle.md. local FORMAT = '{logerror "%03X A=%02X PSW=%02X SP=%X P1=%02X P2=%02X TC=%02X TPRE=%02X EA=%X ' .. 'R0=%02X R1=%02X R2=%02X R3=%02X R4=%02X R5=%02X R6=%02X R7=%02X\\n",' .. 'curpc,a,curflags,sp,p1,p2,tc,tpre,ea,r0,r1,r2,r3,r4,r5,r6,r7}' local function start_trace() debugger:command('trace ' .. TRACE_FILE .. ',,noloop,' .. FORMAT) end local function hold(port_tag, field_name, pressed) local port = machine.ioport.ports[port_tag] if port == nil then return end local field = port.fields[field_name] if field == nil then return end field:set_value(pressed and 1 or 0) end local tracing = false if START_FRAME <= 0 then start_trace() tracing = true end local frame = 0 emu.register_frame_done(function() frame = frame + 1 -- The sound MCU idles in a short DAC loop until the main Z80 commands it, -- and the Z80 only does that in a game. Insert a coin, press start, then -- walk and jump so the program reaches its jump, walk and fall routines. if not IDLE then if frame == 240 then hold(":IN2", "Coin 1", true) end if frame == 252 then hold(":IN2", "Coin 1", false) end if frame == 300 then hold(":IN2", "1 Player Start", true) end if frame == 312 then hold(":IN2", "1 Player Start", false) end if frame >= 380 then hold(":IN0", "P1 Right", (frame % 60) < 40) hold(":IN0", "P1 Button 1", (frame % 60) == 45) end end if frame == START_FRAME then start_trace() tracing = true end if frame >= STOP_FRAME then if tracing then debugger:command("trace off") end machine:exit() end end) debugger:command("go")