diff --git a/illNES/illNES.CPU/BaseMemoryInterface.cs b/illNES/illNES.CPU/BaseMemoryInterface.cs new file mode 100644 index 0000000..72a0b94 --- /dev/null +++ b/illNES/illNES.CPU/BaseMemoryInterface.cs @@ -0,0 +1,45 @@ +namespace illNES.CPU +{ + public class BaseMemoryInterface : IMemoryInterface + { + /// + /// Always provide 64k of RAM, allow AccessMemory() to handle mapping and what is actually used. + /// This represents what the CPU is capable of accessing (64k) while AccessMemory() can choose to only use + /// for example, the range 0x0 - 0x7ff, which would provide 2k of RAM like in the NES. + /// + protected byte[] ram = new byte[0xffff]; + + /// + /// Accesses memory at a given address, and writes a value if provided. + /// Overriding this method allows for changing the way in which RAM is accessed. + /// + /// The address to access + /// An optional value to write + /// The value at the memory address when the method returns + protected virtual byte AccessMemory(ushort address, byte? value = null) + { + //This provides direct access to the full 64k of RAM + if (value.HasValue) ram[address] = value.Value; + return ram[address]; + } + + public string DumpRam(ushort start, byte offset) + { + throw new System.NotImplementedException(); + } + + public void FileDump(string filePath, ushort address) + { + throw new System.NotImplementedException(); + } + + public byte Read(ushort address) + => AccessMemory(address); + + public ushort ReadWord(ushort address) + => (ushort)(AccessMemory((ushort)(address + 1)) << 8 | AccessMemory(address)); + + public void Write(ushort address, byte value) + => AccessMemory(address, value); + } +} \ No newline at end of file diff --git a/illNES/illNES.CPU/Class1.cs b/illNES/illNES.CPU/Class1.cs deleted file mode 100644 index 33de113..0000000 --- a/illNES/illNES.CPU/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace illNES.CPU -{ - public class Class1 - { - } -} diff --git a/illNES/illNES.CPU/IMemoryInterface.cs b/illNES/illNES.CPU/IMemoryInterface.cs new file mode 100644 index 0000000..70d27c4 --- /dev/null +++ b/illNES/illNES.CPU/IMemoryInterface.cs @@ -0,0 +1,52 @@ +namespace illNES.CPU { + + /// + /// The public API for the CPU to interface with RAM, 64k at a time ;) + /// + public interface IMemoryInterface { + + /// + /// Read a byte value at a 16bit memory address + /// + /// The memory address to read from + /// The byte stored at the requested address + byte Read(ushort address); + + /// + /// Read a 16 bit value comprised of the byte at the specified 16-bit address, + /// and the next consecutive address. + /// Note that memory wraps within the 16-bit address range, + /// so the word read from `0xffff` will be the byte from `0xffff` and the byte from `0x0000`. + /// + /// MOS6502 is little endian. + /// + /// The memory address to start reading at + /// + /// The 16 bit value comprised of the byte stored at `address`, + /// and the byte stored at `address+1` + /// + ushort ReadWord(ushort address); + + /// + /// Write a byte value to the specified 16bit memory address. + /// + /// The memory address to write to + /// The byte value to write + void Write(ushort address, byte value); + + /// + /// Dump up to 256 contiguous values from memory, starting at a given address. + /// + /// The memory address to start from + /// The offset relative to `start` to cease dumping at + /// + string DumpRam(ushort start, byte offset); + + /// + /// Dump the contents of a binary file into memory, starting at the specified address. + /// + /// Path to the file + /// The memory address to start the dump at + void FileDump(string filePath, ushort address); + } +} \ No newline at end of file