Skip to content

Commit

Permalink
Block VM input processing when paused (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
netalondon authored May 8, 2024
1 parent ef01864 commit 98fb9f9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
3 changes: 3 additions & 0 deletions components/src/stores/vm.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ export function makeVmStore(
return true;
}
},
setPaused(paused = true) {
vm.setPaused(paused);
},
step() {
showHighlight = true;
try {
Expand Down
62 changes: 34 additions & 28 deletions simulator/src/vm/os/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 2 additions & 0 deletions simulator/src/vm/os/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions simulator/src/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions web/src/pages/vm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const VM = () => {
}

override toggle() {
actions.setPaused(!this.running);
dispatch.current({ action: "update" });
}
})();
Expand All @@ -119,6 +120,7 @@ const VM = () => {
}

override toggle() {
actions.setPaused(!this.running);
dispatch.current({ action: "update" });
}
})();
Expand Down

0 comments on commit 98fb9f9

Please sign in to comment.