diff --git a/bit2txt.c b/bit2txt.c index 3465a6d..116a9b4 100644 --- a/bit2txt.c +++ b/bit2txt.c @@ -604,6 +604,9 @@ void print_ramb16_cfg(ramb16_cfg_t* cfg) } +// for an equivalent schematic, see lut.svg +const int lut_base_vars[6] = {0 /* A1 */, 1, 0, 0, 0, 1 /* A6 */}; + int bool_nextlen(const char* expr, int len) { int i, depth; @@ -690,11 +693,10 @@ fail: int parse_boolexpr(const char* expr, uint64_t* lut) { int i, j, result, vars[6]; - const int base_vars[6] = {0 /* A1 */, 1, 0, 0, 0, 1 /* A6 */}; *lut = 0; for (i = 0; i < 64; i++) { - memcpy(vars, base_vars, sizeof(vars)); + memcpy(vars, lut_base_vars, sizeof(vars)); for (j = 0; j < 6; j++) { if (i & (1< empty string + if (mt_size[0] == 0) { + str[0] = 0; + return; + } + + // go through five rounds of merging + for (round = 1; round < 7; round++) { + for (i = 0; i < mt_size[round-1]; i++) { + for (j = i+1; j < mt_size[round-1]; j++) { + only_diff_bit = -1; + for (k = 0; k < 6; k++) { + if (mt[round-1][i].a[k] != mt[round-1][j].a[k]) { + if (only_diff_bit != -1) { + only_diff_bit = -1; + break; + } + only_diff_bit = k; + } + } + if (only_diff_bit != -1) { + char new_term[6]; + + for (k = 0; k < 6; k++) + new_term[k] = + (k == only_diff_bit) ? 2 + : mt[round-1][i].a[k]; + for (k = 0; k < mt_size[round]; k++) { + if (new_term[0] == mt[round][k].a[0] + && new_term[1] == mt[round][k].a[1] + && new_term[2] == mt[round][k].a[2] + && new_term[3] == mt[round][k].a[3] + && new_term[4] == mt[round][k].a[4] + && new_term[5] == mt[round][k].a[5]) + break; + } + if (k >= mt_size[round]) { + mt[round][mt_size[round]].a[0] = new_term[0]; + mt[round][mt_size[round]].a[1] = new_term[1]; + mt[round][mt_size[round]].a[2] = new_term[2]; + mt[round][mt_size[round]].a[3] = new_term[3]; + mt[round][mt_size[round]].a[4] = new_term[4]; + mt[round][mt_size[round]].a[5] = new_term[5]; + mt_size[round]++; + } + mt[round-1][i].merged = 1; + mt[round-1][j].merged = 1; + } + } + } + } + // special case: 222222 -> (A6+~A6) + for (i = 0; i < mt_size[6]; i++) { + if (mt[6][i].a[0] == 2 + && mt[6][i].a[1] == 2 + && mt[6][i].a[2] == 2 + && mt[6][i].a[3] == 2 + && mt[6][i].a[4] == 2 + && mt[6][i].a[5] == 2) { + strcpy(str, "A6+~A6"); + return; + } + } + + str_end = 0; + for (round = 0; round < 7; round++) { + for (i = 0; i < mt_size[round]; i++) { + if (!mt[round][i].merged) { + if (str_end) + str[str_end++] = '+'; + first_op = 1; + for (j = 0; j < 6; j++) { + if (mt[round][i].a[j] != 2) { + if (!first_op) + str[str_end++] = '*'; + if (!mt[round][i].a[j]) + str[str_end++] = '~'; + str[str_end++] = 'A'; + str[str_end++] = '1' + j; + first_op = 0; + } + } + } + } + } + str[str_end] = 0; + // TODO: This could be further simplified, see Petrick's method. + // XOR don't simplify well, try A2@A3 +} + + int g_FLR_value = -1; int main(int argc, char** argv)