Files
LithosAnanake/v4/src/capsule.c
T
rajamesandClaude Opus 5.5 42610e844f feat(v4.0.0): the nucleus is a capsule, sent to a node born empty
MESH.md step 2.  A capsule of F18 code is the words a neighbour writes to a
node's port: for each stretch of memory, "@p a! @p push", the address and
count, "@p !+ unext" and the words; then a jump to the start.  A node born
empty executes that from its port, so it needs nothing in it beforehand.

- capsule.h/.c: v4_capsule_write, any node's memory as such a capsule
- mkimage writes the nucleus so, to capsules/v4/nucleus-64.f18, and the
  addresses a host needs as a C file; the memory image is no longer linked
  into either product
- mkcapsule is unchanged: the nucleus capsule is a built file kept under
  capsules/, as BLOCK_MAP.md is, and is baked, hashed and signed with the
  rest
- boot: the node is born empty (v4_image_born); the nucleus capsule is
  found, its hash and signature checked, and given to the node a word at a
  time as it reads its port; PARITY:V4_NUCLEUS carries its name and hash

Verified: test_fabric.c (59 checks, both widths, and under ASan and UBSan):
a memory with a programme and scattered words arrives word for word in an
empty node and runs.  The nucleus capsule rebuilds byte for byte.
hosted-check passes on three ISAs; clean qemu with STARFORTH_V4=1 on amd64,
aarch64 and riscv64 takes the nucleus in, passes POST (550 of 550) and
answers lines typed at each prompt (logs/20261006-102421, -102706,
-103048).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 10:33:15 -04:00

56 lines
1.8 KiB
C

/* capsule.c -- a capsule of F18 code. See capsule.h.
*
* Nothing here uses the C library.
*/
#include "v4/capsule.h"
#include "v4/iword.h"
#include "v4/opcode.h"
/* A stretch of zeros shorter than this, between words that are not zero, is
* sent with them: starting a new stretch costs four words. */
#define GAP 4u
static v4_cell words_of(unsigned a, unsigned b, unsigned c, unsigned d)
{
unsigned op[V4_SLOT_COUNT];
unsigned k;
op[0] = a; op[1] = b; op[2] = c; op[3] = d;
for (k = 4; k < V4_SLOT_COUNT; k++) op[k] = V4_OP_NOP;
return (v4_cell)v4_iword_assemble(op, 0);
}
/* `jump addr`, the jump in the first slot so that its address is as wide as
* an address can be */
static v4_cell jump_to(v4_cell addr)
{
v4_ucell mask = (v4_ucell)v4_iword_slot_mask(0);
v4_ucell word = (v4_ucell)words_of(V4_OP_JUMP, V4_OP_NOP, V4_OP_NOP, V4_OP_NOP);
return (v4_cell)((word & ~mask) | ((v4_ucell)addr & mask));
}
unsigned v4_capsule_write(const v4_node *n, v4_cell start,
void (*out)(void *self, v4_cell word), void *self)
{
unsigned k = 0, written = 0;
while (k < V4_NODE_WORDS) {
unsigned first, last, zeros, i;
if (n->mem[k] == 0) { k++; continue; }
first = last = k;
zeros = 0;
for (i = k + 1; i < V4_NODE_WORDS && zeros < GAP; i++) {
if (n->mem[i] != 0) { last = i; zeros = 0; } else zeros++;
}
out(self, words_of(V4_OP_FETCH_P, V4_OP_BANG_A, V4_OP_FETCH_P, V4_OP_PUSH));
out(self, (v4_cell)first);
out(self, (v4_cell)(last - first));
out(self, words_of(V4_OP_FETCH_P, V4_OP_STORE_INC, V4_OP_UNEXT, V4_OP_NOP));
for (i = first; i <= last; i++) out(self, n->mem[i]);
written += 4u + (last - first + 1u);
k = last + 1;
}
out(self, jump_to(start));
return written + 1u;
}