1 /**CFile****************************************************************
2 
3   FileName    [wlnWlc.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Word-level network.]
8 
9   Synopsis    [Network transformation.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - September 23, 2018.]
16 
17   Revision    [$Id: wlnWlc.c,v 1.00 2018/09/23 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "wln.h"
22 #include "base/wlc/wlc.h"
23 
24 ABC_NAMESPACE_IMPL_START
25 
26 ////////////////////////////////////////////////////////////////////////
27 ///                        DECLARATIONS                              ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 extern int Ndr_TypeWlc2Ndr( int Type );
31 
32 ////////////////////////////////////////////////////////////////////////
33 ///                     FUNCTION DEFINITIONS                         ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38   Synopsis    []
39 
40   Description []
41 
42   SideEffects []
43 
44   SeeAlso     []
45 
46 ***********************************************************************/
Wln_ConstFromBits(int * pBits,int nBits)47 char * Wln_ConstFromBits( int * pBits, int nBits )
48 {
49     char * pBuffer = ABC_ALLOC( char, nBits+100 ); int i, Len;
50     sprintf( pBuffer, "%d\'b", nBits );
51     Len = strlen(pBuffer);
52     for ( i = nBits-1; i >= 0; i-- )
53         pBuffer[Len++] = '0' + Abc_InfoHasBit((unsigned *)pBits, i);
54     pBuffer[Len] = 0;
55     return pBuffer;
56 }
Wln_ConstFromStr(char * pBits,int nBits)57 char * Wln_ConstFromStr( char * pBits, int nBits )
58 {
59     char * pBuffer = ABC_ALLOC( char, nBits+100 ); int i, Len;
60     sprintf( pBuffer, "%d\'b", nBits );
61     Len = strlen(pBuffer);
62     for ( i = 0; i < nBits; i++ )
63         pBuffer[Len++] = pBits[i];
64     pBuffer[Len] = 0;
65     return pBuffer;
66 }
Wln_TrasformNameId(Wln_Ntk_t * pNew,Wlc_Ntk_t * p,Wlc_Obj_t * pObj)67 int Wln_TrasformNameId( Wln_Ntk_t * pNew, Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
68 {
69     return Abc_NamStrFindOrAdd( pNew->pManName, Wlc_ObjName(p, Wlc_ObjId(p, pObj)), NULL );
70 }
Wln_NtkFromWlc(Wlc_Ntk_t * p)71 Wln_Ntk_t * Wln_NtkFromWlc( Wlc_Ntk_t * p )
72 {
73     Wlc_Obj_t * pObj;
74     char Buffer[1000];
75     int i, j, n, Type, iFanin, iOutId, iBit = 0;
76     Vec_Int_t * vFanins = Vec_IntAlloc( 10 );
77     Vec_Int_t * vInits = Vec_IntAlloc( Wlc_NtkFfNum(p) );
78     Wln_Ntk_t * pNew = Wln_NtkAlloc( p->pName, Wlc_NtkObjNum(p)+Wlc_NtkCoNum(p)+Wlc_NtkFfNum(p) );
79     pNew->pManName = Abc_NamStart( Abc_NamObjNumMax(p->pManName), 10 );
80     if ( p->pSpec ) pNew->pSpec = Abc_UtilStrsav( p->pSpec );
81     pNew->fSmtLib = p->fSmtLib;
82     Wlc_NtkCleanCopy( p );
83     Wln_NtkCleanNameId( pNew );
84     // add primary inputs
85     Wlc_NtkForEachPi( p, pObj, i )
86     {
87         iOutId = Wln_ObjAlloc( pNew, ABC_OPER_CI, pObj->Signed, pObj->End, pObj->Beg );
88         Wln_ObjSetNameId( pNew, iOutId, Wln_TrasformNameId(pNew, p, pObj) );
89         Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), iOutId );
90     }
91     // create initial state of the flops
92     Wlc_NtkForEachCi( p, pObj, i )
93     {
94         assert( i == Wlc_ObjCiId(pObj) );
95         if ( pObj->Type == WLC_OBJ_PI )
96             continue;
97         for ( j = 0; j < Wlc_ObjRange(pObj); j++ )
98             if ( p->pInits[iBit+j] == 'x' )
99                 break;
100         // print flop init state
101         if ( 1 )
102         {
103             printf( "Flop %3d init state: %d\'b", i-Wlc_NtkPiNum(p), Wlc_ObjRange(pObj) );
104             if ( j == Wlc_ObjRange(pObj) )
105             {
106                 int Count = 0;
107                 for ( n = 0; n < Wlc_ObjRange(pObj); n++ )
108                     Count += p->pInits[iBit+n] == '0';
109                 if ( Count == Wlc_ObjRange(pObj) )
110                     printf( "0" );
111                 else
112                     for ( n = 0; n < Wlc_ObjRange(pObj); n++ )
113                         printf( "%c", p->pInits[iBit+n] );
114             }
115             else
116             {
117                 int Count = 0;
118                 for ( n = 0; n < Wlc_ObjRange(pObj); n++ )
119                     Count += p->pInits[iBit+n] == 'x';
120                 printf( "x" );
121                 if ( Count != Wlc_ObjRange(pObj) )
122                     printf( " (range %d)", Wlc_ObjRange(pObj) );
123             }
124             printf( "\n" );
125         }
126         Type = j == Wlc_ObjRange(pObj) ? ABC_OPER_CONST : ABC_OPER_CI;
127         iOutId = Wln_ObjAlloc( pNew, Type, pObj->Signed, pObj->End, pObj->Beg );
128         if ( j == Wlc_ObjRange(pObj) ) // constant
129         {
130             char * pString = Wln_ConstFromStr(p->pInits + iBit, Wlc_ObjRange(pObj));
131             Wln_ObjSetConst( pNew, iOutId, Abc_NamStrFindOrAdd(pNew->pManName, pString, NULL) );
132             ABC_FREE( pString );
133         }
134         sprintf( Buffer, "ff_init_%d", Vec_IntSize(vInits) );
135         Wln_ObjSetNameId( pNew, iOutId, Abc_NamStrFindOrAdd(pNew->pManName, Buffer, NULL) );
136         Vec_IntPush( vInits, iOutId );
137         iBit += Wlc_ObjRange(pObj);
138     }
139     assert( p->pInits == NULL || iBit == (int)strlen(p->pInits) );
140     // add flop outputs
141     Wlc_NtkForEachCi( p, pObj, i )
142     {
143         assert( i == Wlc_ObjCiId(pObj) );
144         if ( pObj->Type == WLC_OBJ_PI )
145             continue;
146         iOutId = Wln_ObjAlloc( pNew, ABC_OPER_DFFRSE, pObj->Signed, pObj->End, pObj->Beg );
147         Wln_ObjSetNameId( pNew, iOutId, Wln_TrasformNameId(pNew, p, pObj) );
148         Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), iOutId );
149     }
150     // add internal nodes
151     Wlc_NtkForEachObj( p, pObj, i )
152     {
153         if ( Wlc_ObjIsCi(pObj) || pObj->Type == 0 )
154             continue;
155         iOutId = Wln_ObjAlloc( pNew, Ndr_TypeWlc2Ndr(pObj->Type), pObj->Signed, pObj->End, pObj->Beg );
156         Vec_IntClear( vFanins );
157         Wlc_ObjForEachFanin( pObj, iFanin, n )
158             Vec_IntPush( vFanins, Wlc_ObjCopy(p, iFanin) );
159         Wln_ObjAddFanins( pNew, iOutId, vFanins );
160         if ( pObj->Type == WLC_OBJ_BIT_SELECT )
161             Wln_ObjSetSlice( pNew, iOutId, Hash_Int2ManInsert(pNew->pRanges, pObj->End, pObj->Beg, 0) );
162         else if ( pObj->Type == WLC_OBJ_CONST )
163         {
164             char * pString = Wln_ConstFromBits(Wlc_ObjConstValue(pObj), Wlc_ObjRange(pObj));
165             Wln_ObjSetConst( pNew, iOutId, Abc_NamStrFindOrAdd(pNew->pManName, pString, NULL) );
166             ABC_FREE( pString );
167         }
168 //        else if ( Type == ABC_OPER_BIT_MUX && Vec_IntSize(vFanins) == 3 )
169 //            ABC_SWAP( int, Wln_ObjFanins(p, iObj)[1], Wln_ObjFanins(p, iObj)[2] );
170         Wln_ObjSetNameId( pNew, iOutId, Wln_TrasformNameId(pNew, p, pObj) );
171         Wlc_ObjSetCopy( p, i, iOutId );
172     }
173     Wlc_NtkForEachPo( p, pObj, i )
174     {
175         iOutId = Wln_ObjAlloc( pNew, ABC_OPER_CO, pObj->Signed, pObj->End, pObj->Beg );
176         Wln_ObjAddFanin( pNew, iOutId, Wlc_ObjCopy(p, Wlc_ObjId(p, pObj)) );
177         //Wln_ObjSetNameId( pNew, iOutId, Wln_TrasformNameId(pNew, p, pObj) );
178     }
179     assert( Vec_IntSize(vInits) == Wlc_NtkCoNum(p) - Wlc_NtkPoNum(p) );
180     Wlc_NtkForEachCo( p, pObj, i )
181     {
182         if ( i < Wlc_NtkPoNum(p) )
183             continue;
184         //char * pInNames[8] = {"d", "clk", "reset", "set", "enable", "async", "sre", "init"};
185         Vec_IntClear( vFanins );
186         Vec_IntPush( vFanins, Wlc_ObjCopy(p, Wlc_ObjFaninId0(pObj)) );
187         for ( n = 0; n < 6; n++ )
188             Vec_IntPush( vFanins, 0 );
189         Vec_IntPush( vFanins, Vec_IntEntry(vInits, i-Wlc_NtkPoNum(p)) );
190         Wln_ObjAddFanins( pNew, Vec_IntEntry(&pNew->vFfs, i-Wlc_NtkPoNum(p)), vFanins );
191     }
192     Vec_IntFree( vFanins );
193     Vec_IntFree( vInits );
194     return pNew;
195 }
Wln_NtkFromWlcTest(Wlc_Ntk_t * p)196 void Wln_NtkFromWlcTest( Wlc_Ntk_t * p )
197 {
198     Wln_Ntk_t * pNew = Wln_NtkFromWlc( p );
199     Wln_WriteVer( pNew, "test_wlc2wln.v" );
200     Wln_NtkFree( pNew );
201 }
202 
203 /**Function*************************************************************
204 
205   Synopsis    []
206 
207   Description []
208 
209   SideEffects []
210 
211   SeeAlso     []
212 
213 ***********************************************************************/
214 
215 ////////////////////////////////////////////////////////////////////////
216 ///                       END OF FILE                                ///
217 ////////////////////////////////////////////////////////////////////////
218 
219 
220 ABC_NAMESPACE_IMPL_END
221 
222