Initial c64 memory mapping

This commit is contained in:
KallDrexx
2025-12-29 15:59:42 -05:00
parent 4108b821ce
commit f34c40bffa
6 changed files with 191 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Dotnet6502.Common\Dotnet6502.Common.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,43 @@
using Dotnet6502.Common.Hardware;
namespace Dotnet6502.C64.Hardware;
/// <summary>
/// A memory mapped device that can be swapped out to different memory devices
/// </summary>
public class BankableMemoryDevice : IMemoryDevice
{
private IMemoryDevice _currentMemoryMappedDevice;
public uint Size => _currentMemoryMappedDevice.Size;
public ReadOnlyMemory<byte>? RawBlockFromZero => _currentMemoryMappedDevice.RawBlockFromZero;
public BankableMemoryDevice(IMemoryDevice initialMappedDevice)
{
_currentMemoryMappedDevice = initialMappedDevice;
}
public void SwapTo(IMemoryDevice memoryDevice)
{
if (memoryDevice.Size != _currentMemoryMappedDevice.Size)
{
var message = $"Attempted to swap memory device from one with {_currentMemoryMappedDevice.Size} " +
$"to one with {memoryDevice.Size}. Sizes must match";
throw new ArgumentException(message);
}
_currentMemoryMappedDevice = memoryDevice;
}
public void Write(ushort offset, byte value)
{
_currentMemoryMappedDevice.Write(offset, value);
}
public byte Read(ushort offset)
{
return _currentMemoryMappedDevice.Read(offset);
}
}
@@ -0,0 +1,20 @@
using Dotnet6502.Common.Hardware;
namespace Dotnet6502.C64.Hardware;
public class IoMemoryArea : IMemoryDevice
{
public uint Size => 0xdfff - 0xd000 + 1;
public ReadOnlyMemory<byte>? RawBlockFromZero { get; }
public void Write(ushort offset, byte value)
{
throw new NotImplementedException();
}
public byte Read(ushort offset)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,92 @@
using Dotnet6502.Common.Hardware;
namespace Dotnet6502.C64.Hardware;
/// <summary>
/// The PLA was a chip the C64 used to swap out RAM vs ROM vs I/O the memory mapping. This class emulates that behavior
/// by creating memory devices for each memory mappable section.
/// </summary>
public class ProgrammableLogicArray : IMemoryDevice
{
private readonly byte[] _cpuIoPort = [0b00101111, 0b00110111];
private BankableMemoryDevice _a000ToBfffDevice;
private BankableMemoryDevice _d000ToDfffDevice;
private BankableMemoryDevice _e000ToFfffDevice;
private readonly BasicRamMemoryDevice _basicRom;
private readonly BasicRamMemoryDevice _kernelRom;
private readonly BasicRamMemoryDevice _ramA000ToBfff;
private readonly BasicRamMemoryDevice _ramD000ToDfff;
private readonly BasicRamMemoryDevice _ramE000ToFfff;
private readonly BasicRamMemoryDevice _characterRom;
private readonly IoMemoryArea _ioMemoryArea;
public ProgrammableLogicArray()
{
_basicRom = new BasicRamMemoryDevice(0xBFFF - 0xA000 + 1);
_kernelRom = new BasicRamMemoryDevice(0xFFFF - 0xE000 + 1);
_ramA000ToBfff = new BasicRamMemoryDevice(0xBFFF - 0xA000 + 1);
_ramD000ToDfff = new BasicRamMemoryDevice(0xDFFF - 0xD000 + 1);
_ramE000ToFfff = new BasicRamMemoryDevice(0xFFFF - 0xE000 + 1);
_characterRom = new BasicRamMemoryDevice(0xDFFF - 0xD000 + 1);
_ioMemoryArea = new IoMemoryArea();
_a000ToBfffDevice = new BankableMemoryDevice(_ramA000ToBfff);
_d000ToDfffDevice = new BankableMemoryDevice(_ramD000ToDfff);
_e000ToFfffDevice = new BankableMemoryDevice(_ramE000ToFfff);
UpdateDevices();
}
public void AttachToBus(MemoryBus memoryBus)
{
memoryBus.Attach(_a000ToBfffDevice, 0xa000);
memoryBus.Attach(_d000ToDfffDevice, 0xd000);
memoryBus.Attach(_e000ToFfffDevice, 0xe000);
}
private void UpdateDevices()
{
var section = _cpuIoPort[1] & 0b111;
if ((section & 0b11) == 0)
{
// RAM visible in all 3 sections
_a000ToBfffDevice.SwapTo(_ramA000ToBfff);
_d000ToDfffDevice.SwapTo(_ramD000ToDfff);
_e000ToFfffDevice.SwapTo(_ramE000ToFfff);
return;
}
_d000ToDfffDevice.SwapTo((section & 0b100) > 0
? _ioMemoryArea
: _characterRom);
_e000ToFfffDevice.SwapTo((section & 0b011) == 0b01
? _ramE000ToFfff
: _kernelRom);
_a000ToBfffDevice.SwapTo((section & 0b011) == 0b11
? _basicRom
: _ramA000ToBfff);
}
public uint Size => (uint) _cpuIoPort.Length;
public ReadOnlyMemory<byte>? RawBlockFromZero => _cpuIoPort.AsMemory();
public void Write(ushort offset, byte value)
{
// TODO: Add port direction functionality
_cpuIoPort[offset] = value;
if (offset == 1)
{
UpdateDevices();
}
}
public byte Read(ushort offset)
{
return _cpuIoPort[offset];
}
}
+16
View File
@@ -0,0 +1,16 @@
using Dotnet6502.C64.Hardware;
using Dotnet6502.Common.Hardware;
var pla = new ProgrammableLogicArray();
var memoryBus = new MemoryBus();
memoryBus.Attach(pla, 0x0000);
pla.AttachToBus(memoryBus);
// fill in the rest with ram
memoryBus.Attach(new BasicRamMemoryDevice(0x7fff - 0x0002 + 1), 0x0002);
memoryBus.Attach(new BasicRamMemoryDevice(0xcfff - 0xc000 + 1), 0xc000);
// TODO: Add cartridge rom low swapping to this region
memoryBus.Attach(new BasicRamMemoryDevice(0x9fff - 0x8000 + 1), 0x8000);
+6
View File
@@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotnet6502.Nes", "Dotnet650
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotnet6502.ComprehensiveTestRunner", "Dotnet6502.ComprehensiveTestRunner\Dotnet6502.ComprehensiveTestRunner.csproj", "{29E009E1-BE2A-4A05-A30D-7015DBC780E0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotnet6502.C64", "Dotnet6502.C64\Dotnet6502.C64.csproj", "{081295C7-75C9-4B85-BB3F-6E97A26429E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -42,5 +44,9 @@ Global
{29E009E1-BE2A-4A05-A30D-7015DBC780E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29E009E1-BE2A-4A05-A30D-7015DBC780E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29E009E1-BE2A-4A05-A30D-7015DBC780E0}.Release|Any CPU.Build.0 = Release|Any CPU
{081295C7-75C9-4B85-BB3F-6E97A26429E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{081295C7-75C9-4B85-BB3F-6E97A26429E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{081295C7-75C9-4B85-BB3F-6E97A26429E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{081295C7-75C9-4B85-BB3F-6E97A26429E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal