decodeInstruction static method

String decodeInstruction(
  1. int inst1,
  2. int inst2,
  3. int inst3,
  4. int inst4,
)

Implementation

static String decodeInstruction(int inst1, int inst2, int inst3, int inst4) {
  const unknownOpcode = '<UNKNOWN>';

  switch (inst1) {
    case 0xED: // extended instructions
      final opcode = toHex8(inst1) + toHex8(inst2);
      if (!z80Opcodes.containsKey(opcode)) {
        print('Opcode $opcode missing.');
        return unknownOpcode;
      } else {
        return replaceOperand(z80Opcodes[opcode]!, inst3, inst4);
      }
    case 0xCB: // bit instructions
      // none of these have displacement values, so don't bother to escape
      // with replaceOperand()
      final opcode = toHex8(inst1) + toHex8(inst2);

      return z80Opcodes[opcode] ?? unknownOpcode;

    case 0xDD:
    case 0xFD:
      // IX or IY instructions
      if (inst2 == 0xCB) // IX or IY bit instructions
      {
        // IX and IY bit instructions are formatted DDCB**XX,
        // where ** is the displacement and XX is the opcode that determines
        // which instruction type. We map these as DDCBXX, so we skip
        // inst3 when searching the map.
        final opcode = toHex8(inst1) + toHex8(inst2) + toHex8(inst4);
        if (!z80Opcodes.containsKey(opcode)) {
          print('Opcode $opcode missing.');
          return unknownOpcode;
        } else {
          return replaceOperand(z80Opcodes[opcode]!, inst3, 0);
        }
      }
      if (inst2 == 0x36) // LD (IX+*), *
      {
        final opcode = toHex8(inst1) + toHex8(inst2);

        if (!z80Opcodes.containsKey(opcode)) {
          print('Opcode $opcode missing.');
          return unknownOpcode;
        } else {
          // This is a unique opcode which takes two operands, so we call
          // replaceOperand twice, rather than special-casing code elsewhere.
          return replaceOperand(
              replaceOperand(z80Opcodes[opcode]!, inst3, 0), inst4, 0);
        }
      }

      // Just a regular DDxx or FDxx instruction
      final opcode = toHex8(inst1) + toHex8(inst2);

      if (!z80Opcodes.containsKey(opcode)) {
        print('Opcode $opcode missing.');
        return unknownOpcode;
      } else {
        return replaceOperand(z80Opcodes[opcode]!, inst3, inst4);
      }
    default:
      // Just a regular single byte opcode
      final opcode = toHex8(inst1);
      if (!z80Opcodes.containsKey(opcode)) {
        print('Opcode $opcode missing.');
        return unknownOpcode;
      } else {
        return replaceOperand(z80Opcodes[opcode]!, inst2, inst3);
      }
  }
}