-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathc8.html
355 lines (279 loc) · 10.7 KB
/
c8.html
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<canvas id=C width=64></canvas><a href=//github.com/xem/chip8><img style="position:absolute;top:0;right:0;border:0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a><h1>1kb chip8 emulator</h1><h2>Commented cource code, with sound</h2><div><a href=.>Homepage</a><br><br>Choose a ROM:<br><a href="c8.html?1#ROM/C8PIC.ch8">C8PIC</a><br><a href="c8.html?2#ROM/IBM.ch8">IBM</a><br><a href="c8.html?3#ROM/Rocket2.ch8">Rocket2</a> (press Y to launch)<br><a href="c8.html?4#ROM/X-MIRROR.ch8">X-MIRROR</a> (press 2,4,6,8 to draw)<br><a href="c8.html?5#ROM/TAPEWORM.ch8">TAPEWORM</a> (Y to start, 2,4,6,8 to move)<br><a href="c8.html?6#ROM/TIMEBOMB.ch8">TIMEBOMB</a> (set up with 2 and 8, start with 5)<br><a href="c8.html?8#ROM/AIRPLANE.ch8">AIRPLANE</a> (send bombs with 2)<br><a href="c8.html?10#ROM/BLITZ.ch8">BLITZ</a> (send bombs with 2)<br><a href="c8.html?11#ROM/BREAKOUT.ch8">BREAKOUT</a> (move with 4 and 6)<br><a href="c8.html?12#ROM/BRIX.ch8">BRIX</a> (move with 4 and 6)<br><a href="c8.html?13#ROM/CAVE.ch8">CAVE</a> (start with Y, move with 2,4,6,8)<br><a href="c8.html?14#ROM/CONNECT4.ch8">CONNECT4</a><br><a href="c8.html?15#ROM/FIGURES.ch8">FIGURES</a><br><a href="c8.html?16#ROM/FILTER.ch8">FILTER</a><br><a href="c8.html?17#ROM/GUESS.ch8">GUESS</a><br><a href="c8.html?18#ROM/HIDDEN.ch8">HIDDEN</a><br><a href="c8.html?19#ROM/INVADERS.ch8">INVADERS</a><br><a href="c8.html?20#ROM/KALEID.ch8">KALEID</a><br><a href="c8.html?21#ROM/LANDING.ch8">LANDING</a><br><a href="c8.html?22#ROM/MAZE.ch8">MAZE</a><br><a href="c8.html?23#ROM/MERLIN.ch8">MERLIN</a><br><a href="c8.html?24#ROM/MISSILE.ch8">MISSILE</a><br><a href="c8.html?25#ROM/PADDLES.ch8">PADDLES</a><br><a href="c8.html?26#ROM/PONG (1P).ch8">PONG (1P)</a><br><a href="c8.html?27#ROM/PONG.ch8">PONG</a><br><a href="c8.html?28#ROM/PONG2.ch8">PONG2</a><br><a href="c8.html?29#ROM/PUZZLE.ch8">PUZZLE</a><br><a href="c8.html?30#ROM/ROCKET.ch8">ROCKET</a><br><a href="c8.html?31#ROM/SOCCER.ch8">SOCCER</a><br><a href="c8.html?32#ROM/SPACEF.ch8">SPACEF</a><br><a href="c8.html?33#ROM/SQUASH.ch8">SQUASH</a><br><a href="c8.html?34#ROM/SYZYGY.ch8">SYZYGY</a><br><a href="c8.html?35#ROM/TANK.ch8">TANK</a><br><a href="c8.html?36#ROM/TETRIS.ch8">TETRIS</a><br><a href="c8.html?37#ROM/TICTAC.ch8">TICTAC</a><br><a href="c8.html?38#ROM/TRON.ch8">TRON</a><br><a href="c8.html?39#ROM/UFO.ch8">UFO</a><br><a href="c8.html?40#ROM/VBRIX.ch8">VBRIX</a><br><a href="c8.html?41#ROM/VERS.ch8">VERS</a><br><a href="c8.html?42#ROM/WALL.ch8">WALL</a><br><a href="c8.html?43#ROM/WIPEOFF.ch8">WIPEOFF</a><br><br></div><style>*{text-indent:-9999;color:#fff;font-family:arial}h1,h2,div{color: #000;text-indent:0}a{color:blue}h1,h2,canvas{position:absolute;left:10px}canvas{overflow:hidden;top:100px;-webkit-transform:scale(4);transform:scale(4);-webkit-transform-origin:0 0;transform-origin:0 0}h1{top:0;text-indent:0}h2{top:40px;text-indent:0}div{position:absolute;top:250px}</style><script>
/** Initialization **/
// Vars
var imageData, audio, oscillator, i, memory, stack, pc, sp, I, timer, sound_timer, keys, keys_pressed, xhr, opcodes, tmp, opcode, interval, prefix, NNN, NN, N, X, Y, VX, VY, value, i, j, x, y;
// Canvas
// The context2D (C) already exists
imageData = (C = C.getContext("2d")).createImageData(64, 32);
// The canvas is white. The pixels are black. We just have to toggle their opacity to render black or white
for(i=64*32*4; i--;){
imageData.data[i] = i%4-3 ? 0 : 255;
}
// Audio
audio = new (AudioContext = this.AudioContext || webkitAudioContext || 0);
// Memory
memory = [];
// Sprites of the characters 0-9,A-F (8*5px) are placed in the 160 first c8es of memory: 0xF0, 0x90, ...
for(i=80; i--;){
memory[i] = eval("0x" + "F999F26227F1F8FF1F1F99F11F8F1FF8F9FF1244F9F9FF9F1FF9F99E9E9EF888FE999EF8F8FF8F88"[i] + "0");
}
// Registers
V = new Uint8Array(new ArrayBuffer(16));
// Stack
stack = [];
// Program counter
pc = 512;
// Stack pointer
sp = 0;
// Address register
I = 0;
// Timer
timer = 0;
// Sound timer
sound_timer = 0;
/** Detect keys pressed **/
// key map (keycodes to use for each chip8 key: numpad0-9 + QWERTY)
keys = [96,103,104,105,100,101,102,97,98,99,81,87,69,82,84,89];
// Keys currently pressed
keys_pressed = [];
/** Load ROM **/
xhr = new XMLHttpRequest;
xhr.open('GET', location.hash.slice(1));
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(){
// Put ROM in memory
for(i=(tmp = new Uint8Array(xhr.response)).length; i--;){
memory[i+512] = tmp[i];
}
/** Loop **/
interval = setInterval(function(){
// Decrement the timers while > 0
timer && timer--;
sound_timer && sound_timer--;
// Start sound if sound timer > 0
if(audio && sound_timer && !oscillator){
// New oscillator
oscillator = audio.createOscillator();
// Cross-browser start/stop
if(!oscillator.start){
oscillator.noteOn = oscillator.start;
}
if(!oscillator.stop){
oscillator.noteOff = oscillator.stop;
}
// Start sound
oscillator.connect(audio.destination);
oscillator.type = 3;
oscillator.start(0);
}
// Stop sound if sound timer == 0
if(audio && !sound_timer && oscillator){
oscillator.stop(0);
oscillator = 0;
}
// Execute 4 opcodes
for(opcodes = 4; opcodes--;){
// Read opcode
opcode = memory[pc] << 8 | memory[pc+1];
//console.log("@"+pc + ": "+("00" + opcode.toString(16)).slice(-4));
// Read opcode fields
prefix = opcode >> 12;
NNN = opcode & 0xFFF;
NN = opcode & 0xFF;
N = opcode & 0xF;
X = opcode >> 8 & 0xF;
VX = V[X];
VY = V[opcode >> 4 & 0xF];
// 00E0: clear
if(opcode == 0xE0){
for(i = 64*32*4; i--;){
imageData.data[i] = i%4-3 ? 0 : 255;
}
}
// 00EE: return from subprogram
if(opcode == 0xEE){
pc = stack.pop();
}
// 1NNN: jump to NNN
if(prefix == 1){
pc = NNN - 2;
}
// 2NNN: call subprogram at NNN
if(prefix == 2){
stack.push(pc);
pc = NNN - 2;
}
// 3XNN: ignore next opcode if VX == NN
// 4XNN: ignore next opcode if VX != NN
// 5XY0: ignore next opcode if VX == VY
// 9XY0: ignore next opcode if VX != VY
// EX9E: ignore next opcode if VX is pressed
// EXA1: ignore next opcode if VX is not pressed
if((prefix == 3 && VX == NN)
|| (prefix == 4 && VX != NN)
|| (prefix == 5 && VX == VY)
|| (prefix == 9 && VX != VY)
|| (prefix == 0xE && ((NN == 0x9E && keys_pressed[VX]) || (NN == 0xA1 && !keys_pressed[VX])))
){
pc += 2;
}
// 6XNN: set VX to NN
if(prefix == 6){
V[X] = NN;
}
// 7XNN: add NN to VX
if(prefix == 7){
V[X] = VX + NN;
}
// 8XYN:
if(prefix == 8){
// 8XY0: set VX to VY
if(!N){
V[X] = VY;
}
// 8XY1: set VX to VX OR VY
if(N == 1){
V[X] |= VY;
}
// 8XY2: set VX to VX AND VY
if(N == 2){
V[X] &= VY;
}
// 8XY3: set VX to VX XOR VY
if(N == 3){
V[X] ^= VY;
}
// 8XY4: add VY to VX, put carry in VF
if(N == 4){
V[0xF] = +((V[X] += VY) > 0xFF);
}
// 8XY5: substract VY to VX, put NOT borrow in VF
if(N == 5){
V[0xF] = +((V[X] -= VY) >= 0);
}
// 8XY6: right shift VX, put shifted bit in VF
if(N == 6){
V[0xF] = VX & 0x1;
V[X] /= 2;
}
// 8XY7: VX = VY - VX, put NOT borrow in VF
if(N == 7){
V[0xF] = +((V[X] = VY - VX) >= 0);
}
// 8XYE: left shift VX, put shifted bit in VF
if(N == 0xE){
V[0xF] = VX >> 7;
V[X] *= 2;
}
}
// ANNN: set I to NNN
if(prefix == 0xA){
I = NNN;
}
// BNNN: jump to NNN + V0
if(prefix == 0xB){
pc = NNN + V[0];
}
// CXNN: set VX to a random number < NN
if(prefix == 0xC){
V[X] = Math.floor(Math.random() * NN);
}
// DXYN: draw the 8*N px sprite stored at address I at coordinates [X:Y]
if(prefix == 0xD){
V[0xF] = 0;
// Loop on the N lines of the sprite
for(i = N; i--;){
// Loop on the 8 pixels of the current line of the sprite
for(j = 8; j--;){
// Get the value of the pixel
value = (memory[I+i]>>(7-j))&0x1;
// Get the coordinates of the pixel
x = VX + j;
y = VY + i;
// If the pixel is on
if(value){
// If the pixel is out of bounds, wrap
if(x > 63){
x -= 63;
}
if(y > 31){
y -= 31;
}
// If the pixel is already white (opacity == 0), make it black and set VF to 1. Else, make it white.
tmp = 4*(64*y+x)+3;
if(imageData.data[tmp]){
imageData.data[tmp] = 0;
}
else{
imageData.data[tmp] = 255;
V[0xF] = 1;
}
}
}
}
}
// FXNN:
if(prefix == 0xF){
// FX07: set VX to the timer value
if(NN == 0x07){
V[X] = timer;
}
// FX0A: prompt, store key pressed in VX
if(NN == 0x0A){
// Loop until VX != -1
if(!~(V[X] = keys_pressed.c8Of(!0))){
pc -= 2;
}
}
// FX15: set timer to VX
if(NN == 0x15){
timer = VX;
}
// FX18: set sound timer to VX
if(NN == 0x18){
sound_timer = VX;
}
// FX1E: add VX to I, set VF to I's overflow (I>0xFFF)
if(NN == 0x1E){
I += VX;
V[0xF] = +(I > 0xFFF);
I &= 0xFFF;
}
// FX29: set I to the character VX
if(NN == 0x29){
I = VX * 5;
}
// FX33: store the decimal value of VX in memory at address I (hundreds), I+1 (dozens), I+2 (units)
if(NN == 0x33){
for(i = 3; i--;){
memory[I+i]=(""+VX)[i];
}
}
// FX55: store V0 to VX in memory from address I
if(NN == 0x55){
for(i = X; i--;){
memory[I + i] = V[i];
}
}
// FX65: load V0 to VX from memory at address I
if(NN == 0x65){
for(i = X; i--;){
V[i] = memory[I + i];
}
}
}
// Next instruction
pc += 2;
}
// Refresh screen
C.putImageData(imageData, 0, 0);
},16);
}
onkeydown = function(e){
keys_pressed[keys.indexOf(e.keyCode)] = !0;
}
onkeyup = function(e){
keys_pressed[keys.indexOf(e.keyCode)] = !1;
}
</script>