bmcFault.c 63.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**CFile****************************************************************

  FileName    [bmcFault.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based bounded model checking.]

  Synopsis    [Checking for functional faults.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: bmcFault.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "bmc.h"
#include "sat/cnf/cnf.h"
#include "sat/bsat/satStore.h"
#include "aig/gia/giaAig.h"
25
#include "misc/extra/extra.h"
26 27 28 29 30 31 32 33

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

34 35 36
#define FFTEST_MAX_VARS 2
#define FFTEST_MAX_PARS 8

37 38 39 40 41
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

  Synopsis    [This procedure sets default parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_DeriveFormula_rec( Gia_Man_t * pGia, char ** ppNamesIn, Vec_Str_t * vStr, int iLit )
{
    Gia_Obj_t * pObj = Gia_ManObj( pGia, Abc_Lit2Var(iLit) );
    int fCompl = Abc_LitIsCompl(iLit);
    if ( Gia_ObjIsAnd(pObj) )
    {
        Vec_StrPush( vStr, '(' );
        if ( Gia_ObjIsMux(pGia, pObj) )
        {
            Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Gia_ObjFaninLit0p(pGia, pObj) );
            Vec_StrPush( vStr, '?' );
            Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Abc_LitNotCond( Gia_ObjFaninLit1p(pGia, pObj), fCompl ) );
            Vec_StrPush( vStr, ':' );
            Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Abc_LitNotCond( Gia_ObjFaninLit2p(pGia, pObj), fCompl ) );
        }
        else
        {
            Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Abc_LitNotCond( Gia_ObjFaninLit0p(pGia, pObj), fCompl ) );
            Vec_StrPush( vStr, (char)(Gia_ObjIsXor(pObj) ? '^' : (char)(fCompl ? '|' : '&')) );
            Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Abc_LitNotCond( Gia_ObjFaninLit1p(pGia, pObj), fCompl ) );
        }
        Vec_StrPush( vStr, ')' );
    }
    else
    {
        if ( fCompl ) Vec_StrPush( vStr, '~' );
        Vec_StrPrintF( vStr, "%s", ppNamesIn[Gia_ObjCioId(pObj)] );
    }
}
char * Gia_DeriveFormula( Gia_Man_t * pGia, char ** ppNamesIn )
{
    char * pResult;
    Vec_Str_t * vStr   = Vec_StrAlloc( 1000 );
    Gia_Man_t * pMuxes = Gia_ManDupMuxes( pGia, 2 );
    Gia_Obj_t * pObj   = Gia_ManCo( pGia, 0 );
    Vec_StrPush( vStr, '(' );
    Gia_DeriveFormula_rec( pGia, ppNamesIn, vStr, Gia_ObjFaninLit0p(pGia, pObj) );
    Vec_StrPush( vStr, ')' );
    Vec_StrPush( vStr, '\0' );
    Gia_ManStop( pMuxes );
    pResult = Vec_StrReleaseArray( vStr );
    Vec_StrFree( vStr );
    return pResult;
}

