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
Kit: NumPlay.nwa is linked from pieces a website can link too
Mason36390d0d65cba51
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 7ef63814..48f187f1 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -42,7 +42,7 @@ jobs:rustup target add thumbv7em-none-eabihf- name: Build- run: make -j4 nwa apps check NWLINK=nwlink && make lang NWLINK=nwlink+ run: make -j4 nwa apps check NWLINK=nwlink && make lang NWLINK=nwlink && make kit NWLINK=nwlink- uses: actions/upload-artifact@v4with:@@ -78,6 +78,21 @@ jobs:+ - name: Kit for the website+ if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'+ env:+ GH_TOKEN: ${{ github.token }}+ # NumPlay Studio (customize/) links a NumPlay of your own from the kit (make kit): it goes on the+ # branch "kit", one commit, and the site is deployed again with it+ run: |+ cd build/site-kit+ git init -q -b kit+ git add -A+ git -c user.name="github-actions[bot]" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \+ commit -q -m "Kit of NumPlay $(tr -d '[:space:]' < ../../VERSION), from $GITHUB_SHA"+ git push -q -f "https://x-access-token:[email protected]/$GITHUB_REPOSITORY.git" kit+ gh workflow run pages.yml --ref main || echo "(the site was not deployed again)"+- name: Releaseif: startsWith(github.ref, 'refs/tags/v') || (github.ref == 'refs/heads/main' && github.event_name != 'pull_request')env:diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.ymlindex 2dba35f1..796b59f5 100644--- a/.github/workflows/pages.yml+++ b/.github/workflows/pages.yml@@ -2,8 +2,9 @@ name: Pages# Builds the site (mason363.github.io/NumPlay: the README and docs, with Jekyll, as GitHub Pages# did on its own) and deploys it, with the README's downloads, stars and version badges made from-# GitHub's numbers (tools/badges.py). On each push to main, every six hours so the numbers stay-# fresh, and after each release (Build starts it).+# GitHub's numbers (tools/badges.py), and NumPlay Studio (customize/) with the kit Build leaves on+# the branch "kit". On each push to main, every six hours so the numbers stay fresh, and after each+# release or new kit (Build starts it).on:push:@@ -35,6 +36,11 @@ jobs:version=$(gh api "repos/$GITHUB_REPOSITORY/releases/latest" --jq .tag_name)python3 tools/badges.py --downloads "$downloads" --stars "$stars" --version "$version" --out docs/badgescat docs/badges/*.json+ - name: NumPlay Studio's kit+ run: |+ mkdir -p customize/kit+ if git fetch -q --depth 1 origin kit; then git archive FETCH_HEAD | tar -x -C customize/kit; ls customize/kit+ else echo "(no kit yet)"; fi- uses: actions/configure-pages@v5- uses: actions/jekyll-build-pages@v1with:diff --git a/.gitignore b/.gitignoreindex ffc1e5a1..0661637c 100644--- a/.gitignore+++ b/.gitignore@@ -8,3 +8,6 @@ games/tetris/tetris/src/data/image.nppm# made by the Pages workflow (tools/badges.py)docs/badges/dist/++# the customizer website's kit (make kit), put there when the site is deployed+/customize/kitdiff --git a/Makefile b/Makefileindex f4ac69ac..91ae80c1 100644--- a/Makefile+++ b/Makefile@@ -62,7 +62,7 @@ ARM_CFLAGS = -std=gnu11 $(EADK_CFLAGS) -DNP_VERSION='"$(VERSION)"' -Os -Wall -WeARM_LINK = -nostartfiles --specs=nano.specs -Wl,--relocatable -Wl,--gc-sections -Wl,-e,main \-Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level-.PHONY: all nwa apps check sim emu clean lang FORCE+.PHONY: all nwa apps check sim emu clean lang kit FORCEall: nwa appsnwa: $(foreach v,$(VARIANTS),$(B)/$(v).nwa) $(B)/NumPlay-Upsilon.nwa@@ -107,26 +107,47 @@ $(foreach g,$(STD_GAMES),$(eval MOD_$(g) = games/$(g)/output/module.o)$(eval SIMMODULES = $(foreach g,$(GAMES),$(B)/modules/$(g).o)# ------------------------------------------------------------------ generated glue-$(B)/gen/gametable.c: games/games.json tools/gen_games.py $(MODULES)+$(B)/gen/numplay.ld: games/games.json tools/gen_games.py $(foreach g,$(KIT_ALL),$(B)/modules/$(g).o)$(PY) tools/gen_games.py games/games.json $(B)/modules $(B)/gen$(B)/gen/shots_%.c: games/games.json tools/shots.py $$(SHOTS_$$*) | $(B)cd games/$* && $(PY) ../../tools/shots.py --id $* --index $(INDEX_$*) $(COLORS_$*) ../../$@ $(SHOTS_REL_$*)-# ------------------------------------------------------------------ NumPlay.nwa-ARM_OBJS = $(patsubst launcher/src/%.c,$(B)/arm/%.o,$(LAUNCHER_SRC)) $(B)/arm/gametable.o $(B)/arm/arena.o \- $(B)/arm/marks.o $(foreach g,$(GAMES),$(B)/arm/shots_$(g).o)+# ------------------------------------------------------------------ the kit+# NumPlay.nwa is linked from a kit, the same pieces the customizer website links in the browser+# for a NumPlay with the games you pick (site/): base.o, the launcher with every library function+# a game calls; arena.o, the RAM the games share; the app's name and icon; and one object per game+# with its module, its entry in the launcher's table, its screenshots and the marks of its block.+# Games marked "default": false in games.json (OPTIONAL_GAMES) are only in the kit.+KIT = $(B)/kit+KIT_ALL = $(GAMES) $(OPTIONAL_GAMES)+LAUNCHER_OBJS = $(patsubst launcher/src/%.c,$(B)/arm/%.o,$(LAUNCHER_SRC))+KIT_GAMES = $(foreach g,$(KIT_ALL),$(KIT)/games/$(g).o)+NP_GAMES = $(foreach g,$(GAMES),$(KIT)/games/$(g).o)+KIT_LINK = -r --gc-sections -z noexecstack -e main -u eadk_app_name -u eadk_app_icon -u eadk_api_level$(B)/arm/%.o: launcher/src/%.c $(LAUNCHER_H) VERSION | $(B)$(ARM_CC) $(ARM_CFLAGS) -flto -c $< -o $@-$(B)/arm/gametable.o: $(B)/gen/gametable.c launcher/src/np.h- $(ARM_CC) $(ARM_CFLAGS) -c $< -o $@$(B)/arm/shots_%.o: $(B)/gen/shots_%.c launcher/src/np.h$(ARM_CC) $(ARM_CFLAGS) -c $< -o $@-$(B)/arm/arena.o $(B)/arm/marks.o: $(B)/arm/%.o: $(B)/gen/gametable.c- $(ARM_CC) $(EADK_CFLAGS) -c $(B)/gen/$*.s -o $@+$(B)/arm/entry_%.o: $(B)/gen/numplay.ld launcher/src/np.h+ $(ARM_CC) $(ARM_CFLAGS) -c $(B)/gen/entry_$*.c -o $@+$(B)/arm/marks_%.o: $(B)/gen/numplay.ld+ $(ARM_CC) $(EADK_CFLAGS) -c $(B)/gen/marks_$*.s -o $@+$(B)/arm/arena.o: $(B)/gen/numplay.ld+ $(ARM_CC) $(EADK_CFLAGS) -c $(B)/gen/arena.s -o $@+$(KIT)/games/%.o: $(B)/modules/%.o $(B)/arm/entry_%.o $(B)/arm/shots_%.o $(B)/arm/marks_%.o+ @mkdir -p $(KIT)/games+ $(ARM_LD) -r -z noexecstack $^ -o $@+# Every symbol a game needs from the C library, libm or libgcc goes in base.o; each stays a+# section of its own (tools/kit_base.ld), so the final link keeps only those its games call.+$(KIT)/base.o: $(LAUNCHER_OBJS) $(KIT_GAMES) tools/kit_base.ld+ $(ARM_CC) $(ARM_CFLAGS) -nostartfiles --specs=nano.specs -Wl,--relocatable -Wl,--unique -Wl,--gc-sections \+ -Wl,-e,main -Wl,-u,eadk_api_level -Wl,-T,tools/kit_base.ld -flinker-output=nolto-rel $(LAUNCHER_OBJS) \+ $$(arm-none-eabi-nm -u $(KIT_GAMES) | awk '$$1 == "U" || $$1 == "w" {print "-Wl,-u," $$2}' | sort -u) -lm -lgcc -o $@+.SECONDARY: $(foreach v,$(VARIANTS),$(B)/variant/$(v)-name.o $(B)/variant/$(v)-icon.o) \- $(foreach g,$(GAMES),$(B)/arm/shots_$(g).o)+ $(foreach g,$(KIT_ALL),$(B)/arm/shots_$(g).o $(B)/arm/entry_$(g).o $(B)/arm/marks_$(g).o)$(B)/variant/%-name.o: launcher/app_name.c Makefile | $(B)mkdir -p $(B)/variant$(ARM_CC) $(ARM_CFLAGS) '-DNP_APP_NAME=$(NAME_$*)' -c $< -o $@@@ -134,12 +155,22 @@ $(B)/variant/%-icon.o: $$(ICON_$$*) | $(B)mkdir -p $(B)/variant$(NWLINK) png-icon-o $< $@-$(B)/%.nwa: $(ARM_OBJS) $(MODULES) $(B)/variant/%-name.o $(B)/variant/%-icon.o- $(ARM_CC) $(ARM_CFLAGS) $(ARM_LINK) -Wl,-T,$(B)/gen/numplay.ld -flinker-output=nolto-rel \- $(ARM_OBJS) $(MODULES) $(B)/variant/$*-name.o $(B)/variant/$*-icon.o -lm -lgcc -o $@+# ------------------------------------------------------------------ NumPlay.nwa+$(B)/%.nwa: $(KIT)/base.o $(B)/arm/arena.o $(NP_GAMES) $(B)/variant/%-name.o $(B)/variant/%-icon.o+ $(ARM_LD) $(KIT_LINK) -T $(B)/gen/numplay.ld $(KIT)/base.o $(B)/arm/arena.o \+ $(B)/variant/$*-name.o $(B)/variant/$*-icon.o $(NP_GAMES) -o $@arm-none-eabi-strip --strip-unneeded $@@if [ "$*" = NumPlay ]; then $(PY) tools/sizes.py $@ $(B)/modules; fi+# ------------------------------------------------------------------ the website's kit+# build/site-kit: what the customizer website (site/) downloads to link a NumPlay of your own+# (tools/kit.py checks it links NumPlay.nwa to the same bytes the Makefile does).+kit: $(KIT)/base.o $(B)/arm/arena.o $(KIT_GAMES) $(B)/NumPlay.nwa \+ $(foreach v,$(VARIANTS),$(B)/variant/$(v)-name.o $(B)/variant/$(v)-icon.o)+ $(PY) tools/kit.py games/games.json $(B) $(B)/site-kit --version $(VERSION) --variants "$(VARIANTS)" \+ --default "$(GAMES)" --upsilon "$(UPSILON_GAMES)" --app-space $(APP_SPACE) --ram $(CUSTOM_RAM) \+ --upsilon-ram $(UPSILON_RAM) --nwlink "$(NWLINK)"+check: nwa apps@x=$$(cat $(B)/gen/arena_extra); \$(NWLINK) nwa-bin --ram-length $$(( $(CUSTOM_RAM) - x )) $(B)/NumPlay.nwa $(B)/NumPlay-custom.bin >/dev/null || \@@ -157,20 +188,13 @@ check: nwa apps[ $$n -le $(APP_SPACE) ] || { echo "$$v.nwa is bigger than the calculator's app space ($(APP_SPACE) bytes)"; exit 1; }; done# ------------------------------------------------------------------ NumPlay-Upsilon.nwa-$(B)/gen-upsilon/gametable.c: games/games.json tools/gen_games.py $(foreach g,$(UPSILON_GAMES),$(B)/modules/$(g).o)+# The same kit, with the games that fit Upsilon's RAM (its arena is NumPlay's: each game's place+# in it doesn't depend on the others).+$(B)/gen-upsilon/numplay.ld: games/games.json tools/gen_games.py $(foreach g,$(UPSILON_GAMES),$(B)/modules/$(g).o)$(PY) tools/gen_games.py games/games.json $(B)/modules $(B)/gen-upsilon --only "$(UPSILON_GAMES)"-$(B)/arm-upsilon/gametable.o: $(B)/gen-upsilon/gametable.c launcher/src/np.h- mkdir -p $(B)/arm-upsilon- $(ARM_CC) $(ARM_CFLAGS) -c $< -o $@-$(B)/arm-upsilon/arena.o $(B)/arm-upsilon/marks.o: $(B)/arm-upsilon/%.o: $(B)/gen-upsilon/gametable.c- mkdir -p $(B)/arm-upsilon- $(ARM_CC) $(EADK_CFLAGS) -c $(B)/gen-upsilon/$*.s -o $@-UPSILON_OBJS = $(patsubst launcher/src/%.c,$(B)/arm/%.o,$(LAUNCHER_SRC)) $(B)/arm-upsilon/gametable.o \- $(B)/arm-upsilon/arena.o $(B)/arm-upsilon/marks.o $(foreach g,$(UPSILON_GAMES),$(B)/arm/shots_$(g).o) \- $(foreach g,$(UPSILON_GAMES),$(B)/modules/$(g).o)-$(B)/NumPlay-Upsilon.nwa: $(UPSILON_OBJS) $(B)/variant/NumPlay-name.o $(B)/variant/NumPlay-icon.o- $(ARM_CC) $(ARM_CFLAGS) $(ARM_LINK) -Wl,-T,$(B)/gen-upsilon/numplay.ld -flinker-output=nolto-rel \- $^ -lm -lgcc -o $@+$(B)/NumPlay-Upsilon.nwa: $(KIT)/base.o $(B)/arm/arena.o $(foreach g,$(UPSILON_GAMES),$(KIT)/games/$(g).o) \+ $(B)/variant/NumPlay-name.o $(B)/variant/NumPlay-icon.o $(B)/gen-upsilon/numplay.ld+ $(ARM_LD) $(KIT_LINK) -T $(B)/gen-upsilon/numplay.ld $(filter %.o,$^) -o $@arm-none-eabi-strip --strip-unneeded $@# ------------------------------------------------------------------ NumPlay in other languagesdiff --git a/launcher/src/np.h b/launcher/src/np.hindex 2d20be6a..05c48f30 100644--- a/launcher/src/np.h+++ b/launcher/src/np.h@@ -64,8 +64,14 @@ typedef struct {} np_game_t;#define NP_MAX_GAMES 32 /* tools/gen_games.py checks games.json against it */+#if NP_SIMULATORextern const np_game_t np_games[];extern const int np_game_count;+#else+/* Each game's entry is a section of its own; numplay.ld puts those of the games it links in a row. */+extern const np_game_t np_games[], np_games_end[];+#define np_game_count ((int)(np_games_end - np_games))+#endifbool np_game_installed(int i);uint32_t np_game_size(int i); /* bytes of flash */diff --git a/tools/gen_games.py b/tools/gen_games.pyindex 1409ad51..6dbea4ce 100644--- a/tools/gen_games.py+++ b/tools/gen_games.py@@ -2,7 +2,10 @@"""Generates the glue between the launcher and the game modules.From games/games.json and the module sizes written by npmodule.py:-- gametable.c: the table of games the launcher shows (np_games);+- entry_<game>.c: the game's entry in the table of games the launcher shows. Each entry+ is a section of its own (.rodata.np_games.<n>), and numplay.ld puts the entries of the+ games it links in a row, np_games to np_games_end, so a NumPlay with any set of games is+ linked from the same objects (the customizer website does it in the browser);- 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 RAMand holds what the launcher needs; a game uses it and the RAM after it, up to@@ -10,12 +13,16 @@ From games/games.json and the module sizes written by npmodule.py:installs with a smaller userland too and a game that doesn't fit there saysso (launcher/src/games.c). arena_extra: how much more than the arena thehungriest 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;+- marks_<game>.s: a head and a tail word around the 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.+ data image, screenshots and marks together as one block; script.json: the+ same script in parts (the start, each game's block, the end) for the website.-With --only "id id ...", only those games (NumPlay for Upsilon: those that fit its RAM); each keeps-the index of its place in games.json, which its module's sections are named after.+With --simulator, gametable.c instead: the whole table as one array, for the simulator.++numplay.ld links the games of games.json but those marked "default": false (they only come from+the website), or with --only "id id ...", those games (NumPlay for Upsilon: those that fit its+RAM); each keeps the index of its place in games.json, which its module's sections are named after.Usage: gen_games.py games.json MODULE_DIR OUT_DIR [--simulator] [--only "id id ..."]"""@@ -40,7 +47,7 @@ def main():ap.add_argument("--simulator", action="store_true")ap.add_argument("--only", help="space separated game ids")a = ap.parse_args()- games = json.load(open(a.manifest))+ games = manifest = json.load(open(a.manifest))index = {g["id"]: n for n, g in enumerate(games, 1)}if a.only:keep = a.only.split()@@ -48,81 +55,98 @@ def main():if missing:raise SystemExit(f"--only: no such games: {missing}")games = [g for g in games if g["id"] in keep]+ else:+ games = [g for g in games if g.get("default", True)] # the others only come from the websitenp_h = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "launcher", "src", "np.h")).read()limit = int(re.search(r"#define NP_MAX_GAMES (\d+)", np_h).group(1))if len(games) > limit:raise SystemExit(f"{len(games)} games, but the launcher holds {limit} (NP_MAX_GAMES in launcher/src/np.h)")os.makedirs(a.out, exist_ok=True)sizes = {}- for g in games:+ for g in manifest:p = os.path.join(a.modules, g["id"] + ".json")sizes[g["id"]] = json.load(open(p)) if os.path.exists(p) else {"code": 0, "data": 0, "bss": 0, "ram_align": 8}+ # every game whose module is built gets its entry, marks and place in the arena, for the kit+ kit = [g for g in manifest if g in games or os.path.exists(os.path.join(a.modules, g["id"] + ".json"))]# ---- RAM layoutneed = LAUNCHER_RAMlayout = {}- for g in games:+ for g in kit:s = sizes[g["id"]]data = s["data"]bss_off = (data + 7) & ~7layout[g["id"]] = (0, bss_off)- need = max(need, bss_off + s["bss"])+ if g in games:+ 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.c- L = ["/* Generated by tools/gen_games.py from games/games.json. */", '#include "np.h"', ""]- for g in games:- i = g["id"]- L.append(f"extern const uint32_t np_{i}_begin[], np_{i}_end[];")- L.append(f"extern uint8_t np_{i}_data[], np_{i}_bss[];")- L.append(f"extern const uint8_t np_{i}_init[];")- L.append(f"extern const np_shot_t np_{i}_shots[];")- L.append(f"int np_{i}_main(void);")+ def externs(i):+ return [f"extern const uint32_t np_{i}_begin[], np_{i}_end[];",+ f"extern uint8_t np_{i}_data[], np_{i}_bss[];",+ f"extern const uint8_t np_{i}_init[];",+ f"extern const np_shot_t np_{i}_shots[];",+ f"int np_{i}_main(void);"]++ def lists(g, prefix):recs = ", ".join(json.dumps(r) for r in g["records"])- L.append(f"static const char *const {i}_records[] = {{{recs + ', ' if recs else ''}NULL}};")prog = ", ".join(json.dumps(r) for r in g.get("reset", g["records"]))- L.append(f"static const char *const {i}_progress[] = {{{prog + ', ' if prog else ''}NULL}};")- L.append("")- if a.simulator:- L.append("/* The simulator has no flash blocks and gives each game its own RAM. */")- for g in games:- i = g["id"]- L.append(f"const uint32_t np_{i}_begin[1], np_{i}_end[1];")- L.append("#define RAM(x) NULL")- L.append("#define INIT(x) NULL")- L.append("#define SIZE(x) 0")- L.append(f"uint8_t np_arena[{arena}] __attribute__((aligned(8)));")- else:- L.append("#define RAM(x) x")- L.append("#define INIT(x) x")- L.append("#define SIZE(x) x")- L.append(f"const uint32_t np_arena_size = {arena};")- L.append("")- L.append("const np_game_t np_games[] = {")- for g in games:+ return [f"static const char *const {prefix}records[] = {{{recs + ', ' if recs else ''}NULL}};",+ f"static const char *const {prefix}progress[] = {{{prog + ', ' if prog else ''}NULL}};"]++ def entry(g, prefix, ram):i = g["id"]s = sizes[i]est = s["code"] + s["data"]- L.append(f" {{{c_str(i)}, {c_str(g['title'])}, {c_str(g['tagline'])}, {c_str(g.get('credit'))},")- L.append(f" {g['top']}, {g['bottom']}, {g['accent']}, np_{i}_begin, np_{i}_end, np_{i}_main,")- L.append(f" RAM(np_{i}_data), INIT(np_{i}_init), SIZE({s['data']}), RAM(np_{i}_bss), SIZE({s['bss']}),")- L.append(f" np_{i}_shots, {len(g['shots'])}, {i}_records, {i}_progress, {est}}},")- L.append("};")- L.append(f"const int np_game_count = {len(games)};")- open(os.path.join(a.out, "gametable.c"), "w").write("\n".join(L) + "\n")+ r = (lambda x: x) if ram else (lambda x: "NULL")+ z = (lambda x: x) if ram else (lambda x: 0)+ return [f" {{{c_str(i)}, {c_str(g['title'])}, {c_str(g['tagline'])}, {c_str(g.get('credit'))},",+ f" {g['top']}, {g['bottom']}, {g['accent']}, np_{i}_begin, np_{i}_end, np_{i}_main,",+ f" {r(f'np_{i}_data')}, {r(f'np_{i}_init')}, {z(s['data'])}, {r(f'np_{i}_bss')}, {z(s['bss'])},",+ f" np_{i}_shots, {len(g['shots'])}, {prefix}records, {prefix}progress, {est}}}"]if a.simulator:+ # ---- gametable.c: the simulator has no flash blocks and gives each game its own RAM+ L = ["/* Generated by tools/gen_games.py from games/games.json. */", '#include "np.h"', ""]+ for g in games:+ L += externs(g["id"]) + lists(g, g["id"] + "_") + [""]+ for g in games:+ L.append(f"const uint32_t np_{g['id']}_begin[1], np_{g['id']}_end[1];")+ L.append(f"uint8_t np_arena[{arena}] __attribute__((aligned(8)));")+ L.append(f"const uint32_t np_arena_size = {arena};")+ L.append("")+ L.append("const np_game_t np_games[] = {")+ for g in games:+ e = entry(g, g["id"] + "_", False)+ L += e[:-1] + [e[-1] + ","]+ L.append("};")+ L.append(f"const int np_game_count = {len(games)};")+ open(os.path.join(a.out, "gametable.c"), "w").write("\n".join(L) + "\n")print(f"games: {len(games)} (simulator)")return+ # ---- entry_<game>.c+ for g in kit:+ i, n = g["id"], index[g["id"]]+ L = [f"/* Generated by tools/gen_games.py from games/games.json: {g['title']} in the launcher's table. */",+ '#include "np.h"', ""] + externs(i) + lists(g, "") + [+ "",+ f'__attribute__((used, section(".rodata.np_games.{n:02d}")))',+ "static const np_game_t entry ="]+ e = entry(g, "", True)+ L += e[:-1] + [e[-1] + ";"]+ open(os.path.join(a.out, f"entry_{i}.c"), "w").write("\n".join(L) + "\n")+# ---- arena.sS = ["@ 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:+ f" .space {arena}",+ ' .section .rodata.np_arena_size,"a"', " .balign 4", " .global np_arena_size", "np_arena_size:",+ f" .word {arena}"]+ for g in kit:i = g["id"]d, b = layout[i]S += [f" .global np_{i}_data", f" .set np_{i}_data, np_arena + {d}",@@ -130,41 +154,52 @@ def main():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.s- M = ["@ Generated by tools/gen_games.py: the first and last words of each game's block."]- for g in games:+ # ---- marks_<game>.s+ for g in kit:i, n = g["id"], index[g["id"]]- M += [f' .section .rodata.np.{n}.{i}.0head,"a"', " .balign 4", f" .word {HEAD:#x}",- f' .section .rodata.np.{n}.{i}.zztail,"a"', " .balign 4", f" .word {TAIL:#x}"]- open(os.path.join(a.out, "marks.s"), "w").write("\n".join(M) + "\n")-- # ---- numplay.ld- D = ["/* Generated by tools/gen_games.py. Script of the final `ld -r` that makes NumPlay.nwa:",- " each game becomes one section, so the calculator's installer keeps its code, data image,",- " screenshots and marks in one block of flash that uninstalling can wipe. */",- "SECTIONS {",- " .rodata.eadk_app_name : { KEEP(*(.rodata.eadk_app_name)) }",- " .rodata.eadk_api_level : { KEEP(*(.rodata.eadk_api_level)) }",- " .rodata.eadk_app_icon : { KEEP(*(.rodata.eadk_app_icon)) }"]- for g in games:+ M = [f"@ Generated by tools/gen_games.py: the first and last words of {g['title']}'s block.",+ f' .section .rodata.np.{n}.{i}.0head,"a"', " .balign 4", f" .word {HEAD:#x}",+ f' .section .rodata.np.{n}.{i}.zztail,"a"', " .balign 4", f" .word {TAIL:#x}"]+ open(os.path.join(a.out, f"marks_{i}.s"), "w").write("\n".join(M) + "\n")++ # ---- numplay.ld, and script.json for the website+ head = ["/* Generated by tools/gen_games.py. Script of the final `ld -r` that makes NumPlay.nwa:",+ " each game becomes one section, so the calculator's installer keeps its code, data image,",+ " screenshots and marks in one block of flash that uninstalling can wipe. */",+ "SECTIONS {",+ " .rodata.eadk_app_name : { KEEP(*(.rodata.eadk_app_name)) }",+ " .rodata.eadk_api_level : { KEEP(*(.rodata.eadk_api_level)) }",+ " .rodata.eadk_app_icon : { KEEP(*(.rodata.eadk_app_icon)) }",+ " .rodata.np_games : {",+ " np_games = .;",+ " KEEP(*(SORT_BY_NAME(.rodata.np_games.*)))",+ " np_games_end = .;",+ " }"]+ blocks = {}+ for g in manifest:i, n = g["id"], index[g["id"]]p = f".rodata.np.{n}.{i}"- D += [f" {p} : {{",- f" np_{i}_begin = .;",- f" KEEP(*({p}.0head))",- f" *({p})",- f" *({p}.init)",- f" KEEP(*({p}.shots))",- f" KEEP(*({p}.zztail))",- f" np_{i}_end = .;",- " }"]- D += [" .text : { *(.text .text.*) }",- " .rodata : { *(.rodata .rodata.*) }",- " .data : { *(.data .data.*) }",- " .bss : { *(.bss .bss.* COMMON) *(.np_arena) }",- " /DISCARD/ : { *(.comment) *(.note*) *(.ARM.exidx*) *(.ARM.extab*) }",- "}"]+ blocks[i] = [f" {p} : {{",+ f" np_{i}_begin = .;",+ f" KEEP(*({p}.0head))",+ f" *({p})",+ f" *({p}.init)"] + (+ [" *(.rodata.np_roms)"] if g.get("roms") else []) + [+ f" KEEP(*({p}.shots))",+ f" KEEP(*({p}.zztail))",+ f" np_{i}_end = .;",+ " }"]+ tail = [" .text : { *(.text .text.*) }",+ " .rodata : { *(.rodata .rodata.*) }",+ " .data : { *(.data .data.*) }",+ " .bss : { *(.bss .bss.* COMMON) *(.np_arena) }",+ " /DISCARD/ : { *(.comment) *(.note*) *(.ARM.exidx*) *(.ARM.extab*) }",+ "}"]+ D = head + [x for g in games for x in blocks[g["id"]]] + tailopen(os.path.join(a.out, "numplay.ld"), "w").write("\n".join(D) + "\n")+ json.dump({"head": "\n".join(head) + "\n", "tail": "\n".join(tail) + "\n",+ "blocks": {i: "\n".join(b) + "\n" for i, b in blocks.items()}},+ open(os.path.join(a.out, "script.json"), "w"), indent=1)print(f"games: {len(games)}, arena {arena} bytes, the hungriest game {need}")diff --git a/tools/kit.py b/tools/kit.pynew file mode 100644index 00000000..9759ff8b--- /dev/null+++ b/tools/kit.py@@ -0,0 +1,193 @@+#!/usr/bin/env python3+"""Packs the kit the customizer website links a NumPlay of your own from (`make kit`).++The website (site/) runs GNU ld in the browser on the same objects the Makefile links+NumPlay.nwa from: base.o (the launcher), arena.o (the RAM the games share), an app name and+icon, and one object per game, with a script made of script.json's parts (tools/gen_games.py).+This copies them to OUT, gzipped, with kit.json (the games, their sizes, colours and+limits), each game's icon and thumbnail, and checks that linking the default games from OUT+gives NumPlay.nwa's bytes on the calculator.++Usage: kit.py games.json BUILD OUT --version V --variants "..." --default "ids" --upsilon "ids"+ --app-space N --ram N --upsilon-ram N --nwlink NWLINK+"""+import argparse+import gzip+import json+import os+import re+import shutil+import struct+import subprocess+import sys+import tempfile++ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")+SHF_ALLOC = 2+SHT_NOBITS = 8+LINK = ["-r", "--gc-sections", "-z", "noexecstack", "-e", "main",+ "-u", "eadk_app_name", "-u", "eadk_app_icon", "-u", "eadk_api_level"]+++def sections(path):+ """(name, type, flags, size, align) of each section of an ELF object."""+ d = open(path, "rb").read()+ shoff, = struct.unpack_from("<I", d, 0x20)+ shentsize, shnum, shstrndx = struct.unpack_from("<HHH", d, 0x2E)+ hdr = [struct.unpack_from("<10I", d, shoff + i * shentsize) for i in range(shnum)]+ strtab = hdr[shstrndx]+ names = d[strtab[4]:strtab[4] + strtab[5]]+ out = []+ for h in hdr:+ n = names[h[0]:names.index(b"\0", h[0])].decode()+ out.append((n, h[1], h[2], h[5], max(h[8], 1)))+ return out+++def installed_size(nwa):+ """Bytes the installer writes to flash for an .nwa: its 32 byte header, then its sections+ in the order of nwlink's script (the name, the icon, code, read-only data, the data image)."""+ secs = [s for s in sections(nwa) if s[2] & SHF_ALLOC and s[1] != SHT_NOBITS]+ def group(s):+ n = s[0]+ if n == ".rodata.eadk_app_name": return 0+ if n == ".rodata.eadk_app_icon": return 1+ if n == ".text" or n.startswith(".text."): return 2+ if n == ".rodata" or n.startswith(".rodata."): return 3 if n != ".rodata.eadk_api_level" else -1+ if n == ".data" or n.startswith(".data."): return 4+ return 3+ at = 32+ for g in range(5):+ if g in (2, 4): at = (at + 3) & ~3+ for s in secs:+ if group(s) == g:+ at = (at + s[4] - 1) & ~(s[4] - 1)+ at += s[3]+ return at+++def icon_of(gid):+ for p in ("src/icon.png", "assets/icon.png", "tetris/src/data/icon.png", "docs/icon.png"):+ f = os.path.join(ROOT, "games", gid, p)+ if os.path.exists(f):+ return f+ return None+++def gz(src, dst):+ with open(src, "rb") as f, open(dst, "wb") as g:+ g.write(gzip.compress(f.read(), 9, mtime=0))+++def main():+ ap = argparse.ArgumentParser()+ ap.add_argument("manifest")+ ap.add_argument("build")+ ap.add_argument("out")+ ap.add_argument("--version", required=True)+ ap.add_argument("--variants", required=True)+ ap.add_argument("--default", required=True)+ ap.add_argument("--upsilon", default="")+ ap.add_argument("--app-space", type=int, required=True)+ ap.add_argument("--ram", type=int, required=True)+ ap.add_argument("--upsilon-ram", type=int, required=True)+ ap.add_argument("--nwlink", default="nwlink")+ a = ap.parse_args()+ B, O = a.build, a.out+ manifest = json.load(open(a.manifest))+ index = {g["id"]: n for n, g in enumerate(manifest, 1)}+ default, upsilon = a.default.split(), set(a.upsilon.split())+ script = json.load(open(os.path.join(B, "gen", "script.json")))+ if os.path.exists(O):+ shutil.rmtree(O)+ for d in ("games", "icons", "thumbs", "variants"):+ os.makedirs(os.path.join(O, d))++ # ---- the objects, stripped of what no link needs, and gzipped+ tmp = tempfile.mkdtemp()+ def put(src, rel):+ t = os.path.join(tmp, os.path.basename(rel))+ subprocess.run(["arm-none-eabi-strip", "--strip-unneeded", src, "-o", t], check=True)+ gz(t, os.path.join(O, rel + ".gz"))+ return t+ local = {"base.o": put(os.path.join(B, "kit", "base.o"), "base.o"),+ "arena.o": put(os.path.join(B, "arm", "arena.o"), "arena.o")}+ variants = []+ for v in a.variants.split():+ for part in ("name", "icon"):+ local[f"{v}-{part}.o"] = put(os.path.join(B, "variant", f"{v}-{part}.o"), f"variants/{v}-{part}.o")+ png = "icon.png" if v == "NumPlay" else "icon-" + v.split("-", 1)[1].lower() + ".png"+ shutil.copy(os.path.join(ROOT, "launcher", "assets", png), os.path.join(O, "variants", v + ".png"))+ variants.append(v)++ games = []+ for g in manifest:+ i = g["id"]+ obj = os.path.join(B, "kit", "games", i + ".o")+ if not os.path.exists(obj):+ continue+ local[i] = put(obj, f"games/{i}.o")+ n = index[i]+ flash = 0+ for s in sections(obj):+ if s[2] & SHF_ALLOC and s[1] != SHT_NOBITS:+ flash = (flash + s[4] - 1) & ~(s[4] - 1)+ flash += s[3]+ mod = json.load(open(os.path.join(B, "modules", i + ".json")))+ ram = ((mod["data"] + 7) & ~7) + mod["bss"]+ icon = icon_of(i)+ if icon:+ shutil.copy(icon, os.path.join(O, "icons", i + ".png"))+ thumb = os.path.join(ROOT, "docs", "media", f"thumb_{i}.png")+ if os.path.exists(thumb):+ shutil.copy(thumb, os.path.join(O, "thumbs", i + ".png"))+ games.append({+ "id": i, "index": n, "title": g["title"], "tagline": g["tagline"], "credit": g.get("credit"),+ "top": g["top"], "bottom": g["bottom"], "accent": g["accent"],+ "flash": flash, "ram": ram, "default": i in default, "upsilon": i in upsilon,+ "roms": bool(g.get("roms")), "xl": bool(g.get("xl")),+ "bytes": os.path.getsize(os.path.join(O, "games", i + ".o.gz")),+ "icon": bool(icon), "thumb": os.path.exists(thumb)})++ # ---- check: the default games, linked from the kit, are NumPlay.nwa on the calculator+ ld_script = os.path.join(tmp, "custom.ld")+ open(ld_script, "w").write(script["head"] + "".join(script["blocks"][i] for i in default) + script["tail"])+ nwa = os.path.join(tmp, "custom.nwa")+ subprocess.run(["arm-none-eabi-ld"] + LINK + ["-T", ld_script, local["base.o"], local["arena.o"],+ local["NumPlay-name.o"], local["NumPlay-icon.o"]] + [local[i] for i in default] ++ ["-o", nwa], check=True)+ subprocess.run(["arm-none-eabi-strip", "--strip-unneeded", nwa], check=True)+ # only what the installer gives may be left undefined (a game's weak `numplay_launcher`, say,+ # must find the launcher's)+ nm = subprocess.run(["arm-none-eabi-nm", "-u", nwa], capture_output=True, text=True, check=True).stdout+ loose = [l.split()[-1] for l in nm.splitlines()+ if not re.match(r"(eadk_|_heap_|_data_section_|_bss_section_|_sbrk$)", l.split()[-1])+ and l.split() not in (["w", "__sf"], ["w", "_printf_float"])] # (newlib's own weak hooks)+ if loose:+ sys.exit(f"kit: left undefined: {' '.join(loose)}")+ ram = 148928+ bins = []+ for f in (nwa, os.path.join(B, "NumPlay.nwa")):+ b = f + ".bin" if f == nwa else os.path.join(tmp, "make.bin")+ subprocess.run([a.nwlink, "nwa-bin", "--ram-length", str(ram), f, b], check=True,+ stdout=subprocess.DEVNULL)+ bins.append(open(b, "rb").read())+ if bins[0] != bins[1]:+ sys.exit("kit: the default games linked from the kit are not NumPlay.nwa")+ # the installer adds its own start code and EADK calls: the same few hundred bytes for any games+ runtime = len(bins[0]) - installed_size(nwa)+ if not 0 <= runtime < 4096:+ sys.exit(f"kit: installed_size says {installed_size(nwa)}, nwlink {len(bins[0])}")+ base = len(bins[0]) - sum(g["flash"] for g in games if g["default"])++ kit = {"version": a.version, "appSpace": a.app_space, "ram": a.ram, "upsilonRam": a.upsilon_ram,+ "arena": int(re.search(r"\.space (\d+)", open(os.path.join(B, "gen", "arena.s")).read()).group(1)),+ "base": base, "runtime": runtime, "numplay": len(bins[0]), "link": LINK, "variants": variants,+ "script": script, "games": games}+ json.dump(kit, open(os.path.join(O, "kit.json"), "w"), indent=1)+ shutil.rmtree(tmp)+ print(f"kit: {len(games)} games, NumPlay.nwa's {len(bins[0])} bytes linked again from {O}")+++if __name__ == "__main__":+ main()diff --git a/tools/kit_base.ld b/tools/kit_base.ldnew file mode 100644index 00000000..fcc44773--- /dev/null+++ b/tools/kit_base.ld@@ -0,0 +1,7 @@+/* Script of the `ld -r` that makes the kit's base.o (Makefile): with no output section named,+ every input section is an orphan, so --unique keeps each one apart (a library's .text too)+ and the final link of NumPlay.nwa drops those its games don't call. */+SECTIONS {+ .ARM.attributes 0 : { KEEP(*(.ARM.attributes)) }+ /DISCARD/ : { *(.comment) *(.note*) *(.ARM.exidx*) *(.ARM.extab*) }+}