Convert "memory" concept to an interface

This commit is contained in:
Adrian Conlon committed 2026-10-10 09:08:56 +01:00
1 parent f2bf17fc2a
commit d12f29f253
6 files changed
+49 -47

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
// <copyright file="Memory.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace EightBit
{
public interface IMemory
{
abstract int Size
{
get;
}
byte Peek(ushort address);
void Poke(ushort address, byte value);
ref byte Reference(ushort address);
int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1);
int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1);
int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1);
}
}
-26
View File
@@ -1,26 +0,0 @@
// <copyright file="Memory.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace EightBit
{
public abstract class Memory
{
public abstract int Size
{
get;
}
public abstract byte Peek(ushort address);
public virtual ref byte Reference(ushort address) => throw new NotImplementedException();
public abstract int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1);
public abstract int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1);
public abstract int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1);
protected abstract void Poke(ushort address, byte value);
}
}
+4 -4
View File
@@ -6,7 +6,7 @@ namespace EightBit
{
public sealed class MemoryMapping
{
public MemoryMapping(Memory memory, ushort begin, ushort mask, AccessLevel access)
public MemoryMapping(IMemory memory, ushort begin, ushort mask, AccessLevel access)
{
this._memory = memory;
this._begin = begin;
@@ -20,18 +20,18 @@ namespace EightBit
}
}
public MemoryMapping(Memory memory, ushort begin, Mask mask, AccessLevel access)
public MemoryMapping(IMemory memory, ushort begin, Mask mask, AccessLevel access)
: this(memory, begin, (ushort)mask, access)
{
}
private readonly ushort[] _offsets = new ushort[ushort.MaxValue + 1];
private readonly Memory _memory;
private readonly IMemory _memory;
private readonly ushort _begin;
private readonly ushort _mask;
private readonly AccessLevel _access;
public Memory Memory => this._memory;
public IMemory Memory => this._memory;
public ushort Begin => this._begin;
-2
View File
@@ -12,7 +12,5 @@ namespace EightBit
}
public sealed override ref byte Reference(ushort address) => ref this.Bytes()[address];
public new void Poke(ushort address, byte value) => base.Poke(address, value);
}
}
+10 -8
View File
@@ -6,11 +6,11 @@ namespace EightBit
{
using System.Diagnostics;
public class Rom(int size = 0) : Memory
public class Rom(int size = 0) : IMemory
{
private byte[] _bytes = new byte[size];
public override int Size => this._bytes.Length;
public int Size => this._bytes.Length;
public static int Load(Stream file, ref byte[] output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1)
{
@@ -54,19 +54,19 @@ namespace EightBit
return Load(file, ref output, writeOffset, readOffset, limit, maximumSize);
}
public override int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1)
public int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1)
{
var maximumSize = this.Size - writeOffset;
return Load(file, ref this.Bytes(), writeOffset, readOffset, limit, maximumSize);
}
public override int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1)
public int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1)
{
var maximumSize = this.Size - writeOffset;
return Load(path, ref this.Bytes(), writeOffset, readOffset, limit, maximumSize);
}
public override int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1)
public int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1)
{
ArgumentNullException.ThrowIfNull(from);
@@ -88,10 +88,12 @@ namespace EightBit
return limit;
}
public override byte Peek(ushort address) => this.Bytes()[address];
public byte Peek(ushort address) => this.Bytes()[address];
public void Poke(ushort address, byte value) => this.Bytes()[address] = value;
public virtual ref byte Reference(ushort address) => throw new NotImplementedException();
protected ref byte[] Bytes() => ref this._bytes;
protected override void Poke(ushort address, byte value) => this.Bytes()[address] = value;
}
}
+9 -7
View File
@@ -4,21 +4,23 @@
namespace EightBit
{
public class UnusedMemory(int size, byte unchanging) : Memory
public class UnusedMemory(int size, byte unchanging) : IMemory
{
private readonly int _size = size;
private readonly byte _unchanging = unchanging;
public override int Size => this._size;
public int Size => this._size;
public override int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public override int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public override int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1) => throw new NotImplementedException();
public override byte Peek(ushort address) => this._unchanging;
public byte Peek(ushort address) => this._unchanging;
protected override void Poke(ushort address, byte value) => throw new NotImplementedException();
public void Poke(ushort address, byte value) => throw new NotImplementedException();
public ref byte Reference(ushort address) => throw new NotImplementedException();
}
}