ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
ioReadBench.c
Go to the documentation of this file.
1
20
21#include "ioAbc.h"
22
24
25
29
30static Abc_Ntk_t * Io_ReadBenchNetwork( Extra_FileReader_t * p );
31
35
47Abc_Ntk_t * Io_ReadBench( char * pFileName, int fCheck )
48{
50 Abc_Ntk_t * pNtk;
51
52 // start the file
53 p = Extra_FileReaderAlloc( pFileName, "#", "\n\r", " \t,()=" );
54 if ( p == NULL )
55 return NULL;
56
57 // read the network
58 pNtk = Io_ReadBenchNetwork( p );
60 if ( pNtk == NULL )
61 return NULL;
62
63 // make sure that everything is okay with the network structure
64 if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
65 {
66 printf( "Io_ReadBench: The network check has failed.\n" );
67 Abc_NtkDelete( pNtk );
68 return NULL;
69 }
70 return pNtk;
71}
72
84Abc_Ntk_t * Io_ReadBenchNetwork( Extra_FileReader_t * p )
85{
86 ProgressBar * pProgress;
87 Vec_Ptr_t * vTokens;
88 Abc_Ntk_t * pNtk;
89 Abc_Obj_t * pNode, * pNet;
90 Vec_Str_t * vString;
91 unsigned uTruth[2048];
92 char * pType, ** ppNames, * pString;
93 int iLine, nNames, nDigits, fLutsPresent = 0;
94
95 // allocate the empty network
97 pNtk->nConstrs = 0;
98
99 // go through the lines of the file
100 vString = Vec_StrAlloc( 100 );
102 for ( iLine = 0; (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)); iLine++ )
103 {
104 Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p), NULL );
105
106 if ( vTokens->nSize == 1 )
107 {
108 printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
109 Vec_StrFree( vString );
110 Abc_NtkDelete( pNtk );
111 return NULL;
112 }
113
114 // get the type of the line
115 if ( strncmp( (char *)vTokens->pArray[0], "INPUT", 5 ) == 0 )
116 Io_ReadCreatePi( pNtk, (char *)vTokens->pArray[1] );
117 else if ( strncmp( (char *)vTokens->pArray[0], "OUTPUT", 5 ) == 0 )
118 Io_ReadCreatePo( pNtk, (char *)vTokens->pArray[1] );
119 else
120 {
121 // get the node name and the node type
122 pType = (char *)vTokens->pArray[1];
123 if ( strncmp(pType, "DFF", 3) == 0 ) // works for both DFF and DFFRSE
124 {
125 if ( Vec_PtrSize(vTokens) == 6 )
126 {
127 // create primary input to represent flop output
128 char pNetName[1000]; char * pName; int i;
129 char * pFlopOut = (char *)vTokens->pArray[0];
130 Abc_Obj_t * pNet = Abc_NtkFindOrCreateNet( pNtk, pFlopOut );
131 Abc_Obj_t * pTerm = Abc_NtkCreatePi( pNtk );
132 Abc_ObjAddFanin( pNet, pTerm );
133 // create four primary outputs to represent flop inputs
134 Vec_PtrForEachEntryStart( char *, vTokens, pName, i, 2 )
135 {
136 sprintf( pNetName, "%s_%s", pFlopOut, pName );
137 pNet = Abc_NtkFindOrCreateNet( pNtk, pName );
138 pTerm = Abc_NtkCreateNodeBuf( pNtk, pNet );
139 pNet = Abc_NtkFindOrCreateNet( pNtk, pNetName );
140 Abc_ObjAddFanin( pNet, pTerm );
141 pTerm = Abc_NtkCreatePo( pNtk );
142 Abc_ObjAddFanin( pTerm, pNet );
143 }
144 pNtk->nConstrs++;
145 }
146 else
147 {
148 pNode = Io_ReadCreateLatch( pNtk, (char *)vTokens->pArray[2], (char *)vTokens->pArray[0] );
149 // Abc_LatchSetInit0( pNode );
150 if ( pType[3] == '0' )
151 Abc_LatchSetInit0( pNode );
152 else if ( pType[3] == '1' )
153 Abc_LatchSetInit1( pNode );
154 else
155 Abc_LatchSetInitDc( pNode );
156 }
157 }
158 else if ( strcmp(pType, "LUT") == 0 )
159 {
160 fLutsPresent = 1;
161 ppNames = (char **)vTokens->pArray + 3;
162 nNames = vTokens->nSize - 3;
163 // check the number of inputs
164 if ( nNames > 15 )
165 {
166 printf( "%s: Currently cannot read truth tables with more than 8 inputs (%d).\n", Extra_FileReaderGetFileName(p), nNames );
167 Vec_StrFree( vString );
168 Abc_NtkDelete( pNtk );
169 return NULL;
170 }
171 // get the hex string
172 pString = (char *)vTokens->pArray[2];
173 if ( strncmp( pString, "0x", 2 ) )
174 {
175 printf( "%s: The LUT signature (%s) does not look like a hexadecimal beginning with \"0x\".\n", Extra_FileReaderGetFileName(p), pString );
176 Vec_StrFree( vString );
177 Abc_NtkDelete( pNtk );
178 return NULL;
179 }
180 pString += 2;
181 // pad the string with zero's if needed
182 nDigits = (1 << nNames) / 4;
183 if ( nDigits == 0 )
184 nDigits = 1;
185 if ( strlen(pString) < (unsigned)nDigits )
186 {
187 Vec_StrFill( vString, nDigits - strlen(pString), '0' );
188 Vec_StrPrintStr( vString, pString );
189 Vec_StrPush( vString, 0 );
190 pString = Vec_StrArray( vString );
191 }
192 // read the hex number from the string
193 if ( !Extra_ReadHexadecimal( uTruth, pString, nNames ) )
194 {
195 printf( "%s: Reading hexadecimal number (%s) has failed.\n", Extra_FileReaderGetFileName(p), pString );
196 Vec_StrFree( vString );
197 Abc_NtkDelete( pNtk );
198 return NULL;
199 }
200 // check if the node is a constant node
201 if ( Extra_TruthIsConst0(uTruth, nNames) )
202 {
203 pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, 0 );
204 Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" ) );
205 }
206 else if ( Extra_TruthIsConst1(uTruth, nNames) )
207 {
208 pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, 0 );
209 Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 1\n" ) );
210 }
211 else
212 {
213 // create the node
214 pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, nNames );
215 assert( nNames > 0 );
216 if ( nNames > 1 )
217 Abc_ObjSetData( pNode, Abc_SopCreateFromTruth((Mem_Flex_t *)pNtk->pManFunc, nNames, uTruth) );
218 else if ( pString[0] == '2' )
219 Abc_ObjSetData( pNode, Abc_SopCreateBuf((Mem_Flex_t *)pNtk->pManFunc) );
220 else if ( pString[0] == '1' )
221 Abc_ObjSetData( pNode, Abc_SopCreateInv((Mem_Flex_t *)pNtk->pManFunc) );
222 else
223 {
224 printf( "%s: Reading truth table (%s) of single-input node has failed.\n", Extra_FileReaderGetFileName(p), pString );
225 Vec_StrFree( vString );
226 Abc_NtkDelete( pNtk );
227 return NULL;
228 }
229 }
230 }
231 else
232 {
233 // create a new node and add it to the network
234 ppNames = (char **)vTokens->pArray + 2;
235 nNames = vTokens->nSize - 2;
236 pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, nNames );
237 // assign the cover
238 if ( strcmp(pType, "AND") == 0 || strcmp(pType, "and") == 0 )
239 Abc_ObjSetData( pNode, Abc_SopCreateAnd((Mem_Flex_t *)pNtk->pManFunc, nNames, NULL) );
240 else if ( strcmp(pType, "OR") == 0 || strcmp(pType, "or") == 0 )
241 Abc_ObjSetData( pNode, Abc_SopCreateOr((Mem_Flex_t *)pNtk->pManFunc, nNames, NULL) );
242 else if ( strcmp(pType, "NAND") == 0 || strcmp(pType, "nand") == 0 )
243 Abc_ObjSetData( pNode, Abc_SopCreateNand((Mem_Flex_t *)pNtk->pManFunc, nNames) );
244 else if ( strcmp(pType, "NOR") == 0 || strcmp(pType, "nor") == 0 )
245 Abc_ObjSetData( pNode, Abc_SopCreateNor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
246 else if ( strcmp(pType, "XOR") == 0 || strcmp(pType, "xor") == 0 )
247 Abc_ObjSetData( pNode, Abc_SopCreateXor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
248 else if ( strcmp(pType, "NXOR") == 0 || strcmp(pType, "XNOR") == 0 || strcmp(pType, "nxor") == 0 || strcmp(pType, "xnor") == 0 )
249 Abc_ObjSetData( pNode, Abc_SopCreateNxor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
250 else if ( strncmp(pType, "BUF", 3) == 0 || strcmp(pType, "buf") == 0 )
251 Abc_ObjSetData( pNode, Abc_SopCreateBuf((Mem_Flex_t *)pNtk->pManFunc) );
252 else if ( strcmp(pType, "NOT") == 0 || strcmp(pType, "not") == 0 )
253 Abc_ObjSetData( pNode, Abc_SopCreateInv((Mem_Flex_t *)pNtk->pManFunc) );
254 else if ( strncmp(pType, "MUX", 3) == 0 || strcmp(pType, "mux") == 0 )
255// Abc_ObjSetData( pNode, Abc_SopRegister(pNtk->pManFunc, "1-0 1\n-11 1\n") );
256 Abc_ObjSetData( pNode, Abc_SopRegister((Mem_Flex_t *)pNtk->pManFunc, "0-1 1\n11- 1\n") );
257 else if ( strncmp(pType, "gnd", 3) == 0 )
258 Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" ) );
259 else if ( strncmp(pType, "vdd", 3) == 0 )
260 Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 1\n" ) );
261 else
262 {
263 printf( "Io_ReadBenchNetwork(): Cannot determine gate type \"%s\" in line %d.\n", pType, Extra_FileReaderGetLineNumber(p, 0) );
264 Vec_StrFree( vString );
265 Abc_NtkDelete( pNtk );
266 return NULL;
267 }
268 }
269 }
270 }
271 Extra_ProgressBarStop( pProgress );
272 Vec_StrFree( vString );
273
274 // check if constant 0 is present
275 if ( (pNet = Abc_NtkFindNet( pNtk, "gnd" )) )
276 {
277 if ( Abc_ObjFaninNum(pNet) == 0 )
278 Io_ReadCreateConst( pNtk, "gnd", 0 );
279 }
280 if ( (pNet = Abc_NtkFindNet( pNtk, "1" )) )
281 {
282 if ( Abc_ObjFaninNum(pNet) == 0 )
283 {
284 printf( "Io_ReadBenchNetwork(): Adding constant 0 fanin to non-driven net \"1\".\n" );
285 Io_ReadCreateConst( pNtk, "1", 0 );
286 }
287 }
288 // check if constant 1 is present
289 if ( (pNet = Abc_NtkFindNet( pNtk, "vdd" )) )
290 {
291 if ( Abc_ObjFaninNum(pNet) == 0 )
292 Io_ReadCreateConst( pNtk, "vdd", 1 );
293 }
294 if ( (pNet = Abc_NtkFindNet( pNtk, "2" )) )
295 {
296 if ( Abc_ObjFaninNum(pNet) == 0 )
297 {
298 printf( "Io_ReadBenchNetwork(): Adding constant 1 fanin to non-driven net \"2\".\n" );
299 Io_ReadCreateConst( pNtk, "2", 1 );
300 }
301 }
302
303 Abc_NtkFinalizeRead( pNtk );
304
305 // if LUTs are present, collapse the truth tables into cubes
306 if ( fLutsPresent )
307 {
308 if ( !Abc_NtkToBdd(pNtk) )
309 {
310 printf( "Io_ReadBenchNetwork(): Converting to BDD has failed.\n" );
311 Abc_NtkDelete( pNtk );
312 return NULL;
313 }
314 if ( !Abc_NtkToSop(pNtk, -1, ABC_INFINITY) )
315 {
316 printf( "Io_ReadBenchNetwork(): Converting to SOP has failed.\n" );
317 Abc_NtkDelete( pNtk );
318 return NULL;
319 }
320 }
321 return pNtk;
322}
323
324
336void Io_ReadBenchInit( Abc_Ntk_t * pNtk, char * pFileName )
337{
338 char pBuffer[1000];
339 FILE * pFile;
340 char * pToken;
341 Abc_Obj_t * pObj;
342 int Num;
343 pFile = fopen( pFileName, "r" );
344 if ( pFile == NULL )
345 {
346 printf( "Io_ReadBenchInit(): Failed to open file \"%s\".\n", pFileName );
347 return;
348 }
349 while ( fgets( pBuffer, 999, pFile ) )
350 {
351 pToken = strtok( pBuffer, " \n\t\r" );
352 // find the latch output
353 Num = Nm_ManFindIdByName( pNtk->pManName, pToken, ABC_OBJ_BO );
354 if ( Num < 0 )
355 {
356 printf( "Io_ReadBenchInit(): Cannot find register with output %s.\n", pToken );
357 continue;
358 }
359 pObj = Abc_ObjFanin0( Abc_NtkObj( pNtk, Num ) );
360 if ( !Abc_ObjIsLatch(pObj) )
361 {
362 printf( "Io_ReadBenchInit(): The signal is not a register output %s.\n", pToken );
363 continue;
364 }
365 // assign the new init state
366 pToken = strtok( NULL, " \n\t\r" );
367 if ( pToken[0] == '0' )
368 Abc_LatchSetInit0( pObj );
369 else if ( pToken[0] == '1' )
370 Abc_LatchSetInit1( pObj );
371 else if ( pToken[0] == '2' )
372 Abc_LatchSetInitDc( pObj );
373 else
374 {
375 printf( "Io_ReadBenchInit(): The signal %s has unknown initial value (%s).\n",
376 Abc_ObjName(Abc_ObjFanout0(pObj)), pToken );
377 continue;
378 }
379 }
380 fclose( pFile );
381}
382
383
387
388
389
391
struct Abc_Obj_t_ Abc_Obj_t
Definition abc.h:116
ABC_DLL Abc_Obj_t * Abc_NtkFindOrCreateNet(Abc_Ntk_t *pNtk, char *pName)
Definition abcObj.c:587
ABC_DLL void Abc_ObjAddFanin(Abc_Obj_t *pObj, Abc_Obj_t *pFanin)
Definition abcFanio.c:84
ABC_DLL int Abc_NtkCheckRead(Abc_Ntk_t *pNtk)
Definition abcCheck.c:80
ABC_DLL Abc_Obj_t * Abc_NtkCreateNodeBuf(Abc_Ntk_t *pNtk, Abc_Obj_t *pFanin)
Definition abcObj.c:706
ABC_DLL char * Abc_SopCreateNxor(Mem_Flex_t *pMan, int nVars)
Definition abcSop.c:317
ABC_DLL int Abc_NtkToBdd(Abc_Ntk_t *pNtk)
Definition abcFunc.c:1299
@ ABC_OBJ_BO
Definition abc.h:92
ABC_DLL char * Abc_SopCreateInv(Mem_Flex_t *pMan)
Definition abcSop.c:351
ABC_DLL char * Abc_SopCreateOr(Mem_Flex_t *pMan, int nVars, int *pfCompl)
Definition abcSop.c:212
ABC_DLL Abc_Ntk_t * Abc_NtkStartRead(char *pName)
Definition abcNtk.c:386
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
ABC_DLL void Abc_NtkFinalizeRead(Abc_Ntk_t *pNtk)
Definition abcNtk.c:413
ABC_DLL char * Abc_SopCreateFromTruth(Mem_Flex_t *pMan, int nVars, unsigned *pTruth)
Definition abcSop.c:383
ABC_DLL char * Abc_SopCreateNand(Mem_Flex_t *pMan, int nVars)
Definition abcSop.c:190
ABC_DLL char * Abc_SopCreateAnd(Mem_Flex_t *pMan, int nVars, int *pfCompl)
Definition abcSop.c:168
ABC_DLL int Abc_NtkToSop(Abc_Ntk_t *pNtk, int fMode, int nCubeLimit)
Definition abcFunc.c:1261
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition abcNtk.c:1421
ABC_DLL char * Abc_SopCreateXor(Mem_Flex_t *pMan, int nVars)
Definition abcSop.c:280
ABC_DLL char * Abc_SopCreateNor(Mem_Flex_t *pMan, int nVars)
Definition abcSop.c:259
ABC_DLL char * Abc_SopRegister(Mem_Flex_t *pMan, const char *pName)
DECLARATIONS ///.
Definition abcSop.c:62
ABC_DLL char * Abc_SopCreateBuf(Mem_Flex_t *pMan)
Definition abcSop.c:367
ABC_DLL Abc_Obj_t * Abc_NtkFindNet(Abc_Ntk_t *pNtk, char *pName)
Definition abcObj.c:515
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Definition abc_global.h:250
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
struct Vec_Str_t_ Vec_Str_t
Definition bblif.c:46
ABC_NAMESPACE_IMPL_START typedef char ProgressBar
Definition bbrNtbdd.c:27
Cube * p
Definition exorList.c:222
void Extra_ProgressBarStop(ProgressBar *p)
char * Extra_FileReaderGetFileName(Extra_FileReader_t *p)
void * Extra_FileReaderGetTokens(Extra_FileReader_t *p)
Extra_FileReader_t * Extra_FileReaderAlloc(char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean)
FUNCTION DEFINITIONS ///.
int Extra_ReadHexadecimal(unsigned Sign[], char *pString, int nVars)
struct Extra_FileReader_t_ Extra_FileReader_t
Definition extra.h:135
int Extra_FileReaderGetLineNumber(Extra_FileReader_t *p, int iToken)
int Extra_FileReaderGetCurPosition(Extra_FileReader_t *p)
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
int Extra_FileReaderGetFileSize(Extra_FileReader_t *p)
void Extra_FileReaderFree(Extra_FileReader_t *p)
Abc_Obj_t * Io_ReadCreatePo(Abc_Ntk_t *pNtk, char *pName)
Definition ioUtil.c:644
Abc_Obj_t * Io_ReadCreateConst(Abc_Ntk_t *pNtk, char *pName, int fConst1)
Definition ioUtil.c:763
Abc_Obj_t * Io_ReadCreatePi(Abc_Ntk_t *pNtk, char *pName)
Definition ioUtil.c:619
Abc_Obj_t * Io_ReadCreateLatch(Abc_Ntk_t *pNtk, char *pNetLI, char *pNetLO)
Definition ioUtil.c:669
Abc_Obj_t * Io_ReadCreateNode(Abc_Ntk_t *pNtk, char *pNameOut, char *pNamesIn[], int nInputs)
Definition ioUtil.c:734
void Io_ReadBenchInit(Abc_Ntk_t *pNtk, char *pFileName)
Abc_Ntk_t * Io_ReadBench(char *pFileName, int fCheck)
FUNCTION DEFINITIONS ///.
Definition ioReadBench.c:47
struct Mem_Flex_t_ Mem_Flex_t
Definition mem.h:34
int Nm_ManFindIdByName(Nm_Man_t *p, char *pName, int Type)
Definition nmApi.c:219
int nConstrs
Definition abc.h:173
void * pManFunc
Definition abc.h:191
Nm_Man_t * pManName
Definition abc.h:160
#define assert(ex)
Definition util_old.h:213
int strncmp()
int strlen()
int strcmp()
char * sprintf()
char * strtok()
#define Vec_PtrForEachEntryStart(Type, vVec, pEntry, i, Start)
Definition vecPtr.h:57
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42