abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioReadBench.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioReadBench.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures to read BENCH files.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: ioReadBench.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "ioAbc.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis [Reads the network from a BENCH file.]
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
47 Abc_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 
73 /**Function*************************************************************
74 
75  Synopsis []
76 
77  Description []
78 
79  SideEffects []
80 
81  SeeAlso []
82 
83 ***********************************************************************/
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[8];
92  char * pType, ** ppNames, * pString;
93  int iLine, nNames, nDigits, fLutsPresent = 0;
94 
95  // allocate the empty network
97 
98  // go through the lines of the file
99  vString = Vec_StrAlloc( 100 );
100  pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
101  for ( iLine = 0; (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)); iLine++ )
102  {
104 
105  if ( vTokens->nSize == 1 )
106  {
107  printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
108  Vec_StrFree( vString );
109  Abc_NtkDelete( pNtk );
110  return NULL;
111  }
112 
113  // get the type of the line
114  if ( strncmp( (char *)vTokens->pArray[0], "INPUT", 5 ) == 0 )
115  Io_ReadCreatePi( pNtk, (char *)vTokens->pArray[1] );
116  else if ( strncmp( (char *)vTokens->pArray[0], "OUTPUT", 5 ) == 0 )
117  Io_ReadCreatePo( pNtk, (char *)vTokens->pArray[1] );
118  else
119  {
120  // get the node name and the node type
121  pType = (char *)vTokens->pArray[1];
122  if ( strncmp(pType, "DFF", 3) == 0 ) // works for both DFF and DFFRSE
123  {
124  pNode = Io_ReadCreateLatch( pNtk, (char *)vTokens->pArray[2], (char *)vTokens->pArray[0] );
125 // Abc_LatchSetInit0( pNode );
126  if ( pType[3] == '0' )
127  Abc_LatchSetInit0( pNode );
128  else if ( pType[3] == '1' )
129  Abc_LatchSetInit1( pNode );
130  else
131  Abc_LatchSetInitDc( pNode );
132  }
133  else if ( strcmp(pType, "LUT") == 0 )
134  {
135  fLutsPresent = 1;
136  ppNames = (char **)vTokens->pArray + 3;
137  nNames = vTokens->nSize - 3;
138  // check the number of inputs
139  if ( nNames > 8 )
140  {
141  printf( "%s: Currently cannot read truth tables with more than 8 inputs (%d).\n", Extra_FileReaderGetFileName(p), nNames );
142  Vec_StrFree( vString );
143  Abc_NtkDelete( pNtk );
144  return NULL;
145  }
146  // get the hex string
147  pString = (char *)vTokens->pArray[2];
148  if ( strncmp( pString, "0x", 2 ) )
149  {
150  printf( "%s: The LUT signature (%s) does not look like a hexadecimal beginning with \"0x\".\n", Extra_FileReaderGetFileName(p), pString );
151  Vec_StrFree( vString );
152  Abc_NtkDelete( pNtk );
153  return NULL;
154  }
155  pString += 2;
156  // pad the string with zero's if needed
157  nDigits = (1 << nNames) / 4;
158  if ( nDigits == 0 )
159  nDigits = 1;
160  if ( strlen(pString) < (unsigned)nDigits )
161  {
162  Vec_StrFill( vString, nDigits - strlen(pString), '0' );
163  Vec_StrPrintStr( vString, pString );
164  Vec_StrPush( vString, 0 );
165  pString = Vec_StrArray( vString );
166  }
167  // read the hex number from the string
168  if ( !Extra_ReadHexadecimal( uTruth, pString, nNames ) )
169  {
170  printf( "%s: Reading hexadecimal number (%s) has failed.\n", Extra_FileReaderGetFileName(p), pString );
171  Vec_StrFree( vString );
172  Abc_NtkDelete( pNtk );
173  return NULL;
174  }
175  // check if the node is a constant node
176  if ( Extra_TruthIsConst0(uTruth, nNames) )
177  {
178  pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, 0 );
179  Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" ) );
180  }
181  else if ( Extra_TruthIsConst1(uTruth, nNames) )
182  {
183  pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, 0 );
184  Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 1\n" ) );
185  }
186  else
187  {
188  // create the node
189  pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, nNames );
190  assert( nNames > 0 );
191  if ( nNames > 1 )
192  Abc_ObjSetData( pNode, Abc_SopCreateFromTruth((Mem_Flex_t *)pNtk->pManFunc, nNames, uTruth) );
193  else if ( pString[0] == '2' )
195  else if ( pString[0] == '1' )
197  else
198  {
199  printf( "%s: Reading truth table (%s) of single-input node has failed.\n", Extra_FileReaderGetFileName(p), pString );
200  Vec_StrFree( vString );
201  Abc_NtkDelete( pNtk );
202  return NULL;
203  }
204  }
205  }
206  else
207  {
208  // create a new node and add it to the network
209  ppNames = (char **)vTokens->pArray + 2;
210  nNames = vTokens->nSize - 2;
211  pNode = Io_ReadCreateNode( pNtk, (char *)vTokens->pArray[0], ppNames, nNames );
212  // assign the cover
213  if ( strcmp(pType, "AND") == 0 )
214  Abc_ObjSetData( pNode, Abc_SopCreateAnd((Mem_Flex_t *)pNtk->pManFunc, nNames, NULL) );
215  else if ( strcmp(pType, "OR") == 0 )
216  Abc_ObjSetData( pNode, Abc_SopCreateOr((Mem_Flex_t *)pNtk->pManFunc, nNames, NULL) );
217  else if ( strcmp(pType, "NAND") == 0 )
218  Abc_ObjSetData( pNode, Abc_SopCreateNand((Mem_Flex_t *)pNtk->pManFunc, nNames) );
219  else if ( strcmp(pType, "NOR") == 0 )
220  Abc_ObjSetData( pNode, Abc_SopCreateNor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
221  else if ( strcmp(pType, "XOR") == 0 )
222  Abc_ObjSetData( pNode, Abc_SopCreateXor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
223  else if ( strcmp(pType, "NXOR") == 0 || strcmp(pType, "XNOR") == 0 )
224  Abc_ObjSetData( pNode, Abc_SopCreateNxor((Mem_Flex_t *)pNtk->pManFunc, nNames) );
225  else if ( strncmp(pType, "BUF", 3) == 0 )
227  else if ( strcmp(pType, "NOT") == 0 )
229  else if ( strncmp(pType, "MUX", 3) == 0 )
230 // Abc_ObjSetData( pNode, Abc_SopRegister(pNtk->pManFunc, "1-0 1\n-11 1\n") );
231  Abc_ObjSetData( pNode, Abc_SopRegister((Mem_Flex_t *)pNtk->pManFunc, "0-1 1\n11- 1\n") );
232  else if ( strncmp(pType, "gnd", 3) == 0 )
233  Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" ) );
234  else if ( strncmp(pType, "vdd", 3) == 0 )
235  Abc_ObjSetData( pNode, Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 1\n" ) );
236  else
237  {
238  printf( "Io_ReadBenchNetwork(): Cannot determine gate type \"%s\" in line %d.\n", pType, Extra_FileReaderGetLineNumber(p, 0) );
239  Vec_StrFree( vString );
240  Abc_NtkDelete( pNtk );
241  return NULL;
242  }
243  }
244  }
245  }
246  Extra_ProgressBarStop( pProgress );
247  Vec_StrFree( vString );
248 
249  // check if constant 0 is present
250  if ( (pNet = Abc_NtkFindNet( pNtk, "gnd" )) )
251  {
252  if ( Abc_ObjFaninNum(pNet) == 0 )
253  Io_ReadCreateConst( pNtk, "gnd", 0 );
254  }
255  if ( (pNet = Abc_NtkFindNet( pNtk, "1" )) )
256  {
257  if ( Abc_ObjFaninNum(pNet) == 0 )
258  {
259  printf( "Io_ReadBenchNetwork(): Adding constant 0 fanin to non-driven net \"1\".\n" );
260  Io_ReadCreateConst( pNtk, "1", 0 );
261  }
262  }
263  // check if constant 1 is present
264  if ( (pNet = Abc_NtkFindNet( pNtk, "vdd" )) )
265  {
266  if ( Abc_ObjFaninNum(pNet) == 0 )
267  Io_ReadCreateConst( pNtk, "vdd", 1 );
268  }
269  if ( (pNet = Abc_NtkFindNet( pNtk, "2" )) )
270  {
271  if ( Abc_ObjFaninNum(pNet) == 0 )
272  {
273  printf( "Io_ReadBenchNetwork(): Adding constant 1 fanin to non-driven net \"2\".\n" );
274  Io_ReadCreateConst( pNtk, "2", 1 );
275  }
276  }
277 
278  Abc_NtkFinalizeRead( pNtk );
279 
280  // if LUTs are present, collapse the truth tables into cubes
281  if ( fLutsPresent )
282  {
283  if ( !Abc_NtkToBdd(pNtk) )
284  {
285  printf( "Io_ReadBenchNetwork(): Converting to BDD has failed.\n" );
286  Abc_NtkDelete( pNtk );
287  return NULL;
288  }
289  if ( !Abc_NtkToSop(pNtk, 0) )
290  {
291  printf( "Io_ReadBenchNetwork(): Converting to SOP has failed.\n" );
292  Abc_NtkDelete( pNtk );
293  return NULL;
294  }
295  }
296  return pNtk;
297 }
298 
299 
300 /**Function*************************************************************
301 
302  Synopsis [Reads initial state in BENCH format.]
303 
304  Description []
305 
306  SideEffects []
307 
308  SeeAlso []
309 
310 ***********************************************************************/
311 void Io_ReadBenchInit( Abc_Ntk_t * pNtk, char * pFileName )
312 {
313  char pBuffer[1000];
314  FILE * pFile;
315  char * pToken;
316  Abc_Obj_t * pObj;
317  int Num;
318  pFile = fopen( pFileName, "r" );
319  if ( pFile == NULL )
320  {
321  printf( "Io_ReadBenchInit(): Failed to open file \"%s\".\n", pFileName );
322  return;
323  }
324  while ( fgets( pBuffer, 999, pFile ) )
325  {
326  pToken = strtok( pBuffer, " \n\t\r" );
327  // find the latch output
328  Num = Nm_ManFindIdByName( pNtk->pManName, pToken, ABC_OBJ_BO );
329  if ( Num < 0 )
330  {
331  printf( "Io_ReadBenchInit(): Cannot find register with output %s.\n", pToken );
332  continue;
333  }
334  pObj = Abc_ObjFanin0( Abc_NtkObj( pNtk, Num ) );
335  if ( !Abc_ObjIsLatch(pObj) )
336  {
337  printf( "Io_ReadBenchInit(): The signal is not a register output %s.\n", pToken );
338  continue;
339  }
340  // assign the new init state
341  pToken = strtok( NULL, " \n\t\r" );
342  if ( pToken[0] == '0' )
343  Abc_LatchSetInit0( pObj );
344  else if ( pToken[0] == '1' )
345  Abc_LatchSetInit1( pObj );
346  else if ( pToken[0] == '2' )
347  Abc_LatchSetInitDc( pObj );
348  else
349  {
350  printf( "Io_ReadBenchInit(): The signal %s has unknown initial value (%s).\n",
351  Abc_ObjName(Abc_ObjFanout0(pObj)), pToken );
352  continue;
353  }
354  }
355  fclose( pFile );
356 }
357 
358 
359 ////////////////////////////////////////////////////////////////////////
360 /// END OF FILE ///
361 ////////////////////////////////////////////////////////////////////////
362 
363 
364 
366 
static ABC_NAMESPACE_IMPL_START Abc_Ntk_t * Io_ReadBenchNetwork(Extra_FileReader_t *p)
DECLARATIONS ///.
Definition: ioReadBench.c:84
int Nm_ManFindIdByName(Nm_Man_t *p, char *pName, int Type)
Definition: nmApi.c:219
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
Nm_Man_t * pManName
Definition: abc.h:160
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static char * Vec_StrArray(Vec_Str_t *p)
Definition: vecStr.h:272
static int Abc_ObjIsLatch(Abc_Obj_t *pObj)
Definition: abc.h:356
char * Extra_FileReaderGetFileName(Extra_FileReader_t *p)
int Extra_FileReaderGetLineNumber(Extra_FileReader_t *p, int iToken)
ABC_DLL char * Abc_SopCreateAnd(Mem_Flex_t *pMan, int nVars, int *pfCompl)
Definition: abcSop.c:162
ABC_DLL char * Abc_SopCreateInv(Mem_Flex_t *pMan)
Definition: abcSop.c:345
static int Abc_ObjFaninNum(Abc_Obj_t *pObj)
Definition: abc.h:364
char * strtok()
void Extra_FileReaderFree(Extra_FileReader_t *p)
Abc_Obj_t * Io_ReadCreateConst(Abc_Ntk_t *pNtk, char *pName, int fConst1)
Definition: ioUtil.c:754
Abc_Ntk_t * Io_ReadBench(char *pFileName, int fCheck)
FUNCTION DEFINITIONS ///.
Definition: ioReadBench.c:47
static Vec_Str_t * Vec_StrAlloc(int nCap)
Definition: bblif.c:495
static void Vec_StrPush(Vec_Str_t *p, char Entry)
Definition: vecStr.h:535
ABC_DLL void Abc_NtkFinalizeRead(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:360
static Abc_Obj_t * Abc_NtkObj(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:314
static void Abc_LatchSetInitDc(Abc_Obj_t *pLatch)
Definition: abc.h:420
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
ABC_DLL char * Abc_SopCreateNand(Mem_Flex_t *pMan, int nVars)
Definition: abcSop.c:184
Extra_FileReader_t * Extra_FileReaderAlloc(char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean)
FUNCTION DEFINITIONS ///.
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
static void Abc_ObjSetData(Abc_Obj_t *pObj, void *pData)
Definition: abc.h:343
ABC_DLL char * Abc_SopCreateBuf(Mem_Flex_t *pMan)
Definition: abcSop.c:361
int strcmp()
void * pManFunc
Definition: abc.h:191
ABC_DLL char * Abc_SopRegister(Mem_Flex_t *pMan, char *pName)
DECLARATIONS ///.
Definition: abcSop.c:56
DECLARATIONS ///.
Abc_Obj_t * Io_ReadCreatePi(Abc_Ntk_t *pNtk, char *pName)
Definition: ioUtil.c:610
ABC_DLL Abc_Ntk_t * Abc_NtkStartRead(char *pName)
Definition: abcNtk.c:333
ABC_DLL int Abc_NtkToSop(Abc_Ntk_t *pNtk, int fDirect)
Definition: abcFunc.c:1124
static void Vec_StrFree(Vec_Str_t *p)
Definition: bblif.c:616
ABC_DLL char * Abc_SopCreateNxor(Mem_Flex_t *pMan, int nVars)
Definition: abcSop.c:311
ABC_DLL int Abc_NtkCheckRead(Abc_Ntk_t *pNtk)
Definition: abcCheck.c:77
static void Vec_StrFill(Vec_Str_t *p, int nSize, char Fill)
Definition: vecStr.h:423
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
ABC_DLL Abc_Obj_t * Abc_NtkFindNet(Abc_Ntk_t *pNtk, char *pName)
Definition: abcObj.c:507
if(last==0)
Definition: sparse_int.h:34
ABC_DLL char * Abc_SopCreateXor(Mem_Flex_t *pMan, int nVars)
Definition: abcSop.c:274
ABC_DLL int Abc_NtkToBdd(Abc_Ntk_t *pNtk)
Definition: abcFunc.c:1160
int Extra_ReadHexadecimal(unsigned Sign[], char *pString, int nVars)
void Extra_ProgressBarStop(ProgressBar *p)
Abc_Obj_t * Io_ReadCreateLatch(Abc_Ntk_t *pNtk, char *pNetLI, char *pNetLO)
Definition: ioUtil.c:660
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void Abc_LatchSetInit1(Abc_Obj_t *pLatch)
Definition: abc.h:419
ABC_DLL char * Abc_SopCreateNor(Mem_Flex_t *pMan, int nVars)
Definition: abcSop.c:253
ABC_DLL char * Abc_SopCreateFromTruth(Mem_Flex_t *pMan, int nVars, unsigned *pTruth)
Definition: abcSop.c:377
static int Extra_TruthIsConst1(unsigned *pIn, int nVars)
Definition: extra.h:286
void * Extra_FileReaderGetTokens(Extra_FileReader_t *p)
int Extra_FileReaderGetFileSize(Extra_FileReader_t *p)
ABC_DLL char * Abc_ObjName(Abc_Obj_t *pNode)
DECLARATIONS ///.
Definition: abcNames.c:48
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
int strncmp()
static int Extra_TruthIsConst0(unsigned *pIn, int nVars)
Definition: extra.h:278
Definition: abc.h:92
Abc_Obj_t * Io_ReadCreateNode(Abc_Ntk_t *pNtk, char *pNameOut, char *pNamesIn[], int nInputs)
Definition: ioUtil.c:725
#define assert(ex)
Definition: util_old.h:213
static void Extra_ProgressBarUpdate(ProgressBar *p, int nItemsCur, char *pString)
Definition: extra.h:243
int strlen()
static void Vec_StrPrintStr(Vec_Str_t *p, const char *pStr)
Definition: vecStr.h:627
Abc_Obj_t * Io_ReadCreatePo(Abc_Ntk_t *pNtk, char *pName)
Definition: ioUtil.c:635
static void Abc_LatchSetInit0(Abc_Obj_t *pLatch)
Definition: abc.h:418
ABC_DLL char * Abc_SopCreateOr(Mem_Flex_t *pMan, int nVars, int *pfCompl)
Definition: abcSop.c:206
void Io_ReadBenchInit(Abc_Ntk_t *pNtk, char *pFileName)
Definition: ioReadBench.c:311
static Abc_Obj_t * Abc_ObjFanout0(Abc_Obj_t *pObj)
Definition: abc.h:371
int Extra_FileReaderGetCurPosition(Extra_FileReader_t *p)