From 4f56ea85a6c0fc49ed158c808a3700eb3b6c4033 Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Tue, 9 Jul 2019 22:36:21 -0700 Subject: [PATCH 1/4] Added support for SNES autodetection of 256 Kb SRAM in v2proto_hirom --- host/scripts/snes/v2proto_hirom.lua | 44 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/host/scripts/snes/v2proto_hirom.lua b/host/scripts/snes/v2proto_hirom.lua index 1148b64..5bbe8c5 100644 --- a/host/scripts/snes/v2proto_hirom.lua +++ b/host/scripts/snes/v2proto_hirom.lua @@ -64,6 +64,7 @@ local ram_size_tbl = { [0x01] = "16 kilobits", [0x02] = "32 kilobits", [0x03] = "64 kilobits", + [0x05] = "256 kilobits" } -- Translates ram size in header to KBytes. @@ -72,6 +73,7 @@ local ram_size_kb_tbl = { [0x01] = 2, [0x02] = 4, [0x03] = 8, + [0x05] = 32 } local destination_code = { @@ -288,7 +290,7 @@ end function print_header(internal_header) local map_mode_str = lorom_name if (internal_header["map_mode"] & 1) == 1 then map_mode_str = hirom_name end - + -- For each field, default to showing hex value so program doesn't crash if a value is unexpected. local rom_type_str = "UNKNOWN - " .. hexfmt(internal_header["rom_type"]) if hardware_type[internal_header["rom_type"]] then rom_type_str = hardware_type[internal_header["rom_type"]] end @@ -308,7 +310,7 @@ function print_header(internal_header) if developer_code[internal_header["developer_code"]] then developer_code_str = developer_code[internal_header["developer_code"]] end - + print("Rom Title:\t\t" .. internal_header["rom_name"]) print("Map Mode:\t\t" .. map_mode_str .. " - " .. hexfmt(internal_header["map_mode"])) print("Hardware Type:\t\t" .. rom_type_str) @@ -338,7 +340,7 @@ function get_header(map_adjust) local addr_destination_code = 0xFFD9 - map_adjust -- 1 byte local addr_developer_code = 0xFFDA - map_adjust -- 1 byte (This is actually manufacturer ID) local addr_version = 0xFFDB - map_adjust -- 1 byte - local addr_compliment_check = 0xFFDC - map_adjust -- 2 bytes + local addr_compliment_check = 0xFFDC - map_adjust -- 2 bytes local addr_checksum = 0xFFDD - map_adjust -- 2 bytes local internal_header = { @@ -374,7 +376,7 @@ function isvalidheader(internal_header) return valid_rom_type and valid_rom_size and valid_sram_size and valid_destination_code end -function test() +function test() local hirom_header = get_header(0x0000) local lorom_header = get_header(0x8000) local internal_header = nil @@ -396,7 +398,7 @@ end -- local functions --- Desc: attempt to read flash rom ID +-- Desc: attempt to read flash rom ID -- Pre: snes_init() been called to setup i/o -- Post:Address left on bus memories disabled -- Rtn: true if proper flash ID found @@ -650,7 +652,7 @@ local function flash_rom(file, rom_size_KB, mapping, debug) --program the entire bank's worth of data - --[[ This version of the code programs a single byte at a time but doesn't require + --[[ This version of the code programs a single byte at a time but doesn't require -- board specific functions in the firmware print("This is slow as molasses, but gets the job done") byte_num = 0 --current byte within the bank @@ -703,7 +705,7 @@ local function wr_ram(file, first_bank, ram_size_KB, mapping, debug) --last of 512KB -- dict.snes("SNES_SET_BANK", 0x0F) wr_flash_byte(0x8000, 0xF5, true) wr_flash_byte(0xFFFF, 0xFA, true) - local base_addr + local base_addr local bank_size local buff_size = 1 --number of bytes to write at a time local cur_bank = 0 @@ -750,7 +752,7 @@ local function wr_ram(file, first_bank, ram_size_KB, mapping, debug) --program the entire bank's worth of data - ---[[ This version of the code programs a single byte at a time but doesn't require + ---[[ This version of the code programs a single byte at a time but doesn't require -- board specific functions in the firmware print("This is slow as molasses, but gets the job done") byte_num = 0 --current byte within the bank @@ -794,18 +796,18 @@ end ---Cart should be in reset state upon calling this function +--Cart should be in reset state upon calling this function --this function processes all user requests for this specific board/mapper local function process(process_opts, console_opts) local rv = nil - local file + local file --initialize device i/o for SNES dict.io("IO_RESET") dict.io("SNES_INIT") local internal_header = nil - + -- Use specified mapper if provided, otherwise autodetect. local snes_mapping = console_opts["mapper"] if (snes_mapping == lorom_name) then @@ -825,12 +827,12 @@ local function process(process_opts, console_opts) -- Use specified ram size if provided, otherwise autodetect. local ram_size = console_opts["wram_size_kb"] - + -- Use specified rom size if provided, otherwise autodetect. local rom_size = console_opts["rom_size_kbyte"] - - + + -- TODO: Put this in a function. -- SNES memory map banking @@ -839,8 +841,8 @@ local function process(process_opts, console_opts) -- A23 splits the map in half -- A22 splits it in quarters (between what's typically low half and high half) -- b 7 6 5 4 : 3 2 1 0 - -- A23 22 21 20 : 19 18 17 16 - + -- A23 22 21 20 : 19 18 17 16 + local rombank --first bank of rom byte that contains A23-16 local rambank --first bank of ram @@ -852,8 +854,8 @@ local function process(process_opts, console_opts) print_header(internal_header) -- Autodetect any missing parameters. - if isempty(snes_mapping) then - snes_mapping = internal_header["mapping"] + if isempty(snes_mapping) then + snes_mapping = internal_header["mapping"] print("Mapping not provided, " .. snes_mapping .. " detected.") if (snes_mapping == lorom_name) then -- LOROM typically sees the upper half (A15=1) of the first address 0b0000:1000_0000 @@ -865,7 +867,7 @@ local function process(process_opts, console_opts) rombank = 0xC0 --rombank = 0x40 --second HiROM bank (slow) rambank = 0x30 - end + end end if ram_size == 0 then @@ -887,7 +889,7 @@ local function process(process_opts, console_opts) end ---dump the ram to file +--dump the ram to file if dumpram then print("\nDumping SAVE RAM...") @@ -950,7 +952,7 @@ local function process(process_opts, console_opts) --open file file = assert(io.open(flashfile, "rb")) - --determine if auto-doubling, deinterleaving, etc, + --determine if auto-doubling, deinterleaving, etc, --needs done to make board compatible with rom --flash cart From 8cda1efe021e4189d096ee853fcc5402b6675bc9 Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Tue, 9 Jul 2019 23:24:06 -0700 Subject: [PATCH 2/4] Allow SNES Rom and Ram sizes to be displayed if not in table --- host/scripts/snes/v2proto_hirom.lua | 46 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/host/scripts/snes/v2proto_hirom.lua b/host/scripts/snes/v2proto_hirom.lua index 1148b64..0561958 100644 --- a/host/scripts/snes/v2proto_hirom.lua +++ b/host/scripts/snes/v2proto_hirom.lua @@ -288,7 +288,7 @@ end function print_header(internal_header) local map_mode_str = lorom_name if (internal_header["map_mode"] & 1) == 1 then map_mode_str = hirom_name end - + -- For each field, default to showing hex value so program doesn't crash if a value is unexpected. local rom_type_str = "UNKNOWN - " .. hexfmt(internal_header["rom_type"]) if hardware_type[internal_header["rom_type"]] then rom_type_str = hardware_type[internal_header["rom_type"]] end @@ -308,7 +308,7 @@ function print_header(internal_header) if developer_code[internal_header["developer_code"]] then developer_code_str = developer_code[internal_header["developer_code"]] end - + print("Rom Title:\t\t" .. internal_header["rom_name"]) print("Map Mode:\t\t" .. map_mode_str .. " - " .. hexfmt(internal_header["map_mode"])) print("Hardware Type:\t\t" .. rom_type_str) @@ -338,7 +338,7 @@ function get_header(map_adjust) local addr_destination_code = 0xFFD9 - map_adjust -- 1 byte local addr_developer_code = 0xFFDA - map_adjust -- 1 byte (This is actually manufacturer ID) local addr_version = 0xFFDB - map_adjust -- 1 byte - local addr_compliment_check = 0xFFDC - map_adjust -- 2 bytes + local addr_compliment_check = 0xFFDC - map_adjust -- 2 bytes local addr_checksum = 0xFFDD - map_adjust -- 2 bytes local internal_header = { @@ -368,13 +368,11 @@ function isvalidheader(internal_header) -- Spot check a few fields. -- TODO: Check more/all fields, look for printable name? local valid_rom_type = hardware_type[internal_header["rom_type"]] - local valid_rom_size = rom_ubound[internal_header["rom_size"]] - local valid_sram_size = ram_size_tbl[internal_header["sram_size"]] local valid_destination_code = destination_code[internal_header["destination_code"]] - return valid_rom_type and valid_rom_size and valid_sram_size and valid_destination_code + return valid_rom_type and internal_header["rom_size"] and internal_header["sram_size"] and valid_destination_code end -function test() +function test() local hirom_header = get_header(0x0000) local lorom_header = get_header(0x8000) local internal_header = nil @@ -396,7 +394,7 @@ end -- local functions --- Desc: attempt to read flash rom ID +-- Desc: attempt to read flash rom ID -- Pre: snes_init() been called to setup i/o -- Post:Address left on bus memories disabled -- Rtn: true if proper flash ID found @@ -650,7 +648,7 @@ local function flash_rom(file, rom_size_KB, mapping, debug) --program the entire bank's worth of data - --[[ This version of the code programs a single byte at a time but doesn't require + --[[ This version of the code programs a single byte at a time but doesn't require -- board specific functions in the firmware print("This is slow as molasses, but gets the job done") byte_num = 0 --current byte within the bank @@ -703,7 +701,7 @@ local function wr_ram(file, first_bank, ram_size_KB, mapping, debug) --last of 512KB -- dict.snes("SNES_SET_BANK", 0x0F) wr_flash_byte(0x8000, 0xF5, true) wr_flash_byte(0xFFFF, 0xFA, true) - local base_addr + local base_addr local bank_size local buff_size = 1 --number of bytes to write at a time local cur_bank = 0 @@ -750,7 +748,7 @@ local function wr_ram(file, first_bank, ram_size_KB, mapping, debug) --program the entire bank's worth of data - ---[[ This version of the code programs a single byte at a time but doesn't require + ---[[ This version of the code programs a single byte at a time but doesn't require -- board specific functions in the firmware print("This is slow as molasses, but gets the job done") byte_num = 0 --current byte within the bank @@ -794,18 +792,18 @@ end ---Cart should be in reset state upon calling this function +--Cart should be in reset state upon calling this function --this function processes all user requests for this specific board/mapper local function process(process_opts, console_opts) local rv = nil - local file + local file --initialize device i/o for SNES dict.io("IO_RESET") dict.io("SNES_INIT") local internal_header = nil - + -- Use specified mapper if provided, otherwise autodetect. local snes_mapping = console_opts["mapper"] if (snes_mapping == lorom_name) then @@ -825,12 +823,12 @@ local function process(process_opts, console_opts) -- Use specified ram size if provided, otherwise autodetect. local ram_size = console_opts["wram_size_kb"] - + -- Use specified rom size if provided, otherwise autodetect. local rom_size = console_opts["rom_size_kbyte"] - - + + -- TODO: Put this in a function. -- SNES memory map banking @@ -839,8 +837,8 @@ local function process(process_opts, console_opts) -- A23 splits the map in half -- A22 splits it in quarters (between what's typically low half and high half) -- b 7 6 5 4 : 3 2 1 0 - -- A23 22 21 20 : 19 18 17 16 - + -- A23 22 21 20 : 19 18 17 16 + local rombank --first bank of rom byte that contains A23-16 local rambank --first bank of ram @@ -852,8 +850,8 @@ local function process(process_opts, console_opts) print_header(internal_header) -- Autodetect any missing parameters. - if isempty(snes_mapping) then - snes_mapping = internal_header["mapping"] + if isempty(snes_mapping) then + snes_mapping = internal_header["mapping"] print("Mapping not provided, " .. snes_mapping .. " detected.") if (snes_mapping == lorom_name) then -- LOROM typically sees the upper half (A15=1) of the first address 0b0000:1000_0000 @@ -865,7 +863,7 @@ local function process(process_opts, console_opts) rombank = 0xC0 --rombank = 0x40 --second HiROM bank (slow) rambank = 0x30 - end + end end if ram_size == 0 then @@ -887,7 +885,7 @@ local function process(process_opts, console_opts) end ---dump the ram to file +--dump the ram to file if dumpram then print("\nDumping SAVE RAM...") @@ -950,7 +948,7 @@ local function process(process_opts, console_opts) --open file file = assert(io.open(flashfile, "rb")) - --determine if auto-doubling, deinterleaving, etc, + --determine if auto-doubling, deinterleaving, etc, --needs done to make board compatible with rom --flash cart From 20bb2b729e6891f18f04d3069c921299b2a5b448 Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Tue, 9 Jul 2019 23:59:36 -0700 Subject: [PATCH 3/4] Fixed SNES RAM and ROM check so if it is unknown and not provided it will break and alert user --- host/scripts/snes/v2proto_hirom.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/host/scripts/snes/v2proto_hirom.lua b/host/scripts/snes/v2proto_hirom.lua index 0561958..5d3d671 100644 --- a/host/scripts/snes/v2proto_hirom.lua +++ b/host/scripts/snes/v2proto_hirom.lua @@ -868,11 +868,13 @@ local function process(process_opts, console_opts) if ram_size == 0 then ram_size = ram_size_kb_tbl[internal_header["sram_size"]] - print("RAM Size not provided, " .. ram_size_tbl[internal_header["sram_size"]] .. " detected.") + assert(ram_size, "SRAM Size unknown and not provided, please add ram size to console_opts") + print("SRAM Size not provided, " .. ram_size_tbl[internal_header["sram_size"]] .. " detected.") end if rom_size == 0 then rom_size = rom_size_kb_tbl[internal_header["rom_size"]] + assert(rom_size, "ROM Size unknown and not provided, please add rom size to console_opts") print("ROM Size not provided, " .. rom_ubound[internal_header["rom_size"]] .. " detected.") end From c26dca8c8e2febc3459a485128c8d83a6696ad56 Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Wed, 10 Jul 2019 00:33:50 -0700 Subject: [PATCH 4/4] Fixed SNES v2proto_hirom to detect and set ROM size if not provided (or set to 0) and is known in rom_ubound table --- host/scripts/snes/v2proto_hirom.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/host/scripts/snes/v2proto_hirom.lua b/host/scripts/snes/v2proto_hirom.lua index 234e79f..3f8e48f 100644 --- a/host/scripts/snes/v2proto_hirom.lua +++ b/host/scripts/snes/v2proto_hirom.lua @@ -536,6 +536,7 @@ local function dump_ram( file, start_bank, ram_size_KB, mapping, debug ) local num_banks --determine how much ram to read per bank + if ram_size_KB == nil then ram_size_KB = 0 end if (ram_size_KB < KB_per_bank) then num_banks = 1 KB_per_bank = ram_size_KB @@ -868,13 +869,13 @@ local function process(process_opts, console_opts) end end - if ram_size == 0 then + if (ram_size == 0) or (ram_size == nil) then ram_size = ram_size_kb_tbl[internal_header["sram_size"]] assert(ram_size, "SRAM Size unknown and not provided, please add ram size to console_opts") print("SRAM Size not provided, " .. ram_size_tbl[internal_header["sram_size"]] .. " detected.") end - if rom_size == 0 then + if (rom_size == 0) or (rom_size == nil) then rom_size = rom_size_kb_tbl[internal_header["rom_size"]] assert(rom_size, "ROM Size unknown and not provided, please add rom size to console_opts") print("ROM Size not provided, " .. rom_ubound[internal_header["rom_size"]] .. " detected.")