Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/base memory interface #11

Merged
merged 2 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions illNES/illNES.CPU/BaseMemoryInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace illNES.CPU
{
public class BaseMemoryInterface : IMemoryInterface
{
/// <summary>
/// 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.
/// </summary>
protected byte[] ram = new byte[0xffff];

/// <summary>
/// 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.
/// </summary>
/// <param name="address">The address to access</param>
/// <param name="value">An optional value to write</param>
/// <returns>The value at the memory address when the method returns</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);
}
}
8 changes: 0 additions & 8 deletions illNES/illNES.CPU/Class1.cs

This file was deleted.

52 changes: 52 additions & 0 deletions illNES/illNES.CPU/IMemoryInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace illNES.CPU {

/// <summary>
/// The public API for the CPU to interface with RAM, 64k at a time ;)
/// </summary>
public interface IMemoryInterface {

/// <summary>
/// Read a byte value at a 16bit memory address
/// </summary>
/// <param name="address">The memory address to read from</param>
/// <returns>The byte stored at the requested address</returns>
byte Read(ushort address);

/// <summary>
/// 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.
/// </summary>
/// <param name="address">The memory address to start reading at</param>
/// <returns>
/// The 16 bit value comprised of the byte stored at `address`,
/// and the byte stored at `address+1`
/// </returns>
ushort ReadWord(ushort address);

/// <summary>
/// Write a byte value to the specified 16bit memory address.
/// </summary>
/// <param name="address">The memory address to write to</param>
/// <param name="value">The byte value to write</param>
void Write(ushort address, byte value);

/// <summary>
/// Dump up to 256 contiguous values from memory, starting at a given address.
/// </summary>
/// <param name="start">The memory address to start from</param>
/// <param name="offset">The offset relative to `start` to cease dumping at</param>
/// <returns></returns>
string DumpRam(ushort start, byte offset);

/// <summary>
/// Dump the contents of a binary file into memory, starting at the specified address.
/// </summary>
/// <param name="filePath">Path to the file</param>
/// <param name="address">The memory address to start the dump at</param>
void FileDump(string filePath, ushort address);
}
}