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: the list's items and scaled rows in eZ80 code, easing skipped when done

Mason3636146e43642c8
diff --git a/games/balatro/ce/makefile b/games/balatro/ce/makefile
index 14f30762..a5f11da8 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 ui draw
+SHARED = game score jokers cons ev util 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)/data.c $(GEN)/ce_art.c
-DEPS = $(GEN)/ce_art.h $(GEN)/main.c $(GEN)/data.c
+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
LINKER_SCRIPT = ce.ld
# (the toolchain's makefile builds bin/BALATRO.all: everything linked; the rules below cut it up)
TARGET = $(NAME).all
@@ -34,9 +34,9 @@ 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: ../src/main.c tools/cekeys.py
+$(GEN)/main.c $(GEN)/ui.c: $(GEN)/%.c: ../src/%.c tools/cesrc.py
$(Q)mkdir -p $(GEN)
- $(Q)$(PY) tools/cekeys.py $< $@
+ $(Q)$(PY) tools/cesrc.py $< $@
$(GEN)/data.c: ../src/data.c tools/cedata.py
$(Q)mkdir -p $(GEN)
diff --git a/games/balatro/ce/src/blit.S b/games/balatro/ce/src/blit.S
index 6dfb7c0b..6095b6f9 100644
--- a/games/balatro/ce/src/blit.S
+++ b/games/balatro/ce/src/blit.S
@@ -186,6 +186,141 @@ _ce_hash_items:
pop ix
ret
+/* item_t *ce_add(int type, int x, int y, int w, int h) (src/gfx_ce.c): a new item at the list's end (ce_item_next,
+ ce_nitems, at most 360), or NULL when none is left, it is empty or wholly outside the clip (ce_clip: x0, y0, x1, y1
+ in 16 bits, 0 to 320; ce_clip_i its number). Its type, place and clip, its strips (rows of 16) the clip leaves,
+ the rest 0. Coordinates are small: no overflow in the compares. */
+ .section .text.ce_add, "ax", @progbits
+ .global _ce_add
+ .type _ce_add, @function
+_ce_add:
+ ld iy, 0
+ add iy, sp ; type +3, x +6, y +9, w +12, h +15
+ ld hl, (_ce_nitems)
+ ld de, 360
+ or a, a
+ sbc hl, de
+ jp nc, .L.anone
+ ld de, 1
+ ld hl, (iy + 12)
+ or a, a
+ sbc hl, de
+ jp m, .L.anone ; w <= 0
+ ld hl, (iy + 15)
+ or a, a
+ sbc hl, de
+ jp m, .L.anone ; h <= 0
+ push ix
+ ld ix, (_ce_clip)
+ ld de, 0
+ ld e, (ix + 4)
+ ld d, (ix + 5)
+ ld hl, (iy + 6)
+ or a, a
+ sbc hl, de
+ jp p, .L.aout ; x >= x1
+ ld e, (ix + 6)
+ ld d, (ix + 7)
+ ld hl, (iy + 9)
+ or a, a
+ sbc hl, de
+ jp p, .L.aout ; y >= y1
+ ld e, (ix + 0)
+ ld d, (ix + 1)
+ ld hl, (iy + 6)
+ ld bc, (iy + 12)
+ add hl, bc
+ scf
+ sbc hl, de
+ jp m, .L.aout ; x + w <= x0
+ ld e, (ix + 2)
+ ld d, (ix + 3)
+ ld hl, (iy + 9)
+ ld bc, (iy + 15)
+ add hl, bc
+ scf
+ sbc hl, de
+ jp m, .L.aout ; y + h <= y0
+ ld hl, (iy + 9) ; its first strip: max(y, y0) / 16
+ or a, a
+ sbc hl, de
+ ld a, e
+ jp m, .L.ay0
+ ld a, (iy + 9)
+.L.ay0:
+ rrca
+ rrca
+ rrca
+ rrca
+ and a, 15
+ ld c, a
+ ld e, (ix + 6) ; its last: (min(y + h, y1) - 1) / 16
+ ld d, (ix + 7)
+ ld hl, (iy + 9)
+ ld a, (iy + 15)
+ add a, (iy + 9) ; (y + h's low byte, if under y1)
+ push af
+ push bc
+ ld bc, (iy + 15)
+ add hl, bc
+ pop bc
+ or a, a
+ sbc hl, de
+ pop hl ; (h: the low byte's sum)
+ ld a, h
+ jp m, .L.ay1
+ ld a, e
+.L.ay1:
+ dec a
+ rrca
+ rrca
+ rrca
+ rrca
+ and a, 15
+ ld b, a
+ ld hl, (_ce_item_next) ; the item
+ push hl
+ ld de, 22
+ add hl, de
+ ld (_ce_item_next), hl
+ ld hl, (_ce_nitems)
+ inc hl
+ ld (_ce_nitems), hl
+ pop ix
+ ld a, (iy + 3)
+ ld (ix + 8), a ; type
+ ld hl, (iy + 6)
+ ld (ix + 0), l
+ ld (ix + 1), h
+ ld hl, (iy + 9)
+ ld (ix + 2), l
+ ld (ix + 3), h
+ ld hl, (iy + 12)
+ ld (ix + 4), l
+ ld (ix + 5), h
+ ld hl, (iy + 15)
+ ld (ix + 6), l
+ ld (ix + 7), h
+ ld a, (_ce_clip_i)
+ ld (ix + 9), a
+ ld (ix + 17), c ; its strips
+ ld (ix + 18), b
+ or a, a
+ sbc hl, hl
+ ld (ix + 10), hl ; r, fx, a's low byte
+ ld (ix + 13), hl ; a's high byte, b
+ ld (ix + 16), l ; anim
+ ld (ix + 19), hl ; no text
+ lea hl, ix + 0
+ pop ix
+ ret
+.L.aout:
+ pop ix
+.L.anone:
+ or a, a
+ sbc hl, hl
+ ret
+
/* unsigned ce_hash_str(unsigned h, const char *s): the same as ce_hash over a string's bytes */
.section .text.ce_hash_str, "ax", @progbits
.global _ce_hash_str
diff --git a/games/balatro/ce/src/gfx_ce.c b/games/balatro/ce/src/gfx_ce.c
index d665ae0b..ae233ace 100644
--- a/games/balatro/ce/src/gfx_ce.c
+++ b/games/balatro/ce/src/gfx_ce.c
@@ -49,11 +49,15 @@ _Static_assert(offsetof(item_t, clip) == 9 && offsetof(item_t, anim) == 16 && of
#endif
void ce_hash_items(unsigned *hs, const item_t *it, unsigned n, const unsigned *clip_hash);
-static item_t items[MAXI], *item_next;
+static item_t items[MAXI];
+item_t *ce_item_next; /* (blit.S's ce_add reads these) */
+int ce_nitems;
+const int16_t *ce_clip; /* the clip's: x0, y0, x1, y1 */
+uint8_t ce_clip_i;
static char strbuf[2560]; /* (a frame takes under 1 KB: the How to play page) */
-static int strpos, nitems;
+static int strpos;
static int16_t clips[MAXCLIP][4];
-static int nclips, cur_clip;
+static int nclips;
static uint8_t strip[SCR_W * SH];
static const uint16_t ROW_OFF[SH] = {0, 320, 640, 960, 1280, 1600, 1920, 2240, 2560, 2880, 3200, 3520, 3840, 4160,
4480, 4800}; /* (a strip's rows: the eZ80 multiplies by calls) */
@@ -277,12 +281,12 @@ static unsigned long prof_now(void) {
void g_begin(void) {
PROF(0); /* (the time between frames: the rules, the keys, the waiting) */
- nitems = 0;
- item_next = items;
+ ce_nitems = 0;
+ ce_item_next = items;
bg_cover_x = 0;
strpos = 0;
nclips = 1;
- cur_clip = 0;
+ ce_clip_i = 0, ce_clip = clips[0];
frame_dyn = 0;
ed_frame = (unsigned)(g_time / ED_DT_MS);
clips[0][0] = 0, clips[0][1] = 0, clips[0][2] = SCR_W, clips[0][3] = SCR_H;
@@ -294,24 +298,32 @@ void g_clip(int x, int y, int w, int h) {
clips[nclips][1] = (int16_t)(y < 0 ? 0 : y);
clips[nclips][2] = (int16_t)(x + w > SCR_W ? SCR_W : x + w);
clips[nclips][3] = (int16_t)(y + h > SCR_H ? SCR_H : y + h);
- cur_clip = nclips++;
+ ce_clip_i = (uint8_t)nclips, ce_clip = clips[nclips++];
}
-void g_noclip(void) { cur_clip = 0; }
+void g_noclip(void) { ce_clip_i = 0, ce_clip = clips[0]; }
-static item_t *add(int type, int x, int y, int w, int h) {
- if (nitems >= MAXI || w <= 0 || h <= 0) return 0;
- const int16_t *c = clips[cur_clip];
+/* a new item in the list, or NULL (none left, empty, or wholly outside the clip): its strips worked out, the rest 0
+ (blit.S; its C on a computer) */
+item_t *ce_add(int type, int x, int y, int w, int h);
+#ifdef NP_HOST
+item_t *ce_add(int type, int x, int y, int w, int h) {
+ if (ce_nitems >= MAXI || w <= 0 || h <= 0) return 0;
+ const int16_t *c = ce_clip;
if (x >= c[2] || y >= c[3] || x + w <= c[0] || y + h <= c[1]) return 0;
- item_t *it = item_next++;
- nitems++;
+ item_t *it = ce_item_next++;
+ ce_nitems++;
it->type = (uint8_t)type;
it->x = (int16_t)x, it->y = (int16_t)y, it->w = (int16_t)w, it->h = (int16_t)h;
- it->clip = (uint8_t)cur_clip;
+ it->clip = ce_clip_i;
it->fx = 0, it->r = 0, it->a = 0, it->b = 0, it->s = 0, it->anim = 0;
int y0 = y > c[1] ? y : c[1], y1 = y + h < c[3] ? y + h : c[3];
- it->s0 = (uint8_t)((uint8_t)y0 >> 4), it->s1 = (uint8_t)((uint8_t)(y1 - 1) >> 4); /* (SH 16; y0 < y1 <= 240) */
+ it->s0 = (uint8_t)(y0 / SH), it->s1 = (uint8_t)((y1 - 1) / SH);
return it;
}
+#else
+_Static_assert(SH == 16 && MAXI == 360, "blit.S's ce_add");
+#endif
+#define add ce_add
void g_rect(int x, int y, int w, int h, C c) {
item_t *it = add(I_RECT, x, y, w, h);
@@ -837,7 +849,7 @@ long ce_strips_drawn, ce_pal_misses;
void g_end(void) {
#ifdef NP_HOST
- if (nitems > ce_max_items) ce_max_items = nitems;
+ if (ce_nitems > ce_max_items) ce_max_items = ce_nitems;
if (strpos > ce_max_str) ce_max_str = strpos;
#endif
PROF(1); /* (the frame's list) */
@@ -851,7 +863,7 @@ void g_end(void) {
unsigned b = ce_hash_words(0, base, 3);
for (int s = 0; s < NSTRIP; s++) hs[s] = b + (unsigned)s;
for (int i = 0; i < nclips; i++) clip_hash[i] = ce_hash(0x5A17u, clips[i], sizeof clips[i]);
- ce_hash_items(hs, items, (unsigned)nitems, clip_hash);
+ ce_hash_items(hs, items, (unsigned)ce_nitems, clip_hash);
PROF(2); /* (the hashes) */
uint8_t *d = strip;
for (int s = 0, y0 = 0; s < NSTRIP; s++, y0 += SH) {
@@ -875,7 +887,7 @@ void g_end(void) {
} else
memset(strip, bg_solid, sizeof strip);
PROF(13);
- for (int i = 0; i < nitems; i++) {
+ for (int i = 0; i < ce_nitems; i++) {
item_t *it = &items[i];
if (s < it->s0 || s > it->s1) continue;
const int16_t *c = clips[it->clip];
diff --git a/games/balatro/ce/tools/cekeys.py b/games/balatro/ce/tools/cekeys.py
deleted file mode 100644
index 09b811e1..00000000
--- a/games/balatro/ce/tools/cekeys.py
+++ /dev/null
@@ -1,50 +0,0 @@
-#!/usr/bin/env python3
-"""The NumWorks version's main.c for the TI-84 Plus CE: the same file 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.
-Every text below must be found, so a change to the NumWorks texts shows here.
-
-Usage: cekeys.py ../src/main.c gen/main.c"""
-import sys
-
-SWAPS = [
- # the How to play page
- (r'\004OK\020 select a card', r'\0042nd\020 select a card'),
- (r'\004EXE\020 \002Play Hand\020 \004DEL\020 \003Discard\020',
- r'\004enter\020 \002Play Hand\020 \004del\020 \003Discard\020'),
- (r'\004shift\020 sort the hand', r'\004y=\020 sort the hand'),
- (r'\004left/right\020 to carry it, \004OK\020 to put it down', r'\004left/right\020 to carry it, \0042nd\020 to put it down'),
- (r'\004toolbox\020 Run Info \004var\020 view deck', r'\004math\020 Run Info \004vars\020 view deck'),
- (r'\004back\020 back / Options \004home\020 save, quit', r'\004clear\020 back / Options \004mode\020 save, quit'),
- (r'Hold \004OK\020 or \004EXE\020 to speed up scoring.', r'Hold \0042nd\020 or \004enter\020 to speed up scoring.'),
- # the keys under the round's buttons, the move hint
- ('keycap(px + pw / 2, 229, "exe");', 'keycap(px + pw / 2, 229, "enter");'),
- ('keycap(sx + sw / 2, 229, "shift");', 'keycap(sx + sw / 2, 229, "y=");'),
- ('const char *k = moving ? "OK" : "alpha"', 'const char *k = moving ? "2nd" : "alpha"'),
- # Run Info
- ('T("left/right: tabs back: close")', 'T("left/right: tabs clear: close")'),
- # the keys newly down: none most frames (the eZ80 shifts 64 bits by calls)
- (" for (int k = 0; k < 53; k++)\n if (((keys >> k) & 1) && !((prev_keys >> k) & 1)) {",
- " if (keys & ~prev_keys)\n for (int k = 0; k < 53; k++)\n if (((keys >> k) & 1) && !((prev_keys >> k) & 1)) {"),
- # the entry point
- ("int main(void) {", "int balatro_main(void) {"),
-]
-
-
-def main():
- src, dst = sys.argv[1], sys.argv[2]
- s = open(src, encoding="utf-8").read()
- for a, b in SWAPS:
- if s.count(a) != 1:
- raise SystemExit(f"cekeys: {a!r} found {s.count(a)} times in {src}")
- s = s.replace(a, b)
- head = "/* Made by tools/cekeys.py from src/main.c: do not edit. */\n"
- s = head + s.replace('#include "', '#include "../../src/', s.count('#include "'))
- # (for tools/host.c's player: which screen shows)
- s += "\nint ce_main_screen(void) { return screen == SCR_GAME ? 1 + overlay : 0; }\n"
- open(dst, "w", encoding="utf-8").write(s)
-
-
-if __name__ == "__main__":
- main()
diff --git a/games/balatro/ce/tools/cesrc.py b/games/balatro/ce/tools/cesrc.py
new file mode 100644
index 00000000..3626fc25
--- /dev/null
+++ b/games/balatro/ce/tools/cesrc.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+"""The NumWorks version's files the TI-84 Plus CE changes a little (gen/, from ../src):
+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).
+Every text below must be found, so a change to the NumWorks files shows here.
+
+Usage: cesrc.py ../src/main.c gen/main.c"""
+import os
+import sys
+
+SWAPS = {
+ "main.c": [
+ # the How to play page
+ (r'\004OK\020 select a card', r'\0042nd\020 select a card'),
+ (r'\004EXE\020 \002Play Hand\020 \004DEL\020 \003Discard\020',
+ r'\004enter\020 \002Play Hand\020 \004del\020 \003Discard\020'),
+ (r'\004shift\020 sort the hand', r'\004y=\020 sort the hand'),
+ (r'\004left/right\020 to carry it, \004OK\020 to put it down',
+ r'\004left/right\020 to carry it, \0042nd\020 to put it down'),
+ (r'\004toolbox\020 Run Info \004var\020 view deck', r'\004math\020 Run Info \004vars\020 view deck'),
+ (r'\004back\020 back / Options \004home\020 save, quit',
+ r'\004clear\020 back / Options \004mode\020 save, quit'),
+ (r'Hold \004OK\020 or \004EXE\020 to speed up scoring.', r'Hold \0042nd\020 or \004enter\020 to speed up scoring.'),
+ # the keys under the round's buttons, the move hint
+ ('keycap(px + pw / 2, 229, "exe");', 'keycap(px + pw / 2, 229, "enter");'),
+ ('keycap(sx + sw / 2, 229, "shift");', 'keycap(sx + sw / 2, 229, "y=");'),
+ ('const char *k = moving ? "OK" : "alpha"', 'const char *k = moving ? "2nd" : "alpha"'),
+ # Run Info
+ ('T("left/right: tabs back: close")', 'T("left/right: tabs clear: close")'),
+ # the keys newly down: none most frames (the eZ80 shifts 64 bits by calls)
+ (" for (int k = 0; k < 53; k++)\n if (((keys >> k) & 1) && !((prev_keys >> k) & 1)) {",
+ " if (keys & ~prev_keys)\n for (int k = 0; k < 53; k++)\n if (((keys >> k) & 1) && !((prev_keys >> k) & 1)) {"),
+ # the entry point
+ ("int main(void) {", "int balatro_main(void) {"),
+ ],
+ "ui.c": [
+ ("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"),
+ ],
+}
+
+
+def main():
+ src, dst = sys.argv[1], sys.argv[2]
+ name = os.path.basename(src)
+ s = open(src, encoding="utf-8").read()
+ for a, b in SWAPS[name]:
+ if s.count(a) != 1:
+ raise SystemExit(f"cesrc: {a!r} found {s.count(a)} times in {src}")
+ 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"
+ open(dst, "w", encoding="utf-8").write(s)
+
+
+if __name__ == "__main__":
+ main()