Skip to content

Commit 499e52c

Browse files
committed
Fix keyboard issues
1 parent 0b8205f commit 499e52c

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

docs/silicon8.wasm

614 Bytes
Binary file not shown.

src/silicon8/cpu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (cpu *CPU) Reset(interpreter int) {
112112
cpu.Keyboard[i] = false
113113
}
114114

115-
cpu.waitForKey = false
115+
cpu.waitForKey = 0
116116
cpu.WaitForInt = 0
117117
cpu.playing = false
118118
cpu.SD = true

src/silicon8/cycle.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,32 @@ func (cpu *CPU) Cycle() {
132132
cpu.v[x] = cpu.dt
133133
case 0x0A:
134134
// Wait for keypress and return key in vX
135-
if cpu.waitForKey {
135+
switch cpu.waitForKey {
136+
case 0:
137+
cpu.pc -= 2
138+
for _, p := range cpu.Keyboard {
139+
if p {
140+
return
141+
}
142+
}
143+
cpu.waitForKey = 1
144+
case 1:
145+
cpu.pc -= 2
136146
for i, p := range cpu.Keyboard {
137147
if p {
138148
cpu.v[x] = uint8(i)
139-
cpu.waitForKey = false
149+
cpu.waitForKey = 2
140150
return
141151
}
142152
}
143-
cpu.pc -= 2
144-
} else {
145-
cpu.pc -= 2
153+
case 2:
146154
for _, p := range cpu.Keyboard {
147155
if p {
156+
cpu.pc -= 2
148157
return
149158
}
150159
}
151-
cpu.waitForKey = true
160+
cpu.waitForKey = 0
152161
}
153162
case 0x15:
154163
// Set delay timer to value in vX

src/silicon8/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type CPU struct {
4444

4545
// Interpreter internal state
4646
Keyboard [16]bool
47-
waitForKey bool // Waiting for key press?
47+
waitForKey uint8 // Waiting for key press?
4848
WaitForInt uint8 // Waiting for display refresh "interrupt"?
4949
playing bool // Playing sound?
5050
SD bool // Screen dirty?

web-client/keyboard.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ module.exports = instance => {
5252
cyclesPerFrame /= 2;
5353
return instance.setCyclesPerFrame(cyclesPerFrame);
5454
default:
55-
if ( Object.keys(keys).includes(e.keyCode.toString()) )
55+
if ( instance && Object.keys(keys).includes(e.keyCode.toString()) )
5656
instance.pressKey(keys[e.keyCode]);
5757
}
5858
});
5959

6060
window.addEventListener('keyup', e => {
61-
if ( instance && keys[e.keyCode] )
61+
if ( instance && Object.keys(keys).includes(e.keyCode.toString()) )
6262
instance.releaseKey(keys[e.keyCode]);
6363
});
6464

0 commit comments

Comments
 (0)