1 /**CFile****************************************************************
2 
3   FileName    [amapLib.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Technology mapper for standard cells.]
8 
9   Synopsis    [Standard-cell library.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: amapLib.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "amapInt.h"
22 
23 ABC_NAMESPACE_IMPL_START
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 ///                        DECLARATIONS                              ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 ///                     FUNCTION DEFINITIONS                         ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36   Synopsis    [Allocs a library.]
37 
38   Description []
39 
40   SideEffects []
41 
42   SeeAlso     []
43 
44 ***********************************************************************/
Amap_LibAlloc()45 Amap_Lib_t * Amap_LibAlloc()
46 {
47     Amap_Lib_t * p;
48     p = (Amap_Lib_t *)ABC_ALLOC( Amap_Lib_t, 1 );
49     memset( p, 0, sizeof(Amap_Lib_t) );
50     p->vGates = Vec_PtrAlloc( 100 );
51     p->pMemGates = Aig_MmFlexStart();
52     p->pMemSet = Aig_MmFlexStart();
53     return p;
54 }
55 
56 /**Function*************************************************************
57 
58   Synopsis    [Deallocs a library.]
59 
60   Description []
61 
62   SideEffects []
63 
64   SeeAlso     []
65 
66 ***********************************************************************/
Amap_LibFree(Amap_Lib_t * p)67 void Amap_LibFree( Amap_Lib_t * p )
68 {
69     if ( p == NULL )
70         return;
71     if ( p->vSelect )
72         Vec_PtrFree( p->vSelect );
73     if ( p->vSorted )
74         Vec_PtrFree( p->vSorted );
75     if ( p->vGates )
76         Vec_PtrFree( p->vGates );
77     if ( p->vRules )
78         Vec_VecFree( (Vec_Vec_t *)p->vRules );
79     if ( p->vRulesX )
80         Vec_VecFree( (Vec_Vec_t *)p->vRulesX );
81     if ( p->vRules3 )
82         Vec_IntFree( p->vRules3 );
83     Aig_MmFlexStop( p->pMemGates, 0 );
84     Aig_MmFlexStop( p->pMemSet, 0 );
85     ABC_FREE( p->pRules );
86     ABC_FREE( p->pRulesX );
87     ABC_FREE( p->pNodes );
88     ABC_FREE( p->pName );
89     ABC_FREE( p );
90 }
91 
92 /**Function*************************************************************
93 
94   Synopsis    [Returns the largest gate size.]
95 
96   Description []
97 
98   SideEffects []
99 
100   SeeAlso     []
101 
102 ***********************************************************************/
Amap_LibNumPinsMax(Amap_Lib_t * p)103 int Amap_LibNumPinsMax( Amap_Lib_t * p )
104 {
105     Amap_Gat_t * pGate;
106     int i, Counter = 0;
107     Amap_LibForEachGate( p, pGate, i )
108         if ( Counter < (int)pGate->nPins )
109             Counter = pGate->nPins;
110     return Counter;
111 }
112 
113 /**Function*************************************************************
114 
115   Synopsis    [Writes one pin.]
116 
117   Description []
118 
119   SideEffects []
120 
121   SeeAlso     []
122 
123 ***********************************************************************/
Amap_LibWritePin(FILE * pFile,Amap_Pin_t * pPin)124 void Amap_LibWritePin( FILE * pFile, Amap_Pin_t * pPin )
125 {
126     char * pPhaseNames[10] = { "UNKNOWN", "INV", "NONINV" };
127     fprintf( pFile, "    PIN " );
128     fprintf( pFile, "%9s ",     pPin->pName );
129     fprintf( pFile, "%10s ",    pPhaseNames[pPin->Phase] );
130     fprintf( pFile, "%6d ",     (int)pPin->dLoadInput );
131     fprintf( pFile, "%6d ",     (int)pPin->dLoadMax );
132     fprintf( pFile, "%6.2f ",   pPin->dDelayBlockRise );
133     fprintf( pFile, "%6.2f ",   pPin->dDelayFanoutRise );
134     fprintf( pFile, "%6.2f ",   pPin->dDelayBlockFall );
135     fprintf( pFile, "%6.2f",    pPin->dDelayFanoutFall );
136     fprintf( pFile, "\n" );
137 }
138 
139 /**Function*************************************************************
140 
141   Synopsis    [Writes one gate.]
142 
143   Description []
144 
145   SideEffects []
146 
147   SeeAlso     []
148 
149 ***********************************************************************/
Amap_LibWriteGate(FILE * pFile,Amap_Gat_t * pGate,int fPrintDsd)150 void Amap_LibWriteGate( FILE * pFile, Amap_Gat_t * pGate, int fPrintDsd )
151 {
152     Amap_Pin_t * pPin;
153     fprintf( pFile, "GATE " );
154     fprintf( pFile, "%12s ",      pGate->pName );
155     fprintf( pFile, "%10.2f   ",  pGate->dArea );
156     fprintf( pFile, "%s=%s;\n",   pGate->pOutName,    pGate->pForm );
157     if ( fPrintDsd )
158     {
159         if ( pGate->pFunc == NULL )
160             printf( "Truth table is not available.\n" );
161         else
162             Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
163     }
164     Amap_GateForEachPin( pGate, pPin )
165         Amap_LibWritePin( pFile, pPin );
166 }
167 
168 /**Function*************************************************************
169 
170   Synopsis    [Writes library.]
171 
172   Description []
173 
174   SideEffects []
175 
176   SeeAlso     []
177 
178 ***********************************************************************/
Amap_LibWrite(FILE * pFile,Amap_Lib_t * pLib,int fPrintDsd)179 void Amap_LibWrite( FILE * pFile, Amap_Lib_t * pLib, int fPrintDsd )
180 {
181     Amap_Gat_t * pGate;
182     int i;
183     fprintf( pFile, "# The genlib library \"%s\".\n", pLib->pName );
184     Amap_LibForEachGate( pLib, pGate, i )
185         Amap_LibWriteGate( pFile, pGate, fPrintDsd );
186 }
187 
188 /**Function*************************************************************
189 
190   Synopsis    [Compares two gates by area.]
191 
192   Description []
193 
194   SideEffects []
195 
196   SeeAlso     []
197 
198 ***********************************************************************/
Amap_LibCompareGatesByArea(Amap_Gat_t ** pp1,Amap_Gat_t ** pp2)199 int Amap_LibCompareGatesByArea( Amap_Gat_t ** pp1, Amap_Gat_t ** pp2 )
200 {
201     double Diff = (*pp1)->dArea - (*pp2)->dArea;
202     if ( Diff < 0.0 )
203         return -1;
204     if ( Diff > 0.0 )
205         return 1;
206     return 0;
207 }
208 
209 /**Function*************************************************************
210 
211   Synopsis    [Compares gates by area.]
212 
213   Description []
214 
215   SideEffects []
216 
217   SeeAlso     []
218 
219 ***********************************************************************/
Amap_LibSortGatesByArea(Amap_Lib_t * pLib)220 Vec_Ptr_t * Amap_LibSortGatesByArea( Amap_Lib_t * pLib )
221 {
222     Vec_Ptr_t * vSorted;
223     vSorted = Vec_PtrDup( pLib->vGates );
224     qsort( (void *)Vec_PtrArray(vSorted), (size_t)Vec_PtrSize(vSorted), sizeof(void *),
225             (int (*)(const void *, const void *)) Amap_LibCompareGatesByArea );
226     return vSorted;
227 }
228 
229 /**Function*************************************************************
230 
231   Synopsis    [Finds min-area gate with the given function.]
232 
233   Description []
234 
235   SideEffects []
236 
237   SeeAlso     []
238 
239 ***********************************************************************/
Amap_LibFindGate(Amap_Lib_t * p,unsigned uTruth)240 Amap_Gat_t * Amap_LibFindGate( Amap_Lib_t * p, unsigned uTruth )
241 {
242     Amap_Gat_t * pGate;
243     int i;
244     Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
245     {
246         if (( pGate == NULL ) || ( pGate->pFunc == NULL )) continue;
247         if ( pGate->nPins <= 5 && pGate->pFunc[0] == uTruth )
248             return pGate;
249     }
250     return NULL;
251 }
252 
253 /**Function*************************************************************
254 
255   Synopsis    [Selects gates useful for area-only mapping.]
256 
257   Description []
258 
259   SideEffects []
260 
261   SeeAlso     []
262 
263 ***********************************************************************/
Amap_LibSelectGates(Amap_Lib_t * p,int fVerbose)264 Vec_Ptr_t * Amap_LibSelectGates( Amap_Lib_t * p, int fVerbose )
265 {
266     Vec_Ptr_t * vSelect;
267     Amap_Gat_t * pGate, * pGate2;
268     int i, k;//, clk = Abc_Clock();
269     p->pGate0   = Amap_LibFindGate( p, 0 );
270     p->pGate1   = Amap_LibFindGate( p, ~0 );
271     p->pGateBuf = Amap_LibFindGate( p, 0xAAAAAAAA );
272     p->pGateInv = Amap_LibFindGate( p, ~0xAAAAAAAA );
273     vSelect = Vec_PtrAlloc( 100 );
274     Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
275     {
276         if ( pGate->pFunc == NULL || pGate->pTwin != NULL )
277             continue;
278         Vec_PtrForEachEntryStop( Amap_Gat_t *, p->vSorted, pGate2, k, i )
279         {
280             if ( pGate2->pFunc == NULL || pGate2->pTwin != NULL )
281                 continue;
282             if ( pGate2->nPins != pGate->nPins )
283                 continue;
284             if ( !memcmp( pGate2->pFunc, pGate->pFunc, sizeof(unsigned) * Abc_TruthWordNum(pGate->nPins) ) )
285                 break;
286         }
287         if ( k < i )
288             continue;
289         Vec_PtrPush( vSelect, pGate );
290     }
291     return vSelect;
292 }
293 
294 /**Function*************************************************************
295 
296   Synopsis    [Selects gates useful for area-only mapping.]
297 
298   Description []
299 
300   SideEffects []
301 
302   SeeAlso     []
303 
304 ***********************************************************************/
Amap_LibPrintSelectedGates(Amap_Lib_t * p,int fAllGates)305 void Amap_LibPrintSelectedGates( Amap_Lib_t * p, int fAllGates )
306 {
307     Vec_Ptr_t * vArray;
308     Amap_Gat_t * pGate;
309     int i;
310     vArray = fAllGates? p->vGates : p->vSelect;
311     Vec_PtrForEachEntry( Amap_Gat_t *, vArray, pGate, i )
312     {
313         printf( "%3d :%12s %d %9.2f  ", i, pGate->pName, pGate->nPins, pGate->dArea );
314         printf( "%4s=%40s  ", pGate->pOutName, pGate->pForm );
315         printf( "DSD: " );
316         Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
317         printf( "\n" );
318     }
319 }
320 
321 /**Function*************************************************************
322 
323   Synopsis    [Parses equations for the gates.]
324 
325   Description []
326 
327   SideEffects []
328 
329   SeeAlso     []
330 
331 ***********************************************************************/
Amap_LibReadAndPrepare(char * pFileName,char * pBuffer,int fVerbose,int fVeryVerbose)332 Amap_Lib_t * Amap_LibReadAndPrepare( char * pFileName, char * pBuffer, int fVerbose, int fVeryVerbose )
333 {
334     Amap_Lib_t * p;
335     abctime clk = Abc_Clock();
336     if ( pBuffer == NULL )
337         p = Amap_LibReadFile( pFileName, fVerbose );
338     else
339     {
340         p = Amap_LibReadBuffer( pBuffer, fVerbose );
341         if ( p )
342             p->pName = Abc_UtilStrsav( pFileName );
343     }
344     if ( fVerbose )
345         printf( "Read %d gates from file \"%s\".\n", Vec_PtrSize(p->vGates), pFileName );
346     if ( p == NULL )
347         return NULL;
348     if ( !Amap_LibParseEquations( p, fVerbose ) )
349     {
350         Amap_LibFree( p );
351         return NULL;
352     }
353     p->vSorted = Amap_LibSortGatesByArea( p );
354     p->vSelect = Amap_LibSelectGates( p, fVerbose );
355     if ( fVerbose )
356     {
357         printf( "Selected %d functionally unique gates. ", Vec_PtrSize(p->vSelect) );
358         ABC_PRT( "Time", Abc_Clock() - clk );
359 //       Amap_LibPrintSelectedGates( p, 0 );
360     }
361     clk = Abc_Clock();
362     Amap_LibCreateRules( p, fVeryVerbose );
363     if ( fVerbose )
364     {
365         printf( "Created %d rules and %d matches. ", p->nNodes, p->nSets );
366         ABC_PRT( "Time", Abc_Clock() - clk );
367     }
368     return p;
369 }
370 
371 ////////////////////////////////////////////////////////////////////////
372 ///                       END OF FILE                                ///
373 ////////////////////////////////////////////////////////////////////////
374 
375 
376 ABC_NAMESPACE_IMPL_END
377 
378