Commit: d57dc5451bd20669c94eb855067aa3758d50321d Parent: d159c16b5dd27ce17f7a7c63aeeef00310389673 Author: Vi Grey Date: 2023-08-06 00:53 UTC Summary: Make dragonwarrior script more stable host/scripts/nes/dragonwarrior.lua | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/host/scripts/nes/dragonwarrior.lua b/host/scripts/nes/dragonwarrior.lua index 4cdb04c..e93b7e6 100644 --- a/host/scripts/nes/dragonwarrior.lua +++ b/host/scripts/nes/dragonwarrior.lua @@ -1,6 +1,13 @@ +-- create the module's table +local dragonwarrior = {} + -- import required modules local dict = require "scripts.app.dict" +-- file constants +local mapname = "DRAGONWARRIOR" + + local function unset_game_mode() dict.nes("NES_CPU_WR", 0xFFF0, 0x00) dict.nes("NES_CPU_WR", 0xFFF1, 0x00) @@ -67,40 +74,55 @@ local function sanity_check(content) return false end +-- Get 3 save datas, one for each slot, and return each save slot data local function check_save_slots(file, content) - local save_data = "" + local slot_1 = "" + local slot_2 = "" + local slot_3 = "" + local slot_1_valid = false + local slot_2_valid = false + local slot_3_valid = false for i = 0, 2, 1 do for x = 0, 9, 1 do local c_start = 0x69 + (i * 0x140) + (x * 0x20) local slot_content = string.sub(content, c_start, c_start + 0x1F) local sc = sanity_check(slot_content) if sc then - print(sc) for y = 0, 9, 1 do - save_data = save_data .. slot_content + if i == 0 then + slot_1 = slot_1 .. slot_content + slot_1_valid = true + elseif i == 1 then + slot_2 = slot_2 ..slot_content + slot_2_valid = true + else + slot_3 = slot_3 .. slot_content + slot_3_valid = true + end end break end if x == 9 then - print("ERROR: Issue Detecting Dragon Warrior Save Data") - print("Try readjusting the Dragon Warrior cartridge") - return save_data + slot_content = string.sub(content, c_start, c_start + 0x13F) + if i == 0 then + slot_1 = slot_content + elseif i == 1 then + slot_2 = slot_content + else + slot_3 = slot_content + end end end end - return save_data + return slot_1, slot_1_valid, slot_2, slot_2_valid, slot_3, slot_3_valid end local function process(process_opts, console_opts) + local dumpram = process_opts["dumpram"] local ramdumpfile = process_opts["dumpram_filename"] - - print("**IO_RESET**") dict.io("IO_RESET") - print("**NES_INIT**") dict.io("NES_INIT") - print("Be sure Dragon Warrior is connected to a Game Genie") - if dumpram then print("\nDumping WRAM...") file = assert(io.open(ramdumpfile, "wb")) @@ -110,18 +132,36 @@ local function process(process_opts, console_opts) enable_sram() local content = read_sram(file) disable_sram() - local save_data = check_save_slots(file, content) - - if string.len(save_data) > 0 then - save_data = string.rep("\x00", 0x23) .. save_data - save_data = "KEN MASUTA" .. save_data - save_data = string.rep("\x00", 0x02) .. save_data - save_data = string.sub(content, 0x36, 0x39) .. save_data - save_data = string.rep("\x00", 0x35) .. save_data - save_data = save_data .. "KEN MASUTA" - save_data = save_data .. string.rep("\x00", 0x1BCE) - file:write(save_data) - end + local slot_1, slot_1_valid, slot_2, slot_2_valid, slot_3, slot_3_valid = check_save_slots(file, content) + file:write(string.rep("\x00", 0x35)) + file:write(string.sub(content, 0x36, 0x39)) + file:write(string.rep("\x00", 0x02)) + file:write("KEN MASUTA") + file:write(string.rep("\x00", 0x23)) + if slot_1_valid == false then + print("[!] Corrupted slot 1 save data. Saving slot 1 data anyways.") + print("[!] Dragon Warrior cartridge or Game Genie may need to be adjusted.") + end + file:write(slot_1) + if slot_2_valid == false then + print("[!] Corrupted slot 2 save data. Saving slot 2 data anyways.") + print("[!] Dragon Warrior cartridge or Game Genie may need to be adjusted.") + end + file:write(slot_2) + if slot_3_valid == false then + print("[!] Corrupted slot 3 save data. Saving slot 3 data anyways.") + print("[!] Dragon Warrior cartridge or Game Genie may need to be adjusted.") + end + file:write(slot_3) + file:write("KEN MASUTA") + file:write(string.rep("\x00", 0x1bce)) + print("Dragon Warrior save file written to " .. ramdumpfile) file.close() end end + +-- functions other modules are able to call +dragonwarrior.process = process + +-- return the module's table +return dragonwarrior