# 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")