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 drawing in eZ80 code, 5 times faster

Mason3631bafaf3f0dd4
diff --git a/games/balatro/ce/makefile b/games/balatro/ce/makefile
index 96458120..14f30762 100644
--- a/games/balatro/ce/makefile
+++ b/games/balatro/ce/makefile
@@ -1,7 +1,8 @@
# Balatro for the TI-84 Plus CE (CE C toolchain v15: cedev-config on the PATH). `make` builds bin/: BALATRO.8xp
-# (with BALATRO0... when the program is past 64 KB), the code the loader puts in place (BALCV0, BALCV1, BALCB), the art
-# (BALART0...) and tables (BALLUT, BALFIRE, BALBG0, BALBG1). The game's rules and screens are the NumWorks version's
-# (../src); src/ is the CE's own: the screen at 8 bits a pixel, the background's colours turning, the keys, the files.
+# (with BALATRO0... when the program is past 64 KB), the code the loader puts in place (BALCV0, BALCV1, BALCP,
+# BALCB), the art (BALART0...) and tables (BALLUT, BALFIRE, BALBG0 to 2, BALED). The game's rules and screens are the
+# NumWorks version's (../src); src/ is the CE's own: the screen at 8 bits a pixel, the background's colours turning,
+# the keys, the files; its busiest drawing in eZ80 code (src/blit.S).
# make host the same code on a computer (tools/host.c, SDL 3): bin/host/balatro
NAME = BALATRO
ICON = icon.png
diff --git a/games/balatro/ce/src/archive.S b/games/balatro/ce/src/archive.S
index 77ffcaa7..70d752d1 100644
--- a/games/balatro/ce/src/archive.S
+++ b/games/balatro/ce/src/archive.S
@@ -30,6 +30,98 @@ _ce_archive:
pop ix
ret
+/* The appvars through TI-OS's own routines (every OS since 5.0 has them): var is the 9 bytes for OP1.
+ * uint8_t *ce_var_find(const char *var): its size bytes (then its content), in RAM or the archive, or NULL */
+ .section .text.boot.ce_var_find, "ax", @progbits
+ .global _ce_var_find
+ .type _ce_var_find, @function
+ .equ EnoughMem, 0x02051C
+ .equ CreateVar, 0x021338
+ .equ DelVarArc, 0x021434
+_ce_var_find:
+ pop de
+ pop hl
+ push hl
+ push de
+ push ix
+ push iy
+ ld iy, flags
+ call Mov9ToOP1
+ call ChkFindSym
+ jr c, .L.none
+ call ChkInRam
+ ex de, hl
+ jr z, .L.found
+ ld de, 9 ; archived: past its header and name
+ add hl, de
+ ld e, (hl)
+ add hl, de
+ inc hl
+.L.found:
+ pop iy
+ pop ix
+ ret
+.L.none:
+ or a, a
+ sbc hl, hl
+ jr .L.found
+
+/* uint8_t *ce_var_create(const char *var, unsigned size): a new appvar in RAM, its size bytes, or NULL (no room) */
+ .section .text.boot.ce_var_create, "ax", @progbits
+ .global _ce_var_create
+ .type _ce_var_create, @function
+_ce_var_create:
+ ld hl, 3
+ add hl, sp
+ ld de, (hl) ; var
+ inc hl
+ inc hl
+ inc hl
+ ld hl, (hl) ; size
+ push ix
+ push iy
+ ld iy, flags
+ push hl
+ ex de, hl
+ call Mov9ToOP1
+ pop hl
+ push hl
+ ld de, 32
+ add hl, de ; (and its symbol table entry)
+ call EnoughMem
+ pop hl
+ jr c, .L.full
+ ld a, 0x15
+ call CreateVar
+ ex de, hl
+ jr .L.made
+.L.full:
+ or a, a
+ sbc hl, hl
+.L.made:
+ pop iy
+ pop ix
+ ret
+
+/* void ce_var_delete(const char *var): gone, from RAM or the archive */
+ .section .text.boot.ce_var_delete, "ax", @progbits
+ .global _ce_var_delete
+ .type _ce_var_delete, @function
+_ce_var_delete:
+ pop de
+ pop hl
+ push hl
+ push de
+ push ix
+ push iy
+ ld iy, flags
+ call Mov9ToOP1
+ call ChkFindSym
+ call nc, DelVarArc
+ pop iy
+ pop ix
+ ret
+
/* void ce_on_stack(void (*f)(void), void *top): f called with the stack from top down (the game needs more than the
* OS's 4 KB: a Blueprint copying a Blueprint... goes deep), the OS's stack back after. */
.section .text.boot.ce_on_stack, "ax", @progbits
diff --git a/games/balatro/ce/src/bg_ce.c b/games/balatro/ce/src/bg_ce.c
index c1d809a6..e67c3a10 100644
--- a/games/balatro/ce/src/bg_ce.c
+++ b/games/balatro/ce/src/bg_ce.c
@@ -1,5 +1,5 @@
/* Balatro's paint background on the TI-84 Plus CE. Its shaders (worked out as the NumWorks version does, src/bg.c)
- * give a picture of the paint's value, 16 levels (tools/mkart.py: BALBG0 in a run, BALBG1 on the title screen); the
+ * give a picture of the paint's value, 16 levels (tools/mkart.py: BALBG0 in a run, BALBG1 and 2 on the title screen); the
* 64 palette entries those levels use take the shader's colours, and the colours run through the levels with time:
* the paint flows without a pixel being drawn. Four banks of the 16 levels, darker and darker, are the background
* under the shaded panels and the cards' shadows. */
@@ -8,57 +8,81 @@
#include "ce.h"
uint8_t ce_bg_ver;
-static const uint8_t *field[2]; /* the run's (right of the sidebar), the title's: levels, two pixels a byte */
+static const uint8_t *field[3]; /* the run's (right of the sidebar), the title's (its halves): palette entries */
static int mode;
-static float colours[3][3], target[3][3], contrast = 1, contrast_target = 1, spin, spin_target;
+/* in 1/256: the colours (0..255 a channel, times 256), the contrast and the spin */
+static int colours[3][3], target[3][3], contrast = 256, contrast_target = 256, spin, spin_target;
static int lut_dirty = 1;
static uint8_t lut[33][3]; /* the colour every 8 paint values */
static unsigned phase; /* 0..511 in 1/256 units of the paint: where the colours are */
-static uint32_t phase_frac;
+static unsigned phase_frac;
+static unsigned shown_phase = 1024; /* the palette's */
+static uint8_t lut_made;
+
+/* the banks' darkness as factors in 1/256 (0: 256) */
+static uint8_t keep[CE_BANKS];
+
+#ifdef NP_HOST
+/* (blit.S on the calculator) */
+void ce_bg_pal(uint8_t *out, const uint8_t *lt, unsigned ph, const uint8_t *kp) {
+ for (int l = 0; l < CE_LEVELS; l++) {
+ unsigned q = ((unsigned)l * 16 + 8 + ph) & 511;
+ if (q > 255) q = 511 - q;
+ int p = (int)q, f = p & 7, rgb[3];
+ const uint8_t *a = lt + (p >> 3) * 3;
+ for (int j = 0; j < 3; j++) rgb[j] = f ? (a[j] * (256 - f * 32) + a[j + 3] * f * 32) >> 8 : a[j];
+ for (int b = 0; b < CE_BANKS; b++) {
+ int r = rgb[0], g = rgb[1], bl = rgb[2], k = kp[b];
+ if (k) r = r * k >> 8, g = g * k >> 8, bl = bl * k >> 8;
+ uint8_t *e = out + (b * CE_LEVELS + l) * 2;
+ e[0] = (uint8_t)((g << 2 & 0xE0) | bl >> 3);
+ e[1] = (uint8_t)((r >> 1 & 0x7C) | g >> 6);
+ }
+ }
+}
+#endif
int ce_bg_init(void) {
+ static const uint8_t dark[CE_BANKS] = CE_BANK_DARK;
+ for (int b = 0; b < CE_BANKS; b++) keep[b] = (uint8_t)((32 - dark[b]) * 8);
size_t n;
field[0] = ce_appvar("BALBG0", &n);
field[1] = ce_appvar("BALBG1", &n);
- return field[0] && field[1];
+ field[2] = ce_appvar("BALBG2", &n);
+ return field[0] && field[1] && field[2];
}
-static float fabsf_(float x) { return x < 0 ? -x : x; }
-static float fmaxf_(float a, float b) { return a > b ? a : b; }
-static float fminf_(float a, float b) { return a < b ? a : b; }
+static int iabs(int x) { return x < 0 ? -x : x; }
+static int imax(int a, int b) { return a > b ? a : b; }
+static int imin(int a, int b) { return a < b ? a : b; }
-/* src/bg.c's colour of a paint value */
+/* src/bg.c's colour of a paint value v (0..255), in integers: 256 is 1 */
static void colour_at(int v, uint8_t out[3]) {
- float r = v / 127.5f, o[3];
+ int r = v * 2, o[3]; /* 0..2 */
if (mode == 0) {
- float cm = 0.25f * contrast + 0.5f * spin + 1.2f;
- float c1p = fmaxf_(0.f, 1.f - cm * fabsf_(1.f - r));
- float c2p = fmaxf_(0.f, 1.f - cm * fabsf_(r));
- float c3p = 1.f - fminf_(1.f, c1p + c2p);
- float k = 0.3f / contrast;
- for (int j = 0; j < 3; j++)
- o[j] = k * colours[0][j] + (1 - k) * (colours[0][j] * c1p + colours[1][j] * c2p + colours[2][j] * c3p);
+ int cm = contrast / 4 + spin / 2 + 307; /* 0.25 contrast + 0.5 spin + 1.2 */
+ int c1p = imax(0, 256 - cm * iabs(256 - r) / 256), c2p = imax(0, 256 - cm * r / 256);
+ int c3p = 256 - imin(256, c1p + c2p), k = imin(256, 19661 / contrast); /* 0.3 / contrast */
+ for (int j = 0; j < 3; j++) {
+ int mixed = (colours[0][j] / 256 * c1p + colours[1][j] / 256 * c2p + colours[2][j] / 256 * c3p) / 256;
+ o[j] = (k * (colours[0][j] / 256) + (256 - k) * mixed) / 256;
+ }
} else {
- float s = r * 2.f - 2.f;
- float c1p = fmaxf_(0.f, 1.f - 2.f * fabsf_(1.f - s));
- float c2p = fmaxf_(0.f, 1.f - 2.f * s);
- float cb = 1.f - fminf_(1.f, c1p + c2p);
- static const float red[3] = {0xFE / 255.f, 0x5F / 255.f, 0x55 / 255.f};
- static const float blue[3] = {0x00, 0x9D / 255.f, 1.f};
- static const float black[3] = {0.6f * 79 / 255.f, 0.6f * 99 / 255.f, 0.6f * 103 / 255.f};
- float fl = fmaxf_(c1p, c2p) * 5.f - 4.4f;
- if (fl < 0) fl = 0;
+ int s = r * 2 - 512; /* -2..2 */
+ int c1p = imax(0, 256 - 2 * iabs(256 - s)), c2p = imax(0, 256 - 2 * s), cb = 256 - imin(256, c1p + c2p);
+ static const int16_t red[3] = {0xFE, 0x5F, 0x55}, blue[3] = {0x00, 0x9D, 0xFF}, black[3] = {47, 59, 62};
+ int fl = imin(1024, imax(0, imax(c1p, c2p) * 5 - 1126)); /* max * 5 - 4.4 (past 4: white all the same) */
for (int j = 0; j < 3; j++) {
- float w = red[j] * c1p + blue[j] * c2p + black[j] * cb;
- o[j] = w * (1 - fl) + fl;
+ int w = (red[j] * c1p + blue[j] * c2p + black[j] * cb) / 256;
+ o[j] = (w * (256 - fl) + 255 * fl) / 256;
}
}
- for (int j = 0; j < 3; j++) out[j] = (uint8_t)(fminf_(1.f, fmaxf_(0.f, o[j])) * 255.f + .5f);
+ for (int j = 0; j < 3; j++) out[j] = (uint8_t)imax(0, imin(255, o[j]));
}
static void make_lut(void) {
for (int i = 0; i <= 32; i++) colour_at(i * 8 > 255 ? 255 : i * 8, lut[i]);
- lut_dirty = 0;
+ lut_dirty = 0, lut_made = 1;
}
void bg_mode(int m) {
@@ -69,91 +93,84 @@ void bg_mode(int m) {
}
}
-static void set3(float d[3], uint32_t hex, float k) {
- d[0] = ((hex >> 16) & 255) / 255.f * k;
- d[1] = ((hex >> 8) & 255) / 255.f * k;
- d[2] = (hex & 255) / 255.f * k;
+/* a channel times k (in 1/10), times 256 */
+static void set3(int d[3], uint32_t hex, int k10) {
+ d[0] = (int)((hex >> 16) & 255) * k10 * 256 / 10;
+ d[1] = (int)((hex >> 8) & 255) * k10 * 256 / 10;
+ d[2] = (int)(hex & 255) * k10 * 256 / 10;
}
/* Balatro's ease_background_colour, as src/bg.c */
void bg_colours(uint32_t col, uint32_t special, uint32_t tertiary, float contr, float spin_amount, int instant) {
if (special && tertiary) {
- set3(target[1], col, 1.f);
- set3(target[0], special, 1.f);
- set3(target[2], tertiary, 1.f);
+ set3(target[1], col, 10);
+ set3(target[0], special, 10);
+ set3(target[2], tertiary, 10);
} else {
- set3(target[1], col, 1.3f);
- set3(target[2], col, special ? 0.4f : 0.7f);
- if (special) set3(target[0], special, 1.f);
- else set3(target[0], col, 0.9f);
+ set3(target[1], col, 13);
+ set3(target[2], col, special ? 4 : 7);
+ if (special) set3(target[0], special, 10);
+ else set3(target[0], col, 9);
}
- contrast_target = contr;
- spin_target = spin_amount;
+ contrast_target = (int)(contr * 256);
+ spin_target = (int)(spin_amount * 256);
+ if (contrast_target < 64) contrast_target = 64;
if (instant) {
memcpy(colours, target, sizeof colours);
- contrast = contr;
- spin = spin_amount;
+ contrast = contrast_target;
+ spin = spin_target;
}
lut_dirty = 1;
}
void bg_reset(void) { lut_dirty = 1; }
-/* the colours: eased like the game, then run through the levels */
+/* the colours: eased like the game (over 0.6 s), then run through the levels */
void bg_update(float dt, int rows) {
(void)rows;
- float k = dt / 0.6f * 3.f;
- if (k > 1) k = 1;
+ int ms = (int)(dt * 1000.f);
+ if (ms > 200) ms = 200;
+ int k = ms * 1280 / 1000; /* dt / 0.6 * 3, in 1/256 */
+ if (k > 256) k = 256;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
- float d = target[i][j] - colours[i][j];
- if (d > 0.002f || d < -0.002f) lut_dirty = 1, colours[i][j] += d * k;
+ int d = target[i][j] - colours[i][j];
+ if (iabs(d) > 128) lut_dirty = 1, colours[i][j] += d / 256 * k + (d % 256) * k / 256;
else colours[i][j] = target[i][j];
}
- float dc = contrast_target - contrast, ds = spin_target - spin;
- if (dc > 0.01f || dc < -0.01f) lut_dirty = 1, contrast += dc * k;
+ int dc = contrast_target - contrast, ds = spin_target - spin;
+ if (iabs(dc) > 3) lut_dirty = 1, contrast += dc * k / 256;
else contrast = contrast_target;
- if (ds > 0.001f || ds < -0.001f) lut_dirty = 1, spin += ds * (dt * 2.f > 1 ? 1 : dt * 2.f);
+ if (iabs(ds) > 1) lut_dirty = 1, spin += ds * imin(256, ms * 512 / 1000) / 256;
else spin = spin_target;
if (lut_dirty) make_lut();
- /* a full turn of the colours every 8 s in a run, 4 s on the title (in 1/65536 of a paint value) */
- phase_frac += (uint32_t)(dt * (mode ? 128.f : 64.f) * 65536.f);
- phase = (phase + (unsigned)(phase_frac >> 16)) & 511;
- phase_frac &= 0xFFFF;
- static const uint8_t dark[CE_BANKS] = CE_BANK_DARK;
+ /* the colours sway along the paint, two slow waves: its bands move as the paint flowing, and every colour stays
+ where the game has it (a sine table, a quarter in 16 steps) */
+ static const int8_t sine[16] = {0, 12, 25, 37, 49, 60, 71, 81, 90, 98, 106, 112, 117, 122, 125, 127};
+ phase_frac = (phase_frac + (unsigned)ms) % 42000u; /* (a common period of both waves) */
+ int a = (int)(phase_frac % 7000u * 64 / 7000), b2 = (int)(phase_frac % 3000u * 64 / 3000);
+#define SINE(t) ((t) < 16 ? sine[t] : (t) < 32 ? sine[31 - (t)] : (t) < 48 ? -sine[(t) - 32] : -sine[63 - (t)])
+ phase = (unsigned)(512 + (SINE(a) * (mode ? 40 : 32) + SINE(b2) * (mode ? 18 : 14)) / 127) & 511;
+#undef SINE
+ if (phase == shown_phase && !lut_made) return;
+ shown_phase = phase, lut_made = 0;
uint16_t pal[CE_BANKS * CE_LEVELS];
- for (int l = 0; l < CE_LEVELS; l++) {
- unsigned p = (unsigned)(l * 16 + 8 + (int)phase) & 511;
- if (p > 255) p = 511 - p;
- int i = (int)(p >> 3), f = (int)(p & 7);
- int rgb[3];
- for (int j = 0; j < 3; j++) rgb[j] = (lut[i][j] * (8 - f) + lut[i + 1][j] * f) >> 3;
- for (int b = 0; b < CE_BANKS; b++) {
- int m = 32 - dark[b], r = rgb[0] * m >> 5, g = rgb[1] * m >> 5, bl = rgb[2] * m >> 5;
- pal[b * CE_LEVELS + l] = (uint16_t)((r & 248) << 7 | (g & 248) << 2 | bl >> 3);
- }
- }
+ ce_bg_pal((uint8_t *)pal, &lut[0][0], phase, keep);
ce_set_palette(CE_SWIRL0, CE_BANKS * CE_LEVELS, pal);
}
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 (n pixels, even) */
-static void unpack(uint8_t *dst, const uint8_t *src, int n) {
- if (n > 0) ce_unpack(dst, src, (unsigned)n / 2);
-}
-
void ce_bg_row(uint8_t *dst, int y, int x0) {
- x0 &= ~1; /* (the pixel before is covered) */
if (mode == 0) {
- const uint8_t *row = field[0] + y * (BG_RUN_W / 2);
+ const uint8_t *row = field[0] + y * BG_RUN_W;
if (x0 < BG_RUN_X) { /* (mirrored: the sidebar covers it) */
- static uint8_t tmp[BG_RUN_X];
- unpack(tmp, row, BG_RUN_X);
- for (int x = x0; x < BG_RUN_X; x++) dst[x] = tmp[BG_RUN_X - 1 - x];
+ for (int x = x0; x < BG_RUN_X; x++) dst[x] = row[BG_RUN_X - 1 - x];
x0 = BG_RUN_X;
}
- unpack(dst + x0, row + (x0 - BG_RUN_X) / 2, SCR_W - x0);
- } else
- unpack(dst + x0, field[1] + y * (SCR_W / 2) + x0 / 2, SCR_W - x0);
+ memcpy(dst + x0, row + (x0 - BG_RUN_X), (size_t)(SCR_W - x0));
+ } else {
+ const uint8_t *row = y < SCR_H / 2 ? field[1] + y * SCR_W : field[2] + (y - SCR_H / 2) * SCR_W;
+ memcpy(dst + x0, row + x0, (size_t)(SCR_W - x0));
+ }
}
diff --git a/games/balatro/ce/src/blit.S b/games/balatro/ce/src/blit.S
index 2cf936a5..6dfb7c0b 100644
--- a/games/balatro/ce/src/blit.S
+++ b/games/balatro/ce/src/blit.S
@@ -1,4 +1,4 @@
-/* The renderer's busiest loops in eZ80 code (src/gfx_ce.c, bg_ce.c). The C library's calling convention: arguments
+/* The renderer's busiest loops in eZ80 code (src/gfx_ce.c). The C library's calling convention: arguments
* on the stack (3 bytes each), ix kept. */
.assume adl = 1
@@ -19,54 +19,556 @@ _ce_shade:
dec hl
ld hl, (hl) ; d
ex de, hl ; de = d, hl = tab
+ ld a, c ; (b the count's low byte, c its 256s: the first round b's)
+ ld c, b
+ ld b, a
+ or a, a
+ jr z, .L.shade
+ inc c
.L.shade:
ld a, (de)
ld l, a
ld a, (hl)
ld (de), a
inc de
- dec bc
- ld a, c
- or a, b
+ djnz .L.shade
+ dec c
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
+/* unsigned ce_hash(unsigned h, const void *p, unsigned n): h times 33, then xor each byte; 0 < n < 256 */
+ .section .text.ce_hash, "ax", @progbits
+ .global _ce_hash
+ .type _ce_hash, @function
+_ce_hash:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy + 3) ; h
+ ld bc, (iy + 6) ; p
+.L.hash:
+ push hl
+ pop de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, de
+ ld a, (bc)
+ xor a, l
+ ld l, a
+ inc bc
+ dec (iy + 9)
+ jr nz, .L.hash
+ ret
+
+/* unsigned ce_hash_words(unsigned h, const void *p, unsigned n): h times 33, plus each 3 bytes; 0 < n < 256 */
+ .section .text.ce_hash_words, "ax", @progbits
+ .global _ce_hash_words
+ .type _ce_hash_words, @function
+_ce_hash_words:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy + 3) ; h
+ ld b, (iy + 9) ; n
+ ld iy, (iy + 6) ; p
+.L.words:
+ push hl
+ pop de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, de
+ ld de, (iy)
+ add hl, de
+ lea iy, iy + 3
+ djnz .L.words
+ ret
+
+/* The frame's strips' hashes (src/gfx_ce.c): void ce_hash_items(unsigned *hs, const item_t *it, unsigned n,
+ const unsigned *clip_hash). An item's hash: its clip's, times 33 plus each 3 bytes of its first 18 (where, what,
+ its animation's frame, its first strip), times 33 xor each byte of its text; each strip it covers then gets
+ times 33 plus it (the order counts). An item: +9 its clip, +17 and +18 its strips, +19 its text, 22 bytes. */
+ .section .text.ce_hash_items, "ax", @progbits
+ .global _ce_hash_items
+ .type _ce_hash_items, @function
+_ce_hash_items:
+ push ix
+ ld ix, 0
+ add ix, sp
+ dec sp ; (ix - 1) strips left
+ ld iy, (ix + 9)
+.L.hitem:
+ ld hl, (ix + 12) ; items left
+ ld de, 0
+ or a, a
+ sbc hl, de
+ jp z, .L.hdone
dec hl
- ld hl, (hl) ; d
- ex de, hl ; de = d, hl = s
-.L.unpack:
+ ld (ix + 12), hl
+ ld e, (iy + 9) ; the clip's hash
+ ld hl, (ix + 15)
+ add hl, de
+ add hl, de
+ add hl, de
+ ld hl, (hl)
+ .irp k, 0, 3, 6, 9, 12, 15
+ push hl
+ pop de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, de
+ ld de, (iy + \k)
+ add hl, de
+ .endr
+ ld a, (iy + 21) ; the text (none: its pointer's top byte 0)
+ or a, a
+ jr z, .L.hstrips
+ ld de, (iy + 19)
+.L.hstr:
+ ld a, (de)
+ or a, a
+ jr z, .L.hstrips
+ push hl
+ pop bc
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, bc
+ xor a, l
+ ld l, a
+ inc de
+ jr .L.hstr
+.L.hstrips:
+ ex de, hl ; de = the item's hash
+ ld a, (iy + 18)
+ sub a, (iy + 17)
+ inc a
+ ld (ix - 1), a
+ ld bc, 0
+ ld c, (iy + 17)
+ ld hl, (ix + 6)
+ add hl, bc
+ add hl, bc
+ add hl, bc ; its first strip's
+.L.hstrip:
+ push hl
+ ld hl, (hl)
+ push hl
+ pop bc
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, bc
+ add hl, de
+ ex de, hl
+ ex (sp), hl
+ ld (hl), de
+ inc hl
+ inc hl
+ inc hl
+ pop de
+ dec (ix - 1)
+ jr nz, .L.hstrip
+ lea iy, iy + 22
+ jp .L.hitem
+.L.hdone:
+ ld sp, ix
+ pop ix
+ 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
+ .type _ce_hash_str, @function
+_ce_hash_str:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy + 3) ; h
+ ld bc, (iy + 6) ; s
+.L.str:
+ ld a, (bc)
+ or a, a
+ ret z
+ push hl
+ pop de
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, de
+ xor a, l
+ ld l, a
+ inc bc
+ jr .L.str
+
+/* void ce_glyph(uint8_t *d, const uint8_t *rows, unsigned n, unsigned col): n rows (1..255) of a glyph, a byte a row
+ (the left column its top bit), in colour col; d its left on the first row, in the strip (rows of 320) */
+ .section .text.ce_glyph, "ax", @progbits
+ .global _ce_glyph
+ .type _ce_glyph, @function
+_ce_glyph:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy + 3) ; d
+ ld de, (iy + 6) ; rows
+ ld b, (iy + 9) ; n
+ ld c, (iy + 12) ; col
+.L.grow:
+ ld a, (de)
+ inc de
+ push hl
+ or a, a
+ jr z, .L.gend
+.L.gbit:
+ add a, a
+ jr nc, .L.g0
+ ld (hl), c
+.L.g0:
+ inc hl
+ jr nz, .L.gbit ; (bits left: add's zero flag)
+.L.gend:
+ pop hl
+ push de
+ ld de, 320
+ add hl, de
+ pop de
+ djnz .L.grow
+ ret
+
+/* void ce_glyph_shade(uint8_t *d, const uint8_t *rows, unsigned n, const uint8_t *tab): the same, the pixels under it
+ darkened through tab (256-byte aligned) */
+ .section .text.ce_glyph_shade, "ax", @progbits
+ .global _ce_glyph_shade
+ .type _ce_glyph_shade, @function
+_ce_glyph_shade:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy + 3) ; d
+ ld c, (iy + 9) ; n
+ ld de, (iy + 12) ; tab
+ ld iy, (iy + 6) ; rows
+.L.srow:
+ ld b, (iy)
+ inc iy
+ push hl
+.L.sbit:
+ sla b
+ jr nc, .L.s0
+ ld e, (hl)
+ ld a, (de)
+ ld (hl), a
+.L.s0:
+ inc hl
+ jr nz, .L.sbit ; (bits left: sla's zero flag)
+ pop hl
+ push bc
+ ld bc, 320
+ add hl, bc
+ pop bc
+ dec c
+ jr nz, .L.srow
+ ret
+
+/* A sprite's rows of runs (tools/mkart.py) into the strip, the sprite wholly between the clip's columns:
+ void ce_rle(ce_rle_t *b), b as src/ce.h has it */
+ .section .text.ce_rle, "ax", @progbits
+ .global _ce_rle
+ .type _ce_rle, @function
+_ce_rle:
+ pop de
+ pop iy
+ push iy
+ push de
+ push ix
+.L.rrow:
+ ld hl, (iy + 3) ; the row's offset in the table
+ ld bc, 0
+ ld c, (hl)
+ inc hl
+ ld b, (hl)
+ inc hl
+ ld (iy + 3), hl
+ ld hl, (iy + 6)
+ add hl, bc ; its runs
+ ld bc, 0
+ ld de, (iy + 0) ; where they go
+ ld a, (iy + 10)
+ ld ixl, a ; the width left
+ ld a, (iy + 13) ; (the table's top byte: none, a copy)
+ or a, a
+ jr nz, .L.shrun
+.L.run: ; (bc's top bytes are 0)
+ ld c, (hl) ; the skip
+ inc hl
+ ex de, hl
+ add hl, bc
+ ex de, hl
+ ld a, ixl
+ sub a, c
+ ld c, (hl) ; the length
+ inc hl
+ sub a, c
+ ld ixl, a
+ inc c
+ dec c
+ jr z, .L.rnext
+ ldir
+.L.rnext:
+ ld a, ixl
+ or a, a
+ jr nz, .L.run
+.L.rdone:
+ ld hl, (iy + 0)
+ ld bc, 320
+ add hl, bc
+ ld (iy + 0), hl
+ dec (iy + 9)
+ jr nz, .L.rrow
+ pop ix
+ ret
+.L.shrun: ; a shadow: the pixels skipped, what is under them darkened
+ ld c, (hl)
+ inc hl
+ ex de, hl
+ add hl, bc
+ ex de, hl
+ ld a, ixl
+ sub a, c
+ ld c, (hl)
+ inc hl
+ sub a, c
+ ld ixl, a
+ inc c
+ dec c
+ jr z, .L.shnext
+ add hl, bc ; (past the pixels)
+ push hl
+ ld hl, (iy + 11) ; the table
+ ld b, c
+.L.shpix:
+ ld a, (de)
+ ld l, a
ld a, (hl)
- and a, 15
- add a, 192
ld (de), a
inc de
- ld a, (hl)
+ djnz .L.shpix
+ pop hl
+.L.shnext:
+ ld a, ixl
+ or a, a
+ jr nz, .L.shrun
+ jr .L.rdone
+
+/* The background's 64 colours (src/bg_ce.c): void ce_bg_pal(uint8_t *out, const uint8_t *lut, unsigned phase,
+ const uint8_t *keep). Level l (0..15) shows the paint value p = (l * 16 + 8 + phase) mod 512, folded at 256; its
+ colour is lut's (33 RGB colours, every 8 values) between the two around it, then for each of the 4 banks times
+ keep[b] / 256 (0: as it is); out gets the 1555 colours bank after bank, the low byte first. */
+ .section .text.ce_bg_pal, "ax", @progbits
+ .global _ce_bg_pal
+ .type _ce_bg_pal, @function
+_ce_bg_pal:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld hl, -5 ; (ix - 3..1) the level's colour, (ix - 4) the level, (ix - 5) a green
+ add hl, sp
+ ld sp, hl
+ ld (ix - 4), 0
+.L.lev:
+ ld a, (ix - 4)
+ add a, a
+ add a, a
+ add a, a
+ add a, a
+ add a, 8
+ ld hl, (ix + 12) ; phase
+ ld de, 0
+ ld e, a
+ add hl, de
+ ld a, l
+ bit 0, h
+ jr z, .L.fold
+ cpl ; (511 - q)
+.L.fold:
+ ld b, a ; p
+ rrca
+ rrca
+ rrca
+ and a, 31
+ ld e, a
+ add a, a
+ add a, e
+ ld e, a ; (d is 0)
+ ld iy, (ix + 9)
+ add iy, de ; the lut's colour at or below p
+ ld a, b
+ and a, 7
+ jr nz, .L.mix
+ ld a, (iy + 0)
+ ld (ix - 3), a
+ ld a, (iy + 1)
+ ld (ix - 2), a
+ ld a, (iy + 2)
+ ld (ix - 1), a
+ jr .L.banks
+.L.mix: ; the two colours' mean, weighed (8 - f) / 8 and f / 8, in 1/256
rrca
rrca
rrca
+ ld b, a ; f * 32
+ neg
+ ld c, a ; 256 - f * 32
+ ld d, (iy + 0)
+ ld e, c
+ mlt de
+ ld h, (iy + 3)
+ ld l, b
+ mlt hl
+ add hl, de
+ ld (ix - 3), h
+ ld d, (iy + 1)
+ ld e, c
+ mlt de
+ ld h, (iy + 4)
+ ld l, b
+ mlt hl
+ add hl, de
+ ld (ix - 2), h
+ ld d, (iy + 2)
+ ld e, c
+ mlt de
+ ld h, (iy + 5)
+ ld l, b
+ mlt hl
+ add hl, de
+ ld (ix - 1), h
+.L.banks:
+ ld hl, (ix + 6)
+ ld de, 0
+ ld e, (ix - 4)
+ add hl, de
+ add hl, de ; out + 2 l
+ ld iy, (ix + 15) ; keep
+ ld b, 4
+.L.bank:
+ push bc
+ ld c, (iy)
+ inc iy
+ ld a, (ix - 2)
+ call .L.scale
+ ld (ix - 5), a ; green
+ ld a, (ix - 3)
+ call .L.scale ; red
+ rrca
+ and a, 0x7C
+ ld b, a
+ ld a, (ix - 5)
+ rlca
+ rlca
+ and a, 3
+ or a, b
+ inc hl
+ ld (hl), a ; 0rrrrrgg
+ dec hl
+ ld a, (ix - 1)
+ call .L.scale ; blue
rrca
- and a, 15
- add a, 192
+ rrca
+ rrca
+ and a, 31
+ ld b, a
+ ld a, (ix - 5)
+ add a, a
+ add a, a
+ and a, 0xE0
+ or a, b
+ ld (hl), a ; gggbbbbb
+ ld de, 32
+ add hl, de
+ pop bc
+ djnz .L.bank
+ inc (ix - 4)
+ ld a, (ix - 4)
+ cp a, 16
+ jp nz, .L.lev
+ ld sp, ix
+ pop ix
+ ret
+.L.scale: ; a times c / 256 (c 0: as it is)
+ inc c
+ dec c
+ ret z
+ ld d, a
+ ld e, c
+ mlt de
+ ld a, d
+ ret
+
+/* void ce_scale_row(uint8_t *d, const uint8_t *row, unsigned n, unsigned x8, unsigned step, const uint8_t *tab):
+ n pixels (1..255) of a sprite's row drawn at another width, pixel i from row[(x8 + i * step) / 256] (row 256-byte
+ aligned, 255 where nothing is): copied (tab NULL), or what is under it darkened through tab (256-byte aligned) */
+ .section .text.ce_scale_row, "ax", @progbits
+ .global _ce_scale_row
+ .type _ce_scale_row, @function
+_ce_scale_row:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld de, (ix + 6) ; d
+ ld hl, (ix + 9) ; row
+ ld b, (ix + 12) ; n
+ ld c, (ix + 15) ; x8's 256ths
+ ld a, (ix + 16)
+ ld l, a ; its whole pixels
+ ld a, (ix + 23) ; (the table's top byte: 0, a copy)
+ ld iy, (ix + 21)
+ ld ix, (ix + 18) ; the step: ixh whole pixels, ixl 256ths
+ or a, a
+ jr nz, .L.scsh
+.L.sccp:
+ ld a, (hl)
+ inc a
+ jr z, .L.sc0
+ dec a
ld (de), a
+.L.sc0:
inc de
- inc hl
- dec bc
ld a, c
- or a, b
- jr nz, .L.unpack
+ add a, ixl
+ ld c, a
+ ld a, l
+ adc a, ixh
+ ld l, a
+ djnz .L.sccp
+ pop ix
+ ret
+.L.scsh:
+ ld a, (hl)
+ inc a
+ jr z, .L.ss0
+ ld a, (de)
+ ld iyl, a
+ ld a, (iy)
+ ld (de), a
+.L.ss0:
+ inc de
+ ld a, c
+ add a, ixl
+ ld c, a
+ ld a, l
+ adc a, ixh
+ ld l, a
+ djnz .L.scsh
+ pop ix
ret
diff --git a/games/balatro/ce/src/ce.h b/games/balatro/ce/src/ce.h
index 9a90fedd..f7e8e627 100644
--- a/games/balatro/ce/src/ce.h
+++ b/games/balatro/ce/src/ce.h
@@ -18,7 +18,25 @@ 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 */
+unsigned ce_hash(unsigned h, const void *p, unsigned n); /* h * 33 ^ each byte, 0 < n < 256 */
+unsigned ce_hash_str(unsigned h, const char *s); /* the same over a string */
+unsigned ce_hash_words(unsigned h, const void *p, unsigned n); /* h * 33 + each 3 bytes (n of them), 0 < n < 256 */
+/* n rows (1..255) of a glyph, a byte a row (the left column its top bit), d its left on the first row (the strip's
+ rows are 320 bytes): in colour col, or the pixels under it darkened through tab (256-byte aligned) */
+void ce_glyph(uint8_t *d, const uint8_t *rows, unsigned n, unsigned col);
+void ce_glyph_shade(uint8_t *d, const uint8_t *rows, unsigned n, const uint8_t *tab);
+/* a sprite's rows of runs into the strip, the sprite wholly between the clip's columns */
+typedef struct {
+ uint8_t *d; /* the strip at the sprite's left, on the first row drawn */
+ const uint8_t *rt; /* that row's entry in the sprite's table of rows */
+ const uint8_t *spr; /* the sprite */
+ uint8_t n, sw; /* rows (1..255), the sprite's width (1..255) */
+ const uint8_t *tab; /* NULL: the pixels copied; or a table (256-byte aligned) darkening what is under them */
+} ce_rle_t;
+void ce_rle(ce_rle_t *b);
+/* n pixels (1..255) of a sprite's row at another width, pixel i from row[(x8 + i * step) / 256] (row 256-byte aligned,
+ 255 where nothing is): copied (tab NULL), or what is under it darkened through tab */
+void ce_scale_row(uint8_t *d, const uint8_t *row, unsigned n, unsigned x8, unsigned step, const uint8_t *tab);
/* ---- colours (gfx_ce.c) */
uint8_t ce_pal(C c); /* the palette index showing c: a fixed colour, or one taken for it */
@@ -34,6 +52,8 @@ extern const uint8_t *ce_edpat; /* BALED: the editions' sheen */
/* ---- the background (bg_ce.c) */
void ce_bg_row(uint8_t *dst, int y, int x0); /* row y's background, from x0 to the right edge */
+/* the background's 64 colours (1555, the low byte first) at phase (0..511) from lut's 33 colours (blit.S) */
+void ce_bg_pal(uint8_t *out, const uint8_t *lut, unsigned phase, const uint8_t *keep);
extern uint8_t ce_bg_ver; /* changes when the background's picture does (not its colours) */
int ce_bg_init(void);
diff --git a/games/balatro/ce/src/ce_main.c b/games/balatro/ce/src/ce_main.c
index 4e82f2f1..d4c7ef96 100644
--- a/games/balatro/ce/src/ce_main.c
+++ b/games/balatro/ce/src/ce_main.c
@@ -2,6 +2,7 @@
* (platform/ti84ce/README.md: 2nd OK, enter EXE, clear Back, mode and on Home, del backspace, y= shift, alpha, math
* toolbox, vars var), the clock and the files (the appvar NPbalato, archived when the game ends). The game itself is
* the NumWorks version's (../src, its main() as balatro_main()). */
+#include <stdio.h>
#include <string.h>
#include <eadk.h>
#include "ce.h"
@@ -56,6 +57,7 @@ uint32_t eadk_random(void) {
/* ================================================================== the calculator */
#include <sys/lcd.h>
#include <sys/rtc.h>
+#include <sys/timers.h>
#include <ti/getcsc.h>
#include <ti/screen.h>
#include <ti/vars.h>
@@ -68,11 +70,24 @@ void ce_set_palette(int first, int n, const uint16_t *c) {
for (int i = 0; i < n; i++) p[i] = c[i];
}
+/* the appvars through TI-OS's own routines (archive.S): a name as OP1 holds it */
+uint8_t *ce_var_find(const char *op1);
+uint8_t *ce_var_create(const char *op1, unsigned size);
+void ce_var_delete(const char *op1);
+static void op1_of(char op1[9], const char *name) {
+ int k = 0;
+ op1[0] = 0x15;
+ for (; k < 8 && name[k]; k++) op1[1 + k] = name[k];
+ for (; k < 8; k++) op1[1 + k] = 0;
+}
+
const uint8_t *ce_appvar(const char *name, size_t *size) {
- var_t *v = os_GetAppVarData(name, 0);
+ char op1[9];
+ op1_of(op1, name);
+ const uint8_t *v = ce_var_find(op1);
if (!v) return 0;
- *size = v->size;
- return v->data;
+ *size = (size_t)(v[0] | v[1] << 8);
+ return v + 2;
}
/* the clock: the 32768 Hz timer the C library starts for clock() */
@@ -82,8 +97,8 @@ uint64_t eadk_timing_millis(void) {
}
uint32_t ce_ms(void) { return (uint32_t)eadk_timing_millis(); }
void eadk_timing_msleep(uint32_t ms) {
- uint64_t until = eadk_timing_millis() + ms;
- while (eadk_timing_millis() < until) {}
+ if (ms > 1000) ms = 1000;
+ msleep((uint16_t)ms);
}
/* the keys: the keypad scanned once, its groups read */
@@ -111,24 +126,22 @@ eadk_keyboard_state_t eadk_keyboard_scan(void) {
/* the save in a RAM appvar of a fixed size while the game runs (archiving it could move the art in the archive: only
when the game ends); an archived one, or one of another size, is copied into a new one (through the strip buffer) */
uint8_t *ce_storage_open(const char *name, size_t size) {
- int arch = 0, scr;
- var_t *v = os_GetAppVarData(name, &arch);
- size_t keep = 0;
- uint8_t *tmp = g_scratch(&scr);
- if (v && (arch || v->size != size)) {
- keep = v->size < size ? v->size : size;
+ char op1[9];
+ op1_of(op1, name);
+ int scr;
+ uint8_t *v = ce_var_find(op1), *tmp = g_scratch(&scr);
+ size_t keep = 0, n = v ? (size_t)(v[0] | v[1] << 8) : 0;
+ if (v && v >= (uint8_t *)0xD00000 && n == size) return v + 2;
+ if (v) {
+ keep = n < size ? n : size;
if (keep > (size_t)scr) keep = (size_t)scr;
- memcpy(tmp, v->data, keep);
- void *entry, *data;
- if (os_ChkFindSym(OS_TYPE_APPVAR, name, &entry, &data)) os_DelSymEntry(entry);
- v = 0;
- } else if (v)
- return v->data;
- v = os_CreateAppVar(name, (uint16_t)size);
- if (!v) return 0;
- memcpy(v->data, tmp, keep);
- memset(v->data + keep, 0, size - keep);
- return v->data;
+ memcpy(tmp, v + 2, keep);
+ ce_var_delete(op1);
+ }
+ if (!(v = ce_var_create(op1, (unsigned)size))) return 0;
+ memcpy(v + 2, tmp, keep);
+ memset(v + 2 + keep, 0, size - keep);
+ return v + 2;
}
void ce_storage_keep(void) {}
@@ -146,10 +159,10 @@ static void game(void) { balatro_main(); }
/* the code's appvars (where each goes), then the others */
#define NCODE 4
-static const char BOOTDATA var_names[][9] = {"BALCV0", "BALCV1", "BALCP", "BALCB", "BALLUT", "BALFIRE",
- "BALBG0", "BALBG1", "BALED", "BALART0", "BALART1", "BALART2",
- "BALART3", "BALART4", "BALART5"};
-#define NVARS (NCODE + 5 + CE_ART_NVARS)
+static const char BOOTDATA var_names[][9] = {"BALCV0", "BALCV1", "BALCP", "BALCB", "BALLUT",
+ "BALFIRE", "BALBG0", "BALBG1", "BALBG2", "BALED",
+ "BALART0", "BALART1", "BALART2", "BALART3", "BALART4", "BALART5"};
+#define NVARS (NCODE + 6 + CE_ART_NVARS)
static const char BOOTDATA save_name[9] = "NPbalato";
static const char BOOTDATA msg_missing[] = "Balatro: a file is missing";
static const char BOOTDATA msg_send[] = "Send every Balatro file:";
@@ -178,12 +191,15 @@ BOOT int main(void) {
size_t len[NCODE];
for (unsigned i = 0; i < NVARS; i++) boot_archive(var_names[i]);
for (unsigned i = 0; i < NVARS; i++) {
- var_t *v = os_GetAppVarData(var_names[i], 0);
+ char op1[9];
+ op1[0] = 0x15;
+ for (int k = 0; k < 8; k++) op1[1 + k] = var_names[i][k];
+ const uint8_t *v = ce_var_find(op1);
if (!v) {
boot_fail();
return 0;
}
- if (i < NCODE) src[i] = v->data, len[i] = v->size;
+ if (i < NCODE) src[i] = v + 2, len[i] = (size_t)(v[0] | v[1] << 8);
}
if (len[0] + len[1] != (size_t)(ce_vram_hi - ce_vram_lo) || len[2] != (size_t)(ce_pshadow_hi - ce_pshadow_lo) ||
len[3] != (size_t)(ce_bssx_hi - ce_bssx_lo)) {
@@ -205,6 +221,14 @@ BOOT int main(void) {
ce_set_palette(0, CE_NSTATIC, ce_palette);
ce_on_stack(game, stack + sizeof stack);
}
+#ifdef CE_PROF
+ static char prof_text[160];
+ {
+ extern unsigned long ce_prof[16];
+ char *o = prof_text;
+ for (int i = 0; i < 16; i++) o += sprintf(o, "%lu,", ce_prof[i]);
+ }
+#endif
/* the OS's screen, its pixel shadow cleared */
memset((void *)0xD40000, 255, 320 * 240 * 2);
memset((void *)0xD031F6, 0, 8400);
@@ -214,6 +238,9 @@ BOOT int main(void) {
return 0;
}
os_ClrHomeFull();
+#ifdef CE_PROF
+ os_PutStrFull(prof_text);
+#endif
if (ce_save_pending) boot_archive(save_name); /* (the screen's half with code is gone: names from here) */
return 0;
}
diff --git a/games/balatro/ce/src/gfx_ce.c b/games/balatro/ce/src/gfx_ce.c
index f79f3c91..d665ae0b 100644
--- a/games/balatro/ce/src/gfx_ce.c
+++ b/games/balatro/ce/src/gfx_ce.c
@@ -7,6 +7,10 @@
* one of 16 free entries while the screen shows it. Shading (panels, shadows, Options over the game) goes through
* tables of every entry darkened. */
#include <string.h>
+#ifdef NP_HOST
+#include <stdio.h>
+#include <stdlib.h>
+#endif
#include "art.h"
#include "assets.h"
#include "bg.h"
@@ -34,19 +38,30 @@ typedef struct {
int16_t x, y, w, h;
uint8_t type, clip, r, fx;
uint16_t a, b;
+ uint8_t anim; /* the frame of what moves on its own (an edition's sheen, the fire) */
+ uint8_t s0, s1; /* the strips it covers */
const char *s;
} item_t;
+#ifndef NP_HOST
+_Static_assert(offsetof(item_t, clip) == 9 && offsetof(item_t, anim) == 16 && offsetof(item_t, s0) == 17 &&
+ offsetof(item_t, s) == 19 && sizeof(item_t) == 22,
+ "blit.S's ce_hash_items reads items");
+#endif
+void ce_hash_items(unsigned *hs, const item_t *it, unsigned n, const unsigned *clip_hash);
-static item_t items[MAXI];
+static item_t items[MAXI], *item_next;
static char strbuf[2560]; /* (a frame takes under 1 KB: the How to play page) */
static int strpos, nitems;
static int16_t clips[MAXCLIP][4];
static int nclips, cur_clip;
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) */
static int bg_swirl, bg_cover_x;
static uint8_t bg_solid;
static unsigned strip_hash[NSTRIP];
static uint8_t drawn_once;
+static unsigned ed_frame; /* the editions' sheen's frame */
uint32_t g_time;
void *g_scratch(int *size) {
@@ -56,13 +71,55 @@ void *g_scratch(int *size) {
void g_bg_cover(int x) { bg_cover_x = x; }
#ifdef NP_HOST
+#define CE_SLOW (getenv("NP_SLOW") != 0) /* (the C paths, to compare) */
/* (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));
+unsigned ce_hash(unsigned h, const void *p, unsigned n) {
+ for (const uint8_t *q = p; n; n--) h = (h * 33 ^ *q++) & 0xFFFFFF;
+ return h;
+}
+unsigned ce_hash_words(unsigned h, const void *p, unsigned n) {
+ for (const uint8_t *q = p; n; n--, q += 3) h = (h * 33 + (unsigned)(q[0] | q[1] << 8 | q[2] << 16)) & 0xFFFFFF;
+ return h;
+}
+unsigned ce_hash_str(unsigned h, const char *s) {
+ while (*s) h = (h * 33 ^ (uint8_t)*s++) & 0xFFFFFF;
+ return h;
+}
+void ce_glyph(uint8_t *d, const uint8_t *rows, unsigned n, unsigned col) {
+ for (; n; n--, d += SCR_W, rows++)
+ for (int x = 0; x < 8; x++)
+ if (*rows & (0x80 >> x)) d[x] = (uint8_t)col;
}
+void ce_glyph_shade(uint8_t *d, const uint8_t *rows, unsigned n, const uint8_t *tab) {
+ for (; n; n--, d += SCR_W, rows++)
+ for (int x = 0; x < 8; x++)
+ if (*rows & (0x80 >> x)) d[x] = tab[d[x]];
+}
+void ce_scale_row(uint8_t *d, const uint8_t *row, unsigned n, unsigned x8, unsigned step, const uint8_t *tab) {
+ for (; n; n--, d++, x8 += step) {
+ uint8_t v = row[(x8 >> 8) & 255];
+ if (v != 255) *d = tab ? tab[*d] : v;
+ }
+}
+void ce_rle(ce_rle_t *b) {
+ for (; b->n; b->n--, b->d += SCR_W, b->rt += 2) {
+ const uint8_t *row = b->spr + (b->rt[0] | b->rt[1] << 8);
+ uint8_t *d = b->d;
+ for (int left = b->sw; left > 0;) {
+ d += row[0];
+ int n = row[1];
+ left -= row[0] + n;
+ for (int i = 0; i < n; i++, d++) *d = b->tab ? b->tab[*d] : row[2 + i];
+ row += 2 + n;
+ }
+ }
+}
+#else
+#define CE_SLOW 0
+_Static_assert(offsetof(ce_rle_t, tab) == 11, "blit.S reads ce_rle_t");
#endif
C darken(C a, int t) { return mix565(a, 0, t); }
@@ -74,46 +131,54 @@ static uint16_t strip_dyn[NSTRIP], frame_dyn; /* the taken entries each strip sh
static struct {
C c;
uint8_t v, ok;
-} pcache[64];
+} pcache[256];
+static const uint16_t BIT16[16] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
+static uint8_t pal_r[CE_NSTATIC], pal_g[CE_NSTATIC], pal_b[CE_NSTATIC]; /* the fixed colours, 5 bits a channel */
+static uint16_t sq2[63], sq4[63], sq3[63]; /* the distance's terms, by difference */
-static int dist(C a, uint16_t p) {
- int dr = (int)(a >> 11) - (int)(p >> 10 & 31), dg = (int)((a >> 6) & 31) - (int)(p >> 5 & 31),
- db = (int)(a & 31) - (int)(p & 31);
- return 2 * dr * dr + 4 * dg * dg + 3 * db * db;
-}
static uint8_t nearest_fixed(C c, int *d) {
- int best = 0, bd = 1 << 20;
- for (int i = 0; i < CE_NSTATIC; i++) {
- int e = dist(c, ce_palette[i]);
- if (e < bd) bd = e, best = i;
+ int r = c >> 11, g = (c >> 6) & 31, b = c & 31;
+ unsigned best = 0, bd = 0xFFFF;
+ for (unsigned i = 0; i < CE_NSTATIC; i++) {
+ unsigned e = (unsigned)sq2[r - pal_r[i] + 31] + sq4[g - pal_g[i] + 31] + sq3[b - pal_b[i] + 31];
+ if (e < bd) {
+ bd = e, best = i;
+ if (!e) break;
+ }
}
- if (d) *d = bd;
+ if (d) *d = (int)bd;
return (uint8_t)best;
}
static void shade_tabs_reset(void);
uint8_t ce_pal(C c) {
- unsigned h = (c ^ c >> 6 ^ c >> 11) & 63;
+ 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) */
if (pcache[h].ok && pcache[h].c == c) {
uint8_t v = pcache[h].v;
- if (v < CE_DYN0 || v >= CE_SWIRL0 || dyn_col[v - CE_DYN0] == c) {
- if (v >= CE_DYN0 && v < CE_SWIRL0) frame_dyn |= (uint16_t)(1u << (v - CE_DYN0));
+ if (v < CE_DYN0 || v >= CE_SWIRL0) return v;
+ if (dyn_col[v - CE_DYN0] == c) {
+ frame_dyn |= BIT16[v - CE_DYN0];
return v;
}
}
int d;
+#ifdef NP_HOST
+ extern long ce_pal_misses;
+ ce_pal_misses++;
+ if (getenv("NP_PAL")) fprintf(stderr, "%u: miss %04X (slot %d had %04X)\n", (unsigned)g_time, c, h, pcache[h].c);
+#endif
uint8_t v = nearest_fixed(c, &d);
- if (d > 6) {
+ if (d > 24) {
/* a free entry, or one already showing it */
uint16_t used = frame_dyn;
for (int s = 0; s < NSTRIP; s++) used |= strip_dyn[s];
int k = -1;
for (int i = 0; i < NDYN; i++)
- if (dyn_col[i] == c && (used >> i & 1)) k = i;
+ if (dyn_col[i] == c && (used & BIT16[i])) k = i;
if (k < 0)
for (int i = 0; i < NDYN && k < 0; i++)
- if (!(used >> i & 1)) k = i;
+ if (!(used & BIT16[i])) k = i;
if (k >= 0) {
if (dyn_col[k] != c) {
dyn_col[k] = c;
@@ -121,7 +186,7 @@ uint8_t ce_pal(C c) {
ce_set_palette(CE_DYN0 + k, 1, &p);
shade_tabs_reset();
}
- frame_dyn |= (uint16_t)(1u << k);
+ frame_dyn |= BIT16[k];
v = (uint8_t)(CE_DYN0 + k);
}
}
@@ -151,7 +216,7 @@ const uint8_t *ce_shade_tab(int alpha) {
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];
- for (int k = 0; k < NDYN; k++) t[CE_DYN0 + k] = used >> k & 1 ? nearest_fixed(mix565(dyn_col[k], 0, alpha), 0) : 0;
+ for (int k = 0; k < NDYN; k++) t[CE_DYN0 + k] = used & BIT16[k] ? nearest_fixed(mix565(dyn_col[k], 0, alpha), 0) : 0;
for (int b = 0; b < CE_BANKS; b++) {
/* the background's banks: the darkness after shading, to the nearest bank */
int dark = 32 - (32 - bank_dark[b]) * (32 - alpha) / 32, nb = 0;
@@ -165,7 +230,7 @@ const uint8_t *ce_shade_tab(int alpha) {
/* ------------------------------------------------------------------ the list */
#define MAXR 8 /* (the interface's corners are 7 at most) */
-static uint8_t cov[MAXR + 1][MAXR][MAXR];
+static uint8_t cut[MAXR + 1][MAXR]; /* a corner's row: the pixels left out at the edge (under half covered) */
void g_init(void) {
for (int r = 1; r <= MAXR; r++)
@@ -178,18 +243,48 @@ void g_init(void) {
int dx = 8 * r - (8 * x + 2 * sx + 1), dy = 8 * r - (8 * y + 2 * sy + 1);
if (dx * dx + dy * dy <= 64 * r * r) n++;
}
- cov[r][y][x] = (uint8_t)(n * 2);
+ if (n * 2 < 16) cut[r][y] = (uint8_t)(x + 1); /* (coverage grows towards the middle) */
}
+ for (int i = 0; i < CE_NSTATIC; i++)
+ pal_r[i] = ce_palette[i] >> 10 & 31, pal_g[i] = ce_palette[i] >> 5 & 31, pal_b[i] = ce_palette[i] & 31;
+ for (int d = -31; d <= 31; d++) sq2[d + 31] = (uint16_t)(2 * d * d), sq4[d + 31] = (uint16_t)(4 * d * d),
+ sq3[d + 31] = (uint16_t)(3 * d * d);
drawn_once = 0;
}
+#ifdef CE_PROF
+/* where a frame's time goes (ce_main.c prints it at the end), in thousands of cycles: 0 between frames, 1 the list,
+ 2 the hashes, 3 the strips (4 how many, 5 frames), then in the strips 6 + an item's type, 13 the background, 14 the
+ copy to the screen */
+#include <sys/timers.h>
+unsigned long ce_prof[16];
+static unsigned long prof_t, prof_c[16];
+static unsigned long prof_now(void) {
+ static int on;
+ if (!on) on = 1, timer_Enable(2, TIMER_CPU, TIMER_NOINT, TIMER_UP);
+ return timer_GetSafe(2, TIMER_UP);
+}
+#define PROF(k) \
+ do { \
+ unsigned long t_ = prof_now(); \
+ prof_c[k] += t_ - prof_t; \
+ ce_prof[k] = prof_c[k] / 1000; \
+ prof_t = t_; \
+ } while (0)
+#else
+#define PROF(k) ((void)0)
+#endif
+
void g_begin(void) {
+ PROF(0); /* (the time between frames: the rules, the keys, the waiting) */
nitems = 0;
+ item_next = items;
bg_cover_x = 0;
strpos = 0;
nclips = 1;
cur_clip = 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;
}
@@ -207,11 +302,14 @@ 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];
if (x >= c[2] || y >= c[3] || x + w <= c[0] || y + h <= c[1]) return 0;
- item_t *it = &items[nitems++];
+ item_t *it = item_next++;
+ 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->fx = 0, it->r = 0, it->a = 0, it->b = 0, it->s = 0;
+ 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) */
return it;
}
@@ -244,11 +342,11 @@ void g_sprite(int id, int x, int y, int fx, int scale) {
w = nw, h = nh;
}
item_t *it = add(I_SPRITE, x, y, w, h);
- if (it) it->a = (uint16_t)id, it->fx = (uint8_t)fx, it->b = (uint16_t)scale;
+ if (it) it->a = (uint16_t)id, it->fx = (uint8_t)fx, it->b = (uint16_t)scale, it->anim = fx & FX_EDMASK ? ed_frame : 0;
}
void g_sprite_wh(int id, int x, int y, int w, int h, int fx) {
item_t *it = add(I_SPRITE, x, y, w, h);
- if (it) it->a = (uint16_t)id, it->fx = (uint8_t)fx, it->b = 0;
+ if (it) it->a = (uint16_t)id, it->fx = (uint8_t)fx, it->b = 0, it->anim = fx & FX_EDMASK ? ed_frame : 0;
}
void g_rsquare(int cx, int cy, int hs, int angle, C c) {
int R = hs * 3 / 2 + 1;
@@ -258,7 +356,7 @@ void g_rsquare(int cx, int cy, int hs, int angle, C c) {
void g_flame(int k, int x, int y, int w, int h, int top) {
if (!flame_on()) return;
item_t *it = add(I_FLAME, x, y, w, h);
- if (it) it->a = (uint16_t)k, it->b = (uint16_t)top;
+ if (it) it->a = (uint16_t)k, it->b = (uint16_t)top, it->anim = (uint8_t)ce_flame_frame();
}
void g_bg(int swirl, C solid) {
bg_swirl = swirl;
@@ -308,11 +406,43 @@ static int advance(int f, unsigned ch, const glyph_t *g) {
return a;
}
+/* 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;
+ int8_t a[99];
+} advs[NADV];
+static uint8_t adv_next;
+#ifdef NP_HOST
+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;
+ 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;
+ advs[i].a[ch - 32] = (int8_t)advance(f, c, glyph(f, c, &bits));
+ }
+ advs[i].key = (uint16_t)key;
+#ifdef NP_HOST
+ ce_adv_made++;
+#endif
+ return advs[i].a - 32;
+}
+
static int textw(const char *s, int f, int one_line) {
int w = 0, best = 0, badge = 0;
- const uint8_t *bits;
+ const int8_t *adv = adv_tab(f);
for (; *s; s++) {
unsigned char ch = (unsigned char)*s;
+ if (ch >= 32) {
+ w += adv[ch > 130 ? '?' : ch];
+ continue;
+ }
if (ch == '\n') {
if (badge) w += 2, badge = 0;
if (w > best) best = w;
@@ -320,13 +450,8 @@ static int textw(const char *s, int f, int one_line) {
w = 0;
continue;
}
- if (ch < 32) {
- if (ch == 14 && !badge) w += 2, badge = 1;
- else if (badge && ch != 14) w += 2, badge = 0;
- continue;
- }
- if (ch > (f & (T_LARGE | T_MED) ? 126 : 130)) ch = '?';
- w += advance(f, ch, glyph(f, ch, &bits));
+ if (ch == 14 && !badge) w += 2, badge = 1;
+ else if (badge && ch != 14) w += 2, badge = 0;
}
if (badge) w += 2;
if (w > best) best = w;
@@ -378,24 +503,40 @@ void g_text_box(const char *s, int x, int y, int w, int h, int f, C c) {
/* one glyph's pixels (its shadow first), into the strip rows ra..rb, columns cx0..cx1 */
static void put_glyph(const glyph_t *g, const uint8_t *b, int f, int dc, int x, int y, uint8_t col, int y0, int ra,
int rb, int cx0, int cx1, const uint8_t *shadow) {
+ static const uint8_t BIT8[8] = {1, 2, 4, 8, 16, 32, 64, 128};
int sc = (f & T_X2) ? 2 : 1, gh = glyph_rows(f);
for (int pass = shadow ? 0 : 1; pass < 2; pass++) {
- unsigned bit = g->off;
- int dy = pass ? 0 : sc;
+ /* the bits column by column, a byte and a mask walked along, the strip by a pointer moved a row at a time (the
+ eZ80 shifts and multiplies by calls) */
+ const uint8_t *p = b + (g->off >> 3);
+ uint8_t m = BIT8[g->off & 7];
+ int top = y + (pass ? 0 : sc);
+ uint8_t *base = strip + (top - y0) * SCR_W;
for (int gx = 0; gx < g->w; gx++) {
- if (gx == dc) {
- bit += gh;
+ int px = x + (gx - (dc >= 0 && gx > dc)) * sc, skip = gx == dc || px >= cx1 || px + sc <= cx0;
+ if (skip) {
+ for (int gy = 0; gy < gh; gy++) {
+ m = (uint8_t)(m + m);
+ if (!m) m = 1, p++;
+ }
continue;
}
- int px = x + (gx - (dc >= 0 && gx > dc)) * sc;
- for (int gy = 0; gy < gh; gy++, bit++) {
- if (!((b[bit >> 3] >> (bit & 7)) & 1)) continue;
- int py = y + gy * sc + dy;
- for (int k = 0; k < sc * sc; k++) {
- int X = px + (k & (sc - 1)), Y = py + (k >> (sc - 1));
+ uint8_t *d = base + px;
+ int py = top;
+ for (int gy = 0; gy < gh; gy++, py += sc, d += sc * SCR_W) {
+ int on = *p & m;
+ m = (uint8_t)(m + m);
+ if (!m) m = 1, p++;
+ if (!on) continue;
+ if (sc == 1) {
+ if (py >= ra && py < rb) *d = pass ? col : shadow[*d];
+ continue;
+ }
+ for (int k = 0; k < 4; k++) {
+ int X = px + (k & 1), Y = py + (k >> 1);
if (Y >= ra && Y < rb && X >= cx0 && X < cx1) {
- uint8_t *d = strip + (Y - y0) * SCR_W + X;
- *d = pass ? col : shadow[*d];
+ uint8_t *q = d + (k & 1) + (k >> 1) * SCR_W;
+ *q = pass ? col : shadow[*q];
}
}
}
@@ -403,6 +544,24 @@ static void put_glyph(const glyph_t *g, const uint8_t *b, int f, int dc, int x,
}
}
+/* the same row by row in eZ80 code (tools/mkart.py's glyph_rows), the glyph wholly between the clip's columns and
+ at its size: e its entry (where its rows start, its width), bits the size's rows */
+static void fast_glyph(const uint8_t *e, const uint8_t *bits, int gh, int x, int y, uint8_t col, int y0, int ra, int rb,
+ const uint8_t *shadow) {
+ const uint8_t *rows = bits + *(const uint16_t *)e;
+ for (int pass = shadow ? 0 : 1; pass < 2; pass++) {
+ int top = y + 1 - pass, r0 = ra > top ? ra - top : 0, r1 = rb - top < gh ? rb - top : gh;
+ if (r0 >= r1) continue;
+ uint8_t *d = strip + ROW_OFF[top + r0 - y0] + x;
+ const uint8_t *r = rows + r0;
+ unsigned n = (unsigned)(r1 - r0);
+ for (int part = e[2] > 8 ? 2 : 1; part; part--, d += 8, r += gh) {
+ if (pass) ce_glyph(d, r, n, col);
+ else ce_glyph_shade(d, r, n, shadow);
+ }
+ }
+}
+
static void draw_text_rot(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
int f = it->b, gh = glyph_rows(f);
int x = it->x + 2, pen = it->y + it->h - 2;
@@ -440,6 +599,16 @@ static void draw_text(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
uint8_t col = (uint8_t)it->a, base = col;
const uint8_t *sh = (f & (T_SHADOW | T_OUTLINE)) ? ce_shade_tab(12) : 0;
int badge = 0;
+ const int8_t *advt = adv_tab(f);
+ /* the size's rows (glyphs drawn by fast_glyph) */
+ const uint8_t *rtab, *rbits;
+ int gh = glyph_rows(f), digits = (f & (T_COND | T_LARGE)) == (T_COND | T_LARGE);
+ if (f & T_LARGE) rtab = ce_lut + LUT_ROWS_LARGE, rbits = ce_lut + LUT_ROWBITS_LARGE;
+ else if (f & T_MED) {
+ if (f & T_COND) rtab = ce_lut + LUT_ROWS_MED_COND, rbits = ce_lut + LUT_ROWBITS_MED_COND;
+ else rtab = ce_lut + LUT_ROWS_MED, rbits = ce_lut + LUT_ROWBITS_MED;
+ } else
+ rtab = ce_lut + LUT_ROWS_SMALL, rbits = ce_lut + LUT_ROWBITS_SMALL;
int align = (f & (T_CENTER | T_RIGHT)) && multi, total = it->w - 4;
if (align) {
int lw = textw(it->s, f, 1);
@@ -464,8 +633,7 @@ static void draw_text(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
/* a red badge behind the following text, until the next code */
const char *e = s + 1;
int bw = 0;
- const uint8_t *bb;
- while (*e && (unsigned char)*e >= 32) bw += advance(f, (unsigned char)*e, glyph(f, (unsigned char)*e, &bb)), e++;
+ for (; (unsigned char)*e >= 32; e++) bw += advt[(unsigned char)*e > 130 ? '?' : (unsigned char)*e];
bw = bw * sc + 3;
int by0 = y - 2, by1 = y + cap * sc + 2;
uint8_t red = ce_pal(HEXC(0xFE5F55));
@@ -483,11 +651,20 @@ static void draw_text(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
continue;
}
if (ch > (f & (T_LARGE | T_MED) ? 126 : 130)) ch = '?';
- const uint8_t *b;
- const glyph_t *g = glyph(f, ch, &b);
- int adv = advance(f, ch, g) * sc;
- if (x < cx1 && x + adv + sc > cx0 && y < rb && y + glyph_rows(f) * sc + sc > ra)
- put_glyph(g, b, f, drop_col(f, ch, g), x, y, col, y0, ra, rb, cx0, cx1, sh);
+ int adv = advt[ch] * sc;
+ if (x < cx1 && x + adv + sc > cx0 && y < rb && y + gh * sc + sc > ra) {
+ int dg = digits && ch >= '0' && ch <= '9';
+ const uint8_t *e = dg ? ce_lut + LUT_ROWS_LARGE_DIGITS + (ch - '0') * 4 : rtab + (ch - 32) * 4;
+ if (!e[2])
+ ; /* (a space) */
+ else if (sc == 1 && x >= cx0 && x + e[2] <= cx1 && !CE_SLOW)
+ fast_glyph(e, dg ? ce_lut + LUT_ROWBITS_LARGE_DIGITS : rbits, gh, x, y, col, y0, ra, rb, sh);
+ else {
+ const uint8_t *b;
+ const glyph_t *g = glyph(f, ch, &b);
+ put_glyph(g, b, f, drop_col(f, ch, g), x, y, col, y0, ra, rb, cx0, cx1, sh);
+ }
+ }
x += adv;
}
}
@@ -495,11 +672,11 @@ static void draw_text(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
/* ------------------------------------------------------------------ sprites */
/* editions: a level a 2 x 2 cell of the card, a frame every ED_DT_MS, each card a little behind the next */
static const uint8_t *ed_pattern(int ed, int x) {
- int f = (int)((g_time / ED_DT_MS + (unsigned)(x / 12)) % ED_FRAMES);
+ int f = (int)((ed_frame + (unsigned)(x / 12)) % ED_FRAMES);
return ce_edpat + ((ed - 1) * ED_FRAMES + f) * (ED_CH * ED_CW / 2);
}
-static uint8_t rowbuf[256];
+static uint8_t rowbuf[256] __attribute__((aligned(256))); /* (ce_scale_row looks up by the low byte) */
static void draw_sprite(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
int id = it->a;
@@ -516,11 +693,21 @@ static void draw_sprite(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
const uint8_t *edl = ed ? ce_lut + LUT_EDLUT + (ed - 1) * ED_LEVELS * CE_NSTATIC : 0;
if (fx & FX_SHADOW) tab = ce_shade_tab(11);
int plain = !(fx & ~FX_SHADOW);
- for (int y = ya; y < yb; y++) {
- int sy = same ? y - it->y : (int)((unsigned)(y - it->y) * sh / h);
- const uint8_t *row = spr + (spr[2 * sy] | spr[2 * sy + 1] << 8);
- uint8_t *d = strip + (y - y0) * SCR_W;
+ /* the source row: a step of sh / h in 1/256 */
+ unsigned ystep = same ? 256 : ((unsigned)sh << 8) / (unsigned)h, sy8 = (unsigned)(ya - it->y) * ystep;
+ uint8_t *d = strip + (ya - y0) * SCR_W;
+ for (int y = ya; y < yb; y++, sy8 += ystep, d += SCR_W) {
+ int sy = (int)(sy8 >> 8);
+ const uint8_t *rt = spr + sy + sy;
+ const uint8_t *row = spr + (rt[0] | (unsigned)rt[1] << 8);
if (same && plain) {
+ if (it->x >= xa && it->x + sw <= xb && sw < 256 && !CE_SLOW) {
+ /* (in eZ80 code: the whole sprite) */
+ static ce_rle_t b;
+ b.d = d + it->x, b.rt = rt, b.spr = spr, b.n = (uint8_t)(yb - y), b.sw = (uint8_t)sw, b.tab = tab;
+ ce_rle(&b);
+ return;
+ }
/* the runs straight in (or their shadow) */
int x = it->x, end = it->x + sw;
while (x < end) {
@@ -550,6 +737,10 @@ static void draw_sprite(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
}
const uint8_t *prow = pat ? pat + (sy >> 1 < ED_CH ? sy >> 1 : ED_CH - 1) * (ED_CW / 2) : 0;
unsigned step = ((unsigned)sw << 8) / (unsigned)w, sx8 = (unsigned)(xa - it->x) * step;
+ if (!(fx & ~FX_SHADOW) && xb - xa < 256 && !CE_SLOW) {
+ ce_scale_row(d + xa, rowbuf, (unsigned)(xb - xa), sx8, step, tab);
+ continue;
+ }
for (int x = xa; x < xb; x++, sx8 += step) {
int sx = same ? x - it->x : (int)(sx8 >> 8);
int v = rowbuf[sx];
@@ -564,7 +755,8 @@ static void draw_sprite(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
}
if (edl) {
int cx = sx >> 1 < ED_CW ? sx >> 1 : ED_CW - 1;
- int lv = (prow[cx >> 1] >> ((cx & 1) * 4)) & 15;
+ uint8_t pv = prow[cx >> 1];
+ int lv = cx & 1 ? pv >> 4 : pv & 15;
v = edl[lv * CE_NSTATIC + v];
}
if (grey) v = grey[v];
@@ -581,16 +773,13 @@ static void draw_rsq(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
int hs = it->r * 256 + 128, R = it->w / 2, cx = it->x + R, cy = it->y + R;
int ya = it->y > ra ? it->y : ra, yb = it->y + it->h < rb ? it->y + it->h : rb;
int xa = it->x > cx0 ? it->x : cx0, xb = it->x + it->w < cx1 ? it->x + it->w : cx1;
- for (int y = ya; y < yb; y++) {
- uint8_t *d = strip + (y - y0) * SCR_W;
- int dy = y - cy;
- for (int x = xa; x < xb; x++) {
- int dx = x - cx;
- int u = dx * co + dy * si, v = dy * co - dx * si;
- if (u < 0) u = -u;
- if (v < 0) v = -v;
- if (u <= hs && v <= hs) d[x] = (uint8_t)it->a;
- }
+ uint8_t *d = strip + (ya - y0) * SCR_W;
+ for (int y = ya; y < yb; y++, d += SCR_W) {
+ int dy = y - cy, dx = xa - cx;
+ /* u and v along the row: a step of co and -si a pixel (no multiplying per pixel) */
+ int u = dx * co + dy * si, v = dy * co - dx * si;
+ for (int x = xa; x < xb; x++, u += co, v -= si)
+ if ((u < 0 ? -u : u) <= hs && (v < 0 ? -v : v) <= hs) d[x] = (uint8_t)it->a;
}
}
@@ -600,33 +789,19 @@ static void draw_rrect(item_t *it, int y0, int ra, int rb, int cx0, int cx1, int
int xa = x > cx0 ? x : cx0, xb = x + w < cx1 ? x + w : cx1;
uint8_t c = (uint8_t)it->a;
const uint8_t *tab = shade ? ce_shade_tab(it->a) : 0;
- for (int yy = ya; yy < yb; yy++) {
- int j = yy - it->y, cy = -1;
- if (j < r) cy = j;
- else if (j >= h - r) cy = h - 1 - j;
- uint8_t *d = strip + (yy - y0) * SCR_W;
- int ma = xa, mb = xb;
- if (cy >= 0) {
- if (ma < x + r) ma = x + r;
- if (mb > x + w - r) mb = x + w - r;
- }
+ uint8_t *d = strip + ROW_OFF[ya - y0];
+ const uint8_t *cr = cut[r];
+ for (int j = ya - it->y, je = yb - it->y; j < je; j++, d += SCR_W) {
+ /* the row less its corners' cut */
+ int k = j < r ? cr[j] : j >= h - r ? cr[h - 1 - j] : 0, ma = x + k, mb = x + w - k;
+ if (ma < xa) ma = xa;
+ if (mb > xb) mb = xb;
if (ma < mb) {
if (shade)
ce_shade(d + ma, tab, (unsigned)(mb - ma));
else
memset(d + ma, c, (size_t)(mb - ma));
}
- if (cy < 0) continue;
- for (int side = 0; side < 2; side++) {
- int ea = side ? (mb > xa ? mb : xa) : xa, eb = side ? xb : (ma < xb ? ma : xb);
- for (int xx = ea; xx < eb; xx++) {
- int i = xx - x;
- int a = cov[r][cy][i < r ? i : w - 1 - i];
- if (!a) continue;
- if (a < 16) continue;
- d[xx] = shade ? tab[d[xx]] : c;
- }
- }
}
}
@@ -634,33 +809,30 @@ static void draw_rect(item_t *it, int y0, int ra, int rb, int cx0, int cx1) {
int ya = it->y > ra ? it->y : ra, yb = it->y + it->h < rb ? it->y + it->h : rb;
int xa = it->x > cx0 ? it->x : cx0, xb = it->x + it->w < cx1 ? it->x + it->w : cx1;
if (xa >= xb) return;
- for (int y = ya; y < yb; y++) memset(strip + (y - y0) * SCR_W + xa, it->a, (size_t)(xb - xa));
+ uint8_t *d = strip + (ya - y0) * SCR_W + xa;
+ for (int y = ya; y < yb; y++, d += SCR_W) memset(d, it->a, (size_t)(xb - xa));
}
/* ------------------------------------------------------------------ the frame */
-static unsigned mix(unsigned h, unsigned v) {
- h ^= v;
- return (h << 5) + h + (h >> 13);
-}
-
-static unsigned item_hash(const item_t *it) {
- unsigned h = mix(mix(mix((unsigned)(uint16_t)it->x, (unsigned)(uint16_t)it->y), (unsigned)it->w),
- (unsigned)it->h);
- h = mix(h, (unsigned)it->type | (unsigned)it->r << 8 | (unsigned)it->fx << 16);
- h = mix(h, (unsigned)it->a ^ (unsigned)it->b << 9);
- const int16_t *c = clips[it->clip];
- h = mix(h, (unsigned)(c[0] ^ c[1] << 9) ^ (unsigned)(c[2] << 3 ^ c[3] << 12));
- if (it->s)
- for (const char *p = it->s; *p; p++) h = mix(h, (unsigned char)*p);
- /* what moves on its own: the editions' sheen, the fire */
- if (it->type == I_SPRITE && (it->fx & FX_EDMASK)) h = mix(h, g_time / ED_DT_MS);
- if (it->type == I_FLAME) h = mix(h, (unsigned)ce_flame_frame());
- return h;
+static unsigned clip_hash[MAXCLIP];
+
+#ifdef NP_HOST
+/* (blit.S on the calculator) */
+void ce_hash_items(unsigned *hs, const item_t *it, unsigned n, const unsigned *ch) {
+ for (; n; n--, it++) {
+ uint8_t b[18];
+ memcpy(b, it, 16), b[16] = it->anim, b[17] = it->s0; /* (the calculator's layout) */
+ unsigned h = ce_hash_words(ch[it->clip], b, 6);
+ if (it->s) h = ce_hash_str(h, it->s);
+ for (int s = it->s0; s <= it->s1; s++) hs[s] = (hs[s] * 33 + h) & 0xFFFFFF;
+ }
}
+#endif
#ifdef NP_HOST
/* how full the frame's lists get (tools/host.c prints it) */
int ce_max_items, ce_max_str;
+long ce_strips_drawn, ce_pal_misses;
#endif
void g_end(void) {
@@ -668,38 +840,50 @@ void g_end(void) {
if (nitems > ce_max_items) ce_max_items = nitems;
if (strpos > ce_max_str) ce_max_str = strpos;
#endif
- unsigned hs[NSTRIP];
- unsigned base = mix(mix((unsigned)bg_swirl, (unsigned)bg_cover_x), (unsigned)bg_solid ^ (unsigned)ce_bg_ver << 8);
- for (int s = 0; s < NSTRIP; s++) hs[s] = base + (unsigned)s;
- for (int i = 0; i < nitems; i++) {
- const item_t *it = &items[i];
- int s0 = it->y < 0 ? 0 : it->y / SH, s1 = (it->y + it->h - 1) / SH;
- if (s1 >= NSTRIP) s1 = NSTRIP - 1;
- if (s0 > s1) continue;
- unsigned h = item_hash(it) + (unsigned)i;
- for (int s = s0; s <= s1; s++) hs[s] = mix(hs[s], h);
- }
- for (int s = 0; s < NSTRIP; s++) {
+ PROF(1); /* (the frame's list) */
+#ifdef CE_PROF
+ ce_prof[5]++;
+#endif
+ unsigned hs[NSTRIP], base[3] = {(unsigned)bg_swirl, (unsigned)bg_cover_x, bg_solid + (unsigned)ce_bg_ver * 256};
+#ifdef NP_HOST
+ for (int i = 0; i < 3; i++) base[i] &= 0xFFFFFF;
+#endif
+ 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);
+ PROF(2); /* (the hashes) */
+ uint8_t *d = strip;
+ for (int s = 0, y0 = 0; s < NSTRIP; s++, y0 += SH) {
if (drawn_once && hs[s] == strip_hash[s]) continue;
+#ifdef CE_PROF
+ ce_prof[4]++;
+#endif
strip_hash[s] = hs[s];
- int y0 = s * SH, y1 = y0 + SH;
+#ifdef NP_HOST
+ ce_strips_drawn++;
+ if (getenv("NP_STRIPS")) fprintf(stderr, "%u: strip %d\n", (unsigned)g_time, s);
+#endif
+ int y1 = y0 + SH;
uint16_t dyn = 0;
if (bg_swirl) {
- for (int y = y0; y < y1; y++) {
- uint8_t *d = strip + (y - y0) * SCR_W;
+ d = strip;
+ for (int y = y0; y < y1; y++, d += SCR_W) {
if (bg_cover_x > 0) memset(d, bg_solid, (size_t)(bg_cover_x < SCR_W ? bg_cover_x : SCR_W));
if (bg_cover_x < SCR_W) ce_bg_row(d, y, bg_cover_x);
}
} else
memset(strip, bg_solid, sizeof strip);
+ PROF(13);
for (int i = 0; i < nitems; i++) {
item_t *it = &items[i];
- if (it->y >= y1 || it->y + it->h <= y0) continue;
+ if (s < it->s0 || s > it->s1) continue;
const int16_t *c = clips[it->clip];
int cy0 = y0 > c[1] ? y0 : c[1], cy1 = y1 < c[3] ? y1 : c[3];
if (cy0 >= cy1) continue;
if (it->type != I_SPRITE && it->type != I_SHADE && it->type != I_FLAME && it->a >= CE_DYN0 && it->a < CE_SWIRL0)
- dyn |= (uint16_t)(1u << (it->a - CE_DYN0));
+ dyn |= BIT16[it->a - CE_DYN0];
+ PROF(15);
switch (it->type) {
case I_RECT: draw_rect(it, y0, cy0, cy1, c[0], c[2]); break;
case I_RRECT: draw_rrect(it, y0, cy0, cy1, c[0], c[2], 0); break;
@@ -709,9 +893,13 @@ void g_end(void) {
case I_RSQ: draw_rsq(it, y0, cy0, cy1, c[0], c[2]); break;
case I_FLAME: ce_flame_rows(it->a & 1, it->x, it->y, it->w, it->h, it->b, strip, y0, cy0, cy1, c[0], c[2]); break;
}
+ PROF(6 + it->type);
}
strip_dyn[s] = dyn;
+ PROF(15);
memcpy(ce_vram + y0 * SCR_W, strip, sizeof strip);
+ PROF(14);
}
drawn_once = 1;
+ PROF(3); /* (the strips drawn: what the items left) */
}
diff --git a/games/balatro/ce/tools/cekeys.py b/games/balatro/ce/tools/cekeys.py
index 7131f4d8..09b811e1 100644
--- a/games/balatro/ce/tools/cekeys.py
+++ b/games/balatro/ce/tools/cekeys.py
@@ -1,7 +1,8 @@
#!/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), and its main() renamed for src/ce_main.c to call.
+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"""
@@ -23,6 +24,9 @@ SWAPS = [
('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) {"),
]
diff --git a/games/balatro/ce/tools/host.c b/games/balatro/ce/tools/host.c
index 9d68e128..e09097da 100644
--- a/games/balatro/ce/tools/host.c
+++ b/games/balatro/ce/tools/host.c
@@ -386,8 +386,10 @@ int main(void) {
ce_set_palette(0, CE_NSTATIC, ce_palette);
balatro_main();
extern int ce_max_items, ce_max_str;
- fprintf(stderr, "host: %ld frames in %llu ms (at most %d items, %d bytes of text a frame)\n", frames,
- (unsigned long long)eadk_timing_millis(), ce_max_items, ce_max_str);
+ extern long ce_strips_drawn, ce_adv_made, ce_pal_misses;
+ fprintf(stderr, "host: %ld frames in %llu ms (at most %d items, %d bytes of text a frame), %ld strips drawn, "
+ "%ld advance tables made, %ld colours looked up\n", frames, (unsigned long long)eadk_timing_millis(),
+ ce_max_items, ce_max_str, ce_strips_drawn, ce_adv_made, ce_pal_misses);
if (getenv("NP_BOT"))
fprintf(stderr, "bot: ante %d reached, %d wins, round %d, best hand %.0f, money %ld\n", bot_ante_reached, bot_wins,
R.round, R.best_hand, (long)R.money);
diff --git a/games/balatro/ce/tools/mkart.py b/games/balatro/ce/tools/mkart.py
index 630d6457..8a0643ca 100644
--- a/games/balatro/ce/tools/mkart.py
+++ b/games/balatro/ce/tools/mkart.py
@@ -85,6 +85,44 @@ def rle_rows(rows, w):
return bytes(out + body)
+GLYPH_H = {"font_small": 9, "font_med": 12, "font_large": 14, "font_large_digits": 14}
+
+
+def drop_col(ch, w):
+ """the column the condensed medium size leaves out (src/gfx.c), or None"""
+ if ch in "TI1":
+ return 0
+ if ch in "ft":
+ return 4
+ return {5: 3, 6: 2, 7: 3, 8: 4}.get(w)
+
+
+def glyph_rows(fonts):
+ """The fonts row by row for src/gfx_ce.c (its eZ80 code draws a row from a byte, the left column its top bit):
+ per size a table of 4 bytes a glyph (where its rows start, its width), then the rows; a glyph past 8 columns has
+ its ninth column's rows after. The condensed medium size has a table and rows of its own."""
+ out = []
+ sizes = [(n, n) for n in fonts] + [("font_med_cond", "font_med")]
+ for name, src in sizes:
+ glyphs, bits = fonts[src]
+ gh = GLYPH_H[src]
+ tab, rows = bytearray(), bytearray()
+ for k, (o, w, _a) in enumerate(glyphs):
+ cols = [[(bits[(o + x * gh + y) >> 3] >> ((o + x * gh + y) & 7)) & 1 for y in range(gh)] for x in range(w)]
+ if name == "font_med_cond":
+ dc = drop_col(chr(32 + k), w)
+ if dc is not None and dc < w:
+ cols = cols[:dc] + cols[dc + 1:]
+ if len(cols) > 9:
+ raise SystemExit(f"mkart: a glyph of {name} is wider than 9")
+ tab += bytes((len(rows) & 255, len(rows) >> 8, len(cols), 0))
+ for part in (cols[:8], cols[8:]):
+ if part:
+ rows += bytes(sum(c[y] << (7 - x) for x, c in enumerate(part)) for y in range(gh))
+ out += [("rows_" + name[5:], tab), ("rowbits_" + name[5:], rows)]
+ return out
+
+
def zx0(data, convbin, tmp):
src, dst = os.path.join(tmp, "zx.in"), os.path.join(tmp, "zx.out")
open(src, "wb").write(data)
@@ -141,7 +179,7 @@ BAYER = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]]
def field(mode, x0, w, h, cell):
"""The paint as src/bg.c draws it: worked out on a grid every `cell` pixels (each point the mean of 4 samples a
pixel around it, as bg.c's passes average them), interpolated between the points for every pixel, then as levels
- 0..15 (ordered dither), two pixels a byte (the left one in the low half)."""
+ 0..15 (ordered dither), as their palette entries (the background's from SWIRL0)."""
gw, gh = (w + cell - 1) // cell + 2, (h + cell - 1) // cell + 2
t = 20.0 if mode else 36.0
jit = ((-1, -1), (1, 1), (1, -1), (-1, 1))
@@ -155,10 +193,7 @@ def field(mode, x0, w, h, cell):
v = (grid[gy][gx] * (1 - fx) + grid[gy][gx + 1] * fx) * (1 - fy) + \
(grid[gy + 1][gx] * (1 - fx) + grid[gy + 1][gx + 1] * fx) * fy
lv = max(0, min(LEVELS - 1, int((v * (LEVELS - 1) * 16 / 255 + BAYER[y & 3][x & 3]) // 16)))
- if x & 1:
- out[-1] |= lv << 4
- else:
- out.append(lv)
+ out.append(SWIRL0 + lv)
return bytes(out)
@@ -495,6 +530,7 @@ def main():
for o, w, a in glyphs:
tab += bytes((o & 255, o >> 8, w, a))
fonts += [(name, tab), (name + "_bits", bits)]
+ fonts += glyph_rows(art.fonts)
parts = {}
for name, blob in (("shade", shade), ("grey", grey), ("edlut", edl), ("fcol", fcols), ("text", texts)) + tuple(fonts):
if len(lut) & 1:
@@ -507,7 +543,8 @@ def main():
blobs = dict(vars_)
blobs["BALLUT"] = lut
blobs["BALBG0"] = bg_run
- blobs["BALBG1"] = bg_title
+ blobs["BALBG1"] = bg_title[:len(bg_title) // 2] # (the title's: in two)
+ blobs["BALBG2"] = bg_title[len(bg_title) // 2:]
blobs["BALFIRE"] = fgrids
blobs["BALED"] = edp
if max(len(b) for b in blobs.values()) > MAXVAR:
diff --git a/games/balatro/ce/tools/place.py b/games/balatro/ce/tools/place.py
index 49ff4593..f07007e9 100644
--- a/games/balatro/ce/tools/place.py
+++ b/games/balatro/ce/tools/place.py
@@ -24,8 +24,9 @@ def main():
bss += size
elif name.startswith((".text", ".rodata")) and size and not KEEP.match(name):
secs.append((size, name))
- bss_size = (bss + 64 + 255) & ~255
- bins = [["vram", VRAM, []], ["pshadow", PSHADOW, []], ["bssx", BSS_HIGH - BSS_LOW - bss_size, []]]
+ bss_size = (bss + 64 + 511 + 255) & ~255 # (and the 256-byte aligned tables' padding)
+ # (a few bytes kept free in each: the alignment of some sections)
+ bins = [["vram", VRAM - 32, []], ["pshadow", PSHADOW - 32, []], ["bssx", BSS_HIGH - BSS_LOW - bss_size - 32, []]]
left = 0
for size, name in sorted(secs, reverse=True):
for b in bins: