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.

Celeste: the CE version's floats in assembly

Mason3635d317dad3456
diff --git a/games/celeste/ce/host/plat_host.c b/games/celeste/ce/host/plat_host.c
index e6c50a9f..c211ade4 100644
--- a/games/celeste/ce/host/plat_host.c
+++ b/games/celeste/ce/host/plat_host.c
@@ -125,6 +125,7 @@ void plat_begin_draw(void) {}
uint8_t *plat_ram(unsigned n) { return calloc(1, n); }
void plat_palette(const uint16_t *p, int first, int n) { memcpy(pal + first, p, (size_t)n * 2); }
+void plat_poll(void) {}
unsigned plat_keys(void) {
updates++;
if (getenv("CEL_DEBUG") && updates == 2 && g_level.room) {
diff --git a/games/celeste/ce/src/ce.h b/games/celeste/ce/src/ce.h
index 10c44eff..b2e56334 100644
--- a/games/celeste/ce/src/ce.h
+++ b/games/celeste/ce/src/ce.h
@@ -39,6 +39,7 @@ void plat_frame(void); /* shows g_fb, and g_fb becomes the other
void plat_begin_draw(void); /* waits until the buffer to draw is no longer shown */
void plat_palette(const uint16_t *pal, int first, int n); /* 1555 colours */
unsigned plat_keys(void);
+void plat_poll(void); /* (a look at the keys between plat_keys: drawing calls it) */
unsigned plat_ticks(void); /* a 32768 Hz clock (wraps at 16 bits) */
void plat_wait(unsigned ticks);
const uint8_t *plat_appvar(const char *name); /* an appvar's data in place, NULL if missing */
@@ -70,6 +71,7 @@ static inline const uint8_t *far_ptr(const uint8_t *const *parts, const uint8_t
bool data_open(void); /* the common appvars; false if missing */
bool chapter_open(int n); /* chapter n's appvars and colours */
void palette_apply(int fade); /* the chapter's colours (fade 0..256: to black) */
+void palette_tint(bool on); /* the chapter's tint on them (playing) or not (menus): then palette_apply */
void palette_hair(uint32_t rgb); /* Madeline's hair colour */
const uint8_t *sprite(int id); /* a common sprite */
const uint8_t *csprite(int id); /* a chapter sprite */
@@ -232,6 +234,8 @@ static inline int e_top(const Ent *e) { return e->y + e->hy; }
static inline int e_right(const Ent *e) { return e->x + e->hx + e->hw; }
static inline int e_bottom(const Ent *e) { return e->y + e->hy + e->hh; }
Ent *ent_new(int type, int x, int y);
+const uint8_t *ents_kind(int flag); /* EF_SOLID or EF_JUMPTHRU ones: a count, their indices */
+void ents_kinds_stale(void); /* (their flags changed: made again) */
void ent_free(Ent *e);
void ents_spawn_room(int slot);
void ents_update(void);
@@ -254,6 +258,7 @@ typedef struct {
int gx, gy, tw, th; /* its grid: the top left cell's world pixel, cells (with MARGIN) */
uint8_t *grid; /* fg[tw * th], bg[tw * th], solid bits (rb a row) */
int rb;
+ uint8_t *solid; /* (its solid bits: grid + 2 tw th) */
TEnt *tab; /* its tiles, by its own numbers (256) */
const uint8_t *decals, *ents, *trigs, *bg;
int bgx, bgy;
diff --git a/games/celeste/ce/src/data.c b/games/celeste/ce/src/data.c
index e1945dc1..bef67982 100644
--- a/games/celeste/ce/src/data.c
+++ b/games/celeste/ce/src/data.c
@@ -78,9 +78,10 @@ const uint8_t *player_meta(int id) { return meta + 2 + 3 * id; }
const uint16_t *hair_mask(int which) { return (const uint16_t *)(const void *)(hair + 40 * which); }
/* 1555 colours, plus the chapter's tint (its additive sunset), faded to black by fade (256: none) */
+static bool tinted; /* (the level's colours: the menus' are not) */
static uint16_t shade(uint16_t c, int fade) {
int r = (c >> 10 & 31) << 3, g = (c >> 5 & 31) << 3, b = (c & 31) << 3;
- r = imin(255, r + tint[0]), g = imin(255, g + tint[1]), b = imin(255, b + tint[2]);
+ if (tinted) r = imin(255, r + tint[0]), g = imin(255, g + tint[1]), b = imin(255, b + tint[2]);
r = r * fade >> 8, g = g * fade >> 8, b = b * fade >> 8;
return (uint16_t)((r >> 3) << 10 | (g >> 3) << 5 | (b >> 3));
}
@@ -99,6 +100,7 @@ uint8_t pal_nearest(uint32_t rgb) {
static uint32_t cur_hair = 0xAC3232;
static bool hair_set;
uint8_t g_palette_gen = 1; /* (changes with the palette: src/text.c finds its shades again) */
+void palette_tint(bool on) { tinted = on; }
void palette_apply(int fade) {
if (++g_palette_gen == 0) g_palette_gen = 1;
uint16_t out[256];
diff --git a/games/celeste/ce/src/draw.c b/games/celeste/ce/src/draw.c
index cf3bc6ca..689ebe07 100644
--- a/games/celeste/ce/src/draw.c
+++ b/games/celeste/ce/src/draw.c
@@ -453,7 +453,46 @@ void draw_text(const char *s, int x, int y, uint8_t c) {
#ifdef CEL_BENCH
/* the assembly against the C (tests/bench.sh): 0 when they agree */
+unsigned long g_float_cost[4]; /* cycles: f_add, f_mul, f_lt, tiles_solid */
+static void float_cost(void) {
+ volatile f32 x = 0x3F8CCCCDul, y = 0x40133333ul, z = 0;
+ unsigned long t = plat_cycles();
+ for (int i = 0; i < 100; i++) z = f_add(x, y), x = z ^ 0x80000000ul;
+ g_float_cost[0] = ((plat_cycles() - t) & 0xFFFFFF) / 100, t = plat_cycles();
+ for (int i = 0; i < 100; i++) z = f_mul(x, y);
+ g_float_cost[1] = ((plat_cycles() - t) & 0xFFFFFF) / 100, t = plat_cycles();
+ for (int i = 0; i < 100; i++) z += f_lt(x, y);
+ g_float_cost[2] = ((plat_cycles() - t) & 0xFFFFFF) / 100, t = plat_cycles();
+ for (int i = 0; i < 100; i++) z += solid_at(i, 50, i + 8, 61, NULL);
+ g_float_cost[3] = ((plat_cycles() - t) & 0xFFFFFF) / 100;
+}
+extern f32 ref_add(f32 a, f32 b), ref_mul(f32 a, f32 b);
+extern bool ref_lt(f32 a, f32 b), ref_le(f32 a, f32 b);
+static uint32_t st_seed = 12345;
+static uint32_t st_rand(void) {
+ st_seed ^= st_seed << 13, st_seed ^= st_seed >> 17, st_seed ^= st_seed << 5;
+ return st_seed;
+}
+static f32 st_float(int k) { /* (zeros, a denormal, near 1, far apart, and any) */
+ uint32_t r = st_rand(), m = st_rand() & 0x7FFFFF, sign = r & 0x80000000ul;
+ switch (k % 8) {
+ case 0: return sign;
+ case 1: return sign | (st_rand() & 0x7FFFFF);
+ case 2: case 3: return sign | (uint32_t)(120 + (r >> 8) % 16) << 23 | m;
+ case 4: return sign | (uint32_t)(1 + (r >> 8) % 253) << 23 | m;
+ default: return sign | (uint32_t)(126 + (r >> 8) % 4) << 23 | (k & 1 ? m : m & 0x7FFF00);
+ }
+}
+int g_sf_bad;
int draw_selftest(void) {
+ float_cost();
+ for (int i = 0; i < 600; i++) {
+ f32 a = st_float(st_rand() % 8 == 0 ? 0 : (int)(st_rand() % 8)), b = st_float((int)(st_rand() % 8));
+ if (i % 5 == 0) b = (a ^ 0x80000000ul) + (st_rand() % 5) - 2; /* (cancelling) */
+ if (f_add(a, b) != ref_add(a, b)) g_sf_bad |= 1;
+ if (f_mul(a, b) != ref_mul(a, b)) g_sf_bad |= 2;
+ if (f_lt(a, b) != ref_lt(a, b) || f_le(a, b) != ref_le(a, b)) g_sf_bad |= 4;
+ }
int bad = 0;
for (int v = 0; v < (1 << 23); v += 99991)
if (isqrt24(v) != isqrt(v)) bad |= 1;
diff --git a/games/celeste/ce/src/ents.c b/games/celeste/ce/src/ents.c
index 36b0474c..21340f4b 100644
--- a/games/celeste/ce/src/ents.c
+++ b/games/celeste/ce/src/ents.c
@@ -19,6 +19,7 @@ static int rec_flags(const uint8_t *r) { return r[13 + 4 * r[12]]; }
static int rec_nvals(const uint8_t *r) { return r[14 + 4 * r[12]]; }
static int rec_val(const uint8_t *r, int i) { return (int)R16(r + 15 + 4 * r[12] + 2 * i); }
+static bool kinds_stale = true;
Ent *ent_new(int type, int x, int y) {
Ent *e = NULL;
for (int i = 0; i < g_nents; i++)
@@ -28,13 +29,32 @@ Ent *ent_new(int type, int x, int y) {
e = &g_ents[g_nents++];
}
memset(e, 0, sizeof *e);
+ kinds_stale = true;
e->type = (uint8_t)type;
e->flags = EF_ACTIVE | EF_VISIBLE | EF_COLLIDABLE;
e->room = (uint8_t)g_level.slot;
e->x = x, e->y = y;
return e;
}
+/* the entities of a kind (EF_SOLID, EF_JUMPTHRU...: a flag they are made with), in order: a count, then their
+ * indices; made again after entities come or go */
+static uint8_t kinds[2][1 + MAX_ENTS];
+const uint8_t *ents_kind(int flag) {
+ if (kinds_stale) {
+ kinds[0][0] = kinds[1][0] = 0;
+ for (int i = 0; i < g_nents; i++) {
+ const Ent *e = &g_ents[i];
+ if (e->type == 255) continue;
+ if (e->flags & EF_SOLID) kinds[0][++kinds[0][0]] = (uint8_t)i;
+ if (e->flags & EF_JUMPTHRU) kinds[1][++kinds[1][0]] = (uint8_t)i;
+ }
+ kinds_stale = false;
+ }
+ return kinds[flag == EF_SOLID ? 0 : 1];
+}
+void ents_kinds_stale(void) { kinds_stale = true; }
void ent_free(Ent *e) {
+ kinds_stale = true;
for (int k = 0; k < 2; k++) /* what it drew goes */
if (e->bb[k][0] < e->bb[k][2]) draw_gone_buf(k, e->bb[k][0], e->bb[k][1], e->bb[k][2], e->bb[k][3]);
e->type = 255;
@@ -672,9 +692,13 @@ static void zip_render(Ent *e) {
}
/* ZipMoverPathRenderer (depth 5000): the two ropes with their teeth moving along them, a cog at each end turning a
* full turn as it goes; all once more in black a pixel lower first, and a black frame round the block after */
-static void zippath_lines(Ent *z, int fx, int fy, int tx, int ty, int off, bool black) {
- int dx = tx - fx, dy = ty - fy, len = isqrt24(dx * dx + dy * dy);
- int ux = len ? dx * 256 / len : 0, uy = len ? dy * 256 / len : 0; /* (1/256 px) */
+static void zippath_lines(Ent *path, Ent *z, int fx, int fy, int tx, int ty, int off, bool black) {
+ if (!path->t2) { /* (its length and direction, once) */
+ int dx = tx - fx, dy = ty - fy;
+ path->t2 = isqrt24(dx * dx + dy * dy) + 1;
+ path->t0 = path->t2 > 1 ? dx * 256 / (path->t2 - 1) : 0, path->t1 = path->t2 > 1 ? dy * 256 / (path->t2 - 1) : 0;
+ }
+ int len = path->t2 - 1, ux = path->t0, uy = path->t1; /* (1/256 px) */
int px = -uy, py = ux; /* Perpendicular */
int ax = fx * 256 + px * 3, ay = (fy + off) * 256 + py * 3, bx = fx * 256 - px * 4, by = (fy + off) * 256 - py * 4;
int ex = (tx - fx) * 256, ey = (ty - fy) * 256;
@@ -702,8 +726,8 @@ static void zippath_render(Ent *e) {
int w = z->hw, h = z->hh;
int fx = rm->x + REC_X(z->rec) + w / 2, fy = rm->y + REC_Y(z->rec) + h / 2;
int tx = rm->x + S16(n) + w / 2, ty = rm->y + S16(n + 2) + h / 2;
- zippath_lines(z, fx, fy, tx, ty, 1, true);
- zippath_lines(z, fx, fy, tx, ty, 0, false);
+ zippath_lines(e, z, fx, fy, tx, ty, 1, true);
+ zippath_lines(e, z, fx, fy, tx, ty, 0, false);
draw_rect(z->x + z->a2 - 1, z->y + z->a3 - 1, w + 2, h + 2, BLACK);
}
diff --git a/games/celeste/ce/src/game.c b/games/celeste/ce/src/game.c
index d09cebf3..f50d982e 100644
--- a/games/celeste/ce/src/game.c
+++ b/games/celeste/ce/src/game.c
@@ -44,8 +44,9 @@ void save_add_berry(int ch, int index, bool golden) {
else if (index >= 0 && index < 64) save.ch[ch].berries[index >> 3] |= (uint8_t)(1 << (index & 7));
}
static int berries_of(int ch) {
+ static const uint8_t ONES[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
int n = 0;
- for (int i = 0; i < 64; i++) n += save_has_berry(ch, i);
+ for (int i = 0; i < 8; i++) n += ONES[save.ch[ch].berries[i] & 15] + ONES[save.ch[ch].berries[i] >> 4];
return n;
}
void save_add_death(void) {
@@ -108,7 +109,7 @@ static void hud_reset(void);
static void play_chapter(int ch) {
if (!chapter_open(ch)) return;
chapter = ch;
- palette_apply(256);
+ palette_tint(true), palette_apply(256);
g_berries_now = 0;
hud_reset();
level_load(ch, -1, INTRO_JUMP);
@@ -163,7 +164,10 @@ static void hud_draw(void) {
/* ---------------------------------------------------------------- the frame */
static bool ok_pressed(void) { return btn_pressed(&g_in.ok) || btn_pressed(&g_in.jump); }
-static void go(int sc) { screen = sc, sel = 0, screen_t = 0, full_redraw = 2; }
+static void go(int sc) {
+ if (sc != SC_PLAY) palette_tint(false), palette_apply(256); /* (the menus untinted) */
+ screen = sc, sel = 0, screen_t = 0, full_redraw = 2;
+}
void game_frame(void) {
input_update(plat_keys());
if (g_in.keys & K_QUIT) {
@@ -247,8 +251,17 @@ static void fade_view(void) {
}
void game_draw(void) {
static int paused; /* (frames drawn paused: the black goes into both buffers) */
- if (full_redraw) draw_clear_screen(0), full_redraw--, level_render_forget();
+ static int shown[2] = {-1, -1}, buf; /* the menu each buffer has (what it shows, the button's blink), drawn again
+ * only when that changes */
+ buf ^= 1;
+ if (full_redraw) draw_clear_screen(0), full_redraw--, level_render_forget(), shown[0] = shown[1] = -1;
if (screen != SC_PAUSE) paused = 0;
+ if (screen != SC_PLAY && screen != SC_PAUSE) {
+ int now = screen * 4 + (screen_t % 60 < 45) * 2 + (screen_t > 60);
+ if (shown[buf] == now) return;
+ shown[buf] = now;
+ } else
+ shown[0] = shown[1] = -1;
switch (screen) {
case SC_KEYS: { /* NumPlay's keys on the CE (platform/ti84ce/README.md), as the NumWorks version has them */
static const char *const keys[][2] = {{"arrows", "Move"}, {"2nd", "Jump"}, {"clear", "Dash"}, {"math", "Grab"},
@@ -335,7 +348,19 @@ void game_draw(void) {
*q = 0;
draw_text(buf, 2, 4, WHITE);
if (q - buf > 52) draw_text(buf + 52, 2, 14, WHITE);
- draw_text(selftest ? "BAD" : "ok", 290, 14, WHITE);
+ {
+ extern int g_sf_bad;
+ draw_text(selftest || g_sf_bad ? "BAD" : "ok", 290, 14, WHITE);
+ char sb[4] = {(char)('0' + g_sf_bad), 0};
+ draw_text(sb, 280, 14, WHITE);
+ }
+ {
+ extern unsigned long g_float_cost[4];
+ char fb[40], *w = fb;
+ for (int k = 0; k < 4; k++) num(w, (uint32_t)g_float_cost[k]), w += strlen(w), *w++ = ' ';
+ *w = 0;
+ draw_text(fb, 2, 22, WHITE);
+ }
}
#endif
break;
@@ -346,7 +371,7 @@ void game_draw(void) {
void game_start_room(int ch, const char *room, int x, int y) {
if (!chapter_open(ch)) return;
chapter = ch;
- palette_apply(256);
+ palette_tint(true), palette_apply(256);
level_load(ch, level_room_find(room), INTRO_NONE);
g_level.wipe = 0;
if (x || y) {
diff --git a/games/celeste/ce/src/level.c b/games/celeste/ce/src/level.c
index e8ba5e2a..f0b12496 100644
--- a/games/celeste/ce/src/level.c
+++ b/games/celeste/ce/src/level.c
@@ -102,6 +102,7 @@ static bool room_load(int slot, int index) {
int raw = (int)R16(p);
if (raw > GRID_BYTES) return false;
rm->grid = grid_mem[slot];
+ rm->solid = rm->grid + 2 * rm->tw * rm->th;
plat_unpack(rm->grid, HPTR(p + 2));
rm->decals = HPTR(p + 5);
rm->ents = HPTR(p + 8);
@@ -114,30 +115,36 @@ static bool room_load(int slot, int index) {
}
/* ---------------------------------------------------------------- collisions with the tiles */
-static bool cell_solid(int cx, int cy) {
- for (int k = 0; k < 2; k++) {
- const Room *rm = &g_level.rooms[k ? g_level.slot ^ 1 : g_level.slot];
- if (rm->index < 0) continue;
- int x = cx - (rm->gx >> 3), y = cy - (rm->gy >> 3);
- if ((unsigned)x >= (unsigned)rm->tw || (unsigned)y >= (unsigned)rm->th) continue;
- return rm->grid[2 * rm->tw * rm->th + y * rm->rb + (x >> 3)] >> (x & 7) & 1;
- }
- return false;
-}
-/* Grid.Collide: [l, r) x [t, b) in pixels */
+/* Grid.Collide: [l, r) x [t, b) in pixels; a cell is the current room's if it has it, else the other's. A row's
+ * bytes worked out once a row, a cell's bit from a table (no shifts by a number) */
+static const uint8_t BIT[8] = {1, 2, 4, 8, 16, 32, 64, 128};
bool tiles_solid(int l, int t, int r, int b) {
if (r <= l || b <= t) return false;
int cx0 = l >> 3, cy0 = t >> 3, cx1 = (r - 1) >> 3, cy1 = (b - 1) >> 3;
- for (int cy = cy0; cy <= cy1; cy++)
- for (int cx = cx0; cx <= cx1; cx++)
- if (cell_solid(cx, cy)) return true;
+ const Room *cur = &g_level.rooms[g_level.slot], *oth = &g_level.rooms[g_level.slot ^ 1];
+ int cgx = cur->gx >> 3, cgy = cur->gy >> 3, ogx = oth->gx >> 3, ogy = oth->gy >> 3;
+ for (int cy = cy0; cy <= cy1; cy++) {
+ int cyr = cy - cgy, oyr = cy - ogy;
+ const uint8_t *crow = cur->index >= 0 && (unsigned)cyr < (unsigned)cur->th ? cur->solid + cyr * cur->rb : NULL;
+ const uint8_t *orow = oth->index >= 0 && (unsigned)oyr < (unsigned)oth->th ? oth->solid + oyr * oth->rb : NULL;
+ if (!crow && !orow) continue;
+ for (int cx = cx0; cx <= cx1; cx++) {
+ int x = cx - cgx;
+ if (crow && (unsigned)x < (unsigned)cur->tw) {
+ if (crow[x >> 3] & BIT[x & 7]) return true;
+ continue;
+ }
+ x = cx - ogx;
+ if (orow && (unsigned)x < (unsigned)oth->tw && (orow[x >> 3] & BIT[x & 7])) return true;
+ }
+ }
return false;
}
Ent *solid_first(int l, int t, int r, int b, const Ent *self) {
- for (int i = 0; i < g_nents; i++) {
- Ent *o = &g_ents[i];
- if (o != self && (o->flags & (EF_SOLID | EF_COLLIDABLE)) == (EF_SOLID | EF_COLLIDABLE) && o->type != 255 &&
- rect_hits(l, t, r, b, o))
+ const uint8_t *k = ents_kind(EF_SOLID); /* (the solid ones, in order) */
+ for (int n = *k++; n; n--) {
+ Ent *o = &g_ents[*k++];
+ if (o != self && (o->flags & (EF_SOLID | EF_COLLIDABLE)) == (EF_SOLID | EF_COLLIDABLE) && rect_hits(l, t, r, b, o))
return o;
}
return NULL;
@@ -151,8 +158,9 @@ Ent *solid_hit(int l, int t, int r, int b, const Ent *self) {
}
/* CollideFirstOutside<JumpThru>: one e touches at (x, y) and not where it is */
Ent *jumpthru_first_outside(const Ent *e, int x, int y) {
- for (int i = 0; i < g_nents; i++) {
- Ent *o = &g_ents[i];
+ const uint8_t *k = ents_kind(EF_JUMPTHRU);
+ for (int n = *k++; n; n--) {
+ Ent *o = &g_ents[*k++];
if ((o->flags & (EF_JUMPTHRU | EF_COLLIDABLE)) == (EF_JUMPTHRU | EF_COLLIDABLE) && o != e &&
ent_collide_at(e, x, y, o) && !ent_collide_at(e, e->x, e->y, o))
return o;
@@ -403,12 +411,14 @@ static void render_cells(int ca, int cb, int ra, int rb) {
g_clip_x0 = imax(ca * 8 - g_offx, 0), g_clip_x1 = imin(cb * 8 + 8 - g_offx, VIEW_W);
g_clip_y0 = imax(ra * 8 - g_offy, 0), g_clip_y1 = imin(rb * 8 + 8 - g_offy, VIEW_H);
draw_layers_back(ca, cb, ra, rb);
+ plat_poll();
BENCH(4);
draw_decals(0);
BENCH(10);
ents_render(32767, -10000);
BENCH(5);
draw_layer_fg(ca, cb, ra, rb);
+ plat_poll();
BENCH(6);
draw_decals(1);
BENCH(10);
diff --git a/games/celeste/ce/src/main.c b/games/celeste/ce/src/main.c
index d8f91998..b1afad23 100644
--- a/games/celeste/ce/src/main.c
+++ b/games/celeste/ce/src/main.c
@@ -50,6 +50,7 @@ int main(void) {
plat_begin_draw();
BENCH(1);
game_draw();
+ plat_poll();
plat_frame();
BENCH(8);
g_perf_frames++;
diff --git a/games/celeste/ce/src/plat_ce.c b/games/celeste/ce/src/plat_ce.c
index 93bccb48..cbb1d5ba 100644
--- a/games/celeste/ce/src/plat_ce.c
+++ b/games/celeste/ce/src/plat_ce.c
@@ -69,9 +69,17 @@ __attribute__((noinline)) void plat_frame(void) { /* (not inlined: the emulato
void plat_begin_draw(void) { gfx_Wait(); }
void plat_palette(const uint16_t *pal, int first, int n) { memcpy(gfx_palette + first, pal, (size_t)n * 2); }
+/* the keys down at any of the looks since the last plat_keys (plat_poll while drawing: a quick press during a long
+ * frame still counts) */
+static uint8_t seen[4];
+void plat_poll(void) {
+ seen[0] |= kb_Data[1], seen[1] |= kb_Data[2], seen[2] |= kb_Data[6], seen[3] |= kb_Data[7];
+}
unsigned plat_keys(void) {
unsigned k = 0;
- uint8_t g1 = kb_Data[1], g2 = kb_Data[2], g6 = kb_Data[6], g7 = kb_Data[7];
+ plat_poll();
+ uint8_t g1 = seen[0], g2 = seen[1], g6 = seen[2], g7 = seen[3];
+ seen[0] = seen[1] = seen[2] = seen[3] = 0;
if (g7 & kb_Left) k |= K_LEFT;
if (g7 & kb_Right) k |= K_RIGHT;
if (g7 & kb_Up) k |= K_UP;
diff --git a/games/celeste/ce/src/sf.S b/games/celeste/ce/src/sf.S
new file mode 100644
index 00000000..86f428f1
--- /dev/null
+++ b/games/celeste/ce/src/sf.S
@@ -0,0 +1,527 @@
+/* src/sf.c's float add, multiply and comparisons in eZ80 assembly (the C does them with a call for every 32-bit
+ * operation: 11000 and 19000 cycles each). The same results bit for bit: IEEE single, round to nearest even, no
+ * denormals (taken as 0, results too small give 0), no NaN. A float argument takes two stack slots: its low 24 bits,
+ * then its top byte; a float comes back in E:HL. A significand is worked on a byte at a time, 32 bits: its 24 bits
+ * and 8 below them (the last, bit 0, ORed with all that was shifted out). */
+ .assume adl=1
+
+ .section .bss
+sf_exp: .skip 3 /* the result's exponent + 128 (24 bits) */
+sf_sign:
+ .skip 1
+sf_prod:
+ .skip 8 /* the product's 6 bytes (and 2 zero ones read past them) */
+
+ .section .text
+
+/* the end of both: H L D E the significand (bit 31 set), sf_exp, sf_sign; rounds, packs, returns E:HL */
+sf_round:
+ ld a, e
+ cp a, $80
+ jr c, .Lr_pack ; below half: down
+ jr nz, .Lr_up ; above: up
+ bit 0, d ; half: to even
+ jr z, .Lr_pack
+.Lr_up:
+ inc d
+ jr nz, .Lr_pack
+ inc l
+ jr nz, .Lr_pack
+ inc h
+ jr nz, .Lr_pack
+ ld h, $80 ; (it carried out: 1.0, a power up)
+ push hl
+ ld hl, (sf_exp)
+ inc hl
+ ld (sf_exp), hl
+ pop hl
+.Lr_pack:
+ push hl
+ ld hl, (sf_exp)
+ ld bc, 129
+ or a, a
+ sbc hl, bc
+ jr c, .Lr_zero ; exponent <= 0
+ ld hl, (sf_exp)
+ ld bc, 383
+ or a, a
+ sbc hl, bc
+ jr nc, .Lr_inf ; exponent >= 255
+ pop hl
+ ld a, (sf_exp)
+ add a, $80 ; the exponent's 8 bits
+ ld c, a
+ ld a, h
+ add a, a ; (the hidden bit out)
+ srl c ; exponent >> 1, its bit 0 in carry
+ rra ; exponent's bit 0, then the significand's top 7
+ ld (sf_prod), a ; (bits 16-23)
+ ld a, (sf_sign)
+ or a, c
+ ld e, a ; sign, exponent's top 7
+ ld a, (sf_prod)
+ ld (sf_prod + 2), a
+ ld a, l
+ ld (sf_prod + 1), a
+ ld a, d
+ ld (sf_prod), a
+ ld hl, (sf_prod)
+ ret
+.Lr_zero:
+ pop hl
+ ld a, (sf_sign)
+ ld e, a
+ ld hl, 0
+ ret
+.Lr_inf:
+ pop hl
+ ld a, (sf_sign)
+ or a, $7F
+ ld e, a
+ ld hl, $800000
+ ret
+
+/* f32 f_add(f32 a, f32 b) */
+ .global _f_add
+ .type _f_add, @function
+_f_add:
+ ld iy, 0
+ add iy, sp ; a: (iy + 3) low, (iy + 6) top; b: (iy + 9), (iy + 12)
+ ld de, 0
+ ld hl, (iy + 3)
+ or a, a
+ sbc hl, de
+ jr nz, .La_nz
+ ld a, (iy + 6)
+ and a, $7F
+ jr nz, .La_nz
+ ld hl, (iy + 9) ; a is 0: b, unless it is 0 too (then a & b: -0 only for -0 + -0)
+ or a, a
+ sbc hl, de
+ jr nz, .Lret_b
+ ld a, (iy + 12)
+ and a, $7F
+ jr nz, .Lret_b
+ ld a, (iy + 6)
+ and a, (iy + 12)
+ ld e, a
+ ld hl, 0
+ ret
+.Lret_b:
+ ld hl, (iy + 9)
+ ld e, (iy + 12)
+ ret
+.La_nz:
+ ld hl, (iy + 9)
+ or a, a
+ sbc hl, de
+ jr nz, .Lb_nz
+ ld a, (iy + 12)
+ and a, $7F
+ jr nz, .Lb_nz
+.Lret_a:
+ ld hl, (iy + 3)
+ ld e, (iy + 6)
+ ret
+.Lb_nz:
+ ld a, (iy + 5)
+ rla
+ ld a, (iy + 6)
+ rla
+ ld c, a ; a's exponent
+ ld a, (iy + 11)
+ rla
+ ld a, (iy + 12)
+ rla
+ ld b, a ; b's
+ ld a, c
+ or a, a
+ jr z, .Lret_b ; (a denormal: as 0)
+ ld a, b
+ or a, a
+ jr z, .Lret_a
+ /* the bigger one first */
+ ld a, (iy + 6)
+ and a, $7F
+ ld d, a
+ ld a, (iy + 12)
+ and a, $7F
+ cp a, d
+ jr c, .Lordered ; b's top smaller
+ jr nz, .Lswap
+ ld hl, (iy + 3)
+ ld de, (iy + 9)
+ or a, a
+ sbc hl, de
+ jr nc, .Lordered ; a's low as big or bigger
+.Lswap:
+ ld hl, (iy + 3)
+ ld de, (iy + 9)
+ ld (iy + 3), de
+ ld (iy + 9), hl
+ ld a, (iy + 6)
+ ld d, (iy + 12)
+ ld (iy + 12), a
+ ld (iy + 6), d
+ ld a, c
+ ld c, b
+ ld b, a
+.Lordered:
+ ld a, (iy + 6)
+ and a, $80
+ ld (sf_sign), a
+ ld hl, 0
+ ld l, c
+ ld de, 128
+ add hl, de
+ ld (sf_exp), hl ; (the exponent + 128)
+ ld a, c
+ sub a, b ; d: how far apart
+ ld b, 0 ; (sticky)
+ ld h, (iy + 11) ; the small one's significand: h l d e
+ set 7, h
+ ld l, (iy + 10)
+ ld d, (iy + 9)
+ ld e, 0
+ cp a, 32
+ jr c, .Lalign
+ ld hl, 0
+ ld de, 1 ; (only its sticky bit)
+ jr .Laligned
+.Lalign:
+ cp a, 8
+ jr c, .Lbits
+ ld c, a
+ ld a, e
+ or a, b
+ ld b, a
+ ld e, d
+ ld d, l
+ ld l, h
+ ld h, 0
+ ld a, c
+ sub a, 8
+ jr .Lalign
+.Lbits:
+ or a, a
+ jr z, .Lsticky
+.Lbit:
+ srl h
+ rr l
+ rr d
+ rr e
+ jr nc, .Lbit0
+ set 0, b
+.Lbit0:
+ dec a
+ jr nz, .Lbit
+.Lsticky:
+ ld a, b
+ or a, a
+ jr z, .Laligned
+ set 0, e
+.Laligned:
+ ld a, (iy + 5)
+ or a, $80
+ ld b, a ; the big one's top byte
+ ld a, (iy + 6)
+ xor a, (iy + 12)
+ jp m, .Lsub
+ /* the same sign: add (0, then its three bytes) */
+ ld a, d
+ add a, (iy + 3)
+ ld d, a
+ ld a, l
+ adc a, (iy + 4)
+ ld l, a
+ ld a, h
+ adc a, b
+ ld h, a
+ jp nc, sf_round
+ rr h ; (a bit over: back one, the one out kept as sticky)
+ rr l
+ rr d
+ rr e
+ jr nc, .Lcarry0
+ set 0, e
+.Lcarry0:
+ push hl
+ ld hl, (sf_exp)
+ inc hl
+ ld (sf_exp), hl
+ pop hl
+ jp sf_round
+.Lsub:
+ xor a, a
+ sub a, e
+ ld e, a
+ ld a, (iy + 3)
+ sbc a, d
+ ld d, a
+ ld a, (iy + 4)
+ sbc a, l
+ ld l, a
+ ld a, b
+ sbc a, h
+ ld h, a
+ or a, l
+ or a, d
+ or a, e
+ jr nz, .Lnorm
+ ld e, 0 ; equal: +0
+ ld hl, 0
+ ret
+.Lnorm:
+ ld bc, 0 ; the shifts left
+.Lnorm_byte:
+ ld a, h
+ or a, a
+ jr nz, .Lnorm_bit
+ ld h, l
+ ld l, d
+ ld d, e
+ ld e, 0
+ ld a, c
+ add a, 8
+ ld c, a
+ jr .Lnorm_byte
+.Lnorm_bit:
+ bit 7, h
+ jr nz, .Lnormed
+ sla e
+ rl d
+ rl l
+ rl h
+ inc c
+ jr .Lnorm_bit
+.Lnormed:
+ push hl
+ ld hl, (sf_exp)
+ or a, a
+ sbc hl, bc
+ ld (sf_exp), hl
+ pop hl
+ jp sf_round
+
+/* f32 f_mul(f32 a, f32 b): the 48-bit product of the significands a byte at a time (mlt), its columns added up */
+ .global _f_mul
+ .type _f_mul, @function
+_f_mul:
+ ld iy, 0
+ add iy, sp
+ ld a, (iy + 6)
+ xor a, (iy + 12)
+ and a, $80
+ ld (sf_sign), a
+ ld a, (iy + 5)
+ rla
+ ld a, (iy + 6)
+ rla
+ or a, a
+ jp z, .Lm_zero ; (0, or a denormal)
+ ld c, a
+ ld a, (iy + 11)
+ rla
+ ld a, (iy + 12)
+ rla
+ or a, a
+ jp z, .Lm_zero
+ ld hl, 0 ; the exponent: ea + eb - 127 (+ 128)
+ ld l, a
+ ld de, 0
+ ld e, c
+ add hl, de
+ inc hl
+ ld (sf_exp), hl
+ set 7, (iy + 5) ; (the hidden bits)
+ set 7, (iy + 11)
+ /* columns: k = i + j of a[i] * b[j], each added to what carried from the one before */
+ ld de, 0
+ ld bc, 0
+ ld b, (iy + 3)
+ ld c, (iy + 9)
+ mlt bc
+ ld hl, 0
+ add hl, bc ; column 0
+ ld a, l
+ ld (sf_prod), a
+ ld (sf_prod + 3), hl
+ ld hl, (sf_prod + 4) ; (>> 8: sf_prod + 6 is 0)
+ ld bc, 0
+ ld b, (iy + 3)
+ ld c, (iy + 10)
+ mlt bc
+ add hl, bc
+ ld bc, 0
+ ld b, (iy + 4)
+ ld c, (iy + 9)
+ mlt bc
+ add hl, bc ; column 1
+ ld a, l
+ ld (sf_prod + 1), a
+ ld (sf_prod + 3), hl
+ ld hl, (sf_prod + 4)
+ ld bc, 0
+ ld b, (iy + 3)
+ ld c, (iy + 11)
+ mlt bc
+ add hl, bc
+ ld bc, 0
+ ld b, (iy + 4)
+ ld c, (iy + 10)
+ mlt bc
+ add hl, bc
+ ld bc, 0
+ ld b, (iy + 5)
+ ld c, (iy + 9)
+ mlt bc
+ add hl, bc ; column 2
+ ld a, l
+ ld (sf_prod + 2), a
+ ld (sf_prod + 3), hl
+ ld hl, (sf_prod + 4)
+ ld bc, 0
+ ld b, (iy + 4)
+ ld c, (iy + 11)
+ mlt bc
+ add hl, bc
+ ld bc, 0
+ ld b, (iy + 5)
+ ld c, (iy + 10)
+ mlt bc
+ add hl, bc ; column 3
+ ld d, l ; (byte 3)
+ ld (sf_prod + 3), hl
+ ld hl, (sf_prod + 4)
+ ld bc, 0
+ ld b, (iy + 5)
+ ld c, (iy + 11)
+ mlt bc
+ add hl, bc ; column 4, and what is over it: bytes 4 and 5
+ ld a, (sf_prod + 1)
+ ld c, a
+ ld a, (sf_prod)
+ or a, c
+ ld c, a ; (bytes 0 and 1 for the sticky)
+ ld a, (sf_prod + 2)
+ ld e, a ; h l d e: bytes 5 4 3 2
+ bit 7, h
+ jr nz, .Lm_top
+ ld a, (sf_prod + 1) ; 47 bits: up one, byte 1's top bit in
+ rla
+ rl e
+ rl d
+ rl l
+ rl h
+ ld a, (sf_prod + 1)
+ and a, $7F
+ ld b, a
+ ld a, (sf_prod)
+ or a, b
+ jr .Lm_sticky
+.Lm_top:
+ push hl ; 48 bits: a power up
+ ld hl, (sf_exp)
+ inc hl
+ ld (sf_exp), hl
+ pop hl
+ ld a, c
+.Lm_sticky:
+ or a, a
+ jp z, sf_round
+ set 0, e
+ jp sf_round
+.Lm_zero:
+ ld a, (sf_sign)
+ ld e, a
+ ld hl, 0
+ ret
+
+/* bool f_lt(f32 a, f32 b), f_le: in order, -0 the same as 0 */
+ .global _f_lt
+ .type _f_lt, @function
+_f_lt:
+ call sf_cmp ; a: -1, 0, 1 as a < b, a = b, a > b
+ rla ; (carry: negative)
+ ld a, 0
+ rla
+ ret
+ .global _f_le
+ .type _f_le, @function
+_f_le:
+ call sf_cmp
+ dec a ; (-2 or -1 when a <= b)
+ rla
+ ld a, 0
+ rla
+ ret
+/* a against b (the caller's arguments, past its return address): a = -1, 0 or 1 */
+sf_cmp:
+ ld iy, 0
+ add iy, sp ; a: (iy + 6) low, (iy + 9) top; b: (iy + 12), (iy + 15)
+ ld a, (iy + 9)
+ and a, $7F
+ ld d, a
+ ld a, (iy + 15)
+ and a, $7F
+ ld e, a
+ /* |a| against |b|: c = -1, 0, 1 */
+ ld c, 0
+ ld a, e
+ cp a, d
+ jr c, .Lc_agt ; |b| top < |a| top
+ jr nz, .Lc_alt
+ ld hl, (iy + 6)
+ ld bc, (iy + 12)
+ or a, a
+ sbc hl, bc
+ ld c, 0
+ jr z, .Lc_mag
+ jr nc, .Lc_agt
+.Lc_alt:
+ ld c, 255
+ jr .Lc_mag
+.Lc_agt:
+ ld c, 1
+.Lc_mag:
+ /* both zero: equal */
+ ld hl, (iy + 6)
+ ld a, d
+ ld b, 0
+ or a, a
+ jr nz, .Lc_nz
+ push bc
+ ld bc, 0
+ or a, a
+ sbc hl, bc
+ pop bc
+ jr nz, .Lc_nz
+ ld hl, (iy + 12)
+ ld a, e
+ or a, a
+ jr nz, .Lc_nz
+ push bc
+ ld bc, 0
+ or a, a
+ sbc hl, bc
+ pop bc
+ jr nz, .Lc_nz
+ xor a, a
+ ret
+.Lc_nz:
+ ld a, (iy + 9)
+ xor a, (iy + 15)
+ jp p, .Lc_same
+ ld a, (iy + 9) ; signs differ: the negative one is less
+ or a, a
+ ld a, 255
+ ret m
+ ld a, 1
+ ret
+.Lc_same:
+ ld a, (iy + 9)
+ or a, a
+ ld a, c
+ ret p ; both positive: as their sizes
+ neg ; both negative: the other way
+ ret
diff --git a/games/celeste/ce/src/sf.c b/games/celeste/ce/src/sf.c
index b2988cdb..26ff0ab7 100644
--- a/games/celeste/ce/src/sf.c
+++ b/games/celeste/ce/src/sf.c
@@ -24,6 +24,13 @@ static f32 pack(uint32_t sign, int e, uint32_t m) {
return sign | (uint32_t)e << 23 | (m & 0x7FFFFFul);
}
+#ifdef __TICE__ /* (src/sf.S has them; the C is the bench build's check of it) */
+#define f_add ref_add
+#define f_mul ref_mul
+#define f_lt ref_lt
+#define f_le ref_le
+#endif
+#if !defined(__TICE__) || defined(CEL_BENCH)
f32 f_add(f32 a, f32 b) {
if (!(a & ~SIGN)) return (b & ~SIGN) ? b : (a & b); /* 0 + b; -0 + -0 = -0 */
if (!(b & ~SIGN)) return a;
@@ -52,7 +59,6 @@ f32 f_add(f32 a, f32 b) {
}
return pack(a & SIGN, ea, ma + mb);
}
-f32 f_sub(f32 a, f32 b) { return f_add(a, b ^ SIGN); }
f32 f_mul(f32 a, f32 b) {
uint32_t sign = (a ^ b) & SIGN;
@@ -80,6 +86,7 @@ f32 f_mul(f32 a, f32 b) {
return pack(sign, e, m);
}
+#endif
f32 f_div(f32 a, f32 b) {
uint32_t sign = (a ^ b) & SIGN;
int ea = fexp(a), eb = fexp(b);
@@ -100,9 +107,18 @@ f32 f_div(f32 a, f32 b) {
}
/* comparisons: the bits in order (zeros equal) */
+#if !defined(__TICE__) || defined(CEL_BENCH)
static inline int32_t key(f32 a) { return (a & SIGN) ? -(int32_t)(a & ~SIGN) : (int32_t)a; }
bool f_lt(f32 a, f32 b) { return key(a) < key(b); }
bool f_le(f32 a, f32 b) { return key(a) <= key(b); }
+#endif
+#ifdef __TICE__
+#undef f_add
+#undef f_mul
+#undef f_lt
+#undef f_le
+#endif
+f32 f_sub(f32 a, f32 b) { return f_add(a, b ^ SIGN); }
f32 f_int(int v) { /* exact below 2^24 */
if (!v) return 0;
diff --git a/games/celeste/ce/tests/bench.sh b/games/celeste/ce/tests/bench.sh
index a4551bf3..bee7396e 100755
--- a/games/celeste/ce/tests/bench.sh
+++ b/games/celeste/ce/tests/bench.sh
@@ -14,5 +14,5 @@ echo "camera moving (room $ROOM)"
python3 ../../../tools/ce_emu.py build/bench/bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame --ms 12000 \
--frames-from 2000 --shots 4000 --out build/bench "$@" | tail -n +3
echo "standing still (room 1)"
-python3 ../../../tools/ce_emu.py bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame --keys "1000-1100:2nd,2000-2100:2nd" \
- --ms 16000 --frames-from 6000 --shots 8000 --out build/bench/still "$@" | tail -n +3
+python3 ../../../tools/ce_emu.py bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame --keys "1000-1100:2nd,2000-2100:2nd,3000-3100:2nd" \
+ --ms 17000 --frames-from 7000 --shots 9000 --out build/bench/still "$@" | tail -n +3
diff --git a/games/celeste/ce/tests/emu.sh b/games/celeste/ce/tests/emu.sh
index 435a3f2d..519dcd72 100755
--- a/games/celeste/ce/tests/emu.sh
+++ b/games/celeste/ce/tests/emu.sh
@@ -9,5 +9,5 @@ cd "$HERE"
PATH="$HOME/Documents/Assets/toolchains/CEdev/bin:$PATH" make -s 2>&1 | grep -E "error|success" || true
python3 ../../../platform/ti84ce/check.py .
python3 ../../../tools/ce_emu.py bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame \
- --keys "1000-1100:2nd,2000-2100:2nd,5000-7000:right,6000-6300:2nd,7500-8000:left,8200-8400:clear" \
- --shots 1500,2500,5500,6500,7800,9000 --ms 10000 --frames-from 4500 --out "$OUT" "$@"
+ --keys "1000-1100:2nd,2000-2100:2nd,3000-3100:2nd,6000-8000:right,7000-7300:2nd,8500-9000:left,9200-9400:clear" \
+ --shots 1500,2500,3500,6500,7500,8800,10000 --ms 11000 --frames-from 5500 --out "$OUT" "$@"