Skip to content

Commit c7a5676

Browse files
committed
switch code to use browser modules
1 parent 16037da commit c7a5676

17 files changed

+73
-245
lines changed

.eslintignore

-2
This file was deleted.

.eslintrc.json

-26
This file was deleted.
File renamed without changes.

src/index.html renamed to index.html

+2-21
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>chip8.js</title>
6-
{% for (var chunk in o.htmlWebpackPlugin.files.css) { %}
7-
<link rel="stylesheet" type="text/css" href="{%= o.htmlWebpackPlugin.files.css[chunk] %}">
8-
{% } %}
6+
<link rel="stylesheet" type="text/css" href="css/style.css">
97
</head>
108
<body>
119
<h2>chip8.js <span class="subtitle">by <a href="https://chrisledet.com">Chris Ledet</a></span></h2>
@@ -77,23 +75,6 @@ <h3 class="tweak-header" title="You can tweak the VM using the web console">Sett
7775
window.chip8vm.keybindings[71] = 1;</pre>
7876
</p>
7977
</div>
80-
{% for (var chunk in o.htmlWebpackPlugin.files.js) { %}
81-
<script src="{%= o.htmlWebpackPlugin.files.js[chunk] %}"></script>
82-
{% } %}
83-
84-
<script type="text/javascript">
85-
var _gauges = _gauges || [];
86-
(function() {
87-
var t = document.createElement('script');
88-
t.type = 'text/javascript';
89-
t.async = true;
90-
t.id = 'gauges-tracker';
91-
t.setAttribute('data-site-id', '56d296a94b2ffa05ff002613');
92-
t.setAttribute('data-track-path', 'https://track.gaug.es/track.gif');
93-
t.src = 'https://d36ee2fcip1434.cloudfront.net/track.js';
94-
var s = document.getElementsByTagName('script')[0];
95-
s.parentNode.insertBefore(t, s);
96-
})();
97-
</script>
78+
<script type="module" src="index.js"></script>
9879
</body>
9980
</html>

index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import System from "./js/System.js";
2+
import RomLoader from "./js/RomLoader.js";
3+
4+
const displaySource = document.getElementById("display");
5+
const resumeSwitch = document.getElementById("resume");
6+
const fileUpload = document.getElementById("rom-file");
7+
const romSelector = document.getElementById("rom-selector");
8+
const stopSwitch = document.getElementById("stop");
9+
// TODO: figure out audio sources
10+
// let audioSource from "../sounds/beep.wav";
11+
12+
const vm = new System(displaySource, null);
13+
14+
/* event handlers */
15+
document.onkeydown = e => {
16+
const input = vm.keybindings[e.keyCode] || 0;
17+
console.log("input", input);
18+
vm.setInput(input);
19+
};
20+
21+
resumeSwitch.onclick = () => {
22+
vm.start();
23+
};
24+
25+
stopSwitch.onclick = () => {
26+
vm.stop();
27+
};
28+
29+
fileUpload.onchange = (e) => {
30+
if (e.target.files.length < 1) { return; }
31+
const romFile = e.target.files[0];
32+
new RomLoader(romFile, (rom) => {
33+
vm.boot(rom);
34+
});
35+
};
36+
37+
romSelector.onchange = (e) => {
38+
if (e.target.value === "Select ROM") { return; }
39+
const romName = e.target.value;
40+
41+
fetch("./roms/" + romName)
42+
.then(r => r.blob())
43+
.then((blob) => {
44+
new RomLoader(blob, (rom) => {
45+
vm.boot(rom);
46+
});
47+
});
48+
};
49+
50+
window.chip8vm = vm;

src/js/cpu.js renamed to js/CPU.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
/*eslint no-console: 0*/
2-
"use strict";
1+
import Util from "./Util.js";
32

