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.

Platform: one interface for every target, and a computer build

Mason363ee187fd59663
diff --git a/docs/media/calcs/casio_cg50.png b/docs/media/calcs/casio_cg50.png
new file mode 100644
index 00000000..8f839cad
Binary files /dev/null and b/docs/media/calcs/casio_cg50.png differ
diff --git a/docs/media/calcs/casio_graph35.png b/docs/media/calcs/casio_graph35.png
new file mode 100644
index 00000000..2a0b3fba
Binary files /dev/null and b/docs/media/calcs/casio_graph35.png differ
diff --git a/docs/media/calcs/lineup.png b/docs/media/calcs/lineup.png
new file mode 100644
index 00000000..db6d7689
Binary files /dev/null and b/docs/media/calcs/lineup.png differ
diff --git a/docs/media/calcs/nspire.png b/docs/media/calcs/nspire.png
new file mode 100644
index 00000000..c214e4b3
Binary files /dev/null and b/docs/media/calcs/nspire.png differ
diff --git a/docs/media/calcs/numworks.png b/docs/media/calcs/numworks.png
new file mode 100644
index 00000000..9643944c
Binary files /dev/null and b/docs/media/calcs/numworks.png differ
diff --git a/docs/media/calcs/raspberrypi.png b/docs/media/calcs/raspberrypi.png
new file mode 100644
index 00000000..4f18daba
Binary files /dev/null and b/docs/media/calcs/raspberrypi.png differ
diff --git a/docs/media/calcs/ti84ce.png b/docs/media/calcs/ti84ce.png
new file mode 100644
index 00000000..737a682b
Binary files /dev/null and b/docs/media/calcs/ti84ce.png differ
diff --git a/games/common/epsilon_app.h b/games/common/epsilon_app.h
index c919dfea..a016a391 100644
--- a/games/common/epsilon_app.h
+++ b/games/common/epsilon_app.h
@@ -45,7 +45,16 @@ typedef struct {
uint32_t footer;
} epsilon_userland_t;
-#if PLATFORM_DEVICE && !defined(HOST)
+#if defined(NP_PORT) /* any other target: platform/include/np_port.h */
+#include <eadk.h>
+#include "np_port.h"
+__attribute__((unused)) static uint8_t *epsilon_storage(uint32_t *size) { return np_storage(size); }
+static inline bool epsilon_is_upsilon(void) { return false; }
+static inline bool epsilon_slow_model(void) { return np_slow(); }
+static inline void np_app_begin(void) { np_begin(); }
+static inline int np_app_end(void) { return np_end(); }
+
+#elif PLATFORM_DEVICE && !defined(HOST)
#include <eadk.h>
/* The header of the software running this app, or NULL; *upsilon tells which. */
diff --git a/games/common/epsilon_files.h b/games/common/epsilon_files.h
index c99efe9f..b2549f6b 100644
--- a/games/common/epsilon_files.h
+++ b/games/common/epsilon_files.h
@@ -19,6 +19,10 @@ typedef struct {
uint8_t *buf;
uint32_t size;
} ef_fs_t;
+/* (another target keeps its files when they change: platform/include/np_port.h) */
+#ifndef NP_PORT
+#define EF_CHANGED() ((void)0)
+#endif
static uint16_t ef_rd16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
static void ef_wr16(uint8_t *p, uint32_t v) {
@@ -61,7 +65,15 @@ static int ef_find(const ef_fs_t *fs, const char *name, int end) {
return -1;
}
-#if PLATFORM_DEVICE && !defined(HOST)
+#if defined(NP_PORT)
+/* (another target: no Epsilon to keep a cache of the last record looked up) */
+static uint32_t *ef_cache(const ef_fs_t *fs, int end) {
+ static uint32_t none[3];
+ (void)fs, (void)end;
+ return none;
+}
+#define EF_CHANGED() np_storage_changed()
+#elif PLATFORM_DEVICE && !defined(HOST)
static uint32_t ef_crc_bytes(const void *data, uint32_t len) {
if (!len) return 0;
register uint32_t r0 __asm__("r0") = (uint32_t)(uintptr_t)data;
@@ -132,6 +144,7 @@ __attribute__((unused)) static bool ef_remove(const char *name) {
uint16_t size = ef_rd16(fs.buf + at);
if (at + size == end) { /* the last one: nothing moves */
ef_wr16(fs.buf + at, 0);
+ EF_CHANGED();
return true;
}
uint32_t *cache = ef_cache(&fs, end);
@@ -141,6 +154,7 @@ __attribute__((unused)) static bool ef_remove(const char *name) {
for (int n = end + 2 - (at + size); n > 0; n--) *d++ = *s++;
for (int n = size; n > 0; n--) *d++ = 0;
cache[1] = cache[2] = 0;
+ EF_CHANGED();
return true;
}
@@ -169,6 +183,7 @@ __attribute__((unused)) static bool ef_write(const char *name, const void *conte
}
const uint8_t *c = (const uint8_t *)content;
for (uint32_t i = 0; i < len; i++) fs.buf[at + 2 + nlen + i] = c ? c[i] : 0;
+ EF_CHANGED();
return true;
}
@@ -228,6 +243,7 @@ __attribute__((unused)) static uint8_t *ef_resize(const char *name, uint32_t len
cache[1] = cache[2] = 0;
}
ef_wr16(fs.buf + at, total);
+ EF_CHANGED();
return fs.buf + at + 2 + nlen;
}
#endif
diff --git a/games/motox3m/Makefile b/games/motox3m/Makefile
index 009965db..3fe05b90 100644
--- a/games/motox3m/Makefile
+++ b/games/motox3m/Makefile
@@ -82,3 +82,11 @@ data:
clean:
$(Q) rm -rf $(BUILD_DIR) build
+
+# the other targets (platform/port.mk): make port TARGET=sdl
+PORT_ID = motox3m
+PORT_NAME = Moto X3M
+PORT_SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c))
+PORT_ASM = src/data.S
+PORT_FLAGS = -Isrc -DEPSILON_XL=1 -DDATA_BIN='"src/data.bin"' -fno-math-errno
+include ../../platform/port.mk
diff --git a/games/motox3m/src/main.c b/games/motox3m/src/main.c
index fbf939be..83b3bfb6 100644
--- a/games/motox3m/src/main.c
+++ b/games/motox3m/src/main.c
@@ -8,8 +8,10 @@
#include "view.h"
#ifndef HOST
+#ifndef NP_PORT /* (another target: platform/port.mk names the app) */
const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Moto X3M";
const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;
+#endif
uint32_t perf_frames __attribute__((used)); /* read by tools/emu.py */
uint32_t perf_max __attribute__((used));
diff --git a/platform/README.md b/platform/README.md
new file mode 100644
index 00000000..1a8b84cf
--- /dev/null
+++ b/platform/README.md
@@ -0,0 +1,38 @@
+# NumPlay's platforms
+
+Every NumPlay game talks to its calculator through one interface: [EADK](include/eadk.h), the NumWorks's app interface (a 320 x 240 screen, its keypad, a clock), and a few more functions in [np_port.h](include/np_port.h) (files, the start and end of the app). On a NumWorks they are the calculator's own. Every other target implements them in its folder here, so a game is written once and builds everywhere.
+
+| Target | Folder | Builds | Status |
+| --- | --- | --- | --- |
+| 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 | `.8xp`, `.8xv` | next |
+| TI-Nspire CX, CX II | nspire | `.tns` | next |
+| Casio fx-CG50, Graph 90+E | casio-cg | `.g3a` | next |
+| Casio Graph 35+E II | casio-fx | `.g1a` | later |
+| A web browser | web | WebAssembly | next |
+
+## A game on another target
+
+A game's Makefile names its files and includes [port.mk](port.mk):
+
+```make
+PORT_ID = motox3m
+PORT_NAME = Moto X3M
+PORT_SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c))
+PORT_ASM = src/data.S
+PORT_FLAGS = -Isrc -DEPSILON_XL=1
+include ../../platform/port.mk
+```
+
+Then `make port TARGET=sdl`. Built for another target, `NP_PORT` is defined: the app's name and icon sections (`eadk_app_name`...) stay out (`#ifndef NP_PORT`).
+
+## A new target
+
+Implement the functions of `include/eadk.h` and `include/np_port.h` in `platform/<target>/`, and its rule in `port.mk`. [sdl/np_sdl.c](sdl/np_sdl.c) is the model: about 300 lines.
+
+- **Screen:** keep a 320 x 240 RGB565 picture; show it (scaled, or its middle on a smaller screen).
+- **Keys:** return the NumWorks keys held, as bits (`eadk_key_*`). Map yours onto them: arrows, OK, Back, Home at least.
+- **Time:** milliseconds since the start; sleep.
+- **Files:** one buffer of records, kept when `np_storage_changed()` is called.
+- **Heap:** `_heap_start` and `_heap_end` around the RAM a game may use beyond its own.
diff --git a/platform/common/eadk_text.c b/platform/common/eadk_text.c
new file mode 100644
index 00000000..3110b6d3
--- /dev/null
+++ b/platform/common/eadk_text.c
@@ -0,0 +1,47 @@
+/* eadk_display_draw_string for every target but the NumWorks: the firmware's cells (7 x 14, 10 x 18 large) with
+ * NumPlay's own fonts (launcher/src/font_data.c), as the launcher does on Upsilon (launcher/src/compat.c). Letters
+ * beyond ASCII show as '?'. */
+#include <eadk.h>
+#include "../../launcher/src/font.h"
+
+static eadk_color_t blend(eadk_color_t bg, eadk_color_t fg, int a16) {
+ if (a16 >= 15) return fg;
+ if (a16 <= 0) return bg;
+ uint32_t B = (bg | (uint32_t)bg << 16) & 0x07E0F81F, F = (fg | (uint32_t)fg << 16) & 0x07E0F81F;
+ uint32_t M = ((B * (uint32_t)(16 - a16) + F * (uint32_t)a16) >> 4) & 0x07E0F81F;
+ return (eadk_color_t)(M | M >> 16);
+}
+
+static void cell(const np_font_t *f, int cw, int ch, int x, int y, char c, eadk_color_t fg, eadk_color_t bg) {
+ eadk_color_t px[10 * 18];
+ for (int i = 0; i < cw * ch; i++) px[i] = bg;
+ unsigned k = (uint8_t)c;
+ if (k >= f->first && k < (unsigned)f->first + f->count) {
+ const np_glyph_t *g = &f->glyphs[k - f->first];
+ int pen = (cw - g->advance) / 2, base = (ch + f->ascent - f->descent) / 2;
+ const uint8_t *d = f->data + g->offset;
+ for (int j = 0; j < g->h; j++)
+ for (int i = 0; i < g->w; i++) {
+ int n = j * g->w + i, a = (d[n >> 1] >> ((n & 1) * 4)) & 15;
+ int cx = pen + g->x + i, cy = base + g->y + j;
+ if (a && cx >= 0 && cx < cw && cy >= 0 && cy < ch) px[cy * cw + cx] = blend(bg, fg, a);
+ }
+ }
+ if (x + cw > EADK_SCREEN_WIDTH || y + ch > EADK_SCREEN_HEIGHT) return;
+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)cw, (uint16_t)ch}, px);
+}
+
+void eadk_display_draw_string(const char *text, eadk_point_t point, bool large_font, eadk_color_t text_color,
+ eadk_color_t background_color) {
+ const np_font_t *f = large_font ? &np_font_body : &np_font_small;
+ int cw = large_font ? 10 : 7, ch = large_font ? 18 : 14, x = point.x, y = point.y;
+ for (const char *s = text; *s; s++) {
+ if (*s == '\n') {
+ x = point.x, y += ch;
+ continue;
+ }
+ if (((uint8_t)*s & 0xC0) == 0x80) continue; /* (the rest of a UTF-8 letter) */
+ cell(f, cw, ch, x, y, (uint8_t)*s >= 0x80 ? '?' : *s, text_color, background_color);
+ x += cw;
+ }
+}
diff --git a/platform/include/eadk.h b/platform/include/eadk.h
new file mode 100644
index 00000000..caa331c9
--- /dev/null
+++ b/platform/include/eadk.h
@@ -0,0 +1,81 @@
+/* NumPlay's games talk to the calculator through these functions: NumWorks's EADK (its apps' interface), the
+ * same names and values. On a NumWorks the calculator's own eadk.h is used; every other target (platform/<target>/)
+ * implements this one, so a game builds there unchanged.
+ *
+ * The screen is 320 x 240, RGB565. The keyboard is the NumWorks keypad: a target maps its own keys onto it (see
+ * its README). Time is in milliseconds since the app started. */
+#ifndef EADK_H
+#define EADK_H
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+typedef uint16_t eadk_color_t;
+static const eadk_color_t eadk_color_black = 0x0000;
+static const eadk_color_t eadk_color_white = 0xFFFF;
+static const eadk_color_t eadk_color_red = 0xF800;
+static const eadk_color_t eadk_color_green = 0x07E0;
+static const eadk_color_t eadk_color_blue = 0x001F;
+
+typedef struct {
+ uint16_t x, y;
+} eadk_point_t;
+typedef struct {
+ uint16_t x, y, width, height;
+} eadk_rect_t;
+
+#define EADK_SCREEN_WIDTH 320
+#define EADK_SCREEN_HEIGHT 240
+static const eadk_rect_t eadk_screen_rect = {0, 0, EADK_SCREEN_WIDTH, EADK_SCREEN_HEIGHT};
+
+/* the keys, as bits of a keyboard state: bit n is key n */
+typedef uint64_t eadk_keyboard_state_t;
+typedef enum {
+ eadk_key_left = 0, eadk_key_up = 1, eadk_key_down = 2, eadk_key_right = 3, eadk_key_ok = 4, eadk_key_back = 5,
+ eadk_key_home = 6, eadk_key_on_off = 8, eadk_key_shift = 12, eadk_key_alpha = 13, eadk_key_xnt = 14,
+ eadk_key_var = 15, eadk_key_toolbox = 16, eadk_key_backspace = 17, eadk_key_exp = 18, eadk_key_ln = 19,
+ eadk_key_log = 20, eadk_key_imaginary = 21, eadk_key_comma = 22, eadk_key_power = 23, eadk_key_sine = 24,
+ eadk_key_cosine = 25, eadk_key_tangent = 26, eadk_key_pi = 27, eadk_key_sqrt = 28, eadk_key_square = 29,
+ eadk_key_seven = 30, eadk_key_eight = 31, eadk_key_nine = 32, eadk_key_left_parenthesis = 33,
+ eadk_key_right_parenthesis = 34, eadk_key_four = 36, eadk_key_five = 37, eadk_key_six = 38,
+ eadk_key_multiplication = 39, eadk_key_division = 40, eadk_key_one = 42, eadk_key_two = 43, eadk_key_three = 44,
+ eadk_key_plus = 45, eadk_key_minus = 46, eadk_key_zero = 48, eadk_key_dot = 49, eadk_key_ee = 50,
+ eadk_key_ans = 51, eadk_key_exe = 52
+} eadk_key_t;
+
+eadk_keyboard_state_t eadk_keyboard_scan(void);
+static inline bool eadk_keyboard_key_down(eadk_keyboard_state_t state, eadk_key_t key) {
+ return (state >> (uint8_t)key) & 1;
+}
+
+/* key presses as events (the few games that use them): a key's event is its number */
+typedef uint16_t eadk_event_t;
+enum {
+ eadk_event_left = 0, eadk_event_up = 1, eadk_event_down = 2, eadk_event_right = 3, eadk_event_ok = 4,
+ eadk_event_back = 5, eadk_event_backspace = 17, eadk_event_exe = 52, eadk_event_none = 0xFFFF
+};
+eadk_event_t eadk_event_get(int32_t *timeout);
+
+void eadk_display_push_rect(eadk_rect_t rect, const eadk_color_t *pixels);
+void eadk_display_push_rect_uniform(eadk_rect_t rect, eadk_color_t color);
+void eadk_display_pull_rect(eadk_rect_t rect, eadk_color_t *pixels);
+bool eadk_display_wait_for_vblank(void);
+void eadk_display_draw_string(const char *text, eadk_point_t point, bool large_font, eadk_color_t text_color,
+ eadk_color_t background_color);
+
+void eadk_timing_usleep(uint32_t us);
+void eadk_timing_msleep(uint32_t ms);
+uint64_t eadk_timing_millis(void);
+
+void eadk_backlight_set_brightness(uint8_t brightness);
+uint8_t eadk_backlight_brightness(void);
+bool eadk_battery_is_charging(void);
+uint8_t eadk_battery_level(void);
+float eadk_battery_voltage(void);
+bool eadk_usb_is_plugged(void);
+uint32_t eadk_random(void);
+
+extern const char *eadk_external_data;
+extern size_t eadk_external_data_size;
+
+#endif
diff --git a/platform/include/np_port.h b/platform/include/np_port.h
new file mode 100644
index 00000000..6bb56d97
--- /dev/null
+++ b/platform/include/np_port.h
@@ -0,0 +1,28 @@
+/* What a NumPlay target gives the games besides EADK (eadk.h): their files, the RAM after their own, and the app's
+ * start and end. Built for any target but the NumWorks (NP_PORT defined), games/common/epsilon_app.h and
+ * epsilon_files.h use these instead of the NumWorks's own.
+ *
+ * Files. A game's files are records in one buffer laid out as the NumWorks keeps them (a 16-bit size, the name,
+ * the content; a zero size ends the list), so every game's saves work unchanged. The target loads the buffer when
+ * the app starts and keeps it (a file, an appvar...) whenever np_storage_changed() is called. */
+#ifndef NP_PORT_H
+#define NP_PORT_H
+#include <stdbool.h>
+#include <stdint.h>
+
+/* the files' buffer and its size (the records, then their end) */
+uint8_t *np_storage(uint32_t *size);
+/* a record was written or removed: keep the buffer */
+void np_storage_changed(void);
+
+/* a slow target: games draw less (the NumWorks N0110 and N0115 answer true) */
+bool np_slow(void);
+
+/* the target's name: "sdl", "web", "nspire", "casio-cg", "ti84ce"... */
+const char *np_target(void);
+
+/* the app starts; it ends (`return np_end();` from main): the files are kept */
+void np_begin(void);
+int np_end(void);
+
+#endif
diff --git a/platform/port.mk b/platform/port.mk
new file mode 100644
index 00000000..681b5d3a
--- /dev/null
+++ b/platform/port.mk
@@ -0,0 +1,29 @@
+# A game on NumPlay's other targets. A game's Makefile sets, then includes this file:
+# PORT_ID the game's id (games.json), also its files' name
+# PORT_NAME its title
+# PORT_SRC its C files (with its plat_eadk.c: EADK is what every target implements)
+# PORT_ASM its .S files (data.S...)
+# PORT_FLAGS its own compiler flags (-D..., -I...)
+# then `make port TARGET=sdl` builds build/sdl/<id>. TARGET: sdl (a computer, a Raspberry Pi).
+TARGET ?= sdl
+NP_PLATFORM := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
+NP_ROOT := $(NP_PLATFORM)/..
+PORT_OUT = build/$(TARGET)
+PORT_COMMON = $(NP_PLATFORM)/common/eadk_text.c $(NP_ROOT)/launcher/src/font_data.c
+PORT_CFLAGS = -std=gnu11 -O2 -g -Wall -Wno-unused-parameter -Wno-unused-function -DNP_PORT=1 \
+ -DNP_APP_ID='"$(PORT_ID)"' -DNP_APP_NAME='"$(PORT_NAME)"' -I$(NP_PLATFORM)/include $(PORT_FLAGS)
+
+.PHONY: port
+ifeq ($(TARGET),sdl)
+SDL_CFLAGS := $(shell pkg-config --cflags sdl3 2>/dev/null)
+SDL_LIBS := $(shell pkg-config --libs sdl3 2>/dev/null)
+port: $(PORT_OUT)/$(PORT_ID)
+$(PORT_OUT)/$(PORT_ID): $(PORT_SRC) $(PORT_ASM) $(wildcard $(NP_PLATFORM)/include/*.h) $(NP_PLATFORM)/sdl/np_sdl.c
+ @mkdir -p $(@D)
+ $(Q) cc $(PORT_CFLAGS) $(SDL_CFLAGS) $(PORT_SRC) $(PORT_ASM) $(NP_PLATFORM)/sdl/np_sdl.c $(PORT_COMMON) \
+ $(SDL_LIBS) -lm -o $@
+ @echo "SDL $@"
+else
+port:
+ @echo "TARGET=$(TARGET): not a target yet (platform/README.md lists them)"; exit 1
+endif
diff --git a/platform/sdl/README.md b/platform/sdl/README.md
new file mode 100644
index 00000000..521c921b
--- /dev/null
+++ b/platform/sdl/README.md
@@ -0,0 +1,25 @@
+# NumPlay on a computer
+
+Windows, macOS, Linux and a Raspberry Pi, with [SDL 3](https://libsdl.org).
+
+```
+make port TARGET=sdl (in a game's folder): build/sdl/<game>
+```
+
+| Key | NumWorks key |
+| --- | --- |
+| Arrows | Arrows |
+| Enter | OK |
+| Space, keypad Enter | EXE |
+| Escape | Back |
+| Home, closing the window | Home (save and quit) |
+| Shift | shift |
+| Alt | alpha |
+| Tab | toolbox |
+| Backspace, Delete | ⌫ |
+| 0 to 9, + - * / . , [ ] | the same |
+| V, X | var, x,n,t |
+
+Saves go to `~/.numplay/<game>.files` (`NP_SAVES` to change). `NP_SCALE=1` to `6` sets the window's size.
+
+**Tests:** `NP_HEADLESS=1` runs without a window. `NP_KEYS="1500:ok,3000-6000:up"` holds keys (milliseconds from the start), `NP_SHOTS="2000:title.ppm"` saves the screen, `NP_QUIT_MS=9000` presses Home.
diff --git a/platform/sdl/np_sdl.c b/platform/sdl/np_sdl.c
new file mode 100644
index 00000000..7da18ca6
--- /dev/null
+++ b/platform/sdl/np_sdl.c
@@ -0,0 +1,319 @@
+/* NumPlay on a computer (Windows, macOS, Linux, a Raspberry Pi) with SDL 3: the 320 x 240 screen in a window, the
+ * NumWorks keypad on the keyboard (platform/sdl/README.md), the files in one file per game.
+ *
+ * Build defines: NP_APP_ID (the game's id: its files' name), NP_APP_NAME (the window's title). In the environment:
+ * NP_SAVES the folder for the files (default ~/.numplay), NP_SCALE the window's size, 1 to 6 (default 3).
+ *
+ * Tests (tools/verify.py), with NP_HEADLESS=1 for no window: NP_KEYS "1500-1700:ok,3000:back" holds keys
+ * from one time to another (ms since the start; key names as eadk_key_*, without the prefix), NP_SHOTS
+ * "2000:title.ppm,..." saves the screen at those times, NP_QUIT_MS holds Home then (the game saves and ends). */
+#include <SDL3/SDL.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
+#ifndef NP_APP_NAME
+#define NP_APP_NAME "NumPlay"
+#endif
+
+static SDL_Window *win;
+static SDL_Renderer *ren;
+static SDL_Texture *tex;
+static uint16_t fb[EADK_SCREEN_WIDTH * EADK_SCREEN_HEIGHT];
+static bool dirty, quitting, headless, started;
+static uint64_t t0, last_present;
+
+/* ---- scripted keys and pictures (tests) ---- */
+static const struct {
+ const char *name;
+ eadk_key_t key;
+} NAMES[] = {{"left", eadk_key_left}, {"up", eadk_key_up}, {"down", eadk_key_down}, {"right", eadk_key_right},
+ {"ok", eadk_key_ok}, {"back", eadk_key_back}, {"home", eadk_key_home}, {"shift", eadk_key_shift},
+ {"alpha", eadk_key_alpha}, {"xnt", eadk_key_xnt}, {"var", eadk_key_var}, {"toolbox", eadk_key_toolbox},
+ {"backspace", eadk_key_backspace}, {"exe", eadk_key_exe}, {"zero", eadk_key_zero}, {"one", eadk_key_one},
+ {"two", eadk_key_two}, {"three", eadk_key_three}, {"four", eadk_key_four}, {"five", eadk_key_five},
+ {"six", eadk_key_six}, {"seven", eadk_key_seven}, {"eight", eadk_key_eight}, {"nine", eadk_key_nine},
+ {"plus", eadk_key_plus}, {"minus", eadk_key_minus}};
+static eadk_keyboard_state_t script_keys(uint64_t now) {
+ const char *s = getenv("NP_KEYS");
+ eadk_keyboard_state_t k = 0;
+ while (s && *s) {
+ unsigned long a = strtoul(s, (char **)&s, 10), b = a + 90;
+ if (*s == '-') b = strtoul(s + 1, (char **)&s, 10);
+ if (*s++ != ':') break;
+ size_t n = strcspn(s, ",");
+ for (size_t i = 0; i < sizeof NAMES / sizeof NAMES[0]; i++)
+ if (strlen(NAMES[i].name) == n && !strncmp(s, NAMES[i].name, n) && now >= a && now <= b)
+ k |= (eadk_keyboard_state_t)1 << NAMES[i].key;
+ s += n;
+ if (*s == ',') s++;
+ }
+ const char *q = getenv("NP_QUIT_MS");
+ if (q && now >= strtoull(q, NULL, 10)) k |= (eadk_keyboard_state_t)1 << eadk_key_home;
+ return k;
+}
+static void script_shots(uint64_t now) {
+ static int done;
+ const char *s = getenv("NP_SHOTS");
+ for (int i = 0; s && *s; i++) {
+ unsigned long t = strtoul(s, (char **)&s, 10);
+ if (*s++ != ':') return;
+ size_t n = strcspn(s, ",");
+ if (i >= done && now >= t) {
+ char path[512];
+ snprintf(path, sizeof path, "%.*s", (int)n, s);
+ FILE *f = fopen(path, "wb");
+ if (f) {
+ fprintf(f, "P6\n%d %d\n255\n", EADK_SCREEN_WIDTH, EADK_SCREEN_HEIGHT);
+ for (int p = 0; p < EADK_SCREEN_WIDTH * EADK_SCREEN_HEIGHT; p++) {
+ uint16_t c = fb[p];
+ uint8_t rgb[3] = {(uint8_t)((c >> 11) * 255 / 31), (uint8_t)((c >> 5 & 63) * 255 / 63), (uint8_t)((c & 31) * 255 / 31)};
+ fwrite(rgb, 1, 3, f);
+ }
+ fclose(f);
+ }
+ done = i + 1;
+ }
+ s += n;
+ if (*s == ',') s++;
+ }
+}
+
+/* the RAM after a game's own (the NumWorks gives a game what its software leaves): 256 KB here */
+#define NP_HEAP_SIZE (256 * 1024)
+#if defined(__APPLE__)
+__asm__(".data\n.p2align 4\n.globl __heap_start\n__heap_start:\n.space 262144\n.globl __heap_end\n__heap_end:\n");
+#else
+__asm__(".section .bss\n.balign 16\n.globl _heap_start\n_heap_start:\n.space 262144\n.globl _heap_end\n_heap_end:\n"
+ ".previous\n");
+#endif
+
+static void start(void) {
+ if (started) return;
+ started = true;
+ headless = getenv("NP_HEADLESS") != NULL;
+ if (headless) {
+ SDL_Init(0);
+ t0 = SDL_GetTicks();
+ return;
+ }
+ if (!SDL_Init(SDL_INIT_VIDEO)) {
+ fprintf(stderr, "NumPlay: %s\n", SDL_GetError());
+ exit(1);
+ }
+ int scale = getenv("NP_SCALE") ? atoi(getenv("NP_SCALE")) : 3;
+ if (scale < 1 || scale > 6) scale = 3;
+ win = SDL_CreateWindow(NP_APP_NAME, EADK_SCREEN_WIDTH * scale, EADK_SCREEN_HEIGHT * scale, SDL_WINDOW_RESIZABLE);
+ ren = win ? SDL_CreateRenderer(win, NULL) : NULL;
+ if (win && !ren) ren = SDL_CreateRenderer(win, "software"); /* (no GPU: tests, a bare Raspberry Pi) */
+ tex = ren ? SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, EADK_SCREEN_WIDTH,
+ EADK_SCREEN_HEIGHT)
+ : NULL;
+ if (!tex) {
+ fprintf(stderr, "NumPlay: %s\n", SDL_GetError());
+ exit(1);
+ }
+ SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST);
+ SDL_SetRenderLogicalPresentation(ren, EADK_SCREEN_WIDTH, EADK_SCREEN_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
+ t0 = SDL_GetTicks();
+}
+
+static void present(void) {
+ last_present = SDL_GetTicks();
+ if (!dirty || headless) return;
+ dirty = false;
+ SDL_UpdateTexture(tex, NULL, fb, EADK_SCREEN_WIDTH * 2);
+ SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
+ SDL_RenderClear(ren);
+ SDL_RenderTexture(ren, tex, NULL, NULL);
+ SDL_RenderPresent(ren);
+}
+/* the window is kept alive: events read, and the screen shown 60 times a second at most */
+static void pump(void) {
+ start();
+ SDL_Event e;
+ while (!headless && SDL_PollEvent(&e))
+ if (e.type == SDL_EVENT_QUIT) quitting = true;
+ script_shots(SDL_GetTicks() - t0);
+ if (SDL_GetTicks() - last_present >= 16) present();
+}
+
+/* ---- the screen ---- */
+void eadk_display_push_rect(eadk_rect_t r, const eadk_color_t *px) {
+ start();
+ for (int y = 0; y < r.height; y++) {
+ int sy = r.y + y;
+ if (sy >= EADK_SCREEN_HEIGHT) break;
+ for (int x = 0; x < r.width; x++)
+ if (r.x + x < EADK_SCREEN_WIDTH) fb[sy * EADK_SCREEN_WIDTH + r.x + x] = px[y * r.width + x];
+ }
+ dirty = true;
+ if (SDL_GetTicks() - last_present >= 16) pump();
+}
+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) {
+ pump();
+ present();
+ return true;
+}
+
+/* ---- the keyboard: the NumWorks keypad on a computer's keys ---- */
+static const struct {
+ SDL_Scancode sc;
+ eadk_key_t key;
+} KEYS[] = {
+ {SDL_SCANCODE_LEFT, eadk_key_left}, {SDL_SCANCODE_RIGHT, eadk_key_right}, {SDL_SCANCODE_UP, eadk_key_up},
+ {SDL_SCANCODE_DOWN, eadk_key_down}, {SDL_SCANCODE_RETURN, eadk_key_ok}, {SDL_SCANCODE_KP_ENTER, eadk_key_exe},
+ {SDL_SCANCODE_SPACE, eadk_key_exe}, {SDL_SCANCODE_ESCAPE, eadk_key_back}, {SDL_SCANCODE_HOME, eadk_key_home},
+ {SDL_SCANCODE_LSHIFT, eadk_key_shift}, {SDL_SCANCODE_RSHIFT, eadk_key_shift}, {SDL_SCANCODE_LALT, eadk_key_alpha},
+ {SDL_SCANCODE_RALT, eadk_key_alpha}, {SDL_SCANCODE_TAB, eadk_key_toolbox}, {SDL_SCANCODE_BACKSPACE, eadk_key_backspace},
+ {SDL_SCANCODE_DELETE, eadk_key_backspace}, {SDL_SCANCODE_V, eadk_key_var}, {SDL_SCANCODE_X, eadk_key_xnt},
+ {SDL_SCANCODE_0, eadk_key_zero}, {SDL_SCANCODE_1, eadk_key_one}, {SDL_SCANCODE_2, eadk_key_two},
+ {SDL_SCANCODE_3, eadk_key_three}, {SDL_SCANCODE_4, eadk_key_four}, {SDL_SCANCODE_5, eadk_key_five},
+ {SDL_SCANCODE_6, eadk_key_six}, {SDL_SCANCODE_7, eadk_key_seven}, {SDL_SCANCODE_8, eadk_key_eight},
+ {SDL_SCANCODE_9, eadk_key_nine}, {SDL_SCANCODE_KP_0, eadk_key_zero}, {SDL_SCANCODE_KP_1, eadk_key_one},
+ {SDL_SCANCODE_KP_2, eadk_key_two}, {SDL_SCANCODE_KP_3, eadk_key_three}, {SDL_SCANCODE_KP_4, eadk_key_four},
+ {SDL_SCANCODE_KP_5, eadk_key_five}, {SDL_SCANCODE_KP_6, eadk_key_six}, {SDL_SCANCODE_KP_7, eadk_key_seven},
+ {SDL_SCANCODE_KP_8, eadk_key_eight}, {SDL_SCANCODE_KP_9, eadk_key_nine}, {SDL_SCANCODE_EQUALS, eadk_key_plus},
+ {SDL_SCANCODE_KP_PLUS, eadk_key_plus}, {SDL_SCANCODE_MINUS, eadk_key_minus}, {SDL_SCANCODE_KP_MINUS, eadk_key_minus},
+ {SDL_SCANCODE_KP_MULTIPLY, eadk_key_multiplication}, {SDL_SCANCODE_SLASH, eadk_key_division},
+ {SDL_SCANCODE_KP_DIVIDE, eadk_key_division}, {SDL_SCANCODE_PERIOD, eadk_key_dot}, {SDL_SCANCODE_COMMA, eadk_key_comma},
+ {SDL_SCANCODE_LEFTBRACKET, eadk_key_left_parenthesis}, {SDL_SCANCODE_RIGHTBRACKET, eadk_key_right_parenthesis},
+ {SDL_SCANCODE_E, eadk_key_exp}, {SDL_SCANCODE_L, eadk_key_ln}, {SDL_SCANCODE_S, eadk_key_sine},
+ {SDL_SCANCODE_C, eadk_key_cosine}, {SDL_SCANCODE_T, eadk_key_tangent}, {SDL_SCANCODE_P, eadk_key_pi},
+ {SDL_SCANCODE_R, eadk_key_sqrt}, {SDL_SCANCODE_Q, eadk_key_square}, {SDL_SCANCODE_A, eadk_key_ans},
+};
+
+eadk_keyboard_state_t eadk_keyboard_scan(void) {
+ pump();
+ int n = 0;
+ const bool *down = headless ? NULL : SDL_GetKeyboardState(&n);
+ eadk_keyboard_state_t s = 0;
+ for (size_t i = 0; i < sizeof KEYS / sizeof KEYS[0]; i++)
+ if ((int)KEYS[i].sc < n && down[KEYS[i].sc]) s |= (eadk_keyboard_state_t)1 << KEYS[i].key;
+ if (quitting) s |= (eadk_keyboard_state_t)1 << eadk_key_home | (eadk_keyboard_state_t)1 << eadk_key_on_off;
+ return s | script_keys(SDL_GetTicks() - t0);
+}
+
+/* a key press as an event, waiting up to *timeout ms */
+eadk_event_t eadk_event_get(int32_t *timeout) {
+ static eadk_keyboard_state_t before = ~(eadk_keyboard_state_t)0;
+ uint64_t until = SDL_GetTicks() + (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;
+ SDL_Delay(5);
+ } while (SDL_GetTicks() < until);
+ if (timeout) *timeout = 0;
+ return eadk_event_none;
+}
+
+/* ---- time, chance, the rest ---- */
+uint64_t eadk_timing_millis(void) {
+ start();
+ return SDL_GetTicks() - t0;
+}
+void eadk_timing_msleep(uint32_t ms) {
+ pump();
+ present();
+ SDL_Delay(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)SDL_GetPerformanceCounter() | 1;
+ 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.2f; }
+bool eadk_usb_is_plugged(void) { return false; }
+const char *eadk_external_data = NULL;
+size_t eadk_external_data_size = 0;
+
+/* ---- the files: one file, NP_SAVES/<game>.files ---- */
+#define STORAGE_SIZE (64 * 1024)
+static uint8_t storage[STORAGE_SIZE + 64]; /* (the records, then room for the cache words after them) */
+static bool storage_loaded;
+
+static void storage_path(char *p, size_t n) {
+ const char *dir = getenv("NP_SAVES");
+ char home[512];
+ if (!dir) {
+ const char *h = getenv("HOME");
+ if (!h) h = getenv("USERPROFILE");
+ snprintf(home, sizeof home, "%s/.numplay", h ? h : ".");
+ SDL_CreateDirectory(home);
+ dir = home;
+ }
+ snprintf(p, n, "%s/%s.files", dir, NP_APP_ID);
+}
+
+uint8_t *np_storage(uint32_t *size) {
+ if (!storage_loaded) {
+ storage_loaded = true;
+ char p[600];
+ storage_path(p, sizeof p);
+ FILE *f = fopen(p, "rb");
+ if (f) {
+ size_t n = fread(storage, 1, STORAGE_SIZE, f);
+ fclose(f);
+ /* (a file that isn't a list of records ending with a zero size starts empty) */
+ uint32_t at = 0;
+ while (at + 2 <= n && (storage[at] | storage[at + 1] << 8)) at += storage[at] | storage[at + 1] << 8;
+ if (at + 2 > n) memset(storage, 0, sizeof storage);
+ }
+ }
+ *size = STORAGE_SIZE;
+ return storage;
+}
+
+void np_storage_changed(void) {
+ char p[600], tmp[620];
+ storage_path(p, sizeof p);
+ snprintf(tmp, sizeof tmp, "%s.new", p);
+ uint32_t end = 0;
+ while (end + 2 <= STORAGE_SIZE && (storage[end] | storage[end + 1] << 8)) end += storage[end] | storage[end + 1] << 8;
+ FILE *f = fopen(tmp, "wb");
+ if (!f) return;
+ bool ok = fwrite(storage, 1, end + 2, f) == end + 2;
+ ok = fclose(f) == 0 && ok;
+ if (ok) rename(tmp, p); /* (the old file stays until the new one is whole) */
+}
+
+bool np_slow(void) { return false; }
+const char *np_target(void) { return "sdl"; }
+void np_begin(void) { start(); }
+int np_end(void) {
+ script_shots(~(uint64_t)0 >> 1); /* (the pictures not taken yet: the last screen) */
+ if (win) {
+ SDL_DestroyTexture(tex);
+ SDL_DestroyRenderer(ren);
+ SDL_DestroyWindow(win);
+ SDL_Quit();
+ win = NULL;
+ }
+ return 0;
+}