-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
220 lines (176 loc) · 4.67 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var NES = NES || { };
var shouldDrawSprites = true;
var shouldDrawBackground = true;
var testBit = function(value, bit) {
return (value & (1 << bit)) != 0;
};
var setBit = function(value, bit, on) {
if (on) {
return value |= (1 << bit);
}
else {
return value &= ~(1 << bit);
}
};
var controller = new NES.Controller();
var cpu = new NES.CPU();
var ppu = new NES.PPU();
var memoryMapper = undefined;
var frameNumber = 0;
var frame = function() {
var startTime = new Date();
for (var i = 0; i < 263; i++) {
ppu.run();
cpu.run(Math.floor(1364 / 12));
}
var endTime = new Date();
var elapsedTime = endTime.getTime() - startTime.getTime();
if (elapsedTime > 16) {
//console.log(endTime.getTime() - startTime.getTime());
}
};
var initRom = function(file) {
var reader = new FileReader();
reader.onloadend = function() {
var header = new Uint8Array(reader.result, 0, 16);
if (!(header[0] == 78 && header[1] == 69 && header[2] == 83)) {
screenContext.putImageData(id, 0, 0);
screenContext.fillText("Invalid File Selected", 256, 50);
return;
}
var prgSize = 16384 * header[4];
var chrSize = 8192 * header[5];
var prgData = new Uint8Array(reader.result, 16, prgSize);
var chrData = new Uint8Array(reader.result, 16 + prgSize, chrSize);
if (chrSize == 0) {
chrData = new Uint8Array(0x2000);
}
var mmc = ((0xF0 & header[6]) >> 4) | ((0x0F & header[7]) << 4);
var mirroringType = header[6] & 0x01;
if (mmc == 0) {
memoryMapper = new NES.Mapper0(prgData, chrData);
}
else if (mmc == 1) {
memoryMapper = new NES.Mapper1(prgData, chrData);
}
else if (mmc == 4) {
memoryMapper = new NES.Mapper4(prgData, chrData);
}
else {
screenContext.putImageData(id, 0, 0);
screenContext.fillText("Invalid Memory Mapper - " + mmc, 256, 50);
return;
}
if (mirroringType == 0) {
memoryMapper.mirror = ppu.Mirror.Horizontal;
}
else {
memoryMapper.mirror = ppu.Mirror.Vertical;
}
fileSelectorElement.style.display = 'none';
console.log('mmc - ' + mmc);
console.log('prgSize - ' + prgSize);
console.log('chrSize - ' + chrSize);
console.log('mirroring - ' + mirroringType);
cpu.reset();
ppu.init();
setInterval(frame, 16);
};
reader.readAsArrayBuffer(file);
};
var fileSelectorElement = document.getElementById('rom-file-selector');
fileSelectorElement.onchange = function() {
initRom(fileSelectorElement.files[0]);
};
document.onkeydown = function(event) {
// K, A
if (event.keyCode == 75) {
controller.setKeyDown(1);
}
// J, B
if (event.keyCode == 74) {
controller.setKeyDown(2);
}
// U, SELECT
if (event.keyCode == 85) {
controller.setKeyDown(3);
}
// I, START
if (event.keyCode == 73) {
controller.setKeyDown(4);
}
// W, UP
if (event.keyCode == 87) {
controller.setKeyDown(5);
}
// S, DOWN
if (event.keyCode == 83) {
controller.setKeyDown(6);
}
// A, LEFT
if (event.keyCode == 65) {
controller.setKeyDown(7);
}
// D, RIGHT
if (event.keyCode == 68) {
controller.setKeyDown(8);
}
// Up
if (event.keyCode == 38) {
selectedPixelY -= 1;
}
// Left
if (event.keyCode == 37) {
selectedPixelX -= 1;
}
// Right
if (event.keyCode == 39) {
selectedPixelX += 1;
}
// Down
if (event.keyCode == 40) {
selectedPixelY += 1;
}
};
document.onkeyup = function(event) {
// K, A
if (event.keyCode == 75) {
controller.setKeyUp(1);
}
// J, B
if (event.keyCode == 74) {
controller.setKeyUp(2);
}
// U, SELECT
if (event.keyCode == 85) {
controller.setKeyUp(3);
}
// I, START
if (event.keyCode == 73) {
controller.setKeyUp(4);
}
// W, UP
if (event.keyCode == 87) {
controller.setKeyUp(5);
}
// S, DOWN
if (event.keyCode == 83) {
controller.setKeyUp(6);
}
// A, LEFT
if (event.keyCode == 65) {
controller.setKeyUp(7);
}
// D, RIGHT
if (event.keyCode == 68) {
controller.setKeyUp(8);
}
// Left
if (event.keyCode == 37) {
shouldDrawBackground = !shouldDrawBackground;
}
// Right
if (event.keyCode == 39) {
shouldDrawSprites = !shouldDrawSprites;
}
};