fraSim.c 35 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [fraSim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [New FRAIG package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 30, 2007.]

  Revision    [$Id: fraSim.c,v 1.00 2007/06/30 00:00:00 alanmi Exp $]

***********************************************************************/

#include "fra.h"
22
#include "aig/saig/saig.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47
  Synopsis    [Computes hash value of the node using its simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_SmlNodeHash( Aig_Obj_t * pObj, int nTableSize )
{
48
    Fra_Man_t * p = (Fra_Man_t *)pObj->pData;
Alan Mishchenko committed
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
    static int s_FPrimes[128] = { 
        1009, 1049, 1093, 1151, 1201, 1249, 1297, 1361, 1427, 1459, 
        1499, 1559, 1607, 1657, 1709, 1759, 1823, 1877, 1933, 1997, 
        2039, 2089, 2141, 2213, 2269, 2311, 2371, 2411, 2467, 2543, 
        2609, 2663, 2699, 2741, 2797, 2851, 2909, 2969, 3037, 3089, 
        3169, 3221, 3299, 3331, 3389, 3461, 3517, 3557, 3613, 3671, 
        3719, 3779, 3847, 3907, 3943, 4013, 4073, 4129, 4201, 4243, 
        4289, 4363, 4441, 4493, 4549, 4621, 4663, 4729, 4793, 4871, 
        4933, 4973, 5021, 5087, 5153, 5227, 5281, 5351, 5417, 5471, 
        5519, 5573, 5651, 5693, 5749, 5821, 5861, 5923, 6011, 6073, 
        6131, 6199, 6257, 6301, 6353, 6397, 6481, 6563, 6619, 6689, 
        6737, 6803, 6863, 6917, 6977, 7027, 7109, 7187, 7237, 7309, 
        7393, 7477, 7523, 7561, 7607, 7681, 7727, 7817, 7877, 7933, 
        8011, 8039, 8059, 8081, 8093, 8111, 8123, 8147
    };
    unsigned * pSims;
    unsigned uHash;
    int i;
//    assert( p->pSml->nWordsTotal <= 128 );
    uHash = 0;
    pSims = Fra_ObjSim(p->pSml, pObj->Id);
    for ( i = p->pSml->nWordsPref; i < p->pSml->nWordsTotal; i++ )
        uHash ^= pSims[i] * s_FPrimes[i & 0x7F];
    return uHash % nTableSize;
}

/**Function*************************************************************

Alan Mishchenko committed
77
  Synopsis    [Returns 1 if simulation info is composed of all zeros.]
Alan Mishchenko committed
78 79 80 81 82 83 84 85

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
86
int Fra_SmlNodeIsConst( Aig_Obj_t * pObj )
Alan Mishchenko committed
87
{
88
    Fra_Man_t * p = (Fra_Man_t *)pObj->pData;
Alan Mishchenko committed
89 90
    unsigned * pSims;
    int i;
Alan Mishchenko committed
91
    pSims = Fra_ObjSim(p->pSml, pObj->Id);
Alan Mishchenko committed
92
    for ( i = p->pSml->nWordsPref; i < p->pSml->nWordsTotal; i++ )
Alan Mishchenko committed
93 94 95
        if ( pSims[i] )
            return 0;
    return 1;
Alan Mishchenko committed
96 97 98 99
}

/**Function*************************************************************

Alan Mishchenko committed
100 101 102 103 104 105 106 107 108
  Synopsis    [Returns 1 if simulation infos are equal.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
109
int Fra_SmlNodesAreEqual( Aig_Obj_t * pObj0, Aig_Obj_t * pObj1 )
Alan Mishchenko committed
110
{
111
    Fra_Man_t * p = (Fra_Man_t *)pObj0->pData;
Alan Mishchenko committed
112 113 114 115
    unsigned * pSims0, * pSims1;
    int i;
    pSims0 = Fra_ObjSim(p->pSml, pObj0->Id);
    pSims1 = Fra_ObjSim(p->pSml, pObj1->Id);
Alan Mishchenko committed
116
    for ( i = p->pSml->nWordsPref; i < p->pSml->nWordsTotal; i++ )
Alan Mishchenko committed
117 118 119 120 121 122 123
        if ( pSims0[i] != pSims1[i] )
            return 0;
    return 1;
}

/**Function*************************************************************

Alan Mishchenko committed
124
  Synopsis    [Counts the number of 1s in the XOR of simulation data.]
Alan Mishchenko committed
125 126 127 128 129 130 131 132

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
133
int Fra_SmlNodeNotEquWeight( Fra_Sml_t * p, int Left, int Right )
Alan Mishchenko committed
134
{
Alan Mishchenko committed
135 136 137 138 139 140 141
    unsigned * pSimL, * pSimR;
    int k, Counter = 0;
    pSimL = Fra_ObjSim( p, Left );
    pSimR = Fra_ObjSim( p, Right );
    for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
        Counter += Aig_WordCountOnes( pSimL[k] ^ pSimR[k] );
    return Counter;
Alan Mishchenko committed
142 143
}

Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/**Function*************************************************************

  Synopsis    [Returns 1 if simulation info is composed of all zeros.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_SmlNodeIsZero( Fra_Sml_t * p, Aig_Obj_t * pObj )
{
    unsigned * pSims;
    int i;
    pSims = Fra_ObjSim(p, pObj->Id);
    for ( i = p->nWordsPref; i < p->nWordsTotal; i++ )
        if ( pSims[i] )
            return 0;
    return 1;
}

Alan Mishchenko committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/**Function*************************************************************

  Synopsis    [Counts the number of one's in the patten of the output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_SmlNodeCountOnes( Fra_Sml_t * p, Aig_Obj_t * pObj )
{
    unsigned * pSims;
    int i, Counter = 0;
    pSims = Fra_ObjSim(p, pObj->Id);
    for ( i = 0; i < p->nWordsTotal; i++ )
        Counter += Aig_WordCountOnes( pSims[i] );
    return Counter;
}

Alan Mishchenko committed
187 188


Alan Mishchenko committed
189 190
/**Function*************************************************************

Alan Mishchenko committed
191 192 193 194 195 196 197 198 199
  Synopsis    [Generated const 0 pattern.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
200
void Fra_SmlSavePattern0( Fra_Man_t * p, int fInit )
Alan Mishchenko committed
201 202 203 204 205 206 207
{
    memset( p->pPatWords, 0, sizeof(unsigned) * p->nPatWords );
}

/**Function*************************************************************

  Synopsis    [[Generated const 1 pattern.]
Alan Mishchenko committed
208 209 210 211 212 213 214 215

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
216
void Fra_SmlSavePattern1( Fra_Man_t * p, int fInit )
Alan Mishchenko committed
217 218 219 220 221 222
{
    Aig_Obj_t * pObj;
    int i, k, nTruePis;
    memset( p->pPatWords, 0xff, sizeof(unsigned) * p->nPatWords );
    if ( !fInit )
        return;
Alan Mishchenko committed
223
    // clear the state bits to correspond to all-0 initial state
224
    nTruePis = Aig_ManCiNum(p->pManAig) - Aig_ManRegNum(p->pManAig);
Alan Mishchenko committed
225 226
    k = 0;
    Aig_ManForEachLoSeq( p->pManAig, pObj, i )
227
        Abc_InfoXorBit( p->pPatWords, nTruePis * p->nFramesAll + k++ );
Alan Mishchenko committed
228 229 230 231 232 233 234 235 236 237 238 239 240
}

/**Function*************************************************************

  Synopsis    [Copy pattern from the solver into the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
241
void Fra_SmlSavePattern( Fra_Man_t * p )
Alan Mishchenko committed
242
{
Alan Mishchenko committed
243
    Aig_Obj_t * pObj;
Alan Mishchenko committed
244
    int i;
Alan Mishchenko committed
245
    memset( p->pPatWords, 0, sizeof(unsigned) * p->nPatWords );
246
    Aig_ManForEachCi( p->pManFraig, pObj, i )
247 248
//        if ( p->pSat->model.ptr[Fra_ObjSatNum(pObj)] == l_True )
        if ( sat_solver_var_value(p->pSat, Fra_ObjSatNum(pObj)) )
249
            Abc_InfoSetBit( p->pPatWords, i );
Alan Mishchenko committed
250

Alan Mishchenko committed
251 252 253
    if ( p->vCex )
    {
        Vec_IntClear( p->vCex );
254
        for ( i = 0; i < Aig_ManCiNum(p->pManAig) - Aig_ManRegNum(p->pManAig); i++ )
255
            Vec_IntPush( p->vCex, Abc_InfoHasBit( p->pPatWords, i ) );
256
        for ( i = Aig_ManCiNum(p->pManFraig) - Aig_ManRegNum(p->pManFraig); i < Aig_ManCiNum(p->pManFraig); i++ )
257
            Vec_IntPush( p->vCex, Abc_InfoHasBit( p->pPatWords, i ) );
Alan Mishchenko committed
258 259 260
    }

/*
Alan Mishchenko committed
261
    printf( "Pattern: " );
262
    Aig_ManForEachCi( p->pManFraig, pObj, i )
263
        printf( "%d", Abc_InfoHasBit( p->pPatWords, i ) );
Alan Mishchenko committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    printf( "\n" );
*/
}



/**Function*************************************************************

  Synopsis    [Creates the counter-example from the successful pattern.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
281
void Fra_SmlCheckOutputSavePattern( Fra_Man_t * p, Aig_Obj_t * pObjPo )
Alan Mishchenko committed
282
{ 
Alan Mishchenko committed
283
    Aig_Obj_t * pFanin, * pObjPi;
Alan Mishchenko committed
284 285 286
    unsigned * pSims;
    int i, k, BestPat, * pModel;
    // find the word of the pattern
Alan Mishchenko committed
287 288
    pFanin = Aig_ObjFanin0(pObjPo);
    pSims = Fra_ObjSim(p->pSml, pFanin->Id);
Alan Mishchenko committed
289 290 291 292 293 294 295 296 297 298 299 300
    for ( i = 0; i < p->pSml->nWordsTotal; i++ )
        if ( pSims[i] )
            break;
    assert( i < p->pSml->nWordsTotal );
    // find the bit of the pattern
    for ( k = 0; k < 32; k++ )
        if ( pSims[i] & (1 << k) )
            break;
    assert( k < 32 );
    // determine the best pattern
    BestPat = i * 32 + k;
    // fill in the counter-example data
301
    pModel = ABC_ALLOC( int, Aig_ManCiNum(p->pManFraig)+1 );
302
    Aig_ManForEachCi( p->pManAig, pObjPi, i )
Alan Mishchenko committed
303
    {
304
        pModel[i] = Abc_InfoHasBit(Fra_ObjSim(p->pSml, pObjPi->Id), BestPat);
Alan Mishchenko committed
305
//        printf( "%d", pModel[i] );
Alan Mishchenko committed
306
    }
307
    pModel[Aig_ManCiNum(p->pManAig)] = pObjPo->Id;
Alan Mishchenko committed
308 309 310 311 312
//    printf( "\n" );
    // set the model
    assert( p->pManFraig->pData == NULL );
    p->pManFraig->pData = pModel;
    return;
Alan Mishchenko committed
313 314 315 316
}

/**Function*************************************************************

Alan Mishchenko committed
317
  Synopsis    [Returns 1 if the one of the output is already non-constant 0.]
Alan Mishchenko committed
318 319 320 321 322 323 324 325

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
326
int Fra_SmlCheckOutput( Fra_Man_t * p )
Alan Mishchenko committed
327
{
Alan Mishchenko committed
328
    Aig_Obj_t * pObj;
Alan Mishchenko committed
329 330
    int i;
    // make sure the reference simulation pattern does not detect the bug
331
    pObj = Aig_ManCo( p->pManAig, 0 );
Alan Mishchenko committed
332
    assert( Aig_ObjFanin0(pObj)->fPhase == (unsigned)Aig_ObjFaninC0(pObj) ); 
333
    Aig_ManForEachCo( p->pManAig, pObj, i )
Alan Mishchenko committed
334
    {
Alan Mishchenko committed
335
        if ( !Fra_SmlNodeIsConst( Aig_ObjFanin0(pObj) ) )
Alan Mishchenko committed
336
        {
Alan Mishchenko committed
337
            // create the counter-example from this pattern
Alan Mishchenko committed
338
            Fra_SmlCheckOutputSavePattern( p, pObj );
Alan Mishchenko committed
339
            return 1;
Alan Mishchenko committed
340
        }
Alan Mishchenko committed
341
    }
Alan Mishchenko committed
342
    return 0;
Alan Mishchenko committed
343 344
}

Alan Mishchenko committed
345 346


Alan Mishchenko committed
347 348
/**Function*************************************************************

Alan Mishchenko committed
349
  Synopsis    [Assigns random patterns to the PI node.]
Alan Mishchenko committed
350 351 352 353 354 355 356 357

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
358
void Fra_SmlAssignRandom( Fra_Sml_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
359 360 361
{
    unsigned * pSims;
    int i;
362
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
363 364 365
    pSims = Fra_ObjSim( p, pObj->Id );
    for ( i = 0; i < p->nWordsTotal; i++ )
        pSims[i] = Fra_ObjRandomSim();
Alan Mishchenko committed
366 367 368 369
}

/**Function*************************************************************

Alan Mishchenko committed
370
  Synopsis    [Assigns constant patterns to the PI node.]
Alan Mishchenko committed
371 372 373 374 375 376 377 378

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
379
void Fra_SmlAssignConst( Fra_Sml_t * p, Aig_Obj_t * pObj, int fConst1, int iFrame )
Alan Mishchenko committed
380
{
Alan Mishchenko committed
381
    unsigned * pSims;
Alan Mishchenko committed
382
    int i;
383
    assert( Aig_ObjIsCi(pObj) || Aig_ObjIsConst1(pObj) );
Alan Mishchenko committed
384 385 386
    pSims = Fra_ObjSim( p, pObj->Id ) + p->nWordsFrame * iFrame;
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims[i] = fConst1? ~(unsigned)0 : 0;
Alan Mishchenko committed
387 388
}

Alan Mishchenko committed
389 390
/**Function*************************************************************

Alan Mishchenko committed
391
  Synopsis    [Assings random simulation info for the PIs.]
Alan Mishchenko committed
392 393 394 395 396 397 398 399

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
400
void Fra_SmlInitialize( Fra_Sml_t * p, int fInit )
Alan Mishchenko committed
401
{
Alan Mishchenko committed
402
    Aig_Obj_t * pObj;
Alan Mishchenko committed
403
    int i;
Alan Mishchenko committed
404 405 406
    if ( fInit )
    {
        assert( Aig_ManRegNum(p->pAig) > 0 );
407
        assert( Aig_ManRegNum(p->pAig) < Aig_ManCiNum(p->pAig) );
Alan Mishchenko committed
408 409 410 411 412 413 414 415 416
        // assign random info for primary inputs
        Aig_ManForEachPiSeq( p->pAig, pObj, i )
            Fra_SmlAssignRandom( p, pObj );
        // assign the initial state for the latches
        Aig_ManForEachLoSeq( p->pAig, pObj, i )
            Fra_SmlAssignConst( p, pObj, 0, 0 );
    }
    else
    {
417
        Aig_ManForEachCi( p->pAig, pObj, i )
Alan Mishchenko committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
            Fra_SmlAssignRandom( p, pObj );
    }
}

/**Function*************************************************************

  Synopsis    [Assings distance-1 simulation info for the PIs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_SmlAssignDist1( Fra_Sml_t * p, unsigned * pPat )
{
    Aig_Obj_t * pObj;
    int f, i, k, Limit, nTruePis;
    assert( p->nFrames > 0 );
    if ( p->nFrames == 1 )
    {
        // copy the PI info 
441
        Aig_ManForEachCi( p->pAig, pObj, i )
442
            Fra_SmlAssignConst( p, pObj, Abc_InfoHasBit(pPat, i), 0 );
Alan Mishchenko committed
443
        // flip one bit
444
        Limit = Abc_MinInt( Aig_ManCiNum(p->pAig), p->nWordsTotal * 32 - 1 );
Alan Mishchenko committed
445
        for ( i = 0; i < Limit; i++ )
446
            Abc_InfoXorBit( Fra_ObjSim( p, Aig_ManCi(p->pAig,i)->Id ), i+1 );
Alan Mishchenko committed
447 448 449
    }
    else
    {
Alan Mishchenko committed
450 451
        int fUseDist1 = 0;

Alan Mishchenko committed
452
        // copy the PI info for each frame
453
        nTruePis = Aig_ManCiNum(p->pAig) - Aig_ManRegNum(p->pAig);
Alan Mishchenko committed
454 455
        for ( f = 0; f < p->nFrames; f++ )
            Aig_ManForEachPiSeq( p->pAig, pObj, i )
456
                Fra_SmlAssignConst( p, pObj, Abc_InfoHasBit(pPat, nTruePis * f + i), f );
Alan Mishchenko committed
457 458 459
        // copy the latch info 
        k = 0;
        Aig_ManForEachLoSeq( p->pAig, pObj, i )
460
            Fra_SmlAssignConst( p, pObj, Abc_InfoHasBit(pPat, nTruePis * p->nFrames + k++), 0 );
461
//        assert( p->pManFraig == NULL || nTruePis * p->nFrames + k == Aig_ManCiNum(p->pManFraig) );
Alan Mishchenko committed
462

Alan Mishchenko committed
463
        // flip one bit of the last frame
Alan Mishchenko committed
464
        if ( fUseDist1 ) //&& p->nFrames == 2 )
Alan Mishchenko committed
465
        {
466
            Limit = Abc_MinInt( nTruePis, p->nWordsFrame * 32 - 1 );
Alan Mishchenko committed
467
            for ( i = 0; i < Limit; i++ )
468
                Abc_InfoXorBit( Fra_ObjSim( p, Aig_ManCi(p->pAig, i)->Id ) + p->nWordsFrame*(p->nFrames-1), i+1 );
Alan Mishchenko committed
469 470
        }
    }
Alan Mishchenko committed
471
}
Alan Mishchenko committed
472

Alan Mishchenko committed
473

Alan Mishchenko committed
474 475 476 477 478 479 480 481 482 483 484
/**Function*************************************************************

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
485
void Fra_SmlNodeSimulate( Fra_Sml_t * p, Aig_Obj_t * pObj, int iFrame )
Alan Mishchenko committed
486 487 488
{
    unsigned * pSims, * pSims0, * pSims1;
    int fCompl, fCompl0, fCompl1, i;
Alan Mishchenko committed
489 490
    assert( !Aig_IsComplement(pObj) );
    assert( Aig_ObjIsNode(pObj) );
Alan Mishchenko committed
491
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
Alan Mishchenko committed
492
    // get hold of the simulation information
Alan Mishchenko committed
493 494 495
    pSims  = Fra_ObjSim(p, pObj->Id) + p->nWordsFrame * iFrame;
    pSims0 = Fra_ObjSim(p, Aig_ObjFanin0(pObj)->Id) + p->nWordsFrame * iFrame;
    pSims1 = Fra_ObjSim(p, Aig_ObjFanin1(pObj)->Id) + p->nWordsFrame * iFrame;
Alan Mishchenko committed
496 497
    // get complemented attributes of the children using their random info
    fCompl  = pObj->fPhase;
Alan Mishchenko committed
498 499
    fCompl0 = Aig_ObjPhaseReal(Aig_ObjChild0(pObj));
    fCompl1 = Aig_ObjPhaseReal(Aig_ObjChild1(pObj));
Alan Mishchenko committed
500 501 502 503
    // simulate
    if ( fCompl0 && fCompl1 )
    {
        if ( fCompl )
Alan Mishchenko committed
504
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
505 506
                pSims[i] = (pSims0[i] | pSims1[i]);
        else
Alan Mishchenko committed
507
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
508 509 510 511 512
                pSims[i] = ~(pSims0[i] | pSims1[i]);
    }
    else if ( fCompl0 && !fCompl1 )
    {
        if ( fCompl )
Alan Mishchenko committed
513
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
514 515
                pSims[i] = (pSims0[i] | ~pSims1[i]);
        else
Alan Mishchenko committed
516
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
517 518 519 520 521
                pSims[i] = (~pSims0[i] & pSims1[i]);
    }
    else if ( !fCompl0 && fCompl1 )
    {
        if ( fCompl )
Alan Mishchenko committed
522
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
523 524
                pSims[i] = (~pSims0[i] | pSims1[i]);
        else
Alan Mishchenko committed
525
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
526 527 528 529 530
                pSims[i] = (pSims0[i] & ~pSims1[i]);
    }
    else // if ( !fCompl0 && !fCompl1 )
    {
        if ( fCompl )
Alan Mishchenko committed
531
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
532 533
                pSims[i] = ~(pSims0[i] & pSims1[i]);
        else
Alan Mishchenko committed
534
            for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
535 536 537 538
                pSims[i] = (pSims0[i] & pSims1[i]);
    }
}

Alan Mishchenko committed
539
/**Function*************************************************************
Alan Mishchenko committed
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

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_SmlNodesCompareInFrame( Fra_Sml_t * p, Aig_Obj_t * pObj0, Aig_Obj_t * pObj1, int iFrame0, int iFrame1 )
{
    unsigned * pSims0, * pSims1;
    int i;
    assert( !Aig_IsComplement(pObj0) );
    assert( !Aig_IsComplement(pObj1) );
    assert( iFrame0 == 0 || p->nWordsFrame < p->nWordsTotal );
    assert( iFrame1 == 0 || p->nWordsFrame < p->nWordsTotal );
    // get hold of the simulation information
    pSims0  = Fra_ObjSim(p, pObj0->Id) + p->nWordsFrame * iFrame0;
    pSims1  = Fra_ObjSim(p, pObj1->Id) + p->nWordsFrame * iFrame1;
    // compare
    for ( i = 0; i < p->nWordsFrame; i++ )
        if ( pSims0[i] != pSims1[i] )
            return 0;
    return 1;
}

/**Function*************************************************************
Alan Mishchenko committed
569 570 571 572 573 574 575 576 577 578

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
579
void Fra_SmlNodeCopyFanin( Fra_Sml_t * p, Aig_Obj_t * pObj, int iFrame )
Alan Mishchenko committed
580 581 582 583
{
    unsigned * pSims, * pSims0;
    int fCompl, fCompl0, i;
    assert( !Aig_IsComplement(pObj) );
584
    assert( Aig_ObjIsCo(pObj) );
Alan Mishchenko committed
585
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
Alan Mishchenko committed
586
    // get hold of the simulation information
Alan Mishchenko committed
587 588
    pSims  = Fra_ObjSim(p, pObj->Id) + p->nWordsFrame * iFrame;
    pSims0 = Fra_ObjSim(p, Aig_ObjFanin0(pObj)->Id) + p->nWordsFrame * iFrame;
Alan Mishchenko committed
589 590
    // get complemented attributes of the children using their random info
    fCompl  = pObj->fPhase;
Alan Mishchenko committed
591
    fCompl0 = Aig_ObjPhaseReal(Aig_ObjChild0(pObj));
Alan Mishchenko committed
592
    // copy information as it is
593
//    if ( Aig_ObjFaninC0(pObj) )
Alan Mishchenko committed
594
    if ( fCompl0 )
Alan Mishchenko committed
595
        for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
596 597
            pSims[i] = ~pSims0[i];
    else
Alan Mishchenko committed
598
        for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612
            pSims[i] = pSims0[i];
}

/**Function*************************************************************

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
613
void Fra_SmlNodeTransferNext( Fra_Sml_t * p, Aig_Obj_t * pOut, Aig_Obj_t * pIn, int iFrame )
Alan Mishchenko committed
614 615
{
    unsigned * pSims0, * pSims1;
Alan Mishchenko committed
616
    int i;
Alan Mishchenko committed
617 618
    assert( !Aig_IsComplement(pOut) );
    assert( !Aig_IsComplement(pIn) );
619 620
    assert( Aig_ObjIsCo(pOut) );
    assert( Aig_ObjIsCi(pIn) );
Alan Mishchenko committed
621
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
Alan Mishchenko committed
622
    // get hold of the simulation information
Alan Mishchenko committed
623 624
    pSims0 = Fra_ObjSim(p, pOut->Id) + p->nWordsFrame * iFrame;
    pSims1 = Fra_ObjSim(p, pIn->Id) + p->nWordsFrame * (iFrame+1);
Alan Mishchenko committed
625
    // copy information as it is
Alan Mishchenko committed
626
    for ( i = 0; i < p->nWordsFrame; i++ )
Alan Mishchenko committed
627 628 629
        pSims1[i] = pSims0[i];
}

Alan Mishchenko committed
630

Alan Mishchenko committed
631 632
/**Function*************************************************************

Alan Mishchenko committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  Synopsis    [Check if any of the POs becomes non-constant.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_SmlCheckNonConstOutputs( Fra_Sml_t * p )
{
    Aig_Obj_t * pObj;
    int i;
    Aig_ManForEachPoSeq( p->pAig, pObj, i )
        if ( !Fra_SmlNodeIsZero(p, pObj) )
            return 1;
    return 0;
}

/**Function*************************************************************

Alan Mishchenko committed
654 655 656 657 658 659 660 661 662
  Synopsis    [Simulates AIG manager.]

  Description [Assumes that the PI simulation info is attached.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
663
void Fra_SmlSimulateOne( Fra_Sml_t * p )
Alan Mishchenko committed
664
{
Alan Mishchenko committed
665
    Aig_Obj_t * pObj, * pObjLi, * pObjLo;
666 667
    int f, i;
    clock_t clk;
Alan Mishchenko committed
668
clk = clock();
Alan Mishchenko committed
669
    for ( f = 0; f < p->nFrames; f++ )
Alan Mishchenko committed
670
    {
Alan Mishchenko committed
671
        // simulate the nodes
Alan Mishchenko committed
672 673
        Aig_ManForEachNode( p->pAig, pObj, i )
            Fra_SmlNodeSimulate( p, pObj, f );
Alan Mishchenko committed
674 675 676 677
        // copy simulation info into outputs
        Aig_ManForEachPoSeq( p->pAig, pObj, i )
            Fra_SmlNodeCopyFanin( p, pObj, f );
        // quit if this is the last timeframe
Alan Mishchenko committed
678
        if ( f == p->nFrames - 1 )
Alan Mishchenko committed
679 680
            break;
        // copy simulation info into outputs
Alan Mishchenko committed
681 682
        Aig_ManForEachLiSeq( p->pAig, pObj, i )
            Fra_SmlNodeCopyFanin( p, pObj, f );
Alan Mishchenko committed
683
        // copy simulation info into the inputs
Alan Mishchenko committed
684 685
        Aig_ManForEachLiLoSeq( p->pAig, pObjLi, pObjLo, i )
            Fra_SmlNodeTransferNext( p, pObjLi, pObjLo, f );
Alan Mishchenko committed
686 687 688 689 690
    }
p->timeSim += clock() - clk;
p->nSimRounds++;
}

Alan Mishchenko committed
691

Alan Mishchenko committed
692 693 694 695 696 697 698 699 700 701 702
/**Function*************************************************************

  Synopsis    [Resimulates fraiging manager after finding a counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
703
void Fra_SmlResimulate( Fra_Man_t * p )
Alan Mishchenko committed
704
{
705 706
    int nChanges;
    clock_t clk;
Alan Mishchenko committed
707 708 709 710
    Fra_SmlAssignDist1( p->pSml, p->pPatWords );
    Fra_SmlSimulateOne( p->pSml );
//    if ( p->pPars->fPatScores )
//        Fra_CleanPatScores( p );
Alan Mishchenko committed
711
    if ( p->pPars->fProve && Fra_SmlCheckOutput(p) )
Alan Mishchenko committed
712
        return;
Alan Mishchenko committed
713 714
clk = clock();
    nChanges = Fra_ClassesRefine( p->pCla );
Alan Mishchenko committed
715
    nChanges += Fra_ClassesRefine1( p->pCla, 1, NULL );
Alan Mishchenko committed
716 717
    if ( p->pCla->vImps )
        nChanges += Fra_ImpRefineUsingCex( p, p->pCla->vImps );
Alan Mishchenko committed
718 719
    if ( p->vOneHots )
        nChanges += Fra_OneHotRefineUsingCex( p, p->vOneHots );
Alan Mishchenko committed
720
p->timeRef += clock() - clk;
Alan Mishchenko committed
721
    if ( !p->pPars->nFramesK && nChanges < 1 )
Alan Mishchenko committed
722
        printf( "Error: A counter-example did not refine classes!\n" );
Alan Mishchenko committed
723
//    assert( nChanges >= 1 );
Alan Mishchenko committed
724
//printf( "Refined classes = %5d.   Changes = %4d.\n", Vec_PtrSize(p->vClasses), nChanges );
Alan Mishchenko committed
725 726 727 728 729 730 731 732 733 734 735 736 737
}

/**Function*************************************************************

  Synopsis    [Performs simulation of the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
738
void Fra_SmlSimulate( Fra_Man_t * p, int fInit )
Alan Mishchenko committed
739
{
Alan Mishchenko committed
740
    int fVerbose = 0;
741 742
    int nChanges, nClasses;
    clock_t clk;
Alan Mishchenko committed
743
    assert( !fInit || Aig_ManRegNum(p->pManAig) );
Alan Mishchenko committed
744
    // start the classes
Alan Mishchenko committed
745 746
    Fra_SmlInitialize( p->pSml, fInit );
    Fra_SmlSimulateOne( p->pSml );
Alan Mishchenko committed
747 748
    if ( p->pPars->fProve && Fra_SmlCheckOutput(p) )
        return;
Alan Mishchenko committed
749
    Fra_ClassesPrepare( p->pCla, p->pPars->fLatchCorr, 0 );
Alan Mishchenko committed
750
//    Fra_ClassesPrint( p->pCla, 0 );
Alan Mishchenko committed
751 752
if ( fVerbose )
printf( "Starting classes = %5d.   Lits = %6d.\n", Vec_PtrSize(p->pCla->vClasses), Fra_ClassesCountLits(p->pCla) );
Alan Mishchenko committed
753

Alan Mishchenko committed
754 755
//return;

Alan Mishchenko committed
756
    // refine classes by walking 0/1 patterns
Alan Mishchenko committed
757
    Fra_SmlSavePattern0( p, fInit );
Alan Mishchenko committed
758 759
    Fra_SmlAssignDist1( p->pSml, p->pPatWords );
    Fra_SmlSimulateOne( p->pSml );
Alan Mishchenko committed
760
    if ( p->pPars->fProve && Fra_SmlCheckOutput(p) )
Alan Mishchenko committed
761
        return;
Alan Mishchenko committed
762 763
clk = clock();
    nChanges = Fra_ClassesRefine( p->pCla );
Alan Mishchenko committed
764
    nChanges += Fra_ClassesRefine1( p->pCla, 1, NULL );
Alan Mishchenko committed
765
p->timeRef += clock() - clk;
Alan Mishchenko committed
766 767
if ( fVerbose )
printf( "Refined classes  = %5d.   Changes = %4d.   Lits = %6d.\n", Vec_PtrSize(p->pCla->vClasses), nChanges, Fra_ClassesCountLits(p->pCla) );
Alan Mishchenko committed
768
    Fra_SmlSavePattern1( p, fInit );
Alan Mishchenko committed
769 770
    Fra_SmlAssignDist1( p->pSml, p->pPatWords );
    Fra_SmlSimulateOne( p->pSml );
Alan Mishchenko committed
771
    if ( p->pPars->fProve && Fra_SmlCheckOutput(p) )
Alan Mishchenko committed
772
        return;
Alan Mishchenko committed
773 774
clk = clock();
    nChanges = Fra_ClassesRefine( p->pCla );
Alan Mishchenko committed
775
    nChanges += Fra_ClassesRefine1( p->pCla, 1, NULL );
Alan Mishchenko committed
776
p->timeRef += clock() - clk;
Alan Mishchenko committed
777

Alan Mishchenko committed
778 779
if ( fVerbose )
printf( "Refined classes  = %5d.   Changes = %4d.   Lits = %6d.\n", Vec_PtrSize(p->pCla->vClasses), nChanges, Fra_ClassesCountLits(p->pCla) );
Alan Mishchenko committed
780 781
    // refine classes by random simulation
    do {
Alan Mishchenko committed
782 783
        Fra_SmlInitialize( p->pSml, fInit );
        Fra_SmlSimulateOne( p->pSml );
Alan Mishchenko committed
784
        nClasses = Vec_PtrSize(p->pCla->vClasses);
Alan Mishchenko committed
785
        if ( p->pPars->fProve && Fra_SmlCheckOutput(p) )
Alan Mishchenko committed
786
            return;
Alan Mishchenko committed
787 788
clk = clock();
        nChanges = Fra_ClassesRefine( p->pCla );
Alan Mishchenko committed
789
        nChanges += Fra_ClassesRefine1( p->pCla, 1, NULL );
Alan Mishchenko committed
790
p->timeRef += clock() - clk;
Alan Mishchenko committed
791 792
if ( fVerbose )
printf( "Refined classes  = %5d.   Changes = %4d.   Lits = %6d.\n", Vec_PtrSize(p->pCla->vClasses), nChanges, Fra_ClassesCountLits(p->pCla) );
Alan Mishchenko committed
793
    } while ( (double)nChanges / nClasses > p->pPars->dSimSatur );
Alan Mishchenko committed
794 795 796 797

//    if ( p->pPars->fVerbose )
//    printf( "Consts = %6d. Classes = %6d. Literals = %6d.\n", 
//        Vec_PtrSize(p->pCla->vClasses1), Vec_PtrSize(p->pCla->vClasses), Fra_ClassesCountLits(p->pCla) );
Alan Mishchenko committed
798
//    Fra_ClassesPrint( p->pCla, 0 );
Alan Mishchenko committed
799 800
}
 
Alan Mishchenko committed
801

Alan Mishchenko committed
802 803
/**Function*************************************************************

Alan Mishchenko committed
804
  Synopsis    [Allocates simulation manager.]
Alan Mishchenko committed
805 806 807 808 809 810 811 812

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
813
Fra_Sml_t * Fra_SmlStart( Aig_Man_t * pAig, int nPref, int nFrames, int nWordsFrame )
Alan Mishchenko committed
814 815
{
    Fra_Sml_t * p;
Alan Mishchenko committed
816
    p = (Fra_Sml_t *)ABC_ALLOC( char, sizeof(Fra_Sml_t) + sizeof(unsigned) * Aig_ManObjNumMax(pAig) * (nPref + nFrames) * nWordsFrame );
Alan Mishchenko committed
817
    memset( p, 0, sizeof(Fra_Sml_t) + sizeof(unsigned) * (nPref + nFrames) * nWordsFrame );
Alan Mishchenko committed
818
    p->pAig        = pAig;
Alan Mishchenko committed
819 820
    p->nPref       = nPref;
    p->nFrames     = nPref + nFrames;
Alan Mishchenko committed
821
    p->nWordsFrame = nWordsFrame;
Alan Mishchenko committed
822 823
    p->nWordsTotal = (nPref + nFrames) * nWordsFrame;
    p->nWordsPref  = nPref * nWordsFrame;
824
    // constant 1 is initialized to 0 because we store values modulus phase (pObj->fPhase)
Alan Mishchenko committed
825
    return p;
Alan Mishchenko committed
826 827 828 829
}

/**Function*************************************************************

Alan Mishchenko committed
830
  Synopsis    [Deallocates simulation manager.]
Alan Mishchenko committed
831 832 833 834 835 836 837 838

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
839
void Fra_SmlStop( Fra_Sml_t * p )
Alan Mishchenko committed
840
{
Alan Mishchenko committed
841
    ABC_FREE( p );
Alan Mishchenko committed
842 843 844
}


Alan Mishchenko committed
845 846 847 848 849 850 851 852 853 854 855
/**Function*************************************************************

  Synopsis    [Performs simulation of the uninitialized circuit.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
856
Fra_Sml_t * Fra_SmlSimulateComb( Aig_Man_t * pAig, int nWords, int fCheckMiter )
Alan Mishchenko committed
857 858
{
    Fra_Sml_t * p;
Alan Mishchenko committed
859
    p = Fra_SmlStart( pAig, 0, 1, nWords );
Alan Mishchenko committed
860 861
    Fra_SmlInitialize( p, 0 );
    Fra_SmlSimulateOne( p );
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    if ( fCheckMiter )
        p->fNonConstOut = Fra_SmlCheckNonConstOutputs( p );
    return p;
}

/**Function*************************************************************

  Synopsis    [Reads simulation patterns from file.]

  Description [Each pattern contains the given number (nInputs) of binary digits.
  No other symbols (except spaces and line endings) are allowed in the file.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Fra_SmlSimulateReadFile( char * pFileName )
{
    Vec_Str_t * vRes;
    FILE * pFile;
    int c;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" with simulation patterns.\n", pFileName );
        return NULL;
    }
    vRes = Vec_StrAlloc( 1000 );
    while ( (c = fgetc(pFile)) != EOF )
    {
        if ( c == '0' || c == '1' )
            Vec_StrPush( vRes, (char)(c - '0') );
        else if ( c != ' ' && c != '\r' && c != '\n' && c != '\t' )
        {
Alan Mishchenko committed
897
            printf( "File \"%s\" contains symbol (%c) other than \'0\' or \'1\'.\n", pFileName, (char)c );
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
            Vec_StrFreeP( &vRes );
            break;
        }
    }
    fclose( pFile );
    return vRes;
}

/**Function*************************************************************

  Synopsis    [Assigns simulation patters derived from file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_SmlInitializeGiven( Fra_Sml_t * p, Vec_Str_t * vSimInfo )
{
    Aig_Obj_t * pObj;
    unsigned * pSims;
    int i, k, nPats = Vec_StrSize(vSimInfo) / Aig_ManCiNum(p->pAig);
    int nPatsPadded = p->nWordsTotal * 32;
    assert( Aig_ManRegNum(p->pAig) == 0 );
    assert( Vec_StrSize(vSimInfo) % Aig_ManCiNum(p->pAig) == 0 );
    assert( nPats <= nPatsPadded );
    Aig_ManForEachCi( p->pAig, pObj, i )
    {
        pSims = Fra_ObjSim( p, pObj->Id );
        // clean data
        for ( k = 0; k < p->nWordsTotal; k++ )
            pSims[k] = 0;
        // load patterns
        for ( k = 0; k < nPats; k++ )
            if ( Vec_StrEntry(vSimInfo, k * Aig_ManCiNum(p->pAig) + i) )
                Abc_InfoSetBit( pSims, k );
        // pad the remaining bits with the value of the last pattern
        for ( ; k < nPatsPadded; k++ )
            if ( Vec_StrEntry(vSimInfo, (nPats-1) * Aig_ManCiNum(p->pAig) + i) )
                Abc_InfoSetBit( pSims, k );
    }
}

/**Function*************************************************************

  Synopsis    [Prints output values.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_SmlPrintOutputs( Fra_Sml_t * p, int nPatterns )
{
    Aig_Obj_t * pObj;
    unsigned * pSims;
    int i, k;
    for ( k = 0; k < nPatterns; k++ )
    {
        Aig_ManForEachCo( p->pAig, pObj, i )
        {
            pSims = Fra_ObjSim( p, pObj->Id );
            printf( "%d", Abc_InfoHasBit( pSims, k ) );
        }
        printf( "\n" );               ;
    }
}

/**Function*************************************************************

  Synopsis    [Assigns simulation patters derived from file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Fra_Sml_t * Fra_SmlSimulateCombGiven( Aig_Man_t * pAig, char * pFileName, int fCheckMiter, int fVerbose )
{
    Vec_Str_t * vSimInfo;
    Fra_Sml_t * p;
    int nPatterns;
    assert( Aig_ManRegNum(pAig) == 0 );
    // read comb patterns from file
    vSimInfo = Fra_SmlSimulateReadFile( pFileName );
    if ( vSimInfo == NULL )
        return NULL;
    if ( Vec_StrSize(vSimInfo) % Aig_ManCiNum(pAig) != 0 )
    {
        printf( "File \"%s\": The number of binary digits (%d) is not divisible by the number of primary inputs (%d).\n", 
            pFileName, Vec_StrSize(vSimInfo), Aig_ManCiNum(pAig) );
        Vec_StrFree( vSimInfo );
        return NULL;
    }
    p = Fra_SmlStart( pAig, 0, 1, Abc_BitWordNum(Vec_StrSize(vSimInfo) / Aig_ManCiNum(pAig)) );
    Fra_SmlInitializeGiven( p, vSimInfo );
    nPatterns = Vec_StrSize(vSimInfo) / Aig_ManCiNum(pAig);
    Vec_StrFree( vSimInfo );
    Fra_SmlSimulateOne( p );
    if ( fCheckMiter )
        p->fNonConstOut = Fra_SmlCheckNonConstOutputs( p );
    if ( fVerbose )
        Fra_SmlPrintOutputs( p, nPatterns );
Alan Mishchenko committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    return p;
}

/**Function*************************************************************

  Synopsis    [Performs simulation of the initialized circuit.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1021
Fra_Sml_t * Fra_SmlSimulateSeq( Aig_Man_t * pAig, int nPref, int nFrames, int nWords, int fCheckMiter )
Alan Mishchenko committed
1022 1023
{
    Fra_Sml_t * p;
Alan Mishchenko committed
1024
    p = Fra_SmlStart( pAig, nPref, nFrames, nWords );
Alan Mishchenko committed
1025 1026
    Fra_SmlInitialize( p, 1 );
    Fra_SmlSimulateOne( p );
Alan Mishchenko committed
1027
    if ( fCheckMiter )
1028
        p->fNonConstOut = Fra_SmlCheckNonConstOutputs( p );
Alan Mishchenko committed
1029 1030 1031
    return p;
}

Alan Mishchenko committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
/**Function*************************************************************

  Synopsis    [Creates sequential counter-example from the simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1043
Abc_Cex_t * Fra_SmlGetCounterExample( Fra_Sml_t * p )
Alan Mishchenko committed
1044
{
1045
    Abc_Cex_t * pCex;
Alan Mishchenko committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    Aig_Obj_t * pObj;
    unsigned * pSims;
    int iPo, iFrame, iBit, i, k;

    // make sure the simulation manager has it
    assert( p->fNonConstOut );

    // find the first output that failed
    iPo = -1;
    iBit = -1;
    iFrame = -1;
    Aig_ManForEachPoSeq( p->pAig, pObj, iPo )
    {
        if ( Fra_SmlNodeIsZero(p, pObj) )
            continue;
        pSims = Fra_ObjSim( p, pObj->Id );
        for ( i = p->nWordsPref; i < p->nWordsTotal; i++ )
            if ( pSims[i] )
            {
                iFrame = i / p->nWordsFrame;
                iBit = 32 * (i % p->nWordsFrame) + Aig_WordFindFirstBit( pSims[i] );
                break;
            }
        break;
    }
1071
    assert( iPo < Aig_ManCoNum(p->pAig)-Aig_ManRegNum(p->pAig) );
Alan Mishchenko committed
1072 1073 1074 1075
    assert( iFrame < p->nFrames );
    assert( iBit < 32 * p->nWordsFrame );

    // allocate the counter example
1076
    pCex = Abc_CexAlloc( Aig_ManRegNum(p->pAig), Aig_ManCiNum(p->pAig) - Aig_ManRegNum(p->pAig), iFrame + 1 );
Alan Mishchenko committed
1077 1078 1079 1080 1081 1082 1083
    pCex->iPo    = iPo;
    pCex->iFrame = iFrame;

    // copy the bit data
    Aig_ManForEachLoSeq( p->pAig, pObj, k )
    {
        pSims = Fra_ObjSim( p, pObj->Id );
1084 1085
        if ( Abc_InfoHasBit( pSims, iBit ) )
            Abc_InfoSetBit( pCex->pData, k );
Alan Mishchenko committed
1086 1087 1088 1089 1090 1091
    }
    for ( i = 0; i <= iFrame; i++ )
    {
        Aig_ManForEachPiSeq( p->pAig, pObj, k )
        {
            pSims = Fra_ObjSim( p, pObj->Id );
1092 1093
            if ( Abc_InfoHasBit( pSims, 32 * p->nWordsFrame * i + iBit ) )
                Abc_InfoSetBit( pCex->pData, pCex->nRegs + pCex->nPis * i + k );
Alan Mishchenko committed
1094 1095 1096
        }
    }
    // verify the counter example
1097
    if ( !Saig_ManVerifyCex( p->pAig, pCex ) )
Alan Mishchenko committed
1098 1099
    {
        printf( "Fra_SmlGetCounterExample(): Counter-example is invalid.\n" );
1100
        Abc_CexFree( pCex );
Alan Mishchenko committed
1101 1102 1103 1104
        pCex = NULL;
    }
    return pCex;
}
Alan Mishchenko committed
1105
 
Alan Mishchenko committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
/**Function*************************************************************

  Synopsis    [Generates seq counter-example from the combinational one.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1117
Abc_Cex_t * Fra_SmlCopyCounterExample( Aig_Man_t * pAig, Aig_Man_t * pFrames, int * pModel )
Alan Mishchenko committed
1118
{
1119
    Abc_Cex_t * pCex;
Alan Mishchenko committed
1120 1121 1122 1123 1124
    Aig_Obj_t * pObj;
    int i, nFrames, nTruePis, nTruePos, iPo, iFrame;
    // get the number of frames
    assert( Aig_ManRegNum(pAig) > 0 );
    assert( Aig_ManRegNum(pFrames) == 0 );
1125 1126 1127 1128 1129
    nTruePis = Aig_ManCiNum(pAig)-Aig_ManRegNum(pAig);
    nTruePos = Aig_ManCoNum(pAig)-Aig_ManRegNum(pAig);
    nFrames = Aig_ManCiNum(pFrames) / nTruePis;
    assert( nTruePis * nFrames == Aig_ManCiNum(pFrames) );
    assert( nTruePos * nFrames == Aig_ManCoNum(pFrames) );
Alan Mishchenko committed
1130 1131 1132
    // find the PO that failed
    iPo = -1;
    iFrame = -1;
1133
    Aig_ManForEachCo( pFrames, pObj, i )
1134
        if ( pObj->Id == pModel[Aig_ManCiNum(pFrames)] )
Alan Mishchenko committed
1135 1136 1137 1138 1139 1140 1141
        {
            iPo = i % nTruePos;
            iFrame = i / nTruePos;
            break;
        }
    assert( iPo >= 0 );
    // allocate the counter example
1142
    pCex = Abc_CexAlloc( Aig_ManRegNum(pAig), nTruePis, iFrame + 1 );
Alan Mishchenko committed
1143 1144 1145 1146
    pCex->iPo    = iPo;
    pCex->iFrame = iFrame;

    // copy the bit data
1147
    for ( i = 0; i < Aig_ManCiNum(pFrames); i++ )
Alan Mishchenko committed
1148 1149
    {
        if ( pModel[i] )
1150
            Abc_InfoSetBit( pCex->pData, pCex->nRegs + i );
Alan Mishchenko committed
1151 1152 1153 1154 1155
        if ( pCex->nRegs + i == pCex->nBits - 1 )
            break;
    }

    // verify the counter example
1156
    if ( !Saig_ManVerifyCex( pAig, pCex ) )
Alan Mishchenko committed
1157 1158
    {
        printf( "Fra_SmlGetCounterExample(): Counter-example is invalid.\n" );
1159
        Abc_CexFree( pCex );
Alan Mishchenko committed
1160 1161 1162 1163 1164 1165
        pCex = NULL;
    }
    return pCex;

}

Alan Mishchenko committed
1166

Alan Mishchenko committed
1167 1168 1169 1170 1171
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1172 1173
ABC_NAMESPACE_IMPL_END