magic // byte 1..3 -> unpacked size (24 bits) // byte 4..5 -> main script if zero // if main: byte 6..21 -> main header // if main: byte 22..29 -> dic // if not main: byte 6..13 -> dic function is_packedx(int $magic): int { return (int)(((($magic >> 24) & 0xff) & 0xf0) == 0xa0); } function get_unpacked_size(int $magic): int { return $magic & 0x00ffffff; } function is_main(int $check): int { return (int)(C::kMainScriptID == ($check & 0xffff)); } // ============================================================================= // MARK: - Script API // ============================================================================= function search_insert(int $size, int $target_id): int { $start = 0; $end = s32($size - 1); while ($start <= $end) { $mid = $start + intdiv($end - $start, 2); $current_id = read16(ATPROG($mid)); if ($current_id == $target_id) { return $mid; } if ($current_id < $target_id) { $start = $mid + 1; } else { $end = $mid - 1; } } return $start; } function protect(): void { if (Alis::$vprotect != 0) { // u8 *workbuffptr = alis.buffer; (unused) Alis::$basemain = Alis::$atprog; } Alis::$vprotect = 0; } function debprot(): void { protect(); } /** $sample: offset of the sample in Alis::$mem. */ function invdigit(int $sample): void { $type = xread8($sample); // Ishar 1 ST plays MV1 // samples stay as stored, 0xFF marks loop end / release end $mv1st = ($type == 2 && Alis::$platform->kind == C::EPlatformAtari && Alis::$platform->uid == C::EGameIshar_1); if ($type == 2 && !$mv1st) { if (read32($sample + 0xc) != 0) { $length = s16((read32($sample + 2) - read32($sample + 0xc)) - 0x21); if (-1 < $length) { $dst = s32($sample + 0x10 + read32($sample + 0xc)); $src = $dst + 8; mem_move($dst, $src, $length); } write32($sample + 0xc, s32(read32($sample + 0xc) - 0x10)); } write32($sample + 2, s32(read32($sample + 2) - 0x10)); } // PORT: C u32 length compared with int i (unsigned compare); kept signed here, so a wrapped // (negative) length runs no iteration instead of overrunning the memory like the C. $length = s32(read32($sample + 2) - 0x10); $smpdata = $sample + 0x10; if (Alis::$platform->kind == C::EPlatformMac) { // instruments are stored at 1/3 level if (xread8($sample) == 2) { if (ScriptVars::$mac3 === '') { $mac3 = ''; for ($i = 0; $i < 256; $i++) { $v = s8($i) * 3; $v = $v < -128 ? -128 : ($v > 127 ? 127 : $v); $mac3 .= chr($v ? ($v & 0xff) : 1); } ScriptVars::$mac3 = $mac3; } if ($length > 0) { $all = ''; for ($i = 0; $i < 256; $i++) { $all .= chr($i); } mem_write($smpdata, strtr(mem_read($smpdata, $length), $all, ScriptVars::$mac3)); } } } elseif (Alis::$platform->kind == C::EPlatformAtari) { if ($length > 0) { $all = ''; $map = ''; $is2 = xread8($sample) == 2; for ($i = 0; $i < 256; $i++) { $all .= chr($i); // MV1 marker -> end of sample if ($mv1st && $i == 0xff) { $map .= "\0"; continue; } $c = $i; if ($is2) { $c = ($c * 2) & 0xff; } $c = ($c + 0x80) & 0xff; if ($c == 0) { $c = 1; } $map .= chr($c); } mem_write($smpdata, strtr(mem_read($smpdata, $length), $all, $map)); } } elseif (Alis::$platform->kind == C::EPlatformAmiga) { if ($length > 0) { mem_write($smpdata, strtr(mem_read($smpdata, $length), "\0", "\1")); } } if (Alis::$platform->version >= 20) { xwrite8($sample + 0x10 + $length - 1, 0); } } // main header fields are read before the platform readers are set up function hdr32le(string $p, int $o): int { return s32(ord($p[$o]) | (ord($p[$o + 1]) << 8) | (ord($p[$o + 2]) << 16) | (ord($p[$o + 3]) << 24)); } function script_guess_game(string $script_path): void { Alis::$platform->uid = 0; Alis::$platform->ega = 0; $fp = sys_fopen($script_path, 'rb'); if ($fp) { // get packed file size fseek($fp, 0, SEEK_END); $input_sz = (int)ftell($fp); rewind($fp); if ($input_sz > 0x24) { // Try v6.0+ format detection first: // v6.0 scripts have an 8-byte name field at offset 0x08, // and the main script has "main" there. $header_buf = str_pad(fread_bytes($fp, 0x24), 0x24, "\0"); $is_v60 = (substr($header_buf, 8, 4) === 'main' && $header_buf[12] === "\0"); rewind($fp); // read header (old format) $magic = fread32($fp); $check = fread16($fp); if ($is_v60) { // v6.0+: version byte at +0x06, the same in all of a game's scripts $specs = hdr32le($header_buf, 0x10); switch (ord($header_buf[6])) { case 0x3c: Alis::$platform->uid = Alis::$platform->kind == C::EPlatform3DO ? C::EGameRobinsonsRequiem3DO : (hdr32le($header_buf, 0x20) == 0 ? C::EGameManualRRQ : C::EGameDeus); break; case 0x48: case 0x4a: Alis::$platform->uid = C::EGameTimeWarriors; break; case 0x5f: Alis::$platform->uid = C::EGameAsghan; break; case 0x61: Alis::$platform->uid = C::EGameTournamentOfWarriors; break; case 0x71: Alis::$platform->uid = $specs == 700 ? C::EGameLesVisiteurs : C::EGameArabianNights; break; case 0xe0: Alis::$platform->uid = C::EGameInspectorGadget; break; default: Alis::$platform->uid = C::EGameUnknown; break; } } elseif (is_main($check)) { Alis::$header->val0 = fread16($fp); Alis::$header->val1 = fread16($fp); Alis::$header->val2 = fread32($fp); Alis::$header->val3 = fread32($fp); Alis::$header->val4 = fread32($fp); Alis::$platform->uid = mul32(Alis::$header->val3, Alis::$header->val4); } if (Alis::$platform->uid != 0) { $px_format = C::EPxFormatChunky; switch (Alis::$platform->kind) { case C::EPlatformMac: $px_format = C::EPxFormatSTPlanar; break; case C::EPlatformAtari: $px_format = C::EPxFormatSTPlanar; break; case C::EPlatformAmiga: $px_format = C::EPxFormatAmPlanar; break; default: $px_format = C::EPxFormatChunky; } $pl = Alis::$platform; switch ($pl->uid) { //####################################################################################### // In order of release //####################################################################################### case C::EGameManhattanDealers0: case C::EGameManhattanDealers1: $pl->name = 'Manhattan Dealers'; $pl->version = 10; // 0 [0x00] $pl->bpp = 4; $pl->dbl_buf = 0; Alis::$basemem = 0x22400; break; case C::EGameMadShow: $pl->name = 'Mad Show'; $pl->version = 11; // 2.1 [0x15], 2.3 [0x17], 0 [0x00] $pl->bpp = 4; // EGA PC re-release (dictionary packer 0xa1) $pl->ega = (int)($pl->kind == C::EPlatformPC && (($magic >> 24) & 0xff) == 0xa1); $pl->px_format = $px_format; Alis::$basemem = 0x23e00; break; case C::EGameTarghan0: case C::EGameTarghan1: $pl->name = 'Targhan'; $pl->version = 12; // 2.1 [0x15], 2.2 [0x16], 0 [0x00] $pl->bpp = 4; $pl->px_format = $px_format; $pl->dbl_buf = 0; Alis::$basemem = 0x26000; break; case C::EGameWindsurfWilly: $pl->name = 'Windsurf Willy'; $pl->version = 12; // 2.1 [0x15], 0 [0x00] $pl->bpp = 4; $pl->px_format = $px_format; Alis::$basemem = 0x25400; break; case C::EGameLeFeticheMaya: $pl->name = 'Le Fetiche Maya'; $pl->version = 13; // 2.1 [0x15], 0 [0x00] $pl->bpp = 4; $pl->px_format = $px_format; $pl->dbl_buf = 0; Alis::$basemem = 0x25e00; break; case C::EGameColorado: $pl->name = 'Colorado'; $pl->version = 13; // 2.1 [0x15] $pl->bpp = 4; $pl->px_format = $px_format; Alis::$basemem = 0x26000; break; case C::EGameStarblade: $pl->name = 'Starblade'; $pl->version = 20; // 2.1 [0x15] $pl->bpp = 4; $pl->px_format = $px_format; $pl->dbl_buf = 0; Alis::$basemem = 0x21400; break; case C::EGameCrystalsOfArborea0: case C::EGameCrystalsOfArborea1: case C::EGameCrystalsOfArborea2: $pl->name = 'Crystals Of Arborea'; $pl->version = 20; // 2.2 [0x16] $pl->bpp = 4; Alis::$basemem = 0x1f300; break; case PlatformConst::EGameMetalMutant: $pl->name = 'Metal Mutant'; $pl->version = 20; // 2.2 [0x16] $pl->bpp = 4; Alis::$basemem = 0x20800; $pl->dbl_buf = 0; break; case C::EGameBostonBombClub: $pl->name = 'Boston Bomb Club'; $pl->version = 21; // 2.2 [0x16] $pl->bpp = 4; $pl->px_format = $px_format; Alis::$basemem = 0x27d00; break; case C::EGameXyphoesFantasy: $pl->name = 'Xyphoes Fantasy'; $pl->version = 0; $pl->bpp = 4; $pl->dbl_buf = 0; Alis::$basemem = 0x22400; break; case C::EGameKillingFist: $pl->name = 'Killing Fist'; $pl->version = 0; $pl->bpp = 4; $pl->dbl_buf = 0; Alis::$basemem = 0x22400; break; case C::EGameStormMaster: $pl->name = 'Storm Master'; $pl->version = 20; // 2.3 [0x17] $pl->bpp = 4; Alis::$basemem = 0x1ff00; break; case C::EGameIshar_1: $pl->name = 'Ishar 1'; $pl->version = 20; // 2.3 [0x17] Alis::$basemem = 0x20000; break; case C::EGameBunnyBricks: $pl->name = 'Bunny Bricks'; $pl->version = 21; // 2.3 [0x17] Alis::$basemem = 0x22200; break; case C::EGameTransarctica: $pl->name = 'Transarctica'; $pl->version = 22; // 2.3 [0x17], 2.8 [0x1c] Alis::$basemem = 0x21d00; break; case C::EGameIshar_2: $pl->name = 'Ishar 2'; $pl->version = 21; // 2.8 [0x1c] Alis::$basemem = 0x22400; break; case C::EGameRobinsonsRequiem0: case C::EGameRobinsonsRequiem1: $pl->name = "Robinson's Requiem"; $pl->version = 31; // 3.0 [0x1e] Alis::$basemem = 0x2b400; break; case C::EGameIshar_3: $pl->name = 'Ishar 3'; $pl->version = 30; // 3.0 [0x1e] Alis::$basemem = 0x25a00; break; //####################################################################################### // v6.0+ games: detected only (the port does not run them, alis_init() refuses version >= 60) case C::EGameManualRRQ: $pl->name = "Robinson's Requiem CD Manual"; $pl->version = 60; // 6.0 [0x3c] Alis::$basemem = 0x22400; break; case C::EGameRobinsonsRequiem3DO: $pl->name = "Robinson's Requiem"; $pl->version = 60; // 6.0 [0x3c] Alis::$basemem = 0x22400; $pl->width = 320; $pl->height = 200; break; case C::EGameDeus: $pl->name = 'Deus'; $pl->version = 60; // 6.0 [0x3c] Alis::$basemem = 0x22400; break; case C::EGameTimeWarriors: $pl->name = 'Time Warriors'; $pl->version = ord($header_buf[6]); // 7.2 [0x48], 7.4 [0x4a] Alis::$basemem = 0x22400; break; case C::EGameAsghan: $pl->name = 'Asghan'; $pl->version = 95; // 9.5 [0x5f] Alis::$basemem = 0x22400; break; case C::EGameTournamentOfWarriors: $pl->name = 'Tournament Of Warriors'; $pl->version = 97; // 9.7 [0x61] Alis::$basemem = 0x22400; break; case C::EGameArabianNights: $pl->name = 'Arabian Nights'; $pl->version = 113; // 11.3 [0x71] Alis::$basemem = 0x22400; break; case C::EGameLesVisiteurs: $pl->name = 'Les Visiteurs'; $pl->version = 113; // 11.3 [0x71] Alis::$basemem = 0x22400; break; case C::EGameInspectorGadget: $pl->name = 'Inspector Gadget'; $pl->version = 224; // 22.4 [0xe0] Alis::$basemem = 0x22400; break; //####################################################################################### default: $pl->name = 'UNKNOWN'; $pl->version = 20; Alis::$basemem = 0x22400; break; } } } // cleanup fclose($fp); } } /** PORT: lookup table of tochunky(): 8 bits of one plane spread to the 4-bit pixels of a 32-bit word. */ function tochunky_spread4(): array { if (!ScriptVars::$spread4) { for ($b = 0; $b < 256; $b++) { $v = 0; for ($c = 0; $c < 8; $c++) { if (($b >> (7 - $c)) & 1) { $v |= 1 << ((3 - ($c >> 1)) * 8 + (($c & 1) ? 0 : 4)); } } ScriptVars::$spread4[$b] = $v; } } return ScriptVars::$spread4; } /** PORT: lookup table of the 2-plane Mac conversion: 8 bits spread to the 2-bit pixels of a 16-bit word. */ function tochunky_spread2(): array { if (!ScriptVars::$spread2) { for ($b = 0; $b < 256; $b++) { $v = 0; for ($k = 0; $k < 8; $k++) { if (($b >> $k) & 1) { $v |= 1 << (2 * $k); } } ScriptVars::$spread2[$b] = $v; } } return ScriptVars::$spread2; } /** 4 interleaved ST planes -> 4-bit chunky, in place ($bitmap: offset in Alis::$mem). */ function tochunky(int $bitmap): void { $width = (read16($bitmap + 2) + 1) & 0xffff; $height = (read16($bitmap + 4) + 1) & 0xffff; $groups = intdiv($width * $height + 15, 16); // for (b = 0; b < width * height; b += 16) if ($groups <= 0) { return; } $S = tochunky_spread4(); $n = $groups * 8; $src = str_pad(substr(Alis::$mem, $bitmap + 6, $n), $n, "\0"); $out = ''; for ($at = 0; $at < $n; $at += 8) { $out .= pack('NN', $S[ord($src[$at + 0])] | ($S[ord($src[$at + 2])] << 1) | ($S[ord($src[$at + 4])] << 2) | ($S[ord($src[$at + 6])] << 3), $S[ord($src[$at + 1])] | ($S[ord($src[$at + 3])] << 1) | ($S[ord($src[$at + 5])] << 2) | ($S[ord($src[$at + 7])] << 3)); } mem_write($bitmap + 6, $out); } function all_tosigned(int $data): void { $hdr = s32($data + read32($data + 0xe)); $tab = s32($hdr + read32($hdr + 0xc)); $count = s16(read16($hdr + 0x10)); if ($count <= 0) { return; } for ($i = 0; $i < $count; $i++) { $off = read16($tab + $i * 2); $smp = $tab + $off; if (xread8($smp) != 1) { continue; } $j = 0; while ($j < $i && read16($tab + $j * 2) != $off) { $j++; } if ($j < $i) { continue; } $len = (xread8($smp + 1) << 8) | xread8($smp + 2); if ($len > 0) { mem_write($smp + 3, mem_read($smp + 3, $len) ^ str_repeat("\x80", $len)); } } } function all_tochunky(int $data): void { $hdr = s32($data + read32($data + 0xe)); $tab = s32($hdr + read32($hdr)); $count = s16(read16($hdr + 4)); if ($count <= 0) { return; } for ($i = 0; $i < $count; $i++) { $off = read16($tab + $i * 2); $bmp = $tab + s16($off); if (xread8($bmp) != 0) { continue; } $j = 0; while ($j < $i && read16($tab + $j * 2) != $off) { $j++; } if ($j < $i) { continue; } tochunky($bmp); } } // v6 memory nodes: see script_v6.php /** $data: offset of the unpacked script bytes in Alis::$mem (may already be alis.finprog). */ function script_init(string $name, int $data, int $data_sz): ?ScriptData { // v6 low word of the u32 size (on BE the high word would be 0 for most scripts) $id = Alis::$platform->version >= 60 ? s16(read32($data + 0)) : s16(read16($data + 0)); $insert = s16(debprotf($id)); // v6 checks by name before loading (node_find); its ids may collide if (Alis::$platform->version < 60 && $insert > 0 && $insert < Alis::$nbprog) { $script = Alis::$loaded_scripts[$insert]; if ($script->header->id == $id) { return $script; } } if (Alis::$platform->version < 60) { script_fix($name, $data, $data_sz); } // init script $script = new ScriptData(); $script->name = $name; $script->sz = $data_sz; $script->type = Alis::$typepack & 0xff; // script data if (Alis::$platform->version >= 60) { // v6.0+ header layout (0x38 bytes): 0x00 u32 total_size, 0x04 version info, 0x08 8-byte name, // 0x10 inisprit, 0x18 code_loc/ret_offset, 0x1c flag1 (scan/inter bit 0), 0x20 flag2 (bit 1), // 0x24 flag3 / sub-script ret_offset, 0x30 u32 vram_alloc_sz, 0x34 w_unknown5, 0x36 w_unknown7 // Use first u16 as synthetic ID for script table compatibility $script->header->id = $id & 0xffff; $script->header->unknown01 = xread8($data + 4); $script->header->unknown02 = xread8($data + 5); $script->header->code_loc_offset = read32($data + 0x18) & 0xffff; $script->header->ret_offset = read32($data + 0x18); $script->header->dw_unknown3 = read32($data + 0x1c); $script->header->dw_unknown4 = read32($data + 0x20); $script->header->w_unknown5 = read16($data + 0x34); $script->header->vram_alloc_sz = read32($data + 0x30) & 0xffff; $script->header->w_unknown7 = read16($data + 0x36); } else { $script->header->id = read16($data + 0); if (Alis::$platform->kind == C::EPlatformPC) { $script->header->unknown01 = xread8($data + 4); $script->header->unknown02 = xread8($data + 5); $script->header->code_loc_offset = read16($data + 2); } else { $script->header->unknown01 = xread8($data + 2); $script->header->unknown02 = xread8($data + 3); $script->header->code_loc_offset = read16($data + 4); } $script->header->ret_offset = read32($data + 6); $script->header->dw_unknown3 = read32($data + 10); $script->header->dw_unknown4 = read32($data + 14); $script->header->w_unknown5 = read16($data + 18); $script->header->vram_alloc_sz = read16($data + 20); $script->header->w_unknown7 = read16($data + 22); } $script->data_org = Alis::$finprog; // Scripts must stay below finmem (io_malloc blocks sit above it), as the original. // Overflow-safe: data_sz may be a wrapped negative s32. if (ult(Alis::$finmem, $script->data_org) || ult(s32(Alis::$finmem - $script->data_org), $data_sz)) { AlisVars::$alis_fatal = 'Out of game memory: this machine has too little RAM for this game.'; return null; } if ($script->data_org != $data) { mem_move($script->data_org, $data, $data_sz); } // get insert point $insert = search_insert(Alis::$nbprog, $script->header->id); // v1.0 keeps scripts word aligned Alis::$finprog = s32(Alis::$finprog + (Alis::$platform->version == 10 ? (($data_sz + 1) & ~1) : $data_sz)); Alis::$dernprog = s32(Alis::$dernprog + 4); Alis::$nbprog = (Alis::$nbprog + 1) & 0xffff; for ($i = Alis::$nbprog - 2; $i >= $insert; $i--) { ATPROG_SET($i + 1, ATPROG($i)); Alis::$loaded_scripts[$i + 1] = Alis::$loaded_scripts[$i]; } ATPROG_SET($insert, $script->data_org); Alis::$loaded_scripts[$insert] = $script; if (Alis::$platform->version >= 60) { $script->node = node_take(2); // DOS 0000dd37: program node node_setname($script->node, $script->data_org + 8); } if (Alis::$platform->version >= 60) { // v6 samples (type 9) are signed 8-bit // the mixer takes unsigned $data = $script->data_org; $n = read16($data + 0x3e); for ($i = 0, $slot = 0x40; $i < $n && ule($slot + 4, $data_sz); $i++, $slot += 4) { $off = read32($data + $slot); $at = s32($slot + $off); if (!$off || !ule(s32($at + 0x20), $data_sz) || xread8($data + $at) != 9) { continue; } $len = read32($data + $at + 0x10); $smp = $data + $at + 0x20; // for (k = 0; k < len && at + 0x20 + k < data_sz; k++) smp[k] ^= 0x80; $cnt = s32($data_sz - ($at + 0x20)); if (ult($len, $cnt)) { $cnt = $len; } if ($cnt > 0) { mem_write($smp, mem_read($smp, $cnt) ^ str_repeat("\x80", $cnt)); } } } if (Alis::$platform->version == 10) { if (Alis::$platform->kind != C::EPlatformPC) { all_tochunky($script->data_org); } if (Alis::$platform->kind == C::EPlatformOldAtari) { all_tosigned($script->data_org); } return $script; } if (Alis::$platform->version >= 60) { return $script; } if (Alis::$platform->kind == C::EPlatformMac) { $data = $script->data_org; $l = read32($data + 0xe); $sprites = read16($data + $l + 4); $S2 = tochunky_spread2(); for ($i = 0; $i < $sprites; $i++) { $a = s32(read32($data + $l) + $l + $i * 4); $at = s32(read32($data + $a) + $a); $bitmap = $data + $at; $b0 = xread8($bitmap); if ($b0 == 0 || $b0 == 2) { $width = (read16($bitmap + 2) + 1) & 0xffff; $height = (read16($bitmap + 4) + 1) & 0xffff; $groups = intdiv($width * $height + 15, 16); if ($groups > 0) { $n = $groups * 4; $src = str_pad(substr(Alis::$mem, $bitmap + 6, $n), $n, "\0"); $out = ''; for ($at = 0; $at < $n; $at += 4) { $out .= pack('nn', $S2[ord($src[$at + 0])] | ($S2[ord($src[$at + 2])] << 1), $S2[ord($src[$at + 1])] | ($S2[ord($src[$at + 3])] << 1)); } mem_write($bitmap + 6, $out); } } } } { $data = $script->data_org; $l = read32($data + 0xe); // convert images if needed if (Alis::$platform->kind == C::EPlatformAtari) { $sprites = read16($data + $l + 4); for ($i = 0; $i < $sprites; $i++) { $a = s32(read32($data + $l) + $l + $i * 4); $at = s32(read32($data + $a) + $a); $bitmap = $data + $at; $b0 = xread8($bitmap); if ($b0 == 0 || $b0 == 2) { if (($script->type & 1) == 0) { tochunky($bitmap); } } elseif ($b0 == 0x18 || $b0 == 0x1a) { script_nibble_dup($bitmap); } } } elseif (Alis::$platform->kind == C::EPlatformAmiga) { $sprites = read16($data + $l + 4); for ($i = 0; $i < $sprites; $i++) { $a = s32(read32($data + $l) + $l + $i * 4); $at = s32(read32($data + $a) + $a); $bitmap = $data + $at; $b0 = xread8($bitmap); if ($b0 == 0 || $b0 == 2) { if (($script->type & 1) == 0) { $uid = Alis::$platform->uid; if ($uid == C::EGameMadShow || $uid == C::EGameLeFeticheMaya || $uid == C::EGameTarghan0 || $uid == C::EGameTarghan1) { $width = (read16($bitmap + 2) + 1) & 0xffff; $height = (read16($bitmap + 4) + 1) & 0xffff; $at = 6; $planesize = intdiv($width * $height, 8); $buf = str_pad(substr(Alis::$mem, $bitmap, $at + 4 * $planesize + 8), $at + 4 * $planesize + 8, "\0"); $temp = []; for ($h = 0; $h < $height; $h++) { for ($w = 0; $w < $width; $w++) { $idx = $at + intdiv($w + $h * $width, 8); $c0 = ord($buf[$idx]); $c1 = ord($buf[$idx += $planesize]); $c2 = ord($buf[$idx += $planesize]); $c3 = ord($buf[$idx += $planesize]); $bit = 7 - ($w % 8); $temp[] = (($c0 >> $bit) & 1) | ((($c1 >> $bit) & 1) << 1) | ((($c2 >> $bit) & 1) << 2) | ((($c3 >> $bit) & 1) << 3); } } $out = ''; $half = intdiv($width * $height, 2); for ($b = 0; $b < $half; $b++) { $out .= chr((($temp[$b * 2 + 0] << 4) | $temp[$b * 2 + 1]) & 0xff); } mem_write($bitmap + $at, $out); } } } elseif ($b0 == 0x18 || $b0 == 0x1a) { script_nibble_dup($bitmap); } } } elseif (Alis::$platform->kind == C::EPlatformAmigaAGA) { $sprites = read16($data + $l + 4); for ($i = 0; $i < $sprites; $i++) { $a = s32(read32($data + $l) + $l + $i * 4); $at = s32(read32($data + $a) + $a); $bitmap = $data + $at; $b0 = xread8($bitmap); if ($b0 == 0x10 || $b0 == 0x12) { // Bake palette offset low nibble into pixel data // (matches Amiga io_pixel behavior) $pal_offset = xread8($bitmap + 6) & 0x0f; xwrite8($bitmap + 6, xread8($bitmap + 6) - $pal_offset); if ($pal_offset != 0) { $width = (read16($bitmap + 2) + 1) & 0xffff; $height = (read16($bitmap + 4) + 1) & 0xffff; $pixels = $bitmap + 8; $nbytes = ($width * $height) >> 1; if ($nbytes > 0) { $all = ''; $map = ''; for ($v = 0; $v < 256; $v++) { $hi = ($v >> 4) & 0x0f; $lo = $v & 0x0f; if ($b0 == 0x10) { if ($hi != 0) { $hi = ($hi + $pal_offset) & 0xff; } if ($lo != 0) { $lo = ($lo + $pal_offset) & 0xff; } } else { $hi = ($hi + $pal_offset) & 0xff; $lo = ($lo + $pal_offset) & 0xff; } $all .= chr($v); $map .= chr((($hi << 4) | ($lo & 0x0f)) & 0xff); } mem_write($pixels, strtr(mem_read($pixels, $nbytes), $all, $map)); } } } } } // Convert every sprite IN PLACE at load (as the original Falcon io_pixel): to 8-bit chunky, // or planar on native Atari. destofen reads the result straight from bitmap+8. { // `name` selects the per-script 2D/3D policy on DOS data (opcodes.c // conv_planar_allowed_for): the 3D renderer samples its resources as chunky. $script->sz = s32($script->sz + convert_sprites_inplace($script->data_org, $script->name)); } } { $data = $script->data_org; $l = read32($data + 0xe); // convert samples $maxlen = 0; $samples = read16($data + $l + 0x10); for ($s = 0; $s < $samples; $s++) { $a = s32(read32($data + 0xc + $l) + $l + $s * 4); $at = s32(read32($data + $a) + $a); if ($at < 0 || !ult($at, $script->sz)) { continue; } $sample = $data + $at; $t0 = xread8($sample); if ($t0 == 1 || $t0 == 2) { if (script_sample_packed($sample, $script)) { $fulllen = read32($sample + 2); $length = shr32(s32($fulllen - 0x10), 1); if (ult($maxlen, $length)) { $maxlen = $length; } } } } $tfibo = ScriptVars::$tfibo; for ($s = 0; $s < $samples; $s++) { $a = s32(read32($data + 0xc + $l) + $l + $s * 4); $at = s32(read32($data + $a) + $a); if ($at < 0 || !ult($at, $script->sz)) { continue; } $sample = $data + $at; $t0 = xread8($sample); if ($t0 == 1 || $t0 == 2) { if (script_sample_packed($sample, $script)) { $fulllen = read32($sample + 2); $length = shr32(s32($fulllen - 0x10), 1); $temp = $length > 0 ? mem_read($sample + 0x10, $length) : ''; $newval = $temp !== '' ? s8(ord($temp[0])) : 0; xwrite8($sample + 0x10, $newval); // smpptr2[-1] = newval $out = ''; for ($i = 1; $i < $length; $i++) { $t = ord($temp[$i]); $newval = s8($newval + $tfibo[$t >> 4]); $out .= chr($newval & 0xff); $newval = s8($newval + $tfibo[$t & 0xf]); $out .= chr($newval & 0xff); } // smpptr2[0] = smpptr2[1] = 0: the last written pair (or the first one) $out = $out === '' ? "\0\0" : substr($out, 0, -2) . "\0\0"; mem_write($sample + 0x11, $out); $fulllen = s32($fulllen - 2); write32($sample + 2, $fulllen); if (xread8($sample) == 2) { if (read32($sample + 0xc) != 0) { mem_set(s32($sample + read32($sample + 0xc) + 0x10), 0xff, 8); } $smplen = s32($fulllen - 0x10); if (read32($sample + 0xc) != 0) { $smplen = s32($fulllen - 8); } mem_set($sample + $smplen, 0xff, 8); } } invdigit($sample); } } } return $script; } /** PORT: helper of script_init(): ptr[h] |= (u8)(ptr[h] << 4) over the width * height pixels. */ function script_nibble_dup(int $bitmap): void { $width = (read16($bitmap + 2) + 1) & 0xffff; $height = (read16($bitmap + 4) + 1) & 0xffff; $n = $width * $height; if ($n <= 0) { return; } $all = ''; $map = ''; for ($v = 0; $v < 256; $v++) { $all .= chr($v); $map .= chr(($v | ($v << 4)) & 0xff); } mem_write($bitmap + 6, strtr(mem_read($bitmap + 6, $n), $all, $map)); } /** PORT: helper of script_init(): the "packed sample" test of both sample loops. */ function script_sample_packed(int $sample, ScriptData $script): bool { $t0 = xread8($sample); $t6 = xread8($sample + 6); return Alis::$platform->kind == C::EPlatformPC ? ($t6 == 1 && ($t0 == 1 || $t0 == 2) && $script->type != 0) : ($t6 == 1 && ($script->type & 1)); } // TOOD: use engine version instead of specific game // move to platform function get_context_size(): int { switch (Alis::$platform->uid) { case C::EGameManhattanDealers0: case C::EGameManhattanDealers1: case C::EGameLeFeticheMaya: case C::EGameColorado: case C::EGameStarblade: case C::EGameMadShow: case C::EGameWindsurfWilly: return 0x34; case C::EGameTarghan0: case C::EGameTarghan1: return 0x34; case C::EGameCrystalsOfArborea0: case C::EGameCrystalsOfArborea1: case C::EGameCrystalsOfArborea2: case C::EGameStormMaster: case PlatformConst::EGameMetalMutant: case C::EGameTransarctica: case C::EGameBostonBombClub: case C::EGameBunnyBricks: case C::EGameIshar_1: case C::EGameIshar_2: return 0x34; case C::EGameIshar_3: case C::EGameRobinsonsRequiem0: case C::EGameRobinsonsRequiem1: return 0x3e; case C::EGameRobinsonsRequiem3DO: case C::EGameDeus: return 0x80; default: return -1; } } function script_live(ScriptData $prog): ?ScriptLive { // Deus entity room comes from the shared arena, 3DO is using fixed pool, perror 0x23 when full if (Alis::$platform->uid == C::EGameDeus || Alis::$platform->kind == C::EPlatform3DO) { $pd = $prog->data_org; $need = read16($pd + 0x34) + read16($pd + 0x36) + get_context_size() + (read32($pd + 0x30) & 0xffff); if (Alis::$platform->kind == C::EPlatform3DO ? ult(v6_enttop(), s32(Alis::$finent + $need)) : !v6_growent($need)) { AlisVars::$alis_fatal = 'Out of game memory: this machine has too little RAM for this game.'; return null; } } $newent = Alis::$platform->version >= 60 ? node_take(1) : (Alis::$dernent & 0xffff); if ($newent == 0 && Alis::$platform->version >= 60) { return null; } $script = new ScriptLive(); $script->name = $prog->name; $script->data = $prog; $data = $script->data->data_org; if (Alis::$platform->version >= 60) { $w_unknown5 = read16($data + 0x34); $w_unknown7 = read16($data + 0x36); $vram_length = read32($data + 0x30) & 0xffff; } else { $w_unknown5 = read16($data + 0x12); $w_unknown7 = read16($data + 0x16); $vram_length = read16($data + 0x14); } $prevfin = s32($w_unknown5 + Alis::$finent); $contextsize = get_context_size(); Alis::$finent = s32(Alis::$finent + $w_unknown7 + $w_unknown5 + $contextsize); $script->vacc_off = s16(($prevfin - Alis::$finent) & 0xffff); $script->vram_org = Alis::$finent; mem_set($script->vram_org, 0, $vram_length); $curent = s16(Alis::$varD5); Alis::$varD7 = $newent; $script_idx = intdiv($newent, ENT_STRIDE()); $vram = $script->vram_org; set_0x0a_vacc_offset($vram, $script->vacc_off); set_0x1c_scan_clr($vram, $script->vacc_off); set_0x1e_scan_clr($vram, $script->vacc_off); if (Alis::$platform->version >= 60) { set_0x08_script_ret_offset($vram, s32($script->data->data_org + 0x18 + $script->data->header->ret_offset)); } else { set_0x08_script_ret_offset($vram, $script->data->data_org + $script->data->header->code_loc_offset + 2); } set_0x10_script_id($vram, $script->data->header->id); set_0x14_script_org_offset($vram, $script->data->data_org); set_0x2e_script_header_word_2($vram, $script->data->header->unknown01); set_0x02_wait_cycles($vram, 1); set_0x01_wait_count($vram, 1); set_0x04_cstart_csleep($vram, 0xff); set_0x1a_cforme($vram, -1); set_0x0e_script_ent($vram, $newent); set_0x24_scan_inter($vram, Alis::$platform->version >= 60 ? 4 : (Alis::$platform->version >= 31 ? 6 : 2)); set_0x18_unknown($vram, 0); set_0x0c_vacc_offset($vram, 0); set_0x22_cworld($vram, 0); set_0x20_set_vect($vram, 0); set_0x03_xinv($vram, 0); set_0x25_credon_credoff($vram, 0); set_0x26_creducing($vram, 0xff); set_0x28_unknown($vram, 0); // wide set_0x2a_clinking($vram, 0); // byte set_0x2c_calign($vram, 0); set_0x2d_calign($vram, 0); // TODO: RRQ changes if (!(Alis::$platform->kind == C::EPlatformPC && Alis::$platform->uid == C::EGameColorado)) { // Colorado DOS keeps 0xff set_0x26_creducing($vram, 0); } if ($contextsize > 0x2e) { set_0x2f_chsprite($vram, 0); set_0x30_unknown($vram, 0); set_0x32_unknown($vram, 0); // wide set_0x34_unknown($vram, 0); } if ($contextsize > 0x34) { set_0x36_unknown($vram, 0); set_0x38_unknown($vram, 0); set_0x3a_wait_cycles($vram, 0); set_0x3e_wait_time($vram, 0); } if ($contextsize > 0x3e) { xwrite16($vram - 0x40, 0); } if (Alis::$platform->version >= 60) { $head = v6_elemhead($vram); $tail = v6_elemtail($vram); v6el_set_next($head, $tail); v6el_set_prev($head, $head); v6el_set_prev($tail, $head); v6el_set_next($tail, $tail); xwrite16($vram - 0x64, 0); xwrite8($vram - 0x66, 0); xwrite8(V6_FARSLEEP($vram), 0); xwrite16($vram - 0x7c, 0); xwrite32($vram - 0x80, 0); { $qsize = xread16($script->data->data_org + 0x36) & 0xffff; $qbase = s16(-(0x40 + $qsize)); set_0x1c_scan_clr($vram, $qbase); set_0x1e_scan_clr($vram, $qbase); } { $d = $script->data->data_org; $vf = 0x4; if (xread32($d + 0x1c) != 0) { $vf |= 0x1; } if (xread32($d + 0x20) != 0) { $vf |= 0x2; } if (xread32($d + 0x24) != 0) { $vf |= 0x8; } xwrite8($vram - 0x67, $vf); } xwrite16($vram + 0x3a, $newent); xwrite16(V6_CLINKING($vram), $newent); xwrite32($vram + 0x3c, xread32($script->data->data_org + 0x8)); xwrite32($vram + 0x40, xread32($script->data->data_org + 0xc)); if (Alis::$script !== null && Alis::$script->vram_org != 0 && Alis::$script->vram_org != $vram) { $p = Alis::$script->vram_org; $c = $vram; xwrite16($c + 0x38, xread16($p + 0x3a)); xwrite32($c + 0x0c, xread32($p + 0x0c)); xwrite32($c + 0x10, xread32($p + 0x10)); xwrite32($c + 0x14, xread32($p + 0x14)); xwrite16($c + 0x18, xread16($p + 0x18)); xwrite16($c + 0x1a, xread16($p + 0x1a)); xwrite16($c + 0x1c, xread16($p + 0x1c)); xwrite16($c + 0x26, xread16($p + 0x26)); xwrite16($c + 0x28, xread16($p + 0x28)); xwrite16($c + 0x2a, xread16($p + 0x2a)); xwrite8($c + 0x24, xread8($p + 0x24)); xwrite16($c - 0x64, xread16($p - 0x64)); set_0x16_screen_id($c, get_0x16_screen_id($p)); xwrite8($c - 0x66, xread8($p - 0x66)); } } $script->pc = $script->pc_org = get_0x08_script_ret_offset($vram); // link right after the creator $nextent = xread16(Alis::$atent + $curent + 4) & 0xffff; xwrite16(Alis::$atent + $curent + 4, $newent); if (Alis::$platform->version < 60) { Alis::$dernent = xread16(Alis::$atent + $newent + 4); } xwrite16(Alis::$atent + $newent + 4, $nextent); xwrite32(Alis::$atent + $newent, $script->vram_org); if (Alis::$platform->version >= 60) { // block layout for killent xwrite16(Alis::$atent + $newent + 6, $w_unknown5 + $w_unknown7 + $contextsize); xwrite32(Alis::$atent + $newent + 8, $w_unknown5 + $w_unknown7 + $contextsize + $vram_length); } Alis::$live_scripts[$script_idx] = $script; Alis::$finent = s32(Alis::$finent + $vram_length); Alis::$nbent = s16(Alis::$nbent + 1); return $script; } /** PORT: reusable host buffer for script_load()'s unpack buffer (C mallocs/frees one per load). */ function script_scratch(int $size): int { if ($size > ScriptVars::$scratch_sz) { $cap = max($size, ScriptVars::$scratch_sz * 2); ScriptVars::$scratch = host_alloc($cap); ScriptVars::$scratch_sz = $cap; } return ScriptVars::$scratch; } /* PACKED SCRIPT FILE FORMAT byte(s) len role ------------------------------------------------------------------------------- 0 1 if high nibble is A, file is packed (b[0] & 0xf0 == 0xa0) 1..3 3 unpacked size 4...5 2 if zero, this is the main script (main only) 6...21 16 main header bytes, 22...29 8 unpack dictionary, 30...xx packed data (other) 6...13 8 unpack dictionary, 14...xx packed data UNPACKED SCRIPT FILE FORMAT 0...23 24 header bytes 24...xx ? unpacked data (1st word is ID, must be zero) */ function script_load(string $script_path): ?ScriptData { $script = null; $unpack_sz = -1; $fp = sys_fopen($script_path, 'rb'); if ($fp) { // get packed file size fseek($fp, 0, SEEK_END); $input_sz = (int)ftell($fp); rewind($fp); $slash = strrpos($script_path, AlisConst::kPathSeparator); $name = $slash === false ? $script_path : substr($script_path, $slash + 1); if (Alis::$platform->version >= 60) { $unpack_sz = $input_sz; // read straight into the arena where script_init puts it $in_arena = ule(Alis::$finprog, Alis::$finmem) && ule($unpack_sz, s32(Alis::$finmem - Alis::$finprog)) ? Alis::$finprog : 0; $unpack_buf = $in_arena ? $in_arena : script_scratch(1024 + $unpack_sz); mem_set($unpack_buf, 0, $unpack_sz); mem_write($unpack_buf, fread_bytes($fp, $unpack_sz)); $script = script_init($name, $unpack_buf, $unpack_sz); fclose($fp); } else { // read header $magic = fread32($fp); $check = fread16($fp); // alloc and unpack $unpack_sz = get_unpacked_size($magic); $unpack_buf = script_scratch(1024 + $unpack_sz); mem_set($unpack_buf, 0, $unpack_sz); // TODO: check if this was already loaded, if so use cache // decrunch if needed Alis::$typepack = s8(shr32($magic, 24)); // v1.1 re-release (Mad Show) marks the dictionary packer with 0xa1 if (Alis::$platform->kind == C::EPlatformPC && Alis::$platform->version != 11) { Alis::$typepack = s8(Alis::$typepack & 0xfe); } if (Alis::$typepack < 0) { $pak_sz = $input_sz - UnpackVars::$kPackedHeaderSize - UnpackVars::$kPackedDictionarySize; if (is_main($check)) { // skip vm specs fseek($fp, UnpackVars::$kVMSpecsSize, SEEK_CUR); $pak_sz -= UnpackVars::$kVMSpecsSize; } $unpack_sz = unpack_script($script_path, $unpack_buf); if ($unpack_sz > 0) { $script = script_init($name, $unpack_buf, $unpack_sz); } } else { // not packed, still might be script $seekto = UnpackVars::$kPackedHeaderSize; if (is_main($check)) { $seekto += UnpackVars::$kVMSpecsSize; } $unpack_sz -= $seekto; if ($unpack_sz > 0) { fseek($fp, $seekto, SEEK_SET); mem_write($unpack_buf, fread_bytes($fp, $unpack_sz)); // PORT: a script compiled from its source (sys_source_io) keeps the packing byte of its original // .io: the VM reads it later (cfreadb unpacks the data files when the last script was packed) $srcKey = strtolower(preg_replace('/\.io$/i', '', $name)); if (isset(SysVars::$sourcePack[$srcKey])) { Alis::$typepack = SysVars::$sourcePack[$srcKey]; } $script = script_init($name, $unpack_buf, $unpack_sz); } } // cleanup fclose($fp); // damaged data already set alis_fatal: let the VM stop and show it if ($unpack_sz < 0 && AlisVars::$alis_fatal === null) { // PORT: the C calls exit(-1); the port cannot end the process: throw to stop the VM with a message. throw new \RuntimeException("ALIS: failed to unpack script at path '$script_path'"); } } } if (Alis::$platform->uid == C::EGameMadShow && Alis::$platform->kind == C::EPlatformAtari) { if ($script !== null && $script->header->id == 0) { // HACK: patch main script of Mad Show to work from hdd // NOTE: game was distributed on two single-sided 3.5" disks and check disk by loading disk.fic containig number of diskette $locations = [null, null]; if (($locations[0] = strarr($script->data_org, 'disk.fic', $unpack_sz)) !== null) { if (($locations[1] = strarr($locations[0] + 8, 'disk.fic', $unpack_sz)) !== null) { for ($i = 1; $i < 3; $i++) { $name = "dsk$i.fic"; mem_putstr($locations[$i - 1], $name); $path = Alis::$platform->path . $name; if (!sys_fexists($path)) { $fp = sys_fopen($path, 'wb'); if ($fp) { fwrite($fp, chr(0) . chr($i)); sys_fclose($fp); // PORT: commits the write stream to the JumPHP storage } } } } } } } return $script; } function script_unload(?ScriptData $script): void { } function is_delay_script(string $name): bool { $uid = Alis::$platform->uid; $kind = Alis::$platform->kind; if ($uid == C::EGameIshar_1 && stristr($name, 'auteur.') !== null) { Alis::$unload_delay = 5000; return true; } elseif ($uid == C::EGameLeFeticheMaya) { if (stristr($name, 'abomaya.') !== null) { Alis::$load_delay = 1000; } elseif (stristr($name, 'presente.') !== null) { Alis::$load_delay = $kind == C::EPlatformPC ? 2500 : 30000; } elseif (stristr($name, 'sdivers.') !== null) { Alis::$load_delay = 2500; } elseif ($kind == C::EPlatformPC && stristr($name, 'charge.') !== null) { Alis::$load_delay = 2500; } elseif ($kind == C::EPlatformAmiga && stristr($name, 'debuvisi.') !== null) { Alis::$load_delay = 2500; } return true; } elseif ($uid == C::EGameTarghan0 || $uid == C::EGameTarghan1) { if (stristr($name, 'gen.') !== null) { Alis::$load_delay = $kind == C::EPlatformPC || $kind == C::EPlatformMac ? 2500 : 60000; } return true; } else { // EPlatformUnknown matches any platform $loadscr = [ [C::EGameColorado, C::EPlatformPC, 'neigepc.'], [C::EGameStarblade, C::EPlatformPC, 'loading.'], [C::EGameStormMaster, C::EPlatformPC, 'panneau.'], [PlatformConst::EGameMetalMutant, C::EPlatformUnknown, 'apolo.'], [PlatformConst::EGameMetalMutant, C::EPlatformUnknown, 'sono.'], [C::EGameIshar_2, C::EPlatformUnknown, 'present1.'], ]; foreach ($loadscr as [$luid, $lkind, $lname]) { if ($uid == $luid && ($lkind == C::EPlatformUnknown || $lkind == $kind) && stristr($name, $lname) !== null) { Alis::$load_delay = 2500; return true; } } } return false; } // ============================================================================= // MARK: - Script data access // ============================================================================= /** $dest: offset in Alis::$mem. */ function script_read_bytes(int $len, int $dest): void { $s = Alis::$script; if ($len > 0) { mem_write($dest, substr(Alis::$mem, $s->pc, $len)); $s->pc += $len; } } /** $dest: offset in Alis::$mem; copies the NUL-terminated string at pc, NUL included. */ function script_read_until_zero(int $dest): void { $s = Alis::$script; $str = mem_str($s->pc); mem_putstr($dest, $str); $s->pc += strlen($str) + 1; }