From f14bfa787f756ce89d29111717808691b6b2bf9c Mon Sep 17 00:00:00 2001 From: RenDev <34013705+sonich2401@users.noreply.github.com> Date: Thu, 15 Dec 2022 23:24:42 -0800 Subject: [PATCH] Fixes to ROM Loading and Branching Instructions ROM loading did not remove the 5 bytes of padding when loading the rom. This caused the last 5 bytes of the rom to never be read. These last 5 bytes contained the vector to set the PC to during boot. Now that that was implemented, any rom can now load rather than a Hardcoded location for all roms. BEQ was facing some issues as well. Seems that we did not take into account negivite numbers in relation to address resolution. This has now been accounted for by typecasting the 'unsigned char' into a 'char' --- bus.c | 14 ++++++-------- cartridge.c | 11 ++++++++++- cartridge.h | 1 + cpu.c | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/bus.c b/bus.c index 3d67808..c4aa084 100644 --- a/bus.c +++ b/bus.c @@ -97,7 +97,7 @@ static inline void debug_print_instruction(CPU* __restrict__ cpu, byte opcode){ opcode, cpu->PC, cpu->opcodes[opcode].bytes, - &cpu->opcodes[opcode].microcode + cpu->opcodes[opcode].microcode ); } @@ -107,9 +107,6 @@ void activateCpuNmi(){ } - -#define PROG_START_ADDR 0xC000 - int main(int argc, char * argv[]){ #ifdef DEBUG @@ -132,7 +129,6 @@ int main(int argc, char * argv[]){ #endif initCpu(cpu); //put new CPU in starting mode and dock it to the bus - cpu->PC = PROG_START_ADDR; if(argc <= 1){ //Check to see if a rom was given fprintf(stderr, "ERR: No Rom file Specified in Arguments\n"); @@ -141,6 +137,7 @@ int main(int argc, char * argv[]){ //load the CHR and PRG banks from the .nes file (argv[1]), also loads the header for mapper construction initBanks(argv[1]); + cpu->PC = rom_start_address; #ifdef DEBUG //Test to see if writing to the bus is working correctly @@ -171,14 +168,12 @@ int main(int argc, char * argv[]){ #endif - for(long iterations = 0; iterations != 9000; iterations++){ - + while(true){ #ifdef DEBUG fprintf(PClogFILE, "%4X\n", cpu->PC); debug_print_instruction(cpu, busRead8(cpu->PC)); printRegisters(cpu); printCpu(cpu); - printf("\n\n----\n%i\n-----", iterations); #endif if(activateCpuNmiBool){ @@ -187,6 +182,9 @@ int main(int argc, char * argv[]){ } //RUN THE CPU CLOCK ONE TIME cpuClock(cpu); + #ifdef DEBUG + getchar(); + #endif } #ifdef DEBUG diff --git a/cartridge.c b/cartridge.c index 6963a7c..9f2efab 100644 --- a/cartridge.c +++ b/cartridge.c @@ -7,6 +7,8 @@ byte CHRROM[0xFFFF]; word not_handling_this = 0x100; //0xFF + 1 +word rom_start_address = 0x0; + void loadRomfileHeader(FILE * romfile){ byte verificationToken[3] = "NES"; for(byte i = 0; i < 3; i++) @@ -22,6 +24,9 @@ void loadRomfileHeader(FILE * romfile){ for(byte i = 0; i < 5; i++){ Header.flags.array[i] = getc(romfile); } + for(byte i = 0; i < 5; i++){ //Remove padding + getc(romfile); + } } void initBanks(char name[]){ @@ -48,8 +53,12 @@ void initBanks(char name[]){ for(word i = 0; i < GET_CHR_BANK_SIZE(Header.CHR_BANKS); i++){ CHRROM[i] = getc(romfile); } - + fclose(romfile); + + //Read rom start Address + #define ROM_START_VECTOR_ADDR (0xFFFC) + rom_start_address = busRead16(ROM_START_VECTOR_ADDR); } word mapper000_Read(word address, bool ppu){ diff --git a/cartridge.h b/cartridge.h index 47be3fc..5478ae4 100644 --- a/cartridge.h +++ b/cartridge.h @@ -4,6 +4,7 @@ //extern? void (*mapper_read)(word); //void (*mapper_write)(bool); +extern word rom_start_address; #define K_SIZE 1024 diff --git a/cpu.c b/cpu.c index fd41d21..8a42f1f 100644 --- a/cpu.c +++ b/cpu.c @@ -65,7 +65,8 @@ busTransaction ZPGY(CPU * __restrict__ cpu, word bytes){ busTransaction REL(CPU * __restrict__ cpu, word bytes){ busTransaction x; - x.address = cpu->PC + (byte)bytes; + byte offset = (byte)bytes; + x.address = cpu->PC + *(char*)&offset; //typecast offset to convert it into a signed number x.value = busRead8(x.address); return x; }