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: Retro-Go's games from a folder, NumPico's carts built in

Mason36326c8e7503635
diff --git a/games/numpico/Makefile b/games/numpico/Makefile
index 8c2fa832..c22eee7e 100644
--- a/games/numpico/Makefile
+++ b/games/numpico/Makefile
@@ -107,6 +107,12 @@ perf: $(OBJ)
# the other targets (platform/port.mk): make port TARGET=sdl, web, nspire, ti84ce
PORT_ID = numpico
PORT_NAME = NumPico
-PORT_SRC = src/femto8/lua/lapi.c src/femto8/lua/lauxlib.c src/femto8/lua/lbaselib.c src/femto8/lua/lcode.c src/femto8/lua/lcorolib.c src/femto8/lua/lctype.c src/femto8/lua/ldebug.c src/femto8/lua/ldo.c src/femto8/lua/lfunc.c src/femto8/lua/lgc.c src/femto8/lua/linit.c src/femto8/lua/llex.c src/femto8/lua/lmem.c src/femto8/lua/lobject.c src/femto8/lua/lopcodes.c src/femto8/lua/lparser.c src/femto8/lua/lpico8lib.c src/femto8/lua/lstate.c src/femto8/lua/lstring.c src/femto8/lua/lstrlib.c src/femto8/lua/ltable.c src/femto8/lua/ltablib.c src/femto8/lua/ltm.c src/femto8/lua/lvm.c src/femto8/lua/lzio.c src/femto8/p8_lua.c src/pico.c src/heap.c src/main.c src/carts.c src/plat_eadk.c ../../launcher/src/gfx.c ../../launcher/src/xfont.c
-PORT_FLAGS = -DEPSILON_XL=1 -DNDEBUG -I../../launcher/src -Isrc -Isrc/femto8 -Isrc/femto8/lua
+PORT_SRC = $(CORE_SRC) src/main.c src/carts.c src/plat_eadk.c $(L)/gfx.c $(L)/xfont.c $(PORT_OUT)/carts.c
+# (Lua's headers for quoted includes only, and its functions renamed there: the TI-Nspire has a Lua of its own)
+PORT_FLAGS = -DEPSILON_XL=1 -DNDEBUG -I$(L) -Isrc -Isrc/femto8 -iquote src/femto8/lua \
+ $(if $(filter nspire,$(TARGET)),-include src/nspire_lua.h)
include ../../platform/port.mk
+# the carts, built in as on the calculator (CARTS, CARTS_LIST)
+$(PORT_OUT)/carts.c: FORCE
+ @mkdir -p $(@D)
+ $(Q) $(PYTHON) -I tools/p8cart.py --c $@ $(CARTS) $(if $(CARTS_LIST),--list $(CARTS_LIST))
diff --git a/games/numpico/src/main.c b/games/numpico/src/main.c
index 7b18d3b2..56427d34 100644
--- a/games/numpico/src/main.c
+++ b/games/numpico/src/main.c
@@ -172,7 +172,11 @@ static void none_scene(void *ctx) {
blank_scene(ctx);
ptext(14, 12, "numpico", WHITE, 2);
ptext(14, 70, "no carts yet", WHITE, 2);
+#ifdef NP_PORT /* (another target: the carts are built in, CARTS in the Makefile) */
+ paragraph(14, 100, 292, "build numpico with your .p8 and .p8.png carts: make port carts=\"...\"", GREY, 2);
+#else
paragraph(14, 100, 292, "add your .p8 and .p8.png carts in numplay studio.", GREY, 2);
+#endif
keys_line("home quit", NULL);
}
@@ -311,6 +315,6 @@ int numpico_main(void) {
return plat_end();
}
-#if PLATFORM_DEVICE
+#if PLATFORM_DEVICE || defined(NP_PORT)
int main(void) { return numpico_main(); }
#endif
diff --git a/games/numpico/src/nspire_lua.h b/games/numpico/src/nspire_lua.h
new file mode 100644
index 00000000..d9c48926
--- /dev/null
+++ b/games/numpico/src/nspire_lua.h
@@ -0,0 +1,85 @@
+/* NumPico on the TI-Nspire (platform/nspire): the calculator has a Lua of its own, which Ndless's library
+ * names, so femto8's takes other names there (the Makefile's port lines include this file). */
+#ifndef NUMPICO_NSPIRE_LUA_H
+#define NUMPICO_NSPIRE_LUA_H
+#define luaL_addlstring p8_luaL_addlstring
+#define luaL_addstring p8_luaL_addstring
+#define luaL_addvalue p8_luaL_addvalue
+#define luaL_argerror p8_luaL_argerror
+#define luaL_buffinit p8_luaL_buffinit
+#define luaL_callmeta p8_luaL_callmeta
+#define luaL_checkany p8_luaL_checkany
+#define luaL_checkinteger p8_luaL_checkinteger
+#define luaL_checklstring p8_luaL_checklstring
+#define luaL_checknumber p8_luaL_checknumber
+#define luaL_checkoption p8_luaL_checkoption
+#define luaL_checkstack p8_luaL_checkstack
+#define luaL_checktype p8_luaL_checktype
+#define luaL_checkudata p8_luaL_checkudata
+#define luaL_error p8_luaL_error
+#define luaL_getmetafield p8_luaL_getmetafield
+#define luaL_gsub p8_luaL_gsub
+#define luaL_loadstring p8_luaL_loadstring
+#define luaL_newmetatable p8_luaL_newmetatable
+#define luaL_optinteger p8_luaL_optinteger
+#define luaL_optlstring p8_luaL_optlstring
+#define luaL_optnumber p8_luaL_optnumber
+#define luaL_pushresult p8_luaL_pushresult
+#define luaL_ref p8_luaL_ref
+#define luaL_unref p8_luaL_unref
+#define luaL_where p8_luaL_where
+#define lua_atpanic p8_lua_atpanic
+#define lua_checkstack p8_lua_checkstack
+#define lua_close p8_lua_close
+#define lua_concat p8_lua_concat
+#define lua_createtable p8_lua_createtable
+#define lua_dump p8_lua_dump
+#define lua_error p8_lua_error
+#define lua_gc p8_lua_gc
+#define lua_getfield p8_lua_getfield
+#define lua_getmetatable p8_lua_getmetatable
+#define lua_getstack p8_lua_getstack
+#define lua_gettable p8_lua_gettable
+#define lua_gettop p8_lua_gettop
+#define lua_insert p8_lua_insert
+#define lua_iscfunction p8_lua_iscfunction
+#define lua_isnumber p8_lua_isnumber
+#define lua_isstring p8_lua_isstring
+#define lua_isuserdata p8_lua_isuserdata
+#define lua_load p8_lua_load
+#define lua_newstate p8_lua_newstate
+#define lua_newthread p8_lua_newthread
+#define lua_newuserdata p8_lua_newuserdata
+#define lua_next p8_lua_next
+#define lua_pushboolean p8_lua_pushboolean
+#define lua_pushcclosure p8_lua_pushcclosure
+#define lua_pushfstring p8_lua_pushfstring
+#define lua_pushinteger p8_lua_pushinteger
+#define lua_pushlstring p8_lua_pushlstring
+#define lua_pushnil p8_lua_pushnil
+#define lua_pushnumber p8_lua_pushnumber
+#define lua_pushstring p8_lua_pushstring
+#define lua_pushvalue p8_lua_pushvalue
+#define lua_rawequal p8_lua_rawequal
+#define lua_rawget p8_lua_rawget
+#define lua_rawgeti p8_lua_rawgeti
+#define lua_rawset p8_lua_rawset
+#define lua_rawseti p8_lua_rawseti
+#define lua_remove p8_lua_remove
+#define lua_replace p8_lua_replace
+#define lua_resume p8_lua_resume
+#define lua_setfield p8_lua_setfield
+#define lua_setmetatable p8_lua_setmetatable
+#define lua_settable p8_lua_settable
+#define lua_settop p8_lua_settop
+#define lua_status p8_lua_status
+#define lua_toboolean p8_lua_toboolean
+#define lua_tocfunction p8_lua_tocfunction
+#define lua_tolstring p8_lua_tolstring
+#define lua_topointer p8_lua_topointer
+#define lua_tothread p8_lua_tothread
+#define lua_touserdata p8_lua_touserdata
+#define lua_type p8_lua_type
+#define lua_typename p8_lua_typename
+#define lua_xmove p8_lua_xmove
+#endif
diff --git a/games/numpico/src/pico.c b/games/numpico/src/pico.c
index 526c1262..4fb54ef5 100644
--- a/games/numpico/src/pico.c
+++ b/games/numpico/src/pico.c
@@ -570,7 +570,7 @@ int pico_panic(lua_State *l) {
end_run(PICO_MEMORY);
}
-#if !defined(HOST)
+#if !defined(HOST) && !defined(NP_PORT)
/* (Lua's last resort, after a panic that returned: never here, but not the C library's, which needs
the calculator to have a process to kill) */
void abort(void) { end_run(PICO_MEMORY); }
diff --git a/games/numpico/src/plat_eadk.c b/games/numpico/src/plat_eadk.c
index 773640b8..f8192227 100644
--- a/games/numpico/src/plat_eadk.c
+++ b/games/numpico/src/plat_eadk.c
@@ -39,10 +39,12 @@ void plat_heap(uint8_t **start, uint8_t **end) {
extern char _heap_start[], _heap_end[];
*start = (uint8_t *)_heap_start, *end = (uint8_t *)_heap_end;
}
+#ifndef NP_PORT /* (another target's malloc is its own) */
/* nothing takes memory with malloc (Lua has the RAM after the app's: heap.c), but newlib's printf names it */
void *_sbrk(ptrdiff_t incr) {
(void)incr;
return (void *)-1;
}
+#endif
void plat_begin(void) { np_app_begin(); }
int plat_end(void) { return np_app_end(); }
diff --git a/games/retrogo/Makefile b/games/retrogo/Makefile
index a60456d6..0ebea4ba 100644
--- a/games/retrogo/Makefile
+++ b/games/retrogo/Makefile
@@ -130,6 +130,6 @@ test: $(BUILD_DIR)/host/core $(BUILD_DIR)/host/romtable $(BUILD_DIR)/host/pack $
# the other targets (platform/port.mk): make port TARGET=sdl, web, nspire, ti84ce
PORT_ID = retrogo
PORT_NAME = Retro-Go
-PORT_SRC = src/gb/cpu.c src/gb/hw.c src/gb/mem.c src/gb/lcdc.c src/gb/rtc.c src/gb/lcd.c src/gb/gb.c src/nes/nes6502.c src/nes/nes_ppu.c src/nes/nes_mmc.c src/nes/mmclist.c src/nes/nes_apu.c src/nes/nes.c src/nes/mappers/map000.c src/nes/mappers/map001.c src/nes/mappers/map002.c src/nes/mappers/map003.c src/nes/mappers/map004.c src/nes/mappers/map005.c src/nes/mappers/map007.c src/nes/mappers/map008.c src/nes/mappers/map009.c src/nes/mappers/map011.c src/nes/mappers/map015.c src/nes/mappers/map016.c src/nes/mappers/map018.c src/nes/mappers/map019.c src/nes/mappers/map024.c src/nes/mappers/map032.c src/nes/mappers/map033.c src/nes/mappers/map034.c src/nes/mappers/map040.c src/nes/mappers/map064.c src/nes/mappers/map065.c src/nes/mappers/map066.c src/nes/mappers/map070.c src/nes/mappers/map075.c src/nes/mappers/map078.c src/nes/mappers/map079.c src/nes/mappers/map085.c src/nes/mappers/map094.c src/nes/mappers/map099.c src/nes/mappers/map231.c src/nes/mappers/mapvrc.c src/sms/z80.c src/sms/sms.c src/sms/coleco_bios.c src/main.c src/romtable.c src/pack.c ../../launcher/src/gfx.c ../../launcher/src/xfont.c
+PORT_SRC = src/gb/cpu.c src/gb/hw.c src/gb/mem.c src/gb/lcdc.c src/gb/rtc.c src/gb/lcd.c src/gb/gb.c src/nes/nes6502.c src/nes/nes_ppu.c src/nes/nes_mmc.c src/nes/mmclist.c src/nes/nes_apu.c src/nes/nes.c src/nes/mappers/map000.c src/nes/mappers/map001.c src/nes/mappers/map002.c src/nes/mappers/map003.c src/nes/mappers/map004.c src/nes/mappers/map005.c src/nes/mappers/map007.c src/nes/mappers/map008.c src/nes/mappers/map009.c src/nes/mappers/map011.c src/nes/mappers/map015.c src/nes/mappers/map016.c src/nes/mappers/map018.c src/nes/mappers/map019.c src/nes/mappers/map024.c src/nes/mappers/map032.c src/nes/mappers/map033.c src/nes/mappers/map034.c src/nes/mappers/map040.c src/nes/mappers/map064.c src/nes/mappers/map065.c src/nes/mappers/map066.c src/nes/mappers/map070.c src/nes/mappers/map075.c src/nes/mappers/map078.c src/nes/mappers/map079.c src/nes/mappers/map085.c src/nes/mappers/map094.c src/nes/mappers/map099.c src/nes/mappers/map231.c src/nes/mappers/mapvrc.c src/sms/z80.c src/sms/sms.c src/sms/coleco_bios.c src/main.c src/romtable.c src/pack.c src/roms_port.c ../../launcher/src/gfx.c ../../launcher/src/xfont.c
PORT_FLAGS = -Isrc/nes
include ../../platform/port.mk
diff --git a/games/retrogo/src/main.c b/games/retrogo/src/main.c
index 15b62931..2994579c 100644
--- a/games/retrogo/src/main.c
+++ b/games/retrogo/src/main.c
@@ -49,6 +49,12 @@ void np_push(int x, int y, int w, int h, const color_t *px) {
#if PLATFORM_DEVICE
extern const uint8_t np_roms[], np_retrogo_end[];
#define TABLE_LIMIT ((uintptr_t)np_retrogo_end > (uintptr_t)np_roms ? (uint32_t)((uintptr_t)np_retrogo_end - (uintptr_t)np_roms) : 0)
+#elif defined(NP_PORT) /* (another target: the games are files in a folder, roms_port.c) */
+const uint8_t *rg_port_roms(void);
+uint32_t rg_port_roms_size(void);
+const char *rg_port_folder_shown(void);
+#define np_roms rg_port_roms()
+#define TABLE_LIMIT rg_port_roms_size()
#else
extern const uint8_t np_roms[];
extern const uint32_t np_roms_size;
@@ -1350,9 +1356,15 @@ static void picker_scene(void *ctx) {
background(sys, browse ? 4 : 0);
if (!ntabs) { /* (no games: Retro-Go's welcome, and where they come from) */
header(0, 0);
+#ifdef NP_PORT
+ const char *const lines[] = {T("Welcome to Retro-Go!"), "", "Add games to this folder:", rg_port_folder_shown(),
+ T("Game Boy, Game Boy Color, NES,"), T("Master System, Game Gear, SG-1000,"),
+ T("ColecoVision."), T("Home: quit")};
+#else
static const char *const lines[] = {T("Welcome to Retro-Go!"), "", T("Add games on NumPlay's"),
T("customizer website:"), T("Game Boy, Game Boy Color, NES,"), T("Master System, Game Gear, SG-1000,"),
T("ColecoVision."), T("Home: quit")};
+#endif
for (int i = 0; i < 8; i++) gfx_text(&np_font_body, 8, 80 + 18 * i, lines[i], i ? 0xAD55 : WHITE, 256);
return;
}
diff --git a/games/retrogo/src/roms_port.c b/games/retrogo/src/roms_port.c
new file mode 100644
index 00000000..e95fb2fa
--- /dev/null
+++ b/games/retrogo/src/roms_port.c
@@ -0,0 +1,138 @@
+/* Retro-Go on NumPlay's other targets (platform/): the games are files in a folder, read once into the table
+ * tools/romtable.py makes (main.c reads it as it reads np_roms on the calculator).
+ *
+ * The folder: NP_ROMS, or retrogo in the folder of NumPlay's files (NP_SAVES, ~/.numplay on a computer,
+ * /documents/retrogo on a TI-Nspire, where a file's name may end with .tns). A file's last extension gives its
+ * system (.gb .gbc .nes .sms .gg .sg .col, in any case), the rest of its name the game's. */
+#ifdef NP_PORT
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "np_port.h"
+#include "romtable.h"
+
+static const struct {
+ const char *ext;
+ uint8_t system;
+} EXT[] = {{"gb", RG_SYS_GB}, {"gbc", RG_SYS_GBC}, {"nes", RG_SYS_NES}, {"sms", RG_SYS_SMS},
+ {"gg", RG_SYS_GG}, {"sg", RG_SYS_SG}, {"col", RG_SYS_CV}};
+
+static int lower(int c) { return c >= 'A' && c <= 'Z' ? c + 32 : c; }
+static bool same(const char *a, const char *b, size_t n) {
+ for (size_t i = 0; i < n; i++)
+ if (lower((unsigned char)a[i]) != lower((unsigned char)b[i])) return false;
+ return !b[n];
+}
+
+const char *rg_port_folder(void) {
+ static char path[512];
+ if (path[0]) return path;
+ const char *env = getenv("NP_ROMS"), *saves = getenv("NP_SAVES"), *home = getenv("HOME");
+ if (!home) home = getenv("USERPROFILE");
+ if (env) snprintf(path, sizeof path, "%s", env);
+ else if (!strcmp(np_target(), "nspire")) snprintf(path, sizeof path, "/documents/retrogo");
+ else if (saves) snprintf(path, sizeof path, "%s/retrogo", saves);
+ else snprintf(path, sizeof path, "%s/.numplay/retrogo", home ? home : ".");
+ return path;
+}
+
+/* the folder as the welcome screen shows it: ~ for the home folder, the end of a long one */
+const char *rg_port_folder_shown(void) {
+ static char shown[40];
+ const char *p = rg_port_folder(), *home = getenv("HOME");
+ size_t h = home ? strlen(home) : 0;
+ char tmp[520];
+ if (h > 1 && !strncmp(p, home, h) && p[h] == '/') snprintf(tmp, sizeof tmp, "~%s", p + h);
+ else snprintf(tmp, sizeof tmp, "%s", p);
+ size_t n = strlen(tmp);
+ if (n < sizeof shown) memcpy(shown, tmp, n + 1);
+ else memcpy(shown, "...", 3), memcpy(shown + 3, tmp + n - (sizeof shown - 4), sizeof shown - 3);
+ return shown;
+}
+
+/* a file's system (0: not a game), and the length of its name without its extension */
+static uint8_t system_of(const char *file, size_t *name_len) {
+ size_t n = strlen(file);
+ if (n > 4 && same(file + n - 4, ".tns", 4)) n -= 4;
+ size_t dot = n;
+ while (dot > 0 && file[dot - 1] != '.') dot--;
+ if (dot < 2) return 0;
+ for (size_t i = 0; i < sizeof EXT / sizeof EXT[0]; i++)
+ if (same(file + dot, EXT[i].ext, n - dot)) {
+ *name_len = dot - 1;
+ return EXT[i].system;
+ }
+ return 0;
+}
+
+static void wr32(uint8_t *p, uint32_t v) {
+ for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i));
+}
+
+static uint8_t empty[12], *table;
+static uint32_t table_size;
+
+static void load(void) {
+ if (table) return;
+ wr32(empty, RG_ROMS_MAGIC), wr32(empty + 4, RG_ROMS_VERSION);
+ table = empty, table_size = sizeof empty;
+ struct {
+ char path[600];
+ uint32_t size;
+ uint8_t system, name[RG_ROMS_NAME];
+ } *games = calloc(RG_MAX_ROMS, sizeof *games);
+ DIR *dir = games ? opendir(rg_port_folder()) : NULL;
+ uint32_t count = 0, total = 0;
+ for (struct dirent *e; dir && count < RG_MAX_ROMS && (e = readdir(dir));) {
+ size_t name_len;
+ uint8_t system = system_of(e->d_name, &name_len);
+ if (!system) continue;
+ snprintf(games[count].path, sizeof games[count].path, "%s/%s", rg_port_folder(), e->d_name);
+ FILE *f = fopen(games[count].path, "rb");
+ if (!f) continue;
+ long n = fseek(f, 0, SEEK_END) ? -1 : ftell(f);
+ fclose(f);
+ if (n <= 0 || n > 64L << 20) continue;
+ /* (the name cut to 40 bytes, not inside a letter) */
+ size_t k = name_len < RG_ROMS_NAME ? name_len : RG_ROMS_NAME;
+ while (k < name_len && k > 0 && ((unsigned char)e->d_name[k] & 0xC0) == 0x80) k--;
+ memcpy(games[count].name, e->d_name, k);
+ games[count].size = (uint32_t)n, games[count].system = system;
+ total += ((uint32_t)n + 3) & ~3u;
+ count++;
+ }
+ if (dir) closedir(dir);
+ uint32_t first = (12 + RG_ROMS_ENTRY * count + 3) & ~3u;
+ uint8_t *t = count ? calloc(1, first + total) : NULL;
+ if (t) {
+ wr32(t, RG_ROMS_MAGIC), wr32(t + 4, RG_ROMS_VERSION);
+ uint32_t at = first, kept = 0;
+ for (uint32_t i = 0; i < count; i++) {
+ FILE *f = fopen(games[i].path, "rb");
+ if (!f) continue;
+ bool whole = fread(t + at, 1, games[i].size, f) == games[i].size;
+ fclose(f);
+ if (!whole) continue;
+ uint8_t *e = t + 12 + RG_ROMS_ENTRY * kept++;
+ wr32(e, at), wr32(e + 4, games[i].size);
+ e[8] = games[i].system;
+ memcpy(e + 12, games[i].name, RG_ROMS_NAME);
+ at += (games[i].size + 3) & ~3u;
+ }
+ wr32(t + 8, kept);
+ table = t, table_size = at;
+ }
+ free(games);
+}
+
+/* the table, and its size (read the first time either is asked) */
+const uint8_t *rg_port_roms(void) {
+ load();
+ return table;
+}
+uint32_t rg_port_roms_size(void) {
+ load();
+ return table_size;
+}
+#endif