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.

Ports: the web and the Casio fx-CG50

Mason36398cf861286d4
diff --git a/games/amongus/Makefile b/games/amongus/Makefile
index 315e00e5..5894dfbf 100644
--- a/games/amongus/Makefile
+++ b/games/amongus/Makefile
@@ -88,6 +88,5 @@ PORT_ID = amongus
PORT_NAME = Among Us
PORT_SRC = $(filter-out src/plat_host.c,$(wildcard src/*.c))
PORT_ASM = src/data.S
-PORT_DATA = au_data:src/data.bin
PORT_FLAGS = -Isrc -DEPSILON_XL=1 -DDATA_BIN='"src/data.bin"' -fno-math-errno
include ../../platform/port.mk
diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.c
index c683e67f..23760954 100644
--- a/games/hollowknight/src/gfx.c
+++ b/games/hollowknight/src/gfx.c
@@ -1441,7 +1441,7 @@ static uint32_t bgline[BG_W + 2]; /* a row of the background (0xRRGGBB), its e
/* light (x 128) to 5 or 6 bits (dithering added before): one instruction on the calculator */
-#if defined(__arm__)
+#if defined(__arm__) && defined(__ARM_FEATURE_SIMD32) /* (ARMv6 and later: not the Nspire's ARM926) */
static inline uint32_t to5(uint32_t v) {
uint32_t r;
__asm__("usat %0, #5, %1, asr #10" : "=r"(r) : "r"(v));
diff --git a/platform/casio-cg/addin.ld b/platform/casio-cg/addin.ld
new file mode 100644
index 00000000..027ab3fd
--- /dev/null
+++ b/platform/casio-cg/addin.ld
@@ -0,0 +1,18 @@
+/* A fx-CG add-in: its code and read-only data where the calculator maps the .g3a (after the header), its data in
+ the add-in RAM (512 KB at 0x08100000 shared with the stack, which grows down from the end: 128 KB left for it).
+ From libfxcg's toolchain/prizm.x, with room for bigger games. */
+OUTPUT_ARCH(sh3)
+ENTRY(initialize)
+MEMORY {
+ rom (rx) : o = 0x00300000, l = 2048k
+ ram (rwx) : o = 0x08100004, l = 384k
+ ilram (rwx) : o = 0xE5200000, l = 4k
+}
+SECTIONS {
+ .text : { *(.pretext) *(.text) *(.text.*) } > rom
+ .ilram : { _ilramld = LOADADDR(.ilram); _silram = .; *(.ilram) *(.ilram.*) _eilram = .; } > ilram AT> rom
+ .rodata : { *(.rodata) *(.rodata.*) } > rom
+ .data : ALIGN(4) { _datald = LOADADDR(.data); _sdata = .; *(.data) *(.data.*); _edata = .; } > ram AT> rom
+ .bss : ALIGN(4) { _bbss = .; *(.bss) *(.bss*) *(COMMON) _ebss = .; } > ram
+ /DISCARD/ : { *(.comment) *(.note*) *(.eh_frame*) }
+}
diff --git a/platform/casio-cg/g3a.py b/platform/casio-cg/g3a.py
new file mode 100644
index 00000000..0dc830cd
--- /dev/null
+++ b/platform/casio-cg/g3a.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+"""An add-in for the Casio fx-CG50 / Graph 90+E (a .g3a file): the game's code after a 0x7000-byte header with its
+name and its two 92 x 64 icons (the layout of mkg3a, gitlab.com/taricorp/mkg3a).
+
+ python3 platform/casio-cg/g3a.py code.bin out.g3a --name "Snake" --icon src/icon.png"""
+import argparse
+import os
+import time
+
+from PIL import Image
+
+HEADER = 0x7000
+ICON_W, ICON_H = 92, 64
+
+
+def rgb565_be(im):
+ out = bytearray()
+ for r, g, b in im.convert("RGB").getdata():
+ v = (r >> 3) << 11 | (g >> 2) << 5 | b >> 3
+ out += bytes((v >> 8, v & 0xFF))
+ return bytes(out)
+
+
+def icon(path, selected):
+ """The game's icon in the middle of the menu's 92 x 64 cell: white, or light blue when selected."""
+ cell = Image.new("RGB", (ICON_W, ICON_H), (0xB8, 0xDC, 0xF8) if selected else (255, 255, 255))
+ if path and os.path.exists(path):
+ im = Image.open(path).convert("RGBA")
+ im.thumbnail((ICON_H - 6, ICON_H - 6), Image.LANCZOS)
+ cell.paste(im, ((ICON_W - im.width) // 2, (ICON_H - im.height) // 2), im)
+ return rgb565_be(cell)
+
+
+def put(h, at, data):
+ h[at:at + len(data)] = data
+
+
+def main():
+ a = argparse.ArgumentParser()
+ a.add_argument("code")
+ a.add_argument("out")
+ a.add_argument("--name", required=True)
+ a.add_argument("--icon")
+ a.add_argument("--version", default="01.00.0000")
+ o = a.parse_args()
+ code = open(o.code, "rb").read()
+ size = len(code) + HEADER + 4
+ h = bytearray(HEADER)
+ put(h, 0x000, bytes([0xAA, 0xAC, 0xBD, 0xAF, 0x90, 0x88, 0x9A, 0x8D, 0xD3, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF]))
+ # copy protection: the file size's bytes inverted, and two bytes from them
+ inv = bytes(~b & 0xFF for b in size.to_bytes(4, "big"))
+ put(h, 0x010, inv)
+ h[0x00E] = (h[0x013] - 0x41) & 0xFF
+ h[0x00F] = 0xFE
+ h[0x014] = (h[0x013] - 0xB8) & 0xFF
+ h[0x024], h[0x025] = 0x01, 0x01
+ put(h, 0x02E, len(code).to_bytes(4, "big")) # where the checksum's copy is, after the code
+ name = o.name.encode("ascii", "replace")
+ put(h, 0x040, name[:0x1B])
+ put(h, 0x05C, size.to_bytes(4, "big"))
+ internal = b"@" + bytes(c for c in name.upper() if 0x20 < c < 0x7F)[:9]
+ put(h, 0x060, internal)
+ for i in range(8): # its name in the menu's languages
+ put(h, 0x06B + i * 0x18, name[:0x17])
+ put(h, 0x130, o.version.encode()[:0xB])
+ put(h, 0x13C, time.strftime("%Y.%m%d.%H%M").encode()[:14])
+ put(h, 0x0EBC, os.path.basename(o.out).encode()[:0x143])
+ put(h, 0x1000, icon(o.icon, False))
+ put(h, 0x4000, icon(o.icon, True))
+ cksum = (sum(h) + sum(code)) & 0xFFFFFFFF
+ put(h, 0x020, cksum.to_bytes(4, "big"))
+ with open(o.out, "wb") as f:
+ f.write(h + code + cksum.to_bytes(4, "big"))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/platform/casio-cg/np_casio_cg.c b/platform/casio-cg/np_casio_cg.c
new file mode 100644
index 00000000..5b7e0200
--- /dev/null
+++ b/platform/casio-cg/np_casio_cg.c
@@ -0,0 +1,275 @@
+/* NumPlay on the Casio fx-CG50, Graph 90+E, fx-CG10 and fx-CG20, with libfxcg (the Prizm SDK).
+ *
+ * The screen: the 320 x 240 picture in the middle of the 384 x 216 one, its 240 rows shown on 216 (one in ten left
+ * out), drawn straight into the calculator's screen memory as the game pushes its rows. The keys: read from the
+ * keypad's own registers, every key held (platform/casio-cg/README.md has the map). The time: the calculator's
+ * clock, 128 ticks a second. The files: <game>.sav in the storage memory, written when the game saves (at most
+ * every few seconds) and when it ends.
+ *
+ * Build defines: NP_APP_ID (the game's id). */
+#include <fxcg/display.h>
+#include <fxcg/file.h>
+#include <fxcg/keyboard.h>
+#include <fxcg/rtc.h>
+#include <fxcg/system.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: 128 KB (the add-in's 512 KB hold its data, this and the stack) */
+__asm__(".section .bss\n.balign 16\n.global __heap_start\n__heap_start:\n.space 131072\n.global __heap_end\n__heap_end:\n"
+ ".previous\n");
+
+#define VW 384
+#define VH 216
+#define OX ((VW - EADK_SCREEN_WIDTH) / 2)
+static uint16_t *vram;
+static int16_t row_to[EADK_SCREEN_HEIGHT]; /* the screen row a picture row goes to, -1: left out */
+static int16_t row_from[VH]; /* the picture row a screen row shows */
+static bool started, dirty;
+static int last_update;
+
+static int ticks(void) { return RTC_GetTicks(); } /* 1/128 s, from midnight */
+static void start(void) {
+ if (started) return;
+ started = true;
+ Bdisp_EnableColor(1);
+ EnableStatusArea(3); /* no status bar */
+ vram = (uint16_t *)GetVRAMAddress();
+ for (int y = 0; y < EADK_SCREEN_HEIGHT; y++) row_to[y] = -1;
+ for (int d = 0; d < VH; d++) {
+ int s = d * EADK_SCREEN_HEIGHT / VH;
+ row_from[d] = (int16_t)s;
+ row_to[s] = (int16_t)d;
+ }
+ memset(vram, 0, VW * VH * 2);
+ last_update = ticks();
+}
+
+/* ---- the screen ---- */
+static void update(void) {
+ if (!dirty) return;
+ dirty = false;
+ Bdisp_PutDisp_DD();
+ last_update = ticks();
+}
+static void maybe_update(void) {
+ int t = ticks();
+ if (t - last_update >= 2 || t < last_update) update(); /* (about 60 times a second at most) */
+}
+void eadk_display_push_rect(eadk_rect_t r, const eadk_color_t *px) {
+ start();
+ int w = r.x + r.width > EADK_SCREEN_WIDTH ? EADK_SCREEN_WIDTH - r.x : r.width;
+ if (w <= 0) return;
+ for (int y = 0; y < r.height && r.y + y < EADK_SCREEN_HEIGHT; y++) {
+ int d = row_to[r.y + y];
+ if (d >= 0) memcpy(&vram[d * VW + OX + r.x], px + y * r.width, (size_t)w * 2);
+ }
+ dirty = true;
+ maybe_update();
+}
+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++) {
+ int d = row_to[y];
+ if (d < 0) continue;
+ uint16_t *o = &vram[d * VW + OX];
+ for (int x = r.x; x < r.x + r.width && x < EADK_SCREEN_WIDTH; x++) o[x] = c;
+ }
+ dirty = true;
+}
+void eadk_display_pull_rect(eadk_rect_t r, eadk_color_t *px) {
+ start();
+ for (int y = 0; y < r.height; y++) {
+ int sy = r.y + y, d = sy < EADK_SCREEN_HEIGHT ? row_to[sy] : -1;
+ if (d < 0 && sy < EADK_SCREEN_HEIGHT) d = row_to[sy > 0 ? sy - 1 : sy + 1]; /* (a row left out: its neighbour) */
+ for (int x = 0; x < r.width; x++) {
+ int sx = r.x + x;
+ px[y * r.width + x] = d >= 0 && sx < EADK_SCREEN_WIDTH ? vram[d * VW + OX + sx] : 0;
+ }
+ }
+}
+bool eadk_display_wait_for_vblank(void) {
+ start();
+ update();
+ return true;
+}
+
+/* ---- the keys: the keypad's registers, a bit a key (row and column of the keys' numbers, KEY_PRGM_*) ---- */
+static const struct {
+ uint8_t code;
+ uint8_t to;
+} KEYS[] = {
+ {KEY_PRGM_LEFT, eadk_key_left}, {KEY_PRGM_RIGHT, eadk_key_right}, {KEY_PRGM_UP, eadk_key_up},
+ {KEY_PRGM_DOWN, eadk_key_down}, {KEY_PRGM_RETURN, eadk_key_ok}, {KEY_PRGM_F1, eadk_key_exe},
+ {KEY_PRGM_EXIT, eadk_key_back}, {KEY_PRGM_MENU, eadk_key_home}, {44, eadk_key_backspace},
+ {KEY_PRGM_SHIFT, eadk_key_shift}, {KEY_PRGM_ALPHA, eadk_key_alpha}, {KEY_PRGM_OPTN, eadk_key_toolbox},
+ {58, eadk_key_var}, {76, eadk_key_xnt}, {KEY_PRGM_0, eadk_key_zero}, {KEY_PRGM_1, eadk_key_one},
+ {KEY_PRGM_2, eadk_key_two}, {KEY_PRGM_3, eadk_key_three}, {KEY_PRGM_4, eadk_key_four}, {KEY_PRGM_5, eadk_key_five},
+ {KEY_PRGM_6, eadk_key_six}, {KEY_PRGM_7, eadk_key_seven}, {KEY_PRGM_8, eadk_key_eight},
+ {KEY_PRGM_9, eadk_key_nine}, {42, eadk_key_plus}, {32, eadk_key_minus}, {43, eadk_key_multiplication},
+ {33, eadk_key_division}, {61, eadk_key_dot}, {35, eadk_key_comma}, {55, eadk_key_left_parenthesis},
+ {45, eadk_key_right_parenthesis}, {57, eadk_key_power}, {67, eadk_key_square}, {56, eadk_key_ln},
+ {66, eadk_key_log}, {46, eadk_key_sine}, {36, eadk_key_cosine}, {26, eadk_key_tangent}, {51, eadk_key_ee},
+ {41, eadk_key_ans}, {75, eadk_key_exp}, {65, eadk_key_pi}, {25, eadk_key_sqrt}, {KEY_PRGM_F2, eadk_key_imaginary},
+ {KEY_PRGM_ACON, eadk_key_home},
+};
+static bool held(const uint16_t *k, int code) {
+ int row = code % 10, col = code / 10 - 1;
+ return k[row >> 1] >> (col + 8 * (row & 1)) & 1;
+}
+eadk_keyboard_state_t eadk_keyboard_scan(void) {
+ start();
+ uint16_t k[8];
+ memcpy(k, (const void *)0xA44B0000, sizeof k);
+ eadk_keyboard_state_t s = 0;
+ for (unsigned i = 0; i < sizeof KEYS / sizeof KEYS[0]; i++)
+ if (held(k, KEYS[i].code)) s |= (eadk_keyboard_state_t)1 << KEYS[i].to;
+ 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;
+ update();
+ OS_InnerWait_ms(5);
+ } while (eadk_timing_millis() < until);
+ if (timeout) *timeout = 0;
+ return eadk_event_none;
+}
+
+/* ---- time, chance, the rest ---- */
+uint64_t eadk_timing_millis(void) {
+ static int first = -1, wraps;
+ static int before;
+ int t = ticks();
+ if (first < 0) first = before = t;
+ if (t < before) wraps++; /* (midnight) */
+ before = t;
+ return (uint64_t)((int64_t)wraps * 128 * 86400 + t - first) * 1000 / 128;
+}
+void eadk_timing_msleep(uint32_t ms) {
+ start();
+ update();
+ if (ms) OS_InnerWait_ms((int)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() * 2654435761u ^ 0x9E3779B9u ^ *(volatile uint32_t *)0xA449000C;
+ 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 (float)GetMainBatteryVoltage(1) / 100.0f; }
+bool eadk_usb_is_plugged(void) { return false; }
+const char *eadk_external_data = NULL;
+size_t eadk_external_data_size = 0;
+
+/* ---- the files: <game>.sav in the storage memory ---- */
+#define STORAGE_SIZE (16 * 1024)
+static uint8_t storage[STORAGE_SIZE + 64];
+static bool loaded, unsaved;
+static int last_save;
+static void save_path(uint16_t *out) {
+ char p[40] = "\\\\fls0\\";
+ strncat(p, NP_APP_ID, 8);
+ strcat(p, ".sav");
+ Bfile_StrToName_ncpy(out, p, 40);
+}
+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;
+ uint16_t path[40];
+ save_path(path);
+ int h = Bfile_OpenFile_OS(path, READ, 0);
+ if (h >= 0) {
+ int n = Bfile_GetFileSize_OS(h);
+ if (n > STORAGE_SIZE) n = STORAGE_SIZE;
+ if (n > 0) Bfile_ReadFile_OS(h, storage, n, 0);
+ Bfile_CloseFile_OS(h);
+ if (n < 2 || storage_end() + 2 > (uint32_t)n) memset(storage, 0, sizeof storage);
+ }
+ }
+ *size = STORAGE_SIZE;
+ return storage;
+}
+static void save_now(void) {
+ uint16_t path[40];
+ save_path(path);
+ size_t n = (storage_end() + 2 + 1) & ~(size_t)1; /* (an even size) */
+ Bfile_DeleteEntry(path);
+ if (Bfile_CreateEntry_OS(path, CREATEMODE_FILE, &n) < 0) return;
+ int h = Bfile_OpenFile_OS(path, WRITE, 0);
+ if (h < 0) return;
+ Bfile_WriteFile_OS(h, storage, (int)n);
+ Bfile_CloseFile_OS(h);
+ unsaved = false;
+ last_save = ticks();
+}
+void np_storage_changed(void) {
+ unsaved = true;
+ int t = ticks();
+ if (t - last_save >= 5 * 128 || t < last_save) save_now(); /* (flash is slow to write: a few seconds apart) */
+}
+
+bool np_slow(void) { return true; }
+const char *np_target(void) { return "casio-cg"; }
+void np_begin(void) { start(); }
+int np_end(void) {
+ if (unsaved) save_now();
+ return 0;
+}
+
+/* ---- the C library's ties to the calculator (newlib, built without its own malloc and system calls) ---- */
+#include <fxcg/heap.h>
+#include <errno.h>
+#include <sys/stat.h>
+void *malloc(size_t n) { return sys_malloc((int)n); }
+void free(void *p) {
+ if (p) sys_free(p);
+}
+void *calloc(size_t n, size_t s) {
+ void *p = sys_malloc((int)(n * s));
+ if (p) memset(p, 0, n * s);
+ return p;
+}
+void *realloc(void *p, size_t n) { return p ? sys_realloc(p, (int)n) : sys_malloc((int)n); }
+void *_malloc_r(struct _reent *r, size_t n) { (void)r; return malloc(n); }
+void _free_r(struct _reent *r, void *p) { (void)r; free(p); }
+void *_calloc_r(struct _reent *r, size_t n, size_t s) { (void)r; return calloc(n, s); }
+void *_realloc_r(struct _reent *r, void *p, size_t n) { (void)r; return realloc(p, n); }
+void abort(void) {
+ for (;;) {
+ int key;
+ GetKey(&key);
+ }
+}
+void *_sbrk(ptrdiff_t n) { (void)n; errno = ENOMEM; return (void *)-1; }
+int _write(int f, const void *b, size_t n) { (void)f, (void)b; return (int)n; }
+int _read(int f, void *b, size_t n) { (void)f, (void)b, (void)n; return 0; }
+int _close(int f) { (void)f; return -1; }
+int _fstat(int f, struct stat *s) { (void)f; s->st_mode = S_IFCHR; return 0; }
+int _isatty(int f) { (void)f; return 1; }
+int _lseek(int f, int o, int w) { (void)f, (void)o, (void)w; return 0; }
+void _exit(int c) { (void)c; abort(); }
+int _kill(int p, int s) { (void)p, (void)s; return -1; }
+int _getpid(void) { return 1; }
+int _gettimeofday(void *t, void *z) { (void)t, (void)z; return -1; }
diff --git a/platform/embed.py b/platform/embed.py
new file mode 100644
index 00000000..bed8b8e1
--- /dev/null
+++ b/platform/embed.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+"""A game's data files (its .S files: `.incbin` under a global label) as C, for targets with no assembler for them
+(the web's WebAssembly). The .S files are read as the calculator's assembler would (ELF), their macros (DATA_BIN...)
+from the flags given.
+
+ python3 platform/embed.py out.c [-Dflags...] file.S...
+
+Writes, for each label, `const unsigned char label[] = { #embed "file" };`, and for a `.long end - label` word,
+`const unsigned int name = sizeof label;`."""
+import os
+import re
+import subprocess
+import sys
+
+
+def main():
+ out, rest = sys.argv[1], sys.argv[2:]
+ flags = [a for a in rest if a.startswith(("-D", "-I", "-U"))]
+ files = [a for a in rest if not a.startswith("-")]
+ lines = ["/* made by platform/embed.py */", "#include <stdint.h>"]
+ for f in files:
+ text = subprocess.run(["cc", "-E", "-P", "-x", "assembler-with-cpp", "-U__APPLE__", "-D__ELF__", *flags, f],
+ capture_output=True, text=True, check=True).stdout
+ glob, label, blobs = set(), None, {}
+ for raw in text.splitlines():
+ s = raw.split("/*")[0].split("@")[0].strip()
+ m = re.match(r"\.globa?l\s+(\w+)", s)
+ if m:
+ glob.add(m.group(1))
+ continue
+ m = re.match(r"(\w+):\s*(.*)", s)
+ if m:
+ label, s = m.group(1), m.group(2)
+ m = re.match(r"\.incbin\s+\"([^\"]+)\"", s)
+ if m and label:
+ path = m.group(1)
+ for d in [".", os.path.dirname(f)] + [a[2:] for a in flags if a.startswith("-I")]:
+ if os.path.exists(os.path.join(d, path)):
+ path = os.path.join(d, path)
+ break
+ path = os.path.abspath(path)
+ blobs[label] = path
+ continue
+ m = re.match(r"\.(long|word|int)\s+(\w+)\s*-\s*(\w+)", s)
+ if m and label in glob and m.group(3) in blobs:
+ lines.append(f"const unsigned int {label} = {os.path.getsize(blobs[m.group(3)])};")
+ for name, path in blobs.items():
+ if name in glob:
+ lines.append(f"__attribute__((aligned(4))) const unsigned char {name}[] = {{\n#embed \"{path}\"\n}};")
+ open(out, "w").write("\n".join(lines) + "\n")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/platform/port.mk b/platform/port.mk
index 514b1c88..dc1ef1d5 100644
--- a/platform/port.mk
+++ b/platform/port.mk
@@ -59,18 +59,41 @@ $(PORT_OUT)/$(PORT_ID).tns: $(NS_SRC) $(PORT_ASM) $(wildcard $(NP_PLATFORM)/incl
@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
-WEB_DATA = $(if $(PORT_DATA),$(PORT_OUT)/data_embed.c)
+# The data files of PORT_ASM come in as C (platform/embed.py: #embed), WebAssembly having no assembler for them.
+WEB_DATA = $(if $(PORT_ASM),$(PORT_OUT)/data_embed.c)
port: $(PORT_OUT)/$(PORT_ID).html
-$(PORT_OUT)/data_embed.c: $(lastword $(subst :, ,$(PORT_DATA)))
+$(PORT_OUT)/data_embed.c: $(PORT_ASM) $(NP_PLATFORM)/embed.py
@mkdir -p $(@D)
- @printf '__attribute__((aligned(4))) const unsigned char %s[] = {\n#embed "%s"\n};\n' \
- '$(firstword $(subst :, ,$(PORT_DATA)))' '$(abspath $(lastword $(subst :, ,$(PORT_DATA))))' > $@
-$(PORT_OUT)/$(PORT_ID).html: $(PORT_SRC) $(WEB_DATA) $(wildcard $(NP_PLATFORM)/include/*.h) $(NP_PLATFORM)/sdl/np_sdl.c
+ $(Q) python3 $(NP_PLATFORM)/embed.py $@ $(filter -D% -I%,$(PORT_FLAGS)) $(PORT_ASM)
+$(PORT_OUT)/$(PORT_ID).html: $(PORT_SRC) $(WEB_DATA) $(wildcard $(NP_PLATFORM)/include/*.h) $(NP_PLATFORM)/sdl/np_sdl.c \
+ $(NP_PLATFORM)/sdl/shell.html
@mkdir -p $(@D)
$(Q) emcc $(PORT_CFLAGS) -g0 -O2 --use-port=sdl3 -sASYNCIFY -sALLOW_MEMORY_GROWTH -sENVIRONMENT=web \
+ --shell-file $(NP_PLATFORM)/sdl/shell.html \
$(PORT_SRC) $(WEB_DATA) $(NP_PLATFORM)/sdl/np_sdl.c $(PORT_COMMON) -o $@
@echo "WEB $@"
+else ifeq ($(TARGET),casio-cg)
+# the fx-CG50 / Graph 90+E: GCC for sh3eb-elf and libfxcg (FXCG: their folder, see platform/casio-cg/README.md)
+FXCG ?= $(HOME)/Documents/Assets/toolchains/casio
+CG_PATH = $(FXCG)/sh3eb/bin
+CG_DIR = $(PORT_OUT)/$(PORT_ID)
+CG_SRC = $(PORT_SRC) $(NP_PLATFORM)/casio-cg/np_casio_cg.c $(PORT_COMMON)
+CG_LIBC = $(FXCG)/newlib/sh-elf
+CG_FLAGS = $(PORT_CFLAGS) -Os -mb -m4a-nofpu -mhitachi -nostdlib -ffunction-sections -fdata-sections \
+ -isystem $(CG_LIBC)/include -idirafter $(FXCG)/libfxcg/include
+port: $(PORT_OUT)/$(PORT_ID).g3a
+$(PORT_OUT)/$(PORT_ID).g3a: $(CG_SRC) $(PORT_ASM) $(wildcard $(NP_PLATFORM)/include/*.h) $(NP_PLATFORM)/casio-cg/addin.ld \
+ $(NP_PLATFORM)/casio-cg/g3a.py
+ @mkdir -p $(CG_DIR)
+ $(Q) n=0; objs=; for f in $(CG_SRC) $(PORT_ASM); do n=$$((n+1)); objs="$$objs $(CG_DIR)/f$$n.o"; \
+ PATH="$(CG_PATH):$$PATH" sh3eb-elf-gcc $(CG_FLAGS) -c $$f -o $(CG_DIR)/f$$n.o || exit 1; \
+ case $$f in *.S) PATH="$(CG_PATH):$$PATH" sh3eb-elf-objcopy --prefix-symbols=_ $(CG_DIR)/f$$n.o;; esac; done; \
+ PATH="$(CG_PATH):$$PATH" sh3eb-elf-gcc -mb -m4a-nofpu -mhitachi -nostdlib -T$(NP_PLATFORM)/casio-cg/addin.ld \
+ $$objs -Wl,--gc-sections -Wl,-u,initialize -L$(CG_LIBC)/lib -L$(FXCG)/sdk/lib -lc -lm -lgcc -lfxcg -lc -lgcc \
+ -o $(CG_DIR)/$(PORT_ID).elf
+ $(Q) PATH="$(CG_PATH):$$PATH" sh3eb-elf-objcopy -O binary -R .ilram $(CG_DIR)/$(PORT_ID).elf $(CG_DIR)/$(PORT_ID).bin
+ $(Q) python3 $(NP_PLATFORM)/casio-cg/g3a.py $(CG_DIR)/$(PORT_ID).bin $@ --name "$(PORT_NAME)" --icon src/icon.png
+ @echo "CASIO $@"
else
port:
@echo "TARGET=$(TARGET): not a target yet (platform/README.md lists them)"; exit 1
diff --git a/platform/sdl/np_sdl.c b/platform/sdl/np_sdl.c
index 5586fc87..27e75d1a 100644
--- a/platform/sdl/np_sdl.c
+++ b/platform/sdl/np_sdl.c
@@ -91,7 +91,9 @@ static void script_shots(uint64_t now) {
/* 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(__EMSCRIPTEN__)
-/* (WebAssembly has no labels around a block: the few games that use _heap_start don't build for the web yet) */
+/* (WebAssembly: two labels in one block of the module's data) */
+__asm__(".section .bss.np_heap,\"\",@\n.p2align 4\n.globl _heap_start\n_heap_start:\n.skip 262144\n.size _heap_start, 262144\n"
+ ".globl _heap_end\n_heap_end:\n.skip 16\n.size _heap_end, 16\n");
#elif 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
diff --git a/platform/sdl/shell.html b/platform/sdl/shell.html
new file mode 100644
index 00000000..38489f35
--- /dev/null
+++ b/platform/sdl/shell.html
@@ -0,0 +1,28 @@
+<!doctype html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<title>NumPlay</title>
+<style>
+ html, body { margin: 0; height: 100%; background: #000; color: #9a9a9a; font: 14px/1.4 system-ui, sans-serif; }
+ body { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; }
+ canvas { display: block; image-rendering: pixelated; outline: none; max-width: 100vw; max-height: calc(100vh - 60px);
+ aspect-ratio: 4 / 3; width: min(100vw, calc((100vh - 60px) * 4 / 3)); height: auto; }
+ p { margin: 0; text-align: center; padding: 0 16px; }
+ kbd { color: #ddd; font: inherit; }
+</style>
+</head>
+<body>
+<canvas id="canvas" tabindex="-1" oncontextmenu="event.preventDefault()"></canvas>
+<p><kbd>Arrows</kbd> move · <kbd>Enter</kbd> OK · <kbd>Space</kbd> EXE · <kbd>Esc</kbd> Back · <kbd>Home</kbd> save and quit</p>
+<script>
+ var Module = { canvas: document.getElementById('canvas') };
+ Module.canvas.focus();
+ addEventListener('keydown', function (e) {
+ if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' ', 'Tab', 'Backspace'].indexOf(e.key) >= 0) e.preventDefault();
+ });
+</script>
+{{{ SCRIPT }}}
+</body>
+</html>