ioWriteBook.c 31.6 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
/**CFile****************************************************************

  FileName    [ioWriteBook.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write Bookshelf files.]

  Author      [Myungchul Kim]
  
  Affiliation [U of Michigan]

  Date        [Ver. 1.0. Started - October 25, 2008.]

  Revision    [$Id: ioWriteBook.c,v 1.00 2005/11/10 00:00:00 mckima Exp $]

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

21 22
#include <math.h>

23 24
#include "base/main/main.h"
#include "map/mio/mio.h"
Alan Mishchenko committed
25
#include "ioAbc.h"
26 27 28

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
29 30 31 32 33 34 35 36 37 38 39 40
#define    NODES       0
#define    PL       1
#define coreHeight 1
#define termWidth  1
#define termHeight 1

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

static unsigned Io_NtkWriteNodes( FILE * pFile, Abc_Ntk_t * pNtk );
static void Io_NtkWritePiPoNodes( FILE * pFile, Abc_Ntk_t * pNtk );
41 42
static void Io_NtkWriteLatchNode( FILE * pFile, Abc_Obj_t * pLatch, int NodesOrPl );
static unsigned Io_NtkWriteIntNode( FILE * pFile, Abc_Obj_t * pNode, int NodesOrPl );
Alan Mishchenko committed
43 44 45 46 47 48 49
static unsigned Io_NtkWriteNodeGate( FILE * pFile, Abc_Obj_t * pNode );
static void Io_NtkWriteNets( FILE * pFile, Abc_Ntk_t * pNtk );
static void Io_NtkWriteIntNet( FILE * pFile, Abc_Obj_t * pNode );
static void Io_NtkBuildLayout( FILE * pFile1, FILE *pFile2, Abc_Ntk_t * pNtk, double aspectRatio, double whiteSpace, unsigned coreCellArea );
static void Io_NtkWriteScl( FILE * pFile, unsigned numCoreRows, double layoutWidth );
static void Io_NtkWritePl( FILE * pFile, Abc_Ntk_t * pNtk, unsigned numTerms, double layoutHeight, double layoutWidth );
static Vec_Ptr_t * Io_NtkOrderingPads( Abc_Ntk_t * pNtk, Vec_Ptr_t * vTerms ); 
50 51 52 53 54 55 56
static Abc_Obj_t * Io_NtkBfsPads( Abc_Ntk_t * pNtk, Abc_Obj_t * pCurrEntry, unsigned numTerms, int * pOrdered );
static int Abc_NodeIsNand2( Abc_Obj_t * pNode );
static int Abc_NodeIsNor2( Abc_Obj_t * pNode );
static int Abc_NodeIsAnd2( Abc_Obj_t * pNode );
static int Abc_NodeIsOr2( Abc_Obj_t * pNode );
static int Abc_NodeIsXor2( Abc_Obj_t * pNode );
static int Abc_NodeIsXnor2( Abc_Obj_t * pNode );
Alan Mishchenko committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

static inline double Abc_Rint( double x )   { return (double)(int)x;  }

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

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

  Synopsis    [Write the network into a Bookshelf file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_WriteBookLogic( Abc_Ntk_t * pNtk, char * FileName )
{
    Abc_Ntk_t * pNtkTemp;
    // derive the netlist
    pNtkTemp = Abc_NtkToNetlist(pNtk);
    if ( pNtkTemp == NULL )
    {
        fprintf( stdout, "Writing BOOK has failed.\n" );
        return;
    }
    Io_WriteBook( pNtkTemp, FileName );
    Abc_NtkDelete( pNtkTemp );
}

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

  Synopsis    [Write the network into a BOOK file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_WriteBook( Abc_Ntk_t * pNtk, char * FileName )
{

    FILE * pFileNodes, * pFileNets, * pFileAux;
    FILE * pFileScl, * pFilePl, * pFileWts;
Alan Mishchenko committed
105
    char * FileExt = ABC_CALLOC( char, strlen(FileName)+7 );
Alan Mishchenko committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    unsigned coreCellArea=0;
    Abc_Ntk_t * pExdc, * pNtkTemp;
    int i;

    assert( Abc_NtkIsNetlist(pNtk) );
    // start writing the files
    strcpy(FileExt, FileName);
    pFileNodes = fopen( strcat(FileExt,".nodes"), "w" );
    strcpy(FileExt, FileName);
    pFileNets  = fopen( strcat(FileExt,".nets"), "w" );
    strcpy(FileExt, FileName);
    pFileAux   = fopen( strcat(FileExt,".aux"), "w" );

        // write the aux file
    if ( (pFileNodes == NULL) || (pFileNets == NULL) || (pFileAux == NULL) )
    {
122
        fclose( pFileAux );
Alan Mishchenko committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        fprintf( stdout, "Io_WriteBook(): Cannot open the output files.\n" );
        return;
    }
    fprintf( pFileAux, "RowBasedPlacement : %s.nodes %s.nets %s.scl %s.pl %s.wts", 
         FileName, FileName, FileName, FileName, FileName );
    fclose( pFileAux );

    // write the master network
    coreCellArea+=Io_NtkWriteNodes( pFileNodes, pNtk );
    Io_NtkWriteNets( pFileNets, pNtk );

    // write EXDC network if it exists
    pExdc = Abc_NtkExdc( pNtk );
    if ( pExdc )
    {
    coreCellArea+=Io_NtkWriteNodes( pFileNodes, pNtk );
        Io_NtkWriteNets( pFileNets, pNtk );
    }

    // make sure there is no logic hierarchy
    assert( Abc_NtkWhiteboxNum(pNtk) == 0 );

    // write the hierarchy if present
    if ( Abc_NtkBlackboxNum(pNtk) > 0 )
    {
148
        Vec_PtrForEachEntry( Abc_Ntk_t *, pNtk->pDesign->vModules, pNtkTemp, i )
Alan Mishchenko committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        {
            if ( pNtkTemp == pNtk )
                continue;
            coreCellArea+=Io_NtkWriteNodes( pFileNodes, pNtkTemp );
            Io_NtkWriteNets( pFileNets, pNtkTemp );
        }
    }
    fclose( pFileNodes );
    fclose( pFileNets );

    strcpy(FileExt, FileName);
    pFileScl = fopen( strcat(FileExt,".scl"), "w" );
    strcpy(FileExt, FileName);
    pFilePl  = fopen( strcat(FileExt,".pl"), "w" );
    strcpy(FileExt, FileName);
    pFileWts   = fopen( strcat(FileExt,".wts"), "w" );
Alan Mishchenko committed
165
    ABC_FREE(FileExt);
Alan Mishchenko committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    Io_NtkBuildLayout( pFileScl, pFilePl, pNtk, 1.0, 10, coreCellArea );
    fclose( pFileScl );
    fclose( pFilePl );
    fclose( pFileWts );
}

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

  Synopsis    [Write the network into a BOOK file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Io_NtkWriteNodes( FILE * pFile, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Obj_t * pLatch, * pNode;
    unsigned numTerms, numNodes, coreCellArea=0;
    int i;

    assert( Abc_NtkIsNetlist(pNtk) );
    // write the forehead
    numTerms=Abc_NtkPiNum(pNtk)+Abc_NtkPoNum(pNtk);
    numNodes=numTerms+Abc_NtkNodeNum(pNtk)+Abc_NtkLatchNum(pNtk);
    printf("NumNodes : %d\t", numNodes );
    printf("NumTerminals : %d\n", numTerms );
    fprintf( pFile, "UCLA    nodes    1.0\n");
    fprintf( pFile, "NumNodes : %d\n", numNodes );
    fprintf( pFile, "NumTerminals : %d\n", numTerms );
    // write the PI/POs
    Io_NtkWritePiPoNodes( pFile, pNtk );
    // write the latches
    if ( !Abc_NtkIsComb(pNtk) )
    {
Alan Mishchenko committed
205 206 207 208 209
        Abc_NtkForEachLatch( pNtk, pLatch, i )
        {
            Io_NtkWriteLatchNode( pFile, pLatch, NODES );
            coreCellArea+=6*coreHeight;
        }
Alan Mishchenko committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }
    // write each internal node
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkNodeNum(pNtk) );
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        coreCellArea+=Io_NtkWriteIntNode( pFile, pNode, NODES );
    }
    Extra_ProgressBarStop( pProgress );
    return coreCellArea;
}

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

  Synopsis    [Writes the primary input nodes into a file]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWritePiPoNodes( FILE * pFile, Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pTerm, * pNet;
    int i;

    Abc_NtkForEachPi( pNtk, pTerm, i )
    {
        pNet = Abc_ObjFanout0(pTerm);
    fprintf( pFile, "i%s_input\t", Abc_ObjName(pNet) ); 
    fprintf( pFile, "terminal ");
    fprintf( pFile, " %d %d\n", termWidth, termHeight );
    }
    
    Abc_NtkForEachPo( pNtk, pTerm, i )
    {
    pNet = Abc_ObjFanin0(pTerm);
    fprintf( pFile, "o%s_output\t", Abc_ObjName(pNet) ); 
    fprintf( pFile, "terminal ");
    fprintf( pFile, " %d %d\n", termWidth, termHeight );
    }
}

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

  Synopsis    [Write the latch nodes into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
266
void Io_NtkWriteLatchNode( FILE * pFile, Abc_Obj_t * pLatch, int NodesOrPl )
Alan Mishchenko committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
    Abc_Obj_t * pNetLi, * pNetLo;
    
    pNetLi = Abc_ObjFanin0( Abc_ObjFanin0(pLatch) );
    pNetLo = Abc_ObjFanout0( Abc_ObjFanout0(pLatch) );
    /// write the latch line
    fprintf( pFile, "%s_%s_latch\t", Abc_ObjName(pNetLi), Abc_ObjName(pNetLo) );
    if (NodesOrPl == NODES)
        fprintf( pFile, " %d %d\n", 6, 1 );
}

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

  Synopsis    [Write the internal node into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
289
unsigned Io_NtkWriteIntNode( FILE * pFile, Abc_Obj_t * pNode, int NodesOrPl )
Alan Mishchenko committed
290 291 292 293 294 295 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 323 324 325 326 327
{
    unsigned sizex=0, sizey=coreHeight, isize=0;
    //double nx, ny, xstep, ystep;    
    Abc_Obj_t * pNeti, *pNeto;
    int i;
        
    // write the network after mapping 
    if ( Abc_NtkHasMapping(pNode->pNtk) )
        sizex=Io_NtkWriteNodeGate( pFile, pNode );
    else
    {
    Abc_ObjForEachFanin( pNode, pNeti, i )
        fprintf( pFile, "%s_", Abc_ObjName(pNeti) );
    Abc_ObjForEachFanout( pNode, pNeto, i )
        fprintf( pFile, "%s_", Abc_ObjName(pNeto) );    
    fprintf( pFile, "name\t" );
    
    if(NodesOrPl == NODES)
    {
        isize=Abc_ObjFaninNum(pNode);
        if ( Abc_NodeIsConst0(pNode) || Abc_NodeIsConst1(pNode) ) 
            sizex=0;
        else if ( Abc_NodeIsInv(pNode) )
            sizex=1;
        else if ( Abc_NodeIsBuf(pNode) )
            sizex=2;
        else
        {
            assert( Abc_NtkHasSop(pNode->pNtk) );
            if ( Abc_NodeIsNand2(pNode) || Abc_NodeIsNor2(pNode) )
            sizex=2;
        else if ( Abc_NodeIsAnd2(pNode) || Abc_NodeIsOr2(pNode) )
            sizex=3;
        else if ( Abc_NodeIsXor2(pNode) || Abc_NodeIsXnor2(pNode) )
            sizex=5;
        else
        {
            assert( isize > 2 );
328
            sizex=isize+Abc_SopGetCubeNum((char *)pNode->pData);
Alan Mishchenko committed
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        }
        }
    }
    }
    if(NodesOrPl == NODES)
    {
    fprintf( pFile, " %d %d\n", sizex, sizey );

    // Equally place pins. Size pins needs /  isize+#output+1
    isize= isize + Abc_ObjFanoutNum(pNode) + 1;
    }
    return sizex*sizey;
    /*
    xstep = sizex / isize;
    ystep = sizey / isize;
    nx= -0.5 * sizex;
    ny= -0.5 * sizey;

    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
            nx+= xstep;
            ny+= ystep;
            if (fabs(nx) < 0.001)
                    nx= 0;
            if (fabs(ny) < 0.001)
                    ny= 0;
    }
    Abc_ObjForEachFanout( pNode, pFanout, i )
    {
            nx+= xstep;
            ny+= ystep;
            if (fabs(nx) < 0.001)
                    nx= 0;
            if (fabs(ny) < 0.001)
                    ny= 0;
    }
    */
}

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

  Synopsis    [Writes the internal node after tech mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Io_NtkWriteNodeGate( FILE * pFile, Abc_Obj_t * pNode )
{
381
    Mio_Gate_t * pGate = (Mio_Gate_t *)pNode->pData;
Alan Mishchenko committed
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 410 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
    Mio_Pin_t * pGatePin;
    int i;
    // write the node gate
    for ( pGatePin = Mio_GateReadPins(pGate), i = 0; pGatePin; pGatePin = Mio_PinReadNext(pGatePin), i++ )
        fprintf( pFile, "%s_", Abc_ObjName( Abc_ObjFanin(pNode,i) ) );
    assert ( i == Abc_ObjFaninNum(pNode) );
    fprintf( pFile, "%s_%s\t", Abc_ObjName( Abc_ObjFanout0(pNode) ), Mio_GateReadName(pGate) );
    return Mio_GateReadArea(pGate);
}

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

  Synopsis    [Write the nets into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWriteNets( FILE * pFile, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Obj_t * pNet;
    unsigned numPin=0;
    int i;

    assert( Abc_NtkIsNetlist(pNtk) );
    // write the head
    Abc_NtkForEachNet( pNtk, pNet, i )
    numPin+=Abc_ObjFaninNum(pNet)+Abc_ObjFanoutNum(pNet);
    printf( "NumNets  : %d\t", Abc_NtkNetNum(pNtk) );
    printf( "NumPins      : %d\n\n", numPin );
    fprintf( pFile, "UCLA    nets    1.0\n");
    fprintf( pFile, "NumNets : %d\n", Abc_NtkNetNum(pNtk) );
    fprintf( pFile, "NumPins : %d\n", numPin );

    // write nets
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkNetNum(pNtk) );
    Abc_NtkForEachNet( pNtk, pNet, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        Io_NtkWriteIntNet( pFile, pNet );
    }
    Extra_ProgressBarStop( pProgress );
}

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

  Synopsis    [Write the nets into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWriteIntNet( FILE * pFile, Abc_Obj_t * pNet )
{
    Abc_Obj_t * pFanin, * pFanout;
    Abc_Obj_t * pNeti, * pNeto;
    Abc_Obj_t * pNetLi, * pNetLo, * pLatch;
    int i, j;
    int NetDegree=Abc_ObjFaninNum(pNet)+Abc_ObjFanoutNum(pNet);

    fprintf( pFile, "NetDegree\t:\t\t%d\t\t%s\n", NetDegree, Abc_ObjName(Abc_ObjFanin0(pNet)) );

    pFanin=Abc_ObjFanin0(pNet);
    if ( Abc_ObjIsPi(pFanin) )
    fprintf( pFile, "i%s_input I\n", Abc_ObjName(pNet) );
    else 
    {
    if(!Abc_NtkIsComb(pNet->pNtk) && Abc_ObjFaninNum(pFanin) && Abc_ObjIsLatch(Abc_ObjFanin0(pFanin)) )
    {
        pLatch=Abc_ObjFanin0(pFanin);
        pNetLi=Abc_ObjFanin0(Abc_ObjFanin0(pLatch));
        pNetLo=Abc_ObjFanout0(Abc_ObjFanout0(pLatch));
        fprintf( pFile, "%s_%s_latch I : ", Abc_ObjName(pNetLi), Abc_ObjName(pNetLo) );
    }
    else
    {
        Abc_ObjForEachFanin( pFanin, pNeti, j )
        fprintf( pFile, "%s_", Abc_ObjName(pNeti) );
        Abc_ObjForEachFanout( pFanin, pNeto, j )
        fprintf( pFile, "%s_", Abc_ObjName(pNeto) );    
        if ( Abc_NtkHasMapping(pNet->pNtk) )
470
        fprintf( pFile, "%s : ", Mio_GateReadName((Mio_Gate_t *)pFanin->pData) ); 
Alan Mishchenko committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        else
        fprintf( pFile, "name I : " );                                
    }
    // offsets are simlply 0.00 0.00 at the moment
    fprintf( pFile, "%.2f %.2f\n", .0, .0 );
    }

    Abc_ObjForEachFanout( pNet, pFanout, i )
    {
    if ( Abc_ObjIsPo(pFanout) )
        fprintf( pFile, "o%s_output O\n", Abc_ObjName(pNet) );
    else
    {
        if(!Abc_NtkIsComb(pNet->pNtk) && Abc_ObjFanoutNum(pFanout) && Abc_ObjIsLatch( Abc_ObjFanout0(pFanout) ) )
        {
        pLatch=Abc_ObjFanout0(pFanout);
        pNetLi=Abc_ObjFanin0(Abc_ObjFanin0(pLatch));
        pNetLo=Abc_ObjFanout0(Abc_ObjFanout0(pLatch));
        fprintf( pFile, "%s_%s_latch O : ", Abc_ObjName(pNetLi), Abc_ObjName(pNetLo) );
        }
        else
        {
        Abc_ObjForEachFanin( pFanout, pNeti, j )
        fprintf( pFile, "%s_", Abc_ObjName(pNeti) );
        Abc_ObjForEachFanout( pFanout, pNeto, j )
        fprintf( pFile, "%s_", Abc_ObjName(pNeto) );    
        if ( Abc_NtkHasMapping(pNet->pNtk) )
498
            fprintf( pFile, "%s : ", Mio_GateReadName((Mio_Gate_t *)pFanout->pData) ); 
Alan Mishchenko committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
        else
            fprintf( pFile, "name O : " );
        }
    // offsets are simlply 0.00 0.00 at the moment
    fprintf( pFile, "%.2f %.2f\n", .0, .0 );
    }
    }
}

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

  Synopsis    [Write the network into a BOOK file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkBuildLayout( FILE * pFileScl, FILE * pFilePl, Abc_Ntk_t * pNtk, double aspectRatio, double whiteSpace, unsigned coreCellArea )
{
    unsigned numCoreCells=Abc_NtkNodeNum(pNtk)+Abc_NtkLatchNum(pNtk);
    double targetLayoutArea = coreCellArea/(1.0-(whiteSpace/100.0));
    unsigned numCoreRows=(aspectRatio>0.0) ? (Abc_Rint(sqrt(targetLayoutArea/aspectRatio)/coreHeight)) : 0;
    unsigned numTerms=Abc_NtkPiNum(pNtk)+Abc_NtkPoNum(pNtk);
    unsigned totalWidth=coreCellArea/coreHeight;
    double layoutHeight = numCoreRows * coreHeight;
    double layoutWidth = Abc_Rint(targetLayoutArea/layoutHeight);
    double actualLayoutArea = layoutWidth * layoutHeight;

    printf( "Core cell height(==site height) is %d\n", coreHeight );
    printf( "Total core cell width is %d giving an ave width of %f\n", totalWidth, (double)(totalWidth/numCoreCells));
    printf( "Target Dimensions:\n" );
    printf( "  Area  :   %f\n", targetLayoutArea );
    printf( "  WS%%   :   %f\n", whiteSpace );
    printf( "  AR    :   %f\n", aspectRatio );
    printf( "Actual Dimensions:\n" );
    printf( "  Width :   %f\n", layoutWidth );
    printf( "  Height:   %f (%d rows)\n", layoutHeight, numCoreRows);
    printf( "  Area  :   %f\n", actualLayoutArea );
    printf( "  WS%%   :   %f\n", 100*(actualLayoutArea-coreCellArea)/actualLayoutArea );
    printf( "  AR    :   %f\n\n", layoutWidth/layoutHeight );    

    Io_NtkWriteScl( pFileScl, numCoreRows, layoutWidth ); 
    Io_NtkWritePl( pFilePl, pNtk, numTerms, layoutHeight, layoutWidth );
}

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

  Synopsis    [Write the network into a BOOK file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWriteScl( FILE * pFile, unsigned numCoreRows, double layoutWidth )
{
    int origin_y=0;
    char * rowOrients[2] = {"N", "FS"};
    char symmetry='Y';
    double sitewidth=1.0;
    double spacing=1.0;
        
Alan Mishchenko committed
566
    unsigned rowId;
Alan Mishchenko committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    // write the forehead
    fprintf( pFile, "UCLA    scl    1.0\n\n" );
    fprintf( pFile, "Numrows : %d\n\n", numCoreRows );

    for( rowId=0 ; rowId<numCoreRows ; rowId++, origin_y += coreHeight )
    {
    fprintf( pFile, "CoreRow Horizontal\n" );
    fprintf( pFile, " Coordinate   : \t%d\n", origin_y);
    fprintf( pFile, " Height       : \t%d\n", coreHeight);
    fprintf( pFile, " Sitewidth    : \t%d\n", (unsigned)sitewidth );
    fprintf( pFile, " Sitespacing  : \t%d\n", (unsigned)spacing );
    fprintf( pFile, " Siteorient   : \t%s\n", rowOrients[rowId%2] );
    //if( coreRow[i].site.symmetry.rot90 || coreRow[i].site.symmetry.y || coreRow[i].site.symmetry.x )
    fprintf( pFile, " Sitesymmetry : \t%c\n", symmetry );
    //else fprintf( pFile, "Sitesymmetry            : \t\t\t1\n" );
    fprintf( pFile, " SubrowOrigin : \t%d Numsites :    \t%d\n", 0, (unsigned)layoutWidth );
    fprintf( pFile, "End\n" );
    }
}

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

  Synopsis    [Write the network into a BOOK file with the given name.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWritePl( FILE * pFile, Abc_Ntk_t * pNtk, unsigned numTerms, double layoutWidth, double layoutHeight )
{
    Abc_Obj_t * pTerm, * pLatch, * pNode;
    Vec_Ptr_t * vTerms = Vec_PtrAlloc ( numTerms ); 
    Vec_Ptr_t * vOrderedTerms = Vec_PtrAlloc ( numTerms ); 
    double layoutPerim = 2*layoutWidth + 2*layoutHeight;
    double nextLoc_x, nextLoc_y;
    double delta;
    unsigned termsOnTop, termsOnBottom, termsOnLeft, termsOnRight;
Alan Mishchenko committed
607 608
    unsigned t;
    int i;
Alan Mishchenko committed
609 610 611 612 613 614 615 616 617 618 619 620

    termsOnTop = termsOnBottom = (unsigned)(Abc_Rint(numTerms*(layoutWidth/layoutPerim)));
    termsOnLeft = numTerms - (termsOnTop+termsOnBottom);
    termsOnRight = (unsigned)(ceil(termsOnLeft/2.0));
    termsOnLeft -= termsOnRight;

    Abc_NtkForEachPi( pNtk, pTerm, i )
    Vec_PtrPush( vTerms, pTerm );    
    Abc_NtkForEachPo( pNtk, pTerm, i )
    Vec_PtrPush( vTerms, pTerm );
    // Ordering Pads
    vOrderedTerms=Io_NtkOrderingPads( pNtk, vTerms );
Alan Mishchenko committed
621
    assert( termsOnTop+termsOnBottom+termsOnLeft+termsOnRight == (unsigned)Vec_PtrSize(vOrderedTerms) );
Alan Mishchenko committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

    printf( "Done constructing layout region\n" );
    printf( "Terminals: %d\n", numTerms );
    printf( "  Top:     %d\n", termsOnTop );
    printf( "  Bottom:  %d\n", termsOnBottom );
    printf( "  Left:    %d\n", termsOnLeft );
    printf( "  Right:   %d\n", termsOnRight );

    fprintf( pFile, "UCLA    pl    1.0\n\n" );

    nextLoc_x = floor(.0);
    nextLoc_y = ceil(layoutHeight + 2*coreHeight);
    delta   = layoutWidth / termsOnTop;
    for(t = 0; t < termsOnTop; t++)
    {
637
    pTerm = (Abc_Obj_t *)Vec_PtrEntry( vOrderedTerms, t );
Alan Mishchenko committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    if( Abc_ObjIsPi(pTerm) )
        fprintf( pFile, "i%s_input\t\t", Abc_ObjName(Abc_ObjFanout0(pTerm)) ); 
    else
        fprintf( pFile, "o%s_output\t\t", Abc_ObjName(Abc_ObjFanin0(pTerm)) ); 
    if( t && Abc_Rint(nextLoc_x) < Abc_Rint(nextLoc_x-delta)+termWidth )
        nextLoc_x++;
    fprintf( pFile, "%d\t\t%d\t: %s /FIXED\n", (int)Abc_Rint(nextLoc_x), (int)Abc_Rint(nextLoc_y), "FS" );    
    nextLoc_x += delta;
    }

    nextLoc_x = floor(.0);
    nextLoc_y = floor(.0 - 2*coreHeight - termHeight);
    delta   = layoutWidth / termsOnBottom;
    for(;t < termsOnTop+termsOnBottom; t++)
    {
653
    pTerm = (Abc_Obj_t *)Vec_PtrEntry( vOrderedTerms, t );
Alan Mishchenko committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    if( Abc_ObjIsPi(pTerm) )
        fprintf( pFile, "i%s_input\t\t", Abc_ObjName(Abc_ObjFanout0(pTerm)) ); 
    else
        fprintf( pFile, "o%s_output\t\t", Abc_ObjName(Abc_ObjFanin0(pTerm)) ); 
    if( t!=termsOnTop && Abc_Rint(nextLoc_x) < Abc_Rint(nextLoc_x-delta)+termWidth )
        nextLoc_x++;
    fprintf( pFile, "%d\t\t%d\t: %s /FIXED\n", (int)Abc_Rint(nextLoc_x), (int)Abc_Rint(nextLoc_y), "N" );    
    nextLoc_x     += delta;
    }

    nextLoc_x = floor(.0-2*coreHeight-termWidth);
    nextLoc_y = floor(.0);
    delta   = layoutHeight / termsOnLeft;
    for(;t < termsOnTop+termsOnBottom+termsOnLeft; t++)
    {
669
    pTerm = (Abc_Obj_t *)Vec_PtrEntry( vOrderedTerms, t );
Alan Mishchenko committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    if( Abc_ObjIsPi(pTerm) )
        fprintf( pFile, "i%s_input\t\t", Abc_ObjName(Abc_ObjFanout0(pTerm)) ); 
    else
        fprintf( pFile, "o%s_output\t\t", Abc_ObjName(Abc_ObjFanin0(pTerm)) ); 
    if( Abc_Rint(nextLoc_y) < Abc_Rint(nextLoc_y-delta)+termHeight )
        nextLoc_y++;
    fprintf( pFile, "%d\t\t%d\t: %s /FIXED\n", (int)Abc_Rint(nextLoc_x), (int)Abc_Rint(nextLoc_y), "E" );    
    nextLoc_y     += delta;
    }

    nextLoc_x = ceil(layoutWidth+2*coreHeight);
    nextLoc_y = floor(.0);
    delta   = layoutHeight / termsOnRight;
    for(;t < termsOnTop+termsOnBottom+termsOnLeft+termsOnRight; t++)
    {
685
           pTerm = (Abc_Obj_t *)Vec_PtrEntry( vOrderedTerms, t );
Alan Mishchenko committed
686 687 688 689 690 691 692 693 694 695 696
    if( Abc_ObjIsPi(pTerm) )
        fprintf( pFile, "i%s_input\t\t", Abc_ObjName(Abc_ObjFanout0(pTerm)) ); 
    else
        fprintf( pFile, "o%s_output\t\t", Abc_ObjName(Abc_ObjFanin0(pTerm)) ); 
    if( Abc_Rint(nextLoc_y) < Abc_Rint(nextLoc_y-delta)+termHeight )
        nextLoc_y++;
    fprintf( pFile, "%d\t\t%d\t: %s /FIXED\n", (int)Abc_Rint(nextLoc_x), (int)Abc_Rint(nextLoc_y), "FW" );    
    nextLoc_y     += delta;
    }

    if( !Abc_NtkIsComb(pNtk) )
Alan Mishchenko committed
697
    {
Alan Mishchenko committed
698 699 700 701 702
    Abc_NtkForEachLatch( pNtk, pLatch, i )
    {
        Io_NtkWriteLatchNode( pFile, pLatch, PL );
        fprintf( pFile, "\t%d\t\t%d\t: %s\n", 0, 0, "N" );    
    }
Alan Mishchenko committed
703
    }
Alan Mishchenko committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
    Io_NtkWriteIntNode( pFile, pNode, PL );
    fprintf( pFile, "\t%d\t\t%d\t: %s\n", 0, 0, "N" );    
    }
}

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

  Synopsis    [Returns the closest I/O to a given I/O.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Io_NtkOrderingPads( Abc_Ntk_t * pNtk, Vec_Ptr_t * vTerms )
{ 
    ProgressBar * pProgress;
    unsigned numTerms=Vec_PtrSize(vTerms); 
    unsigned termIdx=0, termCount=0;
728 729
    int * pOrdered = ABC_ALLOC(int, numTerms); 
    int newNeighbor=1;
Alan Mishchenko committed
730
    Vec_Ptr_t * vOrderedTerms = Vec_PtrAlloc ( numTerms );
Alan Mishchenko committed
731
    Abc_Obj_t * pNeighbor = NULL, * pNextTerm;     
Alan Mishchenko committed
732
    unsigned i; 
Alan Mishchenko committed
733 734 735 736

    for( i=0 ; i<numTerms ; i++ )
    pOrdered[i]=0; 
   
737
    pNextTerm = (Abc_Obj_t *)Vec_PtrEntry(vTerms, termIdx++);
Alan Mishchenko committed
738 739 740 741 742
    pProgress = Extra_ProgressBarStart( stdout, numTerms );
    while( termCount < numTerms && termIdx < numTerms )
    {
    if( pOrdered[Abc_ObjId(pNextTerm)] && !newNeighbor )
    {
743
        pNextTerm = (Abc_Obj_t *)Vec_PtrEntry( vTerms, termIdx++ );
Alan Mishchenko committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
        continue;
    }
    if(!Vec_PtrPushUnique( vOrderedTerms, pNextTerm ))
    {
        pOrdered[Abc_ObjId(pNextTerm)]=1;
        termCount++; 
    }
    pNeighbor=Io_NtkBfsPads( pNtk, pNextTerm, numTerms, pOrdered );
    if( (newNeighbor=!Vec_PtrPushUnique( vOrderedTerms, pNeighbor )) )
    {
       pOrdered[Abc_ObjId(pNeighbor)]=1;
       termCount++;
       pNextTerm=pNeighbor;
    }
    else if(termIdx < numTerms)
759
        pNextTerm = (Abc_Obj_t *)Vec_PtrEntry( vTerms, termIdx++ );
Alan Mishchenko committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

    Extra_ProgressBarUpdate( pProgress, termCount, NULL );
    }
    Extra_ProgressBarStop( pProgress );
    assert(termCount==numTerms);
    return vOrderedTerms;
}

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

  Synopsis    [Returns the closest I/O to a given I/O.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
779
Abc_Obj_t * Io_NtkBfsPads( Abc_Ntk_t * pNtk, Abc_Obj_t * pTerm, unsigned numTerms, int * pOrdered )
Alan Mishchenko committed
780 781 782
{
    Vec_Ptr_t * vNeighbors = Vec_PtrAlloc ( numTerms ); 
    Abc_Obj_t * pNet, * pNode, * pNeighbor;
783
    int foundNeighbor=0;
Alan Mishchenko committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    int i;

    assert(Abc_ObjIsPi(pTerm) || Abc_ObjIsPo(pTerm) );
    Abc_NtkIncrementTravId ( pNtk );
    Abc_NodeSetTravIdCurrent( pTerm );
    if(Abc_ObjIsPi(pTerm))
    {
    pNet = Abc_ObjFanout0(pTerm);
    Abc_ObjForEachFanout( pNet, pNode, i ) 
        Vec_PtrPush( vNeighbors, pNode );
    }
    else 
    {
    pNet = Abc_ObjFanin0(pTerm);
    Abc_ObjForEachFanin( pNet, pNode, i ) 
        Vec_PtrPush( vNeighbors, pNode );   
    }

    while ( Vec_PtrSize(vNeighbors) >0 )
    {
804
    pNeighbor = (Abc_Obj_t *)Vec_PtrEntry( vNeighbors, 0 );
Alan Mishchenko committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    assert( Abc_ObjIsNode(pNeighbor) || Abc_ObjIsTerm(pNeighbor) );
    Vec_PtrRemove( vNeighbors, pNeighbor );

    if( Abc_NodeIsTravIdCurrent( pNeighbor ) )
        continue;
    Abc_NodeSetTravIdCurrent( pNeighbor );

    if( ((Abc_ObjIsPi(pNeighbor) || Abc_ObjIsPo(pNeighbor))) && !pOrdered[Abc_ObjId(pNeighbor)] )
    {
        foundNeighbor=1;
        break;
    }
    if( Abc_ObjFanoutNum( pNeighbor ) )
    {   
        pNet=Abc_ObjFanout0( pNeighbor );
        if( !Abc_NtkIsComb(pNtk) && Abc_ObjIsLatch(pNet) )
        pNet=Abc_ObjFanout0( Abc_ObjFanout0(pNet) );
        Abc_ObjForEachFanout( pNet, pNode, i )
        if( !Abc_NodeIsTravIdCurrent(pNode) ) 
            Vec_PtrPush( vNeighbors, pNode ); 
    }
    if( Abc_ObjFaninNum( pNeighbor ) )
    {
        if( !Abc_NtkIsComb(pNtk) && Abc_ObjIsLatch(Abc_ObjFanin0(pNeighbor)) )
        pNeighbor=Abc_ObjFanin0( Abc_ObjFanin0(pNeighbor) );
        Abc_ObjForEachFanin( pNeighbor, pNet, i )
        if( !Abc_NodeIsTravIdCurrent(pNode=Abc_ObjFanin0(pNet)) ) 
            Vec_PtrPush( vNeighbors, pNode );
    }
    }
    return ( foundNeighbor ) ? pNeighbor : pTerm;
} 

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

  Synopsis    [Test is the node is nand2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
849
int Abc_NodeIsNand2( Abc_Obj_t * pNode )
Alan Mishchenko committed
850 851 852 853 854 855 856
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
857 858 859
    return ( !strcmp(((char *)pNode->pData), "-0 1\n0- 1\n") || 
         !strcmp(((char *)pNode->pData), "0- 1\n-0 1\n") || 
         !strcmp(((char *)pNode->pData), "11 0\n") );
Alan Mishchenko committed
860
    if ( Abc_NtkHasMapping(pNtk) )
861
        return pNode->pData == (void *)Mio_LibraryReadNand2((Mio_Library_t *)Abc_FrameReadLibGen());
Alan Mishchenko committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    assert( 0 );
    return 0;
}

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

  Synopsis    [Test is the node is nand2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
877
int Abc_NodeIsNor2( Abc_Obj_t * pNode )
Alan Mishchenko committed
878 879 880 881 882 883 884
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
885
        return ( !strcmp(((char *)pNode->pData), "00 1\n") );
Alan Mishchenko committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    assert( 0 );
    return 0;
}

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

  Synopsis    [Test is the node is and2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
901
int Abc_NodeIsAnd2( Abc_Obj_t * pNode )
Alan Mishchenko committed
902 903 904 905 906 907 908
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
909
    return Abc_SopIsAndType(((char *)pNode->pData));
Alan Mishchenko committed
910
    if ( Abc_NtkHasMapping(pNtk) )
911
        return pNode->pData == (void *)Mio_LibraryReadAnd2((Mio_Library_t *)Abc_FrameReadLibGen());
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    assert( 0 );
    return 0;
}

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

  Synopsis    [Test is the node is or2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
927
int Abc_NodeIsOr2( Abc_Obj_t * pNode )
Alan Mishchenko committed
928 929 930 931 932 933 934
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
935 936 937 938
    return ( Abc_SopIsOrType(((char *)pNode->pData))   || 
         !strcmp(((char *)pNode->pData), "01 0\n") ||
         !strcmp(((char *)pNode->pData), "10 0\n") ||
         !strcmp(((char *)pNode->pData), "00 0\n") );
Alan Mishchenko committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
         //off-sets, too
    assert( 0 );
    return 0;
}

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

  Synopsis    [Test is the node is xor2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
955
int Abc_NodeIsXor2( Abc_Obj_t * pNode )
Alan Mishchenko committed
956 957 958 959 960 961 962
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
963
    return ( !strcmp(((char *)pNode->pData), "01 1\n10 1\n") || !strcmp(((char *)pNode->pData), "10 1\n01 1\n")  );
Alan Mishchenko committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
    assert( 0 );
    return 0;
}

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

  Synopsis    [Test is the node is xnor2.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
979
int Abc_NodeIsXnor2( Abc_Obj_t * pNode )
Alan Mishchenko committed
980 981 982 983 984 985 986
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    assert( Abc_NtkIsNetlist(pNtk) );
    assert( Abc_ObjIsNode(pNode) ); 
    if ( Abc_ObjFaninNum(pNode) != 2 )
        return 0;
    if ( Abc_NtkHasSop(pNtk) )
987
    return ( !strcmp(((char *)pNode->pData), "11 1\n00 1\n") || !strcmp(((char *)pNode->pData), "00 1\n11 1\n") );
Alan Mishchenko committed
988 989 990 991 992 993 994 995 996
    assert( 0 );
    return 0;
}

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


997 998
ABC_NAMESPACE_IMPL_END