Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

NumBlocks CE: all the inner loops run from the archive

Mason363818c84b7b036
diff --git a/games/numblocks/ce/flash/rsf.s b/games/numblocks/ce/flash/rsf.s
index 5eb1dba6..0b58748f 100644
--- a/games/numblocks/ce/flash/rsf.s
+++ b/games/numblocks/ce/flash/rsf.s
@@ -1,110 +1,24 @@
-; The 3D view's busiest loops again, to run from the archive (appended to NBCETEX): on the CE, code read from the
-; flash's cache costs 2 or 3 cycles a byte, from RAM 4. Each at a fixed offset (rs.h: RSF_*), without an absolute
-; address of its own (relative jumps only, what it needs handed in registers): they run wherever the appvar is.
+; The archive's copy of the 3D view's inner loops (src/rsb.inc), after NBCETEX's tables: on the CE, code read through
+; the flash's cache costs 2 or 3 cycles a byte, from RAM 4. Built after the program, its RAM addresses from the
+; program's map (rsf_syms.inc, tools/rsfsyms.py); the program runs it when its build matches (rs_flash_init).
.assume adl=1
- .section .text
-
-; ------------------------------------------------------------------------------------------------- 0: span_plain
-; (rs.s's span sets up: de dst, hl the texture page, bc the shade row; other set: hl u, de du, bc dv; ix v;
-; iy the end's low byte; ix pushed)
-f_span_plain:
- ld a, (de)
- or a, a
- jr nz, .Lp_skip
- exx
- ld a, h
- exx
- ld l, a
- ld a, ixh
- and a, 0xF0
- or a, l
- ld l, a
- ld c, (hl)
- ld a, (bc)
- ld (de), a
-.Lp_skip:
- inc de
- exx
- add hl, de
- add ix, bc
- exx
- ld a, e
- cp a, (iy)
- jr nz, f_span_plain
- pop ix
- ret
-
-; ------------------------------------------------------------------------------------------------- 64: span_alpha
- .org 64
-f_span_alpha:
- ld a, (de)
- or a, a
- jr nz, .La_skip
- exx
- ld a, h
- exx
- ld l, a
- ld a, ixh
- and a, 0xF0
- or a, l
- ld l, a
- ld a, (hl)
- or a, a
- jr z, .La_skip
- ld c, a
- ld a, (bc)
- ld (de), a
-.La_skip:
- inc de
- exx
- add hl, de
- add ix, bc
- exx
- ld a, e
- cp a, (iy)
- jr nz, f_span_alpha
- pop ix
- ret
+ .include "src/rsb_list.inc"
+ .include "rsf_syms.inc"
-; ------------------------------------------------------------------------------------------------- 128: double
-; b pixels from hl doubled to de (0: c, the row's sky), hl and de after them
- .org 128
-f_double:
- ld a, (hl)
- inc hl
- or a, a
- jr nz, .Ld_set
- ld a, c
-.Ld_set:
- ld (de), a
- inc de
- ld (de), a
- inc de
- ld a, (hl)
- inc hl
- or a, a
- jr nz, .Ld_set2
- ld a, c
-.Ld_set2:
- ld (de), a
- inc de
- ld (de), a
- inc de
- djnz f_double
- ret
-
-; ------------------------------------------------------------------------------------------------- 192: overlay
-; b pixels from hl over de where not 0 (the HUD's layer), hl and de after them
- .org 192
-f_overlay:
- ld a, (hl)
- inc hl
- or a, a
- jr z, .Lo_skip
- ld (de), a
-.Lo_skip:
- inc de
- djnz f_overlay
- ret
+ .section .text
+rsf_start:
+ .ascii "NBRS"
+ .d24 RSF_BUILD
+ .set nroutines, 0
+ .macro rsf_count n
+ .set nroutines, nroutines + 1
+ .endm
+ rsb_each rsf_count
+ .db nroutines
+ ; each routine's offset, in the trampolines' order
+ .macro rsf_off n
+ .d24 b_\n - rsf_start
+ .endm
+ rsb_each rsf_off
- .org 224
+ .include "src/rsb.inc"
diff --git a/games/numblocks/ce/makefile b/games/numblocks/ce/makefile
index db54c671..f3a2ac9b 100644
--- a/games/numblocks/ce/makefile
+++ b/games/numblocks/ce/makefile
@@ -12,16 +12,22 @@ override TARGET := $(NAME).8xp
include $(shell cedev-config --makefile)
-target: bin/NBCEDAT.8xv bin/NBCETEX.8xv
-bin/%.8xv: gfx/%.bin
- @mkdir -p bin
+target: $(BINDIR)/NBCEDAT.8xv $(BINDIR)/NBCETEX.8xv
+$(BINDIR)/%.8xv: gfx/%.bin
+ @mkdir -p $(BINDIR)
convbin -j bin -k 8xv -r -n $* -i $< -o $@
-# the textures' appvar carries the busiest loops too (flash/rsf.s), run from the archive
-obj/rsf.bin: flash/rsf.s
- @mkdir -p obj
- $(AS) -march=ez80+full $< -o obj/rsf.o
- $(OBJCOPY) -O binary -j .text obj/rsf.o $@
-bin/NBCETEX.8xv: gfx/NBCETEX.bin obj/rsf.bin
- @mkdir -p bin
- cat $^ > obj/NBCETEX.bin
- convbin -j bin -k 8xv -r -n NBCETEX -i obj/NBCETEX.bin -o $@
+# the textures' appvar carries a copy of the inner loops too (flash/rsf.s), run from the archive; built after the
+# program, against its map, and run only by the program of the same build (sources and flags hashed)
+RSF_BUILD := 0x$(shell (echo '$(NAME) $(CFLAGS)'; cat src/*.c src/*.h src/*.s src/*.inc flash/*.s) | shasum | cut -c1-6)
+ASFLAGS += --defsym RSF_BUILD=$(RSF_BUILD)
+$(BINDIR)/$(NAME).map: $(BINDIR)/$(TARGET)
+$(OBJDIR)/rsf_syms.inc: $(BINDIR)/$(NAME).map tools/rsfsyms.py
+ @mkdir -p $(OBJDIR)
+ python3 tools/rsfsyms.py $< > $@
+$(OBJDIR)/rsf.bin: flash/rsf.s src/rsb.inc src/rsb_list.inc $(OBJDIR)/rsf_syms.inc
+ $(AS) -march=ez80+full -I$(OBJDIR) --defsym RSF_BUILD=$(RSF_BUILD) $< -o $(OBJDIR)/rsf.o
+ $(OBJCOPY) -O binary -j .text $(OBJDIR)/rsf.o $@
+$(BINDIR)/NBCETEX.8xv: gfx/NBCETEX.bin $(OBJDIR)/rsf.bin
+ @mkdir -p $(BINDIR)
+ cat $^ > $(OBJDIR)/NBCETEX.bin
+ convbin -j bin -k 8xv -r -n NBCETEX -i $(OBJDIR)/NBCETEX.bin -o $@
diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.c
index 36ca1373..19c85fc6 100644
--- a/games/numblocks/ce/src/main.c
+++ b/games/numblocks/ce/src/main.c
@@ -1,6 +1,7 @@
/* NumBlocks on the TI-84 Plus CE: the title, the worlds, and the game's loop (Minecraft's 20 ticks a second, the
* picture as often as the CE can draw it). */
#include "nb.h"
+#include "rs.h"
#include "draw.h"
extern int new_mode;
@@ -275,7 +276,7 @@ int main(void) {
const uint8_t *fl = rs_flash;
uint32_t ha = 0, hb = 0;
for (int pass = 0; pass < 2; pass++) {
- rs_flash = pass ? 0 : fl;
+ if (pass) rs_flash_init(0, 0);
render_stale = 1;
render_frame();
uint32_t h = 0;
diff --git a/games/numblocks/ce/src/plat_ce.c b/games/numblocks/ce/src/plat_ce.c
index e2457af1..19b1503a 100644
--- a/games/numblocks/ce/src/plat_ce.c
+++ b/games/numblocks/ce/src/plat_ce.c
@@ -33,8 +33,8 @@ static void gfx_reload(void) {
unsigned n;
gfx = appvar("NBCEDAT", GFX_SIZE, &n);
texdat = appvar("NBCETEX", TEXDAT_SIZE, &n);
- /* (the busiest loops' copy, after the tables: run where they are, in the archive) */
- rs_flash = texdat && n >= TEXDAT_SIZE + RSF_SIZE ? texdat + TEXDAT_SIZE : 0;
+ /* (the inner loops' copy, after the tables: run where it is, in the archive) */
+ rs_flash_init(texdat && n > TEXDAT_SIZE ? texdat + TEXDAT_SIZE : 0, texdat ? n - TEXDAT_SIZE : 0);
}
static void gc_before(void) { gfx_End(); }
static void gc_after(void) {
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index 75abf361..76d86f8e 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -353,3 +353,26 @@ void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const u
}
}
#endif
+
+#ifdef __ez80__
+/* (rs.s: the trampolines, jp and an address each; the program's copy's addresses; this build's mark) */
+extern uint8_t rs_tramps[], rs_tramps_ram[], rs_ntramps, rs_build[3];
+void rs_flash_init(const uint8_t *blob, unsigned size) {
+ uint8_t n = rs_ntramps;
+ bool ok = blob && size >= 8 + 3 * (unsigned)n && !memcmp(blob, "NBRS", 4) && !memcmp(blob + 4, rs_build, 3) &&
+ blob[7] == n;
+ uint8_t *t = rs_tramps + 1;
+ const uint8_t *r = rs_tramps_ram, *o = blob + 8;
+ for (uint8_t i = 0; i < n; i++, t += 4, r += 3, o += 3) {
+ if (ok) {
+ const uint8_t *at = blob + (o[0] | o[1] << 8 | (unsigned)o[2] << 16);
+ memcpy(t, &at, 3);
+ } else
+ memcpy(t, r, 3);
+ }
+ rs_flash = ok ? blob : 0;
+}
+#else
+void rs_flash_init(const uint8_t *blob, unsigned size) { (void)blob, (void)size; }
+#endif
+
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index d6cb2169..aec9f7c9 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -57,9 +57,10 @@ extern const uint16_t *rs_qsqi, *rs_qsqk;
extern uint8_t rs_qsx, rs_qsz, rs_qxs0, rs_qzs0, rs_vd;
extern int rs_x, rs_z, rs_ci, rs_X0;
extern uint8_t rs_xs, rs_zs;
-/* the busiest loops' copy in the archive (flash/rsf.s, after NBCETEX's tables; 0: none), RSF_SIZE bytes */
+/* the inner loops' copy in the archive (flash/rsf.s, after NBCETEX's tables), run when of this build: size its
+ * bytes; rs_flash it, 0 when not run (the program's own copy then) */
extern const uint8_t *rs_flash;
-#define RSF_SIZE 224
+void rs_flash_init(const uint8_t *blob, unsigned size);
/* 2^28 / k for k 16..2560 (3 bytes each, from k = 16), and f * 256 / d for d 1..15, f < d ([d * 16 + f]): the
* tables rs_poly divides with (in NBCETEX) */
extern const uint8_t *rs_recip, *rs_ratio;
diff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.s
index 02e5cb00..44bf033a 100644
--- a/games/numblocks/ce/src/rs.s
+++ b/games/numblocks/ce/src/rs.s
@@ -3,2360 +3,209 @@
.assume adl=1
-; walkers: an edge each side of a polygon, stepped a row at a time
- .equ W_X, 0 ; x in 1/256 pixels
- .equ W_U, 3 ; u, v: the texel x 65536
- .equ W_V, 6
- .equ W_SX, 9 ; their steps a row
- .equ W_SU, 12
- .equ W_SV, 15
- .equ W_ROWS, 18 ; rows left on this edge
- .equ W_IDX, 19 ; the vertex it goes to
- .equ W_DIR, 20 ; 1 or n - 1
- .equ W_SIZE, 21
- .equ RW, 160
- .equ RSF_SPAN_ALPHA, 64 ; (flash/rsf.s's offsets)
- .equ RSF_DOUBLE, 128
- .equ RSF_OVERLAY, 192
; ------------------------------------------------------------------------------------------------- rs_setup
; void rs_setup(const uint8_t *tex, const uint8_t *cm, uint8_t alpha)
- .section .text._rs_setup
+; ------------------------------------------------------------------------------------------------- the routines
+ .section .text._rs_body
+ .include "src/rsb.inc"
+
+; ------------------------------------------------------------------------------------------------- trampolines
+; each routine's: jp to the program's copy, or the archive's when it is there and of this build (rs_flash_init)
+ .section .data._rs_tramps
+ .global _rs_tramps
+ .global _rs_tramps_ram
+ .macro tramp n
+ .global t_\n
+t_\n: jp b_\n
+ .endm
+ .macro ramaddr n
+ .d24 b_\n
+ .endm
+ .include "src/rsb_list.inc"
+_rs_tramps:
+ rsb_each tramp
+_rs_tramps_end:
+ .global _rs_ntramps
+_rs_ntramps: .db (_rs_tramps_end - _rs_tramps) / 4
+_rs_tramps_ram:
+ rsb_each ramaddr
+ .global _rs_build
+_rs_build: .d24 RSF_BUILD
.global _rs_setup
- .type _rs_setup, @function
-_rs_setup:
- ld iy, 0
- add iy, sp
- ld hl, (iy+3)
- ld (r_tex), hl
- ld hl, (iy+6)
- ld (r_cm), hl
- ; the loop: the archive's copy when there is one (flash/rsf.s: faster), else this one
- ld de, (_rs_flash)
- or a, a
- sbc hl, hl
- adc hl, de
- jr z, .Lsetup_ram
- ld a, (iy+9)
- or a, a
- jr z, .Lsetup_set
- ld de, RSF_SPAN_ALPHA
- add hl, de
- jr .Lsetup_set
-.Lsetup_ram:
- ld hl, span_plain
- ld a, (iy+9)
- or a, a
- jr z, .Lsetup_set
- ld hl, span_alpha
-.Lsetup_set:
- ld (r_loop), hl
- ret
-
- .section .text._rs_span
-; ------------------------------------------------------------------------------------------------- the span
-; de dst, a n (1..160); r_u, r_v, r_du, r_dv (u 8.8, v 4.12)
-; main set: de dst, hl the texture (h its page), bc the shade row (b its page, c the texel)
-; other set: hl u, de du, bc dv; ix v; iy points at the end's low byte
-span:
- push ix
- add a, e
- ld (r_end), a ; the end's low byte (n <= 160)
- ld hl, (r_u)
- push de
- ld de, (r_du)
- ld bc, (r_dv)
- exx
- pop de
- ld ix, (r_v)
- ld hl, (r_tex)
- ld bc, (r_cm)
- ld iy, r_end
- push hl
- ld hl, (r_loop)
- ex (sp), hl
- ret ; to the loop
-
-span_plain:
- ld a, (de)
- or a, a
- jr nz, .Lp_skip
- exx
- ld a, h ; u's texel column
- exx
- ld l, a
- ld a, ixh ; v's row, x 16
- and a, 0xF0
- or a, l
- ld l, a
- ld c, (hl) ; the texel
- ld a, (bc) ; shaded
- ld (de), a
-.Lp_skip:
- inc de
- exx
- add hl, de
- add ix, bc
- exx
- ld a, e
- cp a, (iy)
- jr nz, span_plain
- pop ix
- ret
-
-span_alpha:
- ld a, (de)
- or a, a
- jr nz, .La_skip
- exx
- ld a, h
- exx
- ld l, a
- ld a, ixh
- and a, 0xF0
- or a, l
- ld l, a
- ld a, (hl)
- or a, a ; see-through
- jr z, .La_skip
- ld c, a
- ld a, (bc)
- ld (de), a
-.La_skip:
- inc de
- exx
- add hl, de
- add ix, bc
- exx
- ld a, e
- cp a, (iy)
- jr nz, span_alpha
- pop ix
- ret
-
- .section .text._rs_arith
-; ------------------------------------------------------------------------------------------------- arithmetic
-; hl = hl x de >> 24: hl signed, de 0..2^24 (the low partial products left out)
-mulhi:
- ld (m_a), hl
- ld (m_r), de
- xor a, a
- ld (m_neg), a
- ld a, (m_a+2)
- bit 7, a
- jr z, .Lmh_pos
- ex de, hl
- or a, a
- sbc hl, hl
- sbc hl, de
- ld (m_a), hl
- ld a, 1
- ld (m_neg), a
-.Lmh_pos:
- ; t = a2 r0 + a1 r1 + a0 r2
- ld a, (m_a+2)
- ld h, a
- ld a, (m_r)
- ld l, a
- mlt hl
- ld a, (m_a+1)
- ld d, a
- ld a, (m_r+1)
- ld e, a
- mlt de
- add hl, de
- ld a, (m_a)
- ld d, a
- ld a, (m_r+2)
- ld e, a
- mlt de
- add hl, de
- ld (m_t), hl
- ld hl, (m_t+1) ; t >> 8 (m_t+3 is 0)
- ; + a2 r1 + a1 r2
- ld a, (m_a+2)
- ld d, a
- ld a, (m_r+1)
- ld e, a
- mlt de
- add hl, de
- ld a, (m_a+1)
- ld d, a
- ld a, (m_r+2)
- ld e, a
- mlt de
- add hl, de
- ; + a2 r2 << 8
- ld a, (m_a+2)
- ld d, a
- ld a, (m_r+2)
- ld e, a
- mlt de
- xor a, a
- ld (m_t), a
- ld (m_t+1), de ; (de's top byte is 0 after mlt: m_t+3 stays 0)
- ld de, (m_t)
- add hl, de
- jr neg_if
-
-; hl = hl x a >> 8: hl signed, a 0..255
-mul8:
- ld (m_f), a
- ld (m_a), hl
- xor a, a
- ld (m_neg), a
- ld a, (m_a+2)
- bit 7, a
- jr z, .Lm8_pos
- ex de, hl
- or a, a
- sbc hl, hl
- sbc hl, de
- ld (m_a), hl
- ld a, 1
- ld (m_neg), a
-.Lm8_pos:
- ; (s0 f >> 8) + s1 f + (s2 f << 8)
- ld a, (m_f)
- ld e, a
- ld a, (m_a)
- ld d, a
- mlt de ; s0 f
- ld c, d ; s0 f >> 8
- ld a, (m_f)
- ld e, a
- ld a, (m_a+2)
- ld d, a
- mlt de ; s2 f
- xor a, a
- ld (m_t), a
- ld (m_t+1), de
- ld hl, (m_t) ; s2 f << 8
- ld a, (m_f)
- ld e, a
- ld a, (m_a+1)
- ld d, a
- mlt de ; s1 f
- add hl, de
- ld de, 0
- ld e, c
- add hl, de
- ; (fall through)
-neg_if:
- ld a, (m_neg)
- or a, a
- ret z
- ex de, hl
- or a, a
- sbc hl, hl
- sbc hl, de
- ret
-
-; hl = p + a * 12
-vptr:
- ld l, a
- ld h, 12
- mlt hl
- ld de, (p_base)
- add hl, de
- ret
-
- .section .text._rs_edge
-; ------------------------------------------------------------------------------------------------- edges
-; the walker ix's next edge that crosses a row's middle; nz: one, z: none left
-edge:
- ld a, (p_n)
- ld b, a
-.Led_try:
- push bc
- ld a, (ix+W_IDX)
- call vptr
- push hl
- pop iy ; iy: the vertex a
- ld a, (ix+W_IDX)
- add a, (ix+W_DIR)
- ld hl, p_n
- cp a, (hl)
- jr c, .Led_in
- sub a, (hl)
-.Led_in:
- ld (ix+W_IDX), a
- call vptr ; hl: the vertex b
- ld (e_b), hl
- ; r0 = (ya + 7) >> 4, r1 = (yb + 7) >> 4
- push hl
- pop de
- inc de
- inc de
- inc de
- ex de, hl
- ld hl, (hl) ; yb
- ld (e_yb), hl
- ld de, 7
- add hl, de
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ld a, h
- ld (e_r1), a
- ld hl, (iy+3) ; ya
- add hl, de
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ld c, h ; r0
- ld a, (e_r1)
- sub a, c
- jp z, .Led_next
- jp c, .Led_next
- ld (ix+W_ROWS), a
- ; f = r0 * 16 + 8 - ya (0..15), x 16
- ld a, c
- add a, a
- add a, a
- add a, a
- add a, a
- add a, 8
- sub a, (iy+3)
- add a, a
- add a, a
- add a, a
- add a, a
- ld (e_f), a
- ; d = yb - ya
- ld hl, (e_yb)
- ld de, (iy+3)
- or a, a
- sbc hl, de
- ld (e_d), hl
- ; the deltas, x 16
- ld hl, (e_b)
- ld de, (hl) ; xb
- ex de, hl
- ld de, (iy+0)
- or a, a
- sbc hl, de
- call x16
- ld (e_dx), hl
- ld hl, (e_b)
- ld de, 6
- add hl, de
- ld de, (hl) ; ub
- ex de, hl
- ld de, (iy+6)
- or a, a
- sbc hl, de
- call x16
- ld (e_du), hl
- ld hl, (e_b)
- ld de, 9
- add hl, de
- ld de, (hl) ; vb
- ex de, hl
- ld de, (iy+9)
- or a, a
- sbc hl, de
- call x16
- ld (e_dv), hl
- ld hl, (e_d)
- ld de, 16
- or a, a
- sbc hl, de
- jr c, .Led_short
- ; d >= 16: the steps, delta x 2^28 / d >> 24
- add hl, hl ; (d - 16) * 3
- ld de, (e_d)
- add hl, de
- ld de, -16
- add hl, de
- ld de, (_rs_recip)
- add hl, de
- ld de, (hl)
- ld (e_r), de
- ld hl, (e_dx)
- call mulhi
- ld (ix+W_SX), hl
- ld de, (e_r)
- ld hl, (e_du)
- call mulhi
- ld (ix+W_SU), hl
- ld de, (e_r)
- ld hl, (e_dv)
- call mulhi
- ld (ix+W_SV), hl
- ; the first row: the vertex + step x f / 16
- ld hl, (ix+W_SX)
- ld a, (e_f)
- call mul8
- ld (e_dx), hl
- ld hl, (ix+W_SU)
- ld a, (e_f)
- call mul8
- ld (e_du), hl
- ld hl, (ix+W_SV)
- ld a, (e_f)
- call mul8
- ld (e_dv), hl
- jr .Led_start
-.Led_short:
- ; d < 16 (one row): delta x f / d
- xor a, a
- sbc hl, hl
- ld (ix+W_SX), hl
- ld (ix+W_SU), hl
- ld (ix+W_SV), hl
- ld a, (e_d)
- add a, a
- add a, a
- add a, a
- add a, a
- ld c, a
- ld a, (e_f)
- rrca
- rrca
- rrca
- rrca
- add a, c ; d * 16 + f
- ld de, 0
- ld e, a
- ld hl, (_rs_ratio)
- add hl, de
- ld a, (hl)
- ld (e_q), a
- ld hl, (e_dx)
- call mul8
- ld (e_dx), hl
- ld a, (e_q)
- ld hl, (e_du)
- call mul8
- ld (e_du), hl
- ld a, (e_q)
- ld hl, (e_dv)
- call mul8
- ld (e_dv), hl
-.Led_start:
- ; x = xa * 16 + e_dx, ...
- ld hl, (iy+0)
- call x16
- ld de, (e_dx)
- add hl, de
- ld (ix+W_X), hl
- ld hl, (iy+6)
- call x16
- ld de, (e_du)
- add hl, de
- ld (ix+W_U), hl
- ld hl, (iy+9)
- call x16
- ld de, (e_dv)
- add hl, de
- ld (ix+W_V), hl
- pop bc
- or a, 1 ; nz: found
- ret
-.Led_next:
- pop bc
- dec b
- jp nz, .Led_try
- xor a, a ; z: none
- ret
-
-x16:
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ret
-
-; de: a table of 16 steps of hl (texel x 65536 a pixel): hl x (k x 16 + 8) / 256, k 0..15
-ktable:
- push de
- push hl
- ld a, 8
- call mul8
- ld (kt_base), hl
- pop hl
- ld a, 16
- call mul8
- ex de, hl ; the step
- pop iy
- ld hl, (kt_base)
- ld b, 16
-.Lkt:
- ld (iy+0), hl
- lea iy, iy+3
- add hl, de
- djnz .Lkt
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_poly
-; void rs_poly(const SV *p, uint8_t n)
- .section .text._rs_poly
+ .set _rs_setup, t__rs_setup
.global _rs_poly
- .type _rs_poly, @function
-_rs_poly:
- push ix
- ld iy, 0
- add iy, sp
- ld hl, (iy+6)
- ld (p_base), hl
- xor a, a
- ld (r_lock), a
- ld a, (iy+9)
- ld (p_n), a
- ; the top vertex, the lowest and highest y
- ld b, a
- ld c, 0 ; index
- ld ix, (p_base)
- ld hl, (ix+3)
- ld (e_ymin), hl
- ld (e_ymax), hl
- xor a, a
- ld (e_top), a
-.Lpo_scan:
- ld hl, (ix+3)
- ld de, (e_ymin)
- or a, a
- sbc hl, de
- jr nc, .Lpo_notmin
- add hl, de
- ld (e_ymin), hl
- ld a, c
- ld (e_top), a
- jr .Lpo_nextv
-.Lpo_notmin:
- add hl, de
- ld de, (e_ymax)
- or a, a
- sbc hl, de
- jr c, .Lpo_nextv
- add hl, de
- ld (e_ymax), hl
-.Lpo_nextv:
- lea ix, ix+12
- inc c
- djnz .Lpo_scan
- ; rows y .. yend
- ld hl, (e_ymin)
- ld de, 7
- add hl, de
- call x16
- ld a, h
- ld (r_y), a
- ld hl, (e_ymax)
- add hl, de
- call x16
- ld a, h
- ld (r_yend), a
- ld hl, r_y
- sub a, (hl)
- jp z, .Lpo_done
- jp c, .Lpo_done
- ; the walkers: a forwards, b backwards
- ld a, (e_top)
- ld (wa+W_IDX), a
- ld (wb+W_IDX), a
- ld a, 1
- ld (wa+W_DIR), a
- ld a, (p_n)
- dec a
- ld (wb+W_DIR), a
- ld ix, wa
- call edge
- jp z, .Lpo_done
- ld ix, wb
- call edge
- jp z, .Lpo_done
- ; which is left: the smaller x (or step)
- ld hl, wa
- ld (w_l), hl
- ld hl, wb
- ld (w_r), hl
- ld hl, (wa+W_X)
- ld de, (wb+W_X)
- or a, a
- sbc hl, de
- jr z, .Lpo_tie
- jp m, .Lpo_row ; (x small: no overflow)
- jr .Lpo_swap
-.Lpo_tie:
- ld hl, (wa+W_SX)
- ld de, (wb+W_SX)
- or a, a
- sbc hl, de
- jp m, .Lpo_row
- jr z, .Lpo_row
-.Lpo_swap:
- ld hl, wb
- ld (w_l), hl
- ld hl, wa
- ld (w_r), hl
- ; the row's start in rbuf
-.Lpo_row:
- ld a, (r_y)
- ld l, a
- ld h, RW
- mlt hl
- ld de, (_rbuf)
- add hl, de
- ld (r_row), hl
-.Lpo_loop:
- ; px1 = (xr + 127) >> 8, at most 160; px0 = (xl + 127) >> 8 (x >= 0 on the screen: the high byte)
- ld de, 127
- ld ix, (w_r)
- ld hl, (ix+W_X)
- add hl, de
- ld a, h
- cp a, RW + 1
- jr c, .Lpo_x1ok
- ld a, RW
-.Lpo_x1ok:
- ld c, a
- ld ix, (w_l)
- ld hl, (ix+W_X)
- add hl, de
- ld a, c
- sub a, h
- jp z, .Lpo_step
- jp c, .Lpo_step
- ld (r_n), a
- ld a, h
- ld (r_px0), a
- ld a, (r_lock)
- or a, a
- jp nz, .Lpo_locked
- ld ix, (w_r)
- ; w = (xr - xl) >> 4: du, dv across it (0 under a pixel)
- ld hl, (ix+W_X)
- ld ix, (w_l)
- ld de, (ix+W_X)
- or a, a
- sbc hl, de
- call x16
- ld (m_t), hl
- ld hl, (m_t+1) ; (xr - xl) >> 4 (m_t+3 is 0)
- ld de, 16
- or a, a
- sbc hl, de
- jr nc, .Lpo_wide
- or a, a
- sbc hl, hl
- ld (r_du), hl
- ld (r_dv), hl
- jp .Lpo_grad
-.Lpo_wide:
- push hl
- ld de, 48
- or a, a
- sbc hl, de ; (w >= 64: four pixels or more, the gradient kept for the rows below)
- pop hl
- jr c, .Lpo_nolock
- ld a, 1
- ld (r_lock), a
-.Lpo_nolock:
- push hl ; (w - 16) * 3
- pop de
- add hl, hl
- add hl, de
- ld de, (_rs_recip)
- add hl, de
- ld de, (hl)
- ld (e_r), de
- ld ix, (w_r)
- ld hl, (ix+W_U)
- ld ix, (w_l)
- ld de, (ix+W_U)
- or a, a
- sbc hl, de
- ld de, (e_r)
- call mulhi
- ld (r_du), hl
- ld ix, (w_r)
- ld hl, (ix+W_V)
- ld ix, (w_l)
- ld de, (ix+W_V)
- or a, a
- sbc hl, de
- ld de, (e_r)
- call mulhi
- ld (r_dv), hl
- ld a, (r_lock)
- or a, a
- jr z, .Lpo_grad
- ; locked: k/16 + 1/32 of a pixel's step, k 0..15, to step from the edge to a pixel's middle
- ld hl, (r_du)
- ld de, r_ku
- call ktable
- ld hl, (r_dv)
- ld de, r_kv
- call ktable
- ; the steps in the span's units: du >> 8, dv >> 4 (kept for the rows below)
- ld hl, (r_du)
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_du), hl
- ld hl, (r_dv)
- call x16
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_dv), hl
- jr .Lpo_locked
-.Lpo_grad:
- ; frac = 128 - xl (low byte): from the edge to the first pixel's middle
- ld ix, (w_l)
- ld a, 128
- sub a, (ix+W_X)
- ld (e_q), a
- ld hl, (r_du)
- call mul8
- ld de, (ix+W_U)
- add hl, de
- call clamp20
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_u), hl ; u >> 8: 8.8
- ld a, (e_q)
- ld hl, (r_dv)
- call mul8
- ld de, (ix+W_V)
- add hl, de
- call clamp20
- call x16
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_v), hl ; v >> 4: 4.12
- ld hl, (r_du)
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_du), hl ; du >> 8
- ld hl, (r_dv)
- call x16
- ld (m_t), hl
- ld hl, (m_t+1)
- ld (r_dv), hl ; dv >> 4
- jr .Lpo_span
-.Lpo_locked:
- ; (ix: the left walker) from the edge to the first pixel's middle: the table's step for (128 - xl) >> 4;
- ; u >> 8 (8.8) and v >> 4 (4.12) for the span, kept to the texture
- ld a, 128
- sub a, (ix+W_X)
- rrca
- rrca
- rrca
- rrca
- and a, 15
- ld l, a
- ld h, 3
- mlt hl
- push hl
- ld de, r_ku
- add hl, de
- ld de, (hl)
- ld hl, (ix+W_U)
- add hl, de
- ld (m_t), hl
- ld a, (m_t+2)
- cp a, 16
- jr nc, .Lpo_uc
- ld hl, (m_t+1)
-.Lpo_us:
- ld (r_u), hl
- pop hl
- ld de, r_kv
- add hl, de
- ld de, (hl)
- ld hl, (ix+W_V)
- add hl, de
- ld (m_t), hl
- ld a, (m_t+2)
- cp a, 16
- jr nc, .Lpo_vc
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ld (m_t), hl
- ld hl, (m_t+1)
-.Lpo_vs:
- ld (r_v), hl
- jr .Lpo_span
-.Lpo_uc:
- rla ; (past the texture: below its start, or past its end)
- ld hl, 0x0FFF
- jr nc, .Lpo_us
- ld hl, 0
- jr .Lpo_us
-.Lpo_vc:
- rla
- ld hl, 0xFFFF
- jr nc, .Lpo_vs
- ld hl, 0
- jr .Lpo_vs
-.Lpo_span:
- ld a, (r_px0)
- ld de, 0
- ld e, a
- ld hl, (r_row)
- add hl, de
- ex de, hl
- ld a, (r_n)
- call span
-.Lpo_step:
- ld hl, r_y
- inc (hl)
- ld a, (r_yend)
- cp a, (hl)
- jr z, .Lpo_done
- ld hl, (r_row)
- ld de, RW
- add hl, de
- ld (r_row), hl
- ld ix, (w_l)
- call wstep
- ld ix, (w_r)
- ld a, (r_lock)
- or a, a
- jr z, .Lpo_rboth
- call wstep_x ; (locked: the right edge's u and v no longer needed)
- jp .Lpo_loop
-.Lpo_rboth:
- call wstep
- jp .Lpo_loop
-.Lpo_done:
- pop ix
- ret
-
- .section .text._rs_wstep
-; the walker ix a row on; its next edge when this one is done; nz: going on
-wstep:
- ld hl, (ix+W_X)
- ld de, (ix+W_SX)
- add hl, de
- ld (ix+W_X), hl
- ld hl, (ix+W_U)
- ld de, (ix+W_SU)
- add hl, de
- ld (ix+W_U), hl
- ld hl, (ix+W_V)
- ld de, (ix+W_SV)
- add hl, de
- ld (ix+W_V), hl
- dec (ix+W_ROWS)
- ret nz
- call edge
- ret nz
- pop hl ; (none left: the polygon is done)
- jp .Lpo_done
-
-; the same, x alone
-wstep_x:
- ld hl, (ix+W_X)
- ld de, (ix+W_SX)
- add hl, de
- ld (ix+W_X), hl
- dec (ix+W_ROWS)
- ret nz
- call edge
- ret nz
- pop hl
- jp .Lpo_done
-
-; hl into 0..0xFFFFF
-clamp20:
- ld (m_t), hl
- ld a, (m_t+2)
- bit 7, a
- jr nz, .Lcl_zero
- cp a, 0x10
- ret c
- ld hl, 0xFFFFF
- ret
-.Lcl_zero:
- or a, a
- sbc hl, hl
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_proj
-; void rs_proj(int x, int y, int z, int *out)
-; z shifted up until its top bit falls out (c shifts): the next 8 bits pick t = 1371 << 12 / (256 + i);
-; x and y shifted up c - 3 (the top 16 bits kept), times t, >> 16: the offset from the middle in 1/16 pixels
- .section .text._rs_proj
+ .set _rs_poly, t__rs_poly
.global _rs_proj
- .type _rs_proj, @function
-_rs_proj:
- ld iy, 0
- add iy, sp
- ld hl, (iy+9) ; z
- call pr_z
- ld hl, (iy+3)
- call pr_mul
- ld de, 1280
- add hl, de
- push hl
- ld hl, (iy+6)
- call pr_mul
- ex de, hl
- ld hl, 960
- or a, a
- sbc hl, de
- ld iy, (iy+12)
- ld (iy+3), hl
- pop hl
- ld (iy+0), hl
- ret
-
- .section .text._rs_prz
-; m_r = t, m_f = the shift, for z (hl)
-pr_z:
- ld b, 0
-.Lpr_norm:
- add hl, hl
- jr c, .Lpr_found
- inc b
- jr .Lpr_norm
-.Lpr_found:
- ld (m_t), hl
- ld a, (m_t+2) ; the index: the 8 bits after the top one
- or a, a
- sbc hl, hl
- ld l, a
- add hl, hl
- ld de, r_rtab
- add hl, de
- ld de, (hl)
- ld (m_r), de ; t
- ld a, b
- sub a, 3
- ld (m_f), a ; the shift
- ret
-
-; hl = ((hl << m_f), its top 16 bits) x t >> 16, the sign apart
-pr_mul:
- ld a, (m_f)
- or a, a
- jr z, .Lpm_shifted
- ld b, a
-.Lpm_shift:
- add hl, hl
- djnz .Lpm_shift
-.Lpm_shifted:
- ld (m_a), hl
- xor a, a
- ld (m_neg), a
- ld a, (m_a+2)
- bit 7, a
- jr z, .Lpm_pos
- ex de, hl
- or a, a
- sbc hl, hl
- sbc hl, de
- ld (m_a), hl
- ld a, 1
- ld (m_neg), a
-.Lpm_pos:
- ; (ah al) x (th tl) >> 16 = ah th + (ah tl + al th + (al tl >> 8)) >> 8, a = m_a+1, m_a+2
- ld a, (m_a+1)
- ld h, a
- ld a, (m_r)
- ld l, a
- mlt hl ; al tl
- ld a, h
- ld de, 0
- ld e, a
- push de
- ld a, (m_a+2)
- ld h, a
- ld a, (m_r)
- ld l, a
- mlt hl ; ah tl
- ld a, (m_a+1)
- ld d, a
- ld a, (m_r+1)
- ld e, a
- mlt de ; al th
- add hl, de
- pop de
- add hl, de
- ld (m_t), hl
- ld hl, (m_t+1) ; >> 8
- ld a, (m_a+2)
- ld d, a
- ld a, (m_r+1)
- ld e, a
- mlt de ; ah th
- add hl, de
- jp neg_if
-
-; ------------------------------------------------------------------------------------------------- rs_hidden
-; uint8_t rs_hidden(int x, int y, int z, int r)
-; 1 when the pixels around (x, y, z)'s projection, as far as r's projection and a pixel more each way, are all
-; drawn already (or off the screen)
- .section .text._rs_hidden
+ .set _rs_proj, t__rs_proj
.global _rs_hidden
- .type _rs_hidden, @function
-_rs_hidden:
- ld iy, 0
- add iy, sp
- ld hl, (iy+9)
- call pr_z
- ld hl, (iy+12)
- call pr_mul
- ld de, 16
- add hl, de
- ld (h_r), hl
- ld hl, (iy+6)
- call pr_mul
- ex de, hl
- ld hl, 960
- or a, a
- sbc hl, de
- ld c, 119
- call h_range
- jr c, .Lh_yes
- ld a, b
- ld (h_y0), a
- ld a, c
- ld (h_rows), a
- ld hl, (iy+3)
- call pr_mul
- ld de, 1280
- add hl, de
- ld c, RW - 1
- call h_range
- jr c, .Lh_yes
- ld a, c
- ld (h_w), a
- ld a, (h_y0)
- ld h, a
- ld l, RW
- mlt hl
- ld de, (_rbuf)
- add hl, de
- ld de, 0
- ld e, b
- add hl, de
-.Lh_row:
- push hl
- ld bc, 0
- ld a, (h_w)
- ld c, a
- xor a, a
- cpir
- pop hl
- jr z, .Lh_no
- ld de, RW
- add hl, de
- ld a, (h_rows)
- dec a
- ld (h_rows), a
- jr nz, .Lh_row
-.Lh_yes:
- ld a, 1
- ret
-.Lh_no:
- xor a, a
- ret
-
-; hl the middle (1/16 pixels), c the last pixel: b the first pixel of (hl - h_r, hl + h_r), c how many; carry: none
-h_range:
- ld (h_c), hl
- ld de, (h_r)
- or a, a
- sbc hl, de
- call h_px
- jr nc, .Lhr_lo
- xor a, a ; (from 0)
-.Lhr_lo:
- cp a, c
- jr z, .Lhr_lok
- jr nc, .Lhr_off ; (past the last)
-.Lhr_lok:
- ld b, a
- ld hl, (h_c)
- ld de, (h_r)
- add hl, de
- call h_px
- ret c ; (before the first)
- cp a, c
- jr c, .Lhr_hok
- ld a, c
-.Lhr_hok:
- sub a, b
- inc a
- ld c, a
- or a, a
- ret
-.Lhr_off:
- scf
- ret
-
-; a = hl >> 4 (hl in 1/16 pixels, |hl| < 2^19), 255 past 255; carry: hl < 0
-h_px:
- add hl, hl
- ret c
- add hl, hl
- add hl, hl
- add hl, hl
- ld (m_t), hl
- ld a, (m_t+2)
- or a, a
- ld a, (m_t+1)
- ret z
- ld a, 255
- or a, a
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_expand
-; void rs_expand(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud)
-; 120 rows of 160 to 240 rows of 320; 0 is the row's sky colour; from screen row 180 on, the HUD's layer
-; (187 wide at x 67, 0 see-through) over both rows, the rows made in a buffer first (nothing flickers)
- .section .text._rs_expand
+ .set _rs_hidden, t__rs_hidden
.global _rs_expand
- .type _rs_expand, @function
- .equ HUD_X, 67
- .equ HUD_W, 187
-_rs_expand:
- push ix
- ; the loops: the archive's when there is one (flash/rsf.s), written into the calls below
- ld de, (_rs_flash)
- or a, a
- sbc hl, hl
- adc hl, de
- jr z, .Lex_ram
- ld de, RSF_DOUBLE
- add hl, de
- ld (.Lex_c1 + 1), hl
- ld (.Lex_c2 + 1), hl
- ld de, RSF_OVERLAY - RSF_DOUBLE
- add hl, de
- ld (.Lex_c3 + 1), hl
- ld (.Lex_c4 + 1), hl
- jr .Lex_go
-.Lex_ram:
- ld hl, r_double
- ld (.Lex_c1 + 1), hl
- ld (.Lex_c2 + 1), hl
- ld hl, r_overlay
- ld (.Lex_c3 + 1), hl
- ld (.Lex_c4 + 1), hl
-.Lex_go:
- ld iy, 0
- add iy, sp
- ld hl, (iy+15)
- ld (r_hud), hl
- ld a, 31 ; (rows left under 31: under the HUD; none without one)
- add hl, de
- or a, a
- sbc hl, de
- jr nz, .Lex_withhud
- xor a, a
-.Lex_withhud:
- ld (r_hudlim), a
- ld hl, (iy+6) ; src
- ld de, (iy+9) ; dst
- ld ix, (iy+12) ; sky
- ld a, 120
- ld (r_rows), a
- ld iy, r_hudlim
-.Lex_row:
- ld a, (r_rows)
- cp a, (iy)
- jr c, .Lex_hudrow
- ld c, (ix+0)
- ld b, RW / 2
-.Lex_c1:
- call r_double
- push hl
- ld hl, -320
- add hl, de
- ld bc, 320
- ldir
- pop hl
-.Lex_next:
- inc ix
- ld a, (r_rows)
- dec a
- ld (r_rows), a
- jr nz, .Lex_row
- pop ix
- ret
-
-.Lex_hudrow:
- push de
- ld de, r_line
- ld c, (ix+0)
- ld b, RW / 2
-.Lex_c2:
- call r_double
- push hl
- ld hl, r_line
- ld de, r_line2
- ld bc, 320
- ldir
- ld de, r_line + HUD_X
- ld hl, (r_hud)
- ld b, HUD_W
-.Lex_c3:
- call r_overlay
- ld de, r_line2 + HUD_X
- ld b, HUD_W
-.Lex_c4:
- call r_overlay
- ld (r_hud), hl
- pop hl
- pop de
- push hl
- ld hl, r_line
- ld bc, 640
- ldir
- pop hl
- jr .Lex_next
-
-; b pairs of pixels from hl doubled to de (0: c, the row's sky); hl and de after them (flash/rsf.s has it too)
-r_double:
- ld a, (hl)
- inc hl
- or a, a
- jr nz, .Lrd_set
- ld a, c
-.Lrd_set:
- ld (de), a
- inc de
- ld (de), a
- inc de
- ld a, (hl)
- inc hl
- or a, a
- jr nz, .Lrd_set2
- ld a, c
-.Lrd_set2:
- ld (de), a
- inc de
- ld (de), a
- inc de
- djnz r_double
- ret
-
-; b pixels of the layer (hl) over de, 0 see-through; hl and de after them
-r_overlay:
- ld a, (hl)
- inc hl
- or a, a
- jr z, .Lov_skip
- ld (de), a
-.Lov_skip:
- inc de
- djnz r_overlay
- ret
-
-
-; ------------------------------------------------------------------------------------------------- rs_cells
-; uint8_t rs_cells(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out)
-; a column's cells with a face (mask: 32 bits, bit y & 31): from slot up (0, 1, .. up - 1 levels above), then
-; down (-1, -2, .. -down): the offsets of those set, in that order; how many
- .section .text._rs_cells
+ .set _rs_expand, t__rs_expand
.global _rs_cells
- .type _rs_cells, @function
-_rs_cells:
- push ix
- ld ix, 0
- add ix, sp
- ld iy, (ix+18) ; out
- ld hl, (ix+6)
- ld de, cl_mask
- ld bc, 4
- ldir ; (the mask, twice over: no wrapping to mind)
- ld hl, (ix+6)
- ld bc, 4
- ldir
- ld c, 0 ; how many
- ; up: bit slot onwards
- ld a, (ix+12)
- or a, a
- jr z, .Lcl_down
- ld b, a ; levels
- ld a, (ix+9)
- call cl_at ; hl: the byte, e: the bit
- ld d, 0 ; offset
-.Lcl_up:
- ld a, (hl)
- and a, e
- jr z, .Lcl_up_no
- ld (iy), d
- inc iy
- inc c
-.Lcl_up_no:
- inc d
- rlc e
- jr nc, .Lcl_up_same
- inc hl
-.Lcl_up_same:
- djnz .Lcl_up
-.Lcl_down:
- ld a, (ix+15)
- or a, a
- jr z, .Lcl_done
- ld b, a
- ld a, (ix+9)
- add a, 31 ; slot - 1 (+ 32)
- call cl_at
- ld d, -1
-.Lcl_dn:
- ld a, (hl)
- and a, e
- jr z, .Lcl_dn_no
- ld (iy), d
- inc iy
- inc c
-.Lcl_dn_no:
- dec d
- rrc e
- jr nc, .Lcl_dn_same
- dec hl
-.Lcl_dn_same:
- djnz .Lcl_dn
-.Lcl_done:
- ld a, c
- pop ix
- ret
-
-; a: a bit 0..63 of cl_mask: hl its byte, e its bit (bc kept)
-cl_at:
- push af
- rrca
- rrca
- rrca
- and a, 7
- ld hl, cl_mask
- ld de, 0
- ld e, a
- add hl, de
- pop af
- and a, 7
- ld e, 1
- ret z
-.Lcla_shift:
- rlc e
- dec a
- jr nz, .Lcla_shift
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_column
-; void rs_column(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx)
-; a column's cells with a face (rs_cells' order), each in the view (distance, sides, top and bottom) and not
-; hidden by what is drawn already: rs_cell(Y, Z, fog, j + 32, its y slot)
- .section .text._rs_column
+ .set _rs_cells, t__rs_cells
.global _rs_column
- .type _rs_column, @function
-_rs_column:
- push ix
- ld ix, 0
- add ix, sp
- ld hl, (ix+9)
- ld (c_h2), hl
- ld hl, (ix+12)
- ld (c_Y0), hl
- ld hl, (ix+15)
- ld (c_Z0), hl
- ld hl, (ix+18)
- ld de, (_rs_vzk)
- add hl, de
- ld (c_V0), hl ; (V0 + vz0 + KY)
- ld hl, (ix+21)
- ld (c_cx), hl
- ld de, (_rs_KX)
- or a, a
- sbc hl, de
- ld (c_xa), hl ; cx - KX
- add hl, de
- add hl, de
- ld (c_xb), hl ; cx + KX
- ld hl, c_list
- push hl
- ld a, (_rs_down)
- ld l, a
- push hl
- ld a, (_rs_up)
- ld l, a
- push hl
- ld a, (_rs_eys)
- ld l, a
- push hl
- ld hl, (ix+6)
- push hl
- call _rs_cells
- pop hl
- pop hl
- pop hl
- pop hl
- pop hl
- ld (c_left), a
- ld hl, c_list
- ld (c_ptr), hl
-.Lco_next:
- ld a, (c_left)
- or a, a
- jp z, .Lco_done
- dec a
- ld (c_left), a
- ld hl, (c_ptr)
- ld a, (hl)
- inc hl
- ld (c_ptr), hl
- ld (c_lj), a
- ld hl, _rs_jbase
- add a, (hl)
- add a, 32
- ld (c_o), a
- ; d2 = h2 + j^2, past the view: passed by
- or a, a
- sbc hl, hl
- ld l, a
- add hl, hl
- ld de, _rs_jS
- add hl, de
- ld de, 0
- ld e, (hl)
- inc hl
- ld d, (hl)
- ld hl, (c_h2)
- add hl, de
- ld (c_d2), hl
- ex de, hl
- ld hl, (_rs_vlim)
- or a, a
- sbc hl, de
- jr c, .Lco_next
- ; Y, Z, V from the tables (3 bytes each)
- ld a, (c_o)
- ld l, a
- ld h, 3
- mlt hl
- push hl
- ld de, _rs_jY
- add hl, de
- ld de, (hl)
- ld hl, (c_Y0)
- add hl, de
- ld (c_Y), hl
- ld de, (_rs_cy0)
- add hl, de
- ld (c_cy), hl
- pop hl
- push hl
- ld de, _rs_jZ
- add hl, de
- ld de, (hl)
- ld hl, (c_Z0)
- add hl, de
- ld (c_Z), hl
- ld de, (_rs_cz0)
- add hl, de
- ld (c_cz), hl
- pop hl
- ld de, _rs_jV
- add hl, de
- ld de, (hl)
- ld hl, (c_V0)
- add hl, de
- ld (c_vz), hl
- ; the view: cz >= -R, |cx| <= cz + KX, |cy| <= vz
- ld hl, (c_cz)
- ld de, (_rs_R)
- or a, a
- adc hl, de
- jp m, .Lco_next
- ld hl, (c_cz)
- ld de, (c_xa)
- or a, a
- sbc hl, de
- jp m, .Lco_next
- ld hl, (c_cz)
- ld de, (c_xb)
- or a, a
- adc hl, de
- jp m, .Lco_next
- ld hl, (c_vz)
- ld de, (c_cy)
- or a, a
- sbc hl, de
- jp m, .Lco_next
- ld hl, (c_vz)
- or a, a
- adc hl, de
- jp m, .Lco_next
- ; the fog
- ld hl, (c_d2)
- ld de, _rs_fog
- add hl, de
- ld a, (hl)
- cp a, 9
- jp nc, .Lco_next
- ld (c_fog), a
- ; hidden? (far enough to tell: cz > 2R)
- ld hl, (_rs_R)
- add hl, hl
- ld de, (c_cz)
- or a, a
- sbc hl, de
- jp p, .Lco_draw
- ld hl, (_rs_R)
- push hl
- push de
- ld hl, (c_cy)
- push hl
- ld hl, (c_cx)
- push hl
- call _rs_hidden
- pop hl
- pop hl
- pop hl
- pop hl
- or a, a
- jp nz, .Lco_next
-.Lco_draw:
- ld a, (c_lj)
- ld hl, _rs_eys
- add a, (hl)
- and a, 31
- ld l, a
- push hl
- ld a, (c_o)
- ld l, a
- push hl
- ld a, (c_fog)
- ld l, a
- push hl
- ld hl, (c_Z)
- push hl
- ld hl, (c_Y)
- push hl
- call _rs_cell
- pop hl
- pop hl
- pop hl
- pop hl
- pop hl
- jp .Lco_next
-.Lco_done:
- pop ix
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_quad
-; void rs_quad(void)
-; a quadrant's columns (rs_q*), rows out from the eye along x, columns out along z, near to far: those in the
-; window, in the view's distance and sides and with a face, to rs_column (rs_x, rs_z, rs_ci, rs_X0, rs_xs, rs_zs:
-; the column)
- .section .text._rs_quad
+ .set _rs_column, t__rs_column
.global _rs_quad
- .type _rs_quad, @function
-_rs_quad:
- push ix
- xor a, a
- ld (q_a), a
- ld a, (_rs_qxs0)
- ld (q_xs), a
- ld hl, (_rs_qx0)
- ld (q_x), hl
-.Lq_row:
- ; x in the window?
- ld hl, (q_x)
- ld de, (_rs_wx0)
- or a, a
- sbc hl, de
- jp m, .Lq_done
- ld hl, (q_x)
- ld de, (_rs_wx1)
- or a, a
- sbc hl, de
- jp p, .Lq_done
- ; the row's first column
- ld hl, (_rs_qXi)
- ld (q_X), hl
- ld hl, (_rs_qYi)
- ld (q_Y), hl
- ld hl, (_rs_qZi)
- ld (q_Z), hl
- ld hl, (_rs_qVi)
- ld (q_V), hl
- ld a, (_rs_qzs0)
- ld (q_zs), a
- ld hl, (_rs_qz0)
- ld (q_z), hl
- ld a, (q_a)
- or a, a
- sbc hl, hl
- ld l, a
- add hl, hl
- ld de, (_rs_qsqi)
- add hl, de
- ld de, 0
- ld e, (hl)
- inc hl
- ld d, (hl)
- ld (q_hi), de ; |i| squared
- xor a, a
- ld (q_c), a
-.Lq_col:
- ; h2 = |i|^2 + |k|^2, past the view: the row's end
- ld a, (q_c)
- or a, a
- sbc hl, hl
- ld l, a
- add hl, hl
- ld de, (_rs_qsqk)
- add hl, de
- ld de, 0
- ld e, (hl)
- inc hl
- ld d, (hl)
- ld hl, (q_hi)
- add hl, de
- ld (q_h2), hl
- ex de, hl
- ld hl, (_rs_vlim)
- or a, a
- sbc hl, de
- jp c, .Lq_rowend
- ; z in the window?
- ld hl, (q_z)
- ld de, (_rs_wz0)
- or a, a
- sbc hl, de
- jp m, .Lq_rowend
- ld hl, (q_z)
- ld de, (_rs_wz1)
- or a, a
- sbc hl, de
- jp p, .Lq_rowend
- ; the view's sides: |cx| <= Z + czm
- ld hl, (q_X)
- ld de, (_rs_cx0)
- add hl, de
- ld (q_cx), hl
- ex de, hl
- ld hl, (q_Z)
- ld bc, (_rs_czm)
- add hl, bc
- push hl
- or a, a
- sbc hl, de
- pop hl
- jp m, .Lq_next
- or a, a
- adc hl, de
- jp m, .Lq_next
- ; the column: ci = zs * 32 + xs, its mask
- ld a, (q_zs)
- ld l, a
- ld h, 32
- mlt hl
- ld a, (q_xs)
- ld de, 0
- ld e, a
- add hl, de
- ld (_rs_ci), hl
- add hl, hl
- add hl, hl
- ld de, _wexp
- add hl, de
- push hl
- ld a, (hl)
- inc hl
- or a, (hl)
- inc hl
- or a, (hl)
- inc hl
- or a, (hl)
- pop de
- jp z, .Lq_next
- ld hl, (q_x)
- ld (_rs_x), hl
- ld hl, (q_z)
- ld (_rs_z), hl
- ld hl, (q_X)
- ld (_rs_X0), hl
- ld a, (q_xs)
- ld (_rs_xs), a
- ld a, (q_zs)
- ld (_rs_zs), a
- ld hl, (q_cx)
- push hl
- ld hl, (q_V)
- push hl
- ld hl, (q_Z)
- push hl
- ld hl, (q_Y)
- push hl
- ld hl, (q_h2)
- push hl
- push de
- call _rs_column
- ld hl, 18
- add hl, sp
- ld sp, hl
-.Lq_next:
- ; the next column out along z
- ld a, (q_c)
- inc a
- ld (q_c), a
- ld hl, _rs_vd
- cp a, (hl)
- jr z, .Lq_cok
- jp nc, .Lq_rowend
-.Lq_cok:
- ld a, (_rs_qsz)
- ld hl, q_zs
- add a, (hl)
- and a, 31
- ld (hl), a
- ld hl, (q_z)
- ld de, (_rs_qszi)
- add hl, de
- ld (q_z), hl
- ld hl, (q_X)
- ld de, (_rs_qsXz)
- add hl, de
- ld (q_X), hl
- ld hl, (q_Y)
- ld de, (_rs_qsYz)
- add hl, de
- ld (q_Y), hl
- ld hl, (q_Z)
- ld de, (_rs_qsZz)
- add hl, de
- ld (q_Z), hl
- ld hl, (q_V)
- ld de, (_rs_qsVz)
- add hl, de
- ld (q_V), hl
- jp .Lq_col
-.Lq_rowend:
- call _plat_poll ; (a key tapped while the picture is made)
- ; the next row out along x
- ld a, (q_a)
- inc a
- ld (q_a), a
- ld hl, _rs_vd
- cp a, (hl)
- jr z, .Lq_aok
- jp nc, .Lq_done
-.Lq_aok:
- ld a, (_rs_qsx)
- ld hl, q_xs
- add a, (hl)
- and a, 31
- ld (hl), a
- ld hl, (q_x)
- ld de, (_rs_qsxi)
- add hl, de
- ld (q_x), hl
- ld hl, (_rs_qXi)
- ld de, (_rs_qsXx)
- add hl, de
- ld (_rs_qXi), hl
- ld hl, (_rs_qYi)
- ld de, (_rs_qsYx)
- add hl, de
- ld (_rs_qYi), hl
- ld hl, (_rs_qZi)
- ld de, (_rs_qsZx)
- add hl, de
- ld (_rs_qZi), hl
- ld hl, (_rs_qVi)
- ld de, (_rs_qsVx)
- add hl, de
- ld (_rs_qVi), hl
- jp .Lq_row
-.Lq_done:
- pop ix
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_code
-; int rs_code(int x, int y, int z)
-; bits 0-4: the planes a polygon through (x, y, z) is cut by (z < NEAR, x > 4z, -x > 4z, y > 4z, -y > 4z); bits
-; 8-12: the view's sides it is past, a little wider than the screen's (z < NEAR, 16x > 15z, -16x > 15z, 4y > 3z,
-; -4y > 3z)
- .section .text._rs_code
+ .set _rs_quad, t__rs_quad
.global _rs_code
- .type _rs_code, @function
- .equ NEAR, 819
-_rs_code:
- ld iy, 0
- add iy, sp
- ld bc, 0
- ld hl, (iy+9)
- ld de, NEAR
- or a, a
- sbc hl, de
- jp p, .Lrc_front
- ld bc, 0x0101
-.Lrc_front:
- ld hl, (iy+9)
- add hl, hl
- add hl, hl
- ld (rc_t), hl ; 4z
- ld de, (iy+3)
- or a, a
- sbc hl, de
- jp p, .Lrc1
- set 1, c
-.Lrc1:
- ld hl, (rc_t)
- or a, a
- adc hl, de
- jp p, .Lrc2
- set 2, c
-.Lrc2:
- ld hl, (rc_t)
- ld de, (iy+6)
- or a, a
- sbc hl, de
- jp p, .Lrc3
- set 3, c
-.Lrc3:
- ld hl, (rc_t)
- or a, a
- adc hl, de
- jp p, .Lrc4
- set 4, c
-.Lrc4:
- ld hl, (iy+9)
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ld de, (iy+9)
- or a, a
- sbc hl, de
- ld (rc_t), hl ; 15z
- ld hl, (iy+3)
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ex de, hl ; 16x
- ld hl, (rc_t)
- or a, a
- sbc hl, de
- jp p, .Lrc5
- set 1, b
-.Lrc5:
- ld hl, (rc_t)
- or a, a
- adc hl, de
- jp p, .Lrc6
- set 2, b
-.Lrc6:
- ld hl, (iy+9)
- push hl
- pop de
- add hl, hl
- add hl, de
- ld (rc_t), hl ; 3z
- ld hl, (iy+6)
- add hl, hl
- add hl, hl
- ex de, hl ; 4y
- ld hl, (rc_t)
- or a, a
- sbc hl, de
- jp p, .Lrc7
- set 3, b
-.Lrc7:
- ld hl, (rc_t)
- or a, a
- adc hl, de
- jp p, .Lrc8
- set 4, b
-.Lrc8:
- or a, a
- sbc hl, hl
- ld h, b
- ld l, c
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_compose
-; void rs_compose(uint8_t *dst, const uint8_t *a, const uint8_t *f): dst[i] = f[a[i]], all three 256-byte aligned
- .section .text._rs_compose
+ .set _rs_code, t__rs_code
.global _rs_compose
- .type _rs_compose, @function
-_rs_compose:
- ld iy, 0
- add iy, sp
- ld de, (iy+3)
- ld hl, (iy+6)
- ld bc, (iy+9)
-.Lcm_loop:
- ld c, (hl)
- ld a, (bc)
- ld (de), a
- inc l
- inc e
- ld c, (hl)
- ld a, (bc)
- ld (de), a
- inc l
- inc e
- jr nz, .Lcm_loop
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_lerp
-; int rs_lerp(int a, int b, int f): a + floor((b - a) f / 256), a and b 0..255, f 0..256
- .section .text._rs_lerp
+ .set _rs_compose, t__rs_compose
.global _rs_lerp
- .type _rs_lerp, @function
-_rs_lerp:
- ld iy, 0
- add iy, sp
- ld a, (iy+10)
- or a, a
- jr nz, .Llp_b
- ld a, (iy+6)
- sub a, (iy+3)
- jr c, .Llp_neg
- ld h, a
- ld l, (iy+9)
- mlt hl
- ld a, h
- add a, (iy+3)
- sbc hl, hl
- ld l, a
- ret
-.Llp_neg:
- neg
- ld h, a
- ld l, (iy+9)
- mlt hl
- ld de, 255
- add hl, de
- ld a, (iy+3)
- sub a, h
- sbc hl, hl
- ld l, a
- ret
-.Llp_b:
- ld hl, (iy+6)
- ret
-
-; ------------------------------------------------------------------------------------------------- w_neighbours
-; void w_neighbours(uint16_t i): the window cell i's neighbours inside it (-Y +Y -Z +Z -X +X) into w_nbr, how many
-; into w_nbr_n (world.c; i = y << 10 | z << 5 | x, each 0..31; w_esx.. the window's first and last slots)
- .section .text._w_neighbours
+ .set _rs_lerp, t__rs_lerp
.global _w_neighbours
- .type _w_neighbours, @function
-_w_neighbours:
- ld iy, 0
- add iy, sp
- ld l, (iy+3)
- ld h, (iy+4)
- ld iy, _w_nbr
- ld c, 0
- ld a, h
- rrca
- rrca
- and a, 0x1F
- ld b, a ; y
- ld a, (_w_esy)
- cp a, b
- jr z, .Lnb_y1
- ld a, h
- sub a, 4
- and a, 0x7C
- ld d, a
- ld a, h
- and a, 3
- or a, d
- ld (iy+1), a
- ld (iy+0), l
- lea iy, iy+2
- inc c
-.Lnb_y1:
- ld a, (_w_eey)
- cp a, b
- jr z, .Lnb_z0
- ld a, h
- add a, 4
- and a, 0x7C
- ld d, a
- ld a, h
- and a, 3
- or a, d
- ld (iy+1), a
- ld (iy+0), l
- lea iy, iy+2
- inc c
-.Lnb_z0:
- ld a, h
- and a, 3
- rlca
- rlca
- rlca
- ld b, a
- ld a, l
- rlca
- rlca
- rlca
- and a, 7
- or a, b
- ld b, a ; z
- ld a, (_w_esz)
- cp a, b
- jr z, .Lnb_z1
- ld a, b
- dec a
- and a, 31
- call nb_z
- inc c
-.Lnb_z1:
- ld a, (_w_eez)
- cp a, b
- jr z, .Lnb_x0
- ld a, b
- inc a
- and a, 31
- call nb_z
- inc c
-.Lnb_x0:
- ld a, l
- and a, 31
- ld b, a ; x
- ld a, (_w_esx)
- cp a, b
- jr z, .Lnb_x1
- ld a, b
- dec a
- and a, 31
- ld d, a
- ld a, l
- and a, 0xE0
- or a, d
- ld (iy+0), a
- ld (iy+1), h
- lea iy, iy+2
- inc c
-.Lnb_x1:
- ld a, (_w_eex)
- cp a, b
- jr z, .Lnb_done
- ld a, b
- inc a
- and a, 31
- ld d, a
- ld a, l
- and a, 0xE0
- or a, d
- ld (iy+0), a
- ld (iy+1), h
- inc c
-.Lnb_done:
- ld a, c
- ld (_w_nbr_n), a
- ret
-
-; z' in a: lo = (z' & 7) << 5 | x, hi = y << 2 | z' >> 3, to (iy), iy on (b, c, h, l kept)
-nb_z:
- ld e, a
- and a, 7
- rrca
- rrca
- rrca
- ld d, a
- ld a, l
- and a, 31
- or a, d
- ld (iy+0), a
- ld a, e
- rrca
- rrca
- rrca
- and a, 3
- ld d, a
- ld a, h
- and a, 0x7C
- or a, d
- ld (iy+1), a
- lea iy, iy+2
- ret
-
-; ------------------------------------------------------------------------------------------------- w_expose
-; void w_expose(void): a window column's cells with a face to draw, levels xp_k0 to xp_k1 - 1 up from the window's
-; bottom (from the cell xp_p, slot xp_ys), into xp_out (4 bytes, bit y & 31, the other levels' kept); a face shows unless what is next to it is opaque, the same block or
-; both are liquids; plants and torches always; past the window: below it as if solid, above air; the columns
-; around it at xp_ox0.. (bits of xp_sides: 1 -x, 2 +x, 4 -z, 8 +z inside the window) (world.c)
- .section .text._w_expose
+ .set _w_neighbours, t__w_neighbours
.global _w_expose
- .type _w_expose, @function
- .equ M_CROSS, 2
- .equ M_TORCH, 6
- .equ BF_OPAQUE, 1
- .equ BF_LIQUID, 8
-_w_expose:
- push ix
- ld ix, (_xp_p)
- ld a, (_xp_k0)
- ld (xp_k), a
- ld hl, (_xp_out) ; (the column's other levels kept)
- ld de, xp_m
- ld bc, 4
- ldir
-.Lxp_cell:
- ; (this level's bit cleared first)
- call xp_bit
- cpl
- and a, (hl)
- ld (hl), a
- ld a, (_xp_ys)
- cp a, 31
- ld de, 1024
- jr nz, .Lxp_up
- ld de, -31 * 1024
-.Lxp_up:
- lea hl, ix+0
- add hl, de
- ld (xp_up), hl
- ld a, (ix+0)
- or a, a
- jp z, .Lxp_next
- ld (xp_b), a
- ld de, 0
- ld e, a
- ld hl, _blk_model
- add hl, de
- ld a, (hl)
- cp a, M_CROSS
- jp z, .Lxp_show
- cp a, M_TORCH
- jp z, .Lxp_show
- ld a, (xp_k)
- cp a, 31
- jp z, .Lxp_show
- ld hl, _blk_flags
- add hl, de
- ld a, (hl)
- and a, BF_LIQUID
- ld (xp_liq), a
- ld hl, (xp_up)
- call xp_shows
- jp nz, .Lxp_show
- ld a, (xp_k)
- or a, a
- jr z, .Lxp_sides
- ld a, (_xp_ys)
- or a, a
- ld de, -1024
- jr nz, .Lxp_dn
- ld de, 31 * 1024
-.Lxp_dn:
- lea hl, ix+0
- add hl, de
- call xp_shows
- jp nz, .Lxp_show
-.Lxp_sides:
- ld a, (_xp_sides)
- bit 2, a
- jr z, .Lxp_s1
- ld de, (_xp_oz0)
- lea hl, ix+0
- add hl, de
- call xp_shows
- jp nz, .Lxp_show
-.Lxp_s1:
- ld a, (_xp_sides)
- bit 3, a
- jr z, .Lxp_s2
- ld de, (_xp_oz1)
- lea hl, ix+0
- add hl, de
- call xp_shows
- jp nz, .Lxp_show
-.Lxp_s2:
- ld a, (_xp_sides)
- bit 0, a
- jr z, .Lxp_s3
- ld de, (_xp_ox0)
- lea hl, ix+0
- add hl, de
- call xp_shows
- jp nz, .Lxp_show
-.Lxp_s3:
- ld a, (_xp_sides)
- bit 1, a
- jr z, .Lxp_next
- ld de, (_xp_ox1)
- lea hl, ix+0
- add hl, de
- call xp_shows
- jr z, .Lxp_next
-.Lxp_show:
- call xp_bit
- or a, (hl)
- ld (hl), a
-.Lxp_next:
- ld ix, (xp_up)
- ld a, (_xp_ys)
- inc a
- and a, 31
- ld (_xp_ys), a
- ld a, (xp_k)
- inc a
- ld (xp_k), a
- ld hl, _xp_k1
- cp a, (hl)
- jp nz, .Lxp_cell
- ld hl, xp_m
- ld de, (_xp_out)
- ld bc, 4
- ldir
- pop ix
- ret
-
-; hl: xp_m's byte of slot xp_ys, a: its bit
-xp_bit:
- ld a, (_xp_ys)
- ld c, a
- rrca
- rrca
- rrca
- and a, 3
- ld hl, xp_m
- ld de, 0
- ld e, a
- add hl, de
- ld a, c
- and a, 7
- ld b, a
- ld a, 1
- ret z
-.Lxb_sh:
- add a, a
- djnz .Lxb_sh
- ret
-
-; nz when the cell at hl shows xp_b's face next to it
-xp_shows:
- ld a, (hl)
- ld hl, xp_b
- cp a, (hl)
- ret z
- ld de, 0
- ld e, a
- ld hl, _blk_flags
- add hl, de
- ld a, (hl)
- ld c, a
- and a, BF_OPAQUE
- jr nz, .Lxs_hid
- ld a, (xp_liq)
- and a, c
- jr nz, .Lxs_hid
- or a, 1
- ret
-.Lxs_hid:
- xor a, a
- ret
-
-; ------------------------------------------------------------------------------------------------- rs_shade
-; void rs_shade(uint8_t *p, const uint8_t *row): p[i] = row[p[i]] for 256 bytes, row 256-byte aligned
- .section .text._rs_shade
+ .set _w_expose, t__w_expose
.global _rs_shade
- .type _rs_shade, @function
-_rs_shade:
- ld iy, 0
- add iy, sp
- ld de, (iy+3)
- ld hl, (iy+6)
- ld b, 128
-.Lsh_loop:
- ld a, (de)
- ld l, a
- ld a, (hl)
- ld (de), a
- inc de
- ld a, (de)
- ld l, a
- ld a, (hl)
- ld (de), a
- inc de
- djnz .Lsh_loop
- ret
+ .set _rs_shade, t__rs_shade
-; ------------------------------------------------------------------------------------------------- data
.section .data._rs_vars
+ .global r_tex
r_tex: .d24 0
+ .global r_cm
r_cm: .d24 0
-r_loop: .d24 span_plain
+ .global r_loop
+r_loop: .d24 0
+ .global r_u
r_u: .d24 0
+ .global r_v
r_v: .d24 0
+ .global r_du
r_du: .d24 0
+ .global r_dv
r_dv: .d24 0
+ .global r_end
r_end: .db 0
+ .global r_row
r_row: .d24 0
+ .global r_y
r_y: .db 0
+ .global r_yend
r_yend: .db 0
+ .global r_px0
r_px0: .db 0
+ .global r_n
r_n: .db 0
+ .global m_a
m_a: .d24 0
+ .global m_r
m_r: .d24 0
+ .global m_f
m_f: .d24 0
+ .global m_neg
m_neg: .db 0
+ .global m_t
m_t: .d24 0
.db 0
+ .global p_base
p_base: .d24 0
+ .global p_n
p_n: .db 0
+ .global e_b
e_b: .d24 0
+ .global e_yb
e_yb: .d24 0
+ .global e_r1
e_r1: .db 0
+ .global e_f
e_f: .db 0
+ .global e_q
e_q: .db 0
+ .global e_d
e_d: .d24 0
+ .global e_dx
e_dx: .d24 0
+ .global e_du
e_du: .d24 0
+ .global e_dv
e_dv: .d24 0
+ .global e_r
e_r: .d24 0
+ .global e_ymin
e_ymin: .d24 0
+ .global e_ymax
e_ymax: .d24 0
+ .global e_top
e_top: .db 0
+ .global w_l
w_l: .d24 0
+ .global w_r
w_r: .d24 0
+ .global wa
wa: .ds W_SIZE
+ .global wb
wb: .ds W_SIZE
+ .global r_hud
r_hud: .d24 0
+ .global r_rows
r_rows: .db 0
+ .global r_hudlim
r_hudlim: .db 0
+ .global r_line
r_line: .ds 320
+ .global r_line2
r_line2: .ds 320
+ .global cl_mask
cl_mask: .ds 8
+ .global r_lock
r_lock: .db 0
+ .global r_ku
r_ku: .ds 48
+ .global r_kv
r_kv: .ds 48
+ .global kt_base
kt_base: .d24 0
+ .global h_r
h_r: .d24 0
+ .global h_c
h_c: .d24 0
+ .global h_y0
h_y0: .db 0
+ .global h_rows
h_rows: .db 0
+ .global h_w
h_w: .db 0
+ .global rc_t
rc_t: .d24 0
+ .global c_h2
c_h2: .d24 0
+ .global c_Y0
c_Y0: .d24 0
+ .global c_Z0
c_Z0: .d24 0
+ .global c_V0
c_V0: .d24 0
+ .global c_cx
c_cx: .d24 0
+ .global c_xa
c_xa: .d24 0
+ .global c_xb
c_xb: .d24 0
+ .global c_ptr
c_ptr: .d24 0
+ .global c_d2
c_d2: .d24 0
+ .global c_Y
c_Y: .d24 0
+ .global c_cy
c_cy: .d24 0
+ .global c_Z
c_Z: .d24 0
+ .global c_cz
c_cz: .d24 0
+ .global c_vz
c_vz: .d24 0
+ .global c_left
c_left: .db 0
+ .global c_lj
c_lj: .db 0
+ .global c_o
c_o: .db 0
+ .global c_fog
c_fog: .db 0
+ .global c_list
c_list: .ds 64
.global _rs_jY
.global _rs_jZ
@@ -2460,18 +309,31 @@ _rs_qzs0: .db 0
_rs_vd: .db 0
_rs_xs: .db 0
_rs_zs: .db 0
+ .global q_X
q_X: .d24 0
+ .global q_Y
q_Y: .d24 0
+ .global q_Z
q_Z: .d24 0
+ .global q_V
q_V: .d24 0
+ .global q_x
q_x: .d24 0
+ .global q_z
q_z: .d24 0
+ .global q_hi
q_hi: .d24 0
+ .global q_h2
q_h2: .d24 0
+ .global q_cx
q_cx: .d24 0
+ .global q_a
q_a: .db 0
+ .global q_c
q_c: .db 0
+ .global q_xs
q_xs: .db 0
+ .global q_zs
q_zs: .db 0
.global _xp_p
.global _xp_out
@@ -2493,14 +355,20 @@ _xp_sides: .db 0
.global _xp_k1
_xp_k0: .db 0
_xp_k1: .db 32
+ .global xp_up
xp_up: .d24 0
+ .global xp_k
xp_k: .db 0
+ .global xp_b
xp_b: .db 0
+ .global xp_liq
xp_liq: .db 0
+ .global xp_m
xp_m: .ds 4
.section .rodata._rs_rtab
; 1371 << 12 / (256 + i)
+ .global r_rtab
r_rtab:
.irp i, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
.dw (1371 * 4096) / (256 + \i)
diff --git a/games/numblocks/ce/src/rsb.inc b/games/numblocks/ce/src/rsb.inc
new file mode 100644
index 00000000..48372f49
--- /dev/null
+++ b/games/numblocks/ce/src/rsb.inc
@@ -0,0 +1,2262 @@
+; The 3D view's inner loops (rs.h), position independent: run from the program (rs.s includes this) or from the
+; archive (flash/rsf.s: NBCETEX carries a copy), where code costs 2 or 3 cycles a byte to read, not 4. Relative jumps
+; only; the routines call each other through their trampolines (t_*, in RAM: rs.s); data at its RAM addresses.
+
+; walkers: an edge each side of a polygon, stepped a row at a time
+ .equ W_X, 0 ; x in 1/256 pixels
+ .equ W_U, 3 ; u, v: the texel x 65536
+ .equ W_V, 6
+ .equ W_SX, 9 ; their steps a row
+ .equ W_SU, 12
+ .equ W_SV, 15
+ .equ W_ROWS, 18 ; rows left on this edge
+ .equ W_IDX, 19 ; the vertex it goes to
+ .equ W_DIR, 20 ; 1 or n - 1
+ .equ W_SIZE, 21
+ .equ RW, 160
+b__rs_setup:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+3)
+ ld (r_tex), hl
+ ld hl, (iy+6)
+ ld (r_cm), hl
+ ; the loop (the copy run: its trampoline's)
+ ld hl, (t_span_plain + 1)
+ ld a, (iy+9)
+ or a, a
+ jr z, .Lsetup_set
+ ld hl, (t_span_alpha + 1)
+.Lsetup_set:
+ ld (r_loop), hl
+ ret
+
+; ------------------------------------------------------------------------------------------------- the span
+; de dst, a n (1..160); r_u, r_v, r_du, r_dv (u 8.8, v 4.12)
+; main set: de dst, hl the texture (h its page), bc the shade row (b its page, c the texel)
+; other set: hl u, de du, bc dv; ix v; iy points at the end's low byte
+b_span:
+ push ix
+ add a, e
+ ld (r_end), a ; the end's low byte (n <= 160)
+ ld hl, (r_u)
+ push de
+ ld de, (r_du)
+ ld bc, (r_dv)
+ exx
+ pop de
+ ld ix, (r_v)
+ ld hl, (r_tex)
+ ld bc, (r_cm)
+ ld iy, r_end
+ push hl
+ ld hl, (r_loop)
+ ex (sp), hl
+ ret ; to the loop
+
+b_span_plain:
+ ld a, (de)
+ or a, a
+ jr nz, .Lp_skip
+ exx
+ ld a, h ; u's texel column
+ exx
+ ld l, a
+ ld a, ixh ; v's row, x 16
+ and a, 0xF0
+ or a, l
+ ld l, a
+ ld c, (hl) ; the texel
+ ld a, (bc) ; shaded
+ ld (de), a
+.Lp_skip:
+ inc de
+ exx
+ add hl, de
+ add ix, bc
+ exx
+ ld a, e
+ cp a, (iy)
+ jr nz, b_span_plain
+ pop ix
+ ret
+
+b_span_alpha:
+ ld a, (de)
+ or a, a
+ jr nz, .La_skip
+ exx
+ ld a, h
+ exx
+ ld l, a
+ ld a, ixh
+ and a, 0xF0
+ or a, l
+ ld l, a
+ ld a, (hl)
+ or a, a ; see-through
+ jr z, .La_skip
+ ld c, a
+ ld a, (bc)
+ ld (de), a
+.La_skip:
+ inc de
+ exx
+ add hl, de
+ add ix, bc
+ exx
+ ld a, e
+ cp a, (iy)
+ jr nz, b_span_alpha
+ pop ix
+ ret
+
+; ------------------------------------------------------------------------------------------------- arithmetic
+; hl = hl x de >> 24: hl signed, de 0..2^24 (the low partial products left out)
+b_mulhi:
+ ld (m_a), hl
+ ld (m_r), de
+ xor a, a
+ ld (m_neg), a
+ ld a, (m_a+2)
+ bit 7, a
+ jr z, .Lmh_pos
+ ex de, hl
+ or a, a
+ sbc hl, hl
+ sbc hl, de
+ ld (m_a), hl
+ ld a, 1
+ ld (m_neg), a
+.Lmh_pos:
+ ; t = a2 r0 + a1 r1 + a0 r2
+ ld a, (m_a+2)
+ ld h, a
+ ld a, (m_r)
+ ld l, a
+ mlt hl
+ ld a, (m_a+1)
+ ld d, a
+ ld a, (m_r+1)
+ ld e, a
+ mlt de
+ add hl, de
+ ld a, (m_a)
+ ld d, a
+ ld a, (m_r+2)
+ ld e, a
+ mlt de
+ add hl, de
+ ld (m_t), hl
+ ld hl, (m_t+1) ; t >> 8 (m_t+3 is 0)
+ ; + a2 r1 + a1 r2
+ ld a, (m_a+2)
+ ld d, a
+ ld a, (m_r+1)
+ ld e, a
+ mlt de
+ add hl, de
+ ld a, (m_a+1)
+ ld d, a
+ ld a, (m_r+2)
+ ld e, a
+ mlt de
+ add hl, de
+ ; + a2 r2 << 8
+ ld a, (m_a+2)
+ ld d, a
+ ld a, (m_r+2)
+ ld e, a
+ mlt de
+ xor a, a
+ ld (m_t), a
+ ld (m_t+1), de ; (de's top byte is 0 after mlt: m_t+3 stays 0)
+ ld de, (m_t)
+ add hl, de
+ jr b_neg_if
+
+; hl = hl x a >> 8: hl signed, a 0..255
+b_mul8:
+ ld (m_f), a
+ ld (m_a), hl
+ xor a, a
+ ld (m_neg), a
+ ld a, (m_a+2)
+ bit 7, a
+ jr z, .Lm8_pos
+ ex de, hl
+ or a, a
+ sbc hl, hl
+ sbc hl, de
+ ld (m_a), hl
+ ld a, 1
+ ld (m_neg), a
+.Lm8_pos:
+ ; (s0 f >> 8) + s1 f + (s2 f << 8)
+ ld a, (m_f)
+ ld e, a
+ ld a, (m_a)
+ ld d, a
+ mlt de ; s0 f
+ ld c, d ; s0 f >> 8
+ ld a, (m_f)
+ ld e, a
+ ld a, (m_a+2)
+ ld d, a
+ mlt de ; s2 f
+ xor a, a
+ ld (m_t), a
+ ld (m_t+1), de
+ ld hl, (m_t) ; s2 f << 8
+ ld a, (m_f)
+ ld e, a
+ ld a, (m_a+1)
+ ld d, a
+ mlt de ; s1 f
+ add hl, de
+ ld de, 0
+ ld e, c
+ add hl, de
+ ; (fall through)
+b_neg_if:
+ ld a, (m_neg)
+ or a, a
+ ret z
+ ex de, hl
+ or a, a
+ sbc hl, hl
+ sbc hl, de
+ ret
+
+; hl = p + a * 12
+b_vptr:
+ ld l, a
+ ld h, 12
+ mlt hl
+ ld de, (p_base)
+ add hl, de
+ ret
+
+; ------------------------------------------------------------------------------------------------- edges
+; the walker ix's next edge that crosses a row's middle; nz: one, z: none left
+b_edge:
+ ld a, (p_n)
+ ld b, a
+b_Led_try:
+.Led_try:
+ push bc
+ ld a, (ix+W_IDX)
+ call t_vptr
+ push hl
+ pop iy ; iy: the vertex a
+ ld a, (ix+W_IDX)
+ add a, (ix+W_DIR)
+ ld hl, p_n
+ cp a, (hl)
+ jr c, .Led_in
+ sub a, (hl)
+.Led_in:
+ ld (ix+W_IDX), a
+ call t_vptr ; hl: the vertex b
+ ld (e_b), hl
+ ; r0 = (ya + 7) >> 4, r1 = (yb + 7) >> 4
+ push hl
+ pop de
+ inc de
+ inc de
+ inc de
+ ex de, hl
+ ld hl, (hl) ; yb
+ ld (e_yb), hl
+ ld de, 7
+ add hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld a, h
+ ld (e_r1), a
+ ld hl, (iy+3) ; ya
+ add hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld c, h ; r0
+ ld a, (e_r1)
+ sub a, c
+ jp z, t_Led_next
+ jp c, t_Led_next
+ ld (ix+W_ROWS), a
+ ; f = r0 * 16 + 8 - ya (0..15), x 16
+ ld a, c
+ add a, a
+ add a, a
+ add a, a
+ add a, a
+ add a, 8
+ sub a, (iy+3)
+ add a, a
+ add a, a
+ add a, a
+ add a, a
+ ld (e_f), a
+ ; d = yb - ya
+ ld hl, (e_yb)
+ ld de, (iy+3)
+ or a, a
+ sbc hl, de
+ ld (e_d), hl
+ ; the deltas, x 16
+ ld hl, (e_b)
+ ld de, (hl) ; xb
+ ex de, hl
+ ld de, (iy+0)
+ or a, a
+ sbc hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (e_dx), hl
+ ld hl, (e_b)
+ ld de, 6
+ add hl, de
+ ld de, (hl) ; ub
+ ex de, hl
+ ld de, (iy+6)
+ or a, a
+ sbc hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (e_du), hl
+ ld hl, (e_b)
+ ld de, 9
+ add hl, de
+ ld de, (hl) ; vb
+ ex de, hl
+ ld de, (iy+9)
+ or a, a
+ sbc hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (e_dv), hl
+ ld hl, (e_d)
+ ld de, 16
+ or a, a
+ sbc hl, de
+ jr c, .Led_short
+ ; d >= 16: the steps, delta x 2^28 / d >> 24
+ add hl, hl ; (d - 16) * 3
+ ld de, (e_d)
+ add hl, de
+ ld de, -16
+ add hl, de
+ ld de, (_rs_recip)
+ add hl, de
+ ld de, (hl)
+ ld (e_r), de
+ ld hl, (e_dx)
+ call t_mulhi
+ ld (ix+W_SX), hl
+ ld de, (e_r)
+ ld hl, (e_du)
+ call t_mulhi
+ ld (ix+W_SU), hl
+ ld de, (e_r)
+ ld hl, (e_dv)
+ call t_mulhi
+ ld (ix+W_SV), hl
+ ; the first row: the vertex + step x f / 16
+ ld hl, (ix+W_SX)
+ ld a, (e_f)
+ call t_mul8
+ ld (e_dx), hl
+ ld hl, (ix+W_SU)
+ ld a, (e_f)
+ call t_mul8
+ ld (e_du), hl
+ ld hl, (ix+W_SV)
+ ld a, (e_f)
+ call t_mul8
+ ld (e_dv), hl
+ jr .Led_start
+.Led_short:
+ ; d < 16 (one row): delta x f / d
+ xor a, a
+ sbc hl, hl
+ ld (ix+W_SX), hl
+ ld (ix+W_SU), hl
+ ld (ix+W_SV), hl
+ ld a, (e_d)
+ add a, a
+ add a, a
+ add a, a
+ add a, a
+ ld c, a
+ ld a, (e_f)
+ rrca
+ rrca
+ rrca
+ rrca
+ add a, c ; d * 16 + f
+ ld de, 0
+ ld e, a
+ ld hl, (_rs_ratio)
+ add hl, de
+ ld a, (hl)
+ ld (e_q), a
+ ld hl, (e_dx)
+ call t_mul8
+ ld (e_dx), hl
+ ld a, (e_q)
+ ld hl, (e_du)
+ call t_mul8
+ ld (e_du), hl
+ ld a, (e_q)
+ ld hl, (e_dv)
+ call t_mul8
+ ld (e_dv), hl
+.Led_start:
+ ; x = xa * 16 + e_dx, ...
+ ld hl, (iy+0)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (e_dx)
+ add hl, de
+ ld (ix+W_X), hl
+ ld hl, (iy+6)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (e_du)
+ add hl, de
+ ld (ix+W_U), hl
+ ld hl, (iy+9)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (e_dv)
+ add hl, de
+ ld (ix+W_V), hl
+ pop bc
+ or a, 1 ; nz: found
+ ret
+b_Led_next:
+.Led_next:
+ pop bc
+ dec b
+ jp nz, t_Led_try
+ xor a, a ; z: none
+ ret
+
+
+; de: a table of 16 steps of hl (texel x 65536 a pixel): hl x (k x 16 + 8) / 256, k 0..15
+b_ktable:
+ push de
+ push hl
+ ld a, 8
+ call t_mul8
+ ld (kt_base), hl
+ pop hl
+ ld a, 16
+ call t_mul8
+ ex de, hl ; the step
+ pop iy
+ ld hl, (kt_base)
+ ld b, 16
+.Lkt:
+ ld (iy+0), hl
+ lea iy, iy+3
+ add hl, de
+ djnz .Lkt
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_poly
+; void rs_poly(const SV *p, uint8_t n)
+b__rs_poly:
+ push ix
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+6)
+ ld (p_base), hl
+ xor a, a
+ ld (r_lock), a
+ ld a, (iy+9)
+ ld (p_n), a
+ ; the top vertex, the lowest and highest y
+ ld b, a
+ ld c, 0 ; index
+ ld ix, (p_base)
+ ld hl, (ix+3)
+ ld (e_ymin), hl
+ ld (e_ymax), hl
+ xor a, a
+ ld (e_top), a
+.Lpo_scan:
+ ld hl, (ix+3)
+ ld de, (e_ymin)
+ or a, a
+ sbc hl, de
+ jr nc, .Lpo_notmin
+ add hl, de
+ ld (e_ymin), hl
+ ld a, c
+ ld (e_top), a
+ jr .Lpo_nextv
+.Lpo_notmin:
+ add hl, de
+ ld de, (e_ymax)
+ or a, a
+ sbc hl, de
+ jr c, .Lpo_nextv
+ add hl, de
+ ld (e_ymax), hl
+.Lpo_nextv:
+ lea ix, ix+12
+ inc c
+ djnz .Lpo_scan
+ ; rows y .. yend
+ ld hl, (e_ymin)
+ ld de, 7
+ add hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld a, h
+ ld (r_y), a
+ ld hl, (e_ymax)
+ add hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld a, h
+ ld (r_yend), a
+ ld hl, r_y
+ sub a, (hl)
+ jp z, t_Lpo_done
+ jp c, t_Lpo_done
+ ; the walkers: a forwards, b backwards
+ ld a, (e_top)
+ ld (wa+W_IDX), a
+ ld (wb+W_IDX), a
+ ld a, 1
+ ld (wa+W_DIR), a
+ ld a, (p_n)
+ dec a
+ ld (wb+W_DIR), a
+ ld ix, wa
+ call t_edge
+ jp z, t_Lpo_done
+ ld ix, wb
+ call t_edge
+ jp z, t_Lpo_done
+ ; which is left: the smaller x (or step)
+ ld hl, wa
+ ld (w_l), hl
+ ld hl, wb
+ ld (w_r), hl
+ ld hl, (wa+W_X)
+ ld de, (wb+W_X)
+ or a, a
+ sbc hl, de
+ jr z, .Lpo_tie
+ add hl, hl ; (the sign to the carry)
+ jr c, .Lpo_row ; (x small: no overflow)
+ jr .Lpo_swap
+.Lpo_tie:
+ ld hl, (wa+W_SX)
+ ld de, (wb+W_SX)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr c, .Lpo_row
+ jr z, .Lpo_row
+.Lpo_swap:
+ ld hl, wb
+ ld (w_l), hl
+ ld hl, wa
+ ld (w_r), hl
+ ; the row's start in rbuf
+.Lpo_row:
+ ld a, (r_y)
+ ld l, a
+ ld h, RW
+ mlt hl
+ ld de, (_rbuf)
+ add hl, de
+ ld (r_row), hl
+b_Lpo_loop:
+.Lpo_loop:
+ ; px1 = (xr + 127) >> 8, at most 160; px0 = (xl + 127) >> 8 (x >= 0 on the screen: the high byte)
+ ld de, 127
+ ld ix, (w_r)
+ ld hl, (ix+W_X)
+ add hl, de
+ ld a, h
+ cp a, RW + 1
+ jr c, .Lpo_x1ok
+ ld a, RW
+.Lpo_x1ok:
+ ld c, a
+ ld ix, (w_l)
+ ld hl, (ix+W_X)
+ add hl, de
+ ld a, c
+ sub a, h
+ jp z, t_Lpo_step
+ jp c, t_Lpo_step
+ ld (r_n), a
+ ld a, h
+ ld (r_px0), a
+ ld a, (r_lock)
+ or a, a
+ jp nz, t_Lpo_locked
+ ld ix, (w_r)
+ ; w = (xr - xl) >> 4: du, dv across it (0 under a pixel)
+ ld hl, (ix+W_X)
+ ld ix, (w_l)
+ ld de, (ix+W_X)
+ or a, a
+ sbc hl, de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld hl, (m_t+1) ; (xr - xl) >> 4 (m_t+3 is 0)
+ ld de, 16
+ or a, a
+ sbc hl, de
+ jr nc, .Lpo_wide
+ or a, a
+ sbc hl, hl
+ ld (r_du), hl
+ ld (r_dv), hl
+ jp t_Lpo_grad
+.Lpo_wide:
+ push hl
+ ld de, 48
+ or a, a
+ sbc hl, de ; (w >= 64: four pixels or more, the gradient kept for the rows below)
+ pop hl
+ jr c, .Lpo_nolock
+ ld a, 1
+ ld (r_lock), a
+.Lpo_nolock:
+ push hl ; (w - 16) * 3
+ pop de
+ add hl, hl
+ add hl, de
+ ld de, (_rs_recip)
+ add hl, de
+ ld de, (hl)
+ ld (e_r), de
+ ld ix, (w_r)
+ ld hl, (ix+W_U)
+ ld ix, (w_l)
+ ld de, (ix+W_U)
+ or a, a
+ sbc hl, de
+ ld de, (e_r)
+ call t_mulhi
+ ld (r_du), hl
+ ld ix, (w_r)
+ ld hl, (ix+W_V)
+ ld ix, (w_l)
+ ld de, (ix+W_V)
+ or a, a
+ sbc hl, de
+ ld de, (e_r)
+ call t_mulhi
+ ld (r_dv), hl
+ ld a, (r_lock)
+ or a, a
+ jr z, .Lpo_grad
+ ; locked: k/16 + 1/32 of a pixel's step, k 0..15, to step from the edge to a pixel's middle
+ ld hl, (r_du)
+ ld de, r_ku
+ call t_ktable
+ ld hl, (r_dv)
+ ld de, r_kv
+ call t_ktable
+ ; the steps in the span's units: du >> 8, dv >> 4 (kept for the rows below)
+ ld hl, (r_du)
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_du), hl
+ ld hl, (r_dv)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_dv), hl
+ jr .Lpo_locked
+b_Lpo_grad:
+.Lpo_grad:
+ ; frac = 128 - xl (low byte): from the edge to the first pixel's middle
+ ld ix, (w_l)
+ ld a, 128
+ sub a, (ix+W_X)
+ ld (e_q), a
+ ld hl, (r_du)
+ call t_mul8
+ ld de, (ix+W_U)
+ add hl, de
+ call t_clamp20
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_u), hl ; u >> 8: 8.8
+ ld a, (e_q)
+ ld hl, (r_dv)
+ call t_mul8
+ ld de, (ix+W_V)
+ add hl, de
+ call t_clamp20
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_v), hl ; v >> 4: 4.12
+ ld hl, (r_du)
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_du), hl ; du >> 8
+ ld hl, (r_dv)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld hl, (m_t+1)
+ ld (r_dv), hl ; dv >> 4
+ jr .Lpo_span
+b_Lpo_locked:
+.Lpo_locked:
+ ; (ix: the left walker) from the edge to the first pixel's middle: the table's step for (128 - xl) >> 4;
+ ; u >> 8 (8.8) and v >> 4 (4.12) for the span, kept to the texture
+ ld a, 128
+ sub a, (ix+W_X)
+ rrca
+ rrca
+ rrca
+ rrca
+ and a, 15
+ ld l, a
+ ld h, 3
+ mlt hl
+ push hl
+ ld de, r_ku
+ add hl, de
+ ld de, (hl)
+ ld hl, (ix+W_U)
+ add hl, de
+ ld (m_t), hl
+ ld a, (m_t+2)
+ cp a, 16
+ jr nc, .Lpo_uc
+ ld hl, (m_t+1)
+.Lpo_us:
+ ld (r_u), hl
+ pop hl
+ ld de, r_kv
+ add hl, de
+ ld de, (hl)
+ ld hl, (ix+W_V)
+ add hl, de
+ ld (m_t), hl
+ ld a, (m_t+2)
+ cp a, 16
+ jr nc, .Lpo_vc
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld hl, (m_t+1)
+.Lpo_vs:
+ ld (r_v), hl
+ jr .Lpo_span
+.Lpo_uc:
+ rla ; (past the texture: below its start, or past its end)
+ ld hl, 0x0FFF
+ jr nc, .Lpo_us
+ ld hl, 0
+ jr .Lpo_us
+.Lpo_vc:
+ rla
+ ld hl, 0xFFFF
+ jr nc, .Lpo_vs
+ ld hl, 0
+ jr .Lpo_vs
+.Lpo_span:
+ ld a, (r_px0)
+ ld de, 0
+ ld e, a
+ ld hl, (r_row)
+ add hl, de
+ ex de, hl
+ ld a, (r_n)
+ call t_span
+b_Lpo_step:
+.Lpo_step:
+ ld hl, r_y
+ inc (hl)
+ ld a, (r_yend)
+ cp a, (hl)
+ jr z, .Lpo_done
+ ld hl, (r_row)
+ ld de, RW
+ add hl, de
+ ld (r_row), hl
+ ld ix, (w_l)
+ call t_wstep
+ ld ix, (w_r)
+ ld a, (r_lock)
+ or a, a
+ jr z, .Lpo_rboth
+ call t_wstep_x ; (locked: the right edge's u and v no longer needed)
+ jp t_Lpo_loop
+.Lpo_rboth:
+ call t_wstep
+ jp t_Lpo_loop
+b_Lpo_done:
+.Lpo_done:
+ pop ix
+ ret
+
+; the walker ix a row on; its next edge when this one is done; nz: going on
+b_wstep:
+ ld hl, (ix+W_X)
+ ld de, (ix+W_SX)
+ add hl, de
+ ld (ix+W_X), hl
+ ld hl, (ix+W_U)
+ ld de, (ix+W_SU)
+ add hl, de
+ ld (ix+W_U), hl
+ ld hl, (ix+W_V)
+ ld de, (ix+W_SV)
+ add hl, de
+ ld (ix+W_V), hl
+ dec (ix+W_ROWS)
+ ret nz
+ call t_edge
+ ret nz
+ pop hl ; (none left: the polygon is done)
+ jr .Lpo_done
+
+; the same, x alone
+b_wstep_x:
+ ld hl, (ix+W_X)
+ ld de, (ix+W_SX)
+ add hl, de
+ ld (ix+W_X), hl
+ dec (ix+W_ROWS)
+ ret nz
+ call t_edge
+ ret nz
+ pop hl
+ jr .Lpo_done
+
+; hl into 0..0xFFFFF
+b_clamp20:
+ ld (m_t), hl
+ ld a, (m_t+2)
+ bit 7, a
+ jr nz, .Lcl_zero
+ cp a, 0x10
+ ret c
+ ld hl, 0xFFFFF
+ ret
+.Lcl_zero:
+ or a, a
+ sbc hl, hl
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_proj
+; void rs_proj(int x, int y, int z, int *out)
+; z shifted up until its top bit falls out (c shifts): the next 8 bits pick t = 1371 << 12 / (256 + i);
+; x and y shifted up c - 3 (the top 16 bits kept), times t, >> 16: the offset from the middle in 1/16 pixels
+b__rs_proj:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+9) ; z
+ call t_pr_z
+ ld hl, (iy+3)
+ call t_pr_mul
+ ld de, 1280
+ add hl, de
+ push hl
+ ld hl, (iy+6)
+ call t_pr_mul
+ ex de, hl
+ ld hl, 960
+ or a, a
+ sbc hl, de
+ ld iy, (iy+12)
+ ld (iy+3), hl
+ pop hl
+ ld (iy+0), hl
+ ret
+
+; m_r = t, m_f = the shift, for z (hl)
+b_pr_z:
+ ld b, 0
+.Lpr_norm:
+ add hl, hl
+ jr c, .Lpr_found
+ inc b
+ jr .Lpr_norm
+.Lpr_found:
+ ld (m_t), hl
+ ld a, (m_t+2) ; the index: the 8 bits after the top one
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ ld de, r_rtab
+ add hl, de
+ ld de, (hl)
+ ld (m_r), de ; t
+ ld a, b
+ sub a, 3
+ ld (m_f), a ; the shift
+ ret
+
+; hl = ((hl << m_f), its top 16 bits) x t >> 16, the sign apart
+b_pr_mul:
+ ld a, (m_f)
+ or a, a
+ jr z, .Lpm_shifted
+ ld b, a
+.Lpm_shift:
+ add hl, hl
+ djnz .Lpm_shift
+.Lpm_shifted:
+ ld (m_a), hl
+ xor a, a
+ ld (m_neg), a
+ ld a, (m_a+2)
+ bit 7, a
+ jr z, .Lpm_pos
+ ex de, hl
+ or a, a
+ sbc hl, hl
+ sbc hl, de
+ ld (m_a), hl
+ ld a, 1
+ ld (m_neg), a
+.Lpm_pos:
+ ; (ah al) x (th tl) >> 16 = ah th + (ah tl + al th + (al tl >> 8)) >> 8, a = m_a+1, m_a+2
+ ld a, (m_a+1)
+ ld h, a
+ ld a, (m_r)
+ ld l, a
+ mlt hl ; al tl
+ ld a, h
+ ld de, 0
+ ld e, a
+ push de
+ ld a, (m_a+2)
+ ld h, a
+ ld a, (m_r)
+ ld l, a
+ mlt hl ; ah tl
+ ld a, (m_a+1)
+ ld d, a
+ ld a, (m_r+1)
+ ld e, a
+ mlt de ; al th
+ add hl, de
+ pop de
+ add hl, de
+ ld (m_t), hl
+ ld hl, (m_t+1) ; >> 8
+ ld a, (m_a+2)
+ ld d, a
+ ld a, (m_r+1)
+ ld e, a
+ mlt de ; ah th
+ add hl, de
+ jp t_neg_if
+
+; ------------------------------------------------------------------------------------------------- rs_hidden
+; uint8_t rs_hidden(int x, int y, int z, int r)
+; 1 when the pixels around (x, y, z)'s projection, as far as r's projection and a pixel more each way, are all
+; drawn already (or off the screen)
+b__rs_hidden:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+9)
+ call t_pr_z
+ ld hl, (iy+12)
+ call t_pr_mul
+ ld de, 16
+ add hl, de
+ ld (h_r), hl
+ ld hl, (iy+6)
+ call t_pr_mul
+ ex de, hl
+ ld hl, 960
+ or a, a
+ sbc hl, de
+ ld c, 119
+ call t_h_range
+ jr c, .Lh_yes
+ ld a, b
+ ld (h_y0), a
+ ld a, c
+ ld (h_rows), a
+ ld hl, (iy+3)
+ call t_pr_mul
+ ld de, 1280
+ add hl, de
+ ld c, RW - 1
+ call t_h_range
+ jr c, .Lh_yes
+ ld a, c
+ ld (h_w), a
+ ld a, (h_y0)
+ ld h, a
+ ld l, RW
+ mlt hl
+ ld de, (_rbuf)
+ add hl, de
+ ld de, 0
+ ld e, b
+ add hl, de
+.Lh_row:
+ push hl
+ ld bc, 0
+ ld a, (h_w)
+ ld c, a
+ xor a, a
+ cpir
+ pop hl
+ jr z, .Lh_no
+ ld de, RW
+ add hl, de
+ ld a, (h_rows)
+ dec a
+ ld (h_rows), a
+ jr nz, .Lh_row
+.Lh_yes:
+ ld a, 1
+ ret
+.Lh_no:
+ xor a, a
+ ret
+
+; hl the middle (1/16 pixels), c the last pixel: b the first pixel of (hl - h_r, hl + h_r), c how many; carry: none
+b_h_range:
+ ld (h_c), hl
+ ld de, (h_r)
+ or a, a
+ sbc hl, de
+ call t_h_px
+ jr nc, .Lhr_lo
+ xor a, a ; (from 0)
+.Lhr_lo:
+ cp a, c
+ jr z, .Lhr_lok
+ jr nc, .Lhr_off ; (past the last)
+.Lhr_lok:
+ ld b, a
+ ld hl, (h_c)
+ ld de, (h_r)
+ add hl, de
+ call t_h_px
+ ret c ; (before the first)
+ cp a, c
+ jr c, .Lhr_hok
+ ld a, c
+.Lhr_hok:
+ sub a, b
+ inc a
+ ld c, a
+ or a, a
+ ret
+.Lhr_off:
+ scf
+ ret
+
+; a = hl >> 4 (hl in 1/16 pixels, |hl| < 2^19), 255 past 255; carry: hl < 0
+b_h_px:
+ add hl, hl
+ ret c
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (m_t), hl
+ ld a, (m_t+2)
+ or a, a
+ ld a, (m_t+1)
+ ret z
+ ld a, 255
+ or a, a
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_expand
+; void rs_expand(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud)
+; 120 rows of 160 to 240 rows of 320; 0 is the row's sky colour; from screen row 180 on, the HUD's layer
+; (187 wide at x 67, 0 see-through) over both rows, the rows made in a buffer first (nothing flickers)
+ .equ HUD_X, 67
+ .equ HUD_W, 187
+b__rs_expand:
+ push ix
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+15)
+ ld (r_hud), hl
+ ld a, 31 ; (rows left under 31: under the HUD; none without one)
+ add hl, de
+ or a, a
+ sbc hl, de
+ jr nz, .Lex_withhud
+ xor a, a
+.Lex_withhud:
+ ld (r_hudlim), a
+ ld hl, (iy+6) ; src
+ ld de, (iy+9) ; dst
+ ld ix, (iy+12) ; sky
+ ld a, 120
+ ld (r_rows), a
+ ld iy, r_hudlim
+.Lex_row:
+ ld a, (r_rows)
+ cp a, (iy)
+ jr c, .Lex_hudrow
+ ld c, (ix+0)
+ ld b, RW / 2
+ call t_r_double
+ push hl
+ ld hl, -320
+ add hl, de
+ ld bc, 320
+ ldir
+ pop hl
+.Lex_next:
+ inc ix
+ ld a, (r_rows)
+ dec a
+ ld (r_rows), a
+ jr nz, .Lex_row
+ pop ix
+ ret
+
+.Lex_hudrow:
+ push de
+ ld de, r_line
+ ld c, (ix+0)
+ ld b, RW / 2
+ call t_r_double
+ push hl
+ ld hl, r_line
+ ld de, r_line2
+ ld bc, 320
+ ldir
+ ld de, r_line + HUD_X
+ ld hl, (r_hud)
+ ld b, HUD_W
+ call t_r_overlay
+ ld de, r_line2 + HUD_X
+ ld b, HUD_W
+ call t_r_overlay
+ ld (r_hud), hl
+ pop hl
+ pop de
+ push hl
+ ld hl, r_line
+ ld bc, 640
+ ldir
+ pop hl
+ jr .Lex_next
+
+; b pairs of pixels from hl doubled to de (0: c, the row's sky); hl and de after them (flash/rsf.s has it too)
+b_r_double:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr nz, .Lrd_set
+ ld a, c
+.Lrd_set:
+ ld (de), a
+ inc de
+ ld (de), a
+ inc de
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr nz, .Lrd_set2
+ ld a, c
+.Lrd_set2:
+ ld (de), a
+ inc de
+ ld (de), a
+ inc de
+ djnz b_r_double
+ ret
+
+; b pixels of the layer (hl) over de, 0 see-through; hl and de after them
+b_r_overlay:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr z, .Lov_skip
+ ld (de), a
+.Lov_skip:
+ inc de
+ djnz b_r_overlay
+ ret
+
+
+; ------------------------------------------------------------------------------------------------- rs_cells
+; uint8_t rs_cells(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out)
+; a column's cells with a face (mask: 32 bits, bit y & 31): from slot up (0, 1, .. up - 1 levels above), then
+; down (-1, -2, .. -down): the offsets of those set, in that order; how many
+b__rs_cells:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld iy, (ix+18) ; out
+ ld hl, (ix+6)
+ ld de, cl_mask
+ ld bc, 4
+ ldir ; (the mask, twice over: no wrapping to mind)
+ ld hl, (ix+6)
+ ld bc, 4
+ ldir
+ ld c, 0 ; how many
+ ; up: bit slot onwards
+ ld a, (ix+12)
+ or a, a
+ jr z, .Lcl_down
+ ld b, a ; levels
+ ld a, (ix+9)
+ call t_cl_at ; hl: the byte, e: the bit
+ ld d, 0 ; offset
+.Lcl_up:
+ ld a, (hl)
+ and a, e
+ jr z, .Lcl_up_no
+ ld (iy), d
+ inc iy
+ inc c
+.Lcl_up_no:
+ inc d
+ rlc e
+ jr nc, .Lcl_up_same
+ inc hl
+.Lcl_up_same:
+ djnz .Lcl_up
+.Lcl_down:
+ ld a, (ix+15)
+ or a, a
+ jr z, .Lcl_done
+ ld b, a
+ ld a, (ix+9)
+ add a, 31 ; slot - 1 (+ 32)
+ call t_cl_at
+ ld d, -1
+.Lcl_dn:
+ ld a, (hl)
+ and a, e
+ jr z, .Lcl_dn_no
+ ld (iy), d
+ inc iy
+ inc c
+.Lcl_dn_no:
+ dec d
+ rrc e
+ jr nc, .Lcl_dn_same
+ dec hl
+.Lcl_dn_same:
+ djnz .Lcl_dn
+.Lcl_done:
+ ld a, c
+ pop ix
+ ret
+
+; a: a bit 0..63 of cl_mask: hl its byte, e its bit (bc kept)
+b_cl_at:
+ push af
+ rrca
+ rrca
+ rrca
+ and a, 7
+ ld hl, cl_mask
+ ld de, 0
+ ld e, a
+ add hl, de
+ pop af
+ and a, 7
+ ld e, 1
+ ret z
+.Lcla_shift:
+ rlc e
+ dec a
+ jr nz, .Lcla_shift
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_column
+; void rs_column(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx)
+; a column's cells with a face (rs_cells' order), each in the view (distance, sides, top and bottom) and not
+; hidden by what is drawn already: rs_cell(Y, Z, fog, j + 32, its y slot)
+b__rs_column:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld hl, (ix+9)
+ ld (c_h2), hl
+ ld hl, (ix+12)
+ ld (c_Y0), hl
+ ld hl, (ix+15)
+ ld (c_Z0), hl
+ ld hl, (ix+18)
+ ld de, (_rs_vzk)
+ add hl, de
+ ld (c_V0), hl ; (V0 + vz0 + KY)
+ ld hl, (ix+21)
+ ld (c_cx), hl
+ ld de, (_rs_KX)
+ or a, a
+ sbc hl, de
+ ld (c_xa), hl ; cx - KX
+ add hl, de
+ add hl, de
+ ld (c_xb), hl ; cx + KX
+ ld hl, c_list
+ push hl
+ ld a, (_rs_down)
+ ld l, a
+ push hl
+ ld a, (_rs_up)
+ ld l, a
+ push hl
+ ld a, (_rs_eys)
+ ld l, a
+ push hl
+ ld hl, (ix+6)
+ push hl
+ call t__rs_cells
+ pop hl
+ pop hl
+ pop hl
+ pop hl
+ pop hl
+ ld (c_left), a
+ ld hl, c_list
+ ld (c_ptr), hl
+b_Lco_next:
+.Lco_next:
+ ld a, (c_left)
+ or a, a
+ jp z, t_Lco_done
+ dec a
+ ld (c_left), a
+ ld hl, (c_ptr)
+ ld a, (hl)
+ inc hl
+ ld (c_ptr), hl
+ ld (c_lj), a
+ ld hl, _rs_jbase
+ add a, (hl)
+ add a, 32
+ ld (c_o), a
+ ; d2 = h2 + j^2, past the view: passed by
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ ld de, _rs_jS
+ add hl, de
+ ld de, 0
+ ld e, (hl)
+ inc hl
+ ld d, (hl)
+ ld hl, (c_h2)
+ add hl, de
+ ld (c_d2), hl
+ ex de, hl
+ ld hl, (_rs_vlim)
+ or a, a
+ sbc hl, de
+ jr c, .Lco_next
+ ; Y, Z, V from the tables (3 bytes each)
+ ld a, (c_o)
+ ld l, a
+ ld h, 3
+ mlt hl
+ push hl
+ ld de, _rs_jY
+ add hl, de
+ ld de, (hl)
+ ld hl, (c_Y0)
+ add hl, de
+ ld (c_Y), hl
+ ld de, (_rs_cy0)
+ add hl, de
+ ld (c_cy), hl
+ pop hl
+ push hl
+ ld de, _rs_jZ
+ add hl, de
+ ld de, (hl)
+ ld hl, (c_Z0)
+ add hl, de
+ ld (c_Z), hl
+ ld de, (_rs_cz0)
+ add hl, de
+ ld (c_cz), hl
+ pop hl
+ ld de, _rs_jV
+ add hl, de
+ ld de, (hl)
+ ld hl, (c_V0)
+ add hl, de
+ ld (c_vz), hl
+ ; the view: cz >= -R, |cx| <= cz + KX, |cy| <= vz
+ ld hl, (c_cz)
+ ld de, (_rs_R)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lco_next
+ ld hl, (c_cz)
+ ld de, (c_xa)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lco_next
+ ld hl, (c_cz)
+ ld de, (c_xb)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lco_next
+ ld hl, (c_vz)
+ ld de, (c_cy)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lco_next
+ ld hl, (c_vz)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lco_next
+ ; the fog
+ ld hl, (c_d2)
+ ld de, _rs_fog
+ add hl, de
+ ld a, (hl)
+ cp a, 9
+ jp nc, t_Lco_next
+ ld (c_fog), a
+ ; hidden? (far enough to tell: cz > 2R)
+ ld hl, (_rs_R)
+ add hl, hl
+ ld de, (c_cz)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lco_draw
+ ld hl, (_rs_R)
+ push hl
+ push de
+ ld hl, (c_cy)
+ push hl
+ ld hl, (c_cx)
+ push hl
+ call t__rs_hidden
+ pop hl
+ pop hl
+ pop hl
+ pop hl
+ or a, a
+ jp nz, t_Lco_next
+.Lco_draw:
+ ld a, (c_lj)
+ ld hl, _rs_eys
+ add a, (hl)
+ and a, 31
+ ld l, a
+ push hl
+ ld a, (c_o)
+ ld l, a
+ push hl
+ ld a, (c_fog)
+ ld l, a
+ push hl
+ ld hl, (c_Z)
+ push hl
+ ld hl, (c_Y)
+ push hl
+ call _rs_cell
+ pop hl
+ pop hl
+ pop hl
+ pop hl
+ pop hl
+ jp t_Lco_next
+b_Lco_done:
+.Lco_done:
+ pop ix
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_quad
+; void rs_quad(void)
+; a quadrant's columns (rs_q*), rows out from the eye along x, columns out along z, near to far: those in the
+; window, in the view's distance and sides and with a face, to rs_column (rs_x, rs_z, rs_ci, rs_X0, rs_xs, rs_zs:
+; the column)
+b__rs_quad:
+ push ix
+ xor a, a
+ ld (q_a), a
+ ld a, (_rs_qxs0)
+ ld (q_xs), a
+ ld hl, (_rs_qx0)
+ ld (q_x), hl
+b_Lq_row:
+.Lq_row:
+ ; x in the window?
+ ld hl, (q_x)
+ ld de, (_rs_wx0)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lq_done
+ ld hl, (q_x)
+ ld de, (_rs_wx1)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp nc, t_Lq_done
+ ; the row's first column
+ ld hl, (_rs_qXi)
+ ld (q_X), hl
+ ld hl, (_rs_qYi)
+ ld (q_Y), hl
+ ld hl, (_rs_qZi)
+ ld (q_Z), hl
+ ld hl, (_rs_qVi)
+ ld (q_V), hl
+ ld a, (_rs_qzs0)
+ ld (q_zs), a
+ ld hl, (_rs_qz0)
+ ld (q_z), hl
+ ld a, (q_a)
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ ld de, (_rs_qsqi)
+ add hl, de
+ ld de, 0
+ ld e, (hl)
+ inc hl
+ ld d, (hl)
+ ld (q_hi), de ; |i| squared
+ xor a, a
+ ld (q_c), a
+b_Lq_col:
+.Lq_col:
+ ; h2 = |i|^2 + |k|^2, past the view: the row's end
+ ld a, (q_c)
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ ld de, (_rs_qsqk)
+ add hl, de
+ ld de, 0
+ ld e, (hl)
+ inc hl
+ ld d, (hl)
+ ld hl, (q_hi)
+ add hl, de
+ ld (q_h2), hl
+ ex de, hl
+ ld hl, (_rs_vlim)
+ or a, a
+ sbc hl, de
+ jp c, t_Lq_rowend
+ ; z in the window?
+ ld hl, (q_z)
+ ld de, (_rs_wz0)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp c, t_Lq_rowend
+ ld hl, (q_z)
+ ld de, (_rs_wz1)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jp nc, t_Lq_rowend
+ ; the view's sides: |cx| <= Z + czm
+ ld hl, (q_X)
+ ld de, (_rs_cx0)
+ add hl, de
+ ld (q_cx), hl
+ ex de, hl
+ ld hl, (q_Z)
+ ld bc, (_rs_czm)
+ add hl, bc
+ push hl
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ pop hl
+ jr c, .Lq_next
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr c, .Lq_next
+ ; the column: ci = zs * 32 + xs, its mask
+ ld a, (q_zs)
+ ld l, a
+ ld h, 32
+ mlt hl
+ ld a, (q_xs)
+ ld de, 0
+ ld e, a
+ add hl, de
+ ld (_rs_ci), hl
+ add hl, hl
+ add hl, hl
+ ld de, _wexp
+ add hl, de
+ push hl
+ ld a, (hl)
+ inc hl
+ or a, (hl)
+ inc hl
+ or a, (hl)
+ inc hl
+ or a, (hl)
+ pop de
+ jr z, .Lq_next
+ ld hl, (q_x)
+ ld (_rs_x), hl
+ ld hl, (q_z)
+ ld (_rs_z), hl
+ ld hl, (q_X)
+ ld (_rs_X0), hl
+ ld a, (q_xs)
+ ld (_rs_xs), a
+ ld a, (q_zs)
+ ld (_rs_zs), a
+ ld hl, (q_cx)
+ push hl
+ ld hl, (q_V)
+ push hl
+ ld hl, (q_Z)
+ push hl
+ ld hl, (q_Y)
+ push hl
+ ld hl, (q_h2)
+ push hl
+ push de
+ call t__rs_column
+ ld hl, 18
+ add hl, sp
+ ld sp, hl
+.Lq_next:
+ ; the next column out along z
+ ld a, (q_c)
+ inc a
+ ld (q_c), a
+ ld hl, _rs_vd
+ cp a, (hl)
+ jr z, .Lq_cok
+ jr nc, .Lq_rowend
+.Lq_cok:
+ ld a, (_rs_qsz)
+ ld hl, q_zs
+ add a, (hl)
+ and a, 31
+ ld (hl), a
+ ld hl, (q_z)
+ ld de, (_rs_qszi)
+ add hl, de
+ ld (q_z), hl
+ ld hl, (q_X)
+ ld de, (_rs_qsXz)
+ add hl, de
+ ld (q_X), hl
+ ld hl, (q_Y)
+ ld de, (_rs_qsYz)
+ add hl, de
+ ld (q_Y), hl
+ ld hl, (q_Z)
+ ld de, (_rs_qsZz)
+ add hl, de
+ ld (q_Z), hl
+ ld hl, (q_V)
+ ld de, (_rs_qsVz)
+ add hl, de
+ ld (q_V), hl
+ jp t_Lq_col
+b_Lq_rowend:
+.Lq_rowend:
+ call _plat_poll ; (a key tapped while the picture is made)
+ ; the next row out along x
+ ld a, (q_a)
+ inc a
+ ld (q_a), a
+ ld hl, _rs_vd
+ cp a, (hl)
+ jr z, .Lq_aok
+ jr nc, .Lq_done
+.Lq_aok:
+ ld a, (_rs_qsx)
+ ld hl, q_xs
+ add a, (hl)
+ and a, 31
+ ld (hl), a
+ ld hl, (q_x)
+ ld de, (_rs_qsxi)
+ add hl, de
+ ld (q_x), hl
+ ld hl, (_rs_qXi)
+ ld de, (_rs_qsXx)
+ add hl, de
+ ld (_rs_qXi), hl
+ ld hl, (_rs_qYi)
+ ld de, (_rs_qsYx)
+ add hl, de
+ ld (_rs_qYi), hl
+ ld hl, (_rs_qZi)
+ ld de, (_rs_qsZx)
+ add hl, de
+ ld (_rs_qZi), hl
+ ld hl, (_rs_qVi)
+ ld de, (_rs_qsVx)
+ add hl, de
+ ld (_rs_qVi), hl
+ jp t_Lq_row
+b_Lq_done:
+.Lq_done:
+ pop ix
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_code
+; int rs_code(int x, int y, int z)
+; bits 0-4: the planes a polygon through (x, y, z) is cut by (z < NEAR, x > 4z, -x > 4z, y > 4z, -y > 4z); bits
+; 8-12: the view's sides it is past, a little wider than the screen's (z < NEAR, 16x > 15z, -16x > 15z, 4y > 3z,
+; -4y > 3z)
+ .equ NEAR, 819
+b__rs_code:
+ ld iy, 0
+ add iy, sp
+ ld bc, 0
+ ld hl, (iy+9)
+ ld de, NEAR
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc_front
+ ld bc, 0x0101
+.Lrc_front:
+ ld hl, (iy+9)
+ add hl, hl
+ add hl, hl
+ ld (rc_t), hl ; 4z
+ ld de, (iy+3)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc1
+ set 1, c
+.Lrc1:
+ ld hl, (rc_t)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc2
+ set 2, c
+.Lrc2:
+ ld hl, (rc_t)
+ ld de, (iy+6)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc3
+ set 3, c
+.Lrc3:
+ ld hl, (rc_t)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc4
+ set 4, c
+.Lrc4:
+ ld hl, (iy+9)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (iy+9)
+ or a, a
+ sbc hl, de
+ ld (rc_t), hl ; 15z
+ ld hl, (iy+3)
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ex de, hl ; 16x
+ ld hl, (rc_t)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc5
+ set 1, b
+.Lrc5:
+ ld hl, (rc_t)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc6
+ set 2, b
+.Lrc6:
+ ld hl, (iy+9)
+ push hl
+ pop de
+ add hl, hl
+ add hl, de
+ ld (rc_t), hl ; 3z
+ ld hl, (iy+6)
+ add hl, hl
+ add hl, hl
+ ex de, hl ; 4y
+ ld hl, (rc_t)
+ or a, a
+ sbc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc7
+ set 3, b
+.Lrc7:
+ ld hl, (rc_t)
+ or a, a
+ adc hl, de
+ add hl, hl ; (the sign to the carry)
+ jr nc, .Lrc8
+ set 4, b
+.Lrc8:
+ or a, a
+ sbc hl, hl
+ ld h, b
+ ld l, c
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_compose
+; void rs_compose(uint8_t *dst, const uint8_t *a, const uint8_t *f): dst[i] = f[a[i]], all three 256-byte aligned
+b__rs_compose:
+ ld iy, 0
+ add iy, sp
+ ld de, (iy+3)
+ ld hl, (iy+6)
+ ld bc, (iy+9)
+.Lcm_loop:
+ ld c, (hl)
+ ld a, (bc)
+ ld (de), a
+ inc l
+ inc e
+ ld c, (hl)
+ ld a, (bc)
+ ld (de), a
+ inc l
+ inc e
+ jr nz, .Lcm_loop
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_lerp
+; int rs_lerp(int a, int b, int f): a + floor((b - a) f / 256), a and b 0..255, f 0..256
+b__rs_lerp:
+ ld iy, 0
+ add iy, sp
+ ld a, (iy+10)
+ or a, a
+ jr nz, .Llp_b
+ ld a, (iy+6)
+ sub a, (iy+3)
+ jr c, .Llp_neg
+ ld h, a
+ ld l, (iy+9)
+ mlt hl
+ ld a, h
+ add a, (iy+3)
+ sbc hl, hl
+ ld l, a
+ ret
+.Llp_neg:
+ neg
+ ld h, a
+ ld l, (iy+9)
+ mlt hl
+ ld de, 255
+ add hl, de
+ ld a, (iy+3)
+ sub a, h
+ sbc hl, hl
+ ld l, a
+ ret
+.Llp_b:
+ ld hl, (iy+6)
+ ret
+
+; ------------------------------------------------------------------------------------------------- w_neighbours
+; void w_neighbours(uint16_t i): the window cell i's neighbours inside it (-Y +Y -Z +Z -X +X) into w_nbr, how many
+; into w_nbr_n (world.c; i = y << 10 | z << 5 | x, each 0..31; w_esx.. the window's first and last slots)
+b__w_neighbours:
+ ld iy, 0
+ add iy, sp
+ ld l, (iy+3)
+ ld h, (iy+4)
+ ld iy, _w_nbr
+ ld c, 0
+ ld a, h
+ rrca
+ rrca
+ and a, 0x1F
+ ld b, a ; y
+ ld a, (_w_esy)
+ cp a, b
+ jr z, .Lnb_y1
+ ld a, h
+ sub a, 4
+ and a, 0x7C
+ ld d, a
+ ld a, h
+ and a, 3
+ or a, d
+ ld (iy+1), a
+ ld (iy+0), l
+ lea iy, iy+2
+ inc c
+.Lnb_y1:
+ ld a, (_w_eey)
+ cp a, b
+ jr z, .Lnb_z0
+ ld a, h
+ add a, 4
+ and a, 0x7C
+ ld d, a
+ ld a, h
+ and a, 3
+ or a, d
+ ld (iy+1), a
+ ld (iy+0), l
+ lea iy, iy+2
+ inc c
+.Lnb_z0:
+ ld a, h
+ and a, 3
+ rlca
+ rlca
+ rlca
+ ld b, a
+ ld a, l
+ rlca
+ rlca
+ rlca
+ and a, 7
+ or a, b
+ ld b, a ; z
+ ld a, (_w_esz)
+ cp a, b
+ jr z, .Lnb_z1
+ ld a, b
+ dec a
+ and a, 31
+ call t_nb_z
+ inc c
+.Lnb_z1:
+ ld a, (_w_eez)
+ cp a, b
+ jr z, .Lnb_x0
+ ld a, b
+ inc a
+ and a, 31
+ call t_nb_z
+ inc c
+.Lnb_x0:
+ ld a, l
+ and a, 31
+ ld b, a ; x
+ ld a, (_w_esx)
+ cp a, b
+ jr z, .Lnb_x1
+ ld a, b
+ dec a
+ and a, 31
+ ld d, a
+ ld a, l
+ and a, 0xE0
+ or a, d
+ ld (iy+0), a
+ ld (iy+1), h
+ lea iy, iy+2
+ inc c
+.Lnb_x1:
+ ld a, (_w_eex)
+ cp a, b
+ jr z, .Lnb_done
+ ld a, b
+ inc a
+ and a, 31
+ ld d, a
+ ld a, l
+ and a, 0xE0
+ or a, d
+ ld (iy+0), a
+ ld (iy+1), h
+ inc c
+.Lnb_done:
+ ld a, c
+ ld (_w_nbr_n), a
+ ret
+
+; z' in a: lo = (z' & 7) << 5 | x, hi = y << 2 | z' >> 3, to (iy), iy on (b, c, h, l kept)
+b_nb_z:
+ ld e, a
+ and a, 7
+ rrca
+ rrca
+ rrca
+ ld d, a
+ ld a, l
+ and a, 31
+ or a, d
+ ld (iy+0), a
+ ld a, e
+ rrca
+ rrca
+ rrca
+ and a, 3
+ ld d, a
+ ld a, h
+ and a, 0x7C
+ or a, d
+ ld (iy+1), a
+ lea iy, iy+2
+ ret
+
+; ------------------------------------------------------------------------------------------------- w_expose
+; void w_expose(void): a window column's cells with a face to draw, levels xp_k0 to xp_k1 - 1 up from the window's
+; bottom (from the cell xp_p, slot xp_ys), into xp_out (4 bytes, bit y & 31, the other levels' kept); a face shows unless what is next to it is opaque, the same block or
+; both are liquids; plants and torches always; past the window: below it as if solid, above air; the columns
+; around it at xp_ox0.. (bits of xp_sides: 1 -x, 2 +x, 4 -z, 8 +z inside the window) (world.c)
+ .equ M_CROSS, 2
+ .equ M_TORCH, 6
+ .equ BF_OPAQUE, 1
+ .equ BF_LIQUID, 8
+b__w_expose:
+ push ix
+ ld ix, (_xp_p)
+ ld a, (_xp_k0)
+ ld (xp_k), a
+ ld hl, (_xp_out) ; (the column's other levels kept)
+ ld de, xp_m
+ ld bc, 4
+ ldir
+b_Lxp_cell:
+.Lxp_cell:
+ ; (this level's bit cleared first)
+ call t_xp_bit
+ cpl
+ and a, (hl)
+ ld (hl), a
+ ld a, (_xp_ys)
+ cp a, 31
+ ld de, 1024
+ jr nz, .Lxp_up
+ ld de, -31 * 1024
+.Lxp_up:
+ lea hl, ix+0
+ add hl, de
+ ld (xp_up), hl
+ ld a, (ix+0)
+ or a, a
+ jp z, t_Lxp_next
+ ld (xp_b), a
+ ld de, 0
+ ld e, a
+ ld hl, _blk_model
+ add hl, de
+ ld a, (hl)
+ cp a, M_CROSS
+ jp z, t_Lxp_show
+ cp a, M_TORCH
+ jp z, t_Lxp_show
+ ld a, (xp_k)
+ cp a, 31
+ jp z, t_Lxp_show
+ ld hl, _blk_flags
+ add hl, de
+ ld a, (hl)
+ and a, BF_LIQUID
+ ld (xp_liq), a
+ ld hl, (xp_up)
+ call t_xp_shows
+ jr nz, .Lxp_show
+ ld a, (xp_k)
+ or a, a
+ jr z, .Lxp_sides
+ ld a, (_xp_ys)
+ or a, a
+ ld de, -1024
+ jr nz, .Lxp_dn
+ ld de, 31 * 1024
+.Lxp_dn:
+ lea hl, ix+0
+ add hl, de
+ call t_xp_shows
+ jr nz, .Lxp_show
+.Lxp_sides:
+ ld a, (_xp_sides)
+ bit 2, a
+ jr z, .Lxp_s1
+ ld de, (_xp_oz0)
+ lea hl, ix+0
+ add hl, de
+ call t_xp_shows
+ jr nz, .Lxp_show
+.Lxp_s1:
+ ld a, (_xp_sides)
+ bit 3, a
+ jr z, .Lxp_s2
+ ld de, (_xp_oz1)
+ lea hl, ix+0
+ add hl, de
+ call t_xp_shows
+ jr nz, .Lxp_show
+.Lxp_s2:
+ ld a, (_xp_sides)
+ bit 0, a
+ jr z, .Lxp_s3
+ ld de, (_xp_ox0)
+ lea hl, ix+0
+ add hl, de
+ call t_xp_shows
+ jr nz, .Lxp_show
+.Lxp_s3:
+ ld a, (_xp_sides)
+ bit 1, a
+ jr z, .Lxp_next
+ ld de, (_xp_ox1)
+ lea hl, ix+0
+ add hl, de
+ call t_xp_shows
+ jr z, .Lxp_next
+b_Lxp_show:
+.Lxp_show:
+ call t_xp_bit
+ or a, (hl)
+ ld (hl), a
+b_Lxp_next:
+.Lxp_next:
+ ld ix, (xp_up)
+ ld a, (_xp_ys)
+ inc a
+ and a, 31
+ ld (_xp_ys), a
+ ld a, (xp_k)
+ inc a
+ ld (xp_k), a
+ ld hl, _xp_k1
+ cp a, (hl)
+ jp nz, t_Lxp_cell
+ ld hl, xp_m
+ ld de, (_xp_out)
+ ld bc, 4
+ ldir
+ pop ix
+ ret
+
+; hl: xp_m's byte of slot xp_ys, a: its bit
+b_xp_bit:
+ ld a, (_xp_ys)
+ ld c, a
+ rrca
+ rrca
+ rrca
+ and a, 3
+ ld hl, xp_m
+ ld de, 0
+ ld e, a
+ add hl, de
+ ld a, c
+ and a, 7
+ ld b, a
+ ld a, 1
+ ret z
+.Lxb_sh:
+ add a, a
+ djnz .Lxb_sh
+ ret
+
+; nz when the cell at hl shows xp_b's face next to it
+b_xp_shows:
+ ld a, (hl)
+ ld hl, xp_b
+ cp a, (hl)
+ ret z
+ ld de, 0
+ ld e, a
+ ld hl, _blk_flags
+ add hl, de
+ ld a, (hl)
+ ld c, a
+ and a, BF_OPAQUE
+ jr nz, .Lxs_hid
+ ld a, (xp_liq)
+ and a, c
+ jr nz, .Lxs_hid
+ or a, 1
+ ret
+.Lxs_hid:
+ xor a, a
+ ret
+
+; ------------------------------------------------------------------------------------------------- rs_shade
+; void rs_shade(uint8_t *p, const uint8_t *row): p[i] = row[p[i]] for 256 bytes, row 256-byte aligned
+b__rs_shade:
+ ld iy, 0
+ add iy, sp
+ ld de, (iy+3)
+ ld hl, (iy+6)
+ ld b, 128
+.Lsh_loop:
+ ld a, (de)
+ ld l, a
+ ld a, (hl)
+ ld (de), a
+ inc de
+ ld a, (de)
+ ld l, a
+ ld a, (hl)
+ ld (de), a
+ inc de
+ djnz .Lsh_loop
+ ret
+
+; ------------------------------------------------------------------------------------------------- data
diff --git a/games/numblocks/ce/src/rsb_list.inc b/games/numblocks/ce/src/rsb_list.inc
new file mode 100644
index 00000000..88d08b95
--- /dev/null
+++ b/games/numblocks/ce/src/rsb_list.inc
@@ -0,0 +1,55 @@
+; the routines of rsb.inc, in their trampolines' order (rs.s, flash/rsf.s)
+ .macro rsb_each m
+ \m _rs_setup
+ \m span
+ \m span_plain
+ \m span_alpha
+ \m mulhi
+ \m mul8
+ \m neg_if
+ \m vptr
+ \m edge
+ \m ktable
+ \m _rs_poly
+ \m wstep
+ \m wstep_x
+ \m clamp20
+ \m _rs_proj
+ \m pr_z
+ \m pr_mul
+ \m _rs_hidden
+ \m h_range
+ \m h_px
+ \m _rs_expand
+ \m r_double
+ \m r_overlay
+ \m _rs_cells
+ \m cl_at
+ \m _rs_column
+ \m _rs_quad
+ \m _rs_code
+ \m _rs_compose
+ \m _rs_lerp
+ \m _w_neighbours
+ \m nb_z
+ \m _w_expose
+ \m xp_bit
+ \m xp_shows
+ \m _rs_shade
+ \m Lco_done
+ \m Lco_next
+ \m Led_next
+ \m Led_try
+ \m Lpo_done
+ \m Lpo_grad
+ \m Lpo_locked
+ \m Lpo_loop
+ \m Lpo_step
+ \m Lq_col
+ \m Lq_done
+ \m Lq_row
+ \m Lq_rowend
+ \m Lxp_cell
+ \m Lxp_next
+ \m Lxp_show
+ .endm
diff --git a/games/numblocks/ce/tools/rsfsyms.py b/games/numblocks/ce/tools/rsfsyms.py
new file mode 100644
index 00000000..6ad6ca45
--- /dev/null
+++ b/games/numblocks/ce/tools/rsfsyms.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+"""The program's symbols, from its map, for the archive's copy of the inner loops (flash/rsf.s):
+ rsfsyms.py bin/NUMBLOCK.map > obj/rsf_syms.inc"""
+import re
+import sys
+
+seen = set()
+for m in re.finditer(r"^\s+0x([0-9a-f]+)\s+([A-Za-z_]\w*)\s*$", open(sys.argv[1]).read(), re.M):
+ addr, name = int(m.group(1), 16), m.group(2)
+ if name in seen or name.startswith("b_"):
+ continue
+ seen.add(name)
+ print(f"\t.equ\t{name}, 0x{addr:06X}")