Drop the inputs/outputs declarations: the table already says it
A synth block used to spell out which parameter is an input and which is an output. That was redundant: with map, 2^K entries already mean %1..%K are the inputs and the value's bits land on the parameters after them; with a raw table, nothing needs saying at all. So 'inputs' and 'outputs' now fail with a message pointing at map/stat, and the map form derives both. Added 'stat' as the fully explicit form: 2^params values, one per input combination, which is exactly the state table the search consumes. examples/full_add_stat.r8asm is the same adder written that way and produces a byte-identical tape to the map version (95 characters), both pass the eight combination check.
This commit is contained in:
1 parent
6fcdc4a1ea
commit
a2b715ae29
7 files changed
+129
-56
No files matched your search
+33
-4
@@ -461,9 +461,11 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
if (kw == "params") {
|
||||
spec.params = std::stoi(it->tokens[1].value);
|
||||
} else if (kw == "inputs" || kw == "outputs") {
|
||||
std::vector<int> &dst = (kw == "inputs") ? spec.in : spec.out;
|
||||
for (size_t i = 1; i < it->tokens.size(); i++)
|
||||
dst.push_back(std::stoi(it->tokens[i].value) - 1);
|
||||
// implied by the table now: with map the inputs are %1..%K (K from
|
||||
// the number of entries) and the outputs follow them in bit order
|
||||
main_tree.fail("'" + kw +
|
||||
"' is gone: map already says which parameter is "
|
||||
"which, use stat for the raw table");
|
||||
} else if (kw == "mode") {
|
||||
const std::string &m = it->tokens[1].value;
|
||||
if (m == "layers")
|
||||
@@ -478,12 +480,39 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
} else if (kw == "map") {
|
||||
for (size_t i = 1; i < it->tokens.size(); i++)
|
||||
spec.map.push_back(std::stoi(it->tokens[i].value));
|
||||
} else if (kw == "stat") {
|
||||
// the whole state table: one P-bit value per input combination
|
||||
for (size_t i = 1; i < it->tokens.size(); i++)
|
||||
spec.stat.push_back(std::stoi(it->tokens[i].value));
|
||||
} else {
|
||||
main_tree.fail("unknown line '" + kw + "' inside %synth");
|
||||
}
|
||||
}
|
||||
// map alone says which parameter is which: 2^K entries mean %1..%K are
|
||||
// the inputs and the value's bits land on the parameters after them
|
||||
if (!spec.map.empty() && spec.in.empty()) {
|
||||
size_t k = 0;
|
||||
while ((size_t(1) << k) < spec.map.size())
|
||||
k++;
|
||||
if ((size_t(1) << k) != spec.map.size()) {
|
||||
main_tree.fail("%synth '" + name + "': map needs 2^inputs entries");
|
||||
} else {
|
||||
for (size_t i = 0; i < k; i++)
|
||||
spec.in.push_back(int(i));
|
||||
int width = 0;
|
||||
for (int val : spec.map)
|
||||
for (int b = 0; b < 31; b++)
|
||||
if ((val >> b) & 1)
|
||||
width = std::max(width, b + 1);
|
||||
for (int i = 0; i < width; i++)
|
||||
spec.out.push_back(int(k) + i);
|
||||
if (int(k) + width > spec.params)
|
||||
main_tree.fail("%synth '" + name + "': map does not fit in " +
|
||||
std::to_string(spec.params) + " parameters");
|
||||
}
|
||||
}
|
||||
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
|
||||
spec.map, name, spec.layout,
|
||||
spec.map, spec.stat, name, spec.layout,
|
||||
spec.mode);
|
||||
if (prog.empty()) {
|
||||
main_tree.fail("%synth '" + name + "': nothing emitted");
|
||||
|
||||
+46
-29
@@ -11,8 +11,9 @@
|
||||
template <int P>
|
||||
std::vector<std::array<int, 3>>
|
||||
r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
const std::vector<int> &map, const std::string &name,
|
||||
const std::vector<int> &layout, int mode) {
|
||||
const std::vector<int> &map, const std::vector<int> &stat,
|
||||
const std::string &name, const std::vector<int> &layout,
|
||||
int mode) {
|
||||
using V = r8veritas<P>;
|
||||
V v;
|
||||
v.initialize_nicknames();
|
||||
@@ -21,16 +22,30 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
// the input cells are the low parameters, so the stat index already is the
|
||||
// input combination
|
||||
const size_t n = size_t(1) << P;
|
||||
const unsigned in_mask = (1u << in.size()) - 1u;
|
||||
typename V::stat start{}, target{};
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
const unsigned c = unsigned(i) & in_mask;
|
||||
start[i] = typename V::cell(i);
|
||||
unsigned val = c; // input cells keep their value
|
||||
for (size_t k = 0; k < out.size(); k++)
|
||||
if ((unsigned(map[c]) >> k) & 1u)
|
||||
val |= 1u << out[k];
|
||||
target[i] = typename V::cell(val);
|
||||
if (!stat.empty()) {
|
||||
// raw state table: one P-bit value per input combination, nothing to
|
||||
// derive -- this is the whole specification
|
||||
if (stat.size() != n) {
|
||||
std::cerr << "r8asm: %synth " << name << ": stat wants " << n
|
||||
<< " values for " << P << " parameters\n";
|
||||
return {};
|
||||
}
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
start[i] = typename V::cell(i);
|
||||
target[i] = typename V::cell(stat[i]);
|
||||
}
|
||||
} else {
|
||||
const unsigned in_mask = (1u << in.size()) - 1u;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
const unsigned c = unsigned(i) & in_mask;
|
||||
start[i] = typename V::cell(i);
|
||||
unsigned val = c; // input cells keep their value
|
||||
for (size_t k = 0; k < out.size(); k++)
|
||||
if ((unsigned(map[c]) >> k) & 1u)
|
||||
val |= 1u << out[k];
|
||||
target[i] = typename V::cell(val);
|
||||
}
|
||||
}
|
||||
|
||||
auto tt = v.make_truthtable(start, target);
|
||||
@@ -102,40 +117,42 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
std::vector<std::array<int, 3>>
|
||||
r8asm_synth_program(int params, const std::vector<int> &in,
|
||||
const std::vector<int> &out, const std::vector<int> &map,
|
||||
const std::string &name,
|
||||
const std::vector<int> &stat, const std::string &name,
|
||||
const std::vector<int> &layout, int mode) {
|
||||
if (params < 1 || params > 8) {
|
||||
std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n";
|
||||
return {};
|
||||
}
|
||||
if (in.empty() || map.size() != (size_t(1) << in.size())) {
|
||||
std::cerr << "r8asm: %synth " << name
|
||||
<< ": map must have 2^inputs entries\n";
|
||||
return {};
|
||||
}
|
||||
for (size_t i = 0; i < in.size(); i++)
|
||||
if (in[i] != int(i)) {
|
||||
std::cerr << "r8asm: %synth " << name << ": input %" << i + 1
|
||||
<< " must be %" << i + 1 << " (inputs come first)\n";
|
||||
if (stat.empty()) { // with a raw table there is nothing to check here
|
||||
if (in.empty() || map.size() != (size_t(1) << in.size())) {
|
||||
std::cerr << "r8asm: %synth " << name
|
||||
<< ": map must have 2^inputs entries\n";
|
||||
return {};
|
||||
}
|
||||
for (size_t i = 0; i < in.size(); i++)
|
||||
if (in[i] != int(i)) {
|
||||
std::cerr << "r8asm: %synth " << name << ": input %" << i + 1
|
||||
<< " must be %" << i + 1 << " (inputs come first)\n";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
switch (params) {
|
||||
case 1:
|
||||
return r8asm_synth_run<1>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode);
|
||||
case 2:
|
||||
return r8asm_synth_run<2>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode);
|
||||
case 3:
|
||||
return r8asm_synth_run<3>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode);
|
||||
case 4:
|
||||
return r8asm_synth_run<4>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode);
|
||||
case 5:
|
||||
return r8asm_synth_run<5>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode);
|
||||
case 6:
|
||||
return r8asm_synth_run<6>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode);
|
||||
case 7:
|
||||
return r8asm_synth_run<7>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode);
|
||||
default:
|
||||
return r8asm_synth_run<8>(in, out, map, name, layout, mode);
|
||||
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user