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: shading and the paint's rows in eZ80 code

Mason3636243b01acc70
diff --git a/games/balatro/ce/src/bg_ce.c b/games/balatro/ce/src/bg_ce.c
index 42a30c3a..c1d809a6 100644
--- a/games/balatro/ce/src/bg_ce.c
+++ b/games/balatro/ce/src/bg_ce.c
@@ -138,12 +138,9 @@ void bg_update(float dt, int rows) {
void bg_row(C *dst, int y, int x0, int x1) { (void)dst, (void)y, (void)x0, (void)x1; }
-/* two pixels a byte into their palette entries, from an even x0 */
+/* two pixels a byte into their palette entries (n pixels, even) */
static void unpack(uint8_t *dst, const uint8_t *src, int n) {
- for (; n > 0; n -= 2, src++) {
- *dst++ = (uint8_t)(CE_SWIRL0 + (*src & 15));
- *dst++ = (uint8_t)(CE_SWIRL0 + (*src >> 4));
- }
+ if (n > 0) ce_unpack(dst, src, (unsigned)n / 2);
}
void ce_bg_row(uint8_t *dst, int y, int x0) {
diff --git a/games/balatro/ce/src/blit.S b/games/balatro/ce/src/blit.S
new file mode 100644
index 00000000..2cf936a5
--- /dev/null
+++ b/games/balatro/ce/src/blit.S
@@ -0,0 +1,72 @@
+/* The renderer's busiest loops in eZ80 code (src/gfx_ce.c, bg_ce.c). The C library's calling convention: arguments
+ * on the stack (3 bytes each), ix kept. */
+ .assume adl = 1
+
+/* void ce_shade(uint8_t *d, const uint8_t *tab, unsigned n): d[i] = tab[d[i]], n > 0, tab 256-byte aligned */
+ .section .text.ce_shade, "ax", @progbits
+ .global _ce_shade
+ .type _ce_shade, @function
+_ce_shade:
+ ld hl, 9
+ add hl, sp
+ ld bc, (hl) ; n
+ dec hl
+ dec hl
+ dec hl
+ ld de, (hl) ; tab
+ dec hl
+ dec hl
+ dec hl
+ ld hl, (hl) ; d
+ ex de, hl ; de = d, hl = tab
+.L.shade:
+ ld a, (de)
+ ld l, a
+ ld a, (hl)
+ ld (de), a
+ inc de
+ dec bc
+ ld a, c
+ or a, b
+ jr nz, .L.shade
+ ret
+
+/* void ce_unpack(uint8_t *d, const uint8_t *s, unsigned n): n bytes of two 4-bit levels (the low first) into 2n
+ palette indices from 192, n > 0 */
+ .section .text.ce_unpack, "ax", @progbits
+ .global _ce_unpack
+ .type _ce_unpack, @function
+_ce_unpack:
+ ld hl, 9
+ add hl, sp
+ ld bc, (hl) ; n
+ dec hl
+ dec hl
+ dec hl
+ ld de, (hl) ; s
+ dec hl
+ dec hl
+ dec hl
+ ld hl, (hl) ; d
+ ex de, hl ; de = d, hl = s
+.L.unpack:
+ ld a, (hl)
+ and a, 15
+ add a, 192
+ ld (de), a
+ inc de
+ ld a, (hl)
+ rrca
+ rrca
+ rrca
+ rrca
+ and a, 15
+ add a, 192
+ ld (de), a
+ inc de
+ inc hl
+ dec bc
+ ld a, c
+ or a, b
+ jr nz, .L.unpack
+ ret
diff --git a/games/balatro/ce/src/ce.h b/games/balatro/ce/src/ce.h
index 4af26a52..9a90fedd 100644
--- a/games/balatro/ce/src/ce.h
+++ b/games/balatro/ce/src/ce.h
@@ -16,6 +16,10 @@ void ce_set_palette(int first, int n, const uint16_t *c); /* colours as the CE'
const uint8_t *ce_appvar(const char *name, size_t *size); /* an appvar's content, where it is (flash), or NULL */
uint32_t ce_ms(void); /* milliseconds */
+/* ---- the busiest loops (blit.S; C on a computer, in gfx_ce.c) */
+void ce_shade(uint8_t *d, const uint8_t *tab, unsigned n); /* d[i] = tab[d[i]]: tab 256-byte aligned, n > 0 */
+void ce_unpack(uint8_t *d, const uint8_t *s, unsigned n); /* n bytes of two levels into 2n indices from 192 */
+
/* ---- colours (gfx_ce.c) */
uint8_t ce_pal(C c); /* the palette index showing c: a fixed colour, or one taken for it */
const uint8_t *ce_shade_tab(int alpha); /* every index darkened by alpha/32 */
diff --git a/games/balatro/ce/src/gfx_ce.c b/games/balatro/ce/src/gfx_ce.c
index dd82094c..f79f3c91 100644
--- a/games/balatro/ce/src/gfx_ce.c
+++ b/games/balatro/ce/src/gfx_ce.c
@@ -55,6 +55,16 @@ void *g_scratch(int *size) {
}
void g_bg_cover(int x) { bg_cover_x = x; }
+#ifdef NP_HOST
+/* (blit.S on the calculator) */
+void ce_shade(uint8_t *d, const uint8_t *tab, unsigned n) {
+ while (n--) *d = tab[*d], d++;
+}
+void ce_unpack(uint8_t *d, const uint8_t *s, unsigned n) {
+ for (; n; n--, s++) *d++ = (uint8_t)(CE_SWIRL0 + (*s & 15)), *d++ = (uint8_t)(CE_SWIRL0 + (*s >> 4));
+}
+#endif
+
C darken(C a, int t) { return mix565(a, 0, t); }
C lighten(C a, int t) { return mix565(a, 0xFFFF, t); }
@@ -124,8 +134,8 @@ uint8_t ce_pal(C c) {
static const int8_t bank_dark[CE_BANKS] = CE_BANK_DARK;
static struct {
uint8_t alpha, ok;
- uint8_t t[256];
} stabs[NSTAB];
+static uint8_t stab_t[NSTAB][256] __attribute__((aligned(256))); /* (ce_shade looks up by the low byte) */
static uint8_t stab_next;
static void shade_tabs_reset(void) {
for (int i = 0; i < NSTAB; i++) stabs[i].ok = 0;
@@ -134,10 +144,10 @@ const uint8_t *ce_shade_tab(int alpha) {
if (alpha < 0) alpha = 0;
if (alpha > 32) alpha = 32;
for (int i = 0; i < NSTAB; i++)
- if (stabs[i].ok && stabs[i].alpha == alpha) return stabs[i].t;
+ if (stabs[i].ok && stabs[i].alpha == alpha) return stab_t[i];
int i = stab_next;
stab_next = (uint8_t)((stab_next + 1) % NSTAB);
- uint8_t *t = stabs[i].t;
+ uint8_t *t = stab_t[i];
memcpy(t, ce_lut + LUT_SHADE + alpha * CE_NSTATIC, CE_NSTATIC);
uint16_t used = frame_dyn;
for (int s = 0; s < NSTRIP; s++) used |= strip_dyn[s];
@@ -521,7 +531,7 @@ static void draw_sprite(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
int a = x < xa ? xa : x, b = x + n > xb ? xb : x + n;
if (a < b) {
if (tab)
- for (int i = a; i < b; i++) d[i] = tab[d[i]];
+ ce_shade(d + a, tab, (unsigned)(b - a));
else
memcpy(d + a, p + (a - x), (size_t)(b - a));
}
@@ -602,7 +612,7 @@ static void draw_rrect(item_t *it, int y0, int ra, int rb, int cx0, int cx1, int
}
if (ma < mb) {
if (shade)
- for (int xx = ma; xx < mb; xx++) d[xx] = tab[d[xx]];
+ ce_shade(d + ma, tab, (unsigned)(mb - ma));
else
memset(d + ma, c, (size_t)(mb - ma));
}