diff --git a/Core/Cpu/Z80.cs b/Core/Cpu/Z80.cs index ca3a57d..ad7be26 100644 --- a/Core/Cpu/Z80.cs +++ b/Core/Cpu/Z80.cs @@ -322,6 +322,9 @@ namespace Core.Cpu case 0x04: // INC B BC.High = Inc8(BC.High); return 4; + case 0x0E: // LD C, n + BC.Low = FetchByte(); + return 7; // Takes 7 T-States case 0x10: // DJNZ d sbyte djnzOffset = (sbyte)FetchByte(); @@ -606,7 +609,7 @@ namespace Core.Cpu targetAddress = (ushort)(IY.Word + offset75); // Write the low byte of HL to memory _memory.Write(targetAddress, HL.Low); - return 19; + return 19; case 0xCB: // The FD CB nested prefix { sbyte offsetCB = (sbyte)FetchByte(); // This is the '01' @@ -615,6 +618,35 @@ namespace Core.Cpu switch (bitOpcode) { + case 0x4E: // BIT 1, (IY+d) + { + byte memValBit = _memory.Read(targetAddress); + + // Check if bit 1 is 0 + bool bitIsZero = (memValBit & 0x02) == 0; + + // Preserve the Carry flag (Bit 0), clear everything else + AF.Low &= 0x01; + + // Set Half-Carry (Bit 4) - Standard Z80 behavior for BIT + AF.Low |= 0x10; + + if (bitIsZero) + { + AF.Low |= 0x40; // Set Zero Flag (Bit 6) + AF.Low |= 0x04; // Set P/V Flag (Bit 2) + } + + return 20; // Takes 20 T-States + } + case 0x8E: // RES 1, (IY+d) + byte memValRes = _memory.Read(targetAddress); + + // 0xFD is Binary 1111 1101. + // ANDing with this preserves all bits except Bit 1, which becomes 0. + memValRes &= 0xFD; + _memory.Write(targetAddress, memValRes); + return 23; case 0xCE: // SET 1, (IY+d) memVal = _memory.Read(targetAddress); memVal |= 0x02; // 0x02 is Binary 0000 0010 (Bit 1) diff --git a/Desktop/DebuggerForm.cs b/Desktop/DebuggerForm.cs index 2377c39..55a9a82 100644 --- a/Desktop/DebuggerForm.cs +++ b/Desktop/DebuggerForm.cs @@ -235,6 +235,11 @@ namespace Desktop case 0x04: mnemonic = "INC B"; break; + case 0x0E: + byte cImm = _memoryBus.Read((ushort)(currentPc + 1)); + mnemonic = $"LD C, 0x{cImm:X2}"; + instructionLength = 2; + break; case 0x10: sbyte djnzOffset = (sbyte)_memoryBus.Read((ushort)(currentPc + 1)); ushort djnzDest = (ushort)(currentPc + 2 + djnzOffset); @@ -435,8 +440,11 @@ namespace Desktop sbyte d = (sbyte)_memoryBus.Read((ushort)(currentPc + 2)); byte cbOpcode = _memoryBus.Read((ushort)(currentPc + 3)); string sign = d >= 0 ? "+" : ""; - - if (cbOpcode == 0xCE) + if (cbOpcode == 0x4E) + { + mnemonic = $"BIT 1, (IY{sign}{d})"; + } + else if (cbOpcode == 0xCE) { mnemonic = $"SET 1, (IY{sign}{d})"; } @@ -453,6 +461,26 @@ namespace Desktop mnemonic = $"LD (IY{sign}{d}), L"; instructionLength = 3; } + else if (fdOpcode == 0xCB) // FD CB prefix + { + sbyte d = (sbyte)_memoryBus.Read((ushort)(currentPc + 2)); + byte cbOpcode = _memoryBus.Read((ushort)(currentPc + 3)); + string sign = d >= 0 ? "+" : ""; + + if (cbOpcode == 0xCE) + { + mnemonic = $"SET 1, (IY{sign}{d})"; + } + else if (cbOpcode == 0x8E) + { + mnemonic = $"RES 1, (IY{sign}{d})"; + } + else + { + mnemonic = $"FD CB {d:X2} {cbOpcode:X2}"; // Fallback + } + instructionLength = 4; + } else { mnemonic = $"FD PREFIX UNKNOWN (0x{fdOpcode:X2})";