Files
R8Forge/tests/tape_pack.cc
T
dsh f6fb77ea8e
ci / linux-arm64 (push) Successful in 10m23s
hardware: stage one on a breadboard, with a pin table and a block diagram
hardware/bringup.md is the first hands on step: two 74LS161 counters for the low eight
address bits, a 28C256, and eight lamps showing the fetched byte.  It has the bill (80
to 120 CNY), a pin by pin table for both counters and the ROM, the reset network, three
clock options (button, 74LS14 RC at 5 Hz, 1 MHz only later), how to burn the chip, what
you should see, and the six things that usually go wrong.

Two decisions recorded there: use a 28C256 EEPROM for bring up and keep the 27C512 UV
part for later, because erasing for twenty minutes per fix kills debugging; and do not
test with hello, which starts with a run of moves that light nothing -- the note gives a
16 instruction test tape that walks all eight opcodes so the lamp patterns differ.

Also added: r8forge_tape_pack --raw writes the payload without the sixteen byte
trailer, which is what a ROM burner wants; --verify checks the length instead of
decoding a trailer that is not there any more.

hardware/bringup.png is a block diagram (clock to counters, carry chain, address buses
into the ROM, data bus to the lamps), not a breadboard top view: the first attempt at a
placement picture came out as a drawer of unlabelled parts and said less than the pin
table.
2026-11-24 20:02:48 +08:00

178 lines
5.9 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] [--eprom] [--raw] [--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.
// `--eprom` writes one byte per instruction instead of three bits. That is the
// format a bare EPROM wants: `ip` is the address and D2..D0 are the opcode, so
// the fetch needs no shift register and no decoder, at the cost of five wasted
// bits per instruction -- and a 27C512 is exactly 64K, the historical budget.
//
// `--raw` drops the 16 byte trailer, which is what a ROM burner wants.
//
// `--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;
bool eprom = false; // one byte per instruction: the 27C512 format
bool payload_only = false; // write the payload only: what a ROM burner wants
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 == "--raw") {
payload_only = true;
} else if (a == "--eprom") {
eprom = true;
} 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;
}
std::vector<uint8_t> image =
tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells,
eprom ? 2u : 1u);
const size_t body_bytes = image.size() - 16;
// --raw drops the 16 byte trailer: a ROM burner takes the payload and nothing
// else, and the trailer would otherwise sit in the address space after the
// halt, which is harmless but ugly when reading the chip back.
if (payload_only)
image.resize(body_bytes);
{
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;
}
if (payload_only) {
// no trailer to decode in a raw payload: the byte comparison above is the
// whole check, and the length is checked against the body size
if (back.size() != body_bytes) {
std::cerr << "verify: raw payload is " << back.size()
<< " bytes, expected " << body_bytes << "\n";
return 1;
}
} else {
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;
}