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.

Balatro CE: numbers kept once written, key masks made when compiling

Mason36378a6f2c6aaf5
diff --git a/games/balatro/ce/makefile b/games/balatro/ce/makefile
index a5f11da8..004bc3a2 100644
--- a/games/balatro/ce/makefile
+++ b/games/balatro/ce/makefile
@@ -13,13 +13,13 @@ LTO = YES
OUTPUT_MAP = YES
GEN = gen
DBL ?= -Xclang -mdouble=64
-SHARED = game score jokers cons ev util save draw
+SHARED = game score jokers cons ev save draw
CEFLAGS = $(DBL) -DNP_PORT=1 -DNP_CE=1 -DNP_APP_ID=\"balatro\" -DNP_APP_NAME=\"Balatro\" \
-I../src -I../../../platform/include -I$(GEN) -Isrc -Wno-unused-function -Wno-unused-parameter
# (no inlining: 11 KB smaller; a frame pointer biased per function: 15 KB smaller)
CFLAGS = -Wall -Oz -fno-inline-functions -mllvm -z80-frame-bias-policy=auto $(XFLAGS) $(CEFLAGS)
-EXTRA_C_SOURCES = $(SHARED:%=../src/%.c) $(GEN)/main.c $(GEN)/ui.c $(GEN)/data.c $(GEN)/ce_art.c
-DEPS = $(GEN)/ce_art.h $(GEN)/main.c $(GEN)/ui.c $(GEN)/data.c
+EXTRA_C_SOURCES = $(SHARED:%=../src/%.c) $(GEN)/main.c $(GEN)/ui.c $(GEN)/util.c $(GEN)/data.c $(GEN)/ce_art.c
+DEPS = $(GEN)/ce_art.h $(GEN)/main.c $(GEN)/ui.c $(GEN)/util.c $(GEN)/data.c
LINKER_SCRIPT = ce.ld
# (the toolchain's makefile builds bin/BALATRO.all: everything linked; the rules below cut it up)
TARGET = $(NAME).all
@@ -34,7 +34,7 @@ ARTVARS = $(shell cat $(GEN)/appvars.txt 2>/dev/null)
.PHONY: all host check
all: $(BINDIR)/$(NAME).8xp $(CODEVARS:%=$(BINDIR)/%.8xv) $(BINDIR)/art.stamp
-$(GEN)/main.c $(GEN)/ui.c: $(GEN)/%.c: ../src/%.c tools/cesrc.py
+$(GEN)/main.c $(GEN)/ui.c $(GEN)/util.c: $(GEN)/%.c: ../src/%.c tools/cesrc.py
$(Q)mkdir -p $(GEN)
$(Q)$(PY) tools/cesrc.py $< $@
diff --git a/games/balatro/ce/src/ce_main.c b/games/balatro/ce/src/ce_main.c
index d4c7ef96..c0f7861d 100644
--- a/games/balatro/ce/src/ce_main.c
+++ b/games/balatro/ce/src/ce_main.c
@@ -105,21 +105,24 @@ void eadk_timing_msleep(uint32_t ms) {
#define KB_MODE (*(volatile uint8_t *)0xF50000)
#define KB_DATA(g) (*(volatile uint16_t *)(0xF50010 + 2 * (g)))
#define KB_ON (*(volatile uint8_t *)0xF00020 & 1)
+#define K(k) ((eadk_keyboard_state_t)1 << (k)) /* (made when compiling: the eZ80 shifts 64 bits by calls) */
static const struct {
- uint8_t group, bit, key;
+ uint8_t group, bit;
+ eadk_keyboard_state_t key;
} KEYS[] = {
- {7, 1 << 1, eadk_key_left}, {7, 1 << 2, eadk_key_right}, {7, 1 << 3, eadk_key_up}, {7, 1 << 0, eadk_key_down},
- {1, 1 << 5, eadk_key_ok}, {6, 1 << 0, eadk_key_exe}, {6, 1 << 6, eadk_key_back}, {1, 1 << 6, eadk_key_home},
- {1, 1 << 7, eadk_key_backspace}, {2, 1 << 7, eadk_key_alpha}, {1, 1 << 4, eadk_key_shift},
- {2, 1 << 6, eadk_key_toolbox}, {5, 1 << 6, eadk_key_var}, {3, 1 << 7, eadk_key_xnt},
+ {7, 1 << 1, K(eadk_key_left)}, {7, 1 << 2, K(eadk_key_right)}, {7, 1 << 3, K(eadk_key_up)},
+ {7, 1 << 0, K(eadk_key_down)}, {1, 1 << 5, K(eadk_key_ok)}, {6, 1 << 0, K(eadk_key_exe)},
+ {6, 1 << 6, K(eadk_key_back)}, {1, 1 << 6, K(eadk_key_home)}, {1, 1 << 7, K(eadk_key_backspace)},
+ {2, 1 << 7, K(eadk_key_alpha)}, {1, 1 << 4, K(eadk_key_shift)}, {2, 1 << 6, K(eadk_key_toolbox)},
+ {5, 1 << 6, K(eadk_key_var)}, {3, 1 << 7, K(eadk_key_xnt)},
};
eadk_keyboard_state_t eadk_keyboard_scan(void) {
KB_MODE = 2;
while (KB_MODE & 3) {}
eadk_keyboard_state_t s = 0;
for (unsigned i = 0; i < sizeof KEYS / sizeof KEYS[0]; i++)
- if ((uint8_t)KB_DATA(KEYS[i].group) & KEYS[i].bit) s |= (eadk_keyboard_state_t)1 << KEYS[i].key;
- if (KB_ON) s |= (eadk_keyboard_state_t)1 << eadk_key_home;
+ if ((uint8_t)KB_DATA(KEYS[i].group) & KEYS[i].bit) s |= KEYS[i].key;
+ if (KB_ON) s |= K(eadk_key_home);
return s;
}
diff --git a/games/balatro/ce/src/gfx_ce.c b/games/balatro/ce/src/gfx_ce.c
index ae233ace..fdafad13 100644
--- a/games/balatro/ce/src/gfx_ce.c
+++ b/games/balatro/ce/src/gfx_ce.c
@@ -157,7 +157,7 @@ static uint8_t nearest_fixed(C c, int *d) {
static void shade_tabs_reset(void);
uint8_t ce_pal(C c) {
- uint8_t hi = (uint8_t)(c >> 8), h = (uint8_t)((uint8_t)c + (uint8_t)(hi << 3 | hi >> 5)); /* (bytes: the eZ80 shifts by calls) */
+ uint8_t hi = ((const uint8_t *)&c)[1], h = (uint8_t)((uint8_t)c + (uint8_t)(hi << 3 | hi >> 5)); /* (bytes: the eZ80 shifts by calls) */
if (pcache[h].ok && pcache[h].c == c) {
uint8_t v = pcache[h].v;
if (v < CE_DYN0 || v >= CE_SWIRL0) return v;
@@ -381,13 +381,22 @@ static const C code_col[17] = {
HEXC(0xA782D1), HEXC(0x13AFCE), 0, HEXC(0x4584FA), HEXC(0x4F6367), 0, 0xFFFF, HEXC(0x4CA893), 0,
};
-static const glyph_t *font(int f) { return (f & T_LARGE) ? font_large : (f & T_MED) ? font_med : font_small; }
+/* the flags' low byte (the sizes, T_X2, T_TIGHT...), and T_COND in the next */
+#define FLO(f) ((uint8_t)(f))
+#define FCOND(f) (((const uint8_t *)&(f))[1] & (T_COND >> 8))
+static const glyph_t *font(int f) { return FLO(f) & T_LARGE ? font_large : FLO(f) & T_MED ? font_med : font_small; }
static const uint8_t *font_bits(int f) {
- return (f & T_LARGE) ? font_large_bits : (f & T_MED) ? font_med_bits : font_small_bits;
+ return FLO(f) & T_LARGE ? font_large_bits : FLO(f) & T_MED ? font_med_bits : font_small_bits;
+}
+static int glyph_rows(int f) { return FLO(f) & T_LARGE ? FONT_LARGE_H : FLO(f) & T_MED ? FONT_MED_H : FONT_SMALL_H; }
+int g_cap(int f) {
+ int c = FLO(f) & T_LARGE ? 11 : FLO(f) & T_MED ? 9 : 7;
+ return FLO(f) & T_X2 ? c + c : c;
+}
+int g_pitch(int f) {
+ int p = FLO(f) & T_LARGE ? 15 : FLO(f) & T_MED ? 13 : LINE_H;
+ return FLO(f) & T_X2 ? p + p : p;
}
-static int glyph_rows(int f) { return (f & T_LARGE) ? FONT_LARGE_H : (f & T_MED) ? FONT_MED_H : FONT_SMALL_H; }
-int g_cap(int f) { return ((f & T_LARGE) ? 11 : (f & T_MED) ? 9 : 7) * ((f & T_X2) ? 2 : 1); }
-int g_pitch(int f) { return ((f & T_LARGE) ? 15 : (f & T_MED) ? 13 : LINE_H) * ((f & T_X2) ? 2 : 1); }
static const glyph_t *glyph(int f, unsigned ch, const uint8_t **bits) {
if ((f & (T_COND | T_LARGE)) == (T_COND | T_LARGE) && ch >= '0' && ch <= '9') {
@@ -421,7 +430,7 @@ static int advance(int f, unsigned ch, const glyph_t *g) {
/* a size's advances (characters 32 to 130), made when first asked for: measuring texts is a frame's busiest work */
#define NADV 6
static struct {
- uint16_t key;
+ uint8_t key;
int8_t a[99];
} advs[NADV];
static uint8_t adv_next;
@@ -429,17 +438,17 @@ static uint8_t adv_next;
long ce_adv_made;
#endif
static const int8_t *adv_tab(int f) {
- unsigned key = (unsigned)(f & (T_LARGE | T_MED | T_COND | T_TIGHT)) + 1;
+ uint8_t key = (uint8_t)((FLO(f) & (T_LARGE | T_MED | T_TIGHT)) | (FCOND(f) ? T_X2 : 0)) + 1; /* (T_X2: not a size) */
for (int i = 0; i < NADV; i++)
if (advs[i].key == key) return advs[i].a - 32;
int i = adv_next;
adv_next = (uint8_t)(adv_next == NADV - 1 ? 0 : adv_next + 1);
const uint8_t *bits;
for (unsigned ch = 32; ch <= 130; ch++) {
- unsigned c = ch > (f & (T_LARGE | T_MED) ? 126u : 130u) ? '?' : ch;
+ unsigned c = ch > (FLO(f) & (T_LARGE | T_MED) ? 126u : 130u) ? '?' : ch;
advs[i].a[ch - 32] = (int8_t)advance(f, c, glyph(f, c, &bits));
}
- advs[i].key = (uint16_t)key;
+ advs[i].key = key;
#ifdef NP_HOST
ce_adv_made++;
#endif
@@ -468,7 +477,7 @@ static int textw(const char *s, int f, int one_line) {
if (badge) w += 2;
if (w > best) best = w;
if (best > 0) best--;
- return best * ((f & T_X2) ? 2 : 1);
+ return FLO(f) & T_X2 ? best + best : best;
}
int g_textw(const char *s, int f) { return textw(s, f, 0); }
diff --git a/games/balatro/ce/tools/cesrc.py b/games/balatro/ce/tools/cesrc.py
index 3626fc25..8d66f2eb 100644
--- a/games/balatro/ce/tools/cesrc.py
+++ b/games/balatro/ce/tools/cesrc.py
@@ -3,7 +3,8 @@
main.c with the CE's keys named where the screens name keys (platform/ti84ce/README.md: 2nd is OK, enter is EXE,
clear is Back, mode is Home, del is the NumWorks's backspace, y= is shift, math is toolbox, vars is var), the keys
looked through only when one is newly down, and its main() renamed for src/ce_main.c to call; ui.c with its easing
-skipped where there is nothing left to ease (a frame's busiest float work on the eZ80, which has no floats).
+skipped where there is nothing left to ease (a frame's busiest float work on the eZ80, which has no floats); util.c
+with the numbers it wrote last kept (the same few every frame, and 64-bit divisions are slow calls on the eZ80).
Every text below must be found, so a change to the NumWorks files shows here.
Usage: cesrc.py ../src/main.c gen/main.c"""
@@ -39,6 +40,38 @@ SWAPS = {
("static float ease(float cur, float target, int dt) {\n",
"static float ease(float cur, float target, int dt) {\n if (cur == target) return cur;\n"),
],
+ "util.c": [
+ ("char *fmt_commas(char *o, double v) {", "static char *fmt_commas_raw(char *o, double v) {"),
+ ],
+}
+
+TAILS = {
+ # (for tools/host.c's player: which screen shows)
+ "main.c": "\nint ce_main_screen(void) { return screen == SCR_GAME ? 1 + overlay : 0; }\n",
+ "util.c": '''
+/* the last numbers written, kept */
+#include <string.h>
+char *fmt_commas(char *o, double v) {
+ static struct {
+ double v;
+ uint8_t n;
+ char s[27];
+ } kept[6];
+ static uint8_t next;
+ for (int i = 0; i < 6; i++)
+ if (kept[i].n && !memcmp(&kept[i].v, &v, sizeof v)) {
+ memcpy(o, kept[i].s, kept[i].n + 1u);
+ return o + kept[i].n;
+ }
+ char *e = fmt_commas_raw(o, v);
+ if (e - o < 27) {
+ kept[next].v = v, kept[next].n = (uint8_t)(e - o);
+ memcpy(kept[next].s, o, kept[next].n + 1u);
+ next = next == 5 ? 0 : next + 1;
+ }
+ return e;
+}
+''',
}
@@ -52,9 +85,7 @@ def main():
s = s.replace(a, b)
head = f"/* Made by tools/cesrc.py from src/{name}: do not edit. */\n"
s = head + s.replace('#include "', '#include "../../src/', s.count('#include "'))
- if name == "main.c":
- # (for tools/host.c's player: which screen shows)
- s += "\nint ce_main_screen(void) { return screen == SCR_GAME ? 1 + overlay : 0; }\n"
+ s += TAILS.get(name, "")
open(dst, "w", encoding="utf-8").write(s)