kDefaultCellBits -> kDefaultCellSize: the width is cell_size, not cell bits

The cell width is cell_size everywhere else now -- cell_size in the AST, in
ROT-8-viz's rot8_config, and the docs -- but the default in geometry.hh was
still kDefaultCellBits, and the four VMs spelled it that way too.

  * include/r8forge/geometry.hh: kDefaultCellBits -> kDefaultCellSize.
    kDefaultCellMask keeps its name: it is a mask, not a size.
  * src/derive.cc, tests/asm_test.cc, tests/synth_test.cc, tests/tape_run.cc,
    tests/byte_op_test.cc: same rename, and the local shorthand kBits in
    byte_op_test.cc is now kCellSize so the test file does not put the old
    word back.
  * AGENTS.md and README.md follow.  The cross check bullet now describes the
    workspace ROT-8-viz as it actually is (fork dsh/ROT-8-viz, branch main,
    runtime --cell / --mem over cell_size / mem_size), instead of the feature
    branch it used to point at, which no longer exists.

No behaviour change.  All six self checks re-run green: 28729 character hash
tape, synth_test 8/8, both verify_ tapes, byte_op_test (clb_mul 3977 chars,
65536/65536, "byte level library ok") and the macro docs test.  The hash tape
still gives 1322 1786 1600 5 94 127 from r8forge_derive, r8forge_tape_run and
ROT-8-viz's rot8_test -- three implementations, one answer.
This commit is contained in:
dsh committed 2026-11-07 12:33:56 +08:00
1 parent 94868be24f
commit ac5810580d
8 files changed
+37 -37

No files matched your search

