sswSim.c 40.2 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 22
/**CFile****************************************************************

  FileName    [sswSim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Inductive prover with constraints.]

  Synopsis    [Sequential simulator used by the inductive prover.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - September 1, 2008.]

  Revision    [$Id: sswSim.c,v 1.00 2008/09/01 00:00:00 alanmi Exp $]

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

#include "sswInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

// simulation manager
struct Ssw_Sml_t_
{
    Aig_Man_t *      pAig;              // the original AIG manager
    int              nPref;             // the number of timeframes in the prefix
    int              nFrames;           // the number of timeframes 
    int              nWordsFrame;       // the number of words in each timeframe
    int              nWordsTotal;       // the total number of words at a node
    int              nWordsPref;        // the number of word in the prefix
    int              fNonConstOut;      // have seen a non-const-0 output during simulation
    int              nSimRounds;        // statistics
41
    abctime          timeSim;           // statistics
Alan Mishchenko committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    unsigned         pData[0];          // simulation data for the nodes
};

static inline unsigned * Ssw_ObjSim( Ssw_Sml_t * p, int Id )  { return p->pData + p->nWordsTotal * Id; }
static inline unsigned   Ssw_ObjRandomSim()                   { return Aig_ManRandom(0);               }

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

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

  Synopsis    [Computes hash value of the node using its simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
63
unsigned Ssw_SmlObjHashWord( Ssw_Sml_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
64
{
65 66 67 68 69 70 71 72 73 74 75 76 77
    static int s_SPrimes[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,
Alan Mishchenko committed
78 79 80 81 82
        8011, 8039, 8059, 8081, 8093, 8111, 8123, 8147
    };
    unsigned * pSims;
    unsigned uHash;
    int i;
83
//    assert( p->nWordsTotal <= 128 );
Alan Mishchenko committed
84 85 86 87 88 89 90 91 92 93 94 95
    uHash = 0;
    pSims = Ssw_ObjSim(p, pObj->Id);
    for ( i = p->nWordsPref; i < p->nWordsTotal; i++ )
        uHash ^= pSims[i] * s_SPrimes[i & 0x7F];
    return uHash;
}

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

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

  Description []
96

Alan Mishchenko committed
97 98 99 100 101
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
102
int Ssw_SmlObjIsConstWord( Ssw_Sml_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
    unsigned * pSims;
    int i;
    pSims = Ssw_ObjSim(p, pObj->Id);
    for ( i = p->nWordsPref; i < p->nWordsTotal; i++ )
        if ( pSims[i] )
            return 0;
    return 1;
}

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

  Synopsis    [Returns 1 if simulation infos are equal.]

  Description []
118

Alan Mishchenko committed
119 120 121 122 123
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
124
int Ssw_SmlObjsAreEqualWord( Ssw_Sml_t * p, Aig_Obj_t * pObj0, Aig_Obj_t * pObj1 )
Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137
{
    unsigned * pSims0, * pSims1;
    int i;
    pSims0 = Ssw_ObjSim(p, pObj0->Id);
    pSims1 = Ssw_ObjSim(p, pObj1->Id);
    for ( i = p->nWordsPref; i < p->nWordsTotal; i++ )
        if ( pSims0[i] != pSims1[i] )
            return 0;
    return 1;
}

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

Alan Mishchenko committed
138 139 140
  Synopsis    [Returns 1 if the node appears to be constant 1 candidate.]

  Description []
141

Alan Mishchenko committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlObjIsConstBit( void * p, Aig_Obj_t * pObj )
{
    return pObj->fPhase == pObj->fMarkB;
}

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

  Synopsis    [Returns 1 if the nodes appear equal.]

  Description []
157

Alan Mishchenko committed
158 159 160 161 162 163 164 165 166 167 168 169 170
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlObjsAreEqualBit( void * p, Aig_Obj_t * pObj0, Aig_Obj_t * pObj1 )
{
    return (pObj0->fPhase == pObj1->fPhase) == (pObj0->fMarkB == pObj1->fMarkB);
}


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

Alan Mishchenko committed
171 172 173
  Synopsis    [Counts the number of 1s in the XOR of simulation data.]

  Description []
174

Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNodeNotEquWeight( Ssw_Sml_t * p, int Left, int Right )
{
    unsigned * pSimL, * pSimR;
    int k, Counter = 0;
    pSimL = Ssw_ObjSim( p, Left );
    pSimR = Ssw_ObjSim( p, Right );
    for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
        Counter += Aig_WordCountOnes( pSimL[k] ^ pSimR[k] );
    return Counter;
}

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

Alan Mishchenko committed
193 194 195
  Synopsis    [Checks implication.]

  Description []
196

Alan Mishchenko committed
197 198 199 200 201 202 203 204 205
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlCheckXorImplication( Ssw_Sml_t * p, Aig_Obj_t * pObjLi, Aig_Obj_t * pObjLo, Aig_Obj_t * pCand )
{
    unsigned * pSimLi, * pSimLo, * pSimCand;
    int k;
Alan Mishchenko committed
206 207
    assert( pObjLo->fPhase == 0 );
    // pObjLi->fPhase may be 1, but the LI simulation data is not complemented!
Alan Mishchenko committed
208 209 210
    pSimCand = Ssw_ObjSim( p, Aig_Regular(pCand)->Id );
    pSimLi   = Ssw_ObjSim( p, pObjLi->Id );
    pSimLo   = Ssw_ObjSim( p, pObjLo->Id );
Alan Mishchenko committed
211
    if ( Aig_Regular(pCand)->fPhase ^ Aig_IsComplement(pCand) )
Alan Mishchenko committed
212 213
    {
        for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
Alan Mishchenko committed
214
            if ( ~pSimCand[k] & (pSimLi[k] ^ pSimLo[k]) )
Alan Mishchenko committed
215 216 217 218 219
                return 0;
    }
    else
    {
        for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
Alan Mishchenko committed
220
            if ( pSimCand[k] & (pSimLi[k] ^ pSimLo[k]) )
Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230
                return 0;
    }
    return 1;
}

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

  Synopsis    [Counts the number of 1s in the implication.]

  Description []
231

Alan Mishchenko committed
232 233 234 235 236 237 238 239 240
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlCountXorImplication( Ssw_Sml_t * p, Aig_Obj_t * pObjLi, Aig_Obj_t * pObjLo, Aig_Obj_t * pCand )
{
    unsigned * pSimLi, * pSimLo, * pSimCand;
    int k, Counter = 0;
Alan Mishchenko committed
241 242
    assert( pObjLo->fPhase == 0 );
    // pObjLi->fPhase may be 1, but the LI simulation data is not complemented!
Alan Mishchenko committed
243 244 245
    pSimCand = Ssw_ObjSim( p, Aig_Regular(pCand)->Id );
    pSimLi   = Ssw_ObjSim( p, pObjLi->Id );
    pSimLo   = Ssw_ObjSim( p, pObjLo->Id );
Alan Mishchenko committed
246
    if ( Aig_Regular(pCand)->fPhase ^ Aig_IsComplement(pCand) )
Alan Mishchenko committed
247 248
    {
        for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
Alan Mishchenko committed
249
            Counter += Aig_WordCountOnes(~pSimCand[k] & ~(pSimLi[k] ^ pSimLo[k]));
Alan Mishchenko committed
250 251 252 253
    }
    else
    {
        for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
Alan Mishchenko committed
254
            Counter += Aig_WordCountOnes(pSimCand[k] & ~(pSimLi[k] ^ pSimLo[k]));
Alan Mishchenko committed
255 256 257 258 259 260
    }
    return Counter;
}

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

Alan Mishchenko committed
261 262 263
  Synopsis    [Counts the number of 1s in the implication.]

  Description []
264

Alan Mishchenko committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlCountEqual( Ssw_Sml_t * p, Aig_Obj_t * pObjLi, Aig_Obj_t * pObjLo )
{
    unsigned * pSimLi, * pSimLo;
    int k, Counter = 0;
    assert( pObjLo->fPhase == 0 );
    // pObjLi->fPhase may be 1, but the LI simulation data is not complemented!
    pSimLi   = Ssw_ObjSim( p, pObjLi->Id );
    pSimLo   = Ssw_ObjSim( p, pObjLo->Id );
    for ( k = p->nWordsPref; k < p->nWordsTotal; k++ )
        Counter += Aig_WordCountOnes( ~(pSimLi[k] ^ pSimLo[k]) );
    return Counter;
}

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

Alan Mishchenko committed
285 286 287
  Synopsis    [Returns 1 if simulation info is composed of all zeros.]

  Description []
288

Alan Mishchenko committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
310

311 312 313 314 315 316 317 318 319 320 321 322 323
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNodeIsZeroFrame( Ssw_Sml_t * p, Aig_Obj_t * pObj, int f )
{
    unsigned * pSims = Ssw_ObjSim(p, pObj->Id);
    return pSims[f] == 0;
}

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

Alan Mishchenko committed
324
  Synopsis    [Counts the number of one's in the patten the object.]
Alan Mishchenko committed
325 326

  Description []
327

Alan Mishchenko committed
328 329 330 331 332
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
333
int Ssw_SmlNodeCountOnesReal( Ssw_Sml_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
334 335 336
{
    unsigned * pSims;
    int i, Counter = 0;
Alan Mishchenko committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    pSims = Ssw_ObjSim(p, Aig_Regular(pObj)->Id);
    if ( Aig_Regular(pObj)->fPhase ^ Aig_IsComplement(pObj) )
    {
        for ( i = 0; i < p->nWordsTotal; i++ )
            Counter += Aig_WordCountOnes( ~pSims[i] );
    }
    else
    {
        for ( i = 0; i < p->nWordsTotal; i++ )
            Counter += Aig_WordCountOnes( pSims[i] );
    }
    return Counter;
}

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

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

  Description []
356

Alan Mishchenko committed
357 358 359 360 361 362 363 364 365 366 367 368
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNodeCountOnesRealVec( Ssw_Sml_t * p, Vec_Ptr_t * vObjs )
{
    Aig_Obj_t * pObj;
    unsigned * pSims, uWord;
    int i, k, Counter = 0;
    if ( Vec_PtrSize(vObjs) == 0 )
        return 0;
Alan Mishchenko committed
369
    for ( i = 0; i < p->nWordsTotal; i++ )
Alan Mishchenko committed
370 371
    {
        uWord = 0;
372
        Vec_PtrForEachEntry( Aig_Obj_t *, vObjs, pObj, k )
Alan Mishchenko committed
373 374 375 376 377 378 379 380 381
        {
            pSims = Ssw_ObjSim(p, Aig_Regular(pObj)->Id);
            if ( Aig_Regular(pObj)->fPhase ^ Aig_IsComplement(pObj) )
                uWord |= ~pSims[i];
            else
                uWord |= pSims[i];
        }
        Counter += Aig_WordCountOnes( uWord );
    }
Alan Mishchenko committed
382 383 384 385 386 387 388 389 390 391
    return Counter;
}



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

  Synopsis    [Generated const 0 pattern.]

  Description []
392

Alan Mishchenko committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlSavePattern0( Ssw_Man_t * p, int fInit )
{
    memset( p->pPatWords, 0, sizeof(unsigned) * p->nPatWords );
}

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

  Synopsis    [[Generated const 1 pattern.]

  Description []
408

Alan Mishchenko committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlSavePattern1( Ssw_Man_t * p, int fInit )
{
    Aig_Obj_t * pObj;
    int i, k, nTruePis;
    memset( p->pPatWords, 0xff, sizeof(unsigned) * p->nPatWords );
    if ( !fInit )
        return;
    // clear the state bits to correspond to all-0 initial state
    nTruePis = Saig_ManPiNum(p->pAig);
    k = 0;
    Saig_ManForEachLo( p->pAig, pObj, i )
425
        Abc_InfoXorBit( p->pPatWords, nTruePis * p->nFrames + k++ );
Alan Mishchenko committed
426 427 428 429 430 431 432 433
}


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

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

  Description []
434

Alan Mishchenko committed
435 436 437 438 439 440
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Ssw_SmlCheckOutputSavePattern( Ssw_Sml_t * p, Aig_Obj_t * pObjPo )
441
{
Alan Mishchenko committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    Aig_Obj_t * pFanin, * pObjPi;
    unsigned * pSims;
    int i, k, BestPat, * pModel;
    // find the word of the pattern
    pFanin = Aig_ObjFanin0(pObjPo);
    pSims = Ssw_ObjSim(p, pFanin->Id);
    for ( i = 0; i < p->nWordsTotal; i++ )
        if ( pSims[i] )
            break;
    assert( i < p->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
460
    pModel = ABC_ALLOC( int, Aig_ManCiNum(p->pAig)+1 );
461
    Aig_ManForEachCi( p->pAig, pObjPi, i )
Alan Mishchenko committed
462
    {
463
        pModel[i] = Abc_InfoHasBit(Ssw_ObjSim(p, pObjPi->Id), BestPat);
464
//        Abc_Print( 1, "%d", pModel[i] );
Alan Mishchenko committed
465
    }
466
    pModel[Aig_ManCiNum(p->pAig)] = pObjPo->Id;
467
//    Abc_Print( 1, "\n" );
Alan Mishchenko committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    return pModel;
}

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

  Synopsis    [Returns 1 if the one of the output is already non-constant 0.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Ssw_SmlCheckOutput( Ssw_Sml_t * p )
{
    Aig_Obj_t * pObj;
    int i;
    // make sure the reference simulation pattern does not detect the bug
487
    pObj = Aig_ManCo( p->pAig, 0 );
488
    assert( Aig_ObjFanin0(pObj)->fPhase == (unsigned)Aig_ObjFaninC0(pObj) );
489
    Aig_ManForEachCo( p->pAig, pObj, i )
Alan Mishchenko committed
490
    {
Alan Mishchenko committed
491
        if ( !Ssw_SmlObjIsConstWord( p, Aig_ObjFanin0(pObj) ) )
Alan Mishchenko committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        {
            // create the counter-example from this pattern
            return Ssw_SmlCheckOutputSavePattern( p, pObj );
        }
    }
    return NULL;
}



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

  Synopsis    [Assigns random patterns to the PI node.]

  Description []
507

Alan Mishchenko committed
508 509 510 511 512 513 514 515
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlAssignRandom( Ssw_Sml_t * p, Aig_Obj_t * pObj )
{
    unsigned * pSims;
Alan Mishchenko committed
516
    int i, f;
517
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
518 519 520
    pSims = Ssw_ObjSim( p, pObj->Id );
    for ( i = 0; i < p->nWordsTotal; i++ )
        pSims[i] = Ssw_ObjRandomSim();
Alan Mishchenko committed
521 522 523 524
    // set the first bit 0 in each frame
    assert( p->nWordsFrame * p->nFrames == p->nWordsTotal );
    for ( f = 0; f < p->nFrames; f++ )
        pSims[p->nWordsFrame*f] <<= 1;
Alan Mishchenko committed
525 526 527 528 529 530 531
}

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

  Synopsis    [Assigns random patterns to the PI node.]

  Description []
532

Alan Mishchenko committed
533 534 535 536 537 538 539 540 541
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlAssignRandomFrame( Ssw_Sml_t * p, Aig_Obj_t * pObj, int iFrame )
{
    unsigned * pSims;
    int i;
Alan Mishchenko committed
542
    assert( iFrame < p->nFrames );
543
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
544 545 546 547 548 549 550 551 552 553
    pSims = Ssw_ObjSim( p, pObj->Id ) + p->nWordsFrame * iFrame;
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims[i] = Ssw_ObjRandomSim();
}

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

  Synopsis    [Assigns constant patterns to the PI node.]

  Description []
554

Alan Mishchenko committed
555 556 557 558 559
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
560
void Ssw_SmlObjAssignConst( Ssw_Sml_t * p, Aig_Obj_t * pObj, int fConst1, int iFrame )
Alan Mishchenko committed
561 562 563
{
    unsigned * pSims;
    int i;
Alan Mishchenko committed
564
    assert( iFrame < p->nFrames );
565
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
566 567 568
    pSims = Ssw_ObjSim( p, pObj->Id ) + p->nWordsFrame * iFrame;
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims[i] = fConst1? ~(unsigned)0 : 0;
569
}
Alan Mishchenko committed
570 571 572

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

Alan Mishchenko committed
573 574 575
  Synopsis    [Assigns constant patterns to the PI node.]

  Description []
576

Alan Mishchenko committed
577 578 579 580 581
  SideEffects []

  SeeAlso     []

***********************************************************************/
582 583 584 585 586
void Ssw_SmlObjAssignConstWord( Ssw_Sml_t * p, Aig_Obj_t * pObj, int fConst1, int iFrame, int iWord )
{
    unsigned * pSims;
    assert( iFrame < p->nFrames );
    assert( iWord < p->nWordsFrame );
587
    assert( Aig_ObjIsCi(pObj) );
588 589
    pSims = Ssw_ObjSim( p, pObj->Id ) + p->nWordsFrame * iFrame;
    pSims[iWord] = fConst1? ~(unsigned)0 : 0;
590
}
591 592 593 594 595 596

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

  Synopsis    [Assigns constant patterns to the PI node.]

  Description []