/**Function*************************************************************
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  Synopsis    [This procedure sets default parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ParFfSetDefault( Bmc_ParFf_t * p )
{
    memset( p, 0, sizeof(Bmc_ParFf_t) );
    p->pFileName     =  NULL; 
    p->Algo          =     0; 
    p->fStartPats    =     0; 
    p->nTimeOut      =     0; 
115
    p->nIterCheck    =     0;
116
    p->fBasic        =     0; 
117
    p->fFfOnly       =     0;
118
    p->fCheckUntest  =     0; 
119 120 121 122 123 124 125
    p->fDump         =     0; 
    p->fDumpUntest   =     0; 
    p->fVerbose      =     0; 
}

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

126 127 128 129 130 131 132 133 134 135
  Synopsis    [Adds a general cardinality constraint in terms of vVars.]

  Description [Strict is "exactly K out of N". Non-strict is 
  "less or equal than K out of N".]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static inline void Cnf_AddSorder( sat_solver * p, int * pVars, int i, int k, int * pnVars )
{
    int iVar1 = (*pnVars)++;
    int iVar2 = (*pnVars)++;
    sat_solver_add_and( p, iVar1, pVars[i], pVars[k], 1, 1, 1 );
    sat_solver_add_and( p, iVar2, pVars[i], pVars[k], 0, 0, 0 );
    pVars[i] = iVar1;
    pVars[k] = iVar2;
}
static inline void Cnf_AddCardinConstrMerge( sat_solver * p, int * pVars, int lo, int hi, int r, int * pnVars )
{
    int i, step = r * 2;
    if ( step < hi - lo )
    {
        Cnf_AddCardinConstrMerge( p, pVars, lo, hi-r, step, pnVars );
        Cnf_AddCardinConstrMerge( p, pVars, lo+r, hi, step, pnVars );
        for ( i = lo+r; i < hi-r; i += step )
            Cnf_AddSorder( p, pVars, i, i+r, pnVars );
    }
}
static inline void Cnf_AddCardinConstrRange( sat_solver * p, int * pVars, int lo, int hi, int * pnVars )
{
    if ( hi - lo >= 1 )
    {
        int i, mid = lo + (hi - lo) / 2;
        for ( i = lo; i <= mid; i++ )
            Cnf_AddSorder( p, pVars, i, i + (hi - lo + 1) / 2, pnVars );
        Cnf_AddCardinConstrRange( p, pVars, lo, mid, pnVars );
        Cnf_AddCardinConstrRange( p, pVars, mid+1, hi, pnVars );
        Cnf_AddCardinConstrMerge( p, pVars, lo, hi, 1, pnVars );
    }
}
void Cnf_AddCardinConstrPairWise( sat_solver * p, Vec_Int_t * vVars, int K, int fStrict )
{
    int nVars = sat_solver_nvars(p);
    int nSizeOld = Vec_IntSize(vVars);
    int i, iVar, nSize, Lit, nVarsOld;
    assert( nSizeOld >= 2 );
    // check that vars are ok
    Vec_IntForEachEntry( vVars, iVar, i )
        assert( iVar >= 0 && iVar < nVars );
    // make the size a degree of two
    for ( nSize = 1; nSize < nSizeOld; nSize *= 2 );
    // extend
    sat_solver_setnvars( p, nVars + 1 + nSize * nSize / 2 );
    if ( nSize != nSizeOld )
    {
        // fill in with const0
        Vec_IntFillExtra( vVars, nSize, nVars );
        // add const0 variable
        Lit = Abc_Var2Lit( nVars++, 1 );
        if ( !sat_solver_addclause( p, &Lit, &Lit+1 ) )
            assert( 0 );
    }
    // construct recursively
    nVarsOld = nVars;
    Cnf_AddCardinConstrRange( p, Vec_IntArray(vVars), 0, nSize - 1, &nVars );
    // add final constraint
    assert( K > 0 && K < nSizeOld );
    Lit = Abc_Var2Lit( Vec_IntEntry(vVars, K), 1 );
    if ( !sat_solver_addclause( p, &Lit, &Lit+1 ) )
        assert( 0 );
    if ( fStrict )
    {
        Lit = Abc_Var2Lit( Vec_IntEntry(vVars, K-1), 0 );
        if ( !sat_solver_addclause( p, &Lit, &Lit+1 ) )
            assert( 0 );
    }
    // return to the old size
    Vec_IntShrink( vVars, 0 ); // make it unusable
    //printf( "The %d input sorting network contains %d sorters.\n", nSize, (nVars - nVarsOld)/2 );
}

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

  Synopsis    [Adds a general cardinality constraint in terms of vVars.]

  Description [Strict is "exactly K out of N". Non-strict is 
  "less or equal than K out of N".]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
221 222 223 224
static inline int Cnf_AddCardinVar( int Level, int Base, Vec_Int_t * vVars, int k )
{
    return Level ? Base + k : Vec_IntEntry( vVars, k );
}
225
void Cnf_AddCardinConstrGeneral( sat_solver * p, Vec_Int_t * vVars, int K, int fStrict )
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
{
    int i, k, iCur, iNext, iVar, Lit;
    int nVars = sat_solver_nvars(p);
    int nBits = Vec_IntSize(vVars);
    Vec_IntForEachEntry( vVars, iVar, i )
        assert( iVar >= 0 && iVar < nVars );
    sat_solver_setnvars( p, nVars + nBits * nBits );
    for ( i = 0; i < nBits; i++ )
    {
        iCur = nVars + nBits * (i-1);
        iNext = nVars + nBits * i;
        if ( i & 1 )
            sat_solver_add_buffer( p, iNext, Cnf_AddCardinVar(i, iCur, vVars, 0), 0 );
        for ( k = i & 1; k + 1 < nBits; k += 2 )
        {
            sat_solver_add_and( p, iNext+k  , Cnf_AddCardinVar(i, iCur, vVars, k), Cnf_AddCardinVar(i, iCur, vVars, k+1), 1, 1, 1 );
            sat_solver_add_and( p, iNext+k+1, Cnf_AddCardinVar(i, iCur, vVars, k), Cnf_AddCardinVar(i, iCur, vVars, k+1), 0, 0, 0 );
        }
        if ( k == nBits - 1 )
            sat_solver_add_buffer( p, iNext + nBits-1, Cnf_AddCardinVar(i, iCur, vVars, nBits-1), 0 );
    }
    // add final constraint
    assert( K > 0 && K < nBits );
    Lit = Abc_Var2Lit( nVars + nBits * (nBits - 1) + K, 1 );
    if ( !sat_solver_addclause( p, &Lit, &Lit+1 ) )
        assert( 0 );
    if ( fStrict )
    {
        Lit = Abc_Var2Lit( nVars + nBits * (nBits - 1) + K - 1, 0 );
        if ( !sat_solver_addclause( p, &Lit, &Lit+1 ) )
            assert( 0 );
    }
}
/**Function*************************************************************

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  Synopsis    [Add constraint that no more than 1 variable is 1.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cnf_AddCardinConstr( sat_solver * p, Vec_Int_t * vVars )
{
    int i, k, pLits[2], iVar, nVars = sat_solver_nvars(p);
    Vec_IntForEachEntry( vVars, iVar, i )
        assert( iVar >= 0 && iVar < nVars );
    iVar = nVars;
    sat_solver_setnvars( p, nVars + Vec_IntSize(vVars) - 1 );
    while ( Vec_IntSize(vVars) > 1 )
    {
        for ( k = i = 0; i < Vec_IntSize(vVars)/2; i++ )
        {
            pLits[0] = Abc_Var2Lit( Vec_IntEntry(vVars, 2*i), 1 );
            pLits[1] = Abc_Var2Lit( Vec_IntEntry(vVars, 2*i+1), 1 );
            sat_solver_addclause( p, pLits, pLits + 2 );
            sat_solver_add_and( p, iVar, Vec_IntEntry(vVars, 2*i), Vec_IntEntry(vVars, 2*i+1), 1, 1, 1 );
            Vec_IntWriteEntry( vVars, k++, iVar++ );
        }
        if ( Vec_IntSize(vVars) & 1 )
            Vec_IntWriteEntry( vVars, k++, Vec_IntEntryLast(vVars) );
        Vec_IntShrink( vVars, k );
    }
    return iVar;
}
void Cnf_AddCardinConstrTest()
{
295
    int i, status, Count = 1, nVars = 8;
296 297 298
    Vec_Int_t * vVars = Vec_IntStartNatural( nVars );
    sat_solver * pSat = sat_solver_new();
    sat_solver_setnvars( pSat, nVars );
299 300
    //Cnf_AddCardinConstr( pSat, vVars );
    Cnf_AddCardinConstrPairWise( pSat, vVars, 2, 1 );
301 302 303 304 305 306
    while ( 1 )
    {
        status = sat_solver_solve( pSat, NULL, NULL, 0, 0, 0, 0 );
        if ( status != l_True )
            break;
        Vec_IntClear( vVars );
307
        printf( "%3d : ", Count++ );
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        for ( i = 0; i < nVars; i++ )
        {
            Vec_IntPush( vVars, Abc_Var2Lit(i, sat_solver_var_value(pSat, i)) );
            printf( "%d", sat_solver_var_value(pSat, i) );
        }
        printf( "\n" );
        status = sat_solver_addclause( pSat, Vec_IntArray(vVars), Vec_IntArray(vVars) + Vec_IntSize(vVars) );
        if ( status == 0 )
            break;
    }
    sat_solver_delete( pSat );
    Vec_IntFree( vVars );
}

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

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Cnf_Dat_t * Cnf_DeriveGiaRemapped( Gia_Man_t * p )
{
    Cnf_Dat_t * pCnf;
    Aig_Man_t * pAig = Gia_ManToAigSimple( p );
    pAig->nRegs = 0;
    pCnf = Cnf_Derive( pAig, Aig_ManCoNum(pAig) );
    Aig_ManStop( pAig );
    return pCnf;
341
//    return (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cnf_DataLiftGia( Cnf_Dat_t * p, Gia_Man_t * pGia, int nVarsPlus )
{
    Gia_Obj_t * pObj;
    int v;
    Gia_ManForEachObj( pGia, pObj, v )
        if ( p->pVarNums[Gia_ObjId(pGia, pObj)] >= 0 )
            p->pVarNums[Gia_ObjId(pGia, pObj)] += nVarsPlus;
    for ( v = 0; v < p->nLiterals; v++ )
        p->pClauses[0][v] += 2*nVarsPlus;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
377
Gia_Man_t * Gia_ManFaultUnfold( Gia_Man_t * p, int fUseMuxes, int fFfOnly )
378 379 380
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
381
    int i, iCtrl, iThis;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    pNew = Gia_ManStart( (2 + 3 * fUseMuxes) * Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    // add first timeframe
    Gia_ManForEachRo( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachPi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachAnd( p, pObj, i )
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ObjFanin0Copy(pObj);
    // add second timeframe
    Gia_ManForEachRo( p, pObj, i )
397
        pObj->Value = Gia_ObjRoToRi(p, pObj)->Value;
398 399
    Gia_ManForEachPi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
400
    if ( fFfOnly )
401
    {
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        Gia_ManForEachAnd( p, pObj, i )
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        Gia_ManForEachPo( p, pObj, i )
            pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
        Gia_ManForEachRi( p, pObj, i )
        {
            iCtrl = Gia_ManAppendCi(pNew);
            iThis = Gia_ObjFanin0Copy(pObj);
            if ( fUseMuxes )
                pObj->Value = Gia_ManHashMux( pNew, iCtrl, pObj->Value, iThis );
            else
                pObj->Value = iThis;
            pObj->Value = Gia_ManAppendCo( pNew, pObj->Value );
        }
    }
    else
    {
        Gia_ManForEachAnd( p, pObj, i )
        {
            iCtrl = Gia_ManAppendCi(pNew);
            iThis = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
            if ( fUseMuxes )
                pObj->Value = Gia_ManHashMux( pNew, iCtrl, pObj->Value, iThis );
            else
                pObj->Value = iThis;
        }
        Gia_ManForEachCo( p, pObj, i )
            pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
430 431 432
    }
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
433
    assert( Gia_ManPiNum(pNew) == Gia_ManRegNum(p) + 2 * Gia_ManPiNum(p) + (fFfOnly ? Gia_ManRegNum(p) : Gia_ManAndNum(p)) );
434 435 436 437 438 439 440 441 442 443 444 445 446 447
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
448
Gia_Man_t * Gia_ManStuckAtUnfold( Gia_Man_t * p, Vec_Int_t * vMap )
449 450 451
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
452
    int i, iFuncVars = 0;
453
    pNew = Gia_ManStart( 3 * Gia_ManObjNum(p) );
454 455 456 457 458 459 460 461
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachAnd( p, pObj, i )
    {
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
462 463 464 465 466 467 468 469 470 471

        if ( Vec_IntEntry(vMap, iFuncVars++) )
            pObj->Value = Gia_ManHashAnd( pNew, Abc_LitNot(Gia_ManAppendCi(pNew)), pObj->Value );
        else
            Gia_ManAppendCi(pNew);

        if ( Vec_IntEntry(vMap, iFuncVars++) )
            pObj->Value = Gia_ManHashOr( pNew, Gia_ManAppendCi(pNew), pObj->Value );
        else
            Gia_ManAppendCi(pNew);
472
    }
473
    assert( iFuncVars == Vec_IntSize(vMap) );
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    assert( Gia_ManPiNum(pNew) == Gia_ManCiNum(p) + 2 * Gia_ManAndNum(p) );
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
493
Gia_Man_t * Gia_ManFlipUnfold( Gia_Man_t * p, Vec_Int_t * vMap )
494 495 496
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
497
    int i, iFuncVars = 0;
498
    pNew = Gia_ManStart( 4 * Gia_ManObjNum(p) );
499 500 501 502 503 504 505 506
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachAnd( p, pObj, i )
    {
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
507 508 509 510
        if ( Vec_IntEntry(vMap, iFuncVars++) )
            pObj->Value = Gia_ManHashXor( pNew, Gia_ManAppendCi(pNew), pObj->Value );
        else
            Gia_ManAppendCi(pNew);
511
    }
512
    assert( iFuncVars == Vec_IntSize(vMap) );
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    assert( Gia_ManPiNum(pNew) == Gia_ManCiNum(p) + Gia_ManAndNum(p) );
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
532
Gia_Man_t * Gia_ManFOFUnfold( Gia_Man_t * p, Vec_Int_t * vMap )
533 534 535
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
536
    int i, iCtrl0, iCtrl1, iCtrl2, iCtrl3, iMuxA, iMuxB, iFuncVars = 0;
537
    int VarLimit = 4 * Gia_ManAndNum(p);
538
    pNew = Gia_ManStart( 9 * Gia_ManObjNum(p) );
539 540 541 542 543 544 545
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachAnd( p, pObj, i )
    {
546
        if ( Vec_IntEntry(vMap, iFuncVars++) && iFuncVars < VarLimit )
547 548 549 550
            iCtrl0 = Gia_ManAppendCi(pNew);
        else
            iCtrl0 = 0, Gia_ManAppendCi(pNew);

551
        if ( Vec_IntEntry(vMap, iFuncVars++) && iFuncVars < VarLimit )
552 553 554 555
            iCtrl1 = Gia_ManAppendCi(pNew);
        else
            iCtrl1 = 0, Gia_ManAppendCi(pNew);

556
        if ( Vec_IntEntry(vMap, iFuncVars++) && iFuncVars < VarLimit )
557 558 559 560
            iCtrl2 = Gia_ManAppendCi(pNew);
        else
            iCtrl2 = 0, Gia_ManAppendCi(pNew);

561
        if ( Vec_IntEntry(vMap, iFuncVars++) && iFuncVars < VarLimit )
562 563 564 565
            iCtrl3 = Gia_ManAppendCi(pNew);
        else
            iCtrl3 = 0, Gia_ManAppendCi(pNew);

566 567 568 569 570 571 572 573
        if ( Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) )
            iCtrl0 = Abc_LitNot(iCtrl0);
        else if ( !Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) )
            iCtrl1 = Abc_LitNot(iCtrl1);
        else if ( Gia_ObjFaninC0(pObj) && !Gia_ObjFaninC1(pObj) )
            iCtrl2 = Abc_LitNot(iCtrl2);
        else //if ( !Gia_ObjFaninC0(pObj) && !Gia_ObjFaninC1(pObj) )
            iCtrl3 = Abc_LitNot(iCtrl3);
574

575 576 577
        iMuxA       = Gia_ManHashMux( pNew, Gia_ObjFanin0(pObj)->Value, iCtrl1, iCtrl0 );
        iMuxB       = Gia_ManHashMux( pNew, Gia_ObjFanin0(pObj)->Value, iCtrl3, iCtrl2 );
        pObj->Value = Gia_ManHashMux( pNew, Gia_ObjFanin1(pObj)->Value, iMuxB,  iMuxA );
578
    }
579
    assert( iFuncVars == Vec_IntSize(vMap) );
580 581 582 583 584 585 586 587 588 589
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    assert( Gia_ManPiNum(pNew) == Gia_ManCiNum(p) + 4 * Gia_ManAndNum(p) );
    return pNew;
}

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

590 591 592 593 594 595 596 597 598 599 600
  Synopsis    [This procedure sets default parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_FormStrCount( char * pStr, int * pnVars, int * pnPars )
{
601
    int i, Counter = 0;
602 603
    if ( pStr[0] != '(' )
    {
604
        printf( "The first symbol should be the opening parenthesis \"(\".\n" );
605 606 607 608
        return 1;
    }
    if ( pStr[strlen(pStr)-1] != ')' )
    {
609
        printf( "The last symbol should be the closing parenthesis \")\".\n" );
610 611
        return 1;
    }
612 613 614 615 616 617 618
    for ( i = 0; pStr[i]; i++ )
        if (  pStr[i] == '(' )
            Counter++;
        else if (  pStr[i] == ')' )
            Counter--;
    if ( Counter != 0 )
    {
619
        printf( "The number of opening and closing parentheses is not equal.\n" );
620 621
        return 1;
    }
622 623 624 625 626 627 628 629 630 631 632 633
    *pnVars = 0;
    *pnPars = 0;
    for ( i = 0; pStr[i]; i++ )
    {
        if ( pStr[i] >= 'a' && pStr[i] <= 'b' )
            *pnVars = Abc_MaxInt( *pnVars, pStr[i] - 'a' + 1 );
        else if ( pStr[i] >= 'p' && pStr[i] <= 's' )
            *pnPars = Abc_MaxInt( *pnPars, pStr[i] - 'p' + 1 );
        else if ( pStr[i] == '(' || pStr[i] == ')' )
        {}
        else if ( pStr[i] == '&' || pStr[i] == '|' || pStr[i] == '^' )
        {}
634
        else if ( pStr[i] == '?' || pStr[i] == ':' )
635
        {}
636 637 638 639 640 641 642 643
        else if ( pStr[i] == '~' )
        {
            if ( pStr[i+1] < 'a' || pStr[i+1] > 'z' )
            {
                printf( "Expecting alphabetic symbol (instead of \"%c\") after negation (~)\n",  pStr[i+1] );
                return 1;
            }
        }
644 645 646 647 648 649
        else 
        {
            printf( "Unknown symbol (%c) in the formula (%s)\n", pStr[i], pStr );
            return 1;
        }
    }
650
    if ( *pnVars != FFTEST_MAX_VARS )
651
        { printf( "The number of input variables (%d) should be 2\n", *pnVars ); return 1; }
652
    if ( *pnPars < 1 || *pnPars > FFTEST_MAX_PARS )
653
        { printf( "The number of parameters should be between 1 and %d\n", FFTEST_MAX_PARS ); return 1; }
654 655
    return 0;
}
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
void Gia_FormStrTransform( char * pStr, char * pForm )
{
    int i, k;
    for ( k = i = 0; pForm[i]; i++ )
    {
        if ( pForm[i] == '~' )
        {
            i++;
            assert( pForm[i] >= 'a' && pForm[i] <= 'z' );
            pStr[k++] = 'A' + pForm[i] - 'a';
        }
        else
            pStr[k++] = pForm[i];
    }
    pStr[k] = 0; 
}   

673 674
/**Function*************************************************************

675
  Synopsis    [Print formula.]
676 677 678 679 680 681 682 683 684 685 686 687 688 689

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Gia_ManFormulaEndToken( char * pForm )
{
    int Counter = 0;
    char * pThis;
    for ( pThis = pForm; *pThis; pThis++ )
    {
690
        assert( *pThis != '~' );
691 692 693 694 695 696 697 698 699 700
        if ( *pThis == '(' )
            Counter++;
        else if ( *pThis == ')' )
            Counter--;
        if ( Counter == 0 )
            return pThis + 1;
    }
    assert( 0 );
    return NULL;
}
701 702 703 704 705 706 707 708 709 710 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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
void Gia_ManPrintFormula_rec( char * pBeg, char * pEnd )
{
    int Oper = -1;
    char * pEndNew;
    if ( pBeg + 1 == pEnd )
    {
        if ( pBeg[0] >= 'a' && pBeg[0] <= 'b' )
            printf( "%c", pBeg[0] );
        else if ( pBeg[0] >= 'A' && pBeg[0] <= 'B' )
            printf( "~%c", pBeg[0]-'A'+'a' );
        else if ( pBeg[0] >= 'p' && pBeg[0] <= 'w' ) // pqrstuvw
            printf( "%c", pBeg[0] );
        else if ( pBeg[0] >= 'P' && pBeg[0] <= 'W' )
            printf( "~%c", pBeg[0]-'A'+'a' );
        return;
    }
    if ( pBeg[0] == '(' )
    {
        pEndNew = Gia_ManFormulaEndToken( pBeg );
        if ( pEndNew == pEnd )
        {
            assert( pBeg[0] == '(' );
            assert( pBeg[pEnd-pBeg-1] == ')' );
            Gia_ManPrintFormula_rec( pBeg + 1, pEnd - 1 );
            return;
        }
    }
    // get first part
    pEndNew  = Gia_ManFormulaEndToken( pBeg );
    printf( "(" );
    Gia_ManPrintFormula_rec( pBeg, pEndNew );
    printf( ")" );
    Oper     = pEndNew[0];
    // derive the formula
    if ( Oper == '&' )
        printf( "&" );
    else if ( Oper == '|' )
        printf( "|" );
    else if ( Oper == '^' )
        printf( "^" );
    else if ( Oper == '?' )
        printf( "?" );
    else assert( 0 );
    // get second part
    pBeg     = pEndNew + 1;
    pEndNew  = Gia_ManFormulaEndToken( pBeg );
    printf( "(" );
    Gia_ManPrintFormula_rec( pBeg, pEndNew );
    printf( ")" );
    if ( Oper == '?' )
    {
        printf( ":" );
        // get third part
        assert( Oper == '?' );
        assert( pEndNew[0] == ':' );
        pBeg     = pEndNew + 1;
        pEndNew  = Gia_ManFormulaEndToken( pBeg );
        printf( "(" );
        Gia_ManPrintFormula_rec( pBeg, pEndNew );
        printf( ")" );
    }
}
void Gia_ManPrintFormula( char * pStr )
{
    printf( "Using formula: " );
    printf( "(" );
    Gia_ManPrintFormula_rec( pStr, pStr + strlen(pStr) );
    printf( ")" );
    printf( "\n" );
}

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

  Synopsis    [Implements fault model formula using functional/parameter vars.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
783
int Gia_ManRealizeFormula_rec( Gia_Man_t * p, int * pVars, int * pPars, char * pBeg, char * pEnd, int nPars )
784 785 786 787 788 789 790
{
    int iFans[3], Oper = -1;
    char * pEndNew;
    if ( pBeg + 1 == pEnd )
    {
        if ( pBeg[0] >= 'a' && pBeg[0] <= 'b' )
            return pVars[pBeg[0] - 'a'];
791 792
        if ( pBeg[0] >= 'A' && pBeg[0] <= 'B' )
            return Abc_LitNot( pVars[pBeg[0] - 'A'] );
793
        if ( pBeg[0] >= 'p' && pBeg[0] <= 'w' ) // pqrstuvw
794
            return pPars[pBeg[0] - 'p'];
795 796
        if ( pBeg[0] >= 'P' && pBeg[0] <= 'W' )
            return Abc_LitNot( pPars[pBeg[0] - 'P'] );
797 798 799 800 801 802 803 804 805 806
        assert( 0 );
        return -1;
    }
    if ( pBeg[0] == '(' )
    {
        pEndNew = Gia_ManFormulaEndToken( pBeg );
        if ( pEndNew == pEnd )
        {
            assert( pBeg[0] == '(' );
            assert( pBeg[pEnd-pBeg-1] == ')' );
807
            return Gia_ManRealizeFormula_rec( p, pVars, pPars, pBeg + 1, pEnd - 1, nPars );
808 809 810 811
        }
    }
    // get first part
    pEndNew  = Gia_ManFormulaEndToken( pBeg );
812
    iFans[0] = Gia_ManRealizeFormula_rec( p, pVars, pPars, pBeg, pEndNew, nPars );
813 814 815 816
    Oper     = pEndNew[0];
    // get second part
    pBeg     = pEndNew + 1;
    pEndNew  = Gia_ManFormulaEndToken( pBeg );
817
    iFans[1] = Gia_ManRealizeFormula_rec( p, pVars, pPars, pBeg, pEndNew, nPars );
818 819 820 821 822 823 824 825 826 827 828 829
    // derive the formula
    if ( Oper == '&' )
        return Gia_ManHashAnd( p, iFans[0], iFans[1] );
    if ( Oper == '|' )
        return Gia_ManHashOr( p, iFans[0], iFans[1] );
    if ( Oper == '^' )
        return Gia_ManHashXor( p, iFans[0], iFans[1] );
    // get third part
    assert( Oper == '?' );
    assert( pEndNew[0] == ':' );
    pBeg     = pEndNew + 1;
    pEndNew  = Gia_ManFormulaEndToken( pBeg );
830
    iFans[2] = Gia_ManRealizeFormula_rec( p, pVars, pPars, pBeg, pEndNew, nPars );
831 832
    return Gia_ManHashMux( p, iFans[0], iFans[1], iFans[2] );
}
833
int Gia_ManRealizeFormula( Gia_Man_t * p, int * pVars, int * pPars, char * pStr, int nPars )
834
{
835
    return Gia_ManRealizeFormula_rec( p, pVars, pPars, pStr, pStr + strlen(pStr), nPars );
836
}
837
Gia_Man_t * Gia_ManFormulaUnfold( Gia_Man_t * p, char * pForm, int fFfOnly )
838
{
839
    char pStr[100];
840
    int Count = 0;
841 842 843 844
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
    int i, k, iCtrl[FFTEST_MAX_PARS], iFans[FFTEST_MAX_VARS];
    int nVars, nPars;
845
    assert( strlen(pForm) < 100 );
846
    Gia_FormStrCount( pForm, &nVars, &nPars );
847
    assert( nVars == 2 );
848
    Gia_FormStrTransform( pStr, pForm );
849
    Gia_ManPrintFormula( pStr );
850 851 852 853 854 855
    pNew = Gia_ManStart( 5 * Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    if ( fFfOnly )
    {
        Gia_ManCleanMark0( p );
        Gia_ManForEachRi( p, pObj, i )
            Gia_ObjFanin0(pObj)->fMark0 = 1;
        Gia_ManForEachAnd( p, pObj, i )
        {
            if ( pObj->fMark0 )
            {
                for ( k = 0; k < nPars; k++ )
                    iCtrl[k] = Gia_ManAppendCi(pNew);
                iFans[0] = Gia_ObjFanin0Copy(pObj);
                iFans[1] = Gia_ObjFanin1Copy(pObj);
                pObj->Value = Gia_ManRealizeFormula( pNew, iFans, iCtrl, pStr, nPars );
                Count++;
            }
            else
                pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        }
        Gia_ManForEachRi( p, pObj, i )
            Gia_ObjFanin0(pObj)->fMark0 = 0;
    }
    else
879
    {
880 881 882 883 884 885 886 887
        Gia_ManForEachAnd( p, pObj, i )
        {
            for ( k = 0; k < nPars; k++ )
                iCtrl[k] = Gia_ManAppendCi(pNew);
            iFans[0] = Gia_ObjFanin0Copy(pObj);
            iFans[1] = Gia_ObjFanin1Copy(pObj);
            pObj->Value = Gia_ManRealizeFormula( pNew, iFans, iCtrl, pStr, nPars );
        }
888 889 890 891 892
    }
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
893
    assert( Gia_ManPiNum(pNew) == Gia_ManCiNum(p) + nPars * (fFfOnly ? Count : Gia_ManAndNum(p)) );
894
//    if ( fUseFaults )
895
//        Gia_AigerWrite( pNew, "newfault.aig", 0, 0, 0 );
896 897 898 899 900
    return pNew;
}

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

901 902 903 904 905 906 907 908 909
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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
Gia_Man_t * Gia_ManFaultCofactor( Gia_Man_t * p, Vec_Int_t * vValues )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
    int i;
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachPi( p, pObj, i )
    {
        pObj->Value = Gia_ManAppendCi( pNew );
        if ( i < Vec_IntSize(vValues) )
            pObj->Value = Vec_IntEntry( vValues, i );
    }
    Gia_ManForEachAnd( p, pObj, i )
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    Gia_ManForEachCo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    assert( Gia_ManPiNum(pNew) == Gia_ManPiNum(p) );
    return pNew;

}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
947
void Gia_ManDumpTests( Vec_Int_t * vTests, int nIter, char * pFileName )
948
{
949
    FILE * pFile = fopen( pFileName, "wb" );
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
    int i, k, v, nVars = Vec_IntSize(vTests) / nIter;
    assert( Vec_IntSize(vTests) % nIter == 0 );
    for ( v = i = 0; i < nIter; i++, fprintf(pFile, "\n") )
        for ( k = 0; k < nVars; k++ )
            fprintf( pFile, "%d", Vec_IntEntry(vTests, v++) );
    fclose( pFile );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
void Gia_ManDumpTestsSimulate( Gia_Man_t * p, Vec_Int_t * vValues )
{
    Gia_Obj_t * pObj; int k;
    assert( Vec_IntSize(vValues) == Gia_ManCiNum(p) );
    Gia_ManConst0(p)->fMark0 = 0;
    Gia_ManForEachCi( p, pObj, k )
        pObj->fMark0 = Vec_IntEntry( vValues, k );
    Gia_ManForEachAnd( p, pObj, k )
        pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) & 
                       (Gia_ObjFanin1(pObj)->fMark0 ^ Gia_ObjFaninC1(pObj));
    Gia_ManForEachCo( p, pObj, k )
        pObj->fMark0 = Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj);
    // collect flop input values
    Vec_IntClear( vValues );
    Gia_ManForEachRi( p, pObj, k )
        Vec_IntPush( vValues, pObj->fMark0 );
    assert( Vec_IntSize(vValues) == Gia_ManRegNum(p) );
}
void Gia_ManDumpTestsDelay( Vec_Int_t * vTests, int nIter, char * pFileName, Gia_Man_t * p )
{
    FILE * pFile = fopen( pFileName, "wb" );
    Vec_Int_t * vValues = Vec_IntAlloc( Gia_ManCiNum(p) );
    int i, v, nVars = Vec_IntSize(vTests) / nIter;
    assert( Vec_IntSize(vTests) % nIter == 0 );
    assert( nVars == 2 * Gia_ManPiNum(p) + Gia_ManRegNum(p) );
    for ( i = 0; i < nIter; i++ )
    {
        // collect PIs followed by flops
        Vec_IntClear( vValues );
        for ( v = Gia_ManRegNum(p); v < Gia_ManCiNum(p); v++ )
        {
            fprintf( pFile, "%d", Vec_IntEntry(vTests, i * nVars + v) );
            Vec_IntPush( vValues, Vec_IntEntry(vTests, i * nVars + v) );
        }
        for ( v = 0; v < Gia_ManRegNum(p); v++ )
        {
            fprintf( pFile, "%d", Vec_IntEntry(vTests, i * nVars + v) );
            Vec_IntPush( vValues, Vec_IntEntry(vTests, i * nVars + v) );
        }
        fprintf( pFile, "\n" );
        // derive next-state values
        Gia_ManDumpTestsSimulate( p, vValues );
        // collect PIs followed by flops
        for ( v = Gia_ManCiNum(p); v < nVars; v++ )
            fprintf( pFile, "%d", Vec_IntEntry(vTests, i * nVars + v) );
        for ( v = 0; v < Vec_IntSize(vValues); v++ )
            fprintf( pFile, "%d", Vec_IntEntry(vValues, v) );
        fprintf( pFile, "\n" );
    }
1018
    Gia_ManCleanMark0(p);
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    fclose( pFile );
    Vec_IntFree( vValues );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1034 1035 1036 1037 1038 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
void Gia_ManPrintResults( Gia_Man_t * p, sat_solver * pSat, int nIter, abctime clk )
{
    FILE * pTable = fopen( "fault_stats.txt", "a+" );

    fprintf( pTable, "%s ", Gia_ManName(p) );
    fprintf( pTable, "%d ", Gia_ManPiNum(p) );
    fprintf( pTable, "%d ", Gia_ManPoNum(p) );
    fprintf( pTable, "%d ", Gia_ManRegNum(p) );
    fprintf( pTable, "%d ", Gia_ManAndNum(p) );

    fprintf( pTable, "%d ", sat_solver_nvars(pSat) );
    fprintf( pTable, "%d ", sat_solver_nclauses(pSat) );
    fprintf( pTable, "%d ", sat_solver_nconflicts(pSat) );

    fprintf( pTable, "%d ", nIter );
    fprintf( pTable, "%.2f", 1.0*clk/CLOCKS_PER_SEC );
    fprintf( pTable, "\n" );
    fclose( pTable );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1065
int Gia_ManFaultAddOne( Gia_Man_t * pM, Cnf_Dat_t * pCnf, sat_solver * pSat, Vec_Int_t * vLits, int nFuncVars, int fAddOr, Gia_Man_t * pGiaCnf )
1066
{
1067
    Gia_Man_t * pC;//, * pTemp;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    Cnf_Dat_t * pCnf2;
    Gia_Obj_t * pObj;
    int i, Lit;
    // derive the cofactor
    pC = Gia_ManFaultCofactor( pM, vLits );
    // derive new CNF
    pCnf2 = Cnf_DeriveGiaRemapped( pC );
    Cnf_DataLiftGia( pCnf2, pC, sat_solver_nvars(pSat) );
    // add timeframe clauses
    for ( i = 0; i < pCnf2->nClauses; i++ )
        if ( !sat_solver_addclause( pSat, pCnf2->pClauses[i], pCnf2->pClauses[i+1] ) )
1079 1080 1081 1082 1083
        {
            Cnf_DataFree( pCnf2 );
            Gia_ManStop( pC );
            return 0;
        }
1084
    // add constraint clauses
1085
    if ( fAddOr )
1086
    {
1087 1088 1089
        // add one OR gate to assert difference of at least one output of the miter
        Vec_Int_t * vOrGate = Vec_IntAlloc( Gia_ManPoNum(pC) );
        Gia_ManForEachPo( pC, pObj, i )
1090
        {
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
            Lit = Abc_Var2Lit( pCnf2->pVarNums[Gia_ObjId(pC, pObj)], 0 );
            Vec_IntPush( vOrGate, Lit );
        }
        if ( !sat_solver_addclause( pSat, Vec_IntArray(vOrGate), Vec_IntArray(vOrGate) + Vec_IntSize(vOrGate) ) )
            assert( 0 );
        Vec_IntFree( vOrGate );
    }
    else
    {
        // add negative literals to assert equality of all outputs of the miter
        Gia_ManForEachPo( pC, pObj, i )
        {
            Lit = Abc_Var2Lit( pCnf2->pVarNums[Gia_ObjId(pC, pObj)], 1 );
            if ( !sat_solver_addclause( pSat, &Lit, &Lit+1 ) )
            {
                Cnf_DataFree( pCnf2 );
                Gia_ManStop( pC );
                return 0;
            }
1110
        }
1111 1112
    }
    // add connection clauses
1113 1114 1115 1116 1117 1118
    if ( pGiaCnf )
    {
        Gia_ManForEachPi( pGiaCnf, pObj, i )
            if ( i >= nFuncVars )
                sat_solver_add_buffer( pSat, pCnf->pVarNums[Gia_ObjId(pGiaCnf, pObj)], pCnf2->pVarNums[Gia_ObjId(pC, Gia_ManPi(pC, i))], 0 );
    }
1119 1120
    Cnf_DataFree( pCnf2 );
    Gia_ManStop( pC );
1121
    return 1;
1122 1123
}

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1136
int Gia_ManDumpUntests( Gia_Man_t * pM, Cnf_Dat_t * pCnf, sat_solver * pSat, int nFuncVars, char * pFileName, int fVerbose )
1137 1138 1139 1140 1141 1142 1143 1144 1145
{
    FILE * pFile = fopen( pFileName, "wb" );
    Vec_Int_t * vLits;
    Gia_Obj_t * pObj;
    int nItersMax = 10000;
    int i, nIters, status, Value, Count = 0;
    vLits = Vec_IntAlloc( Gia_ManPiNum(pM) - nFuncVars );
    for ( nIters = 0; nIters < nItersMax; nIters++ )
    {
1146
        status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
1147 1148
        if ( status == l_Undef )
            printf( "Timeout reached after dumping %d untestable faults.\n", nIters );
1149
        if ( status == l_Undef )
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
            break;
        if ( status == l_False )
            break;
        // collect literals
        Vec_IntClear( vLits );
        Gia_ManForEachPi( pM, pObj, i )
            if ( i >= nFuncVars )
                Vec_IntPush( vLits, Abc_Var2Lit(pCnf->pVarNums[Gia_ObjId(pM, pObj)], sat_solver_var_value(pSat, pCnf->pVarNums[Gia_ObjId(pM, pObj)])) );
        // dump the fault
        Vec_IntForEachEntry( vLits, Value, i )
            if ( Abc_LitIsCompl(Value) )
                break;
        if ( i < Vec_IntSize(vLits) )
        {
            if ( fVerbose )
            {
                printf( "Untestable fault %4d : ", ++Count );
                Vec_IntForEachEntry( vLits, Value, i )
                    if ( Abc_LitIsCompl(Value) )
                        printf( "%d ", i );
                printf( "\n" );
            }
            Vec_IntForEachEntry( vLits, Value, i )
                if ( Abc_LitIsCompl(Value) )
                    fprintf( pFile, "%d ", i );
            fprintf( pFile, "\n" );
        }
        // add this clause
1178 1179
        if ( !sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) ) )
            break;
1180 1181 1182
    }
    Vec_IntFree( vLits );
    fclose( pFile );
1183
    return nIters;
1184 1185
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Gia_ManGetTestPatterns( char * pFileName )
{
    FILE * pFile = fopen( pFileName, "rb" );
    Vec_Int_t * vTests; int c;
    if ( pFile == NULL )
    {
        printf( "Cannot open input file \"%s\".\n", pFileName );
        return NULL;
    }
    vTests = Vec_IntAlloc( 10000 );
    while ( (c = fgetc(pFile)) != EOF )
    {
        if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' )
            continue;
        if ( c != '0' && c != '1' )
        {
1213
            printf( "Wrong symbol (%c) in the input file.\n", c );
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
            Vec_IntFreeP( &vTests );
            break;
        }
        Vec_IntPush( vTests, c - '0' );
    }
    fclose( pFile );
    return vTests;
}

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

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  Synopsis    [Derive the second AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDeriveDup( Gia_Man_t * p, int nPisNew )
{
    int i;
    Gia_Man_t * pNew = Gia_ManDup(p);
    for ( i = 0; i < nPisNew; i++ )
        Gia_ManAppendCi( pNew );
    return pNew;
}

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

1245 1246 1247 1248 1249 1250 1251 1252 1253
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1254
int Gia_ManFaultAnalyze( sat_solver * pSat, Vec_Int_t * vPars, Vec_Int_t * vMap, Vec_Int_t * vLits, int Iter )
1255
{
1256
    int nConfLimit = 100;
1257 1258 1259
    int status, i, v, iVar, Lit;
    int nUnsats = 0, nRuns = 0;
    abctime clk = Abc_Clock();
1260 1261 1262 1263
    assert( Vec_IntSize(vPars) == Vec_IntSize(vMap) );
    // check presence of each variable
    Vec_IntClear( vLits );
    Vec_IntAppend( vLits, vMap );
1264 1265
    for ( v = 0; v < Vec_IntSize(vPars); v++ )
    {
1266
        if ( !Vec_IntEntry(vLits, v) )
1267
            continue;
1268
        assert( Vec_IntEntry(vLits, v) == 1 );
1269 1270
        nRuns++;
        Lit = Abc_Var2Lit( Vec_IntEntry(vPars, v), 0 );
1271
        status = sat_solver_solve( pSat, &Lit, &Lit+1, (ABC_INT64_T)nConfLimit, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
1272 1273 1274 1275 1276
        if ( status == l_Undef )
            continue;
        if ( status == l_False )
        {
            nUnsats++;
1277 1278
            assert( Vec_IntEntry(vMap, v) == 1 );
            Vec_IntWriteEntry( vMap, v, 0 );
1279 1280 1281 1282 1283 1284
            Lit = Abc_LitNot(Lit);
            //status = sat_solver_addclause( pSat, &Lit, &Lit+1 );
            //assert( status );
            continue;
        }
        Vec_IntForEachEntry( vPars, iVar, i )
1285 1286 1287
            if ( Vec_IntEntry(vLits, i) && sat_solver_var_value(pSat, iVar) )
                Vec_IntWriteEntry( vLits, i, 0 );
        assert( Vec_IntEntry(vLits, v) == 0 );
1288
    }
1289
    printf( "Iteration %3d has determined %5d (out of %5d) parameters after %6d SAT calls.  ", Iter, Vec_IntSize(vMap) - Vec_IntCountPositive(vMap), Vec_IntSize(vPars), nRuns );
1290 1291 1292 1293 1294 1295
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    return nUnsats;
}

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

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
  Synopsis    [Dump faults detected by the new test, which are not detected by previous tests.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManFaultDumpNewFaults( Gia_Man_t * pM, int nFuncVars, Vec_Int_t * vTests, Vec_Int_t * vTestNew, Bmc_ParFf_t * pPars )
{
    char * pFileName = "newfaults.txt";
    abctime clk;
    Gia_Man_t * pC;
    Cnf_Dat_t * pCnf2;
    sat_solver * pSat;
    Vec_Int_t * vLits;
    int i, Iter, IterMax, nNewFaults;

    // derive the cofactor
    pC = Gia_ManFaultCofactor( pM, vTestNew );
    // derive new CNF
    pCnf2 = Cnf_DeriveGiaRemapped( pC );

    // create SAT solver
    pSat = sat_solver_new();
    sat_solver_setnvars( pSat, 1 );
    sat_solver_set_runtime_limit( pSat, pPars->nTimeOut ? pPars->nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
    // create the first cofactor
    Gia_ManFaultAddOne( pM, NULL, pSat, vTestNew, nFuncVars, 1, NULL );

    // add other test patterns
    assert( Vec_IntSize(vTests) % nFuncVars == 0 );
    IterMax = Vec_IntSize(vTests) / nFuncVars;
    vLits = Vec_IntAlloc( nFuncVars );
    for ( Iter = 0; Iter < IterMax; Iter++ )
    {
        // get pattern
        Vec_IntClear( vLits );
        for ( i = 0; i < nFuncVars; i++ )
            Vec_IntPush( vLits, Vec_IntEntry(vTests, Iter*nFuncVars + i) );
        // the resulting problem cannot be UNSAT, because the new test is guaranteed 
        // to detect something that the given test set does not detect
        if ( !Gia_ManFaultAddOne( pM, pCnf2, pSat, vLits, nFuncVars, 0, pC ) )
            assert( 0 );
    }
    Vec_IntFree( vLits );

    // dump the new faults
    clk = Abc_Clock();
    nNewFaults = Gia_ManDumpUntests( pC, pCnf2, pSat, nFuncVars, pFileName, pPars->fVerbose );
    printf( "Dumped %d new multiple faults into file \"%s\".  ", nNewFaults, pFileName );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );

    // cleanup
    sat_solver_delete( pSat );
    Cnf_DataFree( pCnf2 );
    Gia_ManStop( pC );
    return 1;
}

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

1359
  Synopsis    [Generate miter, CNF and solver.]
1360 1361 1362 1363 1364 1365 1366 1367

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1368
int Gia_ManFaultPrepare( Gia_Man_t * p, Gia_Man_t * pG, Bmc_ParFf_t * pPars, int nFuncVars, Vec_Int_t * vMap, Vec_Int_t * vTests, Vec_Int_t * vLits, Gia_Man_t ** ppMiter, Cnf_Dat_t ** ppCnf, sat_solver ** ppSat, int fWarmUp )
1369
{
Alan Mishchenko committed
1370
    Gia_Man_t * p0 = NULL, * p1 = NULL, * pM;
1371
    Gia_Obj_t * pObj;
1372 1373
    Cnf_Dat_t * pCnf;
    sat_solver * pSat;
1374 1375
    int i, Iter, status;
    abctime clkSat = 0;
1376

1377
    if ( Vec_IntSize(vTests) && (Vec_IntSize(vTests) % nFuncVars != 0) )
1378 1379 1380
    {
        printf( "The number of symbols in the input patterns (%d) does not divide evenly on the number of test variables (%d).\n", Vec_IntSize(vTests), nFuncVars );
        Vec_IntFree( vTests );
1381
        return 0;
1382
    }
1383 1384

    // select algorithm
1385
    if ( pPars->Algo == 0 )
1386
        p1 = Gia_ManFormulaUnfold( p, pPars->pFormStr, pPars->fFfOnly );
1387
    else if ( pPars->Algo == 1 )
1388 1389
    {
        assert( Gia_ManRegNum(p) > 0 );
1390 1391
        p0 = Gia_ManFaultUnfold( pG, 0, pPars->fFfOnly );
        p1 = Gia_ManFaultUnfold( p, 1, pPars->fFfOnly );
1392
    }
1393
    else if ( pPars->Algo == 2 )
1394
        p1 = Gia_ManStuckAtUnfold( p, vMap );
1395
    else if ( pPars->Algo == 3 )
1396
        p1 = Gia_ManFlipUnfold( p, vMap );
1397
    else if ( pPars->Algo == 4 )
1398
        p1 = Gia_ManFOFUnfold( p, vMap );
1399 1400
    if ( pPars->Algo != 1 )
        p0 = Gia_ManDeriveDup( pG, Gia_ManCiNum(p1) - Gia_ManCiNum(pG) );
1401
//    Gia_AigerWrite( p1, "newfault.aig", 0, 0, 0 );
1402
//    printf( "Dumped circuit with fault parameters into file \"newfault.aig\".\n" );
1403 1404 1405 1406

    // create miter
    pM = Gia_ManMiter( p0, p1, 0, 0, 0, 0, 0 );
    pCnf = Cnf_DeriveGiaRemapped( pM );
1407 1408 1409 1410 1411
    Gia_ManStop( p0 );
    Gia_ManStop( p1 );

    // start the SAT solver
    pSat = sat_solver_new();
1412
    sat_solver_setnvars( pSat, pCnf->nVars );
1413
    sat_solver_set_runtime_limit( pSat, pPars->nTimeOut ? pPars->nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
1414 1415 1416 1417 1418 1419
    // add timeframe clauses
    for ( i = 0; i < pCnf->nClauses; i++ )
        if ( !sat_solver_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1] ) )
            assert( 0 );

    // add one large OR clause
1420
    Vec_IntClear( vLits );
1421 1422 1423 1424
    Gia_ManForEachCo( pM, pObj, i )
        Vec_IntPush( vLits, Abc_Var2Lit(pCnf->pVarNums[Gia_ObjId(pM, pObj)], 0) );
    sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );

1425 1426 1427 1428 1429
    // save return data
    *ppMiter = pM;
    *ppCnf = pCnf;
    *ppSat = pSat;

1430
    // add cardinality constraint
1431
    if ( pPars->fBasic )
1432 1433 1434 1435 1436 1437 1438
    {
        Vec_IntClear( vLits );
        Gia_ManForEachPi( pM, pObj, i )
            if ( i >= nFuncVars )
                Vec_IntPush( vLits, pCnf->pVarNums[Gia_ObjId(pM, pObj)] );
        Cnf_AddCardinConstr( pSat, vLits );
    }
1439 1440 1441 1442 1443 1444 1445 1446
    else if ( pPars->nCardConstr )
    {
        Vec_IntClear( vLits );
        Gia_ManForEachPi( pM, pObj, i )
            if ( i >= nFuncVars )
                Vec_IntPush( vLits, pCnf->pVarNums[Gia_ObjId(pM, pObj)] );
        Cnf_AddCardinConstrGeneral( pSat, vLits, pPars->nCardConstr, !pPars->fNonStrict );
    }
1447

1448 1449 1450 1451 1452
    // add available test-patterns
    if ( Vec_IntSize(vTests) > 0 )
    {
        int nTests = Vec_IntSize(vTests) / nFuncVars;
        assert( Vec_IntSize(vTests) % nFuncVars == 0 );
1453 1454 1455 1456
        if ( pPars->pFileName )
            printf( "Reading %d pre-computed test patterns from file \"%s\".\n", Vec_IntSize(vTests) / nFuncVars, pPars->pFileName );
        else
            printf( "Reading %d pre-computed test patterns from previous rounds.\n", Vec_IntSize(vTests) / nFuncVars );
1457 1458
        for ( Iter = 0; Iter < nTests; Iter++ )
        {
1459
            if ( fWarmUp )
1460
            {
1461 1462 1463 1464 1465 1466 1467 1468
                abctime clk = Abc_Clock();
                status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
                clkSat += Abc_Clock() - clk;
                if ( status == l_Undef )
                {
                    if ( pPars->fVerbose )
                        printf( "\n" );
                    printf( "Timeout reached after %d seconds and adding %d tests.\n", pPars->nTimeOut, Iter );
1469
                    Vec_IntShrink( vTests, Iter * nFuncVars );
1470 1471 1472 1473 1474 1475 1476
                    return 0;
                }
                if ( status == l_False )
                {
                    if ( pPars->fVerbose )
                        printf( "\n" );
                    printf( "The problem is UNSAT after adding %d tests.\n", Iter );
1477
                    Vec_IntShrink( vTests, Iter * nFuncVars );
1478 1479
                    return 0;
                }
1480
            }
1481 1482 1483 1484
            // get pattern
            Vec_IntClear( vLits );
            for ( i = 0; i < nFuncVars; i++ )
                Vec_IntPush( vLits, Vec_IntEntry(vTests, Iter*nFuncVars + i) );
1485
            if ( !Gia_ManFaultAddOne( pM, pCnf, pSat, vLits, nFuncVars, 0, pM ) )
1486 1487 1488 1489
            {
                if ( pPars->fVerbose )
                    printf( "\n" );
                printf( "The problem is UNSAT after adding %d tests.\n", Iter );
1490
                Vec_IntShrink( vTests, Iter * nFuncVars );
1491 1492
                return 0;
            }
1493
            if ( pPars->fVerbose )
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
            {
                printf( "Iter%6d : ",       Iter );
                printf( "Var =%10d  ",      sat_solver_nvars(pSat) );
                printf( "Clause =%10d  ",   sat_solver_nclauses(pSat) );
                printf( "Conflict =%10d  ", sat_solver_nconflicts(pSat) );
                //Abc_PrintTime( 1, "Time", clkSat );
                ABC_PRTr( "Solver time", clkSat );
            }
        }
    }
1504
    else if ( pPars->fStartPats )
1505 1506 1507
    {
        for ( Iter = 0; Iter < 2; Iter++ )
        {
1508
            status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
1509 1510
            if ( status == l_Undef )
            {
1511
                if ( pPars->fVerbose )
1512
                    printf( "\n" );
1513
                printf( "Timeout reached after %d seconds and %d iterations.\n", pPars->nTimeOut, Iter );
1514
                Vec_IntShrink( vTests, Iter * nFuncVars );
1515
                return 0;
1516 1517 1518
            }
            if ( status == l_False )
            {
1519
                if ( pPars->fVerbose )
1520
                    printf( "\n" );
1521
                printf( "The problem is UNSAT after %d iterations.\n", Iter );
1522
                Vec_IntShrink( vTests, Iter * nFuncVars );
1523
                return 0;
1524 1525 1526 1527
            }
            // initialize simple pattern
            Vec_IntFill( vLits, nFuncVars, Iter );
            Vec_IntAppend( vTests, vLits );
1528
            if ( !Gia_ManFaultAddOne( pM, pCnf, pSat, vLits, nFuncVars, 0, pM ) )
1529 1530 1531 1532
            {
                if ( pPars->fVerbose )
                    printf( "\n" );
                printf( "The problem is UNSAT after adding %d tests.\n", Iter );
1533
                Vec_IntShrink( vTests, Iter * nFuncVars );
1534 1535
                return 0;
            }
1536 1537
        }
    }
1538

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    printf( "Using miter with:  AIG nodes = %6d.  CNF variables = %6d.  CNF clauses = %8d.\n", Gia_ManAndNum(pM), pCnf->nVars, pCnf->nClauses );
    return 1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFaultTest( Gia_Man_t * p, Gia_Man_t * pG, Bmc_ParFf_t * pPars )
{
    int nIterMax = 1000000, nVars, nPars;
    int i, Iter, Iter2, status, nFuncVars = -1;
    abctime clk, clkSat = 0, clkTotal = Abc_Clock();
    Vec_Int_t * vLits, * vMap = NULL, * vTests, * vPars = NULL;
Alan Mishchenko committed
1560
    Gia_Man_t * pM;
1561 1562
    Gia_Obj_t * pObj;
    Cnf_Dat_t * pCnf;
1563
    sat_solver * pSat = NULL;
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584

    if ( pPars->Algo == 0 && Gia_FormStrCount( pPars->pFormStr, &nVars, &nPars ) )
        return;

    // select algorithm
    if ( pPars->Algo == 0 )
        printf( "FFTEST is computing test patterns for fault model \"%s\"...\n", pPars->pFormStr );
    else if ( pPars->Algo == 1 )
        printf( "FFTEST is computing test patterns for %sdelay faults...\n", pPars->fBasic ? "single " : "" );
    else if ( pPars->Algo == 2 )
        printf( "FFTEST is computing test patterns for %sstuck-at faults...\n", pPars->fBasic ? "single " : "" );
    else if ( pPars->Algo == 3 )
        printf( "FFTEST is computing test patterns for %scomplement faults...\n", pPars->fBasic ? "single " : "" );
    else if ( pPars->Algo == 4 )
        printf( "FFTEST is computing test patterns for %sfunctionally observable faults...\n", pPars->fBasic ? "single " : "" );
    else
    {
        printf( "Unrecognized algorithm (%d).\n", pPars->Algo );
        return;
    }

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
    printf( "Options: " );
    printf( "Untestable faults = %s. ", pPars->fCheckUntest || pPars->fDumpDelay ? "yes": "no" );
    if ( pPars->nCardConstr )
        printf( "Using %sstrict cardinality %d. ", pPars->fNonStrict ? "non-" : "", pPars->nCardConstr );
    if ( pPars->fFfOnly )
        printf( "Faults at FF outputs only = yes. " );
    if ( pPars->nTimeOut )
        printf( "Runtime limit = %d sec.  ", pPars->nTimeOut );
    if ( p != pG && pG->pSpec )
        printf( "Golden model = %s. ", pG->pSpec );
    printf( "Verbose = %s. ", pPars->fVerbose ? "yes": "no" );
    printf( "\n" );

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
    // select algorithm
    if ( pPars->Algo == 0 )
        nFuncVars = Gia_ManCiNum(p);
    else if ( pPars->Algo == 1 )
        nFuncVars = Gia_ManRegNum(p) + 2 * Gia_ManPiNum(p);
    else if ( pPars->Algo == 2 )
        nFuncVars = Gia_ManCiNum(p);
    else if ( pPars->Algo == 3 )
        nFuncVars = Gia_ManCiNum(p);
    else if ( pPars->Algo == 4 )
        nFuncVars = Gia_ManCiNum(p);

    // collect test patterns from file
    if ( pPars->pFileName )
        vTests = Gia_ManGetTestPatterns( pPars->pFileName );
    else
        vTests = Vec_IntAlloc( 10000 );
    if ( vTests == NULL )
        return;

    // select algorithm
    vMap = Vec_IntAlloc( 0 );
    if ( pPars->Algo == 2 )
        Vec_IntFill( vMap, 2 * Gia_ManAndNum(p), 1 );
    else if ( pPars->Algo == 3 )
        Vec_IntFill( vMap,     Gia_ManAndNum(p), 1 );
    else if ( pPars->Algo == 4 )
        Vec_IntFill( vMap, 4 * Gia_ManAndNum(p), 1 );

    // prepare SAT solver
    vLits = Vec_IntAlloc( Gia_ManCoNum(p) );

1630 1631
    // add user-speicified test-vectors (if given) and create missing ones (if needed)
    if ( Gia_ManFaultPrepare(p, pG, pPars, nFuncVars, vMap, vTests, vLits, &pM, &pCnf, &pSat, 1) )
1632
    for ( Iter = pPars->fStartPats ? 2 : Vec_IntSize(vTests) / nFuncVars; Iter < nIterMax; Iter++ )
1633
    {
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
        // collect parameter variables
        if ( pPars->nIterCheck && vPars == NULL )
        {
            vPars = Vec_IntAlloc( Gia_ManPiNum(pM) - nFuncVars );
            Gia_ManForEachPi( pM, pObj, i )
                if ( i >= nFuncVars )
                    Vec_IntPush( vPars, pCnf->pVarNums[Gia_ObjId(pM, pObj)] );
            assert( Vec_IntSize(vPars) == Gia_ManPiNum(pM) - nFuncVars );
        }
        // derive unit parameter variables
        if ( Iter && pPars->nIterCheck && (Iter % pPars->nIterCheck) == 0 )
        {
            Gia_ManFaultAnalyze( pSat, vPars, vMap, vLits, Iter );
            // cleanup
            Gia_ManStop( pM );
            Cnf_DataFree( pCnf );
            sat_solver_delete( pSat );
            // recompute
            if ( !Gia_ManFaultPrepare(p, pG, pPars, nFuncVars, vMap, vTests, vLits, &pM, &pCnf, &pSat, 0) )
            {
                printf( "This should never happen.\n" );
                return;
            }
            Vec_IntFreeP( &vPars );
        }
        // solve
        clk = Abc_Clock();
1661
        status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
1662
        clkSat += Abc_Clock() - clk;
1663
        if ( pPars->fVerbose )
1664 1665 1666 1667 1668
        {
            printf( "Iter%6d : ",       Iter );
            printf( "Var =%10d  ",      sat_solver_nvars(pSat) );
            printf( "Clause =%10d  ",   sat_solver_nclauses(pSat) );
            printf( "Conflict =%10d  ", sat_solver_nconflicts(pSat) );
1669
            //Abc_PrintTime( 1, "Time", clkSat );
1670 1671
            ABC_PRTr( "Solver time", clkSat );
        }
1672 1673
        if ( status == l_Undef )
        {
1674
            if ( pPars->fVerbose )
1675
                printf( "\n" );
1676 1677
            printf( "Timeout reached after %d seconds and %d iterations.\n", pPars->nTimeOut, Iter );
            goto finish;
1678 1679 1680
        }
        if ( status == l_False )
        {
1681
            if ( pPars->fVerbose )
1682 1683
                printf( "\n" );
            printf( "The problem is UNSAT after %d iterations.  ", Iter );
1684 1685 1686 1687 1688 1689
            break;
        }
        assert( status == l_True );
        // collect SAT assignment
        Vec_IntClear( vLits );
        Gia_ManForEachPi( pM, pObj, i )
1690
            if ( i < nFuncVars )
1691
                Vec_IntPush( vLits, sat_solver_var_value(pSat, pCnf->pVarNums[Gia_ObjId(pM, pObj)]) );
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
        // if the user selected to generate new faults detected by this test (vLits)
        // and not detected by the given test set (vTests), we compute the new faults here and quit
        if ( pPars->fDumpNewFaults )
        {
            if ( Vec_IntSize(vTests) == 0 )
                printf( "The input test patterns are not given.\n" );
            else
                Gia_ManFaultDumpNewFaults( pM, nFuncVars, vTests, vLits, pPars );
            goto finish_all;
        }
1702
        Vec_IntAppend( vTests, vLits );
1703
        // add constraint
1704
        if ( !Gia_ManFaultAddOne( pM, pCnf, pSat, vLits, nFuncVars, 0, pM ) )
1705 1706 1707 1708 1709 1710
        {
            if ( pPars->fVerbose )
                printf( "\n" );
            printf( "The problem is UNSAT after adding %d tests.\n", Iter );
            break;
        }
1711
    }
1712
    else Iter = Vec_IntSize(vTests) / nFuncVars;
1713
finish:
1714 1715 1716
    // print results
//    if ( status == l_False )
//        Gia_ManPrintResults( p, pSat, Iter, Abc_Clock() - clkTotal );
1717
    // cleanup
1718
    Abc_PrintTime( 1, "Testing runtime", Abc_Clock() - clkTotal );
1719
    // dump the test suite
1720
    if ( pPars->fDump )
1721
    {
1722
        char * pFileName = p->pSpec ? Extra_FileNameGenericAppend(p->pSpec, "_tests.txt") : (char *)"tests.txt";
1723
        if ( pPars->fDumpDelay && pPars->Algo == 1 )
1724 1725 1726 1727 1728 1729 1730 1731 1732
        {
            Gia_ManDumpTestsDelay( vTests, Iter, pFileName, p );
            printf( "Dumping %d pairs of test patterns (total %d pattern) into file \"%s\".\n", Vec_IntSize(vTests) / nFuncVars, 2*Vec_IntSize(vTests) / nFuncVars, pFileName );
        }
        else
        {
            Gia_ManDumpTests( vTests, Iter, pFileName );
            printf( "Dumping %d test patterns into file \"%s\".\n", Vec_IntSize(vTests) / nFuncVars, pFileName );
        }
1733
    }
1734 1735

    // compute untestable faults
1736
    if ( Iter && (p != pG || pPars->fDumpUntest || pPars->fCheckUntest) )
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
    {
        abctime clkTotal = Abc_Clock();
        // restart the SAT solver
        sat_solver_delete( pSat );
        pSat = sat_solver_new();
        sat_solver_setnvars( pSat, pCnf->nVars );
        sat_solver_set_runtime_limit( pSat, pPars->nTimeOut ? pPars->nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
        // add timeframe clauses
        for ( i = 0; i < pCnf->nClauses; i++ )
            if ( !sat_solver_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1] ) )
                assert( 0 );
        // add constraint to rule out no fault
//        if ( p == pG )
        {
            Vec_IntClear( vLits );
            Gia_ManForEachPi( pM, pObj, i )
                if ( i >= nFuncVars )
                    Vec_IntPush( vLits, Abc_Var2Lit(pCnf->pVarNums[Gia_ObjId(pM, pObj)], 0) );
            sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );
        }
        // add cardinality constraint
        if ( pPars->fBasic )
        {
            Vec_IntClear( vLits );
            Gia_ManForEachPi( pM, pObj, i )
                if ( i >= nFuncVars )
                    Vec_IntPush( vLits, pCnf->pVarNums[Gia_ObjId(pM, pObj)] );
            Cnf_AddCardinConstr( pSat, vLits );
        }
        // add output clauses
        Gia_ManForEachCo( pM, pObj, i )
        {
            int Lit = Abc_Var2Lit( pCnf->pVarNums[Gia_ObjId(pM, pObj)], 1 );
            sat_solver_addclause( pSat, &Lit, &Lit + 1 );
        }
        // simplify the SAT solver
        status = sat_solver_simplify( pSat );
        assert( status );

        // add test patterns
        assert( Vec_IntSize(vTests) == Iter * nFuncVars );
        for ( Iter2 = 0; ; Iter2++ )
        {
            abctime clk = Abc_Clock();
            status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
            clkSat += Abc_Clock() - clk;
            if ( pPars->fVerbose )
            {
                printf( "Iter%6d : ",       Iter2 );
                printf( "Var =%10d  ",      sat_solver_nvars(pSat) );
                printf( "Clause =%10d  ",   sat_solver_nclauses(pSat) );
                printf( "Conflict =%10d  ", sat_solver_nconflicts(pSat) );
                //Abc_PrintTime( 1, "Time", clkSat );
                ABC_PRTr( "Solver time", clkSat );
            }
            if ( status == l_Undef )
            {
                if ( pPars->fVerbose )
                    printf( "\n" );
                printf( "Timeout reached after %d seconds and %d iterations.\n", pPars->nTimeOut, Iter2 );
                goto finish;
            }
            if ( Iter2 == Iter )
                break;
            assert( status == l_True );
            // get pattern
            Vec_IntClear( vLits );
            for ( i = 0; i < nFuncVars; i++ )
                Vec_IntPush( vLits, Vec_IntEntry(vTests, Iter2*nFuncVars + i) );
1806
            if ( !Gia_ManFaultAddOne( pM, pCnf, pSat, vLits, nFuncVars, 0, pM ) )
1807 1808 1809 1810
            {
                printf( "The problem is UNSAT after adding %d tests.\n", Iter2 );
                goto finish;
            }
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
        }
        assert( Iter2 == Iter );
        if ( pPars->fVerbose )
            printf( "\n" );
        if ( p == pG )
        {
            if ( status == l_True )
                printf( "There are untestable faults.  " );
            else if ( status == l_False )
                printf( "There is no untestable faults.  " );
            else assert( 0 );
            Abc_PrintTime( 1, "Fault computation runtime", Abc_Clock() - clkTotal );
        }
        else
        {
            if ( status == l_True )
                printf( "The circuit is rectifiable.  " );
            else if ( status == l_False )
                printf( "The circuit is not rectifiable (or equivalent to the golden one).  " );
            else assert( 0 );
            Abc_PrintTime( 1, "Rectification runtime", Abc_Clock() - clkTotal );
        }
        // dump untestable faults
        if ( pPars->fDumpUntest && status == l_True )
        {
            abctime clk = Abc_Clock();
1837
            char * pFileName = p->pSpec ? Extra_FileNameGenericAppend(p->pSpec, "_untest.txt") : (char *)"untest.txt";
1838 1839 1840 1841 1842 1843 1844 1845
            int nUntests = Gia_ManDumpUntests( pM, pCnf, pSat, nFuncVars, pFileName, pPars->fVerbose );
            if ( p == pG )
                printf( "Dumped %d untestable multiple faults into file \"%s\".  ", nUntests, pFileName );
            else
                printf( "Dumped %d ways of rectifying the circuit into file \"%s\".  ", nUntests, pFileName );
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
        }
    }
1846
finish_all:
1847 1848 1849
    sat_solver_delete( pSat );
    Cnf_DataFree( pCnf );
    Gia_ManStop( pM );
1850
    Vec_IntFree( vTests );
1851
    Vec_IntFree( vMap );
1852
    Vec_IntFree( vLits );
1853
    Vec_IntFreeP( &vPars );
1854 1855
}

1856 1857


1858 1859 1860 1861 1862 1863 1864
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END