sclLibUtil.c 35.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/**CFile****************************************************************

  FileName    [sclLibUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Standard-cell library representation.]

  Synopsis    [Various library utilities.]

  Author      [Alan Mishchenko, Niklas Een]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 24, 2012.]

  Revision    [$Id: sclLibUtil.c,v 1.0 2012/08/24 00:00:00 alanmi Exp $]

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

#include "sclLib.h"
#include "misc/st/st.h"
#include "map/mio/mio.h"
#include "bool/kit/kit.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Reading library from file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static unsigned Abc_SclHashString( char * pName, int TableSize ) 
{
    static int s_Primes[10] = { 1291, 1699, 2357, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
    unsigned i, Key = 0;
    for ( i = 0; pName[i] != '\0'; i++ )
        Key += s_Primes[i%10]*pName[i]*pName[i];
    return Key % TableSize;
}
int * Abc_SclHashLookup( SC_Lib * p, char * pName )
{
    int i;
    for ( i = Abc_SclHashString(pName, p->nBins); i < p->nBins; i = (i + 1) % p->nBins )
        if ( p->pBins[i] == -1 || !strcmp(pName, SC_LibCell(p, p->pBins[i])->pName) )
            return p->pBins + i;
    assert( 0 );
    return NULL;
}
void Abc_SclHashCells( SC_Lib * p )
{
    SC_Cell * pCell;
    int i, * pPlace;
    assert( p->nBins == 0 );
70
    p->nBins = Abc_PrimeCudd( 5 * SC_LibCellNum(p) );
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    p->pBins = ABC_FALLOC( int, p->nBins );
    SC_LibForEachCell( p, pCell, i )
    {
        pPlace = Abc_SclHashLookup( p, pCell->pName );
        assert( *pPlace == -1 );
        *pPlace = i;
    }
}
int Abc_SclCellFind( SC_Lib * p, char * pName )
{
    int *pPlace = Abc_SclHashLookup( p, pName );
    return pPlace ? *pPlace : -1;
}
int Abc_SclClassCellNum( SC_Cell * pClass )
{
    SC_Cell * pCell;
    int i, Count = 0;
    SC_RingForEachCell( pClass, pCell, i )
        if ( !pCell->fSkip )
            Count++;
    return Count;
}
93 94 95 96 97 98 99 100
int Abc_SclLibClassNum( SC_Lib * pLib )
{
    SC_Cell * pRepr;
    int i, Count = 0;
    SC_LibForEachCellClass( pLib, pRepr, i )
        Count++;
    return Count;
}
101 102 103

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

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 148 149 150 151 152 153 154
  Synopsis    [Change cell names and pin names.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_SclIsChar( char c )
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
static inline int Abc_SclIsName( char c )
{
    return Abc_SclIsChar(c) || (c >= '0' && c <= '9');
}
static inline char * Abc_SclFindLimit( char * pName )
{
    assert( Abc_SclIsChar(*pName) );
    while ( Abc_SclIsName(*pName) )
        pName++;
    return pName;
}
static inline int Abc_SclAreEqual( char * pBase, char * pName, char * pLimit )
{
    return !strncmp( pBase, pName, pLimit - pName );
}
void Abc_SclShortFormula( SC_Cell * pCell, char * pForm, char * pBuffer )
{
    SC_Pin * pPin; int i;
    char * pTemp, * pLimit;
    for ( pTemp = pForm; *pTemp; )
    {
        if ( !Abc_SclIsChar(*pTemp) )
        {
            *pBuffer++ = *pTemp++;
            continue;
        }
        pLimit = Abc_SclFindLimit( pTemp );
        SC_CellForEachPinIn( pCell, pPin, i )
            if ( Abc_SclAreEqual( pPin->pName, pTemp, pLimit ) )
            {
                *pBuffer++ = 'a' + i;
                break;
            }
        assert( i < pCell->n_inputs );
        pTemp = pLimit;
    }
    *pBuffer++ = 0;
}
155 156 157 158 159 160 161 162 163 164 165 166 167 168
static inline void Abc_SclTimingUpdate( SC_Cell * pCell, SC_Timing * p, char * Buffer )
{
    SC_Pin * pPin; int i;
    SC_CellForEachPinIn( pCell, pPin, i )
        if ( p->related_pin && !strcmp(p->related_pin, pPin->pName) )
        {
            ABC_FREE( p->related_pin );
            sprintf( Buffer, "%c", 'a'+i );
            p->related_pin = Abc_UtilStrsav( Buffer );
        }
}
static inline void Abc_SclTimingsUpdate( SC_Cell * pCell, SC_Timings * p, char * Buffer )
{
    SC_Timing * pTemp; int i;
169
    Vec_PtrForEachEntry( SC_Timing *, &p->vTimings, pTemp, i )
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        Abc_SclTimingUpdate( pCell, pTemp, Buffer );
}
static inline void Abc_SclPinUpdate( SC_Cell * pCell, SC_Pin * p, char * Buffer )
{
    // update pin names in the timing
    SC_Timings * pTemp; int i;
    SC_PinForEachRTiming( p, pTemp, i )
    {
        SC_Pin * pPin; int k;
        Abc_SclTimingsUpdate( pCell, pTemp, Buffer );
        SC_CellForEachPinIn( pCell, pPin, k )
            if ( pTemp->pName && !strcmp(pTemp->pName, pPin->pName) )
            {
                ABC_FREE( pTemp->pName );
                sprintf( Buffer, "%c", 'a'+k );
                pTemp->pName = Abc_UtilStrsav( Buffer );
            }
    }
    // update formula
    Abc_SclShortFormula( pCell, p->func_text, Buffer );
    ABC_FREE( p->func_text );
    p->func_text = Abc_UtilStrsav( Buffer );
}
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
void Abc_SclShortNames( SC_Lib * p )
{
    char Buffer[10000];
    SC_Cell * pClass, * pCell; SC_Pin * pPin;
    int i, k, n, nClasses = Abc_SclLibClassNum(p);
    int nDigits = Abc_Base10Log( nClasses );
    // itereate through classes
    SC_LibForEachCellClass( p, pClass, i )
    {
        int nDigits2 = Abc_Base10Log( Abc_SclClassCellNum(pClass) );
        SC_RingForEachCell( pClass, pCell, k )
        {
            ABC_FREE( pCell->pName );
            sprintf( Buffer, "g%0*d_%0*d", nDigits, i, nDigits2, k );
            pCell->pName = Abc_UtilStrsav( Buffer );
            // formula
            SC_CellForEachPinOut( pCell, pPin, n )
210
                Abc_SclPinUpdate( pCell, pPin, Buffer );
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
            // pin names
            SC_CellForEachPinIn( pCell, pPin, n )
            {
                ABC_FREE( pPin->pName );
                sprintf( Buffer, "%c", 'a'+n );
                pPin->pName = Abc_UtilStrsav( Buffer );
            }
            SC_CellForEachPinOut( pCell, pPin, n )
            {
                ABC_FREE( pPin->pName );
                sprintf( Buffer, "%c", 'z'-n+pCell->n_inputs );
                pPin->pName = Abc_UtilStrsav( Buffer );
            }
        }
    }
    p->nBins = 0;
    ABC_FREE( p->pBins );
    Abc_SclHashCells( p );
229 230 231 232 233
    // update library name
    printf( "Renaming library \"%s\" into \"%s%d\".\n", p->pName, "lib", SC_LibCellNum(p) );
    ABC_FREE( p->pName );
    sprintf( Buffer, "lib%d", SC_LibCellNum(p) );
    p->pName = Abc_UtilStrsav( Buffer );
234 235 236
}
/**Function*************************************************************

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 266
  Synopsis    [Links equal gates into rings while sorting them by area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Abc_SclCompareCells( SC_Cell ** pp1, SC_Cell ** pp2 )
{
    if ( (*pp1)->n_inputs < (*pp2)->n_inputs )
        return -1;
    if ( (*pp1)->n_inputs > (*pp2)->n_inputs )
        return 1;
//    if ( (*pp1)->area < (*pp2)->area )
//        return -1;
//    if ( (*pp1)->area > (*pp2)->area )
//        return 1;
    if ( SC_CellPinCapAve(*pp1) < SC_CellPinCapAve(*pp2) )
        return -1;
    if ( SC_CellPinCapAve(*pp1) > SC_CellPinCapAve(*pp2) )
        return 1;
    return strcmp( (*pp1)->pName, (*pp2)->pName );
}
void Abc_SclLinkCells( SC_Lib * p )
{
    Vec_Ptr_t * vList;
    SC_Cell * pCell, * pRepr = NULL;
    int i, k;
267
    assert( Vec_PtrSize(&p->vCellClasses) == 0 );
268 269 270 271 272 273 274 275
    SC_LibForEachCell( p, pCell, i )
    {
        // find gate with the same function
        SC_LibForEachCellClass( p, pRepr, k )
            if ( pCell->n_inputs  == pRepr->n_inputs && 
                 pCell->n_outputs == pRepr->n_outputs && 
                 Vec_WrdEqual(SC_CellFunc(pCell), SC_CellFunc(pRepr)) )
                break;
276
        if ( k == Vec_PtrSize(&p->vCellClasses) )
277
        {
278
            Vec_PtrPush( &p->vCellClasses, pCell );
279 280 281 282 283 284 285 286
            pCell->pNext = pCell->pPrev = pCell;
            continue;
        }
        // add it to the list before the cell
        pRepr->pPrev->pNext = pCell; pCell->pNext = pRepr;
        pCell->pPrev = pRepr->pPrev; pRepr->pPrev = pCell;
    }
    // sort cells by size then by name
287
    qsort( (void *)Vec_PtrArray(&p->vCellClasses), Vec_PtrSize(&p->vCellClasses), sizeof(void *), (int(*)(const void *,const void *))Abc_SclCompareCells );
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    // sort cell lists
    vList = Vec_PtrAlloc( 100 );
    SC_LibForEachCellClass( p, pRepr, k )
    {
        Vec_PtrClear( vList );
        SC_RingForEachCell( pRepr, pCell, i )
            Vec_PtrPush( vList, pCell );
        qsort( (void *)Vec_PtrArray(vList), Vec_PtrSize(vList), sizeof(void *), (int(*)(const void *,const void *))Abc_SclCompareCells );
        // create new representative
        pRepr = (SC_Cell *)Vec_PtrEntry( vList, 0 );
        pRepr->pNext = pRepr->pPrev = pRepr;
        pRepr->pRepr = pRepr;
        pRepr->pAve  = (SC_Cell *)Vec_PtrEntry( vList, Vec_PtrSize(vList)/2 );
        pRepr->Order = 0;
        pRepr->nGates = Vec_PtrSize(vList);
        // relink cells
        Vec_PtrForEachEntryStart( SC_Cell *, vList, pCell, i, 1 )
        {
            pRepr->pPrev->pNext = pCell; pCell->pNext = pRepr;
            pCell->pPrev = pRepr->pPrev; pRepr->pPrev = pCell;
            pCell->pRepr = pRepr;
            pCell->pAve  = (SC_Cell *)Vec_PtrEntry( vList, Vec_PtrSize(vList)/2 );
            pCell->Order = i;
            pCell->nGates = Vec_PtrSize(vList);
        }
        // update list
314
        Vec_PtrWriteEntry( &p->vCellClasses, k, pRepr );
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    }
    Vec_PtrFree( vList );
}

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

  Synopsis    [Returns the largest inverter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
SC_Cell * Abc_SclFindInvertor( SC_Lib * p, int fFindBuff )
{
    SC_Cell * pCell = NULL;
    word Truth = fFindBuff ? ABC_CONST(0xAAAAAAAAAAAAAAAA) : ABC_CONST(0x5555555555555555);
    int k;
    SC_LibForEachCellClass( p, pCell, k )
336
        if ( pCell->n_inputs == 1 && Vec_WrdEntry(&SC_CellPin(pCell, 1)->vFunc, 0) == Truth )
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
            break;
    // take representative
    return pCell ? pCell->pRepr : NULL;
}
SC_Cell * Abc_SclFindSmallestGate( SC_Cell * p, float CinMin )
{
    SC_Cell * pRes = NULL;
    int i;
    SC_RingForEachCell( p->pRepr, pRes, i )
        if ( SC_CellPinCapAve(pRes) > CinMin )
            return pRes;
    // take the largest gate
    return p->pRepr->pPrev;
}

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

  Synopsis    [Returns the wireload model for the given area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
SC_WireLoad * Abc_SclFetchWireLoadModel( SC_Lib * p, char * pWLoadUsed )
{
    SC_WireLoad * pWL = NULL;
    int i;
    // Get the actual table and reformat it for 'wire_cap' output:
    assert( pWLoadUsed != NULL );
    SC_LibForEachWireLoad( p, pWL, i )
        if ( !strcmp(pWL->pName, pWLoadUsed) )
            break;
372
    if ( i == Vec_PtrSize(&p->vWireLoads) )
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    {
        Abc_Print( -1, "Cannot find wire load model \"%s\".\n", pWLoadUsed );
        exit(1);
    }
//    printf( "Using wireload model \"%s\".\n", pWL->pName );
    return pWL;
}
SC_WireLoad * Abc_SclFindWireLoadModel( SC_Lib * p, float Area )
{
    char * pWLoadUsed = NULL;
    int i;
    if ( p->default_wire_load_sel && strlen(p->default_wire_load_sel) )
    {
        SC_WireLoadSel * pWLS = NULL;
        SC_LibForEachWireLoadSel( p, pWLS, i )
            if ( !strcmp(pWLS->pName, p->default_wire_load_sel) )
                break;
390
        if ( i == Vec_PtrSize(&p->vWireLoadSels) )
391 392 393 394
        {
            Abc_Print( -1, "Cannot find wire load selection model \"%s\".\n", p->default_wire_load_sel );
            exit(1);
        }
395 396
        for ( i = 0; i < Vec_FltSize(&pWLS->vAreaFrom); i++)
            if ( Area >= Vec_FltEntry(&pWLS->vAreaFrom, i) && Area <  Vec_FltEntry(&pWLS->vAreaTo, i) )
397
            {
398
                pWLoadUsed = (char *)Vec_PtrEntry(&pWLS->vWireLoadModel, i);
399 400
                break;
            }
401 402
        if ( i == Vec_FltSize(&pWLS->vAreaFrom) )
            pWLoadUsed = (char *)Vec_PtrEntryLast(&pWLS->vWireLoadModel);
403 404 405 406 407
    }
    else if ( p->default_wire_load && strlen(p->default_wire_load) )
        pWLoadUsed = p->default_wire_load;
    else
    {
408
//        Abc_Print( 0, "No wire model given.\n" );
409 410 411 412 413 414 415
        return NULL;
    }
    return Abc_SclFetchWireLoadModel( p, pWLoadUsed );
}

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

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
  Synopsis    [Returns 1 if the library has delay info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_SclHasDelayInfo( void * pScl )
{
    SC_Lib * p = (SC_Lib *)pScl;
    SC_Cell * pCell;
    SC_Timing * pTime;
    pCell = Abc_SclFindInvertor(p, 0);
    if ( pCell == NULL )
        return 0;
    pTime = Scl_CellPinTime( pCell, 0 );
    if ( pTime == NULL )
        return 0;
    return 1;
}

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

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  Synopsis    [Returns "average" slew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Abc_SclComputeAverageSlew( SC_Lib * p )
{
    SC_Cell * pCell;
    SC_Timing * pTime;
    Vec_Flt_t * vIndex;
    pCell = Abc_SclFindInvertor(p, 0);
    if ( pCell == NULL )
        return 0;
    pTime = Scl_CellPinTime( pCell, 0 );
459 460
    if ( pTime == NULL )
        return 0;
461
    vIndex = &pTime->pCellRise.vIndex0; // slew
462 463 464 465 466
    return Vec_FltEntry( vIndex, Vec_FltSize(vIndex)/3 );
}

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

467 468 469 470 471 472 473 474 475
  Synopsis    [Compute delay parameters of pin/cell/class.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
476
int Abc_SclComputeParametersPin( SC_Lib * p, SC_Cell * pCell, int iPin, float Slew, float * pLD, float * pPD )
477 478 479 480 481 482 483 484 485
{
    SC_Pair Load0, Load1, Load2;
    SC_Pair ArrIn  = { 0.0, 0.0 };
    SC_Pair SlewIn = { Slew, Slew };
    SC_Pair ArrOut0 = { 0.0, 0.0 };
    SC_Pair ArrOut1 = { 0.0, 0.0 };
    SC_Pair ArrOut2 = { 0.0, 0.0 };
    SC_Pair SlewOut = { 0.0, 0.0 };
    SC_Timing * pTime = Scl_CellPinTime( pCell, iPin );
486
    Vec_Flt_t * vIndex = pTime ? &pTime->pCellRise.vIndex1 : NULL; // capacitance
487 488
    if ( vIndex == NULL )
        return 0;
489 490 491 492
    // handle constant table
    if ( Vec_FltSize(vIndex) == 1 )
    {
        *pLD = 0;
493
        *pPD = Vec_FltEntry( (Vec_Flt_t *)Vec_PtrEntry(&pTime->pCellRise.vData, 0), 0 );
494 495
        return 1;
    }
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    // get load points
    Load0.rise = Load0.fall = 0.0;
    Load1.rise = Load1.fall = Vec_FltEntry( vIndex, 0 );
    Load2.rise = Load2.fall = Vec_FltEntry( vIndex, Vec_FltSize(vIndex) - 2 );
    // compute delay
    Scl_LibPinArrival( pTime, &ArrIn, &SlewIn, &Load0, &ArrOut0, &SlewOut );
    Scl_LibPinArrival( pTime, &ArrIn, &SlewIn, &Load1, &ArrOut1, &SlewOut );
    Scl_LibPinArrival( pTime, &ArrIn, &SlewIn, &Load2, &ArrOut2, &SlewOut );
    ArrOut0.rise = 0.5 * ArrOut0.rise + 0.5 * ArrOut0.fall;
    ArrOut1.rise = 0.5 * ArrOut1.rise + 0.5 * ArrOut1.fall;
    ArrOut2.rise = 0.5 * ArrOut2.rise + 0.5 * ArrOut2.fall;
    // get tangent
    *pLD = (ArrOut2.rise - ArrOut1.rise) / ((Load2.rise - Load1.rise) / SC_CellPinCap(pCell, iPin));
    // get constant
    *pPD = ArrOut0.rise;
511
    return 1;
512
}
513
int Abc_SclComputeParametersCell( SC_Lib * p, SC_Cell * pCell, float Slew, float * pLD, float * pPD )
514 515 516 517 518 519 520
{
    SC_Pin * pPin;
    float LD, PD, ld, pd;
    int i;
    LD = PD = ld = pd = 0;
    SC_CellForEachPinIn( pCell, pPin, i )
    {
521 522
        if ( !Abc_SclComputeParametersPin( p, pCell, i, Slew, &ld, &pd ) )
            return 0;
523 524 525 526
        LD += ld; PD += pd;
    }
    *pLD = LD / Abc_MaxInt(1, pCell->n_inputs);
    *pPD = PD / Abc_MaxInt(1, pCell->n_inputs);
527
    return 1;
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 566 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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
}
void Abc_SclComputeParametersClass( SC_Lib * p, SC_Cell * pRepr, float Slew, float * pLD, float * pPD )
{
    SC_Cell * pCell;
    float LD, PD, ld, pd;
    int i, Count = 0;
    LD = PD = ld = pd = 0;
    SC_RingForEachCell( pRepr, pCell, i )
    {
        Abc_SclComputeParametersCell( p, pCell, Slew, &ld, &pd );
        LD += ld; PD += pd;
        Count++;
    }
    *pLD = LD / Abc_MaxInt(1, Count);
    *pPD = PD / Abc_MaxInt(1, Count);
}
void Abc_SclComputeParametersClassPin( SC_Lib * p, SC_Cell * pRepr, int iPin, float Slew, float * pLD, float * pPD )
{
    SC_Cell * pCell;
    float LD, PD, ld, pd;
    int i, Count = 0;
    LD = PD = ld = pd = 0;
    SC_RingForEachCell( pRepr, pCell, i )
    {
        Abc_SclComputeParametersPin( p, pCell, iPin, Slew, &ld, &pd );
        LD += ld; PD += pd;
        Count++;
    }
    *pLD = LD / Abc_MaxInt(1, Count);
    *pPD = PD / Abc_MaxInt(1, Count);
}
float Abc_SclComputeDelayCellPin( SC_Lib * p, SC_Cell * pCell, int iPin, float Slew, float Gain )
{
    float LD = 0, PD = 0;
    Abc_SclComputeParametersPin( p, pCell, iPin, Slew, &LD, &PD );
    return 0.01 * LD * Gain + PD;
}
float Abc_SclComputeDelayClassPin( SC_Lib * p, SC_Cell * pRepr, int iPin, float Slew, float Gain )
{
    SC_Cell * pCell;
    float Delay = 0;
    int i, Count = 0;
    SC_RingForEachCell( pRepr, pCell, i )
    {
        if ( pCell->fSkip ) 
            continue;
//        if ( pRepr == pCell ) // skip the first gate
//            continue;
        Delay += Abc_SclComputeDelayCellPin( p, pCell, iPin, Slew, Gain );
        Count++;
    }
    return Delay / Abc_MaxInt(1, Count);
}
float Abc_SclComputeAreaClass( SC_Cell * pRepr )
{
    SC_Cell * pCell;
    float Area = 0;
    int i, Count = 0;
    SC_RingForEachCell( pRepr, pCell, i )
    {
        if ( pCell->fSkip ) 
            continue;
        Area += pCell->area;
        Count++;
    }
    return Area / Abc_MaxInt(1, Count);
}

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

  Synopsis    [Print cells]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclMarkSkippedCells( SC_Lib * p )
{
    char FileName[1000];
    char Buffer[1000], * pName;
    SC_Cell * pCell;
    FILE * pFile;
    int CellId, nSkipped = 0;
    sprintf( FileName, "%s.skip", p->pName );
    pFile = fopen( FileName, "rb" );
    if ( pFile == NULL )
        return;
    while ( fgets( Buffer, 999, pFile ) != NULL )
    {
        pName = strtok( Buffer, "\r\n\t " );
        if ( pName == NULL )
            continue;
        CellId = Abc_SclCellFind( p, pName );
        if ( CellId == -1 )
        {
            printf( "Cannot find cell \"%s\" in the library \"%s\".\n", pName, p->pName );
            continue;
        }
        pCell = SC_LibCell( p, CellId );
        pCell->fSkip = 1;
        nSkipped++;
    }
    fclose( pFile );
    printf( "Marked %d cells for skipping in the library \"%s\".\n", nSkipped, p->pName );
}
636
void Abc_SclPrintCells( SC_Lib * p, float SlewInit, float Gain, int fInvOnly, int fShort )
637 638 639 640
{
    SC_Cell * pCell, * pRepr;
    SC_Pin * pPin;
    int i, j, k, nLength = 0;
641
    float Slew = (SlewInit == 0) ? Abc_SclComputeAverageSlew(p) : SlewInit;
642
    float LD = 0, PD = 0;
643
    assert( Vec_PtrSize(&p->vCellClasses) > 0 );
644 645
    printf( "Library \"%s\" ", p->pName );
    printf( "has %d cells in %d classes.  ", 
646
        Vec_PtrSize(&p->vCells), Vec_PtrSize(&p->vCellClasses) );
647
    if ( !fShort )
648
        printf( "Delay estimate is based on slew %.2f ps and gain %.2f.", Slew, Gain );
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    printf( "\n" );
    Abc_SclMarkSkippedCells( p );
    // find the longest name
    SC_LibForEachCellClass( p, pRepr, k )
        SC_RingForEachCell( pRepr, pCell, i )
            nLength = Abc_MaxInt( nLength, strlen(pRepr->pName) );
    // print cells
    SC_LibForEachCellClass( p, pRepr, k )
    {
        if ( fInvOnly && pRepr->n_inputs != 1 )
            continue;
        SC_CellForEachPinOut( pRepr, pPin, i )
        {
            if ( i == pRepr->n_inputs )
            {
                printf( "Class%4d : ",   k );
                printf( "Cells =%3d   ", Abc_SclClassCellNum(pRepr) );
                printf( "Ins =%2d  ",    pRepr->n_inputs );
                printf( "Outs =%2d  ",   pRepr->n_outputs );
            }
            else
                printf( "                                            " );
            if ( pPin->func_text )
                printf( "%-30s", pPin->func_text  );
            printf( "    "  );
674
            Kit_DsdPrintFromTruth( (unsigned *)Vec_WrdArray(&pPin->vFunc), pRepr->n_inputs );
675 676 677 678 679 680 681 682 683 684
            printf( "\n" );
            if ( fShort )
                continue;
            SC_RingForEachCell( pRepr, pCell, j )
            {
                printf( "  %3d ",           j+1 );
                printf( "%s",               pCell->fSkip ? "s" : " " );
                printf( " : " );
                printf( "%-*s  ",           nLength, pCell->pName );
                printf( "%2d   ",           pCell->drive_strength );
685 686
                printf( "A =%8.2f  ",       pCell->area );
                printf( "L =%8.2f  ",       pCell->leakage );
687 688
                if ( pCell->n_outputs == 1 )
                {
689 690 691 692 693 694 695 696 697
                    if ( Abc_SclComputeParametersCell( p, pCell, Slew, &LD, &PD ) )
                    {
                        printf( "D =%6.1f ps  ",    0.01 * Gain * LD + PD );
                        printf( "LD =%6.1f ps  ",   LD );
                        printf( "PD =%6.1f ps    ", PD );
                        printf( "C =%5.1f ff  ",    SC_CellPinCapAve(pCell) );
                        printf( "Cm =%5.0f ff    ", SC_CellPin(pCell, pCell->n_inputs)->max_out_cap );
                        printf( "Sm =%5.1f ps ",    SC_CellPin(pCell, pCell->n_inputs)->max_out_slew );
                    }
698
                }
699 700 701 702 703 704 705 706 707
                printf( "\n" );
            }
            break;
        }
    }
}

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

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclConvertLeakageIntoArea( SC_Lib * p, float A, float B )
{
    SC_Cell * pCell; int i;
    SC_LibForEachCell( p, pCell, i )
        pCell->area = A * pCell->area + B * pCell->leakage;
}


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

727 728 729 730 731 732 733 734 735 736 737 738 739
  Synopsis    [Print cells]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclLibNormalizeSurface( SC_Surface * p, float Time, float Load )
{
    Vec_Flt_t * vArray;
    int i, k; float Entry;
740 741 742 743 744
    Vec_FltForEachEntry( &p->vIndex0, Entry, i ) // slew
        Vec_FltWriteEntry( &p->vIndex0, i, Time * Entry );
    Vec_FltForEachEntry( &p->vIndex1, Entry, i ) // load
        Vec_FltWriteEntry( &p->vIndex1, i, Load * Entry );
    Vec_PtrForEachEntry( Vec_Flt_t *, &p->vData, vArray, k )
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
        Vec_FltForEachEntry( vArray, Entry, i ) // delay/slew
            Vec_FltWriteEntry( vArray, i, Time * Entry );
}
void Abc_SclLibNormalize( SC_Lib * p )
{
    SC_WireLoad * pWL;
    SC_Cell * pCell;
    SC_Pin * pPin;
    SC_Timings * pTimings;
    SC_Timing * pTiming;
    int i, k, m, n;
    float Time = 1.0 * pow(10.0, 12 - p->unit_time);
    float Load = p->unit_cap_fst * pow(10.0, 15 - p->unit_cap_snd);
    if ( Time == 1 && Load == 1 )
        return;
    p->unit_time = 12;
    p->unit_cap_fst = 1;
    p->unit_cap_snd = 15;
    p->default_max_out_slew *= Time;
    SC_LibForEachWireLoad( p, pWL, i )
        pWL->cap *= Load;
    SC_LibForEachCell( p, pCell, i )
    SC_CellForEachPin( pCell, pPin, k )
    {
        pPin->cap *= Load;
        pPin->rise_cap *= Load;
        pPin->fall_cap *= Load;
        pPin->max_out_cap *= Load;
        pPin->max_out_slew *= Time;
        SC_PinForEachRTiming( pPin, pTimings, m )
775
        Vec_PtrForEachEntry( SC_Timing *, &pTimings->vTimings, pTiming, n )
776
        {
777 778 779 780
            Abc_SclLibNormalizeSurface( &pTiming->pCellRise, Time, Load );
            Abc_SclLibNormalizeSurface( &pTiming->pCellFall, Time, Load );
            Abc_SclLibNormalizeSurface( &pTiming->pRiseTrans, Time, Load );
            Abc_SclLibNormalizeSurface( &pTiming->pFallTrans, Time, Load );
781 782 783 784 785 786
        }
    }
}

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

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 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 849
  Synopsis    [Derives simple GENLIB library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Abc_SclProduceGenlibStrSimple( SC_Lib * p )
{
    char Buffer[200];
    Vec_Str_t * vStr;
    SC_Cell * pCell;
    SC_Pin * pPin, * pPinOut;
    int i, j, k, Count = 2;
    // mark skipped cells
//    Abc_SclMarkSkippedCells( p );
    vStr = Vec_StrAlloc( 1000 );
    Vec_StrPrintStr( vStr, "GATE _const0_            0.00 z=CONST0;\n" );
    Vec_StrPrintStr( vStr, "GATE _const1_            0.00 z=CONST1;\n" );
    SC_LibForEachCell( p, pCell, i )
    {
        if ( pCell->n_inputs == 0 )
            continue;
        assert( strlen(pCell->pName) < 200 );
        SC_CellForEachPinOut( pCell, pPinOut, j )
        {
            Vec_StrPrintStr( vStr, "GATE " );
            sprintf( Buffer, "%-16s", pCell->pName );
            Vec_StrPrintStr( vStr, Buffer );
            Vec_StrPrintStr( vStr, " " );
            sprintf( Buffer, "%7.2f", pCell->area );
            Vec_StrPrintStr( vStr, Buffer );
            Vec_StrPrintStr( vStr, " " );
            Vec_StrPrintStr( vStr, pPinOut->pName );
            Vec_StrPrintStr( vStr, "=" );
            Vec_StrPrintStr( vStr, pPinOut->func_text ? pPinOut->func_text : "?" );
            Vec_StrPrintStr( vStr, ";\n" );
            SC_CellForEachPinIn( pCell, pPin, k )
            {
                Vec_StrPrintStr( vStr, "         PIN " );
                sprintf( Buffer, "%-4s", pPin->pName );
                Vec_StrPrintStr( vStr, Buffer );
                sprintf( Buffer, " UNKNOWN  1  999  1.00  0.00  1.00  0.00\n" );
                Vec_StrPrintStr( vStr, Buffer );
            }
            Count++;
        }
    }
    Vec_StrPrintStr( vStr, "\n.end\n" );
    Vec_StrPush( vStr, '\0' );
//    printf( "GENLIB library with %d gates is produced:\n", Count );
//    printf( "%s", Vec_StrArray(vStr) );
    return vStr;
}
Mio_Library_t * Abc_SclDeriveGenlibSimple( void * pScl )
{
    SC_Lib * p = (SC_Lib *)pScl;
    Vec_Str_t * vStr = Abc_SclProduceGenlibStrSimple( p );
    Mio_Library_t * pLib = Mio_LibraryRead( p->pFileName, Vec_StrArray(vStr), NULL, 0 );  
    Vec_StrFree( vStr );
    if ( pLib )
850
        printf( "Derived GENLIB library \"%s\" with %d gates.\n", p->pName, SC_LibCellNum(p) );
851 852 853 854 855 856 857 858
    else
        printf( "Reading library has filed.\n" );
    return pLib;
}


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

859 860 861 862 863 864 865 866 867
  Synopsis    [Derive GENLIB library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
868
Vec_Str_t * Abc_SclProduceGenlibStr( SC_Lib * p, float Slew, float Gain, int nGatesMin, int * pnCellCount )
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
{
    char Buffer[200];
    Vec_Str_t * vStr;
    SC_Cell * pRepr;
    SC_Pin * pPin;
    int i, k, Count = 2, nClassMax = 0;
    // find the largest number of cells in a class
    SC_LibForEachCellClass( p, pRepr, i )
        if ( pRepr->n_outputs == 1 )
            nClassMax = Abc_MaxInt( nClassMax, Abc_SclClassCellNum(pRepr) );
    // update the number
    if ( nGatesMin && nGatesMin >= nClassMax )
        nGatesMin = 0;
    // mark skipped cells
    Abc_SclMarkSkippedCells( p );
    vStr = Vec_StrAlloc( 1000 );
    Vec_StrPrintStr( vStr, "GATE _const0_            0.00 z=CONST0;\n" );
    Vec_StrPrintStr( vStr, "GATE _const1_            0.00 z=CONST1;\n" );
    SC_LibForEachCellClass( p, pRepr, i )
    {
        if ( pRepr->n_inputs == 0 )
            continue;
        if ( pRepr->n_outputs > 1 )
            continue;
        if ( nGatesMin && pRepr->n_inputs > 2 && Abc_SclClassCellNum(pRepr) < nGatesMin )
            continue;
        assert( strlen(pRepr->pName) < 200 );
        Vec_StrPrintStr( vStr, "GATE " );
        sprintf( Buffer, "%-16s", pRepr->pName );
        Vec_StrPrintStr( vStr, Buffer );
        Vec_StrPrintStr( vStr, " " );
//        sprintf( Buffer, "%7.2f", Abc_SclComputeAreaClass(pRepr) );
        sprintf( Buffer, "%7.2f", pRepr->area );
        Vec_StrPrintStr( vStr, Buffer );
        Vec_StrPrintStr( vStr, " " );
        Vec_StrPrintStr( vStr, SC_CellPinName(pRepr, pRepr->n_inputs) );
        Vec_StrPrintStr( vStr, "=" );
        Vec_StrPrintStr( vStr, SC_CellPinOutFunc(pRepr, 0) ? SC_CellPinOutFunc(pRepr, 0) : "?" );
        Vec_StrPrintStr( vStr, ";\n" );
        SC_CellForEachPinIn( pRepr, pPin, k )
        {
            float Delay = Abc_SclComputeDelayClassPin( p, pRepr, k, Slew, Gain );
            assert( Delay > 0 );
            Vec_StrPrintStr( vStr, "         PIN " );
            sprintf( Buffer, "%-4s", pPin->pName );
            Vec_StrPrintStr( vStr, Buffer );
            sprintf( Buffer, " UNKNOWN  1  999  %7.2f  0.00  %7.2f  0.00\n", Delay, Delay );
            Vec_StrPrintStr( vStr, Buffer );
        }
        Count++;
    }
    Vec_StrPrintStr( vStr, "\n.end\n" );
    Vec_StrPush( vStr, '\0' );
//    printf( "GENLIB library with %d gates is produced:\n", Count );
//    printf( "%s", Vec_StrArray(vStr) );
    if ( pnCellCount )
        *pnCellCount = Count;
    return vStr;
}
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
Vec_Str_t * Abc_SclProduceGenlibStrProfile( SC_Lib * p, Mio_Library_t * pLib, float Slew, float Gain, int nGatesMin, int * pnCellCount )
{
    char Buffer[200];
    Vec_Str_t * vStr;
    SC_Cell * pRepr;
    SC_Pin * pPin;
    int i, k, Count = 2, nClassMax = 0;
    // find the largest number of cells in a class
    SC_LibForEachCellClass( p, pRepr, i )
        if ( pRepr->n_outputs == 1 )
            nClassMax = Abc_MaxInt( nClassMax, Abc_SclClassCellNum(pRepr) );
    // update the number
    if ( nGatesMin && nGatesMin >= nClassMax )
        nGatesMin = 0;
    // mark skipped cells
    Abc_SclMarkSkippedCells( p );
    vStr = Vec_StrAlloc( 1000 );
    Vec_StrPrintStr( vStr, "GATE _const0_            0.00 z=CONST0;\n" );
    Vec_StrPrintStr( vStr, "GATE _const1_            0.00 z=CONST1;\n" );
    SC_LibForEachCell( p, pRepr, i )
    {
        if ( pRepr->n_inputs == 0 )
            continue;
        if ( pRepr->n_outputs > 1 )
            continue;
        if ( nGatesMin && pRepr->n_inputs > 2 && Abc_SclClassCellNum(pRepr) < nGatesMin )
            continue;
        // check if the gate is in the profile
        if ( pRepr->n_inputs > 1 )
        {
            Mio_Gate_t * pGate = Mio_LibraryReadGateByName( pLib, pRepr->pName, NULL );
            if ( pGate == NULL || Mio_GateReadProfile(pGate) == 0 )
                continue;
        }
        // process gate
        assert( strlen(pRepr->pName) < 200 );
        Vec_StrPrintStr( vStr, "GATE " );
        sprintf( Buffer, "%-16s", pRepr->pName );
        Vec_StrPrintStr( vStr, Buffer );
        Vec_StrPrintStr( vStr, " " );
//        sprintf( Buffer, "%7.2f", Abc_SclComputeAreaClass(pRepr) );
        sprintf( Buffer, "%7.2f", pRepr->area );
        Vec_StrPrintStr( vStr, Buffer );
        Vec_StrPrintStr( vStr, " " );
        Vec_StrPrintStr( vStr, SC_CellPinName(pRepr, pRepr->n_inputs) );
        Vec_StrPrintStr( vStr, "=" );
        Vec_StrPrintStr( vStr, SC_CellPinOutFunc(pRepr, 0) ? SC_CellPinOutFunc(pRepr, 0) : "?" );
        Vec_StrPrintStr( vStr, ";\n" );
        SC_CellForEachPinIn( pRepr, pPin, k )
        {
            float Delay = Abc_SclComputeDelayClassPin( p, pRepr, k, Slew, Gain );
            assert( Delay > 0 );
            Vec_StrPrintStr( vStr, "         PIN " );
            sprintf( Buffer, "%-4s", pPin->pName );
            Vec_StrPrintStr( vStr, Buffer );
            sprintf( Buffer, " UNKNOWN  1  999  %7.2f  0.00  %7.2f  0.00\n", Delay, Delay );
            Vec_StrPrintStr( vStr, Buffer );
        }
        Count++;
    }
    Vec_StrPrintStr( vStr, "\n.end\n" );
    Vec_StrPush( vStr, '\0' );
//    printf( "GENLIB library with %d gates is produced:\n", Count );
//    printf( "%s", Vec_StrArray(vStr) );
    if ( pnCellCount )
        *pnCellCount = Count;
    return vStr;
}
996
void Abc_SclDumpGenlib( char * pFileName, SC_Lib * p, float SlewInit, float Gain, int nGatesMin )
997 998
{
    int nCellCount = 0;
999
    char FileName[1000];
1000
    float Slew = (SlewInit == 0) ? Abc_SclComputeAverageSlew(p) : SlewInit;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    Vec_Str_t * vStr;
    FILE * pFile;
    if ( pFileName == NULL )
        sprintf( FileName, "%s_s%03d_g%03d_m%d.genlib", p->pName, (int)Slew, (int)Gain, nGatesMin );
    else
        sprintf( FileName, "%s", pFileName );
    pFile = fopen( FileName, "wb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for writing.\n", FileName );
        return;
    }
1013
    vStr = Abc_SclProduceGenlibStr( p, Slew, Gain, nGatesMin, &nCellCount );
1014 1015 1016 1017 1018
    fprintf( pFile, "%s", Vec_StrArray(vStr) );
    Vec_StrFree( vStr );
    fclose( pFile );
    printf( "Written GENLIB library with %d gates into file \"%s\".\n", nCellCount, FileName );
}
1019
Mio_Library_t * Abc_SclDeriveGenlib( void * pScl, void * pMio, float SlewInit, float Gain, int nGatesMin, int fVerbose )
1020
{
1021 1022
    int nCellCount = 0;
    SC_Lib * p = (SC_Lib *)pScl;
1023
    float Slew = (SlewInit == 0) ? Abc_SclComputeAverageSlew(p) : SlewInit;
1024 1025 1026 1027 1028 1029 1030
    Vec_Str_t * vStr;
    Mio_Library_t * pLib;
    if ( pMio == NULL )
        vStr = Abc_SclProduceGenlibStr( p, Slew, Gain, nGatesMin, &nCellCount );
    else
        vStr = Abc_SclProduceGenlibStrProfile( p, (Mio_Library_t *)pMio, Slew, Gain, nGatesMin, &nCellCount );
    pLib = Mio_LibraryRead( p->pFileName, Vec_StrArray(vStr), NULL, 0 );  
1031
    Vec_StrFree( vStr );
1032
    if ( !pLib )
1033
        printf( "Reading library has filed.\n" );
1034 1035
    else if ( fVerbose )
        printf( "Derived GENLIB library \"%s\" with %d gates using slew %.2f ps and gain %.2f.\n", p->pName, nCellCount, Slew, Gain );
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    return pLib;
}

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

  Synopsis    [Install library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1050
void Abc_SclInstallGenlib( void * pScl, float SlewInit, float Gain, int nGatesMin )
1051 1052 1053
{
    SC_Lib * p = (SC_Lib *)pScl;
    Vec_Str_t * vStr, * vStr2;
1054
    float Slew = (SlewInit == 0) ? Abc_SclComputeAverageSlew(p) : SlewInit;
1055 1056 1057 1058 1059 1060 1061
    int RetValue, nGateCount = SC_LibCellNum(p);
    if ( Gain == 0 )
        vStr = Abc_SclProduceGenlibStrSimple(p);
    else
        vStr = Abc_SclProduceGenlibStr( p, Slew, Gain, nGatesMin, &nGateCount );
    vStr2 = Vec_StrDup( vStr );
    RetValue = Mio_UpdateGenlib2( vStr, vStr2, p->pName, 0 );
1062 1063
    Vec_StrFree( vStr );
    Vec_StrFree( vStr2 );
1064
    if ( !RetValue )
1065
        printf( "Reading library has filed.\n" );
1066 1067 1068 1069
    else if ( Gain != 0 )
        printf( "Derived GENLIB library \"%s\" with %d gates using slew %.2f ps and gain %.2f.\n", p->pName, nGateCount, Slew, Gain );
//    else
//        printf( "Derived unit-delay GENLIB library \"%s\" with %d gates.\n", p->pName, nGateCount );
1070 1071 1072 1073 1074 1075 1076 1077 1078
}

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


ABC_NAMESPACE_IMPL_END