597

598 599 600 601 602
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
603 604 605
void Ssw_SmlObjSetWord( Ssw_Sml_t * p, Aig_Obj_t * pObj, unsigned Word, int iWord, int iFrame )
{
    unsigned * pSims;
Alan Mishchenko committed
606
    assert( iFrame < p->nFrames );
607
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
608 609
    pSims = Ssw_ObjSim( p, pObj->Id ) + p->nWordsFrame * iFrame;
    pSims[iWord] = Word;
610
}
Alan Mishchenko committed
611 612 613

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

Alan Mishchenko committed
614 615 616
  Synopsis    [Assings distance-1 simulation info for the PIs.]

  Description []
617

Alan Mishchenko committed
618 619 620 621 622 623 624 625 626 627 628 629
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlAssignDist1( Ssw_Sml_t * p, unsigned * pPat )
{
    Aig_Obj_t * pObj;
    int f, i, k, Limit, nTruePis;
    assert( p->nFrames > 0 );
    if ( p->nFrames == 1 )
    {
630
        // copy the PI info
631
        Aig_ManForEachCi( p->pAig, pObj, i )
632
            Ssw_SmlObjAssignConst( p, pObj, Abc_InfoHasBit(pPat, i), 0 );
Alan Mishchenko committed
633
        // flip one bit
634
        Limit = Abc_MinInt( Aig_ManCiNum(p->pAig), p->nWordsTotal * 32 - 1 );
Alan Mishchenko committed
635
        for ( i = 0; i < Limit; i++ )
636
            Abc_InfoXorBit( Ssw_ObjSim( p, Aig_ManCi(p->pAig,i)->Id ), i+1 );
Alan Mishchenko committed
637 638 639 640 641 642
    }
    else
    {
        int fUseDist1 = 0;

        // copy the PI info for each frame
643
        nTruePis = Aig_ManCiNum(p->pAig) - Aig_ManRegNum(p->pAig);
Alan Mishchenko committed
644 645
        for ( f = 0; f < p->nFrames; f++ )
            Saig_ManForEachPi( p->pAig, pObj, i )
646
                Ssw_SmlObjAssignConst( p, pObj, Abc_InfoHasBit(pPat, nTruePis * f + i), f );
647
        // copy the latch info
Alan Mishchenko committed
648 649
        k = 0;
        Saig_ManForEachLo( p->pAig, pObj, i )
650
            Ssw_SmlObjAssignConst( p, pObj, Abc_InfoHasBit(pPat, nTruePis * p->nFrames + k++), 0 );
651
//        assert( p->pFrames == NULL || nTruePis * p->nFrames + k == Aig_ManCiNum(p->pFrames) );
Alan Mishchenko committed
652 653 654 655

        // flip one bit of the last frame
        if ( fUseDist1 ) //&& p->nFrames == 2 )
        {
656
            Limit = Abc_MinInt( nTruePis, p->nWordsFrame * 32 - 1 );
Alan Mishchenko committed
657
            for ( i = 0; i < Limit; i++ )
658
                Abc_InfoXorBit( Ssw_ObjSim( p, Aig_ManCi(p->pAig, i)->Id ) + p->nWordsFrame*(p->nFrames-1), i+1 );
Alan Mishchenko committed
659 660 661 662 663 664 665 666 667
        }
    }
}

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

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

  Description []
