= $pos) { $out .= substr($old, $pos, $col - $pos) . $run; $pos = $col + $n; return; } // writing back over the rebuilt part (not in valid films): materialise the line and go on $old = substr_replace($out . substr($old, $pos), $run, $col, $n); $out = ''; $pos = 0; } /** PORT: the finished line ($w bytes). */ function fli_row_end(string $old, string $out, int $pos, int $w): string { if ($pos == 0) { return $old; } $line = $out . substr($old, $pos); return strlen($line) == $w ? $line : substr($line . str_repeat("\0", $w), 0, $w); } /** FLI colour chunk (0xb). PORT: $size (chunk length) to hand the chunk to dos_film_palette() in memory. */ function fli_palette(int $addr, int $size = 0x400): void { $size = \max(2, \min($size, film_data_len() - $addr)); $scratch = film_scratch($size); $bytes = substr(FilmVars::$data, $addr, $size); film_mem_poke($scratch, $bytes, strlen($bytes)); if (!dos_film_palette($scratch)) { $d = FilmVars::$data; $packets = film_rd16le($addr); $index = 0; $addr += 2; do { $index = ($index + ord($d[$addr])) & 0xffff; $addr++; $len = ord($d[$addr]); $addr++; if ($len == 0) { $len = 256; } if ($index + $len > 256) { $len = 256 - $index; $packets = 1; } // PAL_WRITE(image.mpalet, index, len, RGB18(addr)) for ($c = 0; $c < $len; $c++) { image_pal_write('mpalet', $index + $c, ord($d[$addr]) << 2, ord($d[$addr + 1]) << 2, ord($d[$addr + 2]) << 2); $addr += 3; } $index = ($index + $len) & 0xffff; $packets = ($packets - 1) & 0xffff; } while ($packets); } Img::$ftopal = 0xff; } function fli_blackdata(): void { $bf = FilmVars::$bfilm; $w = $bf->width; $line = str_repeat("\0", $w); $rows = &FilmVars::$rows; for ($i = 0; $i < $bf->height; $i++) { $rows[$i] = $line; } } function fli_data(int $addr): void { $bf = FilmVars::$bfilm; $w = $bf->width; $d = FilmVars::$data; $rows = &FilmVars::$rows; for ($i = 0; $i < $bf->height; $i++, $addr += $w) { $line = substr($d, $addr, $w); $rows[$i] = strlen($line) == $w ? $line : substr($line . str_repeat("\0", $w), 0, $w); } } // Reference decoder (BRUN / LC), the C fli_decomp_ref without the unused dirty-rect parameters. function fli_decomp_ref(int $addr, int $partial): void { $bf = FilmVars::$bfilm; $d = FilmVars::$data; $rows = &FilmVars::$rows; $w = $bf->width; $y = 0; $len = $bf->height; if ($partial) { $y = film_rd16le($addr); $addr += 2; $len = film_rd16le($addr); $addr += 2; } // guard: don't write past the vgalogic frame buffer $vgamax = FilmVars::$vgalogic_sz - FilmVars::$vgalogic; $blank = str_repeat("\0", $w); while ($len-- > 0) { if ($y * $w + $w > $vgamax) { break; } $packets = ord($d[$addr]); $addr++; $col = 0; $old = $rows[$y] ?? $blank; $out = ''; $pos = 0; while ($packets-- > 0) { if ($partial) { $col = ($col + ord($d[$addr])) & 0xffff; $addr++; } $count = ord($d[$addr]); $addr++; if ($count >= 0x80) { $count -= 0x100; } if ($partial) { $count = -$count; } if ($count >= 0) { if ($count == 0) { $count = 256; } if ($col + $count > $w) { $count = $w - $col; $len = $packets = 0; } if ($count > 0) { fli_row_put($old, $out, $pos, $col, str_repeat($d[$addr], $count)); } $addr++; } else { $count = -$count; if ($col + $count > $w) { $count = $w - $col; $len = $packets = 0; } if ($count > 0) { fli_row_put($old, $out, $pos, $col, substr($d, $addr, $count)); } $addr += $count; } $col += $count; } if (isset($rows[$y])) { $rows[$y] = fli_row_end($old, $out, $pos, $w); } $y++; } } function fli_decomp(int $addr, int $partial): void { // SDL2 renders full-frame: no dirty tracking. fli_decomp_ref($addr, $partial); } // Push one per-frame audio chunk onto the FLI audio queue. The audio callback splices queued // chunks back-to-back on channel 3 (no per-frame channel reset -> no clicking) and runfilm paces // the video to the queued audio's real-time duration, keeping A/V in sync. function fli_audio(int $nextaddr, int $addr, int $type): void { $length = $nextaddr - $addr; if ($length <= 0) { return; } // Chunks are word-aligned: drop the padding byte of odd-length data (else a click). if ($length & 1) { $length--; } // Rate = chunk bytes * 60 / waitclock: FLI files are sized for the DOS 60 Hz PIT. $src_fps = 60; $bf = FilmVars::$bfilm; if ($type == 0x1300) { // Deus's single audio track is 16-bit signed LE PCM: downconverted to 8-bit below $sample = null; $sample_len = $length >> 1; } else { // Per-language 8-bit audio tracks, selected by the bfilm.id bitmask. // type 0x1000 -> bit 8 (French), 0x1001 -> bit 9 (English), 0x1002 -> bit 10 (German) $mask = ((1 << ($type - 0x1000)) & 0xffff) << 8; if (($mask & ($bf->id & 0xffff)) == 0) { return; } $sample = substr(FilmVars::$data, $addr, $length); $sample_len = $length; } // Sample rate = (samples * original_FLI_tick_hz) / waitclock $f = $bf->waitclock > 0 ? intdiv($src_fps * $sample_len, $bf->waitclock) : 22050; if ($f > 32767) { $f = 32767; // sChannel.freq is s16 } $freq = $f; if ($sample !== null) { fli_audio_push($sample, $sample_len, $freq); return; } $d = FilmVars::$data; for ($done = 0; $done < $sample_len;) { $n = $sample_len - $done > FilmConst::FLI_AUDIO_CHUNK_MAX ? FilmConst::FLI_AUDIO_CHUNK_MAX : $sample_len - $done; $buf = ''; for ($i = 0; $i < $n; $i++) { $buf .= $d[$addr + ($done + $i) * 2 + 1]; } if (fli_audio_push($buf, $n, $freq) < $n) { break; } $done += $n; } } /** * Queue samples on channel 3 for gapless playback; returns how many fit. * PORT: $src is a PHP string of signed 8-bit samples. Each queued chunk is copied to its slot of * fli_audio_copybuf (a host block of Alis::$mem, so sChannel.address works as for any sample) and also kept as * a PHP string in fli_audio_queue[i]['data']. * PORT for fn/audio.php (mixer of sys.c, channel 3): when the sample of channel 3 ends and * FilmVars::$fli_audio_q_head != FilmVars::$fli_audio_q_tail, splice in the next chunk instead of stopping: * FilmVars::$fli_chunks_played++; $t = FilmVars::$fli_audio_q_tail; $q = FilmVars::$fli_audio_queue[$t]; * $ch->address = $q['addr']; $ch->length = $q['length']; $ch->freq = $q['freq']; (restart the sample position) * FilmVars::$fli_audio_q_tail = ($t + 1) & (C::FLI_AUDIO_QUEUE_SIZE - 1); * else (queue empty) FilmVars::$fli_chunks_played++, $ch->type = eChannelTypeNone, $ch->curson = s8(0x80). * While FilmVars::$bfilm->playing, the sound effects must not take channel 3 (audio.c: max_chan = 3). */ function fli_audio_push(string $src, int $len, int $freq): int { // Chunks are copied, not referenced: a streamed film recycles its window before the mixer drains the queue if (FilmVars::$fli_audio_copybuf == 0) { FilmVars::$fli_audio_copybuf = host_alloc(FilmConst::FLI_AUDIO_CHUNK_MAX * C::FLI_AUDIO_QUEUE_SIZE); } $qmask = C::FLI_AUDIO_QUEUE_SIZE - 1; $left = $len; $off = 0; while ($left != 0) { $piece = $left > FilmConst::FLI_AUDIO_CHUNK_MAX ? FilmConst::FLI_AUDIO_CHUNK_MAX : $left; $next = (FilmVars::$fli_audio_q_head + 1) & $qmask; if ($next == FilmVars::$fli_audio_q_tail) { break; // queue full (~2 s buffered) } $head = FilmVars::$fli_audio_q_head; $slot = FilmVars::$fli_audio_copybuf + $head * FilmConst::FLI_AUDIO_CHUNK_MAX; $bytes = substr($src, $off, $piece); film_mem_poke($slot, $bytes, $piece); FilmVars::$fli_audio_queue[$head] = ['addr' => $slot, 'length' => $piece, 'freq' => $freq, 'data' => $bytes]; FilmVars::$fli_audio_q_head = $next; $off += $piece; $left -= $piece; } // Prime idle channel 3; the mixer then advances through the queue itself. $canal = film_channel3(); if ($canal->type == C::eChannelTypeNone && FilmVars::$fli_audio_q_head != FilmVars::$fli_audio_q_tail) { $t = FilmVars::$fli_audio_q_tail; $canal->type = C::eChannelTypeNone; // gate against IRQ mid-update $canal->address = FilmVars::$fli_audio_queue[$t]['addr']; $canal->length = FilmVars::$fli_audio_queue[$t]['length']; $canal->freq = FilmVars::$fli_audio_queue[$t]['freq']; $canal->volume = 0x7f; $canal->loop = 0; $canal->forever = false; $canal->played = 0; $canal->curson = 0x7f; FilmVars::$fli_audio_q_tail = ($t + 1) & $qmask; $canal->type = C::eChannelTypeSample; // last write: re-arm } return $len - $left; } function fli_palette256(int $addr): void { $d = FilmVars::$data; $packets = film_rd16le($addr); $index = 0; $addr += 2; do { $index = ($index + ord($d[$addr])) & 0xffff; $addr++; $len = ord($d[$addr]); $addr++; if ($len == 0) { $len = 256; } if ($index + $len > 256) { $len = 256 - $index; $packets = 1; } // PAL_WRITE(image.mpalet, index, len, RGB24(addr)) for ($c = 0; $c < $len; $c++) { image_pal_write('mpalet', $index + $c, ord($d[$addr]), ord($d[$addr + 1]), ord($d[$addr + 2])); $addr += 3; } $index = ($index + $len) & 0xffff; $packets = ($packets - 1) & 0xffff; } while ($packets); Img::$ftopal = 0xff; } // FLC SS2 word delta (0x7) function fli_ss2(int $addr): void { $bf = FilmVars::$bfilm; $d = FilmVars::$data; $rows = &FilmVars::$rows; $w = $bf->width; $lines = film_rd16le($addr); $addr += 2; $y = 0; $vgamax = FilmVars::$vgalogic_sz - FilmVars::$vgalogic; $blank = str_repeat("\0", $w); while ($lines > 0) { $opcode = film_rd16le($addr); $addr += 2; $sel = $opcode & 0xc000; if ($sel == 0xc000) { $y += (-s16($opcode)) & 0xffff; continue; } if ($sel == 0x8000) { if ($y * $w + $w - 1 < $vgamax && isset($rows[$y]) && $w > 0) { $rows[$y][$w - 1] = chr($opcode & 0xff); } continue; } // packet count for this line $packets = $opcode; $x = 0; $old = $rows[$y] ?? $blank; $out = ''; $pos = 0; while ($packets-- > 0) { $x += ord($d[$addr]); $addr++; $count = ord($d[$addr]); $addr++; if ($count >= 0x80) { $count -= 0x100; } if ($count >= 0) { $n = $count * 2; if ($x + $n > $w) { $n = $x < $w ? $w - $x : 0; } if ($y * $w + $x + $n <= $vgamax && $n > 0) { fli_row_put($old, $out, $pos, $x, substr($d, $addr, $n)); } $addr += $count * 2; $x += $n; } else { $words = -$count; $pair = $d[$addr] . $d[$addr + 1]; $addr += 2; $k = $x + 2 <= $w ? \min($words, ($w - $x) >> 1) : 0; if ($k > 0) { // the C writes word by word while inside vgamax: the whole run is inside it here if ($y * $w + $x + 2 * $k - 1 < $vgamax) { fli_row_put($old, $out, $pos, $x, str_repeat($pair, $k)); } $x += 2 * $k; } } } if (isset($rows[$y])) { $rows[$y] = fli_row_end($old, $out, $pos, $w); } $y++; $lines--; } } function fli_elements(int $addr): void { $endframe = FilmVars::$endframe; while ($addr < $endframe) { $offset = film_rd32le($addr); $addr += 4; $type = film_rd16le($addr); $addr += 2; $nextaddr = $addr + $offset - 6; if ($nextaddr <= $addr - 6) { break; // PORT: a chunk of length 0 (or negative) loops forever in the C } switch ($type) { case 0x4: // FLC 256-colour palette fli_palette256($addr); break; case 0x7: // FLC SS2 word delta fli_ss2($addr); break; case 0xb: fli_palette($addr, $nextaddr - $addr); break; case 0xc: // LC delta fli_decomp($addr, 1); break; case 0xd: // black fli_blackdata(); break; case 0xf: // BRUN fli_decomp($addr, 0); break; case 0x10: // raw fli_data($addr); break; default: // Silmarils audio extension: 0x1000..0x1007 are the 8 per-language track variants // (8-bit) and 0x1300 is Deus's single 16-bit track. if (($type >= 0x1000 && $type <= 0x1007) || $type == 0x1300) { fli_audio($nextaddr, $addr, $type); } } $addr = $nextaddr; } } // ============================================================================ // Film streaming (large films that do not fit in RAM) // ============================================================================ // Bounded sliding window, as the CD original. Each frame is made resident before parsing; the tail is // compacted to the base only when a frame would run past the window end. // PORT: the window is FilmVars::$data (sbuf = 0); see FilmConst::kFilmStreamAbove for when it is used. /** @param resource $fp */ function fli_stream_open($fp, int $file_size): int { $cap = 512 * 1024; fseek($fp, 0, SEEK_SET); $bf = FilmVars::$bfilm; $bf->sfp = $fp; $bf->sbuf = 0; $bf->sbuf_size = $cap; $bf->sfill = 0; // window empty: fli_stream_ensure primes on demand $bf->sremain = $file_size; $bf->sfile_size = $file_size; film_set_data(''); return 1; } function fli_stream_close(): void { $bf = FilmVars::$bfilm; if ($bf->sfp !== null) { fclose($bf->sfp); $bf->sfp = null; } // The window buffer is owned by bfilm.delptr (cdelfilm frees it). $bf->sbuf = null; $bf->sfill = null; $bf->sbuf_size = 0; $bf->sremain = 0; $bf->sfile_size = 0; } // Make `need` bytes starting at bfilm.addr1 resident in the window. // Returns 0 when that can never happen (EOF / I/O error / frame > window). // Keeps bfilm.endptr == bfilm.sfill so flitofen's bounds check stays valid. function fli_stream_ensure(int $need): int { $bf = FilmVars::$bfilm; $have = $bf->sfill - $bf->addr1; if ($have >= $need) { $bf->endptr = $bf->sfill; return 1; } if ($need > $bf->sbuf_size) { return 0; } // Compact only when the frame would extend past the window end. if (($bf->addr1 - $bf->sbuf) + $need > $bf->sbuf_size) { film_set_data(substr(FilmVars::$data, $bf->addr1, $have)); $bf->addr1 = $bf->sbuf; $bf->sfill = $bf->sbuf + $have; } $space = $bf->sbuf_size - ($bf->sfill - $bf->sbuf); $want = $need - $have; if ($want < FilmConst::FLI_STREAM_TOPUP) { $want = FilmConst::FLI_STREAM_TOPUP; } if ($want > $space) { $want = $space; } if ($want > $bf->sremain) { $want = $bf->sremain; } if ($want != 0) { $chunk = ''; while (strlen($chunk) < $want) { $part = fread($bf->sfp, $want - strlen($chunk)); if ($part === false || $part === '') { break; } $chunk .= $part; } $got = strlen($chunk); film_set_data(substr(FilmVars::$data, 0, $bf->sfill) . $chunk); $bf->sfill += $got; $bf->sremain -= $got; if ($got < $want) { $bf->sremain = 0; // short read = EOF or I/O error: end the film cleanly } } $bf->endptr = $bf->sfill; return ($bf->sfill - $bf->addr1) >= $need ? 1 : 0; } // Make the whole chunk at bfilm.addr1 resident: the 0x80-byte FLC file header, or one complete 0xf1fa // frame (its 32-bit length prefix tells us how much). Returns 0 at end of film. function fli_stream_frame(): int { if (!fli_stream_ensure(6)) { return 0; } $addr1 = FilmVars::$bfilm->addr1; $type = film_rd16le($addr1 + 4); $need = 6; if ($type == 0xaf11) { $need = 0x80; // FLC header; first frame starts at +0x80 } elseif ($type == 0xf1fa) { $need = film_rd32le($addr1); // complete frame chunk } return fli_stream_ensure($need); } // Loop replay: rewind the file and empty the window; fli_stream_ensure re-primes on the next flitofen. function fli_stream_rewind(): void { $bf = FilmVars::$bfilm; fseek($bf->sfp, 0, SEEK_SET); $bf->sremain = $bf->sfile_size; $bf->sfill = $bf->sbuf; $bf->addr1 = $bf->sbuf; $bf->addr2 = $bf->sbuf; $bf->endptr = $bf->sbuf; film_set_data(''); } function fli_init(int $flcaddr): void { $bf = FilmVars::$bfilm; $length = film_rd32le($flcaddr); $bf->endptr = $flcaddr + $length; $bf->addr1 = $flcaddr + 0x80; $bf->frames = s16(film_rd16le($flcaddr + 6)); $bf->width = film_rd16le($flcaddr + 8); $bf->height = film_rd16le($flcaddr + 10); $bf->frame = 0; if (!vgalogic_fit(FilmVars::$vgalogic + $bf->width * $bf->height)) { $bf->width = $bf->height = 0; } // Streaming: the file-length-based endptr points far past the window; the window fill boundary is the real bound. if ($bf->sfp !== null) { $bf->endptr = $bf->sfill; } } function fli_next(int $addr): void { $bf = FilmVars::$bfilm; $length = film_rd32le($addr); FilmVars::$endframe = $addr + $length; $fw = $bf->width; $fh = $bf->height; $fsz = $fw * $fh; // PORT: vgalogic as one string per line while the chunks are decoded FilmVars::$rows = $fsz > 0 ? str_split(substr(FilmVars::$pvgalogic, FilmVars::$vgalogic, $fsz), $fw) : []; fli_elements($addr + 16); if ($fsz > 0) { FilmVars::$pvgalogic = substr_replace(FilmVars::$pvgalogic, implode('', FilmVars::$rows), FilmVars::$vgalogic, $fsz); } FilmVars::$rows = []; $dw = Alis::$platform->width; $dh = Alis::$platform->height; $scrsz = $dw * $dh; // the frame's place (C: image.fenx1 / feny1, set by runfilm() from its scene; read again here: the port draws // the screen between two film frames, which changes image.fen*) $bm = $bf->basemain; $dx = s16(read16le($bm + 0xe)); $dy = s16(read16le($bm + 0x10)); $frame = $fsz > 0 ? substr(FilmVars::$pvgalogic, FilmVars::$vgalogic, $fsz) : ''; // PORT: where the film has a picture (FilmVars::$box), and the palette entries it uses if ($fsz > 0) { [$bx1, $by1, $bx2, $by2] = FilmVars::$box ?? [$fw, $fh, 0, 0]; for ($y = 0; $y < $fh; $y++) { $line = substr($frame, $y * $fw, $fw); $left = strspn($line, "\0"); if ($left < $fw) { $bx1 = min($bx1, $left); $bx2 = max($bx2, strlen(rtrim($line, "\0"))); $by1 = min($by1, $y); $by2 = max($by2, $y + 1); } } FilmVars::$box = [$bx1, $by1, $bx2, $by2]; // the entries the film does not use keep the game's colours (the interface around the film) foreach (count_chars($frame, 1) as $i => $_) { FilmVars::$palUsed[$i] = true; } if (FilmVars::$gamePal !== []) { $dpal = FilmVars::$gameDpal !== []; for ($i = 0; $i < 256; $i++) { if (!isset(FilmVars::$palUsed[$i])) { Img::$mpalet[$i] = FilmVars::$gamePal[$i] ?? 0; if ($dpal) { ImageVars::$dpal_mpal[$i * 3] = FilmVars::$gameDpal[$i * 3]; ImageVars::$dpal_mpal[$i * 3 + 1] = FilmVars::$gameDpal[$i * 3 + 1]; ImageVars::$dpal_mpal[$i * 3 + 2] = FilmVars::$gameDpal[$i * 3 + 2]; } } } if ($dpal) { dpal_upload(); } Img::$ftopal = 0xff; } } [$bx1, $by1, $bx2, $by2] = FilmVars::$box ?? [0, 0, 0, 0]; $x1 = max(0, $dx + $bx1); $y1 = max(0, $dy + $by1); $x2 = min($dw, $dx + $bx2); $y2 = min($dh, $dy + $by2); // copy the frame into image.physic (where the film has a picture) $scr = substr(Alis::$mem, Img::$physic, $scrsz); if ($fsz > 0 && $x2 > $x1 && $y2 > $y1) { $n = $x2 - $x1; $lines = str_split($scr, $dw); for ($row = $y1; $row < $y2; $row++) { $lines[$row] = substr($lines[$row], 0, $x1) . substr($frame, ($row - $dy) * $fw + ($x1 - $dx), $n) . substr($lines[$row], $x2); } $scr = implode('', $lines); mem_write(Img::$physic, $scr); } // memcpy(image.logic, image.physic, dw * height) if (Img::$logic != Img::$physic) { mem_write(Img::$logic, $scr); } $bf->addr1 = FilmVars::$endframe; $bf->frame = s16($bf->frame + 1); } function flitofen(): int { $bf = FilmVars::$bfilm; while ($bf->addr1 !== null) { // Streaming film: make the chunk at addr1 fully resident first. if ($bf->sfp !== null && !fli_stream_frame()) { return 0; } if ($bf->addr1 + 6 > film_data_len()) { break; // PORT: past the end of the film bytes (the C reads what follows the buffer) } $type = film_rd16le($bf->addr1 + 4); if ($type == 0xaf11 || $type == 0xaf12) { fli_init($bf->addr1); return 1; } elseif ($type == 0xf100 && $bf->endptr > $bf->addr1) { $length = film_rd32le($bf->addr1); $bf->addr1 += $length ? $length : 16; $bf->frame = s16($bf->frame + 1); continue; } elseif ($type == 0xf1fa && $bf->endptr > $bf->addr1) { fli_next($bf->addr1); return 1; } else { break; } } return 0; }