Skip to content

Instantly share code, notes, and snippets.

@taotao54321
Last active April 18, 2023 05:21
Show Gist options
  • Save taotao54321/86056238b4baad818211f107e7068c3e to your computer and use it in GitHub Desktop.
Save taotao54321/86056238b4baad818211f107e7068c3e to your computer and use it in GitHub Desktop.
NES Dragon Quest (J): lua scripts for memory editing
--[[
Script for Mesen: loads RAM from a file when the glitch is executed.
(just after continuing the message "しのくびかざりが あなたの からだを しめつける。")
--]]
-- NOTE: I recommend to set an absolute path.
local DUMP_PATH = "dump.bin"
local function str_to_bytes(s)
local bytes = {}
for i = 1, #s do
table.insert(bytes, string.byte(s:sub(i, i)))
end
return bytes
end
local function load_dump(path)
local rdr = assert(io.open(path, "rb"))
local ram_str = rdr:read("*all")
rdr:close()
local ram_bytes = str_to_bytes(ram_str)
for i, b in ipairs(ram_bytes) do
emu.write(i - 1, b, emu.memType.cpu)
end
end
local function main()
local handle = nil
handle = emu.addMemoryCallback(function (_addr, _value)
load_dump(DUMP_PATH)
emu.log("loaded from " .. DUMP_PATH)
emu.removeMemoryCallback(handle, emu.memCallbackType.cpuExec, 0xBEE3)
end, emu.memCallbackType.cpuExec, 0xBEE3)
end
main()
--[[
Script for Mesen: loads RAM to a file when the glitch is executed.
(just after cancelling to drop item)
--]]
-- NOTE: I recommend to set an absolute path.
local DUMP_PATH = "dump.bin"
local function str_to_bytes(s)
local bytes = {}
for i = 1, #s do
table.insert(bytes, string.byte(s:sub(i, i)))
end
return bytes
end
local function load_dump(path)
local rdr = assert(io.open(path, "rb"))
local ram_str = rdr:read("*all")
rdr:close()
local ram_bytes = str_to_bytes(ram_str)
for i, b in ipairs(ram_bytes) do
emu.write(i - 1, b, emu.memType.cpu)
end
end
local function main()
local handle = nil
handle = emu.addMemoryCallback(function (_addr, _value)
load_dump(DUMP_PATH)
emu.log("loaded from " .. DUMP_PATH)
emu.removeMemoryCallback(handle, emu.memCallbackType.cpuExec, 0xDE72)
end, emu.memCallbackType.cpuExec, 0xDE72)
end
main()
--[[
Script for BizHawk: saves RAM to a file when the glitch is executed.
(just after continuing the message "しのくびかざりが あなたの からだを しめつける。")
--]]
-- NOTE: I recommend to set an absolute path.
local DUMP_PATH = "dump.bin"
local function save_dump(path)
local ram_bytes = mainmemory.read_bytes_as_array(0, 0x800)
local ram_str = string.char(unpack(ram_bytes))
local wtr = assert(io.open(path, "wb"))
wtr:write(ram_str)
wtr:close()
end
local function main()
local guid = nil
guid = event.onmemoryexecute(function (_addr, _val, _flags)
save_dump(DUMP_PATH)
print("dumped to " .. DUMP_PATH)
assert(event.unregisterbyid(guid))
end, 0xBEE3)
end
main()
--[[
Script for FCEUX: saves RAM to a file when the glitch is executed.
(just after cancelling to drop item)
--]]
-- NOTE: I recommend to set an absolute path.
local DUMP_PATH = "dump.bin"
local function save_dump(path)
local ram_str = memory.readbyterange(0, 0x800)
local wtr = assert(io.open(path, "wb"))
wtr:write(ram_str)
wtr:close()
end
local function main()
memory.registerexec(0xDE72, function (_addr, _size, _value)
save_dump(DUMP_PATH)
emu.print("dumped to " .. DUMP_PATH)
memory.registerexec(0xDE72, nil)
end)
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment