From 2b8d49726e448e22b5055da5ba4395d043030984 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 1 Jan 2012 14:21:26 +0000 Subject: Moved sources to help in compilation for other platforms and isolated phone projects. --- src/Noddybox.Emulation.EightBit/Binary.cs | 63 +++++++++++++++++++ src/Noddybox.Emulation.EightBit/ICpu.cs | 57 +++++++++++++++++ src/Noddybox.Emulation.EightBit/IDevice.cs | 37 +++++++++++ src/Noddybox.Emulation.EightBit/IMemory.cs | 36 +++++++++++ src/Noddybox.Emulation.EightBit/Register16.cs | 88 +++++++++++++++++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 src/Noddybox.Emulation.EightBit/Binary.cs create mode 100644 src/Noddybox.Emulation.EightBit/ICpu.cs create mode 100644 src/Noddybox.Emulation.EightBit/IDevice.cs create mode 100644 src/Noddybox.Emulation.EightBit/IMemory.cs create mode 100644 src/Noddybox.Emulation.EightBit/Register16.cs (limited to 'src/Noddybox.Emulation.EightBit') 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 +{ + /// + /// Provides helpers for shifting smaller values around without casts all over the shop. + /// + public static class Binary + { + /// + /// Shift 8-bits to the left. + /// + /// The byte. + /// How many bits to shift. + /// The shifted value. + public static byte ShiftLeft(byte b, int shift) + { + return (byte)((b << shift) & 0xff); + } + + /// + /// Shift 8-bits to the right. + /// + /// The byte. + /// How many bits to shift. + /// The shifted value. + public static byte ShiftRight(byte b, int shift) + { + return (byte)((b >> shift) & 0xff); + } + + /// + /// Shift 16-bits to the left. + /// + /// The word. + /// How many bits to shift. + /// The shifted value. + public static ushort ShiftLeft(ushort w, int shift) + { + return (ushort)((w << shift) & 0xffff); + } + + /// + /// Shift 16-bits to the right. + /// + /// The word. + /// How many bits to shift. + /// The shifted value. + 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 +{ + /// + /// Defines an 8-bit CPU. + /// + public interface ICpu + { + /// + /// Initialise the CPU to give it access to memory and devices. + /// + /// The memory to access. + /// The devices to access. + /// The clock to use. + void Initialise(IMemory memory, IDevice device, Clock clock); + + /// + /// Resets the CPU to its initial state. + /// + void Reset(); + + /// + /// Runs the next instruction. + /// + void Step(); + + /// + /// Runs the CPU until the next frame flyback. + /// + void Run(); + + /// + /// Generates a maskable interrupt to the CPU. + /// + /// Optional value from an interrupting device. May be ignored depending on the CPU type. + void MaskableInterrupt(byte value); + + /// + /// Generates a non-maskable interrupt to the CPU. + /// + /// Optional value from an interrupting device. May be ignored depending on the CPU type. + 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 +{ + /// + /// 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. + /// + public interface IDevice + { + /// + /// Read from a device. + /// + /// The address of the device. + /// The byte returned from the device. + byte Read(ushort device); + + /// + /// Write to a device. + /// + /// The address of the device. + /// The value to write to the device. + 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 +{ + /// + /// Defines an interface for memory. + /// + public interface IMemory + { + /// + /// Reads a byte at a given address. + /// + /// The address to read. + /// The value at that address. + byte Read(ushort address); + + /// + /// Writes a byte at a given address. + /// + /// The address to write to. + /// The value to write. + 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 +{ + /// + /// Provides a common 8-bit register pattern, which is two 8-bit registers rolled into one. + /// While this could be done at runtime using 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. + /// + [StructLayout(LayoutKind.Explicit)] + public struct Register16 + { +#if TARGET_IS_BIGENDIAN + + /// + /// The 16-bit register. + /// + [FieldOffset(0)] + public ushort reg; + + /// + /// The high 8-bits of the register. + /// + [FieldOffset(0)] + public byte high; + + /// + /// The low 8-bits of the register. + /// + [FieldOffset(1)] + public byte low; + +#else + + /// + /// The 16-bit register. + /// + [FieldOffset(0)] + public ushort reg; + + /// + /// The low 8-bits of the register. + /// + [FieldOffset(0)] + public byte low; + + /// + /// The high 8-bits of the register. + /// + [FieldOffset(1)] + public byte high; + +#endif + + /// + /// Public constructor. This is provided to stop the compiler complaining when you use the struct as it + /// doesn't realise that setting will also set and . + /// + /// The 16-bit value to assign. + public Register16(ushort val) + { + high = 0; + low = 0; + reg = val; + } + + /// + /// Used to verify the compile-time setting at runtime. + /// + 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"); + } + } + } +} -- cgit v1.2.3