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: C++ files on the Casio and the web

Mason363770eb5033864
diff --git a/platform/casio-cg/np_cxx.cpp b/platform/casio-cg/np_cxx.cpp
new file mode 100644
index 00000000..190b08ab
--- /dev/null
+++ b/platform/casio-cg/np_cxx.cpp
@@ -0,0 +1,9 @@
+// C++'s few needs on the fx-CG, for games with C++ files (no exceptions, no RTTI, no C++ library)
+#include <stdlib.h>
+void *operator new(size_t n) { return malloc(n); }
+void *operator new[](size_t n) { return malloc(n); }
+void operator delete(void *p) noexcept { free(p); }
+void operator delete[](void *p) noexcept { free(p); }
+void operator delete(void *p, size_t) noexcept { free(p); }
+void operator delete[](void *p, size_t) noexcept { free(p); }
+extern "C" void __cxa_pure_virtual(void) { abort(); }
diff --git a/platform/common/np_cxx_rt.c b/platform/common/np_cxx_rt.c
new file mode 100644
index 00000000..93a200d8
--- /dev/null
+++ b/platform/common/np_cxx_rt.c
@@ -0,0 +1,3 @@
+/* The one piece of the C++ runtime the web builds of games with C++ files need (classes with pure virtual functions) */
+#include <stdlib.h>
+void __cxa_pure_virtual(void) { abort(); }
diff --git a/platform/port.mk b/platform/port.mk
index 298ab2a2..f1d81e76 100644
--- a/platform/port.mk
+++ b/platform/port.mk
@@ -85,7 +85,8 @@ $(PORT_OUT)/$(PORT_ID).html: $(PORT_SRC) $(WEB_DATA) $(PORT_CXX_OBJ) $(wildcard
@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) $(PORT_CXX_OBJ) $(NP_PLATFORM)/sdl/np_sdl.c $(PORT_COMMON) -o $@
+ $(PORT_SRC) $(WEB_DATA) $(PORT_CXX_OBJ) $(NP_PLATFORM)/sdl/np_sdl.c $(PORT_COMMON) \
+ $(if $(PORT_CXX),$(NP_PLATFORM)/common/np_cxx_rt.c) -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)
@@ -96,15 +97,20 @@ 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
+NP_CXX = PATH="$(CG_PATH):$$PATH" sh3eb-elf-g++ -Os -mb -m4a-nofpu -mhitachi -nostdlib -ffunction-sections \
+ -fdata-sections -isystem $(CG_LIBC)/include -idirafter $(FXCG)/libfxcg/include
+CG_CXX = $(if $(PORT_CXX),$(NP_PLATFORM)/casio-cg/np_cxx.cpp)
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
+$(PORT_OUT)/$(PORT_ID).g3a: $(CG_SRC) $(PORT_ASM) $(PORT_CXX_OBJ) $(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; \
+ for f in $(CG_CXX); do n=$$((n+1)); objs="$$objs $(CG_DIR)/f$$n.o"; \
+ $(NP_CXX) $(NP_CXXFLAGS) -c $$f -o $(CG_DIR)/f$$n.o || exit 1; 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 \
+ $$objs $(PORT_CXX_OBJ) -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