From 45ff3d2ff253eb8374022250545b6f48fc3bcea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-J=C3=B6rg=20Meyer?= Date: Mon, 13 Apr 2026 07:58:50 +0200 Subject: [PATCH] Entkopplung BASIC-Interpreter vom SDL2-Frame-Takt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- InputInterpreter.cs | 60 +++++++++++++++++++++++++++++++++++--- MemoryModel.cs | 68 +++++++++++++++++++++++++++++++++----------- MicrononaRenderer.cs | 13 +++++---- Program.cs | 4 +-- 4 files changed, 118 insertions(+), 27 deletions(-) diff --git a/InputInterpreter.cs b/InputInterpreter.cs index 6dd6175..9967790 100644 --- a/InputInterpreter.cs +++ b/InputInterpreter.cs @@ -12,12 +12,18 @@ internal class InputInterpreter private List _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 _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; } diff --git a/MemoryModel.cs b/MemoryModel.cs index 4fcad8d..c8d3dd2 100644 --- a/MemoryModel.cs +++ b/MemoryModel.cs @@ -73,6 +73,18 @@ public class MemoryModel public const int FontBankIndex = BankCount-1; public const int FontSize = 256 * FontGlyphWidth * FontGlyphHeight; // 20480 bytes + // ------------------------------------------------------------------------- + // Threading + // ------------------------------------------------------------------------- + + /// + /// 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. + /// + public readonly object Lock = new(); + // ------------------------------------------------------------------------- // Internal storage // ------------------------------------------------------------------------- @@ -115,11 +127,14 @@ public class MemoryModel /// public byte Read(ushort address) { - if (address >= IoBase && address < IoBase + IoSize) - return ReadIo(address - IoBase); - if (address >= BankWindowBase) - return ReadBank(address - BankWindowBase); - return _fixedRam[address]; + lock (Lock) + { + if (address >= IoBase && address < IoBase + IoSize) + return ReadIo(address - IoBase); + if (address >= BankWindowBase) + return ReadBank(address - BankWindowBase); + return _fixedRam[address]; + } } /// @@ -127,12 +142,15 @@ public class MemoryModel /// public void Write(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; + lock (Lock) + { + if (address >= IoBase && address < IoBase + IoSize) + WriteIo(address - IoBase, value); + else if (address >= BankWindowBase) + WriteBank(address - BankWindowBase, value); + else + _fixedRam[address] = value; + } } /// @@ -142,8 +160,21 @@ public class MemoryModel /// public void Write(ushort address, params byte[] data) { - for (int i = 0; i < data.Length; i++) - Write((ushort)(address + i), data[i]); + lock (Lock) + { + 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. /// public byte ReadFlat(int address) - => _extendedRam[address]; + { + lock (Lock) return _extendedRam[address]; + } /// /// Write one or more bytes to extended RAM starting at absolute offset. @@ -165,8 +198,11 @@ public class MemoryModel /// public void WriteFlat(int address, params byte[] data) { - for (int i = 0; i < data.Length; i++) - _extendedRam[address + i] = data[i]; + lock (Lock) + { + for (int i = 0; i < data.Length; i++) + _extendedRam[address + i] = data[i]; + } } /// diff --git a/MicrononaRenderer.cs b/MicrononaRenderer.cs index 9902472..8660cb4 100644 --- a/MicrononaRenderer.cs +++ b/MicrononaRenderer.cs @@ -29,12 +29,15 @@ public class MicrononaRenderer /// public ReadOnlySpan 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) - RenderText(); - else - RenderGraphics(); + if (mode == 0) + RenderText(); + else + RenderGraphics(); + } return _rgb24; } diff --git a/Program.cs b/Program.cs index 52ae77e..8f44adb 100644 --- a/Program.cs +++ b/Program.cs @@ -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);