ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
abcAttach.c
Go to the documentation of this file.
1
20
21#include "base/abc/abc.h"
22#include "base/main/main.h"
23#include "map/mio/mio.h"
24
26
27
31
32#define ATTACH_FULL (~((unsigned)0))
33#define ATTACH_MASK(n) ((~((unsigned)0)) >> (32-(n)))
34
35static void Abc_AttachSetupTruthTables( unsigned uTruths[][2] );
36static void Abc_AttachComputeTruth( char * pSop, unsigned uTruthsIn[][2], unsigned * uTruthNode );
37static Mio_Gate_t * Abc_AttachFind( Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned * uTruthNode, int * Perm );
38static int Abc_AttachCompare( unsigned ** puTruthGates, int nGates, unsigned * uTruthNode );
39static int Abc_NodeAttach( Abc_Obj_t * pNode, Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned uTruths[][2] );
40static void Abc_TruthPermute( char * pPerm, int nVars, unsigned * uTruthNode, unsigned * uTruthPerm );
41
42static char ** s_pPerms = NULL;
43static int s_nPerms;
44
48
61{
62 Mio_Library_t * pGenlib;
63 unsigned ** puTruthGates;
64 unsigned uTruths[6][2];
65 Abc_Obj_t * pNode;
66 Mio_Gate_t ** ppGates;
67 int nGates, nFanins, i;
68
69 assert( Abc_NtkIsSopLogic(pNtk) );
70
71 // check that the library is available
73 if ( pGenlib == NULL )
74 {
75 printf( "The current library is not available.\n" );
76 return 0;
77 }
78
79 // start the truth tables
80 Abc_AttachSetupTruthTables( uTruths );
81
82 // collect all the gates
83 ppGates = Mio_CollectRoots( pGenlib, 6, (float)1.0e+20, 1, &nGates, 0 );
84
85 // derive the gate truth tables
86 puTruthGates = ABC_ALLOC( unsigned *, nGates );
87 puTruthGates[0] = ABC_ALLOC( unsigned, 2 * nGates );
88 for ( i = 1; i < nGates; i++ )
89 puTruthGates[i] = puTruthGates[i-1] + 2;
90 for ( i = 0; i < nGates; i++ )
91 Mio_DeriveTruthTable( ppGates[i], uTruths, Mio_GateReadPinNum(ppGates[i]), 6, puTruthGates[i] );
92
93 // assign the gates to pNode->pCopy
94 Abc_NtkCleanCopy( pNtk );
95 Abc_NtkForEachNode( pNtk, pNode, i )
96 {
97 nFanins = Abc_ObjFaninNum(pNode);
98 if ( nFanins == 0 )
99 {
100 if ( Abc_SopIsConst1((char *)pNode->pData) )
101 pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadConst1(pGenlib);
102 else
103 pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadConst0(pGenlib);
104 }
105 else if ( nFanins == 1 )
106 {
107 if ( Abc_SopIsBuf((char *)pNode->pData) )
108 pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadBuf(pGenlib);
109 else
110 pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadInv(pGenlib);
111 }
112 else if ( nFanins > 6 )
113 {
114 printf( "Cannot attach gate with more than 6 inputs to node %s.\n", Abc_ObjName(pNode) );
115 ABC_FREE( puTruthGates[0] );
116 ABC_FREE( puTruthGates );
117 ABC_FREE( ppGates );
118 return 0;
119 }
120 else if ( !Abc_NodeAttach( pNode, ppGates, puTruthGates, nGates, uTruths ) )
121 {
122 printf( "Could not attach the library gate to node %s.\n", Abc_ObjName(pNode) );
123 ABC_FREE( puTruthGates[0] );
124 ABC_FREE( puTruthGates );
125 ABC_FREE( ppGates );
126 return 0;
127 }
128 }
129 ABC_FREE( puTruthGates[0] );
130 ABC_FREE( puTruthGates );
131 ABC_FREE( ppGates );
132 ABC_FREE( s_pPerms );
133
134 // perform the final transformation
135 Abc_NtkForEachNode( pNtk, pNode, i )
136 {
137 if ( pNode->pCopy == NULL )
138 {
139 printf( "Some elementary gates (constant, buffer, or inverter) are missing in the library.\n" );
140 return 0;
141 }
142 }
143
144 // replace SOP representation by the gate representation
145 Abc_NtkForEachNode( pNtk, pNode, i )
146 pNode->pData = pNode->pCopy, pNode->pCopy = NULL;
147 pNtk->ntkFunc = ABC_FUNC_MAP;
149 pNtk->pManFunc = pGenlib;
150
151 printf( "Library gates are successfully attached to the nodes.\n" );
152
153 // make sure that everything is okay
154 if ( !Abc_NtkCheck( pNtk ) )
155 {
156 printf( "Abc_NtkAttach: The network check has failed.\n" );
157 return 0;
158 }
159 return 1;
160}
161
173int Abc_NodeAttach( Abc_Obj_t * pNode, Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned uTruths[][2] )
174{
175 int Perm[10];
176 int pTempInts[10];
177 unsigned uTruthNode[2];
178 Abc_Obj_t * pFanin;
179 Mio_Gate_t * pGate;
180 int nFanins, i;
181
182 // compute the node's truth table
183 Abc_AttachComputeTruth( (char *)pNode->pData, uTruths, uTruthNode );
184 // find the matching gate and permutation
185 pGate = Abc_AttachFind( ppGates, puTruthGates, nGates, uTruthNode, Perm );
186 if ( pGate == NULL )
187 return 0;
188 // permute the fanins
189 nFanins = Abc_ObjFaninNum(pNode);
190 Abc_ObjForEachFanin( pNode, pFanin, i )
191 pTempInts[i] = pFanin->Id;
192 for ( i = 0; i < nFanins; i++ )
193 pNode->vFanins.pArray[Perm[i]] = pTempInts[i];
194 // set the gate
195 pNode->pCopy = (Abc_Obj_t *)pGate;
196 return 1;
197}
198
210void Abc_AttachSetupTruthTables( unsigned uTruths[][2] )
211{
212 int m, v;
213 for ( v = 0; v < 5; v++ )
214 uTruths[v][0] = 0;
215 // set up the truth tables
216 for ( m = 0; m < 32; m++ )
217 for ( v = 0; v < 5; v++ )
218 if ( m & (1 << v) )
219 uTruths[v][0] |= (1 << m);
220 // make adjustments for the case of 6 variables
221 for ( v = 0; v < 5; v++ )
222 uTruths[v][1] = uTruths[v][0];
223 uTruths[5][0] = 0;
224 uTruths[5][1] = ATTACH_FULL;
225}
226
238void Abc_AttachComputeTruth( char * pSop, unsigned uTruthsIn[][2], unsigned * uTruthRes )
239{
240// Mvc_Cube_t * pCube;
241 unsigned uSignCube[2];
242 int Value;
243// int nInputs = pCover->nBits/2;
244 int nInputs = 6;
245 int nFanins = Abc_SopGetVarNum(pSop);
246 char * pCube;
247 int k;
248
249 // make sure that the number of input truth tables in equal to the number of gate inputs
250 assert( nInputs < 7 );
251
252 // clean the resulting truth table
253 uTruthRes[0] = 0;
254 uTruthRes[1] = 0;
255 if ( nInputs < 6 )
256 {
257 // consider the case when only one unsigned can be used
258// Mvc_CoverForEachCube( pCover, pCube )
259 Abc_SopForEachCube( pSop, nFanins, pCube )
260 {
261 uSignCube[0] = ATTACH_FULL;
262// Mvc_CubeForEachVarValue( pCover, pCube, Var, Value )
263 Abc_CubeForEachVar( pCube, Value, k )
264 {
265 if ( Value == '0' )
266 uSignCube[0] &= ~uTruthsIn[k][0];
267 else if ( Value == '1' )
268 uSignCube[0] &= uTruthsIn[k][0];
269 }
270 uTruthRes[0] |= uSignCube[0];
271 }
272 if ( Abc_SopGetPhase(pSop) == 0 )
273 uTruthRes[0] = ~uTruthRes[0];
274 if ( nInputs < 5 )
275 uTruthRes[0] &= ATTACH_MASK(1<<nInputs);
276 }
277 else
278 {
279 // consider the case when two unsigneds should be used
280// Mvc_CoverForEachCube( pCover, pCube )
281 Abc_SopForEachCube( pSop, nFanins, pCube )
282 {
283 uSignCube[0] = ATTACH_FULL;
284 uSignCube[1] = ATTACH_FULL;
285// Mvc_CubeForEachVarValue( pCover, pCube, Var, Value )
286 Abc_CubeForEachVar( pCube, Value, k )
287 {
288 if ( Value == '0' )
289 {
290 uSignCube[0] &= ~uTruthsIn[k][0];
291 uSignCube[1] &= ~uTruthsIn[k][1];
292 }
293 else if ( Value == '1' )
294 {
295 uSignCube[0] &= uTruthsIn[k][0];
296 uSignCube[1] &= uTruthsIn[k][1];
297 }
298 }
299 uTruthRes[0] |= uSignCube[0];
300 uTruthRes[1] |= uSignCube[1];
301 }
302
303 // complement if the SOP is complemented
304 if ( Abc_SopGetPhase(pSop) == 0 )
305 {
306 uTruthRes[0] = ~uTruthRes[0];
307 uTruthRes[1] = ~uTruthRes[1];
308 }
309 }
310}
311
323Mio_Gate_t * Abc_AttachFind( Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned * uTruthNode, int * Perm )
324{
325 unsigned uTruthPerm[2];
326 int i, v, iNum;
327
328 // try the gates without permutation
329 if ( (iNum = Abc_AttachCompare( puTruthGates, nGates, uTruthNode )) >= 0 )
330 {
331 for ( v = 0; v < 6; v++ )
332 Perm[v] = v;
333 return ppGates[iNum];
334 }
335 // get permutations
336 if ( s_pPerms == NULL )
337 {
338 s_pPerms = Extra_Permutations( 6 );
339 s_nPerms = Extra_Factorial( 6 );
340 }
341 // try permutations
342 for ( i = 0; i < s_nPerms; i++ )
343 {
344 Abc_TruthPermute( s_pPerms[i], 6, uTruthNode, uTruthPerm );
345 if ( (iNum = Abc_AttachCompare( puTruthGates, nGates, uTruthPerm )) >= 0 )
346 {
347 for ( v = 0; v < 6; v++ )
348 Perm[v] = (int)s_pPerms[i][v];
349 return ppGates[iNum];
350 }
351 }
352 return NULL;
353}
354
366int Abc_AttachCompare( unsigned ** puTruthGates, int nGates, unsigned * uTruthNode )
367{
368 int i;
369 for ( i = 0; i < nGates; i++ )
370 if ( puTruthGates[i][0] == uTruthNode[0] && puTruthGates[i][1] == uTruthNode[1] )
371 return i;
372 return -1;
373}
374
386void Abc_TruthPermute( char * pPerm, int nVars, unsigned * uTruthNode, unsigned * uTruthPerm )
387{
388 int nMints, iMintPerm, iMint, v;
389 uTruthPerm[0] = uTruthPerm[1] = 0;
390 nMints = (1 << nVars);
391 for ( iMint = 0; iMint < nMints; iMint++ )
392 {
393 if ( (uTruthNode[iMint>>5] & (1 << (iMint&31))) == 0 )
394 continue;
395 iMintPerm = 0;
396 for ( v = 0; v < nVars; v++ )
397 if ( iMint & (1 << v) )
398 iMintPerm |= (1 << pPerm[v]);
399 uTruthPerm[iMintPerm>>5] |= (1 << (iMintPerm&31));
400 }
401}
402
406
407
409
int Abc_NtkAttach(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition abcAttach.c:60
#define ATTACH_MASK(n)
Definition abcAttach.c:33
#define ATTACH_FULL
DECLARATIONS ///.
Definition abcAttach.c:32
struct Abc_Obj_t_ Abc_Obj_t
Definition abc.h:116
ABC_DLL int Abc_NtkCheck(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition abcCheck.c:64
#define Abc_ObjForEachFanin(pObj, pFanin, i)
Definition abc.h:527
ABC_DLL int Abc_SopGetPhase(char *pSop)
Definition abcSop.c:604
#define Abc_CubeForEachVar(pCube, Value, i)
Definition abc.h:536
ABC_DLL char * Abc_ObjName(Abc_Obj_t *pNode)
DECLARATIONS ///.
Definition abcNames.c:49
struct Abc_Ntk_t_ Abc_Ntk_t
Definition abc.h:115
#define Abc_SopForEachCube(pSop, nFanins, pCube)
Definition abc.h:538
ABC_DLL int Abc_SopGetVarNum(char *pSop)
Definition abcSop.c:584
ABC_DLL void Abc_NtkCleanCopy(Abc_Ntk_t *pNtk)
Definition abcUtil.c:540
@ ABC_FUNC_MAP
Definition abc.h:68
ABC_DLL int Abc_SopIsBuf(char *pSop)
Definition abcSop.c:756
ABC_DLL int Abc_SopIsConst1(char *pSop)
Definition abcSop.c:740
#define Abc_NtkForEachNode(pNtk, pNode, i)
Definition abc.h:464
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
ABC_DLL void * Abc_FrameReadLibGen()
Definition mainFrame.c:59
char ** Extra_Permutations(int n)
void Extra_MmFlexStop(Extra_MmFlex_t *p)
int Extra_Factorial(int n)
struct Extra_MmFlex_t_ Extra_MmFlex_t
Definition extra.h:148
Mio_Gate_t * Mio_LibraryReadConst0(Mio_Library_t *pLib)
Definition mioApi.c:51
int Mio_GateReadPinNum(Mio_Gate_t *pGate)
Definition mioApi.c:177
Mio_Gate_t ** Mio_CollectRoots(Mio_Library_t *pLib, int nInputs, float tDelay, int fSkipInv, int *pnGates, int fVerbose)
Definition mioUtils.c:515
void Mio_DeriveTruthTable(Mio_Gate_t *pGate, unsigned uTruthsIn[][2], int nSigns, int nInputs, unsigned uTruthRes[])
Definition mioUtils.c:1036
struct Mio_LibraryStruct_t_ Mio_Library_t
Definition mio.h:42
Mio_Gate_t * Mio_LibraryReadConst1(Mio_Library_t *pLib)
Definition mioApi.c:52
Mio_Gate_t * Mio_LibraryReadInv(Mio_Library_t *pLib)
Definition mioApi.c:50
struct Mio_GateStruct_t_ Mio_Gate_t
Definition mio.h:43
Mio_Gate_t * Mio_LibraryReadBuf(Mio_Library_t *pLib)
Definition mioApi.c:49
void * pManFunc
Definition abc.h:191
Abc_NtkFunc_t ntkFunc
Definition abc.h:157
void * pData
Definition abc.h:145
Vec_Int_t vFanins
Definition abc.h:143
int Id
Definition abc.h:132
Abc_Obj_t * pCopy
Definition abc.h:148
#define assert(ex)
Definition util_old.h:213