abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ifLibBox.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ifLibBox.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [FPGA mapping based on priority cuts.]
8 
9  Synopsis [Box library.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - November 21, 2006.]
16 
17  Revision [$Id: ifLibBox.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "if.h"
22 #include "misc/extra/extra.h"
23 #include "base/main/main.h"
24 
26 
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 #define If_LibBoxForEachBox( p, pBox, i ) \
32  Vec_PtrForEachEntry( If_Box_t *, p->vBoxes, pBox, i ) if ( pBox == NULL ) {} else
33 
34 ////////////////////////////////////////////////////////////////////////
35 /// FUNCTION DEFINITIONS ///
36 ////////////////////////////////////////////////////////////////////////
37 
38 /**Function*************************************************************
39 
40  Synopsis []
41 
42  Description []
43 
44  SideEffects []
45 
46  SeeAlso []
47 
48 ***********************************************************************/
49 If_Box_t * If_BoxStart( char * pName, int Id, int nPis, int nPos, int fSeq, int fBlack, int fOuter )
50 {
51  If_Box_t * p;
52  p = ABC_CALLOC( If_Box_t, 1 );
53  p->pName = pName; // consumes memory
54  p->Id = Id;
55  p->fSeq = (char)fSeq;
56  p->fBlack = (char)fBlack;
57  p->fOuter = (char)fOuter;
58  p->nPis = nPis;
59  p->nPos = nPos;
60  p->pDelays = ABC_CALLOC( int, nPis * nPos );
61  return p;
62 }
64 {
65  If_Box_t * pNew = NULL;
66  return pNew;
67 }
69 {
70  ABC_FREE( p->pDelays );
71  ABC_FREE( p->pName );
72  ABC_FREE( p );
73 }
74 
75 /**Function*************************************************************
76 
77  Synopsis []
78 
79  Description []
80 
81  SideEffects []
82 
83  SeeAlso []
84 
85 ***********************************************************************/
87 {
88  If_LibBox_t * p;
89  p = ABC_CALLOC( If_LibBox_t, 1 );
90  p->vBoxes = Vec_PtrAlloc( 100 );
91  return p;
92 }
94 {
95  If_LibBox_t * pNew = NULL;
96  return pNew;
97 }
99 {
100  If_Box_t * pBox;
101  int i;
102  if ( p == NULL )
103  return;
104  If_LibBoxForEachBox( p, pBox, i )
105  If_BoxFree( pBox );
106  Vec_PtrFree( p->vBoxes );
107  ABC_FREE( p );
108 }
109 
110 /**Function*************************************************************
111 
112  Synopsis []
113 
114  Description []
115 
116  SideEffects []
117 
118  SeeAlso []
119 
120 ***********************************************************************/
122 {
123  return (If_Box_t *)Vec_PtrEntry( p->vBoxes, Id );
124 }
126 {
127  If_Box_t * pBox;
128  int i;
129  if ( p == NULL )
130  return NULL;
131  If_LibBoxForEachBox( p, pBox, i )
132  if ( !strcmp(pBox->pName, pName) )
133  return pBox;
134  return NULL;
135 }
137 {
138  if ( pBox->Id >= Vec_PtrSize(p->vBoxes) )
139  Vec_PtrFillExtra( p->vBoxes, 2 * pBox->Id + 10, NULL );
140  assert( Vec_PtrEntry( p->vBoxes, pBox->Id ) == NULL );
141  Vec_PtrWriteEntry( p->vBoxes, pBox->Id, pBox );
142 }
143 
144 /**Function*************************************************************
145 
146  Synopsis []
147 
148  Description []
149 
150  SideEffects []
151 
152  SeeAlso []
153 
154 ***********************************************************************/
155 If_LibBox_t * If_LibBoxRead2( char * pFileName )
156 {
157  int nSize = 100000;
158  char * pBuffer;
159  FILE * pFile;
160  If_LibBox_t * p = NULL;
161  If_Box_t * pBox = NULL;
162  char * pToken, * pName;
163  int fSeq, fBlack, fOuter;
164  int i, Id, nPis, nPos;
165  pFile = fopen( pFileName, "rb" );
166  if ( pFile == NULL )
167  {
168  printf( "Cannot open file \"%s\".\n", pFileName );
169  return NULL;
170  }
171  // read lines
172  nPis = nPos = 0;
173  pBuffer = ABC_ALLOC( char, nSize );
174  while ( fgets( pBuffer, nSize, pFile ) )
175  {
176  pToken = strtok( pBuffer, " \n\r\t" );
177  if ( pToken == NULL )
178  continue;
179  if ( pToken[0] == '.' )
180  {
181  if ( !strcmp(pToken, ".box") )
182  {
183  // save ID
184  pToken = strtok( NULL, " \n\r\t" );
185  Id = atoi( pToken );
186  // save name
187  pToken = strtok( NULL, " \n\r\t" );
188  pName = Abc_UtilStrsav(pToken);
189  // save PIs
190  pToken = strtok( NULL, " \n\r\t" );
191  nPis = atoi( pToken );
192  // save POs
193  pToken = strtok( NULL, " \n\r\t" );
194  nPos = atoi( pToken );
195  // save attributes
196  fSeq = fBlack = fOuter = 0;
197  pToken = strtok( NULL, " \n\r\t" );
198  while ( pToken )
199  {
200  if ( !strcmp(pToken, "seq") )
201  fSeq = 1;
202  else if ( !strcmp(pToken, "black") )
203  fBlack = 1;
204  else if ( !strcmp(pToken, "outer") )
205  fOuter = 1;
206  else assert( !strcmp(pToken, "comb") || !strcmp(pToken, "white") || !strcmp(pToken, "inner") );
207  pToken = strtok( NULL, " \n\r\t" );
208  }
209  // create library
210  if ( p == NULL )
211  p = If_LibBoxStart();
212  // create box
213  pBox = If_BoxStart( pName, Id, nPis, nPos, fSeq, fBlack, fOuter );
214  If_LibBoxAdd( p, pBox );
215  }
216  continue;
217  }
218  // read the table
219  assert( nPis > 0 && nPos > 0 );
220  for ( i = 0; i < nPis * nPos; i++ )
221  {
222  while ( pToken == NULL )
223  {
224  if ( fgets( pBuffer, nSize, pFile ) == NULL )
225  { printf( "The table does not have enough entries.\n" ); fflush(stdout); assert( 0 ); }
226  pToken = strtok( pBuffer, " \n\r\t" );
227  }
228  pBox->pDelays[i] = (pToken[0] == '-') ? -1 : atoi(pToken);
229  pToken = strtok( NULL, " \n\r\t" );
230  }
231  pBox = NULL;
232  }
233  ABC_FREE( pBuffer );
234  fclose( pFile );
235  return p;
236 }
237 
238 
239 /**Function*************************************************************
240 
241  Synopsis []
242 
243  Description []
244 
245  SideEffects []
246 
247  SeeAlso []
248 
249 ***********************************************************************/
250 char * If_LibBoxGetToken( FILE * pFile )
251 {
252  static char pBuffer[1000];
253  char c, * pTemp = pBuffer;
254  while ( (c = fgetc(pFile)) != EOF )
255  {
256  if ( c == '#' )
257  {
258  while ( (c = fgetc(pFile)) != EOF )
259  if ( c == '\n' )
260  break;
261  }
262  if ( c == ' ' || c == '\t' || c == '\n' || c == '\r' )
263  {
264  if ( pTemp > pBuffer )
265  break;
266  continue;
267  }
268  *pTemp++ = c;
269  }
270  *pTemp = 0;
271  return pTemp > pBuffer ? pBuffer : NULL;
272 }
273 If_LibBox_t * If_LibBoxRead( char * pFileName )
274 {
275  FILE * pFile;
276  If_LibBox_t * p;
277  If_Box_t * pBox;
278  char * pToken, * pName;
279  int i, Id, fBlack, nPis, nPos;
280  pFile = fopen( pFileName, "rb" );
281  if ( pFile == NULL )
282  {
283  printf( "Cannot open file \"%s\".\n", pFileName );
284  return NULL;
285  }
286  // get the library name
287  pToken = If_LibBoxGetToken( pFile );
288  if ( pToken == NULL )
289  {
290  fclose( pFile );
291  printf( "Cannot read library name from file \"%s\".\n", pFileName );
292  return NULL;
293  }
294  if ( pToken[0] == '.' )
295  {
296  fclose( pFile );
297  printf( "Wrong box format. Please try \"read_box -e\".\n" );
298  return NULL;
299  }
300 
301  // create library
302  p = If_LibBoxStart();
303  while ( pToken )
304  {
305  // save name
306  pName = Abc_UtilStrsav(pToken);
307  // save ID
308  pToken = If_LibBoxGetToken( pFile );
309  Id = atoi( pToken );
310  // save white/black
311  pToken = If_LibBoxGetToken( pFile );
312  fBlack = !atoi( pToken );
313  // save PIs
314  pToken = If_LibBoxGetToken( pFile );
315  nPis = atoi( pToken );
316  // save POs
317  pToken = If_LibBoxGetToken( pFile );
318  nPos = atoi( pToken );
319  // create box
320  pBox = If_BoxStart( pName, Id, nPis, nPos, 0, fBlack, 0 );
321  If_LibBoxAdd( p, pBox );
322  // read the table
323  for ( i = 0; i < nPis * nPos; i++ )
324  {
325  pToken = If_LibBoxGetToken( pFile );
326  pBox->pDelays[i] = (pToken[0] == '-') ? -ABC_INFINITY : atoi(pToken);
327  }
328  // extract next name
329  pToken = If_LibBoxGetToken( pFile );
330  }
331  fclose( pFile );
332  return p;
333 }
334 void If_LibBoxPrint( FILE * pFile, If_LibBox_t * p )
335 {
336  If_Box_t * pBox;
337  int i, j, k;
338  fprintf( pFile, "# Box library written by ABC on %s.\n", Extra_TimeStamp() );
339  fprintf( pFile, "# <Name> <ID> <Type> <I> <O>\n" );
340  If_LibBoxForEachBox( p, pBox, i )
341  {
342  fprintf( pFile, "%s %d %d %d %d\n", pBox->pName, pBox->Id, !pBox->fBlack, pBox->nPis, pBox->nPos );
343  for ( j = 0; j < pBox->nPos; j++, printf("\n") )
344  for ( k = 0; k < pBox->nPis; k++ )
345  if ( pBox->pDelays[j * pBox->nPis + k] == -ABC_INFINITY )
346  fprintf( pFile, " - " );
347  else
348  fprintf( pFile, "%5d ", pBox->pDelays[j * pBox->nPis + k] );
349  }
350 }
351 void If_LibBoxWrite( char * pFileName, If_LibBox_t * p )
352 {
353  FILE * pFile;
354  pFile = fopen( pFileName, "wb" );
355  if ( pFile == NULL )
356  {
357  printf( "Cannot open file \"%s\".\n", pFileName );
358  return;
359  }
360  If_LibBoxPrint( pFile, p );
361  fclose( pFile );
362 }
363 
364 /**Function*************************************************************
365 
366  Synopsis []
367 
368  Description []
369 
370  SideEffects []
371 
372  SeeAlso []
373 
374 ***********************************************************************/
375 int If_LibBoxLoad( char * pFileName )
376 {
377  FILE * pFile;
378  If_LibBox_t * pLib;
379  char * pFileNameOther;
380  // check if library can be read
381  pFileNameOther = Extra_FileNameGenericAppend( pFileName, ".cdl" );
382  pFile = fopen( pFileNameOther, "r" );
383  if ( pFile == NULL )
384  return 0;
385  fclose( pFile );
386  // read library
387  pLib = If_LibBoxRead2( pFileNameOther );
388  // replace the current library
390  Abc_FrameSetLibBox( pLib );
391  return 1;
392 }
393 
394 
395 ////////////////////////////////////////////////////////////////////////
396 /// END OF FILE ///
397 ////////////////////////////////////////////////////////////////////////
398 
399 
401 
char fBlack
Definition: if.h:341
If_Box_t * If_BoxDup(If_Box_t *p)
Definition: ifLibBox.c:63
int * pDelays
Definition: if.h:347
void If_LibBoxPrint(FILE *pFile, If_LibBox_t *p)
Definition: ifLibBox.c:334
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void If_LibBoxAdd(If_LibBox_t *p, If_Box_t *pBox)
Definition: ifLibBox.c:136
char fOuter
Definition: if.h:342
void If_BoxFree(If_Box_t *p)
Definition: ifLibBox.c:68
static void Vec_PtrFillExtra(Vec_Ptr_t *p, int nSize, void *Fill)
Definition: vecPtr.h:469
char * strtok()
If_Box_t * If_LibBoxFindBox(If_LibBox_t *p, char *pName)
Definition: ifLibBox.c:125
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
If_LibBox_t * If_LibBoxRead(char *pFileName)
Definition: ifLibBox.c:273
#define If_LibBoxForEachBox(p, pBox, i)
DECLARATIONS ///.
Definition: ifLibBox.c:31
int strcmp()
char * pName
Definition: if.h:339
If_Box_t * If_LibBoxReadBox(If_LibBox_t *p, int Id)
Definition: ifLibBox.c:121
char fSeq
Definition: if.h:340
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
ABC_DLL void * Abc_FrameReadLibBox()
Definition: mainFrame.c:55
void If_LibBoxFree(If_LibBox_t *p)
Definition: ifLibBox.c:98
int nPis
Definition: if.h:345
Vec_Ptr_t * vBoxes
Definition: if.h:352
static void Vec_PtrWriteEntry(Vec_Ptr_t *p, int i, void *Entry)
Definition: vecPtr.h:396
ABC_DLL void Abc_FrameSetLibBox(void *pLib)
Definition: mainFrame.c:81
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
void If_LibBoxWrite(char *pFileName, If_LibBox_t *p)
Definition: ifLibBox.c:351
Definition: if.h:337
char * If_LibBoxGetToken(FILE *pFile)
Definition: ifLibBox.c:250
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define ABC_FREE(obj)
Definition: abc_global.h:232
If_LibBox_t * If_LibBoxStart()
Definition: ifLibBox.c:86
char * Extra_TimeStamp()
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
int If_LibBoxLoad(char *pFileName)
Definition: ifLibBox.c:375
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Definition: abc_global.h:216
int Id
Definition: if.h:344
#define assert(ex)
Definition: util_old.h:213
char * Extra_FileNameGenericAppend(char *pBase, char *pSuffix)
If_LibBox_t * If_LibBoxRead2(char *pFileName)
Definition: ifLibBox.c:155
If_Box_t * If_BoxStart(char *pName, int Id, int nPis, int nPos, int fSeq, int fBlack, int fOuter)
FUNCTION DEFINITIONS ///.
Definition: ifLibBox.c:49
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
If_LibBox_t * If_LibBoxDup(If_Box_t *p)
Definition: ifLibBox.c:93
int nPos
Definition: if.h:346
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223