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.
88 lines
3.7 KiB
CMake
88 lines
3.7 KiB
CMake
# Compare the RTL against the software interpreter, on a real tape.
|
|
#
|
|
# The RTL in hardware/rot8.v is the *8 bit, 64 cell* machine (that is the physical
|
|
# one), so this script only does anything when the build has that geometry; other
|
|
# builds report SKIP and succeed, which is what lets the test live in the
|
|
# widthfree label.
|
|
#
|
|
# cmake -DGEOMETRY=8/64 -DIVERILOG=... -DPACK=... -DRUN=... -DR8FORGE=...
|
|
# -DSOURCE=... -DHARDWARE=... -DWORK=... -P rtl_sim.cmake
|
|
#
|
|
# It assembles SOURCE, packs it, runs `tape_run --loop-stack --retest` (the
|
|
# combination the board implements), compiles the RTL with iverilog, runs it, and
|
|
# requires the same OUT stream and the same step count.
|
|
foreach(var GEOMETRY IVERILOG PACK RUN R8FORGE SOURCE HARDWARE WORK)
|
|
if(NOT DEFINED ${var})
|
|
message(FATAL_ERROR "rtl_sim.cmake needs -D${var}=...")
|
|
endif()
|
|
endforeach()
|
|
|
|
if(NOT GEOMETRY STREQUAL "8/64")
|
|
message(STATUS "RTL sim skipped: the RTL is the 8 bit / 64 cell machine, this build is ${GEOMETRY}")
|
|
return()
|
|
endif()
|
|
|
|
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}/rtl_sim.rot8 "${text}")
|
|
execute_process(COMMAND ${PACK} ${WORK}/rtl_sim.rot8 -o ${WORK}/rtl_sim.r8t
|
|
--hex ${WORK}/rtl_sim.hex
|
|
OUTPUT_VARIABLE packed RESULT_VARIABLE rc)
|
|
if(NOT rc EQUAL 0)
|
|
message(FATAL_ERROR "packing failed: ${rc} ${packed}")
|
|
endif()
|
|
message(STATUS "${packed}")
|
|
file(WRITE ${WORK}/rtl_sim_in.txt "")
|
|
|
|
execute_process(COMMAND ${RUN} ${WORK}/rtl_sim.r8t --loop-stack --retest
|
|
-i ""
|
|
OUTPUT_VARIABLE sw RESULT_VARIABLE rc)
|
|
if(rc EQUAL 2)
|
|
# -i "" is rejected; that is fine, this tape reads nothing
|
|
execute_process(COMMAND ${RUN} ${WORK}/rtl_sim.r8t --loop-stack --retest
|
|
OUTPUT_VARIABLE sw RESULT_VARIABLE rc)
|
|
endif()
|
|
if(NOT rc EQUAL 0)
|
|
message(FATAL_ERROR "software run failed: ${rc} ${sw}")
|
|
endif()
|
|
|
|
execute_process(COMMAND ${IVERILOG} -g2012 -o rot8_tb.vvp
|
|
${HARDWARE}/rot8.v ${HARDWARE}/tb_rot8.v
|
|
WORKING_DIRECTORY ${WORK}
|
|
OUTPUT_VARIABLE build_log RESULT_VARIABLE rc)
|
|
if(NOT rc EQUAL 0)
|
|
message(FATAL_ERROR "iverilog failed: ${build_log}")
|
|
endif()
|
|
# Run vvp *from the work directory* with relative file names. Icarus passes
|
|
# plusargs through as raw bytes, so an absolute path containing non-ASCII (this
|
|
# checkout lives under a Chinese directory name) arrives at $readmemh mangled and
|
|
# the memory silently stays empty -- the machine then runs a tape of opcode 0 for
|
|
# LEN steps and prints nothing.
|
|
execute_process(COMMAND vvp rot8_tb.vvp +HEX=rtl_sim.hex
|
|
+LEN=${length} +INFILE=rtl_sim_in.txt
|
|
WORKING_DIRECTORY ${WORK}
|
|
OUTPUT_VARIABLE rtl RESULT_VARIABLE rc)
|
|
if(NOT rc EQUAL 0)
|
|
message(FATAL_ERROR "vvp failed: ${rc} ${rtl}")
|
|
endif()
|
|
# the simulator prints its own $finish line; keep the first "out ..." one
|
|
string(REGEX MATCH "out[^\n]*" rtl_out "${rtl}")
|
|
|
|
string(REGEX MATCH "^out([^()]*) *\\(([0-9]+) steps" _ "${sw}")
|
|
set(sw_values "${CMAKE_MATCH_1}")
|
|
set(sw_steps "${CMAKE_MATCH_2}")
|
|
string(REGEX MATCH "^out([^()]*) *\\(([0-9]+) steps" _ "${rtl_out}")
|
|
set(rtl_values "${CMAKE_MATCH_1}")
|
|
set(rtl_steps "${CMAKE_MATCH_2}")
|
|
|
|
if(NOT sw_values STREQUAL rtl_values)
|
|
message(FATAL_ERROR "OUT streams differ\n sw : ${sw_values}\n rtl: ${rtl_values}")
|
|
endif()
|
|
if(NOT sw_steps STREQUAL rtl_steps)
|
|
message(FATAL_ERROR "step counts differ: sw ${sw_steps}, rtl ${rtl_steps}")
|
|
endif()
|
|
message(STATUS "${length} instructions, ${sw_steps} steps: RTL and software agree")
|