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

  FileName    [ntlInsert.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Netlist representation.]

  Synopsis    [Procedures to insert mapping into a design.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ntl.h"
#include "kit.h"

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Inserts the given mapping into the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
46
Ntl_Man_t * Ntl_ManInsertMapping( Ntl_Man_t * p, Vec_Ptr_t * vMapping, Aig_Man_t * pAig )
Alan Mishchenko committed
47
{
Alan Mishchenko committed
48
    char Buffer[1000];
Alan Mishchenko committed
49 50 51 52 53 54 55
    Vec_Ptr_t * vCopies;
    Vec_Int_t * vCover;
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNet, * pNetCo;
    Ntl_Lut_t * pLut;
    int i, k, nDigits;
Alan Mishchenko committed
56 57 58 59 60
    assert( Vec_PtrSize(p->vCis) == Aig_ManPiNum(pAig) );
    assert( Vec_PtrSize(p->vCos) == Aig_ManPoNum(pAig) );
    p = Ntl_ManStartFrom( p );
    pRoot = Ntl_ManRootModel( p );
    assert( Ntl_ModelNodeNum(pRoot) == 0 );
Alan Mishchenko committed
61 62
    // map the AIG back onto the design
    Ntl_ManForEachCiNet( p, pNet, i )
Alan Mishchenko committed
63
        pNet->pCopy = Aig_ManPi( pAig, i );
Alan Mishchenko committed
64 65 66
    // start mapping of AIG nodes into their copies
    vCopies = Vec_PtrStart( Aig_ManObjNumMax(pAig) );
    Ntl_ManForEachCiNet( p, pNet, i )
Alan Mishchenko committed
67
        Vec_PtrWriteEntry( vCopies, ((Aig_Obj_t *)pNet->pCopy)->Id, pNet );
Alan Mishchenko committed
68 69 70
    // create a new node for each LUT
    vCover = Vec_IntAlloc( 1 << 16 );
    nDigits = Aig_Base10Log( Vec_PtrSize(vMapping) );
71
    Vec_PtrForEachEntry( Ntl_Lut_t *, vMapping, pLut, i )
Alan Mishchenko committed
72 73
    {
        pNode = Ntl_ModelCreateNode( pRoot, pLut->nFanins );
Alan Mishchenko committed
74
        pNode->pSop = Kit_PlaFromTruth( p->pMemSops, pLut->pTruth, pLut->nFanins, vCover );
Alan Mishchenko committed
75 76 77 78
        if ( !Kit_TruthIsConst0(pLut->pTruth, pLut->nFanins) && !Kit_TruthIsConst1(pLut->pTruth, pLut->nFanins) )
        {
            for ( k = 0; k < pLut->nFanins; k++ )
            {
79
                pNet = (Ntl_Net_t *)Vec_PtrEntry( vCopies, pLut->pFanins[k] );
Alan Mishchenko committed
80 81 82 83 84 85 86 87
                if ( pNet == NULL )
                {
                    printf( "Ntl_ManInsert(): Internal error: Net not found.\n" );
                    return 0;
                }
                Ntl_ObjSetFanin( pNode, pNet, k );
            }
        }
Alan Mishchenko committed
88 89
        else
            pNode->nFanins = 0;
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        sprintf( Buffer, "lut%0*d", nDigits, i );
        if ( (pNet = Ntl_ModelFindNet( pRoot, Buffer )) )
        {
            printf( "Ntl_ManInsert(): Internal error: Intermediate net name is not unique.\n" );
            return 0;
        }
        pNet = Ntl_ModelFindOrCreateNet( pRoot, Buffer );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
        {
            printf( "Ntl_ManInsert(): Internal error: Net has more than one fanin.\n" );
            return 0;
        }
        Vec_PtrWriteEntry( vCopies, pLut->Id, pNet );
    }
    Vec_IntFree( vCover );
    // mark CIs and outputs of the registers
    Ntl_ManForEachCiNet( p, pNetCo, i )
Alan Mishchenko committed
107
        pNetCo->fMark = 1;
Alan Mishchenko committed
108 109 110
    // update the CO pointers
    Ntl_ManForEachCoNet( p, pNetCo, i )
    {
Alan Mishchenko committed
111
        if ( pNetCo->fMark )
Alan Mishchenko committed
112
            continue;
Alan Mishchenko committed
113
        pNetCo->fMark = 1;
114
        pNet = (Ntl_Net_t *)Vec_PtrEntry( vCopies, Aig_Regular((Aig_Obj_t *)pNetCo->pCopy)->Id );
Alan Mishchenko committed
115
        pNode = Ntl_ModelCreateNode( pRoot, 1 );
116
        pNode->pSop = Aig_IsComplement((Aig_Obj_t *)pNetCo->pCopy)? Ntl_ManStoreSop( p->pMemSops, "0 1\n" ) : Ntl_ManStoreSop( p->pMemSops, "1 1\n" );
Alan Mishchenko committed
117 118
        Ntl_ObjSetFanin( pNode, pNet, 0 );
        // update the CO driver net
Alan Mishchenko committed
119
        assert( pNetCo->pDriver == NULL );
Alan Mishchenko committed
120 121 122 123 124 125 126
        if ( !Ntl_ModelSetNetDriver( pNode, pNetCo ) )
        {
            printf( "Ntl_ManInsert(): Internal error: PO net has more than one fanin.\n" );
            return 0;
        }
    }
    Vec_PtrFree( vCopies );
Alan Mishchenko committed
127 128 129 130 131
    // clean CI/CO marks
    Ntl_ManUnmarkCiCoNets( p );
    if ( !Ntl_ManCheck( p ) )
        printf( "Ntl_ManInsertNtk: The check has failed for design %s.\n", p->pName );
    return p;
Alan Mishchenko committed
132 133
}

Alan Mishchenko committed
134 135 136 137 138 139 140 141 142 143 144
/**Function*************************************************************

  Synopsis    [Inserts the given mapping into the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
145
Ntl_Man_t * Ntl_ManInsertAig( Ntl_Man_t * p, Aig_Man_t * pAig )
Alan Mishchenko committed
146
{
Alan Mishchenko committed
147
    char Buffer[1000];
Alan Mishchenko committed
148 149 150 151 152 153 154
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNet, * pNetCo;
    Aig_Obj_t * pObj, * pFanin;
    int i, nDigits, Counter;
    assert( Vec_PtrSize(p->vCis) == Aig_ManPiNum(pAig) );
    assert( Vec_PtrSize(p->vCos) == Aig_ManPoNum(pAig) );
Alan Mishchenko committed
155 156 157
    p = Ntl_ManStartFrom( p );
    pRoot = Ntl_ManRootModel( p );
    assert( Ntl_ModelNodeNum(pRoot) == 0 );
Alan Mishchenko committed
158 159 160 161 162 163 164 165
    // set the correspondence between the PI/PO nodes
    Aig_ManCleanData( pAig );
    Ntl_ManForEachCiNet( p, pNet, i )
        Aig_ManPi( pAig, i )->pData = pNet;
    // create constant node if needed
    if ( Aig_ManConst1(pAig)->nRefs > 0 )
    {
        pNode = Ntl_ModelCreateNode( pRoot, 0 );
Alan Mishchenko committed
166
        pNode->pSop = Ntl_ManStoreSop( p->pMemSops, " 1\n" );
Alan Mishchenko committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        if ( (pNet = Ntl_ModelFindNet( pRoot, "Const1" )) )
        {
            printf( "Ntl_ManInsertAig(): Internal error: Intermediate net name is not unique.\n" );
            return 0;
        }
        pNet = Ntl_ModelFindOrCreateNet( pRoot, "Const1" );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
        {
            printf( "Ntl_ManInsertAig(): Internal error: Net has more than one fanin.\n" );
            return 0;
        }
        Aig_ManConst1(pAig)->pData = pNet;
    }
    // create a new node for each LUT
    Counter = 0;
    nDigits = Aig_Base10Log( Aig_ManNodeNum(pAig) );
    Aig_ManForEachObj( pAig, pObj, i )
    {
        if ( !Aig_ObjIsNode(pObj) )
            continue;
        if ( Aig_ObjFanin0(pObj)->pData == NULL || Aig_ObjFanin1(pObj)->pData == NULL )
        {
            printf( "Ntl_ManInsertAig(): Internal error: Net not found.\n" );
            return 0;
        }
        pNode = Ntl_ModelCreateNode( pRoot, 2 );
193 194
        Ntl_ObjSetFanin( pNode, (Ntl_Net_t *)Aig_ObjFanin0(pObj)->pData, 0 );
        Ntl_ObjSetFanin( pNode, (Ntl_Net_t *)Aig_ObjFanin1(pObj)->pData, 1 );
Alan Mishchenko committed
195
        if ( Aig_ObjFaninC0(pObj) && Aig_ObjFaninC1(pObj) )
Alan Mishchenko committed
196
            pNode->pSop = Ntl_ManStoreSop( p->pMemSops, "00 1\n" );
Alan Mishchenko committed
197
        else if ( Aig_ObjFaninC0(pObj) && !Aig_ObjFaninC1(pObj) )
Alan Mishchenko committed
198
            pNode->pSop = Ntl_ManStoreSop( p->pMemSops, "01 1\n" );
Alan Mishchenko committed
199
        else if ( !Aig_ObjFaninC0(pObj) && Aig_ObjFaninC1(pObj) )
Alan Mishchenko committed
200
            pNode->pSop = Ntl_ManStoreSop( p->pMemSops, "10 1\n" );
Alan Mishchenko committed
201
        else // if ( Aig_ObjFaninC0(pObj) && Aig_ObjFaninC1(pObj) )
Alan Mishchenko committed
202
            pNode->pSop = Ntl_ManStoreSop( p->pMemSops, "11 1\n" );
Alan Mishchenko committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        sprintf( Buffer, "and%0*d", nDigits, Counter++ );
        if ( (pNet = Ntl_ModelFindNet( pRoot, Buffer )) )
        {
            printf( "Ntl_ManInsertAig(): Internal error: Intermediate net name is not unique.\n" );
            return 0;
        }
        pNet = Ntl_ModelFindOrCreateNet( pRoot, Buffer );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
        {
            printf( "Ntl_ManInsertAig(): Internal error: Net has more than one fanin.\n" );
            return 0;
        }
        pObj->pData = pNet;
    }
    // mark CIs and outputs of the registers
    Ntl_ManForEachCiNet( p, pNetCo, i )
Alan Mishchenko committed
219
        pNetCo->fMark = 1;
Alan Mishchenko committed
220 221 222
    // update the CO pointers
    Ntl_ManForEachCoNet( p, pNetCo, i )
    {
Alan Mishchenko committed
223
        if ( pNetCo->fMark )
Alan Mishchenko committed
224
            continue;
Alan Mishchenko committed
225
        pNetCo->fMark = 1;
Alan Mishchenko committed
226 227 228 229
        // get the corresponding PO and its driver
        pObj = Aig_ManPo( pAig, i );
        pFanin = Aig_ObjFanin0( pObj );
        // get the net driving the driver
230
        pNet = (Ntl_Net_t *)pFanin->pData; 
Alan Mishchenko committed
231
        pNode = Ntl_ModelCreateNode( pRoot, 1 );
Alan Mishchenko committed
232
        pNode->pSop = Aig_ObjFaninC0(pObj)? Ntl_ManStoreSop( p->pMemSops, "0 1\n" ) : Ntl_ManStoreSop( p->pMemSops, "1 1\n" );
Alan Mishchenko committed
233 234
        Ntl_ObjSetFanin( pNode, pNet, 0 );
        // update the CO driver net
Alan Mishchenko committed
235
        assert( pNetCo->pDriver == NULL );
Alan Mishchenko committed
236 237
        if ( !Ntl_ModelSetNetDriver( pNode, pNetCo ) )
        {
Alan Mishchenko committed
238
            printf( "Ntl_ManInsertAig(): Internal error: PO net has more than one fanin.\n" );
Alan Mishchenko committed
239 240 241
            return 0;
        }
    }
Alan Mishchenko committed
242 243 244 245 246
    // clean CI/CO marks
    Ntl_ManUnmarkCiCoNets( p );
    if ( !Ntl_ManCheck( p ) )
        printf( "Ntl_ManInsertAig: The check has failed for design %s.\n", p->pName );
    return p;
Alan Mishchenko committed
247 248 249 250
}

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

Alan Mishchenko committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  Synopsis    [Find drivers of the given net.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManFindDriver( Ntl_Man_t * p, char * pName )
{
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNet, * pNetThis;
    int i, k;
    pRoot = Ntl_ManRootModel( p );
    pNetThis = Ntl_ModelFindNet( pRoot, pName );
    printf( "\n*** Net %d \"%s\":\n", pNetThis->NetId, pName );
    // mark from the nodes
    Ntl_ModelForEachPo( pRoot, pNode, i )
        if ( pNetThis == Ntl_ObjFanin0(pNode) )
            printf( "driven by PO %d\n", i );
    Ntl_ModelForEachNode( pRoot, pNode, i )
        Ntl_ObjForEachFanin( pNode, pNet, k )
            if ( pNetThis == pNet )
                printf( "driven by node %d with %d fanins and %d fanouts\n (%s)\n", 
                    pNode->Id, Ntl_ObjFaninNum(pNode), Ntl_ObjFanoutNum(pNode), Ntl_ObjFanout(pNode,0)->pName );
    Ntl_ModelForEachBox( pRoot, pNode, i )
        Ntl_ObjForEachFanin( pNode, pNet, k )
            if ( pNetThis == pNet )
                printf( "driven by box %d with %d fanins and %d fanouts\n (%s)\n", 
                    pNode->Id, Ntl_ObjFaninNum(pNode), Ntl_ObjFanoutNum(pNode), Ntl_ObjFanout(pNode,0)->pName );
}

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

Alan Mishchenko committed
287 288 289 290 291 292 293 294 295
  Synopsis    [Inserts the given mapping into the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
Ntl_Man_t * Ntl_ManInsertNtk2( Ntl_Man_t * p, Nwk_Man_t * pNtk )
{
    int fWriteConstants = 1;
    char Buffer[1000]; 
    Vec_Ptr_t * vObjs;
    Vec_Int_t * vTruth;
    Vec_Int_t * vCover;
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNet, * pNetCo;
    Nwk_Obj_t * pObj, * pFanin;
    int i, k, nDigits;
    unsigned * pTruth;
    assert( Vec_PtrSize(p->vCis) == Nwk_ManCiNum(pNtk) );
    assert( Vec_PtrSize(p->vCos) == Nwk_ManCoNum(pNtk) );
    p = Ntl_ManStartFrom( p );
    pRoot = Ntl_ManRootModel( p );
    assert( Ntl_ModelNodeNum(pRoot) == 0 );
    // set the correspondence between the PI/PO nodes
    Ntl_ManForEachCiNet( p, pNet, i )
        Nwk_ManCi( pNtk, i )->pCopy = pNet;
    // create a new node for each LUT
    vTruth  = Vec_IntAlloc( 1 << 16 );
    vCover  = Vec_IntAlloc( 1 << 16 );
    nDigits = Aig_Base10Log( Nwk_ManNodeNum(pNtk) );
    // go through the nodes in the topological order
    vObjs = Nwk_ManDfs( pNtk );
323
    Vec_PtrForEachEntry( Nwk_Obj_t *, vObjs, pObj, i )
Alan Mishchenko committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    {
        if ( !Nwk_ObjIsNode(pObj) )
            continue;
/*
        if ( fWriteConstants && Nwk_ObjFaninNum(pObj) == 0 )
        {
            pObj->pCopy = NULL;
            continue;
        }
*/
        // skip constant drivers if they only drive COs
        if ( fWriteConstants && Nwk_ObjFaninNum(pObj) == 0 )
        {
            Nwk_Obj_t * pFanout;
            int i;
            Nwk_ObjForEachFanout( pObj, pFanout, i )
                if ( Nwk_ObjIsNode(pFanout) )
                    break;
            if ( i == Nwk_ObjFanoutNum(pObj) )
            {
                pObj->pCopy = NULL;
                continue;
            }
        }

        pNode = Ntl_ModelCreateNode( pRoot, Nwk_ObjFaninNum(pObj) );
        pTruth = Hop_ManConvertAigToTruth( pNtk->pManHop, Hop_Regular(pObj->pFunc), Nwk_ObjFaninNum(pObj), vTruth, 0 );
        if ( Hop_IsComplement(pObj->pFunc) )
            Kit_TruthNot( pTruth, pTruth, Nwk_ObjFaninNum(pObj) );
        if ( !Kit_TruthIsConst0(pTruth, Nwk_ObjFaninNum(pObj)) && !Kit_TruthIsConst1(pTruth, Nwk_ObjFaninNum(pObj)) )
        {
            Nwk_ObjForEachFanin( pObj, pFanin, k )
            {
357
                pNet = (Ntl_Net_t *)pFanin->pCopy;
Alan Mishchenko committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
                if ( pNet == NULL )
                {
                    printf( "Ntl_ManInsertNtk(): Internal error: Net not found.\n" );
                    return 0;
                }
                Ntl_ObjSetFanin( pNode, pNet, k );
            }
        }
        else if ( Kit_TruthIsConst0(pTruth, Nwk_ObjFaninNum(pObj)) )
        {
            pObj->pFunc = Hop_ManConst0(pNtk->pManHop);
            pNode->nFanins = 0;
        }
        else if ( Kit_TruthIsConst1(pTruth, Nwk_ObjFaninNum(pObj)) )
        {
            pObj->pFunc = Hop_ManConst1(pNtk->pManHop);
            pNode->nFanins = 0;
        }
        pNode->pSop = Kit_PlaFromTruth( p->pMemSops, pTruth, Nwk_ObjFaninNum(pObj), vCover );
        sprintf( Buffer, "lut%0*d", nDigits, i );
        if ( (pNet = Ntl_ModelFindNet( pRoot, Buffer )) )
        {
            printf( "Ntl_ManInsertNtk(): Internal error: Intermediate net name is not unique.\n" );
            return 0;
        }
        pNet = Ntl_ModelFindOrCreateNet( pRoot, Buffer );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
        {
            printf( "Ntl_ManInsertNtk(): Internal error: Net has more than one fanin.\n" );
            return 0;
        }
        pObj->pCopy = pNet;
    }
    Vec_PtrFree( vObjs );
    Vec_IntFree( vCover );
    Vec_IntFree( vTruth );
    // mark the nets driving special boxes
    if ( p->pNalR )
        p->pNalR( p );
    // mark CIs and outputs of the registers
    Ntl_ManForEachCiNet( p, pNetCo, i )
        pNetCo->fMark = 1;
    // update the CO pointers
    Ntl_ManForEachCoNet( p, pNetCo, i )
    {
        if ( pNetCo->fMark )
            continue; 
        pNetCo->fMark = 1;
        // get the corresponding PO and its driver
        pObj = Nwk_ManCo( pNtk, i );
        pFanin = Nwk_ObjFanin0( pObj );
        // get the net driving this PO
410
        pNet = (Ntl_Net_t *)pFanin->pCopy; 
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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        if ( pNet == NULL ) // constant net
        {
            assert( fWriteConstants );
            pNode = Ntl_ModelCreateNode( pRoot, 0 );
            pNode->pSop = pObj->fInvert? Ntl_ManStoreSop( p->pMemSops, " 0\n" ) : Ntl_ManStoreSop( p->pMemSops, " 1\n" );
        }
        else 
        if ( Nwk_ObjFanoutNum(pFanin) == 1 && Ntl_ObjIsNode(pNet->pDriver) && !pNet->fMark2 )
        {
            pNode = pNet->pDriver;
            if ( !Ntl_ModelClearNetDriver( pNode, pNet ) )
            {
                printf( "Ntl_ManInsertNtk(): Internal error! Net already has no driver.\n" );
                return NULL;
            }
            // remove this net 
            Ntl_ModelDeleteNet( pRoot, pNet );
            Vec_PtrWriteEntry( pRoot->vNets, pNet->NetId, NULL );
            // update node's function
            if ( pObj->fInvert )
                Kit_PlaComplement( pNode->pSop );
        }
        else
        {
/*
            if ( fWriteConstants && Ntl_ObjFaninNum(pNet->pDriver) == 0 )
            {
                pNode = Ntl_ModelCreateNode( pRoot, 0 );
                pNode->pSop = pObj->fInvert? Ntl_ManStoreSop( p->pMemSops, " 0\n" ) : Ntl_ManStoreSop( p->pMemSops, " 1\n" );
            }
            else
*/
            {
//                assert( Ntl_ObjFaninNum(pNet->pDriver) != 0 );
                pNode = Ntl_ModelCreateNode( pRoot, 1 );
                pNode->pSop = pObj->fInvert? Ntl_ManStoreSop( p->pMemSops, "0 1\n" ) : Ntl_ManStoreSop( p->pMemSops, "1 1\n" );
                Ntl_ObjSetFanin( pNode, pNet, 0 );
            }
        }
        // update the CO driver net
        assert( pNetCo->pDriver == NULL );
        if ( !Ntl_ModelSetNetDriver( pNode, pNetCo ) )
        {
            printf( "Ntl_ManInsertNtk(): Internal error: PO net has more than one fanin.\n" );
            return NULL;
        }
    }
    // clean CI/CO marks
    Ntl_ManUnmarkCiCoNets( p );
    if ( !Ntl_ManCheck( p ) )
    {
        printf( "Ntl_ManInsertNtk: The check has failed for design %s.\n", p->pName );
        return NULL;
    }
    return p;
}


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

  Synopsis    [Inserts the given mapping into the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
480
Ntl_Man_t * Ntl_ManInsertNtk( Ntl_Man_t * p, Nwk_Man_t * pNtk )
Alan Mishchenko committed
481
{
Alan Mishchenko committed
482
    char Buffer[1000]; 
Alan Mishchenko committed
483
    Vec_Ptr_t * vObjs;
Alan Mishchenko committed
484 485 486 487 488
    Vec_Int_t * vTruth;
    Vec_Int_t * vCover;
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNet, * pNetCo;
Alan Mishchenko committed
489
    Nwk_Obj_t * pObj, * pFanin;
Alan Mishchenko committed
490 491
    int i, k, nDigits;
    unsigned * pTruth;
Alan Mishchenko committed
492 493
    assert( Vec_PtrSize(p->vCis) == Nwk_ManCiNum(pNtk) );
    assert( Vec_PtrSize(p->vCos) == Nwk_ManCoNum(pNtk) );
Alan Mishchenko committed
494 495 496
    p = Ntl_ManStartFrom( p );
    pRoot = Ntl_ManRootModel( p );
    assert( Ntl_ModelNodeNum(pRoot) == 0 );
Alan Mishchenko committed
497 498
    // set the correspondence between the PI/PO nodes
    Ntl_ManForEachCiNet( p, pNet, i )
Alan Mishchenko committed
499
        Nwk_ManCi( pNtk, i )->pCopy = pNet;
Alan Mishchenko committed
500
    // create a new node for each LUT
Alan Mishchenko committed
501 502
    vTruth  = Vec_IntAlloc( 1 << 16 );
    vCover  = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
503
    nDigits = Aig_Base10Log( Nwk_ManNodeNum(pNtk) );
Alan Mishchenko committed
504
    // go through the nodes in the topological order
Alan Mishchenko committed
505
    vObjs = Nwk_ManDfs( pNtk );
506
    Vec_PtrForEachEntry( Nwk_Obj_t *, vObjs, pObj, i )
Alan Mishchenko committed
507
    {
Alan Mishchenko committed
508 509
        if ( !Nwk_ObjIsNode(pObj) )
            continue;
Alan Mishchenko committed
510 511 512 513 514
        pNode = Ntl_ModelCreateNode( pRoot, Nwk_ObjFaninNum(pObj) );
        pTruth = Hop_ManConvertAigToTruth( pNtk->pManHop, Hop_Regular(pObj->pFunc), Nwk_ObjFaninNum(pObj), vTruth, 0 );
        if ( Hop_IsComplement(pObj->pFunc) )
            Kit_TruthNot( pTruth, pTruth, Nwk_ObjFaninNum(pObj) );
        if ( !Kit_TruthIsConst0(pTruth, Nwk_ObjFaninNum(pObj)) && !Kit_TruthIsConst1(pTruth, Nwk_ObjFaninNum(pObj)) )
Alan Mishchenko committed
515
        {
Alan Mishchenko committed
516
            Nwk_ObjForEachFanin( pObj, pFanin, k )
Alan Mishchenko committed
517
            {
518
                pNet = (Ntl_Net_t *)pFanin->pCopy;
Alan Mishchenko committed
519 520
                if ( pNet == NULL )
                {
Alan Mishchenko committed
521
                    printf( "Ntl_ManInsertNtk(): Internal error: Net not found.\n" );
Alan Mishchenko committed
522 523 524 525 526
                    return 0;
                }
                Ntl_ObjSetFanin( pNode, pNet, k );
            }
        }
Alan Mishchenko committed
527 528 529
        else if ( Kit_TruthIsConst0(pTruth, Nwk_ObjFaninNum(pObj)) )
        {
            pObj->pFunc = Hop_ManConst0(pNtk->pManHop);
Alan Mishchenko committed
530
            pNode->nFanins = 0;
Alan Mishchenko committed
531 532 533 534 535 536 537
        }
        else if ( Kit_TruthIsConst1(pTruth, Nwk_ObjFaninNum(pObj)) )
        {
            pObj->pFunc = Hop_ManConst1(pNtk->pManHop);
            pNode->nFanins = 0;
        }
        pNode->pSop = Kit_PlaFromTruth( p->pMemSops, pTruth, Nwk_ObjFaninNum(pObj), vCover );
Alan Mishchenko committed
538 539 540
        sprintf( Buffer, "lut%0*d", nDigits, i );
        if ( (pNet = Ntl_ModelFindNet( pRoot, Buffer )) )
        {
Alan Mishchenko committed
541
            printf( "Ntl_ManInsertNtk(): Internal error: Intermediate net name is not unique.\n" );
Alan Mishchenko committed
542 543 544 545 546
            return 0;
        }
        pNet = Ntl_ModelFindOrCreateNet( pRoot, Buffer );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
        {
Alan Mishchenko committed
547
            printf( "Ntl_ManInsertNtk(): Internal error: Net has more than one fanin.\n" );
Alan Mishchenko committed
548 549 550 551
            return 0;
        }
        pObj->pCopy = pNet;
    }
Alan Mishchenko committed
552
    Vec_PtrFree( vObjs );
Alan Mishchenko committed
553 554 555 556
    Vec_IntFree( vCover );
    Vec_IntFree( vTruth );
    // mark CIs and outputs of the registers
    Ntl_ManForEachCiNet( p, pNetCo, i )
Alan Mishchenko committed
557
        pNetCo->fMark = 1;
Alan Mishchenko committed
558 559 560
    // update the CO pointers
    Ntl_ManForEachCoNet( p, pNetCo, i )
    {
Alan Mishchenko committed
561
        if ( pNetCo->fMark )
Alan Mishchenko committed
562
            continue;
Alan Mishchenko committed
563
        pNetCo->fMark = 1;
Alan Mishchenko committed
564
        // get the corresponding PO and its driver
Alan Mishchenko committed
565 566
        pObj = Nwk_ManCo( pNtk, i );
        pFanin = Nwk_ObjFanin0( pObj );
Alan Mishchenko committed
567
        // get the net driving this PO
568
        pNet = (Ntl_Net_t *)pFanin->pCopy; 
Alan Mishchenko committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
        if ( Nwk_ObjFanoutNum(pFanin) == 1 && Ntl_ObjIsNode(pNet->pDriver) )
        {
            pNode = pNet->pDriver;
            if ( !Ntl_ModelClearNetDriver( pNode, pNet ) )
            {
                printf( "Ntl_ManInsertNtk(): Internal error! Net already has no driver.\n" );
                return NULL;
            }
            // remove this net 
            Ntl_ModelDeleteNet( pRoot, pNet );
            Vec_PtrWriteEntry( pRoot->vNets, pNet->NetId, NULL );
            // update node's function
            if ( pObj->fInvert )
                Kit_PlaComplement( pNode->pSop );
        }
        else
        {
            pNode = Ntl_ModelCreateNode( pRoot, 1 );
            pNode->pSop = pObj->fInvert? Ntl_ManStoreSop( p->pMemSops, "0 1\n" ) : Ntl_ManStoreSop( p->pMemSops, "1 1\n" );
            Ntl_ObjSetFanin( pNode, pNet, 0 );
        }
Alan Mishchenko committed
590
        // update the CO driver net
Alan Mishchenko committed
591
        assert( pNetCo->pDriver == NULL );
Alan Mishchenko committed
592 593
        if ( !Ntl_ModelSetNetDriver( pNode, pNetCo ) )
        {
Alan Mishchenko committed
594
            printf( "Ntl_ManInsertNtk(): Internal error: PO net has more than one fanin.\n" );
Alan Mishchenko committed
595
            return NULL;
Alan Mishchenko committed
596 597
        }
    }
Alan Mishchenko committed
598 599 600
    // clean CI/CO marks
    Ntl_ManUnmarkCiCoNets( p );
    if ( !Ntl_ManCheck( p ) )
Alan Mishchenko committed
601
    {
Alan Mishchenko committed
602
        printf( "Ntl_ManInsertNtk: The check has failed for design %s.\n", p->pName );
Alan Mishchenko committed
603 604
        return NULL;
    }
Alan Mishchenko committed
605
    return p;
Alan Mishchenko committed
606 607
}

Alan Mishchenko committed
608 609 610 611 612
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


613 614
ABC_NAMESPACE_IMPL_END