668

Alan Mishchenko committed
669 670 671 672 673 674 675 676 677 678 679 680
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlAssignDist1Plus( Ssw_Sml_t * p, unsigned * pPat )
{
    Aig_Obj_t * pObj;
    int f, i, Limit;
    assert( p->nFrames > 0 );

    // copy the pattern into the primary inputs
681
    Aig_ManForEachCi( p->pAig, pObj, i )
682
        Ssw_SmlObjAssignConst( p, pObj, Abc_InfoHasBit(pPat, i), 0 );
Alan Mishchenko committed
683 684

    // set distance one PIs for the first frame
685
    Limit = Abc_MinInt( Saig_ManPiNum(p->pAig), p->nWordsFrame * 32 - 1 );
Alan Mishchenko committed
686
    for ( i = 0; i < Limit; i++ )
687
        Abc_InfoXorBit( Ssw_ObjSim( p, Aig_ManCi(p->pAig, i)->Id ), i+1 );
Alan Mishchenko committed
688 689 690 691 692 693 694 695 696 697 698 699

    // create random info for the remaining timeframes
    for ( f = 1; f < p->nFrames; f++ )
        Saig_ManForEachPi( p->pAig, pObj, i )
            Ssw_SmlAssignRandomFrame( p, pObj, f );
}

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

  Synopsis    [Simulates one node.]

  Description []
