JA
games/alis / src / Alis / fn / runtime.php · commit 9c722cf
<?phpnamespace App\Alis;// Runtime helpers of the ALIS port: C integer semantics on 32/64-bit PHP, the unified memory// (VM RAM + host buffers in one binary string, Alis::$mem), script byte-code reads.// See docs/porting.md.// ---------------------------------------------------------------------------------------------// Integers// ---------------------------------------------------------------------------------------------function s8(int $v): int{    return (($v & 0xff) ^ 0x80) - 0x80;}function s16(int $v): int{    return (($v & 0xffff) ^ 0x8000) - 0x8000;}/** Wraps any int (or float from a 32-bit overflow) to a signed 32-bit int. */function s32(int|float $v): int{    if (PHP_INT_SIZE === 8) {        if (is_float($v)) {            $v = fmod($v, 4294967296.0);            $v = (int) $v;        }        return (($v & 0xffffffff) ^ 0x80000000) - 0x80000000;    }    if (is_int($v)) {        return $v;    }    $v = fmod($v, 4294967296.0);    if ($v >= 2147483648.0) {        $v -= 4294967296.0;    } elseif ($v < -2147483648.0) {        $v += 4294967296.0;    }    return (int) $v;}/** u32 value as a non-negative number (int on 64-bit, float above 2^31 on 32-bit). For display / compares only. */function u32v(int $v): int|float{    return $v < 0 ? $v + 4294967296 : $v;}function mul32(int $a, int $b): int{    if (PHP_INT_SIZE === 8) {        // exact in 64 bits when both fit 32 bits        return s32(s32($a) * s32($b));    }    $r = $a * $b;    if (is_int($r)) {        return $r;    }    // split multiply: (ah*2^16 + al) * b, keeping the low 32 bits    $al = $a & 0xffff;    $ah = ($a >> 16) & 0xffff;    $lo = $al * ($b & 0xffff);    $mid = s32($ah * ($b & 0xffff)) + s32($al * (($b >> 16) & 0xffff));    return s32((float) $lo + (float) (s32($mid) & 0xffff) * 65536.0);}function shl32(int $a, int $n): int{    $n &= 31;    if (PHP_INT_SIZE === 8) {        return s32($a << $n);    }    return $a << $n; // 32-bit PHP shifts within the machine word}/** Logical shift right of a 32-bit value. */function shr32(int $a, int $n): int{    $n &= 31;    if ($n === 0) {        return s32($a);    }    if (PHP_INT_SIZE === 8) {        return ($a & 0xffffffff) >> $n;    }    return ($a >> $n) & ~(-1 << (32 - $n));}/** Unsigned 32-bit a < b. */function ult(int $a, int $b): bool{    if (PHP_INT_SIZE === 8) {        return ($a & 0xffffffff) < ($b & 0xffffffff);    }    return ($a ^ PHP_INT_MIN) < ($b ^ PHP_INT_MIN);}function ule(int $a, int $b): bool{    return $a === $b || ult($a, $b);}function udiv32(int $a, int $b): int{    if ($b === 0) {        return 0;    }    if (PHP_INT_SIZE === 8) {        return s32(intdiv($a & 0xffffffff, $b & 0xffffffff));    }    return s32(floor(u32v($a) / u32v($b)));}function umod32(int $a, int $b): int{    if ($b === 0) {        return 0;    }    if (PHP_INT_SIZE === 8) {        return s32(($a & 0xffffffff) % ($b & 0xffffffff));    }    return s32(fmod(u32v($a), u32v($b)));}/** C integer division (truncation toward zero), 0 when dividing by zero (the C would trap). */function cdiv(int $a, int $b): int{    if ($b === 0) {        return 0;    }    if ($a === PHP_INT_MIN && $b === -1) {        return $a;    }    return intdiv($a, $b);}function cmod(int $a, int $b): int{    return $b === 0 ? 0 : $a % $b;}// ---------------------------------------------------------------------------------------------// Unified memory// ---------------------------------------------------------------------------------------------/** * Allocates a zeroed host block past the VM RAM; returns its offset in Alis::$mem. The memory string grows by * steps of at least 1 MB (each growth reallocates megabytes: rare is better, the browser never gives them back). */function host_alloc(int $size): int{    $len = strlen(Alis::$mem);    if (Alis::$host_top === 0 || Alis::$host_top > $len) {        Alis::$host_top = $len; // a new memory (alis_init): blocks start after it    }    $off = (Alis::$host_top + 15) & ~15; // 16-byte alignment, like malloc    if ($off + $size > $len) {        Alis::$mem .= str_repeat("\0", \max($off + $size - $len, 1 << 20));    } elseif ($size > 0) {        mem_set($off, 0, $size); // a reused area: zeroed like calloc    }    Alis::$host_top = $off + $size;    return $off;}function host_free(int $ptr): void{}function xread8(int $o): int{    return ord(Alis::$mem[$o]);}function xread16(int $o): int{    $m = Alis::$mem;    if (Alis::$xle) {        $v = ord($m[$o]) | (ord($m[$o + 1]) << 8);    } else {        $v = (ord($m[$o]) << 8) | ord($m[$o + 1]);    }    return ($v ^ 0x8000) - 0x8000;}function xread32(int $o): int{    $m = Alis::$mem;    if (Alis::$xle) {        $v = ord($m[$o]) | (ord($m[$o + 1]) << 8) | (ord($m[$o + 2]) << 16) | (ord($m[$o + 3]) << 24);    } else {        $v = (ord($m[$o]) << 24) | (ord($m[$o + 1]) << 16) | (ord($m[$o + 2]) << 8) | ord($m[$o + 3]);    }    return PHP_INT_SIZE === 8 ? (($v ^ 0x80000000) - 0x80000000) : $v;}function xwrite8(int $o, int $v): void{    Alis::$mem[$o] = chr($v & 0xff);}function xwrite16(int $o, int $v): void{    if (Alis::$xle) {        Alis::$mem[$o] = chr($v & 0xff);        Alis::$mem[$o + 1] = chr(($v >> 8) & 0xff);    } else {        Alis::$mem[$o] = chr(($v >> 8) & 0xff);        Alis::$mem[$o + 1] = chr($v & 0xff);    }}function xwrite32(int $o, int $v): void{    if (Alis::$xle) {        Alis::$mem[$o] = chr($v & 0xff);        Alis::$mem[$o + 1] = chr(($v >> 8) & 0xff);        Alis::$mem[$o + 2] = chr(($v >> 16) & 0xff);        Alis::$mem[$o + 3] = chr(($v >> 24) & 0xff);    } else {        Alis::$mem[$o] = chr(($v >> 24) & 0xff);        Alis::$mem[$o + 1] = chr(($v >> 16) & 0xff);        Alis::$mem[$o + 2] = chr(($v >> 8) & 0xff);        Alis::$mem[$o + 3] = chr($v & 0xff);    }}function xadd8(int $o, int $v): void{    xwrite8($o, xread8($o) + $v);}function xadd16(int $o, int $v): void{    xwrite16($o, xread16($o) + $v);}function xadd32(int $o, int $v): void{    xwrite32($o, s32(xread32($o) + $v));}function xsub8(int $o, int $v): void{    xwrite8($o, xread8($o) - $v);}function xsub16(int $o, int $v): void{    xwrite16($o, xread16($o) - $v);}function xsub32(int $o, int $v): void{    xwrite32($o, s32(xread32($o) - $v));}// HOST byte order (little endian): C dereferences of typed pointers into memory, e.g. *(s16 *)p, *alis.accfunction hread8(int $o): int{    return ord(Alis::$mem[$o]);}function hread16(int $o): int{    return ((ord(Alis::$mem[$o]) | (ord(Alis::$mem[$o + 1]) << 8)) ^ 0x8000) - 0x8000;}function hread32(int $o): int{    return read32le($o);}function hwrite8(int $o, int $v): void{    Alis::$mem[$o] = chr($v & 0xff);}function hwrite16(int $o, int $v): void{    Alis::$mem[$o] = chr($v & 0xff);    Alis::$mem[$o + 1] = chr(($v >> 8) & 0xff);}function hwrite32(int $o, int $v): void{    Alis::$mem[$o] = chr($v & 0xff);    Alis::$mem[$o + 1] = chr(($v >> 8) & 0xff);    Alis::$mem[$o + 2] = chr(($v >> 16) & 0xff);    Alis::$mem[$o + 3] = chr(($v >> 24) & 0xff);}function read16be(int $o): int{    return (ord(Alis::$mem[$o]) << 8) | ord(Alis::$mem[$o + 1]);}function read16le(int $o): int{    return ord(Alis::$mem[$o]) | (ord(Alis::$mem[$o + 1]) << 8);}function read32be(int $o): int{    $m = Alis::$mem;    return s32((ord($m[$o]) << 24) | (ord($m[$o + 1]) << 16) | (ord($m[$o + 2]) << 8) | ord($m[$o + 3]));}function read32le(int $o): int{    $m = Alis::$mem;    return s32(ord($m[$o]) | (ord($m[$o + 1]) << 8) | (ord($m[$o + 2]) << 16) | (ord($m[$o + 3]) << 24));}function read24be(int $o): int{    $v = (ord(Alis::$mem[$o]) << 16) | (ord(Alis::$mem[$o + 1]) << 8) | ord(Alis::$mem[$o + 2]);    return $v > 0x7fffff ? 0 : $v; // C _read_be_24}function read24le(int $o): int{    $v = ord(Alis::$mem[$o]) | (ord(Alis::$mem[$o + 1]) << 8) | (ord(Alis::$mem[$o + 2]) << 16);    return $v > 0x7fffff ? 0 : $v; // C _read_le24}/** Game-endian unsigned 16-bit read (C read16 function pointer). */function read16(int $o): int{    return Alis::$xle ? read16le($o) : read16be($o);}function read24(int $o): int{    return Alis::$xle ? read24le($o) : read24be($o);}function read32(int $o): int{    return Alis::$xle ? read32le($o) : read32be($o);}/** C write32(ptr, value): game endianness. */function write32(int $o, int $v): void{    xwrite32($o, $v);}/** C swap16(ptr): reads a u16 in the game byte order. */function swap16(int $o): int{    return read16($o); // C: host read + _convert16 = game byte order}function swap32(int $o): int{    return read32($o);}function bswap16(int $v): int{    return (($v & 0xff) << 8) | (($v >> 8) & 0xff);}function bswap32(int $v): int{    return s32((($v & 0xff) << 24) | ((($v >> 8) & 0xff) << 16) | ((($v >> 16) & 0xff) << 8) | (($v >> 24) & 0xff));}/** Values in game endianness <-> host value (the PHP host is "little endian"). */function xswap16(int $v): int{    return s16(Alis::$xle ? $v : bswap16($v & 0xffff));}function xswap32(int $v): int{    return Alis::$xle ? s32($v) : bswap32($v);}function xswap16be(int $v): int{    return s16(bswap16($v & 0xffff));}function xswap32be(int $v): int{    return bswap32($v);}function xread16be(int $o): int{    return s16(read16be($o));}function xread32be(int $o): int{    return read32be($o);}/** * PHP has no in-place memcpy on a string: substr_replace() rebuilds the whole memory (megabytes, and in the * browser such huge blocks are mmap()ed and never given back: "Out of memory" after a few hundred copies). So * bytes are written in place: the target is compared with the new bytes block by block (string compares run * in C) and only the blocks that differ are written, byte by byte. On the desktop (64-bit PHP), a write bigger * than 1/MEM_LOOP_RATIO of the memory still uses one substr_replace (faster there, no leak). */const MEM_LOOP_RATIO = 300;const MEM_BLOCK = 256;/** Writes raw bytes at $dst (memcpy from a PHP string), in place. */function mem_write(int $dst, string $bytes): void{    $n = strlen($bytes);    if ($n === 0) {        return;    }    $m = &Alis::$mem;    if ($n < 64) {        for ($i = 0; $i < $n; $i++) {            $m[$dst + $i] = $bytes[$i];        }        return;    }    if (PHP_INT_SIZE === 8 && $n * MEM_LOOP_RATIO >= strlen($m)) {        $m = substr_replace($m, $bytes, $dst, $n);        return;    }    $old = substr($m, $dst, $n);    if ($old === $bytes) {        return;    }    for ($b = 0; $b < $n; $b += MEM_BLOCK) {        $len = $n - $b < MEM_BLOCK ? $n - $b : MEM_BLOCK;        if (substr_compare($old, $bytes, $b, $len) === 0) {            continue;        }        for ($i = $b, $end = $b + $len, $d = $dst; $i < $end; $i++) {            if ($old[$i] !== $bytes[$i]) {                $m[$d + $i] = $bytes[$i];            }        }    }}function mem_move(int $dst, int $src, int $n): void{    if ($n > 0) {        mem_write($dst, substr(Alis::$mem, $src, $n));    }}function mem_set(int $dst, int $v, int $n): void{    if ($n > 0) {        mem_write($dst, str_repeat(chr($v & 0xff), $n));    }}function mem_read(int $src, int $n): string{    return $n > 0 ? substr(Alis::$mem, $src, $n) : '';}function mem_cmp(int $a, int $b, int $n): int{    return strcmp(substr(Alis::$mem, $a, $n), substr(Alis::$mem, $b, $n)) <=> 0;}function mem_strlen(int $p): int{    $end = strpos(Alis::$mem, "\0", $p);    return $end === false ? strlen(Alis::$mem) - $p : $end - $p;}function mem_str(int $p): string{    return substr(Alis::$mem, $p, mem_strlen($p));}function mem_putstr(int $p, string $s): void{    mem_write($p, $s . "\0");}function mem_strcpy(int $dst, int $src): void{    mem_putstr($dst, mem_str($src));}// ---------------------------------------------------------------------------------------------// Script byte code// ---------------------------------------------------------------------------------------------function script_read8(): int{    return ord(Alis::$mem[Alis::$script->pc++]);}function script_read16(): int{    $s = Alis::$script;    $v = read16($s->pc);    $s->pc += 2;    return $v;}function script_read24(): int{    $s = Alis::$script;    $v = read24($s->pc);    $s->pc += 3;    return $v;}function script_read32(): int{    $s = Alis::$script;    $v = read32($s->pc);    $s->pc += 4;    return $v;}function get_vram(int $offset): int{    return Alis::$script->vram_org + $offset;}// ---------------------------------------------------------------------------------------------// Sprites (C: SPRITE_VAR, sprite memory of SPRITE_SZ-byte records)// ---------------------------------------------------------------------------------------------function sprite_var(int $x): ?Sprite{    if ($x === 0) {        return null;    }    $key = Img::$basesprite + ($x & 0xffff);    return Img::$sprites[$key] ??= new Sprite($key);}// ---------------------------------------------------------------------------------------------// Misc// ---------------------------------------------------------------------------------------------final class UnsupportedFeature extends \RuntimeException{}/** A C path that was not ported (v6, 3D, video...): stops the VM with a message. */function unsupported(string $what): void{    throw new UnsupportedFeature("ALIS: '$what' is not supported by this port");}/** Thrown to end the current frame; the VM resumes where it stopped at the next frame. */final class VmYield extends \Exception{}/** For opcodes that must wait (text input): restart the current opcode at the next frame. */function vm_yield_retry(): void{    Alis::$script->pc = Alis::$opcode_pc;    throw new VmYield();}function BIT_CHK(int $v, int $b): int{    return ($v >> $b) & 1;}