Files
R8Forge/tests/pack_roundtrip.cmake
dsh 9b31a51fec hardware: M0 -- the packer, the RTL, and a simulator that agrees byte for byte
The two things software cannot check by reading its own source are the 3 bit
packing and the bracket strategy, so both got a tool and a test.

New in the library (r8forge/tape.hh, src/tape.cc): the opcode numbering (taken
from ROT-8-viz enum, so the packed image and the hardware decoder cannot drift),
text <-> opcodes, opcodes <-> 3 bit bytes, the .r8t image with its trailer and
crc32, and a machine model that runs a packed tape the way a board would.  The
model takes the bracket strategy as an option: backward scan with loop stack or
not, and jump-back-onto-the-[ (the contract) or past it (what the interpreters
do).  All four combinations print the same thing, and the step count differs by
exactly the number of taken ] instructions -- which is what the test asserts.

Found and fixed while writing it: the loop stack discipline depends on the jump
convention.  Jumping onto the [ means the [ re-pushes, so ] must pop on the jump;
jumping past it means the entry must stay until the loop exits.  Mixing them up
emptied the stack on the second iteration of the calculator main loop -- caught
by the equivalence test on examples/calc.r8asm, not by reading the code.

Tools: r8forge_tape_pack (text tape -> image + --hex for $readmemh + --verify,
727 instructions become 273 bytes for hello, 62%, and the trailer sits past the
halt so address 0 of a ROM is instruction 0), and r8forge_tape_run now reads an
image directly and takes --loop-stack / --retest.

hardware/rot8.v: the 8 bit / 64 cell machine -- 3 bit opcodes from a byte
addressed memory read as a 16 bit window, a 6 bit dp with free wrap, the loop
stack bracket jump, blocking input, halt on ip == length, and a step counter that
counts instructions rather than cycles.  hardware/tb_rot8.v loads a --hex image
and prints the OUT stream in tape_run format.

Verified with Icarus Verilog 14 (the RTL against tape_run --loop-stack --retest):
  hello        577 instructions  417 steps  10 outs
  term         16414            17643      11 (fed "lhi")
  calc         51378           166741      19 (fed "12 34 + .": prints the prompt,
                                                046 and the next prompt)
  flagship     43676           100142      93
Every OUT stream identical.  ctest -R rtl_sim runs that comparison from an 8 bit
build (11 tests in the default build now, where the RTL test self-skips because
the RTL is the 8 bit machine).

Two traps worth remembering: Icarus passes plusargs through as raw bytes, so an
absolute +HEX path containing non-ASCII (this checkout is under a Chinese
directory name) arrives mangled, $readmemh silently leaves the memory empty and
the machine obediently runs LEN steps of opcode 0 printing nothing -- run vvp
from the directory holding the hex with a relative name.  And in_ready must not
be qualified with !in_valid or the input handshake deadlocks.
2026-11-20 16:25:54 +08:00

53 lines
2.2 KiB
CMake

# End-to-end check of the packer, at the CLI level: assemble a tape, pack it,
# then run the *file* both ways and require identical output. The in-process
# test (r8forge_tape_pack_test) covers the codec and the bracket strategies; this
# one covers the two programs a person actually runs, including the image format
# and its trailer.
#
# cmake -DR8FORGE=... -DPACK=... -DRUN=... -DSOURCE=... -DWORK=... -P pack_roundtrip.cmake
foreach(var R8FORGE PACK RUN SOURCE WORK)
if(NOT DEFINED ${var})
message(FATAL_ERROR "pack_roundtrip.cmake needs -D${var}=...")
endif()
endforeach()
execute_process(COMMAND ${R8FORGE} ${SOURCE} OUTPUT_VARIABLE text
RESULT_VARIABLE rc)
if(NOT rc EQUAL 0)
message(FATAL_ERROR "assembling ${SOURCE} failed: ${rc}")
endif()
string(LENGTH "${text}" length)
file(WRITE ${WORK}/pack_roundtrip.rot8 "${text}")
execute_process(COMMAND ${PACK} ${WORK}/pack_roundtrip.rot8
-o ${WORK}/pack_roundtrip.r8t --verify
OUTPUT_VARIABLE packed RESULT_VARIABLE rc)
if(NOT rc EQUAL 0)
message(FATAL_ERROR "packing failed: ${rc} ${packed}")
endif()
message(STATUS "${packed}")
foreach(flags "" "--loop-stack" "--retest" "--loop-stack --retest")
separate_arguments(flag_list UNIX_COMMAND "${flags}")
execute_process(COMMAND ${RUN} ${WORK}/pack_roundtrip.rot8 ${flag_list}
OUTPUT_VARIABLE from_text RESULT_VARIABLE rc)
if(NOT rc EQUAL 0)
message(FATAL_ERROR "text run failed (${flags}): ${rc}")
endif()
execute_process(COMMAND ${RUN} ${WORK}/pack_roundtrip.r8t ${flag_list}
OUTPUT_VARIABLE from_image RESULT_VARIABLE rc)
if(NOT rc EQUAL 0)
message(FATAL_ERROR "packed run failed (${flags}): ${rc}")
endif()
if(NOT from_image STREQUAL from_text)
message(FATAL_ERROR
"packed image prints something else (${flags})\n"
" text: ${from_text}\n image: ${from_image}")
endif()
string(REGEX MATCH "\\(([0-9]+) steps" _ "${from_text}")
message(STATUS "${length} instructions, ${from_image}")
endforeach()
# Image damage (flipped bit, missing trailer, nonsense geometry) is covered by
# r8forge_tape_pack_test, which can corrupt a vector without touching files.