700

Alan Mishchenko committed
701 702 703 704 705 706 707 708 709
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlNodeSimulate( Ssw_Sml_t * p, Aig_Obj_t * pObj, int iFrame )
{
    unsigned * pSims, * pSims0, * pSims1;
    int fCompl, fCompl0, fCompl1, i;
Alan Mishchenko committed
710
    assert( iFrame < p->nFrames );
Alan Mishchenko committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    assert( !Aig_IsComplement(pObj) );
    assert( Aig_ObjIsNode(pObj) );
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
    // get hold of the simulation information
    pSims  = Ssw_ObjSim(p, pObj->Id) + p->nWordsFrame * iFrame;
    pSims0 = Ssw_ObjSim(p, Aig_ObjFanin0(pObj)->Id) + p->nWordsFrame * iFrame;
    pSims1 = Ssw_ObjSim(p, Aig_ObjFanin1(pObj)->Id) + p->nWordsFrame * iFrame;
    // get complemented attributes of the children using their random info
    fCompl  = pObj->fPhase;
    fCompl0 = Aig_ObjPhaseReal(Aig_ObjChild0(pObj));
    fCompl1 = Aig_ObjPhaseReal(Aig_ObjChild1(pObj));
    // simulate
    if ( fCompl0 && fCompl1 )
    {
        if ( fCompl )
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (pSims0[i] | pSims1[i]);
        else
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = ~(pSims0[i] | pSims1[i]);
    }
    else if ( fCompl0 && !fCompl1 )
    {
        if ( fCompl )
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (pSims0[i] | ~pSims1[i]);
        else
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (~pSims0[i] & pSims1[i]);
    }
    else if ( !fCompl0 && fCompl1 )
    {
        if ( fCompl )
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (~pSims0[i] | pSims1[i]);
        else
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (pSims0[i] & ~pSims1[i]);
    }
    else // if ( !fCompl0 && !fCompl1 )
    {
        if ( fCompl )
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = ~(pSims0[i] & pSims1[i]);
        else
            for ( i = 0; i < p->nWordsFrame; i++ )
                pSims[i] = (pSims0[i] & pSims1[i]);
    }
}

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

  Synopsis    [Simulates one node.]

  Description []
