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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumPlay installs with less RAM: a game uses the RAM after it (Mason363/NumPlay#45)
Mason Chen8a8c28c391bd
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 304a2a45..b14779fc 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -50,6 +50,7 @@ jobs:python3 tests/test_progress.py build/NumPlay.nwapython3 tests/test_numblocks_progress.py build/apps/NumBlocks.nwapython3 tests/test_upsilon.py build/NumPlay-Upsilon.nwa+ python3 tests/test_small_ram.py build/NumPlay.nwa- uses: actions/upload-artifact@v4with:diff --git a/Makefile b/Makefileindex cd4c6758..bac4e2fe 100644--- a/Makefile+++ b/Makefile@@ -16,7 +16,10 @@ ARM_OBJCOPY = arm-none-eabi-objcopyPY ?= python3CARGO ?= cargoB = build-# the RAM the oldest calculator software that runs apps gives them (23.2: 148928 bytes; 25.2 gives 153676)+# the RAM the oldest calculator software that runs apps gives them (23.2: 148928 bytes; 25.2 gives 153676).+# NumPlay itself takes less: a game goes on past it into the RAM the software has left (see+# tools/gen_games.py), so it installs with less too, custom builds of the software say, where a game that+# needs more says so. The check: every game fits in RAM_LIMIT.RAM_LIMIT = 148928# the calculator's space for installed apps (0x90200000 to 0x903F0000 in its flash)APP_SPACE = 2031616@@ -135,11 +138,13 @@ $(B)/%.nwa: $(ARM_OBJS) $(MODULES) $(B)/variant/%-name.o $(B)/variant/%-icon.o@if [ "$*" = NumPlay ]; then $(PY) tools/sizes.py $@ $(B)/modules; ficheck: nwa- @$(NWLINK) nwa-bin --ram-length $(UPSILON_RAM) $(B)/NumPlay-Upsilon.nwa $(B)/NumPlay-Upsilon.bin && \- echo "NumPlay-Upsilon.nwa installs as $$(wc -c < $(B)/NumPlay-Upsilon.bin) bytes; its RAM fits in $(UPSILON_RAM) bytes"- @for v in $(VARIANTS); do $(NWLINK) nwa-bin --ram-length $(RAM_LIMIT) $(B)/$$v.nwa $(B)/$$v.bin || exit 1; \+ @x=$$(cat $(B)/gen-upsilon/arena_extra); \+ $(NWLINK) nwa-bin --ram-length $$(( $(UPSILON_RAM) - x )) $(B)/NumPlay-Upsilon.nwa $(B)/NumPlay-Upsilon.bin && \+ echo "NumPlay-Upsilon.nwa installs as $$(wc -c < $(B)/NumPlay-Upsilon.bin) bytes; its RAM and any game's fit in $(UPSILON_RAM) bytes"+ @x=$$(cat $(B)/gen/arena_extra); \+ for v in $(VARIANTS); do $(NWLINK) nwa-bin --ram-length $$(( $(RAM_LIMIT) - x )) $(B)/$$v.nwa $(B)/$$v.bin || exit 1; \n=$$(wc -c < $(B)/$$v.bin); \- echo "$$v.nwa installs as $$n bytes ($$(( $(APP_SPACE) - n )) to spare); its RAM fits in $(RAM_LIMIT) bytes"; \+ echo "$$v.nwa installs as $$n bytes ($$(( $(APP_SPACE) - n )) to spare); its RAM and any game's fit in $(RAM_LIMIT) bytes"; \[ $$n -le $(APP_SPACE) ] || { echo "$$v.nwa is bigger than the calculator's app space ($(APP_SPACE) bytes)"; exit 1; }; done# ------------------------------------------------------------------ NumPlay-Upsilon.nwa@@ -170,7 +175,8 @@ $(foreach l,$(LANGS),$(B)/NumPlay-$(LANG_$(l)).nwa): $(B)/NumPlay-%.nwa: FORCE |$(eval L := $(if $(filter French,$*),fr,zh))$(PY) tools/lang.py tree $(L) $(B)/lang/$(L)$(MAKE) -C $(B)/lang/$(L) build/NumPlay.nwa NWLINK="$(NWLINK)"- $(NWLINK) nwa-bin --ram-length $(RAM_LIMIT) $(B)/lang/$(L)/build/NumPlay.nwa $(B)/lang/$(L)/build/NumPlay.bin+ $(NWLINK) nwa-bin --ram-length $$(( $(RAM_LIMIT) - $$(cat $(B)/lang/$(L)/build/gen/arena_extra) )) \+ $(B)/lang/$(L)/build/NumPlay.nwa $(B)/lang/$(L)/build/NumPlay.bin@n=$$(wc -c < $(B)/lang/$(L)/build/NumPlay.bin); echo "NumPlay-$*.nwa installs as $$n bytes ($$(( $(APP_SPACE) - n )) to spare)"; \[ $$n -le $(APP_SPACE) ] || { echo "NumPlay-$*.nwa is bigger than the calculator's app space"; exit 1; }cp $(B)/lang/$(L)/build/NumPlay.nwa $@diff --git a/README.md b/README.mdindex 562f595b..3dcb1317 100644--- a/README.md+++ b/README.md@@ -284,6 +284,7 @@ You need your calculator, its USB cable, and a computer with **Google Chrome** o| N0120 | Epsilon 23.2 or newer (the calculator's own software) | Everything || N0110, N0115 | Epsilon 23.2 or newer | Everything. These models are about 2.5 times slower, so the big games (Hollow Knight, Celeste, NumDrive, NumBlocks, Champion Island) show fewer frames per second, but they keep their speed. || N0110, N0115 | Upsilon | NumPlay-Upsilon.nwa: 13 games. See [On Upsilon](#on-upsilon). |+| Any | Custom software built from Epsilon's source | NumPlay installs. These builds can give apps less memory: a game that needs more than there is says how much on its card (NumBlocks and NumDrive need about 147 KB and 143 KB). |Champion Island needs Epsilon 25.2 or newer. To see your calculator's model and software, open **Settings**, then **About**. To update the software, go to [my.numworks.com](https://my.numworks.com) with the calculator plugged in.diff --git a/launcher/lang/fr.txt b/launcher/lang/fr.txtindex 799a0479..a0e50e9d 100644--- a/launcher/lang/fr.txt+++ b/launcher/lang/fr.txt@@ -104,3 +104,6 @@ Inspired by Minecraft, not affiliated with Mojang Inspiré de Minecraft, sans liBy Tatone26, from All the Apps Par Tatone26, tiré d'All the AppsInspired by Tatone26's All the Apps Inspiré d'All the Apps de Tatone26Based on 2048 by Gabriele Cirulli D'après 2048 de Gabriele Cirulli+# a game the RAM of the calculator's software is too small for+Needs Il manque+ KB more memory Ko de mémoirediff --git a/launcher/lang/zh.txt b/launcher/lang/zh.txtindex 4db0fefd..6993a15f 100644--- a/launcher/lang/zh.txt+++ b/launcher/lang/zh.txt@@ -103,3 +103,6 @@ Inspired by Minecraft, not affiliated with Mojang 灵感来自《我的世界》By Tatone26, from All the Apps 作者 Tatone26,出自 All the AppsInspired by Tatone26's All the Apps 灵感来自 Tatone26 的 All the AppsBased on 2048 by Gabriele Cirulli 改编自 Gabriele Cirulli 的 2048+# a game the RAM of the calculator's software is too small for+Needs 内存还差+ KB more memory KBdiff --git a/launcher/src/games.c b/launcher/src/games.cindex 07729476..6c9603be 100644--- a/launcher/src/games.c+++ b/launcher/src/games.c@@ -35,8 +35,21 @@ uint32_t np_game_size(int i) {#endif}+uint32_t np_game_ram_missing(int i) {+#if NP_SIMULATOR+ (void)i;+ return 0;+#else+ const np_game_t *g = &np_games[i];+ uint32_t data = (uint32_t)(g->data + g->data_size - np_arena), bss = (uint32_t)(g->bss + g->bss_size - np_arena);+ uint32_t need = data > bss ? data : bss, room = np_arena_room();+ return need > room ? need - room : 0;+#endif+}+void np_game_run(int i) {const np_game_t *g = &np_games[i];+ if (np_game_ram_missing(i)) return; /* (the home screen says so and doesn't start it) *//* fresh RAM, as if the game had just been launched from the home screen */if (g->data_size) memcpy(g->data, g->data_init, g->data_size);if (g->bss_size) memset(g->bss, 0, g->bss_size);diff --git a/launcher/src/home.c b/launcher/src/home.cindex ad88acfb..18408fad 100644--- a/launcher/src/home.c+++ b/launcher/src/home.c@@ -40,7 +40,23 @@ static uint32_t item_color(int game, int which) {}static const char *item_title(int game) { return game < 0 ? T("Settings") : np_games[game].title; }-static const char *item_tagline(int game) { return game < 0 ? T("Reset, uninstall, more") : np_games[game].tagline; }+/* under the name, the tagline; or, when the calculator's software gives apps too little RAM for the+ * game, how much it lacks */+static const char *item_tagline(int game) {+ if (game < 0) return T("Reset, uninstall, more");+ uint32_t missing = np_game_ram_missing(game);+ if (!missing) return np_games[game].tagline;+ static char line[96];+ char digits[8];+ int n = 0;+ for (uint32_t kb = (missing + 1023) / 1024; kb || !n; kb /= 10) digits[n++] = (char)('0' + kb % 10);+ char *o = line;+ for (const char *s = T("Needs "); *s;) *o++ = *s++;+ while (n) *o++ = digits[--n];+ for (const char *s = T(" KB more memory"); *s;) *o++ = *s++;+ *o = 0;+ return line;+}static float absf(float v) { return v < 0 ? -v : v; }@@ -98,8 +114,8 @@ static void draw_card(home_t *h, int k, float d) {gfx_clip_x1 = SCREEN_W;}gfx_corners(ix, iy, iiw, iih, ir, frame_color);- /* play badge on the selected game */- if (k == h->sel && game >= 0 && h->badge > 0 && z == 0) {+ /* play badge on the selected game (not on one there isn't the RAM for) */+ if (k == h->sel && game >= 0 && h->badge > 0 && z == 0 && !np_game_ram_missing(game)) {float b = ui_ease_out(h->badge);int r = (int)(15 * b);int bx = x + iw - 14, by = y + ih - 14;@@ -237,7 +253,7 @@ int np_home(int *selected, bool returning) {h.shot_time = 0;h.badge = 0;}- if (pressed & K_OK) result = h.item[h.sel];+ if ((pressed & K_OK) && (h.item[h.sel] < 0 || !np_game_ram_missing(h.item[h.sel]))) result = h.item[h.sel];if (pressed & K_BACKSPACE) { /* the next background, then none */live_next();np_bg_save(live_mode());diff --git a/launcher/src/np.h b/launcher/src/np.hindex 7e861c20..577bcebe 100644--- a/launcher/src/np.h+++ b/launcher/src/np.h@@ -34,6 +34,7 @@ void np_wait_release(void);* launcher's buffers while no game runs */extern uint8_t np_arena[];extern const uint32_t np_arena_size;+uint32_t np_arena_room(void); /* the arena and the RAM after it, up to the end of the app's */void *np_alloc(uint32_t size); /* bump allocator in the arena */void np_alloc_reset(void);@@ -68,6 +69,7 @@ extern const int np_game_count;bool np_game_installed(int i);uint32_t np_game_size(int i); /* bytes of flash */+uint32_t np_game_ram_missing(int i); /* the bytes of RAM a game needs more than there is, or 0 */void np_game_run(int i);/* ---- storage (storage.c): Epsilon's record file system */diff --git a/launcher/src/platform.c b/launcher/src/platform.cindex 73bdd1e0..9a7189d5 100644--- a/launcher/src/platform.c+++ b/launcher/src/platform.c@@ -33,10 +33,22 @@ void np_wait_release(void) {while (eadk_keyboard_scan()) eadk_timing_msleep(10);}+/* The arena is the last of NumPlay's RAM: after it, up to _heap_end, the RAM the calculator's+ * software gives apps goes on (more of it on some software than on other). */+uint32_t np_arena_room(void) {+#if PLATFORM_DEVICE && !NP_SIMULATOR+ extern char _heap_end[];+ uint32_t room = (uint32_t)(_heap_end - (char *)np_arena);+ return room > np_arena_size ? room : np_arena_size;+#else+ return np_arena_size;+#endif+}+static uint32_t alloc_pos;void *np_alloc(uint32_t size) {size = (size + 7) & ~7u;- if (alloc_pos + size > np_arena_size) return 0;+ if (alloc_pos + size > np_arena_room()) return 0;void *p = np_arena + alloc_pos;alloc_pos += size;return p;diff --git a/tests/test_small_ram.py b/tests/test_small_ram.pynew file mode 100644index 00000000..ceda6b1e--- /dev/null+++ b/tests/test_small_ram.py@@ -0,0 +1,67 @@+#!/usr/bin/env python3+"""Checks NumPlay on calculator software that gives apps less RAM: custom builds of Epsilon 25.2+give them 133652 bytes (tools/emu.py --ram-length). NumPlay installs there, and its games use the+RAM after it up to the end of what there is (tools/gen_games.py):++1. NumBlocks needs more: OK on its card doesn't start it (the card says how much it lacks);+2. NumDash fits: it starts, and makes no access outside the app's RAM.++Usage: test_small_ram.py build/NumPlay.nwa [--out DIR]+"""+import argparse+import os+import sys++sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tools"))+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+import emu # noqa: E402++RAM = 133652+emu.configure("n0120", "epsilon", RAM)+from test_progress import presses, write_storage # noqa: E402+++def changed(a, b):+ """the share of pixels that differ between two screenshots"""+ from PIL import Image, ImageChops+ d = ImageChops.difference(Image.open(a).convert("RGB"), Image.open(b).convert("RGB")).convert("L")+ return sum(d.histogram()[25:]) / (d.width * d.height)+++def run(nwa, out, name, keys, shots, ms):+ st = os.path.join(out, f"{name}.bin")+ write_storage(st, [("pi.py", b"\x01print(3.14159)\n\x00")])+ c = emu.Calculator(nwa, storage_file=st)+ c.enable_checks(reads=True)+ c.keys = presses(*keys)+ paths = [os.path.join(out, f"{name}_{t}.png") for t in shots]+ c.pending_shots = list(zip(shots, paths))+ c.run(ms)+ return c, paths+++def main():+ ap = argparse.ArgumentParser()+ ap.add_argument("nwa")+ ap.add_argument("--out", default="build/test_small_ram")+ a = ap.parse_args()+ os.makedirs(a.out, exist_ok=True)+ problems = []+ # NumBlocks, third on the home screen: Right twice, then OK+ c, (before, after) = run(a.nwa, a.out, "numblocks", ((2500, "right"), (2800, "right"), (4000, "ok")), (3900, 6800), 7000)+ problems += sorted(set(c.violations))+ if changed(before, after) > 0.02:+ problems.append("NumBlocks started with too little RAM")+ # NumDash, the first: OK+ c, (home, game) = run(a.nwa, a.out, "numdash", ((2600, "ok"), (6000, "ok")), (2500, 8800), 9000)+ problems += sorted(set(c.violations))+ if changed(home, game) < 0.3:+ problems.append("NumDash didn't start")+ for p in problems:+ print(" ", p)+ print("PASS" if not problems else "FAIL")+ sys.exit(1 if problems else 0)+++if __name__ == "__main__":+ main()diff --git a/tools/emu.py b/tools/emu.pyindex b20b82f9..f08cc26d 100644--- a/tools/emu.py+++ b/tools/emu.py@@ -54,11 +54,12 @@ CPU_HZ = 216_000_000MODEL, SYSTEM = "n0120", "epsilon"-def configure(model="n0120", system="epsilon"):+def configure(model="n0120", system="epsilon", ram_length=None):"""The calculator to emulate: the N0120 (RAM at 0x24000000, the userland after an extra datasector) or the N0110/N0115 (RAM at 0x20000000); Epsilon or Upsilon (its userland header has nodevice name, apps get 107674 bytes of RAM, its file system is Epsilon 15's and it has none of- the system calls for Home, checksums or flash)."""+ the system calls for Home, checksums or flash). ram_length: the RAM apps get, when another+ build of the software gives them another amount."""global MODEL, SYSTEM, SRAM, USERLAND_HEADER, STORAGE, STORAGE_SIZE, EXT_RAM_END, EXT_RAM_LEN, EXT_RAM_START, \STACK_TOPMODEL, SYSTEM = model, system@@ -67,7 +68,7 @@ def configure(model="n0120", system="epsilon"):STORAGE = SRAM + 0x1000STORAGE_SIZE = 64400 if system == "upsilon" else 42 * 1024EXT_RAM_END = SRAM + 0x37000- EXT_RAM_LEN = 107674 if system == "upsilon" else 153676+ EXT_RAM_LEN = ram_length or (107674 if system == "upsilon" else 153676)EXT_RAM_START = EXT_RAM_END - EXT_RAM_LENSTACK_TOP = SRAM + 0x3F000# Epsilon's SmallFont.ttf and LargeFont.ttf (from the epsilon repository), for text drawn by@@ -562,12 +563,13 @@ def main():ap.add_argument("--nwlink", default="nwlink")ap.add_argument("--model", choices=["n0120", "n0110"], default="n0120")ap.add_argument("--system", choices=["epsilon", "upsilon"], default="epsilon")+ ap.add_argument("--ram-length", type=int, help="the RAM apps get (default: the system's)")ap.add_argument("--records", action="store_true", help="list storage records at the end")ap.add_argument("--frames", help="save raw RGB565 frames here (for tools/record.py --frames)")ap.add_argument("--profile", type=int, default=0,help="print the N functions the CPU was found in most (build the .nwa without stripping it)")a = ap.parse_args()- configure(a.model, a.system)+ configure(a.model, a.system, a.ram_length)os.makedirs(a.out, exist_ok=True)c = Calculator(a.nwa, a.flash_start, a.storage, a.nwlink)c.keys = parse_keys(a.keys)diff --git a/tools/gen_games.py b/tools/gen_games.pyindex 1769c4d4..1409ad51 100644--- a/tools/gen_games.py+++ b/tools/gen_games.py@@ -3,8 +3,13 @@From games/games.json and the module sizes written by npmodule.py:- gametable.c: the table of games the launcher shows (np_games);-- arena.s: the RAM arena, sized for the hungriest game, and the symbols that- place each game's .data and .bss inside it (games never run together);+- arena.s: the RAM arena, and the symbols that place each game's .data and+ .bss inside it (games never run together). It is the last of NumPlay's RAM+ and holds what the launcher needs; a game uses it and the RAM after it, up to+ the end of what the calculator's software gives apps (_heap_end), so NumPlay+ installs with a smaller userland too and a game that doesn't fit there says+ so (launcher/src/games.c). arena_extra: how much more than the arena the+ hungriest game takes, for the Makefile's check of every game's RAM;- marks.s: a head and a tail word around each game's block of flash;- numplay.ld: the script of the final `ld -r`, which keeps each game's code,data image, screenshots and marks together as one block.@@ -19,7 +24,7 @@ import jsonimport osimport re-LAUNCHER_RAM = 96 * 1024 # the launcher adapts its screenshot cache to the arena+LAUNCHER_RAM = 96 * 1024 # the arena's own size; the launcher's screenshot cache takes what there isHEAD, TAIL = 0x3147504E, 0x444E4550@@ -54,15 +59,17 @@ def main():sizes[g["id"]] = json.load(open(p)) if os.path.exists(p) else {"code": 0, "data": 0, "bss": 0, "ram_align": 8}# ---- RAM layout- arena = LAUNCHER_RAM+ need = LAUNCHER_RAMlayout = {}for g in games:s = sizes[g["id"]]data = s["data"]bss_off = (data + 7) & ~7layout[g["id"]] = (0, bss_off)- arena = max(arena, bss_off + s["bss"])- arena = (arena + 7) & ~7+ need = max(need, bss_off + s["bss"])+ need = (need + 7) & ~7+ # (the simulator's arena, a C array, keeps the hungriest game's size)+ arena = need if a.simulator else LAUNCHER_RAM# ---- games.cL = ["/* Generated by tools/gen_games.py from games/games.json. */", '#include "np.h"', ""]@@ -111,8 +118,9 @@ def main():return# ---- arena.s- S = ["@ Generated by tools/gen_games.py: one RAM arena shared by the launcher and the games.",- ' .section .bss.np_arena,"aw",%nobits', " .balign 8", " .global np_arena", "np_arena:",+ S = ["@ Generated by tools/gen_games.py: one RAM arena shared by the launcher and the games, the",+ "@ last of NumPlay's RAM (numplay.ld): a game goes on into the RAM after it.",+ ' .section .np_arena,"aw",%nobits', " .balign 8", " .global np_arena", "np_arena:",f" .space {arena}"]for g in games:i = g["id"]@@ -120,6 +128,7 @@ def main():S += [f" .global np_{i}_data", f" .set np_{i}_data, np_arena + {d}",f" .global np_{i}_bss", f" .set np_{i}_bss, np_arena + {b}"]open(os.path.join(a.out, "arena.s"), "w").write("\n".join(S) + "\n")+ open(os.path.join(a.out, "arena_extra"), "w").write(f"{need - arena}\n")# ---- marks.sM = ["@ Generated by tools/gen_games.py: the first and last words of each game's block."]@@ -152,11 +161,11 @@ def main():D += [" .text : { *(.text .text.*) }"," .rodata : { *(.rodata .rodata.*) }"," .data : { *(.data .data.*) }",- " .bss : { *(.bss .bss.* COMMON) }",+ " .bss : { *(.bss .bss.* COMMON) *(.np_arena) }"," /DISCARD/ : { *(.comment) *(.note*) *(.ARM.exidx*) *(.ARM.extab*) }","}"]open(os.path.join(a.out, "numplay.ld"), "w").write("\n".join(D) + "\n")- print(f"games: {len(games)}, arena {arena} bytes")+ print(f"games: {len(games)}, arena {arena} bytes, the hungriest game {need}")if __name__ == "__main__":