-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.php
455 lines (434 loc) · 12.2 KB
/
chip8.php
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
final class chip8 {
protected $argv;
public $sdl;
public $renderer;
public $pixels;
public $pixelf;
public $event;
public $timer;
public $sound;
public $keypad;
public $keydown;
public $keyup;
public $keymap;
public $memory = [];
public $lmemory = [];
public $V = [];
public $I = 0;
public $cpu = 0x200; //cpu starts from 0x200
public $stack = [];
public $load = 0;
public $stime;
public $close = 0;
public $time = 0;
public function __construct($args) {
$this->stime = time();
$this->argv = $args;
$this->sdl = SDL_CreateWindow('PHP Chip 8', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 325, SDL_WINDOW_SHOWN);
$this->renderer = SDL_CreateRenderer($this->sdl, 0, SDL_RENDERER_SOFTWARE);
$this->event = new SDL_Event;
$this->pixels = [];
$this->pixelf = [];
SDL_SetRenderDrawColor($this->renderer, 0, 0, 0, 255);
SDL_RenderClear($this->renderer);
for($i=0;$i<16;$i++) {
$this->V[$i] = 0;
}
$this->timer = 0;
$this->sound = 0;
$this->memory =
[
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
];
$this->keymap =
[
"49"=>0x01, "50"=>0x02, "51"=>0x03, "52"=>0x0C,
"113"=>0x04, "119"=>0x05, "101"=>0x06, "114"=>0x0D,
"97"=>0x07, "115"=>0x08, "100"=>0x09, "102"=>0x0E,
"122"=>0x0A, "120"=>0x00, "99"=>0x0B, "118"=>0x0F
];
}
public function run() {
//system('stty cbreak -echo');
//$stdin = fopen('php://stdin', 'r');
$this->loadProgram();
while(1) {
$instruction = dechex(($this->memory[$this->cpu] << 8) + $this->memory[$this->cpu + 1]);
// echo $instruction;
$this->step();
// $c = ord(fgetc($stdin));
while( SDL_PollEvent( $this->event ) ){
/* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
switch($this->event->type){
case SDL_KEYDOWN:
if(isset($this->keymap[$this->event->key->keysym->sym])) {
$this->keydown = $this->keymap[$this->event->key->keysym->sym];
$this->keyup = "";
}
break;
case SDL_KEYUP:
$this->keyup = $this->keydown;
$this->keydown = "";
break;
case 512:
if($this->event->window->event == 14) {
die();
}
if($this->event->window->event == 15 && $this->close == 1) {
die();
}
break;
default:
break;
}
}
if(time()-$this->stime > 3 && $this->load == 0) {
$this->load = 1;
$this->memory =
[
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
];
$this->timer = 0;
$this->sound = 0;
$this->stack = [];
$this->I = [];
$this->V = [];
SDL_SetRenderDrawColor($this->renderer, 0, 0, 0, 255);
SDL_RenderClear($this->renderer);
$this->pixels = [];
$this->pixelf = [];
$this->cpu = 0x200;
$this->loadProgram();
}
}
}
public function loadProgram() {
if($this->load == 0) {
$file = "00E0A2486000611E6200D202D21272083240120A6000613E6202A24AD02ED12E720ED02ED12EA258600B6108D01F700AA267D01F700AA276D01F7003A285D01F700AA294D01F1246FFFFC0C0C0C0C0C0C0C0C0C0C0C0C0C0FE818181818181FE8080808080808081818181818181FF81818181818181000080800000008080808080808080FE818181818181FE808080808080807E8181818181817E8181818181817EFF";
$n = 0;
$i = 0;
for($i=0,$max=strlen($file);$i<$max;$i++) {
$this->memory[0x200 + $n] = hexdec($file[$i].$file[$i+1]);
$i++;
$n++;
}
} else {
$file = file_get_contents($this->argv[1]);
for($i=0,$max=strlen($file);$i<$max;$i++) {
$this->memory[0x200 + $i] = (ord($file[$i]) & 0xFF);
}
}
}
public function step() {
$ctime = floor(microtime(true)*100);
//echo $ctime; die();
if($this->timer > 0 && $this->time<$ctime) {
$this->timer--;
$this->time = $ctime;
} else if($this->timer > 0 ){
//echo $this->timer." > 0 && ".$this->time."<$ctime\n";
usleep(500);
}
if($this->sound > 0 && $this->time<$ctime) {
$this->sound--;
$this->time = $ctime;
} else if($this->sound > 0 ){
//echo $this->timer." > 0 && ".$this->time."<$ctime\n";
`aplay beep.wav 2>/dev/null >/dev/null &`;
$this->sound = 0;
}
// $this->keyup = "";
$instruction = ($this->memory[$this->cpu] << 8) + $this->memory[$this->cpu + 1];
if($this->load == 1) {
echo dechex($instruction)." - ".$this->cpu."\n";
}
$this->cpu += 2;
// Extract common operands
$x = ($instruction & 0x0F00) >> 8;
$y = ($instruction & 0x00F0) >> 4;
$kk = $instruction & 0x00FF;
$nnn = $instruction & 0x0FFF;
$n = $instruction & 0x000F;
switch ($instruction & 0xF000) {
case 0x0000:
switch ($instruction) {
case 0x00E0:
SDL_SetRenderDrawColor($this->renderer, 0, 0, 0, 255);
SDL_RenderClear($this->renderer);
$this->pixels = [];
$this->pixelf = [];
break;
case 0x00EE:
// Return from subroutine
$this->cpu = array_pop($this->stack);
break;
default:
throw new RuntimeException("Unsupported instruction at 0x".dechex($this->cpu)." -> 0x".dechex($instruction));
}
break;
case 0x1000:
// Jump to nnn
if($nnn == $this->cpu-2) {
usleep(50000);
}
$this->cpu = $nnn;
break;
case 0xB000:
$this->cpu = $nnn+$this->V[0];
case 0x2000:
//call subr at nnn
$this->stack[] = $this->cpu;
$this->cpu = $nnn;
break;
case 0x3000:
// skip if vx=kk
if($this->V[$x] === $kk) {
$this->cpu += 2; // instr is 3 bytes
}
break;
case 0x5000:
if($this->V[$x] == $this->V[$y]) {
$this->cpu += 2;
}
break;
case 0x4000:
// skip if vx!=kk
if($this->V[$x] != $kk) {
$this->cpu += 2;
}
break;
case 0x6000:
$this->V[$x] = $kk;
break;
case 0x7000:
$this->V[$x] += $kk;
while($this->V[$x] > 255) {
$this->V[$x] -= 256;
}
break;
case 0x8000:
switch ($instruction & 0xF00F) {
case 0x8000:
$this->V[$x] = $this->V[$y];
break;
case 0x8001:
$this->V[$x] = ($this->V[$x] | $this->V[$y]);
break;
case 0x8002:
$this->V[$x] = $this->V[$x] & $this->V[$y];
break;
case 0x8003:
$this->V[$x] = $this->V[$x] ^ $this->V[$y];
break;
case 0x8004:
$this->V[$x] += $this->V[$y];
if($this->V[$x] > 255) {
$this->V[$x] -= 256;
$this->V[15] = 1;
} else {
$this->V[15] = 0;
}
break;
case 0x8005:
$this->V[$x] -= $this->V[$y];
if($this->V[$x] < 0) {
$this->V[$x] += 256;
$this->V[15] = 0;
} else {
$this->V[15] = 1;
}
break;
case 0x8007:
$this->V[$x] = $this->V[$y]-$this->V[$x];
if($this->V[$x] < 0) {
$this->V[$x] += 256;
$this->V[15] = 0;
} else {
$this->V[15] = 1;
}
break;
case 0x8006:
$bin = decbin($this->V[$x]);
if(substr($bin, -1) == 1) { $this->V[15] = 1; } else { $this->V[15] = 0; }
$this->V[$x] = $this->V[$x] >> 1;
break;
case 0x800E:
$bin = str_pad(decbin($this->V[$x]), 8, "0", STR_PAD_LEFT);
if(substr($bin, 0, 1) == 1) { $this->V[15] = 1; } else { $this->V[15] = 0; }
$this->V[$x] = $this->V[$y] << 1;
break;
default:
throw new RuntimeException("Unsupported instruction at 0x".dechex($this->cpu)." -> 0x".dechex($instruction)." | $instruction");
}
break;
case 0x9000:
if($this->V[$x] !== $this->V[$y]) {
$this->cpu += 2;
}
break;
case 0xA000:
$this->I = $nnn;
break;
case 0xC000:
$this->V[$x] = rand(0,255) & $kk;
break;
case 0xD000:
//display/collision
$collision = $this->sdlSprite($this->V[$x],$this->V[$y],$this->I,$n);
SDL_RenderPresent($this->renderer);
usleep(14000);
$this->V[0xF] = $collision ? 1 : 0;
break;
case 0xE000:
switch ($instruction & 0xF0FF) {
case 0xE09E:
if($this->V[$x] == $this->keydown) { // if VX == keypress
$this->cpu += 2;
}
usleep(20000);
break;
case 0xE0A1:
if($this->V[$x] != $this->keydown) { // if VX != keypress
$this->cpu += 2;
}
//usleep(20000);
break;
default:
throw new RuntimeException("Unsupported instruction at 0x".dechex($this->cpu)." -> 0x".dechex($instruction));
}
break;
case 0xF000:
switch ($instruction & 0xF0FF) {
case 0xF00A:
if($this->keyup == "") {
$this->cpu -= 2;
sleep(1);
} else {
$this->V[$x] = $this->keyup;
}
break;
case 0xF01E:
$this->I += $this->V[$x];
break;
case 0xF055:
for($i=0;$i<=$x;$i++) {
$this->memory[$this->I+$i] = $this->V[$i];
}
$this->I = $this->I+$x+1;
break;
case 0xF015:
$this->timer = $this->V[$x];
break;
case 0xF007:
$this->V[$x] = $this->timer;
break;
case 0xF018:
$this->sound = $this->V[$x];
break;
case 0xF065:
for($cv=0;$cv<=$x;$cv++) {
$this->V[$cv] = $this->memory[$this->I+$cv];
}
$this->I += $x+1;
break;
case 0xF029:
$this->I = $this->V[$x]*5;
//echo dechex($this->memory[$this->I]).",".$this->memory[$this->I+1].",".$this->memory[$this->I+2].",".$this->memory[$this->I+3].",".$this->memory[$this->I+4];
break;
case 0xF033:
//echo $this->V[$x]."\n";
$this->memory[$this->I] = floor($this->V[$x]/100);
$this->memory[$this->I+1] = floor($this->V[$x]/10) % 10;
$this->memory[$this->I+2] = ($this->V[$x] % 100) % 10;
break;
default:
throw new RuntimeException("Unsupported instruction at 0x".dechex($this->cpu)." -> 0x".dechex($instruction)." | $instruction");
}
break;
default:
throw new RuntimeException("Unsupported instruction at 0x".dechex($this->cpu)." -> 0x".dechex($instruction));
}
}
public function sdlSprite($x, $y, $address, $nbytes) {
$collision = false;
for($line=0;$line<$nbytes;$line++) {
// echo "$address $line \n";
$bits = $this->memory[$address + $line]; // get sprite line bits
$bits = str_pad(base_convert($bits, 10, 2), 8, 0, STR_PAD_LEFT);
for($bit=0;$bit<8;$bit++) {
if(isset($bits[$bit])) {
$tb = $bits[$bit];
} else {
$tb = 0;
}
// echo "$tb $x+$bit $y+$line\n";
//if($bits & 1) {
if($tb != 0) {
// if($this->display->get($x+$bit,$y+$line)) {
if(isset($this->pixelf[$x+$bit][$y+$line])) {
if($this->pixelf[$x+$bit][$y+$line] == 1) {
$col = 1;
} else {
$col = 0;
}
} else {
$col = 0;
}
if($col == 1) {
$collision = true;
SDL_SetRenderDrawColor($this->renderer, 0, 0, 0, 255);
$this->pixelf[$x+$bit][$y+$line] = 0;
SDL_RenderFillRect($this->renderer, $this->pixels[$x+$bit][$y+$line]);
//SDL_RenderPresent($this->renderer);
} else {
SDL_SetRenderDrawColor($this->renderer, 255, 255, 255, 255);
if(!isset($this->pixels[$x+$bit][$y+$line])) {
$this->pixels[$x+$bit][$y+$line] = new SDL_Rect($x*10+$bit*10, ($y+$line)*10, 10, 10);
}
$this->pixelf[$x+$bit][$y+$line] = 1;
SDL_RenderFillRect($this->renderer, $this->pixels[$x+$bit][$y+$line]);
//SDL_RenderPresent($this->renderer);
}
// $bits >>= 1;
}
}
// echo $this->display->frame();
//
}
return $collision;
}
}
$vm = new chip8($argv);
$vm->run();
?>