Page 1 of 1

Quick example of lua cheat finder scripting.

Posted: Sat May 07, 2016 5:02 am
by crazyc
Here's a quick script to illustrate scripting the lua cheat finder. It watches for the "Coin 1" press and shows all ram bytes that increase by one. Might help in finding the credit value. Use with autoboot_script like "mame.exe asteroid -plugin cheatfind -window -autoboot_script credit_find.lua". The cheat finder plugin must be enabled.

Code: Select all

local matches = {}
local maincpuram = {}
local current = {}
local coin
local last

local space_table = cf.getspaces()
for tag, list in pairs(space_table) do
	if tag == ":maincpu" and list.program then
		local ram = {}
		for num, entry in pairs(list.program.map) do
			if entry.writetype == "ram" then
				ram[#ram + 1] = { offset = entry.offset, size = entry.endoff - entry.offset }
			end
		end
		if next(ram) then
			maincpuram = { tag = tag, space = list.program, ram = ram }
		end
	end
end
for num, region in ipairs(maincpuram.ram) do
	matches[num] = {}
	current[num] = cf.save(maincpuram.space, region.offset, region.size)
end
for tag, port in pairs(manager:machine():ioport().ports) do
	if port.fields["Coin 1"] then
		coin = { port = port, field = port.fields["Coin 1"] }
		last = coin.port:read()
		break
	end
end

local function check()
	if ((coin.port:read() & coin.field.mask) ~ coin.field.defvalue) ~= 0 then
		if coin.port:read() == last then
			return
		end
		for num, region in ipairs(maincpuram.ram) do
			local old = current[num]
			current[num] = cf.save(maincpuram.space, region.offset, region.size)
			if not next(matches[num]) then
				matches[num] = cf.comp(current[num], old, "gt", "B", 1, false)
			else
				matches[num] = cf.compnext(current[num], old, matches[num], "gt", "B", 1, false)
			end
			for num, match in ipairs(matches[num]) do
				print(string.format("%08x %02x %02x", match.addr, match.oldval, match.newval))
			end
		end
	end
	last = coin.port:read()
end

if coin then
	emu.register_frame(check)
else
	print("Coin 1 not found")
end