Two things: the TTL build is now the main target (Tang Nano looks too modern), and the packer learned the format that makes the TTL build simple. Format: r8forge_tape_pack --eprom writes one byte per instruction with the opcode in the low three bits. Then ip is the EPROM address and D2..D0 are the opcode -- no shift register, no 16 bit window, no decoder. It wastes five bits per instruction, and a 27C512 is exactly 64K, which is the historical instruction budget: the 8 bit calculator (51378) and the flagship (43676) both fit. Both formats live in the same .r8t trailer now (format byte 1 = packed, 2 = one byte per instruction), decode dispatches on it, and the test runs both and requires the same output and step count. Carrier: 74LS DIPs on a green board, 27C512 in a socket with its quartz window, 6116 SRAM, 74LS299 for the rotate, 6850 ACIA for serial, 74LS47 and four seven segment displays on the panel, 1 MHz can oscillator. About 31 to 40 chips, 400 to 700 CNY, a few weeks. The FPGA stays as a bench-only stepping stone. hardware/mechanics.md: where mechanics actually pays. The program must stay electronic (130 metres of paper tape for the calculator), and 512 relays for the data ring are out, but the pointer is worth it -- > and < are 71 to 73 percent of all steps, so a 64 position ratchet or uniselector makes the machine physically walk the ring. Also listed: an electromagnetic step counter (10 to 20 Hz, so only in single step mode, with a clutch for free running), a paper tape punch or plotter for output, a tape/card reader or plain keypad for input, and a hand crank as the clock. The rule that keeps this safe: mechanics only ever sit on sensor and actuator pins and never in the logic path, and the handshake just stops the sequencer while something moves -- since nothing in the ISA can observe time, slower is not different. A relay-only machine is priced honestly too: 60 to 120 relays, 10 to 50 Hz, hello in 10 to 40 seconds, the calculator in four hours, so that one is an exhibit. Also fixed: the synthesised macro fallback file name now carries a per-run token. When the system temp directory is not writable every r8asm falls back to the same working directory, so parallel ctest runs could write and import the same file -- which showed up as a random one-test failure in 0.39 seconds and nothing else.
158 lines
5.1 KiB
C++
158 lines
5.1 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] [--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`n// format a bare EPROM wants: `ip` is the address and D2..D0 are the opcode, so`n// the fetch needs no shift register and no decoder, at the cost of five wasted`n// bits per instruction -- and a 27C512 is exactly 64K, the historical budget.`n//`n// `--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
|
|
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 == "--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;
|
|
}
|
|
|
|
const std::vector<uint8_t> image =
|
|
tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells,
|
|
eprom ? 2u : 1u);
|
|
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;
|
|
}
|