cpu - Add run method to run a specific number of mcycles

This commit is contained in:
madmaurice 2023-09-18 23:32:58 +02:00
parent 431e7bb9a7
commit d0a4b5a217
3 changed files with 21 additions and 16 deletions

View file

@ -60,17 +60,23 @@ Cpu::Cpu(Mem_device* bus)
reset(); 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 no isr has been called, decode an instruction
{ if (state.halted) return 4;
if (state.halted)
processed_mcycles += 4; return executeInstruction();
else
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() void Cpu::reset()
@ -281,8 +287,6 @@ bool Cpu::handleInterrupts()
state.IF &= ~it; // clear interrupt in IF state.IF &= ~it; // clear interrupt in IF
doCall(isr); // Call interrupt service routine doCall(isr); // Call interrupt service routine
processed_mcycles += 5;
return true; return true;
} }

View file

@ -163,7 +163,7 @@ private:
bool decodeCond(u8 cc); bool decodeCond(u8 cc);
bool handleInterrupts(); bool handleInterrupts();
void executeInstruction(); unsigned long executeInstruction();
void reset(); void reset();
@ -172,12 +172,13 @@ public:
Cpu_state state; Cpu_state state;
Mem_device* bus; Mem_device* bus;
unsigned long processed_mcycles;
u16 last_instr_addr; u16 last_instr_addr;
void signalInterrupt(InterruptType it); void signalInterrupt(InterruptType it);
void step(); unsigned int step();
unsigned long run(unsigned long mcycles);
}; };
class CpuException : public EmulatorException { class CpuException : public EmulatorException {

View file

@ -5,7 +5,7 @@ static inline u16 make_u16(u8 msb, u8 lsb)
return (((u16)msb << 8) | (u16)lsb); return (((u16)msb << 8) | (u16)lsb);
} }
void Cpu::executeInstruction() unsigned long Cpu::executeInstruction()
{ {
last_instr_addr = state.PC; last_instr_addr = state.PC;
opcode op{ readPC8() }; opcode op{ readPC8() };
@ -458,5 +458,5 @@ void Cpu::executeInstruction()
} }
} }
processed_mcycles += mcycles; return mcycles;
} }