RU
games/alis / src / Alis / Compiled / Templates / Ops04.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** * Opcodes 0x61 to 0x75 (fn/opcodes_a.php): csend, the scan / interaction flags, callentity, cpalette, ctiming, the * file instructions. Not done: czap, cexplode, cding, cnoise, cinitab, cdefcolor, cfcreat, cfdel (unused or rare). */final class Ops04{    public static function register(): void    {        $n = '\App\Alis\\';        // no operand: the VM's own function (it reads nothing from the stream)        foreach (['cscanon', 'cscanoff', 'cinteron', 'cinteroff', 'cscanclr', 'callentity', 'cfclose'] as $name) {            Templates::$op[$name] = static fn (array $i, Context $c): string => $n . $name . '();';        }        Templates::$op['ctiming'] = static fn (array $i, Context $c): string => $c->opername($i['ops'][0][1]) . "\n"            . 'Alis::$ctiming = Alis::$varD7 & 0xff;';        Templates::$op['cfwritev'] = static fn (array $i, Context $c): string => $c->opername($i['ops'][0][1]) . "\n"            . 'if (Alis::$platform->version >= 60) {' . "\n"            . '    $v = Alis::$varD7;' . "\n"            . '    fwrite(Alis::$fp, chr(($v >> 24) & 0xff) . chr(($v >> 16) & 0xff) . chr(($v >> 8) & 0xff) . chr($v & 0xff));' . "\n"            . '} else {' . "\n"            . '    $v = Alis::$varD7 & 0xffff;' . "\n"            . '    fwrite(Alis::$fp, chr($v >> 8) . chr($v & 0xff));' . "\n"            . '}';        // cfreadv: read 2 bytes, D7, then cstore_continue() (sd7 / oldsd7 swapped, the store target)        Templates::$op['cfreadv'] = static fn (array $i, Context $c): string => implode("\n", [            '$b = fread(Alis::$fp, 2);',            '$b = str_pad($b === false ? \'\' : $b, 2, "\0");',            'Alis::$buffer[0] = ord($b[0]);',            'Alis::$buffer[1] = ord($b[1]);',            'Alis::$varD7 = ' . $n . 's16((ord($b[0]) << 8) | ord($b[1]));',            '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];',            $c->storename($i['ops'][0][1]),        ]);        Templates::$op['cfopen'] = static function (array $i, Context $c): string {            [, $path, $a, $b] = $i['ops'][0];            if ($path !== null) {                $head = '$path = Alis::$platform->path . ' . \App\Alis\Compiled\Context::str($path) . ';' . "\n" . '$mode = ' . ((int) $a & 0xffff) . ';';            } else {                $head = $c->opernameSwap($a) . "\n" . $c->opername($b) . "\n"                    . '$path = Alis::$platform->path . \App\Alis\mem_str(Alis::$sd7);' . "\n" . '$mode = Alis::$varD7 & 0xffff;';            }            return $head . "\n" . '\App\Alis\afopen($path, $mode);' . "\n"                . 'if (Alis::$fp === null || Alis::$fp === false) {' . "\n"                . '    \App\Alis\alis_error(\App\Alis\C::ALIS_ERR_FOPEN, $path);' . "\n"                . '}';        };        Templates::$op['cpalette'] = static fn (array $i, Context $c): string => 'Alis::$flagmain = 0;' . "\n"            . $c->opername($i['ops'][0][1]) . "\n"            . <<<'PHP'$palidx = \App\Alis\s16(Alis::$varD7);do {    if (Alis::$platform->version == 10) {        $paldata = \App\Alis\adresdes($palidx);        if (Alis::$platform->kind != \App\Alis\C::EPlatformPC && \App\Alis\xread8($paldata) == 0xfe) {            \App\Alis\topalette($paldata, 0);        }    } elseif (\App\Alis\dos_pal_active()) {        $addr = $palidx < 0 ? 0 : \App\Alis\adresdes($palidx);        \App\Alis\dos_cpalette($palidx, $palidx < 0 ? 0 : \App\Alis\s32($addr + \App\Alis\xread32($addr)));    } elseif ($palidx < 0) {        if (Alis::$platform->version < 60 || $palidx > -3) {            \App\Alis\restorepal($palidx, 0);        }    } else {        $addr = \App\Alis\adresdes($palidx);        $paldata = \App\Alis\s32($addr + \App\Alis\xread32($addr));        if (Alis::$platform->version >= 60 && \App\Alis\xread8($paldata) != 3) {            break;        }        \App\Alis\topalette($paldata, 0);    }} while (false);PHP;        Templates::$op['csend'] = static function (array $i, Context $c) use ($n): string {            [, $len, $to, $vals] = $i['ops'][0];            $u = ++$c->tmp;            $k = "\$k$u";            $vr = "\$vr$u";            $sc1 = "\$sa$u";            $sc2 = "\$sb$u";            $out = [$k . ' = -1;', $c->opername($to), "\$en$u = {$n}s16(Alis::\$varD7);", 'do {',                "if (\$en$u == -1) { break; }",                "$vr = {$n}xread32(Alis::\$atent + \$en$u);",                "if ($vr == 0) { break; }",                "if (({$n}get_0x24_scan_inter($vr) & 1) != 0) { break; }",                "\$lim$u = Alis::\$platform->version >= 30 ? -0x3f : -0x35;",                "\$of$u = {$n}xread16({$n}get_0x14_script_org_offset($vr) + 0x16);",                "$sc1 = {$n}get_0x1c_scan_clr($vr);"];            foreach ($vals as $j => $v) {                $out[] = $c->opername($v);                $out[] = "$sc2 = {$n}s16($sc1 + 2);";                $out[] = "if (\$lim$u < $sc2) { $sc2 = {$n}s16($sc2 - \$of$u); }";                $out[] = "if ($sc2 == {$n}get_0x1e_scan_clr($vr)) {";                $out[] = "    if (Alis::\$platform->version == 10) { $sc2 = {$n}get_0x1c_scan_clr($vr); }";                $out[] = "    $k = $j;";                $out[] = "    {$n}set_0x1c_scan_clr($vr, $sc2);";                $out[] = '    break;';                $out[] = '}';                $out[] = "{$n}xwrite16($vr + $sc1, Alis::\$varD7);";                $out[] = "$sc1 = $sc2;";                if ($j === $len) {                    $out[] = "{$n}set_0x1c_scan_clr($vr, $sc2);";                    $out[] = "{$n}set_0x24_scan_inter($vr, {$n}get_0x24_scan_inter($vr) | 0x80);";                    $out[] = "goto done$u;";                }            }            $out[] = '} while (false);';            $out[] = "switch ($k) {";            foreach ($vals as $j => $v) {                $out[] = 'case ' . ($j - 1) . ':';                $out[] = $c->opername($v);            }            $out[] = '}';            $out[] = "done$u:;";            return implode("\n", $out);        };    }}