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.

Hollow Knight: installs again: 145 KB of fixed RAM, under what every calculator software since 23.2 gives apps (148928 bytes there, 153676 on 25.2), the decoders' scratch memory on the stack; no more black rectangles: a turned smooth sprite (the Crossroads' and Greenpath's big dark squares) drawn in its own shape behind the blur plane, not over its whole box, and in front out to its edges (each smooth texture's outer texels repeated, as the game's quad; one color throughout drawn as one); busy rooms drawn fast sooner (a frame's work over 34 ms, whole again under 18)

Mason Chend467e59f6eaa
diff --git a/games/hollowknight/Makefile b/games/hollowknight/Makefile
index 976fe1e1..ebee2546 100644
--- a/games/hollowknight/Makefile
+++ b/games/hollowknight/Makefile
@@ -24,8 +24,10 @@ LDFLAGS += -Wl,--gc-sections -flinker-output=nolto-rel
.PHONY: build check clean host data test
build: $(BUILD_DIR)/hollowknight.nwa
+# the RAM the calculator gives apps (data + bss) depends on its software: 153676 bytes on 25.2, 149500 on 24.10,
+# 148928 on 23.2; checked against the least
check: $(BUILD_DIR)/hollowknight.nwa
- $(Q) $(NWLINK) nwa-bin --ram-length 153676 $< $(BUILD_DIR)/hollowknight.bin
+ $(Q) $(NWLINK) nwa-bin --ram-length 148928 $< $(BUILD_DIR)/hollowknight.bin
@echo "BIN $(BUILD_DIR)/hollowknight.bin: $$(wc -c < $(BUILD_DIR)/hollowknight.bin) bytes"
$(BUILD_DIR)/data.o: src/data.S src/data.bin | $(BUILD_DIR)
diff --git a/games/hollowknight/README.md b/games/hollowknight/README.md
index 0c9ab7a9..8155dcbb 100644
--- a/games/hollowknight/README.md
+++ b/games/hollowknight/README.md
@@ -75,6 +75,7 @@ In menus, OK or EXE confirms and Back goes back. In the inventory, left and righ
* **Only the start of the game:** it ends with Hornet in Greenpath. The ways on to places further in stay shut, like Jiji's door, the lift down to the mines and the Dream Nail's paths.
* **No music or sound:** the calculator has no speaker.
* **A few things are simpler:** dust, sparks and other particles are left out, and characters and scenery are drawn a little less finely so the whole game fits.
+* **Busy rooms stay smooth:** when the calculator falls behind, the room is drawn at half the height, each line shown twice. The HUD, text and menus stay sharp.
## Credits
diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.c
index ffbc3fc1..d2d9e66d 100644
--- a/games/hollowknight/src/gfx.c
+++ b/games/hollowknight/src/gfx.c
@@ -1044,8 +1044,46 @@ static void run_turned(const Ctx *c, int y, int a, int b, int *last_key, const u
*last_key = lk, *last_t = lt;
}
-/* smooth textures: a few texels, interpolated */
-static void run_soft(const Ctx *c, int y, int a, int b, const uint32_t *tx, uint32_t crb, uint32_t cg, uint32_t ta) {
+/* a smooth texture's texel at (u, v) (16.16, less half a texel), its edges repeated beyond them, as a color */
+static uint32_t soft_edge_at(const TexRec *r, const uint32_t *tx, int32_t u, int32_t v, uint32_t crb, uint32_t cg, uint32_t ta) {
+ int W = r->w, H = r->h, cx = u >> 16, rr = v >> 16;
+ uint32_t fu = (uint32_t)(u >> 8) & 255, fv = (uint32_t)(v >> 8) & 255;
+ int c0 = cx < 0 ? 0 : cx >= W ? W - 1 : cx, c1 = cx + 1 < 0 ? 0 : cx + 1 >= W ? W - 1 : cx + 1;
+ int r0 = rr < 0 ? 0 : rr >= H ? H - 1 : rr, r1 = rr + 1 < 0 ? 0 : rr + 1 >= H ? H - 1 : rr + 1;
+ if (r->fmt == FMT_SOFT)
+ return lerp4(lerp4(tx[r0 * W + c0], tx[r0 * W + c1], fu), lerp4(tx[r1 * W + c0], tx[r1 * W + c1], fu), fv);
+ const uint8_t *al = (const uint8_t *)tx;
+ uint32_t top = al[r0 * W + c0] * (256 - fu) + al[r0 * W + c1] * fu, bot = al[r1 * W + c0] * (256 - fu) + al[r1 * W + c1] * fu;
+ uint32_t aa = ((top * (256 - fv) + bot * fv) >> 16) * ta >> 8;
+ return aa << 24 | ((crb * aa >> 8) & 0xFF00FF) | ((cg * aa >> 8) & 0xFF00);
+}
+
+/* a smooth texture's outer texels not all clear (SOFT_EDGED; most are: then nothing is drawn beyond the centers of
+ * its outer texels), all its texels the same (SOFT_FLAT: the light that fills a room, a black square): a few textures
+ * remembered */
+#define SOFT_EDGED 1
+#define SOFT_FLAT 2
+static uint16_t soft_kind_tex[32]; /* (texture + 1) */
+static uint8_t soft_kind_of[32];
+static int soft_kind(uint16_t tex, const TexRec *r, const uint32_t *tx) {
+ unsigned k = tex & 31;
+ if (soft_kind_tex[k] == tex + 1u) return soft_kind_of[k];
+ int W = r->w, H = r->h, n = W * H, kind = 0;
+ const uint8_t *al = (const uint8_t *)tx;
+ bool mono = r->fmt == FMT_SOFTA, flat = true;
+#define CLEAR(i) (mono ? !al[i] : !(tx[i] >> 24))
+ for (int x = 0; x < W && !kind; x++) kind = !CLEAR(x) || !CLEAR((H - 1) * W + x);
+ for (int y = 0; y < H && !kind; y++) kind = !CLEAR(y * W) || !CLEAR(y * W + W - 1);
+#undef CLEAR
+ for (int i = 1; i < n && flat; i++) flat = mono ? al[i] == al[0] : tx[i] == tx[0];
+ if (flat) kind |= SOFT_FLAT;
+ soft_kind_tex[k] = (uint16_t)(tex + 1u), soft_kind_of[k] = (uint8_t)kind;
+ return kind;
+}
+
+/* smooth textures: a few texels, interpolated; at the pixels whose centers are in the sprite (as the game's quad), its
+ * edges repeated beyond the outer texels' centers (a big black square of 3 x 3 texels is a square, its corners sharp) */
+static void run_soft(const Ctx *c, int y, int a, int b, const uint32_t *tx, uint32_t crb, uint32_t cg, uint32_t ta, int sk) {
const Map *m = c->m;
const TexRec *r = c->r;
int mode = c->mode, row = c->row, W = r->w, H = r->h;
@@ -1054,10 +1092,30 @@ static void run_soft(const Ctx *c, int y, int a, int b, const uint32_t *tx, uint
float fx = a + 0.5f - m->ox, fy = SY(y) - m->oy;
int32_t u0 = (int32_t)((m->iux * fx + m->iuy * fy - 0.5f) * 65536), v0 = (int32_t)((m->ivx * fx + m->ivy * fy - 0.5f) * 65536);
int32_t dux = (int32_t)(m->iux * 65536), dvx = (int32_t)(m->ivx * 65536);
- /* where all four texels are inside (the edges of these textures are clear) */
- int i0 = 0, i1 = b - a;
+ int o0 = 0, o1 = b - a;
+ bool edged = sk & SOFT_EDGED;
+ if (edged) {
+ clip_lin(u0 + 32768, dux, W << 16, b - a, &o0, &o1);
+ clip_lin(v0 + 32768, dvx, H << 16, b - a, &o0, &o1);
+ if (o0 >= o1) return;
+ if (sk & SOFT_FLAT) {
+ /* (one color throughout: no texel to mix) */
+ uint32_t s = soft_edge_at(r, tx, 0, 0, crb, cg, ta);
+ for (int x = a + o0, p = row * VIEW_W + x; x < a + o1; x++, p++) TAKE(row, x, p, s);
+ return;
+ }
+ }
+ /* where all four texels are inside: most of it, read without the edges' checks; the rest (within half a texel of
+ * the edge, clear in most of these textures) with them */
+ int i0 = o0, i1 = o1;
clip_lin(u0, dux, (W - 1) << 16, b - a, &i0, &i1);
clip_lin(v0, dvx, (H - 1) << 16, b - a, &i0, &i1);
+ if (i0 >= i1) i0 = i1 = o1;
+ for (int k = 0; k < 2 && edged; k++)
+ for (int j = k ? i1 : o0, je = k ? o1 : i0; j < je; j++) {
+ uint32_t s = soft_edge_at(r, tx, u0 + dux * j, v0 + dvx * j, crb, cg, ta);
+ if (s >> 24) TAKE(row, a + j, row * VIEW_W + a + j, s);
+ }
if (i0 >= i1) return;
u0 += dux * i0, v0 += dvx * i0;
int x = a + i0, e = a + i1, p = row * VIEW_W + a + i0;
@@ -1137,6 +1195,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
int kind = 0; /* 0 solid, 1 axis, 2 turned, 3 soft, 4 bilinear */
const uint32_t *soft = NULL;
uint32_t crb = 0, cg = 0, ta = 256;
+ int sk = 0;
BiTex bt;
if (!(in->flags & F_SOLID)) {
c.r = tex_rec(in->tex);
@@ -1146,6 +1205,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
kind = 3;
soft = soft_get(in->tex, in->tint, in->flags, inst_alpha(in));
if (!soft) return;
+ sk = soft_kind(in->tex, c.r, soft);
if (c.r->fmt == FMT_SOFTA) {
const uint8_t *t = tint_rgba(in->tint);
float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f;
@@ -1220,7 +1280,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
case 0: run_solid(&c, y, x, e); break;
case 1: run_axis(&c, y, x, e, tp, ty, vr); break;
case 2: run_turned(&c, y, x, e, &last_key, &last_t); break;
- case 3: run_soft(&c, y, x, e, soft, crb, cg, ta); break;
+ case 3: run_soft(&c, y, x, e, soft, crb, cg, ta, sk); break;
default: run_bilinear(&c, y, x, e, &bt); break;
}
x = open_from(cv, e, xe);
@@ -1324,7 +1384,9 @@ static void draw_bg_item(Item *it) {
if (s >> 24) bg_put(d16 + bx, s, mode);
}
} else {
- /* smooth: bilinear, the edges repeated */
+ /* smooth: bilinear, the edges repeated, at the pixels whose centers are in the sprite */
+ clip_lin(u + 32768, du, W << 16, i1, &i0, &i1);
+ clip_lin(v + 32768, dv, H << 16, i1, &i0, &i1);
u += du * i0, v += dv * i0;
for (int bx = bx0 + i0; bx < bx0 + i1; bx++, u += du, v += dv) {
int c = u >> 16, rr = v >> 16;
diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.h
index 6b77d2be..e816afad 100644
--- a/games/hollowknight/src/hk.h
+++ b/games/hollowknight/src/hk.h
@@ -57,8 +57,10 @@ static inline void zero_words(void *d, size_t n) {
for (; n; n -= 4) *dw++ = 0;
}
void lz_decode(const uint8_t *src, uint32_t comp, uint8_t *dst, uint32_t raw);
-/* decoders' working memory (LZMA probabilities or the tile coder's), one at a time */
-extern uint16_t g_scratch[4096] __attribute__((aligned(4)));
+/* decoders' working memory (LZMA probabilities or the tile coder's), one at a time; on the calculator, on main's
+ * stack (main.c) */
+#define SCRATCH_N 4096
+extern uint16_t *g_scratch;
float f16(uint16_t h);
const char *str_at(int id); /* the shared string table (SEC_STR) */
diff --git a/games/hollowknight/src/main.c b/games/hollowknight/src/main.c
index 7c84de21..59f332c8 100644
--- a/games/hollowknight/src/main.c
+++ b/games/hollowknight/src/main.c
@@ -17,8 +17,8 @@ uint32_t perf_updates __attribute__((used));
#define TICK_MS 20 /* the game's step (Unity's fixed step) */
#define MAX_TICKS 4 /* steps caught up before a frame is drawn */
-#define SLOW_MS 45 /* a frame's work (on average) over which the room is drawn fast (g_gfx_fast)... */
-#define QUICK_MS 20 /* ...and under which, fast, it is drawn whole again (whole, about 36) */
+#define SLOW_MS 34 /* a frame's work (on average) over which the room is drawn fast (g_gfx_fast): under 29 fps... */
+#define QUICK_MS 18 /* ...and under which, fast, it is drawn whole again (whole, under 34: not back and forth) */
#ifdef PERF_BENCH
/* timing: a few views (rooms by number), panning a little: each one's ms per frame x 100 (read by the emulator; the
@@ -26,7 +26,7 @@ uint32_t perf_updates __attribute__((used));
static const struct { int room; float x, y; } bench[] = {{0, 30, 14.11f}, {0, 186, 68}, {0, 60, 30}, {5, 52, 10}, {35, 14, 104},
{10, 22, 25}, {32, 18, 66}, {20, 25, 22}, {1, 82, 10}, {0, 30, 14}};
uint32_t perf_bench[sizeof bench / sizeof bench[0]] __attribute__((used));
-int main(void) {
+static int run(void) {
plat_begin();
hk_bin = hk_data;
#ifdef PERF_FAST
@@ -50,7 +50,7 @@ int main(void) {
/* timing: every room, the view at each of its gates and its middle, panning a little; per room: frames, ms, the
* slowest spot's ms per frame (x10) and where (read by the emulator) */
uint32_t perf_survey[NUM_ROOMS][5] __attribute__((used));
-int main(void) {
+static int run(void) {
plat_begin();
hk_bin = hk_data;
for (int r = 0; r < NUM_ROOMS; r++) {
@@ -92,7 +92,7 @@ int main(void) {
#ifndef PERF_DX
#define PERF_DX 0.15f
#endif
-int main(void) {
+static int run(void) {
plat_begin();
hk_bin = hk_data;
plat_fill(0, 0, SCREEN_W, SCREEN_H, 0);
@@ -108,7 +108,7 @@ int main(void) {
return plat_end();
}
#else
-int main(void) {
+static int run(void) {
plat_begin();
hk_bin = hk_data;
plat_fill(0, 0, SCREEN_W, SCREEN_H, 0);
@@ -149,4 +149,13 @@ int main(void) {
return plat_end();
}
#endif
+
+/* The decoders' scratch memory on the stack: the calculator gives apps 32 KB of it, of which the game uses under 5 KB,
+ * while the RAM it gives them is what its software's own variables leave (153676 bytes on 25.2, 148928 on 23.2), and
+ * the installer refuses an app that needs more. */
+int main(void) {
+ uint16_t scratch[SCRATCH_N] __attribute__((aligned(4)));
+ g_scratch = scratch;
+ return run();
+}
#endif
diff --git a/games/hollowknight/src/res.c b/games/hollowknight/src/res.c
index cc11e372..996183a4 100644
--- a/games/hollowknight/src/res.c
+++ b/games/hollowknight/src/res.c
@@ -7,7 +7,12 @@ const uint8_t *hk_bin;
const uint8_t *section(int id) { return hk_bin + rd32(hk_bin + 8 + 8 * id); }
uint32_t section_size(int id) { return rd32(hk_bin + 12 + 8 * id); }
-uint16_t g_scratch[4096] __attribute__((aligned(4)));
+#ifdef HOST
+static uint16_t scratch[SCRATCH_N] __attribute__((aligned(4)));
+uint16_t *g_scratch = scratch;
+#else
+uint16_t *g_scratch;
+#endif
#define LZ_PROBS ((CLzmaProb *)g_scratch) /* NUM_BASE_PROBS + LZMA_LIT_SIZE << (lc + lp) = 2752, lc = lp = 0 */
/* a raw LZMA stream (lc = lp = pb = 0) decoded whole: the output is its own dictionary */
diff --git a/games/hollowknight/src/save.c b/games/hollowknight/src/save.c
index 6c4c6052..e1db7182 100644
--- a/games/hollowknight/src/save.c
+++ b/games/hollowknight/src/save.c
@@ -40,7 +40,7 @@ static void save_copy(void);
bool save_game(void) {
/* (built in the decoders' scratch memory: no decoding goes on meanwhile) */
uint8_t *buf = (uint8_t *)g_scratch;
- uint32_t cap = sizeof g_scratch;
+ uint32_t cap = SCRATCH_N * 2;
SaveHead h = {SAVE_MAGIC, SAVE_VERSION, 0, 0};
uint32_t at = sizeof h;
memcpy(buf + at, &g_pd, PD_SAVED);
@@ -157,7 +157,7 @@ void save_restore(void) {
if (!len || len % 4) continue;
uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');
uint8_t *out = (uint8_t *)g_scratch;
- if (size > sizeof g_scratch) continue;
+ if (size > SCRATCH_N * 2) continue;
bool ok = true;
for (uint32_t i = 0; i < len && ok; i += 4) {
int v0 = l[i] == '=' ? 0 : b64_value(l[i]), v1 = l[i + 1] == '=' ? 0 : b64_value(l[i + 1]);
diff --git a/games/hollowknight/src/tex.c b/games/hollowknight/src/tex.c
index 84edef49..68ce6baa 100644
--- a/games/hollowknight/src/tex.c
+++ b/games/hollowknight/src/tex.c
@@ -21,7 +21,7 @@ static uint16_t frame = 1, hand;
static uint8_t block_buf[BLOCK_TILES * 128] __attribute__((aligned(4)));
static int block_tex = -1, block_first; /* (whose tiles block_buf holds) */
#define tcp g_scratch /* TC_N4 */
-_Static_assert(TC_N4 <= sizeof g_scratch / 2, "the tile decoder's probabilities: in g_scratch");
+_Static_assert(TC_N4 <= SCRATCH_N, "the tile decoder's probabilities: in g_scratch");
#define NONE_T 255
/* ---------------------------------------------------------------- the tile decoder (tilecode.h) */