summaryrefslogtreecommitdiff
path: root/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs')
-rw-r--r--Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs333
1 files changed, 333 insertions, 0 deletions
diff --git a/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs b/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs
new file mode 100644
index 0000000..c42c163
--- /dev/null
+++ b/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs
@@ -0,0 +1,333 @@
+//
+// 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 Stack commands
+
+ /// <summary>
+ /// Push a value on the stack.
+ /// </summary>
+ /// <param name="val">The value.</param>
+ private void Push(ushort val)
+ {
+ unchecked
+ {
+ memory.Write(--SP, val & 0xff);
+ memory.Write(--SP, 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(SP-2) | (memory.Read(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)
+ {
+ unchecked
+ {
+ 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)
+ {
+ unchecked
+ {
+ reg--;
+ }
+
+ F = Z80Flags.Carry | Z80Flags.Neg;
+
+ if (reg == 0x7f)
+ {
+ F |= Z80Flags.PV;
+ }
+
+ if ((reg & 0x0f) == 0x0f)
+ {
+ F |= Z80Flags.HalfCarry;
+ }
+ }
+
+ #endregion
+ }
+}