Four interpreters here (tests/synth_test.cc, src/derive.cc and the two I added
earlier: tests/tape_run.cc, tests/byte_add_test.cc) implemented '[' and ']' as
a Brainfuck style "is this whole cell zero" test. The reference implementation
does not:
ROT-8-viz src/rot8.c
rot8_biz: if ((*mem & 1) == 0) skip to just past the matching ']'
rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['
That one difference is why every byte level program in the library looked like
an endless loop. It is invisible in the bit level world -- for a cell holding 0
or 1 the low bit test and the zero test agree -- which is why the synth tables,
asm/test.r8asm and full_add never noticed anything.
Cross checked against the reference before touching anything: the tape our
assembler produces for "A = 128, set_0, B = 3, raw_add" prints 128 3 0 131 0 on
ROT-8-viz/build/rot8_test.exe and hung in our derive.
With the fix, measured:
raw_add 65536/65536 pairs ok 1647 chars
raw_and 65536/65536 639
raw_or 65536/65536 575
raw_xor 65536/65536 463
raw_inc 512/512 1214
asm/fibonacci.r8asm prints 1 1 2 3 5 8 13 21 34 55, exactly what its
comment claims
So the byte level layer was never broken. Two commits ago it was declared dead
-- set_0 "only converges on 0 and 1", the bits of a packed byte "unreadable",
raw_add "only survives 0+0" -- entirely on the strength of the broken
interpreters. AGENTS.md and README.md now say the opposite and keep the wrong
diagnosis as a worked example: when byte level code looks hung, check the
interpreter first.
New:
tests/byte_op_test.cc + drv/raw_{add,and,or,xor,inc}_byte.r8asm
exhaustive runtime proof for the byte level library, 0.5 s
tests/tape_run.cc + r8forge_tape_run
a second interpreter that runs a .rot8 file directly -- cross checking
derive is now one command
verify_set_0.r8asm now demonstrates what set_0 really does instead of claiming
it hangs: 255 -> 254, 5 -> 4, 128 -> 128 (it clears the low bit, not the cell).
The one bit per cell layer (byte_add / pack_byte) stays as a documented
alternative: it is correct under the real semantics too, just redundant.
202 lines
6.4 KiB
C++
202 lines
6.4 KiB
C++
// Runs the byte level library macros over every input pair on a ROT-8 machine.
|
|
//
|
|
// r8forge_byte_op_test [repo root]
|
|
//
|
|
// The machine below follows the reference implementation in ROT-8-viz
|
|
// (src/rot8.c): `biz` jumps when the LOW BIT of the cell is 0 and `rnz` jumps
|
|
// back when it is 1, so `set_0` (= [-]) clears the low bit of whatever is in
|
|
// the cell. Reading `[`/`]` as a Brainfuck style "is the cell zero" test makes
|
|
// every one of these programs look like an endless loop -- which is exactly the
|
|
// mistake three interpreters in this repo used to make.
|
|
#include "r8forge/r8asm.hh"
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct machine {
|
|
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
|
|
std::vector<uint8_t> out;
|
|
size_t dp = 0;
|
|
size_t steps = 0;
|
|
bool finished = true; // false: the step limit ran out
|
|
|
|
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
|
|
|
|
void run(const std::string &tape, size_t limit) {
|
|
for (size_t pc = 0; pc < tape.size(); pc++) {
|
|
if (++steps > limit) {
|
|
finished = false;
|
|
return;
|
|
}
|
|
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] = ror(cells[dp]);
|
|
break;
|
|
case '[': // biz: branch when the low bit is zero
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']': // rnz: return when the low bit is one
|
|
if ((cells[dp] & 1) == 1) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
default: // ',' (input) is not modelled
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
std::string assemble(const std::string &file) {
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file, 8) || assembler.failed()) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
std::exit(1);
|
|
}
|
|
return assembler.export_rot8();
|
|
}
|
|
|
|
int failures = 0;
|
|
|
|
void report(const std::string &name, int a, int b, const machine &m,
|
|
const std::string &want) {
|
|
if (failures++ < 5) {
|
|
std::cerr << " WRONG " << name << " a=" << a << " b=" << b << " out";
|
|
for (uint8_t v : m.out)
|
|
std::cerr << " " << int(v);
|
|
std::cerr << " want " << want << " finished=" << m.finished
|
|
<< " steps=" << m.steps << "\n";
|
|
}
|
|
}
|
|
|
|
// raw_and / raw_or / raw_xor: both operands are preserved, the result is third.
|
|
int test_bitop(const std::string &file, const char *name, int op) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << name << ": " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
m.cells[0] = uint8_t(a); // A
|
|
m.cells[2] = uint8_t(b); // B
|
|
m.run(tape, 100'000);
|
|
const int want = op == 0 ? (a & b) : op == 1 ? (a | b) : (a ^ b);
|
|
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == a &&
|
|
m.out[1] == b && m.out[2] == want;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report(name, a, b, m,
|
|
std::to_string(a) + " " + std::to_string(b) + " " +
|
|
std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// raw_add: out is A, B, Ci, S, Co.
|
|
int test_add(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "raw_add: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
m.cells[0] = uint8_t(a);
|
|
m.cells[2] = uint8_t(b);
|
|
m.run(tape, 100'000);
|
|
const int sum = a + b;
|
|
const bool ok = m.finished && m.out.size() == 5 && m.out[0] == a &&
|
|
m.out[1] == b && m.out[2] == 0 &&
|
|
m.out[3] == (sum & 0xff) && m.out[4] == (sum >> 8);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("raw_add", a, b, m,
|
|
std::to_string(a) + " " + std::to_string(b) + " 0 " +
|
|
std::to_string(sum & 0xff) + " " +
|
|
std::to_string(sum >> 8));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// raw_inc: out is A, Ci, S, Co; S = A + Ci.
|
|
int test_inc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "raw_inc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int ci = 0; ci < 2; ci++) {
|
|
machine m;
|
|
m.cells[0] = uint8_t(a);
|
|
m.cells[2] = uint8_t(ci);
|
|
m.run(tape, 100'000);
|
|
const int sum = a + ci;
|
|
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == a &&
|
|
m.out[1] == ci && m.out[2] == (sum & 0xff) &&
|
|
m.out[3] == (sum >> 8);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("raw_inc", a, ci, m,
|
|
std::to_string(a) + " " + std::to_string(ci) + " " +
|
|
std::to_string(sum & 0xff) + " " +
|
|
std::to_string(sum >> 8));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 512 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const std::string root = argc > 1 ? argv[1] : ".";
|
|
const std::string drv = root + "/drv/";
|
|
const int bad = test_add(drv + "raw_add_byte.r8asm") +
|
|
test_bitop(drv + "raw_and_byte.r8asm", "raw_and", 0) +
|
|
test_bitop(drv + "raw_or_byte.r8asm", "raw_or", 1) +
|
|
test_bitop(drv + "raw_xor_byte.r8asm", "raw_xor", 2) +
|
|
test_inc(drv + "raw_inc_byte.r8asm");
|
|
std::cout << (bad ? "FAILED\n" : "byte level library ok\n");
|
|
return bad ? 1 : 0;
|
|
}
|