runInstruction method

int runInstruction()

Runs a single instruction

The number of T cycles the instruction took to run, plus any time that went into handling interrupts that fired while this instruction was executing

Implementation

int runInstruction() {
  if (!_halted) {
    // If the previous instruction was a DI or an EI,
    //  we'll need to disable or enable interrupts
    //  after whatever instruction we're about to run is finished.
    var doingDelayedDi = false, doingDelayedEi = false;
    if (_doDelayedDi) {
      _doDelayedDi = false;
      doingDelayedDi = true;
    } else if (_doDelayedEi) {
      _doDelayedEi = false;
      doingDelayedEi = true;
    }
    // R is incremented at the start of every instruction cycle,
    // before the instruction actually runs.
    //
    // The high bit of R is not affected by this increment,
    // it can only be changed using the LD R, A instruction.
    _r = (_r & 0x80) | (((_r & 0x7f) + 1) & 0x7f);
    // Read the byte at the PC and run the instruction it encodes.
    final opcode = _core.memRead(_pc);
    _decodeInstruction(opcode);
    _pc = (_pc + 1) & 0xffff;
    // Actually do the delayed interrupt disable/enable if we have one.
    if (doingDelayedDi) {
      _iff1 = 0;
      _iff2 = 0;
    } else if (doingDelayedEi) {
      _iff1 = 1;
      _iff2 = 1;
    }
    // And finally clear out the cycle counter for the next instruction
    //  before returning it to the emulator core.
    final retValue = _cycleCounter;
    _cycleCounter = 0;
    return retValue;
  } else {
    // While we're halted, claim that we spent a cycle doing nothing,
    //  so that the rest of the emulator can still proceed.
    return 1;
  }
}