Files
ZXSpectrum48K/Core/Memory/MemoryBus.cs

65 lines
1.7 KiB
C#

using System;
using Core.Interfaces;
namespace Core.Memory
{
public class MemoryBus : IMemory
{
// The flat 64KB memory space
private readonly byte[] _memory = new byte[0x10000];
public byte Read(ushort address)
{
return _memory[address];
}
public void Write(ushort address, byte value)
{
/* ZX Spectrum 48K Memory Map:
* 0x0000 - 0x3FFF: ROM (16KB) -> Cannot write here!
* 0x4000 - 0x57FF: Display File (Screen Pixels)
* 0x5800 - 0x5AFF: Color Attributes
* 0x5B00 - 0xFFFF: General Purpose RAM
*/
if (address < 0x4000)
{
// Cannot write to ROM - Do nothing - maybe throw an exception
return;
}
_memory[address] = value;
}
//Put some initial random data into RAM for authenticity
public void CrapRAMData()
{
Random random = new Random();
for (int i = 0x4000; i < 0xFFFF; i++)
{
_memory[i] = (byte)random.Next(0x00, 0xFF);
}
}
public void CleanRAMData()
{
for (int i = 0x4000; i < 0xFFFF; i++)
{
_memory[i] = 0x00;
}
}
// Load the ROM file
public void LoadRom(byte[] romData)
{
if (romData.Length > 0x4000)
{
throw new ArgumentException("ROM file exceeds the 16KB capacity of Bank 0.");
}
// Copy the ROM
Array.Copy(romData, 0, _memory, 0, romData.Length);
}
}
}