[bus/cpu] Clean up code

Clean up code a little
main
Joaquin 3 years ago
parent 72cec71303
commit b9be3f383e
Signed by: puly
GPG Key ID: 9E9299CD96C65EC6
  1. 2
      bus.c
  2. 34
      cpu.c

@ -137,8 +137,8 @@ int main(int argc, char * argv[]){
while(true){
#ifdef DEBUG
fprintf(PClogFILE, "%4X\n", cpu->PC);
debug_print_instruction(cpu, busRead8(cpu->PC));
printRegisters(cpu);
printCpu(cpu);

34
cpu.c

@ -505,16 +505,6 @@ void JMP(CPU * __restrict__ cpu, word bytes, busTransaction (*addressing)(CPU *,
void LDA(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){
busTransaction x = addressing(cpu, bytes); //check line 85 for details
#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));
}
#endif
cpu->A = x.value;
cpu->SR.flags.Zero = (cpu->A == 0);
cpu->SR.flags.Negative = cpu->A >> 7;
@ -680,15 +670,13 @@ void TYA(CPU * cpu, word bytes, busTransaction (*addressing)(CPU *, word) ){
cpu->SR.flags.Negative = cpu->A >> 7;
}
void init_opcodereg(CPU * cpu){ //opcode code defined starting line 139
void initOpcodeReg(CPU * cpu){ //opcode code defined starting line 139
cpu->opcodes = (struct instruction*)malloc(sizeof(struct instruction) * 0xFF); //allow memory for opcode array in cpu_opcodereg.h
#ifdef DEBUG
if(cpu->opcodes == NULL){
fprintf(stderr, "ERR: Out of Memory\n");
exit(EXIT_FAILURE);
}
#endif
//BRK codes
@ -1909,7 +1897,7 @@ void initCpu(CPU * __restrict__ cpu){
cpu->SR.data = 0x6C;
cpu->SP = 0xFD;
init_opcodereg(cpu);//import the stuff about each microcode, stuff like bytes per instruction, cycles, adressing mode, and operation in the array, where the value in the array is the byte that triggers that action for the CPU
initOpcodeReg(cpu);//import the stuff about each microcode, stuff like bytes per instruction, cycles, adressing mode, and operation in the array, where the value in the array is the byte that triggers that action for the CPU
}
void cpuNmi(CPU * cpu){
@ -1933,9 +1921,8 @@ void cpuNmi(CPU * cpu){
void cpuClock(CPU * cpu){
#ifdef DEBUG
handleErrors(cpu);
#endif
cpu->pcNeedsInc = true;
word args = 0;
@ -1950,11 +1937,22 @@ void cpuClock(CPU * cpu){
byte sizeOfInstruction = cpu->opcodes[busRead8(cpu->PC)].bytes;
//
//
//EXECUTION HERE
//
//
byte execOPdata = busRead8(cpu->PC); //get OPCODE byte (ex 0x4C)
struct instruction execOP = cpu->opcodes[execOPdata]; //fetch OPCODE data, addressing mode and microcode (ex 0x4C -> JMP)
cpu->opcodes[busRead8(cpu->PC)].microcode(cpu, args, cpu->opcodes[busRead8(cpu->PC)].mode); //execute opcode function
execOP.microcode(cpu, args, execOP.mode); //EXECUTE OPCODE
//
//
//EXECUTION DONE
//
//
if(cpu->pcNeedsInc){
cpu->PC += sizeOfInstruction;
@ -1962,5 +1960,5 @@ void cpuClock(CPU * cpu){
#ifdef DEBUG
printf("\n%04X %04X\n", cpu->PC - sizeOfInstruction, cpu->PC);
#endif
} //increase program counter
} //increase program counter to move on to the next instruction
}

Loading…
Cancel
Save