diff --git a/cpu/cpu.cpp b/cpu/cpu.cpp index fd2fcd2..fa8345f 100644 --- a/cpu/cpu.cpp +++ b/cpu/cpu.cpp @@ -60,17 +60,23 @@ Cpu::Cpu(Mem_device* bus) reset(); } -void Cpu::step() +unsigned int Cpu::step() { - if(state.stopped) return; + if(state.stopped) return 4; + if(handleInterrupts()) return 5; - if(!handleInterrupts()) // if no isr has been called, decode an instruction - { - if (state.halted) - processed_mcycles += 4; - else - executeInstruction(); - } + // if no isr has been called, decode an instruction + if (state.halted) return 4; + + return executeInstruction(); +} + +unsigned long Cpu::run(unsigned long mcycles) +{ + unsigned long processed_mcycles = 0; + while(processed_mcycles < mcycles) + processed_mcycles += step(); + return processed_mcycles; } void Cpu::reset() @@ -281,8 +287,6 @@ bool Cpu::handleInterrupts() state.IF &= ~it; // clear interrupt in IF doCall(isr); // Call interrupt service routine - processed_mcycles += 5; - return true; } diff --git a/cpu/cpu.h b/cpu/cpu.h index 02e9f06..dd50804 100644 --- a/cpu/cpu.h +++ b/cpu/cpu.h @@ -163,7 +163,7 @@ private: bool decodeCond(u8 cc); bool handleInterrupts(); - void executeInstruction(); + unsigned long executeInstruction(); void reset(); @@ -172,12 +172,13 @@ public: Cpu_state state; Mem_device* bus; - unsigned long processed_mcycles; u16 last_instr_addr; void signalInterrupt(InterruptType it); - void step(); + unsigned int step(); + + unsigned long run(unsigned long mcycles); }; class CpuException : public EmulatorException { diff --git a/cpu/decoder.cpp b/cpu/decoder.cpp index a68cf39..d46f936 100644 --- a/cpu/decoder.cpp +++ b/cpu/decoder.cpp @@ -5,7 +5,7 @@ static inline u16 make_u16(u8 msb, u8 lsb) return (((u16)msb << 8) | (u16)lsb); } -void Cpu::executeInstruction() +unsigned long Cpu::executeInstruction() { last_instr_addr = state.PC; opcode op{ readPC8() }; @@ -458,5 +458,5 @@ void Cpu::executeInstruction() } } - processed_mcycles += mcycles; + return mcycles; }