Any cell width, any tape length, and the frame follows both

The previous commit still treated the geometry as a small menu: 8/16/32 bits and
a grid whose column count was hard wired per width.  ROT-8 does not work that
way -- the tape is a ring of any length and a cell is a ring of any width -- so
both are open now:

    --cell 1 .. 32     (any value, not just 8/16/32)
    --mem  1 .. 1048576 (any value, not just powers of two)

What had to change for that:

* A cell needs ceil(bits/4) hex digits, at least one, so a 1 bit cell prints
  "0x1" instead of nothing and a 12 bit cell prints three digits.
* The number of cells per dump row is computed from the space the gutter leaves
  (VIZ_INNER - addr_digits - 5) divided by one cell's field width, capped at 16.
  Every geometry therefore lays out inside the 56 character frame: 16 columns at
  8 bits, 9 at 16, 5 at 32, 12 at 12 bits, and a ragged last row just ends early.
* rot8_config_apply accepts any cell_bits in 1..32 instead of three values.

Live mode needed nothing beyond that, but it is worth stating why it stays
stable: the frame height depends only on the geometry -- rows are
min(ceil(cells/cols), 16), the window line exists exactly when the tape is taller
than that, and the caret row is always drawn because the pointer is always on the
tape (it is a ring).  So every frame of a run is the same height and the in place
redraw cannot smear.  Width is capped at 58 characters for the same reason.

Checked: --dump for and, or, full_adder, wave and test is still byte identical to
the build before any of this, and -++++ gives 0x10 / 0x1000 / 0x100 / 0x10000000
at 8 / 16 / 12 / 32 bits.
This commit is contained in:
dsh committed 2026-11-07 10:38:56 +08:00
1 parent 4ae4bea2dd
commit 5a23ffd64d
4 files changed
+57 -33

No files matched your search

+26 -13
View File
@@ -97,8 +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` |
| `-m`, `--mem <n>` | 纸带长度(单位 Cell),任意值,默认 256,范围 1..1048576 |
| `-b`, `--cell <bits>` | Cell 位宽,任意值,默认 8,范围 1..32 |
| `-h`, `--help` | 帮助 |
不加 `--live` 与 `--dump` 时行为与以前完全一致:直接跑完,只留下 `OUT` 的输出.\
@@ -111,32 +111,45 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
## 纸带尺寸与 Cell 位宽
两者都不在指令集里,运行时可以选:
两者都不在指令集里 —— 纸带是任意长的环,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
rot8_test --mem 300 tapes/wave.rot8 # 300 格(不是 2 的幂也行)
rot8_test --mem 1000 --cell 16 tapes/wave.rot8
rot8_test --mem 64 --cell 1 tapes/wave.rot8 # 1 bit 的 Cell
rot8_test --mem 100 --cell 12 --live tapes/wave.rot8
```
- **位宽 8 / 16 / 32**:`ROR` 在这个位宽里循环(16 bit 时 bit0 转到 bit15),`FLB` 翻 LSB,
- **位宽 1..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. 比画面能画的行数长时,视图只画指针附近的一窗,末行注明窗口范围.
8 / 16 / 32 bit 下分别是 `0x10` / `0x1000` / `0x10000000`.
- **纸带长度 1..1048576,任意值**:指针走出纸带会**绕回另一端**(和 Cell 的 `ROR` 一样是个环),
不会越界. 末行不满一行时右边留空.
画面完全跟着几何走,而且**每帧高度只由几何决定**,不随指针移动变化(`--live` 的原地重绘因此
不会拖影):
| 场景 | 表现 |
| --- | --- |
| 位宽 ≤ 8 | 每位一列(`b7 … b0`),8 bit 与以前一模一样 |
| 位宽 > 8 | 一行按 4 位分组的字串(`bits b15 .. b0 .... .... .... ..1.`) |
| 一格宽度 | 每格 `ceil(bits/4)` 个十六进制位;一格占的列数由宽度算出,所以画面永远 56 字符宽 |
| 纸带 ≤ 16 行 | 整条画出来 |
| 纸带 > 16 行 | 只画指针附近的一窗(指针始终在窗口里),末行注明 `window A..B of N cells` |
内嵌用法:
```c
rot8_config cfg = {4096, 16}; /* 纸带长度, Cell 位宽 */
rot8_config cfg = {1000, 12}; /* 纸带长度, 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` 的输出都没有变.
`rot8_config_apply` 只在越界(0 格 / 位宽 0 或 >32 / 长度 >1048576)时返回 `false` 并且什么都不改.
默认几何(256 x 8)下的行为与以前逐字节一致:`--dump` 的画面、`OUT` 的输出都没有变.
## 输入
+1 -1
View File
@@ -43,7 +43,7 @@ bool rot8_config_apply(const rot8_config *cfg) {
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) {
if (cfg->cell_bits == 0 || cfg->cell_bits > ROT8_MAX_CELL_BITS) {
return (false);
}
config_current = *cfg;
+22 -13
View File
@@ -288,19 +288,28 @@ static void viz_status(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
* 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)); }
/* Hex digits a cell needs: ceil(bits/4), at least one (a 1 bit cell). */
static int viz_cell_digits(unsigned bits) {
int digits = (int)((bits + 3) / 4);
return (digits > 0 ? digits : 1);
}
/* 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);
/* Cells that fit on one dump row: the gutter takes addr_digits + 5, every cell
* takes its hex digits plus a space, and the frame is VIZ_INNER wide. Any
* cell width and any tape length therefore lay out without overflowing the
* frame; 16 columns is as many as a byte cell can show. */
static int viz_grid_cols(unsigned bits, int addr_digits) {
int field = viz_cell_digits(bits) + 1;
int room = VIZ_INNER - addr_digits - 5;
int cols = room / field;
if (cols > 16) {
cols = 16;
}
if (cols < 1) {
cols = 1;
}
return (cols);
}
/* Hex digits an address needs, so that labels line up. */
@@ -420,9 +429,9 @@ 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);
int cols = viz_grid_cols(bits, addr);
int digits = viz_cell_digits(bits);
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;
+8 -6
View File
@@ -41,7 +41,7 @@ static void usage(const char *program) {
" -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"
" -b, --cell <bits> bits per cell, 1..%u (default %u)\n"
" -h, --help show this text\n"
"\n"
"examples:\n"
@@ -51,8 +51,8 @@ static void usage(const char *program) {
" %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);
(unsigned)ROT8_MAX_CELL_BITS, (unsigned)ROT8_DEFAULT_CELL_BITS, program,
program, program, program, program);
}
static bool parse_uint(const char *text, unsigned int *out) {
@@ -161,9 +161,11 @@ int main(int argc, char **argv) {
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);
if (i + 1 >= argc ||
!parse_geometry(argv[i + 1], (unsigned long)ROT8_MAX_CELL_BITS,
&bits)) {
fprintf(stderr, "%s: --cell needs 1..%u bits\n", program,
(unsigned)ROT8_MAX_CELL_BITS);
return (2);
}
cfg.cell_bits = (unsigned)bits;