forked from dsh/ROT-8-Machine
96 lines
3.6 KiB
CMake
96 lines
3.6 KiB
CMake
# Compare OUT bytes and instruction counts against the R8Forge interpreter.
|
|
# See rtl/README.md for the toolchain setup and an invocation example.
|
|
|
|
foreach(var RTL PACK RUN R8FORGE SOURCE WORK)
|
|
if(NOT DEFINED ${var})
|
|
message(FATAL_ERROR "rtl_sim.cmake needs -D${var}=...")
|
|
endif()
|
|
endforeach()
|
|
if(NOT DEFINED INPUT)
|
|
set(INPUT "")
|
|
endif()
|
|
|
|
get_filename_component(rtl_dir "${RTL}" ABSOLUTE)
|
|
get_filename_component(work_dir "${WORK}" ABSOLUTE)
|
|
get_filename_component(SOURCE "${SOURCE}" ABSOLUTE)
|
|
get_filename_component(source_dir "${SOURCE}" DIRECTORY)
|
|
file(MAKE_DIRECTORY "${work_dir}")
|
|
|
|
find_program(IVERILOG iverilog)
|
|
find_program(VVP vvp)
|
|
if(NOT IVERILOG OR NOT VVP)
|
|
message(FATAL_ERROR "rtl_sim needs iverilog and vvp on PATH")
|
|
endif()
|
|
|
|
# The assembler resolves includes from its working directory. Use the parent
|
|
# of examples/ or asm/ by default.
|
|
if(NOT DEFINED ASSEMBLE_DIR)
|
|
get_filename_component(ASSEMBLE_DIR "${source_dir}/.." ABSOLUTE)
|
|
endif()
|
|
execute_process(COMMAND ${R8FORGE} ${SOURCE} OUTPUT_VARIABLE text RESULT_VARIABLE rc
|
|
WORKING_DIRECTORY ${ASSEMBLE_DIR})
|
|
if(NOT rc STREQUAL "0")
|
|
message(FATAL_ERROR "assembling ${SOURCE} in ${ASSEMBLE_DIR} failed: ${rc}")
|
|
endif()
|
|
string(LENGTH "${text}" length)
|
|
file(WRITE ${work_dir}/rtl_sim.rot8 "${text}")
|
|
execute_process(COMMAND ${PACK} ${work_dir}/rtl_sim.rot8 -o ${work_dir}/rtl_sim.r8t
|
|
--hex ${work_dir}/rtl_sim.hex
|
|
OUTPUT_VARIABLE packed RESULT_VARIABLE rc)
|
|
if(NOT rc STREQUAL "0")
|
|
message(FATAL_ERROR "packing failed: ${rc} ${packed}")
|
|
endif()
|
|
message(STATUS "${packed}")
|
|
file(WRITE ${work_dir}/rtl_sim_in.txt "${INPUT}")
|
|
|
|
if(INPUT STREQUAL "")
|
|
execute_process(COMMAND ${RUN} ${work_dir}/rtl_sim.r8t --loop-stack --retest
|
|
OUTPUT_VARIABLE sw RESULT_VARIABLE rc)
|
|
else()
|
|
execute_process(COMMAND ${RUN} ${work_dir}/rtl_sim.r8t --loop-stack --retest
|
|
-i "${INPUT}"
|
|
OUTPUT_VARIABLE sw RESULT_VARIABLE rc)
|
|
endif()
|
|
if(NOT rc STREQUAL "0")
|
|
message(FATAL_ERROR "software run failed: ${rc} ${sw}")
|
|
endif()
|
|
|
|
execute_process(COMMAND ${IVERILOG} -g2012 -o rot8_tb.vvp
|
|
${rtl_dir}/rot8.v ${rtl_dir}/tb_rot8.v
|
|
WORKING_DIRECTORY ${work_dir}
|
|
OUTPUT_VARIABLE build_log RESULT_VARIABLE rc)
|
|
if(NOT rc STREQUAL "0")
|
|
message(FATAL_ERROR "iverilog failed (${rc}): ${build_log}")
|
|
endif()
|
|
|
|
# Relative ASCII plusargs avoid path encoding issues in Icarus on Windows.
|
|
execute_process(COMMAND ${VVP} rot8_tb.vvp +HEX=rtl_sim.hex
|
|
+LEN=${length} +INFILE=rtl_sim_in.txt
|
|
WORKING_DIRECTORY ${work_dir}
|
|
OUTPUT_VARIABLE rtl RESULT_VARIABLE rc)
|
|
if(NOT rc STREQUAL "0")
|
|
message(FATAL_ERROR "vvp failed: ${rc} ${rtl}")
|
|
endif()
|
|
set(out_pattern "out([^()\r\n]*) *\\(([0-9]+) steps")
|
|
string(REGEX MATCH "^${out_pattern}" sw_summary "${sw}")
|
|
if(sw_summary STREQUAL "")
|
|
message(FATAL_ERROR "Interpreter did not report an OUT stream and step count:\n${sw}")
|
|
endif()
|
|
set(sw_values "${CMAKE_MATCH_1}")
|
|
set(sw_steps "${CMAKE_MATCH_2}")
|
|
|
|
string(REGEX MATCH "(^|\n)${out_pattern}" rtl_summary "${rtl}")
|
|
if(rtl_summary STREQUAL "")
|
|
message(FATAL_ERROR "RTL did not report an OUT stream and step count:\n${rtl}")
|
|
endif()
|
|
set(rtl_values "${CMAKE_MATCH_2}")
|
|
set(rtl_steps "${CMAKE_MATCH_3}")
|
|
|
|
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 interpreter agree")
|