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.

Temple Run: make bench times the start, straights, turns and the busiest frames

Mason363b9529f0768b4
diff --git a/games/templerun/Makefile b/games/templerun/Makefile
index f5f7dc4e..a4199477 100644
--- a/games/templerun/Makefile
+++ b/games/templerun/Makefile
@@ -3,7 +3,8 @@
# make check link it like the calculator does (output/templerun.bin)
# make host build/play: the game on a computer, for tests and screenshots
# make test the game played on a computer (tests/check.py, with the sanitizers)
-# make bench how fast it draws a run at an N0110's speed (tools/bench.py, on NumPlay's emulator tools/emu.py)
+# make bench how fast it draws a run at an N0110's speed (tools/bench.py, on NumPlay's emulator tools/emu.py;
+# MODEL=n0120 for an N0120's memory)
# make data DATA=DIR src/data.c and src/data.h again: the game's own models, from its Android package (DIR: its
# assets/bin/Data; tools/apk.py: UnityPy, NumPy, Pillow; the package is only read)
# make models src/data.c and src/data.h as the port's stand-ins instead (tools/models.py: Python)
@@ -20,6 +21,7 @@ NWLINK ?= npx --yes -- [email protected]
STRIP ?= $(BIN)arm-none-eabi-strip --strip-unneeded
SIZE = $(BIN)arm-none-eabi-size
BUILD_DIR = output
+MODEL ?= n0110
# the drawing runs every frame at -O2; the rest is small at -Os (each file says so)
SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c))
@@ -69,7 +71,7 @@ test: build/play
bench:
$(Q) $(MAKE) --no-print-directory build BUILD_DIR=build/bench EXTRA=-DTR_BENCH NWLINK="$(NWLINK)" TOOLCHAIN="$(TOOLCHAIN)"
- python3 tools/bench.py build/bench/templerun.nwa
+ python3 tools/bench.py build/bench/templerun.nwa --model $(MODEL)
models:
python3 tools/models.py src
diff --git a/games/templerun/src/main.c b/games/templerun/src/main.c
index 4f684765..b8953f2c 100644
--- a/games/templerun/src/main.c
+++ b/games/templerun/src/main.c
@@ -22,6 +22,11 @@ uint32_t perf_max __attribute__((used));
uint32_t perf_slow __attribute__((used));
uint32_t perf_draw __attribute__((used)); /* (milliseconds spent drawing) */
uint32_t host_god __attribute__((used)); /* (set by tools/bench.py: nothing hurts the runner) */
+#ifdef TR_BENCH
+/* (read by tools/bench.py after each drawing: in a run, its distance, turns and polygons) */
+uint32_t bench_run __attribute__((used)), bench_dist __attribute__((used)), bench_turns __attribute__((used));
+uint32_t bench_polys __attribute__((used));
+#endif
int main(void) {
uint16_t band[SCREEN_W * BAND_H]; /* (here: on the stack, out of the RAM apps get) */
@@ -64,6 +69,10 @@ int main(void) {
perf_draw += dt;
if (dt > perf_max) perf_max = dt;
if (dt > 33) perf_slow++;
+#ifdef TR_BENCH
+ bench_run = app_in_run(), bench_dist = (uint32_t)run.dist, bench_turns = (uint32_t)run.turns;
+ bench_polys = (uint32_t)r_count();
+#endif
perf_frames++;
} else
plat_sleep(1);
diff --git a/games/templerun/tools/bench.py b/games/templerun/tools/bench.py
index 18b9c0a5..7bc109e2 100644
--- a/games/templerun/tools/bench.py
+++ b/games/templerun/tools/bench.py
@@ -1,8 +1,11 @@
#!/usr/bin/env python3
-"""How fast Temple Run draws a run at an N0110's speed (../../tools/emu.py): bench.py build/bench/templerun.nwa
-[SECONDS] [PICTURES_DIR] (make bench). The timing build (EXTRA=-DTR_BENCH) takes two game steps a drawing, so every
-build draws the same frames; nothing hurts the runner (it is saved where it would fall). Prints the drawing's
-milliseconds from 8 s on."""
+"""How fast Temple Run draws a run (../../tools/emu.py, which runs at an N0110's speed whatever the model; an N0120 is
+about 2.5 times faster): bench.py build/bench/templerun.nwa [--model n0110|n0120] [--seconds S] [--shots DIR]
+[--csv FILE] (make bench). The timing build (EXTRA=-DTR_BENCH) takes two game steps a drawing, so every build draws the
+same frames; nothing hurts the runner (it is saved where it would fall). Prints the drawing's milliseconds a frame:
+in all, in the run's first 5 s (the start), on straights (a second or more from a turn), at turns (half a second or
+less from one) and on the busiest tenth of the frames (the most polygons)."""
+import argparse
import os
import struct
import sys
@@ -10,11 +13,15 @@ import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", "tools"))
import emu # noqa: E402
-nwa = sys.argv[1]
-secs = int(sys.argv[2]) if len(sys.argv) > 2 else 40
-shots = sys.argv[3] if len(sys.argv) > 3 else None
-emu.configure("n0110")
-c = emu.Calculator(nwa)
+ap = argparse.ArgumentParser()
+ap.add_argument("nwa")
+ap.add_argument("--model", choices=["n0110", "n0120"], default="n0110")
+ap.add_argument("--seconds", type=int, default=40)
+ap.add_argument("--shots", help="a picture every 4 s here")
+ap.add_argument("--csv", help="each drawing in the run: frame, distance, turns, polygons, milliseconds")
+a = ap.parse_args()
+emu.configure(a.model)
+c = emu.Calculator(a.nwa)
c.keys = emu.parse_keys("1500-1600:ok,2500-2600:ok,3500-3600:ok")
@@ -22,16 +29,53 @@ def rd(name):
return struct.unpack("<I", bytes(c.uc.mem_read(c.symbols[name], 4)))[0]
+frames = [] # (distance, turns, polygons, ms) of each drawing in the run
+last = [0, 0]
+
+
+def each_ms(calc):
+ n = rd("perf_frames")
+ if n == last[0]:
+ return
+ d = rd("perf_draw")
+ if rd("bench_run"):
+ frames.append((rd("bench_dist"), rd("bench_turns"), rd("bench_polys"), (d - last[1]) / (n - last[0])))
+ last[0], last[1] = n, d
+ if a.shots and calc.now_ms >= next_shot[0]:
+ os.makedirs(a.shots, exist_ok=True)
+ calc.save_png(os.path.join(a.shots, f"t{int(calc.now_ms) // 1000:02d}.png"))
+ next_shot[0] += 4000
+
+
+next_shot = [4000]
c.run(100)
c.uc.mem_write(c.symbols["host_god"], struct.pack("<I", 1))
-c.run(8000)
-f0, d0 = rd("perf_frames"), rd("perf_draw")
-for t in range(8000, secs * 1000 + 1, 4000):
- c.run(t)
- if shots:
- os.makedirs(shots, exist_ok=True)
- c.save_png(os.path.join(shots, f"t{t // 1000:02d}.png"))
-c.run(secs * 1000)
-n = rd("perf_frames") - f0
-print(f"{n} frames: drawing {(rd('perf_draw') - d0) / max(n, 1):.2f} ms a frame; slowest {rd('perf_max')} ms, "
- f"{rd('perf_slow')} over 33 ms")
+last[0], last[1] = rd("perf_frames"), rd("perf_draw")
+c.run(a.seconds * 1000, each_ms, 1)
+if a.csv:
+ with open(a.csv, "w") as f:
+ f.write("frame,dist,turns,polys,ms\n")
+ for i, r in enumerate(frames):
+ f.write(f"{i},{r[0]},{r[1]},{r[2]},{r[3]:.2f}\n")
+
+
+def mean(rows):
+ return sum(r[3] for r in rows) / max(len(rows), 1)
+
+
+START = 150 # (5 s: two steps a drawing)
+turn_at = [i for i in range(1, len(frames)) if frames[i][1] != frames[i - 1][1]]
+
+
+def near_turn(i, w):
+ return any(abs(i - t) <= w for t in turn_at)
+
+
+rest = list(range(START, len(frames)))
+straight = [frames[i] for i in rest if not near_turn(i, 30)]
+turns = [frames[i] for i in rest if near_turn(i, 15)]
+busy = sorted((frames[i] for i in rest), key=lambda r: -r[2])[:max(len(rest) // 10, 1)]
+print(f"{a.model}: {len(frames)} frames, {mean(frames):.2f} ms a frame; start {mean(frames[:START]):.2f}, "
+ f"straight {mean(straight):.2f} ({len(straight)}), turns {mean(turns):.2f} ({len(turns)}), "
+ f"busiest {mean(busy):.2f} ({len(busy)}, {min(r[2] for r in busy)}+ polygons); "
+ f"slowest {max(r[3] for r in frames):.0f} ms, {sum(r[3] > 33 for r in frames)} over 33 ms")