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
Platform: the TI-Nspire CX and CX II
Mason3638c5dc52b840c
diff --git a/platform/README.md b/platform/README.mdindex d0834900..9b260f54 100644--- a/platform/README.md+++ b/platform/README.md@@ -7,7 +7,7 @@ Every NumPlay game talks to its calculator through one interface: [EADK](include| NumWorks | (the calculator's own EADK) | `.nwa` | all games || A computer, a Raspberry Pi | [sdl](sdl/) | a program | first games || TI-84 Plus CE, TI-83 Premium CE | [ti84ce](ti84ce/) | `.8xp`, `.8xv` | first games build; to run in CEmu |-| TI-Nspire CX, CX II | nspire | `.tns` | next |+| TI-Nspire CX, CX II | [nspire](nspire/) | `.tns` | first games build; to run in Firebird || Casio fx-CG50, Graph 90+E | casio-cg | `.g3a` | next || Casio Graph 35+E II | casio-fx | `.g1a` | later || A web browser | [sdl](sdl/) with Emscripten | WebAssembly | first games |diff --git a/platform/nspire/README.md b/platform/nspire/README.mdnew file mode 100644index 00000000..73ad9992--- /dev/null+++ b/platform/nspire/README.md@@ -0,0 +1,28 @@+# NumPlay on the TI-Nspire++The TI-Nspire CX and CX II (and their CAS models), with [Ndless](https://ndless.me).++```+make port TARGET=nspire (in a game's folder): build/nspire/<game>.tns+```++`NDLESS_SDK` points to Ndless's `ndless-sdk` folder, with its toolchain built (`toolchain/build_toolchain.sh`), then `make build-libsyscalls build-libndls build-thirdparty build-tools` in it.++| Key | NumWorks key |+| --- | --- |+| Arrows (touchpad or clickpad) | Arrows |+| enter, the touchpad's middle | OK |+| return (↵) | EXE |+| esc | Back |+| home, on | Home (save and quit) |+| del | ⌫ |+| shift | shift |+| ctrl | alpha |+| menu | toolbox |+| var | var |+| tab | x,n,t |+| 0 to 9, + − × ÷ . , ( ) ^ x² π sin cos tan EE (−) | the same (−) is Ans |++Saves: `/documents/ndless/numplay_<game>.sav.tns`.++Time: the second fast timer counts at 32768 Hz while a game runs and is put back after. To check on a CX II.diff --git a/platform/nspire/np_nspire.c b/platform/nspire/np_nspire.cnew file mode 100644index 00000000..45099fcf--- /dev/null+++ b/platform/nspire/np_nspire.c@@ -0,0 +1,205 @@+/* NumPlay on the TI-Nspire CX and CX II (Ndless): the 320 x 240 screen through lcd_blit, the keys from the keypad+ * (platform/nspire/README.md), the files in /documents/ndless/numplay_<game>.sav.tns.+ *+ * Time: Ndless's clock counts whole seconds, so the second of the fast timers (0x900D0020) runs free at 32768 Hz+ * while the game runs, as the Nspire's SDL does, and is put back as it was at the end. If it doesn't count, time+ * goes by 16 ms a screen drawn (a game's pace stays right, only its clock is rough).+ *+ * Build defines: NP_APP_ID (the game's id). */+#include <libndls.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <eadk.h>+#include "np_port.h"++#ifndef NP_APP_ID+#define NP_APP_ID "numplay"+#endif++/* the RAM after a game's own: 1 MB (the CX has 64) */+__asm__(".section .bss\n.balign 16\n.globl _heap_start\n_heap_start:\n.space 1048576\n.globl _heap_end\n_heap_end:\n"+ ".previous\n");++static uint16_t fb[EADK_SCREEN_WIDTH * EADK_SCREEN_HEIGHT];+static bool started, dirty, timer_ok;+static uint64_t fallback_ms;+static uint32_t last_blit;++/* ---- the clock ---- */+#define T2 ((volatile uint32_t *)0x900D0020) /* load, value, control */+static uint32_t saved_load, saved_control;+static uint32_t ticks_hi, last_raw;+static void timer_start(void) {+ saved_load = T2[0], saved_control = T2[2];+ T2[2] = 0;+ T2[0] = 0xFFFFFFFF;+ T2[2] = 0x82; /* enabled, 32 bits, free running, no interrupt */+ last_raw = T2[1];+ msleep(2);+ timer_ok = T2[1] != last_raw;+}+static void timer_stop(void) {+ T2[2] = 0;+ T2[0] = saved_load;+ T2[2] = saved_control;+}+static uint64_t ticks(void) { /* 32768ths of a second since the start */+ uint32_t raw = 0xFFFFFFFF - T2[1];+ if (raw < last_raw) ticks_hi++;+ last_raw = raw;+ return (uint64_t)ticks_hi << 32 | raw;+}++static void start(void) {+ if (started) return;+ started = true;+ lcd_init(SCR_320x240_565);+ timer_start();+}++/* ---- the screen ---- */+static void blit(void) {+ if (!dirty) return;+ dirty = false;+ lcd_blit(fb, SCR_320x240_565);+ if (!timer_ok) fallback_ms += 16;+}+void eadk_display_push_rect(eadk_rect_t r, const eadk_color_t *px) {+ start();+ for (int y = 0; y < r.height && r.y + y < EADK_SCREEN_HEIGHT; y++) {+ int w = r.x + r.width > EADK_SCREEN_WIDTH ? EADK_SCREEN_WIDTH - r.x : r.width;+ if (w > 0) memcpy(&fb[(r.y + y) * EADK_SCREEN_WIDTH + r.x], px + y * r.width, (size_t)w * 2);+ }+ dirty = true;+ uint32_t now = (uint32_t)eadk_timing_millis();+ if (now - last_blit >= 16) last_blit = now, blit();+}+void eadk_display_push_rect_uniform(eadk_rect_t r, eadk_color_t c) {+ start();+ for (int y = r.y; y < r.y + r.height && y < EADK_SCREEN_HEIGHT; y++)+ for (int x = r.x; x < r.x + r.width && x < EADK_SCREEN_WIDTH; x++) fb[y * EADK_SCREEN_WIDTH + x] = c;+ dirty = true;+}+void eadk_display_pull_rect(eadk_rect_t r, eadk_color_t *px) {+ for (int y = 0; y < r.height; y++)+ for (int x = 0; x < r.width; x++) {+ int sx = r.x + x, sy = r.y + y;+ px[y * r.width + x] = sx < EADK_SCREEN_WIDTH && sy < EADK_SCREEN_HEIGHT ? fb[sy * EADK_SCREEN_WIDTH + sx] : 0;+ }+}+bool eadk_display_wait_for_vblank(void) {+ start();+ blit();+ return true;+}++/* ---- the keys: the NumWorks keypad on the Nspire's ---- */+#define K(n, k) {&KEY_NSPIRE_##n, k}+static const struct {+ const t_key *key;+ eadk_key_t to;+} KEYS[] = {+ K(LEFT, eadk_key_left), K(RIGHT, eadk_key_right), K(UP, eadk_key_up), K(DOWN, eadk_key_down),+ K(ENTER, eadk_key_ok), K(CLICK, eadk_key_ok), K(RET, eadk_key_exe), K(ESC, eadk_key_back),+ K(HOME, eadk_key_home), K(DEL, eadk_key_backspace), K(SHIFT, eadk_key_shift), K(CTRL, eadk_key_alpha),+ K(MENU, eadk_key_toolbox), K(VAR, eadk_key_var), K(TAB, eadk_key_xnt), K(0, eadk_key_zero), K(1, eadk_key_one),+ K(2, eadk_key_two), K(3, eadk_key_three), K(4, eadk_key_four), K(5, eadk_key_five), K(6, eadk_key_six),+ K(7, eadk_key_seven), K(8, eadk_key_eight), K(9, eadk_key_nine), K(PLUS, eadk_key_plus), K(MINUS, eadk_key_minus),+ K(MULTIPLY, eadk_key_multiplication), K(DIVIDE, eadk_key_division), K(PERIOD, eadk_key_dot),+ K(COMMA, eadk_key_comma), K(LP, eadk_key_left_parenthesis), K(RP, eadk_key_right_parenthesis),+ K(EXP, eadk_key_power), K(SQU, eadk_key_square), K(PI, eadk_key_pi), K(SIN, eadk_key_sine), K(COS, eadk_key_cosine),+ K(TAN, eadk_key_tangent), K(EE, eadk_key_ee), K(NEGATIVE, eadk_key_ans),+};+eadk_keyboard_state_t eadk_keyboard_scan(void) {+ start();+ eadk_keyboard_state_t s = 0;+ for (unsigned i = 0; i < sizeof KEYS / sizeof KEYS[0]; i++)+ if (isKeyPressed(*KEYS[i].key)) s |= (eadk_keyboard_state_t)1 << KEYS[i].to;+ /* (the touchpad's corners: two arrows) */+ if (isKeyPressed(KEY_NSPIRE_UPRIGHT)) s |= 1ull << eadk_key_up | 1ull << eadk_key_right;+ if (isKeyPressed(KEY_NSPIRE_RIGHTDOWN)) s |= 1ull << eadk_key_right | 1ull << eadk_key_down;+ if (isKeyPressed(KEY_NSPIRE_DOWNLEFT)) s |= 1ull << eadk_key_down | 1ull << eadk_key_left;+ if (isKeyPressed(KEY_NSPIRE_LEFTUP)) s |= 1ull << eadk_key_left | 1ull << eadk_key_up;+ if (on_key_pressed()) s |= 1ull << eadk_key_home | 1ull << eadk_key_on_off;+ return s;+}+eadk_event_t eadk_event_get(int32_t *timeout) {+ static eadk_keyboard_state_t before = ~(eadk_keyboard_state_t)0;+ uint64_t until = eadk_timing_millis() + (uint64_t)(timeout && *timeout > 0 ? *timeout : 0);+ do {+ eadk_keyboard_state_t now = eadk_keyboard_scan(), hit = now & ~before;+ before = now;+ for (int k = 0; k < 64; k++)+ if (hit >> k & 1) return (eadk_event_t)k;+ msleep(5);+ } while (eadk_timing_millis() < until);+ if (timeout) *timeout = 0;+ return eadk_event_none;+}++/* ---- time, chance, the rest ---- */+uint64_t eadk_timing_millis(void) {+ start();+ return timer_ok ? ticks() * 1000 / 32768 : fallback_ms;+}+void eadk_timing_msleep(uint32_t ms) {+ start();+ blit();+ msleep(ms);+ if (!timer_ok) fallback_ms += ms;+}+void eadk_timing_usleep(uint32_t us) { eadk_timing_msleep((us + 999) / 1000); }+uint32_t eadk_random(void) {+ static uint32_t x;+ if (!x) x = (uint32_t)ticks() ^ *(volatile uint32_t *)0x90090000 ^ 0x9E3779B9u;+ x ^= x << 13, x ^= x >> 17, x ^= x << 5;+ return x;+}+void eadk_backlight_set_brightness(uint8_t b) { (void)b; }+uint8_t eadk_backlight_brightness(void) { return 255; }+bool eadk_battery_is_charging(void) { return false; }+uint8_t eadk_battery_level(void) { return 3; }+float eadk_battery_voltage(void) { return 4.0f; }+bool eadk_usb_is_plugged(void) { return false; }+const char *eadk_external_data = NULL;+size_t eadk_external_data_size = 0;++/* ---- the files ---- */+#define STORAGE_SIZE (64 * 1024)+static uint8_t storage[STORAGE_SIZE + 64];+static bool loaded;+#define SAVE_PATH "/documents/ndless/numplay_" NP_APP_ID ".sav.tns"+static uint32_t storage_end(void) {+ uint32_t at = 0;+ while (at + 2 <= STORAGE_SIZE && (storage[at] | storage[at + 1] << 8)) at += storage[at] | storage[at + 1] << 8;+ return at;+}+uint8_t *np_storage(uint32_t *size) {+ if (!loaded) {+ loaded = true;+ FILE *f = fopen(SAVE_PATH, "rb");+ if (f) {+ size_t n = fread(storage, 1, STORAGE_SIZE, f);+ fclose(f);+ if (storage_end() + 2 > n) memset(storage, 0, sizeof storage);+ }+ }+ *size = STORAGE_SIZE;+ return storage;+}+void np_storage_changed(void) {+ FILE *f = fopen(SAVE_PATH, "wb");+ if (!f) return;+ fwrite(storage, 1, storage_end() + 2, f);+ fclose(f);+}++bool np_slow(void) { return false; }+const char *np_target(void) { return "nspire"; }+void np_begin(void) { start(); }+int np_end(void) {+ if (started) timer_stop(), started = false;+ refresh_osscr();+ return 0;+}diff --git a/platform/port.mk b/platform/port.mkindex ba2a26bc..829a4582 100644--- a/platform/port.mk+++ b/platform/port.mk@@ -39,6 +39,23 @@ $(CE_DIR)/bin/$(CE_NAME).8xp: $(PORT_SRC) $(wildcard $(NP_PLATFORM)/include/*.h)> $(CE_DIR)/makefile$(Q) PATH="$(CEDEV)/bin:$$PATH" $(MAKE) -C $(CE_DIR) --no-print-directory TARGET=$(CE_NAME).8xp@echo "CE $@"+else ifeq ($(TARGET),nspire)+# Ndless's SDK (NDLESS_SDK: its ndless-sdk folder, with its toolchain built in toolchain/install)+NDLESS_SDK ?= $(HOME)/Documents/Assets/toolchains/ndless-src/ndless-sdk+NS_PATH = $(NDLESS_SDK)/bin:$(NDLESS_SDK)/toolchain/install/bin+NS_DIR = $(PORT_OUT)/$(PORT_ID)+NS_SRC = $(PORT_SRC) $(NP_PLATFORM)/nspire/np_nspire.c $(PORT_COMMON)+port: $(PORT_OUT)/$(PORT_ID).tns+$(PORT_OUT)/$(PORT_ID).tns: $(NS_SRC) $(PORT_ASM) $(wildcard $(NP_PLATFORM)/include/*.h)+ @rm -rf $(NS_DIR) && mkdir -p $(NS_DIR)+ $(Q) n=0; for f in $(NS_SRC) $(PORT_ASM); do n=$$((n+1)); \+ PATH="$(NS_PATH):$$PATH" nspire-gcc $(PORT_CFLAGS) -Os -marm -ffunction-sections -fdata-sections \+ -c $$f -o $(NS_DIR)/f$$n.o || exit 1; done+ $(Q) PATH="$(NS_PATH):$$PATH" nspire-ld $(NS_DIR)/*.o -Wl,--gc-sections -lm -o $(NS_DIR)/$(PORT_ID).elf+ $(Q) PATH="$(NS_PATH):$$PATH" genzehn --input $(NS_DIR)/$(PORT_ID).elf --output $(NS_DIR)/$(PORT_ID).zehn \+ --name "$(PORT_NAME)" --240x320-support true --uses-lcd-blit true+ $(Q) PATH="$(NS_PATH):$$PATH" make-prg $(NS_DIR)/$(PORT_ID).zehn $@+ @echo "NSPIRE $@"else ifeq ($(TARGET),web)# Emscripten: the computer build (sdl/np_sdl.c) in a page; Asyncify lets the game's endless loop pause for the page.# A game's data in a .S file (.incbin) comes in through #embed instead: PORT_DATA = symbol:file