From f96332b0913a8a365db73a1af3443c0e225a23d8 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 29 Dec 2011 00:17:13 +0000 Subject: Development check-in. --- Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs | 179 +++++++++++++++++++----------- 1 file changed, 117 insertions(+), 62 deletions(-) (limited to 'Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs') diff --git a/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs b/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs index d967bb9..1583ae0 100644 --- a/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs +++ b/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs @@ -1,4 +1,7 @@ -using System; +// +// Copyright (c) 2012 Ian Cowburn +// +using System; using System.Net; using System.Windows; using System.Windows.Controls; @@ -22,13 +25,13 @@ namespace Noddybox.Emulation.EightBit.Z80 private enum Z80Flags { None = 0x00, - Carry = 0x01, - Neg = 0x02, - PV = 0x04, - Hidden3 = 0x08, - HalfCarry = 0x10, - Hidden5 = 0x20, - Zero = 0x40, + Carry = 0x01, + Neg = 0x02, + PV = 0x04, + Hidden3 = 0x08, + HalfCarry = 0x10, + Hidden5 = 0x20, + Zero = 0x40, Sign = 0x80 }; @@ -38,11 +41,12 @@ namespace Noddybox.Emulation.EightBit.Z80 // Lookup tables // - private Z80Flags[] PSZtable = new Z80Flags[512]; - private Z80Flags[] SZtable = new Z80Flags[512]; - private Z80Flags[] Ptable = new Z80Flags[512]; - private Z80Flags[] Stable = new Z80Flags[512]; + private Z80Flags[] PSZtable = new Z80Flags[512]; + private Z80Flags[] SZtable = new Z80Flags[512]; + private Z80Flags[] Ptable = new Z80Flags[512]; + private Z80Flags[] Stable = new Z80Flags[512]; private Z80Flags[] Ztable = new Z80Flags[512]; + private Z80Flags[] H35table = new Z80Flags[256]; // Machine accessors // @@ -53,17 +57,14 @@ namespace Noddybox.Emulation.EightBit.Z80 // Main registers // private byte A; - private byte F; - private byte R; - private byte IFF1; - private byte IFF2; + 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 IRegister16 SP = Register16Factory.Create(); - private IRegister16 PC = Register16Factory.Create(); + private ushort SP; + private ushort PC; // Alternate registers // @@ -72,6 +73,15 @@ namespace Noddybox.Emulation.EightBit.Z80 private IRegister16 DE_ = Register16Factory.Create(); private IRegister16 HL_ = Register16Factory.Create(); + // Auxilliary registers and flags + // + private byte I; + private byte R; + private bool IFF1; + private bool IFF2; + private int IM; + private bool HALT; + #endregion #region ICpu Members @@ -87,7 +97,29 @@ namespace Noddybox.Emulation.EightBit.Z80 public void Reset() { - // TODO: Initial register settings. + PC = 0; + 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; + + IX.Value = 0xffff; + IY.Value = 0xffff; + + SP = 0xffff; + + IFF1 = false; + IFF2 = false; + IM = 0; + I = 0; + R = 0; + HALT = false; } public void Step() @@ -97,16 +129,23 @@ namespace Noddybox.Emulation.EightBit.Z80 public void Run() { - // TODO: Run for a frame + while(!clock.FrameDone) + { + Step(); + } } public void MaskableInterrupt(byte value) { + clock.Add(2); + // TODO: INT } public void NonMaskableInterrupt(byte value) { + clock.Add(2); + // TODO: NMI } @@ -118,48 +157,64 @@ namespace Noddybox.Emulation.EightBit.Z80 { // Setup lookup tables // - for(int f = 0; f < 256; f++) - { - Z80Flags p = Z80Flags.None; - Z80Flags z = Z80Flags.None; - Z80Flags s = Z80Flags.None; - int parity = 0; - - for(int b=0; b < 8; b++) - { - if ((f & (1<