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.
109 lines
3.2 KiB
C++
109 lines
3.2 KiB
C++
// Assembles examples/full_add.r8asm (whose body is synthesised at assembly
|
|
// time) and runs the resulting tape on a tiny ROT-8 machine for all eight
|
|
// input combinations.
|
|
#include "r8forge/r8asm.hh"
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
// the eight ROT-8 instructions
|
|
const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias
|
|
// real ROT-8: '>' '<' '-' '+' '[' ']' '.' ','
|
|
// note "-" is ror, and there is no separate 'r'
|
|
|
|
struct machine {
|
|
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
|
|
std::vector<uint8_t> out;
|
|
size_t dp = 0;
|
|
|
|
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
|
|
|
|
void run(const std::string &tape) {
|
|
for (size_t pc = 0; pc < tape.size(); pc++) {
|
|
switch (tape[pc]) {
|
|
case '>':
|
|
dp++;
|
|
break;
|
|
case '<':
|
|
dp--;
|
|
break;
|
|
case '-': // flb: flip last bit
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror: rotate right
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
// biz / rnz test the LOW BIT of the cell, not "is the cell zero":
|
|
// rot8_biz: if ((*mem & 1) == 0) jump past the matching ']'
|
|
// rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['
|
|
// (reference: ROT-8-viz src/rot8.c). With "cell == 0" instead, every
|
|
// byte level program in the library looks like an endless loop.
|
|
case '[':
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
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:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm";
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file, 8)) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
return 1;
|
|
}
|
|
const std::string tape = assembler.export_rot8();
|
|
std::cout << "tape: " << tape.size() << " characters\n";
|
|
|
|
int failures = 0;
|
|
for (int combo = 0; combo < 8; combo++) {
|
|
const int a = combo & 1, b = (combo >> 1) & 1, c = (combo >> 2) & 1;
|
|
machine m;
|
|
m.cells[0] = uint8_t(a); // A
|
|
m.cells[1] = uint8_t(b); // B
|
|
m.cells[2] = uint8_t(c); // Ci
|
|
m.run(tape);
|
|
const int sum = a ^ b ^ c;
|
|
const int carry = (a & b) | (a & c) | (b & c);
|
|
const bool ok = m.out.size() == 2 && m.out[0] == sum && m.out[1] == carry;
|
|
std::cout << " " << a << b << c << " -> " << (m.out.size() > 0 ? int(m.out[0]) : -1)
|
|
<< (m.out.size() > 1 ? int(m.out[1]) : -1) << " expect " << sum
|
|
<< carry << (ok ? " ok" : " WRONG") << "\n";
|
|
if (!ok)
|
|
failures++;
|
|
}
|
|
std::cout << (failures ? "FAILED\n" : "all eight combinations ok\n");
|
|
return failures ? 1 : 0;
|
|
}
|