4-
let Util = require("./util");
5-
6-
class CPU {
3+
export default class CPU {
74
constructor(display, audio) {
85
// cpu state
96
this.v = [];
@@ -73,7 +70,7 @@ class CPU {
7370
this.soundTimer -= 1;
7471

7572
if (this.soundTimer == 0) {
76-
this.audio.play();
73+
if (this.audio) this.audio.play();
7774
}
7875
}
7976

@@ -265,7 +262,7 @@ class CPU {
265262
let newPixelStates = this.display.current();
266263

267264
// N bytes of sprite data starting at the address stored in I
268-
for (var spriteIndex = 0; spriteIndex < n; spriteIndex++) {
265+
for (let spriteIndex = 0; spriteIndex < n; spriteIndex++) {
269266
// what sprite are we looking for
270267
let sprite = this.memory[this.i + spriteIndex];
271268
// convert sprite opscode into switches
@@ -274,7 +271,7 @@ class CPU {
274271
this.v[0xF] = 0;
275272

276273
// for the width of 8 pixels
277-
for (var spriteDrawingIndex = 0; spriteDrawingIndex < 8; spriteDrawingIndex++) {
274+
for (let spriteDrawingIndex = 0; spriteDrawingIndex < 8; spriteDrawingIndex++) {
278275
let drawPosition = {
279276
x: (position.x + spriteDrawingIndex),
280277
y: (position.y + spriteIndex)
@@ -390,9 +387,3 @@ class CPU {
390387
}
391388
}
392389
}
393-
394-
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
395-
module.exports = CPU;
396-
} else {
397-
window.CPU = CPU;
398-
}

src/js/display.js renamed to js/Display.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
class Display {
1+
export default class Display {
42
constructor(displayDomContainer) {
53
let pixelWidth = displayDomContainer.width / 64;
64
let pixelHeight = displayDomContainer.height / 32;
@@ -40,19 +38,13 @@ class Display {
4038
clearStates() {
4139
let states = [];
4240

43-
for (var x = 0; x < 64; x++) {
41+
for (let x = 0; x < 64; x++) {
4442
states[x] = [];
45-
for (var y = 0; y < 32; y++) {
43+
for (let y = 0; y < 32; y++) {
4644
states[x][y] = false;
4745
}
4846
}
4947

5048
return states;
5149
}
5250
}
53-
54-
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
55-
module.exports = Display;
56-
} else {
57-
window.Display = Display;
58-
}

src/js/rom_loader.js renamed to js/RomLoader.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
class RomLoader {
1+
export default class RomLoader {
42
constructor(romFile, callback) {
53
let reader = new FileReader();
64

@@ -20,9 +18,3 @@ class RomLoader {
2018
this.callback = callback;
2119
}
2220
}
23-
24-
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
25-
module.exports = RomLoader;
26-
} else {
27-
window.RomLoader = RomLoader;
28-
}

src/js/system.js renamed to js/System.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
import CPU from "./CPU.js";
2+
import Display from "./Display.js";
3+
import Util from "./Util.js";
24

3-
let CPU = require("./cpu");
4-
let Display = require("./display");
5-
let Util = require("./util");
6-
7-
class System {
5+
export default class System {
86
constructor(displaySource, audioSource) {
97
this.clockRate = 500;
108
this.keybindings = {
@@ -26,11 +24,11 @@ class System {
2624
86: 0x10 // V
2725
};
2826

27+
const display = new Display(displaySource);
28+
const audio = audioSource ? new Audio(audioSource) : null;
29+
2930
this._pid = null;
30-
this._cpu = new CPU(
31-
new Display(displaySource),
32-
new Audio(audioSource)
33-
);
31+
this._cpu = new CPU(display, audio);
3432
}
3533

3634
boot(rom) {
@@ -44,7 +42,7 @@ class System {
4442

4543
start() {
4644
if (this._pid) { return; }
47-
var interval = Util.clockRateInMS(this.clockRate);
45+
let interval = Util.clockRateInMS(this.clockRate);
4846
this._pid = window.setInterval(this._cpu.step.bind(this._cpu), interval);
4947
}
5048

@@ -62,8 +60,3 @@ class System {
6260
}
6361
}
6462

65-
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
66-
module.exports = System;
67-
} else {
68-
window.System = System;
69-
}

src/js/util.js renamed to js/Util.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
class Util {
1+
export default class Util {
42
static byteToSwitch(b) {
53
let s = [
64
(b & 0x80) == 0x80,
@@ -26,9 +24,3 @@ class Util {
2624
return 1000/clockRate;
2725
}
2826
}
29-
30-
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
31-
module.exports = Util;
32-
} else {
33-
window.Util = Util;
34-
}

main.js

-4
This file was deleted.

package.json

-41
This file was deleted.
File renamed without changes.

src/spec/cpu_spec.js renamed to spec/cpu_spec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
var CPU = require("./../js/cpu");
1+
import CPU from "./../js/CPU.js";
42

53
describe("CPU", function(){
64
let cpu;

src/spec/util_spec.js renamed to spec/util_spec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
var Util = require("./../js/util");
1+
import Util from "./../js/Util.js";
42

53
describe("Util", function(){
64
describe("clockRateInMS()", function(){

src/js/web.js

-46
This file was deleted.

0 commit comments

Comments
 (0)