// Assembles the byte-level adders (examples/add8*.r8asm) and runs them on a tiny // ROT-8 machine for every input combination. This is the runtime check the // library never had: pass/fail comes from executing the tape, not from looking // at the character count. // // r8forge_byte_add_test [repo root] // // The paths in the sources are relative to the repo root, so run it from there. #include "r8forge/r8asm.hh" #include #include #include #include namespace { struct machine { std::vector cells = std::vector(256, 0); std::vector out; size_t dp = 0; size_t steps = 0; bool finished = true; // false: the step limit ran out (a loop that never ends) 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++; break; case '<': dp--; break; case '-': // flb cells[dp] ^= 1; break; case '+': // ror cells[dp] = ror(cells[dp]); break; case '[': if (cells[dp] == 0) { int depth = 1; while (depth) { pc++; if (tape[pc] == '[') depth++; else if (tape[pc] == ']') depth--; } } break; case ']': if (cells[dp] != 0) { 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; } } } }; // The examples lay bit i out in cell 4i + off: off 0 is A, 1 is B, 3 is scratch. void put_bits(machine &m, int off, int value) { for (int b = 0; b < 8; b++) if (value & (1 << b)) m.cells[size_t(4 * b + off)] ^= 1; } 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 &what, int a, int b, const machine &m, int sum, int carry) { if (failures++ < 5) std::cerr << " WRONG " << what << " a=" << a << " b=" << b << " out"; if (failures <= 5) { for (uint8_t v : m.out) std::cerr << " " << int(v); std::cerr << " (expect low " << (sum & 0xff) << ", carry " << carry << "), finished=" << m.finished << " steps=" << m.steps << "\n"; } } // Every (a, b) pair through examples/add8.r8asm. int test_add(const std::string &file) { const std::string tape = assemble(file); std::cout << "add8: " << tape.size() << " characters\n"; int bad = 0; for (int a = 0; a < 256; a++) for (int b = 0; b < 256; b++) { machine m; put_bits(m, 0, a); put_bits(m, 1, b); m.run(tape, 200'000); const int sum = a + b; const bool ok = m.finished && m.out.size() == 2 && m.out[0] == (sum & 0xff) && m.out[1] == (sum >> 8); if (!ok) { bad++; if (bad <= 3) report("add8", a, b, m, sum, sum >> 8); } } std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok") << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; return bad; } // examples/add8_inc.r8asm adds 1 three times, so the carry is only the carry of // that last add: earlier carries are dropped (the accumulator is 8 bits). int test_inc(const std::string &file) { const std::string tape = assemble(file); std::cout << "add8_inc: " << tape.size() << " characters\n"; int bad = 0; for (int a = 0; a < 256; a++) { machine m; put_bits(m, 0, a); m.run(tape, 400'000); const int low = (a + 3) & 0xff; const int carry = ((a + 2) & 0xff) == 0xff ? 1 : 0; const bool ok = m.finished && m.out.size() == 2 && m.out[0] == low && m.out[1] == carry; if (!ok) { bad++; if (bad <= 3) report("add8_inc", a, 3, m, a + 3, carry); } } 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 int bad = test_add(root + "/examples/add8.r8asm") + test_inc(root + "/examples/add8_inc.r8asm"); std::cout << (bad ? "FAILED\n" : "byte level adders ok\n"); return bad ? 1 : 0; }