Some minor changes and playing about

This commit is contained in:
2026-04-16 14:40:05 +01:00
parent 92625498bf
commit 968141056b
9 changed files with 135 additions and 46 deletions

View File

@@ -1,5 +1,6 @@
using System;
using Core.Interfaces;
using Core.Io;
namespace Core.Cpu
{
@@ -38,12 +39,12 @@ namespace Core.Cpu
// The Memory Bus
private readonly IMemory _memory;
private readonly IIoBus _ioBus;
private readonly IO_Bus _simpleIoBus;
public Z80(IMemory memory, IIoBus ioBus)
public Z80(IMemory memory, IO_Bus ioBus)
{
_memory = memory;
_ioBus = ioBus;
_simpleIoBus = ioBus;
Reset();
}
@@ -122,15 +123,7 @@ namespace Core.Cpu
// Placeholder for your hardware I/O
private byte ReadPort(ushort portAddress)
{
// If the port is 0xFE, the ROM is asking for keyboard/tape/ULA data!
// For now, we will return 0xFF (meaning "No keys are currently pressed")
if ((portAddress & 0xFF) == 0xFE)
{
return 0xFF;
}
// Default floating bus return
return 0xFF;
return _simpleIoBus.ReadPort(portAddress);
}
public int Step()
@@ -1086,7 +1079,7 @@ namespace Core.Cpu
// The Z80 puts 'A' on the top 8 bits, and 'n' on the bottom 8 bits of the port address
ushort portAddress = (ushort)((AF.High << 8) | portOffset);
_ioBus.Write(portAddress, AF.High);
_simpleIoBus.WritePort(portAddress, AF.High);
return 11;
case 0xd5: //push bc

43
Core/Io/IO_Bus.cs Normal file
View File

@@ -0,0 +1,43 @@
using System.Diagnostics;
using Core.Interfaces;
namespace Core.Io
{
public class IO_Bus
{
// 8 rows representing the Spectrum keyboard matrix. Default to 0xFF (unpressed).
public byte[] KeyboardRows = new byte[8] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
public byte ReadPort(ushort portAddress)
{
// The Spectrum ULA responds to any even port address (where the lowest bit is 0)
if ((portAddress & 0x01) == 0)
{
byte highByte = (byte)(portAddress >> 8); // The B register!
byte result = 0xFF; // Start assuming no keys are pressed
// The ROM pulls a specific bit low (0) in the high byte to request a row.
// Sometimes it pulls multiple bits low to scan multiple rows at once, so we AND the results.
if ((highByte & 0x01) == 0) result &= KeyboardRows[0]; // 0xFE: CAPS, Z, X, C, V
if ((highByte & 0x02) == 0) result &= KeyboardRows[1]; // 0xFD: A, S, D, F, G
if ((highByte & 0x04) == 0) result &= KeyboardRows[2]; // 0xFB: Q, W, E, R, T
if ((highByte & 0x08) == 0) result &= KeyboardRows[3]; // 0xF7: 1, 2, 3, 4, 5
if ((highByte & 0x10) == 0) result &= KeyboardRows[4]; // 0xEF: 0, 9, 8, 7, 6
if ((highByte & 0x20) == 0) result &= KeyboardRows[5]; // 0xDF: P, O, I, U, Y
if ((highByte & 0x40) == 0) result &= KeyboardRows[6]; // 0xBF: ENTER, L, K, J, H
if ((highByte & 0x80) == 0) result &= KeyboardRows[7]; // 0x7F: SPACE, SYM, M, N, B
// The top 3 bits (5, 6, 7) are unused by the keyboard and usually return 1 on a real Spectrum
return (byte)(result | 0xE0);
}
// Return 0xFF for unhandled ports
return 0xFF;
}
public void WritePort(ushort portAddress, byte portValue)
{
}
}
}

View File

@@ -1,20 +0,0 @@
using System.Diagnostics;
using Core.Interfaces;
namespace Core.Io
{
public class SimpleIoBus : IIoBus
{
public byte Read(ushort port)
{
// If the CPU reads an unconnected port, the Z80 usually sees 0xFF
return 0xFF;
}
public void Write(ushort port, byte value)
{
// For now, let's just log it to the Visual Studio Output window
Debug.WriteLine($"Hardware I/O Write -> Port: 0x{port:X4}, Value: 0x{value:X2}");
}
}
}

View File

@@ -35,12 +35,20 @@ namespace Core.Memory
public void CrapRAMData()
{
Random random = new Random();
for (int i = 0x4000; i < 0x5AFF; i++)
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)
{