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'
main
RenDev 3 years ago
parent 3ecac1636f
commit f14bfa787f
  1. 14
      bus.c
  2. 9
      cartridge.c
  3. 1
      cartridge.h
  4. 3
      cpu.c

14
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

@ -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[]){
@ -50,6 +55,10 @@ void initBanks(char name[]){
}
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){

@ -4,6 +4,7 @@
//extern? void (*mapper_read)(word);
//void (*mapper_write)(bool);
extern word rom_start_address;
#define K_SIZE 1024

@ -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;
}

Loading…
Cancel
Save