hardware: M0 -- the packer, the RTL, and a simulator that agrees byte for byte
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.
This commit is contained in:
1 parent
0c7d0c8754
commit
9b31a51fec
13 files changed
+1621
-130
No files matched your search
@@ -0,0 +1,133 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// The ROT-8 tape as *bits*, not as text.
|
||||
//
|
||||
// The assembler emits `.rot8`: one character per instruction, which is what the
|
||||
// tools and the visualizer read. The hardware cannot afford that (an eight
|
||||
// opcode instruction set needs three bits, so text wastes 62% of a ROM and needs
|
||||
// an ASCII decoder in the datapath), so the image that goes into a ROM or a
|
||||
// block RAM is packed.
|
||||
//
|
||||
// Opcode numbering follows the enum in the reference interpreter
|
||||
// (ROT-8-viz/include/rot8/rot8.h: STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP).
|
||||
// That table *is* the hardware's decoder, so the numbering is part of the
|
||||
// interface: do not renumber it and do not change the packing.
|
||||
//
|
||||
// Packing: three bits per instruction, little end first, instructions laid out
|
||||
// consecutively, so eight instructions fill exactly three bytes. Instruction i
|
||||
// occupies bits 3*i .. 3*i+2 of the byte stream.
|
||||
enum class opcode : uint8_t {
|
||||
stp = 0, // '>'
|
||||
btp = 1, // '<'
|
||||
ror = 2, // '+'
|
||||
flb = 3, // '-'
|
||||
biz = 4, // '['
|
||||
rnz = 5, // ']'
|
||||
out = 6, // '.'
|
||||
inp = 7, // ','
|
||||
};
|
||||
|
||||
// Text (".rot8") -> opcodes. A character outside "><+-[].," is an error: the
|
||||
// assembler never emits one, so seeing one means the tape is broken, and
|
||||
// silently skipping it would hide that.
|
||||
bool tape_decode(const std::string &text, std::vector<uint8_t> &code,
|
||||
std::string &error);
|
||||
// Opcodes -> text. `tape_decode` inverts this; a round trip has to be exact.
|
||||
std::string tape_encode(const std::vector<uint8_t> &code);
|
||||
|
||||
// 3 bits per instruction, little end first.
|
||||
std::vector<uint8_t> tape_pack(const std::vector<uint8_t> &code);
|
||||
// The inverse. `length` comes from outside: the byte stream has padding bits at
|
||||
// the top, so it cannot be recovered from the bytes alone.
|
||||
bool tape_unpack(const std::vector<uint8_t> &bytes, size_t length,
|
||||
std::vector<uint8_t> &code, std::string &error);
|
||||
|
||||
// ---- the .r8t image ----------------------------------------------------------
|
||||
//
|
||||
// The file a programmer burns or a block RAM initialises from: the packed body
|
||||
// followed by a 16 byte trailer, so that address 0 of the medium is instruction
|
||||
// 0 and the program can be executed in place (the trailer sits past the halt
|
||||
// point and is never fetched).
|
||||
//
|
||||
// trailer +0 u32 instruction count, little endian
|
||||
// +4 u8 cell width the tape was assembled for
|
||||
// +5 u8 ring size in cells (0 = more than 255, use the default)
|
||||
// +6 u8 format version (1)
|
||||
// +7 u8 reserved (0)
|
||||
// +8 "R8T1"
|
||||
// +12 u32 crc32 of the body, little endian
|
||||
struct tape_image {
|
||||
std::vector<uint8_t> code;
|
||||
size_t width = 0;
|
||||
size_t ring = 0;
|
||||
};
|
||||
|
||||
std::vector<uint8_t> tape_image_encode(const std::vector<uint8_t> &code,
|
||||
size_t width, size_t ring);
|
||||
// True when the bytes end with the trailer (the loader uses this to tell an
|
||||
// image from a text tape without trusting the file name).
|
||||
bool tape_is_image(const std::vector<uint8_t> &bytes);
|
||||
bool tape_image_decode(const std::vector<uint8_t> &bytes, tape_image &img,
|
||||
std::string &error);
|
||||
|
||||
// ---- the machine -------------------------------------------------------------
|
||||
//
|
||||
// A model of the *hardware*, not of the assembler: it executes a packed image
|
||||
// (or the opcode vector, which is the same thing unpacked) and reports what a
|
||||
// board would report -- the output stream, the final cells, and the step count.
|
||||
//
|
||||
// Why it exists: `r8forge_tape_run` reads text and `r8forge_derive` reads source;
|
||||
// both are software interpreters. The board reads three bit opcodes out of a
|
||||
// ROM and has to implement the bracket jump one way or another, so the two
|
||||
// things that can go wrong in the hardware -- the packing and the bracket
|
||||
// strategy -- need a model that is compared against the text level tools.
|
||||
struct machine_options {
|
||||
size_t ring = 0; // data cells, 0 = the build's default
|
||||
size_t width = 0; // bits per cell, 0 = the build's default
|
||||
size_t limit = 20'000'000; // step limit, the same as the other VMs
|
||||
// The board has two ways to implement ']': scan backwards through the tape
|
||||
// (what the interpreters do) or keep a stack of loop entry positions (what the
|
||||
// hardware does, because scanning is O(body) *per iteration*). Both are
|
||||
// compared here so the equivalence is a test and not a claim.
|
||||
bool loop_stack = false;
|
||||
// The contract (SPEC section 1, and ROT-8-viz) says ']' jumps back *onto* the
|
||||
// '[' so it re-executes. The interpreters jump to the instruction after it,
|
||||
// which prints the same thing but is one step shorter per loop. Default to
|
||||
// the interpreters' reading so documented step counts stay comparable;
|
||||
// `retest` is the contract reading, and the difference is exactly the number
|
||||
// of taken ']' instructions, which the test asserts.
|
||||
bool retest = false;
|
||||
// Bookkeeping for the profiler. Off by default: counting every step is not
|
||||
// free on a 20M step run.
|
||||
bool profile = false;
|
||||
// Optional starting cells (the debugger's `addr=value`). Copies over the
|
||||
// zeroed ring, so a shorter vector only overwrites its own prefix.
|
||||
const std::vector<uint32_t> *initial = nullptr;
|
||||
// Called before each instruction when set (the debugger view).
|
||||
void (*trace)(void *ctx, size_t ip, size_t dp, const uint32_t *cells,
|
||||
size_t ring, uint8_t op) = nullptr;
|
||||
void *trace_ctx = nullptr;
|
||||
};
|
||||
|
||||
struct machine_result {
|
||||
bool halted = false; // ran off the end of the tape
|
||||
bool overrun = false; // hit the step limit
|
||||
size_t steps = 0;
|
||||
size_t back_jumps = 0; // taken ']' instructions (the step-count difference)
|
||||
std::string error; // unbalanced bracket or bad opcode, empty when fine
|
||||
std::vector<uint32_t> out;
|
||||
std::vector<uint32_t> cells;
|
||||
// Filled in only when machine_options::profile is set.
|
||||
std::array<size_t, 8> op_counts{};
|
||||
std::vector<size_t> cell_hits;
|
||||
};
|
||||
|
||||
machine_result tape_run(const std::vector<uint8_t> &code,
|
||||
const std::vector<uint32_t> &input,
|
||||
const machine_options &opt = {});
|
||||
Reference in new issue
Block a user