r8forge_tape_run grew a `--trace` flag: it prints pc, instruction, dp and the first four cells before every step. Without it a wrong hand written macro is just "the answer is wrong"; with it the failure is one line, e.g. dp jumping to 1023 because a backwards move was emitted where the runtime was already there. That is what happened to the two cell in place increment I tried after the in place complement. It assembled to 155 characters (against 430 for the slice version) and got 129 of 256 inputs right -- every input whose low bit is 0, i.e. every input that never propagates a carry. The algorithm was fine; the macros were not. The rule it broke: dp is a compile time constant in r8asm while a `[ ... ]` body may be skipped at run time, so **every conditional body has to come back to its own condition cell** (net zero moves). Otherwise the taken and the skipped path leave dp in different places and every later `.tp` is off by one. The library already obeys this: set_0's body never moves, clb_bit_mov ends with `.tp before` and a `flb` that eats the condition on the way out. For a two cell in place add both candidate condition cells hold live data (A's bit is the result, the carry cell's bit is the carry), so each test first has to make its condition consumable, which means moving a bit through the spare bits of the carry cell and rotating it into place -- about eight characters of detour per bit. A correct version should land near 200-220 characters, still about half of the 430 character slice version, but it stays a hand written loop with subtle invariants. Not added to the library yet; the scratch files are gone. AGENTS.md now carries the dp rule under the instruction semantics section, next to the two `set_0` / `%synth` constraints it belongs with.
126 lines
3.7 KiB
C++
126 lines
3.7 KiB
C++
// 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<uint8_t> cells(1024, 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) = uint8_t(std::stoul(a.substr(eq + 1)) & 0xff);
|
|
}
|
|
|
|
std::vector<uint8_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] = uint8_t((cells[dp] >> 1) | (cells[dp] << 7));
|
|
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 (uint8_t v : out)
|
|
std::cout << " " << int(v);
|
|
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
|
|
<< int(cells[0]) << std::dec << ")\n";
|
|
return 0;
|
|
}
|