Added double buffering

This commit is contained in:
2026-04-24 17:24:53 +01:00
parent dce4240842
commit d7fd5b6ad8
14 changed files with 163 additions and 63 deletions

View File

@@ -12,6 +12,7 @@ namespace Core.Io
public const int ScreenWidth = 320;
public const int ScreenHeight = 256; // Perfectly cropped size
public int[] FrameBuffer { get; private set; }
public int[] FrontBuffer { get; private set; } = new int[ScreenWidth * ScreenHeight];
private int _ulaFrameCount = 0;
@@ -124,5 +125,10 @@ namespace Core.Io
FrameBuffer[rowStartIndex + b] = currentBorderColor;
}
}
public void CommitFrame()
{
Array.Copy(FrameBuffer, FrontBuffer, FrameBuffer.Length);
}
}
}

Binary file not shown.

View File

@@ -38,13 +38,10 @@
lblFlags = new Label();
lblTStates = new Label();
txtMemoryStart = new TextBox();
btnStep = new Button();
btnRefreshMemory = new Button();
txtMemoryView = new RichTextBox();
lstDisassembly = new ListBox();
lstStack = new ListBox();
btnExit = new Button();
saveFileDialog1 = new SaveFileDialog();
label1 = new Label();
txtBreakpoint = new TextBox();
label2 = new Label();
@@ -151,17 +148,6 @@
txtMemoryStart.TextAlign = HorizontalAlignment.Center;
txtMemoryStart.TextChanged += btnRefreshMemory_Click;
//
// btnStep
//
btnStep.Location = new Point(6, 418);
btnStep.Margin = new Padding(2);
btnStep.Name = "btnStep";
btnStep.Size = new Size(90, 27);
btnStep.TabIndex = 12;
btnStep.Text = "Step";
btnStep.UseVisualStyleBackColor = true;
btnStep.Click += btnStep_Click;
//
// btnRefreshMemory
//
btnRefreshMemory.Location = new Point(425, 21);
@@ -200,16 +186,6 @@
lstStack.Size = new Size(130, 264);
lstStack.TabIndex = 17;
//
// btnExit
//
btnExit.Location = new Point(815, 416);
btnExit.Margin = new Padding(2);
btnExit.Name = "btnExit";
btnExit.Size = new Size(90, 27);
btnExit.TabIndex = 18;
btnExit.Text = "Exit";
btnExit.UseVisualStyleBackColor = true;
//
// label1
//
label1.AutoSize = true;
@@ -276,7 +252,7 @@
// lblIE
//
lblIE.AutoSize = true;
lblIE.Location = new Point(200, 345);
lblIE.Location = new Point(88, 397);
lblIE.Name = "lblIE";
lblIE.Size = new Size(109, 20);
lblIE.TabIndex = 26;
@@ -346,12 +322,10 @@
Controls.Add(label2);
Controls.Add(txtBreakpoint);
Controls.Add(label1);
Controls.Add(btnExit);
Controls.Add(lstStack);
Controls.Add(lstDisassembly);
Controls.Add(txtMemoryView);
Controls.Add(btnRefreshMemory);
Controls.Add(btnStep);
Controls.Add(txtMemoryStart);
Controls.Add(lblTStates);
Controls.Add(lblFlags);
@@ -379,13 +353,10 @@
private Label lblFlags;
private Label lblTStates;
private TextBox txtMemoryStart;
private Button btnStep;
private Button btnRefreshMemory;
private RichTextBox txtMemoryView;
private ListBox lstStack;
private Button btnExit;
public ListBox lstDisassembly;
private SaveFileDialog saveFileDialog1;
private Label label1;
private TextBox txtBreakpoint;
private Label label2;

View File

@@ -117,9 +117,6 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="uiUpdateTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>182, 17</value>
</metadata>

View File

