Capsule blocks steps 3+4 for v4: capsules are placed in RAM and load their lowest block

Captain Bob ruled: Block n lands at n-R; the lowest block is
interpreted; chaining is the developer's; together, v4 first.
- v4_blocks_place_capsule copies each Block n into RAM block n - R as 16
  lines of 64, space-padded, in any file order. A later block of the
  same number overwrites. It refuses a block below R.
- The mesh's load(), its capsule reader (CAPSULE-OPEN/(LINES), so a
  child's forth79.4th via BIRTH too), boot.c and test_host_unit's model
  each place a capsule, then interpret "<lowest> LOAD".
- mkdictionary.py skips --> when it types capsules at the hosted prompt.
- The v4 capsules are renumbered into R..2R-1, landing at RAM 1900 and
  up, away from POST's blocks 0-5. Each is chained with -->, and
  centre.4th's gap at 8103 is closed.

Test first:
- test_host_blocks.c gained 6 placement checks; it failed to build
  before.
- test_mesh_boot.c checks forth79's lowest block in RAM block 1900; it
  failed before.

After: POST is 990/990; all 52 tests at 64 bits, test-32, hosted-check
and ASan/UBSan pass; the amd64 kernel builds; the dictionary has 404
words.

Logs are in logs/20261118-capsule-place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-18 20:33:21 +00:00
parent bcfca9fa19
commit 6dcfbe6740
27 changed files with 1399 additions and 97 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ $(BINDIR)/san-v3blocks.o: $(V3_BLOCK_SRCS) $(wildcard $(ROOT_DIR)/v3/include/*.h
# flags: its warnings, its sanitizers, its cell width and node size.
# So are POST's runner and its cases (system/post.c, system/post_cases.c;
# docs/v4.0.0/NUCLEUS.md 6.3), which are the kernel's too.
blocks_src = $(if $(call uses_blocks,$(1)),$(BLOCKS_SRC) $(HERE)/system/post.c $(HERE)/system/post_cases.c)
blocks_src = $(if $(call uses_blocks,$(1)),$(BLOCKS_SRC) $(HERE)/system/post.c $(HERE)/system/post_cases.c $(ROOT)/kernel/src/capsule/capsule_blocks.c)
blocks_objs = $(if $(call uses_blocks,$(1)),$(BINDIR)/v3blocks.o)
# the fault dump of a node that stopped (system/dump.c; plan 2026-11-17-emergency-diagnostic-build): the
# kernel's, linked with its own test
+11
View File
@@ -85,4 +85,15 @@ void v4_blocks_init(v4_blocks *w, v4_cell window);
* its port, when this is called. */
int v4_blocks_serve(v4_blocks *w, v4_node *n, v4_cell request);
/* A CAPSULE OF FORTH BLOCKS, PLACED (docs/v4.0.0/plans/2026-11-18-capsule-blocks.md). Each
* `Block n` of the capsule text from p to end is copied into the kernel's RAM block n - R, where
* R is the RAM block count (BLK_RAM_USER_BLOCKS, v3/include/blk_layout.h), as 16 lines of 64
* characters padded with spaces -- in whatever order the file has them; a later block of the same
* number overwrites an earlier one. Where a block lands is the capsule's author's affair: nothing
* checks it but that n is at least R. The lowest block placed is in *lowest, which the loader
* interprets (`LOAD`); what runs after it is the capsule's own (`-->`, `LOAD`, `THRU`). Returns
* how many blocks were placed, or -1: a block below R, a line over 64 characters or more than 16
* lines, or a block storage would not give, in which case some may have been placed. */
int v4_blocks_place_capsule(const uint8_t *p, const uint8_t *end, uint32_t *lowest);
#endif /* V4_BLOCKS_H */
+40
View File
@@ -7,6 +7,7 @@
* through v3's own calls.
*/
#include "v4/blocks.h"
#include "starkernel/capsule_blocks.h"
#include "block_subsystem.h"
static void let_go(v4_blocks *w)
@@ -162,3 +163,42 @@ int v4_blocks_serve(v4_blocks *w, v4_node *n, v4_cell request)
v4_dstack_push(&n->ds, (v4_cell)status);
return 1;
}
/* ---- a capsule of FORTH blocks, placed in RAM (blocks.h) ------------------------------ */
int v4_blocks_place_capsule(const uint8_t *p, const uint8_t *end, uint32_t *lowest)
{
uint8_t *b = 0;
uint32_t dest = 0;
unsigned line = 0;
int placed = 0;
while (p && p < end) {
const uint8_t *after, *nl;
uint32_t num;
unsigned len, i;
if (capsule_block_header(p, end, &num, &after)) {
if (b && blk_update(dest) != BLK_OK) return -1;
if (num < BLK_RAM_USER_BLOCKS) return -1;
dest = num - BLK_RAM_USER_BLOCKS;
b = blk_get_buffer(dest, 1);
if (!b) return -1;
for (i = 0; i < V4_BLOCK_CELLS * 4u; i++) b[i] = ' ';
if (placed == 0 || dest < *lowest) *lowest = dest;
placed++; line = 0; p = after;
continue;
}
for (nl = p; nl < end && *nl != '\n'; nl++) { }
len = (unsigned)(nl - p);
if (len && p[len - 1] == '\r') len--;
if (b) {
if (len > 64u || line >= 16u) return -1;
for (i = 0; i < len; i++) b[64u * line + i] = p[i];
line++;
}
p = nl < end ? nl + 1 : end;
}
if (b && blk_update(dest) != BLK_OK) return -1;
return placed;
}
+25 -41
View File
@@ -36,7 +36,6 @@ static const char *const boot_capsules[] = {
#define V4_POST_AT_BOOT 1
#endif
#define LINE_MAX 1025u /* a line is at most 1024 characters, a block */
#define STEP_LIMIT 4000000000ULL /* instruction words one line may take */
/* ---- printing ------------------------------------------------------------ */
@@ -356,55 +355,40 @@ static const uint8_t *open_capsule(const v4_boot *b, const char *name, const Cap
}
static void say_where(const v4_boot *b, const char *name, uint32_t block, uint32_t line)
{
say(b, "\nV4: capsule "); say(b, name);
say(b, " block "); say_dec(b, block);
say(b, " line "); say_dec(b, line);
}
static int load_capsule(const v4_boot *b, const char *name)
{
const CapsuleDesc *cap;
const uint8_t *p, *end;
uint32_t block = 0, line = 0;
int in_block = 0;
const uint8_t *p;
uint32_t lowest = 0, v;
int placed;
p = open_capsule(b, name, &cap);
if (!p) return 0;
end = p + cap->length;
while (p < end) {
const uint8_t *after, *nl;
uint32_t num;
unsigned len, i;
char text[LINE_MAX];
if (capsule_block_header(p, end, &num, &after)) {
block = num; line = 0; in_block = 1; p = after;
continue;
/* Its blocks are placed in RAM, block n at n - R, and the lowest is loaded: what runs after it is
* the capsule's own (docs/v4.0.0/plans/2026-11-18-capsule-blocks.md). */
placed = v4_blocks_place_capsule(p, p + cap->length, &lowest);
if (placed < 0) { say(b, "V4: "); say(b, name); say(b, " could not be placed in RAM\n"); return 0; }
if (placed > 0) {
char text[24], d[12];
unsigned len = 0, nd = 0;
const char *s;
int how;
v = lowest;
do { d[nd++] = (char)('0' + v % 10u); v /= 10u; } while (v);
while (nd) text[len++] = d[--nd];
for (s = " LOAD"; *s; s++) text[len++] = *s;
how = v4_boot_line(b, text, len);
if (how != V4_TEXT_COMPLETED) {
say(b, "V4: "); say(b, name);
say(b, how == V4_TEXT_ERROR ? " ended in an error: "
: how == V4_TEXT_QUIT ? " ended with QUIT: "
: how == V4_BOOT_LINE_NO_INPUT ? " reads the keyboard: "
: " stopped the node: ");
b->out(text, len); say(b, "\n");
return 0;
}
for (nl = p; nl < end && *nl != '\n'; nl++) { }
len = (unsigned)(nl - p);
if (len && p[len - 1] == '\r') len--;
if (in_block) {
line++;
if (len >= LINE_MAX) { say_where(b, name, block, line); say(b, " is too long\n"); return 0; }
if (len) {
for (i = 0; i < len; i++) text[i] = (char)p[i];
int how = v4_boot_line(b, text, len);
if (how != V4_TEXT_COMPLETED) {
say_where(b, name, block, line);
say(b, how == V4_TEXT_ERROR ? " ended in an error: "
: how == V4_TEXT_QUIT ? " ended with QUIT: "
: how == V4_BOOT_LINE_NO_INPUT ? " reads the keyboard: "
: " stopped the node: ");
b->out(text, len); say(b, "\n");
return 0;
}
}
}
p = (nl < end) ? nl + 1 : end;
}
say(b, "PARITY:V4_CAPSULE name="); say(b, name);
+29 -4
View File
@@ -65,6 +65,25 @@ static void put_cell(void *self, v4_cell w)
typedef struct { const uint8_t *p, *end; int in_block; } reader;
static reader reads[MAX_PLACES];
/* A capsule of FORTH blocks is placed in RAM, block n at n - R, and what is told to the node is the one
* line that loads its lowest block (docs/v4.0.0/plans/2026-11-18-capsule-blocks.md): the rest is the
* capsule's own (`-->`, `LOAD`, `THRU`). The line goes in text; its length, 0 when the capsule had no
* block, or -1 when it could not be placed. */
static int placed_line(const uint8_t *p, uint32_t length, char *text)
{
uint32_t lowest = 0, v;
char d[12];
int placed = v4_blocks_place_capsule(p, p + length, &lowest), at = 0, nd = 0;
const char *s;
if (placed <= 0) return placed;
v = lowest;
do { d[nd++] = (char)('0' + v % 10u); v /= 10u; } while (v);
while (nd) text[at++] = d[--nd];
for (s = " LOAD"; *s; s++) text[at++] = *s;
return at;
}
static char load_text[MAX_PLACES][24]; /* each place's one line, while its reader gives it */
static int open_cb(void *self, unsigned asker, const char *name, unsigned len)
{
char cname[V4_MANAGE_LINE + 1u];
@@ -84,7 +103,13 @@ static int open_cb(void *self, unsigned asker, const char *name, unsigned len)
p = v4_boot_capsule(&bt, cname, &length, &id, &hash);
if (!p) return 0;
the->reading_nucleus[asker] = 0;
reads[asker].p = p; reads[asker].end = p + length; reads[asker].in_block = 0;
{
int len = placed_line(p, length, load_text[asker]);
if (len < 0) return 0;
reads[asker].p = (const uint8_t *)load_text[asker];
reads[asker].end = reads[asker].p + len;
reads[asker].in_block = 1; /* the one line is the reader's: no header before it */
}
return 1;
}
static int cell_cb(void *self, unsigned asker, v4_cell *w)
@@ -1225,7 +1250,6 @@ static void post_say(void *self, const char *text, unsigned len) { ((v4_mesh *)s
/* Every line of a capsule of FORTH blocks, told to Hera; then its parity line, as v4_boot_run prints it. */
static int load(v4_mesh *ms, const char *name)
{
static reader r;
char text[V4_MSG_MAX_CHARS + 1u];
uint32_t length;
uint64_t id, hash;
@@ -1233,8 +1257,9 @@ static int load(v4_mesh *ms, const char *name)
int n;
v4_fabric_node *h = v4_fabric_node_at(&ms->f, ms->hera_place);
if (!p) return 0;
r.p = p; r.end = p + length; r.in_block = 0;
while ((n = next_line(&r, text, V4_MSG_MAX_CHARS)) >= 0) {
n = placed_line(p, length, text);
if (n < 0) { say("V4: "); say(name); say(" could not be placed in RAM\n"); return 0; }
if (n > 0) {
int how = v4_mesh_tell(ms, V4_MESH_HERA, text, (unsigned)n);
if (how != V4_TEXT_COMPLETED) {
say("V4: "); say(name); say(" did not complete: "); ms->out(text, (unsigned)n); say("\n");
+30
View File
@@ -217,6 +217,36 @@ int main(void)
CHECK(!v4_blocks_serve(&w, &n, 5) && !v4_blocks_serve(&w, &n, 0) && !v4_blocks_serve(&w, &n, -6) && v4_dstack_pop(&n.ds) == 22 && v4_dstack_pop(&n.ds) == 11,
"any other request is not served here, and the stack is as it was");
/* ---- a capsule placed in RAM (docs/v4.0.0/plans/2026-11-18-capsule-blocks.md) ----
* Each `Block n` goes to RAM block n - R (R the RAM block count, blk_layout.h), as 16
* lines of 64 characters padded with spaces, whatever its order in the file; a later
* block of the same number overwrites; the lowest placed is given back. */
{
static const char cap[] =
"Block 2050\n( two )\n: TWO 2 ;\n"
"Block 2049\n( one, written first in RAM order )\n"
"Block 2051\n( first )\n"
"Block 2051\n( second, overwrites )\n";
uint32_t lowest = 0;
int placed = v4_blocks_place_capsule((const uint8_t *)cap, (const uint8_t *)cap + sizeof cap - 1u, &lowest);
const uint8_t *b1 = blk_get_buffer(1, 0), *b2 = blk_get_buffer(2, 0), *b3 = blk_get_buffer(3, 0);
unsigned k, spaces = 1;
CHECK(placed == 4 && lowest == 1, "four blocks placed, the lowest at RAM block 1 (%d, %u)", placed, (unsigned)lowest);
CHECK(b2 && memcmp(b2, "( two )", 7) == 0 && memcmp(b2 + 64, ": TWO 2 ;", 9) == 0,
"block 2050 is at RAM block 2: its lines at 0 and 64");
for (k = 7; b2 && k < 64; k++) if (b2[k] != ' ') spaces = 0;
for (k = 128 + 0; b2 && k < 1024; k++) if (b2[k] != ' ') spaces = 0;
CHECK(spaces, " each line padded with spaces to 64, and the rest of the block spaces");
CHECK(b1 && memcmp(b1, "( one, written first in RAM order )", 35) == 0, "block 2049 is at RAM block 1, whatever the file's order");
CHECK(b3 && memcmp(b3, "( second, overwrites )", 22) == 0, "a block of the same number overwrites the first");
}
{
static const char low[] = "Block 2047\n( in the RAM itself )\n";
uint32_t lowest = 99;
CHECK(v4_blocks_place_capsule((const uint8_t *)low, (const uint8_t *)low + sizeof low - 1u, &lowest) == -1,
"a block below the RAM count is refused");
}
printf(" %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}
+25 -5
View File
@@ -100,6 +100,26 @@ static int read_file(reader *r, const char *name)
r->at = 0; r->in_block = 0; r->is_f18 = 0; r->cells_at = 0;
return 1;
}
/* A capsule of FORTH blocks, as the kernel takes it in (v4/system/mesh.c; docs/v4.0.0/plans/
* 2026-11-18-capsule-blocks.md): its blocks placed in RAM, block n at n - R, and the reader left
* with the one line that loads the lowest. 0 if it could not be read or placed. */
static int place_file(reader *r, const char *name)
{
uint32_t lowest = 0;
int placed;
char one[40];
if (!read_file(r, name)) return 0;
placed = v4_blocks_place_capsule((const uint8_t *)r->text, (const uint8_t *)r->text + r->len, &lowest);
if (placed < 0) return 0;
snprintf(one, sizeof one, placed ? "Block 1\n%u LOAD\n" : "\n", (unsigned)lowest);
free(r->text);
r->len = (long)strlen(one);
r->text = (char *)malloc((size_t)r->len + 1);
if (!r->text) return 0;
memcpy(r->text, one, (size_t)r->len + 1);
r->at = 0; r->in_block = 0;
return 1;
}
/* the next line of a block that has anything on it: its length, or -1 */
static int next_line(reader *r, char *text)
{
@@ -175,8 +195,8 @@ static int host_open(void *self, unsigned asker, const char *name, unsigned len)
reader *r = &reads[asker];
(void)self;
if (len == 17 && memcmp(name, "v4:nucleus-64.f18", 17) == 0) { r->is_f18 = 1; r->cells_at = 0; return 1; }
if (len == 14 && memcmp(name, "v4:forth79.4th", 14) == 0) return read_file(r, "forth79.4th");
if (len == 13 && memcmp(name, "v4:centre.4th", 13) == 0) return read_file(r, "centre.4th");
if (len == 14 && memcmp(name, "v4:forth79.4th", 14) == 0) return place_file(r, "forth79.4th");
if (len == 13 && memcmp(name, "v4:centre.4th", 13) == 0) return place_file(r, "centre.4th");
return 0;
}
static int host_cell(void *self, unsigned asker, v4_cell *w)
@@ -331,15 +351,15 @@ static void post_say(void *self, const char *text, unsigned len)
if (post_said_len + len < sizeof post_said) { memcpy(post_said + post_said_len, text, len); post_said_len += len; post_said[post_said_len] = 0; }
}
/* Every line of a capsule of FORTH blocks, told to a node from the console:
* how many lines did not complete. */
/* A capsule of FORTH blocks, placed in RAM and its lowest block loaded, told to a node from the
* console: how many lines did not complete. */
static unsigned load(v4_cell node, const char *file)
{
static reader r;
char line[V4_MANAGE_LINE + 1];
unsigned bad = 0;
int n;
if (!read_file(&r, file)) return 1;
if (!place_file(&r, file)) return 1;
while ((n = next_line(&r, line)) >= 0) {
line[n] = 0;
(void)tell(node, line);
+4
View File
@@ -143,6 +143,10 @@ int main(void)
/* ---- lines, as messages ---- */
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "7 ") == 0, "a line to Hera: \"%s\" (%d)", r, how);
/* the capsules were placed in RAM, block n at n - R, and loaded from there (capsule blocks plan, step 4):
* forth79.4th's lowest block, 3948, is RAM block 1900 */
r = line(V4_MESH_HERA, "1900 BLOCK 16 TYPE", &how);
CHECK(how == V4_TEXT_COMPLETED && strncmp(r, "( forth79.4th --", 16) == 0, "forth79.4th's lowest block is in RAM block 1900: \"%.20s\" (%d)", r, how);
/* starshipos.4th, a system capsule loaded before POST (MACHINE.md 3, step 3; ruled 2026-11-18) */
r = line(V4_MESH_HERA, "STARSHIPOS S\" ok\" DOE", &how); /* COLD leaves FORTH alone in the search; init.4th will set it */
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "ok") == 0, "starshipos.4th is loaded: in STARSHIPOS, DOE types its name: \"%s\" (%d)", r, how);
+3 -1
View File
@@ -44,7 +44,9 @@ def live_words():
feed = []
for path in kernel_capsules():
for l in open(os.path.join(ROOT, path)):
if not re.match(r"^Block \d+\s*$", l):
# every block, in order, typed at the prompt: a block's header is not FORTH, and its `-->`
# (which chains to the next block when the kernel LOADs it) is an error at the terminal
if not re.match(r"^Block \d+\s*$", l) and l.strip() != "-->":
feed.append(l.rstrip("\n"))
feed.append('." [[WORDS]]" CR WORDS')
out = subprocess.run([HOSTED], input="\n".join(feed) + "\n", capture_output=True, text=True, timeout=120).stdout