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.
346 lines
11 KiB
C++
346 lines
11 KiB
C++
#include "r8forge/tape.hh"
|
|
|
|
#include "r8forge/geometry.hh"
|
|
|
|
#include <algorithm>`n#include <cstring>
|
|
|
|
namespace {
|
|
|
|
char to_char(opcode op) {
|
|
switch (op) {
|
|
case opcode::stp: return '>';
|
|
case opcode::btp: return '<';
|
|
case opcode::ror: return '+';
|
|
case opcode::flb: return '-';
|
|
case opcode::biz: return '[';
|
|
case opcode::rnz: return ']';
|
|
case opcode::out: return '.';
|
|
case opcode::inp: return ',';
|
|
}
|
|
return '?';
|
|
}
|
|
|
|
bool from_char(char c, uint8_t &op) {
|
|
switch (c) {
|
|
case '>': op = uint8_t(opcode::stp); return true;
|
|
case '<': op = uint8_t(opcode::btp); return true;
|
|
case '+': op = uint8_t(opcode::ror); return true;
|
|
case '-': op = uint8_t(opcode::flb); return true;
|
|
case '[': op = uint8_t(opcode::biz); return true;
|
|
case ']': op = uint8_t(opcode::rnz); return true;
|
|
case '.': op = uint8_t(opcode::out); return true;
|
|
case ',': op = uint8_t(opcode::inp); return true;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool tape_decode(const std::string &text, std::vector<uint8_t> &code,
|
|
std::string &error) {
|
|
code.clear();
|
|
error.clear();
|
|
code.reserve(text.size());
|
|
for (size_t i = 0; i < text.size(); i++) {
|
|
const char c = text[i];
|
|
// Whitespace is formatting, not instructions: a .rot8 file that came from a
|
|
// shell redirect or an editor usually ends with a newline, and a wrapped
|
|
// tape is still the same tape. Anything else that is not an instruction is
|
|
// an error -- the old interpreter silently ignored junk, which hid exactly
|
|
// the corruption this tool exists to catch.
|
|
if (c == '\n' || c == '\r' || c == ' ' || c == '\t')
|
|
continue;
|
|
uint8_t op = 0;
|
|
if (!from_char(c, op)) {
|
|
error = "character " + std::to_string(i) + " is not an instruction (0x" +
|
|
std::to_string(int((unsigned char)c)) + ")";
|
|
return false;
|
|
}
|
|
code.push_back(op);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string tape_encode(const std::vector<uint8_t> &code) {
|
|
std::string text;
|
|
text.reserve(code.size());
|
|
for (uint8_t op : code)
|
|
text.push_back(to_char(opcode(op)));
|
|
return text;
|
|
}
|
|
|
|
std::vector<uint8_t> tape_pack(const std::vector<uint8_t> &code) {
|
|
std::vector<uint8_t> bytes((code.size() * 3 + 7) / 8, 0);
|
|
for (size_t i = 0; i < code.size(); i++)
|
|
for (int b = 0; b < 3; b++)
|
|
if ((code[i] >> b) & 1u) {
|
|
const size_t bit = i * 3 + size_t(b);
|
|
bytes[bit / 8] |= uint8_t(1u << (bit % 8));
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
bool tape_unpack(const std::vector<uint8_t> &bytes, size_t length,
|
|
std::vector<uint8_t> &code, std::string &error) {
|
|
code.clear();
|
|
code.reserve(length);
|
|
if ((length * 3 + 7) / 8 > bytes.size()) {
|
|
error = "image holds " + std::to_string(bytes.size()) +
|
|
" bytes, too few for " + std::to_string(length) + " instructions";
|
|
return false;
|
|
}
|
|
for (size_t i = 0; i < length; i++) {
|
|
uint8_t op = 0;
|
|
for (int b = 0; b < 3; b++) {
|
|
const size_t bit = i * 3 + size_t(b);
|
|
if ((bytes[bit / 8] >> (bit % 8)) & 1u)
|
|
op = uint8_t(op | (1u << b));
|
|
}
|
|
if (op > uint8_t(opcode::inp)) {
|
|
error = "instruction " + std::to_string(i) + " decodes to opcode " +
|
|
std::to_string(int(op)) + ", which does not exist";
|
|
return false;
|
|
}
|
|
code.push_back(op);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// ---- the .r8t image ----------------------------------------------------------
|
|
|
|
namespace {
|
|
|
|
uint32_t crc32(const std::vector<uint8_t> &data) {
|
|
uint32_t crc = 0xFFFFFFFFu;
|
|
for (uint8_t b : data) {
|
|
crc ^= b;
|
|
for (int i = 0; i < 8; i++)
|
|
crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u)));
|
|
}
|
|
return ~crc;
|
|
}
|
|
|
|
void put_u32(std::vector<uint8_t> &v, size_t at, uint32_t value) {
|
|
for (int i = 0; i < 4; i++)
|
|
v[at + size_t(i)] = uint8_t((value >> (8 * i)) & 0xFF);
|
|
}
|
|
|
|
uint32_t get_u32(const std::vector<uint8_t> &v, size_t at) {
|
|
uint32_t value = 0;
|
|
for (int i = 0; i < 4; i++)
|
|
value |= uint32_t(v[at + size_t(i)]) << (8 * i);
|
|
return value;
|
|
}
|
|
|
|
constexpr size_t kTrailer = 16;
|
|
constexpr unsigned kFormatPacked = 1; // 3 bits per instruction
|
|
constexpr unsigned kFormatEprom = 2; // one byte per instruction, low 3 bits
|
|
|
|
} // namespace
|
|
|
|
std::vector<uint8_t> tape_image_encode(const std::vector<uint8_t> &code,
|
|
size_t width, size_t ring,
|
|
unsigned format) {
|
|
std::vector<uint8_t> image;
|
|
if (format == kFormatEprom) {
|
|
// One byte per instruction: what a plain EPROM wants, because `ip` is then
|
|
// the address and D2..D0 are the opcode -- no shift register, no two byte
|
|
// window, no decoder. It wastes five bits per instruction, and a 27C512
|
|
// happens to be exactly 64K, which is the historical instruction budget.
|
|
image.assign(code.begin(), code.end());
|
|
} else {
|
|
image = tape_pack(code);
|
|
}
|
|
const size_t at = image.size();
|
|
image.resize(at + kTrailer, 0);
|
|
put_u32(image, at + 0, uint32_t(code.size()));
|
|
image[at + 4] = uint8_t(width);
|
|
image[at + 5] = uint8_t(ring > 255 ? 0 : ring);
|
|
image[at + 6] = uint8_t(format == kFormatEprom ? kFormatEprom : kFormatPacked);
|
|
std::memcpy(&image[at + 8], "R8T1", 4);
|
|
const std::vector<uint8_t> body(image.begin(), image.begin() + std::ptrdiff_t(at));
|
|
put_u32(image, at + 12, crc32(body));
|
|
return image;
|
|
}
|
|
|
|
bool tape_is_image(const std::vector<uint8_t> &bytes) {
|
|
return bytes.size() >= kTrailer && bytes[bytes.size() - 8] == 'R' &&
|
|
bytes[bytes.size() - 7] == '8' && bytes[bytes.size() - 6] == 'T' &&
|
|
bytes[bytes.size() - 5] == '1';
|
|
}
|
|
|
|
bool tape_image_decode(const std::vector<uint8_t> &bytes, tape_image &img,
|
|
std::string &error) {
|
|
img = tape_image{};
|
|
error.clear();
|
|
if (!tape_is_image(bytes)) {
|
|
error = "no R8T1 trailer: not a packed image";
|
|
return false;
|
|
}
|
|
const size_t body = bytes.size() - kTrailer;
|
|
const size_t length = get_u32(bytes, body);
|
|
const unsigned width = bytes[body + 4];
|
|
const unsigned ring = bytes[body + 5];
|
|
const unsigned format = bytes[body + 6];
|
|
if (width < 1 || width > 32) {
|
|
error = "image says " + std::to_string(width) + " bits per cell";
|
|
return false;
|
|
}
|
|
const std::vector<uint8_t> payload(bytes.begin(),
|
|
bytes.begin() + std::ptrdiff_t(body));
|
|
if (get_u32(bytes, body + 12) != crc32(payload)) {
|
|
error = "image crc mismatch (truncated or edited)";
|
|
return false;
|
|
}
|
|
img.width = width;
|
|
img.ring = ring ? size_t(ring) : kDefaultMemoryCells;
|
|
img.format = format;
|
|
if (format == kFormatEprom) {
|
|
if (payload.size() < length) {
|
|
error = "eprom image holds " + std::to_string(payload.size()) +
|
|
" bytes, too few for " + std::to_string(length) + " instructions";
|
|
return false;
|
|
}
|
|
img.code.assign(payload.begin(), payload.begin() + std::ptrdiff_t(length));
|
|
for (size_t i = 0; i < img.code.size(); i++)
|
|
if (img.code[i] > uint8_t(opcode::inp)) {
|
|
error = "eprom instruction " + std::to_string(i) + " is 0x" +
|
|
std::to_string(int(img.code[i])) + ", not an opcode";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return tape_unpack(payload, length, img.code, error);
|
|
}
|
|
|
|
machine_result tape_run(const std::vector<uint8_t> &code,
|
|
const std::vector<uint32_t> &input,
|
|
const machine_options &opt) {
|
|
machine_result r;
|
|
const size_t ring = opt.ring ? opt.ring : kDefaultMemoryCells;
|
|
const size_t width = opt.width ? opt.width : kDefaultCellSize;
|
|
const uint32_t mask =
|
|
width >= 32 ? 0xFFFFFFFFu : uint32_t((uint64_t(1) << width) - 1);
|
|
r.cells.assign(ring, 0);
|
|
if (opt.initial) {
|
|
const size_t n = std::min(ring, opt.initial->size());
|
|
for (size_t i = 0; i < n; i++)
|
|
r.cells[i] = (*opt.initial)[i] & mask;
|
|
}
|
|
if (opt.profile)
|
|
r.cell_hits.assign(ring, 0);
|
|
|
|
size_t dp = 0;
|
|
size_t ip = 0;
|
|
size_t in_pos = 0;
|
|
std::vector<size_t> loop; // loop_stack mode: the '[' positions that were entered
|
|
|
|
// Forward skip for '[': stop on the matching ']' (the caller then advances
|
|
// past it, which is what "jump to the instruction after the loop" means).
|
|
auto skip_forward = [&](size_t at) -> size_t {
|
|
int depth = 1;
|
|
for (size_t p = at + 1; p < code.size(); p++) {
|
|
if (code[p] == uint8_t(opcode::biz)) depth++;
|
|
else if (code[p] == uint8_t(opcode::rnz)) depth--;
|
|
if (depth == 0) return p;
|
|
}
|
|
return code.size();
|
|
};
|
|
// Backward scan for ']': stop on the matching '['.
|
|
auto scan_backward = [&](size_t at) -> size_t {
|
|
int depth = 1;
|
|
for (size_t p = at; p-- > 0;) {
|
|
if (code[p] == uint8_t(opcode::rnz)) depth++;
|
|
else if (code[p] == uint8_t(opcode::biz)) depth--;
|
|
if (depth == 0) return p;
|
|
}
|
|
return code.size();
|
|
};
|
|
|
|
while (ip < code.size()) {
|
|
if (++r.steps > opt.limit) {
|
|
r.overrun = true;
|
|
r.error = "step limit";
|
|
return r;
|
|
}
|
|
if (opt.profile) {
|
|
r.op_counts[size_t(code[ip]) & 7]++;
|
|
r.cell_hits[dp]++;
|
|
}
|
|
if (opt.trace)
|
|
opt.trace(opt.trace_ctx, ip, dp, r.cells.data(), ring, code[ip]);
|
|
bool advance = true;
|
|
switch (opcode(code[ip])) {
|
|
case opcode::stp:
|
|
dp = (dp + 1) % ring;
|
|
break;
|
|
case opcode::btp:
|
|
dp = (dp + ring - 1) % ring;
|
|
break;
|
|
case opcode::ror: {
|
|
const uint32_t v = r.cells[dp];
|
|
r.cells[dp] = uint32_t(((v >> 1) | ((v & 1u) << (width - 1))) & mask);
|
|
break;
|
|
}
|
|
case opcode::flb:
|
|
r.cells[dp] ^= 1;
|
|
break;
|
|
case opcode::biz:
|
|
if ((r.cells[dp] & 1) == 0) {
|
|
const size_t partner = skip_forward(ip);
|
|
if (partner >= code.size()) {
|
|
r.error = "unbalanced '[' at instruction " + std::to_string(ip);
|
|
return r;
|
|
}
|
|
ip = partner; // the ']' itself; the advance below steps past it
|
|
} else if (opt.loop_stack) {
|
|
loop.push_back(ip); // the loop is entered: remember where it starts
|
|
}
|
|
break;
|
|
case opcode::rnz:
|
|
if ((r.cells[dp] & 1) == 1) {
|
|
r.back_jumps++;
|
|
size_t target = code.size();
|
|
if (opt.loop_stack) {
|
|
if (loop.empty()) {
|
|
r.error = "empty loop stack: ']' at instruction " + std::to_string(ip);
|
|
return r;
|
|
}
|
|
target = loop.back();
|
|
// Which entry discipline goes with which jump:
|
|
// retest -> the jump lands ON the '[', which pushes again, so this
|
|
// entry has done its job and is popped now;
|
|
// !retest -> the jump lands PAST the '[', which therefore never
|
|
// re-pushes, so the entry has to stay until the loop
|
|
// exits. Popping it here emptied the stack for the next
|
|
// iteration -- a real bug, caught by the equivalence test
|
|
// on examples/calc.r8asm.
|
|
if (opt.retest)
|
|
loop.pop_back();
|
|
} else {
|
|
target = scan_backward(ip);
|
|
if (target >= code.size()) {
|
|
r.error = "unbalanced ']' at instruction " + std::to_string(ip);
|
|
return r;
|
|
}
|
|
}
|
|
ip = opt.retest ? target : target + 1;
|
|
advance = false;
|
|
} else if (opt.loop_stack && !loop.empty()) {
|
|
loop.pop_back(); // the loop finished: drop its entry
|
|
}
|
|
break;
|
|
case opcode::out:
|
|
r.out.push_back(r.cells[dp]);
|
|
break;
|
|
case opcode::inp:
|
|
r.cells[dp] = in_pos < input.size() ? (input[in_pos++] & mask) : 0;
|
|
break;
|
|
}
|
|
if (advance)
|
|
ip++;
|
|
}
|
|
r.halted = true;
|
|
return r;
|
|
}
|