insert method Null safety

void insert(
  1. ProgramInstruction<OT> instruction
)

Insert a new instruction, and increment currentLine to refer to it.

Implementation

void insert(final ProgramInstruction<OT> instruction) {
  final needed = instruction.isExtended ? 2 : 1;
  if (bytesToNextAllocation < needed) {
    memory.policy.checkExtendProgramMemory();
  }
  assert(_currentLine >= 0 && _currentLine <= _lines);
  final storage = memory.storage;
  _setCachedAddress(_currentLine + 1);
  int addr = _cachedAddress; // stored as nybbles
  for (int a = _maxAddress; a >= addr; a--) {
    storage.setUint8(a + 2 * needed, storage.getUint8(a));
  }
  final int opcode = instruction.opcode;
  if (opcode >= 0x100) {
    assert(needed == 2);
    final int pageCode = (opcode >> 8) - 1 + _extendedOpcode;
    assert(pageCode >= _extendedOpcode && pageCode < 0x100);
    storage.setUint8(addr++, pageCode >> 4);
    storage.setUint8(addr++, pageCode & 0xf);
    _extendedLines++;
  } else {
    assert(needed == 1);
  }
  storage.setUint8(addr++, (opcode >> 4) & 0xf);
  storage.setUint8(addr++, opcode & 0xf);
  _lines++;
  _currentLine++; // Where we just inserted the instruction

  memory.model.needsSave = true;
}