diff --git a/.r8forge_cache/dc15c5db7642559d.txt b/.r8forge_cache/dc15c5db7642559d.txt new file mode 100644 index 0000000..988dc5c --- /dev/null +++ b/.r8forge_cache/dc15c5db7642559d.txt @@ -0,0 +1,6 @@ +# params 5 mode 0 metric 1 +1 17 0 +2 18 0 +0 6 0 +4 4 1 +1 12 0 diff --git a/asm/raw_add_nc.r8asm b/asm/raw_add_nc.r8asm new file mode 100644 index 0000000..8c00f17 --- /dev/null +++ b/asm/raw_add_nc.r8asm @@ -0,0 +1,50 @@ +%include "asm/lib.r8asm" + +; 忽略进位的加法器切片:只有两个操作数,进位在 ci 这个 cell 里就地复用(进->出), +; 最高位产生的进位直接丢掉(前提:和不会溢出,即 A+B <= _CELL_MAX). +; +; p1..p5 = A, B, 进位(进->出), S, tmp +; +; stat 是从定义直接算出来的:S = A^B^Ci,进位 = AB|AC|BC;A、B、tmp 结束为 0, +; 所以每个输入组合后的值只落在「进位」和 S 这两位上。 +%synth bit_add_nc_synth + params 5 + stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 + metric flat +%endsynth + +; 合成块假定不参与运算的参数从 0 开始(compress_stat_space 的语义),而 S 和 tmp +; 在循环里带着上一轮的值,所以外面包一层先清掉它们。 +; +; 已知限制:合成出来的程序只碰每个 cell 的低 K 位(K = 参与运算的输入位数), +; 高位是"不追踪"的。所以调用者必须保证喂进来的 cell 高位是 0 —— 本轮实测: +; 单片对所有输入组合都对(包括全零),但用 ror 把上一片的进位/和挪到下一位之后, +; cell 里会留下第 7 位,下一片里那些"循环直到 cell 归零"的块就再也退不出来了。 +; 也就是说要把多片串起来,还得先把每个 bit 挪到一个干净的 cell 里。 +%macro bit_add_nc 5 + .tp %4 + set_0 + .tp %5 + set_0 + bit_add_nc_synth %1 %2 %3 %4 %5 +%endmacro + +; 8 位: A、B、S 逐位右移,进位一直留在 ci 里;最后一轮产生的进位不再参与运算, +; 也就是被丢掉了。 +; 注意:在补上"清高位"这一步之前,这个 8 位版本会卡住(见上面的已知限制)。 +%macro raw_add_nc 5 +.anchor before_raw_add_nc + .tp %3 + set_0 + +%rep _CELL_SIZE + bit_add_nc %1 %2 %3 %4 %5 + .tp %1 + ror + .tp %2 + ror + .tp %4 + ror +%endrep +.tp before_raw_add_nc +%endmacro diff --git a/src/derive.cc b/src/derive.cc index 225663e..77b21a3 100644 --- a/src/derive.cc +++ b/src/derive.cc @@ -9,6 +9,7 @@ #include "r8forge/r8asm.hh" #include #include +#include #include #include @@ -93,6 +94,30 @@ int main(int argc, char **argv) { std::cout << "\n"; return 0; } + // 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, 8)) { + std::cerr << "could not assemble " << file << "\n"; + return 1; + } + machine m; + for (int i = 3; i < argc; i++) { + const char *eq = std::strchr(argv[i], '='); + if (eq == nullptr) { + continue; + } + const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str()); + m.cells[size_t(at)] = uint8_t(std::atoi(eq + 1) & 0xff); + } + m.run(a.export_rot8()); + std::cout << "out"; + for (uint8_t v : m.out) { + std::cout << " " << int(v); + } + std::cout << "\n"; + return 0; + } if (params < 1 || params > 8) { std::cerr << "params must be 1..8\n"; return 2;