Skip to content

A truly modular keyboard

C

sim: run the real Keybit and Keyword side bus code on the Mac, many Keybits at once

Nathan14f983c0df9d
diff --git a/firmware/keybit/link.c b/firmware/keybit/link.c
index d6662af..9a662f8 100644
--- a/firmware/keybit/link.c
+++ b/firmware/keybit/link.c
@@ -134,6 +134,10 @@ int link_poll(const link_pins_t *p, int doorbell, uint8_t my_depth, const uint8_
* quick: spin on the input register directly, a few cycles per check.
* SPIN bounds the wait (roughly 200 us at 48 MHz). */
#define SPIN 4000
+/* The input register read, swapped out by the host simulator (firmware/sim). */
+#ifndef READ_PORT
+#define READ_PORT(in) (*(in))
+#endif
typedef struct {
const volatile uint32_t *in;
@@ -144,7 +148,7 @@ static inline __attribute__((always_inline)) int spin(const fast_t *f, int level
{
uint32_t want = level ? f->ck : 0;
for (int n = SPIN; n; n--)
- if ((*f->in & f->ck) == want) return 1;
+ if ((READ_PORT(f->in) & f->ck) == want) return 1;
return 0;
}
@@ -153,7 +157,7 @@ static inline __attribute__((always_inline)) int c_get(const fast_t *f, const fa
uint8_t x = 0;
for (int i = 0; i < 8; i++) {
if (!spin(f, 0) || !spin(f, 1)) return 0;
- x = (uint8_t)((x << 1) | ((*d->in & d->dt) != 0));
+ x = (uint8_t)((x << 1) | ((READ_PORT(d->in) & d->dt) != 0));
}
*v = x;
return 1;
diff --git a/firmware/keybit/main.c b/firmware/keybit/main.c
index 0cdc661..56c6cab 100644
--- a/firmware/keybit/main.c
+++ b/firmware/keybit/main.c
@@ -89,7 +89,9 @@
/* Last 64-byte page. The SWIO flasher only rewrites pages the image uses,
* so this survives reflashing. */
+#ifndef CONFIG_PAGE /* the simulator points it at its own copy */
#define CONFIG_PAGE 0x08003FC0u
+#endif
#define CONFIG_MAGIC 0x4B42u
#define EV_PRESSED 0x01
diff --git a/firmware/sim/.gitignore b/firmware/sim/.gitignore
new file mode 100644
index 0000000..003a2e6
--- /dev/null
+++ b/firmware/sim/.gitignore
@@ -0,0 +1,3 @@
+sim
+node.bundle
+*.dSYM/
diff --git a/firmware/sim/Makefile b/firmware/sim/Makefile
new file mode 100644
index 0000000..f8da606
--- /dev/null
+++ b/firmware/sim/Makefile
@@ -0,0 +1,22 @@
+# Host simulator of a Keybit network and the Keyword's side bus, built from
+# the real firmware sources. Apple arm64 only. `make test` runs the checks.
+CC ?= cc
+CFLAGS ?= -O2 -g -Wall -Wno-unused-function
+KEYBIT := ../keybit
+KEYWORD := ../keyword/Core
+
+all : sim node.bundle
+
+node.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 -undefined dynamic_lookup -o $@ node.c $(KEYBIT)/link.c $(KEYBIT)/soft_i2c.c
+
+sim : sim.c kw.c ctx_arm64.S sim.h ch32fun.h stm32wbxx_hal.h $(KEYWORD)/Src/keybit_bus.c $(KEYWORD)/Inc/keybit_bus.h
+ $(CC) $(CFLAGS) -I. -I$(KEYWORD)/Inc -o $@ sim.c kw.c ctx_arm64.S
+
+test : all
+ ./sim node.bundle all
+
+clean :
+ rm -rf sim node.bundle *.dSYM
+
+.PHONY : all test clean
diff --git a/firmware/sim/ch32fun.h b/firmware/sim/ch32fun.h
new file mode 100644
index 0000000..c459580
--- /dev/null
+++ b/firmware/sim/ch32fun.h
@@ -0,0 +1,63 @@
+/* Stand-in for ch32fun.h so the Keybit sources build on the host. */
+#ifndef CH32FUN_SIM_H
+#define CH32FUN_SIM_H
+
+#include <stdint.h>
+#include <stddef.h>
+#include "sim.h"
+
+#define PA0 0x00
+#define PA1 0x01
+#define PA2 0x02
+#define PC0 0x20
+#define PC1 0x21
+#define PC2 0x22
+#define PC3 0x23
+#define PC4 0x24
+#define PD0 0x30
+#define PD1 0x31
+#define PD2 0x32
+#define PD4 0x34
+#define PD5 0x35
+#define PD6 0x36
+
+enum {
+ GPIO_CFGLR_IN_FLOATING = 0x4,
+ GPIO_CFGLR_IN_PUPD = 0x8,
+ GPIO_CFGLR_OUT_10Mhz_PP = 0x1,
+ GPIO_CFGLR_OUT_50Mhz_PP = 0x3,
+ GPIO_CFGLR_OUT_10Mhz_OD = 0x5,
+};
+#define FUN_HIGH 1
+#define FUN_LOW 0
+
+#define funDigitalRead(p) sim_pin_read(p)
+#define funDigitalWrite(p, v) sim_pin_write((p), (v))
+#define funPinMode(p, m) sim_pin_mode((p), (m))
+#define funGpioInitAll() ((void)0)
+#define SystemInit() ((void)0)
+
+/* SysTick at HCLK/8 = 6 MHz, as on the real part. */
+#define DELAY_US_TIME 6
+#define DELAY_MS_TIME 6000
+#define DelaySysTick(n) sim_delay_ticks(n)
+#define Delay_Us(n) sim_delay_ticks((uint32_t)(n) * DELAY_US_TIME)
+
+#define SysTick (sim_systick())
+#define GpioOf(p) (sim_gpio(p))
+#define READ_PORT(in) sim_port_read(in)
+#define ESIG (sim_esig())
+#define FLASH (sim_flash())
+#define CONFIG_PAGE ((uintptr_t)sim_config_page())
+
+#define FLASH_KEY1 0x45670123u
+#define FLASH_KEY2 0xCDEF89ABu
+#define CR_PAGE_ER 0x00020000u
+#define CR_PAGE_PG 0x00010000u
+#define CR_BUF_RST 0x00080000u
+#define CR_STRT_Set 0x00000040u
+#define CR_LOCK_Set 0x00000080u
+#define FLASH_CTLR_BUF_LOAD 0x00040000u
+#define FLASH_STATR_BSY 0x01u
+
+#endif
diff --git a/firmware/sim/ctx_arm64.S b/firmware/sim/ctx_arm64.S
new file mode 100644
index 0000000..1d0f770
--- /dev/null
+++ b/firmware/sim/ctx_arm64.S
@@ -0,0 +1,39 @@
+// Coroutine switch for the simulator (Apple arm64).
+// void ctx_switch(void **save_sp, void *to_sp)
+ .text
+ .p2align 2
+ .globl _ctx_switch
+_ctx_switch:
+ sub sp, sp, #160
+ stp x19, x20, [sp, #0]
+ stp x21, x22, [sp, #16]
+ stp x23, x24, [sp, #32]
+ stp x25, x26, [sp, #48]
+ stp x27, x28, [sp, #64]
+ stp x29, x30, [sp, #80]
+ stp d8, d9, [sp, #96]
+ stp d10, d11, [sp, #112]
+ stp d12, d13, [sp, #128]
+ stp d14, d15, [sp, #144]
+ mov x2, sp
+ str x2, [x0]
+ mov sp, x1
+ ldp x19, x20, [sp, #0]
+ ldp x21, x22, [sp, #16]
+ ldp x23, x24, [sp, #32]
+ ldp x25, x26, [sp, #48]
+ ldp x27, x28, [sp, #64]
+ ldp x29, x30, [sp, #80]
+ ldp d8, d9, [sp, #96]
+ ldp d10, d11, [sp, #112]
+ ldp d12, d13, [sp, #128]
+ ldp d14, d15, [sp, #144]
+ add sp, sp, #160
+ ret
+
+// First switch into a new coroutine lands here with x19 = its node.
+ .globl _ctx_boot
+_ctx_boot:
+ mov x0, x19
+ bl _node_entry
+ brk #0
diff --git a/firmware/sim/kw.c b/firmware/sim/kw.c
new file mode 100644
index 0000000..dee5736
--- /dev/null
+++ b/firmware/sim/kw.c
@@ -0,0 +1,339 @@
+/*
+ * The Keyword in the simulator: the real keybit_bus.c over an I2C3 model
+ * with the real timing (0xF0426363 at 64 MHz: 25 us low, 25 us high, ~20
+ * kHz, clock stretching honoured), the bus poll loop from main.c and the
+ * key handling from keymap.c, reporting every key change it would send.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include "stm32wbxx_hal.h"
+#include "../keyword/Core/Src/keybit_bus.c"
+
+GPIO_TypeDef sim_gpiob;
+IWDG_TypeDef sim_iwdg;
+
+/* Timing knobs, settable from sim.c */
+int kw_keys_ms = 15, kw_relay_ms = 60, kw_scan_ms = 500, kw_task_ms = 5;
+int64_t kw_loop_ps = 50 * PS_US; /* rest of the main loop: BLE, USB */
+int64_t kw_t_low = 25 * PS_US, kw_t_high = 25 * PS_US;
+#define T_SDADEL (500 * PS_NS)
+#define T_POLL (250 * PS_NS)
+#define T_HAL (20 * PS_US) /* HAL call overhead */
+
+/* ------------------------------------------------------------------------ */
+/* I2C3 controller */
+
+static int64_t deadline;
+static int timed_out;
+
+static int scl_up(void)
+{
+ sim_kw_drive(0, 1);
+ while (!sim_kw_read(0)) {
+ if (sim_now() > deadline) { timed_out = 1; return 0; }
+ sim_sleep(T_POLL);
+ }
+ return 1;
+}
+
+/* SCL is low on entry and on return. */
+static int put_bit(int b)
+{
+ sim_sleep(T_SDADEL);
+ sim_kw_drive(1, b);
+ sim_sleep(kw_t_low - T_SDADEL);
+ if (!scl_up()) return 0;
+ sim_sleep(kw_t_high);
+ sim_kw_drive(0, 0);
+ return 1;
+}
+
+static int get_bit(int *b)
+{
+ sim_sleep(T_SDADEL);
+ sim_kw_drive(1, 1);
+ sim_sleep(kw_t_low - T_SDADEL);
+ if (!scl_up()) return 0;
+ sim_sleep(kw_t_high);
+ *b = sim_kw_read(1);
+ sim_kw_drive(0, 0);
+ return 1;
+}
+
+/* 1 ack, 0 nack, -1 timeout */
+static int put_byte(uint8_t v)
+{
+ for (int i = 7; i >= 0; i--)
+ if (!put_bit((v >> i) & 1)) return -1;
+ int b;
+ if (!get_bit(&b)) return -1;
+ return !b;
+}
+
+static int get_byte(uint8_t *v, int ack)
+{
+ uint8_t x = 0;
+ for (int i = 0; i < 8; i++) {
+ int b;
+ if (!get_bit(&b)) return 0;
+ x = (uint8_t)(x << 1 | b);
+ }
+ *v = x;
+ return put_bit(!ack);
+}
+
+/* A START, or a repeated START (SCL low on entry: a full low period first). */
+static int start(void)
+{
+ int repeated = !sim_kw_read(0);
+ sim_sleep(T_SDADEL);
+ sim_kw_drive(1, 1);
+ if (repeated) sim_sleep(kw_t_low - T_SDADEL);
+ if (!scl_up()) return 0;
+ sim_sleep(kw_t_high);
+ sim_kw_drive(1, 0);
+ sim_sleep(kw_t_high);
+ sim_kw_drive(0, 0);
+ return 1;
+}
+
+static void stop(void)
+{
+ sim_sleep(T_SDADEL);
+ sim_kw_drive(1, 0);
+ sim_sleep(kw_t_low);
+ scl_up();
+ sim_sleep(kw_t_high);
+ sim_kw_drive(1, 1);
+ sim_sleep(kw_t_low); /* bus free time */
+}
+
+static HAL_StatusTypeDef fail(void)
+{
+ if (getenv("KW_TRACE2")) printf("%9.3f ms fail%s\n", sim_now() / 1e9, timed_out ? " (timeout)" : "");
+ if (timed_out) {
+ sim_kw_drive(0, 1);
+ sim_kw_drive(1, 1);
+ return HAL_TIMEOUT;
+ }
+ stop();
+ return HAL_ERROR;
+}
+
+static void begin(uint32_t timeout)
+{
+ sim_sleep(T_HAL);
+ deadline = sim_now() + (int64_t)timeout * PS_MS;
+ timed_out = 0;
+}
+
+HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *h, uint16_t addr, uint32_t trials, uint32_t timeout)
+{
+ (void)h; (void)trials;
+ begin(timeout);
+ if (!start()) return fail();
+ int a = put_byte((uint8_t)addr);
+ if (a < 0) return fail();
+ stop();
+ return a ? HAL_OK : HAL_ERROR;
+}
+
+HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *h, uint16_t addr, uint16_t reg, uint16_t regsize,
+ uint8_t *buf, uint16_t len, uint32_t timeout)
+{
+ (void)h; (void)regsize;
+ begin(timeout);
+ if (!start() || put_byte((uint8_t)addr) != 1 || put_byte((uint8_t)reg) != 1) return fail();
+ if (!start() || put_byte((uint8_t)(addr | 1)) != 1) return fail();
+ for (int i = 0; i < len; i++)
+ if (!get_byte(&buf[i], i < len - 1)) return fail();
+ stop();
+ if (getenv("KW_TRACE")) {
+ printf("%9.3f ms read %02x reg %02x:", sim_now() / 1e9, addr >> 1, reg);
+ for (int i = 0; i < len; i++) printf(" %02x", buf[i]);
+ printf("\n");
+ }
+ return HAL_OK;
+}
+
+HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *h, uint16_t addr, uint16_t reg, uint16_t regsize,
+ uint8_t *buf, uint16_t len, uint32_t timeout)
+{
+ (void)h; (void)regsize;
+ begin(timeout);
+ if (!start() || put_byte((uint8_t)addr) != 1 || put_byte((uint8_t)reg) != 1) return fail();
+ for (int i = 0; i < len; i++)
+ if (put_byte(buf[i]) != 1) return fail();
+ stop();
+ return HAL_OK;
+}
+
+uint32_t HAL_GetTick(void) { return (uint32_t)(sim_now() / PS_MS); }
+void HAL_Delay(uint32_t ms) { sim_sleep((int64_t)ms * PS_MS); }
+GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *port, uint16_t pin)
+{
+ (void)port;
+ return sim_kw_read(pin == GPIO_PIN_13 ? 0 : 1) ? GPIO_PIN_SET : GPIO_PIN_RESET;
+}
+void HAL_GPIO_WritePin(GPIO_TypeDef *port, uint16_t pin, GPIO_PinState s)
+{
+ (void)port;
+ if (pin & GPIO_PIN_13) sim_kw_drive(0, s == GPIO_PIN_SET);
+ if (pin & GPIO_PIN_14) sim_kw_drive(1, s == GPIO_PIN_SET);
+}
+void HAL_GPIO_Init(GPIO_TypeDef *port, GPIO_InitTypeDef *init) { (void)port; (void)init; }
+HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *h) { (void)h; sim_kw_drive(0, 1); sim_kw_drive(1, 1); return HAL_OK; }
+HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *h) { (void)h; return HAL_OK; }
+HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *h) { (void)h; return HAL_I2C_STATE_READY; }
+
+/* ------------------------------------------------------------------------ */
+/* main.c: Keybit_Bus_Poll, without the host messages */
+
+#define KEYBIT_MISSES_MAX 2
+
+static I2C_HandleTypeDef hi2c3;
+keybit_info_t keybits[KEYBIT_MAX];
+int keybit_count;
+keybit_remote_t keybit_remotes[KEYBIT_REMOTE_MAX];
+int keybit_remote_count;
+
+static void Keybit_Bus_Poll(void)
+{
+ static uint32_t last_scan, last_keys, last_relay;
+
+ keybit_bus_unstick(&hi2c3);
+ if (keybit_bus_jammed()) return;
+ if (HAL_GetTick() - last_keys >= (uint32_t)kw_keys_ms) {
+ last_keys = HAL_GetTick();
+ keybit_bus_poll_keys(&hi2c3, keybits, keybit_count);
+ }
+ if (HAL_GetTick() - last_relay >= (uint32_t)kw_relay_ms) {
+ last_relay = HAL_GetTick();
+ keybit_bus_poll_relay(&hi2c3, keybits, keybit_count, keybit_remotes, &keybit_remote_count);
+ }
+ if (HAL_GetTick() - last_scan < (uint32_t)kw_scan_ms) return;
+ last_scan = HAL_GetTick();
+
+ keybit_bus_assign(&hi2c3);
+ static uint8_t missed[KEYBIT_MAX];
+ keybit_info_t found[KEYBIT_MAX];
+ uint8_t found_missed[KEYBIT_MAX] = {0};
+ int n = keybit_bus_scan(&hi2c3, found, KEYBIT_MAX);
+ for (int i = 0; i < keybit_count && n < KEYBIT_MAX; i++) {
+ int here = 0;
+ for (int j = 0; j < n && !here; j++)
+ if (memcmp(found[j].uid, keybits[i].uid, sizeof(found[j].uid)) == 0) {
+ here = 1;
+ found[j].saw_down = keybits[i].saw_down;
+ found[j].pend = keybits[i].pend;
+ found[j].tap = keybits[i].tap;
+ }
+ if (!here && missed[i] < KEYBIT_MISSES_MAX) {
+ found[n] = keybits[i];
+ found_missed[n++] = (uint8_t)(missed[i] + 1);
+ }
+ }
+ memcpy(keybits, found, n * sizeof(found[0]));
+ memcpy(missed, found_missed, sizeof(missed));
+ keybit_count = n;
+}
+
+/* ------------------------------------------------------------------------ */
+/* keymap.c: what the keys task would report, per Keybit */
+
+#define SHOWN_MAX 64
+static struct { uint32_t uid; uint8_t down; } shown[SHOWN_MAX];
+static int shown_n;
+
+static void show(uint32_t uid, int down)
+{
+ for (int i = 0; i < shown_n; i++)
+ if (shown[i].uid == uid) {
+ if (shown[i].down != down) { shown[i].down = (uint8_t)down; kw_report(uid, down); }
+ return;
+ }
+ if (shown_n < SHOWN_MAX) {
+ shown[shown_n].uid = uid;
+ shown[shown_n++].down = (uint8_t)down;
+ if (down) kw_report(uid, down);
+ }
+}
+
+static void Keys_Task(void)
+{
+ for (int i = 0; i < keybit_count; i++) {
+ int down = keybits[i].key_down;
+ if (keybits[i].tap == 1) { down = 1; keybits[i].tap = 2; }
+ else if (keybits[i].tap == 2) keybits[i].tap = 0;
+ show(uid32(keybits[i].uid), down);
+ }
+ for (int r = 0; r < keybit_remote_count; r++) {
+ keybit_remote_t *k = &keybit_remotes[r];
+ int down = k->present && (k->key_down || k->tap == 1);
+ if (k->tap == 1) { k->tap = 2; k->dirty = 1; }
+ else if (k->tap == 2) k->tap = 0;
+ if (down) k->fresh_down = 0;
+ k->dirty = 0;
+ k->moved = 0;
+ show(k->uid, down);
+ }
+}
+
+/* Probe mode: instead of the poll loop, IsDeviceReady the first Keybit at
+ * the given half period, like I2CSCAN after I2CSPEED. */
+int kw_probe_left, kw_probe_acks;
+int64_t kw_probe_half;
+
+void kw_main(void)
+{
+ sim_kw_drive(0, 1);
+ sim_kw_drive(1, 1);
+ sim_sleep(5 * PS_MS);
+ uint32_t last_task = 0;
+ for (;;) {
+ if (kw_probe_left > 0 && keybit_count > 0) {
+ /* I2CSCAN: every address in a row, as proto.c does it */
+ int64_t lo = kw_t_low, hi = kw_t_high;
+ kw_t_low = kw_t_high = kw_probe_half;
+ for (uint8_t a = 0x08; a <= 0x77; a++) {
+ int ok = HAL_I2C_IsDeviceReady(&hi2c3, (uint16_t)(a << 1), 1, 10) == HAL_OK;
+ if (a == keybits[0].addr) {
+ kw_probe_acks += ok;
+ if (getenv("KW_TRACE")) printf("probe %02x at %.3f ms: %s\n", a, sim_now() / 1e9, ok ? "ack" : "NACK");
+ }
+ }
+ kw_probe_left--;
+ kw_t_low = lo; kw_t_high = hi;
+ /* the normal poll loop runs between I2CSCAN commands */
+ int64_t until = sim_now() + (100 + rand() % 200) * PS_MS;
+ while (sim_now() < until) { Keybit_Bus_Poll(); sim_sleep(kw_loop_ps); }
+ continue;
+ }
+ Keybit_Bus_Poll();
+ if (HAL_GetTick() - last_task >= (uint32_t)kw_task_ms) {
+ last_task = HAL_GetTick();
+ Keys_Task();
+ }
+ sim_sleep(kw_loop_ps);
+ }
+}
+
+int kw_present(uint32_t uid)
+{
+ for (int i = 0; i < keybit_count; i++) if (uid32(keybits[i].uid) == uid) return 1;
+ for (int r = 0; r < keybit_remote_count; r++) if (keybit_remotes[r].uid == uid && keybit_remotes[r].present) return 2;
+ return 0;
+}
+
+/* The face a v11 Keybit reported, or 0. */
+int kw_where(uint32_t uid, uint32_t *parent, char *pside, char *face)
+{
+ for (int r = 0; r < keybit_remote_count; r++)
+ if (keybit_remotes[r].uid == uid && keybit_remotes[r].present && keybit_remotes[r].face) {
+ *parent = keybit_remotes[r].parent;
+ *pside = keybit_remotes[r].pside;
+ *face = keybit_remotes[r].face;
+ return 1;
+ }
+ return 0;
+}
diff --git a/firmware/sim/node.c b/firmware/sim/node.c
new file mode 100644
index 0000000..44b11df
--- /dev/null
+++ b/firmware/sim/node.c
@@ -0,0 +1,9 @@
+/* One Keybit: its real main.c, plus a few getters for the simulator. */
+#define main keybit_main
+#include "../keybit/main.c"
+#undef main
+
+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]; }
+uint8_t sim_node_backwards(void) { return backwards; }
diff --git a/firmware/sim/sim.c b/firmware/sim/sim.c
new file mode 100644
index 0000000..698bacf
--- /dev/null
+++ b/firmware/sim/sim.c
@@ -0,0 +1,677 @@
+/*
+ * Keybit network simulator.
+ *
+ * Every Keybit runs the real firmware (main.c, link.c, soft_i2c.c), loaded
+ * as its own copy of node.bundle so each has its own globals, on its own
+ * clock (48 MHz HSI give or take 1%, SysTick at HCLK/8). The Keyword runs
+ * the real keybit_bus.c over a model of I2C3 (kw.c). Each is a coroutine;
+ * every register access costs the caller some cycles and the one furthest
+ * behind in time always runs next, so all of them stay within one access
+ * of each other.
+ *
+ * Wires: each side contact pair is a wire with the 10k pull-ups of every
+ * Keybit on it (and the Keyword's on its bus), open drain or push-pull
+ * drivers, and an RC rise after the last low driver lets go. Two Keybits
+ * meet contact i to contact 3-i; a side's contacts going clockwise are
+ * 3V3 SDA GND SCL on N and W and SCL GND SDA 3V3 on E and S (keybit.kicad_pcb),
+ * so an N or W side must meet an E or S side. The Keyword's L and T sides
+ * are like N/W, its R side like E/S. A Keybit is powered while a 3V3 path
+ * of correct contacts reaches the Keyword; it boots from scratch whenever
+ * it gains power.
+ *
+ * Apple arm64 only (ctx_arm64.S).
+ */
+#include <dlfcn.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "sim.h"
+#include "ch32fun.h"
+
+#define MAXN 40
+#define NPIN 64
+#define STACK (256 * 1024)
+#define LOG_MAX 200000
+
+void ctx_switch(void **save, void *to);
+void ctx_boot(void);
+
+enum { KEYWORD, KEYBIT };
+enum { N_, E_, S_, W_ };
+enum { R_V, R_G, R_SDA, R_SCL };
+
+typedef struct {
+ int kind, idx;
+ int64_t t, t_boot, op_ps, spin_ps, pass_ps;
+ double ps_per_tick;
+ uint32_t tick0;
+ void *sp;
+ char *stack;
+ int placed, powered, runnable;
+ int x, y, rot;
+ uint8_t mode[NPIN], out[NPIN];
+ int button;
+ int (*entry)(void);
+ uint8_t (*depth)(void);
+ uint8_t (*qcount)(void);
+ const uint8_t *(*diag)(void);
+ sim_systick_t st;
+ sim_gpio_t gpio[4];
+ sim_esig_t esig;
+ sim_flash_t flash;
+ uint32_t cfg[16];
+ int boots;
+} node_t;
+
+typedef struct {
+ int tie; /* +1 on someone's 3V3, -1 on someone's GND */
+ int low, pp_high;
+ int npull;
+ int64_t rel_at, rise;
+ int64_t glitch_from, glitch_to;
+ int m[24], nm; /* members: node * NPIN + pin */
+} wire_t;
+
+static node_t nodes[MAXN];
+static int n_nodes;
+static node_t *cur;
+static void *host_sp;
+static int64_t t_end, next_min, sim_t;
+static wire_t wires[MAXN * 8 + 4];
+static int n_wires;
+static int pin_wire[MAXN][NPIN];
+static long shorts, fights;
+
+/* knobs */
+static int op_cycles = 16; /* a pin or SysTick access, with the code around it */
+static int spin_cycles = 5; /* one pass of link.c's tight spin on the input register */
+/* The main loop's own work between register accesses (depth, doorbell,
+ * side bookkeeping), charged once per pass on its one read of the key pin.
+ * Fitted to the real Keybits: they answer every address at 25 kHz, half at
+ * 30 kHz and none at 35 kHz (I2CSPEED sweep, 2026-10-29). */
+static int pass_cycles = 300;
+static double c_pf = 15, kw_pullup_k = 4.7;
+static unsigned seed = 1;
+static uint32_t tick0_all;
+static char bundle[1024], tmpdir[512];
+
+static const uint8_t SDA_PIN[4] = { PA1, PC1, PC4, PD4 }; /* N E S W */
+static const uint8_t SCL_PIN[4] = { PA2, PC2, PD2, PD5 };
+static const int ROLE[2][4] = { { R_V, R_SDA, R_G, R_SCL }, /* N, W and the Keyword's L, T */
+ { R_SCL, R_G, R_SDA, R_V } }; /* E, S and the Keyword's R */
+static const int TYPE[4] = { 0, 1, 1, 0 };
+static const int DX[4] = { 0, 1, 0, -1 }, DY[4] = { -1, 0, 1, 0 };
+
+static uint32_t rnd(void) { seed = seed * 1103515245u + 12345u; return seed >> 8; }
+static double frand(void) { return (rnd() & 0xFFFFFF) / (double)0x1000000; }
+
+/* ------------------------------------------------------------------------ */
+/* Scheduler */
+
+static void set_next_min(void)
+{
+ int64_t m = t_end;
+ for (int i = 0; i < n_nodes; i++)
+ if (&nodes[i] != cur && nodes[i].runnable && nodes[i].t < m) m = nodes[i].t;
+ next_min = m;
+}
+
+static void schedule(void)
+{
+ node_t *m = NULL;
+ for (int i = 0; i < n_nodes; i++)
+ if (nodes[i].runnable && (!m || nodes[i].t < m->t)) m = &nodes[i];
+ node_t *from = cur;
+ if (!m || m->t >= t_end) { cur = NULL; ctx_switch(&from->sp, host_sp); return; }
+ if (m == from) { set_next_min(); return; }
+ cur = m;
+ set_next_min();
+ ctx_switch(&from->sp, m->sp);
+}
+
+static inline void advance(int64_t ps)
+{
+ cur->t += ps;
+ if (cur->t > next_min) schedule();
+}
+
+void node_entry(node_t *n)
+{
+ if (n->kind == KEYBIT) n->entry();
+ else kw_main();
+ n->runnable = 0;
+ for (;;) schedule();
+}
+
+static void run_until(int64_t t)
+{
+ t_end = t;
+ for (;;) {
+ node_t *m = NULL;
+ for (int i = 0; i < n_nodes; i++)
+ if (nodes[i].runnable && (!m || nodes[i].t < m->t)) m = &nodes[i];
+ if (!m || m->t >= t) break;
+ cur = m;
+ set_next_min();
+ ctx_switch(&host_sp, m->sp);
+ }
+ sim_t = t;
+}
+
+static void start_coroutine(node_t *n)
+{
+ if (!n->stack) n->stack = malloc(STACK);
+ void **sp = (void **)(n->stack + STACK - 160);
+ memset(sp, 0, 160);
+ sp[0] = n; /* x19 */
+ sp[11] = (void *)ctx_boot; /* x30 */
+ n->sp = sp;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Wires */
+
+static int wire_read(int wi, int64_t t)
+{
+ wire_t *w = &wires[wi];
+ if (w->tie) return w->tie > 0;
+ if (t >= w->glitch_from && t < w->glitch_to) return 0;
+ if (w->low) return 0;
+ if (w->pp_high) return 1;
+ return t >= w->rel_at + w->rise;
+}
+
+static int drives_low(int id)
+{
+ node_t *n = &nodes[id / NPIN];
+ int p = id % NPIN;
+ if (!n->powered) return 0;
+ if (n->kind == KEYWORD) return !n->out[p];
+ return (n->mode[p] == GPIO_CFGLR_OUT_10Mhz_OD || n->mode[p] == GPIO_CFGLR_OUT_50Mhz_PP ||
+ n->mode[p] == GPIO_CFGLR_OUT_10Mhz_PP) && !n->out[p];
+}
+
+static int drives_high(int id)
+{
+ node_t *n = &nodes[id / NPIN];
+ int p = id % NPIN;
+ return n->powered && n->kind == KEYBIT &&
+ (n->mode[p] == GPIO_CFGLR_OUT_50Mhz_PP || n->mode[p] == GPIO_CFGLR_OUT_10Mhz_PP) && n->out[p];
+}
+
+static void wire_update(int wi, int64_t t)
+{
+ wire_t *w = &wires[wi];
+ int low = 0, hi = 0;
+ for (int i = 0; i < w->nm; i++) { low |= drives_low(w->m[i]); hi |= drives_high(w->m[i]); }
+ if (w->low && !low) w->rel_at = t;
+ if (low && hi) fights++;
+ static int64_t tr_from = -2, tr_to;
+ if (tr_from == -2) { const char *e = getenv("WTRACE"); tr_from = e ? (int64_t)(atof(e) * PS_MS) : -1; tr_to = tr_from + 2 * PS_MS; }
+ if (tr_from >= 0 && t >= tr_from && t < tr_to && wi == pin_wire[0][0] && w->low != low) printf("%12.3f us SCL %s by %d\n", t / 1e6, low ? "low " : "rel ", cur ? cur->idx : -1);
+ if (tr_from >= 0 && t >= tr_from && t < tr_to && wi == pin_wire[0][1] && w->low != low) printf("%12.3f us SDA %s by %d\n", t / 1e6, low ? "low " : "rel ", cur ? cur->idx : -1);
+ if ((w->tie > 0 && low) || (w->tie < 0 && hi)) shorts++;
+ w->low = low;
+ w->pp_high = hi;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Topology */
+
+static int uf[MAXN * NPIN];
+static int find(int a) { while (uf[a] != a) a = uf[a] = uf[uf[a]]; return a; }
+
+static int side_facing(const node_t *n, int dx, int dy)
+{
+ for (int s = 0; s < 4; s++) {
+ int x = DX[s], y = DY[s];
+ for (int r = 0; r < n->rot; r++) { int t = x; x = -y; y = t; }
+ if (x == dx && y == dy) return s;
+ }
+ return -1;
+}
+
+static int keybit_at(int x, int y)
+{
+ for (int i = 1; i < n_nodes; i++)
+ if (nodes[i].placed && nodes[i].x == x && nodes[i].y == y) return i;
+ return x == 0 && y == 0 ? 0 : -1;
+}
+
+static int pin_of(int node, int side, int role)
+{
+ if (node == 0) return node * NPIN + (role == R_SCL ? 0 : 1);
+ return node * NPIN + (role == R_SCL ? SCL_PIN[side] : SDA_PIN[side]);
+}
+
+static void build(void)
+{
+ int ties[MAXN * NPIN];
+ memset(ties, 0, sizeof(ties));
+ for (int i = 0; i < MAXN * NPIN; i++) uf[i] = i;
+ int vv[MAXN][MAXN];
+ memset(vv, 0, sizeof(vv));
+
+ for (int a = 1; a < n_nodes; a++) {
+ node_t *A = &nodes[a];
+ if (!A->placed) continue;
+ for (int d = 0; d < 4; d++) {
+ int b = keybit_at(A->x + DX[d], A->y + DY[d]);
+ if (b < 0 || (b > 0 && b < a)) continue;
+ int sa = side_facing(A, DX[d], DY[d]);
+ int tb, sb = -1;
+ if (b == 0) {
+ if (DX[d] == 0 && DY[d] == -1) continue; /* the Keyword's bottom: USB, no contacts */
+ tb = DX[d] == -1 ? 1 : 0; /* we're on its right: its R side is E/S-like */
+ } else {
+ sb = side_facing(&nodes[b], -DX[d], -DY[d]);
+ tb = TYPE[sb];
+ }
+ for (int i = 0; i < 4; i++) {
+ int ra = ROLE[TYPE[sa]][i], rb = ROLE[tb][3 - i];
+ if ((ra == R_SDA || ra == R_SCL) && ra == rb) {
+ uf[find(pin_of(a, sa, ra))] = find(pin_of(b, sb, rb));
+ } else if (ra == R_V && rb == R_V) {
+ vv[a][b] = vv[b][a] = 1;
+ } else if ((ra == R_SDA || ra == R_SCL) && (rb == R_V || rb == R_G)) {
+ ties[pin_of(a, sa, ra)] = rb == R_V ? 1 : -1;
+ } else if ((rb == R_SDA || rb == R_SCL) && (ra == R_V || ra == R_G)) {
+ ties[pin_of(b, sb, rb)] = ra == R_V ? 1 : -1;
+ }
+ }
+ }
+ }
+
+ /* power: 3V3 contacts back to the Keyword */
+ int pw[MAXN] = { 1 }, changed = 1;
+ while (changed) {
+ changed = 0;
+ for (int a = 0; a < n_nodes; a++) if (pw[a])
+ for (int b = 1; b < n_nodes; b++) if (!pw[b] && vv[a][b] && nodes[b].placed) pw[b] = changed = 1;
+ }
+
+ /* wires from the union-find */
+ int root_wire[MAXN * NPIN];
+ for (int i = 0; i < MAXN * NPIN; i++) root_wire[i] = -1;
+ n_wires = 0;
+ memset(pin_wire, -1, sizeof(pin_wire));
+ for (int n = 0; n < n_nodes; n++) {
+ int pins[8], np = 0;
+ if (n == 0) { pins[np++] = 0; pins[np++] = 1; }
+ else for (int s = 0; s < 4; s++) { pins[np++] = SDA_PIN[s]; pins[np++] = SCL_PIN[s]; }
+ for (int k = 0; k < np; k++) {
+ int id = n * NPIN + pins[k], r = find(id);
+ if (root_wire[r] < 0) {
+ root_wire[r] = n_wires;
+ wire_t *w = &wires[n_wires++];
+ memset(w, 0, sizeof(*w));
+ w->rel_at = -PS_MS;
+ w->glitch_from = w->glitch_to = -1;
+ }
+ wire_t *w = &wires[root_wire[r]];
+ if (w->nm < 24) w->m[w->nm++] = id;
+ if (ties[id]) w->tie = ties[id];
+ pin_wire[n][pins[k]] = root_wire[r];
+ }
+ }
+ for (int i = 0; i < n_wires; i++) {
+ wire_t *w = &wires[i];
+ double g = 0;
+ for (int k = 0; k < w->nm; k++) g += w->m[k] / NPIN == 0 ? 1 / kw_pullup_k : 1 / 10.0;
+ double r_k = 1 / g, c = c_pf * (w->nm + 1);
+ w->rise = (int64_t)(1.2 * r_k * 1e3 * c * 1e-12 * 1e12); /* to VIH ~0.7 VDD */
+ }
+
+ /* power changes: boot or kill */
+ for (int n = 1; n < n_nodes; n++) {
+ node_t *k = &nodes[n];
+ int want = pw[n] && k->placed;
+ if (want && !k->powered) {
+ char path[700];
+ snprintf(path, sizeof(path), "%s/n%d_%d.bundle", tmpdir, n, k->boots);
+ char cmd[1600];
+ snprintf(cmd, sizeof(cmd), "cp '%s' '%s'", bundle, path);
+ if (system(cmd) != 0) { fprintf(stderr, "copy failed\n"); exit(1); }
+ void *dl = dlopen(path, RTLD_NOW | RTLD_LOCAL);
+ if (!dl) { fprintf(stderr, "%s\n", dlerror()); exit(1); }
+ k->entry = (int (*)(void))dlsym(dl, "keybit_main");
+ k->depth = (uint8_t (*)(void))dlsym(dl, "sim_node_depth");
+ k->qcount = (uint8_t (*)(void))dlsym(dl, "sim_node_qcount");
+ k->diag = (const uint8_t *(*)(void))dlsym(dl, "sim_node_diag");
+ memset(k->mode, GPIO_CFGLR_IN_FLOATING, sizeof(k->mode));
+ memset(k->out, 1, sizeof(k->out));
+ k->t = k->t_boot = sim_t + (int64_t)(1 + 2 * frand()) * PS_MS;
+ k->tick0 = tick0_all;
+ k->powered = k->runnable = 1;
+ k->boots++;
+ start_coroutine(k);
+ } else if (!want && k->powered) {
+ k->powered = k->runnable = 0;
+ }
+ }
+ for (int i = 0; i < n_wires; i++) wire_update(i, sim_t);
+}
+
+/* ------------------------------------------------------------------------ */
+/* What the code calls */
+
+sim_systick_t *sim_systick(void)
+{
+ advance(cur->op_ps);
+ cur->st.CNT = cur->tick0 + (uint32_t)(uint64_t)((cur->t - cur->t_boot) / cur->ps_per_tick);
+ return &cur->st;
+}
+
+sim_gpio_t *sim_gpio(uint8_t pin) { return &cur->gpio[pin >> 4]; }
+sim_esig_t *sim_esig(void) { return &cur->esig; }
+sim_flash_t *sim_flash(void) { advance(120 * PS_US); return &cur->flash; }
+void *sim_config_page(void) { return cur->cfg; }
+
+static long btn_reads[MAXN];
+static int read_pin(node_t *n, uint8_t pin)
+{
+ if (pin == PD6) { btn_reads[n->idx]++; return !n->button; }
+ int wi = pin_wire[n->idx][pin];
+ return wi < 0 ? 1 : wire_read(wi, n->t);
+}
+
+int sim_pin_read(uint8_t pin)
+{
+ static int64_t rt_from = -2, last;
+ if (rt_from == -2) { const char *e = getenv("RTRACE"); rt_from = e ? (int64_t)(atof(e) * PS_MS) : -1; }
+ if (rt_from >= 0 && cur->idx == 1 && getenv("RALL") && cur->t >= rt_from && cur->t < rt_from + PS_MS) {
+ static int lastpin = -1; static long rep;
+ if (pin != lastpin) { printf("%12.3f us kb1 rd %02x (x%ld before)\n", cur->t / 1e6, pin, rep); rep = 0; lastpin = pin; } else rep++;
+ }
+ if (rt_from >= 0 && cur->idx == 1 && pin == PD4 && cur->t >= rt_from && cur->t < rt_from + PS_MS) {
+ if (cur->t - last > 4 * PS_US) printf("%12.3f us kb1 reads SDA after %.1f us gap\n", cur->t / 1e6, (cur->t - last) / 1e6);
+ last = cur->t;
+ }
+ /* passes vary with the branches taken: +-30% */
+ advance(pin == PD6 ? cur->pass_ps * (70 + (int64_t)(rnd() % 61)) / 100 : cur->op_ps);
+ return read_pin(cur, pin);
+}
+
+void sim_pin_write(uint8_t pin, int high)
+{
+ advance(cur->op_ps);
+ cur->out[pin] = (uint8_t)high;
+ int wi = pin_wire[cur->idx][pin];
+ if (wi >= 0) wire_update(wi, cur->t);
+}
+
+void sim_pin_mode(uint8_t pin, int mode)
+{
+ advance(cur->op_ps);
+ cur->mode[pin] = (uint8_t)mode;
+ int wi = pin_wire[cur->idx][pin];
+ if (wi >= 0) wire_update(wi, cur->t);
+}
+
+uint32_t sim_port_read(const volatile uint32_t *in)
+{
+ advance(cur->spin_ps);
+ int port = (int)((const sim_gpio_t *)in - cur->gpio);
+ uint32_t v = 0;
+ for (int b = 0; b < 8; b++) if (read_pin(cur, (uint8_t)(port << 4 | b))) v |= 1u << b;
+ return v;
+}
+
+/* Like ch32fun's DelaySysTick: spin until the counter has moved n times,
+ * so it comes up short by however far into the current tick we already were. */
+void sim_delay_ticks(uint32_t n)
+{
+ double ticks = (cur->t - cur->t_boot) / cur->ps_per_tick;
+ double into = ticks - (int64_t)ticks;
+ advance((int64_t)((n - into) * cur->ps_per_tick) + cur->op_ps);
+}
+
+int64_t sim_now(void) { return cur->t; }
+void sim_sleep(int64_t ps) { advance(ps); }
+void sim_kw_drive(int pin, int high)
+{
+ advance(10 * PS_NS);
+ cur->out[pin] = (uint8_t)high;
+ wire_update(pin_wire[0][pin], cur->t);
+}
+int sim_kw_read(int pin)
+{
+ advance(10 * PS_NS);
+ return wire_read(pin_wire[0][pin], cur->t);
+}
+
+/* ------------------------------------------------------------------------ */
+/* Reports and presses */
+
+typedef struct { int64_t t; uint32_t uid; int down; } report_t;
+static report_t reports[LOG_MAX];
+static int n_reports;
+void kw_report(uint32_t uid, int down)
+{
+ if (n_reports < LOG_MAX) reports[n_reports++] = (report_t){ cur->t, uid, down };
+}
+
+typedef struct { int node; int64_t down, up; } press_t;
+static press_t presses[LOG_MAX];
+static int n_presses;
+
+extern int kw_present(uint32_t uid);
+extern int kw_where(uint32_t uid, uint32_t *parent, char *pside, char *face);
+extern int kw_keys_ms, kw_relay_ms, kw_scan_ms, kw_task_ms;
+
+static uint32_t uid_of(int n) { return nodes[n].esig.UNIID1; }
+
+static void add_keybit(int x, int y, int rot)
+{
+ node_t *k = &nodes[n_nodes];
+ memset(k, 0, sizeof(*k));
+ k->kind = KEYBIT;
+ k->idx = n_nodes;
+ k->placed = 1;
+ k->x = x; k->y = y; k->rot = rot;
+ double f = 48e6 * (1 + 0.02 * (frand() - 0.5));
+ k->ps_per_tick = 8e12 / f;
+ k->op_ps = (int64_t)(op_cycles * 1e12 / f);
+ k->spin_ps = (int64_t)(spin_cycles * 1e12 / f);
+ k->pass_ps = (int64_t)((op_cycles + pass_cycles) * 1e12 / f);
+ k->esig.UNIID1 = 0x1560AB00u + (uint32_t)n_nodes * 0x01000011u;
+ k->esig.UNIID2 = rnd();
+ k->esig.UNIID3 = 0xFFFFFFFFu;
+ memset(k->cfg, 0xFF, sizeof(k->cfg));
+ n_nodes++;
+}
+
+static void reset_world(void)
+{
+ memset(nodes, 0, sizeof(nodes));
+ n_nodes = 1;
+ node_t *kw = &nodes[0];
+ kw->kind = KEYWORD;
+ kw->powered = kw->runnable = 1;
+ kw->out[0] = kw->out[1] = 1;
+ start_coroutine(kw);
+ n_reports = n_presses = 0;
+ shorts = fights = 0;
+ sim_t = 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Metrics */
+
+static int cmp64(const void *a, const void *b)
+{
+ int64_t x = *(const int64_t *)a, y = *(const int64_t *)b;
+ return x < y ? -1 : x > y;
+}
+
+typedef struct { int n, lost, extra; double p50, p99, max; } lat_t;
+
+/* Latency of each press (down) per depth bucket. A press is lost if no
+ * down report for its Keybit lands between its press and the next one's. */
+static void measure(lat_t *by_depth, int max_depth, const int *depth_of)
+{
+ static int64_t buf[16][LOG_MAX / 8];
+ int nb[16] = { 0 };
+ for (int d = 0; d <= max_depth; d++) memset(&by_depth[d], 0, sizeof(lat_t));
+ for (int i = 0; i < n_presses; i++) {
+ press_t *p = &presses[i];
+ int64_t until = INT64_MAX;
+ for (int j = i + 1; j < n_presses; j++) if (presses[j].node == p->node) { until = presses[j].down; break; }
+ int d = depth_of[p->node];
+ if (d > max_depth) d = max_depth;
+ by_depth[d].n++;
+ int64_t got = -1;
+ for (int r = 0; r < n_reports; r++)
+ if (reports[r].uid == uid_of(p->node) && reports[r].down && reports[r].t >= p->down && reports[r].t < until) {
+ got = reports[r].t; break;
+ }
+ if (got < 0) by_depth[d].lost++;
+ else if (nb[d] < LOG_MAX / 8) buf[d][nb[d]++] = got - p->down;
+ }
+ for (int d = 0; d <= max_depth; d++) {
+ if (!nb[d]) continue;
+ qsort(buf[d], nb[d], sizeof(int64_t), cmp64);
+ by_depth[d].p50 = buf[d][nb[d] / 2] / 1e9;
+ by_depth[d].p99 = buf[d][(nb[d] * 99) / 100] / 1e9;
+ by_depth[d].max = buf[d][nb[d] - 1] / 1e9;
+ }
+}
+
+/* Every placed, powered Keybit known to the Keyword. */
+static int all_present(void)
+{
+ for (int n = 1; n < n_nodes; n++)
+ if (nodes[n].powered && !kw_present(uid_of(n))) return 0;
+ return 1;
+}
+
+static int64_t wait_present(int64_t limit)
+{
+ int64_t t0 = sim_t;
+ while (sim_t - t0 < limit) {
+ run_until(sim_t + 50 * PS_MS);
+ if (all_present()) return sim_t - t0;
+ }
+ return -1;
+}
+
+/* Taps on random powered Keybits: hold hold_ms (+ up to 50%), gap gap_ms (+ up to 100%). */
+static void type_keys(int count, int hold_ms, int gap_ms)
+{
+ for (int i = 0; i < count; i++) {
+ int n;
+ do n = 1 + (int)(rnd() % (unsigned)(n_nodes - 1)); while (!nodes[n].powered);
+ int64_t hold = (int64_t)(hold_ms * (1 + 0.5 * frand()) * PS_MS);
+ int64_t gap = (int64_t)(gap_ms * (1 + frand()) * PS_MS);
+ nodes[n].button = 1;
+ presses[n_presses++] = (press_t){ n, sim_t, sim_t + hold };
+ run_until(sim_t + hold);
+ nodes[n].button = 0;
+ run_until(sim_t + gap);
+ }
+ run_until(sim_t + 200 * PS_MS); /* let the last ones arrive */
+}
+
+static void print_diag(void)
+{
+ static const char S[4] = { 'S', 'E', 'W', 'N' };
+ for (int n = 1; n < n_nodes; n++) {
+ if (!nodes[n].powered) continue;
+ const uint8_t *d = nodes[n].diag();
+ printf(" kb%-2d (%2d,%2d) r%d depth=%d q=%d", n, nodes[n].x, nodes[n].y, nodes[n].rot, nodes[n].depth(), nodes[n].qcount());
+ for (int s = 0; s < 4; s++)
+ if (d[s * 8] || d[s * 8 + 5])
+ printf(" %c req=%d ok=%d noack=%d abort=%d bad=%d served=%d fail=%d", S[s], d[s * 8], d[s * 8 + 1],
+ d[s * 8 + 2], d[s * 8 + 3], d[s * 8 + 4], d[s * 8 + 5], d[s * 8 + 6]);
+ printf("\n");
+ }
+}
+
+/* ------------------------------------------------------------------------ */
+/* Scenarios */
+
+static int fails;
+#define CHECK(c, ...) do { if (!(c)) { fails++; printf(" FAIL: " __VA_ARGS__); printf("\n"); } } while (0)
+
+static void report_latency(const char *name, int max_depth)
+{
+ int depth_of[MAXN];
+ for (int n = 1; n < n_nodes; n++) depth_of[n] = nodes[n].powered ? nodes[n].depth() : 0;
+ lat_t lat[16];
+ measure(lat, max_depth, depth_of);
+ int lost = 0;
+ for (int d = 1; d <= max_depth; d++) {
+ if (!lat[d].n) continue;
+ printf(" %s depth %d: n=%d lost=%d p50=%.1f ms p99=%.1f ms max=%.1f ms\n", name, d, lat[d].n, lat[d].lost,
+ lat[d].p50, lat[d].p99, lat[d].max);
+ lost += lat[d].lost;
+ }
+ CHECK(lost == 0, "%s: %d presses never reached the Keyword", name, lost);
+}
+
+/* A straight line out of the Keyword's right side, every Keybit the usual way round. */
+static void sc_chain(int len, int presses_n)
+{
+ printf("chain of %d\n", len);
+ reset_world();
+ for (int i = 1; i <= len; i++) add_keybit(i, 0, 0);
+ build();
+ int64_t t = wait_present(10000 * PS_MS);
+ printf(" all present after %.0f ms\n", t / 1e9);
+ CHECK(t >= 0, "not every Keybit showed up");
+ for (int n = 1; n < n_nodes; n++) CHECK(nodes[n].depth() == n, "kb%d depth %d, want %d", n, nodes[n].depth(), n);
+ type_keys(presses_n, 60, 120);
+ report_latency("tap", len);
+ print_diag();
+ printf(" shorts=%ld fights=%ld kb1 main loop pass %.1f us\n", shorts, fights,
+ btn_reads[1] ? (sim_t - nodes[1].t_boot) / 1e6 / btn_reads[1] : 0);
+}
+
+/* How often the first Keybit answers an address at each bus speed. */
+static void sc_speed(void)
+{
+ extern int kw_probe_left, kw_probe_acks;
+ extern int64_t kw_probe_half;
+ printf("speed (1 direct + 3 chained)\n");
+ reset_world();
+ for (int i = 1; i <= 4; i++) add_keybit(i, 0, 0);
+ build();
+ CHECK(wait_present(10000 * PS_MS) >= 0, "not every Keybit showed up");
+ static const int khz[] = { 20, 25, 28, 30, 32, 35, 40 };
+ for (int i = 0; i < 7; i++) {
+ kw_probe_half = PS_MS / khz[i] / 2;
+ kw_probe_acks = 0;
+ kw_probe_left = 20;
+ for (int g = 0; kw_probe_left > 0 && g < 500; g++) run_until(sim_t + 20 * PS_MS);
+ printf(" %d kHz: %d/20 acks\n", khz[i], kw_probe_acks);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 2) { fprintf(stderr, "usage: %s node.bundle [scenario]\n", argv[0]); return 2; }
+ strncpy(bundle, argv[1], sizeof(bundle) - 1);
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/keybit-sim-XXXXXX");
+ if (!mkdtemp(tmpdir)) return 1;
+ const char *e;
+ if ((e = getenv("KW_KEYS_MS"))) kw_keys_ms = atoi(e);
+ if ((e = getenv("KW_RELAY_MS"))) kw_relay_ms = atoi(e);
+ if ((e = getenv("KW_SCAN_MS"))) kw_scan_ms = atoi(e);
+ if ((e = getenv("OP_CYCLES"))) op_cycles = atoi(e);
+ if ((e = getenv("SPIN_CYCLES"))) spin_cycles = atoi(e);
+ if ((e = getenv("PASS_CYCLES"))) pass_cycles = atoi(e);
+ if ((e = getenv("KW_HALF_US"))) { extern int64_t kw_t_low, kw_t_high; kw_t_low = kw_t_high = (int64_t)(atof(e) * PS_US); }
+ if ((e = getenv("SEED"))) seed = (unsigned)atoi(e);
+ const char *sc = argc > 2 ? argv[2] : "chain";
+ int len = argc > 3 ? atoi(argv[3]) : 5;
+ int np = argc > 4 ? atoi(argv[4]) : 40;
+ if (!strcmp(sc, "chain")) sc_chain(len, np);
+ if (!strcmp(sc, "speed")) sc_speed();
+ char cmd[600];
+ snprintf(cmd, sizeof(cmd), "rm -rf '%s'", tmpdir);
+ system(cmd);
+ printf(fails ? "FAILED (%d)\n" : "ok\n", fails);
+ return fails != 0;
+}
diff --git a/firmware/sim/sim.h b/firmware/sim/sim.h
new file mode 100644
index 0000000..5571cc6
--- /dev/null
+++ b/firmware/sim/sim.h
@@ -0,0 +1,40 @@
+/*
+ * Host simulator interface: what the Keybit and Keyword code sees instead
+ * of registers. Every call costs the calling node some time on its own
+ * clock, and the scheduler keeps all nodes in step to within one call.
+ */
+#ifndef SIM_H
+#define SIM_H
+
+#include <stdint.h>
+
+typedef struct { volatile uint32_t CNT; } sim_systick_t;
+typedef struct { volatile uint32_t INDR; } sim_gpio_t;
+typedef struct { uint32_t UNIID1, UNIID2, UNIID3; } sim_esig_t;
+typedef struct { volatile uint32_t KEYR, MODEKEYR, CTLR, STATR; volatile uintptr_t ADDR; } sim_flash_t;
+
+sim_systick_t *sim_systick(void);
+sim_gpio_t *sim_gpio(uint8_t pin);
+sim_esig_t *sim_esig(void);
+sim_flash_t *sim_flash(void);
+void *sim_config_page(void);
+
+int sim_pin_read(uint8_t pin);
+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);
+
+/* Keyword side */
+int64_t sim_now(void); /* ps */
+void sim_sleep(int64_t ps);
+void sim_kw_drive(int pin, int high); /* pin 0 SCL, 1 SDA, open drain */
+int sim_kw_read(int pin);
+void kw_main(void);
+void kw_report(uint32_t uid, int down); /* the keys task saw this key change */
+
+#define PS_NS 1000LL
+#define PS_US 1000000LL
+#define PS_MS 1000000000LL
+
+#endif
diff --git a/firmware/sim/stm32wbxx_hal.h b/firmware/sim/stm32wbxx_hal.h
new file mode 100644
index 0000000..27113a7
--- /dev/null
+++ b/firmware/sim/stm32wbxx_hal.h
@@ -0,0 +1,42 @@
+/* Stand-in for the STM32 HAL so keybit_bus.c builds on the host. */
+#ifndef STM32WBXX_HAL_SIM_H
+#define STM32WBXX_HAL_SIM_H
+
+#include <stdint.h>
+#include <stddef.h>
+#include "sim.h"
+
+typedef enum { HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT } HAL_StatusTypeDef;
+typedef enum { GPIO_PIN_RESET, GPIO_PIN_SET } GPIO_PinState;
+typedef enum { HAL_I2C_STATE_RESET = 0, HAL_I2C_STATE_READY = 0x20 } HAL_I2C_StateTypeDef;
+typedef struct { int unused; } I2C_HandleTypeDef;
+typedef struct { int unused; } GPIO_TypeDef;
+typedef struct { uint32_t Pin, Mode, Pull, Speed; } GPIO_InitTypeDef;
+typedef struct { volatile uint32_t KR; } IWDG_TypeDef;
+
+extern GPIO_TypeDef sim_gpiob;
+extern IWDG_TypeDef sim_iwdg;
+#define GPIOB (&sim_gpiob)
+#define IWDG (&sim_iwdg)
+#define GPIO_PIN_13 (1u << 13)
+#define GPIO_PIN_14 (1u << 14)
+#define GPIO_MODE_OUTPUT_OD 0x11u
+#define GPIO_NOPULL 0u
+#define GPIO_SPEED_FREQ_LOW 0u
+#define I2C_MEMADD_SIZE_8BIT 1u
+
+uint32_t HAL_GetTick(void);
+void HAL_Delay(uint32_t ms);
+GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *port, uint16_t pin);
+void HAL_GPIO_WritePin(GPIO_TypeDef *port, uint16_t pin, GPIO_PinState s);
+void HAL_GPIO_Init(GPIO_TypeDef *port, GPIO_InitTypeDef *init);
+HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *h);
+HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *h);
+HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *h);
+HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *h, uint16_t addr, uint32_t trials, uint32_t timeout);
+HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *h, uint16_t addr, uint16_t reg, uint16_t regsize,
+ uint8_t *buf, uint16_t len, uint32_t timeout);
+HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *h, uint16_t addr, uint16_t reg, uint16_t regsize,
+ uint8_t *buf, uint16_t len, uint32_t timeout);
+
+#endif