hopUtil.c 18.7 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [hopUtil.c]
Alan Mishchenko committed
4 5 6 7 8 9 10 11 12 13 14 15 16

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [Various procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - May 11, 2006.]

Alan Mishchenko committed
17
  Revision    [$Id: hopUtil.c,v 1.00 2006/05/11 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20

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

Alan Mishchenko committed
21
#include "hop.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Increments the current traversal ID of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
void Hop_ManIncrementTravId( Hop_Man_t * p )
Alan Mishchenko committed
46 47
{
    if ( p->nTravIds >= (1<<30)-1 )
Alan Mishchenko committed
48
        Hop_ManCleanData( p );
Alan Mishchenko committed
49 50 51 52 53
    p->nTravIds++;
}

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

Alan Mishchenko committed
54
  Synopsis    [Cleans the data pointers for the nodes.]
Alan Mishchenko committed
55 56 57 58 59 60 61 62

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
63
void Hop_ManCleanData( Hop_Man_t * p )
Alan Mishchenko committed
64
{
Alan Mishchenko committed
65
    Hop_Obj_t * pObj;
Alan Mishchenko committed
66 67
    int i;
    p->nTravIds = 1;
Alan Mishchenko committed
68 69
    Hop_ManConst1(p)->pData = NULL;
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
70
        pObj->pData = NULL;
Alan Mishchenko committed
71
    Hop_ManForEachPo( p, pObj, i )
Alan Mishchenko committed
72
        pObj->pData = NULL;
Alan Mishchenko committed
73
    Hop_ManForEachNode( p, pObj, i )
Alan Mishchenko committed
74 75 76 77 78
        pObj->pData = NULL;
}

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

Alan Mishchenko committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  Synopsis    [Recursively cleans the data pointers in the cone of the node.]

  Description [Applicable to small AIGs only because no caching is performed.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Hop_ObjCleanData_rec( Hop_Obj_t * pObj )
{
    assert( !Hop_IsComplement(pObj) );
    assert( !Hop_ObjIsPo(pObj) );
    if ( Hop_ObjIsAnd(pObj) )
    {
        Hop_ObjCleanData_rec( Hop_ObjFanin0(pObj) );
        Hop_ObjCleanData_rec( Hop_ObjFanin1(pObj) );
    }
    pObj->pData = NULL;
}

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

Alan Mishchenko committed
102 103 104 105 106 107 108 109 110
  Synopsis    [Detects multi-input gate rooted at this node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
111
void Hop_ObjCollectMulti_rec( Hop_Obj_t * pRoot, Hop_Obj_t * pObj, Vec_Ptr_t * vSuper )
Alan Mishchenko committed
112
{
Alan Mishchenko committed
113
    if ( pRoot != pObj && (Hop_IsComplement(pObj) || Hop_ObjIsPi(pObj) || Hop_ObjType(pRoot) != Hop_ObjType(pObj)) )
Alan Mishchenko committed
114 115 116 117
    {
        Vec_PtrPushUnique(vSuper, pObj);
        return;
    }
Alan Mishchenko committed
118 119
    Hop_ObjCollectMulti_rec( pRoot, Hop_ObjChild0(pObj), vSuper );
    Hop_ObjCollectMulti_rec( pRoot, Hop_ObjChild1(pObj), vSuper );
Alan Mishchenko committed
120 121 122 123 124 125 126 127 128 129 130 131 132
}

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

  Synopsis    [Detects multi-input gate rooted at this node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
133
void Hop_ObjCollectMulti( Hop_Obj_t * pRoot, Vec_Ptr_t * vSuper )
Alan Mishchenko committed
134
{
Alan Mishchenko committed
135
    assert( !Hop_IsComplement(pRoot) );
Alan Mishchenko committed
136
    Vec_PtrClear( vSuper );
Alan Mishchenko committed
137
    Hop_ObjCollectMulti_rec( pRoot, pRoot, vSuper );
Alan Mishchenko committed
138 139 140 141
}

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

Alan Mishchenko committed
142 143 144 145 146 147 148 149 150
  Synopsis    [Returns 1 if the node is the root of MUX or EXOR/NEXOR.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
151
int Hop_ObjIsMuxType( Hop_Obj_t * pNode )
Alan Mishchenko committed
152
{
Alan Mishchenko committed
153
    Hop_Obj_t * pNode0, * pNode1;
Alan Mishchenko committed
154
    // check that the node is regular
Alan Mishchenko committed
155
    assert( !Hop_IsComplement(pNode) );
Alan Mishchenko committed
156
    // if the node is not AND, this is not MUX
Alan Mishchenko committed
157
    if ( !Hop_ObjIsAnd(pNode) )
Alan Mishchenko committed
158 159
        return 0;
    // if the children are not complemented, this is not MUX
Alan Mishchenko committed
160
    if ( !Hop_ObjFaninC0(pNode) || !Hop_ObjFaninC1(pNode) )
Alan Mishchenko committed
161 162
        return 0;
    // get children
Alan Mishchenko committed
163 164
    pNode0 = Hop_ObjFanin0(pNode);
    pNode1 = Hop_ObjFanin1(pNode);
Alan Mishchenko committed
165
    // if the children are not ANDs, this is not MUX
Alan Mishchenko committed
166
    if ( !Hop_ObjIsAnd(pNode0) || !Hop_ObjIsAnd(pNode1) )
Alan Mishchenko committed
167 168
        return 0;
    // otherwise the node is MUX iff it has a pair of equal grandchildren
Alan Mishchenko committed
169 170 171 172
    return (Hop_ObjFanin0(pNode0) == Hop_ObjFanin0(pNode1) && (Hop_ObjFaninC0(pNode0) ^ Hop_ObjFaninC0(pNode1))) || 
           (Hop_ObjFanin0(pNode0) == Hop_ObjFanin1(pNode1) && (Hop_ObjFaninC0(pNode0) ^ Hop_ObjFaninC1(pNode1))) ||
           (Hop_ObjFanin1(pNode0) == Hop_ObjFanin0(pNode1) && (Hop_ObjFaninC1(pNode0) ^ Hop_ObjFaninC0(pNode1))) ||
           (Hop_ObjFanin1(pNode0) == Hop_ObjFanin1(pNode1) && (Hop_ObjFaninC1(pNode0) ^ Hop_ObjFaninC1(pNode1)));
Alan Mishchenko committed
173 174
}

Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186

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

  Synopsis    [Recognizes what nodes are inputs of the EXOR.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
187
int Hop_ObjRecognizeExor( Hop_Obj_t * pObj, Hop_Obj_t ** ppFan0, Hop_Obj_t ** ppFan1 )
Alan Mishchenko committed
188
{
Alan Mishchenko committed
189 190 191
    Hop_Obj_t * p0, * p1;
    assert( !Hop_IsComplement(pObj) );
    if ( !Hop_ObjIsNode(pObj) )
Alan Mishchenko committed
192
        return 0;
Alan Mishchenko committed
193
    if ( Hop_ObjIsExor(pObj) )
Alan Mishchenko committed
194
    {
Alan Mishchenko committed
195 196
        *ppFan0 = Hop_ObjChild0(pObj);
        *ppFan1 = Hop_ObjChild1(pObj);
Alan Mishchenko committed
197 198
        return 1;
    }
Alan Mishchenko committed
199 200 201 202
    assert( Hop_ObjIsAnd(pObj) );
    p0 = Hop_ObjChild0(pObj);
    p1 = Hop_ObjChild1(pObj);
    if ( !Hop_IsComplement(p0) || !Hop_IsComplement(p1) )
Alan Mishchenko committed
203
        return 0;
Alan Mishchenko committed
204 205 206
    p0 = Hop_Regular(p0);
    p1 = Hop_Regular(p1);
    if ( !Hop_ObjIsAnd(p0) || !Hop_ObjIsAnd(p1) )
Alan Mishchenko committed
207
        return 0;
Alan Mishchenko committed
208
    if ( Hop_ObjFanin0(p0) != Hop_ObjFanin0(p1) || Hop_ObjFanin1(p0) != Hop_ObjFanin1(p1) )
Alan Mishchenko committed
209
        return 0;
Alan Mishchenko committed
210
    if ( Hop_ObjFaninC0(p0) == Hop_ObjFaninC0(p1) || Hop_ObjFaninC1(p0) == Hop_ObjFaninC1(p1) )
Alan Mishchenko committed
211
        return 0;
Alan Mishchenko committed
212 213
    *ppFan0 = Hop_ObjChild0(p0);
    *ppFan1 = Hop_ObjChild1(p0);
Alan Mishchenko committed
214 215 216
    return 1;
}

Alan Mishchenko committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
/**Function*************************************************************

  Synopsis    [Recognizes what nodes are control and data inputs of a MUX.]

  Description [If the node is a MUX, returns the control variable C.
  Assigns nodes T and E to be the then and else variables of the MUX. 
  Node C is never complemented. Nodes T and E can be complemented.
  This function also recognizes EXOR/NEXOR gates as MUXes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
231
Hop_Obj_t * Hop_ObjRecognizeMux( Hop_Obj_t * pNode, Hop_Obj_t ** ppNodeT, Hop_Obj_t ** ppNodeE )
Alan Mishchenko committed
232
{
Alan Mishchenko committed
233 234 235
    Hop_Obj_t * pNode0, * pNode1;
    assert( !Hop_IsComplement(pNode) );
    assert( Hop_ObjIsMuxType(pNode) );
Alan Mishchenko committed
236
    // get children
Alan Mishchenko committed
237 238
    pNode0 = Hop_ObjFanin0(pNode);
    pNode1 = Hop_ObjFanin1(pNode);
Alan Mishchenko committed
239 240

    // find the control variable
Alan Mishchenko committed
241
    if ( Hop_ObjFanin1(pNode0) == Hop_ObjFanin1(pNode1) && (Hop_ObjFaninC1(pNode0) ^ Hop_ObjFaninC1(pNode1)) )
Alan Mishchenko committed
242 243
    {
//        if ( Fraig_IsComplement(pNode1->p2) )
Alan Mishchenko committed
244
        if ( Hop_ObjFaninC1(pNode0) )
Alan Mishchenko committed
245
        { // pNode2->p2 is positive phase of C
Alan Mishchenko committed
246 247 248
            *ppNodeT = Hop_Not(Hop_ObjChild0(pNode1));//pNode2->p1);
            *ppNodeE = Hop_Not(Hop_ObjChild0(pNode0));//pNode1->p1);
            return Hop_ObjChild1(pNode1);//pNode2->p2;
Alan Mishchenko committed
249 250 251
        }
        else
        { // pNode1->p2 is positive phase of C
Alan Mishchenko committed
252 253 254
            *ppNodeT = Hop_Not(Hop_ObjChild0(pNode0));//pNode1->p1);
            *ppNodeE = Hop_Not(Hop_ObjChild0(pNode1));//pNode2->p1);
            return Hop_ObjChild1(pNode0);//pNode1->p2;
Alan Mishchenko committed
255 256
        }
    }
Alan Mishchenko committed
257
    else if ( Hop_ObjFanin0(pNode0) == Hop_ObjFanin0(pNode1) && (Hop_ObjFaninC0(pNode0) ^ Hop_ObjFaninC0(pNode1)) )
Alan Mishchenko committed
258 259
    {
//        if ( Fraig_IsComplement(pNode1->p1) )
Alan Mishchenko committed
260
        if ( Hop_ObjFaninC0(pNode0) )
Alan Mishchenko committed
261
        { // pNode2->p1 is positive phase of C
Alan Mishchenko committed
262 263 264
            *ppNodeT = Hop_Not(Hop_ObjChild1(pNode1));//pNode2->p2);
            *ppNodeE = Hop_Not(Hop_ObjChild1(pNode0));//pNode1->p2);
            return Hop_ObjChild0(pNode1);//pNode2->p1;
Alan Mishchenko committed
265 266 267
        }
        else
        { // pNode1->p1 is positive phase of C
Alan Mishchenko committed
268 269 270
            *ppNodeT = Hop_Not(Hop_ObjChild1(pNode0));//pNode1->p2);
            *ppNodeE = Hop_Not(Hop_ObjChild1(pNode1));//pNode2->p2);
            return Hop_ObjChild0(pNode0);//pNode1->p1;
Alan Mishchenko committed
271 272
        }
    }
Alan Mishchenko committed
273
    else if ( Hop_ObjFanin0(pNode0) == Hop_ObjFanin1(pNode1) && (Hop_ObjFaninC0(pNode0) ^ Hop_ObjFaninC1(pNode1)) )
Alan Mishchenko committed
274 275
    {
//        if ( Fraig_IsComplement(pNode1->p1) )
Alan Mishchenko committed
276
        if ( Hop_ObjFaninC0(pNode0) )
Alan Mishchenko committed
277
        { // pNode2->p2 is positive phase of C
Alan Mishchenko committed
278 279 280
            *ppNodeT = Hop_Not(Hop_ObjChild0(pNode1));//pNode2->p1);
            *ppNodeE = Hop_Not(Hop_ObjChild1(pNode0));//pNode1->p2);
            return Hop_ObjChild1(pNode1);//pNode2->p2;
Alan Mishchenko committed
281 282 283
        }
        else
        { // pNode1->p1 is positive phase of C
Alan Mishchenko committed
284 285 286
            *ppNodeT = Hop_Not(Hop_ObjChild1(pNode0));//pNode1->p2);
            *ppNodeE = Hop_Not(Hop_ObjChild0(pNode1));//pNode2->p1);
            return Hop_ObjChild0(pNode0);//pNode1->p1;
Alan Mishchenko committed
287 288
        }
    }
Alan Mishchenko committed
289
    else if ( Hop_ObjFanin1(pNode0) == Hop_ObjFanin0(pNode1) && (Hop_ObjFaninC1(pNode0) ^ Hop_ObjFaninC0(pNode1)) )
Alan Mishchenko committed
290 291
    {
//        if ( Fraig_IsComplement(pNode1->p2) )
Alan Mishchenko committed
292
        if ( Hop_ObjFaninC1(pNode0) )
Alan Mishchenko committed
293
        { // pNode2->p1 is positive phase of C
Alan Mishchenko committed
294 295 296
            *ppNodeT = Hop_Not(Hop_ObjChild1(pNode1));//pNode2->p2);
            *ppNodeE = Hop_Not(Hop_ObjChild0(pNode0));//pNode1->p1);
            return Hop_ObjChild0(pNode1);//pNode2->p1;
Alan Mishchenko committed
297 298 299
        }
        else
        { // pNode1->p2 is positive phase of C
Alan Mishchenko committed
300 301 302
            *ppNodeT = Hop_Not(Hop_ObjChild0(pNode0));//pNode1->p1);
            *ppNodeE = Hop_Not(Hop_ObjChild1(pNode1));//pNode2->p2);
            return Hop_ObjChild1(pNode0);//pNode1->p2;
Alan Mishchenko committed
303 304 305 306 307 308
        }
    }
    assert( 0 ); // this is not MUX
    return NULL;
}

Alan Mishchenko committed
309 310 311

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

Alan Mishchenko committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  Synopsis    [Prints Eqn formula for the AIG rooted at this node.]

  Description [The formula is in terms of PIs, which should have
  their names assigned in pObj->pData fields.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Hop_ObjPrintEqn( FILE * pFile, Hop_Obj_t * pObj, Vec_Vec_t * vLevels, int Level )
{
    Vec_Ptr_t * vSuper;
    Hop_Obj_t * pFanin;
    int fCompl, i;
    // store the complemented attribute
    fCompl = Hop_IsComplement(pObj);
    pObj = Hop_Regular(pObj);
    // constant case
    if ( Hop_ObjIsConst1(pObj) )
    {
        fprintf( pFile, "%d", !fCompl );
        return;
    }
    // PI case
    if ( Hop_ObjIsPi(pObj) )
    {
Alan Mishchenko committed
339
        fprintf( pFile, "%s%s", fCompl? "!" : "", (char*)pObj->pData );
Alan Mishchenko committed
340 341 342 343
        return;
    }
    // AND case
    Vec_VecExpand( vLevels, Level );
344
    vSuper = Vec_VecEntry(vLevels, Level);
Alan Mishchenko committed
345 346
    Hop_ObjCollectMulti( pObj, vSuper );
    fprintf( pFile, "%s", (Level==0? "" : "(") );
347
    Vec_PtrForEachEntry( Hop_Obj_t *, vSuper, pFanin, i )
Alan Mishchenko committed
348 349 350 351 352 353 354 355 356 357 358
    {
        Hop_ObjPrintEqn( pFile, Hop_NotCond(pFanin, fCompl), vLevels, Level+1 );
        if ( i < Vec_PtrSize(vSuper) - 1 )
            fprintf( pFile, " %s ", fCompl? "+" : "*" );
    }
    fprintf( pFile, "%s", (Level==0? "" : ")") );
    return;
}

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

Alan Mishchenko committed
359 360 361 362 363 364 365 366 367 368
  Synopsis    [Prints Verilog formula for the AIG rooted at this node.]

  Description [The formula is in terms of PIs, which should have
  their names assigned in pObj->pData fields.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
369
void Hop_ObjPrintVerilog( FILE * pFile, Hop_Obj_t * pObj, Vec_Vec_t * vLevels, int Level )
Alan Mishchenko committed
370 371
{
    Vec_Ptr_t * vSuper;
Alan Mishchenko committed
372
    Hop_Obj_t * pFanin, * pFanin0, * pFanin1, * pFaninC;
Alan Mishchenko committed
373 374
    int fCompl, i;
    // store the complemented attribute
Alan Mishchenko committed
375 376
    fCompl = Hop_IsComplement(pObj);
    pObj = Hop_Regular(pObj);
Alan Mishchenko committed
377
    // constant case
Alan Mishchenko committed
378
    if ( Hop_ObjIsConst1(pObj) )
Alan Mishchenko committed
379
    {
Alan Mishchenko committed
380
        fprintf( pFile, "1\'b%d", !fCompl );
Alan Mishchenko committed
381 382 383
        return;
    }
    // PI case
Alan Mishchenko committed
384
    if ( Hop_ObjIsPi(pObj) )
Alan Mishchenko committed
385
    {
Alan Mishchenko committed
386
        fprintf( pFile, "%s%s", fCompl? "~" : "", (char*)pObj->pData );
Alan Mishchenko committed
387 388 389
        return;
    }
    // EXOR case
Alan Mishchenko committed
390
    if ( Hop_ObjIsExor(pObj) )
Alan Mishchenko committed
391 392
    {
        Vec_VecExpand( vLevels, Level );
393
        vSuper = Vec_VecEntry( vLevels, Level );
Alan Mishchenko committed
394
        Hop_ObjCollectMulti( pObj, vSuper );
Alan Mishchenko committed
395
        fprintf( pFile, "%s", (Level==0? "" : "(") );
396
        Vec_PtrForEachEntry( Hop_Obj_t *, vSuper, pFanin, i )
Alan Mishchenko committed
397
        {
Alan Mishchenko committed
398
            Hop_ObjPrintVerilog( pFile, Hop_NotCond(pFanin, (fCompl && i==0)), vLevels, Level+1 );
Alan Mishchenko committed
399 400 401 402 403 404 405
            if ( i < Vec_PtrSize(vSuper) - 1 )
                fprintf( pFile, " ^ " );
        }
        fprintf( pFile, "%s", (Level==0? "" : ")") );
        return;
    }
    // MUX case
Alan Mishchenko committed
406
    if ( Hop_ObjIsMuxType(pObj) )
Alan Mishchenko committed
407
    {
Alan Mishchenko committed
408
        if ( Hop_ObjRecognizeExor( pObj, &pFanin0, &pFanin1 ) )
Alan Mishchenko committed
409 410
        {
            fprintf( pFile, "%s", (Level==0? "" : "(") );
Alan Mishchenko committed
411
            Hop_ObjPrintVerilog( pFile, Hop_NotCond(pFanin0, fCompl), vLevels, Level+1 );
Alan Mishchenko committed
412
            fprintf( pFile, " ^ " );
Alan Mishchenko committed
413
            Hop_ObjPrintVerilog( pFile, pFanin1, vLevels, Level+1 );
Alan Mishchenko committed
414 415 416 417
            fprintf( pFile, "%s", (Level==0? "" : ")") );
        }
        else 
        {
Alan Mishchenko committed
418
            pFaninC = Hop_ObjRecognizeMux( pObj, &pFanin1, &pFanin0 );
Alan Mishchenko committed
419
            fprintf( pFile, "%s", (Level==0? "" : "(") );
Alan Mishchenko committed
420
            Hop_ObjPrintVerilog( pFile, pFaninC, vLevels, Level+1 );
Alan Mishchenko committed
421
            fprintf( pFile, " ? " );
Alan Mishchenko committed
422
            Hop_ObjPrintVerilog( pFile, Hop_NotCond(pFanin1, fCompl), vLevels, Level+1 );
Alan Mishchenko committed
423
            fprintf( pFile, " : " );
Alan Mishchenko committed
424
            Hop_ObjPrintVerilog( pFile, Hop_NotCond(pFanin0, fCompl), vLevels, Level+1 );
Alan Mishchenko committed
425 426 427 428 429 430
            fprintf( pFile, "%s", (Level==0? "" : ")") );
        }
        return;
    }
    // AND case
    Vec_VecExpand( vLevels, Level );
431
    vSuper = Vec_VecEntry(vLevels, Level);
Alan Mishchenko committed
432
    Hop_ObjCollectMulti( pObj, vSuper );
Alan Mishchenko committed
433
    fprintf( pFile, "%s", (Level==0? "" : "(") );
434
    Vec_PtrForEachEntry( Hop_Obj_t *, vSuper, pFanin, i )
Alan Mishchenko committed
435
    {
Alan Mishchenko committed
436
        Hop_ObjPrintVerilog( pFile, Hop_NotCond(pFanin, fCompl), vLevels, Level+1 );
Alan Mishchenko committed
437 438 439 440 441 442 443 444
        if ( i < Vec_PtrSize(vSuper) - 1 )
            fprintf( pFile, " %s ", fCompl? "|" : "&" );
    }
    fprintf( pFile, "%s", (Level==0? "" : ")") );
    return;
}


Alan Mishchenko committed
445 446 447 448 449 450 451 452 453 454 455
/**Function*************************************************************

  Synopsis    [Prints node in HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
456
void Hop_ObjPrintVerbose( Hop_Obj_t * pObj, int fHaig )
Alan Mishchenko committed
457
{
Alan Mishchenko committed
458
    assert( !Hop_IsComplement(pObj) );
Alan Mishchenko committed
459
    printf( "Node %p : ", pObj );
Alan Mishchenko committed
460
    if ( Hop_ObjIsConst1(pObj) )
Alan Mishchenko committed
461
        printf( "constant 1" );
Alan Mishchenko committed
462
    else if ( Hop_ObjIsPi(pObj) )
Alan Mishchenko committed
463 464 465
        printf( "PI" );
    else
        printf( "AND( %p%s, %p%s )", 
Alan Mishchenko committed
466 467 468
            Hop_ObjFanin0(pObj), (Hop_ObjFaninC0(pObj)? "\'" : " "), 
            Hop_ObjFanin1(pObj), (Hop_ObjFaninC1(pObj)? "\'" : " ") );
    printf( " (refs = %3d)", Hop_ObjRefs(pObj) );
Alan Mishchenko committed
469 470 471 472 473 474 475 476 477 478 479 480 481
}

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

  Synopsis    [Prints node in HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
482
void Hop_ManPrintVerbose( Hop_Man_t * p, int fHaig )
Alan Mishchenko committed
483 484
{
    Vec_Ptr_t * vNodes;
Alan Mishchenko committed
485
    Hop_Obj_t * pObj;
Alan Mishchenko committed
486 487
    int i;
    printf( "PIs: " );
Alan Mishchenko committed
488
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
489 490
        printf( " %p", pObj );
    printf( "\n" );
Alan Mishchenko committed
491
    vNodes = Hop_ManDfs( p );
492
    Vec_PtrForEachEntry( Hop_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
493
        Hop_ObjPrintVerbose( pObj, fHaig ), printf( "\n" );
Alan Mishchenko committed
494
    printf( "\n" );
495
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
496 497
}

Alan Mishchenko committed
498 499 500 501 502 503 504 505 506
/**Function*************************************************************

  Synopsis    [Writes the AIG into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []
Alan Mishchenko committed
507

Alan Mishchenko committed
508
***********************************************************************/
Alan Mishchenko committed
509
void Hop_ManDumpBlif( Hop_Man_t * p, char * pFileName )
Alan Mishchenko committed
510 511 512
{
    FILE * pFile;
    Vec_Ptr_t * vNodes;
Alan Mishchenko committed
513
    Hop_Obj_t * pObj, * pConst1 = NULL;
Alan Mishchenko committed
514
    int i, nDigits, Counter = 0;
Alan Mishchenko committed
515
    if ( Hop_ManPoNum(p) == 0 )
Alan Mishchenko committed
516
    {
Alan Mishchenko committed
517
        printf( "Hop_ManDumpBlif(): AIG manager does not have POs.\n" );
Alan Mishchenko committed
518 519 520
        return;
    }
    // collect nodes in the DFS order
Alan Mishchenko committed
521
    vNodes = Hop_ManDfs( p );
Alan Mishchenko committed
522
    // assign IDs to objects
Alan Mishchenko committed
523
    Hop_ManConst1(p)->pData = (void *)(ABC_PTRUINT_T)Counter++;
Alan Mishchenko committed
524
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
525
        pObj->pData = (void *)(ABC_PTRUINT_T)Counter++;
Alan Mishchenko committed
526
    Hop_ManForEachPo( p, pObj, i )
Alan Mishchenko committed
527
        pObj->pData = (void *)(ABC_PTRUINT_T)Counter++;
528
    Vec_PtrForEachEntry( Hop_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
529
        pObj->pData = (void *)(ABC_PTRUINT_T)Counter++;
Alan Mishchenko committed
530
    nDigits = Hop_Base10Log( Counter );
Alan Mishchenko committed
531 532
    // write the file
    pFile = fopen( pFileName, "w" );
Alan Mishchenko committed
533
    fprintf( pFile, "# BLIF file written by procedure Hop_ManDumpBlif() in ABC\n" );
Alan Mishchenko committed
534 535 536 537
    fprintf( pFile, "# http://www.eecs.berkeley.edu/~alanmi/abc/\n" );
    fprintf( pFile, ".model test\n" );
    // write PIs
    fprintf( pFile, ".inputs" );
Alan Mishchenko committed
538
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
539
        fprintf( pFile, " n%0*d", nDigits, (int)(ABC_PTRUINT_T)pObj->pData );
Alan Mishchenko committed
540 541 542
    fprintf( pFile, "\n" );
    // write POs
    fprintf( pFile, ".outputs" );
Alan Mishchenko committed
543
    Hop_ManForEachPo( p, pObj, i )
Alan Mishchenko committed
544
        fprintf( pFile, " n%0*d", nDigits, (int)(ABC_PTRUINT_T)pObj->pData );
Alan Mishchenko committed
545 546
    fprintf( pFile, "\n" );
    // write nodes
547
    Vec_PtrForEachEntry( Hop_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
548 549
    {
        fprintf( pFile, ".names n%0*d n%0*d n%0*d\n", 
Alan Mishchenko committed
550 551 552
            nDigits, (int)(ABC_PTRUINT_T)Hop_ObjFanin0(pObj)->pData, 
            nDigits, (int)(ABC_PTRUINT_T)Hop_ObjFanin1(pObj)->pData, 
            nDigits, (int)(ABC_PTRUINT_T)pObj->pData );
Alan Mishchenko committed
553
        fprintf( pFile, "%d%d 1\n", !Hop_ObjFaninC0(pObj), !Hop_ObjFaninC1(pObj) );
Alan Mishchenko committed
554 555
    }
    // write POs
Alan Mishchenko committed
556
    Hop_ManForEachPo( p, pObj, i )
Alan Mishchenko committed
557 558
    {
        fprintf( pFile, ".names n%0*d n%0*d\n", 
Alan Mishchenko committed
559 560
            nDigits, (int)(ABC_PTRUINT_T)Hop_ObjFanin0(pObj)->pData, 
            nDigits, (int)(ABC_PTRUINT_T)pObj->pData );
Alan Mishchenko committed
561 562 563
        fprintf( pFile, "%d 1\n", !Hop_ObjFaninC0(pObj) );
        if ( Hop_ObjIsConst1(Hop_ObjFanin0(pObj)) )
            pConst1 = Hop_ManConst1(p);
Alan Mishchenko committed
564 565
    }
    if ( pConst1 )
Alan Mishchenko committed
566
        fprintf( pFile, ".names n%0*d\n 1\n", nDigits, (int)(ABC_PTRUINT_T)pConst1->pData );
Alan Mishchenko committed
567 568 569 570
    fprintf( pFile, ".end\n\n" );
    fclose( pFile );
    Vec_PtrFree( vNodes );
}
Alan Mishchenko committed
571

Alan Mishchenko committed
572 573 574 575 576
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


577 578
ABC_NAMESPACE_IMPL_END