Files
LithosAnanke/v4/system/blocks.c
T
Claude 6dcfbe6740 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
2026-11-18 20:33:21 +00:00

205 lines
7.2 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* blocks.c -- the kernel serves a node's block requests. See blocks.h.
*
* This is the one file of v4's that includes v3's headers: every block goes
* 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)
{
unsigned i;
for (i = 0; i < V4_BLOCK_SLOTS; i++) { w->block[i] = 0; w->changed[i] = 0; }
w->next = 0;
w->last = -1;
}
void v4_blocks_init(v4_blocks *w, v4_cell window)
{
w->window = window;
w->epoch = blk_subsys_epoch();
let_go(w);
}
static v4_cell slot_addr(const v4_blocks *w, unsigned s)
{
return w->window + (v4_cell)(s * V4_BLOCK_CELLS);
}
/* The block in slot s is written. Whether or not storage takes it, it is
* no longer marked. */
static int write_slot(v4_blocks *w, v4_node *n, unsigned s)
{
static uint8_t was[V4_BLOCK_CELLS * 4u]; /* the block as the kernel had it, in case it is refused */
uint8_t *b = blk_get_buffer(w->block[s], 1);
v4_cell at = slot_addr(w, s);
unsigned i;
w->changed[s] = 0;
if (!b) return V4_BLOCK_REFUSED;
for (i = 0; i < V4_BLOCK_CELLS * 4u; i++) was[i] = b[i];
for (i = 0; i < V4_BLOCK_CELLS; i++) {
v4_ucell c = (v4_ucell)n->mem[at + (v4_cell)i];
b[4u * i] = (uint8_t)(c & 0xFFu);
b[4u * i + 1u] = (uint8_t)((c >> 8) & 0xFFu);
b[4u * i + 2u] = (uint8_t)((c >> 16) & 0xFFu);
b[4u * i + 3u] = (uint8_t)((c >> 24) & 0xFFu);
}
if (blk_update(w->block[s]) == BLK_OK && blk_flush(w->block[s]) == BLK_OK) return V4_BLOCK_OK;
/* Storage would not have it. The node is told so, and the kernel does
* not keep what was refused: its own copy is put back as it was, so
* that the block reads as before and what was refused is not written
* later. The buffer is asked for again, since the failed write may
* have moved it. */
b = blk_get_buffer(w->block[s], 1);
if (b) {
for (i = 0; i < V4_BLOCK_CELLS * 4u; i++) b[i] = was[i];
(void)blk_update(w->block[s]);
}
return V4_BLOCK_REFUSED;
}
/* A slot to put a block in: an empty one, or one whose block is not marked,
* or, with every one marked, the next in turn, its block written first.
* *status is not V4_BLOCK_OK if that write was refused; the slot is empty
* all the same. */
static unsigned take_slot(v4_blocks *w, v4_node *n, int *status)
{
unsigned i, s;
*status = V4_BLOCK_OK;
for (i = 0; i < V4_BLOCK_SLOTS; i++) if (w->block[i] == 0) return i;
for (i = 0; i < V4_BLOCK_SLOTS; i++) {
s = (w->next + i) % V4_BLOCK_SLOTS;
if (!w->changed[s]) break;
}
if (i == V4_BLOCK_SLOTS) {
s = w->next;
*status = write_slot(w, n, s);
}
w->next = (s + 1u) % V4_BLOCK_SLOTS;
w->block[s] = 0;
w->changed[s] = 0;
if (w->last == (int)s) w->last = -1;
return s;
}
/* BLOCK and BUFFER: ( n -- waddr status ) */
static void slot_for(v4_blocks *w, v4_node *n, int read)
{
v4_cell num, at;
uint32_t block;
unsigned i, s;
int status;
if (n->ds.depth < 1u) { v4_dstack_reset(&n->ds); v4_dstack_push(&n->ds, 0); v4_dstack_push(&n->ds, (v4_cell)V4_BLOCK_REFUSED); return; }
num = v4_dstack_pop(&n->ds);
block = (uint32_t)num;
if (num < 1 || (uint64_t)num > 0xFFFFFFFFull || !blk_is_valid(block)) {
v4_dstack_push(&n->ds, 0); v4_dstack_push(&n->ds, (v4_cell)V4_BLOCK_RANGE);
return;
}
for (s = 0; s < V4_BLOCK_SLOTS && w->block[s] != block; s++) { }
if (s == V4_BLOCK_SLOTS) {
const uint8_t *b = 0;
s = take_slot(w, n, &status);
if (status == V4_BLOCK_OK && read) {
b = blk_get_buffer(block, 0);
if (!b) status = V4_BLOCK_REFUSED;
}
if (status != V4_BLOCK_OK) { v4_dstack_push(&n->ds, 0); v4_dstack_push(&n->ds, (v4_cell)status); return; }
at = slot_addr(w, s);
for (i = 0; i < V4_BLOCK_CELLS; i++)
n->mem[at + (v4_cell)i] = b ? (v4_cell)((v4_ucell)b[4u * i] | ((v4_ucell)b[4u * i + 1u] << 8)
| ((v4_ucell)b[4u * i + 2u] << 16) | ((v4_ucell)b[4u * i + 3u] << 24))
: 0;
w->block[s] = block;
w->changed[s] = 0;
}
w->last = (int)s;
v4_dstack_push(&n->ds, slot_addr(w, s));
v4_dstack_push(&n->ds, (v4_cell)V4_BLOCK_OK);
}
int v4_blocks_serve(v4_blocks *w, v4_node *n, v4_cell request)
{
unsigned s;
int status = V4_BLOCK_OK;
if (request > V4_REQ_BLOCK || request < V4_REQ_EMPTY_BUFFERS) return 0;
/* the chain is not what it was: what the slots hold is of no chain now */
if (blk_subsys_epoch() != w->epoch) { let_go(w); w->epoch = blk_subsys_epoch(); }
if (request == V4_REQ_EMPTY_BUFFERS) { let_go(w); return 1; }
/* the window is all in the node's memory, or nothing is done */
if (!v4_node_addr_ok(w->window) || !v4_node_addr_ok(w->window + (v4_cell)(V4_BLOCK_SLOTS * V4_BLOCK_CELLS - 1u))) {
if (request == V4_REQ_BLOCK || request == V4_REQ_BUFFER) {
if (n->ds.depth >= 1u) (void)v4_dstack_pop(&n->ds);
v4_dstack_push(&n->ds, 0);
}
v4_dstack_push(&n->ds, (v4_cell)V4_BLOCK_REFUSED);
return 1;
}
if (request == V4_REQ_BLOCK) { slot_for(w, n, 1); return 1; }
if (request == V4_REQ_BUFFER) { slot_for(w, n, 0); return 1; }
if (request == V4_REQ_UPDATE) {
if (w->last >= 0 && w->block[w->last] != 0) w->changed[w->last] = 1;
} else { /* SAVE-BUFFERS */
for (s = 0; s < V4_BLOCK_SLOTS; s++)
if (w->block[s] != 0 && w->changed[s] && write_slot(w, n, s) != V4_BLOCK_OK) {
status = V4_BLOCK_REFUSED;
w->block[s] = 0;
if (w->last == (int)s) w->last = -1;
}
}
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;
}