Files
R8Forge/tests/synth_test.cc
T
dsh 62c6f8979f Cell width and tape length: 11 bits, 259 cells, by default
The assembler already took the width as an argument (import_file(path, cell_size))
and exposed _CELL_SIZE / _CELL_MAX / _ROL_STEP to programs, so every macro that
writes %rep _CELL_SIZE follows the width on its own.  What was missing was a
single place saying what the default is, and VMs that agree with it.

include/r8forge/geometry.hh now holds kDefaultCellBits = 11,
kDefaultMemoryCells = 259 and kDefaultCellMask, and the four VMs use it:

* cells are uint16_t, the tape is kDefaultMemoryCells long
* ROR is (v >> 1) | ((v & 1) << (kDefaultCellBits - 1)), not the 8 bit version
* input and output mask with kDefaultCellMask

The tests now state their expectations in terms of the width instead of a byte
(kCellMask, kBits, kCellMod).  Two things were genuinely wrong for 11 bits and
are fixed: clb_neg_* and clb_inc_* were compared against 256 - a and 0xff, and
asm/test.r8asm set B with .xor (~126)+1 -- .xor takes its constant modulo the
cell width, so that is 1922 in an 11 bit cell, not 130.

Results at 11 x 259:

    asm/test.r8asm        7262 characters, out 127 130 0 257 0 1088 19
                          (127+130 = 257, and 200*200 = 19*2048 + 1088)
    r8forge_byte_op_test  all 17 operations pass
    r8forge_synth_test    all eight combinations ok (bit level is width free)
    r8forge_macro_docs_test  41 macros in 17 files
    asm/fibonacci.r8asm   11959 characters, 13 terms now (no wrap at 256)

ROT-8-viz moved its defaults to 11 x 259 in the same change, so the tapes cross
check: both implementations print 127 130 0 257 0 1088 19 for asm/test.r8asm.
Tapes grow when the width does (raw_add 1647 -> 2199 characters), because the
%rep _CELL_SIZE loops are longer.

AGENTS.md gains a section on how the width flows through the tools, including
the trap that 2048 % 256 == 0 makes several 8 bit expectations still pass by
accident.
2026-11-07 10:56:28 +08:00

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) << (kDefaultCellBits - 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, kDefaultCellBits)) {
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;
}