199 lines
7.0 KiB
C#
199 lines
7.0 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
using Core.Cpu;
|
|
using Core.Io;
|
|
using Core.Memory;
|
|
|
|
namespace Desktop
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private Z80 _cpu = null!;
|
|
private MemoryBus _memoryBus = null!;
|
|
private IO_Bus _simpleIoBus = null!;
|
|
private int _ulaFrameCount = 0;
|
|
|
|
// The 16 physical colors of the ZX Spectrum (ARGB format)
|
|
private readonly int[] SpectrumColors = new int[]
|
|
{
|
|
// Normal Colors (Bright = 0)
|
|
unchecked((int)0xFF000000), // 0: Black
|
|
unchecked((int)0xFF0000D7), // 1: Blue
|
|
unchecked((int)0xFFD70000), // 2: Red
|
|
unchecked((int)0xFFD700D7), // 3: Magenta
|
|
unchecked((int)0xFF00D700), // 4: Green
|
|
unchecked((int)0xFF00D7D7), // 5: Cyan
|
|
unchecked((int)0xFFD7D700), // 6: Yellow
|
|
unchecked((int)0xFFD7D7D7), // 7: White
|
|
|
|
// Bright Colors (Bright = 1)
|
|
unchecked((int)0xFF000000), // 8: Bright Black
|
|
unchecked((int)0xFF0000FF), // 9: Bright Blue
|
|
unchecked((int)0xFFFF0000), // 10: Bright Red
|
|
unchecked((int)0xFFFF00FF), // 11: Bright Magenta
|
|
unchecked((int)0xFF00FF00), // 12: Bright Green
|
|
unchecked((int)0xFF00FFFF), // 13: Bright Cyan
|
|
unchecked((int)0xFFFFFF00), // 14: Bright Yellow
|
|
unchecked((int)0xFFFFFFFF) // 15: Bright White
|
|
};
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
InitializeEmulator();
|
|
}
|
|
|
|
private void InitializeEmulator()
|
|
{
|
|
try
|
|
{
|
|
_memoryBus = new MemoryBus();
|
|
_simpleIoBus = new IO_Bus();
|
|
|
|
_memoryBus.CrapRAMData();
|
|
byte[] romData = RomLoader.Load("48.rom");
|
|
_memoryBus.LoadRom(romData);
|
|
|
|
_cpu = new Z80(_memoryBus, _simpleIoBus);
|
|
|
|
// Pass 'this' so the DebuggerForm can talk back to this main window
|
|
DebuggerForm debugger = new DebuggerForm(_cpu, _memoryBus, this);
|
|
debugger.Show();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Failed to initialize emulator:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
// Public so the Debugger's background thread can call it 50 times a second
|
|
public void RenderScreen()
|
|
{
|
|
_ulaFrameCount++;
|
|
// The Spectrum flashes every 16 frames.
|
|
// This boolean flips between true and false every 16 frames.
|
|
bool invertFlashPhase = (_ulaFrameCount % 32) >= 16;
|
|
int[] pixelData = new int[256 * 192];
|
|
|
|
// Loop through the 6144 bytes of Pixel RAM
|
|
for (int offset = 0; offset < 6144; offset++)
|
|
{
|
|
ushort address = (ushort)(0x4000 + offset);
|
|
byte pixels = _memoryBus.Read(address);
|
|
|
|
// Unwind the Sinclair interlace
|
|
int y = ((offset & 0x0700) >> 8) |
|
|
((offset & 0x00E0) >> 2) |
|
|
((offset & 0x1800) >> 5);
|
|
|
|
int x = (offset & 0x001F) * 8;
|
|
|
|
// Fetch Color Attributes
|
|
int attrRow = y / 8;
|
|
int attrCol = x / 8;
|
|
ushort attrAddress = (ushort)(0x5800 + (attrRow * 32) + attrCol);
|
|
byte attr = _memoryBus.Read(attrAddress);
|
|
|
|
int ink = attr & 0x07;
|
|
int paper = (attr >> 3) & 0x07;
|
|
int brightOffset = (attr & 0x40) != 0 ? 8 : 0;
|
|
bool isFlashSet = (attr & 0x80) != 0;
|
|
int inkColor = SpectrumColors[ink + brightOffset];
|
|
int paperColor = SpectrumColors[paper + brightOffset];
|
|
|
|
if (isFlashSet && invertFlashPhase)
|
|
{
|
|
// Swap the ink and paper colors for this character block!
|
|
int temp = inkColor;
|
|
inkColor = paperColor;
|
|
paperColor = temp;
|
|
}
|
|
|
|
// Draw the 8 pixels
|
|
for (int bit = 0; bit < 8; bit++)
|
|
{
|
|
bool isPixelSet = (pixels & (1 << (7 - bit))) != 0;
|
|
pixelData[(y * 256) + (x + bit)] = isPixelSet ? inkColor : paperColor;
|
|
}
|
|
}
|
|
|
|
// Blast it to the PictureBox
|
|
Bitmap bmp = new Bitmap(256, 192, PixelFormat.Format32bppArgb);
|
|
BitmapData bmpData = bmp.LockBits(
|
|
new Rectangle(0, 0, 256, 192),
|
|
ImageLockMode.WriteOnly,
|
|
bmp.PixelFormat);
|
|
|
|
Marshal.Copy(pixelData, 0, bmpData.Scan0, pixelData.Length);
|
|
bmp.UnlockBits(bmpData);
|
|
|
|
if (picScreen.Image != null) picScreen.Image.Dispose();
|
|
picScreen.Image = bmp;
|
|
}
|
|
|
|
// Helper method to update the IO Bus state
|
|
private void UpdateMatrix(int row, int col, bool isPressed)
|
|
{
|
|
if (isPressed)
|
|
{
|
|
// Clear the bit to 0 (Active-Low = Pressed)
|
|
_simpleIoBus.KeyboardRows[row] &= (byte)~(1 << col);
|
|
}
|
|
else
|
|
{
|
|
// Set the bit back to 1 (Unpressed)
|
|
_simpleIoBus.KeyboardRows[row] |= (byte)(1 << col);
|
|
}
|
|
}
|
|
|
|
// Hook this to Form1's KeyDown event
|
|
protected override void OnKeyDown(KeyEventArgs e)
|
|
{
|
|
HandleKey(e.KeyCode, true);
|
|
base.OnKeyDown(e);
|
|
}
|
|
|
|
// Hook this to Form1's KeyUp event
|
|
protected override void OnKeyUp(KeyEventArgs e)
|
|
{
|
|
HandleKey(e.KeyCode, false);
|
|
base.OnKeyUp(e);
|
|
}
|
|
|
|
private void HandleKey(Keys key, bool isPressed)
|
|
{
|
|
switch (key)
|
|
{
|
|
// Row 6: ENTER, L, K, J, H
|
|
case Keys.Enter: UpdateMatrix(6, 0, isPressed); break;
|
|
case Keys.L: UpdateMatrix(6, 1, isPressed); break;
|
|
case Keys.K: UpdateMatrix(6, 2, isPressed); break;
|
|
case Keys.J: UpdateMatrix(6, 3, isPressed); break;
|
|
case Keys.H: UpdateMatrix(6, 4, isPressed); break;
|
|
|
|
// Row 7: SPACE, SYM SHIFT, M, N, B
|
|
case Keys.Space: UpdateMatrix(7, 0, isPressed); break;
|
|
case Keys.M: UpdateMatrix(7, 2, isPressed); break;
|
|
case Keys.N: UpdateMatrix(7, 3, isPressed); break;
|
|
case Keys.B: UpdateMatrix(7, 4, isPressed); break;
|
|
|
|
// Row 1: A, S, D, F, G
|
|
case Keys.A: UpdateMatrix(1, 0, isPressed); break;
|
|
case Keys.S: UpdateMatrix(1, 1, isPressed); break;
|
|
case Keys.D: UpdateMatrix(1, 2, isPressed); break;
|
|
case Keys.F: UpdateMatrix(1, 3, isPressed); break;
|
|
case Keys.G: UpdateMatrix(1, 4, isPressed); break;
|
|
|
|
// Map the rest of the alphabet and numbers here as you need them!
|
|
}
|
|
}
|
|
|
|
private void Form1_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
} |