-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChip8.js
86 lines (75 loc) · 2.42 KB
/
Chip8.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var CHIP_8 = (function () {
"use strict"
var inputSource = document;
var CANVAS_NAME = "MainCanvas";
var cpu, canvas, input;
var gameInterval, renderInterval;
var font_set = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
];
function CHIP_8() {}
function run() {
for(var i = 0; i < 3; i++){
cpu.cycle();
cpu.decrementTime();
}
}
function render(){
if(cpu.drawFlag){
canvas.renderOnCanvas();
cpu.drawFlag = false;
}
}
CHIP_8.prototype.emulateCycle = function emulateCycle() {
if(gameInterval == undefined && renderInterval == undefined){
gameInterval = setInterval(run, 5);
renderInterval = setInterval(render, 16);
}
}
CHIP_8.prototype.start = function start(name) {
this.reset();
var xhr = new XMLHttpRequest();
var chip8;
xhr.open("GET", "programs/"+name, true);
xhr.responseType = "arraybuffer";
var scope = this;
xhr.onload = function () {
var program = new Uint8Array(xhr.response);
canvas = new Canvas(CANVAS_NAME);
cpu = new CPU(program, font_set, canvas);
scope.emulateCycle();
};
xhr.send();
}
CHIP_8.prototype.reset = function reset(){
//RESETING CHIP8 ATTRIBUTES
this.stopLoop();
cpu = undefined;
canvas = undefined;
input = undefined;
}
CHIP_8.prototype.stopLoop = function stopLoop(){
if(gameInterval != undefined && renderInterval != undefined){
clearInterval(gameInterval);
clearInterval(renderInterval);
gameInterval = undefined;
renderInterval = undefined;
}
}
return CHIP_8;
})();