<?phpnamespace App\Alis;// Port of alis/src/script.c (MIT, Olivier Huguenot, Vadim Kindl): the v6 memory nodes// (DOS io_malloc / io_mfree node table, 0x18-byte nodes at alis.atent)./** PORT: the C `const char *name` may be given as a PHP string or as an offset into Alis::$mem. */function node_name_str(int|string $name): string{ return is_int($name) ? mem_str($name) : $name;}// v6 memory nodes// FIFO free list linked through node +0x10, type at +0x15. Returns 0 when exhausted.// Non-entity nodes go to the front of the block list (head NODE_BLKHEAD, next at +0xe,// name at +6, DOS block+8). Entity nodes keep the vram block layout at +6 / +8.function node_take(int $type): int{ $n = Alis::$nodefree & 0xffff; $nx = xread16(Alis::$atent + $n + 0x10) & 0xffff; // NOTE: DOS refuses the last two free nodes; 3DO has no check if ($n == 0 || $n == Alis::$nodetail || (Alis::$platform->kind != C::EPlatform3DO && $nx == Alis::$nodetail)) { return 0; } Alis::$nodefree = $nx; xwrite8(Alis::$atent + $n + 0x15, $type & 0xff); if (($type & 0xff) != 1) { for ($i = 0; $i < 8; $i++) { xwrite8(Alis::$atent + $n + 6 + $i, 0); } xwrite16(Alis::$atent + $n + 0xe, xread16(Alis::$atent + C::NODE_BLKHEAD + 0xe)); xwrite16(Alis::$atent + C::NODE_BLKHEAD + 0xe, $n); } return $n;}function node_free(int $n): void{ $n &= 0xffff; if ($n == 0) { return; } if (xread8(Alis::$atent + $n + 0x15) != 1) { // unlink from the block list $p = C::NODE_BLKHEAD; while ((xread16(Alis::$atent + $p + 0xe) & 0xffff) != $n && $p != C::NODE_BLKTAIL) { $p = xread16(Alis::$atent + $p + 0xe) & 0xffff; } if ($p != C::NODE_BLKTAIL) { xwrite16(Alis::$atent + $p + 0xe, xread16(Alis::$atent + $n + 0xe)); } } xwrite8(Alis::$atent + $n + 0x15, 0); xwrite16(Alis::$atent + Alis::$nodetail + 0x10, $n); Alis::$nodetail = $n;}function node_setname(int $n, int|string $name): void{ $name = node_name_str($name); $len = strlen($name); for ($i = 0, $end = 0; $i < 8; $i++) { $c = $i < $len ? ord($name[$i]) : 0; if (!$c || $c == 0x2e) { // '.' $end = 1; } xwrite8(Alis::$atent + $n + 6 + $i, $end ? 0 : $c); }}function node_find(int|string $name): int{ $name = node_name_str($name); $key = ''; for ($i = 0; $i < 8 && $i < strlen($name) && $name[$i] !== "\0"; $i++) { $key .= $name[$i]; } $key = str_pad($key, 8, "\0"); for ($n = xread16(Alis::$atent + C::NODE_BLKHEAD + 0xe) & 0xffff; $n != C::NODE_BLKTAIL; $n = xread16(Alis::$atent + $n + 0xe) & 0xffff) { $t = xread8(Alis::$atent + $n + 0x15); if ($t != 2 && $t != 0xc && ($t != 0xd || Alis::$platform->kind == C::EPlatform3DO)) { continue; } if (substr(Alis::$mem, Alis::$atent + $n + 6, 8) === $key) { return $n; } } return 0;}function node_swap(int $a, int $b): void{ $a &= 0xffff; $b &= 0xffff; if ($a == $b) { return; } $t = substr(Alis::$mem, Alis::$atent + $a, 0x18); mem_move(Alis::$atent + $a, Alis::$atent + $b, 0x18); mem_write(Alis::$atent + $b, $t); // #define NSW(x) ((x) == a ? b : (x) == b ? a : (x)) $NSW = static fn (int $x): int => $x == $a ? $b : ($x == $b ? $a : $x); for ($i = 0; $i < Alis::$maxent; $i++) { $n = Alis::$atent + $i * 0x18; xwrite16($n + 4, $NSW(xread16($n + 4) & 0xffff)); // run chain xwrite16($n + 0xe, $NSW(xread16($n + 0xe) & 0xffff)); // block list xwrite16($n + 0x10, $NSW(xread16($n + 0x10) & 0xffff)); // free list } Alis::$nodefree = $NSW(Alis::$nodefree); Alis::$nodetail = $NSW(Alis::$nodetail); Alis::$filmnode = $NSW(Alis::$filmnode); for ($i = 0; $i < 16; $i++) { Alis::$scrnode[$i]['node'] = $NSW(Alis::$scrnode[$i]['node'] ?? 0); } for ($i = 0; $i < 8; $i++) { Alis::$v6texts[$i]['node'] = $NSW(Alis::$v6texts[$i]['node'] ?? 0); } for ($i = 0; $i < Alis::$nbprog; $i++) { if (Alis::$loaded_scripts[$i]) { Alis::$loaded_scripts[$i]->node = $NSW(Alis::$loaded_scripts[$i]->node); } } $ia = intdiv($a, 0x18); $ib = intdiv($b, 0x18); $sa = Alis::$live_scripts[$ia]; Alis::$live_scripts[$ia] = Alis::$live_scripts[$ib]; Alis::$live_scripts[$ib] = $sa; for ($i = 0; $i < 2; $i++) { $id = $i ? $b : $a; $e = Alis::$live_scripts[intdiv($id, 0x18)]; if ($e && xread8(Alis::$atent + $id + 0x15) == 1) { if ((xread16(V6_CLINKING($e->vram_org)) & 0xffff) == (xread16($e->vram_org + 0x3a) & 0xffff)) { xwrite16(V6_CLINKING($e->vram_org), $id); } xwrite16($e->vram_org + 0x3a, $id); set_0x0e_script_ent($e->vram_org, $id); } }}// node of a screen buffer field (+0xb4 / +0xbc)function scrnode_set(int $scr, int $field, int $node): void{ $field &= 0xffff; $key = ($scr - Alis::$basemain) & 0xffff; $slot = -1; for ($i = 0; $i < 16; $i++) { $sn = Alis::$scrnode[$i]; if (($sn['scr'] ?? 0) == $key && ($sn['field'] ?? 0) == $field) { $slot = $i; break; } if ($slot < 0 && ($sn['node'] ?? 0) == 0) { $slot = $i; } } if ($slot < 0) { return; } Alis::$scrnode[$slot] = [ 'scr' => $key, 'field' => $field, 'node' => $node & 0xffff, 'handle' => xread32($scr + $field), ];}// 0 if the field no longer holds the buffer the node was taken forfunction scrnode_get(int $scr, int $field): int{ $field &= 0xffff; $key = ($scr - Alis::$basemain) & 0xffff; for ($i = 0; $i < 16; $i++) { $sn = Alis::$scrnode[$i]; if (($sn['node'] ?? 0) && ($sn['scr'] ?? 0) == $key && ($sn['field'] ?? 0) == $field) { return ($sn['handle'] ?? 0) == xread32($scr + $field) ? $sn['node'] : 0; } } return 0;}function node_prog(int $n): ?ScriptData{ for ($i = 0; $n && $i < Alis::$nbprog; $i++) { if (Alis::$loaded_scripts[$i] && Alis::$loaded_scripts[$i]->node == $n) { return Alis::$loaded_scripts[$i]; } } return null;}