diff --git a/.gitignore b/.gitignore index 487f9c3..aa88328 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ exec *cache* *.patch *.out +*dump* \ No newline at end of file diff --git a/bus.c b/bus.c index be5abf1..ac3a970 100644 --- a/bus.c +++ b/bus.c @@ -37,7 +37,11 @@ word mapper_resolve_write(word address, byte data){ //info about what regions ar void bus_write8(word address, word data){ if(!mapper000_write(address, data, false)){ //first thing we do is we hand the operation to the mapper to resolve any cartridge-side bank switching and mirroring, if the address we wanna write to isnt on the cartridge, we return false and we write to the bus normally address = mapper_resolve_write(address, data); //a lot of regions on the NES bus are mirrored/synced, this just ensures we are always writing to the parent region, not to a empty cloned one - printf("\nwrite-ram 0x%04X at 0x%04X\n; old_val 0x%04X", data, address, bus_read8(address)); + + #ifdef DEBUG + printf("\nwrite-ram 0x%04X at 0x%04X\n; old_val 0x%04X", data, address, bus_read8(address)); + #endif + bus[address] = (unsigned char)data; } } @@ -106,6 +110,7 @@ int main(int argc, char * argv[]){ exit(EXIT_FAILURE); } #endif + init_cpu(cpu); //put new CPU in starting mode and dock it to the bus cpu->PC = PROG_START_ADDR; @@ -116,28 +121,33 @@ 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 init_banks(argv[1]); - debug = true; #ifdef DEBUG //Test to see if writing to the bus is working correctly bus_write8(0x0000, 0xFF); - printf("\nCACA CACA %02X %02X\n", bus_read8(0x0000), bus[0x0000]); + printf("\n---- ---- %02X %02X\n", bus_read8(0x0000), bus[0x0000]); + + + //open debug PC logfile + FILE * PClogFILE; + PClogFILE = fopen("PClogFILE", "w"); #endif for(long iterations = 0; iterations != 3500; iterations++){ - debug_print_instruction(cpu, bus_read8(cpu->PC)); - print_registers(cpu); - print_cpu(cpu); - dump_bus(); + + #ifdef DEBUG + fprintf(PClogFILE, "%4X\n", cpu->PC); + debug_print_instruction(cpu, bus_read8(cpu->PC)); + print_registers(cpu); + print_cpu(cpu); + fprintf(stderr, "\n\n----\n%i\n-----", iterations); + #endif + + //RUN THE CPU CLOCK ONE TIME cpu_clock(cpu); - fprintf(stderr, "\n\n----\n%i\n-----", iterations); } - - debug = false; - printf("\n~~~~~~~~~~~~~~~~~~~~\n"); - print_cpu(cpu); - printf("\nREGISTERS:\n\n"); - print_registers(cpu); - dump_bus(); + #ifdef DEBUG + dump_bus(); + #endif } diff --git a/cpu.c b/cpu.c index c4b2f74..9149cef 100644 --- a/cpu.c +++ b/cpu.c @@ -188,7 +188,10 @@ void PHP(CPU* cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ //0x cpu->SP--; cpu->SR.flags.Break = 0; - printStack(cpu); + + #ifdef DEBUG + printStack(cpu); + #endif } @@ -236,7 +239,10 @@ void PLA(CPU *cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ cpu->A = bus_read8(cpu->SP + STACK_RAM_OFFSET); cpu->SR.flags.Negative = cpu->A > 7; cpu->SR.flags.Zero = !cpu->A; - printStack(cpu); + + #ifdef DEBUG + printStack(cpu); + #endif } void ROL(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ @@ -350,8 +356,9 @@ void CMP(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ cpu->SR.flags.Carry = (cpu->A >= x.value); cpu->SR.flags.Zero = (cpu->A == x.value); - printf("\n\n-----\nA: %i\nM: %i\n-----\n", cpu->A, x.value); - + #ifdef DEBUG + printf("\n\n-----\nA: %i\nM: %i\n-----\n", cpu->A, x.value); + #endif } void CPX(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ @@ -444,7 +451,7 @@ void LDA(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ - #ifndef DISABLE_LDA_DEBUG + #ifdef DEBUG //0x00D01C location of error if(cpu->PC == 0x00D01C) { printf("LDA A = %X, VAL = %X, VALUE HIGH BYTE = %X\n", cpu->A, x.value, debug_read_do_not_use_pls(0x200)); @@ -487,7 +494,10 @@ void NOP(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ void PHA(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ bus_write8(cpu->SP + STACK_RAM_OFFSET, cpu->A); cpu->SP--; - printStack(cpu); + + #ifdef DEBUG + printStack(cpu); + #endif } void RTI(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ @@ -506,12 +516,18 @@ void RTI(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ void RTS(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ cpu->SP++; word newPC = bus_read8(cpu->SP + STACK_RAM_OFFSET); - printf("\n%02X\n", newPC); + + #ifdef DEBUG + printf("\n%02X\n", newPC); + #endif + cpu->SP++; newPC |= bus_read8(cpu->SP + STACK_RAM_OFFSET) << 8; - printf("\n%04X\n", newPC); - printStack(cpu); + #ifdef DEBUG + printf("\n%04X\n", newPC); + printStack(cpu); + #endif cpu->PC = newPC; } @@ -1818,7 +1834,11 @@ void init_cpu(CPU * __restrict__ cpu){ } void cpu_clock(CPU * cpu){ - handle_errors(cpu); + + #ifdef DEBUG + handle_errors(cpu); + #endif + cpu->pcNeedsInc = true; word args = 0; if(cpu->opcodes[bus_read8(cpu->PC)].bytes > 2){ //if args is 16bit shifts the first byte to the hi byte (they get reversed basically) @@ -1826,7 +1846,9 @@ void cpu_clock(CPU * cpu){ } args |= bus_read8(cpu->PC + 1); //add first arg byte - printf("\nBYTES: 0x%04X", args); + #ifdef DEBUG + printf("\nBYTES: 0x%04X", args); + #endif byte sizeOfInstruction = cpu->opcodes[bus_read8(cpu->PC)].bytes; @@ -1834,5 +1856,13 @@ void cpu_clock(CPU * cpu){ cpu->opcodes[bus_read8(cpu->PC)].microcode(cpu, args, cpu->opcodes[bus_read8(cpu->PC)].mode); //execute opcode function - if(cpu->pcNeedsInc) {cpu->PC += sizeOfInstruction; printf("\n%04X %04X\n", cpu->PC - sizeOfInstruction, cpu->PC);} //increase program counter + //EXECUTION DONE + + if(cpu->pcNeedsInc){ + cpu->PC += sizeOfInstruction; + + #ifdef DEBUG + printf("\n%04X %04X\n", cpu->PC - sizeOfInstruction, cpu->PC); + #endif + } //increase program counter }