-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.ts
66 lines (53 loc) · 1.49 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Initial setup/plumbing (rom/memory/cpu/input/screen)
// rom loading
var oReq = new XMLHttpRequest();
oReq.open("GET", "invaders.rom", true);
// binary
oReq.responseType = "arraybuffer";
// cpu
var processor = new cpu.Intel8080();
// canvas and container
var canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 224;
var container = document.createElement("div");
container.id = 'container';
container.appendChild(canvas);
document.body.appendChild(container);
// get the 2d context
var context = canvas.getContext("2d");
// screen
var screen = new video.Screen(processor, context, canvas.width, canvas.height, false, 0);
// user input (keyboard)
var input = new io.Input(document, this, processor);
input.init();
processor.setInput(input);
oReq.onload = function (oEvent)
{
// grab the rom bytes
var arrayBuffer = oReq.response;
if (arrayBuffer)
{
var source = new Uint8Array(arrayBuffer);
if ( source.length > 8192 )
throw new Error("Bad rom size!");
// allocate twice memory for RAM
var byteArray = new ArrayBuffer(16384);
var view = new Uint8Array(byteArray);
view.set(source);
// pass the ROM/RAM to the CPU
processor.memory = view;
processor.init();
// loop
var id = setInterval(run, 16);
input.interval = id;
}
};
function run ()
{
// input/cpu/screen update
input.update();
processor.Run();
screen.render();
}
oReq.send(null);