Implemented a load more Z80 OpCodes. Added a breakpoint to the debugger

This commit is contained in:
2026-04-09 17:31:56 +01:00
parent f22da937b5
commit 0ef8b9f3eb
4 changed files with 154 additions and 16 deletions

View File

@@ -160,6 +160,37 @@ namespace Core.Cpu
if (result < 0) AF.Low |= 0x01;
}
private byte Dec8(byte value)
{
byte result = (byte)(value - 1);
// Store the existing Carry flag so we can preserve it
byte carry = (byte)(AF.Low & 0x01);
// Clear all flags
AF.Low = 0;
// Sign Flag (Bit 7)
if ((result & 0x80) != 0) AF.Low |= 0x80;
// Zero Flag (Bit 6)
if (result == 0) AF.Low |= 0x40;
// Half-Carry Flag (Bit 4) - Set if borrow from bit 4 (happens if the lower nibble was 0)
if ((value & 0x0F) == 0) AF.Low |= 0x10;
// Parity/Overflow Flag (Bit 2) - Set if the original value was 0x80 (maximum negative)
if (value == 0x80) AF.Low |= 0x04;
// Subtract Flag (Bit 1) - ALWAYS SET for decrements
AF.Low |= 0x02;
// Restore the original Carry Flag (Bit 0)
AF.Low |= carry;
return result;
}
private void Cp(byte value)
{
byte a = AF.High;
@@ -262,7 +293,17 @@ namespace Core.Cpu
return 7;
case 0x23: // INC HL
HL.Word++;
return 6;
return 6;
case 0x28: // JR Z, e
offset = (sbyte)FetchByte();
// Check if the Zero Flag (Bit 6) IS set
if ((AF.Low & 0x40) != 0)
{
PC = (ushort)(PC + offset);
return 12; // Jump taken
}
return 7; // Jump not taken
case 0x2B: // DEC HL
HL.Word--;
return 6;
@@ -276,6 +317,17 @@ namespace Core.Cpu
return 12; // Jump taken
}
return 7; // Jump not taken
case 0x35: // DEC (HL)
// Read the current byte from memory
byte memValue = _memory.Read(HL.Word);
// Decrement it and update flags
byte decremented = Dec8(memValue);
// Write the new value back to memory
_memory.Write(HL.Word, decremented);
return 11; // Takes 11 T-States
case 0x36: // LD (HL), n
byte nValue = FetchByte();
_memory.Write(HL.Word, nValue);
@@ -310,6 +362,20 @@ namespace Core.Cpu
_ioBus.Write(portAddress, AF.High);
return 11; // Takes 11 T-States
case 0xD9: // EXX
ushort tempBC = BC.Word;
BC.Word = BC_Prime.Word;
BC_Prime.Word = tempBC;
ushort tempDE = DE.Word;
DE.Word = DE_Prime.Word;
DE_Prime.Word = tempDE;
ushort tempHL = HL.Word;
HL.Word = HL_Prime.Word;
HL_Prime.Word = tempHL;
return 4; // Takes 4 T-States
case 0xDE: // SBC A, n
Sbc(FetchByte());
return 7;