summaryrefslogtreecommitdiff
path: root/src/Noddybox.Emulation.EightBit
diff options
context:
space:
mode:
Diffstat (limited to 'src/Noddybox.Emulation.EightBit')
-rw-r--r--src/Noddybox.Emulation.EightBit/Binary.cs63
-rw-r--r--src/Noddybox.Emulation.EightBit/ICpu.cs57
-rw-r--r--src/Noddybox.Emulation.EightBit/IDevice.cs37
-rw-r--r--src/Noddybox.Emulation.EightBit/IMemory.cs36
-rw-r--r--src/Noddybox.Emulation.EightBit/Register16.cs88
5 files changed, 281 insertions, 0 deletions
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");
+ }
+ }
+ }
+}