From c208924e45b8d4dbd0c15d575e92f8bc4bbc864e Mon Sep 17 00:00:00 2001 From: Paul Black ASUS win7 Date: Thu, 15 Feb 2018 14:34:52 -0600 Subject: [PATCH] Have BNROM 512KB PRG-ROM working for dumping and flashing. Realizing much of my problems with discrete boards on original kazzos was that I completely ignored the fact that most discretes are subject to bus conflicts. This makes writes to the mapper bank selecting uber flakey... Created routine in bnrom.lua script to start flash operation by writing the bank table to where Lizard puts it. Need to write routine to find the bank table in a provided rom for flashing. And dumping needs to find the bank table prior to making mapper writes and then use it for bank switching! Tested and working on AVR, stm adapter, and inl6 Lizard 512KB flash/dump: AVR decrepit old firmware & app: 7.9KBps flash AVR new firmware and app (this build) F:12.1KBps D:14.6KBps STM adapter: F:31KBps D:114KBps INL6: F:40KBps D:120KBps --- firmware/source/dump.c | 8 +++++ firmware/source/flash.c | 37 +++++++++++++++++---- host/scripts/inlretro.lua | 8 +++-- host/scripts/nes/bnrom.lua | 68 +++++++++++++++++++++++++++++++++++--- 4 files changed, 107 insertions(+), 14 deletions(-) diff --git a/firmware/source/dump.c b/firmware/source/dump.c index 1a9f4cd..eed0f37 100644 --- a/firmware/source/dump.c +++ b/firmware/source/dump.c @@ -9,6 +9,7 @@ uint8_t dump_buff( buffer *buff ) { uint8_t addrH = buff->page_num; //A15:8 while accessing page + uint8_t bank; //warn uint8_t addrX; //A23:16 while accessing page //TODO use mapper to set mapper controlled address bits @@ -18,6 +19,13 @@ uint8_t dump_buff( buffer *buff ) { switch ( buff->mem_type ) { case PRGROM: addrH |= 0x80; //$8000 + if (buff->mapper == BxROM) { + bank = (buff->page_num)>>7; + //write bank value to bank table + //page_num shift by 6 bits A15 >> A8(0) + //Lizard bank table @ FF94 + nes_cpu_wr( (0xFF94+bank), bank ); + } buff->cur_byte = nes_cpu_page_rd_poll( buff->data, addrH, buff->id, //id contains MSb of page when <256B buffer buff->last_idx, ~FALSE ); diff --git a/firmware/source/flash.c b/firmware/source/flash.c index 8c36852..35f267f 100644 --- a/firmware/source/flash.c +++ b/firmware/source/flash.c @@ -39,10 +39,18 @@ uint8_t write_page( uint8_t bank, uint8_t addrH, buffer *buff, write_funcptr wr_ read = rd_func((addrH<<8)|n); } while( read != rd_func((addrH<<8)|n) ); - //TODO verify byte is value that was trying to be flashed - //move on to next byte - n++; - cur++; + + //retry if write failed + //this helped but still seeing similar fails to dumps + if (read == buff->data[n]) { + n++; + cur++; + LED_IP_PU(); + LED_LO(); + } else { + LED_OP(); + LED_HI(); + } } @@ -94,8 +102,17 @@ uint8_t write_page_chr( uint8_t bank, uint8_t addrH, buffer *buff, write_funcptr } while( read != rd_func((addrH<<8)|n) ); //TODO verify byte is value that was trying to be flashed //move on to next byte - n++; - cur++; + //n++; + //cur++; + if (read == buff->data[n]) { + n++; + cur++; + LED_IP_PU(); + LED_LO(); + } else { + LED_OP(); + LED_HI(); + } } @@ -324,6 +341,7 @@ uint8_t write_page_snes( uint8_t bank, uint8_t addrH, buffer *buff, write_funcpt uint8_t flash_buff( buffer *buff ) { uint8_t addrH = buff->page_num; //A15:8 while accessing page + uint8_t bank; //First need to initialize mapper register bits //Perhaps this only needs to be done on first buffer though..? @@ -339,6 +357,13 @@ uint8_t flash_buff( buffer *buff ) { //also use to get read function pointer switch ( buff->mem_type ) { case PRGROM: //$8000 + if (buff->mapper == BxROM) { + //write bank value + //page_num shift by 7 bits A15 >> A8(0) + bank = buff->page_num >> 7; + //Lizard banktable location + nes_cpu_wr( (0xFF94+bank), bank ); + } write_page( 0, (0x80 | addrH), buff, discrete_exp0_prgrom_wr, nes_cpu_rd ); break; case CHRROM: //$0000 diff --git a/host/scripts/inlretro.lua b/host/scripts/inlretro.lua index 9d22f95..82ceec1 100644 --- a/host/scripts/inlretro.lua +++ b/host/scripts/inlretro.lua @@ -21,8 +21,8 @@ function main () --cart/mapper specific scripts - local curcart = require "scripts.nes.nrom" - --local curcart = require "scripts.nes.bnrom" + --local curcart = require "scripts.nes.nrom" + local curcart = require "scripts.nes.bnrom" local rv -- rv = dict.pinport( "DATA_SET", 0xAA ) @@ -131,7 +131,9 @@ function main () --set rom types and sizes --perform desired operation --CART and programmer should be in a RESET condition upon calling the specific script - curcart.process( true, true, true, true, "ignore/dump.bin", "ignore/ddug2.bin", "ignore/verifyout.bin") + --curcart.process( true, true, true, true, "ignore/dump.bin", "ignore/ddug2.bin", "ignore/verifyout.bin") + curcart.process( false, true, true, true, "ignore/dump.bin", "ignore/lizard_v1.bin", "ignore/verifyout.bin") + --curcart.process( true, false, false, false, "ignore/dump.bin", "ignore/lizard_v1.bin", "ignore/verifyout.bin") --[[ diff --git a/host/scripts/nes/bnrom.lua b/host/scripts/nes/bnrom.lua index 55042c6..192227c 100644 --- a/host/scripts/nes/bnrom.lua +++ b/host/scripts/nes/bnrom.lua @@ -10,7 +10,59 @@ local flash = require "scripts.app.flash" -- file constants -- local functions +local function wr_flash_byte(addr, value, debug) + dict.nes("DISCRETE_EXP0_PRGROM_WR", 0x5555, 0xAA) + dict.nes("DISCRETE_EXP0_PRGROM_WR", 0x2AAA, 0x55) + dict.nes("DISCRETE_EXP0_PRGROM_WR", 0x5555, 0xA0) + dict.nes("DISCRETE_EXP0_PRGROM_WR", addr, value) + + local rv = dict.nes("NES_CPU_RD", addr) + + local i = 0 + + while ( rv ~= value ) do + rv = dict.nes("NES_CPU_RD", addr) + i = i + 1 + end + if debug then print(i, "naks, done writing byte.") end +end + +--base is the actual NES CPU address, not the rom offset (ie $FFF0, not $7FF0) +local function wr_bank_table(base, entries) + + +-- --first select the last bank as cartridge should be erased (all 0xFF) +-- --go ahead and write the value to where it's supposed to be incase rom isn't erased +-- dict.nes("NES_CPU_WR", base+entries-1, entries-1) +-- +-- --write bank table to selected bank +-- while( i < entries) do +-- wr_flash_byte(base+i, i) +-- i = i+1; +-- end +-- --now we can use that bank table to jump to any other bank + + --smarter solution is to simply count down so we can use just one loop + + local cur_bank = entries - 1 --16 minus 1 is 15 = 0x0F + + while( cur_bank >= 0 ) do + --select bank to write to (last bank first) + --use the bank table to make the switch + dict.nes("NES_CPU_WR", base+cur_bank, cur_bank) + + --write bank table to selected bank + local i = 0 + while( i < entries) do + wr_flash_byte(base+i, i) + i = i+1; + end + + cur_bank = cur_bank-1 + end + +end --Cart should be in reset state upon calling this function --this function processes all user requests for this specific board/mapper @@ -28,9 +80,9 @@ local function process( read, erase, program, verify, dumpfile, flashfile, verif if read then file = assert(io.open(dumpfile, "wb")) + --TODO find bank table to avoid bus conflicts! --dump cart into file - dump.dumptofile( file, 32, "BNROM", "PRGROM", true ) - -- dump.dumptofile( file, 512, "BNROM", "PRGROM", true ) + dump.dumptofile( file, 512, "BxROM", "PRGROM", true ) --close file assert(file:close()) @@ -40,7 +92,7 @@ local function process( read, erase, program, verify, dumpfile, flashfile, verif --erase the cart if erase then - print("erasing BNROM"); + print("\nerasing BxROM"); print("erasing PRG-ROM"); dict.nes("DISCRETE_EXP0_PRGROM_WR", 0x5555, 0xAA) @@ -70,8 +122,14 @@ local function process( read, erase, program, verify, dumpfile, flashfile, verif file = assert(io.open(flashfile, "rb")) --determine if auto-doubling, deinterleaving, etc, --needs done to make board compatible with rom + + --find bank table in the rom + --write bank table to all banks of cartridge + --Lizard's bank table is at $FF94 so hard code that for now + wr_bank_table(0xFF94, 16) + --flash cart - flash.write_file( file, 32, "BNROM", "PRGROM", true ) + flash.write_file( file, 512, "BxROM", "PRGROM", true ) --close file assert(file:close()) @@ -84,7 +142,7 @@ local function process( read, erase, program, verify, dumpfile, flashfile, verif file = assert(io.open(verifyfile, "wb")) --dump cart into file - dump.dumptofile( file, 32, "BNROM", "PRGROM", true ) + dump.dumptofile( file, 512, "BxROM", "PRGROM", true ) --close file assert(file:close())