The nc slices do drive a rotation, so two more library macros fall out of them:
clb_add_nc A + B -> S, carry in/out in one cell 4 cells 693 chars
clb_inc_nc A + 1 -> S, carry in/out in one cell 3 cells 433 chars
against raw_add (6 cells, 1647) and raw_inc (5, 1214). Both are in
r8forge_byte_op_test now, exhaustive: 65536 and 256 combinations, all pass.
clb_inc_nc has to start its carry cell at 1 -- that bit is the "+1"; the add
version starts at 0. A rotation driver reads the low bit, throws it away and
rotates, so eating the operands is exactly what it wants.
This retracts a sentence that has been in the tree for a while: "rotation needs
A and B preserved". It does not. Preserving them is a separate wish (raw_*
grants it, with two more cells). AGENTS.md said the wrong thing in the do not
retry list; that bullet now says what was really measured, which is only that a
4 cell slice asked to *preserve* A and B has no solution up to 8 layers.
Renamed for the same reason: bit_add_nc -> clb_bit_add_nc, bit_inc_nc ->
clb_bit_inc_nc, verify_add_nc.r8asm -> verify_clb_bit_add_nc.r8asm,
verify_inc_nc.r8asm -> verify_clb_bit_inc_nc.r8asm. Measured what the prefixes
actually separate here:
bit_raw_and A=1 B=1 -> cells 0=1 1=1 2=1 operands preserved, has tmp
bit_raw_add A=1 B=1 -> cells 0=1 1=1 4=1 operands preserved, has buf
clb_bit_and A=1 B=1 -> cells 2=1 operands eaten, no tmp
clb_bit_add_nc A=1 B=1 -> cells 2=1 3=1 operands eaten, no tmp
so raw means "the caller passes the scratch cell" and those variants happen to
keep their inputs, while clb means the inputs are gone. These slices eat A and
B, so they are clb.
One self inflicted mess to own up to: I rewrote AGENTS.md with a PowerShell
Get-Content / Set-Content round trip, which on this machine decodes UTF-8 as GBK
and wrote the file back as mojibake. Restored it with git checkout -- AGENTS.md
and redid every edit through the editor. The work rule in AGENTS.md now names
that exact trap instead of only warning about -Encoding ascii.
Green: test.r8asm 1666, full_add 8/8, both verify_*.r8asm, byte_op_test
(65536x4 + 512 + 65536 + 256), macro_docs_test 32 macros in 10 files, fibonacci.
258 lines
8.4 KiB
C++
258 lines
8.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;
|
|
}
|
|
|
|
// clb_add_nc: out is A, B, S, Ci. This one eats both operands, so A and B have
|
|
// to come back zero -- that is what lets it use the 4 cell slice.
|
|
int test_add_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_add_nc: " << 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 sum = a + b;
|
|
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
|
|
m.out[1] == 0 && m.out[2] == (sum & 0xff) &&
|
|
m.out[3] == (sum >> 8);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_add_nc", a, b, m,
|
|
"0 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;
|
|
}
|
|
|
|
// clb_inc_nc: out is A, S, Ci.
|
|
int test_inc_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_inc_nc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
m.cells[0] = uint8_t(a); // A
|
|
m.run(tape, 100'000);
|
|
const int sum = a + 1;
|
|
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
|
|
m.out[1] == (sum & 0xff) && m.out[2] == (sum >> 8);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_inc_nc", a, 1, m,
|
|
"0 " + std::to_string(sum & 0xff) + " " +
|
|
std::to_string(sum >> 8));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 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") +
|
|
test_add_nc(drv + "raw_add_nc_byte.r8asm") +
|
|
test_inc_nc(drv + "raw_inc_nc_byte.r8asm");
|
|
std::cout << (bad ? "FAILED\n" : "byte level library ok\n");
|
|
return bad ? 1 : 0;
|
|
}
|