Tape length and cell width are chosen at run time
MEM_SIZE and CELL_SIZE were compile time constants: 256 cells of 8 bits, with
rot8_data pinned to uint8_t. Both now come from a rot8_config that a machine
takes a copy of when it is created:
rot8_config cfg = {4096, 16}; /* cells, bits per cell */
rot8_config_apply(&cfg); /* machines from now on */
rot8_test --mem 4096 --cell 16 --dump tapes/wave.rot8
8, 16 and 32 bit cells are supported. ROR rotates inside the configured width
(four rotations of a set bit give 0x10, 0x1000 and 0x10000000), FLB, BIZ and RNZ
still work on the low bit, and INP masks what it is given (70000 becomes 4464 in
a 16 bit cell and 112 in an 8 bit one). rot8_data is uint32_t now so that a
cell holds the widest case.
The tape is a ring: stepping off either end lands on the other, which is what
makes a short tape safe to watch and matches what ROR already does inside a
cell.
The view follows the geometry. A cell takes 2, 4 or 8 hex digits and a row
holds 16, 8 or 4 of them, so the frame stays 56 characters wide; wider cells get
a nibble grouped bit string instead of one column per bit. A tape longer than
16 rows is drawn as a window around the pointer, with its range on the last
line.
Nothing changes for the default geometry: --dump output for and, or,
full_adder, wave and test is byte identical to before, and so is plain OUT
output. The places that printed a cell with %d/%u print unsigned long now, so a
32 bit cell cannot come out negative.
This commit is contained in:
1 parent
d545899107
commit
4ae4bea2dd
8 files changed
+369
-61
No files matched your search
@@ -24,6 +24,9 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
> 方便起见,INP与OUT的实现不做定义,只要INP能改变整个Cell而OUT能读取整个Cell即可.\
|
||||
> 例如本项目以输入/输出十进制整数作为INP/OUT.
|
||||
|
||||
> Cell 的位宽不属于指令集:任何位宽都成立(LSB 永远是那一位,`ROR` 在这个位宽里循环).\
|
||||
> 本项目默认 8 bit,但位宽和纸带长度都能在运行时改 —— 见下面的「纸带尺寸与 Cell 位宽」.
|
||||
|
||||
## 使用
|
||||
|
||||
```sh
|
||||
@@ -32,7 +35,8 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
|
||||
## 可视化内存
|
||||
|
||||
虚拟机可以一边执行 tape,一边把整条纸带(256 个 Cell x 8 bit)画成一张实时视图:
|
||||
虚拟机可以一边执行 tape,一边把整条纸带(默认 256 个 Cell x 8 bit,`--mem` / `--cell` 可改)
|
||||
画成一张实时视图:
|
||||
|
||||
```sh
|
||||
./build/rot8_test --live tapes/wave.rot8
|
||||
@@ -77,8 +81,8 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
| --- | --- |
|
||||
| `step / pc / op` | 已经执行的指令数,程序计数器,以及 `pc` 处即将执行的指令 |
|
||||
| `cell ... value` | 指针所指 Cell 的编号、十进制值与十六进制值 |
|
||||
| `b7 ... b0` | 该 Cell 的 8 位,`#` 为 1,`.` 为 0 |
|
||||
| 纸带 | 每行 16 个 Cell,行首是十六进制地址,`^^` 标出数据指针 |
|
||||
| `b7 ... b0` | 该 Cell 的各位,`#` 为 1,`.` 为 0;宽 Cell 改画一行按 4 位分组的数据 |
|
||||
| 纸带 | 每行 16 个 Cell(宽 Cell 会变少),行首是十六进制地址,`^^` 标出数据指针;纸带比画面长时只画指针附近的一窗,末行注明窗口范围 |
|
||||
| `out` | 到此刻为止 `OUT` 输出的值(保留最近 10 个) |
|
||||
|
||||
参数:
|
||||
@@ -93,6 +97,8 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
| `-k`, `--compact` / `--full` | 强制精简画面/完整画面 |
|
||||
| `-c`, `--color` / `--no-color` | 强制开/关 ANSI 颜色(默认跟随终端) |
|
||||
| `-i`, `--input <v...>` | 预先喂给 `INP` 的值(空格或逗号分隔) |
|
||||
| `-m`, `--mem <n>` | 纸带长度(单位 Cell),默认 256,范围 1..1048576 |
|
||||
| `-b`, `--cell <bits>` | Cell 位宽,`8`(默认)/`16`/`32` |
|
||||
| `-h`, `--help` | 帮助 |
|
||||
|
||||
不加 `--live` 与 `--dump` 时行为与以前完全一致:直接跑完,只留下 `OUT` 的输出.\
|
||||
@@ -103,6 +109,35 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
可以强制完整版. 用 `--tty` 之前那套原地重绘才生效,所以**如果你的终端其实是通过管道接进来的**
|
||||
(网页版终端、某些 IDE 的面板,程序 `_isatty` 看不出来),画面会变成一帧帧往下滚 —— 加个 `-t` 就好了.
|
||||
|
||||
## 纸带尺寸与 Cell 位宽
|
||||
|
||||
两者都不在指令集里,运行时可以选:
|
||||
|
||||
```sh
|
||||
rot8_test --mem 4096 tapes/wave.rot8 # 4096 格的纸带
|
||||
rot8_test --cell 16 --dump tapes/wave.rot8 # 16 bit 的 Cell
|
||||
rot8_test --mem 64 --cell 32 --live tapes/wave.rot8
|
||||
```
|
||||
|
||||
- **位宽 8 / 16 / 32**:`ROR` 在这个位宽里循环(16 bit 时 bit0 转到 bit15),`FLB` 翻 LSB,
|
||||
`BIZ`/`RNZ` 仍然只看 LSB,`INP` 的值截到该位宽. 同一条 `-++++`(置位后转 4 次)在
|
||||
8 / 16 / 32 bit 下分别是 `0x10` / `0x1000` / `0x10000000`,可以直接拿 `--dump` 看.
|
||||
- **纸带长度**:指针走出纸带会**绕回另一端**(和 Cell 的 `ROR` 一样是个环),所以也不会越界.
|
||||
默认 256,范围 1..1048576. 比画面能画的行数长时,视图只画指针附近的一窗,末行注明窗口范围.
|
||||
|
||||
内嵌用法:
|
||||
|
||||
```c
|
||||
rot8_config cfg = {4096, 16}; /* 纸带长度, Cell 位宽 */
|
||||
rot8_config_apply(&cfg); /* 之后创建的机器都用这个几何 */
|
||||
rot8_config_default(&cfg); /* 恢复 256 x 8 */
|
||||
size_t cells = rot8_mem_cells(iter);
|
||||
unsigned bits = rot8_cell_width(iter);
|
||||
```
|
||||
|
||||
`rot8_config_apply` 参数越界时返回 `false` 并且什么都不改. 默认几何(256 x 8)下的行为与
|
||||
以前逐字节一致:`--dump` 的画面、`OUT` 的输出都没有变.
|
||||
|
||||
## 输入
|
||||
|
||||
`INP` 现在按这个顺序取值:先吃 `--input` 排好的队列,再问现场(live 模式),最后才回落到
|
||||
|
||||
+29
-5
@@ -5,16 +5,20 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MEM_SIZE 256
|
||||
/* Geometry. A tape is mem_size cells and a cell is cell_bits wide; the
|
||||
* defaults are the historical 256 x 8. Both are picked at run time
|
||||
* (rot8_config_apply), so one binary can watch a 256 x 8 tape and a 4096 x 32
|
||||
* one. rot8_data is always wide enough for the largest cell. */
|
||||
#define ROT8_DEFAULT_MEM_SIZE 256
|
||||
#define ROT8_DEFAULT_CELL_BITS 8
|
||||
#define ROT8_MAX_CELL_BITS 32
|
||||
#define ROT8_MAX_MEM_SIZE (1u << 20)
|
||||
#define INS_SIZE 65536
|
||||
|
||||
typedef uint8_t rot8_data;
|
||||
typedef uint32_t rot8_data;
|
||||
typedef uint8_t rot8_loop_depth;
|
||||
typedef uint32_t rot8_ins_size;
|
||||
|
||||
// #define DATA_WIDTH (sizeof(rot8_data) * CHAR_BIT)
|
||||
#define CELL_SIZE 8
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[(bc)])
|
||||
|
||||
typedef enum {
|
||||
@@ -30,12 +34,30 @@ typedef enum {
|
||||
|
||||
typedef struct rot8_iter rot8_iter;
|
||||
|
||||
/* Tape geometry. A machine copies these when it is created, so changing them
|
||||
* later only affects machines made afterwards. */
|
||||
typedef struct rot8_config {
|
||||
size_t mem_size; /* cells on the tape */
|
||||
unsigned cell_bits; /* bits per cell: 8, 16 or 32 */
|
||||
} rot8_config;
|
||||
|
||||
void rot8_config_default(rot8_config *cfg);
|
||||
/* Make cfg the geometry for machines created from now on. Returns false and
|
||||
* changes nothing when the numbers are out of range. */
|
||||
bool rot8_config_apply(const rot8_config *cfg);
|
||||
const rot8_config *rot8_config_current(void);
|
||||
/* Mask that keeps the low cell_bits of a value. */
|
||||
rot8_data rot8_cell_mask(unsigned cell_bits);
|
||||
|
||||
/* Called once after every executed instruction (it already points at the
|
||||
* next one). ctx is whatever rot8_run_hooked was handed, so a debugger or
|
||||
* a visualizer can carry its own state without touching the machine. */
|
||||
typedef void (*rot8_hook)(rot8_iter *iter, void *ctx);
|
||||
|
||||
void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins);
|
||||
/* Same, with an explicit geometry instead of the default one. */
|
||||
void rot8_iter_init_cfg(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins,
|
||||
const rot8_config *cfg);
|
||||
// void rot8_iter_free(rot8_iter *iter);
|
||||
|
||||
void step_ins(rot8_iter *iter);
|
||||
@@ -59,6 +81,8 @@ void rot8_run_hooked(char raw_ins[], rot8_ins_size step, rot8_hook hook,
|
||||
rot8_data *rot8_iter_mem(rot8_iter *iter); /* base of the tape */
|
||||
rot8_data *rot8_iter_cell(rot8_iter *iter); /* cell under the pointer */
|
||||
rot8_bytecode *rot8_iter_ins(rot8_iter *iter); /* base of the program */
|
||||
size_t rot8_mem_cells(const rot8_iter *iter); /* tape length, in cells */
|
||||
unsigned rot8_cell_width(const rot8_iter *iter); /* bits in a cell */
|
||||
ptrdiff_t rot8_mem_offset(rot8_iter *iter); /* pointer - tape base */
|
||||
ptrdiff_t rot8_ins_offset(rot8_iter *iter); /* pc - program base */
|
||||
size_t rot8_ins_length(rot8_iter *iter); /* instructions loaded */
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
#define ROT8_MEM_H
|
||||
#include "rot8/rot8.h"
|
||||
|
||||
void rot8_mem_init(rot8_data **mem);
|
||||
void rot8_mem_init(rot8_data **mem); /* the configured number of cells */
|
||||
void rot8_mem_init_size(rot8_data **mem, size_t cells);
|
||||
void rot8_mem_free(rot8_data *mem);
|
||||
|
||||
void rot8_ins_init(rot8_bytecode **ins);
|
||||
|
||||
+75
-6
@@ -10,6 +10,11 @@ struct rot8_iter {
|
||||
rot8_bytecode *ins_now;
|
||||
rot8_loop_depth loop_depth;
|
||||
|
||||
/* Geometry, copied from the config when the machine was made. */
|
||||
size_t mem_size;
|
||||
unsigned cell_bits;
|
||||
rot8_data mask;
|
||||
|
||||
/* Not touched by the instruction set: kept so that hooks and tools can
|
||||
* locate the pointer and the program counter inside their buffers. */
|
||||
rot8_data *mem_base;
|
||||
@@ -20,6 +25,40 @@ struct rot8_iter {
|
||||
void *hook_ctx;
|
||||
};
|
||||
|
||||
/* The geometry machines are born with. rot8_config_apply replaces it. */
|
||||
static rot8_config config_current = {ROT8_DEFAULT_MEM_SIZE,
|
||||
ROT8_DEFAULT_CELL_BITS};
|
||||
|
||||
void rot8_config_default(rot8_config *cfg) {
|
||||
if (cfg != NULL) {
|
||||
cfg->mem_size = ROT8_DEFAULT_MEM_SIZE;
|
||||
cfg->cell_bits = ROT8_DEFAULT_CELL_BITS;
|
||||
}
|
||||
}
|
||||
|
||||
bool rot8_config_apply(const rot8_config *cfg) {
|
||||
if (cfg == NULL) {
|
||||
return (false);
|
||||
}
|
||||
if (cfg->mem_size == 0 || cfg->mem_size > ROT8_MAX_MEM_SIZE) {
|
||||
return (false);
|
||||
}
|
||||
if (cfg->cell_bits != 8 && cfg->cell_bits != 16 && cfg->cell_bits != 32) {
|
||||
return (false);
|
||||
}
|
||||
config_current = *cfg;
|
||||
return (true);
|
||||
}
|
||||
|
||||
const rot8_config *rot8_config_current(void) { return (&config_current); }
|
||||
|
||||
rot8_data rot8_cell_mask(unsigned cell_bits) {
|
||||
if (cell_bits >= ROT8_MAX_CELL_BITS) {
|
||||
return ((rot8_data)~(rot8_data)0);
|
||||
}
|
||||
return ((rot8_data)(((rot8_data)1 << cell_bits) - 1u));
|
||||
}
|
||||
|
||||
void step_ins(rot8_iter *iter) {
|
||||
switch (*(iter->ins_now)) {
|
||||
case STP:
|
||||
@@ -103,7 +142,8 @@ void rot8_run_hooked(char raw_ins[], rot8_ins_size step, rot8_hook hook,
|
||||
rot8_ins_free(ins);
|
||||
}
|
||||
|
||||
void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins) {
|
||||
void rot8_iter_init_cfg(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins,
|
||||
const rot8_config *cfg) {
|
||||
iter->mem_now = mem;
|
||||
iter->ins_now = ins;
|
||||
iter->loop_depth = 0;
|
||||
@@ -113,15 +153,38 @@ void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins) {
|
||||
iter->steps = 0;
|
||||
iter->hook = NULL;
|
||||
iter->hook_ctx = NULL;
|
||||
iter->mem_size = cfg != NULL ? cfg->mem_size : config_current.mem_size;
|
||||
iter->cell_bits = cfg != NULL ? cfg->cell_bits : config_current.cell_bits;
|
||||
iter->mask = rot8_cell_mask(iter->cell_bits);
|
||||
}
|
||||
|
||||
void rot8_stp(rot8_iter *iter) { iter->mem_now++; }
|
||||
void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins) {
|
||||
rot8_iter_init_cfg(iter, mem, ins, &config_current);
|
||||
}
|
||||
|
||||
void rot8_btp(rot8_iter *iter) { iter->mem_now--; }
|
||||
/* The tape is a ring: stepping off one end lands on the other, so a program
|
||||
* cannot wander into memory that is not part of the machine. */
|
||||
void rot8_stp(rot8_iter *iter) {
|
||||
iter->mem_now++;
|
||||
if (iter->mem_now >= iter->mem_base + iter->mem_size) {
|
||||
iter->mem_now = iter->mem_base;
|
||||
}
|
||||
}
|
||||
|
||||
void rot8_btp(rot8_iter *iter) {
|
||||
if (iter->mem_now == iter->mem_base) {
|
||||
iter->mem_now = iter->mem_base + iter->mem_size - 1;
|
||||
} else {
|
||||
iter->mem_now--;
|
||||
}
|
||||
}
|
||||
|
||||
void rot8_ror(rot8_iter *iter) {
|
||||
*(iter->mem_now) = ((*(iter->mem_now) & 1) << (CELL_SIZE - 1) |
|
||||
(*(iter->mem_now)) >> 1); // Rotate right pointed data
|
||||
rot8_data value = *(iter->mem_now);
|
||||
unsigned bits = iter->cell_bits;
|
||||
rot8_data rotated =
|
||||
(rot8_data)((value >> 1) | ((value & 1u) << (bits - 1)));
|
||||
*(iter->mem_now) = rotated & iter->mask; // Rotate right pointed data
|
||||
}
|
||||
|
||||
void rot8_flb(rot8_iter *iter) { *(iter->mem_now) ^= 1; }
|
||||
@@ -157,7 +220,9 @@ void rot8_rnz(rot8_iter *iter) {
|
||||
|
||||
rot8_data rot8_out(rot8_iter *iter) { return (*(iter->mem_now)); }
|
||||
|
||||
void rot8_inp(rot8_iter *iter, rot8_data input) { *(iter->mem_now) = input; }
|
||||
void rot8_inp(rot8_iter *iter, rot8_data input) {
|
||||
*(iter->mem_now) = input & iter->mask;
|
||||
}
|
||||
|
||||
rot8_data *rot8_iter_mem(rot8_iter *iter) { return (iter->mem_base); }
|
||||
|
||||
@@ -165,6 +230,10 @@ rot8_data *rot8_iter_cell(rot8_iter *iter) { return (iter->mem_now); }
|
||||
|
||||
rot8_bytecode *rot8_iter_ins(rot8_iter *iter) { return (iter->ins_base); }
|
||||
|
||||
size_t rot8_mem_cells(const rot8_iter *iter) { return (iter->mem_size); }
|
||||
|
||||
unsigned rot8_cell_width(const rot8_iter *iter) { return (iter->cell_bits); }
|
||||
|
||||
ptrdiff_t rot8_mem_offset(rot8_iter *iter) {
|
||||
return (iter->mem_now - iter->mem_base);
|
||||
}
|
||||
|
||||
+3
-3
@@ -44,7 +44,7 @@ size_t rot8_io_pending_input(void) {
|
||||
void rot8_out_io(rot8_iter *it) {
|
||||
// putchar(out(it));
|
||||
if (!io_quiet) {
|
||||
printf("%d\n", rot8_out(it));
|
||||
printf("%lu\n", (unsigned long)rot8_out(it));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ void rot8_inp_io(rot8_iter *it) {
|
||||
input_hook(it, input_hook_ctx, &value)) {
|
||||
/* the hook filled value in */
|
||||
} else {
|
||||
int n;
|
||||
if (scanf("%d", &n) != 1) {
|
||||
long n;
|
||||
if (scanf("%ld", &n) != 1) {
|
||||
n = 0;
|
||||
}
|
||||
value = (rot8_data)n;
|
||||
|
||||
+12
-2
@@ -5,9 +5,19 @@
|
||||
|
||||
#include "rot8/rot8.h"
|
||||
|
||||
void rot8_mem_init_size(rot8_data **mem, size_t cells) {
|
||||
if (cells == 0) {
|
||||
*mem = NULL;
|
||||
return;
|
||||
}
|
||||
*mem = malloc(sizeof(**mem) * cells);
|
||||
if (*mem != NULL) {
|
||||
memset(*mem, 0, sizeof(**mem) * cells);
|
||||
}
|
||||
}
|
||||
|
||||
void rot8_mem_init(rot8_data **mem) {
|
||||
*mem = malloc(sizeof(**mem) * MEM_SIZE);
|
||||
memset(*mem, 0, sizeof(**mem) * MEM_SIZE);
|
||||
rot8_mem_init_size(mem, rot8_config_current()->mem_size);
|
||||
}
|
||||
|
||||
void rot8_mem_free(rot8_data *mem) { free(mem); }
|
||||
|
||||
+158
-39
@@ -22,12 +22,14 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
/* The frame is 56 characters wide between its bars: a 7 column gutter, then
|
||||
* the 16 cells of a hex dump row at 3 characters each. */
|
||||
/* The frame is 56 characters wide between its bars: a gutter, then as many
|
||||
* cells as a hex dump row can hold. A cell can be 8, 16 or 32 bits wide, so
|
||||
* the field width, the number of columns and the number of rows all follow
|
||||
* from the geometry; a tape longer than GRID_ROWS_MAX rows is shown as a
|
||||
* window that follows the pointer. */
|
||||
#define VIZ_INNER 56
|
||||
#define VIZ_MAX_TEXT (VIZ_INNER - 2)
|
||||
#define GRID_COLS 16
|
||||
#define GRID_ROWS ((MEM_SIZE + GRID_COLS - 1) / GRID_COLS)
|
||||
#define GRID_ROWS_MAX 16
|
||||
#define OUTPUT_SHOWN 10
|
||||
#define FRAME_LINES 30 /* lines in a full frame */
|
||||
#define FRAMES_TARGET 300 /* frames an automatic run aims for */
|
||||
@@ -282,35 +284,91 @@ static void viz_status(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
line_close(&ln);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* geometry helpers
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
/* Hex digits a cell needs: 2 for 8 bits, 4 for 16, 8 for 32. */
|
||||
static int viz_cell_digits(unsigned bits) { return ((int)(bits / 4)); }
|
||||
|
||||
/* Cells that still fit on one row of the dump (3 characters each at 8 bits). */
|
||||
static int viz_grid_cols(unsigned bits) {
|
||||
switch (bits) {
|
||||
case 16:
|
||||
return (8);
|
||||
case 32:
|
||||
return (4);
|
||||
default:
|
||||
return (16);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hex digits an address needs, so that labels line up. */
|
||||
static int viz_addr_digits(size_t cells) {
|
||||
if (cells <= 0x100u) {
|
||||
return (2);
|
||||
}
|
||||
if (cells <= 0x10000u) {
|
||||
return (4);
|
||||
}
|
||||
return (6);
|
||||
}
|
||||
|
||||
static bool viz_ptr_on_tape(rot8_iter *iter, ptrdiff_t *offset) {
|
||||
ptrdiff_t ptr = rot8_mem_offset(iter);
|
||||
*offset = ptr;
|
||||
return (ptr >= 0 && ptr < (ptrdiff_t)MEM_SIZE);
|
||||
return (ptr >= 0 && (size_t)ptr < rot8_mem_cells(iter));
|
||||
}
|
||||
|
||||
/* Decimal digits a value needs, so that columns do not shift between frames. */
|
||||
static int viz_decimal_digits(unsigned long value) {
|
||||
int digits = 1;
|
||||
while (value >= 10) {
|
||||
value /= 10;
|
||||
digits++;
|
||||
}
|
||||
return (digits);
|
||||
}
|
||||
|
||||
static void viz_focus(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
char buf[96];
|
||||
char fmt[64];
|
||||
ptrdiff_t ptr;
|
||||
size_t cells = rot8_mem_cells(iter);
|
||||
int bits = (int)rot8_cell_width(iter);
|
||||
int digits = viz_cell_digits((unsigned)bits);
|
||||
int ptr_w = viz_decimal_digits((unsigned long)(cells - 1));
|
||||
int value_w = viz_decimal_digits((unsigned long)rot8_cell_mask((unsigned)bits));
|
||||
viz_line ln;
|
||||
|
||||
if (ptr_w < 3) {
|
||||
ptr_w = 3;
|
||||
}
|
||||
line_open(&ln, viz, out, color);
|
||||
if (viz_ptr_on_tape(iter, &ptr)) {
|
||||
unsigned int value = *rot8_iter_cell(iter);
|
||||
char shown = (value >= 0x20 && value < 0x7f) ? (char)value : '.';
|
||||
snprintf(buf, sizeof buf, "cell %3ld/%d value %3u = 0x%02X '%c'",
|
||||
(long)ptr, MEM_SIZE, value, value, shown);
|
||||
unsigned long value = (unsigned long)*rot8_iter_cell(iter);
|
||||
char shown = (value >= 0x20UL && value < 0x7fUL) ? (char)value : '.';
|
||||
snprintf(fmt, sizeof fmt,
|
||||
"cell %%%dld/%%zu value %%%dlu = 0x%%0%dlX '%%c'", ptr_w,
|
||||
value_w, digits);
|
||||
snprintf(buf, sizeof buf, fmt, (long)ptr, cells, value, value, shown);
|
||||
} else {
|
||||
snprintf(buf, sizeof buf, "cell ---/%d pointer off the tape",
|
||||
MEM_SIZE);
|
||||
snprintf(fmt, sizeof fmt, "cell %%%ds/%%zu pointer off the tape",
|
||||
ptr_w);
|
||||
snprintf(buf, sizeof buf, fmt, "----", cells);
|
||||
}
|
||||
line_put(&ln, buf);
|
||||
line_close(&ln);
|
||||
}
|
||||
|
||||
/* The bits of the pointed cell. Up to a byte every bit gets its own column;
|
||||
* wider cells are drawn as one nibble-grouped string so that 32 bits still fit
|
||||
* in the frame. */
|
||||
static void viz_bits(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
char field[8];
|
||||
ptrdiff_t ptr;
|
||||
unsigned int value = 0;
|
||||
rot8_data value = 0;
|
||||
unsigned bits = rot8_cell_width(iter);
|
||||
int bit;
|
||||
viz_line ln;
|
||||
|
||||
@@ -318,9 +376,31 @@ static void viz_bits(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
value = *rot8_iter_cell(iter);
|
||||
}
|
||||
|
||||
if (bits > 8) {
|
||||
unsigned i;
|
||||
char label[40];
|
||||
line_open(&ln, viz, out, color);
|
||||
snprintf(label, sizeof label, " bits b%u .. b0", bits - 1);
|
||||
line_style(&ln, label, ANSI_DIM);
|
||||
line_close(&ln);
|
||||
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, " ");
|
||||
for (i = bits; i > 0; i--) {
|
||||
bool set = ((value >> (i - 1)) & 1u) != 0;
|
||||
snprintf(field, sizeof field, "%s", set ? "1" : ".");
|
||||
line_style(&ln, field, set ? ANSI_BIT_ON : ANSI_BIT_OFF);
|
||||
if (i != 1 && (i - 1) % 4 == 0) {
|
||||
line_put(&ln, " ");
|
||||
}
|
||||
}
|
||||
line_close(&ln);
|
||||
return;
|
||||
}
|
||||
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, " ");
|
||||
for (bit = 7; bit >= 0; bit--) {
|
||||
for (bit = (int)bits - 1; bit >= 0; bit--) {
|
||||
snprintf(field, sizeof field, " b%d", bit);
|
||||
line_style(&ln, field, ANSI_DIM);
|
||||
}
|
||||
@@ -328,7 +408,7 @@ static void viz_bits(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, " ");
|
||||
for (bit = 7; bit >= 0; bit--) {
|
||||
for (bit = (int)bits - 1; bit >= 0; bit--) {
|
||||
bool set = ((value >> bit) & 1u) != 0;
|
||||
snprintf(field, sizeof field, "%s", set ? " #" : " .");
|
||||
line_style(&ln, field, set ? ANSI_BIT_ON : ANSI_BIT_OFF);
|
||||
@@ -338,37 +418,62 @@ static void viz_bits(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
|
||||
static void viz_grid(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
rot8_data *mem = rot8_iter_mem(iter);
|
||||
size_t cells = rot8_mem_cells(iter);
|
||||
unsigned bits = rot8_cell_width(iter);
|
||||
int cols = viz_grid_cols(bits);
|
||||
int digits = viz_cell_digits(bits);
|
||||
int addr = viz_addr_digits(cells);
|
||||
size_t total_rows = (cells + (size_t)cols - 1) / (size_t)cols;
|
||||
size_t shown = total_rows > GRID_ROWS_MAX ? GRID_ROWS_MAX : total_rows;
|
||||
size_t first_row = 0;
|
||||
ptrdiff_t ptr;
|
||||
int row, col;
|
||||
size_t row;
|
||||
int col;
|
||||
viz_line ln;
|
||||
|
||||
(void)viz_ptr_on_tape(iter, &ptr);
|
||||
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, " | ");
|
||||
for (col = 0; col < GRID_COLS; col++) {
|
||||
char label[8];
|
||||
snprintf(label, sizeof label, "+%X ", col);
|
||||
line_style(&ln, label, ANSI_DIM);
|
||||
/* A tape that does not fit in the frame is watched through a window that
|
||||
* keeps the pointer in the middle. */
|
||||
if (total_rows > shown) {
|
||||
size_t here = (ptr >= 0) ? (size_t)ptr / (size_t)cols : 0;
|
||||
first_row = here > shown / 2 ? here - shown / 2 : 0;
|
||||
if (first_row + shown > total_rows) {
|
||||
first_row = total_rows - shown;
|
||||
}
|
||||
}
|
||||
line_close(&ln);
|
||||
|
||||
for (row = 0; row < GRID_ROWS; row++) {
|
||||
char label[16];
|
||||
snprintf(label, sizeof label, " %02X |", row * GRID_COLS);
|
||||
{
|
||||
char gutter[24];
|
||||
char label[8];
|
||||
snprintf(gutter, sizeof gutter, "%*s| ", addr + 3, "");
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, gutter);
|
||||
for (col = 0; col < cols; col++) {
|
||||
snprintf(label, sizeof label, "+%-*X ", digits == 2 ? 1 : digits - 1,
|
||||
col);
|
||||
line_style(&ln, label, ANSI_DIM);
|
||||
}
|
||||
line_close(&ln);
|
||||
}
|
||||
|
||||
for (row = 0; row < shown; row++) {
|
||||
size_t base = (first_row + row) * (size_t)cols;
|
||||
char label[24];
|
||||
snprintf(label, sizeof label, " %0*zX |", addr, base);
|
||||
line_open(&ln, viz, out, color);
|
||||
line_style(&ln, label, ANSI_DIM);
|
||||
line_put(&ln, " ");
|
||||
for (col = 0; col < GRID_COLS; col++) {
|
||||
size_t index = (size_t)(row * GRID_COLS + col);
|
||||
char cell[8];
|
||||
unsigned int value;
|
||||
if (index >= MEM_SIZE) {
|
||||
for (col = 0; col < cols; col++) {
|
||||
size_t index = base + (size_t)col;
|
||||
char cell[16];
|
||||
unsigned long value;
|
||||
if (index >= cells) {
|
||||
line_put(&ln, " ");
|
||||
continue;
|
||||
}
|
||||
value = mem[index];
|
||||
snprintf(cell, sizeof cell, "%02X", value);
|
||||
value = (unsigned long)mem[index];
|
||||
snprintf(cell, sizeof cell, "%0*lX", digits, value);
|
||||
if (ptr >= 0 && (size_t)ptr == index) {
|
||||
line_style(&ln, cell, ANSI_PTR);
|
||||
} else {
|
||||
@@ -379,17 +484,31 @@ static void viz_grid(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
line_close(&ln);
|
||||
|
||||
/* A caret under the tape pointer, for terminals without colour. */
|
||||
if (ptr >= 0 && (long)(ptr / GRID_COLS) == row) {
|
||||
if (ptr >= 0 && (size_t)ptr / (size_t)cols == first_row + row) {
|
||||
line_open(&ln, viz, out, color);
|
||||
line_put(&ln, " | ");
|
||||
for (col = 0; col < GRID_COLS; col++) {
|
||||
bool here = (size_t)ptr == (size_t)(row * GRID_COLS + col);
|
||||
snprintf(label, sizeof label, "%*s| ", addr + 3, "");
|
||||
line_put(&ln, label);
|
||||
for (col = 0; col < cols; col++) {
|
||||
bool here = (size_t)ptr == base + (size_t)col;
|
||||
line_style(&ln, here ? "^^ " : " ",
|
||||
here ? ANSI_PTR : NULL);
|
||||
}
|
||||
line_close(&ln);
|
||||
}
|
||||
}
|
||||
|
||||
if (total_rows > shown) {
|
||||
char note[80];
|
||||
size_t last = (first_row + shown) * (size_t)cols - 1;
|
||||
if (last >= cells) {
|
||||
last = cells - 1;
|
||||
}
|
||||
snprintf(note, sizeof note, " window %0*zX..%0*zX of %zu cells",
|
||||
addr, first_row * (size_t)cols, addr, last, cells);
|
||||
line_open(&ln, viz, out, color);
|
||||
line_style(&ln, note, ANSI_DIM);
|
||||
line_close(&ln);
|
||||
}
|
||||
}
|
||||
|
||||
static void viz_outputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
@@ -569,7 +688,7 @@ static void viz_push_output(rot8_viz *viz, rot8_data value) {
|
||||
static void viz_replay_output(rot8_viz *viz) {
|
||||
size_t i;
|
||||
for (i = 0; i < viz->output_count; i++) {
|
||||
fprintf(viz->out, "%u\n", (unsigned)viz->outputs[i]);
|
||||
fprintf(viz->out, "%lu\n", (unsigned long)viz->outputs[i]);
|
||||
}
|
||||
fflush(viz->out);
|
||||
}
|
||||
@@ -749,7 +868,7 @@ bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) {
|
||||
rot8_viz *viz = (rot8_viz *)ctx;
|
||||
char line[64];
|
||||
char *end = NULL;
|
||||
long parsed;
|
||||
unsigned long parsed;
|
||||
|
||||
if (viz == NULL || value == NULL) {
|
||||
return (false);
|
||||
@@ -772,7 +891,7 @@ bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) {
|
||||
fflush(viz->out);
|
||||
return (false);
|
||||
}
|
||||
parsed = strtol(line, &end, 0);
|
||||
parsed = strtoul(line, &end, 0);
|
||||
if (end == line) {
|
||||
parsed = 0; /* nothing usable typed: feed 0 and keep going */
|
||||
}
|
||||
|
||||
+52
-2
@@ -40,13 +40,19 @@ static void usage(const char *program) {
|
||||
" --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"
|
||||
" -m, --mem <n> cells on the tape (default %u, max %u)\n"
|
||||
" -b, --cell <bits> bits per cell: 8, 16 or 32 (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",
|
||||
program, program, program, program);
|
||||
" %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",
|
||||
program, (unsigned)ROT8_DEFAULT_MEM_SIZE, (unsigned)ROT8_MAX_MEM_SIZE,
|
||||
(unsigned)ROT8_DEFAULT_CELL_BITS, program, program, program, program,
|
||||
program);
|
||||
}
|
||||
|
||||
static bool parse_uint(const char *text, unsigned int *out) {
|
||||
@@ -63,6 +69,22 @@ static bool parse_uint(const char *text, unsigned int *out) {
|
||||
return (true);
|
||||
}
|
||||
|
||||
/* --mem takes anything up to the machine's limit, --cell only 8, 16 or 32. */
|
||||
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') {
|
||||
@@ -81,10 +103,12 @@ 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;
|
||||
|
||||
rot8_config_default(&cfg);
|
||||
memset(&opts, 0, sizeof opts);
|
||||
opts.fps = 30;
|
||||
opts.every = 0;
|
||||
@@ -124,6 +148,26 @@ int main(int argc, char **argv) {
|
||||
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], 32UL, &bits) ||
|
||||
(bits != 8 && bits != 16 && bits != 32)) {
|
||||
fprintf(stderr, "%s: --cell needs 8, 16 or 32\n", program);
|
||||
return (2);
|
||||
}
|
||||
cfg.cell_bits = (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);
|
||||
@@ -146,6 +190,12 @@ int main(int argc, char **argv) {
|
||||
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_bits);
|
||||
return (2);
|
||||
}
|
||||
|
||||
if (!read_tape(path, ins, sizeof ins)) {
|
||||
fprintf(stderr, "%s: cannot read tape '%s'\n", program, path);
|
||||
return (1);
|
||||
|
||||
Reference in new issue
Block a user