abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioWriteBlifMv.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioWriteBlifMv.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures to write BLIF-MV 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: ioWriteBlifMv.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "ioAbc.h"
22 #include "base/main/main.h"
23 #include "map/mio/mio.h"
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////
29 /// DECLARATIONS ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 static void Io_NtkWriteBlifMv( FILE * pFile, Abc_Ntk_t * pNtk );
33 static void Io_NtkWriteBlifMvOne( FILE * pFile, Abc_Ntk_t * pNtk );
34 static void Io_NtkWriteBlifMvPis( FILE * pFile, Abc_Ntk_t * pNtk );
35 static void Io_NtkWriteBlifMvPos( FILE * pFile, Abc_Ntk_t * pNtk );
36 static void Io_NtkWriteBlifMvAsserts( FILE * pFile, Abc_Ntk_t * pNtk );
37 static void Io_NtkWriteBlifMvNodeFanins( FILE * pFile, Abc_Obj_t * pNode );
38 static void Io_NtkWriteBlifMvNode( FILE * pFile, Abc_Obj_t * pNode );
39 static void Io_NtkWriteBlifMvLatch( FILE * pFile, Abc_Obj_t * pLatch );
40 static void Io_NtkWriteBlifMvSubckt( FILE * pFile, Abc_Obj_t * pNode );
41 static void Io_NtkWriteBlifMvValues( FILE * pFile, Abc_Obj_t * pNode );
42 
43 ////////////////////////////////////////////////////////////////////////
44 /// FUNCTION DEFINITIONS ///
45 ////////////////////////////////////////////////////////////////////////
46 
47 /**Function*************************************************************
48 
49  Synopsis [Write the network into a BLIF file with the given name.]
50 
51  Description []
52 
53  SideEffects []
54 
55  SeeAlso []
56 
57 ***********************************************************************/
58 void Io_WriteBlifMv( Abc_Ntk_t * pNtk, char * FileName )
59 {
60  FILE * pFile;
61  Abc_Ntk_t * pNtkTemp;
62  int i;
63  assert( Abc_NtkIsNetlist(pNtk) );
64  assert( Abc_NtkHasBlifMv(pNtk) );
65  // start writing the file
66  pFile = fopen( FileName, "w" );
67  if ( pFile == NULL )
68  {
69  fprintf( stdout, "Io_WriteBlifMv(): Cannot open the output file.\n" );
70  return;
71  }
72  fprintf( pFile, "# Benchmark \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
73  // write the master network
74  Io_NtkWriteBlifMv( pFile, pNtk );
75  // write the remaining networks
76  if ( pNtk->pDesign )
77  {
78  Vec_PtrForEachEntry( Abc_Ntk_t *, pNtk->pDesign->vModules, pNtkTemp, i )
79  {
80  if ( pNtkTemp == pNtk )
81  continue;
82  fprintf( pFile, "\n\n" );
83  Io_NtkWriteBlifMv( pFile, pNtkTemp );
84  }
85  }
86  fclose( pFile );
87 }
88 
89 /**Function*************************************************************
90 
91  Synopsis [Write the network into a BLIF file with the given name.]
92 
93  Description []
94 
95  SideEffects []
96 
97  SeeAlso []
98 
99 ***********************************************************************/
100 void Io_NtkWriteBlifMv( FILE * pFile, Abc_Ntk_t * pNtk )
101 {
102  assert( Abc_NtkIsNetlist(pNtk) );
103  // write the model name
104  fprintf( pFile, ".model %s\n", Abc_NtkName(pNtk) );
105  // write the network
106  Io_NtkWriteBlifMvOne( pFile, pNtk );
107  // write EXDC network if it exists
108  if ( Abc_NtkExdc(pNtk) )
109  printf( "Io_NtkWriteBlifMv(): EXDC is not written.\n" );
110  // finalize the file
111  fprintf( pFile, ".end\n\n\n" );
112 }
113 
114 /**Function*************************************************************
115 
116  Synopsis [Write one network.]
117 
118  Description []
119 
120  SideEffects []
121 
122  SeeAlso []
123 
124 ***********************************************************************/
125 void Io_NtkWriteBlifMvOne( FILE * pFile, Abc_Ntk_t * pNtk )
126 {
127  ProgressBar * pProgress;
128  Abc_Obj_t * pNode, * pTerm, * pLatch;
129  int i;
130 
131  // write the PIs
132  fprintf( pFile, ".inputs" );
133  Io_NtkWriteBlifMvPis( pFile, pNtk );
134  fprintf( pFile, "\n" );
135 
136  // write the POs
137  fprintf( pFile, ".outputs" );
138  Io_NtkWriteBlifMvPos( pFile, pNtk );
139  fprintf( pFile, "\n" );
140 
141  // write the MV directives
142  fprintf( pFile, "\n" );
143  Abc_NtkForEachCi( pNtk, pTerm, i )
144  if ( Abc_ObjMvVarNum(Abc_ObjFanout0(pTerm)) > 2 )
145  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(Abc_ObjFanout0(pTerm)), Abc_ObjMvVarNum(Abc_ObjFanout0(pTerm)) );
146  Abc_NtkForEachCo( pNtk, pTerm, i )
147  if ( Abc_ObjMvVarNum(Abc_ObjFanin0(pTerm)) > 2 )
148  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(Abc_ObjFanin0(pTerm)), Abc_ObjMvVarNum(Abc_ObjFanin0(pTerm)) );
149 
150  // write the blackbox
151  if ( Abc_NtkHasBlackbox( pNtk ) )
152  {
153  fprintf( pFile, ".blackbox\n" );
154  return;
155  }
156 
157  // write the timing info
158 // Io_WriteTimingInfo( pFile, pNtk );
159 
160  // write the latches
161  if ( !Abc_NtkIsComb(pNtk) )
162  {
163  fprintf( pFile, "\n" );
164  Abc_NtkForEachLatch( pNtk, pLatch, i )
165  Io_NtkWriteBlifMvLatch( pFile, pLatch );
166  fprintf( pFile, "\n" );
167  }
168 /*
169  // write the subcircuits
170  assert( Abc_NtkWhiteboxNum(pNtk) == 0 );
171  if ( Abc_NtkBlackboxNum(pNtk) > 0 )
172  {
173  fprintf( pFile, "\n" );
174  Abc_NtkForEachBlackbox( pNtk, pNode, i )
175  Io_NtkWriteBlifMvSubckt( pFile, pNode );
176  fprintf( pFile, "\n" );
177  }
178 */
179  if ( Abc_NtkBlackboxNum(pNtk) > 0 || Abc_NtkWhiteboxNum(pNtk) > 0 )
180  {
181  fprintf( pFile, "\n" );
182  Abc_NtkForEachBox( pNtk, pNode, i )
183  {
184  if ( Abc_ObjIsLatch(pNode) )
185  continue;
186  Io_NtkWriteBlifMvSubckt( pFile, pNode );
187  }
188  fprintf( pFile, "\n" );
189  }
190 
191  // write each internal node
192  pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
193  Abc_NtkForEachNode( pNtk, pNode, i )
194  {
195  Extra_ProgressBarUpdate( pProgress, i, NULL );
196  Io_NtkWriteBlifMvNode( pFile, pNode );
197  }
198  Extra_ProgressBarStop( pProgress );
199 }
200 
201 
202 /**Function*************************************************************
203 
204  Synopsis [Writes the primary input list.]
205 
206  Description []
207 
208  SideEffects []
209 
210  SeeAlso []
211 
212 ***********************************************************************/
213 void Io_NtkWriteBlifMvPis( FILE * pFile, Abc_Ntk_t * pNtk )
214 {
215  Abc_Obj_t * pTerm, * pNet;
216  int LineLength;
217  int AddedLength;
218  int NameCounter;
219  int i;
220 
221  LineLength = 7;
222  NameCounter = 0;
223 
224  Abc_NtkForEachPi( pNtk, pTerm, i )
225  {
226  pNet = Abc_ObjFanout0(pTerm);
227  // get the line length after this name is written
228  AddedLength = strlen(Abc_ObjName(pNet)) + 1;
229  if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
230  { // write the line extender
231  fprintf( pFile, " \\\n" );
232  // reset the line length
233  LineLength = 0;
234  NameCounter = 0;
235  }
236  fprintf( pFile, " %s", Abc_ObjName(pNet) );
237  LineLength += AddedLength;
238  NameCounter++;
239  }
240 }
241 
242 /**Function*************************************************************
243 
244  Synopsis [Writes the primary input list.]
245 
246  Description []
247 
248  SideEffects []
249 
250  SeeAlso []
251 
252 ***********************************************************************/
253 void Io_NtkWriteBlifMvPos( FILE * pFile, Abc_Ntk_t * pNtk )
254 {
255  Abc_Obj_t * pTerm, * pNet;
256  int LineLength;
257  int AddedLength;
258  int NameCounter;
259  int i;
260 
261  LineLength = 8;
262  NameCounter = 0;
263 
264  Abc_NtkForEachPo( pNtk, pTerm, i )
265  {
266  pNet = Abc_ObjFanin0(pTerm);
267  // get the line length after this name is written
268  AddedLength = strlen(Abc_ObjName(pNet)) + 1;
269  if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
270  { // write the line extender
271  fprintf( pFile, " \\\n" );
272  // reset the line length
273  LineLength = 0;
274  NameCounter = 0;
275  }
276  fprintf( pFile, " %s", Abc_ObjName(pNet) );
277  LineLength += AddedLength;
278  NameCounter++;
279  }
280 }
281 
282 /**Function*************************************************************
283 
284  Synopsis [Write the latch into a file.]
285 
286  Description []
287 
288  SideEffects []
289 
290  SeeAlso []
291 
292 ***********************************************************************/
293 void Io_NtkWriteBlifMvLatch( FILE * pFile, Abc_Obj_t * pLatch )
294 {
295  Abc_Obj_t * pNetLi, * pNetLo;
296  int Reset;
297  pNetLi = Abc_ObjFanin0( Abc_ObjFanin0(pLatch) );
298  pNetLo = Abc_ObjFanout0( Abc_ObjFanout0(pLatch) );
299  Reset = (int)(ABC_PTRUINT_T)Abc_ObjData( pLatch );
300  // write the latch line
301  fprintf( pFile, ".latch" );
302  fprintf( pFile, " %10s", Abc_ObjName(pNetLi) );
303  fprintf( pFile, " %10s", Abc_ObjName(pNetLo) );
304  fprintf( pFile, "\n" );
305  // write the reset node
306  fprintf( pFile, ".reset %s\n", Abc_ObjName(pNetLo) );
307  fprintf( pFile, "%d\n", Reset-1 );
308 }
309 
310 /**Function*************************************************************
311 
312  Synopsis [Write the latch into a file.]
313 
314  Description []
315 
316  SideEffects []
317 
318  SeeAlso []
319 
320 ***********************************************************************/
321 void Io_NtkWriteBlifMvSubckt( FILE * pFile, Abc_Obj_t * pNode )
322 {
323  Abc_Ntk_t * pModel = (Abc_Ntk_t *)pNode->pData;
324  Abc_Obj_t * pTerm;
325  int i;
326  // write the MV directives
327  fprintf( pFile, "\n" );
328  Abc_ObjForEachFanin( pNode, pTerm, i )
329  if ( Abc_ObjMvVarNum(pTerm) > 2 )
330  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(pTerm), Abc_ObjMvVarNum(pTerm) );
331  Abc_ObjForEachFanout( pNode, pTerm, i )
332  if ( Abc_ObjMvVarNum(pTerm) > 2 )
333  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(pTerm), Abc_ObjMvVarNum(pTerm) );
334  // write the subcircuit
335  fprintf( pFile, ".subckt %s %s", Abc_NtkName(pModel), Abc_ObjName(pNode) );
336  // write pairs of the formal=actual names
337  Abc_NtkForEachPi( pModel, pTerm, i )
338  {
339  fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanout0(pTerm)) );
340  pTerm = Abc_ObjFanin( pNode, i );
341  fprintf( pFile, "=%s", Abc_ObjName(Abc_ObjFanin0(pTerm)) );
342  }
343  Abc_NtkForEachPo( pModel, pTerm, i )
344  {
345  fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanin0(pTerm)) );
346  pTerm = Abc_ObjFanout( pNode, i );
347  fprintf( pFile, "=%s", Abc_ObjName(Abc_ObjFanout0(pTerm)) );
348  }
349  fprintf( pFile, "\n" );
350 }
351 
352 
353 /**Function*************************************************************
354 
355  Synopsis [Write the node into a file.]
356 
357  Description []
358 
359  SideEffects []
360 
361  SeeAlso []
362 
363 ***********************************************************************/
364 void Io_NtkWriteBlifMvNode( FILE * pFile, Abc_Obj_t * pNode )
365 {
366  Abc_Obj_t * pFanin;
367  char * pCur;
368  int nValues, iFanin, i;
369 
370  // write .mv directives for the fanins
371  fprintf( pFile, "\n" );
372  Abc_ObjForEachFanin( pNode, pFanin, i )
373  {
374 // nValues = atoi(pCur);
375  nValues = Abc_ObjMvVarNum( pFanin );
376  if ( nValues > 2 )
377  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(pFanin), nValues );
378 // while ( *pCur++ != ' ' );
379  }
380 
381  // write .mv directives for the node
382 // nValues = atoi(pCur);
383  nValues = Abc_ObjMvVarNum( Abc_ObjFanout0(pNode) );
384  if ( nValues > 2 )
385  fprintf( pFile, ".mv %s %d\n", Abc_ObjName(Abc_ObjFanout0(pNode)), nValues );
386 // while ( *pCur++ != '\n' );
387 
388  // write the .names line
389  fprintf( pFile, ".table" );
390  Io_NtkWriteBlifMvNodeFanins( pFile, pNode );
391  fprintf( pFile, "\n" );
392 
393  // write the cubes
394  pCur = (char *)Abc_ObjData(pNode);
395  if ( *pCur == 'd' )
396  {
397  fprintf( pFile, ".default " );
398  pCur++;
399  }
400  // write the literals
401  for ( ; *pCur; pCur++ )
402  {
403  fprintf( pFile, "%c", *pCur );
404  if ( *pCur != '=' )
405  continue;
406  // get the number
407  iFanin = atoi( pCur+1 );
408  fprintf( pFile, "%s", Abc_ObjName(Abc_ObjFanin(pNode,iFanin)) );
409  // scroll on to the next symbol
410  while ( *pCur != ' ' && *pCur != '\n' )
411  pCur++;
412  pCur--;
413  }
414 }
415 
416 /**Function*************************************************************
417 
418  Synopsis [Writes the primary input list.]
419 
420  Description []
421 
422  SideEffects []
423 
424  SeeAlso []
425 
426 ***********************************************************************/
427 void Io_NtkWriteBlifMvNodeFanins( FILE * pFile, Abc_Obj_t * pNode )
428 {
429  Abc_Obj_t * pNet;
430  int LineLength;
431  int AddedLength;
432  int NameCounter;
433  char * pName;
434  int i;
435 
436  LineLength = 6;
437  NameCounter = 0;
438  Abc_ObjForEachFanin( pNode, pNet, i )
439  {
440  // get the fanin name
441  pName = Abc_ObjName(pNet);
442  // get the line length after the fanin name is written
443  AddedLength = strlen(pName) + 1;
444  if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
445  { // write the line extender
446  fprintf( pFile, " \\\n" );
447  // reset the line length
448  LineLength = 0;
449  NameCounter = 0;
450  }
451  fprintf( pFile, " %s", pName );
452  LineLength += AddedLength;
453  NameCounter++;
454  }
455 
456  // get the output name
457  pName = Abc_ObjName(Abc_ObjFanout0(pNode));
458  // get the line length after the output name is written
459  AddedLength = strlen(pName) + 1;
460  if ( NameCounter && LineLength + AddedLength > 75 )
461  { // write the line extender
462  fprintf( pFile, " \\\n" );
463  // reset the line length
464  LineLength = 0;
465  NameCounter = 0;
466  }
467  fprintf( pFile, " %s", pName );
468 }
469 
470 ////////////////////////////////////////////////////////////////////////
471 /// END OF FILE ///
472 ////////////////////////////////////////////////////////////////////////
473 
474 
476 
static void Io_NtkWriteBlifMvLatch(FILE *pFile, Abc_Obj_t *pLatch)
static int Abc_NtkIsComb(Abc_Ntk_t *pNtk)
Definition: abc.h:297
static void Io_NtkWriteBlifMvNode(FILE *pFile, Abc_Obj_t *pNode)
static int Abc_NtkObjNumMax(Abc_Ntk_t *pNtk)
Definition: abc.h:284
static Abc_Ntk_t * Abc_NtkExdc(Abc_Ntk_t *pNtk)
Definition: abc.h:272
static int Abc_NtkIsNetlist(Abc_Ntk_t *pNtk)
Definition: abc.h:249
static int Abc_ObjIsLatch(Abc_Obj_t *pObj)
Definition: abc.h:356
static void Io_NtkWriteBlifMvOne(FILE *pFile, Abc_Ntk_t *pNtk)
static void Io_NtkWriteBlifMvSubckt(FILE *pFile, Abc_Obj_t *pNode)
static ABC_NAMESPACE_IMPL_START void Io_NtkWriteBlifMv(FILE *pFile, Abc_Ntk_t *pNtk)
DECLARATIONS ///.
#define Abc_NtkForEachCo(pNtk, pCo, i)
Definition: abc.h:519
static int Abc_NtkHasBlifMv(Abc_Ntk_t *pNtk)
Definition: abc.h:257
static int Abc_NtkWhiteboxNum(Abc_Ntk_t *pNtk)
Definition: abc.h:295
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
DECLARATIONS ///.
static int Abc_NtkHasBlackbox(Abc_Ntk_t *pNtk)
Definition: abc.h:258
#define IO_WRITE_LINE_LENGTH
MACRO DEFINITIONS ///.
Definition: ioAbc.h:71
static void Io_NtkWriteBlifMvNodeFanins(FILE *pFile, Abc_Obj_t *pNode)
static void Io_NtkWriteBlifMvAsserts(FILE *pFile, Abc_Ntk_t *pNtk)
void Io_WriteBlifMv(Abc_Ntk_t *pNtk, char *FileName)
FUNCTION DEFINITIONS ///.
Definition: ioWriteBlifMv.c:58
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define Abc_NtkForEachLatch(pNtk, pObj, i)
Definition: abc.h:497
void Extra_ProgressBarStop(ProgressBar *p)
#define Abc_NtkForEachBox(pNtk, pObj, i)
Definition: abc.h:495
#define Abc_NtkForEachNode(pNtk, pNode, i)
Definition: abc.h:461
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define Abc_ObjForEachFanout(pObj, pFanout, i)
Definition: abc.h:526
static char * Abc_NtkName(Abc_Ntk_t *pNtk)
Definition: abc.h:270
#define Abc_NtkForEachCi(pNtk, pCi, i)
Definition: abc.h:515
static void Io_NtkWriteBlifMvValues(FILE *pFile, Abc_Obj_t *pNode)
#define Abc_ObjForEachFanin(pObj, pFanin, i)
Definition: abc.h:524
ABC_DLL char * Abc_ObjName(Abc_Obj_t *pNode)
DECLARATIONS ///.
Definition: abcNames.c:48
char * Extra_TimeStamp()
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
static int Abc_ObjMvVarNum(Abc_Obj_t *pObj)
Definition: abc.h:438
Vec_Ptr_t * vModules
Definition: abc.h:223
#define assert(ex)
Definition: util_old.h:213
static void Extra_ProgressBarUpdate(ProgressBar *p, int nItemsCur, char *pString)
Definition: extra.h:243
static void * Abc_ObjData(Abc_Obj_t *pObj)
Definition: abc.h:336
int strlen()
void * pData
Definition: abc.h:145
#define Abc_NtkForEachPo(pNtk, pPo, i)
Definition: abc.h:517
static int Abc_NtkBlackboxNum(Abc_Ntk_t *pNtk)
Definition: abc.h:296
static Abc_Obj_t * Abc_ObjFanout(Abc_Obj_t *pObj, int i)
Definition: abc.h:370
static Abc_Obj_t * Abc_ObjFanin(Abc_Obj_t *pObj, int i)
Definition: abc.h:372
static void Io_NtkWriteBlifMvPis(FILE *pFile, Abc_Ntk_t *pNtk)
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
Abc_Des_t * pDesign
Definition: abc.h:180
char * pName
Definition: abc.h:158
static void Io_NtkWriteBlifMvPos(FILE *pFile, Abc_Ntk_t *pNtk)
static Abc_Obj_t * Abc_ObjFanout0(Abc_Obj_t *pObj)
Definition: abc.h:371
#define Abc_NtkForEachPi(pNtk, pPi, i)
Definition: abc.h:513