summaryrefslogtreecommitdiffstats
path: root/boot.php
blob: d2e7cfe7952cb3e381819b4316e8872d08f90464 (plain)
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
<?php


foreach (['../../autoload.php', '../vendor/autoload.php', 'vendor/autoload.php'] as $autoload) {
    $autoload = __DIR__.'/'.$autoload;
    if (file_exists($autoload)) {
        require $autoload;
        break;
    }
}
unset($autoload);

use GameBoy\Canvas\TerminalCanvas;
use GameBoy\Core;
use GameBoy\Keyboard;

if (PHP_VERSION_ID >= 70000) {
    set_exception_handler(function (\Throwable $exception) {
        fwrite(STDERR, $exception->getMessage() . PHP_EOL);
        exit(254);
    });
} else {
    set_exception_handler(function (Exception $exception) {
        fwrite(STDERR, $exception->getMessage() . PHP_EOL);
        exit(254);
    });
}

if (count($argv) < 2) {
    throw new \RuntimeException('You need to pass the ROM file name (Ex: drmario.rom)');
}

$filename = $argv[1];

if (!file_exists($filename)) {
    throw new \RuntimeException(sprintf('"%s" does not exist', $filename));
}

if (extension_loaded('xdebug')) {
    fwrite(STDERR, 'Running php-gameboy with Xdebug enabled reduces its speed considerably.'.PHP_EOL);
    fwrite(STDERR, 'You should consider to disable it before execute php-gameboy.'.PHP_EOL);
    sleep(1);
}

$rom = file_get_contents($filename);

$canvas = new TerminalCanvas();
$core = new Core($rom, $canvas);
$keyboard = new Keyboard($core);

$core->start();

if (($core->stopEmulator & 2) == 0) {
    throw new \RuntimeException('The GameBoy core is already running.');
}

if ($core->stopEmulator & 2 != 2) {
    throw new \RuntimeException('GameBoy core cannot run while it has not been initialized.');
}

$core->stopEmulator &= 1;
$core->lastIteration = (int) (microtime(true) * 1000);

while (true) {
    $core->run();
    $keyboard->check();
}