[debug] change all output to only show in DEBUG mode

main
KoNicks 4 years ago
parent 35b687b916
commit 5bcf79325a
No known key found for this signature in database
GPG Key ID: 6D8132757F8B93E5
  1. 1
      .gitignore
  2. 40
      bus.c
  3. 54
      cpu.c

1
.gitignore vendored

@ -4,3 +4,4 @@ exec
*cache* *cache*
*.patch *.patch
*.out *.out
*dump*

40
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){ 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 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 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; bus[address] = (unsigned char)data;
} }
} }
@ -106,6 +110,7 @@ int main(int argc, char * argv[]){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#endif #endif
init_cpu(cpu); //put new CPU in starting mode and dock it to the bus init_cpu(cpu); //put new CPU in starting mode and dock it to the bus
cpu->PC = PROG_START_ADDR; 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 //load the CHR and PRG banks from the .nes file (argv[1]), also loads the header for mapper construction
init_banks(argv[1]); init_banks(argv[1]);
debug = true;
#ifdef DEBUG #ifdef DEBUG
//Test to see if writing to the bus is working correctly //Test to see if writing to the bus is working correctly
bus_write8(0x0000, 0xFF); 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 #endif
for(long iterations = 0; iterations != 3500; iterations++){ for(long iterations = 0; iterations != 3500; iterations++){
debug_print_instruction(cpu, bus_read8(cpu->PC));
print_registers(cpu); #ifdef DEBUG
print_cpu(cpu); fprintf(PClogFILE, "%4X\n", cpu->PC);
dump_bus(); 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); cpu_clock(cpu);
fprintf(stderr, "\n\n----\n%i\n-----", iterations);
} }
debug = false; #ifdef DEBUG
dump_bus();
printf("\n~~~~~~~~~~~~~~~~~~~~\n"); #endif
print_cpu(cpu);
printf("\nREGISTERS:\n\n");
print_registers(cpu);
dump_bus();
} }

54
cpu.c

@ -188,7 +188,10 @@ void PHP(CPU* cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ //0x
cpu->SP--; cpu->SP--;
cpu->SR.flags.Break = 0; 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->A = bus_read8(cpu->SP + STACK_RAM_OFFSET);
cpu->SR.flags.Negative = cpu->A > 7; cpu->SR.flags.Negative = cpu->A > 7;
cpu->SR.flags.Zero = !cpu->A; cpu->SR.flags.Zero = !cpu->A;
printStack(cpu);
#ifdef DEBUG
printStack(cpu);
#endif
} }
void ROL(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ 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.Carry = (cpu->A >= x.value);
cpu->SR.flags.Zero = (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) ){ 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 //0x00D01C location of error
if(cpu->PC == 0x00D01C) { 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)); 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) ){ void PHA(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){
bus_write8(cpu->SP + STACK_RAM_OFFSET, cpu->A); bus_write8(cpu->SP + STACK_RAM_OFFSET, cpu->A);
cpu->SP--; cpu->SP--;
printStack(cpu);
#ifdef DEBUG
printStack(cpu);
#endif
} }
void RTI(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){ 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) ){ void RTS(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){
cpu->SP++; cpu->SP++;
word newPC = bus_read8(cpu->SP + STACK_RAM_OFFSET); word newPC = bus_read8(cpu->SP + STACK_RAM_OFFSET);
printf("\n%02X\n", newPC);
#ifdef DEBUG
printf("\n%02X\n", newPC);
#endif
cpu->SP++; cpu->SP++;
newPC |= bus_read8(cpu->SP + STACK_RAM_OFFSET) << 8; 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; cpu->PC = newPC;
} }
@ -1818,7 +1834,11 @@ void init_cpu(CPU * __restrict__ cpu){
} }
void cpu_clock(CPU * cpu){ void cpu_clock(CPU * cpu){
handle_errors(cpu);
#ifdef DEBUG
handle_errors(cpu);
#endif
cpu->pcNeedsInc = true; cpu->pcNeedsInc = true;
word args = 0; 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) 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 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; 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 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
} }

Loading…
Cancel
Save