-s / --serial switches INP and OUT from decimal numbers to characters. The
character is the low 7 bits of the cell, so the 8th bit stays the tape's own:
INP writes bits 0..6 and leaves everything from bit 7 up untouched, which is
where a flag, a length or a counter can live without the I/O trampling it.
Checked by hand: a cell preset to 0x80 that then reads 'A' ends at 0xC1, at 8
and at 11 bits per cell.
* include/rot8/rot8_io.h, src/rot8_io.c: the mode itself, plus
rot8_io_wrote_serial() so the CLI can close the line (a serial OUT writes
no newline, that is the point); stdin is read as characters as well, and
EOF feeds 0 instead of blocking.
* src/rot8_viz.c: the frame shows characters in this mode -- the out/in
boxes list them, the console prints output> 'R' 82, unprintables become
\x0A so a frame stays one line of ASCII, and the live prompt takes the
first character typed instead of parsing a number.
* tests/vm_test.c: -s/--serial, a cell_size < 8 check (a 7 bit character
plus a flag does not fit in fewer bits), --input becomes the text itself
(one INP per character, which is why the arguments are now collected and
pushed after the whole command line is known), and one trailing newline
at the end because serial OUT has none.
* tapes/serial_echo.rot8: ,.,.,. -- read three characters, echo them.
* README: what the mode does, the bit 7 rule, and three ways to run it.
Decimal mode is unchanged: --input 7,5 still prints 7 and the existing demos
still print their numbers.
281 lines
9.0 KiB
C
281 lines
9.0 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"
|
|
" -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"
|
|
" -s, --serial serial ASCII: OUT writes the low 7 bits of the\n"
|
|
" cell as one character, INP reads one character\n"
|
|
" into them and leaves bit 7 alone (cell >= 8 bits);\n"
|
|
" --input then takes characters, not numbers\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"
|
|
"examples:\n"
|
|
" %s --live tapes/full_adder.rot8\n"
|
|
" %s --dump tapes/and.rot8\n"
|
|
" %s --live --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 --serial --cell 8 tapes/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);
|
|
}
|
|
|
|
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[INS_SIZE];
|
|
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 --serial may come after it */
|
|
const char *input_texts[64];
|
|
int input_text_count = 0;
|
|
bool serial = false;
|
|
|
|
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, "-s") == 0 || strcmp(arg, "--serial") == 0) {
|
|
serial = true;
|
|
} else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) {
|
|
opts.live = 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 (serial && 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);
|
|
}
|
|
rot8_io_set_serial(serial);
|
|
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 (!read_tape(path, ins, sizeof ins)) {
|
|
fprintf(stderr, "%s: cannot read tape '%s'\n", program, path);
|
|
return (1);
|
|
}
|
|
length = strlen(ins);
|
|
|
|
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 */
|
|
}
|
|
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);
|
|
}
|