A truly modular keyboard
C
sim: charge the Keybit's CRC work, which shows v12's late CRC byte
Nathanba19fa00b1af
diff --git a/firmware/sim/.gitignore b/firmware/sim/.gitignoreindex deb7de3..19b520d 100644--- a/firmware/sim/.gitignore+++ b/firmware/sim/.gitignore@@ -3,4 +3,5 @@ sim_base*.bundle*.dSYM/build_v10/+build_v12/build_base/diff --git a/firmware/sim/Makefile b/firmware/sim/Makefileindex 6c0556a..ec7f8a7 100644--- a/firmware/sim/Makefile+++ b/firmware/sim/Makefile@@ -8,11 +8,13 @@ KEYWORD := ../keyword/Coreifeq ($(shell uname -s),Darwin)CTX := ctx_arm64.S+PIC :=BUNDLE := -bundle -undefined dynamic_lookupSIM_LD :=SED_I := sed -i ''elseCTX := ctx_x86_64.S+PIC := -fPIC# Each Keybit copy keeps its own globals: bind its symbols to itself.BUNDLE := -shared -fPIC -Wl,-BsymbolicSIM_LD := -rdynamic -ldl -lm@@ -32,10 +34,22 @@ node_v10.bundle : node.c ch32fun.h sim.hfor f in main.c link.c link.h soft_i2c.c soft_i2c.h; do git show $(V10_REV):firmware/keybit/$$f > build_v10/$$f; done$(SED_I) 's/(\*f->in \& f->ck)/(READ_PORT(f->in) \& f->ck)/; s/(\*d->in \& d->dt)/(READ_PORT(d->in) \& d->dt)/' build_v10/link.c$(SED_I) 's/^#define CONFIG_PAGE .*/#ifndef CONFIG_PAGE\n#define CONFIG_PAGE 0x08003FC0u\n#endif/' build_v10/main.c- $(CC) $(CFLAGS) -I. -Ibuild_v10 $(BUNDLE) -o $@ node.c build_v10/link.c build_v10/soft_i2c.c+ $(CC) $(CFLAGS) -I. -Ibuild_v10 $(PIC) -Dlink_crc8=link_crc8_real -o build_v10/link.o -c build_v10/link.c+ $(CC) $(CFLAGS) -I. -Ibuild_v10 $(BUNDLE) -o $@ node.c build_v10/link.o build_v10/soft_i2c.c++# The Keybit firmware on the Keybits re-docked on 2026-10-29 (v12), whose+# status stream sends its CRC a bit late: make node_v12.bundle+V12_REV := 7737b2e+node_v12.bundle : node.c ch32fun.h sim.h+ rm -rf build_v12 && mkdir -p build_v12+ for f in main.c link.c link.h soft_i2c.c soft_i2c.h; do git show $(V12_REV):firmware/keybit/$$f > build_v12/$$f; done+ $(CC) $(CFLAGS) -I. -Ibuild_v12 $(PIC) -Dlink_crc8=link_crc8_real -o build_v12/link.o -c build_v12/link.c+ $(CC) $(CFLAGS) -I. -Ibuild_v12 $(BUNDLE) -o $@ node.c build_v12/link.o build_v12/soft_i2c.cnode.bundle : node.c $(KEYBIT)/main.c $(KEYBIT)/link.c $(KEYBIT)/soft_i2c.c $(KEYBIT)/link.h $(KEYBIT)/soft_i2c.h ch32fun.h sim.h- $(CC) $(CFLAGS) -I. -I$(KEYBIT) $(BUNDLE) -o $@ node.c $(KEYBIT)/link.c $(KEYBIT)/soft_i2c.c+ $(CC) $(CFLAGS) -I. -I$(KEYBIT) $(PIC) -Dlink_crc8=link_crc8_real -o link.o -c $(KEYBIT)/link.c+ $(CC) $(CFLAGS) -I. -I$(KEYBIT) $(BUNDLE) -o $@ node.c link.o $(KEYBIT)/soft_i2c.c+ rm -f link.osim : sim.c kw.c $(CTX) sim.h ch32fun.h stm32wbxx_hal.h $(KEYWORD)/Src/keybit_bus.c $(KEYWORD)/Inc/keybit_bus.h$(CC) $(CFLAGS) $(KW_DEFS) -I. -I$(KEYWORD)/Inc -o $@ sim.c kw.c $(CTX) $(SIM_LD)@@ -44,6 +58,6 @@ test : all./sim node.bundle allclean :- rm -rf sim *.bundle *.dSYM build_v10+ rm -rf sim *.bundle *.dSYM build_v10 build_v12.PHONY : all test cleandiff --git a/firmware/sim/node.c b/firmware/sim/node.cindex 6019f15..0565d67 100644--- a/firmware/sim/node.c+++ b/firmware/sim/node.c@@ -3,6 +3,20 @@#include "main.c" /* the Keybit's, found through -I */#undef main+/* link.c is built with its CRC renamed, so main.c's calls land here and pay+ * for the work: the bitwise CRC-8 is ~120 cycles a byte on the CH32V003.+ * The status stream works out an event's CRC while the Keyword is clocking+ * the read, which is where this matters. CRC_CYCLES overrides it. */+#include <stdlib.h>+uint8_t link_crc8_real(uint8_t crc, uint8_t v);+uint8_t link_crc8(uint8_t crc, uint8_t v)+{+ static int cycles = -1;+ if (cycles < 0) { const char *e = getenv("CRC_CYCLES"); cycles = e ? atoi(e) : 120; }+ sim_cycles((uint32_t)cycles);+ return link_crc8_real(crc, v);+}+uint8_t sim_node_depth(void) { return my_depth; }uint8_t sim_node_qcount(void) { return q_count; }const uint8_t *sim_node_diag(void) { return &diag[0][0]; }diff --git a/firmware/sim/sim.c b/firmware/sim/sim.cindex 206b64b..b74cf0b 100644--- a/firmware/sim/sim.c+++ b/firmware/sim/sim.c@@ -449,6 +449,13 @@ void sim_delay_ticks(uint32_t n)advance((int64_t)((n - into) * cur->ps_per_tick) + cur->op_ps);}+/* Register accesses are charged as they happen; plain computation is free+ * unless the code charges it here. A tick is 8 CPU cycles (HCLK/8). */+void sim_cycles(uint32_t n)+{+ advance((int64_t)(n * cur->ps_per_tick / 8));+}+int64_t sim_now(void) { return cur->t; }void sim_sleep(int64_t ps) { advance(ps); }void sim_kw_drive(int pin, int high)@@ -761,6 +768,7 @@ static void settle(const char *name)int64_t t = wait_present(10000 * PS_MS);printf(" %s: all present after %.0f ms\n", name, t / 1e9);CHECK(t >= 0, "%s: not every powered Keybit showed up", name);+ CHECK(t < 2500 * PS_MS, "%s: joining took %.0f ms", name, t / 1e9);run_until(sim_t + 1500 * PS_MS); /* positions settle after the first heartbeats */check_places();}diff --git a/firmware/sim/sim.h b/firmware/sim/sim.hindex 4509e38..c27a548 100644--- a/firmware/sim/sim.h+++ b/firmware/sim/sim.h@@ -24,6 +24,7 @@ void sim_pin_write(uint8_t pin, int high);void sim_pin_mode(uint8_t pin, int mode);uint32_t sim_port_read(const volatile uint32_t *in);void sim_delay_ticks(uint32_t n);+void sim_cycles(uint32_t n); /* charge n CPU cycles of plain computation *//* Keyword side */int64_t sim_now(void); /* ps */