+11 -11
View File
@@ -142,25 +142,25 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
两个默认值集中在 **`include/r8forge/geometry.hh`**,现在是 **11 位 / 259 格**:
```cpp
inline constexpr size_t kDefaultCellBits = 11;
inline constexpr size_t kDefaultCellSize = 11;
inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellBits) - 1u;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
```
位宽和长度**都不在指令集里**(`ROR` 只是在一个 cell 里转圈,纸带是环),所以它们只以
"参数 + 常量"的形式存在,落在三处。改位宽时照这个清单走:
1. **汇编器**:`import_file(path, cell_size)` 的第二个参数就是位宽,6 个调用点都传
`kDefaultCellBits`。`set_cell_size` 顺手把三个名字注入程序作用域:
`kDefaultCellSize`。`set_cell_size` 顺手把三个名字注入程序作用域:
`_CELL_SIZE`(位宽)、`_CELL_MAX = (1 << 位宽) - 1`、`_ROL_STEP = 位宽 - 1`。
宏里写 `%rep _CELL_SIZE { … }` 就自动跟着位宽走(`erase`、`rol`、所有字节级驱动都这么写),
所以**位级宏、`%synth` 切片、位运算天然与位宽无关** —— 实测 `r8forge_synth_test` 在
11 位下照样 8/8 全过。
2. **四份虚拟机**(`tests/synth_test.cc`、`tests/byte_op_test.cc`、`src/derive.cc`、
`tests/tape_run.cc`):cell 存 `uint16_t`、数组长 `kDefaultMemoryCells`、
旋转必须是 `(v >> 1) | ((v & 1) << (kDefaultCellBits - 1))`、输入输出掩码用
旋转必须是 `(v >> 1) | ((v & 1) << (kDefaultCellSize - 1))`、输入输出掩码用
`kDefaultCellMask`。**四份要一起改**,少改一份就会出现"两个实现不一致"的假象。
3. **测试期望**:一律用 `kCellMask` / `kBits` 写,不要出现 `0xff`、`>> 8`、`256 - a`。
3. **测试期望**:一律用 `kCellMask` / `kCellSize` 写,不要出现 `0xff`、`>> 8`、`256 - a`。
**注意 2048 % 256 == 0**,所以不少 8 位期望在 11 位下碰巧还成立(and/or/xor/not/dec/sub
就是),别把"没报错"当成"不用改" —— `neg`、`add`、`inc`、`mul` 一改位宽当场就错。
@@ -173,12 +173,12 @@ inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellBits) - 1u;
* **`output` 打印整个 cell 的值**,不再是一个字节;`out` 里出现 257、1088 这种数是对的
(`127+130 = 257`、`200*200 = 19*2048 + 1088`)。
* **交叉验证要先对齐几何**:几何一致时同一条纸带输出完全相同(`asm/test.r8asm` 两边都是
`1322 1786 1600 5 94 127`);不一致时不是谁错了,是两台不同的机器。作者那份 `ROT-8-viz`
(`C:\Users\peng0\git\ROT-8-viz`)默认 11 位 / 259 格,`--cell` / `--mem` 可改;
**workspace 里另有一份 `ROT-8-viz/`,那是 upstream `starcells/ROT-8` 的 `feat/memory-viz`
分支**,几何是编译期常量(`cell_size` / `mem_size`,在 `include/rot8/rot8.h`),现在也是
**11 位 / 259 格**。对答案的办法:`ROT-8-viz/build/Release/rot8_test.exe <tape.rot8>` 把每个
`OUT` 按行打出来,和 `r8forge_derive <src.r8asm> -1` 的 `out` 逐位比即可。
`1322 1786 1600 5 94 127`);不一致时不是谁错了,是两台不同的机器。workspace 里的
`ROT-8-viz/` 是 upstream `starcells/ROT-8` 的 fork(`origin` = `dsh/ROT-8-viz`,`main`),
默认 **11 位 x 259 格**,几何还能运行期改(`--cell` / `--mem`;默认值与字段在
`include/rot8/rot8.h`,名字就是 `cell_size` / `mem_size`)。对答案的办法:
`ROT-8-viz/build/Release/rot8_test.exe <tape.rot8>` 把每个 `OUT` 按行打出来,
和 `r8forge_derive <src.r8asm> -1` 的 `out` 逐位比即可。
* 位宽变大还有个正面效果:`fibonacci` 不再在 256 处回绕,现在跑出 **13 项**(到 233)。
* **旧 tape 换到新位宽上不只是算错,还可能永远跑不完**:按 8 位写死的程序里
`%rep 8` 只转了 8 圈,在 11 位下 cell 没转回原位,"循环到某位清零"的条件于是永远不成立。
+1 -1
View File
@@ -190,7 +190,7 @@ build/Release/r8forge_derive drv/bit_raw_add.r8asm 6
## 字节级运算:一个 cell 一个机器字
> 机械字默认 **11 位、纸带 259 格**,都定义在 `include/r8forge/geometry.hh`
> (`kDefaultCellBits` / `kDefaultMemoryCells`)。指令集本身不规定位宽,宏里用
> (`kDefaultCellSize` / `kDefaultMemoryCells`)。指令集本身不规定位宽,宏里用
> `%rep _CELL_SIZE` 的地方会自动跟着变,所以下面说的"字节级"其实是"机器字级";
> 下面引用的纸带长度是 8 位时代的实测值,11 位下这些宏会长一些(`raw_add` 2199 字符、
> `fibonacci` 11959 字符,完整数字见 `AGENTS.md`)。
+2 -2
View File
@@ -7,6 +7,6 @@
// cell is in bits, and how many cells the tape holds. The instruction set does
// not fix either (ROR just rotates inside a cell and the tape is a ring), so
// these are the only places the old 8 bit / 256 cell assumptions live.
inline constexpr size_t kDefaultCellBits = 11;
inline constexpr size_t kDefaultCellSize = 11;
inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellBits) - 1u;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
+5 -5
View File
@@ -21,7 +21,7 @@ struct machine {
size_t dp = 0;
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
@@ -90,7 +90,7 @@ int main(int argc, char **argv) {
const int params = std::atoi(argv[2]);
if (params == 0) { // just run the program and dump what it printed
r8asm a;
if (!a.import_file(file, kDefaultCellBits)) {
if (!a.import_file(file, kDefaultCellSize)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -105,7 +105,7 @@ int main(int argc, char **argv) {
// params == -2: same as -1 but dumps every non zero cell afterwards
if (params == -2) {
r8asm a;
if (!a.import_file(file, kDefaultCellBits)) {
if (!a.import_file(file, kDefaultCellSize)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -135,7 +135,7 @@ int main(int argc, char **argv) {
// params == -1: the rest of argv is addr=value; set those cells, run, dump OUT
if (params == -1) {
r8asm a;
if (!a.import_file(file, kDefaultCellBits)) {
if (!a.import_file(file, kDefaultCellSize)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -165,7 +165,7 @@ int main(int argc, char **argv) {
addr[static_cast<size_t>(i)] = argc > 3 + i ? std::atoi(argv[3 + i]) : i;
r8asm assembler;
if (!assembler.import_file(file, kDefaultCellBits)) {
if (!assembler.import_file(file, kDefaultCellSize)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
+1 -1
View File
@@ -8,7 +8,7 @@ int main(int argc, char **argv) {
return (2);
}
r8asm a;
if (!a.import_file(argv[1], kDefaultCellBits))
if (!a.import_file(argv[1], kDefaultCellSize))
return (1);
std::string tape = a.export_rot8();
if (a.failed())
+14 -14
View File
@@ -21,8 +21,8 @@ namespace {
// written in terms of it instead of in terms of a byte: kCellMod is one past
// the largest value a cell holds. The sweeps walk the first 256 values, which
// is a sample of the cell range rather than all of it.
constexpr int kBits = int(kDefaultCellBits);
constexpr int kCellMod = 1 << kBits;
constexpr int kCellSize = int(kDefaultCellSize);
constexpr int kCellMod = 1 << kCellSize;
constexpr int kCellMask = int(kDefaultCellMask);
struct machine {
@@ -33,7 +33,7 @@ struct machine {
bool finished = true; // false: the step limit ran out
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit) {
@@ -91,7 +91,7 @@ struct machine {
std::string assemble(const std::string &file) {
r8asm assembler;
if (!assembler.import_file(file, kDefaultCellBits) || assembler.failed()) {
if (!assembler.import_file(file, kDefaultCellSize) || assembler.failed()) {
std::cerr << "could not assemble " << file << "\n";
std::exit(1);
}
@@ -152,14 +152,14 @@ int test_add(const std::string &file) {
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 5 && m.out[0] == a &&
m.out[1] == b && m.out[2] == 0 &&
m.out[3] == (sum & kCellMask) && m.out[4] == (sum >> kBits);
m.out[3] == (sum & kCellMask) && m.out[4] == (sum >> kCellSize);
if (!ok) {
bad++;
if (bad <= 3)
report("raw_add", a, b, m,
std::to_string(a) + " " + std::to_string(b) + " 0 " +
std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
std::to_string(sum >> kCellSize));
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
@@ -181,14 +181,14 @@ int test_inc(const std::string &file) {
const int sum = a + ci;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == a &&
m.out[1] == ci && m.out[2] == (sum & kCellMask) &&
m.out[3] == (sum >> kBits);
m.out[3] == (sum >> kCellSize);
if (!ok) {
bad++;
if (bad <= 3)
report("raw_inc", a, ci, m,
std::to_string(a) + " " + std::to_string(ci) + " " +
std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
std::to_string(sum >> kCellSize));
}
}
std::cout << " " << (bad ? "FAILED" : "all 512 combinations ok")
@@ -211,13 +211,13 @@ int test_add_nc(const std::string &file) {
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
m.out[1] == 0 && m.out[2] == (sum & kCellMask) &&
m.out[3] == (sum >> kBits);
m.out[3] == (sum >> kCellSize);
if (!ok) {
bad++;
if (bad <= 3)
report("clb_add_nc", a, b, m,
"0 0 " + std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
std::to_string(sum >> kCellSize));
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
@@ -236,13 +236,13 @@ int test_inc_nc(const std::string &file) {
m.run(tape, 100'000);
const int sum = a + 1;
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
m.out[1] == (sum & kCellMask) && m.out[2] == (sum >> kBits);
m.out[1] == (sum & kCellMask) && m.out[2] == (sum >> kCellSize);
if (!ok) {
bad++;
if (bad <= 3)
report("clb_inc_nc", a, 1, m,
"0 " + std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
std::to_string(sum >> kCellSize));
}
}
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
@@ -485,13 +485,13 @@ int test_mul(const std::string &file) {
n++;
const int p = mc * mu;
const bool ok = m.finished && m.out.size() == 3 &&
m.out[0] == (p & kCellMask) && m.out[1] == ((p >> kBits) & kCellMask) &&
m.out[0] == (p & kCellMask) && m.out[1] == ((p >> kCellSize) & kCellMask) &&
m.out[2] == mc;
if (!ok) {
bad++;
if (bad <= 3)
report("clb_mul", mc, mu, m,
std::to_string(p & kCellMask) + " " + std::to_string((p >> kBits) & kCellMask) +
std::to_string(p & kCellMask) + " " + std::to_string((p >> kCellSize) & kCellMask) +
" " + std::to_string(mc));
}
}
+2 -2
View File
@@ -20,7 +20,7 @@ struct machine {
size_t dp = 0;
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape) {
@@ -82,7 +82,7 @@ struct machine {
int main(int argc, char **argv) {
const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm";
r8asm assembler;
if (!assembler.import_file(file, kDefaultCellBits)) {
if (!assembler.import_file(file, kDefaultCellSize)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
+1 -1
View File
@@ -75,7 +75,7 @@ int main(int argc, char **argv) {
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = uint16_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellBits - 1)));
cells[dp] = uint16_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
break;
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
// byte level program in the library look like an endless loop.