From 4a14a990665f766e349b9027dbf980bc39f395f6 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 31 Dec 2011 00:10:45 +0000 Subject: Development check-in. --- Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs | 40 ++- .../Z80CpuBaseOpcodes.cs | 394 ++++++++++++++++++++- 2 files changed, 400 insertions(+), 34 deletions(-) (limited to 'Noddybox.Emulation.EightBit.Z80') diff --git a/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs b/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs index 1583ae0..5c40854 100644 --- a/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs +++ b/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs @@ -58,20 +58,20 @@ namespace Noddybox.Emulation.EightBit.Z80 // private byte A; private Z80Flags F; - private IRegister16 BC = Register16Factory.Create(); - private IRegister16 DE = Register16Factory.Create(); - private IRegister16 HL = Register16Factory.Create(); - private IRegister16 IX = Register16Factory.Create(); - private IRegister16 IY = Register16Factory.Create(); + private Register16 BC = new Register16(0); + private Register16 DE = new Register16(0); + private Register16 HL = new Register16(0); + private Register16 IX = new Register16(0); + private Register16 IY = new Register16(0); private ushort SP; private ushort PC; // Alternate registers // - private IRegister16 AF_ = Register16Factory.Create(); - private IRegister16 BC_ = Register16Factory.Create(); - private IRegister16 DE_ = Register16Factory.Create(); - private IRegister16 HL_ = Register16Factory.Create(); + private Register16 AF_ = new Register16(0); + private Register16 BC_ = new Register16(0); + private Register16 DE_ = new Register16(0); + private Register16 HL_ = new Register16(0); // Auxilliary registers and flags // @@ -101,16 +101,16 @@ namespace Noddybox.Emulation.EightBit.Z80 A = 0xff; F = (Z80Flags)0xff; - BC.Value = 0xffff; - DE.Value = 0xffff; - HL.Value = 0xffff; - AF_.Value = 0xffff; - BC_.Value = 0xffff; - DE_.Value = 0xffff; - HL_.Value = 0xffff; + BC.reg = 0xffff; + DE.reg = 0xffff; + HL.reg = 0xffff; + AF_.reg = 0xffff; + BC_.reg = 0xffff; + DE_.reg = 0xffff; + HL_.reg = 0xffff; - IX.Value = 0xffff; - IY.Value = 0xffff; + IX.reg = 0xffff; + IY.reg = 0xffff; SP = 0xffff; @@ -155,6 +155,10 @@ namespace Noddybox.Emulation.EightBit.Z80 public Z80Cpu() { + // Verify runtime + // + Register16.Verify(); + // Setup lookup tables // for(int f = 0; f < 256; f++) diff --git a/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs b/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs index c42c163..40b31ca 100644 --- a/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs +++ b/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs @@ -16,29 +16,49 @@ namespace Noddybox.Emulation.EightBit.Z80 { public partial class Z80Cpu { + #region Status register helpers + + /// + /// Set a flag in the status register. + /// + /// The flag. + private void SetFlag(Z80Flags flag) + { + F |= flag; + } + + /// + /// Clear a flag in the status register. + /// + /// The flag. + private void ClearFlag(Z80Flags flag) + { + F &= ~flag; + } + + #endregion + #region Stack commands /// /// Push a value on the stack. /// /// The value. - private void Push(ushort val) + private void PUSH(ushort val) { - unchecked - { - memory.Write(--SP, val & 0xff); - memory.Write(--SP, Binary.ShiftRight(val, 8) & 0xff); - } + memory.Write(--SP, (byte)(val & 0xff)); + memory.Write(--SP, (byte)(Binary.ShiftRight(val, 8) & 0xff)); } /// /// Pop a value from the stack. /// /// - private ushort Pop() + private ushort POP() { SP = (ushort)((SP + 2) & 0xffff); - return (ushort)(memory.Read(SP-2) | (memory.Read(SP - 1) >> 8)); + return (ushort)(memory.Read((ushort)(SP-2)) | + (memory.Read((ushort)(SP - 1)) >> 8)); } #endregion @@ -286,10 +306,7 @@ namespace Noddybox.Emulation.EightBit.Z80 /// The register to increment. void Inc8(ref byte reg) { - unchecked - { - reg++; - } + reg++; F = Z80Flags.Carry; @@ -310,10 +327,7 @@ namespace Noddybox.Emulation.EightBit.Z80 /// The register to decrement. void Dec8(ref byte reg) { - unchecked - { - reg--; - } + reg--; F = Z80Flags.Carry | Z80Flags.Neg; @@ -329,5 +343,353 @@ namespace Noddybox.Emulation.EightBit.Z80 } #endregion + + #region ALU rotate and shift operations + + /// + /// Do RRCA. + /// + private void RRCA() + { + F &= Z80Flags.Sign | Z80Flags.Zero | Z80Flags.PV; + F |= (Z80Flags)(A & 1); + A = (byte)(Binary.ShiftRight(A, 1) | Binary.ShiftLeft(A, 7)); + F |= H35table[A]; + } + + /// + /// Do RRA. + /// + private void RRA() + { + byte carry = (byte)(F & Z80Flags.Carry); + F &= Z80Flags.Sign | Z80Flags.Zero | Z80Flags.PV; + F |= (Z80Flags)(A & 1); + A = (byte)(Binary.ShiftRight(A, 1) | Binary.ShiftLeft(carry, 7)); + F |= H35table[A]; + } + + /// + /// Do RRC. + /// + /// The register to operate on. + private void RRC(ref byte reg) + { + F = (Z80Flags)(reg & (int)Z80Flags.Carry); + reg = (byte)(Binary.ShiftRight(reg, 1) | Binary.ShiftLeft(reg, 7)); + F |= PSZtable[reg] | H35table[reg]; + } + + /// + /// Do RR. + /// + /// The register to operate on. + private void RR(ref byte reg) + { + byte carry = (byte)(F & Z80Flags.Carry); + F = (Z80Flags)(reg & (int)Z80Flags.Carry); + reg = (byte)(Binary.ShiftRight(reg, 1) | Binary.ShiftLeft(carry, 7)); + F |= PSZtable[reg] | H35table[reg]; + } + + /// + /// Do RLCA. + /// + private void RLCA() + { + F = (F & Z80Flags.PV | Z80Flags.Sign | Z80Flags.Zero) + | (Z80Flags)Binary.ShiftRight(A, 7); + + A = (byte)(Binary.ShiftLeft(A, 1) | Binary.ShiftRight(A, 7)); + + F |= H35table[A]; + } + + /// + /// Do RLA. + /// + private void RLA() + { + byte carry = (byte)(F & Z80Flags.Carry); + F = (F & Z80Flags.PV | Z80Flags.Sign | Z80Flags.Zero) + | (Z80Flags)Binary.ShiftRight(A, 7); + A = (byte)(Binary.ShiftRight(A, 1) | carry); + F |= H35table[A]; + } + + /// + /// Do RLC. + /// + /// The register to operate on. + private void RLC(ref byte reg) + { + byte carry = Binary.ShiftRight(reg, 7); + reg = (byte)(Binary.ShiftLeft(reg, 1) | carry); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + /// + /// Do RL. + /// + /// The register to operate on. + private void RL(ref byte reg) + { + byte carry = Binary.ShiftRight(reg, 7); + reg = (byte)(Binary.ShiftLeft(reg, 1) | (int)(F & Z80Flags.Carry)); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + /// + /// Do SRL. + /// + /// The register to operate on. + private void SRL(ref byte reg) + { + byte carry = (byte)(reg & 1); + reg = Binary.ShiftLeft(reg, 1); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + /// + /// Do SRA. + /// + /// The register to operate on. + private void SRA(ref byte reg) + { + byte carry = (byte)(reg & 1); + reg = (byte)(Binary.ShiftLeft(reg, 1) | (reg & 0x80)); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + /// + /// Do SLL. + /// + /// The register to operate on. + private void SLL(ref byte reg) + { + byte carry = Binary.ShiftRight(reg, 7); + reg = (byte)(Binary.ShiftRight(reg, 1) | 0x01); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + /// + /// Do SLA. + /// + /// The register to operate on. + private void SLA(ref byte reg) + { + byte carry = Binary.ShiftRight(reg, 7); + reg = Binary.ShiftRight(reg, 1); + F = PSZtable[reg] | (Z80Flags)carry | H35table[reg]; + } + + #endregion + + #region ALU boolean operations + + /// + /// AND a value with the accumulator. + /// + /// The value. + void AND(byte val) + { + A &= val; + F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A]; + } + + /// + /// OR a value with the accumulator. + /// + /// The value. + void OR(byte val) + { + A |= val; + F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A]; + } + + /// + /// XOR a value with the accumulator. + /// + /// The value. + void XOR(byte val) + { + A ^= val; + F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A]; + } + + /// + /// Perform the BIT operation. + /// + /// The register to operate on. + /// The bit to test. + void BIT(ref byte reg, int bit) + { + F &= Z80Flags.Carry; + F |= Z80Flags.HalfCarry; + + if ((reg & (1 << bit)) != 0) + { + if (bit == 7 && (reg & (int)Z80Flags.Sign) != 0) + { + F |= Z80Flags.Sign; + } + + if (bit == 5 && (reg & (int)Z80Flags.Hidden5) != 0) + { + F |= Z80Flags.Hidden5; + } + + if (bit == 3 && (reg & (int)Z80Flags.Hidden3) != 0) + { + F |= Z80Flags.Hidden3; + } + } + else + { + F |= Z80Flags.Zero | Z80Flags.PV; + } + } + + /// + /// Perform the bit set operation. + /// + /// The register to operate on. + /// The bit to test. + void BIT_SET(ref byte reg, int bit) + { + reg |= (byte)(1 << bit); + } + + /// + /// Perform the bit clear operation. + /// + /// The register to operate on. + /// The bit to test. + void BIT_RES(ref byte reg, int bit) + { + reg &= (byte)~(1 << bit); + } + + #endregion + + #region Jump operations + + /// + /// The call operation. + /// + private void CALL() + { + PUSH((ushort)(PC + 2)); + PC = (ushort)(memory.Read(PC) | memory.Read((ushort)(PC+1)) >> 8); + } + + /// + /// The jump operation. + /// + private void JP() + { + PC = (ushort)(memory.Read(PC) | memory.Read((ushort)(PC+1)) >> 8); + } + + /// + /// The jump relative operation. + /// + private void JR() + { + PC = (ushort)(PC + (sbyte)memory.Read(PC) + 1); + } + + /// + /// Jump relative if the passed condition flag ANDed with the flag + /// register equals the passed check value. + /// + /// The condition flag. + /// The check value. + private void JR_COND(Z80Flags cond, Z80Flags val) + { + if ((F & cond) == val) + { + clock.Add(12); + JR(); + } + else + { + clock.Add(7); + PC++; + } + } + + /// + /// Jump if the passed condition flag ANDed with the flag + /// register equals the passed check value. + /// + /// The condition flag. + /// The check value. + private void JP_COND(Z80Flags cond, Z80Flags val) + { + clock.Add(10); + + if ((F & cond) == val) + { + JP(); + } + else + { + PC+=2; + } + } + + /// + /// Call if the passed condition flag ANDed with the flag + /// register equals the passed check value. + /// + /// The condition flag. + /// The check value. + private void CALL_COND(Z80Flags cond, Z80Flags val) + { + if ((F & cond) == val) + { + clock.Add(17); + CALL(); + } + else + { + clock.Add(10); + PC+=2; + } + } + + /// + /// Return if the passed condition flag ANDed with the flag + /// register equals the passed check value. + /// + /// The condition flag. + /// The check value. + private void RET_COND(Z80Flags cond, Z80Flags val) + { + if ((F & cond) == val) + { + clock.Add(11); + PC = POP(); + } + else + { + clock.Add(5); + } + } + + /// + /// Reset the PC to an address + /// register equals the passed check value. + /// + /// The address. + private void RST(ushort addr) + { + clock.Add(11); + PUSH(PC); + PC = addr; + } + + #endregion } } -- cgit v1.2.3