From 98fb9f90455e1ec36eea4745e4574a46c5a2d95e Mon Sep 17 00:00:00 2001 From: Neta London <67196883+netalondon@users.noreply.github.com> Date: Wed, 8 May 2024 13:22:08 +0300 Subject: [PATCH] Block VM input processing when paused (#292) --- components/src/stores/vm.store.ts | 3 ++ simulator/src/vm/os/keyboard.ts | 62 +++++++++++++++++-------------- simulator/src/vm/os/os.ts | 2 + simulator/src/vm/vm.ts | 4 ++ web/src/pages/vm.tsx | 2 + 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/components/src/stores/vm.store.ts b/components/src/stores/vm.store.ts index 8fbddd131..f7c8d9e35 100644 --- a/components/src/stores/vm.store.ts +++ b/components/src/stores/vm.store.ts @@ -314,6 +314,9 @@ export function makeVmStore( return true; } }, + setPaused(paused = true) { + vm.setPaused(paused); + }, step() { showHighlight = true; try { diff --git a/simulator/src/vm/os/keyboard.ts b/simulator/src/vm/os/keyboard.ts index 5eb6ada58..76fcb9099 100644 --- a/simulator/src/vm/os/keyboard.ts +++ b/simulator/src/vm/os/keyboard.ts @@ -29,13 +29,15 @@ export class KeyboardLib { let exit = false; - if (!pressed && this.keyPressed() !== 0) { - pressed = true; - c = this.keyPressed(); - } - if (pressed && this.keyPressed() === 0) { - exit = true; - resolve(c); + if (!this.os.paused) { + if (!pressed && this.keyPressed() !== 0) { + pressed = true; + c = this.keyPressed(); + } + if (pressed && this.keyPressed() === 0) { + exit = true; + resolve(c); + } } if (!exit) { @@ -73,34 +75,38 @@ export class KeyboardLib { let exit = false; - if (!pressed && this.keyPressed() != 0) { - pressed = true; - c = this.keyPressed(); - } - if (pressed && this.keyPressed() == 0) { - pressed = false; - // key was released - if (c == BACKSPACE) { - if (this.os.string.length(str) > 0) { - this.os.output.backspace(); - } - this.os.string.eraseLastChar(str); - } else if (c == NEW_LINE) { - resolve(str); - exit = true; - } else { - this.os.string.appendChar(str, c); - if (this.os.sys.halted) { - resolve(0); + if (!this.os.paused) { + if (!pressed && this.keyPressed() != 0) { + pressed = true; + c = this.keyPressed(); + } + if (pressed && this.keyPressed() == 0) { + pressed = false; + // key was released + if (c == BACKSPACE) { + if (this.os.string.length(str) > 0) { + this.os.output.backspace(); + } + this.os.string.eraseLastChar(str); + } else if (c == NEW_LINE) { + resolve(str); + exit = true; + } else { + this.os.string.appendChar(str, c); + if (this.os.sys.halted) { + resolve(0); + } + this.os.output.printChar(c); + this.os.output.drawCursor(); } - this.os.output.printChar(c); - this.os.output.drawCursor(); } } + if (!exit) { this.animationFrameId = requestAnimationFrame(loop); } }; + loop(); } diff --git a/simulator/src/vm/os/os.ts b/simulator/src/vm/os/os.ts index ef13b033c..e6ebaa8f0 100644 --- a/simulator/src/vm/os/os.ts +++ b/simulator/src/vm/os/os.ts @@ -15,6 +15,8 @@ export class OS { keyboard: KeyboardLib; sys: SysLib; + paused = false; + constructor(memory: VmMemory) { this.vmMemory = memory; this.screen = new ScreenLib(this.vmMemory, this); diff --git a/simulator/src/vm/vm.ts b/simulator/src/vm/vm.ts index a7cedf1a3..189084950 100644 --- a/simulator/src/vm/vm.ts +++ b/simulator/src/vm/vm.ts @@ -615,6 +615,10 @@ export class Vm { } } + setPaused(paused = true) { + this.os.paused = paused; + } + step(): number | undefined { if (this.os.sys.halted) { return this.os.sys.exitCode; diff --git a/web/src/pages/vm.tsx b/web/src/pages/vm.tsx index 960a90b4a..c7262175e 100644 --- a/web/src/pages/vm.tsx +++ b/web/src/pages/vm.tsx @@ -100,6 +100,7 @@ const VM = () => { } override toggle() { + actions.setPaused(!this.running); dispatch.current({ action: "update" }); } })(); @@ -119,6 +120,7 @@ const VM = () => { } override toggle() { + actions.setPaused(!this.running); dispatch.current({ action: "update" }); } })();