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

  FileName    [abcDar.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [DAG-aware rewriting.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"
22
#include "main.h"
Alan Mishchenko committed
23
#include "giaAig.h"
Alan Mishchenko committed
24
#include "dar.h"
Alan Mishchenko committed
25
#include "cnf.h"
Alan Mishchenko committed
26 27
#include "fra.h"
#include "fraig.h"
Alan Mishchenko committed
28
#include "int.h"
Alan Mishchenko committed
29
#include "dch.h"
Alan Mishchenko committed
30
#include "ssw.h"
Alan Mishchenko committed
31
#include "cgt.h"
32
#include "bbr.h"
Alan Mishchenko committed
33
#include "gia.h"
Alan Mishchenko committed
34
#include "cec.h"
35
#include "csw.h"
36
#include "pdr.h" 
Alan Mishchenko committed
37

38 39
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
40 41 42 43 44 45 46 47 48 49 50 51
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
52
  Description [Assumes that registers are ordered after PIs/POs.]
Alan Mishchenko committed
53 54 55 56 57 58
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
59
Aig_Man_t * Abc_NtkToDar( Abc_Ntk_t * pNtk, int fExors, int fRegisters )
Alan Mishchenko committed
60
{
Alan Mishchenko committed
61
    Aig_Man_t * pMan;
Alan Mishchenko committed
62
    Aig_Obj_t * pObjNew;
Alan Mishchenko committed
63
    Abc_Obj_t * pObj;
Alan Mishchenko committed
64
    int i, nNodes, nDontCares;
Alan Mishchenko committed
65
    // make sure the latches follow PIs/POs
Alan Mishchenko committed
66 67
    if ( fRegisters ) 
    { 
Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        assert( Abc_NtkBoxNum(pNtk) == Abc_NtkLatchNum(pNtk) );
        Abc_NtkForEachCi( pNtk, pObj, i )
            if ( i < Abc_NtkPiNum(pNtk) )
            {
                assert( Abc_ObjIsPi(pObj) );
                if ( !Abc_ObjIsPi(pObj) )
                    printf( "Abc_NtkToDar(): Temporary bug: The PI ordering is wrong!\n" );
            }
            else
                assert( Abc_ObjIsBo(pObj) );
        Abc_NtkForEachCo( pNtk, pObj, i )
            if ( i < Abc_NtkPoNum(pNtk) )
            {
                assert( Abc_ObjIsPo(pObj) );
                if ( !Abc_ObjIsPo(pObj) )
                    printf( "Abc_NtkToDar(): Temporary bug: The PO ordering is wrong!\n" );
            }
            else
                assert( Abc_ObjIsBi(pObj) );
Alan Mishchenko committed
87 88 89 90 91 92 93 94 95 96 97 98 99
        // print warning about initial values
        nDontCares = 0;
        Abc_NtkForEachLatch( pNtk, pObj, i )
            if ( Abc_LatchIsInitDc(pObj) )
            {
                Abc_LatchSetInit0(pObj);
                nDontCares++;
            }
        if ( nDontCares )
        {
            printf( "Warning: %d registers in this network have don't-care init values.\n", nDontCares );
            printf( "The don't-care are assumed to be 0. The result may not verify.\n" );
            printf( "Use command \"print_latch\" to see the init values of registers.\n" );
Alan Mishchenko committed
100
            printf( "Use command \"zero\" to convert or \"init\" to change the values.\n" );
Alan Mishchenko committed
101
        }
Alan Mishchenko committed
102
    }
Alan Mishchenko committed
103
    // create the manager
Alan Mishchenko committed
104
    pMan = Aig_ManStart( Abc_NtkNodeNum(pNtk) + 100 );
Alan Mishchenko committed
105
    pMan->fCatchExor = fExors;
106
    pMan->nConstrs = pNtk->nConstrs;
Alan Mishchenko committed
107

Alan Mishchenko committed
108
    pMan->pName = Extra_UtilStrsav( pNtk->pName );
Alan Mishchenko committed
109
    // transfer the pointers to the basic nodes
Alan Mishchenko committed
110
    Abc_AigConst1(pNtk)->pCopy = (Abc_Obj_t *)Aig_ManConst1(pMan);
Alan Mishchenko committed
111
    Abc_NtkForEachCi( pNtk, pObj, i )
Alan Mishchenko committed
112
        pObj->pCopy = (Abc_Obj_t *)Aig_ObjCreatePi(pMan);
Alan Mishchenko committed
113
    // complement the 1-values registers
Alan Mishchenko committed
114
    if ( fRegisters ) {
Alan Mishchenko committed
115 116 117
        Abc_NtkForEachLatch( pNtk, pObj, i )
            if ( Abc_LatchIsInit1(pObj) )
                Abc_ObjFanout0(pObj)->pCopy = Abc_ObjNot(Abc_ObjFanout0(pObj)->pCopy);
Alan Mishchenko committed
118
    }
Alan Mishchenko committed
119
    // perform the conversion of the internal nodes (assumes DFS ordering)
Alan Mishchenko committed
120
//    pMan->fAddStrash = 1;
Alan Mishchenko committed
121
    Abc_NtkForEachNode( pNtk, pObj, i )
Alan Mishchenko committed
122
    {
Alan Mishchenko committed
123 124
        pObj->pCopy = (Abc_Obj_t *)Aig_And( pMan, (Aig_Obj_t *)Abc_ObjChild0Copy(pObj), (Aig_Obj_t *)Abc_ObjChild1Copy(pObj) );
//        printf( "%d->%d ", pObj->Id, ((Aig_Obj_t *)pObj->pCopy)->Id );
Alan Mishchenko committed
125
    }
Alan Mishchenko committed
126
    pMan->fAddStrash = 0;
Alan Mishchenko committed
127 128
    // create the POs
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
129
        Aig_ObjCreatePo( pMan, (Aig_Obj_t *)Abc_ObjChild0Copy(pObj) );
Alan Mishchenko committed
130
    // complement the 1-valued registers
Alan Mishchenko committed
131
    Aig_ManSetRegNum( pMan, Abc_NtkLatchNum(pNtk) );
Alan Mishchenko committed
132 133
    if ( fRegisters )
        Aig_ManForEachLiSeq( pMan, pObjNew, i )
Alan Mishchenko committed
134
            if ( Abc_LatchIsInit1(Abc_ObjFanout0(Abc_NtkCo(pNtk,i))) )
Alan Mishchenko committed
135 136
                pObjNew->pFanin0 = Aig_Not(pObjNew->pFanin0);
    // remove dangling nodes
Alan Mishchenko committed
137
    nNodes = (Abc_NtkGetChoiceNum(pNtk) == 0)? Aig_ManCleanup( pMan ) : 0;
Alan Mishchenko committed
138
    if ( !fExors && nNodes )
Alan Mishchenko committed
139
        printf( "Abc_NtkToDar(): Unexpected %d dangling nodes when converting to AIG!\n", nNodes );
Alan Mishchenko committed
140
//Aig_ManDumpVerilog( pMan, "test.v" );
Alan Mishchenko committed
141 142 143 144 145 146 147 148 149 150
    // save the number of registers
    if ( fRegisters )
    {
        Aig_ManSetRegNum( pMan, Abc_NtkLatchNum(pNtk) );
        pMan->vFlopNums = Vec_IntStartNatural( pMan->nRegs );
//        pMan->vFlopNums = NULL;
//        pMan->vOnehots = Abc_NtkConverLatchNamesIntoNumbers( pNtk );
        if ( pNtk->vOnehots )
            pMan->vOnehots = (Vec_Ptr_t *)Vec_VecDupInt( (Vec_Vec_t *)pNtk->vOnehots );
    }
Alan Mishchenko committed
151 152 153 154 155 156
    if ( !Aig_ManCheck( pMan ) )
    {
        printf( "Abc_NtkToDar: AIG check has failed.\n" );
        Aig_ManStop( pMan );
        return NULL;
    }
Alan Mishchenko committed
157 158 159 160 161 162 163
    return pMan;
}

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  Description [Assumes that registers are ordered after PIs/POs.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Abc_NtkToDarChoices( Abc_Ntk_t * pNtk )
{
    Aig_Man_t * pMan;
    Abc_Obj_t * pObj, * pPrev, * pFanin;
    Vec_Ptr_t * vNodes;
    int i;
    vNodes = Abc_AigDfs( pNtk, 0, 0 );
    // create the manager
    pMan = Aig_ManStart( Abc_NtkNodeNum(pNtk) + 100 );
    pMan->pName = Extra_UtilStrsav( pNtk->pName );
181
    pMan->nConstrs = pNtk->nConstrs;
Alan Mishchenko committed
182 183
    if ( Abc_NtkGetChoiceNum(pNtk) )
    {
Alan Mishchenko committed
184
        pMan->pEquivs = ABC_ALLOC( Aig_Obj_t *, Abc_NtkObjNum(pNtk) );
Alan Mishchenko committed
185 186 187 188 189 190 191
        memset( pMan->pEquivs, 0, sizeof(Aig_Obj_t *) * Abc_NtkObjNum(pNtk) );
    }
    // transfer the pointers to the basic nodes
    Abc_AigConst1(pNtk)->pCopy = (Abc_Obj_t *)Aig_ManConst1(pMan);
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pCopy = (Abc_Obj_t *)Aig_ObjCreatePi(pMan);
    // perform the conversion of the internal nodes (assumes DFS ordering)
192
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
193 194 195 196 197
    {
        pObj->pCopy = (Abc_Obj_t *)Aig_And( pMan, (Aig_Obj_t *)Abc_ObjChild0Copy(pObj), (Aig_Obj_t *)Abc_ObjChild1Copy(pObj) );
//        printf( "%d->%d ", pObj->Id, ((Aig_Obj_t *)pObj->pCopy)->Id );
        if ( Abc_AigNodeIsChoice( pObj ) )
        {
198
            for ( pPrev = pObj, pFanin = (Abc_Obj_t *)pObj->pData; pFanin; pPrev = pFanin, pFanin = (Abc_Obj_t *)pFanin->pData )
Alan Mishchenko committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
                Aig_ObjSetEquiv( pMan, (Aig_Obj_t *)pPrev->pCopy, (Aig_Obj_t *)pFanin->pCopy );
//            Aig_ManCreateChoice( pIfMan, (Aig_Obj_t *)pNode->pCopy );
        }
    }
    Vec_PtrFree( vNodes );
    // create the POs
    Abc_NtkForEachCo( pNtk, pObj, i )
        Aig_ObjCreatePo( pMan, (Aig_Obj_t *)Abc_ObjChild0Copy(pObj) );
    // complement the 1-valued registers
    Aig_ManSetRegNum( pMan, 0 );
    if ( !Aig_ManCheck( pMan ) )
    {
        printf( "Abc_NtkToDar: AIG check has failed.\n" );
        Aig_ManStop( pMan );
        return NULL;
    }
    return pMan;
}

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
222 223 224 225 226 227 228
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
229
Abc_Ntk_t * Abc_NtkFromDar( Abc_Ntk_t * pNtkOld, Aig_Man_t * pMan )
Alan Mishchenko committed
230 231 232
{
    Vec_Ptr_t * vNodes;
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
233
    Aig_Obj_t * pObj;
Alan Mishchenko committed
234
    int i;
Alan Mishchenko committed
235
    assert( pMan->nAsserts == 0 );
Alan Mishchenko committed
236
//    assert( Aig_ManRegNum(pMan) == Abc_NtkLatchNum(pNtkOld) );
Alan Mishchenko committed
237
    // perform strashing
Alan Mishchenko committed
238
    pNtkNew = Abc_NtkStartFrom( pNtkOld, ABC_NTK_STRASH, ABC_FUNC_AIG );
239
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
240
    // transfer the pointers to the basic nodes
Alan Mishchenko committed
241
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
Alan Mishchenko committed
242
    Aig_ManForEachPi( pMan, pObj, i )
Alan Mishchenko committed
243 244
        pObj->pData = Abc_NtkCi(pNtkNew, i);
    // rebuild the AIG
Alan Mishchenko committed
245
    vNodes = Aig_ManDfs( pMan, 1 );
246
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
247 248
        if ( Aig_ObjIsBuf(pObj) )
            pObj->pData = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
Alan Mishchenko committed
249
        else
250
            pObj->pData = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, (Abc_Obj_t *)Aig_ObjChild0Copy(pObj), (Abc_Obj_t *)Aig_ObjChild1Copy(pObj) );
Alan Mishchenko committed
251 252
    Vec_PtrFree( vNodes );
    // connect the PO nodes
Alan Mishchenko committed
253
    Aig_ManForEachPo( pMan, pObj, i )
Alan Mishchenko committed
254 255 256
    {
        if ( pMan->nAsserts && i == Aig_ManPoNum(pMan) - pMan->nAsserts )
            break;
Alan Mishchenko committed
257
        Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, i), (Abc_Obj_t *)Aig_ObjChild0Copy(pObj) );
Alan Mishchenko committed
258 259
    }
    // if there are assertions, add them
Alan Mishchenko committed
260 261 262 263 264
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkFromDar(): Network check has failed.\n" );
    return pNtkNew;
}

Alan Mishchenko committed
265 266 267 268
/**Function*************************************************************

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
269 270 271 272 273 274 275 276 277 278
  Description [This procedure should be called after seq sweeping, 
  which changes the number of registers.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkFromDarSeqSweep( Abc_Ntk_t * pNtkOld, Aig_Man_t * pMan )
{
Alan Mishchenko committed
279
    Vec_Ptr_t * vNodes; 
Alan Mishchenko committed
280 281
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pObjNew, * pLatch;
Alan Mishchenko committed
282
    Aig_Obj_t * pObj, * pObjLo, * pObjLi;
Alan Mishchenko committed
283 284
    int i, iNodeId, nDigits; 
    assert( pMan->nAsserts == 0 );
Alan Mishchenko committed
285 286 287
//    assert( Aig_ManRegNum(pMan) != Abc_NtkLatchNum(pNtkOld) );
    // perform strashing
    pNtkNew = Abc_NtkStartFromNoLatches( pNtkOld, ABC_NTK_STRASH, ABC_FUNC_AIG );
288
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
289 290
    // consider the case of target enlargement
    if ( Abc_NtkCiNum(pNtkNew) < Aig_ManPiNum(pMan) - Aig_ManRegNum(pMan) )
Alan Mishchenko committed
291
    {
Alan Mishchenko committed
292 293 294 295 296 297
        for ( i = Aig_ManPiNum(pMan) - Aig_ManRegNum(pMan) - Abc_NtkCiNum(pNtkNew); i > 0; i-- )
        {
            pObjNew = Abc_NtkCreatePi( pNtkNew );
            Abc_ObjAssignName( pObjNew, Abc_ObjName(pObjNew), NULL );
        }
        Abc_NtkOrderCisCos( pNtkNew );
Alan Mishchenko committed
298
    }
Alan Mishchenko committed
299 300 301 302 303 304 305
    assert( Abc_NtkCiNum(pNtkNew) == Aig_ManPiNum(pMan) - Aig_ManRegNum(pMan) );
    assert( Abc_NtkCoNum(pNtkNew) == Aig_ManPoNum(pMan) - Aig_ManRegNum(pMan) );
    // transfer the pointers to the basic nodes
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
    Aig_ManForEachPiSeq( pMan, pObj, i )
        pObj->pData = Abc_NtkCi(pNtkNew, i);
    // create as many latches as there are registers in the manager
Alan Mishchenko committed
306 307 308
    Aig_ManForEachLiLoSeq( pMan, pObjLi, pObjLo, i )
    {
        pObjNew = Abc_NtkCreateLatch( pNtkNew );
Alan Mishchenko committed
309 310
        pObjLi->pData = Abc_NtkCreateBi( pNtkNew );
        pObjLo->pData = Abc_NtkCreateBo( pNtkNew );
311 312
        Abc_ObjAddFanin( pObjNew, (Abc_Obj_t *)pObjLi->pData );
        Abc_ObjAddFanin( (Abc_Obj_t *)pObjLo->pData, pObjNew );
Alan Mishchenko committed
313 314 315
        Abc_LatchSetInit0( pObjNew );
    }
    // rebuild the AIG
Alan Mishchenko committed
316
    vNodes = Aig_ManDfs( pMan, 1 );
317
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
318 319 320
        if ( Aig_ObjIsBuf(pObj) )
            pObj->pData = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        else
321
            pObj->pData = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, (Abc_Obj_t *)Aig_ObjChild0Copy(pObj), (Abc_Obj_t *)Aig_ObjChild1Copy(pObj) );
Alan Mishchenko committed
322 323 324
    Vec_PtrFree( vNodes );
    // connect the PO nodes
    Aig_ManForEachPo( pMan, pObj, i )
Alan Mishchenko committed
325
    {
Alan Mishchenko committed
326 327
//        if ( pMan->nAsserts && i == Aig_ManPoNum(pMan) - pMan->nAsserts )
//            break;
Alan Mishchenko committed
328 329 330 331 332
        iNodeId = Nm_ManFindIdByNameTwoTypes( pNtkNew->pManName, Abc_ObjName(Abc_NtkCo(pNtkNew, i)), ABC_OBJ_PI, ABC_OBJ_BO );
        if ( iNodeId >= 0 )
            pObjNew = Abc_NtkObj( pNtkNew, iNodeId );
        else
            pObjNew = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
Alan Mishchenko committed
333
        Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, i), pObjNew );
Alan Mishchenko committed
334
    }
Alan Mishchenko committed
335 336 337 338
    if ( pMan->vFlopNums == NULL )
        Abc_NtkAddDummyBoxNames( pNtkNew );
    else
    {
Alan Mishchenko committed
339
/*
Alan Mishchenko committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        {
            int i, k, iFlop, Counter = 0;
            FILE * pFile;
            pFile = fopen( "out.txt", "w" );
            fprintf( pFile, "The total of %d registers were removed (out of %d):\n", 
                Abc_NtkLatchNum(pNtkOld)-Vec_IntSize(pMan->vFlopNums), Abc_NtkLatchNum(pNtkOld) );
            for ( i = 0; i < Abc_NtkLatchNum(pNtkOld); i++ )
            {
                Vec_IntForEachEntry( pMan->vFlopNums, iFlop, k )
                {
                    if ( i == iFlop )
                        break;
                }
                if ( k == Vec_IntSize(pMan->vFlopNums) )
                    fprintf( pFile, "%6d (%6d)  :  %s\n", ++Counter, i, Abc_ObjName( Abc_ObjFanout0(Abc_NtkBox(pNtkOld, i)) ) );
            }
            fclose( pFile );
            //printf( "\n" );
        }
Alan Mishchenko committed
359
*/
Alan Mishchenko committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        assert( Abc_NtkBoxNum(pNtkOld) == Abc_NtkLatchNum(pNtkOld) );
        nDigits = Extra_Base10Log( Abc_NtkLatchNum(pNtkNew) );
        Abc_NtkForEachLatch( pNtkNew, pObjNew, i )
        {
            pLatch = Abc_NtkBox( pNtkOld, Vec_IntEntry( pMan->vFlopNums, i ) );
            iNodeId = Nm_ManFindIdByName( pNtkNew->pManName, Abc_ObjName(Abc_ObjFanout0(pLatch)), ABC_OBJ_PO );
            if ( iNodeId >= 0 )
            {
                Abc_ObjAssignName( pObjNew, Abc_ObjNameDummy("l", i, nDigits), NULL );
                Abc_ObjAssignName( Abc_ObjFanin0(pObjNew), Abc_ObjNameDummy("li", i, nDigits), NULL );
                Abc_ObjAssignName( Abc_ObjFanout0(pObjNew), Abc_ObjNameDummy("lo", i, nDigits), NULL );
//printf( "happening   %s -> %s\n", Abc_ObjName(Abc_ObjFanin0(pObjNew)), Abc_ObjName(Abc_ObjFanout0(pObjNew)) );
                continue;
            }
            Abc_ObjAssignName( pObjNew, Abc_ObjName(pLatch), NULL );
            Abc_ObjAssignName( Abc_ObjFanin0(pObjNew),  Abc_ObjName(Abc_ObjFanin0(pLatch)), NULL );
            Abc_ObjAssignName( Abc_ObjFanout0(pObjNew), Abc_ObjName(Abc_ObjFanout0(pLatch)), NULL );
        }
    }
