PT
games/alis / src / Alis / Compiled / Templates / Ops12.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Alis;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** * The instructions that read the script stream in their own way (fn/opcodes_a.php, fn/opcodes_b.php): cret, the * switches, cdim, cputnat, clive, cload, cunload. Their handlers read the operands then call a core function * (clivin_id(), cunload_id(), cload_name()) that the compiled scripts call with the decoded operands as constants. */final class Ops12{    /**     * The switches' table is aligned on an even ADDRESS (the VM tests pc & 1), the decoder aligned it on an even offset     * of the script: a script loaded at an odd address is left to the VM (never seen: the allocations are even).     */    private static function evenGuard(string $php): string    {        return "if (\$o & 1) {\n    \$s->pc = Alis::\$opcode_pc;\n    \\App\\Alis\\readexec_opcode();\n} else {\n    "            . str_replace("\n", "\n    ", $php) . "\n}";    }    public static function register(): void    {        // cret: no operand, the return offset is on the script's stack (pc set by cret(): the loop dispatches it)        Templates::$op['cret'] = static fn (array $i, Context $c): string => '\App\Alis\cret();';        // cswitch1: a sorted table of (value, target); the first value >= D7 decides: equal = its target, else the end        Templates::$op['cswitch1'] = static function (array $i, Context $c): string {            [, $e, , , $cases] = $i['ops'][0];            $out = [$c->opername($e), '$v = \App\Alis\s16(Alis::$varD7);', 'do {'];            foreach ($cases as [$test, $target]) {                $out[] = sprintf('    if ($v <= %d) {', $test);                $out[] = sprintf('        if ($v == %d) {', $test);                $out[] = '            ' . Context::go($target);                $out[] = '        }';                $out[] = '        break;';                $out[] = '    }';            }            $out[] = '} while (false);';            return self::evenGuard(implode("\n", $out));        };        // cswitch2: D7 + base indexes a table of targets (D7 = the index * 2), out of range = the end        Templates::$op['cswitch2'] = static function (array $i, Context $c): string {            [, $e, $n, , $base, $targets] = $i['ops'][0];            $out = [$c->opername($e), sprintf('$v = \App\Alis\s16(Alis::$varD7 + %d);', $base), 'Alis::$varD7 = $v;',                sprintf('if ($v >= 0 && $v <= %d) {', $n), '    Alis::$varD7 = $v * 2;', '    switch ($v) {'];            foreach ($targets as $k => $target) {                $out[] = sprintf('        case %d: %s break;', $k, Context::go($target));            }            $out[] = '    }';            $out[] = '}';            return self::evenGuard(implode("\n", $out));        };        // cdim (versions 30 .. 59): the array's header and values written backwards under its offset        Templates::$op['cdim'] = static function (array $i, Context $c): ?string {            if (Alis::$platform->version < 30 || Alis::$platform->version >= 60) {                return null;            }            [, $offset, $counter, $byte2, $vals] = $i['ops'][0];            $n = '\App\Alis\\';            $off = self::s16($offset - 1);            $out = [sprintf('%sxwrite8($s->vram_org + %d, %d);', $n, $off, $counter)];            $off = self::s16($off - 1);            $out[] = sprintf('%sxwrite8($s->vram_org + %d, %d);', $n, $off, $byte2);            $wide = ($counter & 0x80) !== 0;            $count = $counter & 0xf;            foreach ($vals as $k => $v) {                if ($k < $count && !$wide) {                    $off = self::s16($off - 2);                    $out[] = sprintf('%sxwrite16($s->vram_org + %d, %d);', $n, $off, $v);                } else {                    $off = self::s16($off - 4);                    $out[] = sprintf('%sxwrite32($s->vram_org + %d, %d);', $n, $off, $v);                }            }            return implode("\n", $out);        };        // cputnat (version != 10, < 60): x, y, z, the picture, the element (D6), then put()        Templates::$op['cputnat'] = static function (array $i, Context $c): ?string {            if (Alis::$platform->version == 10 || Alis::$platform->version >= 60) {                return null;            }            [$x, $y, $z, $pic, $elem] = array_map(static fn ($o) => $o[1], $i['ops']);            return implode("\n", [                'Alis::$flagmain = 0;',                '\App\Alis\Img::$invert_x = \App\Alis\get_0x03_xinv(Alis::$script->vram_org);',                $c->opernameSaveD7($x), '\App\Alis\Img::$depx = \App\Alis\s16(Alis::$varD7);',                $c->opernameSaveD7($y), '\App\Alis\Img::$depy = \App\Alis\s16(Alis::$varD7);',                '\App\Alis\Img::$depz = 0;',                $c->opernameSaveD7($z), '\App\Alis\Img::$depz = \App\Alis\s16(Alis::$varD7);',                $c->opernameSaveD7($pic), $c->opernameSaveD6($elem),                '\App\Alis\Img::$numelem = \App\Alis\s8(Alis::$varD6);',                '\App\Alis\put(Alis::$varD7 & 0xffff);',            ]);        };        // clive (versions < 60): clivin_id() then the store target, unless the original returned before it        // (then the VM goes on reading the target's bytes as instructions: the same here, at that offset)        $live = static function (array $i, Context $c, string $before, int $idAt, array $target): string {            return $before . "\n" . sprintf('if (\App\Alis\clivin_id(%d)) {', $idAt) . "\n    "                . str_replace("\n", "\n    ", '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];' . "\n" . $c->storename($target))                . "\n} else {\n    " . Context::go($i['at'] + 3) . "\n}";        };        Templates::$op['clive'] = static function (array $i, Context $c) use ($live): ?string {            if (Alis::$platform->version >= 60) {                return null;            }            $id = self::s16($i['ops'][0][1]);            return $live($i, $c, 'Alis::$wcx = 0;' . "\n" . 'Alis::$wcy = 0;' . "\n" . 'Alis::$wcz = 0;' . "\n" . 'Alis::$varD7 = -1;', $id, $i['ops'][1][1]);        };        // cunload (versions < 60)        Templates::$op['cunload'] = static fn (array $i, Context $c): ?string => Alis::$platform->version >= 60 ? null            : sprintf('\App\Alis\cunload_id(%d);', self::s16($i['ops'][0][1]));        // cload (versions < 60, not the main script)        Templates::$op['cload'] = static function (array $i, Context $c): ?string {            [, $id, $name] = $i['ops'][0];            if (Alis::$platform->version >= 60 || $id == 0) {                return null;            }            return '\App\Alis\sys_sleep_interactive(Alis::$load_delay, Alis::$load_delay - 100);' . "\n"                . sprintf('\App\Alis\cload_name(%s);', Context::str($name));        };        // cfreadb / cfwriteb (versions 30 .. 59): the cell (script's or main script's variable) and the length        $cell = static fn (array $v): string => ($v[2] ? 'Alis::$basemain' : '$s->vram_org') . ' + ' . $v[1];        Templates::$op['cfreadb'] = static function (array $i, Context $c) use ($cell): ?string {            if (Alis::$platform->version < 30 || Alis::$platform->version >= 60) {                return null;            }            [$v, [, $length]] = $i['ops'];            // the original returns before reading the length when the table is empty: its bytes then run as code            return sprintf('$t = %s;' . "\n" . '$a = \App\Alis\cfreadb_addr($t);' . "\n"                . 'if ($a === null) {' . "\n    %s\n" . '} else {' . "\n" . '    \App\Alis\cfreadb_at($t, $a, %d);' . "\n" . '}',                $cell($v), Context::go($i['at'] + 1 + ($v[2] ? 4 : 2)), $length);        };        Templates::$op['cfwriteb'] = static function (array $i, Context $c) use ($cell): ?string {            if (Alis::$platform->version < 30 || Alis::$platform->version >= 60) {                return null;            }            [$v, [, $length]] = $i['ops'];            return sprintf('\App\Alis\cfwriteb_at(%s, %d);', $cell($v), $length);        };        // cneartyp (PC, versions 20 .. 59): the distance, the type (a word), the list, then crstent() with the target        Templates::$op['cneartyp'] = static function (array $i, Context $c): ?string {            if (Alis::$platform->version == 10 || Alis::$platform->version >= 60 || Alis::$platform->kind != \App\Alis\C::EPlatformPC) {                return null;            }            return implode("\n", [                'Alis::$fview = 0;',                $c->opername($i['ops'][0][1]),                'Alis::$valnorme = \App\Alis\mul32(Alis::$varD7, Alis::$varD7);',                sprintf('Alis::$varD7 = %d;', $i['ops'][1][1] & 0xffff),                '\App\Alis\sviewtyp_list();',                'Alis::$ptrent = 0;',                'Alis::$varD7 = Alis::$tablent[Alis::$ptrent];',                'if (-1 < Alis::$tablent[Alis::$ptrent]) { Alis::$ptrent++; }',                '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];',                $c->storename($i['ops'][2][1]),            ]);        };        // csetmouse (not version 10 on PC: the original returns between the two expressions there)        Templates::$op['csetmouse'] = static function (array $i, Context $c): ?string {            if (Alis::$platform->version == 10 && Alis::$platform->kind == \App\Alis\C::EPlatformPC) {                return null;            }            $head = $c->opername($i['ops'][0][1]) . "\n" . '$x = Alis::$varD7 & 0xffff;' . "\n" . $c->opername($i['ops'][1][1]) . "\n";            return \App\Alis\sys_is_dos_v3() ? $head . '\App\Alis\sys_set_mouse_offset($x, Alis::$varD7 & 0xffff);'                : $head . '\App\Alis\sys_set_mouse($x, Alis::$varD6 & 0xffff);   // (sic) the original reads D6';        };    }    private static function s16(int $v): int    {        $v &= 0xffff;        return $v >= 0x8000 ? $v - 0x10000 : $v;    }}