@@ -8,6 +8,44 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Remove="48.rom" />
<None Remove="ROMS\Snapshot\ChuckieEgg.sna" />
<None Remove="ROMS\Snapshot\GHILL48K.sna" />
<None Remove="ROMS\Snapshot\manic.sna" />
<None Remove="ROMS\TAP\Chuckie Egg.tap" />
<None Remove="ROMS\TAP\GHILL48K.TAP" />
<None Remove="ROMS\TAP\MANIC.TAP" />
<None Remove="ROMS\TAP\zexall.tap" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="48.rom">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\Snapshot\ChuckieEgg.sna">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\Snapshot\GHILL48K.sna">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\Snapshot\manic.sna">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\TAP\Chuckie Egg.tap">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\TAP\GHILL48K.TAP">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\TAP\MANIC.TAP">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="ROMS\TAP\zexall.tap">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NAudio" Version="2.3.0" />
</ItemGroup>
@@ -16,10 +54,4 @@
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="48.rom">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -43,6 +43,9 @@
resetToolStripMenuItem1 = new ToolStripMenuItem();
optionsToolStripMenuItem = new ToolStripMenuItem();
HighSpeedToolStripMenuItem = new ToolStripMenuItem();
includedToolStripMenuItem = new ToolStripMenuItem();
tAPToolStripMenuItem1 = new ToolStripMenuItem();
sNAToolStripMenuItem1 = new ToolStripMenuItem();
menuStrip1.SuspendLayout();
SuspendLayout();
//
@@ -66,7 +69,7 @@
//
// openToolStripMenuItem
//
openToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { tAPToolStripMenuItem, sNAToolStripMenuItem });
openToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { includedToolStripMenuItem, tAPToolStripMenuItem, sNAToolStripMenuItem });
openToolStripMenuItem.Name = "openToolStripMenuItem";
openToolStripMenuItem.Size = new Size(128, 26);
openToolStripMenuItem.Text = "Open";
@@ -74,14 +77,14 @@
// tAPToolStripMenuItem
//
tAPToolStripMenuItem.Name = "tAPToolStripMenuItem";
tAPToolStripMenuItem.Size = new Size(121, 26);
tAPToolStripMenuItem.Size = new Size(149, 26);
tAPToolStripMenuItem.Text = "TAP";
tAPToolStripMenuItem.Click += loadTAPToolStripMenuItem_Click;
//
// sNAToolStripMenuItem
//
sNAToolStripMenuItem.Name = "sNAToolStripMenuItem";
sNAToolStripMenuItem.Size = new Size(121, 26);
sNAToolStripMenuItem.Size = new Size(149, 26);
sNAToolStripMenuItem.Text = "SNA";
sNAToolStripMenuItem.Click += openSNAToolStripMenuItem_Click;
//
@@ -116,28 +119,28 @@
// runToolStripMenuItem
//
runToolStripMenuItem.Name = "runToolStripMenuItem";
runToolStripMenuItem.Size = new Size(129, 26);
runToolStripMenuItem.Size = new Size(224, 26);
runToolStripMenuItem.Text = "Run";
runToolStripMenuItem.Click += btnRun_Click;
//
// resetToolStripMenuItem
//
resetToolStripMenuItem.Name = "resetToolStripMenuItem";
resetToolStripMenuItem.Size = new Size(129, 26);
resetToolStripMenuItem.Size = new Size(224, 26);
resetToolStripMenuItem.Text = "Pause";
resetToolStripMenuItem.Click += btnPause_Click;
//
// stepToolStripMenuItem
//
stepToolStripMenuItem.Name = "stepToolStripMenuItem";
stepToolStripMenuItem.Size = new Size(129, 26);
stepToolStripMenuItem.Size = new Size(224, 26);
stepToolStripMenuItem.Text = "Step";
stepToolStripMenuItem.Click += btnStep_Click;
//
// resetToolStripMenuItem1
//
resetToolStripMenuItem1.Name = "resetToolStripMenuItem1";
resetToolStripMenuItem1.Size = new Size(129, 26);
resetToolStripMenuItem1.Size = new Size(224, 26);
resetToolStripMenuItem1.Text = "Reset";
resetToolStripMenuItem1.Click += btnReset_Click;
//
@@ -151,10 +154,29 @@
// HighSpeedToolStripMenuItem
//
HighSpeedToolStripMenuItem.Name = "HighSpeedToolStripMenuItem";
HighSpeedToolStripMenuItem.Size = new Size(170, 26);
HighSpeedToolStripMenuItem.Size = new Size(224, 26);
HighSpeedToolStripMenuItem.Text = "High Speed";
HighSpeedToolStripMenuItem.Click += btnHighSpeedToggle_Click;
//
// includedToolStripMenuItem
//
includedToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { tAPToolStripMenuItem1, sNAToolStripMenuItem1 });
includedToolStripMenuItem.Name = "includedToolStripMenuItem";
includedToolStripMenuItem.Size = new Size(149, 26);
includedToolStripMenuItem.Text = "Included";
//
// tAPToolStripMenuItem1
//
tAPToolStripMenuItem1.Name = "tAPToolStripMenuItem1";
tAPToolStripMenuItem1.Size = new Size(224, 26);
tAPToolStripMenuItem1.Text = "TAP";
//
// sNAToolStripMenuItem1
//
sNAToolStripMenuItem1.Name = "sNAToolStripMenuItem1";
sNAToolStripMenuItem1.Size = new Size(224, 26);
sNAToolStripMenuItem1.Text = "SNA";
//
// Form1
//
AutoScaleDimensions = new SizeF(8F, 20F);
@@ -187,5 +209,8 @@
private ToolStripMenuItem resetToolStripMenuItem1;
private ToolStripMenuItem optionsToolStripMenuItem;
private ToolStripMenuItem HighSpeedToolStripMenuItem;
private ToolStripMenuItem includedToolStripMenuItem;
private ToolStripMenuItem tAPToolStripMenuItem1;
private ToolStripMenuItem sNAToolStripMenuItem1;
}
}

View File

