The two things software cannot check by reading its own source are the 3 bit
packing and the bracket strategy, so both got a tool and a test.
New in the library (r8forge/tape.hh, src/tape.cc): the opcode numbering (taken
from ROT-8-viz enum, so the packed image and the hardware decoder cannot drift),
text <-> opcodes, opcodes <-> 3 bit bytes, the .r8t image with its trailer and
crc32, and a machine model that runs a packed tape the way a board would. The
model takes the bracket strategy as an option: backward scan with loop stack or
not, and jump-back-onto-the-[ (the contract) or past it (what the interpreters
do). All four combinations print the same thing, and the step count differs by
exactly the number of taken ] instructions -- which is what the test asserts.
Found and fixed while writing it: the loop stack discipline depends on the jump
convention. Jumping onto the [ means the [ re-pushes, so ] must pop on the jump;
jumping past it means the entry must stay until the loop exits. Mixing them up
emptied the stack on the second iteration of the calculator main loop -- caught
by the equivalence test on examples/calc.r8asm, not by reading the code.
Tools: r8forge_tape_pack (text tape -> image + --hex for $readmemh + --verify,
727 instructions become 273 bytes for hello, 62%, and the trailer sits past the
halt so address 0 of a ROM is instruction 0), and r8forge_tape_run now reads an
image directly and takes --loop-stack / --retest.
hardware/rot8.v: the 8 bit / 64 cell machine -- 3 bit opcodes from a byte
addressed memory read as a 16 bit window, a 6 bit dp with free wrap, the loop
stack bracket jump, blocking input, halt on ip == length, and a step counter that
counts instructions rather than cycles. hardware/tb_rot8.v loads a --hex image
and prints the OUT stream in tape_run format.
Verified with Icarus Verilog 14 (the RTL against tape_run --loop-stack --retest):
hello 577 instructions 417 steps 10 outs
term 16414 17643 11 (fed "lhi")
calc 51378 166741 19 (fed "12 34 + .": prints the prompt,
046 and the next prompt)
flagship 43676 100142 93
Every OUT stream identical. ctest -R rtl_sim runs that comparison from an 8 bit
build (11 tests in the default build now, where the RTL test self-skips because
the RTL is the 8 bit machine).
Two traps worth remembering: Icarus passes plusargs through as raw bytes, so an
absolute +HEX path containing non-ASCII (this checkout is under a Chinese
directory name) arrives mangled, $readmemh silently leaves the memory empty and
the machine obediently runs LEN steps of opcode 0 printing nothing -- run vvp
from the directory holding the hex with a relative name. And in_ready must not
be qualified with !in_valid or the input handshake deadlocks.
154 lines
4.6 KiB
C++
154 lines
4.6 KiB
C++
// r8forge_tape_pack -- turn a .rot8 text tape into the image a machine reads.
|
|
//
|
|
// r8forge_tape_pack <in.rot8> -o out.r8t [--hex out.hex] [--verify]
|
|
//
|
|
// The text tape is one character per instruction. The hardware stores three bits
|
|
// per instruction (eight opcodes), so this packs them and appends a small
|
|
// trailer; the format is defined once, in `r8forge/tape.hh`, so the runner and
|
|
// the tests read the file exactly the way this writes it.
|
|
//
|
|
// The trailer goes at the *end* on purpose: if the file is burnt into a ROM,
|
|
// address 0 is instruction 0, so the program is executed in place and the
|
|
// trailer sits past the halt point, never fetched.
|
|
//
|
|
// `--hex` writes the body alone, one byte per line, for Verilog's $readmemh; the
|
|
// comment on the first line carries the length and the geometry.
|
|
//
|
|
// `--verify` reads the file back and compares it, so a wrong bit order or a bad
|
|
// crc cannot slip through.
|
|
#include "r8forge/geometry.hh"
|
|
#include "r8forge/tape.hh"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
bool read_file(const std::string &path, std::vector<uint8_t> &bytes) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
if (!in)
|
|
return false;
|
|
bytes.assign(std::istreambuf_iterator<char>(in),
|
|
std::istreambuf_iterator<char>());
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
std::cerr << "usage: r8forge_tape_pack <in.rot8> -o out.r8t [--hex out.hex] "
|
|
"[--verify]\n";
|
|
return 2;
|
|
}
|
|
const std::string in_path = argv[1];
|
|
std::string out_path;
|
|
std::string hex_path;
|
|
bool verify = false;
|
|
for (int i = 2; i < argc; i++) {
|
|
const std::string a = argv[i];
|
|
if ((a == "-o" || a == "--output") && i + 1 < argc) {
|
|
out_path = argv[++i];
|
|
} else if (a == "--hex" && i + 1 < argc) {
|
|
hex_path = argv[++i];
|
|
} else if (a == "--verify") {
|
|
verify = true;
|
|
} else {
|
|
std::cerr << "unknown argument '" << a << "'\n";
|
|
return 2;
|
|
}
|
|
}
|
|
if (out_path.empty())
|
|
out_path = in_path + ".r8t";
|
|
|
|
std::vector<uint8_t> raw;
|
|
if (!read_file(in_path, raw)) {
|
|
std::cerr << "could not read " << in_path << "\n";
|
|
return 1;
|
|
}
|
|
const std::string text(raw.begin(), raw.end());
|
|
std::vector<uint8_t> code;
|
|
std::string error;
|
|
if (!tape_decode(text, code, error)) {
|
|
std::cerr << in_path << ": " << error << "\n";
|
|
return 1;
|
|
}
|
|
if (code.empty()) {
|
|
std::cerr << in_path << ": the tape is empty\n";
|
|
return 1;
|
|
}
|
|
|
|
const std::vector<uint8_t> image =
|
|
tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells);
|
|
const size_t body_bytes = image.size() - 16;
|
|
{
|
|
std::ofstream out(out_path, std::ios::binary);
|
|
if (!out) {
|
|
std::cerr << "could not write " << out_path << "\n";
|
|
return 1;
|
|
}
|
|
out.write(reinterpret_cast<const char *>(image.data()),
|
|
std::streamsize(image.size()));
|
|
if (!out) {
|
|
std::cerr << "could not write " << out_path << "\n";
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (!hex_path.empty()) {
|
|
std::ofstream hex(hex_path);
|
|
if (!hex) {
|
|
std::cerr << "could not write " << hex_path << "\n";
|
|
return 1;
|
|
}
|
|
hex << "// R8T1 instructions=" << code.size() << " width=" << kDefaultCellSize
|
|
<< " ring=" << kDefaultMemoryCells << " bytes=" << body_bytes << "\n";
|
|
hex << std::hex;
|
|
for (size_t i = 0; i < body_bytes; i++) {
|
|
hex.width(2);
|
|
hex.fill('0');
|
|
hex << unsigned(image[i]) << "\n";
|
|
}
|
|
}
|
|
|
|
if (verify) {
|
|
std::vector<uint8_t> back;
|
|
if (!read_file(out_path, back)) {
|
|
std::cerr << "verify: could not read back " << out_path << "\n";
|
|
return 1;
|
|
}
|
|
if (back != image) {
|
|
std::cerr << "verify: " << out_path << " does not read back byte for byte\n";
|
|
return 1;
|
|
}
|
|
tape_image decoded;
|
|
if (!tape_image_decode(back, decoded, error)) {
|
|
std::cerr << "verify: " << error << "\n";
|
|
return 1;
|
|
}
|
|
if (decoded.code != code) {
|
|
std::cerr << "verify: the image decodes to a different tape\n";
|
|
return 1;
|
|
}
|
|
if (decoded.width != kDefaultCellSize ||
|
|
decoded.ring != kDefaultMemoryCells) {
|
|
std::cerr << "verify: the trailer records the wrong geometry\n";
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
std::cout << in_path << ": " << code.size() << " instructions -> "
|
|
<< body_bytes << " bytes ("
|
|
<< (100.0 * double(body_bytes) / double(code.size()))
|
|
<< "% of the text form)";
|
|
if (!hex_path.empty())
|
|
std::cout << ", hex in " << hex_path;
|
|
std::cout << (verify ? ", verified" : "") << "\n";
|
|
return 0;
|
|
}
|