1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
/*
* Revision Control Information
*
* $Source$
* $Author$
* $Revision$
* $Date$
*
*/
#include "espresso.h"
ABC_NAMESPACE_IMPL_START
void map_dcset(PLA)
pPLA PLA;
{
int var, i;
pcover Tplus, Tminus, Tplusbar, Tminusbar;
pcover newf, term1, term2, dcset, dcsetbar;
pcube cplus, cminus, last, p;
if (PLA->label == NIL(char *) || PLA->label[0] == NIL(char))
return;
/* try to find a binary variable named "DONT_CARE" */
var = -1;
for(i = 0; i < cube.num_binary_vars * 2; i++) {
if (strncmp(PLA->label[i], "DONT_CARE", 9) == 0 ||
strncmp(PLA->label[i], "DONTCARE", 8) == 0 ||
strncmp(PLA->label[i], "dont_care", 9) == 0 ||
strncmp(PLA->label[i], "dontcare", 8) == 0) {
var = i/2;
break;
}
}
if (var == -1) {
return;
}
/* form the cofactor cubes for the don't-care variable */
cplus = set_save(cube.fullset);
cminus = set_save(cube.fullset);
set_remove(cplus, var*2);
set_remove(cminus, var*2 + 1);
/* form the don't-care set */
EXEC(simp_comp(cofactor(cube1list(PLA->F), cplus), &Tplus, &Tplusbar),
"simpcomp+", Tplus);
EXEC(simp_comp(cofactor(cube1list(PLA->F), cminus), &Tminus, &Tminusbar),
"simpcomp-", Tminus);
EXEC(term1 = cv_intersect(Tplus, Tminusbar), "term1 ", term1);
EXEC(term2 = cv_intersect(Tminus, Tplusbar), "term2 ", term2);
EXEC(dcset = sf_union(term1, term2), "union ", dcset);
EXEC(simp_comp(cube1list(dcset), &PLA->D, &dcsetbar), "simplify", PLA->D);
EXEC(newf = cv_intersect(PLA->F, dcsetbar), "separate ", PLA->F);
free_cover(PLA->F);
PLA->F = newf;
free_cover(Tplus);
free_cover(Tminus);
free_cover(Tplusbar);
free_cover(Tminusbar);
free_cover(dcsetbar);
/* remove any cubes dependent on the DONT_CARE variable */
(void) sf_active(PLA->F);
foreach_set(PLA->F, last, p) {
if (! is_in_set(p, var*2) || ! is_in_set(p, var*2+1)) {
RESET(p, ACTIVE);
}
}
PLA->F = sf_inactive(PLA->F);
/* resize the cube and delete the don't-care variable */
setdown_cube();
for(i = 2*var+2; i < cube.size; i++) {
PLA->label[i-2] = PLA->label[i];
}
for(i = var+1; i < cube.num_vars; i++) {
cube.part_size[i-1] = cube.part_size[i];
}
cube.num_binary_vars--;
cube.num_vars--;
cube_setup();
PLA->F = sf_delc(PLA->F, 2*var, 2*var+1);
PLA->D = sf_delc(PLA->D, 2*var, 2*var+1);
}
void map_output_symbolic(PLA)
pPLA PLA;
{
pset_family newF, newD;
pset compress;
symbolic_t *p1;
symbolic_list_t *p2;
int i, bit, tot_size, base, old_size;
/* Remove the DC-set from the ON-set (is this necessary ??) */
if (PLA->D->count > 0) {
sf_free(PLA->F);
PLA->F = complement(cube2list(PLA->D, PLA->R));
}
/* tot_size = width added for all symbolic variables */
tot_size = 0;
for(p1=PLA->symbolic_output; p1!=NIL(symbolic_t); p1=p1->next) {
for(p2=p1->symbolic_list; p2!=NIL(symbolic_list_t); p2=p2->next) {
if (p2->pos<0 || p2->pos>=cube.part_size[cube.output]) {
fatal("symbolic-output index out of range");
/* } else if (p2->variable != cube.output) {
fatal("symbolic-output label must be an output");*/
}
}
tot_size += 1 << p1->symbolic_list_length;
}
/* adjust the indices to skip over new outputs */
for(p1=PLA->symbolic_output; p1!=NIL(symbolic_t); p1=p1->next) {
for(p2=p1->symbolic_list; p2!=NIL(symbolic_list_t); p2=p2->next) {
p2->pos += tot_size;
}
}
/* resize the cube structure -- add enough for the one-hot outputs */
old_size = cube.size;
cube.part_size[cube.output] += tot_size;
setdown_cube();
cube_setup();
/* insert space in the output part for the one-hot output */
base = cube.first_part[cube.output];
PLA->F = sf_addcol(PLA->F, base, tot_size);
PLA->D = sf_addcol(PLA->D, base, tot_size);
PLA->R = sf_addcol(PLA->R, base, tot_size);
/* do the real work */
for(p1=PLA->symbolic_output; p1!=NIL(symbolic_t); p1=p1->next) {
newF = new_cover(100);
newD = new_cover(100);
find_inputs(NIL(set_family_t), PLA, p1->symbolic_list, base, 0,
&newF, &newD);
/*
* Not sure what this means
find_dc_inputs(PLA, p1->symbolic_list,
base, 1 << p1->symbolic_list_length, &newF, &newD);
*/
free_cover(PLA->F);
PLA->F = newF;
/*
* retain OLD DC-set -- but we've lost the don't-care arc information
* (it defaults to branch to the zero state)
free_cover(PLA->D);
PLA->D = newD;
*/
free_cover(newD);
base += 1 << p1->symbolic_list_length;
}
/* delete the old outputs, and resize the cube */
compress = set_full(newF->sf_size);
for(p1=PLA->symbolic_output; p1!=NIL(symbolic_t); p1=p1->next) {
for(p2=p1->symbolic_list; p2!=NIL(symbolic_list_t); p2=p2->next) {
bit = cube.first_part[cube.output] + p2->pos;
set_remove(compress, bit);
}
}
cube.part_size[cube.output] -= newF->sf_size - set_ord(compress);
setdown_cube();
cube_setup();
PLA->F = sf_compress(PLA->F, compress);
PLA->D = sf_compress(PLA->D, compress);
if (cube.size != PLA->F->sf_size) fatal("error");
/* Quick minimization */
PLA->F = sf_contain(PLA->F);
PLA->D = sf_contain(PLA->D);
for(i = 0; i < cube.num_vars; i++) {
PLA->F = d1merge(PLA->F, i);
PLA->D = d1merge(PLA->D, i);
}
PLA->F = sf_contain(PLA->F);
PLA->D = sf_contain(PLA->D);
free_cover(PLA->R);
PLA->R = new_cover(0);
symbolic_hack_labels(PLA, PLA->symbolic_output,
compress, cube.size, old_size, tot_size);
set_free(compress);
}
void find_inputs(A, PLA, list, base, value, newF, newD)
pcover A;
pPLA PLA;
symbolic_list_t *list;
int base, value;
pcover *newF, *newD;
{
pcover S, S1;
register pset last, p;
/*
* A represents th 'input' values for which the outputs assume
* the integer value 'value
*/
if (list == NIL(symbolic_list_t)) {
/*
* Simulate these inputs against the on-set; then, insert into the
* new on-set a 1 in the proper position
*/
S = cv_intersect(A, PLA->F);
foreach_set(S, last, p) {
set_insert(p, base + value);
}
*newF = sf_append(*newF, S);
/*
* 'simulate' these inputs against the don't-care set
S = cv_intersect(A, PLA->D);
*newD = sf_append(*newD, S);
*/
} else {
/* intersect and recur with the OFF-set */
S = cof_output(PLA->R, cube.first_part[cube.output] + list->pos);
if (A != NIL(set_family_t)) {
S1 = cv_intersect(A, S);
free_cover(S);
S = S1;
}
find_inputs(S, PLA, list->next, base, value*2, newF, newD);
free_cover(S);
/* intersect and recur with the ON-set */
S = cof_output(PLA->F, cube.first_part[cube.output] + list->pos);
if (A != NIL(set_family_t)) {
S1 = cv_intersect(A, S);
free_cover(S);
S = S1;
}
find_inputs(S, PLA, list->next, base, value*2 + 1, newF, newD);
free_cover(S);
}
}
#if 0
find_dc_inputs(PLA, list, base, maxval, newF, newD)
pPLA PLA;
symbolic_list_t *list;
int base, maxval;
pcover *newF, *newD;
{
pcover A, S, S1;
symbolic_list_t *p2;
register pset p, last;
register int i;
/* painfully find the points for which the symbolic output is dc */
A = NIL(set_family_t);
for(p2=list; p2!=NIL(symbolic_list_t); p2=p2->next) {
S = cof_output(PLA->D, cube.first_part[cube.output] + p2->pos);
if (A == NIL(set_family_t)) {
A = S;
} else {
S1 = cv_intersect(A, S);
free_cover(S);
free_cover(A);
A = S1;
}
}
S = cv_intersect(A, PLA->F);
*newF = sf_append(*newF, S);
S = cv_intersect(A, PLA->D);
foreach_set(S, last, p) {
for(i = base; i < base + maxval; i++) {
set_insert(p, i);
}
}
*newD = sf_append(*newD, S);
free_cover(A);
}
#endif
void map_symbolic(PLA)
pPLA PLA;
{
symbolic_t *p1;
symbolic_list_t *p2;
int var, base, num_vars, num_binary_vars, *new_part_size;
int new_size, size_added, num_deleted_vars, num_added_vars, newvar;
pset compress;
/* Verify legal values are in the symbolic lists */
for(p1 = PLA->symbolic; p1 != NIL(symbolic_t); p1 = p1->next) {
for(p2=p1->symbolic_list; p2!=NIL(symbolic_list_t); p2=p2->next) {
if (p2->variable < 0 || p2->variable >= cube.num_binary_vars) {
fatal(".symbolic requires binary variables");
}
}
}
/*
* size_added = width added for all symbolic variables
* num_deleted_vars = # binary variables to be deleted
* num_added_vars = # new mv variables
* compress = a cube which will be used to compress the set families
*/
size_added = 0;
num_added_vars = 0;
for(p1 = PLA->symbolic; p1 != NIL(symbolic_t); p1 = p1->next) {
size_added += 1 << p1->symbolic_list_length;
num_added_vars++;
}
compress = set_full(PLA->F->sf_size + size_added);
for(p1 = PLA->symbolic; p1 != NIL(symbolic_t); p1 = p1->next) {
for(p2=p1->symbolic_list; p2!=NIL(symbolic_list_t); p2=p2->next) {
set_remove(compress, p2->variable*2);
set_remove(compress, p2->variable*2+1);
}
}
num_deleted_vars = ((PLA->F->sf_size + size_added) - set_ord(compress))/2;
/* compute the new cube constants */
num_vars = cube.num_vars - num_deleted_vars + num_added_vars;
num_binary_vars = cube.num_binary_vars - num_deleted_vars;
new_size = cube.size - num_deleted_vars*2 + size_added;
new_part_size = ALLOC(int, num_vars);
new_part_size[num_vars-1] = cube.part_size[cube.num_vars-1];
for(var = cube.num_binary_vars; var < cube.num_vars-1; var++) {
new_part_size[var-num_deleted_vars] = cube.part_size[var];
}
/* re-size the covers, opening room for the new mv variables */
base = cube.first_part[cube.output];
PLA->F = sf_addcol(PLA->F, base, size_added);
PLA->D = sf_addcol(PLA->D, base, size_added);
PLA->R = sf_addcol(PLA->R, base, size_added);
/* compute the values for the new mv variables */
newvar = (cube.num_vars - 1) - num_deleted_vars;
for(p1 = PLA->symbolic; p1 != NIL(symbolic_t); p1 = p1->next) {
PLA->F = map_symbolic_cover(PLA->F, p1->symbolic_list, base);
PLA->D = map_symbolic_cover(PLA->D, p1->symbolic_list, base);
PLA->R = map_symbolic_cover(PLA->R, p1->symbolic_list, base);
base += 1 << p1->symbolic_list_length;
new_part_size[newvar++] = 1 << p1->symbolic_list_length;
}
/* delete the binary variables which disappear */
PLA->F = sf_compress(PLA->F, compress);
PLA->D = sf_compress(PLA->D, compress);
PLA->R = sf_compress(PLA->R, compress);
symbolic_hack_labels(PLA, PLA->symbolic, compress,
new_size, cube.size, size_added);
setdown_cube();
FREE(cube.part_size);
cube.num_vars = num_vars;
cube.num_binary_vars = num_binary_vars;
cube.part_size = new_part_size;
cube_setup();
set_free(compress);
}
pcover map_symbolic_cover(T, list, base)
pcover T;
symbolic_list_t *list;
int base;
{
pset last, p;
foreach_set(T, last, p) {
form_bitvector(p, base, 0, list);
}
return T;
}
void form_bitvector(p, base, value, list)
pset p; /* old cube, looking at binary variables */
int base; /* where in mv cube the new variable starts */
int value; /* current value for this recursion */
symbolic_list_t *list; /* current place in the symbolic list */
{
if (list == NIL(symbolic_list_t)) {
set_insert(p, base + value);
} else {
switch(GETINPUT(p, list->variable)) {
case ZERO:
form_bitvector(p, base, value*2, list->next);
break;
case ONE:
form_bitvector(p, base, value*2+1, list->next);
break;
case TWO:
form_bitvector(p, base, value*2, list->next);
form_bitvector(p, base, value*2+1, list->next);
break;
default:
fatal("bad cube in form_bitvector");
}
}
}
void symbolic_hack_labels(PLA, list, compress, new_size, old_size, size_added)
pPLA PLA;
symbolic_t *list;
pset compress;
int new_size, old_size, size_added;
{
int i, base;
char **oldlabel;
symbolic_t *p1;
symbolic_label_t *p3;
/* hack with the labels */
if ((oldlabel = PLA->label) == NIL(char *))
return;
PLA->label = ALLOC(char *, new_size);
for(i = 0; i < new_size; i++) {
PLA->label[i] = NIL(char);
}
/* copy the binary variable labels and unchanged mv variable labels */
base = 0;
for(i = 0; i < cube.first_part[cube.output]; i++) {
if (is_in_set(compress, i)) {
PLA->label[base++] = oldlabel[i];
} else {
if (oldlabel[i] != NIL(char)) {
FREE(oldlabel[i]);
}
}
}
/* add the user-defined labels for the symbolic outputs */
for(p1 = list; p1 != NIL(symbolic_t); p1 = p1->next) {
p3 = p1->symbolic_label;
for(i = 0; i < (1 << p1->symbolic_list_length); i++) {
if (p3 == NIL(symbolic_label_t)) {
PLA->label[base+i] = ALLOC(char, 10);
(void) sprintf(PLA->label[base+i], "X%d", i);
} else {
PLA->label[base+i] = p3->label;
p3 = p3->next;
}
}
base += 1 << p1->symbolic_list_length;
}
/* copy the labels for the binary outputs which remain */
for(i = cube.first_part[cube.output]; i < old_size; i++) {
if (is_in_set(compress, i + size_added)) {
PLA->label[base++] = oldlabel[i];
} else {
if (oldlabel[i] != NIL(char)) {
FREE(oldlabel[i]);
}
}
}
FREE(oldlabel);
}
static pcover fsm_simplify(F)
pcover F;
{
pcover D, R;
D = new_cover(0);
R = complement(cube1list(F));
F = espresso(F, D, R);
free_cover(D);
free_cover(R);
return F;
}
void disassemble_fsm(PLA, verbose_mode)
pPLA PLA;
int verbose_mode;
{
int nin, nstates, nout;
int before, after, present_state, next_state, i, j;
pcube next_state_mask, present_state_mask, state_mask, p, p1, last;
pcover go_nowhere, F, tF;
/* We make the DISGUSTING assumption that the first 'n' outputs have
* been created by .symbolic-output, and represent a one-hot encoding
* of the next state. 'n' is the size of the second-to-last multiple-
* valued variable (i.e., before the outputs
*/
if (cube.num_vars - cube.num_binary_vars != 2) {
(void) fprintf(stderr,
"use .symbolic and .symbolic-output to specify\n");
(void) fprintf(stderr,
"the present state and next state field information\n");
fatal("disassemble_pla: need two multiple-valued variables\n");
}
nin = cube.num_binary_vars;
nstates = cube.part_size[cube.num_binary_vars];
nout = cube.part_size[cube.num_vars - 1];
if (nout < nstates) {
(void) fprintf(stderr,
"use .symbolic and .symbolic-output to specify\n");
(void) fprintf(stderr,
"the present state and next state field information\n");
fatal("disassemble_pla: # outputs < # states\n");
}
present_state = cube.first_part[cube.num_binary_vars];
present_state_mask = new_cube();
for(i = 0; i < nstates; i++) {
set_insert(present_state_mask, i + present_state);
}
next_state = cube.first_part[cube.num_binary_vars+1];
next_state_mask = new_cube();
for(i = 0; i < nstates; i++) {
set_insert(next_state_mask, i + next_state);
}
state_mask = set_or(new_cube(), next_state_mask, present_state_mask);
F = new_cover(10);
/*
* check for arcs which go from ANY state to state #i
*/
for(i = 0; i < nstates; i++) {
tF = new_cover(10);
foreach_set(PLA->F, last, p) {
if (setp_implies(present_state_mask, p)) { /* from any state ! */
if (is_in_set(p, next_state + i)) {
tF = sf_addset(tF, p);
}
}
}
before = tF->count;
if (before > 0) {
tF = fsm_simplify(tF);
/* don't allow the next state to disappear ... */
foreach_set(tF, last, p) {
set_insert(p, next_state + i);
}
after = tF->count;
F = sf_append(F, tF);
if (verbose_mode) {
printf("# state EVERY to %d, before=%d after=%d\n",
i, before, after);
}
}
}
/*
* some 'arcs' may NOT have a next state -- handle these
* we must unravel the present state part
*/
go_nowhere = new_cover(10);
foreach_set(PLA->F, last, p) {
if (setp_disjoint(p, next_state_mask)) { /* no next state !! */
go_nowhere = sf_addset(go_nowhere, p);
}
}
before = go_nowhere->count;
go_nowhere = unravel_range(go_nowhere,
cube.num_binary_vars, cube.num_binary_vars);
after = go_nowhere->count;
F = sf_append(F, go_nowhere);
if (verbose_mode) {
printf("# state ANY to NOWHERE, before=%d after=%d\n", before, after);
}
/*
* minimize cover for all arcs from state #i to state #j
*/
for(i = 0; i < nstates; i++) {
for(j = 0; j < nstates; j++) {
tF = new_cover(10);
foreach_set(PLA->F, last, p) {
/* not EVERY state */
if (! setp_implies(present_state_mask, p)) {
if (is_in_set(p, present_state + i)) {
if (is_in_set(p, next_state + j)) {
p1 = set_save(p);
set_diff(p1, p1, state_mask);
set_insert(p1, present_state + i);
set_insert(p1, next_state + j);
tF = sf_addset(tF, p1);
set_free(p1);
}
}
}
}
before = tF->count;
if (before > 0) {
tF = fsm_simplify(tF);
/* don't allow the next state to disappear ... */
foreach_set(tF, last, p) {
set_insert(p, next_state + j);
}
after = tF->count;
F = sf_append(F, tF);
if (verbose_mode) {
printf("# state %d to %d, before=%d after=%d\n",
i, j, before, after);
}
}
}
}
free_cube(state_mask);
free_cube(present_state_mask);
free_cube(next_state_mask);
free_cover(PLA->F);
PLA->F = F;
free_cover(PLA->D);
PLA->D = new_cover(0);
setdown_cube();
FREE(cube.part_size);
cube.num_binary_vars = nin;
cube.num_vars = nin + 3;
cube.part_size = ALLOC(int, cube.num_vars);
cube.part_size[cube.num_binary_vars] = nstates;
cube.part_size[cube.num_binary_vars+1] = nstates;
cube.part_size[cube.num_binary_vars+2] = nout - nstates;
cube_setup();
foreach_set(PLA->F, last, p) {
kiss_print_cube(stdout, PLA, p, "~1");
}
}
ABC_NAMESPACE_IMPL_END
|