766

Alan Mishchenko committed
767 768 769 770 771 772 773 774 775
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNodesCompareInFrame( Ssw_Sml_t * p, Aig_Obj_t * pObj0, Aig_Obj_t * pObj1, int iFrame0, int iFrame1 )
{
    unsigned * pSims0, * pSims1;
    int i;
Alan Mishchenko committed
776 777
    assert( iFrame0 < p->nFrames );
    assert( iFrame1 < p->nFrames );
Alan Mishchenko committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    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  = Ssw_ObjSim(p, pObj0->Id) + p->nWordsFrame * iFrame0;
    pSims1  = Ssw_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*************************************************************

  Synopsis    [Simulates one node.]

  Description []
797

Alan Mishchenko committed
798 799 800 801 802 803 804 805 806
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlNodeCopyFanin( Ssw_Sml_t * p, Aig_Obj_t * pObj, int iFrame )
{
    unsigned * pSims, * pSims0;
    int fCompl, fCompl0, i;
Alan Mishchenko committed
807
    assert( iFrame < p->nFrames );
Alan Mishchenko committed
808
    assert( !Aig_IsComplement(pObj) );
809
    assert( Aig_ObjIsCo(pObj) );
Alan Mishchenko committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
    // get hold of the simulation information
    pSims  = Ssw_ObjSim(p, pObj->Id) + p->nWordsFrame * iFrame;
    pSims0 = Ssw_ObjSim(p, Aig_ObjFanin0(pObj)->Id) + p->nWordsFrame * iFrame;
    // get complemented attributes of the children using their random info
    fCompl  = pObj->fPhase;
    fCompl0 = Aig_ObjPhaseReal(Aig_ObjChild0(pObj));
    // copy information as it is
    if ( fCompl0 )
        for ( i = 0; i < p->nWordsFrame; i++ )
            pSims[i] = ~pSims0[i];
    else
        for ( i = 0; i < p->nWordsFrame; i++ )
            pSims[i] = pSims0[i];
}

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

  Synopsis    [Simulates one node.]

  Description []
831

Alan Mishchenko committed
832 833 834 835 836 837 838 839 840
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlNodeTransferNext( Ssw_Sml_t * p, Aig_Obj_t * pOut, Aig_Obj_t * pIn, int iFrame )
{
    unsigned * pSims0, * pSims1;
    int i;
Alan Mishchenko committed
841
    assert( iFrame < p->nFrames );
Alan Mishchenko committed
842 843
    assert( !Aig_IsComplement(pOut) );
    assert( !Aig_IsComplement(pIn) );
844 845
    assert( Aig_ObjIsCo(pOut) );
    assert( Aig_ObjIsCi(pIn) );
Alan Mishchenko committed
846 847 848 849 850 851 852 853 854
    assert( iFrame == 0 || p->nWordsFrame < p->nWordsTotal );
    // get hold of the simulation information
    pSims0 = Ssw_ObjSim(p, pOut->Id) + p->nWordsFrame * iFrame;
    pSims1 = Ssw_ObjSim(p, pIn->Id) + p->nWordsFrame * (iFrame+1);
    // copy information as it is
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims1[i] = pSims0[i];
}

Alan Mishchenko committed
855 856 857 858 859
/**Function*************************************************************

  Synopsis    [Simulates one node.]

  Description []
860

Alan Mishchenko committed
861 862 863 864 865 866 867 868 869 870 871
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlNodeTransferFirst( Ssw_Sml_t * p, Aig_Obj_t * pOut, Aig_Obj_t * pIn )
{
    unsigned * pSims0, * pSims1;
    int i;
    assert( !Aig_IsComplement(pOut) );
    assert( !Aig_IsComplement(pIn) );
872 873
    assert( Aig_ObjIsCo(pOut) );
    assert( Aig_ObjIsCi(pIn) );
Alan Mishchenko committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    assert( p->nWordsFrame < p->nWordsTotal );
    // get hold of the simulation information
    pSims0 = Ssw_ObjSim(p, pOut->Id) + p->nWordsFrame * (p->nFrames-1);
    pSims1 = Ssw_ObjSim(p, pIn->Id);
    // copy information as it is
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims1[i] = pSims0[i];
}


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

  Synopsis    [Assings random simulation info for the PIs.]

  Description []
889

Alan Mishchenko committed
890 891 892 893 894 895 896 897 898 899 900 901
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlInitialize( Ssw_Sml_t * p, int fInit )
{
    Aig_Obj_t * pObj;
    int i;
    if ( fInit )
    {
        assert( Aig_ManRegNum(p->pAig) > 0 );
902
        assert( Aig_ManRegNum(p->pAig) <= Aig_ManCiNum(p->pAig) );
Alan Mishchenko committed
903 904 905 906 907 908 909 910 911
        // assign random info for primary inputs
        Saig_ManForEachPi( p->pAig, pObj, i )
            Ssw_SmlAssignRandom( p, pObj );
        // assign the initial state for the latches
        Saig_ManForEachLo( p->pAig, pObj, i )
            Ssw_SmlObjAssignConst( p, pObj, 0, 0 );
    }
    else
    {
912
        Aig_ManForEachCi( p->pAig, pObj, i )
Alan Mishchenko committed
913 914 915 916 917 918 919 920 921
            Ssw_SmlAssignRandom( p, pObj );
    }
}

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

  Synopsis    [Assings random simulation info for the PIs.]

  Description []
922

Alan Mishchenko committed
923 924 925 926 927
  SideEffects []

  SeeAlso     []

***********************************************************************/
928 929 930
void Ssw_SmlInitializeSpecial( Ssw_Sml_t * p, Vec_Int_t * vInit )
{
    Aig_Obj_t * pObj;
931
    int Entry, i, nRegs;
932 933
    nRegs = Aig_ManRegNum(p->pAig);
    assert( nRegs > 0 );
934
    assert( nRegs <= Aig_ManCiNum(p->pAig) );
935
    assert( Vec_IntSize(vInit) == nRegs * p->nWordsFrame );
936 937 938 939 940 941 942 943 944 945 946 947 948
    // assign random info for primary inputs
    Saig_ManForEachPi( p->pAig, pObj, i )
        Ssw_SmlAssignRandom( p, pObj );
    // assign the initial state for the latches
    Vec_IntForEachEntry( vInit, Entry, i )
        Ssw_SmlObjAssignConstWord( p, Saig_ManLo(p->pAig, i % nRegs), Entry, 0, i / nRegs );
}

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

  Synopsis    [Assings random simulation info for the PIs.]

  Description []
949

950 951 952 953 954
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
955 956 957 958 959
void Ssw_SmlReinitialize( Ssw_Sml_t * p )
{
    Aig_Obj_t * pObj, * pObjLi, * pObjLo;
    int i;
    assert( Aig_ManRegNum(p->pAig) > 0 );
960
    assert( Aig_ManRegNum(p->pAig) < Aig_ManCiNum(p->pAig) );
Alan Mishchenko committed
961 962 963 964 965 966 967
    // assign random info for primary inputs
    Saig_ManForEachPi( p->pAig, pObj, i )
        Ssw_SmlAssignRandom( p, pObj );
    // copy simulation info into the inputs
    Saig_ManForEachLiLo( p->pAig, pObjLi, pObjLo, i )
        Ssw_SmlNodeTransferFirst( p, pObjLi, pObjLo );
}
Alan Mishchenko committed
968 969 970 971 972 973

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

  Synopsis    [Check if any of the POs becomes non-constant.]

  Description []
974

Alan Mishchenko committed
975 976 977 978 979 980 981 982 983 984
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlCheckNonConstOutputs( Ssw_Sml_t * p )
{
    Aig_Obj_t * pObj;
    int i;
    Saig_ManForEachPo( p->pAig, pObj, i )
985 986 987
    {
        if ( p->pAig->nConstrs && i >= Saig_ManPoNum(p->pAig) - p->pAig->nConstrs )
            return 0;
Alan Mishchenko committed
988 989
        if ( !Ssw_SmlNodeIsZero(p, pObj) )
            return 1;
990
    }
Alan Mishchenko committed
991 992 993 994 995 996 997 998
    return 0;
}

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

  Synopsis    [Simulates AIG manager.]

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

Alan Mishchenko committed
1000 1001 1002 1003 1004 1005 1006 1007
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlSimulateOne( Ssw_Sml_t * p )
{
    Aig_Obj_t * pObj, * pObjLi, * pObjLo;
1008
    int f, i;
1009 1010
    abctime clk;
clk = Abc_Clock();
Alan Mishchenko committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    for ( f = 0; f < p->nFrames; f++ )
    {
        // simulate the nodes
        Aig_ManForEachNode( p->pAig, pObj, i )
            Ssw_SmlNodeSimulate( p, pObj, f );
        // copy simulation info into outputs
        Saig_ManForEachPo( p->pAig, pObj, i )
            Ssw_SmlNodeCopyFanin( p, pObj, f );
        // copy simulation info into outputs
        Saig_ManForEachLi( p->pAig, pObj, i )
            Ssw_SmlNodeCopyFanin( p, pObj, f );
Alan Mishchenko committed
1022 1023 1024
        // quit if this is the last timeframe
        if ( f == p->nFrames - 1 )
            break;
Alan Mishchenko committed
1025 1026 1027 1028
        // copy simulation info into the inputs
        Saig_ManForEachLiLo( p->pAig, pObjLi, pObjLo, i )
            Ssw_SmlNodeTransferNext( p, pObjLi, pObjLo, f );
    }
1029
p->timeSim += Abc_Clock() - clk;
Alan Mishchenko committed
1030 1031 1032 1033 1034
p->nSimRounds++;
}

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

Alan Mishchenko committed
1035 1036 1037
  Synopsis    [Converts simulation information to be not normallized.]

  Description []
1038

Alan Mishchenko committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlUnnormalize( Ssw_Sml_t * p )
{
    Aig_Obj_t * pObj;
    unsigned * pSims;
    int i, k;
    // convert constant 1
    pSims  = Ssw_ObjSim( p, 0 );
    for ( i = 0; i < p->nWordsFrame; i++ )
        pSims[i] = ~pSims[i];
    // convert internal nodes
    Aig_ManForEachNode( p->pAig, pObj, k )
    {
        if ( pObj->fPhase == 0 )
            continue;
        pSims  = Ssw_ObjSim( p, pObj->Id );
        for ( i = 0; i < p->nWordsFrame; i++ )
            pSims[i] = ~pSims[i];
    }
    // PIs/POs are always stored in their natural state
}

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

  Synopsis    [Simulates AIG manager.]

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

Alan Mishchenko committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlSimulateOneDyn_rec( Ssw_Sml_t * p, Aig_Obj_t * pObj, int f, int * pVisited, int nVisCounter )
{
//    if ( Aig_ObjIsTravIdCurrent(p->pAig, pObj) )
//        return;
//    Aig_ObjSetTravIdCurrent(p->pAig, pObj);
    if ( pVisited[p->nFrames*pObj->Id+f] == nVisCounter )
        return;
    pVisited[p->nFrames*pObj->Id+f] = nVisCounter;
    if ( Saig_ObjIsPi( p->pAig, pObj ) || Aig_ObjIsConst1(pObj) )
        return;
    if ( Saig_ObjIsLo( p->pAig, pObj ) )
    {
        if ( f == 0 )
            return;
        Ssw_SmlSimulateOneDyn_rec( p, Saig_ObjLoToLi(p->pAig, pObj), f-1, pVisited, nVisCounter );
        Ssw_SmlNodeTransferNext( p, Saig_ObjLoToLi(p->pAig, pObj), pObj, f-1 );
        return;
    }
    if ( Saig_ObjIsLi( p->pAig, pObj ) )
    {
        Ssw_SmlSimulateOneDyn_rec( p, Aig_ObjFanin0(pObj), f, pVisited, nVisCounter );
        Ssw_SmlNodeCopyFanin( p, pObj, f );
        return;
    }
    assert( Aig_ObjIsNode(pObj) );
    Ssw_SmlSimulateOneDyn_rec( p, Aig_ObjFanin0(pObj), f, pVisited, nVisCounter );
    Ssw_SmlSimulateOneDyn_rec( p, Aig_ObjFanin1(pObj), f, pVisited, nVisCounter );
    Ssw_SmlNodeSimulate( p, pObj, f );
}

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

Alan Mishchenko committed
1108
  Synopsis    [Simulates AIG manager.]
Alan Mishchenko committed
1109

Alan Mishchenko committed
1110
  Description [Assumes that the PI simulation info is attached.]
1111

Alan Mishchenko committed
1112 1113 1114 1115 1116
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1117
void Ssw_SmlSimulateOneFrame( Ssw_Sml_t * p )
Alan Mishchenko committed
1118
{
Alan Mishchenko committed
1119
    Aig_Obj_t * pObj, * pObjLi, * pObjLo;
1120
    int i;
1121 1122
    abctime clk;
clk = Abc_Clock();
Alan Mishchenko committed
1123 1124 1125 1126 1127 1128 1129 1130 1131
    // simulate the nodes
    Aig_ManForEachNode( p->pAig, pObj, i )
        Ssw_SmlNodeSimulate( p, pObj, 0 );
    // copy simulation info into outputs
    Saig_ManForEachLi( p->pAig, pObj, i )
        Ssw_SmlNodeCopyFanin( p, pObj, 0 );
    // copy simulation info into the inputs
    Saig_ManForEachLiLo( p->pAig, pObjLi, pObjLo, i )
        Ssw_SmlNodeTransferNext( p, pObjLi, pObjLo, 0 );
1132
p->timeSim += Abc_Clock() - clk;
Alan Mishchenko committed
1133
p->nSimRounds++;
Alan Mishchenko committed
1134 1135
}

Alan Mishchenko committed
1136

Alan Mishchenko committed
1137 1138
/**Function*************************************************************

Alan Mishchenko committed
1139
  Synopsis    [Allocates simulation manager.]
Alan Mishchenko committed
1140 1141

  Description []
1142

Alan Mishchenko committed
1143 1144 1145 1146 1147
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1148
Ssw_Sml_t * Ssw_SmlStart( Aig_Man_t * pAig, int nPref, int nFrames, int nWordsFrame )
Alan Mishchenko committed
1149
{
Alan Mishchenko committed
1150
    Ssw_Sml_t * p;
Alan Mishchenko committed
1151
    p = (Ssw_Sml_t *)ABC_ALLOC( char, sizeof(Ssw_Sml_t) + sizeof(unsigned) * Aig_ManObjNumMax(pAig) * (nPref + nFrames) * nWordsFrame );
Alan Mishchenko committed
1152 1153 1154 1155 1156 1157 1158 1159
    memset( p, 0, sizeof(Ssw_Sml_t) + sizeof(unsigned) * (nPref + nFrames) * nWordsFrame );
    p->pAig        = pAig;
    p->nPref       = nPref;
    p->nFrames     = nPref + nFrames;
    p->nWordsFrame = nWordsFrame;
    p->nWordsTotal = (nPref + nFrames) * nWordsFrame;
    p->nWordsPref  = nPref * nWordsFrame;
    return p;
Alan Mishchenko committed
1160 1161 1162 1163 1164 1165 1166
}

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

  Synopsis    [Allocates simulation manager.]

  Description []
1167

Alan Mishchenko committed
1168 1169 1170 1171 1172
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1173
void Ssw_SmlClean( Ssw_Sml_t * p )
Alan Mishchenko committed
1174
{
Alan Mishchenko committed
1175
    memset( p->pData, 0, sizeof(unsigned) * Aig_ManObjNumMax(p->pAig) * p->nWordsTotal );
Alan Mishchenko committed
1176 1177 1178 1179
}

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

1180 1181 1182
  Synopsis    [Get simulation data.]

  Description []
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Ssw_SmlSimDataPointers( Ssw_Sml_t * p )
{
    Vec_Ptr_t * vSimInfo;
    Aig_Obj_t * pObj;
    int i;
    vSimInfo = Vec_PtrStart( Aig_ManObjNumMax(p->pAig) );
    Aig_ManForEachObj( p->pAig, pObj, i )
        Vec_PtrWriteEntry( vSimInfo, i, Ssw_ObjSim(p, i) );
    return vSimInfo;
}

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

Alan Mishchenko committed
1202 1203 1204
  Synopsis    [Deallocates simulation manager.]

  Description []
1205

Alan Mishchenko committed
1206 1207 1208 1209 1210 1211 1212
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlStop( Ssw_Sml_t * p )
{
Alan Mishchenko committed
1213
    ABC_FREE( p );
Alan Mishchenko committed
1214 1215 1216 1217 1218 1219 1220 1221
}


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

  Synopsis    [Performs simulation of the uninitialized circuit.]

  Description []
1222

Alan Mishchenko committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ssw_Sml_t * Ssw_SmlSimulateComb( Aig_Man_t * pAig, int nWords )
{
    Ssw_Sml_t * p;
    p = Ssw_SmlStart( pAig, 0, 1, nWords );
    Ssw_SmlInitialize( p, 0 );
    Ssw_SmlSimulateOne( p );
    return p;
}

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

  Synopsis    [Performs simulation of the initialized circuit.]

  Description []
1242

Alan Mishchenko committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ssw_Sml_t * Ssw_SmlSimulateSeq( Aig_Man_t * pAig, int nPref, int nFrames, int nWords )
{
    Ssw_Sml_t * p;
    p = Ssw_SmlStart( pAig, nPref, nFrames, nWords );
    Ssw_SmlInitialize( p, 1 );
    Ssw_SmlSimulateOne( p );
    p->fNonConstOut = Ssw_SmlCheckNonConstOutputs( p );
    return p;
}

Alan Mishchenko committed
1258 1259 1260 1261 1262
/**Function*************************************************************

  Synopsis    [Performs next round of sequential simulation.]

  Description []
1263

Alan Mishchenko committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlResimulateSeq( Ssw_Sml_t * p )
{
    Ssw_SmlReinitialize( p );
    Ssw_SmlSimulateOne( p );
    p->fNonConstOut = Ssw_SmlCheckNonConstOutputs( p );
}

Alan Mishchenko committed
1276

Alan Mishchenko committed
1277 1278 1279 1280 1281
/**Function*************************************************************

  Synopsis    [Returns the number of frames simulated in the manager.]

  Description []
1282

Alan Mishchenko committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNumFrames( Ssw_Sml_t * p )
{
    return p->nFrames;
}

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

  Synopsis    [Returns the total number of simulation words.]

  Description []
1298

Alan Mishchenko committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_SmlNumWordsTotal( Ssw_Sml_t * p )
{
    return p->nWordsTotal;
}

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

  Synopsis    [Returns the pointer to the simulation info of the node.]

  Description [The simulation info is normalized unless procedure
  Ssw_SmlUnnormalize() is called in advance.]
1315

Alan Mishchenko committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Ssw_SmlSimInfo( Ssw_Sml_t * p, Aig_Obj_t * pObj )
{
    assert( !Aig_IsComplement(pObj) );
    return Ssw_ObjSim( p, pObj->Id );
}

Alan Mishchenko committed
1327 1328
/**Function*************************************************************

Alan Mishchenko committed
1329 1330 1331
  Synopsis    [Creates sequential counter-example from the simulation info.]

  Description []
1332

Alan Mishchenko committed
1333 1334 1335 1336 1337
  SideEffects []

  SeeAlso     []

***********************************************************************/
1338
Abc_Cex_t * Ssw_SmlGetCounterExample( Ssw_Sml_t * p )
Alan Mishchenko committed
1339
{
1340
    Abc_Cex_t * pCex;
Alan Mishchenko committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    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;
    Saig_ManForEachPo( p->pAig, pObj, iPo )
    {
        if ( Ssw_SmlNodeIsZero(p, pObj) )
            continue;
        pSims = Ssw_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;
    }
1366
    assert( iPo < Aig_ManCoNum(p->pAig)-Aig_ManRegNum(p->pAig) );
Alan Mishchenko committed
1367 1368 1369 1370
    assert( iFrame < p->nFrames );
    assert( iBit < 32 * p->nWordsFrame );

    // allocate the counter example
1371
    pCex = Abc_CexAlloc( Aig_ManRegNum(p->pAig), Aig_ManCiNum(p->pAig) - Aig_ManRegNum(p->pAig), iFrame + 1 );
Alan Mishchenko committed
1372 1373 1374 1375 1376 1377 1378
    pCex->iPo    = iPo;
    pCex->iFrame = iFrame;

    // copy the bit data
    Saig_ManForEachLo( p->pAig, pObj, k )
    {
        pSims = Ssw_ObjSim( p, pObj->Id );
1379 1380
        if ( Abc_InfoHasBit( pSims, iBit ) )
            Abc_InfoSetBit( pCex->pData, k );
Alan Mishchenko committed
1381 1382 1383 1384 1385 1386
    }
    for ( i = 0; i <= iFrame; i++ )
    {
        Saig_ManForEachPi( p->pAig, pObj, k )
        {
            pSims = Ssw_ObjSim( p, pObj->Id );
1387 1388
            if ( Abc_InfoHasBit( pSims, 32 * p->nWordsFrame * i + iBit ) )
                Abc_InfoSetBit( pCex->pData, pCex->nRegs + pCex->nPis * i + k );
Alan Mishchenko committed
1389 1390 1391
        }
    }
    // verify the counter example
1392
    if ( !Saig_ManVerifyCex( p->pAig, pCex ) )
Alan Mishchenko committed
1393
    {
1394
        Abc_Print( 1, "Ssw_SmlGetCounterExample(): Counter-example is invalid.\n" );
1395
        Abc_CexFree( pCex );
Alan Mishchenko committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
        pCex = NULL;
    }
    return pCex;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1406
ABC_NAMESPACE_IMPL_END