sclUtil.c 9.69 KB
Newer Older
1 2 3 4 5 6
/**CFile****************************************************************

  FileName    [sclUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

7 8 9
  PackageName [Standard-cell library representation.]

  Synopsis    [Various utilities.]
10 11 12 13 14 15 16 17 18 19 20

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

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

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

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

21
#include "sclSize.h"
22
#include "map/mio/mio.h"
23
#include "base/main/main.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37

ABC_NAMESPACE_IMPL_START


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

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

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

38
  Synopsis    [Converts pNode->pData gates into array of SC_Lit gate IDs and back.]
39 40 41 42 43 44 45 46

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
47
void Abc_SclMioGates2SclGates( SC_Lib * pLib, Abc_Ntk_t * p )
48 49
{
    Abc_Obj_t * pObj;
50 51 52 53 54 55 56 57 58 59
    int i, gateId, bufferId;
    // find buffer
    if ( Mio_LibraryReadBuf((Mio_Library_t *)p->pManFunc) == NULL )
    {
        printf( "Cannot find buffer in the current library. Quitting.\n" );
        return;
    }
    bufferId = Abc_SclCellFind( pLib, Mio_GateReadName(Mio_LibraryReadBuf((Mio_Library_t *)p->pManFunc)) );
    assert( bufferId >= 0 );
    // remap cells
60 61
    assert( p->vGates == NULL );
    p->vGates = Vec_IntStartFull( Abc_NtkObjNumMax(p) );
62
    Abc_NtkForEachNodeNotBarBuf1( p, pObj, i )
63
    {
64
        gateId = Abc_SclCellFind( pLib, Mio_GateReadName((Mio_Gate_t *)pObj->pData) );
65
        assert( gateId >= 0 );
66
        Vec_IntWriteEntry( p->vGates, i, gateId );
67
    }
68
    p->pSCLib = pLib;
69
}
70
void Abc_SclSclGates2MioGates( SC_Lib * pLib, Abc_Ntk_t * p )
71 72
{
    Abc_Obj_t * pObj;
73
    SC_Cell * pCell;
74
    int i, Counter = 0, CounterAll = 0;
75
    assert( p->vGates != NULL );
76
    Abc_NtkForEachNodeNotBarBuf1( p, pObj, i )
77
    {
78
        pCell = Abc_SclObjCell(pObj);
79
        assert( pCell->n_inputs == Abc_ObjFaninNum(pObj) );
80
        pObj->pData = Mio_LibraryReadGateByName( (Mio_Library_t *)p->pManFunc, pCell->pName, NULL );
81
        Counter += (pObj->pData == NULL);
82
        assert( pObj->fMarkA == 0 && pObj->fMarkB == 0 );
83
        CounterAll++;
84
    }
85 86
    if ( Counter )
        printf( "Could not find %d (out of %d) gates in the current library.\n", Counter, CounterAll );
87 88
    Vec_IntFreeP( &p->vGates );
    p->pSCLib = NULL;
89
}
90

91
/**Function*************************************************************
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

  Synopsis    [Transfer gate sizes from AIG without barbufs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclTransferGates( Abc_Ntk_t * pOld, Abc_Ntk_t * pNew )
{
    Abc_Obj_t * pObj; int i;
    assert( pOld->nBarBufs2 > 0 );
    assert( pNew->nBarBufs2 == 0 );
107
    Abc_NtkForEachNodeNotBarBuf( pOld, pObj, i )
108 109 110 111 112 113 114 115 116
    {
        if ( pObj->pCopy == NULL )
            continue;
        assert( Abc_ObjNtk(pObj->pCopy) == pNew );
        pObj->pData = pObj->pCopy->pData;
    }
}

/**Function*************************************************************
117 118 119 120 121 122 123 124 125 126 127 128 129 130

  Synopsis    [Reports percentage of gates of each size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
#define ABC_SCL_MAX_SIZE 64
void Abc_SclManPrintGateSizes( SC_Lib * pLib, Abc_Ntk_t * p, Vec_Int_t * vGates )
{
    Abc_Obj_t * pObj;
131
    SC_Cell * pCell;
132 133
    int i, nGates = 0, Counters[ABC_SCL_MAX_SIZE] = {0};
    double TotArea = 0, Areas[ABC_SCL_MAX_SIZE] = {0};
134
    Abc_NtkForEachNodeNotBarBuf1( p, pObj, i )
135
    {
136
        pCell = SC_LibCell( pLib, Vec_IntEntry(vGates, Abc_ObjId(pObj)) );
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        assert( pCell->Order < ABC_SCL_MAX_SIZE );
        Counters[pCell->Order]++;
        Areas[pCell->Order] += pCell->area;
        TotArea += pCell->area;
        nGates++;
    }
    printf( "Total gates = %d.  Total area = %.1f\n", nGates, TotArea );
    for ( i = 0; i < ABC_SCL_MAX_SIZE; i++ )
    {
        if ( Counters[i] == 0 )
            continue;
        printf( "Cell size = %d.  ", i );
        printf( "Count = %6d  ",     Counters[i] );
        printf( "(%5.1f %%)   ",     100.0 * Counters[i] / nGates );
        printf( "Area = %12.1f  ",   Areas[i] );
        printf( "(%5.1f %%)  ",      100.0 * Areas[i] / TotArea );
        printf( "\n" );
    }
}
void Abc_SclPrintGateSizes( SC_Lib * pLib, Abc_Ntk_t * p )
{
158 159
    Abc_SclMioGates2SclGates( pLib, p );
    Abc_SclManPrintGateSizes( pLib, p, p->vGates );
160
    Abc_SclSclGates2MioGates( pLib, p );
161 162
    Vec_IntFreeP( &p->vGates );
    p->pSCLib = NULL;
163
}
164

165 166 167 168 169 170 171 172 173 174 175
/**Function*************************************************************

  Synopsis    [Downsizes each gate to its minimium size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
176 177 178 179 180 181 182 183 184 185 186 187 188
SC_Cell * Abc_SclFindMaxAreaCell( SC_Cell * pRepr )
{
    SC_Cell * pCell, * pBest = pRepr;
    float AreaBest = pRepr->area;
    int i;
    SC_RingForEachCell( pRepr, pCell, i )
        if ( AreaBest < pCell->area )
        {
            AreaBest = pCell->area;
            pBest = pCell;
        }
    return pBest;
}
189
Vec_Int_t * Abc_SclFindMinAreas( SC_Lib * pLib, int fUseMax )
190
{
191
    Vec_Int_t * vMinCells;
192
    SC_Cell * pCell, * pRepr = NULL, * pBest = NULL;
193 194
    int i, k;
    // map each gate in the library into its min/max-size prototype
195
    vMinCells = Vec_IntStartFull( Vec_PtrSize(&pLib->vCells) );
196
    SC_LibForEachCellClass( pLib, pRepr, i )
197 198
    {
        pBest = fUseMax ? Abc_SclFindMaxAreaCell(pRepr) : pRepr;
199
        SC_RingForEachCell( pRepr, pCell, k )
200 201
            Vec_IntWriteEntry( vMinCells, pCell->Id, pBest->Id );
    }
202 203 204 205 206 207 208 209 210
    return vMinCells;
}
void Abc_SclMinsizePerform( SC_Lib * pLib, Abc_Ntk_t * p, int fUseMax, int fVerbose )
{
    Vec_Int_t * vMinCells;
    Abc_Obj_t * pObj;
    int i, gateId;
    vMinCells = Abc_SclFindMinAreas( pLib, fUseMax );
    Abc_SclMioGates2SclGates( pLib, p );
211
    Abc_NtkForEachNodeNotBarBuf1( p, pObj, i )
212
    {
213
        gateId = Vec_IntEntry( p->vGates, i );
214
        assert( gateId >= 0 && gateId < Vec_PtrSize(&pLib->vCells) );
215
        gateId = Vec_IntEntry( vMinCells, gateId );
216
        assert( gateId >= 0 && gateId < Vec_PtrSize(&pLib->vCells) );
217 218 219 220 221 222 223 224 225 226 227
        Vec_IntWriteEntry( p->vGates, i, gateId );
    }
    Abc_SclSclGates2MioGates( pLib, p );
    Vec_IntFree( vMinCells );
}
int Abc_SclCountMinSize( SC_Lib * pLib, Abc_Ntk_t * p, int fUseMax )
{
    Vec_Int_t * vMinCells;
    Abc_Obj_t * pObj;
    int i, gateId, Counter = 0;
    vMinCells = Abc_SclFindMinAreas( pLib, fUseMax );
228
    Abc_NtkForEachNodeNotBarBuf1( p, pObj, i )
229 230 231
    {
        gateId = Vec_IntEntry( p->vGates, i );
        Counter += ( gateId == Vec_IntEntry(vMinCells, gateId) );
232 233
    }
    Vec_IntFree( vMinCells );
234
    return Counter;
235 236
}

237 238
/**Function*************************************************************

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  Synopsis    [Reads timing constraints.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclReadTimingConstr( Abc_Frame_t * pAbc, char * pFileName, int fVerbose )
{
    char Buffer[1000], * pToken;
    FILE * pFile = fopen( pFileName, "rb" );
    while ( fgets( Buffer, 1000, pFile ) )
    {
        pToken = strtok( Buffer, " \t\r\n" );
255 256
        if ( pToken == NULL )
            continue;
257 258
        if ( !strcmp(pToken, "set_driving_cell") )
//        if ( !strcmp(pToken, "default_input_cell") )
259 260 261 262 263
        {
            Abc_FrameSetDrivingCell( Abc_UtilStrsav(strtok(NULL, " \t\r\n")) );
            if ( fVerbose ) 
                printf( "Setting driving cell to be \"%s\".\n", Abc_FrameReadDrivingCell() );
        }
264 265
        else if ( !strcmp(pToken, "set_load") )
//        else if ( !strcmp(pToken, "default_output_load") )
266 267 268 269 270
        {
            Abc_FrameSetMaxLoad( atof(strtok(NULL, " \t\r\n")) );
            if ( fVerbose ) 
                printf( "Setting driving cell to be %f.\n", Abc_FrameReadMaxLoad() );
        }
271
        else printf( "Unrecognized token \"%s\".\n", pToken );
272 273 274
    }
    fclose( pFile );
}
275

276 277 278 279 280 281 282 283 284 285 286 287 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
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Abc_SclExtractBarBufs( Abc_Ntk_t * pNtk )
{
    Vec_Int_t * vBufs;
    Mio_Gate_t * pBuffer;
    Abc_Obj_t * pObj; int i;
    pBuffer = Mio_LibraryReadBuf( (Mio_Library_t *)pNtk->pManFunc );
    if ( pBuffer == NULL )
    {
        printf( "Cannot find buffer in the current library. Quitting.\n" );
        return NULL;
    }
    vBufs = Vec_IntAlloc( 100 );
    Abc_NtkForEachBarBuf( pNtk, pObj, i )
    {
        assert( pObj->pData == NULL );
        pObj->pData = pBuffer;
        Vec_IntPush( vBufs, i );
    }
    return vBufs;
}
void Abc_SclInsertBarBufs( Abc_Ntk_t * pNtk, Vec_Int_t * vBufs )
{
    Abc_Obj_t * pObj; int i;
    Abc_NtkForEachObjVec( vBufs, pNtk, pObj, i )
        pObj->pData = NULL;
}

314 315 316 317 318 319 320
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END