@@ -1,11 +1,12 @@
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Core.Cpu;
using Core.Io;
using Core.Memory;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
namespace Desktop
{
@@ -35,6 +36,7 @@ namespace Desktop
public Form1()
{
InitializeComponent();
PopulateIncludedTapsMenu();
this.DoubleBuffered = true;
this.ResizeRedraw = true;
InitializeEmulator();
@@ -94,7 +96,7 @@ namespace Desktop
audioSampleCount = 0;
nextScanlineTarget = TStatesPerFrame;
stopwatch.Restart();
tapeLoaded = false;
_pendingReset = false;
}
@@ -174,26 +176,26 @@ namespace Desktop
{
if (TotalFrameCount % 10 == 0)
{
this.BeginInvoke((MethodInvoker)delegate
this.BeginInvoke((System.Windows.Forms.MethodInvoker)delegate
{
UpdateScreenBitmap(); // Your existing method that writes the ULA data to _screenBitmap
this.Invalidate(); // Tells Windows: "The bitmap changed, please run OnPaint()!"
this.Text = $"{_baseTitle} - FPS: {FramesPerSecond:F1}";
this.Text = $"{_baseTitle} - FPS: {FramesPerSecond:F1} - Tape Loaded: {tapeLoaded.ToString()}";
});
}
}
else
{
this.BeginInvoke((MethodInvoker)delegate
this.BeginInvoke((System.Windows.Forms.MethodInvoker)delegate
{
UpdateScreenBitmap(); // Your existing method that writes the ULA data to _screenBitmap
this.Invalidate(); // Tells Windows: "The bitmap changed, please run OnPaint()!"
this.Text = $"{_baseTitle} - FPS: {FramesPerSecond:F1}";
this.Text = $"{_baseTitle} - FPS: {FramesPerSecond:F1} - Tape Loaded: {tapeLoaded.ToString()}";
});
}
@@ -225,7 +227,7 @@ namespace Desktop
catch (Exception ex)
{
_isPaused = true;
this.Invoke((MethodInvoker)delegate
this.Invoke((System.Windows.Forms.MethodInvoker)delegate
{
MessageBox.Show(ex.Message, "CPU Crash", MessageBoxButtons.OK, MessageBoxIcon.Error);
@@ -234,6 +236,77 @@ namespace Desktop
});
}
private void PopulateIncludedTapsMenu()
{
// 1. Get the current assembly (your .exe)
Assembly assembly = Assembly.GetExecutingAssembly();
// 2. Find all embedded resources
string[] resourceNames = assembly.GetManifestResourceNames();
foreach (string resourceName in resourceNames)
{
// Check if it is a TAP file in our TestRoms folder
// (Embedded resources use dot-notation, e.g., "Desktop.TestRoms.ZEXALL.TAP")
if (resourceName.Contains("Desktop.ROMS.TAP.") && resourceName.EndsWith(".TAP", StringComparison.OrdinalIgnoreCase))
{
// Clean up the name for the menu (e.g., "Desktop.TestRoms.ZEXALL.TAP" -> "ZEXALL")
string[] parts = resourceName.Split('.');
string displayName = parts[parts.Length - 2];
// Create the new menu item
ToolStripMenuItem item = new ToolStripMenuItem(displayName);
// Store the full internal path in the Tag so we know what to load when clicked
item.Tag = resourceName;
// Wire up the click event
item.Click += IncludedTapMenuItem_Click;
// Add it to the "Open Included..." dropdown
tAPToolStripMenuItem1.DropDownItems.Add(item);
}
}
}
private void IncludedTapMenuItem_Click(object? sender, EventArgs e)
{
if (sender is ToolStripMenuItem item && item.Tag is string resourceName)
{
_isPaused = true;
try
{
Assembly assembly = Assembly.GetExecutingAssembly();
// Open a stream directly into the binary file
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null) throw new Exception("Could not find embedded resource.");
// Copy the binary stream into a byte array
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
byte[] tapBytes = ms.ToArray();
// Feed it directly to your existing TapManager!
_tapManager.LoadTapData(tapBytes);
tapeLoaded = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load built-in TAP:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
_isPaused = false;
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
@@ -326,7 +399,7 @@ namespace Desktop
bmp.PixelFormat);
// Pull the raw pixel data
Marshal.Copy(_ula.FrameBuffer, 0, bmpData.Scan0, _ula.FrameBuffer.Length);
Marshal.Copy(_ula.FrontBuffer, 0, bmpData.Scan0, _ula.FrontBuffer.Length);
bmp.UnlockBits(bmpData);
// Dispose of the old frame to prevent massive RAM leaks!
@@ -388,10 +461,6 @@ namespace Desktop
private void btnReset_Click(object sender, EventArgs e)
{
_pendingReset = true;
_isPaused = true;
_cpu.Reset();
_memoryBus.CleanRAMData();
_isPaused = false;
}
private void btnExit_Click(object sender, EventArgs e)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Desktop/ROMS/TAP/MANIC.TAP Normal file

Binary file not shown.

BIN
Desktop/ROMS/TAP/zexall.tap Normal file

Binary file not shown.