UK
games/alis / src / Alis / fn / mem.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/mem.c (MIT, Olivier Huguenot, Vadim Kindl).// read16/read24/read32, read16be/le, read32be/le, write32, swap16/swap32, get_vram and the xread/xwrite// helpers are in runtime.php. Only the rest of mem.c is here.// NOTE: in C, swap16(ptr)/swap32(ptr) read in the GAME byte order (_convert16 is linear when the game// and the host share their endianness), i.e. they equal read16(ptr)/read32(ptr).// =============================================================================// MARK: - MEMORY ACCESS// =============================================================================function vram_init(): void{    // Set up thin aliases for inline xread/xwrite (NDEBUG path)    Alis::$xle = Alis::$platform->is_little_endian ? 1 : 0;    MemVars::$_xwc = (Alis::$platform->version >= 30 && Alis::$platform->is_little_endian) ? 1 : 0;    // read16/read24/read32 & co. and _convert16/_convert32 follow Alis::$xle in runtime.php}function xpush32(int $value): void{    $s = Alis::$script;    $s->vacc_off = s16($s->vacc_off - 4);    xwrite32($s->vram_org + $s->vacc_off, $value);}function xpeek32(): int{    return xread32(Alis::$script->vram_org + Alis::$script->vacc_off);}function xpop32(): int{    $val = xpeek32();    $s = Alis::$script;    $s->vacc_off = s16($s->vacc_off + 4);    return $val;}/** PORT: helper for C fread() into a buffer: reads $n bytes (PHP fread() rejects $n <= 0), '' on EOF. */function fread_bytes($fp, int $n): string{    if ($fp === null || $n <= 0) {        return '';    }    // loop: a non-plain-file stream returns at most one chunk (8 KB) per fread()    $s = '';    while (($left = $n - strlen($s)) > 0 && !feof($fp)) {        $chunk = fread($fp, $left);        if ($chunk === false || $chunk === '') {            break;        }        $s .= $chunk;    }    return $s;}function fread16($fp): int{    // u32 tmp = 0; fread(&tmp, 2, 1, fp); return swap16(&tmp) (game byte order)    $b = str_pad(fread_bytes($fp, 2), 2, "\0");    return Alis::$xle        ? ord($b[0]) | (ord($b[1]) << 8)        : (ord($b[0]) << 8) | ord($b[1]);}function fread32($fp): int{    $b = str_pad(fread_bytes($fp, 4), 4, "\0");    if (Alis::$xle) {        return s32(ord($b[0]) | (ord($b[1]) << 8) | (ord($b[2]) << 16) | (ord($b[3]) << 24));    }    return s32((ord($b[0]) << 24) | (ord($b[1]) << 16) | (ord($b[2]) << 8) | ord($b[3]));}// World-coordinate accessors for cwalkmap. All builds: v3.0+ LE stores WC as 16.16; swap words so// xread16 sees the integer.function xread32_wc(int $base, int $wc_offset): int{    if (Alis::$platform->version >= 30 && Alis::$platform->is_little_endian) {        $hi = xread16($base + $wc_offset) & 0xffff;       // integer (bytes 0-1 on LE)        $lo = xread16($base + $wc_offset + 2) & 0xffff;   // fraction (bytes 2-3 on LE)        return shl32($hi, 16) | $lo;    }    return xread32($base + $wc_offset);}function xwrite32_wc(int $base, int $wc_offset, int $val): void{    if (Alis::$platform->version >= 30 && Alis::$platform->is_little_endian) {        xwrite16($base + $wc_offset, s16($val >> 16));         // integer to bytes 0-1        xwrite16($base + $wc_offset + 2, s16($val & 0xffff));  // fraction to bytes 2-3    } else {        xwrite32($base + $wc_offset, $val);    }}function io_malloc(int $rawsize): int{    $size = s32((s32($rawsize - 1) | 3) + 9);    $blockloc = s32(Alis::$finmem - $size);    if (ult(Alis::$finprog, $blockloc)) {        Alis::$finmem = $blockloc;        xwrite32($blockloc, $size);        if (Alis::$platform->version < 30) {            return $blockloc;        }        for ($i = 0; $i < 0x10; $i++) {            if (xread32(Alis::$tabptr + $i * 8) == 0) {                xwrite32(Alis::$tabptr + $i * 8 + 0, $blockloc + 8);                xwrite32(Alis::$tabptr + $i * 8 + 4, $blockloc + 8);                // NOTE: silmarils originaly used 32 bit pointer to pointer table allocated in (alis.mem) block                return Alis::$tabptr + $i * 8;            }        }    }    return 0;}function io_mfree(int $addr): void{    $blockloc = xread32($addr + 4);    xwrite32($addr + 0, 0);    xwrite32($addr + 4, 0);    if (s32(Alis::$finmem) == s32($blockloc - 8)) {        Alis::$finmem = s32(Alis::$finmem + xread32(Alis::$finmem));    }}