""" test_kernel_integration.py — the payoff for the built kernel image. Every other test proves the FORTH kernel works on the HUNT image (built via forth_hunt.ImageKit, memory map: NEXT@0x100, HALT@0x200, code@0x300+). This file proves it works on the BUILT image (src/kernel_builder.py, the one that actually ships in the code cave at 0x004690). Until now "it builds" had never meant "it runs" — the kernel_builder image was size-checked only, never executed. This test closes that gap. It runs the integrated INTERPRET colon word against the built image: - seed input into the kernel's SRAM input buffer - build a runtime dictionary (SRAM) so FIND can locate user words like '+' - write a thread [INTERPRET_cfa, HALT_cfa] and start at kernel NEXT - assert the trapnet passes (reached HALT, no underflow, no fault) AND the data stack matches the spec The linchpin: `3 4 +` → [7] on the BUILT image. The spec the ARM monolith could never meet, now met on the image that actually ships. MEMORY-MAP NOTE: the device image places NEXT at offset 0 (cave base 0x004690, so NEXT → absolute 0x004690, nonzero — fine). But the Unicorn harness maps the image at FLASH_BASE=0 and starts with emu_start(begin, until=0), and Unicorn treats until=0 as 'stop when PC reaches 0'. NEXT at offset 0 would make every branch-to-NEXT halt immediately. So the test builds with KernelBuilder(base= 0x100): NEXT lands at absolute 0x100 (nonzero), the internal layout is identical to the device image, only the base shifts. This mirrors how the hunt image already places NEXT at 0x100. Kernel HALT is auto-assigned; we check PC against THAT address (kb.halt_spin), not the harness's hardcoded 0x0200. """ import struct from src.harness import ForthVM from forth_hunt import ( S_LATEST, S_INPUT, S_INLEN, S_INIDX, S_DICT, write_input, S_THREAD_SRAM, load_kernel_map_cached, build_runtime_dict, ) from unicorn.arm_const import UC_ARM_REG_R12, UC_ARM_REG_PC # Base 0 — the DEVICE base (cave at 0x004690, so NEXT@offset-0 → absolute 0x4690 # on device; here FLASH_BASE=0 so NEXT lands at absolute 0x0). Tests run the # EXACT bytes that ship — branch-bearing primitives (FIND/WORD/NUMBER) encode # differently per base, so building at base=0 (not the old base=0x100 workaround) # means the test image == the cave image modulo the base shift. The old # base=0x100 workaround existed because safe_emu_start treated until=0 as a # "stop at PC=0" sentinel; that's been fixed (until=0 → 0xFFFFFFFF), so NEXT can # live at 0 again. See README.md §"How we hunted" (the base-0 fix). KBASE = 0 KERNEL_NEXT = KBASE # NEXT absolute address (= base, since NEXT is at offset 0) # SRAM thread location (distinct from forth_hunt.S_THREAD, the flash-side ImageKit # region — see forth_hunt.S_THREAD_SRAM docstring). S_THREAD = S_THREAD_SRAM def _build(): """Delegates to the shared cached loader in forth_hunt (load_kernel_map_cached). Kept as a thin wrapper so existing call sites are unchanged.""" return load_kernel_map_cached() # Alias to the shared helper, preserving this file's original call-site name. _build_runtime_dict = build_runtime_dict def run_kernel(input_text, dict_words, stack=(), max_insns=8000, clean=True): """Run the BUILT kernel image's INTERPRET on input_text. dict_words: (name, code_addr) entries to install in the runtime SRAM dict so FIND can locate them. code_addr = kb.words[name][0] (raw machine addr). TRAPNET: clean=True (default) verifies execution was well-formed AND that PC reached the kernel's HALT spin (kb.halt_spin), closing the loop on the integrated image. A fall-through, underflow, or fault fails with a diagnostic instead of returning a wrong stack silently.""" kb, image = _build() vm = ForthVM(image, trace=False) write_input(vm, input_text) _build_runtime_dict(vm, dict_words) if stack: vm.push_data(list(stack)) # Thread: call INTERPRET (loops until empty token → EXIT), then HALT. vm.mu.mem_write( S_THREAD, struct.pack('