Skip to content

Commit 1199255

Browse files
committed
minor fixes
* zero out all registers * capture pids for each timers and use them for pause/reset
1 parent 440e71c commit 1199255

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const resumeSwitch = document.getElementById("resume");
66
const uploader = document.getElementById("rom-uploader");
77
const romSelector = document.getElementById("rom-selector");
88
const stopSwitch = document.getElementById("stop");
9-
const keyButtons = document.getElementsByClassName("key")
9+
const keyButtons = document.getElementsByClassName("key");
10+
1011
Array.from(keyButtons).forEach(b => {
1112
b.onclick = (e) => {
1213
const input = Number(e.target.dataset.input);

js/CPU.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@ export default class CPU {
4848
];
4949
this.awaitInput = false;
5050
this.currentInput = 0x0;
51-
this.debugMode = false;
51+
this.debugMode = true;
5252
}
5353

5454
debug() {
5555
if (this.debugMode) {
56-
console.log(arguments);
56+
console.log(...arguments);
5757
}
5858
}
5959

6060
loadRom(data) {
6161
this.debug("Loading ROM");
62-
6362
this.reset();
64-
6563
this.copyIntoMemory(0x0, this.font);
6664
this.copyIntoMemory(0x200, data);
6765
}
@@ -91,7 +89,7 @@ export default class CPU {
9189
}
9290

9391
reset() {
94-
for (let x = 0; x < 0xF; x++) {
92+
for (let x = 0; x <= 0xF; x++) {
9593
this.v[x] = 0x0;
9694
}
9795

@@ -141,7 +139,7 @@ export default class CPU {
141139
this.pc = nnn - 2; // -2 because pc is incremented after each
142140
break;
143141
case 0x3:
144-
this.debug("EXEC: SKIP IF VX == NN", nn);
142+
this.debug("EXEC: SKIP IF VX == NN", "x", x, "nn", nn, "v[x]", this.v[x]);
145143
if (this.v[x] == nn) {
146144
this.pc += 2;
147145
}
@@ -190,6 +188,11 @@ export default class CPU {
190188

191189
case 0x3:
192190
// Sets VX to VX xor VY.
191+
// Performs a bitwise exclusive OR on the values of Vx and Vy,
192+
// then stores the result in Vx.
193+
// An exclusive OR compares the corrseponding bits from two values,
194+
// and if the bits are not both the same,
195+
// then the corresponding bit in the result is set to 1. Otherwise, it is 0.
193196
if (this.v[x] == this.v[y]) {
194197
this.v[x] = 0x0;
195198
} else {
@@ -383,7 +386,6 @@ export default class CPU {
383386
/* private */
384387
copyIntoMemory(startAddress, data) {
385388
for (let x = 0; x < data.length; x++) {
386-
this.debug("Loading ", data[x].toString(16), " in address " + (startAddress + x).toString(16));
387389
this.memory[startAddress + x] = data[x];
388390
}
389391
}

js/System.js

+23-17
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,51 @@ export default class System {
66
const display = new Display(displaySource);
77
const audio = audioSource ? new Audio(audioSource) : null;
88

9+
this.cpu = new CPU(display, audio);
910
this.clockRate = 300;
10-
this._pid = null;
11-
this._cpu = new CPU(display, audio);
11+
this.stepPid = null;
12+
this.timerPid = null;
1213
}
1314

1415
boot(rom) {
1516
this.stop();
16-
17-
this._cpu.reset();
18-
this._cpu.loadRom(rom);
19-
17+
this.cpu.loadRom(rom);
2018
this.start();
2119
}
2220

2321
start() {
24-
if (this._pid) { return; }
22+
if (this.stepPid || this.timerPid) {
23+
return;
24+
}
2525

26-
this._pid = window.setInterval(() => {
27-
this._cpu.step();
26+
this.stepPid = window.setInterval(() => {
27+
this.cpu.step();
2828
}, 1000/this.clockRate);
29-
this._pid = window.setInterval(() => {
30-
this._cpu.stepTimer();
29+
30+
this.timerPid = window.setInterval(() => {
31+
this.cpu.stepTimer();
3132
}, 1000/60); // delay and sound timers run at fixed 60Hz
3233
}
3334

3435
stop() {
35-
if (!this._pid) { return; }
36-
37-
window.clearInterval(this._pid);
38-
this._pid = null;
36+
if (this.stepPid) {
37+
window.clearInterval(this.stepPid);
38+
}
39+
if (this.timerPid) {
40+
window.clearInterval(this.timerPid);
41+
}
42+
43+
this.stepPid = null;
44+
this.timerPid = null;
3945
}
4046

4147
reset() {
4248
this.stop();
43-
this._cpu.reset();
49+
this.cpu.reset();
4450
}
4551

4652
setInput(input) {
47-
this._cpu.setInput(input);
53+
this.cpu.setInput(input);
4854
}
4955
}
5056

0 commit comments

Comments
 (0)