-
Notifications
You must be signed in to change notification settings - Fork 34
/
learn.cpp
658 lines (588 loc) · 14 KB
/
learn.cpp
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
647
648
649
650
651
652
653
654
655
656
657
658
/***********************************************************************
Copyright (C) 1991,
Virginia Polytechnic Institute & State University
This program was originally written by Mr. Hyung K. Lee
under the supervision of Dr. Dong S. Ha, in the Bradley
Department of Electrical Engineering, VPI&SU, in 1991.
This program is released for research use only. This program,
or any derivative thereof, may not be reproduced nor used
for any commercial product without the written permission
of the authors.
For detailed information, please contact to
Dr. Dong S. Ha
Bradley Department of Electrical Engineering
Virginia Polytechnic Institute & State University
Blacksburg, VA 24061
Ph.: (540) 231-4942
Fax: (540) 231-3362
E-Mail: [email protected]
Web: http://www.ee.vt.edu/ha
REFERENCE:
H. K. Lee and D. S. Ha, "On the Generation of Test Patterns
for Combinational Circuits," Technical Report No. 12_93,
Dep't of Electrical Eng., Virginia Polytechnic Institute
and State University.
***********************************************************************/
/**************************** HISTORY **********************************
atalanta: version 1.1 H. K. Lee, 10/5/1992
atalanta: version 2.0 H. K. Lee, 6/30/1997
***********************************************************************/
/*-----------------------------------------------------------------
filename learn.c
Static Learning procedure
-----------------------------------------------------------------*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "learn.h"
#include "error.h"
#include "parameter.h"
#include "define.h"
#include "macro.h"
/* #define DEBUGLEARN 1 */
#define FAIL 0
#define PASS 1
#define PASS1 2
extern GATEPTR *g_net;
// extern void learn_node(), store_learn();
// extern void initgood(), fatalerror();
extern level g_iTruthTable1[MAXGTYPE][ATALEVEL];
extern level g_iTruthTable2[MAXGTYPE][ATALEVEL][ATALEVEL];
extern STACKTYPE g_stack, *g_pEventListStack;
extern int mac_i;
int SNODE;
level SVAL;
struct EDEN *impo;
int g_iLid = 0;
#ifdef DEBUGLEARN
int norecord = 0, noimpo = 0, learnmemory = 0;
double slearntime_beg, ulearntime_beg, learntime_beg;
double slearntime_end, ulearntime_end, learntime_end;
char *level2str[4] =
{
"0", "1", "x", "z"
};
#endif
/* macros for the gate evaluation using the truthtable */
#define gate_eval1(pGate, iValue, iGateType, i) \
if (pGate->inCount == 1) \
iValue = g_iTruthTable1[pGate->type][pGate->inList[0]->output]; \
else if (pGate->inCount == 2) \
iValue = g_iTruthTable2[pGate->type][pGate->inList[0]->output][pGate->inList[1]->output];\
else if(pGate->inCount == 0) \
iValue = pGate->output; \
else \
{ \
iGateType = (pGate->type == NAND) ? AND : \
(pGate->type == NOR) ? OR : pGate->type; \
iValue = g_iTruthTable2[iGateType][pGate->inList[0]->output][pGate->inList[1]->output]; \
for (i = 2; i < pGate->inCount; i++) \
iValue = g_iTruthTable2[iGateType][iValue][pGate->inList[i]->output]; \
iValue = (pGate->type == NAND||pGate->type == NOR)? A_NOT(iValue) : iValue; \
}
/*------leval---------------------------------------------------
Evaluate good circuit in forward and backward completely.
Set changed flag if the gate is evaluated permanently.
Push the evaluated gate to stack.
Schedule next events.
---------------------------------------------------------------*/
status leval(register GATEPTR pGate)
{
register int i, j;
register level iValue, v1;
int num_x;
logic iGateType;
GATEPTR *pInListGates;
/* forward pGate evaluation */
reset(pGate->changed);
pInListGates = pGate->inList;
/* if a line is a head line, stop */
if (is_free(pGate))
{
return(FORWARD);
}
/* fault free pGate evaluation */
for (i = 0; i< pGate->inCount; i++)
if (pGate->inList[i]->numzero == g_iLid)
{
pGate->numzero = g_iLid; break;
}
gate_eval1(pGate, iValue, iGateType, i);
if (iValue == pGate->output)
{
/* no event */
if (iValue != X)
{
set(pGate->changed);
}
return(FORWARD);
}
if (pGate->output == X)
{
/* forward evaluation */
pGate->output = iValue; /* update pGate output */
push(g_stack, pGate);
set(pGate->changed);
schedule_output(pGate);
store_learn(pGate, iValue);
return(FORWARD);
}
if (iValue != X)
{
return(CONFLICT);
} /* report conflict */
/* backward implication */
switch (pGate->type)
{
case AND:
case NAND:
case OR:
case NOR:
v1 = (pGate->type == AND || pGate->type == NOR) ? ONE : ZERO;
if (pGate->output == v1)
{
set(pGate->changed);
for (i = 0; i< pGate->inCount; i++)
if (pInListGates[i]->output == X)
{
pInListGates[i]->output = g_iTruthTable1[pGate->type][v1];
push(g_stack, pInListGates[i]);
schedule_input(pGate, i);
}
return(BACKWARD);
}
else
{
for (i = num_x = 0; i< pGate->inCount; i++)
if (pInListGates[i]->output == X)
{
num_x++; j = i;
}
if (num_x == 1)
{
pInListGates[j]->output = g_iTruthTable1[pGate->type][pGate->output];
set(pGate->changed);
push(g_stack, pInListGates[j]);
schedule_input(pGate, j);
return(BACKWARD);
}
}
break;
case BUFF:
case NOT:
case PO:
pInListGates[0]->output = g_iTruthTable1[pGate->type][pGate->output];
set(pGate->changed);
push(g_stack, pInListGates[0]);
schedule_input(pGate, 0);
return(BACKWARD);
break;
case XOR:
case XNOR:
for (i = num_x = 0; i< pGate->inCount; i++)
if (pInListGates[i]->output == X)
{
num_x++; j = i;
}
if (num_x == 1)
{
v1 = (j == 0) ? pInListGates[1]->output : pInListGates[0]->output;
iValue = g_iTruthTable1[pGate->type][pGate->output];
if (v1 == ONE)
{
iValue = g_iTruthTable1[NOT][iValue];
}
pInListGates[j]->output = iValue;
set(pGate->changed);
push(g_stack, pInListGates[j]);
schedule_input(pGate, j);
return(BACKWARD);
}
break;
}
/*
v1=(pGate->type==AND || pGate->type==NOR) ? ONE :
(pGate->type==NAND || pGate->type==OR) ? ZERO : X;
if(pGate->output==v1) {
set(pGate->changed);
if(pGate->type==NAND || pGate->type==NOR) v1=A_NOT(v1);
for(i=0;i<pGate->inCount;i++)
if(pInListGates[i]->output==X) {
pInListGates[i]->output=v1;
push(stack,pInListGates[i]);
schedule_input(pGate,i);
}
return(BACKWARD);
}
else {
num_x=0;
for(i=0;i<pGate->inCount;i++)
if(pInListGates[i]->output==X) {
num_x++;
j=i;
}
if(num_x==1) {
if(v1!=X)
pInListGates[j]->output = (pGate->type==NAND||pGate->type==NOR) ?
A_NOT(pGate->output) : pGate->output;
else {
v1=(j==0) ? pInListGates[1]->output : pInListGates[0]->output;
pInListGates[j]->output=(v1==pGate->output)?ZERO:ONE;
}
set(pGate->changed);
push(stack,pInListGates[j]);
schedule_input(pGate,j);
return(BACKWARD);
}
}
*/
return(FORWARD);
}
/* impval
Forward and backward implication.
*/
bool impval(int maxdpi, bool backward, int last)
{
register int i, start;
status st;
GATEPTR g;
if (backward)
{
start = last;
}
else
{
start = 0;
}
while (TRUE)
{
/* backward implication */
if (backward)
{
for (i = start; i >= 0; i--)
while (!is_empty(g_pEventListStack[i]))
{
g = pop(g_pEventListStack[i]);
if ((st = leval(g)) == CONFLICT)
{
return(FALSE);
}
}
}
/* forward implication */
reset(backward);
for (i = 0; i < maxdpi; i++)
{
while (!is_empty(g_pEventListStack[i]))
{
if ((st = leval(pop(g_pEventListStack[i]))) == CONFLICT)
{
return(FALSE);
}
else if (st == BACKWARD)
{
start = i - 1;
set(backward);
break;
}
}
if (backward)
{
break;
}
}
if (!backward)
{
break;
}
}
return(TRUE);
}
/*------learn---------------------------------------------
preprocessing for static learning
Good cct only
----------------------------------------------------------*/
void learn(int iNoGate, int iMaxLevelAdd2)
{
register int ix;
GATEPTR pGate;
struct EDEN *temp;
struct LEARN *record;
#ifdef DEBUGLEARN
getTime(&slearntime_beg, &ulearntime_beg, &learntime_beg);
printf("DEBUG: Start learn(iNoGate,iMaxLevelAdd2), iNoGate=%d, iMaxLevelAdd2=%d\n", iNoGate, iMaxLevelAdd2);
#endif
for (temp = impo; temp != NULL;)
{
impo = temp->next;
MFREE(temp);
temp = impo->next;
}
for (ix = 0; ix < iNoGate; ix++)
{
pGate = g_net[ix];
reset(pGate->changed);
pGate->numzero = (-1);
pGate->output = X;
for (record = pGate->plearn; record != NULL;)
{
pGate->plearn = record->next;
MFREE(record);
record = pGate->plearn;
}
}
for (ix = 0; ix < iNoGate; ix++)
{
pGate = g_net[ix];
if (is_free(pGate))
{
continue;
}
if (pGate->inCount == 1)
{
continue;
}
#ifdef DEBUGLEARN
printf("** Learn node %d: symbol=%s fi=%d fo=%d\n", pGate->index, pGate->hash->symbol, pGate->inCount, pGate->outCount);
#endif
switch (pGate->type)
{
case AND:
case NOR:
learn_node(iMaxLevelAdd2, ix, ONE);
if (pGate->outCount > 1)
{
learn_node(iMaxLevelAdd2, ix, ZERO);
}
break;
case OR:
case NAND:
learn_node(iMaxLevelAdd2, ix, ZERO);
if (pGate->outCount > 1)
{
learn_node(iMaxLevelAdd2, ix, ONE);
}
break;
default:
if (pGate->outCount > 1)
{
learn_node(iMaxLevelAdd2, ix, ZERO);
learn_node(iMaxLevelAdd2, ix, ONE);
}
}
}
clear(g_stack);
#ifdef DEBUGLEARN
getTime(&slearntime_end, &ulearntime_end, &learntime_end);
printf("\n*** End of learning\n");
printf("*** Number of redundant nodes = %d\n", noimpo);
printf("*** Number of learning records = %d\n", norecord);
printf("*** Memory required = %d bytes\n", learnmemory);
printf("*** CPU time spent = %.2f seconds\n", learntime_end - learntime_beg);
#endif
}
/*------learn_node: learning procedure for a node------*/
void learn_node(int maxdpi, int node, level val)
{
register int ix;
struct EDEN *tmp;
GATEPTR gut = g_net[node], tg;
SNODE = node;
gut->output = val;
push(g_stack, gut);
SVAL = A_NOT(val);
pushevent(gut);
gut->numzero = ++g_iLid;
schedule_output(gut);
if (impval(maxdpi, FORWARD, 0) == FALSE)
{
ALLOCATE(tmp, struct EDEN, 1);
tmp->node = node;
tmp->val = A_NOT(val);
tmp->next = impo;
impo = tmp;
#ifdef DEBUGLEARN
noimpo++;
learnmemory += sizeof(struct EDEN);
printf("learn: impo: node %d = %s\n", tmp->node, level2str[tmp->val]);
#endif
for (ix = 0; ix < maxdpi; ix++)
while (!is_empty(g_pEventListStack[ix]))
{
gut = pop(g_pEventListStack[ix]);
reset(gut->changed);
}
}
/* restore good values */
for (ix = g_stack.last; ix >= 0; ix--)
{
tg = g_stack.list[ix];
tg->output = X;
reset(tg->changed);
}
clear(g_stack);
}
/*------store_learn: gather info from learning------*/
void store_learn(GATEPTR pGate, level iValue)
{
struct LEARN *pLearn;
if (pGate->inCount < 2)
{
return;
}
if (pGate->numzero != g_iLid)
{
return;
}
switch (pGate->type)
{
case AND:
case NOR:
if (iValue == ZERO)
{
return;
}
break;
case OR:
case NAND:
if (iValue == ONE)
{
return;
}
break;
case XOR:
case XNOR:
break;
default:
return;
}
ALLOCATE(pLearn, struct LEARN, 1);
pLearn->node = SNODE;
pLearn->tval = SVAL;
pLearn->sval = A_NOT(iValue);
pLearn->next = pGate->plearn;
pGate->plearn = pLearn;
#ifdef DEBUGLEARN
norecord++;
learnmemory += sizeof(struct LEARN);
printf("learn: (node %d = %s) from ", pLearn->node, level2str[pLearn->tval]);
printf("(node %d = %s)\n", pGate->index, level2str[pLearn->sval]);
#endif
}
int conflict_tbl[3][5] =
/* 0 1 x d dbar old/new */
{
{ PASS, FAIL, PASS1, FAIL, PASS }, /* 0 */
{ FAIL, PASS, PASS1, PASS, FAIL }, /* 1 */
{ PASS, PASS, PASS, PASS, PASS } /* x */
};
/*------imply_learn--------------------------------------*/
status imply_learn(register GATEPTR pGate, register level iValue)
{
register struct LEARN *pLearn;
register GATEPTR pLearnGate;
int iState;
switch (pGate->type)
{
case AND:
case NOR:
if (iValue == ONE)
{
return(FORWARD);
} break;
case OR:
case NAND:
if (iValue == ZERO)
{
return(FORWARD);
} break;
}
iState = FORWARD;
for (pLearn = pGate->plearn; pLearn != NULL; pLearn = pLearn->next)
if (pLearn->sval == iValue)
{
pLearnGate = g_net[pLearn->node];
switch (conflict_tbl[pLearnGate->output][pLearn->tval])
{
case PASS:
break;
case FAIL:
#ifdef DEBUGLEARN
printf("Learned: conflict at node=%d old=%s new=%s from node=%d iValue=%s\n", pLearnGate->index, level2str[pLearnGate->output], level2str[pLearn->tval], pGate->index, level2str[iValue]);
#endif
return(CONFLICT);
case PASS1:
pLearnGate->output = pLearn->tval;
push(g_stack, pLearnGate);
pushevent(pLearnGate);
schedule_output(pLearnGate);
#ifdef DEBUGLEARN
printf("Learned: node=%d iValue=%s from node=%d iValue=%s\n", pLearnGate->index, level2str[pLearnGate->output], pGate->index, level2str[iValue]);
#endif
iState = BACKWARD;
break;
}
}
return(iState);
}
/*------imply_learn1-------------------------------------*/
status imply_learn1(register GATEPTR gut, register level val)
{
register struct LEARN *tmp;
register GATEPTR tg;
int state;
if (val == D)
{
val = 1;
}
else if (val == DBAR)
{
val = 0;
}
switch (gut->type)
{
case AND:
case NOR:
if (val == ONE)
{
return(FORWARD);
} break;
case OR:
case NAND:
if (val == ZERO)
{
return(FORWARD);
} break;
}
state = FORWARD;
for (tmp = gut->plearn; tmp != NULL; tmp = tmp->next)
if (tmp->sval == val)
{
tg = g_net[tmp->node];
if (tg->freach)
{
continue;
}
switch (conflict_tbl[tg->output][tmp->tval])
{
case PASS:
break;
case FAIL:
#ifdef DEBUGLEARN
printf("Learned: conflict at node=%d old=%s new=%s from node=%d val=%s\n", tg->index, level2str[tg->output], level2str[tmp->tval], gut->index, level2str[val]);
#endif
return(CONFLICT);
case PASS1:
tg->output = tmp->tval;
push(g_stack, tg);
pushevent(tg);
schedule_output(tg);
#ifdef DEBUGLEARN
printf("Learned: node=%d val=%s from node=%d val=%s\n", tg->index, level2str[tg->output], gut->index, level2str[val]);
#endif
state = BACKWARD;
break;
}
}
return(state);
}