Files
R8Forge/tests/tape_run.cc
T
dsh ac5810580d kDefaultCellBits -> kDefaultCellSize: the width is cell_size, not cell bits
The cell width is cell_size everywhere else now -- cell_size in the AST, in
ROT-8-viz's rot8_config, and the docs -- but the default in geometry.hh was
still kDefaultCellBits, and the four VMs spelled it that way too.

  * include/r8forge/geometry.hh: kDefaultCellBits -> kDefaultCellSize.
    kDefaultCellMask keeps its name: it is a mask, not a size.
  * src/derive.cc, tests/asm_test.cc, tests/synth_test.cc, tests/tape_run.cc,
    tests/byte_op_test.cc: same rename, and the local shorthand kBits in
    byte_op_test.cc is now kCellSize so the test file does not put the old
    word back.
  * AGENTS.md and README.md follow.  The cross check bullet now describes the
    workspace ROT-8-viz as it actually is (fork dsh/ROT-8-viz, branch main,
    runtime --cell / --mem over cell_size / mem_size), instead of the feature
    branch it used to point at, which no longer exists.

No behaviour change.  All six self checks re-run green: 28729 character hash
tape, synth_test 8/8, both verify_ tapes, byte_op_test (clb_mul 3977 chars,
65536/65536, "byte level library ok") and the macro docs test.  The hash tape
still gives 1322 1786 1600 5 94 127 from r8forge_derive, r8forge_tape_run and
ROT-8-viz's rot8_test -- three implementations, one answer.
2026-11-07 12:33:56 +08:00

127 lines
3.8 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> [cells...]
//
// cells are addr=value (decimal), e.g. 0=128
//
// 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 <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<uint16_t> cells(kDefaultMemoryCells, 0);
bool trace = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
if (a == "--trace" || a == "-t") {
trace = true;
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) = uint16_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
}
std::vector<uint16_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] = uint16_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;
default: // ',' and anything else: not modelled
break;
}
}
std::cout << "out";
for (uint16_t v : out)
std::cout << " " << int(v);
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
<< int(cells[0]) << std::dec << ")\n";
return 0;
}