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
ce_emu: the toolchain's tests run (autotest), pointers TI-OS keeps
Mason363e505cd7a5b4a
diff --git a/tools/ce_emu.py b/tools/ce_emu.pyindex 13628de7..55926582 100644--- a/tools/ce_emu.py+++ b/tools/ce_emu.py@@ -108,7 +108,7 @@ def profile_lines(samples, syms, n):def run(files, keys="", shots="", ms=5000, out=".", ram="", lib=None, frame=None, rev="m", trace=False,- vars_out=None, profile=0, float_scale=1.0):+ vars_out=None, profile=0, float_scale=1.0, script=None):"""One run; its report (a dict)."""build()prog = next(f for f in files if f.lower().endswith(".8xp"))@@ -135,6 +135,9 @@ def run(files, keys="", shots="", ms=5000, out=".", ram="", lib=None, frame=Nonecmd += ["--float-scale", str(float_scale)] if float_scale != 1 else []cmd += ["--vars-out", vars_out] if vars_out else []cmd += ["--profile", os.path.join(tmp, "profile")] if profile else []+ if script:+ open(os.path.join(tmp, "script"), "w").write(script)+ cmd += ["--script", os.path.join(tmp, "script")]cmd += files + ([lib] if lib else [])os.makedirs(out, exist_ok=True)r = subprocess.run(cmd, capture_output=True, text=True)@@ -168,6 +171,64 @@ def summary(rep):return s+CE_KEYS = {+ "graph": (1, 0), "trace": (1, 1), "zoom": (1, 2), "window": (1, 3), "yequ": (1, 4), "2nd": (1, 5), "mode": (1, 6),+ "del": (1, 7), "on": (2, 0), "sto": (2, 1), "ln": (2, 2), "log": (2, 3), "square": (2, 4), "recip": (2, 5),+ "math": (2, 6), "alpha": (2, 7), "0": (3, 0), "1": (3, 1), "4": (3, 2), "7": (3, 3), "comma": (3, 4), "sin": (3, 5),+ "apps": (3, 6), "xton": (3, 7), "dot": (4, 0), "2": (4, 1), "5": (4, 2), "8": (4, 3), "lparen": (4, 4),+ "cos": (4, 5), "prgm": (4, 6), "stat": (4, 7), "chs": (5, 0), "3": (5, 1), "6": (5, 2), "9": (5, 3),+ "rparen": (5, 4), "tan": (5, 5), "vars": (5, 6), "enter": (6, 0), "add": (6, 1), "sub": (6, 2), "mul": (6, 3),+ "div": (6, 4), "power": (6, 5), "clear": (6, 6), "down": (7, 0), "left": (7, 1), "right": (7, 2), "up": (7, 3)}+AUTOTEST_KEYS = {"y=": "yequ", "wind": "window", "^2": "square", "sq": "square", "-1": "recip", "inv": "recip",+ ",": "comma", "xtn": "xton", ".": "dot", "(": "lparen", "lpar": "lparen", ")": "rparen",+ "rpar": "rparen", "(-)": "chs", "neg": "chs", "+": "add", "-": "sub", "*": "mul", "/": "div",+ "^": "power", "pow": "power", "clr": "clear"}+AUTOTEST_ADDR = {"vram_start": 0xD40000, "vram2_start": 0xD52C00, "vram_16_size": 153600, "vram_8_size": 76800}+++def autotest(path, out, lib):+ """A CE toolchain test (CEmu's autotester JSON: its keys, delays and screen CRCs), run here; what matched."""+ cfg = json.load(open(path))+ d = os.path.dirname(os.path.abspath(path))+ files = [os.path.join(d, f) for f in cfg["transfer_files"]]+ hashes = cfg.get("hashes", {})+ num = lambda v: AUTOTEST_ADDR[v] if isinstance(v, str) and v in AUTOTEST_ADDR else int(str(v), 0)+ lines = []+ for step in cfg["sequence"]:+ op, _, arg = step.partition("|")+ if op == "delay":+ lines.append(f"delay {int(arg)}")+ elif op == "key":+ g, b = CE_KEYS[AUTOTEST_KEYS.get(arg, arg)]+ lines.append(f"key {g} {b}")+ elif op in ("hash", "hashWait"):+ h = hashes[arg]+ t = h.get("timeout", 1000 if op == "hashWait" else 0)+ crcs = ",".join(c for c in h["expected_CRCs"])+ lines.append(f"hash {arg} {num(h['start']):X} {num(h['size'])} {t} {crcs}")+ rep = run(files, ms=120000, out=out, lib=lib, script="\n".join(lines) + "\n")+ good = bad = 0+ print(f"{os.path.relpath(path)}: {rep['status']}" + (f" ({rep['detail']})" if rep["detail"] else ""))+ done = {h["id"]: h for h in rep["hashes"]}+ for k, h in hashes.items():+ r = done.get(k)+ what = h.get("description", k)+ if r is None:+ note = "not reached" + (" (the program had ended: TI-OS's home screen isn't drawn here)" if rep["exit_code"] >= 0 else "")+ elif r["match"]:+ note, good = "ok", good + 1+ elif r["exited"]:+ note = "skipped: after the program's end (TI-OS's home screen isn't drawn here)"+ elif rep["text"]:+ note = "skipped: TI-OS's text on screen (ce_emu writes it with NumPlay's font; see the home screen line)"+ else:+ note, bad = f"MISMATCH (got {r['crc']})", bad + 1+ print(f" {k}: {what}: {note}")+ if rep["text"]:+ print(" home screen: " + rep["text"].replace("\n", " / "))+ return good, bad, rep++def game_files(g):sys.path.insert(0, os.path.join(ROOT, "tools"))import ports@@ -244,6 +305,18 @@ def main():else:pos.append(a[i])i += 1+ if pos and pos[0] == "autotest":+ lib = None if opts["libs"] == "none" else opts["libs"] or libs()+ good = bad = 0+ for p in pos[1:]:+ try:+ g, b, _ = autotest(p, opts["out"], lib)+ except SystemExit as e:+ print(f"{os.path.relpath(p)}: {e}")+ continue+ good, bad = good + g, bad + b+ print(f"{good} screens matched, {bad} didn't")+ return 1 if bad else 0if pos and pos[0] == "sheet":sys.path.insert(0, os.path.join(ROOT, "tools"))import portsdiff --git a/tools/ce_emu/ce_emu.c b/tools/ce_emu/ce_emu.cindex e30dffe9..b42979c7 100644--- a/tools/ce_emu/ce_emu.c+++ b/tools/ce_emu/ce_emu.c@@ -91,6 +91,7 @@ static char console_err[512];static char text_log[8192];static size_t text_len;static bool held[8][8], on_held;+static uint32_t presses[8][8];static uint64_t key_reads, os_calls, screen_changes;/* frames: a marker reached (--frame), or the LCD's base set (a double-buffered program's swap) */typedef struct {@@ -129,6 +130,7 @@ static uint32_t *samples;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; }void emu_charge(uint32_t cycles) { debug.cpuCycles += cycles; }void emu_jump(uint32_t pc) { debug_set_pc(pc); }@@ -361,12 +363,91 @@ static void apply_keys(void) {if (opt.keys[i].group == 2 && opt.keys[i].bit == 0) want_on = true;else want[opt.keys[i].group][opt.keys[i].bit] = true;}+ for (int i = 0; i < opt.nkeys; i++) /* a press right after another of the same key: let go first */+ if (now_ms && now_ms == opt.keys[i].from && held[opt.keys[i].group][opt.keys[i].bit])+ emu_keypad_event(opt.keys[i].group, opt.keys[i].bit, false), held[opt.keys[i].group][opt.keys[i].bit] = false;for (int g = 1; g < 8; g++)for (int b = 0; b < 8; b++)- if (want[g][b] != held[g][b]) held[g][b] = want[g][b], emu_keypad_event(g, b, want[g][b]);+ if (want[g][b] != held[g][b]) {+ held[g][b] = want[g][b], emu_keypad_event(g, b, want[g][b]);+ if (want[g][b]) presses[g][b]++;+ }if (want_on != on_held) on_held = want_on, emu_keypad_event(2, 0, want_on);}+/* ---- a script (--script, CEmu's autotester's steps): delay MS, key GROUP BIT, hash ID START SIZE TIMEOUT CRC,...+ * A key is held 80 ms; a hash (CRC-32C, the autotester's) is checked every 100 ms until it matches or times out ---- */+static struct step {+ char op;+ uint32_t a, b, timeout, crcs[16];+ int ncrc;+ char id[32];+} steps[512];+static int nsteps, step_i;+static bool step_on;+static uint64_t step_until, step_from;+static char hash_log[16384];+static size_t hash_len;+static int hashes_ok, hashes_bad;+static uint32_t crc32c(const uint8_t *p, size_t n) {+ uint32_t c = 0xFFFFFFFF;+ while (n--) {+ c ^= *p++;+ for (int k = 0; k < 8; k++) c = c >> 1 ^ (0x82F63B78 & -(c & 1));+ }+ return ~c;+}+static void load_script(const char *path) {+ FILE *f = fopen(path, "r");+ char line[512];+ while (f && fgets(line, sizeof line, f) && nsteps < (int)(sizeof steps / sizeof steps[0])) {+ struct step *s = &steps[nsteps];+ memset(s, 0, sizeof *s);+ char crcs[400] = "";+ if (sscanf(line, "delay %u", &s->a) == 1) s->op = 'd';+ else if (sscanf(line, "key %u %u", &s->a, &s->b) == 2) s->op = 'k';+ else if (sscanf(line, "hash %31s %x %u %u %399s", s->id, &s->a, &s->b, &s->timeout, crcs) == 5) {+ s->op = 'h';+ for (char *t = strtok(crcs, ","); t && s->ncrc < 16; t = strtok(NULL, ",")) s->crcs[s->ncrc++] = strtoul(t, NULL, 16);+ } else continue;+ nsteps++;+ }+ if (f) fclose(f);+}+static void script_tick(void) {+ while (step_i < nsteps && !stopping) {+ struct step *s = &steps[step_i];+ if (!step_on) step_on = true, step_from = now_ms, step_until = now_ms;+ if (s->op == 'd') {+ if (now_ms < step_from + s->a) return;+ } else if (s->op == 'k') {+ if (now_ms == step_from && opt.nkeys < (int)(sizeof opt.keys / sizeof opt.keys[0]))+ opt.keys[opt.nkeys++] = (key_t_){now_ms, now_ms + 79, (int)s->a, (int)s->b};+ if (now_ms < step_from + 80) return;+ } else if (s->op == 'h') {+ if (now_ms < step_until) return;+ uint8_t *p = mem_at(s->a, s->b);+ uint32_t c = p ? crc32c(p, s->b) : 0;+ bool ok = false;+ for (int i = 0; i < s->ncrc; i++) ok |= c == s->crcs[i];+ if (!ok && now_ms < step_from + s->timeout) {+ step_until = now_ms + 100;+ return;+ }+ ok ? hashes_ok++ : hashes_bad++;+ hash_len += snprintf(hash_log + hash_len, sizeof hash_log - hash_len, "%s{\"id\": \"%s\", \"ms\": %llu, \"crc\": \"%08X\", \"match\": %s, \"exited\": %s}",+ hash_len ? ", " : "", s->id, (unsigned long long)now_ms, c, ok ? "true" : "false", exited ? "true" : "false");+ if (hash_len >= sizeof hash_log) hash_len = sizeof hash_log - 1;+ }+ step_i++, step_on = false;+ }+ if (nsteps && step_i >= nsteps && !stopping) {+ stopping = true;+ snprintf(detail, sizeof detail, "the script's end");+ cpu_set_signal(CPU_SIGNAL_EXIT);+ }+}+static void json_str(FILE *f, const char *s) {fputc('"', f);for (; *s; s++) {@@ -397,6 +478,7 @@ int main(int argc, char **argv) {else if (!strcmp(a, "--idle") && v) opt.idle_addr = (uint32_t)strtoul(v, NULL, 0), i++;else if (!strcmp(a, "--trace")) os_trace = true;else if (!strcmp(a, "--float-scale") && v) os_float_scale = atof(v), i++;+ else if (!strcmp(a, "--script") && v) load_script(v), i++;else if (!strcmp(a, "--screen-memory")) from_memory = true;else if (!strcmp(a, "--profile") && v) opt.profile = v, i++;else if (!strcmp(a, "--ram") && v) {@@ -487,6 +569,7 @@ int main(int argc, char **argv) {if (sorted) break;}for (now_ms = 0; now_ms < opt.ms && !stopping; now_ms++) {+ if (started) script_tick();apply_keys();if (opt.profile) { /* the PC every 0.1 ms */for (int k = 0; k < 10 && !stopping; k++) {@@ -571,6 +654,7 @@ int main(int argc, char **argv) {fprintf(f, " \"lcd_swaps\": %llu,\n \"screen_changes\": %llu,\n \"key_reads\": %llu,\n",(unsigned long long)swaps.n, (unsigned long long)screen_changes, (unsigned long long)key_reads);os_report(f);+ fprintf(f, " \"hashes\": [%s],\n \"hashes_ok\": %d,\n \"hashes_bad\": %d,\n", hash_log, hashes_ok, hashes_bad);fprintf(f, " \"vars_saved\": %d,\n \"text\": ", saved);json_str(f, text_log);fprintf(f, ",\n \"warnings\": ");diff --git a/tools/ce_emu/ce_emu.h b/tools/ce_emu/ce_emu.hindex fcb7d0cf..5170c4ce 100644--- a/tools/ce_emu/ce_emu.h+++ b/tools/ce_emu/ce_emu.h@@ -75,6 +75,7 @@ void emu_stop(const char *status, const char *fmt, ...);void emu_warn(const char *fmt, ...);void emu_text(const char *s);bool emu_key_held(int group, int bit);+uint32_t emu_key_presses(int group, int bit);uint64_t emu_ms(void);void emu_charge(uint32_t cycles);void emu_jump(uint32_t pc);diff --git a/tools/ce_emu/os.c b/tools/ce_emu/os.cindex afbc7ec2..ecf9a9f7 100644--- a/tools/ce_emu/os.c+++ b/tools/ce_emu/os.c@@ -215,9 +215,29 @@ static void move_ptr(uint32_t addr, uint32_t at, int32_t n) {else p += n;wr24(addr, p);}+/* the pointers TI-OS keeps right through InsertMem and DelMem: its own, and those programs park there to have them+ * kept (fileioc's open files: asm_data_ptr1 and 2, and 0xD01FED, F3, F9) */+static const uint32_t DATA_PTRS[] = {+ 0xD0067E, 0xD00681, 0xD01FED, 0xD01FF3, 0xD01FF9, /* asm_data_ptr1, 2 and the three after them */+ 0xD02317, 0xD0231A, 0xD0231D, /* begPC, curPC, endPC */+ 0xD02437, 0xD0243A, 0xD0243D, 0xD02440, 0xD02451, /* editTop, editCursor, editTail, editBtm, editDat */+};+/* and the VAT pointers it keeps when the VAT moves: editSym, tSymPtr1 and 2, chkDelPtr3 and 4 */+static const uint32_t VAT_PTRS[] = {0xD0244E, 0xD0257B, 0xD0257E, 0xD02581, 0xD02584};+static void move_vat_ptrs(uint32_t lo, uint32_t hi, int32_t n) {+ for (size_t i = 0; i < sizeof VAT_PTRS / sizeof VAT_PTRS[0]; i++) {+ uint32_t p = rd24(VAT_PTRS[i]);+ if (p >= lo && p <= hi) wr24(VAT_PTRS[i], p + n);+ }+}static void move_vars(uint32_t at, int32_t n) {static const uint32_t PTRS[] = {TEMP_MEM, FPS_BASE, FPS, NEW_DATA_PTR};for (size_t i = 0; i < sizeof PTRS / sizeof PTRS[0]; i++) move_ptr(PTRS[i], at, n);+ uint32_t fps = rd24(FPS);+ for (size_t i = 0; i < sizeof DATA_PTRS / sizeof DATA_PTRS[0]; i++) {+ uint32_t p = rd24(DATA_PTRS[i]);+ if (p >= USER_MEM && p <= fps + (n < 0 ? -n : 0)) move_ptr(DATA_PTRS[i], at, n);+ }ent_t e;for (bool ok = ent_next(&e, true); ok; ok = ent_next(&e, false)) {if (!in_ram(e.data) || e.data < at) continue;@@ -257,6 +277,7 @@ static bool vat_open(uint32_t top, uint32_t len, bool symbol) {uint32_t ops = rd24(OPS);if (len > ram_free()) return false;memmove(mem_at(ops - len, top - ops + 1), mem_at(ops, top - ops + 1), top - ops + 1);+ move_vat_ptrs(ops, top, -(int32_t)len);if (symbol) wr24(PROG_PTR, rd24(PROG_PTR) - len);wr24(P_TEMP, rd24(P_TEMP) - len);wr24(OP_BASE, rd24(OP_BASE) - len);@@ -266,6 +287,7 @@ static bool vat_open(uint32_t top, uint32_t len, bool symbol) {static void vat_close(const ent_t *e) {uint32_t ops = rd24(OPS), below = e->vat - e->len;memmove(mem_at(ops + e->len, below - ops + 1), mem_at(ops, below - ops + 1), below - ops + 1);+ move_vat_ptrs(ops, below, (int32_t)e->len);if (!e->prog) wr24(PROG_PTR, rd24(PROG_PTR) + e->len);wr24(P_TEMP, rd24(P_TEMP) + e->len);wr24(OP_BASE, rd24(OP_BASE) + e->len);@@ -549,29 +571,22 @@ static void fcmp(float x, float y) {static uint8_t prog_name[8], prog_namelen;static uint32_t calls[0x30000 / 4];static uint32_t unknown_calls;-static int csc_last = -1;+/* GetCSC: each press once (TI's scan codes: (7 - group) * 8 + bit + 1), a held arrow again and again */+static uint32_t csc_seen[8][8];+static int csc_held = -1;static uint64_t csc_repeat_at;-/* the CE's keys, TI's scan codes ((7 - group) * 8 + bit + 1) */-static int scan_code(void) {- for (int g = 1; g <= 7; g++)- for (int b = 0; b < 8; b++)- if (emu_key_held(g, b)) return (7 - g) * 8 + b + 1;- return 0;-}static uint8_t get_csc(void) {- int k = scan_code();uint64_t now = emu_ms();- if (!k) {- csc_last = -1;- return 0;- }- if (k != csc_last) {- csc_last = k, csc_repeat_at = now + 500;- return k;- }- if (k <= 4 && now >= csc_repeat_at) { /* arrows repeat */+ for (int g = 1; g <= 7; g++)+ for (int b = 0; b < 8; b++)+ if (emu_key_presses(g, b) != csc_seen[g][b]) {+ csc_seen[g][b] = emu_key_presses(g, b);+ csc_held = g * 8 + b, csc_repeat_at = now + 500;+ return (7 - g) * 8 + b + 1;+ }+ if (csc_held >= 0 && emu_key_held(csc_held / 8, csc_held % 8) && csc_held / 8 == 7 && now >= csc_repeat_at) {csc_repeat_at = now + 100;- return k;+ return (7 - csc_held / 8) * 8 + csc_held % 8 + 1;}return 0;}@@ -655,7 +670,9 @@ bool os_trap(uint32_t pc) {bool boot = pc >= 0x80 && pc < 0x800, osj = pc >= 0x20100 && pc < HLE_BASE, own = pc >= HLE_BASE && pc < HLE_BASE + 4 * HLE_SLOTS;if (!(boot || osj || own) || (pc & 3)) return false;if (pc / 4 < sizeof calls / sizeof calls[0]) calls[pc / 4]++;- if (os_trace && !own) fprintf(stderr, "[os] %6llu ms %06X %s\n", (unsigned long long)emu_ms(), pc, os_name(pc));+ if (os_trace && !own)+ fprintf(stderr, "[os] %6llu ms %06X %-16s a %02X bc %06X de %06X hl %06X (from %06X)\n", (unsigned long long)emu_ms(), pc,+ os_name(pc), R.A, R.BC & 0xFFFFFF, R.DE & 0xFFFFFF, R.HL & 0xFFFFFF, rd24(R.SPL));ent_t e;uint8_t name[8];switch (pc) {