Files
LithosAnanake/v4/tests/test_host_rom_range.c
T
rajamesandClaude Opus 5.5 a28b5f2230 fix(v4): the final review's six minors in the ROM boot
- Text after start is refused, as text.c refuses it: before it jumps, the
  ROM reads on to the end of the source; blanks, comments and the ends of
  blobs may follow, a token is ERR-AFTER (26), "text after start".
- A token too long where a name, a target or a value is wanted is "name
  too long", as text.c says: (MISSING) reports "missing" only when no error
  came first.  `", keeps "missing", which is text.c's word for both.
- text.c's "address out of range" names the address, not the value.
- The system's source files are one list, v4/sys_files.h: boot.c, the
  harness and the nucleus test are made from it.
- The ROM's error codes and words are one list, v4/rom_errors.h: boot.c's
  messages, host_map.h's ROM_ERR_* and mkmap's ERR-* are made from it.
- node.h says what v4_node_store does with a ROM address (nothing, as for
  any address outside RAM, D-14); test_host_rom_range pins it.

ROM: 2095 words, rom_hash 0x06d0dcb7a0cb41a1.  dict_hash unchanged,
0xe5a57f3eb72ff94a.  make -C v4 test test-32 hosted-check pass.  QEMU:
amd64, aarch64, riscv64 to ok>, POST 538/538, POST: PASSED.

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

104 lines
4.1 KiB
C

/* test_host_rom_range.c -- the ROM is the machine's: read and run, never written.
*
* docs/v4.0.0/MACHINE.md section 2, DATASHEET-F18.md section 5. The ROM is a
* read-only range at V4_ROM_BASE, above RAM. An instruction fetch, an @p
* literal and a data fetch there read the ROM; a running programme's store
* there is an address fault. Like every device in the model it is attached
* by the node's owner, and a reset detaches it.
*/
#include "v4/asm.h"
#include "v4/testcode.h"
#include <stdio.h>
static int failures = 0, checks = 0;
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
static v4_node n, scratch;
static v4_exec_state es;
static v4_heat h;
static v4_cell rom[16];
/* ROM words 0..: "7 ;" at ROM+0, then a cell holding 42 at ROM+8 */
static void build_rom(void)
{
v4_asm ra;
unsigned i;
v4_node_reset(&scratch);
v4_asm_begin(&ra, &scratch, 0);
v4_asm_lit(&ra, 7);
v4_asm_op(&ra, V4_OP_SEMI);
CHECK(v4_asm_ok(&ra), "the ROM's code assembles");
for (i = 0; i < 16; i++) rom[i] = scratch.mem[i];
rom[8] = 42;
}
int main(void)
{
v4_asm as;
long r;
build_rom();
v4_node_reset(&n);
v4_exec_reset(&es);
v4_heat_reset(&h);
CHECK(V4_ROM_BASE == (v4_cell)V4_NODE_WORDS, "the ROM starts where RAM ends");
CHECK(v4_node_load(&n, V4_ROM_BASE) == 0, "no ROM attached: nothing there");
v4_node_rom_attach(&n, rom, 16);
CHECK(v4_node_load(&n, V4_ROM_BASE) == rom[0], "the ROM reads");
CHECK(v4_node_load(&n, V4_ROM_BASE + 16) == 0, "past the ROM's words: nothing");
v4_node_store(&n, V4_ROM_BASE, 12345);
CHECK(v4_node_load(&n, V4_ROM_BASE) == rom[0] && n.faults == 0, "C code's store into the ROM does nothing and counts no fault (node.h)");
/* RAM 16: call the ROM's word, then return */
v4_asm_begin(&as, &n, 16);
v4_asm_branch(&as, V4_OP_CALL, V4_ROM_BASE);
v4_asm_op(&as, V4_OP_SEMI);
CHECK(v4_asm_ok(&as), "a call into the ROM assembles");
r = v4_test_call(&n, &es, &h, 16, 100);
CHECK(r > 0, "the call returns");
CHECK(n.ds.depth == 1 && n.ds.t == 7, "code in the ROM runs: depth %u T %ld", n.ds.depth, (long)n.ds.t);
/* RAM 32: fetch the ROM's cell 8 through A */
v4_dstack_reset(&n.ds);
v4_asm_begin(&as, &n, 32);
v4_asm_lit(&as, V4_ROM_BASE + 8);
v4_asm_op(&as, V4_OP_BANG_A);
v4_asm_op(&as, V4_OP_FETCH_A);
v4_asm_op(&as, V4_OP_SEMI);
CHECK(v4_asm_ok(&as), "a fetch from the ROM assembles");
r = v4_test_call(&n, &es, &h, 32, 100);
CHECK(r > 0 && n.ds.depth == 1 && n.ds.t == 42, "a data fetch reads the ROM: T %ld", (long)n.ds.t);
/* RAM 48: store 99 into the ROM's cell 8 through B */
v4_dstack_reset(&n.ds);
v4_asm_begin(&as, &n, 48);
v4_asm_lit(&as, V4_ROM_BASE + 8);
v4_asm_op(&as, V4_OP_BANG_B);
v4_asm_lit(&as, 99);
v4_asm_op(&as, V4_OP_STORE_B);
v4_asm_op(&as, V4_OP_SEMI);
CHECK(v4_asm_ok(&as), "a store into the ROM assembles");
r = v4_test_call(&n, &es, &h, 48, 100);
CHECK(r < 0, "a store into the ROM does not return");
CHECK(n.fault_kind == V4_FAULT_ADDRESS && n.fault_addr == V4_ROM_BASE + 8,
"it is an address fault at the ROM's address: kind %u addr %ld", n.fault_kind, (long)n.fault_addr);
CHECK(v4_node_load(&n, V4_ROM_BASE + 8) == 42, "and the ROM is unchanged");
/* C code's store does nothing there either */
v4_node_store(&n, V4_ROM_BASE + 8, 99);
CHECK(v4_node_load(&n, V4_ROM_BASE + 8) == 42, "v4_node_store leaves the ROM alone");
/* a reset detaches it, as it does every device */
v4_node_reset(&n);
CHECK(v4_node_load(&n, V4_ROM_BASE) == 0, "a reset detaches the ROM");
v4_node_rom_attach(&n, rom, 16);
CHECK(v4_node_load(&n, V4_ROM_BASE) == rom[0], "and the owner attaches it again");
/* more words than the range holds are refused: nothing attached */
v4_node_rom_attach(&n, rom, V4_ROM_WORDS + 1u);
CHECK(v4_node_load(&n, V4_ROM_BASE) == 0, "a ROM larger than the range is not attached");
printf("test_host_rom_range: %d checks, %d failures\n", checks, failures);
return failures != 0;
}