From 156f6b2e8aa56a1dd8ea8f460b5a4a62bd0d5467 Mon Sep 17 00:00:00 2001 From: fusefib <55459980+fusefib@users.noreply.github.com> Date: Sun, 23 Aug 2026 19:48:09 +0300 Subject: [PATCH] fix: stale keyboard data reprocessing during INT 16h polling INT 16h may invoke INT 09h while waiting for a keystroke. When the 8042 output buffer is empty, reading port 0x60 returns the previous data byte, causing the same scancode to be processed repeatedly. Check the controller status before reading the keyboard data port. --- .../Input/Keyboard/BiosKeyboardInt9Handler.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Spice86.Core/Emulator/InterruptHandlers/Input/Keyboard/BiosKeyboardInt9Handler.cs b/src/Spice86.Core/Emulator/InterruptHandlers/Input/Keyboard/BiosKeyboardInt9Handler.cs index 9b221fc0ed..bb0dd2d947 100644 --- a/src/Spice86.Core/Emulator/InterruptHandlers/Input/Keyboard/BiosKeyboardInt9Handler.cs +++ b/src/Spice86.Core/Emulator/InterruptHandlers/Input/Keyboard/BiosKeyboardInt9Handler.cs @@ -72,6 +72,15 @@ public BiosKeyboardInt9Handler(IMemory memory, BiosDataArea biosDataArea, /// public override void Run() { + // INT 16h may invoke INT 09h as a polling mechanism. + // Do not reprocess the controller's previous data byte when + // the output buffer contains no new data. + byte status = _ps2Controller.ReadByte(KeyboardPorts.StatusRegister); + if ((status & 0x01) == 0) { + _dualPic.AcknowledgeInterrupt(1); + return; + } + // Disable keyboard first - otherwise Prince of Persia reads it before us! _ps2Controller.WriteByte(KeyboardPorts.Command, (byte)KeyboardCommand.DisablePortKbd);