Alan Mishchenko committed
379
    // if there are assertions, add them
Alan Mishchenko committed
380 381 382 383 384 385 386 387 388
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkFromDar(): Network check has failed.\n" );
    return pNtkNew;
}

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  Description [This procedure should be called after seq sweeping, 
  which changes the number of registers.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkFromAigPhase( Aig_Man_t * pMan )
{
    Vec_Ptr_t * vNodes; 
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pObjNew;
    Aig_Obj_t * pObj, * pObjLo, * pObjLi;
    int i; 
    assert( pMan->nAsserts == 0 );
    // perform strashing
    pNtkNew = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
407
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
408 409 410
    // duplicate the name and the spec
//    pNtkNew->pName = Extra_UtilStrsav(pMan->pName);
//    pNtkNew->pSpec = Extra_UtilStrsav(pMan->pSpec);
Alan Mishchenko committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
    // create PIs
    Aig_ManForEachPiSeq( pMan, pObj, i )
    {
        pObjNew = Abc_NtkCreatePi( pNtkNew );
        Abc_ObjAssignName( pObjNew, Abc_ObjName(pObjNew), NULL );
        pObj->pData = pObjNew;
    }
    // create POs
    Aig_ManForEachPoSeq( pMan, pObj, i )
    {
        pObjNew = Abc_NtkCreatePo( pNtkNew );
        Abc_ObjAssignName( pObjNew, Abc_ObjName(pObjNew), NULL );
        pObj->pData = pObjNew;
    }
    assert( Abc_NtkCiNum(pNtkNew) == Aig_ManPiNum(pMan) - Aig_ManRegNum(pMan) );
    assert( Abc_NtkCoNum(pNtkNew) == Aig_ManPoNum(pMan) - Aig_ManRegNum(pMan) );
    // create as many latches as there are registers in the manager
    Aig_ManForEachLiLoSeq( pMan, pObjLi, pObjLo, i )
    {
        pObjNew = Abc_NtkCreateLatch( pNtkNew );
        pObjLi->pData = Abc_NtkCreateBi( pNtkNew );
        pObjLo->pData = Abc_NtkCreateBo( pNtkNew );
434 435
        Abc_ObjAddFanin( pObjNew, (Abc_Obj_t *)pObjLi->pData );
        Abc_ObjAddFanin( (Abc_Obj_t *)pObjLo->pData, pObjNew );
Alan Mishchenko committed
436
        Abc_LatchSetInit0( pObjNew );
437 438
        Abc_ObjAssignName( (Abc_Obj_t *)pObjLi->pData, Abc_ObjName((Abc_Obj_t *)pObjLi->pData), NULL );
        Abc_ObjAssignName( (Abc_Obj_t *)pObjLo->pData, Abc_ObjName((Abc_Obj_t *)pObjLo->pData), NULL );
Alan Mishchenko committed
439 440 441
    }
    // rebuild the AIG
    vNodes = Aig_ManDfs( pMan, 1 );
442
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
443 444 445
        if ( Aig_ObjIsBuf(pObj) )
            pObj->pData = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        else
446
            pObj->pData = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, (Abc_Obj_t *)Aig_ObjChild0Copy(pObj), (Abc_Obj_t *)Aig_ObjChild1Copy(pObj) );
Alan Mishchenko committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    Vec_PtrFree( vNodes );
    // connect the PO nodes
    Aig_ManForEachPo( pMan, pObj, i )
    {
        pObjNew = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, i), pObjNew );
    }
    // check the resulting AIG
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkFromAigPhase(): Network check has failed.\n" );
    return pNtkNew;
}

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  Description [This procedure should be called after seq sweeping, 
  which changes the number of registers.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkAfterTrim( Aig_Man_t * pMan, Abc_Ntk_t * pNtkOld )
{
    Vec_Ptr_t * vNodes; 
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pObjNew, * pObjOld;
    Aig_Obj_t * pObj, * pObjLo, * pObjLi;
    int i; 
    assert( pMan->nAsserts == 0 );
    assert( Aig_ManRegNum(pMan) <= Abc_NtkLatchNum(pNtkOld) );
    assert( Saig_ManPiNum(pMan) <= Abc_NtkCiNum(pNtkOld) );
    assert( Saig_ManPoNum(pMan) == Abc_NtkPoNum(pNtkOld) );
    assert( pMan->vCiNumsOrig != NULL );
    // perform strashing
    pNtkNew = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
486
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    // duplicate the name and the spec
//    pNtkNew->pName = Extra_UtilStrsav(pMan->pName);
//    pNtkNew->pSpec = Extra_UtilStrsav(pMan->pSpec);
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
    // create PIs
    Aig_ManForEachPiSeq( pMan, pObj, i )
    {
        pObjNew = Abc_NtkCreatePi( pNtkNew );
        pObj->pData = pObjNew;
        // find the name
        pObjOld = Abc_NtkCi( pNtkOld, Vec_IntEntry(pMan->vCiNumsOrig, i) );
        Abc_ObjAssignName( pObjNew, Abc_ObjName(pObjOld), NULL );
    }
    // create POs
    Aig_ManForEachPoSeq( pMan, pObj, i )
    {
        pObjNew = Abc_NtkCreatePo( pNtkNew );
        pObj->pData = pObjNew;
        // find the name
        pObjOld = Abc_NtkCo( pNtkOld, i );
        Abc_ObjAssignName( pObjNew, Abc_ObjName(pObjOld), NULL );
    }
    assert( Abc_NtkCiNum(pNtkNew) == Aig_ManPiNum(pMan) - Aig_ManRegNum(pMan) );
    assert( Abc_NtkCoNum(pNtkNew) == Aig_ManPoNum(pMan) - Aig_ManRegNum(pMan) );
    // create as many latches as there are registers in the manager
    Aig_ManForEachLiLoSeq( pMan, pObjLi, pObjLo, i )
    {
        pObjNew = Abc_NtkCreateLatch( pNtkNew );
        pObjLi->pData = Abc_NtkCreateBi( pNtkNew );
        pObjLo->pData = Abc_NtkCreateBo( pNtkNew );
517 518
        Abc_ObjAddFanin( pObjNew, (Abc_Obj_t *)pObjLi->pData );
        Abc_ObjAddFanin( (Abc_Obj_t *)pObjLo->pData, pObjNew );
Alan Mishchenko committed
519 520 521
        Abc_LatchSetInit0( pObjNew );
        // find the name
        pObjOld = Abc_NtkCi( pNtkOld, Vec_IntEntry(pMan->vCiNumsOrig, Saig_ManPiNum(pMan)+i) );
522
        Abc_ObjAssignName( (Abc_Obj_t *)pObjLo->pData, Abc_ObjName(pObjOld), NULL );
Alan Mishchenko committed
523 524
        // find the name
        pObjOld = Abc_NtkCo( pNtkOld, Saig_ManPoNum(pMan)+i );
525
        Abc_ObjAssignName( (Abc_Obj_t *)pObjLi->pData, Abc_ObjName(pObjOld), NULL );
Alan Mishchenko committed
526 527 528
    }
    // rebuild the AIG
    vNodes = Aig_ManDfs( pMan, 1 );
529
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
530 531 532
        if ( Aig_ObjIsBuf(pObj) )
            pObj->pData = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        else
533
            pObj->pData = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, (Abc_Obj_t *)Aig_ObjChild0Copy(pObj), (Abc_Obj_t *)Aig_ObjChild1Copy(pObj) );
Alan Mishchenko committed
534 535 536 537 538 539 540 541 542
    Vec_PtrFree( vNodes );
    // connect the PO nodes
    Aig_ManForEachPo( pMan, pObj, i )
    {
        pObjNew = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, i), pObjNew );
    }
    // check the resulting AIG
    if ( !Abc_NtkCheck( pNtkNew ) )
543
        fprintf( stdout, "Abc_NtkAfterTrim(): Network check has failed.\n" );
Alan Mishchenko committed
544 545 546 547 548 549 550
    return pNtkNew;
}

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

  Synopsis    [Converts the network from the AIG manager into ABC.]

Alan Mishchenko committed
551 552 553 554 555 556 557
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
558 559 560 561 562 563
Abc_Ntk_t * Abc_NtkFromDarChoices( Abc_Ntk_t * pNtkOld, Aig_Man_t * pMan )
{
    Vec_Ptr_t * vNodes;
    Abc_Ntk_t * pNtkNew;
    Aig_Obj_t * pObj, * pTemp;
    int i;
Alan Mishchenko committed
564
    assert( pMan->pEquivs != NULL );
Alan Mishchenko committed
565 566
    assert( Aig_ManBufNum(pMan) == 0 );
    // perform strashing
Alan Mishchenko committed
567
    pNtkNew = Abc_NtkStartFrom( pNtkOld, ABC_NTK_STRASH, ABC_FUNC_AIG );
568
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
569
    // transfer the pointers to the basic nodes
Alan Mishchenko committed
570
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
Alan Mishchenko committed
571 572
    Aig_ManForEachPi( pMan, pObj, i )
        pObj->pData = Abc_NtkCi(pNtkNew, i);
Alan Mishchenko committed
573

Alan Mishchenko committed
574 575
    // rebuild the AIG
    vNodes = Aig_ManDfsChoices( pMan );
576
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
577
    {
578
        pObj->pData = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, (Abc_Obj_t *)Aig_ObjChild0Copy(pObj), (Abc_Obj_t *)Aig_ObjChild1Copy(pObj) );
Alan Mishchenko committed
579
        if ( (pTemp = Aig_ObjEquiv(pMan, pObj)) )
Alan Mishchenko committed
580 581 582
        {
            Abc_Obj_t * pAbcRepr, * pAbcObj;
            assert( pTemp->pData != NULL );
583 584
            pAbcRepr = (Abc_Obj_t *)pObj->pData;
            pAbcObj  = (Abc_Obj_t *)pTemp->pData;
Alan Mishchenko committed
585 586 587 588
            pAbcObj->pData  = pAbcRepr->pData;
            pAbcRepr->pData = pAbcObj;
        }
    }
Alan Mishchenko committed
589
//printf( "Total = %d.  Collected = %d.\n", Aig_ManNodeNum(pMan), Vec_PtrSize(vNodes) );
Alan Mishchenko committed
590 591 592 593 594 595 596 597 598 599 600
    Vec_PtrFree( vNodes );
    // connect the PO nodes
    Aig_ManForEachPo( pMan, pObj, i )
        Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, i), (Abc_Obj_t *)Aig_ObjChild0Copy(pObj) );
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkFromDar(): Network check has failed.\n" );
    return pNtkNew;
}

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

Alan Mishchenko committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  Synopsis    [Converts the network from the AIG manager into ABC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkFromDarSeq( Abc_Ntk_t * pNtkOld, Aig_Man_t * pMan )
{
    Vec_Ptr_t * vNodes;
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pObjNew, * pFaninNew, * pFaninNew0, * pFaninNew1;
    Aig_Obj_t * pObj;
    int i;
//    assert( Aig_ManLatchNum(pMan) > 0 );
    // perform strashing
    pNtkNew = Abc_NtkStartFromNoLatches( pNtkOld, ABC_NTK_STRASH, ABC_FUNC_AIG );
620
    pNtkNew->nConstrs = pMan->nConstrs;
Alan Mishchenko committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    // transfer the pointers to the basic nodes
    Aig_ManConst1(pMan)->pData = Abc_AigConst1(pNtkNew);
    Aig_ManForEachPi( pMan, pObj, i )
        pObj->pData = Abc_NtkPi(pNtkNew, i);
    // create latches of the new network
    Aig_ManForEachObj( pMan, pObj, i )
    {
        pObjNew = Abc_NtkCreateLatch( pNtkNew );
        pFaninNew0 = Abc_NtkCreateBi( pNtkNew );
        pFaninNew1 = Abc_NtkCreateBo( pNtkNew );
        Abc_ObjAddFanin( pObjNew, pFaninNew0 );
        Abc_ObjAddFanin( pFaninNew1, pObjNew );
        Abc_LatchSetInit0( pObjNew );
        pObj->pData = Abc_ObjFanout0( pObjNew );
    }
    Abc_NtkAddDummyBoxNames( pNtkNew );
    // rebuild the AIG
Alan Mishchenko committed
638
    vNodes = Aig_ManDfs( pMan, 1 );
639
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
640 641 642 643 644 645 646 647 648
    {
        // add the first fanin
        pObj->pData = pFaninNew0 = (Abc_Obj_t *)Aig_ObjChild0Copy(pObj);
        if ( Aig_ObjIsBuf(pObj) )
            continue;
        // add the second fanin
        pFaninNew1 = (Abc_Obj_t *)Aig_ObjChild1Copy(pObj);
        // create the new node
        if ( Aig_ObjIsExor(pObj) )
649
            pObj->pData = pObjNew = Abc_AigXor( (Abc_Aig_t *)pNtkNew->pManFunc, pFaninNew0, pFaninNew1 );
Alan Mishchenko committed
650
        else
651
            pObj->pData = pObjNew = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, pFaninNew0, pFaninNew1 );
Alan Mishchenko committed
652 653 654 655 656 657 658 659 660 661 662 663
    }
    Vec_PtrFree( vNodes );
    // connect the PO nodes
    Aig_ManForEachPo( pMan, pObj, i )
    {
        pFaninNew = (Abc_Obj_t *)Aig_ObjChild0Copy( pObj );
        Abc_ObjAddFanin( Abc_NtkPo(pNtkNew, i), pFaninNew );
    }
    // connect the latches
    Aig_ManForEachObj( pMan, pObj, i )
    {
        pFaninNew = (Abc_Obj_t *)Aig_ObjChild0Copy( pObj );
664
        Abc_ObjAddFanin( Abc_ObjFanin0(Abc_ObjFanin0((Abc_Obj_t *)pObj->pData)), pFaninNew );
Alan Mishchenko committed
665 666 667 668 669 670 671 672
    }
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkFromIvySeq(): Network check has failed.\n" );
    return pNtkNew;
}

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

