pdp.dev

MAME Lua Scripting

I previously wrote a post about the MAME Debugger which showed how to find where certain values are stored in a game’s memory.

MAME also has a Lua Scripting Engine that you can use to display memory values onscreen and do all kinds of cool stuff. I’ll again use Muchi Muchi Pork as an example.

In Muchi Muchi Pork, you build up your medal value by blowing up enemies with your lard shot. Each time you collect medals of a certain value, the value increases. It starts at 100 and increases by hundreds up to 1000, then it increases by thousands up to a maximum of 10000. So the progression is: 100, 200, …, 900, 1000, 2000, …, 10000. Building up your medal value is critical to scoring well in the game. If you let medals drop off the screen without collecting them, your medal value goes down by a factor of 10. Medal value of 10000 and you drop, medals are now worth 1000. Drop again and you’re all the way back to 100.

The medal values are shown on the medals themselves, but if there aren’t any on-screen then you don’t know what your medal value is. There are many cases where I’ve thought ‘oh shit I dropped medals’ and it turns out I didn’t, or I did drop and didn’t know it. What if we could always show the value on screen?

Well… you can! Using the debugger I found the medal value to live in a byte at 0xC53F4A7. Here’s a LUA script that will always show the value on screen:

cpu = manager.machine.devices[":maincpu"]
mem = cpu.spaces["program"]
s = manager.machine.screens[":screen"]

function draw_hud()
 -- medal_val ranges from 0 - 18
 -- 0 = 100
 -- 1 = 200
 -- ...
 -- 9 = 1,000
 -- 10 = 2,000
 -- ...
 -- 18 = 10,000
 medal_val = mem:read_u8(0xC53F4A7) + 1;
 medal_display = medal_val * 100;
 if medal_val > 10 then
   medal_display = (medal_val - 9) * 1000;
 end
 medal = string.format("NEXT MEDAL : %d", medal_display);
 -- Draw the text RED (0xffff0000) on BLACK (0xff000000)
 -- Format: 0xaarrggbb where a = alpha,
 s:draw_text(200, 3, medal, 0xffff0000, 0xff000000);
end
emu.register_frame_done(draw_hud, "frame")

Save that to a file, for example mmpork.medals.lua and then start MAME like this:

./mame mmpork -autoboot_script mmpork.medals.lua

Now you’ve got your medal values on-screen all the time!

Medal Value is now shown in the upper left!
Medal Value is now shown in the upper left!

This is helpful, but kinda ugly. There’s also a bunch of other data that would be helpful, like boss HP. Well… stay tuned!

This post is part of a 3-part series:

  1. MAME Debugger Intro
  2. You are here: MAME Lua Scripting
  3. Muchi Muchi Pork MAME Overlay