int runCPUCycle(){ //Both used in DAA char correction; char beforeA; unsigned short beforeHL; char temp; opcodeInstruction instruction; unsigned char currentInterrupts; if(delayCyclesLeft!=0){ delayCyclesLeft--; return 0; } //Check for Interrupts if(IME == 1){ currentInterrupts = (*interruptFlags)&interruptER; if(currentInterrupts&INT_VBLANK){ writeSP(getSP()-2); writeShortToMem(getSP(),getPC()); writePC(0x40); IME = 0; *interruptFlags = (*interruptFlags)& ~(INT_VBLANK); } else if(currentInterrupts&INT_LCD){ writeSP(getSP()-2); writeShortToMem(getSP(),getPC()); writePC(0x48); IME = 0; *interruptFlags = (*interruptFlags)& ~(INT_LCD); } else if(currentInterrupts&INT_TIMER){ writeSP(getSP()-2); writeShortToMem(getSP(),getPC()); writePC(0x50); IME = 0; *interruptFlags = (*interruptFlags)& ~(INT_TIMER); } else if(currentInterrupts&INT_SERIAL){ writeSP(getSP()-2); writeShortToMem(getSP(),getPC()); writePC(0x58); IME = 0; *interruptFlags = (*interruptFlags)& ~(INT_SERIAL); } else if(currentInterrupts&INT_JOYPAD){ writeSP(getSP()-2); writeShortToMem(getSP(),getPC()); writePC(0x60); IME = 0; *interruptFlags = (*interruptFlags)& ~(INT_JOYPAD); } } delayCyclesLeft = instructionClockCycles[readCharFromMem(getPC())]; //Will be overwritten is instruction is CB if(delayCyclesLeft == -1){ delayCyclesLeft = 20; //My not need any actual implementation } //Add the instruction to the list of called instructions PCRecallAdd(PCRecallHead,getPC()); //Get the instruction from the jump table if((readCharFromMem(getPC())&0xFF)==0xCB){ instruction = opcodes[readCharFromMem(getPC()+1)+0x100]; writePC(getPC()+1); incrementInstructionCount(readCharFromMem(getPC())+0x100); delayCyclesLeft = CBInstructionClockCycles[readCharFromMem(getPC())]; //printf("Using CB instruction at PC: %hX\n",getPC()); } else { if((readCharFromMem(getPC())&0xFF)==0x10){ return 0x10; } //printf("Instruction Index: %hhX PC: %hX\n",readCharFromMem(getPC()),getPC()); incrementInstructionCount(readCharFromMem(getPC())); instruction = opcodes[readCharFromMem(getPC())]; } //Call the instruction if(instruction!=0){ instruction(); } else { printf("Unknown Instruction: %hhX at PC: %hhX\n",readCharFromMem(getPC()),getPC()); return 0x10; //Stop writePC(getPC()+1); } return 0; }
int main(int argc, char * argv[]){ WINDOW * win; initscr(); clear(); noecho(); cbreak(); curs_set(0); m = newMachine(); if(argc == 2){ loadMachine(m,argv[1]); } win = newwin(WINDOW_SIZE_HEIGHT,WINDOW_SIZE_WIDTH,2,2); keypad(win,TRUE); refresh(); createDefaultView(win); updateView(win); char read; int c = 0; while(c!='q'){ c = wgetch(win); switch(c){ // all these if statements are just edge cases where we wrap around // the table case KEY_UP: if(section == SECTION_MEMORY){ if(highlight[section] < 16) highlight[section] = 255 - (15 - highlight[section]); else highlight[section]-=16; } break; case KEY_DOWN: if(section == SECTION_MEMORY){ if(highlight[section] < 256 && highlight[section] >= 240) highlight[section] = highlight[section] - 16*15; else highlight[section]+=16; } break; case KEY_LEFT: if(section == SECTION_MEMORY){ if(highlight[section]%16==0) highlight[section] += 15; else highlight[section]--; }else if(section == SECTION_REGISTERS){ if(highlight[section] == 0) highlight[section] = 15; else highlight[section]--; } break; case KEY_RIGHT: if(section == SECTION_MEMORY){ if((highlight[section]-15) % 16 ==0 ) highlight[section] -= 15; else highlight[section]++; }else if(section == SECTION_REGISTERS){ if(highlight[section] == 15) highlight[section] = 0; else highlight[section]++; } break; // tab key case 9: // toggle section section++; section = section%3; break; case 'r': run(m); break; case 's': saveMachine(m); break; case 'c': resetMachine(m); break; // enter key case 10: read = readInput(win); if(section == SECTION_MEMORY) writeMemoryAt(m, highlight[section], read); else if(section == SECTION_REGISTERS) writeRegistersAt(m, highlight[section], read); else writePC(m, read); break; case 'n': step(m); break; default: refresh(); break; } updateView(win); } clrtoeol(); refresh(); endwin(); return 0; }