Files
R8Forge/CMakeLists.txt
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

149 lines
7.5 KiB
CMake

cmake_minimum_required(VERSION 3.10)
project(R8Forge)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include)
# The width of a cell. 11 is the project default; the tests and the VMs follow
# it, so `-DR8FORGE_CELL_SIZE=16` builds an entirely 16 bit toolchain.
# 32 is the ceiling: the VMs (and ROT-8-viz) store a cell in uint32_t, and the
# assembler's immediate type is narrower than that anyway. Refuse anything else
# here so a wrong -DR8FORGE_CELL_SIZE fails at configure time instead of as a
# C++ error in whichever file happens to shift by the width first.
set(R8FORGE_CELL_SIZE 11 CACHE STRING "bits per cell (1..32)")
if(R8FORGE_CELL_SIZE LESS 1 OR R8FORGE_CELL_SIZE GREATER 32)
message(FATAL_ERROR
"R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE} is out of range: a cell is stored in "
"uint32_t in every VM, so the supported range is 1..32")
endif()
add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE})
# How many cells the tape holds. 259 is the historical default (a prime ring, so
# a walked-off-end bug cannot alias onto a power of two); it is a *hardware*
# knob as much as a software one. Every program we ship peaks well below 64
# cells (calc 51, hello 49, signed_demo 34, term 27 -- geometry.hh lists the
# measurements), so `-DR8FORGE_MEMORY_CELLS=64` runs all of them; only the
# asm/test.r8asm self test, which lays every macro's cell block out side by side,
# needs the default. The ring is a modulo counter either way, so any size is
# legal; 1..65536 keeps the VMs' bookkeeping sane.
set(R8FORGE_MEMORY_CELLS 259 CACHE STRING "cells on the tape (1..65536)")
if(R8FORGE_MEMORY_CELLS LESS 1 OR R8FORGE_MEMORY_CELLS GREATER 65536)
message(FATAL_ERROR
"R8FORGE_MEMORY_CELLS=${R8FORGE_MEMORY_CELLS} is out of range: the ring is "
"addressed by a 16 bit counter in the physical machine, so the supported "
"range is 1..65536")
endif()
add_compile_definitions(R8FORGE_MEMORY_CELLS=${R8FORGE_MEMORY_CELLS})
find_package(Threads REQUIRED)
add_library(r8forge_core src/r8asm.cc src/synth.cc src/tape_opt.cc src/tape.cc)
target_link_libraries(r8forge_core PUBLIC Threads::Threads)
# 别再 set(CMAKE_CXX_FLAGS ...) 了:那会把默认 flag 整个换掉,MSVC 下丢 /EHsc、
# 还会把 -O3 塞进 Debug 和 /RTC1 打架。Release 配置本来就带 -O3 / /O2 和 NDEBUG。
if(NOT MSVC)
add_compile_options($<$<CONFIG:Release>:-march=native>)
endif()
add_executable(r8forge tests/asm_test.cc)
target_link_libraries(r8forge r8forge_core)
add_executable(r8forge_synth_test tests/synth_test.cc)
target_link_libraries(r8forge_synth_test r8forge_core)
add_executable(r8forge_derive src/derive.cc)
target_link_libraries(r8forge_derive r8forge_core)
# A second, independent ROT-8 interpreter: runs a .rot8 file with no assembler
# and no macros involved, so its verdict can be checked against r8forge_derive.
add_executable(r8forge_tape_run tests/tape_run.cc)
target_link_libraries(r8forge_tape_run r8forge_core)
add_executable(r8forge_tape_pack tests/tape_pack.cc)
target_link_libraries(r8forge_tape_pack r8forge_core)
# The two things software cannot check by reading the source: the 3 bit packing
# the hardware decodes, and the bracket strategy it uses.
add_executable(r8forge_tape_pack_test tests/tape_pack_test.cc)
target_link_libraries(r8forge_tape_pack_test r8forge_core)
# Runs the byte level library macros (raw_add / raw_and / raw_or / raw_xor /
# raw_inc) over every input combination.
add_executable(r8forge_byte_op_test tests/byte_op_test.cc)
target_link_libraries(r8forge_byte_op_test r8forge_core)
# Checks that every %macro in asm/ carries a doc comment in the house format.
add_executable(r8forge_macro_docs_test tests/macro_docs_test.cc)
# Runs every examples/*.r8asm as a stand alone program and compares its OUT
# stream with the `; out ...` line in the file header.
add_executable(r8forge_examples_test tests/examples_test.cc)
target_link_libraries(r8forge_examples_test r8forge_core)
# The tape level peephole pass: three run-local rules, no bracket bookkeeping.
add_executable(r8forge_tape_opt tests/tape_opt.cc)
target_link_libraries(r8forge_tape_opt r8forge_core)
add_executable(r8forge_cost_par tests/cost_par.cc)
target_link_libraries(r8forge_cost_par r8forge_core)
add_executable(r8forge_cost_search tests/cost_search.cc)
target_link_libraries(r8forge_cost_search r8forge_core)
# ---- tests ------------------------------------------------------------------
# `ctest --test-dir build -j` runs the whole baseline. Every entry is a program
# under tests/ that exits non zero on failure; the working directory is the repo
# root because the drivers use relative paths.
#
# Labels: `widthfree` means "true at any cell width" (bit level slices, the
# synthesizer, the looping demo, the macro comment format). Everything else
# assumes the default 11 bit geometry -- examples/ fixes 2047 style values and
# byte_op_test drives the 8/11 bit word macros -- so a CI matrix over
# -DR8FORGE_CELL_SIZE runs only the widthfree set.
enable_testing()
set(R8FORGE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME flagship COMMAND r8forge asm/test.r8asm)
add_test(NAME byte_ops COMMAND r8forge_byte_op_test)
add_test(NAME examples COMMAND r8forge_examples_test examples)
add_test(NAME synth_full_add COMMAND r8forge_synth_test examples/full_add.r8asm)
add_test(NAME bit_add_slice COMMAND r8forge verify_clb_bit_add_nc.r8asm)
add_test(NAME bit_inc_slice COMMAND r8forge verify_clb_bit_inc_nc.r8asm)
add_test(NAME loop_derive COMMAND r8forge_derive verify_loop.r8asm -1 0=5)
add_test(NAME macro_docs COMMAND r8forge_macro_docs_test)
# The packer's CLI and the runner's loader, end to end: assemble, pack, then run
# the text tape and the packed file and require the same output under every
# bracket strategy.
add_test(NAME pack_roundtrip
COMMAND ${CMAKE_COMMAND}
-DR8FORGE=$<TARGET_FILE:r8forge>
-DPACK=$<TARGET_FILE:r8forge_tape_pack>
-DRUN=$<TARGET_FILE:r8forge_tape_run>
-DSOURCE=${R8FORGE_TEST_DIR}/examples/hello.r8asm
-DWORK=${CMAKE_CURRENT_BINARY_DIR}
-P ${R8FORGE_TEST_DIR}/tests/pack_roundtrip.cmake)
add_test(NAME tape_pack COMMAND r8forge_tape_pack_test)
# The RTL against the interpreter: compiles hardware/rot8.v with iverilog, runs
# a tape on it, and requires the same OUT stream and step count as
# `tape_run --loop-stack --retest`. The RTL is the 8 bit / 64 cell machine, so
# the script reports SKIP (and passes) in any other geometry.
find_program(IVERILOG_EXE iverilog)
add_test(NAME rtl_sim
COMMAND ${CMAKE_COMMAND}
-DGEOMETRY=${R8FORGE_CELL_SIZE}/${R8FORGE_MEMORY_CELLS}
-DIVERILOG=${IVERILOG_EXE}
-DPACK=$<TARGET_FILE:r8forge_tape_pack>
-DRUN=$<TARGET_FILE:r8forge_tape_run>
-DR8FORGE=$<TARGET_FILE:r8forge>
-DSOURCE=${R8FORGE_TEST_DIR}/examples/hello.r8asm
-DHARDWARE=${R8FORGE_TEST_DIR}/hardware
-DWORK=${CMAKE_CURRENT_BINARY_DIR}
-P ${R8FORGE_TEST_DIR}/tests/rtl_sim.cmake)
set_tests_properties(flagship byte_ops examples PROPERTIES LABELS "default-width")
set_tests_properties(synth_full_add bit_add_slice bit_inc_slice loop_derive pack_roundtrip tape_pack rtl_sim
macro_docs PROPERTIES LABELS "widthfree")
set_tests_properties(flagship byte_ops examples synth_full_add bit_add_slice
bit_inc_slice loop_derive macro_docs tape_pack pack_roundtrip rtl_sim
PROPERTIES WORKING_DIRECTORY ${R8FORGE_TEST_DIR})