Files
R8Forge/tests/tape_run.cc
T
dsh 90feedb37b 32 bit cells: widen the VMs to uint32_t, fix the mask at 32, make width a build option
All five R8Forge machines stored a cell in uint16_t, so anything above 16 bits
was silently truncated even though geometry.hh (and the viz) allow 1..32; and
`(1 << cell_size) - 1` for _CELL_MAX was undefined at 32.

  * cells and out/input buffers are uint32_t in derive, tape_run, byte_op_test,
    examples_test and synth_test (r8veritas*.hh keep their uint16_t search
    state -- that is node ids, unrelated)
  * kDefaultCellMask and the injected _CELL_MAX special-case 32 instead of
    shifting by 32
  * width is now a build option: -DR8FORGE_CELL_SIZE=<1..32> (CMake cache var,
    added as a compile definition), so a whole toolchain of another width is one
    configure away; build*/ is ignored so those trees do not leak into git

Verified: 11 bits unchanged (tape 64513, byte_op ok); a 16 bit build passes
synth_test 8/8 and byte_op_test (65536 exhaustive pairs); a 32 bit build passes
synth_test, verify_clb_bit_add_nc and assembles full_add to the same 95 chars.
Exhaustive 32 bit is 2^64 pairs -- that stays sampled, not verified.

SPEC 13 checklist now says five VMs / uint32_t / the 32 bit mask case.
2026-11-10 18:23:26 +08:00

146 lines
4.5 KiB
C++

#include "r8forge/geometry.hh"
// Independent ROT-8 interpreter. Reads a .rot8 tape straight from a file --
// no macro, no assembler, no r8asm -- and prints what `out` ('.') produced.
//
// r8forge_tape_run <file.rot8> [-i text] [cells...]
//
// cells are addr=value (decimal), e.g. 0=128
// -i/--input gives INP (",") its characters, low 7 bits each; when the text
// runs out INP reads 0. That is enough to drive an interactive program from
// a script -- the ROT-8-viz CLI is the one that prompts a live terminal.
//
// This exists as a second opinion for r8forge_derive: if the two disagree, the
// difference is in derive; if they agree, the disagreement is in the program.
//
// Note the character map: "><+-[].," -- '-' is flb, '+' is ror, and ror does
// not change whether a cell is zero (that is what makes [-] a two value loop).
#include <cstdint>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "usage: r8forge_tape_run <file.rot8> [addr=value ...]\n";
return 2;
}
std::ifstream in(argv[1], std::ios::binary);
if (!in) {
std::cerr << "could not open " << argv[1] << "\n";
return 1;
}
const std::string tape((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
std::vector<uint32_t> cells(kDefaultMemoryCells, 0);
std::deque<uint32_t> input;
bool trace = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
if (a == "--trace" || a == "-t") {
trace = true;
continue;
}
if (a == "-i" || a == "--input") {
if (i + 1 >= argc) {
std::cerr << "-i needs a string of input characters\n";
return 2;
}
for (const char *p = argv[++i]; *p; p++)
input.push_back(uint32_t((unsigned char)*p & 0x7f));
continue;
}
const size_t eq = a.find('=');
if (eq == std::string::npos)
continue;
const size_t at = size_t(std::stoul(a.substr(0, eq)));
cells.at(at) = uint32_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
}
std::vector<uint32_t> out;
size_t dp = 0;
const size_t limit = 5'000'000; // same as r8forge_derive
size_t pc = 0;
size_t steps = 0;
for (; pc < tape.size(); pc++) {
if (++steps > limit) {
std::cout << "HANG after " << limit << " steps (pc " << pc << ", dp " << dp
<< ", cell=0x" << std::hex << int(cells[dp]) << std::dec
<< ")\n";
return 3;
}
if (trace) {
// cells 0..3 are the interesting ones when debugging a library macro
std::cout << "pc " << pc << " " << tape[pc] << " dp " << dp << " [";
for (size_t i = 0; i < 4; i++)
std::cout << (i ? " " : "") << int(cells[i]);
std::cout << "]\n";
}
switch (tape[pc]) {
case '>':
dp = (dp + 1) % cells.size();
break;
case '<':
dp = (dp + cells.size() - 1) % cells.size();
break;
case '-': // flb
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = uint32_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
break;
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
// byte level program in the library look like an endless loop.
case '[':
if ((cells[dp] & 1) == 0) {
for (int depth = 1; depth;) {
pc++;
if (pc >= tape.size()) {
std::cout << "unbalanced '['\n";
return 4;
}
if (tape[pc] == '[')
depth++;
else if (tape[pc] == ']')
depth--;
}
}
break;
case ']':
if ((cells[dp] & 1) == 1) {
for (int depth = 1; depth;) {
if (pc == 0) {
std::cout << "unbalanced ']'\n";
return 4;
}
pc--;
if (tape[pc] == ']')
depth++;
else if (tape[pc] == '[')
depth--;
}
}
break;
case '.':
out.push_back(cells[dp]);
break;
case ',': // INP: the low 7 bits of the next scripted character
cells[dp] = input.empty() ? 0 : input.front();
if (!input.empty())
input.pop_front();
break;
default: // anything else: not modelled
break;
}
}
std::cout << "out";
for (uint32_t v : out)
std::cout << " " << int(v);
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
<< int(cells[0]) << std::dec << ")\n";
return 0;
}