abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioReadPla.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioReadPla.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedure to read network from file.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: ioReadPla.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 
30 static Abc_Ntk_t * Io_ReadPlaNetwork( Extra_FileReader_t * p, int fZeros );
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis [Reads the network from a PLA file.]
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
47 Abc_Ntk_t * Io_ReadPla( char * pFileName, int fZeros, int fCheck )
48 {
50  Abc_Ntk_t * pNtk;
51 
52  // start the file
53  p = Extra_FileReaderAlloc( pFileName, "#", "\n\r", " \t|" );
54 // p = Extra_FileReaderAlloc( pFileName, "", "\n\r", " \t|" );
55  if ( p == NULL )
56  return NULL;
57 
58  // read the network
59  pNtk = Io_ReadPlaNetwork( p, fZeros );
61  if ( pNtk == NULL )
62  return NULL;
63 
64  // make sure that everything is okay with the network structure
65  if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
66  {
67  printf( "Io_ReadPla: The network check has failed.\n" );
68  Abc_NtkDelete( pNtk );
69  return NULL;
70  }
71  return pNtk;
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 * pTermPi, * pTermPo, * pNode;
90  Vec_Str_t ** ppSops = NULL;
91  char Buffer[100];
92  int nInputs = -1, nOutputs = -1, nProducts = -1;
93  char * pCubeIn, * pCubeOut;
94  int i, k, iLine, nDigits, nCubes;
95 
96  // allocate the empty network
98 
99  // go through the lines of the file
100  nCubes = 0;
101  pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
102  while ( (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)) )
103  {
105  iLine = Extra_FileReaderGetLineNumber( p, 0 );
106 
107  // if it is the end of file, quit the loop
108  if ( strncmp( (char *)vTokens->pArray[0], ".e", 2 ) == 0 )
109  break;
110 
111  // if it is the model name, get the name
112  if ( strcmp( (char *)vTokens->pArray[0], ".model" ) == 0 )
113  {
114  ABC_FREE( pNtk->pName );
115  pNtk->pName = Extra_UtilStrsav( (char *)vTokens->pArray[1] );
116  continue;
117  }
118 
119  if ( vTokens->nSize == 1 )
120  {
121  printf( "%s (line %d): Wrong number of token.\n",
122  Extra_FileReaderGetFileName(p), iLine );
123  Abc_NtkDelete( pNtk );
124  Extra_ProgressBarStop( pProgress );
125  ABC_FREE( ppSops );
126  return NULL;
127  }
128 
129  if ( strcmp( (char *)vTokens->pArray[0], ".i" ) == 0 )
130  nInputs = atoi((char *)vTokens->pArray[1]);
131  else if ( strcmp( (char *)vTokens->pArray[0], ".o" ) == 0 )
132  nOutputs = atoi((char *)vTokens->pArray[1]);
133  else if ( strcmp( (char *)vTokens->pArray[0], ".p" ) == 0 )
134  nProducts = atoi((char *)vTokens->pArray[1]);
135  else if ( strcmp( (char *)vTokens->pArray[0], ".ilb" ) == 0 )
136  {
137  if ( vTokens->nSize - 1 != nInputs )
138  printf( "Warning: Mismatch between the number of PIs on the .i line (%d) and the number of PIs on the .ilb line (%d).\n", nInputs, vTokens->nSize - 1 );
139  for ( i = 1; i < vTokens->nSize; i++ )
140  Io_ReadCreatePi( pNtk, (char *)vTokens->pArray[i] );
141  }
142  else if ( strcmp( (char *)vTokens->pArray[0], ".ob" ) == 0 )
143  {
144  if ( vTokens->nSize - 1 != nOutputs )
145  printf( "Warning: Mismatch between the number of POs on the .o line (%d) and the number of POs on the .ob line (%d).\n", nOutputs, vTokens->nSize - 1 );
146  for ( i = 1; i < vTokens->nSize; i++ )
147  Io_ReadCreatePo( pNtk, (char *)vTokens->pArray[i] );
148  }
149  else
150  {
151  // check if the input/output names are given
152  if ( Abc_NtkPiNum(pNtk) == 0 )
153  {
154  if ( nInputs == -1 )
155  {
156  printf( "%s: The number of inputs is not specified.\n", Extra_FileReaderGetFileName(p) );
157  Abc_NtkDelete( pNtk );
158  Extra_ProgressBarStop( pProgress );
159  ABC_FREE( ppSops );
160  return NULL;
161  }
162  nDigits = Abc_Base10Log( nInputs );
163  for ( i = 0; i < nInputs; i++ )
164  {
165  sprintf( Buffer, "x%0*d", nDigits, i );
166  Io_ReadCreatePi( pNtk, Buffer );
167  }
168  }
169  if ( Abc_NtkPoNum(pNtk) == 0 )
170  {
171  if ( nOutputs == -1 )
172  {
173  printf( "%s: The number of outputs is not specified.\n", Extra_FileReaderGetFileName(p) );
174  Abc_NtkDelete( pNtk );
175  Extra_ProgressBarStop( pProgress );
176  ABC_FREE( ppSops );
177  return NULL;
178  }
179  nDigits = Abc_Base10Log( nOutputs );
180  for ( i = 0; i < nOutputs; i++ )
181  {
182  sprintf( Buffer, "z%0*d", nDigits, i );
183  Io_ReadCreatePo( pNtk, Buffer );
184  }
185  }
186  if ( Abc_NtkNodeNum(pNtk) == 0 )
187  { // first time here
188  // create the PO drivers and add them
189  // start the SOP covers
190  ppSops = ABC_ALLOC( Vec_Str_t *, nOutputs );
191  Abc_NtkForEachPo( pNtk, pTermPo, i )
192  {
193  ppSops[i] = Vec_StrAlloc( 100 );
194  // create the node
195  pNode = Abc_NtkCreateNode(pNtk);
196  // connect the node to the PO net
197  Abc_ObjAddFanin( Abc_ObjFanin0Ntk(pTermPo), pNode );
198  // connect the node to the PI nets
199  Abc_NtkForEachPi( pNtk, pTermPi, k )
200  Abc_ObjAddFanin( pNode, Abc_ObjFanout0Ntk(pTermPi) );
201  }
202  }
203  // read the cubes
204  if ( vTokens->nSize != 2 )
205  {
206  printf( "%s (line %d): Input and output cubes are not specified.\n",
207  Extra_FileReaderGetFileName(p), iLine );
208  Abc_NtkDelete( pNtk );
209  Extra_ProgressBarStop( pProgress );
210  ABC_FREE( ppSops );
211  return NULL;
212  }
213  pCubeIn = (char *)vTokens->pArray[0];
214  pCubeOut = (char *)vTokens->pArray[1];
215  if ( strlen(pCubeIn) != (unsigned)nInputs )
216  {
217  printf( "%s (line %d): Input cube length (%zu) differs from the number of inputs (%d).\n",
218  Extra_FileReaderGetFileName(p), iLine, strlen(pCubeIn), nInputs );
219  Abc_NtkDelete( pNtk );
220  return NULL;
221  }
222  if ( strlen(pCubeOut) != (unsigned)nOutputs )
223  {
224  printf( "%s (line %d): Output cube length (%zu) differs from the number of outputs (%d).\n",
225  Extra_FileReaderGetFileName(p), iLine, strlen(pCubeOut), nOutputs );
226  Abc_NtkDelete( pNtk );
227  Extra_ProgressBarStop( pProgress );
228  ABC_FREE( ppSops );
229  return NULL;
230  }
231  if ( fZeros )
232  {
233  for ( i = 0; i < nOutputs; i++ )
234  {
235  if ( pCubeOut[i] == '0' )
236  {
237  Vec_StrPrintStr( ppSops[i], pCubeIn );
238  Vec_StrPrintStr( ppSops[i], " 1\n" );
239  }
240  }
241  }
242  else
243  {
244  for ( i = 0; i < nOutputs; i++ )
245  {
246  if ( pCubeOut[i] == '1' )
247  {
248  Vec_StrPrintStr( ppSops[i], pCubeIn );
249  Vec_StrPrintStr( ppSops[i], " 1\n" );
250  }
251  }
252  }
253  nCubes++;
254  }
255  }
256  Extra_ProgressBarStop( pProgress );
257  if ( nProducts != -1 && nCubes != nProducts )
258  printf( "Warning: Mismatch between the number of cubes (%d) and the number on .p line (%d).\n",
259  nCubes, nProducts );
260 
261  // add the SOP covers
262  Abc_NtkForEachPo( pNtk, pTermPo, i )
263  {
264  pNode = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pTermPo) );
265  if ( ppSops[i]->nSize == 0 )
266  {
267  Abc_ObjRemoveFanins(pNode);
268  pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" );
269  Vec_StrFree( ppSops[i] );
270  continue;
271  }
272  Vec_StrPush( ppSops[i], 0 );
273  pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, ppSops[i]->pArray );
274  Vec_StrFree( ppSops[i] );
275  }
276  ABC_FREE( ppSops );
277  Abc_NtkFinalizeRead( pNtk );
278  return pNtk;
279 }
280 
281 
282 ////////////////////////////////////////////////////////////////////////
283 /// END OF FILE ///
284 ////////////////////////////////////////////////////////////////////////
285 
286 
287 
289 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static Llb_Mgr_t * p
Definition: llb3Image.c:950
char * Extra_FileReaderGetFileName(Extra_FileReader_t *p)
static Abc_Obj_t * Abc_ObjFanin0Ntk(Abc_Obj_t *pObj)
Definition: abc.h:375
int Extra_FileReaderGetLineNumber(Extra_FileReader_t *p, int iToken)
void Extra_FileReaderFree(Extra_FileReader_t *p)
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
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_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
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
char * Extra_UtilStrsav(const char *s)
ABC_DLL void Abc_ObjAddFanin(Abc_Obj_t *pObj, Abc_Obj_t *pFanin)
Definition: abcFanio.c:84
int strcmp()
void * pManFunc
Definition: abc.h:191
static int Abc_NtkNodeNum(Abc_Ntk_t *pNtk)
Definition: abc.h:293
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
char * pArray
Definition: bblif.c:51
ABC_DLL Abc_Ntk_t * Abc_NtkStartRead(char *pName)
Definition: abcNtk.c:333
static void Vec_StrFree(Vec_Str_t *p)
Definition: bblif.c:616
ABC_DLL int Abc_NtkCheckRead(Abc_Ntk_t *pNtk)
Definition: abcCheck.c:77
static int Abc_Base10Log(unsigned n)
Definition: abc_global.h:252
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
char * sprintf()
static Abc_Obj_t * Abc_ObjFanout0Ntk(Abc_Obj_t *pObj)
Definition: abc.h:376
void Extra_ProgressBarStop(ProgressBar *p)
ABC_DLL void Abc_ObjRemoveFanins(Abc_Obj_t *pObj)
Definition: abcFanio.c:141
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Abc_Ntk_t * Io_ReadPla(char *pFileName, int fZeros, int fCheck)
FUNCTION DEFINITIONS ///.
Definition: ioReadPla.c:47
void * Extra_FileReaderGetTokens(Extra_FileReader_t *p)
static int Abc_NtkPoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:286
int Extra_FileReaderGetFileSize(Extra_FileReader_t *p)
#define ABC_FREE(obj)
Definition: abc_global.h:232
static int Abc_NtkPiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:285
static Abc_Obj_t * Abc_NtkCreateNode(Abc_Ntk_t *pNtk)
Definition: abc.h:308
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
int strncmp()
static ABC_NAMESPACE_IMPL_START Abc_Ntk_t * Io_ReadPlaNetwork(Extra_FileReader_t *p, int fZeros)
DECLARATIONS ///.
Definition: ioReadPla.c:84
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
void * pData
Definition: abc.h:145
#define Abc_NtkForEachPo(pNtk, pPo, i)
Definition: abc.h:517
Abc_Obj_t * Io_ReadCreatePo(Abc_Ntk_t *pNtk, char *pName)
Definition: ioUtil.c:635
char * pName
Definition: abc.h:158
#define Abc_NtkForEachPi(pNtk, pPi, i)
Definition: abc.h:513
int Extra_FileReaderGetCurPosition(Extra_FileReader_t *p)