summaryrefslogtreecommitdiffstats
path: root/src/base/abc/abcLib.c
blob: feff147830cddde72a7af5c5d404b47aefe27489 (plain)
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
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
381
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
/**CFile****************************************************************

  FileName    [abcLib.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Functions to manipulate verilog libraries.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Create the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Lib_t * Abc_LibCreate( char * pName )
{
    Abc_Lib_t * p;
    p = ALLOC( Abc_Lib_t, 1 );
    memset( p, 0, sizeof(Abc_Lib_t) );
    p->pName    = Extra_UtilStrsav( pName );
    p->tModules = st_init_table( strcmp, st_strhash );
    p->vTops    = Vec_PtrAlloc( 100 );
    p->vModules = Vec_PtrAlloc( 100 );
    p->pManFunc = Hop_ManStart();
    p->pLibrary = NULL;
    return p;
}

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

  Synopsis    [Frees the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_LibFree( Abc_Lib_t * pLib, Abc_Ntk_t * pNtkSave )
{
    Abc_Ntk_t * pNtk;
    int i;
    if ( pLib->pName )
        free( pLib->pName );
    if ( pLib->pManFunc )
        Hop_ManStop( pLib->pManFunc );
    if ( pLib->tModules )
        st_free_table( pLib->tModules );
    if ( pLib->vModules )
    {
        Vec_PtrForEachEntry( pLib->vModules, pNtk, i )
        {
            if ( pNtk == pNtkSave )
                continue;
//            pNtk->pManFunc = NULL;
            pNtk->pDesign = NULL;
            Abc_NtkDelete( pNtk );
        }
        Vec_PtrFree( pLib->vModules );
    }
    if ( pLib->vTops )
        Vec_PtrFree( pLib->vTops );
    free( pLib );
}

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

  Synopsis    [Frees the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Lib_t * Abc_LibDupBlackboxes( Abc_Lib_t * pLib, Abc_Ntk_t * pNtkSave )
{
    Abc_Lib_t * pLibNew;
    Abc_Ntk_t * pNtkTemp;
    int i;
    assert( Vec_PtrSize(pLib->vTops) > 0 );
    assert( Vec_PtrSize(pLib->vModules) > 1 );
    pLibNew = Abc_LibCreate( pLib->pName );
//    pLibNew->pManFunc = pNtkSave->pManFunc;
    Vec_PtrPush( pLibNew->vTops, pNtkSave );
    Vec_PtrPush( pLibNew->vModules, pNtkSave );
    Vec_PtrForEachEntry( pLib->vModules, pNtkTemp, i )
        if ( Abc_NtkHasBlackbox( pNtkTemp ) )
            Vec_PtrPush( pLibNew->vModules, Abc_NtkDup(pNtkTemp) );
    return pLibNew;
}


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

  Synopsis    [Prints the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_LibPrint( Abc_Lib_t * pLib )
{
    Abc_Ntk_t * pNtk;
    Abc_Obj_t * pObj;
    int i, k;
    printf( "Models of design %s:\n", pLib->pName );
    Vec_PtrForEachEntry( pLib->vModules, pNtk, i )
    {
        printf( "%2d : %20s   ", i+1, pNtk->pName );
        printf( "nd = %6d   lat = %6d   whitebox = %3d   blackbox = %3d\n", 
            Abc_NtkNodeNum(pNtk), Abc_NtkLatchNum(pNtk), 
            Abc_NtkWhiteboxNum(pNtk), Abc_NtkBlackboxNum(pNtk) );
        if ( Abc_NtkBlackboxNum(pNtk) == 0 )
            continue;
        Abc_NtkForEachWhitebox( pNtk, pObj, k )
            printf( "     %20s (whitebox)\n", Abc_NtkName(pObj->pData) );
        Abc_NtkForEachBlackbox( pNtk, pObj, k )
            printf( "     %20s (blackbox)\n", Abc_NtkName(pObj->pData) );
    }
}

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

  Synopsis    [Create the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_LibAddModel( Abc_Lib_t * pLib, Abc_Ntk_t * pNtk )
{
    if ( st_is_member( pLib->tModules, (char *)pNtk->pName ) )
        return 0;
    st_insert( pLib->tModules, (char *)pNtk->pName, (char *)pNtk );
    Vec_PtrPush( pLib->vModules, pNtk );
    pNtk->pDesign = pLib;
    return 1;
}

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

  Synopsis    [Create the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_LibFindModelByName( Abc_Lib_t * pLib, char * pName )
{
    Abc_Ntk_t * pNtk;
    if ( !st_is_member( pLib->tModules, (char *)pName ) )
        return NULL;
    st_lookup( pLib->tModules, (char *)pName, (char **)&pNtk );
    return pNtk;
}

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

  Synopsis    [Frees the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_LibDeriveRoot( Abc_Lib_t * pLib )
{
    Abc_Ntk_t * pNtk;
    if ( Vec_PtrSize(pLib->vModules) > 1 )
    {
        printf( "The design includes more than one module and is currently not used.\n" );
        return NULL;
    }
    pNtk = Vec_PtrEntry( pLib->vModules, 0 );  Vec_PtrClear( pLib->vModules );
    pNtk->pManFunc = pLib->pManFunc;           pLib->pManFunc = NULL;
    return pNtk;
}

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

  Synopsis    [Detects the top-level models.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_LibFindTopLevelModels( Abc_Lib_t * pLib )
{
    Abc_Ntk_t * pNtk, * pNtkBox;
    Abc_Obj_t * pObj;
    int i, k;
    assert( Vec_PtrSize( pLib->vModules ) > 0 );
    // clear the models
    Vec_PtrForEachEntry( pLib->vModules, pNtk, i )
        pNtk->fHieVisited = 0;
    // mark all the models reachable from other models
    Vec_PtrForEachEntry( pLib->vModules, pNtk, i )
    {
        Abc_NtkForEachBox( pNtk, pObj, k )
        {
            if ( Abc_ObjIsLatch(pObj) )
                continue;
            if ( pObj->pData == NULL )
                continue;
            pNtkBox = pObj->pData;
            pNtkBox->fHieVisited = 1;
        }
    }
    // collect the models that are not marked
    Vec_PtrClear( pLib->vTops );
    Vec_PtrForEachEntry( pLib->vModules, pNtk, i )
    {
        if ( pNtk->fHieVisited == 0 )
            Vec_PtrPush( pLib->vTops, pNtk );
        else
            pNtk->fHieVisited = 0;
    }
    return Vec_PtrSize( pLib->vTops );
}


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

  Synopsis    [Surround boxes without content (black boxes) with BIs/BOs.]

  Description [Returns the number of black boxes converted.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_LibDeriveBlackBoxes( Abc_Ntk_t * pNtk, Abc_Lib_t * pLib )
{
/*
    Abc_Obj_t * pObj, * pFanin, * pFanout;
    int i, k;
    assert( Abc_NtkIsNetlist(pNtk) );
    // collect blackbox nodes
    assert( Vec_PtrSize(pNtk->vBoxes) == 0 );
    Vec_PtrClear( pNtk->vBoxes );
    Abc_NtkForEachBox( pNtk, pObj, i )
        if ( Abc_NtkNodeNum(pObj->pData) == 0 )
            Vec_PtrPush( pNtk->vBoxes, pObj );
    // return if there is no black boxes without content
    if ( Vec_PtrSize(pNtk->vBoxes) == 0 )
        return 0;
    // print the boxes
    printf( "Black boxes are: " );
    Abc_NtkForEachBox( pNtk, pObj, i )
        printf( " %s", ((Abc_Ntk_t *)pObj->pData)->pName );
    printf( "\n" );
    // iterate through the boxes and add BIs/BOs
    Abc_NtkForEachBox( pNtk, pObj, i )
    {
        // go through the fanin nets
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Abc_ObjInsertBetween( pFanin, pObj, ABC_OBJ_BI );
        // go through the fanout nets
        Abc_ObjForEachFanout( pObj, pFanout, k )
        {
            Abc_ObjInsertBetween( pObj, pFanout, ABC_OBJ_BO );
            // if the name is not given assign name
            if ( pFanout->pData == NULL )
            {
                pFanout->pData = Abc_ObjName( pFanout );
                Nm_ManStoreIdName( pNtk->pManName, pFanout->Id, pFanout->pData, NULL );
            }
        }
    }
    return Vec_PtrSize(pNtk->vBoxes);
*/
    return 0;
}

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

  Synopsis    [Derive the AIG of the logic in the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeStrashUsingNetwork_rec( Abc_Ntk_t * pNtkAig, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanin;
    int i;
    assert( !Abc_ObjIsNet(pObj) );
    if ( pObj->pCopy )
        return;
    // call for the fanins
    Abc_ObjForEachFanin( pObj, pFanin, i )
        Abc_NodeStrashUsingNetwork_rec( pNtkAig, Abc_ObjFanin0Ntk(Abc_ObjFanin0(pObj)) );
    // compute for the node
    pObj->pCopy = Abc_NodeStrash( pNtkAig, pObj, 0 );
    // set for the fanout net
    Abc_ObjFanout0(pObj)->pCopy = pObj->pCopy;
}

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

  Synopsis    [Derive the AIG of the logic in the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeStrashUsingNetwork( Abc_Ntk_t * pNtkAig, Abc_Obj_t * pBox )
{ 
    Abc_Ntk_t * pNtkGate;
    Abc_Obj_t * pObj;
    unsigned * pPolarity;
    int i, fCompl;
    assert( Abc_ObjIsBox(pBox) );
    pNtkGate = pBox->pData;
    pPolarity = (unsigned *)pBox->pNext;
    assert( Abc_NtkIsNetlist(pNtkGate) );
    assert( Abc_NtkLatchNum(pNtkGate) == 0 );
    Abc_NtkCleanCopy( pNtkGate );
    // set the PI values
    Abc_NtkForEachPi( pNtkGate, pObj, i )
    {
        fCompl = (pPolarity && Abc_InfoHasBit(pPolarity, i));
        pObj->pCopy = Abc_ObjNotCond( Abc_ObjFanin(pBox,i)->pCopy, fCompl );
        Abc_ObjFanout0(pObj)->pCopy = pObj->pCopy;
    }
    // build recursively and set the PO values
    Abc_NtkForEachPo( pNtkGate, pObj, i )
    {
        Abc_NodeStrashUsingNetwork_rec( pNtkAig, Abc_ObjFanin0Ntk(Abc_ObjFanin0(pObj)) );
        Abc_ObjFanout(pBox,i)->pCopy = Abc_ObjFanin0(pObj)->pCopy;
    }
//printf( "processing %d\n", pBox->Id );
}

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

  Synopsis    [Derive the AIG of the logic in the netlist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_LibDeriveAig( Abc_Ntk_t * pNtk, Abc_Lib_t * pLib )
{
    ProgressBar * pProgress;
    Vec_Ptr_t * vNodes;
    Abc_Ntk_t * pNtkAig;
    Abc_Obj_t * pObj;
    int i, nBoxes;
    // explicitly derive black boxes
    assert( Abc_NtkIsNetlist(pNtk) );
    nBoxes = Abc_LibDeriveBlackBoxes( pNtk, pLib );
    if ( nBoxes )
        printf( "Detected and transformed %d black boxes.\n", nBoxes );
    // create the new network with black boxes in place
    pNtkAig = Abc_NtkStartFrom( pNtk, ABC_NTK_STRASH, ABC_FUNC_AIG );
    // transer to the nets
    Abc_NtkForEachCi( pNtk, pObj, i )
        Abc_ObjFanout0(pObj)->pCopy = pObj->pCopy;
    // build the AIG for the remaining logic in the netlist
    vNodes = Abc_NtkDfs( pNtk, 0 );
    pProgress = Extra_ProgressBarStart( stdout, Vec_PtrSize(vNodes) );
    Vec_PtrForEachEntry( vNodes, pObj, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        if ( Abc_ObjIsNode(pObj) )
        {
            pObj->pCopy = Abc_NodeStrash( pNtkAig, pObj, 0 );
            Abc_ObjFanout0(pObj)->pCopy = pObj->pCopy;
            continue;
        }
        Abc_NodeStrashUsingNetwork( pNtkAig, pObj );
    }
    Extra_ProgressBarStop( pProgress );
    Vec_PtrFree( vNodes );
    // deallocate memory manager, which remembers the phase
    if ( pNtk->pData )
    {
        Extra_MmFlexStop( pNtk->pData );
        pNtk->pData = NULL;
    }
    // set the COs
//    Abc_NtkFinalize( pNtk, pNtkAig );
    Abc_NtkForEachCo( pNtk, pObj, i )
        Abc_ObjAddFanin( pObj->pCopy, Abc_ObjFanin0(pObj)->pCopy );
    Abc_AigCleanup( pNtkAig->pManFunc );
    // make sure that everything is okay
    if ( !Abc_NtkCheck( pNtkAig ) )
    {
        printf( "Abc_LibDeriveAig: The network check has failed.\n" );
        return 0;
    }
    return pNtkAig;
}

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