ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
mioForm.c
Go to the documentation of this file.
1
18
19#include "mioInt.h"
20#include "bdd/parse/parse.h"
21
23
24
28
29// these symbols (and no other) can appear in the formulas
30#define MIO_SYMB_AND '*'
31#define MIO_SYMB_AND2 '&'
32#define MIO_SYMB_OR '+'
33#define MIO_SYMB_OR2 '|'
34#define MIO_SYMB_XOR '^'
35#define MIO_SYMB_NOT '!'
36#define MIO_SYMB_AFTNOT '\''
37#define MIO_SYMB_OPEN '('
38#define MIO_SYMB_CLOSE ')'
39
40static int Mio_GateParseFormula( Mio_Gate_t * pGate );
41static int Mio_GateCollectNames( char * pFormula, char * pPinNames[] );
42
46
59{
60 Mio_Gate_t * pGate;
61
62 // count the gates
63 pLib->nGates = 0;
64 Mio_LibraryForEachGate( pLib, pGate )
65 pLib->nGates++;
66
67 // start a temporary BDD manager
68 pLib->dd = Cudd_Init( 20, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
69 // introduce ZDD variables
70 Cudd_zddVarsFromBddVars( pLib->dd, 2 );
71
72 // for each gate, derive its function
73 Mio_LibraryForEachGate( pLib, pGate )
74 if ( Mio_GateParseFormula( pGate ) )
75 return 1;
76 return 0;
77}
78
79
91char * Mio_SopRegister( Mem_Flex_t * pMan, char * pName )
92{
93 char * pRegName;
94 if ( pName == NULL ) return NULL;
95 pRegName = Mem_FlexEntryFetch( pMan, strlen(pName) + 1 );
96 strcpy( pRegName, pName );
97 return pRegName;
98}
99
111int Mio_GateParseFormula( Mio_Gate_t * pGate )
112{
113 extern char * Abc_ConvertBddToSop( Mem_Flex_t * pMan, DdManager * dd, DdNode * bFuncOn, DdNode * bFuncOnDc, int nFanins, int fAllPrimes, Vec_Str_t * vCube, int fMode );
114 DdManager * dd = pGate->pLib->dd;
115 char * pPinNames[100];
116 char * pPinNamesCopy[100];
117 Mio_Pin_t * pPin, ** ppPin;
118 int nPins, iPin, i;
119
120 // set the maximum delay of the gate; count pins
121 pGate->dDelayMax = 0.0;
122 nPins = 0;
123 Mio_GateForEachPin( pGate, pPin )
124 {
125 // set the maximum delay of the gate
126 if ( pGate->dDelayMax < pPin->dDelayBlockMax )
127 pGate->dDelayMax = pPin->dDelayBlockMax;
128 // count the pin
129 nPins++;
130 }
131
132 // check for the gate with const function
133 if ( nPins == 0 )
134 {
135 if ( strcmp( pGate->pForm, MIO_STRING_CONST0 ) == 0 )
136 {
137 pGate->bFunc = b0;
138 pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 0\n" );
139 pGate->pLib->pGate0 = pGate;
140 }
141 else if ( strcmp( pGate->pForm, MIO_STRING_CONST1 ) == 0 )
142 {
143 pGate->bFunc = b1;
144 pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 1\n" );
145 pGate->pLib->pGate1 = pGate;
146 }
147 else
148 {
149 printf( "Cannot parse formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
150 return 1;
151 }
152 Cudd_Ref( pGate->bFunc );
153 return 0;
154 }
155
156 // collect the names as they appear in the formula
157 nPins = Mio_GateCollectNames( pGate->pForm, pPinNames );
158 if ( nPins == 0 )
159 {
160 printf( "Cannot read formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
161 return 1;
162 }
163
164 // set the number of inputs
165 pGate->nInputs = nPins;
166
167 // consider the case when all the pins have identical pin info
168 if ( strcmp( pGate->pPins->pName, "*" ) == 0 )
169 {
170 // get the topmost (generic) pin
171 pPin = pGate->pPins;
172 ABC_FREE( pPin->pName );
173
174 // create individual pins from the generic pin
175 ppPin = &pPin->pNext;
176 for ( i = 1; i < nPins; i++ )
177 {
178 // get the new pin
179 *ppPin = Mio_PinDup( pPin );
180 // set its name
181 (*ppPin)->pName = pPinNames[i];
182 // prepare the next place in the list
183 ppPin = &((*ppPin)->pNext);
184 }
185 *ppPin = NULL;
186
187 // set the name of the topmost pin
188 pPin->pName = pPinNames[0];
189 }
190 else
191 {
192 // reorder the variable names to appear the save way as the pins
193 iPin = 0;
194 Mio_GateForEachPin( pGate, pPin )
195 {
196 // find the pin with the name pPin->pName
197 for ( i = 0; i < nPins; i++ )
198 {
199 if ( pPinNames[i] && strcmp( pPinNames[i], pPin->pName ) == 0 )
200 {
201 // free pPinNames[i] because it is already available as pPin->pName
202 // setting pPinNames[i] to NULL is useful to make sure that
203 // this name is not assigned to two pins in the list
204 ABC_FREE( pPinNames[i] );
205 pPinNamesCopy[iPin++] = pPin->pName;
206 break;
207 }
208 if ( i == nPins )
209 {
210 printf( "Cannot find pin name \"%s\" in the formula \"%s\" of gate \"%s\".\n",
211 pPin->pName, pGate->pForm, pGate->pName );
212 return 1;
213 }
214 }
215 }
216
217 // check for the remaining names
218 for ( i = 0; i < nPins; i++ )
219 if ( pPinNames[i] )
220 {
221 printf( "Name \"%s\" appears in the formula \"%s\" of gate \"%s\" but there is no such pin.\n",
222 pPinNames[i], pGate->pForm, pGate->pName );
223 return 1;
224 }
225
226 // copy the names back
227 memcpy( pPinNames, pPinNamesCopy, nPins * sizeof(char *) );
228 }
229
230 // expand the manager if necessary
231 if ( dd->size < nPins )
232 {
233 Cudd_Quit( dd );
234 dd = Cudd_Init( nPins + 10, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
235 Cudd_zddVarsFromBddVars( dd, 2 );
236 }
237
238 // derive the formula as the BDD
239 pGate->bFunc = Parse_FormulaParser( stdout, pGate->pForm, nPins, 0, pPinNames, dd, dd->vars );
240 if ( pGate->bFunc == NULL )
241 return 1;
242 Cudd_Ref( pGate->bFunc );
243
244 // derive the cover (SOP)
245 pGate->pSop = Abc_ConvertBddToSop( pGate->pLib->pMmFlex, dd, pGate->bFunc, pGate->bFunc, nPins, 0, pGate->pLib->vCube, -1 );
246
247 // derive the truth table
248 if ( pGate->nInputs <= 6 )
249 pGate->uTruth = Mio_DeriveTruthTable6( pGate );
250
251 return 0;
252}
253
265int Mio_GateCollectNames( char * pFormula, char * pPinNames[] )
266{
267 char Buffer[1000];
268 char * pTemp;
269 int nPins, i;
270
271 // save the formula as it was
272 strcpy( Buffer, pFormula );
273
274 // remove the non-name symbols
275 for ( pTemp = Buffer; *pTemp; pTemp++ )
276 if ( *pTemp == MIO_SYMB_AND || *pTemp == MIO_SYMB_AND2 ||
277 *pTemp == MIO_SYMB_OR || *pTemp == MIO_SYMB_OR2 ||
278 *pTemp == MIO_SYMB_XOR ||
279 *pTemp == MIO_SYMB_NOT || *pTemp == MIO_SYMB_AFTNOT ||
280 *pTemp == MIO_SYMB_OPEN || *pTemp == MIO_SYMB_CLOSE )
281 *pTemp = ' ';
282
283 // save the names
284 nPins = 0;
285 pTemp = strtok( Buffer, " " );
286 while ( pTemp )
287 {
288 for ( i = 0; i < nPins; i++ )
289 if ( strcmp( pTemp, pPinNames[i] ) == 0 )
290 break;
291 if ( i == nPins )
292 { // cannot find this name; save it
293 pPinNames[nPins++] = Abc_UtilStrsav(pTemp);
294 }
295 // get the next name
296 pTemp = strtok( NULL, " " );
297 }
298 return nPins;
299}
300
304
305
307
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
struct Vec_Str_t_ Vec_Str_t
Definition bblif.c:46
#define b0
Definition bbrImage.c:96
#define b1
Definition bbrImage.c:97
char * Mem_FlexEntryFetch(Mem_Flex_t *p, int nBytes)
Definition mem.c:388
#define MIO_SYMB_OR2
Definition mioForm.c:33
char * Mio_SopRegister(Mem_Flex_t *pMan, char *pName)
Definition mioForm.c:91
#define MIO_SYMB_XOR
Definition mioForm.c:34
#define MIO_SYMB_OPEN
Definition mioForm.c:37
#define MIO_SYMB_OR
Definition mioForm.c:32
#define MIO_SYMB_AND
DECLARATIONS ///.
Definition mioForm.c:30
#define MIO_SYMB_NOT
Definition mioForm.c:35
#define MIO_SYMB_CLOSE
Definition mioForm.c:38
#define MIO_SYMB_AFTNOT
Definition mioForm.c:36
int Mio_LibraryParseFormulas(Mio_Library_t *pLib)
FUNCTION DEFINITIONS ///.
Definition mioForm.c:58
#define MIO_SYMB_AND2
Definition mioForm.c:31
#define MIO_STRING_CONST1
Definition mioInt.h:51
#define MIO_STRING_CONST0
Definition mioInt.h:50
Mio_Pin_t * Mio_PinDup(Mio_Pin_t *pPin)
Definition mioUtils.c:131
struct Mio_LibraryStruct_t_ Mio_Library_t
Definition mio.h:42
#define Mio_LibraryForEachGate(Lib, Gate)
GLOBAL VARIABLES ///.
Definition mio.h:81
word Mio_DeriveTruthTable6(Mio_Gate_t *pGate)
Definition mioUtils.c:942
struct Mio_PinStruct_t_ Mio_Pin_t
Definition mio.h:44
#define Mio_GateForEachPin(Gate, Pin)
Definition mio.h:92
struct Mio_GateStruct_t_ Mio_Gate_t
Definition mio.h:43
struct Mem_Flex_t_ Mem_Flex_t
Definition mem.h:34
ABC_NAMESPACE_HEADER_START DdNode * Parse_FormulaParser(FILE *pOutput, char *pFormula, int nVars, int nRanks, char *ppVarNames[], DdManager *dd, DdNode *pbVars[])
INCLUDES ///.
Definition parseCore.c:116
Mio_Library_t * pLib
Definition mioInt.h:105
Mio_Pin_t * pPins
Definition mioInt.h:102
double dDelayMax
Definition mioInt.h:115
Mio_Gate_t * pGate1
Definition mioInt.h:70
Mio_Gate_t * pGate0
Definition mioInt.h:69
Vec_Str_t * vCube
Definition mioInt.h:79
Mem_Flex_t * pMmFlex
Definition mioInt.h:78
double dDelayBlockMax
Definition mioInt.h:133
Mio_Pin_t * pNext
Definition mioInt.h:134
char * memcpy()
int strlen()
int strcmp()
char * strtok()
char * strcpy()