The live view drew a frame per instruction, so watching clb_digits walk its 13 cell
scratch block meant dozens of frames of "pointer moved one cell". Now a run of
STP/BTP closes into a single frame whose status line says where the pointer went
(`ptr +5 cell 91`), and `--skip-moves` does not render moves at all, only counts
them until the next data instruction.
Both are view-only: src/rot8.c, rot8_mem.c, the instruction behaviour, the step
count and the tape reader are untouched (git diff shows rot8_viz.c, rot8_viz.h --
one bool -- and vm_test.c's option parsing). The live path cannot be compared byte
for byte with a non-live run, because live mode sets quiet + replay_output=false on
purpose (the frames are the output); the checks are therefore:
* non-live stdout before and after: `--input R8F tapes/serial_echo.rot8` -> R8F,
the calculator tape at cell 11 with `12 34 + .` -> "> 0046" -- both unchanged;
* executed instructions: 280616 for that calculator run, identical to before;
* frames at -e 1 on that tape: 280616 -> 104423 (-63%) draws, and 65234 (-77%)
with --skip-moves; 215382 of the 280616 instructions are moves, in 39189 runs;
* at the default auto rate (every=1592 here) the draw count is 627 either way: a
frame that falls due inside a run is deferred to the run's end, never dropped.
ROT8_VIZ_FRAMES=1 makes a run print `draws/steps/moves/runs` to stderr, which is how
the numbers above were taken; it is documented in the README next to --live.
367 lines
13 KiB
C
367 lines
13 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "rot8/rot8.h"
|
|
#include "rot8/rot8_io.h"
|
|
#include "rot8/rot8_viz.h"
|
|
|
|
static const char *base_name(const char *path) {
|
|
const char *name = path;
|
|
for (; *path != '\0'; path++) {
|
|
if (*path == '/' || *path == '\\') {
|
|
name = path + 1;
|
|
}
|
|
}
|
|
return (name);
|
|
}
|
|
|
|
static void usage(const char *program) {
|
|
printf(
|
|
"usage: %s [options] <tape.rot8>\n"
|
|
"\n"
|
|
"Runs a ROT-8 tape. With neither --live nor --dump the tape simply\n"
|
|
"runs and prints whatever OUT prints, exactly as it always has.\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -l, --live watch the tape, redrawing the memory view in place\n"
|
|
" --skip-moves never draw pointer moves (>, <), only count them:\n"
|
|
" the next non-move instruction shows where the\n"
|
|
" pointer went (off by default; a run of moves is\n"
|
|
" normally drawn as a single frame)\n"
|
|
" -d, --dump print one static memory snapshot when the tape ends\n"
|
|
" -e, --every <n> draw a frame every <n> instructions (0 = about\n"
|
|
" 300 frames per run, the default)\n"
|
|
" -f, --fps <n> cap the animation at <n> frames per second\n"
|
|
" (0 = as fast as the terminal can take it)\n"
|
|
" -c, --color force ANSI colour on\n"
|
|
" --no-color force ANSI colour off\n"
|
|
" -t, --tty assume the far end is a terminal even when stdout\n"
|
|
" is a pipe (use it when the view does not animate)\n"
|
|
" --no-tty never draw in place, just print frames\n"
|
|
" -k, --compact short frame: the tape, without the cell inspector\n"
|
|
" --full always draw the full frame\n"
|
|
" -i, --input <v...> feed INP values up front (spaces or commas)\n"
|
|
" anything left over is asked for on the terminal\n"
|
|
" -n, --numbers numeric I/O: OUT prints the whole cell in decimal\n"
|
|
" and INP reads one decimal number (this is what the\n"
|
|
" machine used before serial became the default);\n"
|
|
" --input then takes numbers, not characters\n"
|
|
" -s, --serial serial ASCII I/O, the default when a cell holds at\n"
|
|
" least 8 bits: OUT writes the low 7 bits of the cell\n"
|
|
" as one character, INP reads one character into them\n"
|
|
" and leaves bit 7 alone; --input takes text\n"
|
|
" --parity <p> serial framing: none (default), even (7E) or odd\n"
|
|
" (7O); INP fills bit 7, OUT checks it\n"
|
|
" --stop serial framing: bit 8 is the stop bit and must be\n"
|
|
" 1 (INP sets it, OUT checks it); needs cell >= 9\n"
|
|
" --7e1 --serial --parity even --stop, i.e. a 7E1 frame in\n"
|
|
" one cell: 7 data bits, parity, stop, and 2 spare\n"
|
|
" -m, --mem <n> cells on the tape (default %u, max %u)\n"
|
|
" -b, --cell <bits> bits per cell, 1..%u (default %u)\n"
|
|
" -h, --help show this text\n"
|
|
"\n"
|
|
"A run of consecutive > / < is drawn as one frame when it ends, not one\n"
|
|
"frame per instruction; its status line says \"ptr +n\" and the cell the\n"
|
|
"pointer reached. --skip-moves drops those frames entirely.\n"
|
|
"\n"
|
|
"examples:\n"
|
|
" %s --live tapes/full_adder.rot8\n"
|
|
" %s --dump tapes/and.rot8\n"
|
|
" %s -n --input 7,5 tapes/full_adder.rot8\n"
|
|
" %s --cell 16 --dump tapes/wave.rot8\n"
|
|
" %s --mem 4096 --live tapes/wave.rot8\n"
|
|
" %s tapes/serial_echo.rot8\n"
|
|
" %s --7e1 --input R8F tapes/serial_echo.rot8\n",
|
|
program, (unsigned)ROT8_DEFAULT_MEM_SIZE, (unsigned)ROT8_MAX_MEM_SIZE,
|
|
(unsigned)ROT8_MAX_CELL_SIZE, (unsigned)ROT8_DEFAULT_CELL_SIZE, program,
|
|
program, program, program, program, program, program);
|
|
}
|
|
|
|
static bool parse_uint(const char *text, unsigned int *out) {
|
|
char *end = NULL;
|
|
unsigned long value;
|
|
if (text == NULL || *text == '\0') {
|
|
return (false);
|
|
}
|
|
value = strtoul(text, &end, 10);
|
|
if (end == NULL || *end != '\0' || value > 100000UL) {
|
|
return (false);
|
|
}
|
|
*out = (unsigned int)value;
|
|
return (true);
|
|
}
|
|
|
|
/* --mem and --cell take anything from 1 up to the machine's limit. */
|
|
static bool parse_geometry(const char *text, unsigned long max,
|
|
unsigned long *out) {
|
|
char *end = NULL;
|
|
unsigned long value;
|
|
if (text == NULL || *text == '\0') {
|
|
return (false);
|
|
}
|
|
value = strtoul(text, &end, 0);
|
|
if (end == NULL || *end != '\0' || value == 0 || value > max) {
|
|
return (false);
|
|
}
|
|
*out = value;
|
|
return (true);
|
|
}
|
|
|
|
/* "1 2 3" or "1,2,3" or "1, 2, 3": every number becomes one INP value. */
|
|
static void push_input_list(const char *text) {
|
|
while (*text != '\0') {
|
|
char *end = NULL;
|
|
long value = strtol(text, &end, 0);
|
|
if (end == text) { /* not a number: step over the separator */
|
|
text++;
|
|
continue;
|
|
}
|
|
rot8_io_push_input((rot8_data)value);
|
|
text = end;
|
|
}
|
|
}
|
|
|
|
/* Serial mode: the argument is the text itself, one character per INP. */
|
|
static void push_input_text(const char *text) {
|
|
for (; *text != '\0'; text++) {
|
|
rot8_io_push_input((rot8_data)((unsigned char)*text & ROT8_SERIAL_MASK));
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *program = base_name(argv[0]);
|
|
const char *path = NULL;
|
|
rot8_viz_opts opts;
|
|
rot8_config cfg;
|
|
char *ins; /* sized from the file by read_tape_alloc */
|
|
size_t length;
|
|
rot8_viz *viz;
|
|
/* --input is collected and only pushed once the whole command line is
|
|
* known: in serial mode the same argument means characters, not numbers,
|
|
* and the I/O mode may come after it */
|
|
const char *input_texts[64];
|
|
int input_text_count = 0;
|
|
/* Serial ASCII is the default -- the library can print decimal digits
|
|
* (ROT-8's clb_digits), so a tape usually wants characters out. A cell
|
|
* below 8 bits cannot carry one, and then numeric I/O is the only thing
|
|
* that fits: see the cell_size check further down. */
|
|
bool serial = true;
|
|
bool serial_chosen = false; /* --serial / --parity / --stop / --7e1 */
|
|
bool numbers_chosen = false;
|
|
bool stop = false;
|
|
rot8_parity parity = ROT8_PARITY_NONE;
|
|
|
|
rot8_config_default(&cfg);
|
|
memset(&opts, 0, sizeof opts);
|
|
opts.fps = 30;
|
|
opts.every = 0;
|
|
opts.color = -1;
|
|
opts.compact = -1;
|
|
opts.tty = -1;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
const char *arg = argv[i];
|
|
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
|
|
usage(program);
|
|
return (0);
|
|
} else if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "%s: --input needs a value or a list\n", program);
|
|
return (2);
|
|
}
|
|
if (input_text_count >= (int)(sizeof input_texts / sizeof input_texts[0])) {
|
|
fprintf(stderr, "%s: too many --input arguments\n", program);
|
|
return (2);
|
|
}
|
|
input_texts[input_text_count++] = argv[++i];
|
|
} else if (strcmp(arg, "-n") == 0 || strcmp(arg, "--numbers") == 0) {
|
|
serial = false;
|
|
numbers_chosen = true;
|
|
} else if (strcmp(arg, "-s") == 0 || strcmp(arg, "--serial") == 0) {
|
|
serial = true;
|
|
serial_chosen = true;
|
|
} else if (strcmp(arg, "--parity") == 0) {
|
|
const char *p = i + 1 < argc ? argv[++i] : NULL;
|
|
if (p == NULL) {
|
|
fprintf(stderr, "%s: --parity needs none, even or odd\n", program);
|
|
return (2);
|
|
}
|
|
if (strcmp(p, "none") == 0) {
|
|
parity = ROT8_PARITY_NONE;
|
|
} else if (strcmp(p, "even") == 0) {
|
|
parity = ROT8_PARITY_EVEN;
|
|
} else if (strcmp(p, "odd") == 0) {
|
|
parity = ROT8_PARITY_ODD;
|
|
} else {
|
|
fprintf(stderr, "%s: --parity wants none, even or odd, not '%s'\n",
|
|
program, p);
|
|
return (2);
|
|
}
|
|
serial = true; /* framing only exists in serial mode */
|
|
serial_chosen = true;
|
|
} else if (strcmp(arg, "--stop") == 0) {
|
|
stop = true;
|
|
serial = true;
|
|
serial_chosen = true;
|
|
} else if (strcmp(arg, "--7e1") == 0) {
|
|
serial = true;
|
|
serial_chosen = true;
|
|
parity = ROT8_PARITY_EVEN;
|
|
stop = true;
|
|
} else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) {
|
|
opts.live = true;
|
|
} else if (strcmp(arg, "--skip-moves") == 0) {
|
|
opts.skip_moves = true;
|
|
} else if (strcmp(arg, "-d") == 0 || strcmp(arg, "--dump") == 0) {
|
|
opts.final_dump = true;
|
|
} else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--color") == 0) {
|
|
opts.color = 1;
|
|
} else if (strcmp(arg, "--no-color") == 0) {
|
|
opts.color = 0;
|
|
} else if (strcmp(arg, "-k") == 0 || strcmp(arg, "--compact") == 0) {
|
|
opts.compact = 1;
|
|
} else if (strcmp(arg, "--full") == 0) {
|
|
opts.compact = 0;
|
|
} else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--tty") == 0) {
|
|
opts.tty = 1;
|
|
} else if (strcmp(arg, "--no-tty") == 0) {
|
|
opts.tty = 0;
|
|
} else if (strcmp(arg, "-e") == 0 || strcmp(arg, "--every") == 0) {
|
|
if (i + 1 >= argc || !parse_uint(argv[++i], &opts.every)) {
|
|
fprintf(stderr, "%s: --every needs a number\n", program);
|
|
return (2);
|
|
}
|
|
} else if (strcmp(arg, "-m") == 0 || strcmp(arg, "--mem") == 0) {
|
|
unsigned long cells = 0;
|
|
if (i + 1 >= argc ||
|
|
!parse_geometry(argv[i + 1], (unsigned long)ROT8_MAX_MEM_SIZE,
|
|
&cells)) {
|
|
fprintf(stderr, "%s: --mem needs 1..%u cells\n", program,
|
|
(unsigned)ROT8_MAX_MEM_SIZE);
|
|
return (2);
|
|
}
|
|
cfg.mem_size = (size_t)cells;
|
|
i++;
|
|
} else if (strcmp(arg, "-b") == 0 || strcmp(arg, "--cell") == 0) {
|
|
unsigned long bits = 0;
|
|
if (i + 1 >= argc ||
|
|
!parse_geometry(argv[i + 1], (unsigned long)ROT8_MAX_CELL_SIZE,
|
|
&bits)) {
|
|
fprintf(stderr, "%s: --cell needs 1..%u bits\n", program,
|
|
(unsigned)ROT8_MAX_CELL_SIZE);
|
|
return (2);
|
|
}
|
|
cfg.cell_size = (unsigned)bits;
|
|
i++;
|
|
} else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fps") == 0) {
|
|
if (i + 1 >= argc || !parse_uint(argv[++i], &opts.fps)) {
|
|
fprintf(stderr, "%s: --fps needs a number\n", program);
|
|
return (2);
|
|
}
|
|
} else if (arg[0] == '-' && arg[1] != '\0') {
|
|
fprintf(stderr, "%s: unknown option '%s'\n", program, arg);
|
|
usage(program);
|
|
return (2);
|
|
} else if (path != NULL) {
|
|
fprintf(stderr, "%s: only one tape at a time\n", program);
|
|
return (2);
|
|
} else {
|
|
path = arg;
|
|
}
|
|
}
|
|
|
|
if (path == NULL) {
|
|
usage(program);
|
|
return (2);
|
|
}
|
|
|
|
if (!rot8_config_apply(&cfg)) {
|
|
fprintf(stderr, "%s: %zu cells of %u bits is not a ROT-8 machine\n",
|
|
program, cfg.mem_size, cfg.cell_size);
|
|
return (2);
|
|
}
|
|
if (numbers_chosen && serial_chosen) {
|
|
fprintf(stderr,
|
|
"%s: --numbers and the serial options are mutually exclusive\n",
|
|
program);
|
|
return (2);
|
|
}
|
|
if (serial_chosen && cfg.cell_size < 8) {
|
|
fprintf(stderr,
|
|
"%s: serial I/O needs at least 8 bits per cell (got %u)\n",
|
|
program, cfg.cell_size);
|
|
return (2);
|
|
}
|
|
if (stop && cfg.cell_size < 9) {
|
|
fprintf(stderr,
|
|
"%s: a stop bit needs at least 9 bits per cell (got %u)\n",
|
|
program, cfg.cell_size);
|
|
return (2);
|
|
}
|
|
if (!numbers_chosen && !serial_chosen) {
|
|
/* Nobody said which I/O to use, so the geometry decides: a serial
|
|
* character needs 8 bits (7 data + 1 spare), and a narrower cell gets
|
|
* the numeric I/O -- that is what 1-bit tapes have always used. */
|
|
serial = cfg.cell_size >= 8;
|
|
}
|
|
rot8_io_set_serial(serial);
|
|
rot8_io_set_parity(parity);
|
|
rot8_io_set_stop(stop);
|
|
for (int i = 0; i < input_text_count; i++) {
|
|
if (serial) {
|
|
push_input_text(input_texts[i]);
|
|
} else {
|
|
push_input_list(input_texts[i]);
|
|
}
|
|
}
|
|
|
|
if ((ins = read_tape_alloc(path, &length)) == NULL) {
|
|
fprintf(stderr, "%s: cannot read tape '%s'\n", program, path);
|
|
return (1);
|
|
}
|
|
|
|
|
|
if (!opts.live && !opts.final_dump) {
|
|
rot8_run(ins, (rot8_ins_size)length);
|
|
if (serial && rot8_io_wrote_serial()) {
|
|
putchar('\n'); /* serial OUT writes no newline; end the line here */
|
|
}
|
|
if (rot8_io_errors() != 0) {
|
|
fprintf(stderr, "%s: %lu bad serial frame(s)\n", program,
|
|
rot8_io_errors());
|
|
return (4);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
opts.title = path;
|
|
/* the console under the frame shows output> already, so there is no
|
|
* point printing every OUT value again at the end */
|
|
opts.replay_output = false;
|
|
viz = rot8_viz_new(&opts);
|
|
if (viz == NULL) {
|
|
fprintf(stderr, "%s: out of memory\n", program);
|
|
return (1);
|
|
}
|
|
if (opts.live && !rot8_viz_terminal(viz) && opts.tty < 0) {
|
|
fprintf(stderr,
|
|
"%s: stdout is not a terminal, printing frames instead; "
|
|
"add -t if it is one behind a pipe\n",
|
|
program);
|
|
}
|
|
|
|
if (opts.live) {
|
|
rot8_io_set_quiet(true);
|
|
/* whatever --input did not cover gets asked for under the frame */
|
|
rot8_io_set_input_hook(rot8_viz_ask, viz);
|
|
}
|
|
rot8_run_hooked(ins, (rot8_ins_size)length, rot8_viz_hook, viz);
|
|
rot8_io_set_input_hook(NULL, NULL);
|
|
rot8_io_set_quiet(false);
|
|
|
|
rot8_viz_free(viz);
|
|
return (0);
|
|
}
|