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

  FileName    [amapLib.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Technology mapper for standard cells.]

  Synopsis    [Standard-cell library.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "amapInt.h"

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 45 46 47
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Allocs a library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Lib_t * Amap_LibAlloc()
{
    Amap_Lib_t * p;
Alan Mishchenko committed
48
    p = (Amap_Lib_t *)ABC_ALLOC( Amap_Lib_t, 1 );
Alan Mishchenko committed
49 50 51 52 53 54 55 56 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
    memset( p, 0, sizeof(Amap_Lib_t) );
    p->vGates = Vec_PtrAlloc( 100 );
    p->pMemGates = Aig_MmFlexStart();
    p->pMemSet = Aig_MmFlexStart();
    return p;
}

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

  Synopsis    [Deallocs a library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibFree( Amap_Lib_t * p )
{
    if ( p == NULL )
        return;
    if ( p->vSelect )
        Vec_PtrFree( p->vSelect );
    if ( p->vSorted )
        Vec_PtrFree( p->vSorted );
    if ( p->vGates )
        Vec_PtrFree( p->vGates );
    if ( p->vRules )
        Vec_VecFree( (Vec_Vec_t *)p->vRules );
    if ( p->vRulesX )
        Vec_VecFree( (Vec_Vec_t *)p->vRulesX );
    if ( p->vRules3 )
        Vec_IntFree( p->vRules3 );
    Aig_MmFlexStop( p->pMemGates, 0 );
    Aig_MmFlexStop( p->pMemSet, 0 );
Alan Mishchenko committed
85 86 87
    ABC_FREE( p->pRules );
    ABC_FREE( p->pRulesX );
    ABC_FREE( p->pNodes );
88
    ABC_FREE( p->pName );
Alan Mishchenko committed
89
    ABC_FREE( p );
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 155 156 157 158 159 160 161 162 163 164 165 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 205 206 207 208 209 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
}

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

  Synopsis    [Returns the largest gate size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibNumPinsMax( Amap_Lib_t * p )
{
    Amap_Gat_t * pGate;
    int i, Counter = 0;
    Amap_LibForEachGate( p, pGate, i )
        if ( Counter < (int)pGate->nPins )
            Counter = pGate->nPins;
    return Counter;
}

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

  Synopsis    [Writes one pin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibWritePin( FILE * pFile, Amap_Pin_t * pPin )
{
    char * pPhaseNames[10] = { "UNKNOWN", "INV", "NONINV" };
    fprintf( pFile, "    PIN " );
    fprintf( pFile, "%9s ",     pPin->pName );
    fprintf( pFile, "%10s ",    pPhaseNames[pPin->Phase] );
    fprintf( pFile, "%6d ",     (int)pPin->dLoadInput );
    fprintf( pFile, "%6d ",     (int)pPin->dLoadMax );
    fprintf( pFile, "%6.2f ",   pPin->dDelayBlockRise );
    fprintf( pFile, "%6.2f ",   pPin->dDelayFanoutRise );
    fprintf( pFile, "%6.2f ",   pPin->dDelayBlockFall );
    fprintf( pFile, "%6.2f",    pPin->dDelayFanoutFall );
    fprintf( pFile, "\n" );
}

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

  Synopsis    [Writes one gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibWriteGate( FILE * pFile, Amap_Gat_t * pGate, int fPrintDsd )
{
    Amap_Pin_t * pPin;
    fprintf( pFile, "GATE " );
    fprintf( pFile, "%12s ",      pGate->pName );
    fprintf( pFile, "%10.2f   ",  pGate->dArea );
    fprintf( pFile, "%s=%s;\n",   pGate->pOutName,    pGate->pForm );
    if ( fPrintDsd )
    {
        if ( pGate->pFunc == NULL )
            printf( "Truth table is not available.\n" );
        else
            Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
    }
    Amap_GateForEachPin( pGate, pPin )
        Amap_LibWritePin( pFile, pPin );
}

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

  Synopsis    [Writes library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibWrite( FILE * pFile, Amap_Lib_t * pLib, int fPrintDsd )
{
    Amap_Gat_t * pGate;
    int i;
    fprintf( pFile, "# The genlib library \"%s\".\n", pLib->pName );
    Amap_LibForEachGate( pLib, pGate, i )
        Amap_LibWriteGate( pFile, pGate, fPrintDsd );
}
        
/**Function*************************************************************

  Synopsis    [Compares two gates by area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCompareGatesByArea( Amap_Gat_t ** pp1, Amap_Gat_t ** pp2 )
{
    double Diff = (*pp1)->dArea - (*pp2)->dArea;
    if ( Diff < 0.0 )
        return -1;
    if ( Diff > 0.0 ) 
        return 1;
    return 0; 
}
        
/**Function*************************************************************

  Synopsis    [Compares gates by area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Amap_LibSortGatesByArea( Amap_Lib_t * pLib )
{
    Vec_Ptr_t * vSorted;
    vSorted = Vec_PtrDup( pLib->vGates );
    qsort( (void *)Vec_PtrArray(vSorted), Vec_PtrSize(vSorted), sizeof(void *), 
            (int (*)(const void *, const void *)) Amap_LibCompareGatesByArea );
    return vSorted;
}

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

  Synopsis    [Finds min-area gate with the given function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Gat_t * Amap_LibFindGate( Amap_Lib_t * p, unsigned uTruth )
{
    Amap_Gat_t * pGate;
    int i;
244
    Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
Alan Mishchenko committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        if ( pGate->nPins <= 5 && pGate->pFunc[0] == uTruth )
            return pGate;
    return NULL;
}

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

  Synopsis    [Selects gates useful for area-only mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Amap_LibSelectGates( Amap_Lib_t * p, int fVerbose )
{
    Vec_Ptr_t * vSelect;
    Amap_Gat_t * pGate, * pGate2;
265
    int i, k;//, clk = Abc_Clock();
Alan Mishchenko committed
266 267 268 269 270
    p->pGate0   = Amap_LibFindGate( p, 0 );
    p->pGate1   = Amap_LibFindGate( p, ~0 );
    p->pGateBuf = Amap_LibFindGate( p, 0xAAAAAAAA );
    p->pGateInv = Amap_LibFindGate( p, ~0xAAAAAAAA );
    vSelect = Vec_PtrAlloc( 100 );
271
    Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
Alan Mishchenko committed
272
    {
273
        if ( pGate->pFunc == NULL || pGate->pTwin != NULL )
Alan Mishchenko committed
274
            continue;
275
        Vec_PtrForEachEntryStop( Amap_Gat_t *, p->vSorted, pGate2, k, i )
Alan Mishchenko committed
276
        {
277
            if ( pGate2->pFunc == NULL || pGate2->pTwin != NULL )
Alan Mishchenko committed
278 279 280
                continue;
            if ( pGate2->nPins != pGate->nPins )
                continue;
281
            if ( !memcmp( pGate2->pFunc, pGate->pFunc, sizeof(unsigned) * Abc_TruthWordNum(pGate->nPins) ) )
Alan Mishchenko committed
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
                break;
        }
        if ( k < i )
            continue;
        Vec_PtrPush( vSelect, pGate );            
    }
    return vSelect;
}

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

  Synopsis    [Selects gates useful for area-only mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibPrintSelectedGates( Amap_Lib_t * p, int fAllGates )
{
    Vec_Ptr_t * vArray;
    Amap_Gat_t * pGate;
    int i;
    vArray = fAllGates? p->vGates : p->vSelect;
308
    Vec_PtrForEachEntry( Amap_Gat_t *, vArray, pGate, i )
Alan Mishchenko committed
309
    {
310 311 312
        printf( "%3d :%12s %d %9.2f  ", i, pGate->pName, pGate->nPins, pGate->dArea );
        printf( "%4s=%40s  ", pGate->pOutName, pGate->pForm );
        printf( "DSD: " );
Alan Mishchenko committed
313
        Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
314
        printf( "\n" );
Alan Mishchenko committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328
    }
}

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

  Synopsis    [Parses equations for the gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
329
Amap_Lib_t * Amap_LibReadAndPrepare( char * pFileName, char * pBuffer, int fVerbose, int fVeryVerbose )
Alan Mishchenko committed
330 331
{
    Amap_Lib_t * p;
332
    abctime clk = Abc_Clock();
333 334 335 336 337 338 339 340
    if ( pBuffer == NULL )
        p = Amap_LibReadFile( pFileName, fVerbose );
    else
    {
        p = Amap_LibReadBuffer( pBuffer, fVerbose );
        if ( p )
            p->pName = Abc_UtilStrsav( pFileName );
    }
Alan Mishchenko committed
341 342 343 344 345 346 347 348 349 350 351 352 353
    if ( fVerbose )
        printf( "Read %d gates from file \"%s\".\n", Vec_PtrSize(p->vGates), pFileName );
    if ( p == NULL )
        return NULL;
    if ( !Amap_LibParseEquations( p, fVerbose ) )
    {
        Amap_LibFree( p );
        return NULL;
    }
    p->vSorted = Amap_LibSortGatesByArea( p );
    p->vSelect = Amap_LibSelectGates( p, fVerbose );
    if ( fVerbose )
    {
354
        printf( "Selected %d functionally unique gates. ", Vec_PtrSize(p->vSelect) );
355
        ABC_PRT( "Time", Abc_Clock() - clk );
356
//       Amap_LibPrintSelectedGates( p, 0 );
Alan Mishchenko committed
357
    }
358
    clk = Abc_Clock();
Alan Mishchenko committed
359 360 361
    Amap_LibCreateRules( p, fVeryVerbose );
    if ( fVerbose )
    {
362
        printf( "Created %d rules and %d matches. ", p->nNodes, p->nSets );
363
        ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
364 365 366 367 368 369 370 371 372
    }
    return p;
}

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


373 374
ABC_NAMESPACE_IMPL_END