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 List<int> _lines = new();
private int _programCounter; private int _programCounter;
private bool _running; private volatile bool _running;
private bool _waitingForInput; private volatile bool _waitingForInput;
private char _inputVariable; private char _inputVariable;
private bool _skipIncrement; private bool _skipIncrement;
private readonly Stack<int> _callStack = new(); 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) public InputInterpreter(MemoryModel mem, ConsoleHelper consoleHelper, DiskManager diskManager)
{ {
this.mem = mem; this.mem = mem;
@ -28,6 +34,45 @@ internal class InputInterpreter
public bool IsRunning => _running; public bool IsRunning => _running;
public bool IsWaitingForInput => _waitingForInput; 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) public void SubmitInput(string line)
{ {
if (!_waitingForInput) return; if (!_waitingForInput) return;
@ -36,6 +81,7 @@ internal class InputInterpreter
_variables[_inputVariable] = val; _variables[_inputVariable] = val;
_waitingForInput = false; _waitingForInput = false;
_programCounter++; _programCounter++;
_inputSignal.Release();
} }
else else
{ {
@ -47,14 +93,19 @@ internal class InputInterpreter
public void Stop() public void Stop()
{ {
_running = false; _running = false;
if (_waitingForInput)
{
_waitingForInput = false;
try { _inputSignal.Release(); } catch { }
}
consoleHelper.NewLine(); consoleHelper.NewLine();
consoleHelper.WriteText("BREAK"); consoleHelper.WriteText("BREAK");
consoleHelper.NewLine(); consoleHelper.NewLine();
} }
public void Tick() private void Tick()
{ {
if (!_running || _waitingForInput) return; if (!_running) return;
if (_programCounter >= _lines.Count) if (_programCounter >= _lines.Count)
{ {
@ -123,6 +174,7 @@ internal class InputInterpreter
_programCounter = 0; _programCounter = 0;
_callStack.Clear(); _callStack.Clear();
_running = true; _running = true;
_runSignal.Set();
return; return;
} }

View file

@ -73,6 +73,18 @@ public class MemoryModel
public const int FontBankIndex = BankCount-1; public const int FontBankIndex = BankCount-1;
public const int FontSize = 256 * FontGlyphWidth * FontGlyphHeight; // 20480 bytes 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 // Internal storage
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -115,11 +127,14 @@ public class MemoryModel
/// </summary> /// </summary>
public byte Read(ushort address) public byte Read(ushort address)
{ {
if (address >= IoBase && address < IoBase + IoSize) lock (Lock)
return ReadIo(address - IoBase); {
if (address >= BankWindowBase) if (address >= IoBase && address < IoBase + IoSize)
return ReadBank(address - BankWindowBase); return ReadIo(address - IoBase);
return _fixedRam[address]; if (address >= BankWindowBase)
return ReadBank(address - BankWindowBase);
return _fixedRam[address];
}
} }
/// <summary> /// <summary>
@ -127,12 +142,15 @@ public class MemoryModel
/// </summary> /// </summary>
public void Write(ushort address, byte value) public void Write(ushort address, byte value)
{ {
if (address >= IoBase && address < IoBase + IoSize) lock (Lock)
WriteIo(address - IoBase, value); {
else if (address >= BankWindowBase) if (address >= IoBase && address < IoBase + IoSize)
WriteBank(address - BankWindowBase, value); WriteIo(address - IoBase, value);
else else if (address >= BankWindowBase)
_fixedRam[address] = value; WriteBank(address - BankWindowBase, value);
else
_fixedRam[address] = value;
}
} }
/// <summary> /// <summary>
@ -142,8 +160,21 @@ public class MemoryModel
/// </summary> /// </summary>
public void Write(ushort address, params byte[] data) public void Write(ushort address, params byte[] data)
{ {
for (int i = 0; i < data.Length; i++) lock (Lock)
Write((ushort)(address + i), data[i]); {
for (int i = 0; i < data.Length; 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,7 +188,9 @@ public class MemoryModel
/// Read a single byte from extended RAM by absolute offset. /// Read a single byte from extended RAM by absolute offset.
/// </summary> /// </summary>
public byte ReadFlat(int address) public byte ReadFlat(int address)
=> _extendedRam[address]; {
lock (Lock) return _extendedRam[address];
}
/// <summary> /// <summary>
/// Write one or more bytes to extended RAM starting at absolute offset. /// Write one or more bytes to extended RAM starting at absolute offset.
@ -165,8 +198,11 @@ public class MemoryModel
/// </summary> /// </summary>
public void WriteFlat(int address, params byte[] data) public void WriteFlat(int address, params byte[] data)
{ {
for (int i = 0; i < data.Length; i++) lock (Lock)
_extendedRam[address + i] = data[i]; {
for (int i = 0; i < data.Length; i++)
_extendedRam[address + i] = data[i];
}
} }
/// <summary> /// <summary>

View file

@ -29,12 +29,15 @@ public class MicrononaRenderer
/// </summary> /// </summary>
public ReadOnlySpan<byte> Render() public ReadOnlySpan<byte> Render()
{ {
byte mode = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_MODE); lock (_mem.Lock)
{
byte mode = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_MODE);
if (mode == 0) if (mode == 0)
RenderText(); RenderText();
else else
RenderGraphics(); RenderGraphics();
}
return _rgb24; return _rgb24;
} }

View file

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