A truly modular keyboard
C
sim: build and run on Linux x86_64 too
Nathane2888aed2f7b
diff --git a/firmware/sim/Makefile b/firmware/sim/Makefileindex 94baca8..6c0556a 100644--- a/firmware/sim/Makefile+++ b/firmware/sim/Makefile@@ -1,10 +1,24 @@# 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.+# the real firmware sources. Apple arm64 or Linux x86_64. `make test` runs+# the checks.CC ?= ccCFLAGS ?= -O2 -g -Wall -Wno-unused-functionKEYBIT := ../keybitKEYWORD := ../keyword/Core+ifeq ($(shell uname -s),Darwin)+CTX := ctx_arm64.S+BUNDLE := -bundle -undefined dynamic_lookup+SIM_LD :=+SED_I := sed -i ''+else+CTX := ctx_x86_64.S+# Each Keybit copy keeps its own globals: bind its symbols to itself.+BUNDLE := -shared -fPIC -Wl,-Bsymbolic+SIM_LD := -rdynamic -ldl -lm+SED_I := sed -i+endif+all : sim node.bundle# Keyword timing experiments: make sim KW_DEFS="-DKEYS_MS=10"@@ -16,15 +30,15 @@ V10_REV := 6039864node_v10.bundle : node.c ch32fun.h sim.hrm -rf build_v10 && mkdir -p build_v10for 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 -undefined dynamic_lookup -o $@ node.c build_v10/link.c build_v10/soft_i2c.c+ $(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.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 -undefined dynamic_lookup -o $@ node.c $(KEYBIT)/link.c $(KEYBIT)/soft_i2c.c+ $(CC) $(CFLAGS) -I. -I$(KEYBIT) $(BUNDLE) -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) $(KW_DEFS) -I. -I$(KEYWORD)/Inc -o $@ sim.c kw.c ctx_arm64.S+sim : 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)test : all./sim node.bundle alldiff --git a/firmware/sim/ctx_x86_64.S b/firmware/sim/ctx_x86_64.Snew file mode 100644index 0000000..28f0dd0--- /dev/null+++ b/firmware/sim/ctx_x86_64.S@@ -0,0 +1,29 @@+// Coroutine switch for the simulator (Linux x86_64, System V).+// void ctx_switch(void **save_sp, void *to_sp)+ .text+ .globl ctx_switch+ctx_switch:+ push %rbp+ push %rbx+ push %r12+ push %r13+ push %r14+ push %r15+ mov %rsp, (%rdi)+ mov %rsi, %rsp+ pop %r15+ pop %r14+ pop %r13+ pop %r12+ pop %rbx+ pop %rbp+ ret++// First switch into a new coroutine lands here with rbx = its node.+ .globl ctx_boot+ctx_boot:+ mov %rbx, %rdi+ call node_entry+ ud2++ .section .note.GNU-stack,"",@progbitsdiff --git a/firmware/sim/sim.c b/firmware/sim/sim.cindex 4e1b75c..206b64b 100644--- a/firmware/sim/sim.c+++ b/firmware/sim/sim.c@@ -19,7 +19,7 @@* of correct contacts reaches the Keyword; it boots from scratch whenever* it gains power.*- * Apple arm64 only (ctx_arm64.S).+ * Apple arm64 (ctx_arm64.S) or Linux x86_64 (ctx_x86_64.S).*/#include <dlfcn.h>#include <math.h>@@ -172,10 +172,19 @@ static void run_until(int64_t t)static void start_coroutine(node_t *n){if (!n->stack) n->stack = malloc(STACK);+#if defined(__x86_64__)+ /* r15 r14 r13 r12 rbx rbp, then the return address; after ctx_switch's+ ret the stack is 16-byte aligned for ctx_boot's call */+ void **sp = (void **)(n->stack + STACK - 72);+ memset(sp, 0, 72);+ sp[4] = n; /* rbx */+ sp[6] = (void *)ctx_boot;+#elsevoid **sp = (void **)(n->stack + STACK - 160);memset(sp, 0, 160);sp[0] = n; /* x19 */sp[11] = (void *)ctx_boot; /* x30 */+#endifn->sp = sp;}