Fix reading/writing 4MB HiROM

HiROM starts at bank 192 and has 64 banks so byte variable currBank overflows.
This commit is contained in:
sanni 2022-10-31 15:41:29 +01:00
parent a3feb32e15
commit cc17c70e30
6 changed files with 258 additions and 245 deletions

View File

@ -109,7 +109,7 @@ char ver[5] = "11.1";
#define enable_PCE
// Benesse Pocket Challenge W
//#define enable_PCW
#define enable_PCW
// Sega Master System
#define enable_SMS
@ -127,10 +127,10 @@ char ver[5] = "11.1";
#define enable_VBOY
// WonderSwan
//#define enable_WS
#define enable_WS
// Watara Supervision
//#define enable_WSV
#define enable_WSV
//******************************************
// HW CONFIGS
@ -298,9 +298,9 @@ bool i2c_found;
#include "FreqCount.h"
#endif
void _print_FatalError(void) __attribute__ ((noreturn));
void print_FatalError(const __FlashStringHelper* errorMessage) __attribute__ ((noreturn));
void print_FatalError(byte errorMessage) __attribute__ ((noreturn));
void _print_FatalError(void) __attribute__((noreturn));
void print_FatalError(const __FlashStringHelper* errorMessage) __attribute__((noreturn));
void print_FatalError(byte errorMessage) __attribute__((noreturn));
/******************************************
Common Strings
@ -495,7 +495,7 @@ byte sdBuffer[512];
// soft reset Arduino: jumps to 0
// using the watchdog timer would be more elegant but some Mega2560 bootloaders are buggy with it
void (*resetArduino)(void) __attribute__ ((noreturn)) = 0;
void (*resetArduino)(void) __attribute__((noreturn)) = 0;
// Progressbar
void draw_progressbar(uint32_t processedsize, uint32_t totalsize);
@ -1983,7 +1983,7 @@ void print_FatalError(const __FlashStringHelper* errorMessage) {
_print_FatalError();
}
void print_FatalError(byte errorMessage){
void print_FatalError(byte errorMessage) {
print_Error(errorMessage);
_print_FatalError();
}

View File

@ -565,7 +565,7 @@ void writeByte_GBA(unsigned long myAddress, byte myData) {
*****************************************/
// Compute the checksum of rom header
// "header" must contain at least the rom's first 188 bytes
byte checksumHeader_GBA(const byte *header) {
byte checksumHeader_GBA(const byte* header) {
byte result = 0x00;
for (byte n = 0xA0; n < 0xBD; n++) {
result -= header[n];

View File

@ -586,7 +586,7 @@ void writeWord_N64(word myWord) {
*****************************************/
static word addrCRC(word address) {
const char n64_address_crc_table[] = { 0x15, 0x1F, 0x0B, 0x16, 0x19, 0x07, 0x0E, 0x1C, 0x0D, 0x1A, 0x01 };
const char *cur_xor = n64_address_crc_table;
const char* cur_xor = n64_address_crc_table;
byte crc = 0;
for (word mask = 0x0020; mask; mask <<= 1, cur_xor++) {
if (address & mask) {
@ -618,23 +618,23 @@ static uint8_t dataCRC(uint8_t* data) {
// (3 instructions) making it the same size as the equivalent 3-cycles NOP
// delay. For shorter delays or non-multiple-of-3-cycle delays, add your own
// NOPs.
#define N64_DELAY_LOOP(cycle_count) do { \
#define N64_DELAY_LOOP(cycle_count) \
do { \
byte i; \
__asm__ __volatile__ ("\n" \
__asm__ __volatile__("\n" \
"\tldi %[i], %[loop_count]\n" \
".delay_loop_%=:\n" \
"\tdec %[i]\n" \
"\tbrne .delay_loop_%=\n" \
: [i] "=r" (i) \
: [loop_count] "i" (cycle_count / 3) \
: "cc" \
); \
} while(0)
: [i] "=r"(i) \
: [loop_count] "i"(cycle_count / 3) \
: "cc"); \
} while (0)
/******************************************
N64 Controller Protocol Functions
*****************************************/
void sendJoyBus(const byte *buffer, char length) {
void sendJoyBus(const byte* buffer, char length) {
// Implemented in assembly as there is very little wiggle room, timing-wise.
// Overall structure:
// outer_loop:
@ -789,19 +789,18 @@ void sendJoyBus(const byte *buffer, char length) {
// when expanding "%[buffer]", causing the assembler to reject the
// instruction. Pick Z as it is the only call-used such register,
// avoiding the need to preserve any value a caller may have set it to.
: [buffer] "+z" (buffer),
[length] "+r" (length),
[cur_byte] "=&r" (cur_byte),
[mask] "=&a" (mask),
[scratch] "=&a" (scratch)
: [line_low] "r" (line_low),
[line_high] "r" (line_high),
[out_byte] "i" (&DDRH)
: "cc", "memory"
);
: [buffer] "+z"(buffer),
[length] "+r"(length),
[cur_byte] "=&r"(cur_byte),
[mask] "=&a"(mask),
[scratch] "=&a"(scratch)
: [line_low] "r"(line_low),
[line_high] "r"(line_high),
[out_byte] "i"(&DDRH)
: "cc", "memory");
}
word recvJoyBus(byte *output, byte byte_count) {
word recvJoyBus(byte* output, byte byte_count) {
// listen for expected byte_count bytes of data back from the controller
// return the number of bytes not (fully) received if the delay for a signal
// edge takes too long.
@ -897,16 +896,15 @@ word recvJoyBus(byte *output, byte byte_count) {
"\tbrne .read_wait_input_high_%=\n" // timeout==0: 1, timeout!=0: 2
"\trjmp .read_end_%=\n" // 2
".read_end_%=:\n"
: [output] "+z" (output),
[byte_count] "+r" (byte_count),
[mask] "=&a" (mask),
[cur_byte] "=&r" (cur_byte),
[timeout] "=&a" (timeout),
[scratch] "=&a" (scratch)
: [in_byte] "i" (&PINH),
[in_bit] "i" (4)
: "cc", "memory"
);
: [output] "+z"(output),
[byte_count] "+r"(byte_count),
[mask] "=&a"(mask),
[cur_byte] "=&r"(cur_byte),
[timeout] "=&a"(timeout),
[scratch] "=&a"(scratch)
: [in_byte] "i"(&PINH),
[in_bit] "i"(4)
: "cc", "memory");
return byte_count;
}
@ -1528,11 +1526,11 @@ void checkController() {
}
// read 32bytes from controller pak and calculate CRC
byte readBlock(byte *output, word myAddress) {
byte readBlock(byte* output, word myAddress) {
byte response_crc;
// Calculate the address CRC
word myAddressCRC = addrCRC(myAddress);
const byte command[] = { 0x02, (byte) (myAddressCRC >> 8), (byte) (myAddressCRC & 0xff) };
const byte command[] = { 0x02, (byte)(myAddressCRC >> 8), (byte)(myAddressCRC & 0xff) };
word error;
// don't want interrupts getting in the way
@ -1798,8 +1796,8 @@ void writeMPK() {
myFile.read(command + 3, sizeof(command) - 3);
word address_with_crc = addrCRC(address);
command[1] = (byte) (address_with_crc >> 8);
command[2] = (byte) (address_with_crc & 0xff);
command[1] = (byte)(address_with_crc >> 8);
command[2] = (byte)(address_with_crc & 0xff);
// don't want interrupts getting in the way
noInterrupts();
@ -2211,7 +2209,7 @@ void writeEeprom() {
}
}
void readEepromPageList(byte *output, byte page_number, byte page_count) {
void readEepromPageList(byte* output, byte page_number, byte page_count) {
byte command[] = { 0x04, page_number };
// Disable interrupts for more uniform clock pulses

View File

@ -1068,7 +1068,7 @@ void CreateROMFolderInSD() {
sd.chdir(folder);
}
FsFile createNewFile(const char *prefix, const char *extension) {
FsFile createNewFile(const char* prefix, const char* extension) {
char filename[FILENAME_LENGTH];
snprintf_P(filename, sizeof(filename), _file_name_no_number_fmt, prefix, extension);
for (byte i = 0; i < 100; i++) {

View File

@ -893,7 +893,7 @@ void readROM_SFM() {
//clear the screen
display_Clear();
println_Msg(F("Creating folder: "));
print_Msg(F("Creating folder "));
println_Msg(folder);
display_Update();
@ -927,7 +927,7 @@ void readROM_SFM() {
println_Msg(F("Dumping HiRom..."));
display_Update();
for (byte currBank = 192; currBank < (numBanks + 192); currBank++) {
for (word currBank = 192; currBank < (numBanks + 192); currBank++) {
for (long currByte = 0; currByte < 65536; currByte += 512) {
for (int c = 0; c < 512; c++) {
sdBuffer[c] = readBank_SFM(currBank, currByte + c);
@ -1009,8 +1009,7 @@ void idFlash_SFM(int startBank) {
void writeFlash_SFM(int startBank, uint32_t pos) {
display_Clear();
print_Msg(F("Writing Bank 0x"));
print_Msg(startBank, HEX);
print_Msg(F("..."));
println_Msg(startBank, HEX);
display_Update();
// Open file on sd card
@ -1026,8 +1025,13 @@ void writeFlash_SFM(int startBank, uint32_t pos) {
dataOut();
if (romType) {
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = numBanks * 0x10000;
draw_progressbar(0, totalProgressBar);
// Write hirom
for (byte currBank = startBank; currBank < startBank + numBanks; currBank++) {
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
// Fill SDBuffer with 1 page at a time then write it repeat until all bytes are written
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 128) {
myFile.read(sdBuffer, 128);
@ -1049,8 +1053,16 @@ void writeFlash_SFM(int startBank, uint32_t pos) {
// Wait until write is finished
busyCheck_SFM(startBank);
}
// update progress bar
processedProgressBar += 0x10000;
draw_progressbar(processedProgressBar, totalProgressBar);
}
} else {
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = numBanks * 0x8000;
draw_progressbar(0, totalProgressBar);
// Write lorom
for (byte currBank = 0; currBank < numBanks; currBank++) {
for (unsigned long currByte = 0x8000; currByte < 0x10000; currByte += 128) {
@ -1072,6 +1084,9 @@ void writeFlash_SFM(int startBank, uint32_t pos) {
// Wait until write is finished
busyCheck_SFM(startBank);
}
// update progress bar
processedProgressBar += 0x8000;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Close the file:
@ -1155,7 +1170,7 @@ byte blankcheck_SFM(int startBank) {
byte blank = 1;
if (romType) {
for (byte currBank = startBank; currBank < startBank + numBanks; currBank++) {
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
for (unsigned long currByte = 0; currByte < 0x10000; currByte++) {
if (readBank_SFM(currBank, currByte) != 0xFF) {
currBank = startBank + numBanks;
@ -1192,7 +1207,7 @@ unsigned long verifyFlash_SFM(int startBank, uint32_t pos) {
controlIn_SFM();
if (romType) {
for (byte currBank = startBank; currBank < startBank + numBanks; currBank++) {
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 512) {
// Fill SDBuffer
myFile.read(sdBuffer, 512);
@ -1244,7 +1259,7 @@ void readFlash_SFM() {
print_FatalError(create_file_STR);
}
if (romType) {
for (byte currBank = 0xC0; currBank < 0xC0 + numBanks; currBank++) {
for (word currBank = 0xC0; currBank < 0xC0 + numBanks; currBank++) {
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 512) {
for (int c = 0; c < 512; c++) {
sdBuffer[c] = readBank_SFM(currBank, currByte + c);

View File

@ -602,7 +602,7 @@ void readLoRomBanks(unsigned int start, unsigned int total, FsFile* file) {
uint32_t totalProgressBar = (uint32_t)(total - start) * 1024;
draw_progressbar(0, totalProgressBar);
for (byte currBank = start; currBank < total; currBank++) {
for (word currBank = start; currBank < total; currBank++) {
PORTL = currBank;
// Blink led
@ -653,7 +653,7 @@ void readHiRomBanks(unsigned int start, unsigned int total, FsFile* file) {
uint32_t totalProgressBar = (uint32_t)(total - start) * 1024;
draw_progressbar(0, totalProgressBar);
for (byte currBank = start; currBank < total; currBank++) {
for (word currBank = start; currBank < total; currBank++) {
PORTL = currBank;
// Blink led