Entkopplung BASIC-Interpreter vom SDL2-Frame-Takt

- InputInterpreter läuft jetzt in eigenem Background-Thread (Vollgas)
- ManualResetEventSlim (_runSignal) blockiert Thread bis RUN aufgerufen wird
- SemaphoreSlim (_inputSignal) ersetzt Busy-Wait bei INPUT
- Stop() und StopThread() entsperren alle Waits sauber vor Join
- _running/_waitingForInput als volatile
- MemoryModel: internes Locking (lock(Lock)) auf allen Read/Write/ReadFlat/WriteFlat
- Lock-Objekt öffentlich für Bulk-Operationen (Renderer)
- MicrononaRenderer: hält Lock für gesamten Frame statt pro ReadFlat-Aufruf

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hans-Jörg Meyer 2026-04-13 07:58:50 +02:00
parent 5cdf17de8c
commit 45ff3d2ff2
4 changed files with 118 additions and 27 deletions

View file

@ -12,12 +12,18 @@ internal class InputInterpreter
private List<int> _lines = new();
private int _programCounter;
private bool _running;
private bool _waitingForInput;
private volatile bool _running;
private volatile bool _waitingForInput;
private char _inputVariable;
private bool _skipIncrement;
private readonly Stack<int> _callStack = new();
// Threading
private Thread? _thread;
private CancellationTokenSource _cts = new();
private readonly ManualResetEventSlim _runSignal = new(false);
private readonly SemaphoreSlim _inputSignal = new(0, 1);
public InputInterpreter(MemoryModel mem, ConsoleHelper consoleHelper, DiskManager diskManager)
{
this.mem = mem;
@ -28,6 +34,45 @@ internal class InputInterpreter
public bool IsRunning => _running;
public bool IsWaitingForInput => _waitingForInput;
public void StartThread()
{
_thread = new Thread(RunLoop) { IsBackground = true, Name = "BasicInterpreter" };
_thread.Start();
}
public void StopThread()
{
_running = false;
_cts.Cancel();
// Unblock any pending Wait calls
try { _runSignal.Set(); } catch { }
try { _inputSignal.Release(); } catch { }
_thread?.Join();
}
private void RunLoop()
{
try
{
while (true)
{
_runSignal.Wait(_cts.Token);
_runSignal.Reset();
while (_running && !_cts.IsCancellationRequested)
{
if (_waitingForInput)
{
_inputSignal.Wait(_cts.Token);
continue;
}
Tick();
}
}
}
catch (OperationCanceledException) { }
}
public void SubmitInput(string line)
{
if (!_waitingForInput) return;
@ -36,6 +81,7 @@ internal class InputInterpreter
_variables[_inputVariable] = val;
_waitingForInput = false;
_programCounter++;
_inputSignal.Release();
}
else
{
@ -47,14 +93,19 @@ internal class InputInterpreter
public void Stop()
{
_running = false;
if (_waitingForInput)
{
_waitingForInput = false;
try { _inputSignal.Release(); } catch { }
}
consoleHelper.NewLine();
consoleHelper.WriteText("BREAK");
consoleHelper.NewLine();
}
public void Tick()
private void Tick()
{
if (!_running || _waitingForInput) return;
if (!_running) return;
if (_programCounter >= _lines.Count)
{
@ -123,6 +174,7 @@ internal class InputInterpreter
_programCounter = 0;
_callStack.Clear();
_running = true;
_runSignal.Set();
return;
}

View file

@ -73,6 +73,18 @@ public class MemoryModel
public const int FontBankIndex = BankCount-1;
public const int FontSize = 256 * FontGlyphWidth * FontGlyphHeight; // 20480 bytes
// -------------------------------------------------------------------------
// Threading
// -------------------------------------------------------------------------
/// <summary>
/// Shared lock for all memory access. Callers may acquire this for bulk
/// operations (e.g. the renderer locks once per frame instead of once per
/// ReadFlat call). C# Monitor is re-entrant on the same thread, so nested
/// lock(Lock) blocks are safe.
/// </summary>
public readonly object Lock = new();
// -------------------------------------------------------------------------
// Internal storage
// -------------------------------------------------------------------------
@ -114,6 +126,8 @@ public class MemoryModel
/// Read a single byte from the 16-bit address space.
/// </summary>
public byte Read(ushort address)
{
lock (Lock)
{
if (address >= IoBase && address < IoBase + IoSize)
return ReadIo(address - IoBase);
@ -121,11 +135,14 @@ public class MemoryModel
return ReadBank(address - BankWindowBase);
return _fixedRam[address];
}
}
/// <summary>
/// Write a single byte to the 16-bit address space.
/// </summary>
public void Write(ushort address, byte value)
{
lock (Lock)
{
if (address >= IoBase && address < IoBase + IoSize)
WriteIo(address - IoBase, value);
@ -134,6 +151,7 @@ public class MemoryModel
else
_fixedRam[address] = value;
}
}
/// <summary>
/// Write multiple bytes sequentially starting at address.
@ -141,9 +159,22 @@ public class MemoryModel
/// Example: mem.Write(0xE000, 0x48, 0x1C, 0x08);
/// </summary>
public void Write(ushort address, params byte[] data)
{
lock (Lock)
{
for (int i = 0; i < data.Length; i++)
Write((ushort)(address + i), data[i]);
WriteUnlocked((ushort)(address + i), data[i]);
}
}
private void WriteUnlocked(ushort address, byte value)
{
if (address >= IoBase && address < IoBase + IoSize)
WriteIo(address - IoBase, value);
else if (address >= BankWindowBase)
WriteBank(address - BankWindowBase, value);
else
_fixedRam[address] = value;
}
// -------------------------------------------------------------------------
@ -157,17 +188,22 @@ public class MemoryModel
/// Read a single byte from extended RAM by absolute offset.
/// </summary>
public byte ReadFlat(int address)
=> _extendedRam[address];
{
lock (Lock) return _extendedRam[address];
}
/// <summary>
/// Write one or more bytes to extended RAM starting at absolute offset.
/// Example: mem.WriteFlat(255 * BankSize, 0x00, 0x18, 0x3C, 0x66, ...);
/// </summary>
public void WriteFlat(int address, params byte[] data)
{
lock (Lock)
{
for (int i = 0; i < data.Length; i++)
_extendedRam[address + i] = data[i];
}
}
/// <summary>
/// Return a Span over one full bank in extended RAM.

View file

@ -28,6 +28,8 @@ public class MicrononaRenderer
/// Returns a ReadOnlySpan over the RGB24 data, ready for SDL_UpdateTexture.
/// </summary>
public ReadOnlySpan<byte> Render()
{
lock (_mem.Lock)
{
byte mode = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_MODE);
@ -35,6 +37,7 @@ public class MicrononaRenderer
RenderText();
else
RenderGraphics();
}
return _rgb24;
}

View file

@ -464,6 +464,7 @@ scrollLock.Set(false);
diskManager.OnMicrononaStickInserted += path => { Console.WriteLine($"Ready: {path}"); scrollLock.Set(true); };
diskManager.OnStickRemoved += () => { Console.WriteLine("Stick removed"); scrollLock.Set(false); };
diskManager.Start();
inputInterpreter.StartThread();
// -------------------------------------------------------------------------
// Main loop
@ -524,8 +525,6 @@ while (running)
}
}
}
// --- Interpreter-Schritt ---
inputInterpreter.Tick();
scrollLock.Refresh();
// --- VSync flag setzen (wird später vom 6809-Emulator gelesen) ---
@ -550,6 +549,7 @@ while (running)
// Cleanup
// -------------------------------------------------------------------------
inputInterpreter.StopThread();
SDL.SDL_DestroyTexture(texture);
SDL.SDL_DestroyRenderer(renderer);
SDL.SDL_DestroyWindow(window);