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.
112 lines
3.3 KiB
C++
112 lines
3.3 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/geometry.hh"
|
|
#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<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
|
std::vector<uint16_t> out;
|
|
size_t dp = 0;
|
|
|
|
static uint16_t ror(uint16_t v) {
|
|
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
|
|
}
|
|
|
|
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, kDefaultCellSize)) {
|
|
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] = uint16_t(a); // A
|
|
m.cells[1] = uint16_t(b); // B
|
|
m.cells[2] = uint16_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;
|
|
}
|