JA
games/alis / src / Alis / fn / alis_v6.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/alis.c (MIT, Olivier Huguenot, Vadim Kindl): the v6 (version >= 60) part —// readexec_opcode_v6 (setjmp), alis_main_V6, v6_tabcell — and the v6 macros of alis.h.// =============================================================================// MARK: - alis.h v6 macros// =============================================================================/** * C: longjmp(alis.ofin_jmp, 1) of ofin (v6). Thrown by ofin, caught by readexec_opcode_v6 where the * C setjmp is (the opcode being executed is abandoned, execution continues with the next one). */final class OfinJump extends \Exception{}/** V6_CLINKING(vram): v6 clinking register (DOS / 3DO ctx -0x14) */function V6_CLINKING(int $vram): int{    return $vram - 0x7a;}// =============================================================================// MARK: - tab functions// =============================================================================// v6 fixed-stride array-cell resolverfunction v6_tabcell(int $addr, int $shift): int{    $base = $addr;    if (xread8($addr - 2) & 0x80) {        $p = xread32($addr);        $base = s32($p + xread32($p));    }    $index = s16(Alis::$varD7);    $dims = xread8($addr - 1);    if ($dims != 0) {        for ($d = 0; $d < $dims; $d++) {            $base = s32($base + mul32(xread32($addr - 4 - ($dims - $d) * 4), $index));            $index = s16(hread32(Alis::$acc));   // index = *alis.acc++ (s32 -> s16)            Alis::$acc += 4;        }    }    $base = s32($base + shl32($index, $shift));    return $base;}/** C: the version >= 60 branch of tabstring() (alis.c). */function v6_tabstring(int $addr): int{    $base = $addr;    if (xread8($addr - 2) & 0x80) {        $p = xread32($addr);        $base = s32($p + xread32($p));    }    $index = s16(Alis::$varD7);    $dims = xread8($addr - 1);    if ($dims != 0) {        for ($d = 0; $d < $dims; $d++) {            $base = s32($base + mul32(xread32($addr - 4 - ($dims - $d) * 4), $index));            $index = s16(hread32(Alis::$acc));            Alis::$acc += 4;        }    }    $base = s32($base + mul32(xread16($addr - 4) & 0xffff, $index));    return $base;}// =============================================================================// MARK: - v6 opcode dispatch// =============================================================================/** * C: setjmp(alis.ofin_jmp) around the opcode; ofin longjmps back here (OfinJump) and the opcode is * abandoned. The previous jump target / ofin_active are restored afterwards (nested calls). */function readexec_opcode_v6(): void{    $savedactive = Alis::$ofin_active;    Alis::$ofin_active = 1;    $s = Alis::$script;    Alis::$opcode_pc = $s->pc;    try {        (Tables::$tab_opcodes[ord(Alis::$mem[$s->pc++])])();    } catch (OfinJump) {        // setjmp() returned 1: nothing more to do    } finally {        Alis::$ofin_active = $savedactive;    }}// =============================================================================// MARK: - v6 main loop// =============================================================================/** * C alis_main_V6(): the blocking entity loop. PORT: the port runs the resumable copy of it in * Machine::mainV6() (one VM cycle per frame); this one is kept for alis_thread() / tools. */function alis_main_V6(): void{    Alis::$varD5 = alis_first_ent();    while (Alis::$state) {        if (Alis::$state == C::eAlisStateSave) {            alis_save_state();            Alis::$state = C::eAlisStateRunning;        } elseif (Alis::$state == C::eAlisStateLoad) {            alis_load_state();            Alis::$state = C::eAlisStateRunning;        }        Alis::$restart_loop = 0;        ALIS_SET_SCRIPT(ENTSCR(Alis::$varD5));        xwrite16(Alis::$script->vram_org + 0x20, 0);        if (xread8(V6_FARSLEEP(Alis::$script->vram_org)) & (2 | AlisV6Const::V6_DORMANT)) {            goto v6_next_entity;        }        Alis::$fallent = 0;        Alis::$fseq = 0;        Alis::$acc = Alis::$acc_org;        $script_org = get_0x14_script_org_offset(Alis::$script->vram_org);        if (get_0x24_scan_inter(Alis::$script->vram_org) < 0) {            $scan_offset = xread32($script_org + 0x24);            if ($scan_offset != 0) {                Alis::$saversp = get_0x0a_vacc_offset(Alis::$script->vram_org) & 0xffff;                Alis::$script->vacc_off = s16(s16(Alis::$saversp) - 4);                savecoord(Alis::$script->vram_org);                Alis::$script->pc = s32($script_org + 0x24 + $scan_offset);                alis_loop();                if (Alis::$restart_loop == 0) {                    Alis::$script->vacc_off = s16(Alis::$saversp);                    set_0x0a_vacc_offset(Alis::$script->vram_org, Alis::$script->vacc_off);                    updtcoord(Alis::$script->vram_org);                }            }        }        $main_vec = s32($script_org + 0x18 + xread32($script_org + 0x18));        if (get_0x08_script_ret_offset(Alis::$script->vram_org) != $main_vec) {            $script_offset = xread32($script_org + 0x20);            if ($script_offset != 0) {                Alis::$saversp = get_0x0a_vacc_offset(Alis::$script->vram_org) & 0xffff;                Alis::$script->vacc_off = s16(s16(Alis::$saversp) - 4);                savecoord(Alis::$script->vram_org);                Alis::$script->pc = s32($script_org + 0x20 + $script_offset);                alis_loop();                if (Alis::$restart_loop == 0) {                    Alis::$script->vacc_off = s16(Alis::$saversp);                    set_0x0a_vacc_offset(Alis::$script->vram_org, Alis::$script->vacc_off);                    updtcoord(Alis::$script->vram_org);                }            }        }        if (get_0x04_cstart_csleep(Alis::$script->vram_org) != 0) {            if (s8(get_0x04_cstart_csleep(Alis::$script->vram_org)) < 0) {                set_0x04_cstart_csleep(Alis::$script->vram_org, 1);            }            if (s32(Alis::$timeclock) >= s32(get_0x3e_wait_time(Alis::$script->vram_org))) {                set_0x3e_wait_time(Alis::$script->vram_org, s32((get_0x3a_wait_cycles(Alis::$script->vram_org) & 0xffff) + Alis::$timeclock));                savecoord(Alis::$script->vram_org);                Alis::$script->pc = get_0x08_script_ret_offset(Alis::$script->vram_org);                Alis::$script->vacc_off = get_0x0a_vacc_offset(Alis::$script->vram_org);                Alis::$fseq++;                alis_loop();                if (Alis::$restart_loop == 0) {                    set_0x0a_vacc_offset(Alis::$script->vram_org, Alis::$script->vacc_off);                    set_0x08_script_ret_offset(Alis::$script->vram_org, Alis::$script->pc);                    $script_offset = xread32($script_org + 0x1c);                    if ($script_offset != 0) {                        Alis::$fseq = 0;                        Alis::$saversp = get_0x0a_vacc_offset(Alis::$script->vram_org) & 0xffff;                        Alis::$script->vacc_off = s16(s16(Alis::$saversp) - 4);                        Alis::$script->pc = s32($script_org + 0x1c + $script_offset);                        alis_loop();                        if (Alis::$restart_loop == 0) {                            Alis::$script->vacc_off = s16(Alis::$saversp);                            set_0x0a_vacc_offset(Alis::$script->vram_org, Alis::$script->vacc_off);                        }                    }                    if (Alis::$restart_loop == 0) {                        updtcoord(Alis::$script->vram_org);                    }                }            }            if (Alis::$restart_loop == 0 && xread8(Alis::$script->vram_org + 0x45)) {                v6_dirtyelems(Alis::$script->vram_org);                xwrite8(Alis::$script->vram_org + 0x45, 0);            }        }        v6_next_entity:        Alis::$varD5 = xread16(Alis::$atent + 4 + Alis::$varD5) & 0xffff;        if (Alis::$varD5 == 0) {            ALIS_SET_SCRIPT(Alis::$main);            draw();            sys_poll_event();            Alis::$varD5 = alis_first_ent();        }    }}