NL
games/alis / src / Alis / fn / rrq3d_68k.php · commit 9c722cf
<?phpnamespace App\Alis;use App\Alis\Rrq3dVars as V;// Port of alis/src/render/rrq3d_68k.c (MIT, Olivier Huguenot, Vadim Kindl).// Robinson's Requiem 3D terrain renderer, 68k (Atari ST / Falcon / Amiga, big-endian) back end.// The shared globals of rrq3d.c (fprectop, adresa, bothigh, ...) are Rrq3dVars (aliased V here).// See fn/rrq3d.php and docs/porting.md.//// Port choices:// - u8 * pixel pointers are offsets into Alis::$mem; hot loops write through $m = &Alis::$mem.// - u32 16.16 accumulators of the textured renderers (68k ADDX emulation: concat22/carry4) are kept as//   two u16 halves (hi, lo) inside the loops: same bits, no float overflow on 32-bit PHP.// - Dropped like in the DOS port (enhanced 32-bit renderer): the per-pixel depth stamping into//   image.depthrows (barland/tbarland), the terrgba/depthbuf clear of clrvga_68k and the emode/vgatobuf//   branch of landtofi_68k (vgatofen() is always called).// - u32 * / u16 * fills (vgatofen 4-bit mask, clrvga 0x1010) are byte loops: the patterns are byte-uniform.function vgatofen_68k(): void{    Img::$switchgo = 1;    $src = Img::$wlogic + (Img::$clipx1 + (Img::$clipy1 - Img::$wlogy1) * Img::$wloglarg);    Img::$vgamodulo = s16(Img::$wloglarg - ((Img::$clipx2 - Img::$clipx1) + 1));    Img::$bitmodulo = s16(Img::$loglarg * 2 - ((Img::$clipx2 - Img::$clipx1) + 1));    $limit = Alis::$platform->bpp == 4 ? 0xf : 0xff;    $tgt = Img::$logic + Img::$clipx1 + Img::$clipy1 * Img::$loglarg * 2;    $roww = (Img::$clipx2 - Img::$clipx1) + 1;    // roww + bitmodulo == loglarg*2 (dst row stride); roww + vgamodulo == wloglarg (src stride).    $dst_stride = Img::$loglarg * 2;    $src_stride = Img::$wloglarg;    if ($roww > 0) {        if ($limit == 0xff) {            // 8-bit games: straight row copy            for ($y = Img::$clipy1; $y <= Img::$clipy2; $y++, $tgt += $dst_stride, $src += $src_stride) {                rrq_copy($tgt, $src, $roww);            }        } else {            // 4-bit games: mask to `limit` (C: a long = 4 px at a time); high nibbles come from clrvga's            // 0x10 fill and shade bytes written every frame.            $m = &Alis::$mem;            $mask = \str_repeat(\chr($limit), $roww);            for ($y = Img::$clipy1; $y <= Img::$clipy2; $y++, $tgt += $dst_stride, $src += $src_stride) {                $row = \substr($m, $src, $roww) & $mask;                for ($i = 0; $i < $roww; $i++) {                    $m[$tgt + $i] = $row[$i];                }            }        }    }    dirty_add(Img::$clipx1, Img::$clipy1, Img::$clipx2 - Img::$clipx1 + 1, Img::$clipy2 - Img::$clipy1 + 1);}function clrvga_68k(): void{    // m68k: fill with 0x1010 using 2-byte writes, width = fenx2-fenx1    $width = (Img::$fenx2 - Img::$fenx1) & 0xffff;    $rows = s16(Img::$feny2 - Img::$feny1);    $w2 = (($width >> 1) + 1) & 0xffff;    $modulo = s16((Img::$wloglarg - 1) - $width);    $ptr = Img::$wlogic + Img::$fenx1 + ((Img::$feny1 - Img::$wlogy1) & 0xffff) * (Img::$wloglarg & 0xffff);    $n = $w2 * 2;    for ($y = $rows; $y >= 0; $y--) {        rrq_fill($ptr, 0x10, $n);        $ptr += $n + $modulo;    }    // PORT: the enhanced RGBA + depth buffer clear (image.terrgba/depthbuf) is dropped.}function calclan0_68k(int $scene_addr, int $render_context): void{    $rc = $render_context;    $gs = xread16($rc - 0x3c0) & 0x3f; // (u32)(u16)grid_shift & 0x3f    // Compute signed axis deltas and their absolute values    Img::$signedx = s16(xread16($rc - 0x378) - xread16($rc - 0x37e));    $abs_dx = Img::$signedx;    if (Img::$signedx < 0) {        $abs_dx = s16(-Img::$signedx);    }    Img::$signedy = s16(xread16($rc - 0x376) - xread16($rc - 0x37c));    $abs_dy = Img::$signedy;    if (Img::$signedy < 0) {        $abs_dy = s16(-Img::$signedy);    }    // Determine dominant axis and compute fixed-point stepping    if ($abs_dx < $abs_dy) {        // Y-dominant: step along Y, interpolate X        xwrite16($rc - 0x2b0, $abs_dy);        $minor_span = (xread16($rc - 0x36a) - xread16($rc - 0x370)) & 0xffff;        if ($minor_span & 0x8000) {            $minor_span = (-$minor_span) & 0xffff;        }        xwrite16($rc - 0x2ac, $minor_span);        $step_num = s16(xread16($rc - 0x378) - xread16($rc - 0x37e));        $frac = s16(cdiv(shl32(cmod($step_num, $abs_dy) & 0xffff, 0x10) >> 1, $abs_dy));        xwrite32($rc - 0x2c4, s32(shl32(cdiv($step_num, $abs_dy) & 0xffff, 0x10) + $frac * 2));        $dir_sign = 0x10000;        if (Img::$signedy < 0) {            $dir_sign = -0x10000; // 0xffff0000        }        xwrite32($rc - 0x2c0, $dir_sign);        $cross_corr = s16((s16(xread16($rc - 0x37c) - xread16($rc - 0x370)) * s16(xread32($rc - 0x2c4) >> 2)) >> 0xe);        if (xread32($rc - 0x2c0) < 0) {            $cross_corr = s16(-$cross_corr);        }        $strip_span = ((xread16($rc - 0x372) + $cross_corr) - xread16($rc - 0x37e)) & 0xffff;        if ($strip_span & 0x8000) {            $strip_span = (-$strip_span) & 0xffff;        }        xwrite16($rc - 0x24e, s16($strip_span) >> $gs);    } else {        // X-dominant: step along X, interpolate Y        xwrite16($rc - 0x2b0, $abs_dx);        $minor_span = (xread16($rc - 0x36c) - xread16($rc - 0x372)) & 0xffff;        if ($minor_span & 0x8000) {            $minor_span = (-$minor_span) & 0xffff;        }        xwrite16($rc - 0x2ac, $minor_span);        $step_num = s16(xread16($rc - 0x376) - xread16($rc - 0x37c));        $frac = s16(cdiv(shl32(cmod($step_num, $abs_dx) & 0xffff, 0x10) >> 1, $abs_dx));        xwrite32($rc - 0x2c0, s32(shl32(cdiv($step_num, $abs_dx) & 0xffff, 0x10) + $frac * 2));        $dir_sign = 0x10000;        if (Img::$signedx < 0) {            $dir_sign = -0x10000; // 0xffff0000        }        xwrite32($rc - 0x2c4, $dir_sign);        $cross_corr = s16((s16(xread16($rc - 0x37e) - xread16($rc - 0x372)) * s16(xread32($rc - 0x2c0) >> 2)) >> 0xe);        if (xread32($rc - 0x2c4) < 0) {            $cross_corr = s16(-$cross_corr);        }        $strip_span = ((xread16($rc - 0x370) + $cross_corr) - xread16($rc - 0x37c)) & 0xffff;        if ($strip_span & 0x8000) {            $strip_span = (-$strip_span) & 0xffff;        }        xwrite16($rc - 0x24e, s16($strip_span) >> $gs);    }    // Compute per-strip grid stepping (16.16 fixed-point)    $strip_count = xread16($rc - 0x24e);    $delta_x = s16(xread16($rc - 0x372) - xread16($rc - 0x37e));    $frac_x = s16(cdiv(shl32(cmod($delta_x, $strip_count) & 0xffff, 0x10) >> 1, $strip_count));    xwrite32($rc - 0x2bc, s32(shl32(cdiv($delta_x, $strip_count) & 0xffff, 0x10) + $frac_x * 2) >> $gs);    $delta_y = s16(xread16($rc - 0x370) - xread16($rc - 0x37c));    $frac_y = s16(cdiv(shl32(cmod($delta_y, $strip_count) & 0xffff, 0x10) >> 1, $strip_count));    xwrite32($rc - 0x2b8, s32(shl32(cdiv($delta_y, $strip_count) & 0xffff, 0x10) + $frac_y * 2) >> $gs);    // Direction signs for grid stepping    $dir_sign_x = 0x10000;    if (xread32($rc - 700) < 0) {        $dir_sign_x = -0x10000;    }    xwrite32($rc - 0x2a0, $dir_sign_x);    $dir_sign_y = 0x10000;    if (xread32($rc - 0x2b8) < 0) {        $dir_sign_y = -0x10000;    }    xwrite32($rc - 0x29c, $dir_sign_y);    // Dimension scaling delta per strip    xwrite32($rc - 0x2b4, (s16(cdiv(s16(xread16($rc - 0x2ac) - xread16($rc - 0x2b0)) << 8, xread16($rc - 0x24e))) << 8) >> $gs);    // Fog/palette interpolation setup    xwrite32($rc - 0x252, 0);    xwrite32($rc - 0x256, shl32(xread16($scene_addr + 0x9e) & 0xffff, 0x10));    if (xread16($scene_addr + 0x98) != 0) {        xwrite32($rc - 0x256, 0x60000);        $fog_level = xread16($scene_addr + 0x9e) & 0xffff;        if (s16($fog_level) < 7) {            xwrite32($rc - 0x256, 0);        }        if (-1 < xread16($scene_addr + 0x98)) {            xwrite32($rc - 0x256, s32(shl32(xread16($scene_addr + 0xa0) & 0xffff, 0x10) - 1));        }        xwrite32($rc - 0x252, s16(cdiv(s32(shl32($fog_level, 0x10) - xread32($rc - 0x256)) >> 8, s16(xread16($rc - 0x24e) + 1))) << 8);    }    // Per-strip interpolation deltas and initial values    $strip_count = xread16($rc - 0x24e);    xwrite32($rc - 0x2e4, s16(cdiv(s32(xread32($rc - 0x318) - xread32($rc - 0x330)), $strip_count)));    xwrite32($rc - 0x2dc, s16(cdiv(mul32(s32(xread32($rc - 0x2f0) - xread32($rc - 0x2fc)), 0x100), $strip_count)) << 8);    xwrite32($rc - 0x2d4, s16(cdiv(s32(xread32($rc - 0x310) - xread32($rc - 0x328)), $strip_count)));    xwrite32($rc - 0x2cc, s16(cdiv(mul32(s32(xread32($rc - 0x2ec) - xread32($rc - 0x2f8)), 0x100), $strip_count)) << 8);    xwrite32($rc - 0x2e8, xread32($rc - 0x330));    xwrite32($rc - 0x2e0, shl32(xread32($rc - 0x32c), 0x10));    xwrite32($rc - 0x2d8, xread32($rc - 0x328));    xwrite32($rc - 0x2d0, xread32($rc - 0x2f8));}// ---------------------------------------------------------------------------------------------// Helpers (static inline in the C)// ---------------------------------------------------------------------------------------------/** (x << 8) | (x >> 24) on a u32 (s32 bits). */function rot8(int $x): int{    return ((((($x >> 16) & 0xff) ^ 0x80) - 0x80) << 24) | (($x & 0xffff) << 8) | (($x >> 24) & 0xff);}// =========================================================================// Flat bars: a transliteration of the original asm (bartra/bartrag/bartrar/bartram/bartramin/bartrab*),// which is the reference. Register names are kept where they carry the logic: d4/d1 (or d2) are the// two dither phases, swapped per row.// =========================================================================// The unrolled line bodies: count index i writes i+1 bytes as i>>2 longs of d4 (one more when// i&3 == 3), then a d4 word (i&3 == 1, 2) and a byte of `alt` (i&3 == 0, 2). Negative i lands// in the table guard and writes nothing. (put_l / put_w: big-endian bytes of d4.)function line_fwd(int $p, int $i, int $d4, int $alt): int{    $i = s16($i);    if ($i < 0) {        return $p;    }    $m = &Alis::$mem;    $j = $i & 3;    $n = ($i >> 2) + ($j == 3 ? 1 : 0);    if ($n > 0) {        $c0 = \chr(($d4 >> 24) & 0xff);        $c1 = \chr(($d4 >> 16) & 0xff);        $c2 = \chr(($d4 >> 8) & 0xff);        $c3 = \chr($d4 & 0xff);        for (; $n > 0; $n--) {            $m[$p++] = $c0;            $m[$p++] = $c1;            $m[$p++] = $c2;            $m[$p++] = $c3;        }    }    if ($j == 1 || $j == 2) {        $m[$p++] = \chr(($d4 >> 8) & 0xff);        $m[$p++] = \chr($d4 & 0xff);    }    if ($j == 0 || $j == 2) {        $m[$p++] = \chr($alt & 0xff);    }    return $p;}// tg bodies: the same, written right to left with pre-decrement.function line_bwd(int $p, int $i, int $d4, int $alt): int{    $i = s16($i);    if ($i < 0) {        return $p;    }    $m = &Alis::$mem;    $j = $i & 3;    $n = ($i >> 2) + ($j == 3 ? 1 : 0);    if ($n > 0) {        $c0 = \chr(($d4 >> 24) & 0xff);        $c1 = \chr(($d4 >> 16) & 0xff);        $c2 = \chr(($d4 >> 8) & 0xff);        $c3 = \chr($d4 & 0xff);        for (; $n > 0; $n--) {            $m[--$p] = $c3;            $m[--$p] = $c2;            $m[--$p] = $c1;            $m[--$p] = $c0;        }    }    if ($j == 1 || $j == 2) {        $m[--$p] = \chr($d4 & 0xff);        $m[--$p] = \chr(($d4 >> 8) & 0xff);    }    if ($j == 0 || $j == 2) {        $m[--$p] = \chr($alt & 0xff);    }    return $p;}function pixrow(int $y): int{    return xread32(Img::$atlpix + s16(($y - Img::$wlogy1) * 4));}// bartra/bartrab head: dark level from the cell, then the shade lookup -> 2-periodic long.function bar_shade(int $cell, int $rc, int $index): int{    $dark = s16((((xread16(xread16($rc - 0x3c4) + $cell) & 0xc0) + ((xread16($cell + 2) >> 8) & 0xc0) + ((xread16($cell) >> 8) & 0xc0) * 2) * -2) + Img::$vdarkw);    if ($dark < 0) {        $dark = 0;    }    if (Alis::$platform->bpp == 4) {        $c = xread16(Alis::$ptrdark + s16(($dark & 0xff00) | ((xread8($rc + 8 + $index) * 2) & 0xff))) & 0xffff;    } else {        $c = (xread8(Alis::$ptrdark + s16(($dark & 0xff00) | xread8($rc + 8 + $index))) << 8) | xread8(Alis::$ptrdark + s16(($dark & 0xff00) | xread8($rc + 9 + $index)));    }    return concat22($c, $c);}// bartra32/bartrab head: slope step from atalias. (u16 d2, u16 larg; returns u16)function alias_step(int $d2, int $larg): int{    $d2 &= 0xffff;    $larg &= 0xffff;    if ($d2 <= 0x40 && $larg <= 0x3f) {        return xread16(Img::$atalias + s16(((($d2 - 1) << 6) + $larg) * 2)) & 0xffff;    }    $d2 >>= 2;    if ($d2 > 0x40) {        $d2 = 0x40;    }    return (xread16(Img::$atalias + s16(((($d2 - 1) << 7) + $larg) & 0xfffe)) >> 1) & 0xffff;}function dither_phase(int $d4, int $y, int $rc): int{    if ($y & 1) {        $d4 = rot8($d4);    }    if (xread16($rc - 0x24e) & 1) {        $d4 = rot8($d4);    }    return $d4;}// 16.16 start of a slope/taper: (y - top) << 8, times the step (mulu.w on the low word).function slope_start(int $d0, int $d1, int $d2): int{    return $d0 > $d1 ? mul32((($d0 - $d1) << 8) & 0xffff, $d2 & 0xffff) : 0;}function add_hi(int $d3, int $v): int{    return concat22(($d3 >> 16) + $v, $d3);}// bartramin (+ bartrams): d5+1 full rows of d6+1 bytes from a0; then the bottom taper if pending.function bartramin_flat(int $rc, int $a0, int $d5, int $d6, int $d4): void{    $d2 = rot8($d4);    $mod = s16(Img::$wloglarg - $d6 - 1);    if ($a0 & 1) {        Alis::$mem[$a0++] = \chr($d4 & 0xff);        for (;;) {            $a0 = line_fwd($a0, $d6 - 1, $d4, $d2) + $mod;            $t = $d2;            $d2 = $d4;            $d4 = $t;            $d5 = s16($d5 - 1);            if ($d5 < 0) {                break;            }            Alis::$mem[$a0++] = \chr($d4 & 0xff);        }    } else {        for (;;) {            $a0 = line_fwd($a0, $d6, $d4, $d2) + $mod;            $t = $d2;            $d2 = $d4;            $d4 = $t;            $d5 = s16($d5 - 1);            if ($d5 < 0) {                break;            }        }    }    if (Img::$vbarbot) {        bartrab_flat($rc, s16(V::$bothigh), s16(V::$bothigh), Img::$vbarbot, $d6, $d4); // bartrams -> bartrabin    }}// bartram: d5 rows from the bar top.function bartram_flat(int $rc, int $d0, int $d5, int $d6, int $d4): void{    $d5 = s16($d5 - 1);    if ($d5 < 0) {        return;    }    $d4 = dither_phase($d4, $d0, $rc);    bartramin_flat($rc, pixrow($d0) + Img::$precx, $d5, $d6, $d4);}// bartrabin: bottom taper, d5 rows from d0 narrowing by the botalt step. d1 = taper top.function bartrab_flat(int $rc, int $d0, int $d1, int $d5, int $d6, int $d4): void{    Img::$vbarbot = 0;    $d5 = s16($d5 - 1);    if ($d5 < 0) {        return;    }    $d2 = alias_step(V::$botalt - V::$bothigh, Img::$vbarlarg);    $d3 = s32(-slope_start($d0, $d1, $d2));    $d3 = add_hi($d3, Img::$vbarlarg);    $step = -(s16($d2) << 8);    $wlog = s16(Img::$wloglarg);    $m = &Alis::$mem;    if ((V::$botalt & 0xffff) != (V::$precboti & 0xffff)) {        // bartrabr: anchored left        if (s16(Img::$vbarlarg - $d6) > 0) {            $d3 = add_hi($d3, Img::$vbarx - Img::$precx);        }        $d4 = dither_phase($d4, $d0, $rc);        $a2 = pixrow($d0) + Img::$precx;        $a0 = $a2;        $d1r = rot8($d4);        $odd = $a0 & 1;        for (;;) {            $d7 = s16($d3 >> 16);            if ($d7 > $d6) {                $d7 = $d6;            }            if ($odd) {                $m[$a0++] = \chr($d4 & 0xff);                line_fwd($a0, $d7 - 1, $d4, $d1r);            } else {                line_fwd($a0, $d7, $d4, $d1r);            }            $t = $d1r;            $d1r = $d4;            $d4 = $t;            $d3 = s32($d3 + $step);            if ($d3 < 0) {                break;            }            $a2 += $wlog;            $a0 = $a2;            $d5 = s16($d5 - 1);            if ($d5 == -1) {                break;            }        }    } else {        // bartrabg: anchored right, written right to left        if (s16(Img::$vbarlarg - $d6) > 0) {            $d3 = add_hi($d3, Img::$precx + $d6 - Img::$vbarx - Img::$vbarlarg);        }        $d4 = dither_phase($d4, $d0, $rc);        $a2 = pixrow($d0) + Img::$precx + 1 + $d6;        $a0 = $a2;        $d1r = rot8($d4);        $odd = $a0 & 1;        for (;;) {            $d7 = s16($d3 >> 16);            if ($odd) {                $m[--$a0] = \chr($d4 & 0xff);                line_bwd($a0, $d7 - 1, $d4, $d1r);            } else {                                                      // no clamp on the odd path                if ($d7 > $d6) {                    $d7 = $d6;                }                line_bwd($a0, $d7, $d4, $d1r);            }            $t = $d1r;            $d1r = $d4;            $d4 = $t;            $d3 = s32($d3 + $step);            if ($d3 < 0) {                break;            }            $a2 += $wlog;            $a0 = $a2;            $d5 = s16($d5 - 1);            if ($d5 == -1) {                break;            }        }    }}// bartrag/bartrar: top slope, d5 rows from d0 widening by the step. Returns the next row start// (left edge) and the phase for it (in $d4p).function bartra_slope(int $rc, int $d0, int $d1, int $d5, int $d6, int $d2, int &$d4p): int{    $d4 = $d4p;    $d3 = slope_start($d0, $d1, $d2);    $step = s16($d2) << 8;    $wlog = s16(Img::$wloglarg);    $rtl = xread16($rc - 0x246) != xread16($rc - 0x25c);    if (s16(Img::$vbarlarg - $d6) > 0) {        $d3 = add_hi($d3, $rtl ? Img::$precx + $d6 - Img::$vbarx - Img::$vbarlarg : Img::$vbarx - Img::$precx);    }    $d4 = dither_phase($d4, $d0, $rc);    $a2 = pixrow($d0) + Img::$precx + ($rtl ? 1 + $d6 : 0);    $a0 = $a2;    $d1r = rot8($d4);    $odd = $a0 & 1;    $m = &Alis::$mem;    $d5 = s16($d5 + 1);    $d7 = s16($d3 >> 16);    for (;;) {        $over = $odd ? ($d7 & 0xffff) > ($d6 & 0xffff) : $d7 > $d6;   // dbhi on odd rows, dbgt on even        if (!$over) {            $d5 = s16($d5 - 1);            if ($d5 == -1) {                break;            }            if ($odd) {                if ($rtl) {                    $m[--$a0] = \chr($d4 & 0xff);                    line_bwd($a0, $d7 - 1, $d4, $d1r);                } else {                    $m[$a0++] = \chr($d4 & 0xff);                    line_fwd($a0, $d7 - 1, $d4, $d1r);                }            } else {                if ($rtl) {                    line_bwd($a0, $d7, $d4, $d1r);                } else {                    line_fwd($a0, $d7, $d4, $d1r);                }            }        } else {            if ($d5 < 0) {                break;            }            if ($odd && $d7 < 0) {                $d5 = s16($d5 - 1);                if ($d5 == -1) {                    break;                  // bartragi11/bartrari11: skip the row                }            } else {                $d7 = $d6;                continue;            }        }        $t = $d1r;        $d1r = $d4;        $d4 = $t;        $d3 = s32($d3 + $step);        $a2 += $wlog;        $a0 = $a2;        $d7 = s16($d3 >> 16);    }    $d4p = $d4;    return $rtl ? $a0 - 1 - $d6 : $a0;}// bartra after the shade lookup: ground or overhang bar. d0 = clipped top, bary = unclipped top.function bartra_shaded(int $rc, int $d0, int $d5, int $d6, int $bary, int $d4): void{    $r246 = xread16($rc - 0x246);    $d7 = s16($r246 - $d0);    if ($d7 <= 1) {        bartram_flat($rc, $d0, $d5, $d6, $d4);        return;    }    $d5 = s16($d5 - $d7);    if ($d5 < 0) {        $d7 = s16($d7 + $d5);    }    if ($d7 <= 0) {        bartram_flat($rc, $d0, $d5, $d6, $d4);        return;    }    Img::$vbarmid = $d5;    $d5 = s16($d7 - 1);    $d7 = s16($r246 - $bary);    if ($d7 <= 1) {        bartram_flat($rc, $d0, Img::$vbarmid, $d6, $d4);        return;    }    $a0 = bartra_slope($rc, $d0, $bary, $d5, $d6, alias_step($d7, Img::$vbarlarg), $d4);    $d5 = s16(Img::$vbarmid - 1);    if ($d5 < 0) {        return;    }    bartramin_flat($rc, $a0, $d5, $d6, $d4);}function bartra_68k(int $terrain_cell, int $render_context, int $drawy, int $index, int $barwidth, int $barheight, int $bary): void{    bartra_shaded($render_context, s16($drawy), s16($barheight), s16($barwidth - 1), s16($bary), bar_shade($terrain_cell, $render_context, $index));}// tbarland bottom-only entry (bartrab): the bar lies wholly below bothigh.function bartrab_68k(int $terrain_cell, int $render_context, int $drawy, int $index, int $barwidth, int $barheight): void{    bartrab_flat($render_context, s16($drawy), s16(V::$bothigh), s16($barheight), s16($barwidth - 1), bar_shade($terrain_cell, $render_context, $index));}// =========================================================================// Falcon CD textured renderer helpers (port: shared code of the C, halves instead of u32)// =========================================================================/** Darkness of the textured bars (same formula as flat bartra); returns the u32 'dark' (0 when negative). */function rrq68_tex_dark(int $terrain_cell, int $render_context): int{    $dark_val = (Img::$vdarkw + (((xread16(xread16($render_context - 0x3c4) + $terrain_cell) & 0xffff & 0xc0) + ((xread16($terrain_cell + 2) >> 8) & 0xc0) + ((xread16($terrain_cell) >> 8) & 0xc0) * 2) * -2)) & 0xffff;    return ($dark_val & 0x8000) ? 0 : $dark_val;}/** * Texture setup of bartra_textured_68k (and the identical inline copy in tbarland_68k): * returns [width_mask, vsh, vsl (v_step), hsh, hsl (h_step_per_line), Vh, Vl (V), height_mask, tex_base]. */function rrq68_tex_setup(int $render_context, int $index, int $max_cols, int $row_offset): array{    // Resolve texture from type entry (need width_mask before shift derivation)    $tex_table_ptr = xread32($render_context + $index + 4);    $tex_offset = xread32($tex_table_ptr);    $width_mask = xread16(s32($tex_table_ptr + $tex_offset + 2)) & 0xffff;    // Shift: read from type entry; if 0, derive from width_mask (Falcon CD    // game scripts don't initialise this field — floppy never needed it)    $shift = xread8($render_context + $index) & 0x3f;    if ($shift == 0 && $width_mask != 0) {        $w = ($width_mask + 1) & 0xffff;        while ($w > 1) {            $shift++;            $w >>= 1;        }    }    $persp = Img::$tex_persp_step;    $persp_unrolled = rot8($persp);    // V step per scanline    $vsh = ($persp >> 16) & 0xffff;    $vsl = (($persp & 0xffff) << $shift) & 0xffff;    // H step per scanline (accounts for left-clipped columns)    $clip_cols = (Img::$vbarlarg - $max_cols) & 0xffff;    $h_step_raw = concat22(($persp_unrolled & 0xffff) * $clip_cols, 0);    $h_step_per_line = rot8($h_step_raw);    $hsh = ($h_step_per_line >> 16) & 0xffff;    $hsl = $h_step_per_line & 0xffff;    // V initial from row offset    $v_init_raw = concat22(($persp_unrolled & 0xffff) * ($row_offset & 0xffff), 0);    $Vh = ($v_init_raw >> 8) & 0xffff;    $Vl = ((($v_init_raw >> 24) & 0xff) << $shift) & 0xffff;    // Height mask and texture base    $type_v_base = xread16($render_context + $index + 2);    $hmask_raw = s16(xread16(s32($tex_table_ptr + $tex_offset + 4)) - $type_v_base);    // Snap to nearest power-of-2 minus 1 (required for AND masking).    // Original Falcon CD resources may have non-power-of-2 heights (e.g. 36).    if ($hmask_raw > 0 && ($hmask_raw & ($hmask_raw + 1)) != 0) {        $h = $hmask_raw & 0xffff;        $h |= $h >> 1;        $h |= $h >> 2;        $h |= $h >> 4;        $h |= $h >> 8;        $hmask_raw = s16($h >> 1);    }    $height_mask = s16($hmask_raw << $shift);    // Image header: +0 format, +2 width_mask, +4 height, +6 extra, +8 pixel data    $tex_base = s32($tex_table_ptr + shl32($type_v_base, $shift) + $tex_offset + 8);    return [$width_mask, $vsh, $vsl, $hsh, $hsl, $Vh, $Vl, $height_mask, $tex_base];}/** * One scanline of bartra_textured_68k (slope and main body): cnt+1 texels, LTR (write, fb++) or RTL * (--fb, write). H = (hh, hl); $vl = low word of Vm, updated as the C does (Vm.lo = H.lo ^ uv). * $dk = alis.ptrdark + (s16)(dark & 0xff00). Returns fb. */function rrq68_tex_row(int $fb, int $cnt, bool $rtl, int $hh, int $hl, int &$vl, int $ph, int $pl, int $wm, int $tex_base, int $dk): int{    $m = &Alis::$mem;    $v = $vl;    if ($rtl) {        while ($cnt >= 0) {            $uv = $hl | $v;            $texel = \ord($m[$tex_base + (($uv ^ 0x8000) - 0x8000)]);            $v = $hl ^ $uv;            $m[--$fb] = $m[$dk + $texel];            $lo = $pl + $hl;            $hi = $ph + $hh + ($lo >> 16);            $hh = $hi & 0xffff;            $hl = $wm & (($lo & 0xffff) + ($hi >> 16));            $cnt--;        }    } else {        while ($cnt >= 0) {            $uv = $hl | $v;            $texel = \ord($m[$tex_base + (($uv ^ 0x8000) - 0x8000)]);            $v = $hl ^ $uv;            $m[$fb++] = $m[$dk + $texel];            $lo = $pl + $hl;            $hi = $ph + $hh + ($lo >> 16);            $hh = $hi & 0xffff;            $hl = $wm & (($lo & 0xffff) + ($hi >> 16));            $cnt--;        }    }    $vl = $v;    return $fb;}/** * One scanline of bartrab_textured_68k: there H.lo is recovered as v_lo ^ uv (H_recov) and v_lo * stays constant. Returns fb. */function rrq68_texb_row(int $fb, int $cnt, bool $rtl, int $hh, int $hl, int $v_lo, int $ph, int $pl, int $wm, int $tex_base, int $dk): int{    $m = &Alis::$mem;    while ($cnt >= 0) {        $uv = $v_lo | $hl;        $texel = \ord($m[$tex_base + (($uv ^ 0x8000) - 0x8000)]);        $rl = $v_lo ^ $uv;        if ($rtl) {            $m[--$fb] = $m[$dk + $texel];        } else {            $m[$fb++] = $m[$dk + $texel];        }        $lo = $pl + $rl;        $hi = $ph + $hh + ($lo >> 16);        $hh = $hi & 0xffff;        $hl = $wm & (($lo & 0xffff) + ($hi >> 16));        $cnt--;    }    return $fb;}// =========================================================================// Falcon CD textured bottom narrowing renderer// Ghidra barland CD lines 23774-23901, tbarland CD lines 24249-24389// persp, v_step, h_step_per_line, V: u32 as s32 bits.// =========================================================================function bartrab_textured_68k(int $render_context, int $index, int $max_cols, int $bot_lines, int $dark, int $persp, int $v_step, int $h_step_per_line, int $V, int $width_mask, int $height_mask, int $tex_base): void{    $max_cols = s16($max_cols);    $bot_lines = s16($bot_lines);    $vbarlarg = Img::$vbarlarg;    // Atalias lookup for bottom slope    $bot_dist = (V::$botalt - V::$bothigh) & 0xffff;    if (($bot_dist < 0x41) && ($vbarlarg < 0x40)) {        $slope_step = xread16(Img::$atalias + s16(($vbarlarg + ($bot_dist - 1) * 0x40) * 2)) & 0xffff;    } else {        $bot_dist >>= 2;        if (0x40 < $bot_dist) {            $bot_dist = 0x40;        }        $slope_step = (xread16(Img::$atalias + s16(($vbarlarg + ($bot_dist - 1) * 0x80) & 0xfffe)) >> 1) & 0xffff;    }    $clip_cols = ($vbarlarg - $max_cols) & 0xffff;    $wlog = Img::$wloglarg;    $dk = Alis::$ptrdark + s16($dark & 0xff00);    $ph = ($persp >> 16) & 0xffff;    $pl = $persp & 0xffff;    $vsh = ($v_step >> 16) & 0xffff;    $vsl = $v_step & 0xffff;    $hh0 = ($h_step_per_line >> 16) & 0xffff;    $hl0 = $width_mask & $h_step_per_line & 0xffff;    $Vh = ($V >> 16) & 0xffff;    $Vl = $V & 0xffff;    $slope_inc = s16($slope_step) * -0x100;    $rtl = (V::$botalt & 0xffff) == (V::$precboti & 0xffff);    if ($rtl) {        // RTL bottom narrowing (Ghidra lines 23789-23844)        // (the C's botf initial offset is dead code: always 0 for the standard after-main-body path)        $col_start = $vbarlarg & 0xffff;        if ($clip_cols != 0 && $max_cols <= $vbarlarg) {            $col_start = ((($max_cols + Img::$precx + $col_start) - Img::$vbarx) - $vbarlarg) & 0xffff;        }        $fb = s32($max_cols + Img::$precx + xread32(Img::$atlpix + s16(((V::$bothigh & 0xffff) - Img::$wlogy1) * 4)) + 1);    } else {        // LTR bottom narrowing (Ghidra lines 23846-23900)        $col_start = $vbarlarg & 0xffff;        if ($clip_cols != 0 && $max_cols <= $vbarlarg) {            $col_start = ((Img::$vbarx + $col_start) - Img::$precx) & 0xffff;        }        $fb = s32(Img::$precx + xread32(Img::$atlpix + s16(((V::$bothigh & 0xffff) - Img::$wlogy1) * 4)));    }    $pix_frac = shl32($col_start, 0x10);    $row_fb = $fb;    do {        $col_count = ($pix_frac >> 16) & 0xffff;        if ($max_cols < s16($col_count)) {            $col_count = $max_cols & 0xffff;        }        $v_lo = $height_mask & $Vl;        rrq68_texb_row($fb, s16($col_count), $rtl, $hh0, $hl0, $v_lo, $ph, $pl, $width_mask, $tex_base, $dk);        // V = v_step + Vsaved (Vsaved = V.hi : v_lo), carry folded into the low word        $lo = $vsl + $v_lo;        $hi = $vsh + $Vh + ($lo >> 16);        $Vh = $hi & 0xffff;        $Vl = $lo & 0xffff;        if ($hi >> 16) {            $Vl = ($width_mask + $Vl + 1) & 0xffff;        }        $pix_frac = s32($pix_frac + $slope_inc);        $fb = $row_fb + $wlog;        $row_fb = $fb;        if ($pix_frac < 0) {            break;        }        $bot_lines = s16($bot_lines - 1);    } while ($bot_lines != -1);}// =========================================================================// Falcon CD textured bar renderer — slope + main body + bottom dispatch// Ghidra barland CD lines 23575-23907// =========================================================================function bartra_textured_68k(int $terrain_cell, int $render_context, int $drawy, int $index, int $barwidth, int $barheight, int $bary): void{    $drawy = s16($drawy);    $bary = s16($bary);    $barheight = s16($barheight);    $max_cols = s16($barwidth - 1);    $row_offset = s16($drawy - $bary);    $vbarlarg = Img::$vbarlarg;    $wlog = Img::$wloglarg;    // Darkness: same formula as flat bartra    $dark = rrq68_tex_dark($terrain_cell, $render_context);    $dk = Alis::$ptrdark + s16($dark & 0xff00);    [$width_mask, $vsh, $vsl, $hsh, $hsl, $Vh, $Vl, $height_mask, $tex_base] = rrq68_tex_setup($render_context, $index, $max_cols, $row_offset);    $persp = Img::$tex_persp_step;    $ph = ($persp >> 16) & 0xffff;    $pl = $persp & 0xffff;    $clip_cols = ($vbarlarg - $max_cols) & 0xffff;    $hl_row = $width_mask & $hsl;    // Framebuffer pointer — flows through slope into main body    $fb = s32(Img::$precx + xread32(Img::$atlpix + s16(($drawy - Img::$wlogy1) * 4)));    // Slope section    $r246 = xread16($render_context - 0x246);    $top_lines = s16($r246 - $drawy);    $mid_height = $barheight;    if ($top_lines != 0 && $drawy < $r246 && s16($r246 - $bary) >= 2) {        $mid_height = s16($barheight - $top_lines);        if ($barheight < $top_lines) {            $top_lines = s16($mid_height + $top_lines); // clamp to barheight        }        if ($top_lines >= 1) {            $tex_step_dist = ($r246 - $bary) & 0xffff;            Img::$vbarmid = $mid_height;            if (s16($tex_step_dist) >= 2) {                if (($tex_step_dist < 0x41) && ($vbarlarg < 0x40)) {                    $slope_step = xread16(Img::$atalias + s16(($vbarlarg + ($tex_step_dist - 1) * 0x40) * 2)) & 0xffff;                } else {                    $tex_step_dist >>= 2;                    if (0x40 < $tex_step_dist) {                        $tex_step_dist = 0x40;                    }                    $slope_step = (xread16(Img::$atalias + s16(($vbarlarg + ($tex_step_dist - 1) * 0x80) & 0xfffe)) >> 1) & 0xffff;                }                $rtl = $r246 != xread16($render_context - 0x25c);                $tex_frac = 0;                if ($bary < $drawy) {                    $tex_frac = mul32((($drawy - $bary) & 0xff) * 0x100, $slope_step);                }                if (!$rtl) {                    // LTR slope (rc[-0x246] == rc[-0x25c])                    if ($clip_cols != 0 && $max_cols <= $vbarlarg) {                        $tex_frac = concat22((Img::$vbarx + s16($tex_frac >> 16)) - Img::$precx, $tex_frac);                    }                } else {                    // RTL slope                    if ($clip_cols != 0 && $max_cols <= $vbarlarg) {                        $tex_frac = concat22((($max_cols + Img::$precx + s16($tex_frac >> 16)) - Img::$vbarx) - $vbarlarg, $tex_frac);                    }                    $fb = s32($max_cols + Img::$precx + xread32(Img::$atlpix + s16(($drawy - Img::$wlogy1) * 4)) + 1);                }                $col_count = ($tex_frac >> 16) & 0xffff;                $row_start = $fb;                $slope_inc = s16($slope_step) * 0x100;                while (true) {                    while (s16($col_count) <= $max_cols) {                        $top_lines = s16($top_lines - 1);                        if ($top_lines == -1) {                            break;                        }                        // RTL: H starts at 0 each row                        $vm = $height_mask & $Vl;                        if ($rtl) {                            rrq68_tex_row($fb, s16($col_count), true, 0, 0, $vm, $ph, $pl, $width_mask, $tex_base, $dk);                        } else {                            rrq68_tex_row($fb, s16($col_count), false, $hsh, $hl_row, $vm, $ph, $pl, $width_mask, $tex_base, $dk);                        }                        // V = v_step + Vm, carry folded into the low word                        $lo = $vsl + $vm;                        $hi = $vsh + $Vh + ($lo >> 16);                        $Vh = $hi & 0xffff;                        $Vl = $lo & 0xffff;                        if ($hi >> 16) {                            $Vl = ($width_mask + $Vl + 1) & 0xffff;                        }                        $tex_frac = s32($tex_frac + $slope_inc);                        $fb = $row_start + $wlog;                        $col_count = ($tex_frac >> 16) & 0xffff;                        $row_start = $fb;                    }                    if ($top_lines < 0) {                        break;                    }                    $col_count = $max_cols & 0xffff;                }                if ($rtl) {                    $fb += (-1 - $max_cols); // adjust back to left edge for main body                }                $mid_height = Img::$vbarmid;            }        }    }    // Main body: full-width LTR textured scanlines    $mid_lines = s16($mid_height - 1);    if ($mid_lines >= 0) {        $stride_advance = s16($wlog - $max_cols);        $adv = s16($stride_advance - 1);        do {            $vm = $height_mask & $Vl;            $fb = rrq68_tex_row($fb, $max_cols, false, $hsh, $hl_row, $vm, $ph, $pl, $width_mask, $tex_base, $dk);            $lo = $vsl + $vm;            $hi = $vsh + $Vh + ($lo >> 16);            $Vh = $hi & 0xffff;            $Vl = $lo & 0xffff;            if ($hi >> 16) {                $Vl = ($width_mask + $Vl + 1) & 0xffff;            }            $fb += $adv;            $mid_lines = s16($mid_lines - 1);        } while ($mid_lines != -1);    }    // Bottom section: textured narrowing (from tbarland with vbarbot > 0)    $saved_vbarbot = Img::$vbarbot;    if ($saved_vbarbot != 0) {        Img::$vbarbot = 0;        $bot_lines = s16($saved_vbarbot - 1);        if ($bot_lines >= 0) {            bartrab_textured_68k($render_context, $index, $max_cols, $bot_lines + 1, $dark, $persp, concat22($vsh, $vsl), concat22($hsh, $hsl), concat22($Vh, $Vl), $width_mask, $height_mask, $tex_base);        }    }}/** C: static bool clip_bar_y(s16 bary, s16 *barheight, u16 *drawy) — drawy kept as s16 here. */function clip_bar_y(int $bary, int &$barheight, int &$drawy): bool{    $barclipy = s16(($bary - Img::$landclipy1) - Img::$landcliph);    $drawy = $bary;    if (((Img::$landcliph & 0xffff) <= (($bary - Img::$landclipy1) & 0xffff) && $barclipy != 0) || (s16(-$barheight - $barclipy) < 0)) {        if (Img::$clipy2 < $bary) {            return false;        }        $barbot = s16($bary + $barheight - 1);        if ($barbot < Img::$landclipy1) {            return false;        }        if ($bary < Img::$landclipy1) {            $barheight = s16($bary + ($barheight - Img::$landclipy1));            $drawy = Img::$landclipy1;        }        if (Img::$clipy2 < $barbot) {            $barheight = s16(Img::$clipy2 + ($barheight - $barbot));        }    }    return true;}function hittest_bar(int $terrain_cell, int $render_context, int $drawy, int $barheight, int $barwidth, int $packed_coord, int $step_x, int $step_y): void{    if ($drawy <= Img::$ytstpix && Img::$ytstpix < s16($barheight + $drawy) && Img::$precx <= Img::$xtstpix && Img::$xtstpix < s16($barwidth + Img::$precx)) {        $shift = xread16($render_context - 0x3c0) & 0x3f;        Img::$ntstpix = s16(xread16($terrain_cell) >> 8);        Img::$cztstpix = xread16($terrain_cell) & 0xff;        Img::$cxtstpix = s16(((((s16(($packed_coord >> 16) & 0xffff) * 2 - $step_x) << $shift) & 0xffff) >> 1));        Img::$cytstpix = s16(((((s16($packed_coord) * 2 - $step_y) << $shift) & 0xffff) >> 1));        Img::$etstpix = -2; // 0xfffe        Img::$dtstpix = xread32hi16($render_context - 0x2e0);    }    barlands($drawy, $barheight, $barwidth);}function barland_68k(int $terrain_cell, int $render_context, int $step_x, int $step_y, int $bary, int $barheight, int $index, int $barx, int $packed_coord, int $d6): void{    $prev_screen_x = Img::$precx;    // Clip bar vertically    $drawy = 0;    if (!clip_bar_y($bary, $barheight, $drawy)) {        return;    }    if (Img::$clipx1 <= $barx) {        // Compute horizontal span from previous cursor to current bar edge        Img::$vbarlarg = s16(($barx - Img::$precx) - 1);        Img::$vbarx = Img::$precx;        if (Img::$precx < Img::$clipx1) {            Img::$precx = Img::$clipx1;        }        // Clip bar width against right boundary        if (Img::$vbarclipx2 < $barx) {            $barwidth = s16(-(Img::$precx - Img::$vbarclipx2));            if ($barwidth == 0 || 0 < s16(Img::$precx - Img::$vbarclipx2)) {                barlands($drawy, $barheight, $barwidth);                return;            }        } else {            $barwidth = s16(-(Img::$precx - $barx));            if ($barwidth == 0 || 0 < s16(Img::$precx - $barx)) {                Img::$vbarx = $prev_screen_x;                return;            }        }        // Render or hit-test the bar        if (-1 < xread16($render_context - 0x24e)) {            // PORT: enhanced-renderer depth stamping (image.depthrows) dropped.            if (Img::$ftstpix == 0) {                if (s8(xread8($render_context - 0x400)) == 0x0B) {                    bartra_textured_68k($terrain_cell, $render_context, $drawy, $index, $barwidth, $barheight, $bary);                } else {                    bartra_68k($terrain_cell, $render_context, $drawy, $index, $barwidth, $barheight, $bary);                }            } else {                hittest_bar($terrain_cell, $render_context, $drawy, $barheight, $barwidth, $packed_coord, $step_x, $step_y);            }        }    }}function tbarland_68k(int $terrain_cell, int $render_context, int $step_x, int $step_y, int $bary, int $barheight, int $index, int $screen_x, int $packed_coord, int $d6): void{    $prev_screen_x = Img::$precx;    $bary &= 0xffff;    $barheight = s16($barheight);    // Clip bar vertically    $drawy = 0;    if (!clip_bar_y(s16($bary), $barheight, $drawy)) {        return;    }    $barx = s16($screen_x);    if (Img::$clipx1 <= $barx) {        // Compute horizontal span from previous cursor to current bar edge        Img::$vbarlarg = s16(($barx - Img::$precx) - 1);        Img::$vbarx = Img::$precx;        if (Img::$precx < Img::$clipx1) {            Img::$precx = Img::$clipx1;        }        // Clip bar width against right boundary        if (Img::$vbarclipx2 < $barx) {            $barwidth = s16(-(Img::$precx - Img::$vbarclipx2));            if ($barwidth == 0 || 0 < s16(Img::$precx - Img::$vbarclipx2)) {                Img::$vbarx = $prev_screen_x;                return;            }        } else {            $barwidth = s16(-(Img::$precx - $barx));            if ($barwidth == 0 || 0 < s16(Img::$precx - $barx)) {                Img::$vbarx = $prev_screen_x;                return;            }        }        if (-1 < xread16($render_context - 0x24e)) {            // Clamp bottom high-water mark            $r246 = xread16($render_context - 0x246);            if (s16(V::$bothigh) < $r246) {   // signed, as the asm cmp.w                V::$bothigh = $r246 & 0xffff;            }            Img::$vbarbot = 0;            if (V::$fbottom != 0) {                // Bar entirely below bottom clip — bottom-only rendering                if (s16(V::$bothigh) <= $drawy) {                    if (s8(xread8($render_context - 0x400)) == 0x0B) {                        // Textured bottom-only path (Ghidra tbarland CD lines 24014-24027)                        $barwidth = s16($barwidth - 1);                        $dark = rrq68_tex_dark($terrain_cell, $render_context);                        [$wmask, $vsh, $vsl, $hsh, $hsl, $Vh, $Vl, $hmask, $tbase] = rrq68_tex_setup($render_context, $index, $barwidth, s16($drawy - s16($bary)));                        bartrab_textured_68k($render_context, $index, $barwidth, $barheight, $dark, Img::$tex_persp_step, concat22($vsh, $vsl), concat22($hsh, $hsl), concat22($Vh, $Vl), $wmask, $hmask, $tbase);                    } else {                        bartrab_68k($terrain_cell, $render_context, $drawy, $index, $barwidth, $barheight);                    }                    return;                }                // Clip bar height against bottom boundary                $clip_calc = s16(($barheight + $drawy) - V::$bothigh);                if ($clip_calc != 0 && s16(V::$bothigh) <= s16($barheight + $drawy)) {                    Img::$vbarbot = $clip_calc;                }            }            // PORT: enhanced-renderer depth stamping (image.depthrows) dropped.            // Render or hit-test the bar            if (Img::$ftstpix == 0) {                if (s8(xread8($render_context - 0x400)) == 0x0B) {                    bartra_textured_68k($terrain_cell, $render_context, $drawy, $index, $barwidth, s16($barheight - Img::$vbarbot), $bary);                } else {                    bartra_68k($terrain_cell, $render_context, $drawy, $index, $barwidth, s16($barheight - Img::$vbarbot), $bary);                }            } else {                hittest_bar($terrain_cell, $render_context, $drawy, $barheight, $barwidth, $packed_coord, $step_x, s16($step_y));            }        }    }}// Occlusion prescan (orig dolanc @0x22862): walk grid cells between the previous and current// screen column tracking max occlusion height; writes rc-0x25e/-0x260/-0x280 and `adresa`.function doland_prescan(int $render_context, int $terrain_grid, int $alt_table, int $col_step_x, int $col_step_y, int $bar_screen_x, int &$scan_xp, int &$scan_yp, int &$prev_max_yp): void{    $scan_x = $scan_xp;    $scan_y = $scan_yp;    $max_y = xread16($render_context - 0x260);    $prev_max_y = xread16($render_context - 0x25e);    if ($prev_max_y < $max_y) {        $prev_max_y = $max_y;    }    $col_target_x = xread32hi16($render_context - 0x280);    $rc_294 = xread16($render_context - 0x294);   // grid X-bound    $rc_292 = xread16($render_context - 0x292);   // grid Y-bound    $u294 = $rc_294 & 0xffff;    $u292 = $rc_292 & 0xffff;    $rc_3fe = xread8($render_context - 0x3fe);    // wrap-enable flag    while ($col_target_x < $bar_screen_x) {        xwrite16($render_context - 0x25e, $max_y);        $prescan_height = 0;        // Advance scan position by one column step (with carry)        $scan_x = rrq_addx($col_step_x, $scan_x);        $next_scan_x = $scan_x & 0xffff;        $scan_y = rrq_addx($col_step_y, $scan_y);        $next_scan_y = $scan_y & 0xffff;        xwrite32($render_context - 0x280, s32(xread32($render_context - 0x27c) + xread32($render_context - 0x280)));        if ($u294 < $next_scan_x) {            if ($rc_3fe == 1) {                $wrap_x = 0;                if ($rc_294 <= s16($next_scan_x)) {                    $wrap_x = $rc_294 * 2;                }                $strip_ptr = xread32($terrain_grid + s16($wrap_x - $next_scan_x) * 4);                if ($next_scan_y <= $u292) {                    if ($rc_292 <= s16($next_scan_y)) {                        $strip_ptr = s32($strip_ptr + ($rc_292 << 2));                    }                    V::$adresa = s32(($strip_ptr - s16($next_scan_y)) - s16($next_scan_y));                } else {                    V::$adresa = s32($strip_ptr + s16($next_scan_y) + s16($next_scan_y));                }                $prescan_height = xread16(V::$adresa) & 0xff;            }        } else {            // X in bounds: as in rrq-falcon.asm, only the Y-out-of-bounds case is wrap-gated;            // an in-bounds cell is always sampled.            if ($u292 < $next_scan_y) {                // Y out of bounds — only real terrain here when wrapping is enabled.                if ($rc_3fe == 1) {                    $strip_ptr = xread32($terrain_grid + s16($next_scan_x * 4));                    if ($rc_292 <= s16($next_scan_y)) {                        $strip_ptr = s32($strip_ptr + ($rc_292 << 2));                    }                    V::$adresa = s32(($strip_ptr - s16($next_scan_y)) - s16($next_scan_y));                    $prescan_height = xread16(V::$adresa) & 0xff;                }                // wrap off: no terrain out here — prescan_height stays 0            } else {                // Y in bounds — always sample the real terrain height (no wrap gate).                $strip_ptr = xread32($terrain_grid + s16($next_scan_x * 4));                V::$adresa = s32($strip_ptr + s16($next_scan_y) + s16($next_scan_y));                $prescan_height = xread16(V::$adresa) & 0xff;            }        }        // Convert height to screen Y and track maximum        $max_y = s16(xread32lo16($render_context - 0x270) + xread16($alt_table + s16(xread16($render_context - 0x25a) + $prescan_height * 2)));        xwrite16($render_context - 0x260, $max_y);        if ($prev_max_y < $max_y) {            $prev_max_y = $max_y;        }        $col_target_x = xread32hi16($render_context - 0x280);    }    $scan_xp = $scan_x;    $scan_yp = $scan_y;    $prev_max_yp = $prev_max_y;}// 68k word-swapped 16.16 step, as the original add.l/addx.w (sub.l/subx.w): the carry folds// back into the integer word, so at fraction boundaries the net step can be zero.// divs.w: 32/16 signed, quotient in the low word; on overflow the 68k leaves the dividend.function divs_w(int $n, int $d): int{    if ($d == 0) {        return s16($n);   // a zero-divide trap in the original    }    $q = cdiv($n, $d);    return ($q < -32768 || $q > 32767) ? s16($n) : $q;}function swapstep_add(int $pos_raw, int $step_swp): int{    $p = rrq_swap32($pos_raw);    $r = s32($p + $step_swp);    $lo = ($r + carry4($p, $step_swp)) & 0xffff;        /* addx.w: carry out of add.l */    return concat22($lo, $r >> 16);}function swapstep_sub(int $pos_raw, int $step_swp): int{    $p = rrq_swap32($pos_raw);    $r = s32($p - $step_swp);    $lo = ($r - (ult($p, $step_swp) ? 1 : 0)) & 0xffff; /* subx.w: borrow out of sub.l */    return concat22($lo, $r >> 16);}function doland_68k(int $scene_addr, int $render_context): void{    $rc = $render_context;    // =========================================================================    // INITIALIZATION    // =========================================================================    // --- Sprite depth sorting setup ---    Img::$spritprof = 0x8000;    $sprite = sprite_var(xread16($scene_addr + 0x2) & 0xffff);    Img::$spritnext = $sprite->link;    if (Img::$spritnext != 0) {        Img::$spritprof = sprite_var(Img::$spritnext)->newd;    }    // --- Render sky background ---    if (xread16($scene_addr + 0xa2) != 0) {        skytofen($scene_addr, $rc);    }    // --- Terrain data pointers ---    $alt_table = Img::$atalti;    $terrain_grid = Img::$atlland;    // --- Reset overhang interpolation state ---    // prec = precedent (previous), top/bot = overhang top/bottom screen Y,    // a/b/c = current/prev/prev-prev column, i = interpolated.    V::$fprectop = 0;    V::$fprectopa = 0;    V::$adresa = 0;    V::$prectopa = 1000;    V::$prectopb = 1000;    V::$prectopc = 1000;    V::$precbota = 1000;    V::$precbotb = 1000;    V::$precbotc = 1000;    // --- Initialize rendering accumulators ---    xwrite32($rc - 0x25a, 0);    xwrite32($rc - 0x27c, 0);    $cam_grid_x = xread16($rc - 0x3a2) & 0xffff;    $cam_grid_y = xread16($rc - 0x3a0) & 0xffff;    xwrite32($rc - 0x2a8, shl32($cam_grid_x, 0x10));    xwrite32($rc - 0x2a4, shl32($cam_grid_y, 0x10));    // Column step direction in terrain grid (16.16 fixed-point)    // Word-swapped form used for 68k ADDX carry-chain emulation    $col_step_x_raw = xread32($rc - 0x2c4);    $col_step_x = rrq_swap32($col_step_x_raw);    $col_step_y_raw = xread32($rc - 0x2c0);    $col_step_y = rrq_swap32($col_step_y_raw);    // Initial screen Y projection    xwrite32($rc - 0x270, (xread16($rc - 0x26a) + xread16($rc - 0x352)) & 0xffff);    // --- Initial perspective projection ---    $cell_shift = xread16($rc - 0x3c0) & 0x3f;    $proj_x = s16(($cam_grid_x << $cell_shift) - xread16($rc - 0x360));    $proj_y = s16(($cam_grid_y << $cell_shift) - xread16($rc - 0x35e));    glandtopix($rc, $proj_x, $proj_y,        s16((xread16($rc - 0x378) + $proj_x) - xread16($rc - 0x37e)),        s16((xread16($rc - 0x376) + $proj_y) - xread16($rc - 0x37c)),        xread32hi16($rc - 0x2e0));    xwrite32($rc - 0x280, shl32((xread16($rc - 0x26c) + $proj_x) & 0xffff, 0x10));    // First row: << 8 / div << 8 (the per-row step below is << 6 / div << 10).    xwrite32($rc - 0x27c, divs_w(s16($proj_y - $proj_x) * 256, xread16($rc - 0x3a4)) * 256);    // Current traversal position in terrain grid (16.16 fixed-point)    $trav_x = shl32($cam_grid_x, 16);    $trav_y = shl32($cam_grid_y, 16);    $col_dir_x = s16($col_step_x_raw >> 0x10);    $col_dir_y = s16($col_step_y_raw >> 0x10);    // =========================================================================    // OUTER LOOP: Process terrain rows from far to near    // =========================================================================    do {        // Falcon CD: compute horizontal texture base from current traversal position        // Ghidra line 25063: DAT_0001fe82 = (trav_y_frac + trav_x_frac + carry) * 0x10        Img::$tex_hbase = s16((s16($trav_y) + s16($trav_x) + carry4($trav_y, $trav_x)) * 0x10);        // ----- Advance row position accumulators -----        xwrite32($rc - 0x2a8, s32(xread32($rc - 0x2bc) + xread32($rc - 0x2a8)));        xwrite32($rc - 0x2a4, s32(xread32($rc - 0x2b8) + xread32($rc - 0x2a4)));        $row_start_x = $trav_x;        $row_start_y = s32($trav_y + shl32(xread32hi16($rc - 0x29c), 16));        // ----- Snap column position to terrain grid alignment -----        // The column step is ±1 in either X or Y axis.        // Safety bound: if s16 wrap-around causes divergence, break after 512 iterations.        if ($col_step_x_raw == 0x00010000) {            if (xread32hi16($rc - 0x2bc) < 0) {                for ($_snap = 512; $_snap > 0 && xread32hi16($rc - 0x2a8) < s16($row_start_x >> 16); $_snap--) {                    $row_start_y = swapstep_sub($row_start_y, $col_step_y);                    $row_start_x = swapstep_sub($row_start_x, $col_step_x);                }            } else {                for ($_snap = 512; $_snap > 0 && s16($row_start_x >> 16) < xread32hi16($rc - 0x2a8); $_snap--) {                    $row_start_y = swapstep_add($row_start_y, $col_step_y);                    $row_start_x = swapstep_add($row_start_x, $col_step_x);                }            }        } elseif ($col_step_x_raw == -0x10000) { // 0xffff0000            if (xread32hi16($rc - 0x2bc) < 0) {                for ($_snap = 512; $_snap > 0 && xread32hi16($rc - 0x2a8) < s16($row_start_x >> 16); $_snap--) {                    $row_start_y = swapstep_add($row_start_y, $col_step_y);                    $row_start_x = swapstep_add($row_start_x, $col_step_x);                }            } else {                for ($_snap = 512; $_snap > 0 && s16($row_start_x >> 16) < xread32hi16($rc - 0x2a8); $_snap--) {                    $row_start_y = swapstep_sub($row_start_y, $col_step_y);                    $row_start_x = swapstep_sub($row_start_x, $col_step_x);                }            }        } else {            // Column step is primarily in Y direction            $row_start_y = $trav_y;            $row_start_x = s32($trav_x + shl32(xread32hi16($rc - 0x2a0), 16));            if ($col_step_y_raw == 0x00010000) {                if (xread32hi16($rc - 0x2b8) < 0) {                    for ($_snap = 512; $_snap > 0 && xread32hi16($rc - 0x2a4) < s16($row_start_y >> 16); $_snap--) {                        $row_start_y = swapstep_sub($row_start_y, $col_step_y);                        $row_start_x = swapstep_sub($row_start_x, $col_step_x);                    }                } else {                    for ($_snap = 512; $_snap > 0 && s16($row_start_y >> 16) < xread32hi16($rc - 0x2a4); $_snap--) {                        $row_start_y = swapstep_add($row_start_y, $col_step_y);                        $row_start_x = swapstep_add($row_start_x, $col_step_x);                    }                }            } else {                if (xread32hi16($rc - 0x2b8) < 0) {                    for ($_snap = 512; $_snap > 0 && xread32hi16($rc - 0x2a4) < s16($row_start_y >> 16); $_snap--) {                        $row_start_y = swapstep_add($row_start_y, $col_step_y);                        $row_start_x = swapstep_add($row_start_x, $col_step_x);                    }                } else {                    for ($_snap = 512; $_snap > 0 && s16($row_start_y >> 16) < xread32hi16($rc - 0x2a4); $_snap--) {                        $row_start_y = swapstep_sub($row_start_y, $col_step_y);                        $row_start_x = swapstep_sub($row_start_x, $col_step_x);                    }                }            }        }        // ----- Update fog/distance shading -----        xwrite32($rc - 0x256, s32(xread32($rc - 0x252) + xread32($rc - 0x256)));        Img::$vdarkw = s16(((xread32($rc - 0x256) >> 16) & 0xff) << 8);        // ----- Update depth (distance from camera) -----        $depth = s32(xread32($rc - 0x2dc) + xread32($rc - 0x2e0));        xwrite32($rc - 0x2e0, $depth);        if ($depth < 0) {            xwrite32($rc - 0x2e0, s32(xread32($rc - 0x2e0) - xread32($rc - 0x2dc)));        }        // ----- Update vertical height projection -----        xwrite32($rc - 0x2d8, s32(xread32($rc - 0x2d4) + xread32($rc - 0x2d8)));        // Prepare column rendering: word-swap for 68k fixed-point carry emulation        // Save pre-glandtopix perspective value for Falcon CD texture step computation        // (glandtopix overwrites rc-0x27c; Ghidra line 25156 saves before call)        $saved_persp_27c = xread32($rc - 0x27c);        $col_x_step_swp = rrq_swap32($saved_persp_27c);        $col_x_pos_swp = rrq_swap32(xread32($rc - 0x280));        xwrite32($rc - 0x278, xread32($rc - 0x270));        // ----- Recalculate perspective if row is in front of camera -----        if (-1 < xread16($rc - 0x24e)) {            $shift = xread16($rc - 0x3c0) & 0x3f;            $proj_x = s16((s16($row_start_x >> 16) << $shift) - xread16($rc - 0x360));            $proj_y = s16((s16($row_start_y >> 16) << $shift) - xread16($rc - 0x35e));            glandtopix($rc, $proj_x, $proj_y,                s16((xread16($rc - 0x378) + $proj_x) - xread16($rc - 0x37e)),                s16((xread16($rc - 0x376) + $proj_y) - xread16($rc - 0x37c)),                xread32hi16($rc - 0x2e0));            xwrite32($rc - 0x280, shl32((xread16($rc - 0x26c) + $proj_x) & 0xffff, 0x10));            xwrite32($rc - 0x27c, divs_w(s16($proj_y - $proj_x) * 64, xread16($rc - 0x3a4)) * 1024);            $x3a8 = xread16($rc - 0x3a8);            $hi2e0 = xread32hi16($rc - 0x2e0);            $proj_denom = ($x3a8 + $hi2e0) & 0xffff;            if ($proj_denom == 0 || scarry2($x3a8, $hi2e0) != (($proj_denom & 0x8000) ? 1 : 0)) {                $proj_denom = 1;            }            xwrite32($rc - 0x270, (xread16($rc - 0x26a) + divs_w(xread32($rc - 0x2d8), s16($proj_denom))) & 0xffff);        }        // ----- Render sprites at this depth layer -----        if (xread32hi16($rc - 0x2e0) <= Img::$spritprof) {            spritaff(xread32hi16($rc - 0x2e0));        }        // ----- Select altitude table segment for current distance -----        $alt_table = s32($alt_table + xread16($rc - 0x25a));        $alt_seg_idx = divs_w(s32(xread32($rc - 0x2e0) - xread32($rc - 0x2c8)) >> 8, xread16($rc - 0x262)) & 0xffff;        $max_seg = 0x31;        if (xread16($rc - 0x24e) == 1) {            $alt_seg_idx = $max_seg;        }        if ($max_seg < $alt_seg_idx) {            if (s16($alt_seg_idx) < 0) {                $alt_seg_idx = 0;            } else {                $alt_seg_idx = $max_seg;            }        }        xwrite16($rc - 0x25a, (s16(Img::$atalti) + $alt_seg_idx * 0x200) - s16($alt_table));        // Set strip depth for enhanced renderer fog: 0=near, 255=far        Img::$stripdepth = (255 - \intdiv($alt_seg_idx * 255, $max_seg)) & 0xff;        // Falcon CD: compute texture perspective step per altitude segment        // Ghidra lines 25199-25207: DAT_0001fe7a = 0x200000 / perspective, ROL-encoded        // Uses saved_persp_27c (pre-glandtopix value), NOT current rc-0x27c which was overwritten        $divisor = ($saved_persp_27c >> 8) & 0xffff;        if ($divisor > 0) {            $quot = \intdiv(0x200000, $divisor) & 0xffff;            Img::$tex_persp_step = shl32($quot & 0xff, 24) | (($quot >> 8) & 0xff);            if (0x200000 % $divisor != 0) {                Img::$tex_persp_step = shl32($quot & 0xff, 24) | ((($quot >> 8) & 0xff) + 1);            }        } else {            Img::$tex_persp_step = 0;        }        // ----- Reset per-row rendering state -----        Img::$precx = s16(Img::$clipx1 - 0x10);        xwrite16($rc - 0x260, Img::$clipy2);        xwrite16($rc - 0x25c, Img::$clipy2);        V::$fprectop = 0;        V::$fprectopa = 0;        // ----- Decrement row counter -----        $rows_remaining = (xread16($rc - 0x24e) - 1) & 0xffff;        xwrite16($rc - 0x24e, $rows_remaining);        if (($rows_remaining & 0x8000) && (xread16($rc - 0x24e) < -3)) {            spritaff(-1);            return;        }        // =====================================================================        // INNER LOOP: Process columns left-to-right within this row        // =====================================================================        $saved_col_x = xread32($rc - 0x280);        $scan_x = rrq_swap32($row_start_x);        $scan_y = rrq_swap32($row_start_y);        do {            $bar_screen_x = s16($col_x_pos_swp);            // Hoisted terrain-map constants — also consumed by the main cell lookup below.            $rc_294 = xread16($rc - 0x294);   // grid X-bound            $rc_292 = xread16($rc - 0x292);   // grid Y-bound            $rc_3fe = xread8($rc - 0x3fe);    // wrap-enable flag            $prev_max_y = 0;            doland_prescan($rc, $terrain_grid, $alt_table, $col_step_x, $col_step_y, $bar_screen_x, $scan_x, $scan_y, $prev_max_y);            // =================================================================            // Main terrain lookup at center column position            // =================================================================            $center_grid_x = ($trav_x >> 16) & 0xffff;            $center_grid_y = ($trav_y >> 16) & 0xffff;            $sgy = s16($center_grid_y);            $strip_ptr_main = 0;            $grid_h = 0;            // C gotos cell_calc_subtract / cell_calc_add: 0 = no cell (falls through to advance_column)            $mode = 0;            if (($rc_294 & 0xffff) < $center_grid_x) {                if ($rc_3fe == 1) {                    $wrap_x = 0;                    if ($rc_294 <= s16($center_grid_x)) {                        $wrap_x = $rc_294 * 2;                    }                    $strip_ptr_main = xread32($terrain_grid + s16(($wrap_x - $center_grid_x) * 4));                    $grid_h = $rc_292 & 0xffff;                    $mode = ($center_grid_y <= $grid_h) ? 1 : 2;                }            } elseif (($rc_292 & 0xffff) < $center_grid_y) {                if ($rc_3fe != 1) {                    goto advance_column;                }                $strip_ptr_main = xread32($terrain_grid + s16($center_grid_x << 2));                $grid_h = $rc_292 & 0xffff;                $mode = 1;            } else {                $strip_ptr_main = xread32($terrain_grid + s16($center_grid_x << 2));                $mode = 2;            }            if ($mode != 0) {                if ($mode == 1) {                    // cell_calc_subtract:                    if (s16($grid_h) <= $sgy) {                        $strip_ptr_main = s32($strip_ptr_main + ($rc_292 << 2));                    }                    $terrain_cell = s32(($strip_ptr_main - $sgy) - $sgy);                } else {                    // cell_calc_add:                    $terrain_cell = s32($strip_ptr_main + $sgy + $sgy);                }                // --- Read terrain cell and compute ground screen Y ---                $ground_clip_y = xread16($rc - 0x25c);                $cell_data = xread16($terrain_cell) & 0xffff;                Img::$solh = $cell_data & 0xff;                Img::$solpixy = s16(xread32lo16($rc - 0x278) + xread16($alt_table + s16(Img::$solh * 2)));                xwrite16($rc - 0x246, Img::$solpixy);                xwrite16($rc - 0x25c, Img::$solpixy);                if (Img::$solpixy < $ground_clip_y) {                    xwrite16($rc - 0x246, $ground_clip_y);                    $ground_clip_y = Img::$solpixy;                }                // Extract terrain type index: bits [13:8] encode type, shifted for table lookup                $terrain_type_idx = s16((($cell_data & 0x3f00) >> 3) - 0xc00);                $bar_height = ($prev_max_y - $ground_clip_y) & 0xffff;                // --- Render ground terrain bar ---                if ($bar_height != 0 && sborrow2($prev_max_y, $ground_clip_y) == (($bar_height & 0x8000) ? 1 : 0)) {                    $packed_pos = rrq_swap32($trav_x);                    barland_68k($terrain_cell, $rc, $col_dir_x, $col_dir_y, $ground_clip_y, s16($bar_height), $terrain_type_idx, $bar_screen_x, $packed_pos, $scan_x);                }                // =============================================================                // Check for terrain features: overhangs or billboard sprites                // =============================================================                // Feature flag at rc[0x14 + type]:                //   < 0 (bit 7 set): overhang/bridge                //   == 1: billboard sprite                //   == 0: no feature                $feature = s8(xread8($rc + $terrain_type_idx + 0x14));                if ($feature != 0) {                    if ($feature < 0) {                        // =====================================================                        // OVERHANG / BRIDGE RENDERING                        // =====================================================                        // dotop (orig 0x22ce8), transliterated from the asm: continuity with the                        // previous column's overhang (adresa), then this cell's overhang bar.                        $d0 = 0;                        $d1 = 0;                        $d2 = 0;                        $d6 = s16(V::$prectopa);                        $d7 = s16(V::$precbota);                        V::$notopa = 1;                        if (V::$adresa != 0) {                            $w = xread16(V::$adresa) & 0xffff;                            V::$solha = $w & 0xff;                            $mm = s16((($w & 0x3f00) >> 3) - 0xc00);                            $open = s8(xread8($rc + 0x14 + $mm)) < 0;                            if ($open) {                                $d2 = xread16($rc + 0x16 + $mm);                                $d0 = xread8(s32(V::$adresa + xread32($rc + 0x10 + $mm) + 1));                                $d1 = s16($d0 - V::$solha);                                $open = false;                                if ($d1 > 0) {                                    $d2 = s16($d2 - $d1);                                    $open = $d2 > 0;                                }                            }                            if (!$open) {                                V::$fprectopa = 0;   // dotopa7                            } else {                                $d2 = s16($d2 + V::$solha);                                $d1 = $d0;                                if ($d2 < $d0) {                                    $d1 = s16($d2 - V::$solha);                                }                                $d1 = s16($d0 - $d1);                                $seg = xread16($rc - 0x25a);                                $base = xread32lo16($rc - 0x270);                                $d0 = s16(xread16($alt_table + s16($d0 * 2 + $seg)) + $base);                                V::$prectopa = $d0 & 0xffff;                                $d1 = s16(xread16($alt_table + s16($d1 * 2 + $seg)) + $base);                                V::$precbota = $d1 & 0xffff;                                V::$adresa = 0;                                if (!V::$fprectopa) {                                    V::$fprectopa = 1;                                    V::$prectopc = 5000;                                    V::$precbotc = (-5000) & 0xffff;                                } else {                                    V::$prectopb = V::$prectopc;                                    V::$precbotb = V::$precbotc;                                    if ($d0 > $d6) {                                        $d6 = $d0;                                    }                                    V::$prectopc = $d6 & 0xffff;                                    if ($d6 < s16(V::$prectopb)) {                                        $d6 = s16(V::$prectopb);                                    }                                    if ($d1 < $d7) {                                        $d7 = $d1;                                    }                                    V::$precbotc = $d7 & 0xffff;                                    if ($d7 > s16(V::$precbotb)) {                                        $d7 = s16(V::$precbotb);                                    }                                    if ($d6 < $d7) {                                        V::$notopa = 0;                                    }                                }                            }                        }                        // dotopa5: this cell's overhang layer                        $terrain_cell = s32(xread32($rc + 0x10 + $terrain_type_idx) + $sgy * 2 + xread32($terrain_grid + s16($center_grid_x << 2)));                        $d2 = xread16($rc + 0x16 + $terrain_type_idx);                        V::$fbottom = 0;                        $oh_cell = xread16($terrain_cell) & 0xffff;                        Img::$toph = $oh_cell & 0xff;                        $oh_type_idx = s16((($oh_cell & 0x3f00) >> 3) - 0xc00);                        $d0 = Img::$toph;                        $d1 = s16($d0 - Img::$solh);                        if ($d1 <= 0) {                            goto advance_column;   // dotop6: no overhang, fprectop unchanged                        }                        $d2 = s16($d2 - $d1);                        if ($d2 <= 0) {                            goto advance_column;                        }                        $d2 = s16($d2 + Img::$solh);                        if ($d2 < $d0) {                            $d1 = s16($d2 - Img::$solh);                            V::$fbottom = 1;                        }                        $d1 = s16($d0 - $d1);                        $row_base = xread32lo16($rc - 0x278);                        $d0 = s16(xread16($alt_table + s16($d0 * 2)) + $row_base);                        Img::$toppixy = $d0;                        $old246 = xread16($rc - 0x246);                        xwrite16($rc - 0x246, $d0);                        $old_top = s16(V::$prectopi);                        V::$prectopi = $d0 & 0xffff;                        if ($old_top > $d0) {                            xwrite16($rc - 0x246, $old_top);   // -0x246 = max, d0 = min                        } else {                            $d0 = $old_top;                        }                        $d1 = V::$fbottom ? s16(xread16($alt_table + s16($d1 * 2)) + $row_base) : $old246;                        V::$bothigh = $d1 & 0xffff;                        $old_bot = s16(V::$precboti);                        V::$precboti = $d1 & 0xffff;                        if ($old_bot < $d1) {                            V::$bothigh = $old_bot & 0xffff;               // bothigh = min, botalt = max                        } else {                            $d1 = $old_bot;                        }                        V::$botalt = $d1 & 0xffff;                        if (V::$fprectop) {                            $d1 = s16($d1 - $d0);                            if ($d1 > 0) {                                $draw = true;                                if (!V::$notopa) {                                    // dotop30/31: clip against the previous column's overhang span                                    $d6 = s16($d6 - $d0);                                    if ($d6 > 0) {                                        $d7 = s16($d7 - $d0);                                        if ($d1 <= $d7) {                                            V::$fbottom = 0;                                            $d1 = $d6;                                        }                                    } else {                                        $d1 = s16($d1 + $d0);                                        $d0 = $d7;                                        $d1 = s16($d1 - $d7);                                        $draw = $d1 > 0;                                    }                                }                                if ($draw) {                                    $saved_clip = xread16($rc - 0x25c);                                    xwrite16($rc - 0x25c, V::$prectopi);                                    $packed_pos = rrq_swap32($trav_x);                                    tbarland_68k($terrain_cell, $rc, $col_dir_x, $col_step_y, $d0 & 0xffff, $d1, $oh_type_idx, $col_x_pos_swp, $packed_pos, 0);                                    xwrite16($rc - 0x25c, $saved_clip);                                }                            }                        }                        V::$fprectop = 1;   // dotop4                        goto advance_column;                    }                    if ($feature != 1) {                        goto advance_column;                    }                    // --- Billboard sprite at this terrain cell ---                    barsprite($rc, $terrain_type_idx, s16($center_grid_x), $sgy, 0);                }                V::$fprectop = 0;                V::$fprectopa = 0;            }            advance_column:            // Falcon CD: accumulate horizontal texture base per column (Ghidra line 25460)            Img::$tex_hbase = s16(Img::$tex_hbase + 0x20);            Img::$precx = $bar_screen_x;            if (Img::$landclipx2 < $bar_screen_x) {                break;            }            // Step to next column. Orig dobar3 @0x22a48: `add.l a4,d4 / addx.w d0,d4` on the            // word-swapped position; same swapped carry as the snap and prescan.            $trav_x = swapstep_add($trav_x, $col_step_x);            $trav_y = swapstep_add($trav_y, $col_step_y);            // Advance screen column X (fixed-point with carry emulation)            $col_x_pos_swp = rrq_addx($col_x_step_swp, $col_x_pos_swp);        } while (true);        // Restore row state for next iteration        xwrite32($rc - 0x280, $saved_col_x);        $trav_x = $row_start_x;        $trav_y = $row_start_y;    } while (true);}function landtofi_68k(int $unused, int $scene_id): void{    $scene_addr = s32(Alis::$basemain + s16($scene_id));    // m68k path: original behavior with fdoland gate    $save_cliph = Img::$cliph;    $save_clipl = Img::$clipl;    $save_clipy2 = Img::$clipy2;    $save_clipx2 = Img::$clipx2;    $save_clipy1 = Img::$clipy1;    $save_clipx1 = Img::$clipx1;    Img::$wloglarg = s16(xread16($scene_addr + 0x12) + 1);    Img::$wlogic = xread32(xread32($scene_addr + 0x30));    Img::$wlogx1 = xread16($scene_addr + 0xe);    Img::$wlogy1 = xread16($scene_addr + 0x10);    Img::$mapscreen = $scene_addr;    if (Img::$fdoland != 0) {        Img::$landclipx2 = s16(xread16($scene_addr + 0x12) + xread16($scene_addr + 0xe));        // Don't widen clipx2 to the scene edge: only the CD build stores it (0x1ea26); ST        // (0x1c918) and Falcon-floppy (0x1f460) keep partial re-renders bounded to the dirty        // window. We follow ST/floppy.        Img::$landcliph = s16((Img::$clipy2 - Img::$clipy1) + 1);        Img::$clipl = s16((Img::$clipx2 - Img::$clipx1) + 1);        Img::$vbarclipx2 = s16(Img::$clipx2 + 1);        Img::$landclipy1 = Img::$clipy1;        $render_context = s32(xread16($scene_addr + 0x42) + xread32(Alis::$atent + xread16($scene_addr + 0x40)));        Img::$cliph = Img::$landcliph;        calctoy($scene_addr, $render_context);        spritland($scene_addr, xread16($scene_addr + 2) & 0xffff);        calclan0_68k($scene_addr, $render_context);        if (((xread8($scene_addr + 1) & 0x40) == 0) && (xread16($scene_addr + 0xa2) == 0)) {            clrvga_68k();        }        /* via doland fn-ptr so the asm renderer is used here too */        doland($scene_addr, $render_context);        Img::$fdoland = 0;        Img::$landone = 1;    }    Img::$clipx1 = $save_clipx1;    Img::$clipy1 = $save_clipy1;    Img::$clipx2 = $save_clipx2;    Img::$clipy2 = $save_clipy2;    Img::$clipl = $save_clipl;    Img::$cliph = $save_cliph;    // PORT: enhanced mode (image.emode -> vgatobuf()) dropped like in the rest of the port.    vgatofen();    Img::$wlogic = Img::$logic;    Img::$wlogx1 = Img::$logx1;    Img::$wlogy1 = Img::$logy1;    Img::$wlogx2 = Img::$logx2;    Img::$wlogy2 = Img::$logy2;    Img::$wloglarg = Img::$loglarg;}