Alan Mishchenko committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  Synopsis    [Collects CI of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkCollectCiNames( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    int i;
    Vec_Ptr_t * vNames;
    vNames = Vec_PtrAlloc( 100 );
    Abc_NtkForEachCi( pNtk, pObj, i )
        Vec_PtrPush( vNames, Extra_UtilStrsav(Abc_ObjName(pObj)) );
    return vNames;
}

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

  Synopsis    [Collects CO of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkCollectCoNames( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    int i;
    Vec_Ptr_t * vNames;
    vNames = Vec_PtrAlloc( 100 );
    Abc_NtkForEachCo( pNtk, pObj, i )
        Vec_PtrPush( vNames, Extra_UtilStrsav(Abc_ObjName(pObj)) );
    return vNames;
}

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

Alan Mishchenko committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
  Synopsis    [Collect latch values.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Abc_NtkGetLatchValues( Abc_Ntk_t * pNtk )
{
    Vec_Int_t * vInits;
    Abc_Obj_t * pLatch;
    int i;
    vInits = Vec_IntAlloc( Abc_NtkLatchNum(pNtk) );
    Abc_NtkForEachLatch( pNtk, pLatch, i )
    {
Alan Mishchenko committed
734 735 736 737 738 739 740 741
        if ( Abc_LatchIsInit0(pLatch) )
            Vec_IntPush( vInits, 0 );
        else if ( Abc_LatchIsInit1(pLatch) )
            Vec_IntPush( vInits, 1 );
        else if ( Abc_LatchIsInitDc(pLatch) )
            Vec_IntPush( vInits, 2 );
        else
            assert( 0 );
Alan Mishchenko committed
742 743 744 745 746 747
    }
    return vInits;
}

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

Alan Mishchenko committed
748 749 750 751 752 753 754 755 756 757 758
  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDar( Abc_Ntk_t * pNtk )
{
Alan Mishchenko committed
759 760 761
    Abc_Ntk_t * pNtkAig = NULL;
    Aig_Man_t * pMan;
    extern void Fra_ManPartitionTest( Aig_Man_t * p, int nComLim );
Alan Mishchenko committed
762 763 764

    assert( Abc_NtkIsStrash(pNtk) );
    // convert to the AIG manager
Alan Mishchenko committed
765
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
766 767 768
    if ( pMan == NULL )
        return NULL;

Alan Mishchenko committed
769 770 771
    // perform computation
//    Fra_ManPartitionTest( pMan, 4 );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
Alan Mishchenko committed
772
    Aig_ManStop( pMan );
Alan Mishchenko committed
773

Alan Mishchenko committed
774
    // make sure everything is okay
Alan Mishchenko committed
775
    if ( pNtkAig && !Abc_NtkCheck( pNtkAig ) )
Alan Mishchenko committed
776 777 778 779 780 781 782 783
    {
        printf( "Abc_NtkDar: The network check has failed.\n" );
        Abc_NtkDelete( pNtkAig );
        return NULL;
    }
    return pNtkAig;
}

Alan Mishchenko committed
784

Alan Mishchenko committed
785 786 787 788 789 790 791 792 793 794 795
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
796
Abc_Ntk_t * Abc_NtkDarFraig( Abc_Ntk_t * pNtk, int nConfLimit, int fDoSparse, int fProve, int fTransfer, int fSpeculate, int fChoicing, int fVerbose )
Alan Mishchenko committed
797
{
Alan Mishchenko committed
798
    Fra_Par_t Pars, * pPars = &Pars; 
Alan Mishchenko committed
799
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
800
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
801
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
802 803
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
804 805 806 807 808 809 810 811 812 813 814 815 816
    Fra_ParamsDefault( pPars );
    pPars->nBTLimitNode = nConfLimit;
    pPars->fChoicing    = fChoicing;
    pPars->fDoSparse    = fDoSparse;
    pPars->fSpeculate   = fSpeculate;
    pPars->fProve       = fProve;
    pPars->fVerbose     = fVerbose;
    pMan = Fra_FraigPerform( pTemp = pMan, pPars );
    if ( fChoicing )
        pNtkAig = Abc_NtkFromDarChoices( pNtk, pMan );
    else
        pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pTemp );
Alan Mishchenko committed
817
    Aig_ManStop( pMan );
Alan Mishchenko committed
818 819 820
    return pNtkAig;
}

Alan Mishchenko committed
821 822
/**Function*************************************************************

Alan Mishchenko committed
823
  Synopsis    [Gives the current ABC network to AIG manager for processing.]
Alan Mishchenko committed
824 825 826 827 828 829

  Description []
               
  SideEffects []

  SeeAlso     []
Alan Mishchenko committed
830

Alan Mishchenko committed
831
***********************************************************************/
Alan Mishchenko committed
832 833 834 835
Abc_Ntk_t * Abc_NtkDarFraigPart( Abc_Ntk_t * pNtk, int nPartSize, int nConfLimit, int nLevelMax, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
836
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    if ( pMan == NULL )
        return NULL;
    pMan = Aig_ManFraigPartitioned( pTemp = pMan, nPartSize, nConfLimit, nLevelMax, fVerbose );
    Aig_ManStop( pTemp );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
857
Abc_Ntk_t * Abc_NtkCSweep( Abc_Ntk_t * pNtk, int nCutsMax, int nLeafMax, int fVerbose )
Alan Mishchenko committed
858
{
859
//    extern Aig_Man_t * Csw_Sweep( Aig_Man_t * pAig, int nCutsMax, int nLeafMax, int fVerbose );
Alan Mishchenko committed
860
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
861
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
862
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
863 864
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
865 866
    pMan = Csw_Sweep( pTemp = pMan, nCutsMax, nLeafMax, fVerbose );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
Alan Mishchenko committed
867
    Aig_ManStop( pTemp );
Alan Mishchenko committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDRewrite( Abc_Ntk_t * pNtk, Dar_RwrPar_t * pPars )
{
    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig;
    int clk;
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
889
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
890 891 892 893 894
    if ( pMan == NULL )
        return NULL;
//    Aig_ManPrintStats( pMan );
/*
//    Aig_ManSupports( pMan );
Alan Mishchenko committed
895
    {
Alan Mishchenko committed
896 897 898
        Vec_Vec_t * vParts;
        vParts = Aig_ManPartitionSmart( pMan, 50, 1, NULL );
        Vec_VecFree( vParts );
Alan Mishchenko committed
899
    }
Alan Mishchenko committed
900 901 902 903 904 905
*/
    Dar_ManRewrite( pMan, pPars );
//    pMan = Dar_ManBalance( pTemp = pMan, pPars->fUpdateLevel );
//    Aig_ManStop( pTemp );

clk = clock();
Alan Mishchenko committed
906
    pMan = Aig_ManDupDfs( pTemp = pMan ); 
Alan Mishchenko committed
907
    Aig_ManStop( pTemp );
Alan Mishchenko committed
908
//ABC_PRT( "time", clock() - clk );
Alan Mishchenko committed
909 910 911

//    Aig_ManPrintStats( pMan );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
927
Abc_Ntk_t * Abc_NtkDRefactor( Abc_Ntk_t * pNtk, Dar_RefPar_t * pPars )
Alan Mishchenko committed
928
{
Alan Mishchenko committed
929
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
930 931 932
    Abc_Ntk_t * pNtkAig;
    int clk;
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
933
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
934 935 936
    if ( pMan == NULL )
        return NULL;
//    Aig_ManPrintStats( pMan );
Alan Mishchenko committed
937

Alan Mishchenko committed
938 939 940 941 942
    Dar_ManRefactor( pMan, pPars );
//    pMan = Dar_ManBalance( pTemp = pMan, pPars->fUpdateLevel );
//    Aig_ManStop( pTemp );

clk = clock();
Alan Mishchenko committed
943
    pMan = Aig_ManDupDfs( pTemp = pMan ); 
Alan Mishchenko committed
944
    Aig_ManStop( pTemp );
Alan Mishchenko committed
945
//ABC_PRT( "time", clock() - clk );
Alan Mishchenko committed
946 947 948 949 950

//    Aig_ManPrintStats( pMan );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
Alan Mishchenko committed
951 952
}

Alan Mishchenko committed
953 954 955 956 957 958 959 960 961 962 963
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
964
Abc_Ntk_t * Abc_NtkDC2( Abc_Ntk_t * pNtk, int fBalance, int fUpdateLevel, int fFanout, int fPower, int fVerbose )
Alan Mishchenko committed
965 966 967 968 969
{
    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig;
    int clk;
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
970
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
971 972
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
973
//    Aig_ManPrintStats( pMan );
Alan Mishchenko committed
974

Alan Mishchenko committed
975
clk = clock();
Alan Mishchenko committed
976
    pMan = Dar_ManCompress2( pTemp = pMan, fBalance, fUpdateLevel, fFanout, fPower, fVerbose ); 
Alan Mishchenko committed
977
    Aig_ManStop( pTemp );
Alan Mishchenko committed
978
//ABC_PRT( "time", clock() - clk );
Alan Mishchenko committed
979

Alan Mishchenko committed
980 981
//    Aig_ManPrintStats( pMan );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
Alan Mishchenko committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
997
Abc_Ntk_t * Abc_NtkDChoice( Abc_Ntk_t * pNtk, int fBalance, int fUpdateLevel, int fConstruct, int nConfMax, int nLevelMax, int fVerbose )
Alan Mishchenko committed
998
{
Alan Mishchenko committed
999 1000 1001
    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig;
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
1002
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
1003 1004
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
1005
    pMan = Dar_ManChoice( pTemp = pMan, fBalance, fUpdateLevel, fConstruct, nConfMax, nLevelMax, fVerbose );
Alan Mishchenko committed
1006 1007 1008 1009 1010 1011 1012
    Aig_ManStop( pTemp );
    pNtkAig = Abc_NtkFromDarChoices( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

/**Function*************************************************************
Alan Mishchenko committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDch( Abc_Ntk_t * pNtk, Dch_Pars_t * pPars )
{
1025
    extern Gia_Man_t * Dar_NewChoiceSynthesis( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fLightSynth, int fVerbose );
Alan Mishchenko committed
1026
    extern Aig_Man_t * Cec_ComputeChoices( Gia_Man_t * pGia, Dch_Pars_t * pPars );
Alan Mishchenko committed
1027 1028 1029

    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
1030 1031
    Gia_Man_t * pGia;
    int clk;
Alan Mishchenko committed
1032 1033 1034 1035
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
1036 1037
clk = clock();
    if ( pPars->fSynthesis )
1038
        pGia = Dar_NewChoiceSynthesis( pMan, 1, 1, pPars->fPower, pPars->fLightSynth, pPars->fVerbose );
Alan Mishchenko committed
1039 1040
    else
    {
Alan Mishchenko committed
1041 1042
        pGia = Gia_ManFromAig( pMan );
        Aig_ManStop( pMan );
Alan Mishchenko committed
1043 1044
    }
pPars->timeSynth = clock() - clk;
Alan Mishchenko committed
1045
    if ( pPars->fUseGia )
Alan Mishchenko committed
1046
        pMan = Cec_ComputeChoices( pGia, pPars );
Alan Mishchenko committed
1047
    else
Alan Mishchenko committed
1048 1049 1050 1051 1052 1053
    {
        pMan = Gia_ManToAigSkip( pGia, 3 );
        Gia_ManStop( pGia );
        pMan = Dch_ComputeChoices( pTemp = pMan, pPars );
        Aig_ManStop( pTemp );
    }
Alan Mishchenko committed
1054
    pNtkAig = Abc_NtkFromDarChoices( pNtk, pMan );
Alan Mishchenko committed
1055 1056 1057 1058 1059
    Aig_ManStop( pMan );
    return pNtkAig;
}

/**Function*************************************************************
Alan Mishchenko committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDrwsat( Abc_Ntk_t * pNtk, int fBalance, int fVerbose )
{
    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig;
    int clk;
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
1076
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
1077 1078 1079 1080 1081 1082 1083
    if ( pMan == NULL )
        return NULL;
//    Aig_ManPrintStats( pMan );

clk = clock();
    pMan = Dar_ManRwsat( pTemp = pMan, fBalance, fVerbose ); 
    Aig_ManStop( pTemp );
Alan Mishchenko committed
1084
//ABC_PRT( "time", clock() - clk );
Alan Mishchenko committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

//    Aig_ManPrintStats( pMan );
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkConstructFromCnf( Abc_Ntk_t * pNtk, Cnf_Man_t * p, Vec_Ptr_t * vMapped )
{
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pNode, * pNodeNew;
    Aig_Obj_t * pObj, * pLeaf;
    Cnf_Cut_t * pCut;
    Vec_Int_t * vCover;
    unsigned uTruth;
    int i, k, nDupGates;
    // create the new network
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_SOP );
    // make the mapper point to the new network
    Aig_ManConst1(p->pManAig)->pData = Abc_NtkCreateNodeConst1(pNtkNew);
    Abc_NtkForEachCi( pNtk, pNode, i )
        Aig_ManPi(p->pManAig, i)->pData = pNode->pCopy;
    // process the nodes in topological order
    vCover = Vec_IntAlloc( 1 << 16 );
1120
    Vec_PtrForEachEntry( Aig_Obj_t *, vMapped, pObj, i )
Alan Mishchenko committed
1121 1122 1123 1124
    {
        // create new node
        pNodeNew = Abc_NtkCreateNode( pNtkNew );
        // add fanins according to the cut
1125
        pCut = (Cnf_Cut_t *)pObj->pData;
Alan Mishchenko committed
1126
        Cnf_CutForEachLeaf( p->pManAig, pCut, pLeaf, k )
1127
            Abc_ObjAddFanin( pNodeNew, (Abc_Obj_t *)pLeaf->pData );
Alan Mishchenko committed
1128 1129 1130 1131 1132
        // add logic function
        if ( pCut->nFanins < 5 )
        {
            uTruth = 0xFFFF & *Cnf_CutTruth(pCut);
            Cnf_SopConvertToVector( p->pSops[uTruth], p->pSopSizes[uTruth], vCover );
1133
            pNodeNew->pData = Abc_SopCreateFromIsop( (Mem_Flex_t *)pNtkNew->pManFunc, pCut->nFanins, vCover );
Alan Mishchenko committed
1134 1135
        }
        else
1136
            pNodeNew->pData = Abc_SopCreateFromIsop( (Mem_Flex_t *)pNtkNew->pManFunc, pCut->nFanins, pCut->vIsop[1] );
Alan Mishchenko committed
1137 1138 1139 1140 1141 1142 1143 1144
        // save the node
        pObj->pData = pNodeNew;
    }
    Vec_IntFree( vCover );
    // add the CO drivers
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pObj = Aig_ManPo(p->pManAig, i);
1145
        pNodeNew = Abc_ObjNotCond( (Abc_Obj_t *)Aig_ObjFanin0(pObj)->pData, Aig_ObjFaninC0(pObj) );
Alan Mishchenko committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
        Abc_ObjAddFanin( pNode->pCopy, pNodeNew );
    }

    // remove the constant node if not used
    pNodeNew = (Abc_Obj_t *)Aig_ManConst1(p->pManAig)->pData;
    if ( Abc_ObjFanoutNum(pNodeNew) == 0 )
        Abc_NtkDeleteObj( pNodeNew );
    // minimize the node
//    Abc_NtkSweep( pNtkNew, 0 );
    // decouple the PO driver nodes to reduce the number of levels
    nDupGates = Abc_NtkLogicMakeSimpleCos( pNtkNew, 1 );
//    if ( nDupGates && If_ManReadVerbose(pIfMan) )
//        printf( "Duplicated %d gates to decouple the CO drivers.\n", nDupGates );
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkConstructFromCnf(): Network check has failed.\n" );
    return pNtkNew;
}
Alan Mishchenko committed
1163
 
Alan Mishchenko committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1175
Abc_Ntk_t * Abc_NtkDarToCnf( Abc_Ntk_t * pNtk, char * pFileName, int fFastAlgo, int fChangePol, int fVerbose )
Alan Mishchenko committed
1176
{
Alan Mishchenko committed
1177
    Vec_Ptr_t * vMapped = NULL;
Alan Mishchenko committed
1178
    Aig_Man_t * pMan;
Alan Mishchenko committed
1179
    Cnf_Man_t * pManCnf = NULL;
Alan Mishchenko committed
1180 1181
    Cnf_Dat_t * pCnf;
    Abc_Ntk_t * pNtkNew = NULL;
1182
    int clk = clock();
Alan Mishchenko committed
1183 1184 1185
    assert( Abc_NtkIsStrash(pNtk) );

    // convert to the AIG manager
Alan Mishchenko committed
1186
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
1187 1188 1189 1190 1191 1192 1193 1194 1195
    if ( pMan == NULL )
        return NULL;
    if ( !Aig_ManCheck( pMan ) )
    {
        printf( "Abc_NtkDarToCnf: AIG check has failed.\n" );
        Aig_ManStop( pMan );
        return NULL;
    }
    // perform balance
1196
    if ( fVerbose )
Alan Mishchenko committed
1197 1198 1199
    Aig_ManPrintStats( pMan );

    // derive CNF
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    if ( fFastAlgo )
        pCnf = Cnf_DeriveFast( pMan, 0 );
    else
        pCnf = Cnf_Derive( pMan, 0 );

    // adjust polarity
    if ( fChangePol )
        Cnf_DataTranformPolarity( pCnf, 0 );

    // print stats
    if ( fVerbose )
    {
        printf( "Vars = %6d. Clauses = %7d. Literals = %8d.   ", pCnf->nVars, pCnf->nClauses, pCnf->nLiterals );
        Abc_PrintTime( 1, "Time", clock() - clk );
    }
Alan Mishchenko committed
1215

Alan Mishchenko committed
1216
/*
Alan Mishchenko committed
1217
    // write the network for verification
Alan Mishchenko committed
1218
    pManCnf = Cnf_ManRead();
Alan Mishchenko committed
1219 1220 1221
    vMapped = Cnf_ManScanMapping( pManCnf, 1, 0 );
    pNtkNew = Abc_NtkConstructFromCnf( pNtk, pManCnf, vMapped );
    Vec_PtrFree( vMapped );
Alan Mishchenko committed
1222
*/
Alan Mishchenko committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    // write CNF into a file
    Cnf_DataWriteIntoFile( pCnf, pFileName, 0 );
    Cnf_DataFree( pCnf );
    Cnf_ClearMemory();
    Aig_ManStop( pMan );
    return pNtkNew;
}


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

  Synopsis    [Solves combinational miter using a SAT solver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1243
int Abc_NtkDSat( Abc_Ntk_t * pNtk, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fAlignPol, int fAndOuts, int fVerbose )
Alan Mishchenko committed
1244 1245 1246 1247 1248
{
    Aig_Man_t * pMan;
    int RetValue;//, clk = clock();
    assert( Abc_NtkIsStrash(pNtk) );
    assert( Abc_NtkLatchNum(pNtk) == 0 );
1249
//    assert( Abc_NtkPoNum(pNtk) == 1 );
Alan Mishchenko committed
1250
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
Alan Mishchenko committed
1251
    RetValue = Fra_FraigSat( pMan, nConfLimit, nInsLimit, fAlignPol, fAndOuts, fVerbose ); 
1252
    pNtk->pModel = (int *)pMan->pData, pMan->pData = NULL;
Alan Mishchenko committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
    Aig_ManStop( pMan );
    return RetValue;
}

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

  Synopsis    [Solves combinational miter using a SAT solver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkPartitionedSat( Abc_Ntk_t * pNtk, int nAlgo, int nPartSize, int nConfPart, int nConfTotal, int fAlignPol, int fSynthesize, int fVerbose )
{
    extern int Aig_ManPartitionedSat( Aig_Man_t * pNtk, int nAlgo, int nPartSize, int nConfPart, int nConfTotal, int fAlignPol, int fSynthesize, int fVerbose );
    Aig_Man_t * pMan;
    int RetValue;//, clk = clock();
    assert( Abc_NtkIsStrash(pNtk) );
    assert( Abc_NtkLatchNum(pNtk) == 0 );
    pMan = Abc_NtkToDar( pNtk, 0, 0 );
    RetValue = Aig_ManPartitionedSat( pMan, nAlgo, nPartSize, nConfPart, nConfTotal, fAlignPol, fSynthesize, fVerbose ); 
1277
    pNtk->pModel = (int *)pMan->pData, pMan->pData = NULL;
Alan Mishchenko committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    Aig_ManStop( pMan );
    return RetValue;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1293
int Abc_NtkDarCec( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int nConfLimit, int fPartition, int fVerbose )
Alan Mishchenko committed
1294 1295 1296 1297
{
    Aig_Man_t * pMan, * pMan1, * pMan2;
    Abc_Ntk_t * pMiter;
    int RetValue, clkTotal = clock();
Alan Mishchenko committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
/*
    {
    extern void Cec_ManVerifyTwoAigs( Aig_Man_t * pAig0, Aig_Man_t * pAig1, int fVerbose );
    Aig_Man_t * pAig0 = Abc_NtkToDar( pNtk1, 0, 0 );
    Aig_Man_t * pAig1 = Abc_NtkToDar( pNtk2, 0, 0 );
    Cec_ManVerifyTwoAigs( pAig0, pAig1, 1 );
    Aig_ManStop( pAig0 );
    Aig_ManStop( pAig1 );
    return 1;
    }
*/
Alan Mishchenko committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    // cannot partition if it is already a miter
    if ( pNtk2 == NULL && fPartition == 1 )
    {
        printf( "Abc_NtkDarCec(): Switching to non-partitioned CEC for the miter.\n" );
        fPartition = 0;
    }

    // if partitioning is selected, call partitioned CEC
    if ( fPartition )
    {
Alan Mishchenko committed
1319 1320
        pMan1 = Abc_NtkToDar( pNtk1, 0, 0 );
        pMan2 = Abc_NtkToDar( pNtk2, 0, 0 );
Alan Mishchenko committed
1321
        RetValue = Fra_FraigCecPartitioned( pMan1, pMan2, nConfLimit, 100, 1, fVerbose );
Alan Mishchenko committed
1322 1323 1324 1325 1326 1327 1328 1329
        Aig_ManStop( pMan1 );
        Aig_ManStop( pMan2 );
        goto finish;
    }

    if ( pNtk2 != NULL )
    {
        // get the miter of the two networks
Alan Mishchenko committed
1330
        pMiter = Abc_NtkMiter( pNtk1, pNtk2, 0, 0, 0, 0 );
Alan Mishchenko committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        if ( pMiter == NULL )
        {
            printf( "Miter computation has failed.\n" );
            return 0;
        }
    }
    else
    {
        pMiter = Abc_NtkDup( pNtk1 );
    }
    RetValue = Abc_NtkMiterIsConstant( pMiter );
    if ( RetValue == 0 )
    {
//        extern void Abc_NtkVerifyReportErrorSeq( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int * pModel, int nFrames );
        printf( "Networks are NOT EQUIVALENT after structural hashing.\n" );
        // report the error
        if ( pNtk2 == NULL )
            pNtk1->pModel = Abc_NtkVerifyGetCleanModel( pNtk1, 1 );
//        pMiter->pModel = Abc_NtkVerifyGetCleanModel( pMiter, nFrames );
//        Abc_NtkVerifyReportErrorSeq( pNtk1, pNtk2, pMiter->pModel, nFrames );
Alan Mishchenko committed
1351
//        ABC_FREE( pMiter->pModel );
Alan Mishchenko committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        Abc_NtkDelete( pMiter );
        return 0;
    }
    if ( RetValue == 1 )
    {
        Abc_NtkDelete( pMiter );
        printf( "Networks are equivalent after structural hashing.\n" );
        return 1;
    }

    // derive the AIG manager
Alan Mishchenko committed
1363
    pMan = Abc_NtkToDar( pMiter, 0, 0 );
Alan Mishchenko committed
1364 1365 1366 1367 1368 1369 1370
    Abc_NtkDelete( pMiter );
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
    // perform verification
Alan Mishchenko committed
1371
    RetValue = Fra_FraigCec( &pMan, 100000, fVerbose );
Alan Mishchenko committed
1372 1373
    // transfer model if given
    if ( pNtk2 == NULL )
1374
        pNtk1->pModel = (int *)pMan->pData, pMan->pData = NULL;
Alan Mishchenko committed
1375 1376 1377 1378 1379 1380 1381
    Aig_ManStop( pMan );

finish:
    // report the miter
    if ( RetValue == 1 )
    {
        printf( "Networks are equivalent.   " );
Alan Mishchenko committed
1382
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
1383 1384 1385 1386
    }
    else if ( RetValue == 0 )
    {
        printf( "Networks are NOT EQUIVALENT.   " );
Alan Mishchenko committed
1387
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
1388 1389 1390 1391
    }
    else
    {
        printf( "Networks are UNDECIDED.   " );
Alan Mishchenko committed
1392
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
    }
    fflush( stdout );
    return RetValue;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1409
Abc_Ntk_t * Abc_NtkDarSeqSweep( Abc_Ntk_t * pNtk, Fra_Ssw_t * pPars )
Alan Mishchenko committed
1410 1411
{
    Fraig_Params_t Params;
Alan Mishchenko committed
1412
    Abc_Ntk_t * pNtkAig = NULL, * pNtkFraig;
Alan Mishchenko committed
1413 1414 1415 1416 1417 1418 1419 1420
    Aig_Man_t * pMan, * pTemp;
    int clk = clock();

    // preprocess the miter by fraiging it
    // (note that for each functional class, fraiging leaves one representative;
    // so fraiging does not reduce the number of functions represented by nodes
    Fraig_ParamsSetDefault( &Params );
    Params.nBTLimit = 100000;
Alan Mishchenko committed
1421 1422
    if ( pPars->fFraiging && pPars->nPartSize == 0 )
    {
Alan Mishchenko committed
1423
        pNtkFraig = Abc_NtkFraig( pNtk, &Params, 0, 0 );
Alan Mishchenko committed
1424
if ( pPars->fVerbose ) 
Alan Mishchenko committed
1425
{
Alan Mishchenko committed
1426
ABC_PRT( "Initial fraiging time", clock() - clk );
Alan Mishchenko committed
1427
}
Alan Mishchenko committed
1428 1429 1430
    }
    else
        pNtkFraig = Abc_NtkDup( pNtk );
Alan Mishchenko committed
1431

Alan Mishchenko committed
1432
    pMan = Abc_NtkToDar( pNtkFraig, 0, 1 );
Alan Mishchenko committed
1433 1434 1435 1436
    Abc_NtkDelete( pNtkFraig );
    if ( pMan == NULL )
        return NULL;

Alan Mishchenko committed
1437
//    pPars->TimeLimit = 5.0;
Alan Mishchenko committed
1438
    pMan = Fra_FraigInduction( pTemp = pMan, pPars );
Alan Mishchenko committed
1439
    Aig_ManStop( pTemp );
Alan Mishchenko committed
1440
    if ( pMan )
Alan Mishchenko committed
1441
    {
Alan Mishchenko committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
        if ( Aig_ManRegNum(pMan) < Abc_NtkLatchNum(pNtk) )
            pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
        else
        {
            Abc_Obj_t * pObj;
            int i;
            pNtkAig = Abc_NtkFromDar( pNtk, pMan );
            Abc_NtkForEachLatch( pNtkAig, pObj, i )
                Abc_LatchSetInit0( pObj );
        }
        Aig_ManStop( pMan );
Alan Mishchenko committed
1453 1454 1455 1456 1457 1458
    }
    return pNtkAig;
}

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

Alan Mishchenko committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
  Synopsis    [Print Latch Equivalence Classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintLatchEquivClasses( Abc_Ntk_t * pNtk, Aig_Man_t * pAig )
{
1470
    int header_dumped = 0;
Alan Mishchenko committed
1471
    int num_orig_latches = Abc_NtkLatchNum(pNtk);
Alan Mishchenko committed
1472
    char **pNames = ABC_ALLOC( char *, num_orig_latches );
1473
    int *p_irrelevant = ABC_ALLOC( int, num_orig_latches );
Alan Mishchenko committed
1474 1475 1476 1477 1478 1479 1480 1481 1482
    char * pFlopName, * pReprName;
    Aig_Obj_t * pFlop, * pRepr;
    Abc_Obj_t * pNtkFlop; 
    int repr_idx;
    int i;

    Abc_NtkForEachLatch( pNtk, pNtkFlop, i )
    {
        char *temp_name = Abc_ObjName( Abc_ObjFanout0(pNtkFlop) );
Alan Mishchenko committed
1483
        pNames[i] = ABC_ALLOC( char , strlen(temp_name)+1);
Alan Mishchenko committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
        strcpy(pNames[i], temp_name);
    }
    i = 0;
    
    Aig_ManSetPioNumbers( pAig );
    Saig_ManForEachLo( pAig, pFlop, i )
    {
        p_irrelevant[i] = false;
        
        pFlopName = pNames[i];

        pRepr = Aig_ObjRepr(pAig, pFlop);

        if ( pRepr == NULL )
        {
            // printf("Nothing equivalent to flop %s\n", pFlopName);
Alan Mishchenko committed
1500
//            p_irrelevant[i] = true;
Alan Mishchenko committed
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
            continue;
        }

        if (!header_dumped)
        {
            printf("Here are the flop equivalences:\n");
            header_dumped = true;
        }

        // pRepr is representative of the equivalence class, to which pFlop belongs
        if ( Aig_ObjIsConst1(pRepr) )
        {
            printf( "Original flop %s is proved equivalent to constant.\n", pFlopName );
            // printf( "Original flop # %d is proved equivalent to constant.\n", i );
            continue;
        }

        assert( Saig_ObjIsLo( pAig, pRepr ) );
        repr_idx = Aig_ObjPioNum(pRepr) - Saig_ManPiNum(pAig);
        pReprName = pNames[repr_idx];
        printf( "Original flop %s is proved equivalent to flop %s.\n",  pFlopName, pReprName );
        // printf( "Original flop # %d is proved equivalent to flop # %d.\n",  i, repr_idx );
    }

    header_dumped = false;
    for (i=0; i<num_orig_latches; ++i)
    {
        if (p_irrelevant[i])
        {
            if (!header_dumped)
            {
                printf("The following flops have been deemed irrelevant:\n");
                header_dumped = true;
            }
            printf("%s ", pNames[i]);
        }
        
Alan Mishchenko committed
1538
        ABC_FREE(pNames[i]);
Alan Mishchenko committed
1539 1540 1541 1542
    }
    if (header_dumped)
        printf("\n");
    
Alan Mishchenko committed
1543 1544
    ABC_FREE(pNames);
    ABC_FREE(p_irrelevant);
Alan Mishchenko committed
1545 1546 1547 1548
}

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

Alan Mishchenko committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarSeqSweep2( Abc_Ntk_t * pNtk, Ssw_Pars_t * pPars )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;

    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;

    pMan = Ssw_SignalCorrespondence( pTemp = pMan, pPars );
Alan Mishchenko committed
1568 1569 1570 1571

    if ( pPars->fFlopVerbose )
        Abc_NtkPrintLatchEquivClasses(pNtk, pTemp);

1572
    Aig_ManStop( pTemp );
Alan Mishchenko committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
    if ( pMan == NULL )
        return NULL;

    if ( Aig_ManRegNum(pMan) < Abc_NtkLatchNum(pNtk) )
        pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    else
    {
        Abc_Obj_t * pObj;
        int i;
        pNtkAig = Abc_NtkFromDar( pNtk, pMan );
        Abc_NtkForEachLatch( pNtkAig, pObj, i )
            Abc_LatchSetInit0( pObj );
    }
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

Alan Mishchenko committed
1592 1593
  Synopsis    [Computes latch correspondence.]

Alan Mishchenko committed
1594
  Description [] 
Alan Mishchenko committed
1595 1596 1597 1598 1599 1600 1601 1602 1603
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
Abc_Ntk_t * Abc_NtkDarLcorr( Abc_Ntk_t * pNtk, int nFramesP, int nConfMax, int fVerbose )
{
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
1604
    Abc_Ntk_t * pNtkAig = NULL;
Alan Mishchenko committed
1605
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
1606 1607
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
1608
    pMan = Fra_FraigLatchCorrespondence( pTemp = pMan, nFramesP, nConfMax, 0, fVerbose, NULL, 0.0 );
Alan Mishchenko committed
1609
    Aig_ManStop( pTemp );
Alan Mishchenko committed
1610
    if ( pMan )
Alan Mishchenko committed
1611
    {
Alan Mishchenko committed
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
        if ( Aig_ManRegNum(pMan) < Abc_NtkLatchNum(pNtk) )
            pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
        else
        {
            Abc_Obj_t * pObj;
            int i;
            pNtkAig = Abc_NtkFromDar( pNtk, pMan );
            Abc_NtkForEachLatch( pNtkAig, pObj, i )
                Abc_LatchSetInit0( pObj );
        }
        Aig_ManStop( pMan );
Alan Mishchenko committed
1623 1624 1625 1626 1627 1628
    }
    return pNtkAig;
}

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

Alan Mishchenko committed
1629 1630 1631 1632 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 1661 1662 1663 1664 1665 1666 1667 1668 1669
  Synopsis    [Computes latch correspondence.]

  Description [] 
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
Abc_Ntk_t * Abc_NtkDarLcorrNew( Abc_Ntk_t * pNtk, int nVarsMax, int nConfMax, int fVerbose )
{
    Ssw_Pars_t Pars, * pPars = &Pars;
    Aig_Man_t * pMan, * pTemp;
    Abc_Ntk_t * pNtkAig = NULL;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    Ssw_ManSetDefaultParams( pPars );
    pPars->fLatchCorrOpt = 1;
    pPars->nBTLimit      = nConfMax;
    pPars->nSatVarMax    = nVarsMax;
    pPars->fVerbose      = fVerbose;
    pMan = Ssw_SignalCorrespondence( pTemp = pMan, pPars );
    Aig_ManStop( pTemp );
    if ( pMan )
    {
        if ( Aig_ManRegNum(pMan) < Abc_NtkLatchNum(pNtk) )
            pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
        else
        {
            Abc_Obj_t * pObj;
            int i;
            pNtkAig = Abc_NtkFromDar( pNtk, pMan );
            Abc_NtkForEachLatch( pNtkAig, pObj, i )
                Abc_LatchSetInit0( pObj );
        }
        Aig_ManStop( pMan );
    }
    return pNtkAig;
}

Alan Mishchenko committed
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
/*
#include <signal.h>
#include "utilMem.h"
static void sigfunc( int signo ) 
{
    if (signo == SIGINT) {
        printf("SIGINT received!\n");
        s_fInterrupt = 1;
    }
}
*/

Alan Mishchenko committed
1682 1683
/**Function*************************************************************

Alan Mishchenko committed
1684 1685 1686 1687 1688 1689 1690 1691 1692
  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1693
int Abc_NtkDarBmc( Abc_Ntk_t * pNtk, int nStart, int nFrames, int nSizeMax, int nNodeDelta, int nTimeOut, int nBTLimit, int nBTLimitAll, int fRewrite, int fNewAlgo, int nCofFanLit, int fVerbose, int * piFrames )
Alan Mishchenko committed
1694 1695
{
    Aig_Man_t * pMan;
Alan Mishchenko committed
1696
    int status, RetValue = -1, clk = clock();
1697
    int nTimeLimit = nTimeOut ? clock() + nTimeOut * CLOCKS_PER_SEC : 0;
Alan Mishchenko committed
1698

Alan Mishchenko committed
1699
    // derive the AIG manager
Alan Mishchenko committed
1700
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
1701 1702 1703
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
Alan Mishchenko committed
1704
        return RetValue;
Alan Mishchenko committed
1705 1706 1707
    }
    assert( pMan->nRegs > 0 );
    // perform verification
Alan Mishchenko committed
1708
    if ( fNewAlgo )
Alan Mishchenko committed
1709
    {
Alan Mishchenko committed
1710
        int iFrame;
Alan Mishchenko committed
1711
        RetValue = Saig_ManBmcSimple( pMan, nFrames, nSizeMax, nBTLimit, fRewrite, fVerbose, &iFrame, nCofFanLit );
1712 1713
        if ( piFrames )
            *piFrames = iFrame;
Alan Mishchenko committed
1714 1715
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
1716
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
Alan Mishchenko committed
1717
        if ( RetValue == 1 )
1718
        {
1719
//            printf( "No output asserted in %d frames. ", iFrame );
1720 1721
            printf( "Incorrect return value.  " );
        }
Alan Mishchenko committed
1722
        else if ( RetValue == -1 )
1723 1724 1725 1726 1727 1728 1729
        {
            printf( "No output asserted in %d frames. Resource limit reached ", iFrame );
            if ( nTimeLimit < clock() )
                printf( "(timeout %d sec). ", nTimeLimit );
            else
                printf( "(conf limit %d). ", nBTLimit );
        }
Alan Mishchenko committed
1730 1731
        else // if ( RetValue == 0 )
        {
1732
//            extern void Aig_ManCounterExampleValueTest( Aig_Man_t * pAig, Abc_Cex_t * pCex );
Alan Mishchenko committed
1733

1734
            Abc_Cex_t * pCex = pNtk->pSeqModel;
1735
            printf( "Output %d asserted in frame %d (use \"write_counter\" to dump a witness). ", pCex->iPo, pCex->iFrame );
Alan Mishchenko committed
1736

Alan Mishchenko committed
1737
//            Aig_ManCounterExampleValueTest( pMan, pCex );
Alan Mishchenko committed
1738
        }
1739
ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
1740 1741
    }
    else
1742
    { 
Alan Mishchenko committed
1743
/*
Alan Mishchenko committed
1744
        Fra_BmcPerformSimple( pMan, nFrames, nBTLimit, fRewrite, fVerbose );
Alan Mishchenko committed
1745 1746
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
1747 1748 1749
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
        if ( pNtk->pSeqModel )
        {
1750
            Abc_Cex_t * pCex = pNtk->pSeqModel;
1751
            printf( "Output %d asserted in frame %d (use \"write_counter\" to dump a witness). ", pCex->iPo, pCex->iFrame );
Alan Mishchenko committed
1752
            RetValue = 0;
Alan Mishchenko committed
1753 1754
        }
        else
Alan Mishchenko committed
1755
        {
1756
            printf( "No output asserted in %d frames. ", nFrames );
Alan Mishchenko committed
1757 1758
            RetValue = 1;
        }
Alan Mishchenko committed
1759
*/
Alan Mishchenko committed
1760
/*
Alan Mishchenko committed
1761 1762
        int iFrame;
        RetValue = Ssw_BmcDynamic( pMan, nFrames, nBTLimit, fVerbose, &iFrame );
Alan Mishchenko committed
1763 1764
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
1765 1766
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
        if ( RetValue == 1 )
1767
            printf( "No output asserted in %d frames. ", iFrame );
Alan Mishchenko committed
1768
        else if ( RetValue == -1 )
1769
            printf( "No output asserted in %d frames. Reached conflict limit (%d). ", iFrame, nBTLimit );
Alan Mishchenko committed
1770 1771
        else // if ( RetValue == 0 )
        {
1772
            Abc_Cex_t * pCex = pNtk->pSeqModel;
1773
            printf( "Output %d asserted in frame %d (use \"write_counter\" to dump a witness). ", pCex->iPo, pCex->iFrame );
Alan Mishchenko committed
1774
        }
Alan Mishchenko committed
1775
*/
1776
        RetValue = Saig_BmcPerform( pMan, nStart, nFrames, nNodeDelta, nTimeOut, nBTLimit, nBTLimitAll, fVerbose, 0, piFrames );
Alan Mishchenko committed
1777 1778
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
1779
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
Alan Mishchenko committed
1780
    }
Alan Mishchenko committed
1781 1782 1783
    // verify counter-example
    if ( pNtk->pSeqModel ) 
    {
1784
        status = Saig_ManVerifyCex( pMan, pNtk->pSeqModel );
Alan Mishchenko committed
1785 1786 1787
        if ( status == 0 )
            printf( "Abc_NtkDarBmc(): Counter-example verification has FAILED.\n" );
    }
Alan Mishchenko committed
1788
    Aig_ManStop( pMan );
Alan Mishchenko committed
1789
    return RetValue;
Alan Mishchenko committed
1790 1791 1792 1793
}

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

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarBmc3( Abc_Ntk_t * pNtk, Saig_ParBmc_t * pPars )
{
    Aig_Man_t * pMan;
    int status, RetValue = -1, clk = clock();
1807
    int nTimeOut = pPars->nTimeOut ? clock() + pPars->nTimeOut * CLOCKS_PER_SEC : 0;
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return RetValue;
    }
    assert( pMan->nRegs > 0 );
    RetValue = Saig_ManBmcScalable( pMan, pPars );
    ABC_FREE( pNtk->pModel );
    ABC_FREE( pNtk->pSeqModel );
    pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
    if ( RetValue == 1 )
    {
1821
        printf( "Explored all reachable states after completing %d frames.  ", 1<<Aig_ManRegNum(pMan) );
1822 1823 1824 1825
    }
    else if ( RetValue == -1 )
    {
        if ( pPars->nFailOuts == 0 )
1826 1827 1828 1829 1830 1831 1832
        {
            printf( "No output asserted in %d frames. Resource limit reached ", pPars->iFrame );
            if ( nTimeOut < clock() )
                printf( "(timeout %d sec). ", pPars->nTimeOut );
            else
                printf( "(conf limit %d). ", pPars->nConfLimit );
        }
1833 1834
        else
        {
1835 1836 1837 1838 1839
            printf( "The total of %d outputs asserted in %d frames. Resource limit reached ", pPars->nFailOuts, pPars->iFrame );
            if ( nTimeOut < clock() )
                printf( "(timeout %d sec). ", pPars->nTimeOut );
            else
                printf( "(conf limit %d). ", pPars->nConfLimit );
Alan Mishchenko committed
1840 1841 1842
//            if ( pNtk->vSeqModelVec )
//                Vec_PtrFreeFree( pNtk->vSeqModelVec );
//            pNtk->vSeqModelVec = pMan->vSeqModelVec;  pMan->vSeqModelVec = NULL;
1843 1844 1845 1846 1847 1848 1849
        }
    }
    else // if ( RetValue == 0 )
    {
        if ( !pPars->fSolveAll )
        {
            Abc_Cex_t * pCex = pNtk->pSeqModel;
1850
            printf( "Output %d asserted in frame %d (use \"write_counter\" to dump a witness). ", pCex->iPo, pCex->iFrame );
1851 1852 1853
        }
        else
        {
Alan Mishchenko committed
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
            int nOutputs = Saig_ManPoNum(pMan) - Saig_ManConstrNum(pMan);
            if ( Vec_PtrCountZero(pMan->vSeqModelVec) == 0 )
                printf( "All %d outputs are found to be SAT.   ", nOutputs );
            else if ( Vec_PtrCountZero(pMan->vSeqModelVec) == nOutputs )
                printf( "None of the %d outputs is found to be SAT.   ", nOutputs );
            else
                printf( "Some outputs (%d out of %d) are proved SAT.   ", 
                    nOutputs - Vec_PtrCountZero(pMan->vSeqModelVec), nOutputs );
            if ( pNtk->vSeqModelVec )
                Vec_PtrFreeFree( pNtk->vSeqModelVec );
            pNtk->vSeqModelVec = pMan->vSeqModelVec;  pMan->vSeqModelVec = NULL;
1865 1866 1867 1868 1869
        }
    }
    ABC_PRT( "Time", clock() - clk );
    if ( pNtk->pSeqModel ) 
    {
1870
        status = Saig_ManVerifyCex( pMan, pNtk->pSeqModel );
1871 1872 1873 1874 1875 1876 1877 1878 1879
        if ( status == 0 )
            printf( "Abc_NtkDarBmc3(): Counter-example verification has FAILED.\n" );
    }
    Aig_ManStop( pMan );
    return RetValue;
}

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

Alan Mishchenko committed
1880 1881 1882 1883 1884 1885 1886 1887 1888
  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1889
int Abc_NtkDarBmcInter_int( Aig_Man_t * pMan, Inter_ManParams_t * pPars, Aig_Man_t ** ppNtkRes )
Alan Mishchenko committed
1890
{
Alan Mishchenko committed
1891
    int RetValue, iFrame, clk = clock();
1892
    int nTotalProvedSat = 0;
Alan Mishchenko committed
1893
    assert( pMan->nRegs > 0 );
1894 1895
    if ( ppNtkRes )
        *ppNtkRes = NULL;
Alan Mishchenko committed
1896
    if ( pPars->fUseSeparate )
Alan Mishchenko committed
1897
    {
Alan Mishchenko committed
1898 1899 1900 1901 1902 1903 1904
        Aig_Man_t * pTemp, * pAux;
        Aig_Obj_t * pObjPo;
        int i, Counter = 0;
        Saig_ManForEachPo( pMan, pObjPo, i )
        {
            if ( Aig_ObjFanin0(pObjPo) == Aig_ManConst1(pMan) )
                continue;
1905 1906
            if ( pPars->fVerbose )
                printf( "Solving output %2d (out of %2d):\n", i, Saig_ManPoNum(pMan) );
Alan Mishchenko committed
1907
            pTemp = Aig_ManDupOneOutput( pMan, i, 1 );
1908
            pTemp = Aig_ManScl( pAux = pTemp, 1, 1, 0, -1, -1, 0, 0 );
Alan Mishchenko committed
1909
            Aig_ManStop( pAux );
1910 1911 1912 1913 1914
            if ( Aig_ManRegNum(pTemp) == 0 )
            {
                pTemp->pSeqModel = NULL;
                RetValue = Fra_FraigSat( pTemp, pPars->nBTLimit, 0, 0, 0, 0 ); 
                if ( pTemp->pData )
1915
                    pTemp->pSeqModel = Abc_CexCreate( Aig_ManRegNum(pMan), Saig_ManPiNum(pMan), (int *)pTemp->pData, 0, i, 1 );
1916 1917 1918 1919
//                pNtk->pModel = pTemp->pData, pTemp->pData = NULL;
            }
            else
                RetValue = Inter_ManPerformInterpolation( pTemp, pPars, &iFrame );
Alan Mishchenko committed
1920 1921
            if ( pTemp->pSeqModel )
            {
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
                if ( pPars->fDropSatOuts )
                {
                    printf( "Output %d proved SAT in frame %d (replacing by const 0 and continuing...)\n", i, pTemp->pSeqModel->iFrame );
                    Aig_ObjPatchFanin0( pMan, pObjPo, Aig_ManConst0(pMan) );
                    Aig_ManStop( pTemp );
                    nTotalProvedSat++;
                    continue;
                }
                else
                {
                    Abc_Cex_t * pCex;
                    pCex = pMan->pSeqModel = pTemp->pSeqModel; pTemp->pSeqModel = NULL;
                    pCex->iPo = i;
                    Aig_ManStop( pTemp );
                    break;
                }
Alan Mishchenko committed
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
            }
            // if solved, remove the output
            if ( RetValue == 1 )
            {
                Aig_ObjPatchFanin0( pMan, pObjPo, Aig_ManConst0(pMan) );
//                    printf( "Output %3d : Solved ", i );
            }
            else
            {
                Counter++;
//                    printf( "Output %3d : Undec  ", i );
            }
//                Aig_ManPrintStats( pTemp );
            Aig_ManStop( pTemp );
            printf( "Solving output %3d (out of %3d) using interpolation.\r", i, Saig_ManPoNum(pMan) );
        }
        Aig_ManCleanup( pMan );
        if ( pMan->pSeqModel == NULL )
        {
            printf( "Interpolation left %d (out of %d) outputs unsolved              \n", Counter, Saig_ManPoNum(pMan) );
            if ( Counter )
                RetValue = -1;
        }
1961 1962 1963
        if ( ppNtkRes )
        {
            pTemp = Aig_ManDupUnsolvedOutputs( pMan, 1 );
1964
            *ppNtkRes = Aig_ManScl( pTemp, 1, 1, 0, -1, -1, 0, 0 );
1965 1966
            Aig_ManStop( pTemp );
        }
Alan Mishchenko committed
1967 1968 1969 1970
    }
    else
    {    
        RetValue = Inter_ManPerformInterpolation( pMan, pPars, &iFrame );
Alan Mishchenko committed
1971
    }
1972 1973
    if ( nTotalProvedSat )
        printf( "The total of %d outputs proved SAT and replaced by const 0 in this run.\n", nTotalProvedSat );
Alan Mishchenko committed
1974 1975 1976
    if ( RetValue == 1 )
        printf( "Property proved.  " );
    else if ( RetValue == 0 )
Alan Mishchenko committed
1977
        printf( "Property DISPROVED in frame %d (use \"write_counter\" to dump a witness).  ", iFrame );
Alan Mishchenko committed
1978 1979 1980 1981
    else if ( RetValue == -1 )
        printf( "Property UNDECIDED.  " );
    else
        assert( 0 );
Alan Mishchenko committed
1982
ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
    return RetValue;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1997
int Abc_NtkDarBmcInter( Abc_Ntk_t * pNtk, Inter_ManParams_t * pPars, Abc_Ntk_t ** ppNtkRes )
Alan Mishchenko committed
1998 1999
{
    Aig_Man_t * pMan;
2000 2001 2002
    int RetValue;
    if ( ppNtkRes )
        *ppNtkRes = NULL;
Alan Mishchenko committed
2003 2004 2005 2006 2007 2008
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    if ( pPars->fUseSeparate && ppNtkRes )
    {
        Aig_Man_t * pManNew;
        RetValue = Abc_NtkDarBmcInter_int( pMan, pPars, &pManNew );
        *ppNtkRes = Abc_NtkFromAigPhase( pManNew );
        Aig_ManStop( pManNew );
    }
    else
    {
        RetValue = Abc_NtkDarBmcInter_int( pMan, pPars, NULL );
    }
Alan Mishchenko committed
2020 2021 2022
    ABC_FREE( pNtk->pModel );
    ABC_FREE( pNtk->pSeqModel );
    pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
Alan Mishchenko committed
2023
    Aig_ManStop( pMan );
2024
    return RetValue;
Alan Mishchenko committed
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2038 2039
int Abc_NtkDarDemiter( Abc_Ntk_t * pNtk )
{ 
2040
    char * pFileNameGeneric, pFileName0[1000], pFileName1[1000];
Alan Mishchenko committed
2041
    Aig_Man_t * pMan, * pPart0, * pPart1;//, * pMiter;
Alan Mishchenko committed
2042 2043 2044 2045 2046 2047 2048
    // derive the AIG manager
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting network into AIG has failed.\n" );
        return 0;
    }
Alan Mishchenko committed
2049 2050
//    if ( !Saig_ManDemiterSimple( pMan, &pPart0, &pPart1 ) )
    if ( !Saig_ManDemiterSimpleDiff( pMan, &pPart0, &pPart1 ) )
Alan Mishchenko committed
2051 2052 2053 2054
    {
        printf( "Demitering has failed.\n" );
        return 0;
    }
2055 2056 2057 2058 2059 2060 2061 2062 2063
    // create file names
    pFileNameGeneric = Extra_FileNameGeneric( pNtk->pSpec );
    sprintf( pFileName0,  "%s%s",  pFileNameGeneric, "_part0.aig" ); 
    sprintf( pFileName1,  "%s%s",  pFileNameGeneric, "_part1.aig" ); 
    ABC_FREE( pFileNameGeneric );
    // dump files
    Ioa_WriteAiger( pPart0, pFileName0, 0, 0 );
    Ioa_WriteAiger( pPart1, pFileName1, 0, 0 );
    printf( "Demitering produced two files \"%s\" and \"%s\".\n", pFileName0, pFileName1 );
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
    // create two-level miter
//    pMiter = Saig_ManCreateMiterTwo( pPart0, pPart1, 2 );
//    Aig_ManDumpBlif( pMiter, "miter01.blif", NULL, NULL );
//    Aig_ManStop( pMiter );
//    printf( "The new miter is written into file \"%s\".\n", "miter01.blif" );
    Aig_ManStop( pPart0 );
    Aig_ManStop( pPart1 );
    Aig_ManStop( pMan );
    return 1;
} 

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarDemiterNew( Abc_Ntk_t * pNtk )
{ 
    char * pFileNameGeneric, pFileName0[1000], pFileName1[1000];
    Aig_Man_t * pMan, * pPart0, * pPart1;//, * pMiter;
    // derive the AIG manager
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting network into AIG has failed.\n" );
        return 0;
    }

    Saig_ManDemiterNew( pMan );
    Aig_ManStop( pMan );
    return 1;

//    if ( !Saig_ManDemiterSimple( pMan, &pPart0, &pPart1 ) )
    if ( !Saig_ManDemiterSimpleDiff( pMan, &pPart0, &pPart1 ) )
    {
        printf( "Demitering has failed.\n" );
        return 0;
    }
    // create file names
    pFileNameGeneric = Extra_FileNameGeneric( pNtk->pSpec );
    sprintf( pFileName0,  "%s%s",  pFileNameGeneric, "_part0.aig" ); 
    sprintf( pFileName1,  "%s%s",  pFileNameGeneric, "_part1.aig" ); 
    ABC_FREE( pFileNameGeneric );
    // dump files
    Ioa_WriteAiger( pPart0, pFileName0, 0, 0 );
    Ioa_WriteAiger( pPart1, pFileName1, 0, 0 );
    printf( "Demitering produced two files \"%s\" and \"%s\".\n", pFileName0, pFileName1 );
Alan Mishchenko committed
2117
    // create two-level miter
Alan Mishchenko committed
2118 2119 2120 2121
//    pMiter = Saig_ManCreateMiterTwo( pPart0, pPart1, 2 );
//    Aig_ManDumpBlif( pMiter, "miter01.blif", NULL, NULL );
//    Aig_ManStop( pMiter );
//    printf( "The new miter is written into file \"%s\".\n", "miter01.blif" );
Alan Mishchenko committed
2122 2123 2124 2125
    Aig_ManStop( pPart0 );
    Aig_ManStop( pPart1 );
    Aig_ManStop( pMan );
    return 1;
Alan Mishchenko committed
2126
} 
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarDemiterDual( Abc_Ntk_t * pNtk, int fVerbose )
{ 
    char * pFileNameGeneric, pFileName0[1000], pFileName1[1000];
    Aig_Man_t * pMan, * pPart0, * pPart1;//, * pMiter;
    if ( (Abc_NtkPoNum(pNtk) & 1) )
    {
        printf( "The number of POs should be even.\n" );
        return 0;
    }
    // derive the AIG manager
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting network into AIG has failed.\n" );
        return 0;
    }
//    if ( !Saig_ManDemiterSimple( pMan, &pPart0, &pPart1 ) )
    if ( !Saig_ManDemiterDual( pMan, &pPart0, &pPart1 ) )
    {
        printf( "Demitering has failed.\n" );
        return 0;
    }
    // create new AIG
    ABC_FREE( pPart0->pName );
    pPart0->pName = Aig_UtilStrsav( "part0" );
    // create new AIGs
    ABC_FREE( pPart1->pName );
    pPart1->pName = Aig_UtilStrsav( "part1" );
    // create file names
    pFileNameGeneric = Extra_FileNameGeneric( pNtk->pSpec );
    sprintf( pFileName0,  "%s%s",  pFileNameGeneric, "_part0.aig" ); 
    sprintf( pFileName1,  "%s%s",  pFileNameGeneric, "_part1.aig" ); 
    ABC_FREE( pFileNameGeneric );
    Ioa_WriteAiger( pPart0, pFileName0, 0, 0 );
    Ioa_WriteAiger( pPart1, pFileName1, 0, 0 );
    printf( "Demitering produced two files \"%s\" and \"%s\".\n", pFileName0, pFileName1 );
    // dump files
    if ( fVerbose )
    {
//        printf( "Init:  " );
        Aig_ManPrintStats( pMan );
//        printf( "Part1: " );
        Aig_ManPrintStats( pPart0 );
//        printf( "Part2: " );
        Aig_ManPrintStats( pPart1 );
    }
    // create two-level miter
//    pMiter = Saig_ManCreateMiterTwo( pPart0, pPart1, 2 );
//    Aig_ManDumpBlif( pMiter, "miter01.blif", NULL, NULL );
//    Aig_ManStop( pMiter );
//    printf( "The new miter is written into file \"%s\".\n", "miter01.blif" );
    Aig_ManStop( pPart0 );
    Aig_ManStop( pPart1 );
    Aig_ManStop( pMan );
    return 1;
} 
Alan Mishchenko committed
2195
 
Alan Mishchenko committed
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2207
int Abc_NtkDarProve( Abc_Ntk_t * pNtk, Fra_Sec_t * pSecPar )
Alan Mishchenko committed
2208 2209
{
    Aig_Man_t * pMan;
2210
    int RetValue = -1, clkTotal = clock();
Alan Mishchenko committed
2211
    if ( pSecPar->fTryComb || Abc_NtkLatchNum(pNtk) == 0 )
Alan Mishchenko committed
2212 2213 2214 2215
    {
        Prove_Params_t Params, * pParams = &Params;
        Abc_Ntk_t * pNtkComb;
        int RetValue, clk = clock();
Alan Mishchenko committed
2216 2217
        if ( Abc_NtkLatchNum(pNtk) == 0 )
            printf( "The network has no latches. Running CEC.\n" );
Alan Mishchenko committed
2218 2219 2220 2221 2222
        // create combinational network
        pNtkComb = Abc_NtkDup( pNtk );
        Abc_NtkMakeComb( pNtkComb, 1 );
        // solve it using combinational equivalence checking
        Prove_ParamsSetDefault( pParams );
Alan Mishchenko committed
2223
        pParams->fVerbose = 1;
Alan Mishchenko committed
2224 2225
        RetValue = Abc_NtkIvyProve( &pNtkComb, pParams );
        // transfer model if given
Alan Mishchenko committed
2226
//        pNtk->pModel = pNtkComb->pModel; pNtkComb->pModel = NULL;
Alan Mishchenko committed
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
        if ( RetValue == 0  && (Abc_NtkLatchNum(pNtk) == 0) )
        {
            pNtk->pModel = pNtkComb->pModel; pNtkComb->pModel = NULL;
            printf( "Networks are not equivalent.\n" );
            ABC_PRT( "Time", clock() - clk );
            if ( pSecPar->fReportSolution )
            {
                printf( "SOLUTION: FAIL       " );
                ABC_PRT( "Time", clock() - clkTotal );
            }
            return RetValue;
        }
Alan Mishchenko committed
2239 2240 2241 2242 2243
        Abc_NtkDelete( pNtkComb );
        // return the result, if solved
        if ( RetValue == 1 )
        {
            printf( "Networks are equivalent after CEC.   " );
Alan Mishchenko committed
2244
            ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
2245 2246 2247
            if ( pSecPar->fReportSolution )
            {
            printf( "SOLUTION: PASS       " );
Alan Mishchenko committed
2248
            ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
2249
            }
Alan Mishchenko committed
2250 2251 2252
            return RetValue;
        }
    }
Alan Mishchenko committed
2253
    if ( pSecPar->fTryBmc )
Alan Mishchenko committed
2254
    {
2255
        RetValue = Abc_NtkDarBmc( pNtk, 0, 20, 100000, -1, 0, 2000, -1, 0, 1, 0, 0, NULL );
Alan Mishchenko committed
2256
        if ( RetValue == 0 )
Alan Mishchenko committed
2257 2258 2259 2260 2261
        {
            printf( "Networks are not equivalent.\n" );
            if ( pSecPar->fReportSolution )
            {
            printf( "SOLUTION: FAIL       " );
Alan Mishchenko committed
2262
            ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
2263
            }
Alan Mishchenko committed
2264
            return RetValue;
Alan Mishchenko committed
2265
        }
Alan Mishchenko committed
2266
    } 
Alan Mishchenko committed
2267
    // derive the AIG manager
Alan Mishchenko committed
2268
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2269 2270 2271 2272 2273 2274 2275
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
    assert( pMan->nRegs > 0 );
    // perform verification
Alan Mishchenko committed
2276
    if ( pSecPar->fUseNewProver )
Alan Mishchenko committed
2277
    {
Alan Mishchenko committed
2278 2279 2280 2281
        RetValue = Ssw_SecGeneralMiter( pMan, NULL );
    }
    else
    {
Alan Mishchenko committed
2282
        RetValue = Fra_FraigSec( pMan, pSecPar, NULL );
Alan Mishchenko committed
2283 2284
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
2285 2286 2287
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
        if ( pNtk->pSeqModel )
        {
2288
            Abc_Cex_t * pCex = pNtk->pSeqModel;
2289
            printf( "Output %d asserted in frame %d (use \"write_counter\" to dump a witness).\n", pCex->iPo, pCex->iFrame );
2290
            if ( !Saig_ManVerifyCex( pMan, pNtk->pSeqModel ) )
Alan Mishchenko committed
2291
                printf( "Abc_NtkDarProve(): Counter-example verification has FAILED.\n" );
Alan Mishchenko committed
2292
        }
Alan Mishchenko committed
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
    }
    Aig_ManStop( pMan );
    return RetValue;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2309
int Abc_NtkDarSec( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, Fra_Sec_t * pSecPar )
Alan Mishchenko committed
2310 2311 2312
{
//    Fraig_Params_t Params;
    Aig_Man_t * pMan;
Alan Mishchenko committed
2313
    Abc_Ntk_t * pMiter;//, * pTemp;
Alan Mishchenko committed
2314
    int RetValue;
Alan Mishchenko committed
2315
 
Alan Mishchenko committed
2316
    // get the miter of the two networks
Alan Mishchenko committed
2317
    pMiter = Abc_NtkMiter( pNtk1, pNtk2, 0, 0, 0, 0 );
Alan Mishchenko committed
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
    if ( pMiter == NULL )
    {
        printf( "Miter computation has failed.\n" );
        return 0;
    }
    RetValue = Abc_NtkMiterIsConstant( pMiter );
    if ( RetValue == 0 )
    {
        extern void Abc_NtkVerifyReportErrorSeq( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int * pModel, int nFrames );
        printf( "Networks are NOT EQUIVALENT after structural hashing.\n" );
        // report the error
Alan Mishchenko committed
2329
        pMiter->pModel = Abc_NtkVerifyGetCleanModel( pMiter, pSecPar->nFramesMax );
2330
//        Abc_NtkVerifyReportErrorSeq( pNtk1, pNtk2, pMiter->pModel, pSecPar->nFramesMax );
Alan Mishchenko committed
2331
        ABC_FREE( pMiter->pModel );
Alan Mishchenko committed
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
        Abc_NtkDelete( pMiter );
        return 0;
    }
    if ( RetValue == 1 )
    {
        Abc_NtkDelete( pMiter );
        printf( "Networks are equivalent after structural hashing.\n" );
        return 1;
    }

Alan Mishchenko committed
2342
    // commented out because sometimes the problem became non-inductive
Alan Mishchenko committed
2343
/*
Alan Mishchenko committed
2344 2345 2346 2347
    // preprocess the miter by fraiging it
    // (note that for each functional class, fraiging leaves one representative;
    // so fraiging does not reduce the number of functions represented by nodes
    Fraig_ParamsSetDefault( &Params );
Alan Mishchenko committed
2348
    Params.nBTLimit = 100000;
Alan Mishchenko committed
2349 2350
    pMiter = Abc_NtkFraig( pTemp = pMiter, &Params, 0, 0 );
    Abc_NtkDelete( pTemp );
Alan Mishchenko committed
2351 2352 2353 2354 2355 2356 2357 2358
    RetValue = Abc_NtkMiterIsConstant( pMiter );
    if ( RetValue == 0 )
    {
        extern void Abc_NtkVerifyReportErrorSeq( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int * pModel, int nFrames );
        printf( "Networks are NOT EQUIVALENT after structural hashing.\n" );
        // report the error
        pMiter->pModel = Abc_NtkVerifyGetCleanModel( pMiter, nFrames );
        Abc_NtkVerifyReportErrorSeq( pNtk1, pNtk2, pMiter->pModel, nFrames );
Alan Mishchenko committed
2359
        ABC_FREE( pMiter->pModel );
Alan Mishchenko committed
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
        Abc_NtkDelete( pMiter );
        return 0;
    }
    if ( RetValue == 1 )
    {
        Abc_NtkDelete( pMiter );
        printf( "Networks are equivalent after structural hashing.\n" );
        return 1;
    }
*/
Alan Mishchenko committed
2370
    // derive the AIG manager
Alan Mishchenko committed
2371
    pMan = Abc_NtkToDar( pMiter, 0, 1 );
Alan Mishchenko committed
2372 2373 2374 2375 2376 2377 2378 2379 2380
    Abc_NtkDelete( pMiter );
    if ( pMan == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
    assert( pMan->nRegs > 0 );

    // perform verification
Alan Mishchenko committed
2381
    RetValue = Fra_FraigSec( pMan, pSecPar, NULL );
Alan Mishchenko committed
2382 2383 2384 2385
    Aig_ManStop( pMan );
    return RetValue;
}

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2397
int Abc_NtkDarPdr( Abc_Ntk_t * pNtk, Pdr_Par_t * pPars, Abc_Cex_t ** ppCex )
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
{
    int RetValue = -1, clk = clock();
    Aig_Man_t * pMan;
    *ppCex = NULL;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
    {
        printf( "Converting network into AIG has failed.\n" );
        return -1;
    }
2408 2409 2410 2411 2412 2413
    // perform ORing the primary outputs
    if ( pPars->iOutput == -1 )
    {
        Aig_Man_t * pTemp = Saig_ManDupOrpos( pMan );
        RetValue = Pdr_ManSolve( pTemp, pPars, ppCex );
        if ( RetValue == 0 )
2414
            (*ppCex)->iPo = Saig_ManFindFailedPoCex( pMan, *ppCex );
2415 2416 2417 2418 2419
        Aig_ManStop( pTemp );
    }
    else
        RetValue = Pdr_ManSolve( pMan, pPars, ppCex );
    // output the result
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
    if ( RetValue == 1 )
        printf( "Property proved.  " );
    else if ( RetValue == 0 )
        printf( "Property DISPROVED in frame %d (use \"write_counter\" to dump a witness).  ", ppCex? (*ppCex)->iFrame : -1 );
    else if ( RetValue == -1 )
        printf( "Property UNDECIDED.  " );
    else
        assert( 0 );
ABC_PRT( "Time", clock() - clk );

2430
    if ( *ppCex && !Saig_ManVerifyCex( pMan, *ppCex ) )
2431 2432 2433 2434
        printf( "Abc_NtkDarPdr(): Counter-example verification has FAILED.\n" );
    Aig_ManStop( pMan );
    return RetValue;
}
Alan Mishchenko committed
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarAbSec( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int nFrames, int fVerbose )
{
    Aig_Man_t * pMan1, * pMan2 = NULL;
    int RetValue;
    // derive AIG manager
    pMan1 = Abc_NtkToDar( pNtk1, 0, 1 );
    if ( pMan1 == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
    assert( Aig_ManRegNum(pMan1) > 0 );
    // derive AIG manager
    if ( pNtk2 )
    {
        pMan2 = Abc_NtkToDar( pNtk2, 0, 1 );
        if ( pMan2 == NULL )
        {
2465
            Aig_ManStop( pMan1 );
Alan Mishchenko committed
2466 2467 2468 2469
            printf( "Converting miter into AIG has failed.\n" );
            return -1;
        }
        assert( Aig_ManRegNum(pMan2) > 0 );
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
        if ( Saig_ManPiNum(pMan1) != Saig_ManPiNum(pMan2) )
        {
            Aig_ManStop( pMan1 );
            Aig_ManStop( pMan2 );
            printf( "The networks have different number of PIs.\n" );
            return -1;
        }
        if ( Saig_ManPoNum(pMan1) != Saig_ManPoNum(pMan2) )
        {
            Aig_ManStop( pMan1 );
            Aig_ManStop( pMan2 );
            printf( "The networks have different number of POs.\n" );
            return -1;
        }
        if ( Aig_ManRegNum(pMan1) != Aig_ManRegNum(pMan2) )
        {
            Aig_ManStop( pMan1 );
            Aig_ManStop( pMan2 );
            printf( "The networks have different number of flops.\n" );
            return -1;
        }
Alan Mishchenko committed
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
    }
    // perform verification
    RetValue = Ssw_SecSpecialMiter( pMan1, pMan2, nFrames, fVerbose );
    Aig_ManStop( pMan1 );
    if ( pMan2 )
        Aig_ManStop( pMan2 );
    return RetValue;
}


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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarSimSec( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, Ssw_Pars_t * pPars )
{
    Aig_Man_t * pMan1, * pMan2 = NULL;
    int RetValue;
    // derive AIG manager
    pMan1 = Abc_NtkToDar( pNtk1, 0, 1 );
    if ( pMan1 == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return -1;
    }
    assert( Aig_ManRegNum(pMan1) > 0 );
    // derive AIG manager
    if ( pNtk2 )
    {
        pMan2 = Abc_NtkToDar( pNtk2, 0, 1 );
        if ( pMan2 == NULL )
        {
            printf( "Converting miter into AIG has failed.\n" );
            return -1;
        }
        assert( Aig_ManRegNum(pMan2) > 0 );
    }

    // perform verification
    RetValue = Ssw_SecWithSimilarity( pMan1, pMan2, pPars );
    Aig_ManStop( pMan1 );
    if ( pMan2 )
        Aig_ManStop( pMan2 );
    return RetValue;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarMatch( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int nDist, int fVerbose )
{
    extern Vec_Int_t * Saig_StrSimPerformMatching( Aig_Man_t * p0, Aig_Man_t * p1, int nDist, int fVerbose, Aig_Man_t ** ppMiter );
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan1, * pMan2 = NULL, * pManRes;
    Vec_Int_t * vPairs;
    assert( Abc_NtkIsStrash(pNtk1) );
    // derive AIG manager
    pMan1 = Abc_NtkToDar( pNtk1, 0, 1 );
    if ( pMan1 == NULL )
    {
        printf( "Converting miter into AIG has failed.\n" );
        return NULL;
    }
    assert( Aig_ManRegNum(pMan1) > 0 );
    // derive AIG manager
    if ( pNtk2 )
    {
        pMan2 = Abc_NtkToDar( pNtk2, 0, 1 );
        if ( pMan2 == NULL )
        {
            printf( "Converting miter into AIG has failed.\n" );
            return NULL;
        }
        assert( Aig_ManRegNum(pMan2) > 0 );
    }

    // perform verification
    vPairs = Saig_StrSimPerformMatching( pMan1, pMan2, nDist, 1, &pManRes );
    pNtkAig = Abc_NtkFromAigPhase( pManRes );
    if ( vPairs )
        Vec_IntFree( vPairs );
    if ( pManRes )
        Aig_ManStop( pManRes );
    Aig_ManStop( pMan1 );
    if ( pMan2 )
        Aig_ManStop( pMan2 );
    return pNtkAig;
}


Alan Mishchenko committed
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
/**Function*************************************************************

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2607
Abc_Ntk_t * Abc_NtkDarLatchSweep( Abc_Ntk_t * pNtk, int fLatchConst, int fLatchEqual, int fSaveNames, int fUseMvSweep, int nFramesSymb, int nFramesSatur, int fVerbose, int fVeryVerbose )
Alan Mishchenko committed
2608
{
Alan Mishchenko committed
2609
    extern void Aig_ManPrintControlFanouts( Aig_Man_t * p );
Alan Mishchenko committed
2610
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
2611
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
2612
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2613 2614
    if ( pMan == NULL )
        return NULL;
2615 2616 2617 2618
    if ( fSaveNames )
    {
        Aig_ManSeqCleanup( pMan );
        if ( fLatchConst && pMan->nRegs )
2619
            pMan = Aig_ManConstReduce( pMan, fUseMvSweep, nFramesSymb, nFramesSatur, fVerbose, fVeryVerbose );
2620 2621 2622 2623 2624 2625 2626 2627
        if ( fLatchEqual && pMan->nRegs )
            pMan = Aig_ManReduceLaches( pMan, fVerbose );
    }
    else
    {
        if ( pMan->vFlopNums )
            Vec_IntFree( pMan->vFlopNums );
        pMan->vFlopNums = NULL;
2628
        pMan = Aig_ManScl( pTemp = pMan, fLatchConst, fLatchEqual, fUseMvSweep, nFramesSymb, nFramesSatur, fVerbose, fVeryVerbose );
2629 2630
        Aig_ManStop( pTemp );
    }
Alan Mishchenko committed
2631

Alan Mishchenko committed
2632
    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
Alan Mishchenko committed
2633
//Aig_ManPrintControlFanouts( pMan );
Alan Mishchenko committed
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarRetime( Abc_Ntk_t * pNtk, int nStepsMax, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
2653
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2654 2655 2656 2657 2658 2659 2660
    if ( pMan == NULL )
        return NULL;
//    Aig_ManReduceLachesCount( pMan );
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

Alan Mishchenko committed
2661
    pMan = Rtm_ManRetime( pTemp = pMan, 1, nStepsMax, fVerbose );
Alan Mishchenko committed
2662 2663 2664
    Aig_ManStop( pTemp );

//    pMan = Aig_ManReduceLaches( pMan, 1 );
2665
//    pMan = Aig_ManConstReduce( pMan, 1, 0 );
Alan Mishchenko committed
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682

    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2683
Abc_Ntk_t * Abc_NtkDarRetimeF( Abc_Ntk_t * pNtk, int nStepsMax, int fVerbose )
Alan Mishchenko committed
2684 2685 2686 2687 2688 2689
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
2690
//    Aig_ManReduceLachesCount( pMan );
Alan Mishchenko committed
2691 2692 2693 2694
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

Alan Mishchenko committed
2695
    pMan = Aig_ManRetimeFrontier( pTemp = pMan, nStepsMax );
Alan Mishchenko committed
2696 2697
    Aig_ManStop( pTemp );

Alan Mishchenko committed
2698
//    pMan = Aig_ManReduceLaches( pMan, 1 );
2699
//    pMan = Aig_ManConstReduce( pMan, 1, 0 );
Alan Mishchenko committed
2700

Alan Mishchenko committed
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2717
Abc_Ntk_t * Abc_NtkDarRetimeMostFwd( Abc_Ntk_t * pNtk, int nMaxIters, int fVerbose )
Alan Mishchenko committed
2718
{
Alan Mishchenko committed
2719 2720
    extern Aig_Man_t * Saig_ManRetimeForward( Aig_Man_t * p, int nIters, int fVerbose );

Alan Mishchenko committed
2721 2722
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
2723
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2724 2725 2726 2727 2728 2729 2730
    if ( pMan == NULL )
        return NULL;
//    Aig_ManReduceLachesCount( pMan );
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

Alan Mishchenko committed
2731
    pMan = Saig_ManRetimeForward( pTemp = pMan, nMaxIters, fVerbose );
Alan Mishchenko committed
2732 2733 2734
    Aig_ManStop( pTemp );

//    pMan = Aig_ManReduceLaches( pMan, 1 );
2735
//    pMan = Aig_ManConstReduce( pMan, 1, 0 );
Alan Mishchenko committed
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752

    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
Abc_Ntk_t * Abc_NtkDarRetimeMinArea( Abc_Ntk_t * pNtk, int nMaxIters, int fForwardOnly, int fBackwardOnly, int fInitial, int fVerbose )
{
    extern Aig_Man_t * Saig_ManRetimeMinArea( Aig_Man_t * p, int nMaxIters, int fForwardOnly, int fBackwardOnly, int fInitial, int fVerbose );
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

    pMan = Saig_ManRetimeMinArea( pTemp = pMan, nMaxIters, fForwardOnly, fBackwardOnly, fInitial, fVerbose );
    Aig_ManStop( pTemp );

    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
Abc_Ntk_t * Abc_NtkDarRetimeStep( Abc_Ntk_t * pNtk, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

    Aig_ManPrintStats(pMan);
Alan Mishchenko committed
2797
    Saig_ManRetimeSteps( pMan, 1000, 1, 0 );
Alan Mishchenko committed
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
    Aig_ManPrintStats(pMan);

    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2816
/*
Alan Mishchenko committed
2817
Abc_Ntk_t * Abc_NtkDarHaigRecord( Abc_Ntk_t * pNtk, int nIters, int nSteps, int fRetimingOnly, int fAddBugs, int fUseCnf, int fVerbose )
Alan Mishchenko committed
2818
{
Alan Mishchenko committed
2819 2820
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
2821
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2822
    if ( pMan == NULL )
Alan Mishchenko committed
2823
        return NULL;
Alan Mishchenko committed
2824 2825 2826
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;
Alan Mishchenko committed
2827

Alan Mishchenko committed
2828 2829 2830 2831
    pMan = Saig_ManHaigRecord( pTemp = pMan, nIters, nSteps, fRetimingOnly, fAddBugs, fUseCnf, fVerbose );
    Aig_ManStop( pTemp );

    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
Alan Mishchenko committed
2832
    Aig_ManStop( pMan );
Alan Mishchenko committed
2833
    return pNtkAig;
Alan Mishchenko committed
2834
}
Alan Mishchenko committed
2835
*/
Alan Mishchenko committed
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847

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

  Synopsis    [Performs random simulation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2848
int Abc_NtkDarSeqSim( Abc_Ntk_t * pNtk, int nFrames, int nWords, int TimeOut, int fNew, int fComb, int fMiter, int fVerbose )
Alan Mishchenko committed
2849
{
Alan Mishchenko committed
2850 2851
    extern int Cec_ManSimulate( Aig_Man_t * pAig, int nWords, int nIters, int TimeLimit, int fMiter, int fVerbose );
    extern int Raig_ManSimulate( Aig_Man_t * pAig, int nWords, int nIters, int TimeLimit, int fMiter, int fVerbose );
Alan Mishchenko committed
2852
    Aig_Man_t * pMan;
2853 2854
    Abc_Cex_t * pCex;
    int status, RetValue = -1, clk = clock();
Alan Mishchenko committed
2855 2856 2857
    if ( Abc_NtkGetChoiceNum(pNtk) )
    {
        printf( "Removing %d choices from the AIG.\n", Abc_NtkGetChoiceNum(pNtk) );
2858
        Abc_AigCleanup((Abc_Aig_t *)pNtk->pManFunc);
Alan Mishchenko committed
2859
    }
Alan Mishchenko committed
2860
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
2861
    if ( fComb || Abc_NtkLatchNum(pNtk) == 0 )
Alan Mishchenko committed
2862
    {
Alan Mishchenko committed
2863
/*
Alan Mishchenko committed
2864 2865 2866 2867 2868 2869
        if ( Cec_ManSimulate( pMan, nWords, nFrames, TimeOut, fMiter, fVerbose ) )
        {
            pCex = pMan->pSeqModel;
            if ( pCex )
            {
            printf( "Simulation iterated %d times with %d words asserted output %d in frame %d. ", 
Alan Mishchenko committed
2870
                nFrames, nWords, pCex->iPo, pCex->iFrame );
2871
            status = Saig_ManVerifyCex( pMan, pCex );
Alan Mishchenko committed
2872 2873 2874
            if ( status == 0 )
                printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
Alan Mishchenko committed
2875 2876
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
2877 2878
            pNtk->pSeqModel = pCex;
            RetValue = 1;
Alan Mishchenko committed
2879
        } 
Alan Mishchenko committed
2880 2881 2882 2883 2884 2885
        else
        {
            RetValue = 0;
            printf( "Simulation iterated %d times with %d words did not assert the outputs. ", 
                nFrames, nWords );
        }
Alan Mishchenko committed
2886 2887
*/
        printf( "Comb simulation is temporarily disabled.\n" );
Alan Mishchenko committed
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
    }
    else if ( fNew )
    {
/*
        if ( Raig_ManSimulate( pMan, nWords, nFrames, TimeOut, fVerbose ) )
        {
            if ( (pCex = pMan->pSeqModel) )
            {
                printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
                    nFrames, nWords, pCex->iPo, pCex->iFrame );
2898
                status = Saig_ManVerifyCex( pMan, pCex );
Alan Mishchenko committed
2899 2900 2901
                if ( status == 0 )
                    printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
Alan Mishchenko committed
2902 2903
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
            pNtk->pSeqModel = pCex; pMan->pSeqModel = NULL;
            RetValue = 1;
        }
        else
        {
            RetValue = 0;
            printf( "Simulation of %d frames with %d words did not assert the outputs. ", 
                nFrames, nWords );
        }
*/
Alan Mishchenko committed
2914
/*
Alan Mishchenko committed
2915 2916 2917 2918 2919 2920 2921 2922
        Fsim_ParSim_t Pars, * pPars = &Pars;
        Fsim_ManSetDefaultParamsSim( pPars );
        pPars->nWords = nWords;
        pPars->nIters = nFrames;
        pPars->TimeLimit = TimeOut;
        pPars->fCheckMiter = fMiter;
        pPars->fVerbose = fVerbose;
        if ( Fsim_ManSimulate( pMan, pPars ) )
Alan Mishchenko committed
2923 2924 2925 2926 2927
        { 
            if ( (pCex = pMan->pSeqModel) )
            {
                printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
                    nFrames, nWords, pCex->iPo, pCex->iFrame );
2928
                status = Saig_ManVerifyCex( pMan, pCex );
Alan Mishchenko committed
2929 2930 2931 2932 2933 2934 2935 2936 2937
                if ( status == 0 )
                    printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
            pNtk->pSeqModel = pCex; pMan->pSeqModel = NULL;
            RetValue = 1;
        }
        else
Alan Mishchenko committed
2938
        {
Alan Mishchenko committed
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
            RetValue = 0;
            printf( "Simulation of %d frames with %d words did not assert the outputs. ", 
                nFrames, nWords );
        }
*/ 
        Gia_Man_t * pGia;
        Gia_ParSim_t Pars, * pPars = &Pars;
        Gia_ManSimSetDefaultParams( pPars );
        pPars->nWords = nWords;
        pPars->nIters = nFrames;
        pPars->TimeLimit = TimeOut;
        pPars->fCheckMiter = fMiter;
        pPars->fVerbose = fVerbose;
        pGia = Gia_ManFromAig( pMan );
        if ( Gia_ManSimSimulate( pGia, pPars ) )
        { 
2955
            if ( pGia->pCexSeq )
Alan Mishchenko committed
2956 2957
            {
                printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
2958
                    nFrames, nWords, pGia->pCexSeq->iPo, pGia->pCexSeq->iFrame );
2959
                status = Saig_ManVerifyCex( pMan, pGia->pCexSeq );
Alan Mishchenko committed
2960 2961 2962
                if ( status == 0 )
                    printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
Alan Mishchenko committed
2963 2964
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
2965 2966
            pNtk->pSeqModel = pGia->pCexSeq; pGia->pCexSeq = NULL;
            RetValue = 0;
Alan Mishchenko committed
2967 2968 2969
        }
        else
        {
Alan Mishchenko committed
2970
            printf( "Simulation of %d frames with %d words did not assert the outputs.    ", 
Alan Mishchenko committed
2971 2972
                nFrames, nWords );
        }
Alan Mishchenko committed
2973
        Gia_ManStop( pGia );
Alan Mishchenko committed
2974 2975 2976
    }
    else
    {
Alan Mishchenko committed
2977
        Fra_Sml_t * pSml;
Alan Mishchenko committed
2978
        pSml = Fra_SmlSimulateSeq( pMan, 0, nFrames, nWords, fMiter );
Alan Mishchenko committed
2979 2980 2981 2982
        if ( pSml->fNonConstOut )
        {
            pCex = Fra_SmlGetCounterExample( pSml );
            if ( pCex )
Alan Mishchenko committed
2983
            {
Alan Mishchenko committed
2984 2985
                printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
                    nFrames, nWords, pCex->iPo, pCex->iFrame );
2986
                status = Saig_ManVerifyCex( pMan, pCex );
Alan Mishchenko committed
2987 2988 2989
                if ( status == 0 )
                    printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
Alan Mishchenko committed
2990 2991
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
2992
            pNtk->pSeqModel = pCex;
2993
            RetValue = 0;
Alan Mishchenko committed
2994 2995 2996
        }
        else
        {
Alan Mishchenko committed
2997
            printf( "Simulation of %d frames with %d words did not assert the outputs.    ", 
Alan Mishchenko committed
2998 2999 3000
                nFrames, nWords );
        }
        Fra_SmlStop( pSml );
Alan Mishchenko committed
3001
/*
Alan Mishchenko committed
3002 3003 3004 3005 3006 3007
        if ( Raig_ManSimulate( pMan, nWords, nFrames, TimeOut, fMiter, fVerbose ) )
        {
            if ( (pCex = pMan->pSeqModel) )
            {
                printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
                    nFrames, nWords, pCex->iPo, pCex->iFrame );
3008
                status = Saig_ManVerifyCex( pMan, pCex );
Alan Mishchenko committed
3009 3010 3011
                if ( status == 0 )
                    printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
            }
Alan Mishchenko committed
3012 3013
            ABC_FREE( pNtk->pModel );
            ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
3014 3015 3016 3017 3018 3019 3020 3021 3022
            pNtk->pSeqModel = pCex; pMan->pSeqModel = NULL;
            RetValue = 1;
        }
        else
        {
            RetValue = 0;
            printf( "Simulation of %d frames with %d words did not assert the outputs. ", 
                nFrames, nWords );
        }
Alan Mishchenko committed
3023
*/
Alan Mishchenko committed
3024
    }
Alan Mishchenko committed
3025
    ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
3026 3027 3028 3029 3030 3031
    Aig_ManStop( pMan );
    return RetValue;
}

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

3032 3033 3034 3035 3036 3037 3038 3039 3040
  Synopsis    [Performs random simulation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3041
int Abc_NtkDarSeqSim3( Abc_Ntk_t * pNtk, int nFrames, int nWords, int nBinSize, int nRounds, int nRandSeed, int TimeOut, int fVerbose )
3042 3043 3044 3045 3046 3047 3048 3049 3050
{
    Aig_Man_t * pMan;
    int status, RetValue = -1, clk = clock();
    if ( Abc_NtkGetChoiceNum(pNtk) )
    {
        printf( "Removing %d choices from the AIG.\n", Abc_NtkGetChoiceNum(pNtk) );
        Abc_AigCleanup((Abc_Aig_t *)pNtk->pManFunc);
    }
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
3051
    if ( Ssw_RarSimulate( pMan, nFrames, nWords, nBinSize, nRounds, nRandSeed, TimeOut, fVerbose ) == 0 )
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
    { 
        if ( pMan->pSeqModel )
        {
            printf( "Simulation of %d frames with %d words asserted output %d in frame %d. ", 
                nFrames, nWords, pMan->pSeqModel->iPo, pMan->pSeqModel->iFrame );
            status = Saig_ManVerifyCex( pMan, pMan->pSeqModel );
            if ( status == 0 )
                printf( "Abc_NtkDarSeqSim(): Counter-example verification has FAILED.\n" );
        }
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
        RetValue = 0;
    }
    else
    {
3068 3069
//        printf( "Simulation of %d frames with %d words did not assert the outputs.    ", 
//            nFrames, nWords );
3070 3071 3072 3073 3074 3075 3076 3077
    }
    ABC_PRT( "Time", clock() - clk );
    Aig_ManStop( pMan );
    return RetValue;
}

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

Alan Mishchenko committed
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096
  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDarClau( Abc_Ntk_t * pNtk, int nFrames, int nPref, int nClauses, int nLutSize, int nLevels, int nCutsMax, int nBatches, int fStepUp, int fBmc, int fRefs, int fTarget, int fVerbose, int fVeryVerbose )
{
    extern int Fra_Clau( Aig_Man_t * pMan, int nIters, int fVerbose, int fVeryVerbose );
    extern int Fra_Claus( Aig_Man_t * pAig, int nFrames, int nPref, int nClauses, int nLutSize, int nLevels, int nCutsMax, int nBatches, int fStepUp, int fBmc, int fRefs, int fTarget, int fVerbose, int fVeryVerbose );
    Aig_Man_t * pMan;
    if ( fTarget && Abc_NtkPoNum(pNtk) != 1 )
    {
        printf( "The number of outputs should be 1.\n" );
        return 1;
    }
Alan Mishchenko committed
3097
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
    if ( pMan == NULL )
        return 1;
//    Aig_ManReduceLachesCount( pMan );
    if ( pMan->vFlopNums )
        Vec_IntFree( pMan->vFlopNums ); 
    pMan->vFlopNums = NULL;

//    Fra_Clau( pMan, nStepsMax, fVerbose, fVeryVerbose );
    Fra_Claus( pMan, nFrames, nPref, nClauses, nLutSize, nLevels, nCutsMax, nBatches, fStepUp, fBmc, fRefs, fTarget, fVerbose, fVeryVerbose );
    Aig_ManStop( pMan );
    return 1;
}

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

  Synopsis    [Performs targe enlargement.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarEnlarge( Abc_Ntk_t * pNtk, int nFrames, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
3126
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3127 3128 3129 3130 3131 3132 3133 3134
    if ( pMan == NULL )
        return NULL;
    pMan = Aig_ManFrames( pTemp = pMan, nFrames, 0, 1, 1, 1, NULL );
    Aig_ManStop( pTemp );
    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}
Alan Mishchenko committed
3135

Alan Mishchenko committed
3136 3137
/**Function*************************************************************

3138 3139 3140 3141 3142 3143 3144 3145 3146
  Synopsis    [Performs targe enlargement.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3147
Abc_Ntk_t * Abc_NtkDarTempor( Abc_Ntk_t * pNtk, int nFrames, int TimeOut, int nConfLimit, int fUseBmc, int fUseTransSigs, int fVerbose, int fVeryVerbose )
3148
{
3149
    extern Aig_Man_t * Saig_ManTempor( Aig_Man_t * pAig, int nFrames, int TimeOut, int nConfLimit, int fUseBmc, int fUseTransSigs, int fVerbose, int fVeryVerbose );
3150 3151 3152 3153 3154
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
3155
    pTemp = Saig_ManTempor( pMan, nFrames, TimeOut, nConfLimit, fUseBmc, fUseTransSigs, fVerbose, fVeryVerbose );
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
    Aig_ManStop( pMan );
    if ( pTemp == NULL )
        return Abc_NtkDup( pNtk );
    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pTemp );
    Aig_ManStop( pTemp );
    return pNtkAig;
}

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

Alan Mishchenko committed
3166
  Synopsis    [Performs induction for property only.]
Alan Mishchenko committed
3167 3168 3169 3170 3171 3172 3173 3174

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3175
int Abc_NtkDarInduction( Abc_Ntk_t * pNtk, int nFramesMax, int nConfMax, int fUnique, int fUniqueAll, int fGetCex, int fVerbose, int fVeryVerbose )
3176
{ 
3177
    Aig_Man_t * pMan;
Alan Mishchenko committed
3178 3179 3180 3181
    int clkTotal = clock();
    int RetValue;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
3182
        return -1;
3183
    RetValue = Saig_ManInduction( pMan, nFramesMax, nConfMax, fUnique, fUniqueAll, fGetCex, fVerbose, fVeryVerbose );
Alan Mishchenko committed
3184 3185 3186
    if ( RetValue == 1 )
    {
        printf( "Networks are equivalent.   " );
Alan Mishchenko committed
3187
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
3188 3189 3190 3191
    }
    else if ( RetValue == 0 )
    {
        printf( "Networks are NOT EQUIVALENT.   " );
Alan Mishchenko committed
3192
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
3193 3194 3195 3196
    }
    else
    {
        printf( "Networks are UNDECIDED.   " );
Alan Mishchenko committed
3197
ABC_PRT( "Time", clock() - clkTotal );
Alan Mishchenko committed
3198
    }
3199 3200 3201 3202 3203 3204 3205
    if ( fGetCex )
    {
        ABC_FREE( pNtk->pModel );
        ABC_FREE( pNtk->pSeqModel );
        pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
    }
    Aig_ManStop( pMan );
3206
    return RetValue;
Alan Mishchenko committed
3207 3208
}

Alan Mishchenko committed
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220

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

  Synopsis    [Interplates two networks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3221
Abc_Ntk_t * Abc_NtkInterOne( Abc_Ntk_t * pNtkOn, Abc_Ntk_t * pNtkOff, int fRelation, int fVerbose )
Alan Mishchenko committed
3222
{
Alan Mishchenko committed
3223
    extern Aig_Man_t * Aig_ManInter( Aig_Man_t * pManOn, Aig_Man_t * pManOff, int fRelation, int fVerbose );
Alan Mishchenko committed
3224 3225 3226 3227
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pManOn, * pManOff, * pManAig;
    if ( Abc_NtkCoNum(pNtkOn) != 1 || Abc_NtkCoNum(pNtkOff) != 1 )
    {
Alan Mishchenko committed
3228
        printf( "Currently works only for single-output networks.\n" );
Alan Mishchenko committed
3229 3230 3231 3232 3233 3234 3235 3236
        return NULL;
    }
    if ( Abc_NtkCiNum(pNtkOn) != Abc_NtkCiNum(pNtkOff) )
    {
        printf( "The number of PIs should be the same.\n" );
        return NULL;
    }
    // create internal AIGs
Alan Mishchenko committed
3237
    pManOn = Abc_NtkToDar( pNtkOn, 0, 0 );
Alan Mishchenko committed
3238 3239
    if ( pManOn == NULL )
        return NULL;
Alan Mishchenko committed
3240
    pManOff = Abc_NtkToDar( pNtkOff, 0, 0 );
Alan Mishchenko committed
3241 3242 3243
    if ( pManOff == NULL )
        return NULL;
    // derive the interpolant
Alan Mishchenko committed
3244
    pManAig = Aig_ManInter( pManOn, pManOff, fRelation, fVerbose );
Alan Mishchenko committed
3245 3246 3247 3248 3249 3250 3251
    if ( pManAig == NULL )
    {
        printf( "Interpolant computation failed.\n" );
        return NULL;
    }
    Aig_ManStop( pManOn );
    Aig_ManStop( pManOff );
Alan Mishchenko committed
3252 3253 3254 3255 3256 3257 3258
    // for the relation, add an extra input
    if ( fRelation )
    {
        Abc_Obj_t * pObj;
        pObj = Abc_NtkCreatePi( pNtkOff );
        Abc_ObjAssignName( pObj, "New", NULL );
    }
Alan Mishchenko committed
3259
    // create logic network
Alan Mishchenko committed
3260
    pNtkAig = Abc_NtkFromDar( pNtkOff, pManAig );
Alan Mishchenko committed
3261 3262 3263 3264
    Aig_ManStop( pManAig );
    return pNtkAig;
}

Alan Mishchenko committed
3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280
/**Function*************************************************************

  Synopsis    [Fast interpolation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkInterFast( Abc_Ntk_t * pNtkOn, Abc_Ntk_t * pNtkOff, int fVerbose )
{
    extern void Aig_ManInterFast( Aig_Man_t * pManOn, Aig_Man_t * pManOff, int fVerbose );
    Aig_Man_t * pManOn, * pManOff;
    // create internal AIGs
Alan Mishchenko committed
3281
    pManOn = Abc_NtkToDar( pNtkOn, 0, 0 );
Alan Mishchenko committed
3282 3283
    if ( pManOn == NULL )
        return;
Alan Mishchenko committed
3284
    pManOff = Abc_NtkToDar( pNtkOff, 0, 0 );
Alan Mishchenko committed
3285 3286 3287 3288 3289 3290
    if ( pManOff == NULL )
        return;
    Aig_ManInterFast( pManOn, pManOff, fVerbose );
    Aig_ManStop( pManOn );
    Aig_ManStop( pManOff );
}
Alan Mishchenko committed
3291 3292 3293 3294 3295

int timeCnf;
int timeSat;
int timeInt;

Alan Mishchenko committed
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306
/**Function*************************************************************

  Synopsis    [Interplates two networks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3307
Abc_Ntk_t * Abc_NtkInter( Abc_Ntk_t * pNtkOn, Abc_Ntk_t * pNtkOff, int fRelation, int fVerbose )
Alan Mishchenko committed
3308 3309 3310
{
    Abc_Ntk_t * pNtkOn1, * pNtkOff1, * pNtkInter1, * pNtkInter;
    Abc_Obj_t * pObj;
Alan Mishchenko committed
3311
    int i; //, clk = clock();
Alan Mishchenko committed
3312 3313 3314 3315 3316
    if ( Abc_NtkCoNum(pNtkOn) != Abc_NtkCoNum(pNtkOff) )
    {
        printf( "Currently works only for networks with equal number of POs.\n" );
        return NULL;
    }
Alan Mishchenko committed
3317 3318 3319
    // compute the fast interpolation time
//    Abc_NtkInterFast( pNtkOn, pNtkOff, fVerbose );
    // consider the case of one output
Alan Mishchenko committed
3320
    if ( Abc_NtkCoNum(pNtkOn) == 1 )
Alan Mishchenko committed
3321
        return Abc_NtkInterOne( pNtkOn, pNtkOff, fRelation, fVerbose );
Alan Mishchenko committed
3322
    // start the new network
Alan Mishchenko committed
3323 3324 3325 3326 3327
    pNtkInter = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
    pNtkInter->pName = Extra_UtilStrsav(pNtkOn->pName);
    Abc_NtkForEachPi( pNtkOn, pObj, i )
        Abc_NtkDupObj( pNtkInter, pObj, 1 );
    // process each POs separately
Alan Mishchenko committed
3328 3329 3330
timeCnf = 0;
timeSat = 0;
timeInt = 0;
Alan Mishchenko committed
3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
    Abc_NtkForEachCo( pNtkOn, pObj, i )
    {
        pNtkOn1 = Abc_NtkCreateCone( pNtkOn, Abc_ObjFanin0(pObj), Abc_ObjName(pObj), 1 );
        if ( Abc_ObjFaninC0(pObj) )
            Abc_ObjXorFaninC( Abc_NtkPo(pNtkOn1, 0), 0 );

        pObj   = Abc_NtkCo(pNtkOff, i);
        pNtkOff1 = Abc_NtkCreateCone( pNtkOff, Abc_ObjFanin0(pObj), Abc_ObjName(pObj), 1 );
        if ( Abc_ObjFaninC0(pObj) )
            Abc_ObjXorFaninC( Abc_NtkPo(pNtkOff1, 0), 0 );

        if ( Abc_NtkNodeNum(pNtkOn1) == 0 )
            pNtkInter1 = Abc_NtkDup( pNtkOn1 );
        else if ( Abc_NtkNodeNum(pNtkOff1) == 0 )
        {
            pNtkInter1 = Abc_NtkDup( pNtkOff1 );
            Abc_ObjXorFaninC( Abc_NtkPo(pNtkInter1, 0), 0 );
        }
        else
Alan Mishchenko committed
3350
            pNtkInter1 = Abc_NtkInterOne( pNtkOn1, pNtkOff1, 0, fVerbose );
Alan Mishchenko committed
3351 3352 3353 3354 3355
        if ( pNtkInter1 )
        {
            Abc_NtkAppend( pNtkInter, pNtkInter1, 1 );
            Abc_NtkDelete( pNtkInter1 );
        }
Alan Mishchenko committed
3356 3357 3358 3359

        Abc_NtkDelete( pNtkOn1 );
        Abc_NtkDelete( pNtkOff1 );
    }
Alan Mishchenko committed
3360 3361 3362 3363
//    ABC_PRT( "CNF", timeCnf );
//    ABC_PRT( "SAT", timeSat );
//    ABC_PRT( "Int", timeInt );
//    ABC_PRT( "Slow interpolation time", clock() - clk );
Alan Mishchenko committed
3364

Alan Mishchenko committed
3365 3366 3367 3368 3369 3370 3371 3372
    // return the network
    if ( !Abc_NtkCheck( pNtkInter ) )
        fprintf( stdout, "Abc_NtkAttachBottom(): Network check has failed.\n" );
    return pNtkInter;
}

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

Alan Mishchenko committed
3373
  Synopsis    []
Alan Mishchenko committed
3374 3375 3376 3377 3378 3379 3380 3381

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3382 3383
void Abc_NtkPrintSccs( Abc_Ntk_t * pNtk, int fVerbose )
{
Alan Mishchenko committed
3384
//    extern Vec_Ptr_t * Aig_ManRegPartitionLinear( Aig_Man_t * pAig, int nPartSize );
Alan Mishchenko committed
3385
    Aig_Man_t * pMan;
Alan Mishchenko committed
3386
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3387 3388 3389
    if ( pMan == NULL )
        return;
    Aig_ManComputeSccs( pMan );
Alan Mishchenko committed
3390
//    Aig_ManRegPartitionLinear( pMan, 1000 );
Alan Mishchenko committed
3391 3392 3393
    Aig_ManStop( pMan );
}

Alan Mishchenko committed
3394 3395
/**Function*************************************************************

Alan Mishchenko committed
3396 3397 3398 3399 3400 3401 3402 3403 3404
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3405
int Abc_NtkDarPrintCone( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
3406 3407 3408 3409 3410
{
    extern void Saig_ManPrintCones( Aig_Man_t * pAig );
    Aig_Man_t * pMan;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
3411
        return 0;
Alan Mishchenko committed
3412 3413 3414
    assert( Aig_ManRegNum(pMan) > 0 );
    Saig_ManPrintCones( pMan );
    Aig_ManStop( pMan );
3415
    return 1;
Alan Mishchenko committed
3416 3417 3418 3419 3420
}

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

  Synopsis    []
Alan Mishchenko committed
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
Abc_Ntk_t * Abc_NtkBalanceExor( Abc_Ntk_t * pNtk, int fUpdateLevel, int fVerbose )
{
    extern void Dar_BalancePrintStats( Aig_Man_t * p );
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;//, * pTemp2;
    assert( Abc_NtkIsStrash(pNtk) );
    // derive AIG with EXORs
    pMan = Abc_NtkToDar( pNtk, 1, 0 );
    if ( pMan == NULL )
        return NULL;
//    Aig_ManPrintStats( pMan );
    if ( fVerbose )
        Dar_BalancePrintStats( pMan );
    // perform balancing
    pTemp = Dar_ManBalance( pMan, fUpdateLevel );
//    Aig_ManPrintStats( pTemp );
    // create logic network
    pNtkAig = Abc_NtkFromDar( pNtk, pTemp );
    Aig_ManStop( pTemp );
    Aig_ManStop( pMan );
    return pNtkAig;
}

Alan Mishchenko committed
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
/**Function*************************************************************

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3463
Abc_Ntk_t * Abc_NtkPhaseAbstract( Abc_Ntk_t * pNtk, int nFrames, int nPref, int fIgnore, int fPrint, int fVerbose )
Alan Mishchenko committed
3464
{
Alan Mishchenko committed
3465
    extern Aig_Man_t * Saig_ManPhaseAbstract( Aig_Man_t * p, Vec_Int_t * vInits, int nFrames, int nPref, int fIgnore, int fPrint, int fVerbose );
Alan Mishchenko committed
3466 3467 3468
    Vec_Int_t * vInits;
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
3469
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3470 3471 3472
    if ( pMan == NULL )
        return NULL;
    vInits = Abc_NtkGetLatchValues(pNtk);
Alan Mishchenko committed
3473
    pMan = Saig_ManPhaseAbstract( pTemp = pMan, vInits, nFrames, nPref, fIgnore, fPrint, fVerbose );
Alan Mishchenko committed
3474 3475 3476 3477 3478
    Vec_IntFree( vInits );
    Aig_ManStop( pTemp );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
Alan Mishchenko committed
3479 3480
//    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
//    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
Alan Mishchenko committed
3481 3482 3483 3484
    Aig_ManStop( pMan );
    return pNtkAig;
}

Alan Mishchenko committed
3485 3486
/**Function*************************************************************

Alan Mishchenko committed
3487 3488 3489 3490 3491 3492 3493 3494 3495
  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
int Abc_NtkPhaseFrameNum( Abc_Ntk_t * pNtk )
{
    extern int Saig_ManPhaseFrameNum( Aig_Man_t * p, Vec_Int_t * vInits );
    Vec_Int_t * vInits;
    Aig_Man_t * pMan;
    int nFrames;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return 1;
    vInits = Abc_NtkGetLatchValues(pNtk);
    nFrames = Saig_ManPhaseFrameNum( pMan, vInits );
    Vec_IntFree( vInits );
    Aig_ManStop( pMan );
    return nFrames;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
Abc_Ntk_t * Abc_NtkDarSynchOne( Abc_Ntk_t * pNtk, int nWords, int fVerbose )
{
    extern Aig_Man_t * Saig_SynchSequenceApply( Aig_Man_t * pAig, int nWords, int fVerbose );
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    pMan = Saig_SynchSequenceApply( pTemp = pMan, nWords, fVerbose );
    Aig_ManStop( pTemp );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarSynch( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int nWords, int fVerbose )
{
    extern Aig_Man_t * Saig_Synchronize( Aig_Man_t * pAig1, Aig_Man_t * pAig2, int nWords, int fVerbose );
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan1, * pMan2, * pMan;
    pMan1 = Abc_NtkToDar( pNtk1, 0, 1 );
    if ( pMan1 == NULL )
        return NULL;
    pMan2 = Abc_NtkToDar( pNtk2, 0, 1 );
    if ( pMan2 == NULL )
    {
        Aig_ManStop( pMan1 );
        return NULL;
    }
    pMan = Saig_Synchronize( pMan1, pMan2, nWords, fVerbose );
    Aig_ManStop( pMan1 );
    Aig_ManStop( pMan2 );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
Alan Mishchenko committed
3571 3572
//    pNtkAig->pName = Extra_UtilStrsav("miter");
//    pNtkAig->pSpec = NULL;
Alan Mishchenko committed
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
Abc_Ntk_t * Abc_NtkDarClockGate( Abc_Ntk_t * pNtk, Abc_Ntk_t * pCare, Cgt_Par_t * pPars )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan1, * pMan2 = NULL, * pMan;
    pMan1 = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan1 == NULL )
        return NULL;
    if ( pCare )
    {
        pMan2 = Abc_NtkToDar( pCare, 0, 0 );
        if ( pMan2 == NULL )
        {
            Aig_ManStop( pMan1 );
            return NULL;
        }
    }
    pMan = Cgt_ClockGating( pMan1, pMan2, pPars );
    Aig_ManStop( pMan1 );
    if ( pMan2 )
        Aig_ManStop( pMan2 );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
Abc_Ntk_t * Abc_NtkDarExtWin( Abc_Ntk_t * pNtk, int nObjId, int nDist, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan1, * pMan;
    Aig_Obj_t * pObj;
    pMan1 = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan1 == NULL )
        return NULL;
    if ( nObjId == -1 )
    {
        pObj = Saig_ManFindPivot( pMan1 );
        printf( "Selected object %d as a window pivot.\n", pObj->Id );
    }
    else
    {
        if ( nObjId >= Aig_ManObjNumMax(pMan1) )
        {
            Aig_ManStop( pMan1 );
            printf( "The ID is too large.\n" );
            return NULL;
        }
        pObj = Aig_ManObj( pMan1, nObjId );
        if ( pObj == NULL )
        {
            Aig_ManStop( pMan1 );
            printf( "Object with ID %d does not exist.\n", nObjId );
            return NULL;
        }
        if ( !Saig_ObjIsLo(pMan1, pObj) && !Aig_ObjIsNode(pObj) )
        {
            Aig_ManStop( pMan1 );
            printf( "Object with ID %d is not a node or reg output.\n", nObjId );
            return NULL;
        }
    }
    pMan = Saig_ManWindowExtract( pMan1, pObj, nDist );
    Aig_ManStop( pMan1 );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarInsWin( Abc_Ntk_t * pNtk, Abc_Ntk_t * pCare, int nObjId, int nDist, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan1, * pMan2 = NULL, * pMan;
    Aig_Obj_t * pObj;
    pMan1 = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan1 == NULL )
        return NULL;
    if ( nObjId == -1 )
    {
        pObj = Saig_ManFindPivot( pMan1 );
        printf( "Selected object %d as a window pivot.\n", pObj->Id );
    }
    else
    {
        if ( nObjId >= Aig_ManObjNumMax(pMan1) )
        {
            Aig_ManStop( pMan1 );
            printf( "The ID is too large.\n" );
            return NULL;
        }
        pObj = Aig_ManObj( pMan1, nObjId );
        if ( pObj == NULL )
        {
            Aig_ManStop( pMan1 );
            printf( "Object with ID %d does not exist.\n", nObjId );
            return NULL;
        }
        if ( !Saig_ObjIsLo(pMan1, pObj) && !Aig_ObjIsNode(pObj) )
        {
            Aig_ManStop( pMan1 );
            printf( "Object with ID %d is not a node or reg output.\n", nObjId );
            return NULL;
        }
    }
    if ( pCare )
    {
        pMan2 = Abc_NtkToDar( pCare, 0, 0 );
        if ( pMan2 == NULL )
        {
            Aig_ManStop( pMan1 );
            return NULL;
        }
    }
    pMan = Saig_ManWindowInsert( pMan1, pObj, nDist, pMan2 );
    Aig_ManStop( pMan1 );
    if ( pMan2 )
        Aig_ManStop( pMan2 );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromDarSeqSweep( pNtk, pMan );
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
3749 3750 3751 3752
Abc_Ntk_t * Abc_NtkDarFrames( Abc_Ntk_t * pNtk, int nPrefix, int nFrames, int fInit, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
Alan Mishchenko committed
3753
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768
    if ( pMan == NULL )
        return NULL;
    pMan = Saig_ManTimeframeSimplify( pTemp = pMan, nPrefix, nFrames, fInit, fVerbose );
    Aig_ManStop( pTemp );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

Alan Mishchenko committed
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805
  Synopsis    [Performs phase abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarCleanupAig( Abc_Ntk_t * pNtk, int fCleanupPis, int fCleanupPos, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan;
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    if ( fCleanupPis )
    {
        int Temp = Aig_ManPiCleanup( pMan );
        if ( fVerbose )
            printf( "Cleanup removed %d primary inputs without fanout.\n", Temp );                                                                     
    }
    if ( fCleanupPos )
    {
        int Temp = Aig_ManPoCleanup( pMan );
        if ( fVerbose )
            printf( "Cleanup removed %d primary outputs driven by const-0.\n", Temp );                                                                     
    }
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

Alan Mishchenko committed
3806 3807 3808 3809 3810 3811 3812 3813 3814
  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3815
int Abc_NtkDarReach( Abc_Ntk_t * pNtk, Saig_ParBbr_t * pPars )
Alan Mishchenko committed
3816 3817
{
    Aig_Man_t * pMan;
3818
    int RetValue;
Alan Mishchenko committed
3819
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
Alan Mishchenko committed
3820
    if ( pMan == NULL )
3821 3822
        return -1;
    RetValue = Aig_ManVerifyUsingBdds( pMan, pPars );
Alan Mishchenko committed
3823 3824
    ABC_FREE( pNtk->pModel );
    ABC_FREE( pNtk->pSeqModel );
Alan Mishchenko committed
3825
    pNtk->pSeqModel = pMan->pSeqModel; pMan->pSeqModel = NULL;
Alan Mishchenko committed
3826
    Aig_ManStop( pMan );
3827
    return RetValue;
Alan Mishchenko committed
3828 3829
}

3830 3831
ABC_NAMESPACE_IMPL_END

Alan Mishchenko committed
3832 3833 3834
#include "amap.h"
#include "mio.h"

3835 3836 3837
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Amap_ManProduceNetwork( Abc_Ntk_t * pNtk, Vec_Ptr_t * vMapping )
{
3851 3852
//    extern void * Abc_FrameReadLibGen();
    Mio_Library_t * pLib = (Mio_Library_t *)Abc_FrameReadLibGen();
Alan Mishchenko committed
3853 3854 3855 3856 3857 3858
    Amap_Out_t * pRes;
    Vec_Ptr_t * vNodesNew;
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pNodeNew, * pFaninNew;
    int i, k, iPis, iPos, nDupGates;
    // make sure gates exist in the current library
3859
    Vec_PtrForEachEntry( Amap_Out_t *, vMapping, pRes, i )
Alan Mishchenko committed
3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
        if ( pRes->pName && Mio_LibraryReadGateByName( pLib, pRes->pName ) == NULL )
        {
            printf( "Current library does not contain gate \"%s\".\n", pRes->pName );
            return NULL;
        }
    // create the network
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_MAP );
    pNtkNew->pManFunc = pLib;
    iPis = iPos = 0;
    vNodesNew = Vec_PtrAlloc( Vec_PtrSize(vMapping) );
3870
    Vec_PtrForEachEntry( Amap_Out_t *, vMapping, pRes, i )
Alan Mishchenko committed
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
    {
        if ( pRes->Type == -1 )
            pNodeNew = Abc_NtkCi( pNtkNew, iPis++ );
        else if ( pRes->Type == 1 )
            pNodeNew = Abc_NtkCo( pNtkNew, iPos++ );
        else
        {
            pNodeNew = Abc_NtkCreateNode( pNtkNew );
            pNodeNew->pData = Mio_LibraryReadGateByName( pLib, pRes->pName );
        }
        for ( k = 0; k < pRes->nFans; k++ )
        {
3883
            pFaninNew = (Abc_Obj_t *)Vec_PtrEntry( vNodesNew, pRes->pFans[k] );
Alan Mishchenko committed
3884 3885 3886 3887 3888 3889 3890 3891
            Abc_ObjAddFanin( pNodeNew, pFaninNew );
        }
        Vec_PtrPush( vNodesNew, pNodeNew );
    }
    Vec_PtrFree( vNodesNew );
    assert( iPis == Abc_NtkCiNum(pNtkNew) );
    assert( iPos == Abc_NtkCoNum(pNtkNew) );
    // decouple the PO driver nodes to reduce the number of levels
3892
    nDupGates = Abc_NtkLogicMakeSimpleCos( pNtkNew, 0 );
Alan Mishchenko committed
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
//    if ( nDupGates && Map_ManReadVerbose(pMan) )
//        printf( "Duplicated %d gates to decouple the CO drivers.\n", nDupGates );
    return pNtkNew;
}

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

  Synopsis    [Gives the current ABC network to AIG manager for processing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarAmap( Abc_Ntk_t * pNtk, Amap_Par_t * pPars )
{
Alan Mishchenko committed
3911
    extern Vec_Ptr_t * Amap_ManTest( Aig_Man_t * pAig, Amap_Par_t * pPars );
Alan Mishchenko committed
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
    Vec_Ptr_t * vMapping;
    Abc_Ntk_t * pNtkAig = NULL;
    Aig_Man_t * pMan;
    Aig_MmFlex_t * pMem;

    assert( Abc_NtkIsStrash(pNtk) );
    // convert to the AIG manager
    pMan = Abc_NtkToDarChoices( pNtk );
    if ( pMan == NULL )
        return NULL;

    // perform computation
    vMapping = Amap_ManTest( pMan, pPars );
    Aig_ManStop( pMan );
    if ( vMapping == NULL )
        return NULL;
3928
    pMem = (Aig_MmFlex_t *)Vec_PtrPop( vMapping );
Alan Mishchenko committed
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
    pNtkAig = Amap_ManProduceNetwork( pNtk, vMapping );
    Aig_MmFlexStop( pMem, 0 );
    Vec_PtrFree( vMapping );

    // make sure everything is okay
    if ( pNtkAig && !Abc_NtkCheck( pNtkAig ) )
    {
        printf( "Abc_NtkDar: The network check has failed.\n" );
        Abc_NtkDelete( pNtkAig );
        return NULL;
    }
    return pNtkAig;
}
Alan Mishchenko committed
3942

Alan Mishchenko committed
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
/**Function*************************************************************

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974
void Abc_NtkDarConstr( Abc_Ntk_t * pNtk, int nFrames, int nConfs, int nProps, int fStruct, int fOldAlgo, int fVerbose )
{
    Aig_Man_t * pMan;//, * pMan2;//, * pTemp;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return;
    if ( fStruct )
        Saig_ManDetectConstrTest( pMan );
    else
        Saig_ManDetectConstrFuncTest( pMan, nFrames, nConfs, nProps, fOldAlgo, fVerbose );
    Aig_ManStop( pMan );
}

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarOutdec( Abc_Ntk_t * pNtk, int nLits, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    pMan = Saig_ManDecPropertyOutput( pTemp = pMan, nLits, fVerbose );
    Aig_ManStop( pTemp );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pMan->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pMan->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []
4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarUnfold( Abc_Ntk_t * pNtk, int nFrames, int nConfs, int nProps, int fStruct, int fOldAlgo, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    if ( fStruct )
        pMan = Saig_ManDupUnfoldConstrs( pTemp = pMan );
    else
        pMan = Saig_ManDupUnfoldConstrsFunc( pTemp = pMan, nFrames, nConfs, nProps, fOldAlgo, fVerbose );
    Aig_ManStop( pTemp );
    if ( pMan == NULL )
        return NULL;
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pMan->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pMan->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarFold( Abc_Ntk_t * pNtk, int fCompl, int fVerbose )
{
    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
    pMan = Saig_ManDupFoldConstrsFunc( pTemp = pMan, fCompl, fVerbose );
    Aig_ManStop( pTemp );
    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pMan->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pMan->pSpec);
    Aig_ManStop( pMan );
    return pNtkAig;
}

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDarConstrProfile( Abc_Ntk_t * pNtk, int fVerbose )
{
    extern int Ssw_ManProfileConstraints( Aig_Man_t * p, int nWords, int nFrames, int fVerbose );
    extern Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * p, int nFrames, int nPref, int fProbOne );
    Aig_Man_t * pMan;
//    Vec_Int_t * vProbOne;
//    Aig_Obj_t * pObj;
//    int i, Entry;
    assert( Abc_NtkIsStrash(pNtk) );
    assert( Abc_NtkConstrNum(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return;
    // value in the init state
//    Abc_AigSetNodePhases( pNtk );
/*
    // derive probabilities
    vProbOne = Saig_ManComputeSwitchProbs( pMan, 48, 16, 1 );
    // iterate over the constraint outputs
    Saig_ManForEachPo( pMan, pObj, i )
    {
        Entry = Vec_IntEntry( vProbOne, Aig_ObjId(pObj) );
        if ( i < Saig_ManPoNum(pMan) - Saig_ManConstrNum(pMan) )
            printf( "Primary output :  ", i );
        else
            printf( "Constraint %3d :  ", i-(Saig_ManPoNum(pMan) - Saig_ManConstrNum(pMan)) );
        printf( "ProbOne = %f  ", Aig_Int2Float(Entry) );
        printf( "AllZeroValue = %d ", Aig_ObjPhase(pObj) );
        printf( "\n" );
    }
*/
    // double-check
    Ssw_ManProfileConstraints( pMan, 16, 64, 1 );
    printf( "TwoFrameSatValue = %d.\n", Ssw_ManSetConstrPhases(pMan, 2, NULL) );
    // clean up
//    Vec_IntFree( vProbOne );
    Aig_ManStop( pMan );
}

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

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
4120
void Abc_NtkDarTest( Abc_Ntk_t * pNtk, int Num )
Alan Mishchenko committed
4121
{
4122 4123
//    extern void Saig_ManDetectConstr( Aig_Man_t * p );
//    extern void Saig_ManDetectConstrFuncTest( Aig_Man_t * p );
4124 4125 4126
//    extern void Saig_ManFoldConstrTest( Aig_Man_t * pAig );
    extern void Llb_ManComputeDomsTest( Aig_Man_t * pAig, int Num );

4127 4128


Alan Mishchenko committed
4129
//    extern void Fsim_ManTest( Aig_Man_t * pAig );
Alan Mishchenko committed
4130 4131 4132
    extern Vec_Int_t * Saig_StrSimPerformMatching( Aig_Man_t * p0, Aig_Man_t * p1, int nDist, int fVerbose, Aig_Man_t ** ppMiter );
//    Vec_Int_t * vPairs;
    Aig_Man_t * pMan;//, * pMan2;//, * pTemp;
Alan Mishchenko committed
4133 4134 4135 4136
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return;
Alan Mishchenko committed
4137
/*
Alan Mishchenko committed
4138 4139 4140 4141 4142 4143
Aig_ManSetRegNum( pMan, pMan->nRegs );
Aig_ManPrintStats( pMan );
Saig_ManDumpBlif( pMan, "_temp_.blif" );
Aig_ManStop( pMan );
pMan = Saig_ManReadBlif( "_temp_.blif" );
Aig_ManPrintStats( pMan );
Alan Mishchenko committed
4144
*/
Alan Mishchenko committed
4145
/*
Alan Mishchenko committed
4146 4147 4148
    Aig_ManSetRegNum( pMan, pMan->nRegs );
    pTemp = Ssw_SignalCorrespondeceTestPairs( pMan );
    Aig_ManStop( pTemp );
Alan Mishchenko committed
4149 4150
*/

Alan Mishchenko committed
4151 4152 4153 4154 4155
/*
//    Ssw_SecSpecialMiter( pMan, NULL, 2, 1 );
    pMan2 = Aig_ManDupSimple(pMan);
    vPairs = Saig_StrSimPerformMatching( pMan, pMan2, 0, 1, NULL );
    Vec_IntFree( vPairs );
Alan Mishchenko committed
4156
    Aig_ManStop( pMan );
Alan Mishchenko committed
4157 4158
    Aig_ManStop( pMan2 );
*/
4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
//    Ioa_WriteAigerBufferTest( pMan, "test.aig", 0, 0 );
//    Saig_ManFoldConstrTest( pMan );
    {
    extern void Saig_ManBmcSectionsTest( Aig_Man_t * p );
    extern void Saig_ManBmcTerSimTest( Aig_Man_t * p );
    extern void Saig_ManBmcSupergateTest( Aig_Man_t * p );
    extern void Saig_ManBmcMappingTest( Aig_Man_t * p );
//    Saig_ManBmcSectionsTest( pMan );
//    Saig_ManBmcTerSimTest( pMan );
//    Saig_ManBmcSupergateTest( pMan );
//    Saig_ManBmcMappingTest( pMan );
    }

4172 4173 4174 4175
    {
//        void Pdr_ManEquivClasses( Aig_Man_t * pMan );
//        Pdr_ManEquivClasses( pMan );
    }
4176

4177 4178 4179 4180 4181 4182 4183 4184 4185
//    Llb_ManComputeDomsTest( pMan, Num );
    {
        extern void Llb_ManMinCutTest( Aig_Man_t * pMan, int Num );
        extern void Llb_BddStructAnalysis( Aig_Man_t * pMan );
        extern void Llb_NonlinExperiment( Aig_Man_t * pAig, int Num );
//        Llb_BddStructAnalysis( pMan );
        Llb_ManMinCutTest( pMan, Num );
//        Llb_NonlinExperiment( pMan, Num );
    }
Alan Mishchenko committed
4186 4187

//    Saig_MvManSimulate( pMan, 1 );
4188 4189
//    Saig_ManDetectConstr( pMan );
//    Saig_ManDetectConstrFuncTest( pMan );
Alan Mishchenko committed
4190

Alan Mishchenko committed
4191
//    Fsim_ManTest( pMan );
Alan Mishchenko committed
4192 4193
    Aig_ManStop( pMan );

Alan Mishchenko committed
4194
}
Alan Mishchenko committed
4195

Alan Mishchenko committed
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
/**Function*************************************************************

  Synopsis    [Performs BDD-based reachability analysis.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDarTestNtk( Abc_Ntk_t * pNtk )
{
Alan Mishchenko committed
4209
//    extern Aig_Man_t * Saig_ManDualRail( Aig_Man_t * p, int fMiter );
Alan Mishchenko committed
4210

Alan Mishchenko committed
4211
/*
Alan Mishchenko committed
4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223
    extern Aig_Man_t * Ssw_SignalCorrespondeceTestPairs( Aig_Man_t * pAig );

    Abc_Ntk_t * pNtkAig;
    Aig_Man_t * pMan, * pTemp;
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;

    Aig_ManSetRegNum( pMan, pMan->nRegs );
    pMan = Ssw_SignalCorrespondeceTestPairs( pTemp = pMan );
    Aig_ManStop( pTemp );
Alan Mishchenko committed
4224 4225
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
4226 4227 4228 4229

    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
Alan Mishchenko committed
4230
    Aig_ManStop( pMan );
Alan Mishchenko committed
4231
    return pNtkAig;
Alan Mishchenko committed
4232 4233
*/
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
4234
    Aig_Man_t * pMan;//, * pTemp;
Alan Mishchenko committed
4235 4236 4237 4238
    assert( Abc_NtkIsStrash(pNtk) );
    pMan = Abc_NtkToDar( pNtk, 0, 1 );
    if ( pMan == NULL )
        return NULL;
4239

Alan Mishchenko committed
4240
/*
Alan Mishchenko committed
4241
    Aig_ManSetRegNum( pMan, pMan->nRegs );
Alan Mishchenko committed
4242
    pMan = Saig_ManDualRail( pTemp = pMan, 1 );
Alan Mishchenko committed
4243
    Aig_ManStop( pTemp );
Alan Mishchenko committed
4244 4245
    if ( pMan == NULL )
        return NULL;
Alan Mishchenko committed
4246 4247 4248 4249 4250

    pNtkAig = Abc_NtkFromAigPhase( pMan );
    pNtkAig->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkAig->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    Aig_ManStop( pMan );
Alan Mishchenko committed
4251 4252 4253 4254 4255
*/

    pNtkAig = Abc_NtkFromDar( pNtk, pMan );
    Aig_ManStop( pMan );

Alan Mishchenko committed
4256 4257
    return pNtkAig;

Alan Mishchenko committed
4258 4259
}

Alan Mishchenko committed
4260 4261 4262 4263 4264
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


4265 4266
ABC_NAMESPACE_IMPL_END