ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
saigSimSeq.c
Go to the documentation of this file.
1
20
21#include "saig.h"
22#include "proof/ssw/ssw.h"
23
25
26
30
31// combinational simulation manager
32typedef struct Raig_Man_t_ Raig_Man_t;
34{
35 // parameters
36 Aig_Man_t * pAig; // the AIG to be used for simulation
37 int nWords; // the number of words to simulate
38 // AIG representation
39 int nPis; // the number of primary inputs
40 int nPos; // the number of primary outputs
41 int nCis; // the number of combinational inputs
42 int nCos; // the number of combinational outputs
43 int nNodes; // the number of internal nodes
44 int nObjs; // nCis + nNodes + nCos + 2
45 int * pFans0; // fanin0 for all objects
46 int * pFans1; // fanin1 for all objects
47 Vec_Int_t * vCis2Ids; // mapping of CIs into their PI ids
48 Vec_Int_t * vLos; // register outputs
49 Vec_Int_t * vLis; // register inputs
50 // simulation info
51 int * pRefs; // reference counter for each node
52 unsigned * pSims; // simlulation information for each node
53 // recycable memory
54 unsigned * pMems; // allocated simulaton memory
55 int nWordsAlloc; // the number of allocated entries
56 int nMems; // the number of used entries
57 int nMemsMax; // the max number of used entries
58 int MemFree; // next free entry
59};
60
61static inline int Raig_Var2Lit( int Var, int fCompl ) { return Var + Var + fCompl; }
62static inline int Raig_Lit2Var( int Lit ) { return Lit >> 1; }
63static inline int Raig_LitIsCompl( int Lit ) { return Lit & 1; }
64static inline int Raig_LitNot( int Lit ) { return Lit ^ 1; }
65static inline int Raig_LitNotCond( int Lit, int c ) { return Lit ^ (int)(c > 0); }
66static inline int Raig_LitRegular( int Lit ) { return Lit & ~01; }
67
71
83int Raig_ManFindPo( Aig_Man_t * pAig, int iNode )
84{
85 Aig_Obj_t * pObj;
86 int i;
87 Saig_ManForEachPo( pAig, pObj, i )
88 if ( pObj->iData == iNode )
89 return i;
90 return -1;
91}
92
105{
106 int iFan0, iFan1;
107 assert( !Aig_IsComplement(pObj) );
108 if ( pObj->iData )
109 return pObj->iData;
110 assert( !Aig_ObjIsConst1(pObj) );
111 if ( Aig_ObjIsNode(pObj) )
112 {
113 iFan0 = Raig_ManCreate_rec( p, Aig_ObjFanin0(pObj) );
114 iFan0 = (iFan0 << 1) | Aig_ObjFaninC0(pObj);
115 iFan1 = Raig_ManCreate_rec( p, Aig_ObjFanin1(pObj) );
116 iFan1 = (iFan1 << 1) | Aig_ObjFaninC1(pObj);
117 }
118 else if ( Aig_ObjIsCo(pObj) )
119 {
120 iFan0 = Raig_ManCreate_rec( p, Aig_ObjFanin0(pObj) );
121 iFan0 = (iFan0 << 1) | Aig_ObjFaninC0(pObj);
122 iFan1 = 0;
123 }
124 else
125 {
126 iFan0 = iFan1 = 0;
127 Vec_IntPush( p->vCis2Ids, Aig_ObjCioId(pObj) );
128 }
129 p->pFans0[p->nObjs] = iFan0;
130 p->pFans1[p->nObjs] = iFan1;
131 p->pRefs[p->nObjs] = Aig_ObjRefs(pObj);
132 return pObj->iData = p->nObjs++;
133}
134
147{
148 Raig_Man_t * p;
149 Aig_Obj_t * pObj;
150 int i, nObjs;
151 Aig_ManCleanData( pAig );
152 p = (Raig_Man_t *)ABC_ALLOC( Raig_Man_t, 1 );
153 memset( p, 0, sizeof(Raig_Man_t) );
154 p->pAig = pAig;
155 p->nPis = Saig_ManPiNum(pAig);
156 p->nPos = Saig_ManPoNum(pAig);
157 p->nCis = Aig_ManCiNum(pAig);
158 p->nCos = Aig_ManCoNum(pAig);
159 p->nNodes = Aig_ManNodeNum(pAig);
160 nObjs = p->nCis + p->nCos + p->nNodes + 2;
161 p->pFans0 = ABC_ALLOC( int, nObjs );
162 p->pFans1 = ABC_ALLOC( int, nObjs );
163 p->pRefs = ABC_ALLOC( int, nObjs );
164 p->pSims = ABC_CALLOC( unsigned, nObjs );
165 p->vCis2Ids = Vec_IntAlloc( Aig_ManCiNum(pAig) );
166 // add objects (0=unused; 1=const1)
167 p->nObjs = 2;
168 pObj = Aig_ManConst1( pAig );
169 pObj->iData = 1;
170 Aig_ManForEachCi( pAig, pObj, i )
171 if ( Aig_ObjRefs(pObj) == 0 )
172 Raig_ManCreate_rec( p, pObj );
173 Aig_ManForEachCo( pAig, pObj, i )
174 Raig_ManCreate_rec( p, pObj );
175 assert( Vec_IntSize(p->vCis2Ids) == Aig_ManCiNum(pAig) );
176 assert( p->nObjs == nObjs );
177 // collect flop outputs
178 p->vLos = Vec_IntAlloc( Aig_ManRegNum(pAig) );
179 Saig_ManForEachLo( pAig, pObj, i )
180 Vec_IntPush( p->vLos, pObj->iData );
181 // collect flop inputs
182 p->vLis = Vec_IntAlloc( Aig_ManRegNum(pAig) );
183 Saig_ManForEachLi( pAig, pObj, i )
184 {
185 Vec_IntPush( p->vLis, pObj->iData );
186 assert( p->pRefs[ pObj->iData ] == 0 );
187 p->pRefs[ pObj->iData ]++;
188 }
189 return p;
190}
191
204{
205 Vec_IntFree( p->vCis2Ids );
206 Vec_IntFree( p->vLos );
207 Vec_IntFree( p->vLis );
208 ABC_FREE( p->pFans0 );
209 ABC_FREE( p->pFans1 );
210 ABC_FREE( p->pRefs );
211 ABC_FREE( p->pSims );
212 ABC_FREE( p->pMems );
213 ABC_FREE( p );
214}
215
227unsigned * Raig_ManSimRef( Raig_Man_t * p, int i )
228{
229 unsigned * pSim;
230 assert( i > 1 );
231 assert( p->pSims[i] == 0 );
232 if ( p->MemFree == 0 )
233 {
234 unsigned * pPlace, Ent;
235 if ( p->nWordsAlloc == 0 )
236 {
237 assert( p->pMems == NULL );
238 p->nWordsAlloc = (1<<17); // -> 1Mb
239 p->nMems = 1;
240 }
241 p->nWordsAlloc *= 2;
242 p->pMems = ABC_REALLOC( unsigned, p->pMems, p->nWordsAlloc );
243 memset( p->pMems, 0xff, sizeof(unsigned) * (p->nWords + 1) );
244 pPlace = (unsigned *)&p->MemFree;
245 for ( Ent = p->nMems * (p->nWords + 1);
246 Ent + p->nWords + 1 < (unsigned)p->nWordsAlloc;
247 Ent += p->nWords + 1 )
248 {
249 *pPlace = Ent;
250 pPlace = p->pMems + Ent;
251 }
252 *pPlace = 0;
253 }
254 p->pSims[i] = p->MemFree;
255 pSim = p->pMems + p->MemFree;
256 p->MemFree = pSim[0];
257 pSim[0] = p->pRefs[i];
258 p->nMems++;
259 if ( p->nMemsMax < p->nMems )
260 p->nMemsMax = p->nMems;
261 return pSim;
262}
263
275unsigned * Raig_ManSimDeref( Raig_Man_t * p, int i )
276{
277 unsigned * pSim;
278 assert( i );
279 if ( i == 1 ) // const 1
280 return p->pMems;
281 assert( p->pSims[i] > 0 );
282 pSim = p->pMems + p->pSims[i];
283 if ( --pSim[0] == 0 )
284 {
285 pSim[0] = p->MemFree;
286 p->MemFree = p->pSims[i];
287 p->pSims[i] = 0;
288 p->nMems--;
289 }
290 return pSim;
291}
292
304int Raig_ManSimulateRound( Raig_Man_t * p, int fMiter, int fFirst, int * piPat )
305{
306 unsigned * pRes0, * pRes1, * pRes;
307 int i, w, nCis, nCos, iFan0, iFan1, iPioNum;
308 // nove the values to the register outputs
309 Vec_IntForEachEntry( p->vCis2Ids, iPioNum, i )
310 {
311 if ( iPioNum < p->nPis )
312 continue;
313 pRes = Raig_ManSimRef( p, Vec_IntEntry(p->vLos, iPioNum-p->nPis) );
314 if ( fFirst )
315 memset( pRes + 1, 0, sizeof(unsigned) * p->nWords );
316 else
317 {
318 pRes0 = Raig_ManSimDeref( p, Vec_IntEntry(p->vLis, iPioNum-p->nPis) );
319 for ( w = 1; w <= p->nWords; w++ )
320 pRes[w] = pRes0[w];
321 }
322 // handle unused PIs
323 if ( pRes[0] == 0 )
324 {
325 pRes[0] = 1;
326 Raig_ManSimDeref( p, Vec_IntEntry(p->vLos, iPioNum-p->nPis) );
327 }
328 }
329 // simulate the logic
330 nCis = nCos = 0;
331 for ( i = 2; i < p->nObjs; i++ )
332 {
333 if ( p->pFans0[i] == 0 ) // ci always has zero first fanin
334 {
335 iPioNum = Vec_IntEntry( p->vCis2Ids, nCis );
336 if ( iPioNum < p->nPis )
337 {
338 pRes = Raig_ManSimRef( p, i );
339 for ( w = 1; w <= p->nWords; w++ )
340 pRes[w] = Aig_ManRandom( 0 );
341 // handle unused PIs
342 if ( pRes[0] == 0 )
343 {
344 pRes[0] = 1;
345 Raig_ManSimDeref( p, i );
346 }
347 }
348 else
349 assert( Vec_IntEntry(p->vLos, iPioNum-p->nPis) == i );
350 nCis++;
351 continue;
352 }
353 if ( p->pFans1[i] == 0 ) // co always has non-zero 1st fanin and zero 2nd fanin
354 {
355 pRes0 = Raig_ManSimDeref( p, Raig_Lit2Var(p->pFans0[i]) );
356 if ( nCos < p->nPos && fMiter )
357 {
358 unsigned Const = Raig_LitIsCompl(p->pFans0[i])? ~0 : 0;
359 for ( w = 1; w <= p->nWords; w++ )
360 if ( pRes0[w] != Const )
361 {
362 *piPat = 32*(w-1) + Aig_WordFindFirstBit( pRes0[w] ^ Const );
363 return i;
364 }
365 }
366 else
367 {
368 pRes = Raig_ManSimRef( p, i );
369 assert( pRes[0] == 1 );
370 if ( Raig_LitIsCompl(p->pFans0[i]) )
371 for ( w = 1; w <= p->nWords; w++ )
372 pRes[w] = ~pRes0[w];
373 else
374 for ( w = 1; w <= p->nWords; w++ )
375 pRes[w] = pRes0[w];
376 }
377 nCos++;
378 continue;
379 }
380 pRes = Raig_ManSimRef( p, i );
381 assert( pRes[0] > 0 );
382 iFan0 = p->pFans0[i];
383 iFan1 = p->pFans1[i];
384 pRes0 = Raig_ManSimDeref( p, Raig_Lit2Var(p->pFans0[i]) );
385 pRes1 = Raig_ManSimDeref( p, Raig_Lit2Var(p->pFans1[i]) );
386 if ( Raig_LitIsCompl(iFan0) && Raig_LitIsCompl(iFan1) )
387 for ( w = 1; w <= p->nWords; w++ )
388 pRes[w] = ~(pRes0[w] | pRes1[w]);
389 else if ( Raig_LitIsCompl(iFan0) && !Raig_LitIsCompl(iFan1) )
390 for ( w = 1; w <= p->nWords; w++ )
391 pRes[w] = ~pRes0[w] & pRes1[w];
392 else if ( !Raig_LitIsCompl(iFan0) && Raig_LitIsCompl(iFan1) )
393 for ( w = 1; w <= p->nWords; w++ )
394 pRes[w] = pRes0[w] & ~pRes1[w];
395 else if ( !Raig_LitIsCompl(iFan0) && !Raig_LitIsCompl(iFan1) )
396 for ( w = 1; w <= p->nWords; w++ )
397 pRes[w] = pRes0[w] & pRes1[w];
398 }
399 assert( nCis == p->nCis );
400 assert( nCos == p->nCos );
401 assert( p->nMems == 1 + Vec_IntSize(p->vLis) );
402 return 0;
403}
404
416Abc_Cex_t * Raig_ManGenerateCounter( Aig_Man_t * pAig, int iFrame, int iOut, int nWords, int iPat, Vec_Int_t * vCis2Ids )
417{
418 Abc_Cex_t * p;
419 unsigned * pData;
420 int f, i, w, iPioId, Counter;
421 p = Abc_CexAlloc( Aig_ManRegNum(pAig), Saig_ManPiNum(pAig), iFrame+1 );
422 p->iFrame = iFrame;
423 p->iPo = iOut;
424 // fill in the binary data
425 Aig_ManRandom( 1 );
426 Counter = p->nRegs;
427 pData = ABC_ALLOC( unsigned, nWords );
428 for ( f = 0; f <= iFrame; f++, Counter += p->nPis )
429 for ( i = 0; i < Aig_ManCiNum(pAig); i++ )
430 {
431 iPioId = Vec_IntEntry( vCis2Ids, i );
432 if ( iPioId >= p->nPis )
433 continue;
434 for ( w = 0; w < nWords; w++ )
435 pData[w] = Aig_ManRandom( 0 );
436 if ( Abc_InfoHasBit( pData, iPat ) )
437 Abc_InfoSetBit( p->pData, Counter + iPioId );
438 }
439 ABC_FREE( pData );
440 return p;
441}
442
454int Raig_ManSimulate( Aig_Man_t * pAig, int nWords, int nIters, int TimeLimit, int fMiter, int fVerbose )
455{
456 Raig_Man_t * p;
457 Sec_MtrStatus_t Status;
458 int i, iPat, RetValue = 0;
459 abctime clk, clkTotal = Abc_Clock();
460 assert( Aig_ManRegNum(pAig) > 0 );
461 Status = Sec_MiterStatus( pAig );
462 if ( Status.nSat > 0 )
463 {
464 printf( "Miter is trivially satisfiable (output %d).\n", Status.iOut );
465 return 1;
466 }
467 if ( Status.nUndec == 0 )
468 {
469 printf( "Miter is trivially unsatisfiable.\n" );
470 return 0;
471 }
472 Aig_ManRandom( 1 );
473 p = Raig_ManCreate( pAig );
474 p->nWords = nWords;
475 // iterate through objects
476 for ( i = 0; i < nIters; i++ )
477 {
478 clk = Abc_Clock();
479 RetValue = Raig_ManSimulateRound( p, fMiter, i==0, &iPat );
480 if ( fVerbose )
481 {
482 printf( "Frame %4d out of %4d and timeout %3d sec. ", i+1, nIters, TimeLimit );
483 printf("Time = %7.2f sec\r", (1.0*Abc_Clock()-clkTotal)/CLOCKS_PER_SEC);
484 }
485 if ( RetValue > 0 )
486 {
487 int iOut = Raig_ManFindPo(p->pAig, RetValue);
488 assert( pAig->pSeqModel == NULL );
489 pAig->pSeqModel = Raig_ManGenerateCounter( pAig, i, iOut, nWords, iPat, p->vCis2Ids );
490 if ( fVerbose )
491 printf( "Miter is satisfiable after simulation (output %d).\n", iOut );
492 break;
493 }
494 if ( (Abc_Clock() - clk)/CLOCKS_PER_SEC >= TimeLimit )
495 {
496 printf( "No bug detected after %d frames with time limit %d seconds.\n", i+1, TimeLimit );
497 break;
498 }
499 }
500 if ( fVerbose )
501 {
502 printf( "Maxcut = %8d. AigMem = %7.2f MB. SimMem = %7.2f MB. ",
503 p->nMemsMax,
504 1.0*(p->nObjs * 16)/(1<<20),
505 1.0*(p->nMemsMax * 4 * (nWords+1))/(1<<20) );
506 ABC_PRT( "Total time", Abc_Clock() - clkTotal );
507 }
508 Raig_ManDelete( p );
509 return RetValue > 0;
510}
511
515
516
518
int nWords
Definition abcNpn.c:127
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_PRT(a, t)
Definition abc_global.h:255
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define Aig_ManForEachCi(p, pObj, i)
ITERATORS ///.
Definition aig.h:393
struct Aig_Obj_t_ Aig_Obj_t
Definition aig.h:51
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
Definition aig.h:50
#define Aig_ManForEachCo(p, pObj, i)
Definition aig.h:398
void Aig_ManCleanData(Aig_Man_t *p)
Definition aigUtil.c:205
unsigned Aig_ManRandom(int fReset)
Definition aigUtil.c:1170
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
int Var
Definition exorList.c:228
unsigned * Raig_ManSimRef(Raig_Man_t *p, int i)
Definition saigSimSeq.c:227
typedefABC_NAMESPACE_IMPL_START struct Raig_Man_t_ Raig_Man_t
DECLARATIONS ///.
Definition saigSimSeq.c:32
int Raig_ManCreate_rec(Raig_Man_t *p, Aig_Obj_t *pObj)
Definition saigSimSeq.c:104
Abc_Cex_t * Raig_ManGenerateCounter(Aig_Man_t *pAig, int iFrame, int iOut, int nWords, int iPat, Vec_Int_t *vCis2Ids)
Definition saigSimSeq.c:416
int Raig_ManSimulate(Aig_Man_t *pAig, int nWords, int nIters, int TimeLimit, int fMiter, int fVerbose)
Definition saigSimSeq.c:454
void Raig_ManDelete(Raig_Man_t *p)
Definition saigSimSeq.c:203
Raig_Man_t * Raig_ManCreate(Aig_Man_t *pAig)
Definition saigSimSeq.c:146
int Raig_ManFindPo(Aig_Man_t *pAig, int iNode)
FUNCTION DEFINITIONS ///.
Definition saigSimSeq.c:83
unsigned * Raig_ManSimDeref(Raig_Man_t *p, int i)
Definition saigSimSeq.c:275
int Raig_ManSimulateRound(Raig_Man_t *p, int fMiter, int fFirst, int *piPat)
Definition saigSimSeq.c:304
#define Saig_ManForEachLi(p, pObj, i)
Definition saig.h:98
#define Saig_ManForEachPo(p, pObj, i)
Definition saig.h:93
Sec_MtrStatus_t Sec_MiterStatus(Aig_Man_t *p)
DECLARATIONS ///.
Definition saigMiter.c:46
#define Saig_ManForEachLo(p, pObj, i)
Definition saig.h:96
typedefABC_NAMESPACE_HEADER_START struct Sec_MtrStatus_t_ Sec_MtrStatus_t
INCLUDES ///.
Definition saig.h:41
int iData
Definition aig.h:88
Vec_Int_t * vCis2Ids
Definition saigSimSeq.c:47
Vec_Int_t * vLis
Definition saigSimSeq.c:49
Aig_Man_t * pAig
Definition saigSimSeq.c:36
int * pRefs
Definition saigSimSeq.c:51
int * pFans1
Definition saigSimSeq.c:46
int * pFans0
Definition saigSimSeq.c:45
int nWordsAlloc
Definition saigSimSeq.c:55
unsigned * pMems
Definition saigSimSeq.c:54
Vec_Int_t * vLos
Definition saigSimSeq.c:48
unsigned * pSims
Definition saigSimSeq.c:52
ABC_NAMESPACE_IMPL_START Abc_Cex_t * Abc_CexAlloc(int nRegs, int nRealPis, int nFrames)
DECLARATIONS ///.
Definition utilCex.c:51
typedefABC_NAMESPACE_HEADER_START struct Abc_Cex_t_ Abc_Cex_t
INCLUDES ///.
Definition utilCex.h:39
#define assert(ex)
Definition util_old.h:213
char * memset()
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54