abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioWriteBaf.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioWriteBaf.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures to write AIG in the binary format.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: ioWriteBaf.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 /*
31  Binary Aig Format
32 
33  The motivation for this format is to have
34  - compact binary representation of large AIGs (~10x more compact than BLIF)
35  - consequently, fast reading/writing of large AIGs (~10x faster than BLIF)
36  - representation for all tech-ind info related to an AIG
37  - human-readable file header
38 
39  The header:
40  (1) May contain several lines of human-readable comments.
41  Each comment line begins with symbol '#' and ends with symbol '\n'.
42  (2) Always contains the following data.
43  - benchmark name
44  - number of primary inputs
45  - number of primary outputs
46  - number of latches
47  - number of AIG nodes (excluding the constant 1 node)
48  Each entry is followed by 0-byte (character '\0'):
49  (3) Next follow the names of the PIs, POs, and latches in this order.
50  Each name is followed by 0-byte (character '\0').
51  Inside each set of names (PIs, POs, latches) there should be no
52  identical names but the PO names may coincide with PI/latch names.
53 
54  The body:
55  (1) First part of the body contains binary information about the internal AIG nodes.
56  Each internal AIG node is represented using two edges (each edge is a 4-byte integer).
57  Each integer is the fanin ID followed by 1-bit representation of the complemented attribute.
58  (For example, complemented edge to node 10 will be represented as 2*10 + 1 = 21.)
59  The IDs of the nodes are created as follows: Constant 1 node has ID=0.
60  CIs (PIs and latch outputs) have 1-based IDs assigned in that order.
61  Each node in the array of the internal AIG nodes has the ID assigned in that order.
62  The constant 1 node is not written into the file.
63  (2) Second part of the body contains binary information about the edges connecting
64  the COs (POs and latch inputs) to the internal AIG nodes.
65  Each edge is a 4-byte integer the same way as a node fanin.
66  The latch initial value (2 bits) is stored in this integer.
67 */
68 
69 ////////////////////////////////////////////////////////////////////////
70 /// FUNCTION DEFINITIONS ///
71 ////////////////////////////////////////////////////////////////////////
72 
73 /**Function*************************************************************
74 
75  Synopsis [Writes the AIG in the binary format.]
76 
77  Description []
78 
79  SideEffects []
80 
81  SeeAlso []
82 
83 ***********************************************************************/
84 void Io_WriteBaf( Abc_Ntk_t * pNtk, char * pFileName )
85 {
86  ProgressBar * pProgress;
87  FILE * pFile;
88  Abc_Obj_t * pObj;
89  int i, nNodes, nAnds, nBufferSize;
90  unsigned * pBufferNode;
91  assert( Abc_NtkIsStrash(pNtk) );
92  // start the output stream
93  pFile = fopen( pFileName, "wb" );
94  if ( pFile == NULL )
95  {
96  fprintf( stdout, "Io_WriteBaf(): Cannot open the output file \"%s\".\n", pFileName );
97  return;
98  }
99 
100  // write the comment
101  fprintf( pFile, "# BAF (Binary Aig Format) for \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
102 
103  // write the network name
104  fprintf( pFile, "%s%c", pNtk->pName, 0 );
105  // write the number of PIs
106  fprintf( pFile, "%d%c", Abc_NtkPiNum(pNtk), 0 );
107  // write the number of POs
108  fprintf( pFile, "%d%c", Abc_NtkPoNum(pNtk), 0 );
109  // write the number of latches
110  fprintf( pFile, "%d%c", Abc_NtkLatchNum(pNtk), 0 );
111  // write the number of internal nodes
112  fprintf( pFile, "%d%c", Abc_NtkNodeNum(pNtk), 0 );
113 
114  // write PIs
115  Abc_NtkForEachPi( pNtk, pObj, i )
116  fprintf( pFile, "%s%c", Abc_ObjName(pObj), 0 );
117  // write POs
118  Abc_NtkForEachPo( pNtk, pObj, i )
119  fprintf( pFile, "%s%c", Abc_ObjName(pObj), 0 );
120  // write latches
121  Abc_NtkForEachLatch( pNtk, pObj, i )
122  {
123  fprintf( pFile, "%s%c", Abc_ObjName(pObj), 0 );
124  fprintf( pFile, "%s%c", Abc_ObjName(Abc_ObjFanin0(pObj)), 0 );
125  fprintf( pFile, "%s%c", Abc_ObjName(Abc_ObjFanout0(pObj)), 0 );
126  }
127 
128  // set the node numbers to be used in the output file
129  Abc_NtkCleanCopy( pNtk );
130  nNodes = 1;
131  Abc_NtkForEachCi( pNtk, pObj, i )
132  pObj->pCopy = (Abc_Obj_t *)(ABC_PTRINT_T)nNodes++;
133  Abc_AigForEachAnd( pNtk, pObj, i )
134  pObj->pCopy = (Abc_Obj_t *)(ABC_PTRINT_T)nNodes++;
135 
136  // write the nodes into the buffer
137  nAnds = 0;
138  nBufferSize = Abc_NtkNodeNum(pNtk) * 2 + Abc_NtkCoNum(pNtk);
139  pBufferNode = ABC_ALLOC( unsigned, nBufferSize );
140  pProgress = Extra_ProgressBarStart( stdout, nBufferSize );
141  Abc_AigForEachAnd( pNtk, pObj, i )
142  {
143  Extra_ProgressBarUpdate( pProgress, nAnds, NULL );
144  pBufferNode[nAnds++] = (((int)(ABC_PTRINT_T)Abc_ObjFanin0(pObj)->pCopy) << 1) | (int)Abc_ObjFaninC0(pObj);
145  pBufferNode[nAnds++] = (((int)(ABC_PTRINT_T)Abc_ObjFanin1(pObj)->pCopy) << 1) | (int)Abc_ObjFaninC1(pObj);
146  }
147 
148  // write the COs into the buffer
149  Abc_NtkForEachCo( pNtk, pObj, i )
150  {
151  Extra_ProgressBarUpdate( pProgress, nAnds, NULL );
152  pBufferNode[nAnds] = (((int)(ABC_PTRINT_T)Abc_ObjFanin0(pObj)->pCopy) << 1) | (int)Abc_ObjFaninC0(pObj);
153  if ( Abc_ObjFanoutNum(pObj) > 0 && Abc_ObjIsLatch(Abc_ObjFanout0(pObj)) )
154  pBufferNode[nAnds] = (pBufferNode[nAnds] << 2) | ((int)(ABC_PTRINT_T)Abc_ObjData(Abc_ObjFanout0(pObj)) & 3);
155  nAnds++;
156  }
157  Extra_ProgressBarStop( pProgress );
158  assert( nBufferSize == nAnds );
159 
160  // write the buffer
161  fwrite( pBufferNode, 1, sizeof(int) * nBufferSize, pFile );
162  fclose( pFile );
163  ABC_FREE( pBufferNode );
164 }
165 
166 
167 ////////////////////////////////////////////////////////////////////////
168 /// END OF FILE ///
169 ////////////////////////////////////////////////////////////////////////
170 
171 
173 
static int Abc_NtkIsStrash(Abc_Ntk_t *pNtk)
Definition: abc.h:251
ABC_NAMESPACE_IMPL_START void Io_WriteBaf(Abc_Ntk_t *pNtk, char *pFileName)
DECLARATIONS ///.
Definition: ioWriteBaf.c:84
static Abc_Obj_t * Abc_ObjFanin1(Abc_Obj_t *pObj)
Definition: abc.h:374
static int Abc_ObjFaninC1(Abc_Obj_t *pObj)
Definition: abc.h:378
static int Abc_ObjIsLatch(Abc_Obj_t *pObj)
Definition: abc.h:356
static int Abc_ObjFanoutNum(Abc_Obj_t *pObj)
Definition: abc.h:365
static int Abc_NtkLatchNum(Abc_Ntk_t *pNtk)
Definition: abc.h:294
static int Abc_ObjFaninC0(Abc_Obj_t *pObj)
Definition: abc.h:377
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define Abc_NtkForEachCo(pNtk, pCo, i)
Definition: abc.h:519
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
static int Abc_NtkCoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:288
#define Abc_AigForEachAnd(pNtk, pNode, i)
Definition: abc.h:485
static int Abc_NtkNodeNum(Abc_Ntk_t *pNtk)
Definition: abc.h:293
DECLARATIONS ///.
Abc_Obj_t * pCopy
Definition: abc.h:148
#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_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define Abc_NtkForEachCi(pNtk, pCi, i)
Definition: abc.h:515
static int Abc_NtkPoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:286
#define ABC_FREE(obj)
Definition: abc_global.h:232
static int Abc_NtkPiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:285
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 ///.
ABC_DLL void Abc_NtkCleanCopy(Abc_Ntk_t *pNtk)
Definition: abcUtil.c:507
#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
#define Abc_NtkForEachPo(pNtk, pPo, i)
Definition: abc.h:517
char * pName
Definition: abc.h:158
static Abc_Obj_t * Abc_ObjFanout0(Abc_Obj_t *pObj)
Definition: abc.h:371
#define Abc_NtkForEachPi(pNtk, pPi, i)
Definition: abc.h:513