diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs | 226 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs | 695 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit/Binary.cs | 63 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit/ICpu.cs | 57 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit/IDevice.cs | 37 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit/IMemory.cs | 36 | ||||
-rw-r--r-- | src/Noddybox.Emulation.EightBit/Register16.cs | 88 | ||||
-rw-r--r-- | src/Noddybox.Emulation/Clock.cs | 112 |
8 files changed, 1314 insertions, 0 deletions
diff --git a/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs b/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs new file mode 100644 index 0000000..5c40854 --- /dev/null +++ b/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs @@ -0,0 +1,226 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit.Z80
+{
+ /// <summary>
+ /// Provides an implementation of a Zilog Z80 processor.
+ /// </summary>
+ public partial class Z80Cpu : ICpu
+ {
+ #region Private types
+
+ [Flags]
+ private enum Z80Flags
+ {
+ None = 0x00,
+ Carry = 0x01,
+ Neg = 0x02,
+ PV = 0x04,
+ Hidden3 = 0x08,
+ HalfCarry = 0x10,
+ Hidden5 = 0x20,
+ Zero = 0x40,
+ Sign = 0x80
+ };
+
+ #endregion
+
+ #region Private data
+
+ // 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[] Ztable = new Z80Flags[512];
+ private Z80Flags[] H35table = new Z80Flags[256];
+
+ // Machine accessors
+ //
+ private IMemory memory;
+ private IDevice device;
+ private Clock clock;
+
+ // Main registers
+ //
+ private byte A;
+ private Z80Flags F;
+ 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 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
+ //
+ private byte I;
+ private byte R;
+ private bool IFF1;
+ private bool IFF2;
+ private int IM;
+ private bool HALT;
+
+ #endregion
+
+ #region ICpu Members
+
+ public void Initialise(IMemory memory, IDevice device, Clock clock)
+ {
+ this.memory = memory;
+ this.device = device;
+ this.clock = clock;
+
+ Reset();
+ }
+
+ public void Reset()
+ {
+ PC = 0;
+ A = 0xff;
+ F = (Z80Flags)0xff;
+
+ BC.reg = 0xffff;
+ DE.reg = 0xffff;
+ HL.reg = 0xffff;
+ AF_.reg = 0xffff;
+ BC_.reg = 0xffff;
+ DE_.reg = 0xffff;
+ HL_.reg = 0xffff;
+
+ IX.reg = 0xffff;
+ IY.reg = 0xffff;
+
+ SP = 0xffff;
+
+ IFF1 = false;
+ IFF2 = false;
+ IM = 0;
+ I = 0;
+ R = 0;
+ HALT = false;
+ }
+
+ public void Step()
+ {
+ // TODO: Single step.
+ }
+
+ public void Run()
+ {
+ while(!clock.FrameDone)
+ {
+ Step();
+ }
+ }
+
+ public void MaskableInterrupt(byte value)
+ {
+ clock.Add(2);
+
+ // TODO: INT
+ }
+
+ public void NonMaskableInterrupt(byte value)
+ {
+ clock.Add(2);
+
+ // TODO: NMI
+ }
+
+ #endregion
+
+ #region Constructors
+
+ public Z80Cpu()
+ {
+ // Verify runtime
+ //
+ Register16.Verify();
+
+ // Setup lookup tables
+ //
+ for(int f = 0; f < 256; f++)
+ {
+ Z80Flags p = Z80Flags.None;
+ Z80Flags z = Z80Flags.None;
+ Z80Flags s = Z80Flags.None;
+ Z80Flags h3 = Z80Flags.None;
+ Z80Flags h5 = Z80Flags.None;
+
+ int parity = 0;
+
+ for(int b=0; b < 8; b++)
+ {
+ if ((f & (1<<b)) == (1<<b))
+ {
+ parity++;
+ }
+ }
+
+ if ((parity & 1) == 1)
+ {
+ p = Z80Flags.PV;
+ }
+
+ if (f == 0)
+ {
+ z = Z80Flags.Zero;
+ }
+
+ if ((f & 0x80) == 0x80)
+ {
+ s = Z80Flags.Sign;
+ }
+
+ if ((f & (int)Z80Flags.Hidden3) == (int)Z80Flags.Hidden3)
+ {
+ h3 = Z80Flags.Hidden3;
+ }
+
+ if ((f & (int)Z80Flags.Hidden5) == (int)Z80Flags.Hidden5)
+ {
+ h5 = Z80Flags.Hidden5;
+ }
+
+ Ptable[f] = p;
+ Stable[f] = s;
+ Ztable[f] = z;
+ SZtable[f] =z | s;
+ PSZtable[f] = z | s | p;
+ H35table[f] = h3 | h5;
+
+ Ptable[f+256] = Ptable[f] | Z80Flags.Carry;
+ Stable[f+256] = Stable[f] | Z80Flags.Carry;
+ Ztable[f+256] = Ztable[f] | Z80Flags.Carry;
+ SZtable[f+256] = SZtable[f] | Z80Flags.Carry;
+ PSZtable[f+256] = PSZtable[f] | Z80Flags.Carry;
+ }
+
+ Reset();
+ }
+
+ #endregion
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs new file mode 100644 index 0000000..40b31ca --- /dev/null +++ b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs @@ -0,0 +1,695 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit.Z80
+{
+ public partial class Z80Cpu
+ {
+ #region Status register helpers
+
+ /// <summary>
+ /// Set a flag in the status register.
+ /// </summary>
+ /// <param name="flag">The flag.</param>
+ private void SetFlag(Z80Flags flag)
+ {
+ F |= flag;
+ }
+
+ /// <summary>
+ /// Clear a flag in the status register.
+ /// </summary>
+ /// <param name="flag">The flag.</param>
+ private void ClearFlag(Z80Flags flag)
+ {
+ F &= ~flag;
+ }
+
+ #endregion
+
+ #region Stack commands
+
+ /// <summary>
+ /// Push a value on the stack.
+ /// </summary>
+ /// <param name="val">The value.</param>
+ private void PUSH(ushort val)
+ {
+ memory.Write(--SP, (byte)(val & 0xff));
+ memory.Write(--SP, (byte)(Binary.ShiftRight(val, 8) & 0xff));
+ }
+
+ /// <summary>
+ /// Pop a value from the stack.
+ /// </summary>
+ /// <returns></returns>
+ private ushort POP()
+ {
+ SP = (ushort)((SP + 2) & 0xffff);
+ return (ushort)(memory.Read((ushort)(SP-2)) |
+ (memory.Read((ushort)(SP - 1)) >> 8));
+ }
+
+ #endregion
+
+ #region ALU arithmetic and comparison
+
+ /// <summary>
+ /// Add an 8-bit value to the accumulator without carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Add8(byte b)
+ {
+ int w = A + b;
+
+ F = SZtable[w] | H35table[w & 0xff];
+
+ if (((A ^ w ^ b) & (int)Z80Flags.HalfCarry) == (int)Z80Flags.HalfCarry)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ if (((b ^ A) & (b ^ w) & 0x80) > 0)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ A = (byte)(w & 0xff);
+ }
+
+ /// <summary>
+ /// Add an 8-bit value to the accumulator with carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Adc8(byte b)
+ {
+ int w = A + b + (int)(F & Z80Flags.Carry);
+
+ F = SZtable[w] | H35table[w & 0xff];
+
+ if (((A ^ w ^ b) & (int)Z80Flags.HalfCarry) == (int)Z80Flags.HalfCarry)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ if (((b ^ A) & (b ^ w) & 0x80) > 0)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ A = (byte)(w & 0xff);
+ }
+
+ /// <summary>
+ /// Subtract an 8-bit value from the accumulator without carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Sub8(byte b)
+ {
+ int w = A - b;
+
+ if (w < 0)
+ {
+ w += 0x200;
+ }
+
+ F = SZtable[w] | H35table[w & 0xff] | Z80Flags.Neg;
+
+ if (((A ^ w ^ b) & (int)Z80Flags.HalfCarry) == (int)Z80Flags.HalfCarry)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ if (((b ^ A) & (b ^ w) & 0x80) > 0)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ A = (byte)(w & 0xff);
+ }
+
+ /// <summary>
+ /// Compare an 8-bit value with the accumulator.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Cmp8(byte b)
+ {
+ int w = A - b;
+
+ if (w < 0)
+ {
+ w += 0x200;
+ }
+
+ F = SZtable[w] | H35table[w & 0xff] | Z80Flags.Neg;
+
+ if (((A ^ w ^ b) & (int)Z80Flags.HalfCarry) == (int)Z80Flags.HalfCarry)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ if (((b ^ A) & (b ^ w) & 0x80) > 0)
+ {
+ F |= Z80Flags.PV;
+ }
+ }
+
+ /// <summary>
+ /// Subtract an 8-bit value from the accumulator with carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Sbc8(byte b)
+ {
+ int w = A - b - (int)(F & Z80Flags.Carry);
+
+ if (w < 0)
+ {
+ w += 0x200;
+ }
+
+ F = SZtable[w] | H35table[w & 0xff] | Z80Flags.Neg;
+
+ if (((A ^ w ^ b) & (int)Z80Flags.HalfCarry) == (int)Z80Flags.HalfCarry)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ if (((b ^ A) & (b ^ w) & 0x80) > 0)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ A = (byte)(w & 0xff);
+ }
+
+ /// <summary>
+ /// Add a 16-bit value to a register without carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Add16(ref ushort reg, ushort b)
+ {
+ int w = reg + b;
+
+ F &= Z80Flags.Sign | Z80Flags.Zero | Z80Flags.PV;
+
+ if (w > 0xffff)
+ {
+ F |= Z80Flags.Carry;
+ }
+
+ if ((reg ^ w ^ b) == 0x1000)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ reg = (ushort)(w & 0xffff);
+
+ F |= H35table[reg >> 8];
+ }
+
+ /// <summary>
+ /// Add a 16-bit value to a register with carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Adc16(ref ushort reg, ushort b)
+ {
+ int w = reg + b + (int)(F & Z80Flags.Carry);
+
+ F = Z80Flags.None;
+
+ if ((w & 0xffff) == 0)
+ {
+ F |= Z80Flags.Zero;
+ }
+
+ if ((w & 0x8000) == 0x8000)
+ {
+ F |= Z80Flags.Sign;
+ }
+
+ if (w > 0xffff)
+ {
+ F |= Z80Flags.Carry;
+ }
+
+ if (((b ^ reg ^ 0x8000) & ((reg ^ w) & 0x8000)) == 0x8000)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ if ((reg ^ w ^ b) == 0x1000)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ reg = (ushort)(w & 0xffff);
+
+ F |= H35table[reg >> 8];
+ }
+
+
+ /// <summary>
+ /// Subtract a 16-bit value from a register with carry.
+ /// </summary>
+ /// <param name="b">The vakue.</param>
+ private void Sbc16(ref ushort reg, ushort b)
+ {
+ int w = reg - b - (int)(F & Z80Flags.Carry);
+
+ F = Z80Flags.Neg;
+
+ if (w < 0)
+ {
+ w += 0x10000;
+ F |= Z80Flags.Carry;
+ }
+
+ if ((w & 0xffff) == 0)
+ {
+ F |= Z80Flags.Zero;
+ }
+
+ if ((w & 0x8000) == 0x8000)
+ {
+ F |= Z80Flags.Sign;
+ }
+
+ if (((b ^ reg) & ((reg ^ w) & 0x8000)) == 0x8000)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ if ((reg ^ w ^ b) == 0x1000)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+
+ reg = (ushort)(w & 0xffff);
+
+ F |= H35table[reg >> 8];
+ }
+
+ /// <summary>
+ /// Increment an 8-bit register.
+ /// </summary>
+ /// <param name="reg">The register to increment.</param>
+ void Inc8(ref byte reg)
+ {
+ reg++;
+
+ F = Z80Flags.Carry;
+
+ if (reg == 0x80)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ if ((reg & 0x0f) == 0x00)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+ }
+
+ /// <summary>
+ /// Decrement an 8-bit register.
+ /// </summary>
+ /// <param name="reg">The register to decrement.</param>
+ void Dec8(ref byte reg)
+ {
+ reg--;
+
+ F = Z80Flags.Carry | Z80Flags.Neg;
+
+ if (reg == 0x7f)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ if ((reg & 0x0f) == 0x0f)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+ }
+
+ #endregion
+
+ #region ALU rotate and shift operations
+
+ /// <summary>
+ /// Do RRCA.
+ /// </summary>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RRA.
+ /// </summary>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RRC.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RR.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RLCA.
+ /// </summary>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RLA.
+ /// </summary>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RLC.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do RL.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do SRL.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ private void SRL(ref byte reg)
+ {
+ byte carry = (byte)(reg & 1);
+ reg = Binary.ShiftLeft(reg, 1);
+ F = PSZtable[reg] | (Z80Flags)carry | H35table[reg];
+ }
+
+ /// <summary>
+ /// Do SRA.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do SLL.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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];
+ }
+
+ /// <summary>
+ /// Do SLA.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ 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
+
+ /// <summary>
+ /// AND a value with the accumulator.
+ /// </summary>
+ /// <param name="val">The value.</param>
+ void AND(byte val)
+ {
+ A &= val;
+ F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A];
+ }
+
+ /// <summary>
+ /// OR a value with the accumulator.
+ /// </summary>
+ /// <param name="val">The value.</param>
+ void OR(byte val)
+ {
+ A |= val;
+ F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A];
+ }
+
+ /// <summary>
+ /// XOR a value with the accumulator.
+ /// </summary>
+ /// <param name="val">The value.</param>
+ void XOR(byte val)
+ {
+ A ^= val;
+ F = PSZtable[A] | Z80Flags.HalfCarry | H35table[A];
+ }
+
+ /// <summary>
+ /// Perform the BIT operation.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ /// <param name="bit">The bit to test.</param>
+ 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;
+ }
+ }
+
+ /// <summary>
+ /// Perform the bit set operation.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ /// <param name="bit">The bit to test.</param>
+ void BIT_SET(ref byte reg, int bit)
+ {
+ reg |= (byte)(1 << bit);
+ }
+
+ /// <summary>
+ /// Perform the bit clear operation.
+ /// </summary>
+ /// <param name="reg">The register to operate on.</param>
+ /// <param name="bit">The bit to test.</param>
+ void BIT_RES(ref byte reg, int bit)
+ {
+ reg &= (byte)~(1 << bit);
+ }
+
+ #endregion
+
+ #region Jump operations
+
+ /// <summary>
+ /// The call operation.
+ /// </summary>
+ private void CALL()
+ {
+ PUSH((ushort)(PC + 2));
+ PC = (ushort)(memory.Read(PC) | memory.Read((ushort)(PC+1)) >> 8);
+ }
+
+ /// <summary>
+ /// The jump operation.
+ /// </summary>
+ private void JP()
+ {
+ PC = (ushort)(memory.Read(PC) | memory.Read((ushort)(PC+1)) >> 8);
+ }
+
+ /// <summary>
+ /// The jump relative operation.
+ /// </summary>
+ private void JR()
+ {
+ PC = (ushort)(PC + (sbyte)memory.Read(PC) + 1);
+ }
+
+ /// <summary>
+ /// Jump relative if the passed condition flag ANDed with the flag
+ /// register equals the passed check value.
+ /// </summary>
+ /// <param name="cond">The condition flag.</param>
+ /// <param name="val">The check value.</param>
+ private void JR_COND(Z80Flags cond, Z80Flags val)
+ {
+ if ((F & cond) == val)
+ {
+ clock.Add(12);
+ JR();
+ }
+ else
+ {
+ clock.Add(7);
+ PC++;
+ }
+ }
+
+ /// <summary>
+ /// Jump if the passed condition flag ANDed with the flag
+ /// register equals the passed check value.
+ /// </summary>
+ /// <param name="cond">The condition flag.</param>
+ /// <param name="val">The check value.</param>
+ private void JP_COND(Z80Flags cond, Z80Flags val)
+ {
+ clock.Add(10);
+
+ if ((F & cond) == val)
+ {
+ JP();
+ }
+ else
+ {
+ PC+=2;
+ }
+ }
+
+ /// <summary>
+ /// Call if the passed condition flag ANDed with the flag
+ /// register equals the passed check value.
+ /// </summary>
+ /// <param name="cond">The condition flag.</param>
+ /// <param name="val">The check value.</param>
+ private void CALL_COND(Z80Flags cond, Z80Flags val)
+ {
+ if ((F & cond) == val)
+ {
+ clock.Add(17);
+ CALL();
+ }
+ else
+ {
+ clock.Add(10);
+ PC+=2;
+ }
+ }
+
+ /// <summary>
+ /// Return if the passed condition flag ANDed with the flag
+ /// register equals the passed check value.
+ /// </summary>
+ /// <param name="cond">The condition flag.</param>
+ /// <param name="val">The check value.</param>
+ private void RET_COND(Z80Flags cond, Z80Flags val)
+ {
+ if ((F & cond) == val)
+ {
+ clock.Add(11);
+ PC = POP();
+ }
+ else
+ {
+ clock.Add(5);
+ }
+ }
+
+ /// <summary>
+ /// Reset the PC to an address
+ /// register equals the passed check value.
+ /// </summary>
+ /// <param name="addr">The address.</param>
+ private void RST(ushort addr)
+ {
+ clock.Add(11);
+ PUSH(PC);
+ PC = addr;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit/Binary.cs b/src/Noddybox.Emulation.EightBit/Binary.cs new file mode 100644 index 0000000..0b1928b --- /dev/null +++ b/src/Noddybox.Emulation.EightBit/Binary.cs @@ -0,0 +1,63 @@ +using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit
+{
+ /// <summary>
+ /// Provides helpers for shifting smaller values around without casts all over the shop.
+ /// </summary>
+ public static class Binary
+ {
+ /// <summary>
+ /// Shift 8-bits to the left.
+ /// </summary>
+ /// <param name="b">The byte.</param>
+ /// <param name="shift">How many bits to shift.</param>
+ /// <returns>The shifted value.</returns>
+ public static byte ShiftLeft(byte b, int shift)
+ {
+ return (byte)((b << shift) & 0xff);
+ }
+
+ /// <summary>
+ /// Shift 8-bits to the right.
+ /// </summary>
+ /// <param name="b">The byte.</param>
+ /// <param name="shift">How many bits to shift.</param>
+ /// <returns>The shifted value.</returns>
+ public static byte ShiftRight(byte b, int shift)
+ {
+ return (byte)((b >> shift) & 0xff);
+ }
+
+ /// <summary>
+ /// Shift 16-bits to the left.
+ /// </summary>
+ /// <param name="w">The word.</param>
+ /// <param name="shift">How many bits to shift.</param>
+ /// <returns>The shifted value.</returns>
+ public static ushort ShiftLeft(ushort w, int shift)
+ {
+ return (ushort)((w << shift) & 0xffff);
+ }
+
+ /// <summary>
+ /// Shift 16-bits to the right.
+ /// </summary>
+ /// <param name="w">The word.</param>
+ /// <param name="shift">How many bits to shift.</param>
+ /// <returns>The shifted value.</returns>
+ public static ushort ShiftRight(ushort w, int shift)
+ {
+ return (ushort)((w >> shift) & 0xffff);
+ }
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit/ICpu.cs b/src/Noddybox.Emulation.EightBit/ICpu.cs new file mode 100644 index 0000000..dc2f811 --- /dev/null +++ b/src/Noddybox.Emulation.EightBit/ICpu.cs @@ -0,0 +1,57 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit
+{
+ /// <summary>
+ /// Defines an 8-bit CPU.
+ /// </summary>
+ public interface ICpu
+ {
+ /// <summary>
+ /// Initialise the CPU to give it access to memory and devices.
+ /// </summary>
+ /// <param name="memory">The memory to access.</param>
+ /// <param name="device">The devices to access.</param>
+ /// <param name="clock">The clock to use.</param>
+ void Initialise(IMemory memory, IDevice device, Clock clock);
+
+ /// <summary>
+ /// Resets the CPU to its initial state.
+ /// </summary>
+ void Reset();
+
+ /// <summary>
+ /// Runs the next instruction.
+ /// </summary>
+ void Step();
+
+ /// <summary>
+ /// Runs the CPU until the next frame flyback.
+ /// </summary>
+ void Run();
+
+ /// <summary>
+ /// Generates a maskable interrupt to the CPU.
+ /// </summary>
+ /// <param name="value">Optional value from an interrupting device. May be ignored depending on the CPU type.</param>
+ void MaskableInterrupt(byte value);
+
+ /// <summary>
+ /// Generates a non-maskable interrupt to the CPU.
+ /// </summary>
+ /// <param name="value">Optional value from an interrupting device. May be ignored depending on the CPU type.</param>
+ void NonMaskableInterrupt(byte value);
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit/IDevice.cs b/src/Noddybox.Emulation.EightBit/IDevice.cs new file mode 100644 index 0000000..b8d234a --- /dev/null +++ b/src/Noddybox.Emulation.EightBit/IDevice.cs @@ -0,0 +1,37 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit
+{
+ /// <summary>
+ /// Provides an interface for devices for 8-bit processers which used special commands to access
+ /// devices rather than using memory-mapped IO, for example the Z80.
+ /// </summary>
+ public interface IDevice
+ {
+ /// <summary>
+ /// Read from a device.
+ /// </summary>
+ /// <param name="device">The address of the device.</param>
+ /// <returns>The byte returned from the device.</returns>
+ byte Read(ushort device);
+
+ /// <summary>
+ /// Write to a device.
+ /// </summary>
+ /// <param name="device">The address of the device.</param>
+ /// <param name="value">The value to write to the device.</param>
+ void Write(ushort device, byte value);
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit/IMemory.cs b/src/Noddybox.Emulation.EightBit/IMemory.cs new file mode 100644 index 0000000..2aa63d9 --- /dev/null +++ b/src/Noddybox.Emulation.EightBit/IMemory.cs @@ -0,0 +1,36 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation.EightBit
+{
+ /// <summary>
+ /// Defines an interface for memory.
+ /// </summary>
+ public interface IMemory
+ {
+ /// <summary>
+ /// Reads a byte at a given address.
+ /// </summary>
+ /// <param name="address">The address to read.</param>
+ /// <returns>The value at that address.</returns>
+ byte Read(ushort address);
+
+ /// <summary>
+ /// Writes a byte at a given address.
+ /// </summary>
+ /// <param name="address">The address to write to.</param>
+ /// <param name="value">The value to write.</param>
+ void Write(ushort address, byte value);
+ }
+}
diff --git a/src/Noddybox.Emulation.EightBit/Register16.cs b/src/Noddybox.Emulation.EightBit/Register16.cs new file mode 100644 index 0000000..06c9c01 --- /dev/null +++ b/src/Noddybox.Emulation.EightBit/Register16.cs @@ -0,0 +1,88 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Runtime.InteropServices;
+
+namespace Noddybox.Emulation.EightBit
+{
+ /// <summary>
+ /// Provides a common 8-bit register pattern, which is two 8-bit registers rolled into one.
+ /// While this could be done at runtime using <see cref="BitConverter.IsLittleEndian"/> this
+ /// method was chosen for speed reasons; interfaces cannot define fields, which is sensible
+ /// enoough, but accessing fields directly would allow ref variables to be used with this type's
+ /// fields.
+ /// </summary>
+ [StructLayout(LayoutKind.Explicit)]
+ public struct Register16
+ {
+#if TARGET_IS_BIGENDIAN
+
+ /// <summary>
+ /// The 16-bit register.
+ /// </summary>
+ [FieldOffset(0)]
+ public ushort reg;
+
+ /// <summary>
+ /// The high 8-bits of the register.
+ /// </summary>
+ [FieldOffset(0)]
+ public byte high;
+
+ /// <summary>
+ /// The low 8-bits of the register.
+ /// </summary>
+ [FieldOffset(1)]
+ public byte low;
+
+#else
+
+ /// <summary>
+ /// The 16-bit register.
+ /// </summary>
+ [FieldOffset(0)]
+ public ushort reg;
+
+ /// <summary>
+ /// The low 8-bits of the register.
+ /// </summary>
+ [FieldOffset(0)]
+ public byte low;
+
+ /// <summary>
+ /// The high 8-bits of the register.
+ /// </summary>
+ [FieldOffset(1)]
+ public byte high;
+
+#endif
+
+ /// <summary>
+ /// Public constructor. This is provided to stop the compiler complaining when you use the struct as it
+ /// doesn't realise that setting <see cref="reg"/> will also set <see cref="low"/> and <see cref="high"/>.
+ /// </summary>
+ /// <param name="val">The 16-bit value to assign.</param>
+ public Register16(ushort val)
+ {
+ high = 0;
+ low = 0;
+ reg = val;
+ }
+
+ /// <summary>
+ /// Used to verify the compile-time setting at runtime.
+ /// </summary>
+ public static void Verify()
+ {
+ Register16 r = new Register16(0);
+
+ r.reg = 0x1234;
+
+ if (r.low != 0x24 || r.high != 0x12)
+ {
+ throw new Exception("Compile time endian setting incorrect");
+ }
+ }
+ }
+}
diff --git a/src/Noddybox.Emulation/Clock.cs b/src/Noddybox.Emulation/Clock.cs new file mode 100644 index 0000000..0b733cb --- /dev/null +++ b/src/Noddybox.Emulation/Clock.cs @@ -0,0 +1,112 @@ +//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace Noddybox.Emulation
+{
+ /// <summary>
+ /// Provides a basic clock implementation that is geared towards the VBL.
+ /// </summary>
+ public class Clock
+ {
+ #region Private fields
+
+ private uint perSecond;
+ private uint perFrame;
+ private uint perLine;
+ private uint frameCount;
+
+ #endregion
+
+ #region Public properties
+
+ /// <summary>
+ /// Returns true if the frame has finished.
+ /// </summary>
+ public bool FrameDone
+ {
+ get
+ {
+ return frameCount > perFrame;
+ }
+ }
+
+ /// <summary>
+ /// Returns the current raster line.
+ /// </summary>
+ public uint RasterLine
+ {
+ get
+ {
+ return frameCount / perLine;
+ }
+ }
+
+ #endregion
+
+ #region Public members
+
+ /// <summary>
+ /// Starts a new frame.
+ /// </summary>
+ public void StartFrame()
+ {
+ if (FrameDone)
+ {
+ frameCount -= perFrame;
+ }
+ }
+
+ /// <summary>
+ /// Adds a number of ticks to the clock.
+ /// </summary>
+ /// <param name="ticks">The ticks to add.</param>
+ public void Add(uint ticks)
+ {
+ frameCount += ticks;
+ }
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Defines a clock.
+ /// </summary>
+ /// <param name="ticksPerSecond">The number of ticks per second.</param>
+ /// <param name="framesPerSecond">The number of frames per second.</param>
+ public Clock(uint ticksPerSecond, uint framesPerSecond)
+ {
+ this.perSecond = ticksPerSecond;
+ this.perFrame = ticksPerSecond / framesPerSecond;
+ this.perLine = 1;
+ this.frameCount = 0;
+ }
+
+ /// <summary>
+ /// Defines a clock.
+ /// </summary>
+ /// <param name="ticksPerSecond">The number of ticks per second.</param>
+ /// <param name="framesPerSecond">The number of frames per second.</param>
+ /// <param name="ticksPerLine">The number of clock ticks per raster line.</param>
+ public Clock(uint ticksPerSecond, uint framesPerSecond, uint ticksPerLine)
+ {
+ this.perSecond = ticksPerSecond;
+ this.perFrame = ticksPerSecond / framesPerSecond;
+ this.perLine = ticksPerLine;
+ this.frameCount = 0;
+ }
+
+ #endregion
+ }
+}
|