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: the inventory opens in 140 ms, not 385

Mason3633b99c540a7a3
diff --git a/games/numblocks/ce/src/draw.c b/games/numblocks/ce/src/draw.c
index d98ae278..686abb5d 100644
--- a/games/numblocks/ce/src/draw.c
+++ b/games/numblocks/ce/src/draw.c
@@ -3,6 +3,7 @@
* HUD over it. */
#include "nb.h"
#include "draw.h"
+#include "rs.h"
Canvas cv;
const uint8_t *gfx, *texdat;
@@ -61,18 +62,36 @@ static void glyphs(const char *s, int x, int y, uint8_t c, int scale) {
}
return;
}
- for (int r = 0; r < 8; r++) {
- uint8_t *d = row_at(y + r);
- if (!d) continue;
- int gx = x;
- for (const char *p = s; *p; p++) {
- int k = *p - 32;
- if (k < 0 || k >= 96) continue;
- uint8_t bits = font_bits[k * 8 + r];
- for (int b = gx; bits; b++, bits >>= 1)
- if (bits & 1 && b >= x0 && b < x1) d[b] = c;
- gx += font_w[k];
- }
+ /* a glyph at a time, only its rows on the canvas; one across all of it a row at a time, each bit by its own test
+ * (a byte's shift is a call on the eZ80) */
+ static const uint8_t BIT8[8] = {1, 2, 4, 8, 16, 32, 64, 128};
+ int top = y - cv.oy;
+ if (top >= cv.h || top + 8 <= 0) return; /* (above or below the canvas: offscreen's other band) */
+ uint8_t r0 = (uint8_t)(top < 0 ? -top : 0), r1 = (uint8_t)(top + 8 > cv.h ? cv.h - top : 8);
+ uint8_t *row0 = cv.p + (top + r0) * cv.stride - cv.ox;
+ for (; *s; s++) {
+ uint8_t k = (uint8_t)(*s - 32);
+ if (k >= 96) continue;
+ const uint8_t *g = font_bits + k * 8 + r0;
+ uint8_t *q = row0 + x;
+ if (x >= cv.ox && x + 8 <= cv.ox + cv.w)
+ for (uint8_t r = r0; r < r1; r++, q += cv.stride) {
+ uint8_t bits = *g++;
+ if (!bits) continue;
+ if (bits & 1) q[0] = c;
+ if (bits & 2) q[1] = c;
+ if (bits & 4) q[2] = c;
+ if (bits & 8) q[3] = c;
+ if (bits & 16) q[4] = c;
+ if (bits & 32) q[5] = c;
+ if (bits & 64) q[6] = c;
+ if (bits & 128) q[7] = c;
+ }
+ else
+ for (uint8_t r = r0; r < r1; r++, q += cv.stride, g++)
+ for (int i = 0; i < 8; i++)
+ if (*g & BIT8[i] && x + i >= cv.ox && x + i < cv.ox + cv.w) q[i] = c;
+ x += font_w[k];
}
}
@@ -86,12 +105,23 @@ void text_big(int x, int y, const char *s, uint8_t c, int scale) { glyphs(s, x,
/* a picture kept as runs (tools/pack.py rle): x, y its top left; sx, sy, w, h a part of it */
static void runs(const uint8_t *base, int sw, int sh, int x, int y, int sx, int sy, int w, int h) {
- for (int r = sy; r < sy + h && r < sh; r++) {
- uint8_t *d = row_at(y + r - sy);
- if (!d) continue;
- const uint8_t *p = base + (base[r * 2] | base[r * 2 + 1] << 8) + sh * 2;
- /* (screen x = col + dx, within [a, b): the part asked for, on the canvas) */
- int dx = x - sx, a = x > x0 ? x : x0, b = x + w < x1 ? x + w : x1;
+ /* (only the rows on the canvas; screen x = col + dx, within [a, b): the part asked for, on the canvas) */
+ int top = y - sy - cv.oy, r0 = sy, r1 = sy + h < sh ? sy + h : sh;
+ if (r0 + top < 0) r0 = -top;
+ if (r1 + top > cv.h) r1 = cv.h - top;
+ int dx = x - sx, a = x > cv.ox ? x : cv.ox, b = x + w < cv.ox + cv.w ? x + w : cv.ox + cv.w;
+ if (r1 <= r0 || b <= a) return;
+ bool whole = !sx && a == x && b == x + sw;
+ uint8_t *d = cv.p + (r0 + top) * cv.stride - cv.ox;
+ const uint8_t *o = base + r0 * 2;
+ for (int r = r0; r < r1; r++, d += cv.stride, o += 2) {
+ union { unsigned u; uint8_t b[3]; } off = {0}; /* (a row's offset, little-endian: no shift) */
+ off.b[0] = o[0], off.b[1] = o[1];
+ const uint8_t *p = base + off.u + sh * 2;
+ if (whole) {
+ rs_rle(d + x, p, sw); /* (the whole row: in assembly) */
+ continue;
+ }
int col = 0;
while (col < sw) {
int n = *p++;
diff --git a/games/numblocks/ce/src/gui.c b/games/numblocks/ce/src/gui.c
index a5dfbb4e..fa1089c5 100644
--- a/games/numblocks/ce/src/gui.c
+++ b/games/numblocks/ce/src/gui.c
@@ -40,10 +40,26 @@ static void offscreen(int x, int y, int w, int h, void (*fn)(void)) {
}
/* darkens what is on the screen (Minecraft's grey veil over the world behind a screen) */
-static void veil(int level) {
- /* (a row of 256 at a time, through the shade row, in assembly) */
- const uint8_t *row = render_veil_row((uint8_t)level);
- for (unsigned i = 0; i < SCREEN_W * SCREEN_H; i += 256) rs_shade(scr + i, row);
+#ifndef NB_VEIL_IN
+#define NB_VEIL_IN 4
+#endif
+static const uint8_t *veil_row;
+static void veil_span(unsigned a, unsigned b) {
+ for (unsigned n; a < b; a += n) {
+ n = b - a > 65535 ? 65535 : b - a;
+ rs_shade(scr + a, veil_row, (uint16_t)n); /* (through the shade row, in assembly) */
+ }
+}
+/* (but for the rectangle hx, hy, hw, hh: under a panel's inside, drawn all over) */
+static void veil(int level, int hx, int hy, int hw, int hh) {
+ veil_row = render_veil_row((uint8_t)level);
+ unsigned a = 0;
+ for (int r = hy; r < hy + hh; r++) {
+ unsigned b = (unsigned)r * SCREEN_W + (unsigned)hx;
+ veil_span(a, b);
+ a = b + (unsigned)hw;
+ }
+ veil_span(a, SCREEN_W * SCREEN_H);
}
void gui_message(const char *s) {
@@ -754,7 +770,11 @@ void gui_draw(void) {
bool over_world = container || gui == GUI_TITLE || gui == GUI_PAUSE || gui == GUI_DEATH;
if (full) {
/* the world behind a screen, darkened (Minecraft's grey veil; red-brown when you died) */
- if (over_world && gui != GUI_TITLE) veil(gui == GUI_DEATH ? 12 : 14);
+ if (over_world && gui != GUI_TITLE) {
+ int in = NB_VEIL_IN; /* (a panel's corners are rounded: its inside from 4 pixels in) */
+ if (container) veil(14, panel_x + in, panel_y + in, panel_w - 2 * in, panel_h - 2 * in);
+ else veil(gui == GUI_DEATH ? 12 : 14, 0, 0, 0, 0);
+ }
if (container) strip_keep(), offscreen(panel_x - 2, panel_y - 14, panel_w + 4, panel_h + 16, draw_panel_off);
else offscreen(0, 0, SCREEN_W, SCREEN_H, draw_menu_off);
} else if (container) offscreen(panel_x - 2, panel_y - 14, panel_w + 4, panel_h + 16, draw_panel_off);
diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.c
index be547d9d..53cd8f89 100644
--- a/games/numblocks/ce/src/main.c
+++ b/games/numblocks/ce/src/main.c
@@ -204,7 +204,7 @@ int main(void) {
#include <stdio.h>
{
/* (timing, built with -DNB_BENCH: each part's ms x 10, as text in the appvar NBBENCH) */
- static char out[400];
+ static char out[700];
int n = 0;
uint32_t t = plat_ms();
#define LAP(name) \
@@ -252,6 +252,10 @@ int main(void) {
LAP("world");
render_sky_palette();
LAP("sky");
+ {
+ extern const uint8_t *rs_flash;
+ n += sprintf(out + n, "flash %p\n", (const void *)rs_flash);
+ }
#if NB_BENCH == 11
/* the window's blocks and light (to check world.c's loops in assembly against the C, -DNB_WORLD_C) */
plat_save("NBWB", wblk, WS * WS * WH / 2, 0, 0);
@@ -261,7 +265,7 @@ int main(void) {
plat_save("NBBENCH", out, (unsigned)n, 0, 0);
plat_exit();
#endif
-#if NB_BENCH == 8
+#if NB_BENCH == 8 || NB_BENCH == 15
{
/* a screen opened over the world: the creative inventory */
in_game = true, gui = GUI_NONE;
@@ -273,6 +277,9 @@ int main(void) {
LAP("open");
gui_draw();
LAP("draw");
+ for (int k = 0; k < (NB_BENCH == 15 ? 20 : 0); k++) gui_redraw(), gui_draw();
+ LAP("draws");
+ for (uint32_t t = plat_ms(); plat_ms() - t < 2000;) plat_frame(); /* (its picture taken) */
plat_save("NBBENCH", out, (unsigned)n, 0, 0);
plat_exit();
}
@@ -365,11 +372,15 @@ int main(void) {
plat_exit();
}
#endif
- for (int v = 0; v < (NB_BENCH > 1 ? 24 : 4); v++) {
- pl.yaw = (uint16_t)((v & 3) * 16384);
- pl.pitch = (v & 3) == 3 ? 6000 : 0;
+ for (int v = 0; v < (NB_BENCH >= 12 ? 96 : NB_BENCH > 1 ? 24 : 4); v++) {
+ /* (13: the fourth view over and over, a little apart) */
+ int w = NB_BENCH == 13 ? 3 : NB_BENCH == 14 ? 0 : v;
+ pl.yaw = (uint16_t)((w & 3) * 16384 + (NB_BENCH >= 13 ? (v & 1) * 64 : 0));
+ pl.pitch = (w & 3) == 3 ? 6000 : 0;
render_frame();
LAP("render");
+ extern uint32_t stat_faces, stat_cells;
+ if (v < 4) n += sprintf(out + n, " cells %lu faces %lu\n", (unsigned long)stat_cells, (unsigned long)stat_faces);
hud_draw();
LAP("hud");
compose(true, true);
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index c855ac82..fe8713a1 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -539,7 +539,11 @@ __attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int f
if (ck[17] < zmin) zmin = ck[17];
if (ck[20] < zmin) zmin = ck[20];
if (ck[23] < zmin) zmin = ck[23];
- int split = zmin < (int)(Q1 / 2) ? 4 : zmin < (int)(2 * Q1) ? 2 : 1;
+#ifndef NB_SPLIT4
+#define NB_SPLIT4 4
+#define NB_SPLIT2 2
+#endif
+ int split = zmin < (int)(Q1 / 2) ? NB_SPLIT4 : zmin < (int)(2 * Q1) ? NB_SPLIT2 : 1;
const uint8_t *bt = &blk_tex[0][0] + blk_tex_off[b];
uint8_t m = blk_model[b], tint = blk_tint[b];
if (m == M_CROSS || m == M_TORCH) {
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index 446d69db..788f359a 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -23,10 +23,26 @@ void RS(rs_compose)(uint8_t *dst, const uint8_t *a, const uint8_t *f);
int RS(rs_lerp)(int a, int b, int f);
uint8_t RS(rs_clip2)(const SV *in, uint8_t n, SV *out, uint8_t k);
uint8_t RS(rs_face)(uint8_t f);
-void RS(rs_shade)(uint8_t *p, const uint8_t *row);
+void RS(rs_shade)(uint8_t *p, const uint8_t *row, uint16_t n);
+void RS(rs_rle)(uint8_t *q, const uint8_t *p, int sw);
void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);
-void RS(rs_shade)(uint8_t *p, const uint8_t *row) {
- for (int i = 0; i < 256; i++) p[i] = row[p[i]];
+void RS(rs_shade)(uint8_t *p, const uint8_t *row, uint16_t n) {
+ for (unsigned i = 0; i < n; i++) p[i] = row[p[i]];
+}
+
+void RS(rs_rle)(uint8_t *q, const uint8_t *p, int sw) {
+ while (sw > 0) {
+ uint8_t n = *p++;
+ if (n < 128) {
+ for (int k = 0; k <= n; k++, q++, p++)
+ if (*p) *q = *p;
+ sw -= n + 1;
+ } else {
+ uint8_t c = *p++;
+ if (c) memset(q, c, (size_t)(n - 126));
+ q += n - 126, sw -= n - 126;
+ }
+ }
}
int RS(rs_lerp)(int a, int b, int f) { return a + (((b - a) * f) >> 8); }
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index 7e1ccedb..082b0404 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -33,8 +33,10 @@ extern int rs_ck[24];
extern SV rs_fs[4];
extern uint8_t rs_fclip;
uint8_t rs_face(uint8_t f);
-/* p[i] = row[p[i]] for 256 bytes, row 256-byte aligned */
-void rs_shade(uint8_t *p, const uint8_t *row);
+/* p[i] = row[p[i]] for n bytes (1..65535), row 256-byte aligned */
+void rs_shade(uint8_t *p, const uint8_t *row, uint16_t n);
+/* a picture's row of runs (tools/pack.py rle) to q (its column 0), sw pixels; 0 see-through */
+void rs_rle(uint8_t *q, const uint8_t *p, int sw);
/* a + floor((b - a) f / 256), a and b 0..255, f 0..256 */
int rs_lerp(int a, int b, int f);
/* the 160 x 120 picture to the screen at 2x, 0s as the row's sky colour (sky[y]); from screen row 180 on, the HUD's
diff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.s
index 75d73cda..c37628ca 100644
--- a/games/numblocks/ce/src/rs.s
+++ b/games/numblocks/ce/src/rs.s
@@ -60,6 +60,8 @@ _rs_build: .d24 RSF_BUILD
.set _w_expose, t__w_expose
.global _rs_shade
.set _rs_shade, t__rs_shade
+ .global _rs_rle
+ .set _rs_rle, t__rs_rle
.global _rs_clip2
.set _rs_clip2, t__rs_clip2
.global _rs_face
diff --git a/games/numblocks/ce/src/rsb.inc b/games/numblocks/ce/src/rsb.inc
index 149b928a..e35ff451 100644
--- a/games/numblocks/ce/src/rsb.inc
+++ b/games/numblocks/ce/src/rsb.inc
@@ -2244,25 +2244,27 @@ b_xp_shows:
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
+; void rs_shade(uint8_t *p, const uint8_t *row, uint16_t n): p[i] = row[p[i]] for n bytes (1..65535), row
+; 256-byte aligned
b__rs_shade:
ld iy, 0
add iy, sp
- ld de, (iy+3)
- ld hl, (iy+6)
- ld b, 128
+ ld hl, (iy+3) ; p
+ ld de, (iy+6) ; row (256-aligned: e the byte)
+ ld a, (iy+9) ; n: djnz's count the low byte, c the high (one more when the low is not 0)
+ ld c, (iy+10)
+ ld b, a
+ or a, a
+ jr z, .Lsh_loop
+ inc c
.Lsh_loop:
+ ld e, (hl)
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
+ ld (hl), a
+ inc hl
djnz .Lsh_loop
+ dec c
+ jr nz, .Lsh_loop
ret
; ------------------------------------------------------------------------------------------------- data
@@ -3043,3 +3045,74 @@ b_Lls_done:
.Lls_done:
pop ix
ret
+
+; ------------------------------------------------------------------------------------------------- rs_rle
+; void rs_rle(uint8_t *q, const uint8_t *p, int sw): a picture's row of runs (tools/pack.py rle: n < 128, n + 1
+; pixels follow; else n - 126 of the next) to q, sw pixels; 0 see-through
+b__rs_rle:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+9)
+ exx
+ ld de, (iy+3)
+ ld hl, (iy+6)
+.Lrl_run:
+ ld a, (hl)
+ inc hl
+ cp a, 128
+ jr nc, .Lrl_fill
+ inc a
+ ld b, a
+ exx
+ ld de, 0
+ ld e, a
+ or a, a
+ sbc hl, de
+ exx
+.Lrl_lit:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr z, .Lrl_skip
+ ld (de), a
+.Lrl_skip:
+ inc de
+ djnz .Lrl_lit
+ jr .Lrl_more
+.Lrl_fill:
+ sub a, 126
+ ld b, a
+ exx
+ ld de, 0
+ ld e, a
+ or a, a
+ sbc hl, de
+ exx
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr z, .Lrl_none
+.Lrl_f:
+ ld (de), a
+ inc de
+ djnz .Lrl_f
+ jr .Lrl_more
+.Lrl_none:
+ push hl
+ ex de, hl
+ ld de, 0
+ ld e, b
+ add hl, de
+ ex de, hl
+ pop hl
+.Lrl_more:
+ exx
+ ld a, h
+ or a, l
+ jr z, .Lrl_done
+ bit 7, h
+ jr nz, .Lrl_done
+ exx
+ jr .Lrl_run
+.Lrl_done:
+ ret
diff --git a/games/numblocks/ce/src/rsb_list.inc b/games/numblocks/ce/src/rsb_list.inc
index a619aeed..8656b083 100644
--- a/games/numblocks/ce/src/rsb_list.inc
+++ b/games/numblocks/ce/src/rsb_list.inc
@@ -76,4 +76,5 @@
\m Lws
\m Lws_done
\m Lwsb
+ \m _rs_rle
.endm
diff --git a/tools/ce_emu/ce_emu.c b/tools/ce_emu/ce_emu.c
index b2f7f66e..be924f10 100644
--- a/tools/ce_emu/ce_emu.c
+++ b/tools/ce_emu/ce_emu.c
@@ -139,19 +139,22 @@ static uint64_t cycles_now(void) { return sched_total_cycles(); }
uint64_t emu_ms(void) { return now_ms; }
bool emu_key_held(int group, int bit) { return group >= 0 && group < 8 && bit >= 0 && bit < 8 && held[group][bit]; }
uint32_t emu_key_presses(int group, int bit) { return group >= 0 && group < 8 && bit >= 0 && bit < 8 ? presses[group][bit] : 0; }
-/* an OS routine's time; a long one's (from 0.1 s: archiving, a garbage collection) in steps of a millisecond, the
- * scheduler's events between them (timers, the LCD, the seconds), as they go on while a calculator runs it: charged
- * at once, a second's worth left events behind the second and stopped the core (sched_second's assertion) */
+/* an OS routine's time, passed as a halted CPU passes it (CEmu's cpu_halt): up to the scheduler's next event, that
+ * event run (timers, the LCD, the seconds), and so on, the LCD's memory reads kept up with; charged at once, archiving
+ * a 19 KB appvar left a second's events behind and stopped the core (sched_second's assertion) */
static bool in_hook;
void emu_charge(uint32_t cycles) {
- if (!in_hook || cycles < 4800000) {
+ if (!in_hook) {
debug.cpuCycles += cycles;
return;
}
cpu.cycles = debug.cpuCycles;
while (cycles) {
- uint32_t step = cycles < 48000 ? cycles : 48000;
+ uint32_t next = sched_event_next_cycle(), step = next > cpu.cycles ? next - cpu.cycles : 0;
+ if (step > cycles) step = cycles;
+ if (step > 4800) step = 4800; /* (0.1 ms) */
cpu.cycles += step, cycles -= step;
+ sched_process_pending_dma(0);
sched_process_pending_events();
}
/* (what the debugger puts back after the hook: these, and the events looked at again at once) */
@@ -632,7 +635,7 @@ int main(int argc, char **argv) {
uint32_t sp = cpu.registers.SPL;
if (cpu.ADL && (sp < STACK_BOT - 0x100 || sp > STACK_TOP)) {
static bool told;
- if (!told) emu_warn("the stack pointer left the stack: %06X at %llu ms", sp, (unsigned long long)now_ms);
+ if (!told) emu_warn("the stack pointer left the stack: %06X at %llu ms (PC %06X)", sp, (unsigned long long)now_ms, pc);
told = true;
}
}