abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioReadBaf.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioReadBaf.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures to read 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: ioReadBaf.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 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Reads the AIG in the binary format.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
45 Abc_Ntk_t * Io_ReadBaf( char * pFileName, int fCheck )
46 {
47  ProgressBar * pProgress;
48  FILE * pFile;
49  Vec_Ptr_t * vNodes;
50  Abc_Obj_t * pObj, * pNode0, * pNode1;
51  Abc_Ntk_t * pNtkNew;
52  int nInputs, nOutputs, nLatches, nAnds, nFileSize, Num, i;
53  char * pContents, * pName, * pCur;
54  unsigned * pBufferNode;
55  int RetValue;
56 
57  // read the file into the buffer
58  nFileSize = Extra_FileSize( pFileName );
59  pFile = fopen( pFileName, "rb" );
60  pContents = ABC_ALLOC( char, nFileSize );
61  RetValue = fread( pContents, nFileSize, 1, pFile );
62  fclose( pFile );
63 
64  // skip the comments (comment lines begin with '#' and end with '\n')
65  for ( pCur = pContents; *pCur == '#'; )
66  while ( *pCur++ != '\n' );
67 
68  // read the name
69  pName = pCur; while ( *pCur++ );
70  // read the number of inputs
71  nInputs = atoi( pCur ); while ( *pCur++ );
72  // read the number of outputs
73  nOutputs = atoi( pCur ); while ( *pCur++ );
74  // read the number of latches
75  nLatches = atoi( pCur ); while ( *pCur++ );
76  // read the number of nodes
77  nAnds = atoi( pCur ); while ( *pCur++ );
78 
79  // allocate the empty AIG
80  pNtkNew = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
81  pNtkNew->pName = Extra_UtilStrsav( pName );
82  pNtkNew->pSpec = Extra_UtilStrsav( pFileName );
83 
84  // prepare the array of nodes
85  vNodes = Vec_PtrAlloc( 1 + nInputs + nLatches + nAnds );
86  Vec_PtrPush( vNodes, Abc_AigConst1(pNtkNew) );
87 
88  // create the PIs
89  for ( i = 0; i < nInputs; i++ )
90  {
91  pObj = Abc_NtkCreatePi(pNtkNew);
92  Abc_ObjAssignName( pObj, pCur, NULL ); while ( *pCur++ );
93  Vec_PtrPush( vNodes, pObj );
94  }
95  // create the POs
96  for ( i = 0; i < nOutputs; i++ )
97  {
98  pObj = Abc_NtkCreatePo(pNtkNew);
99  Abc_ObjAssignName( pObj, pCur, NULL ); while ( *pCur++ );
100  }
101  // create the latches
102  for ( i = 0; i < nLatches; i++ )
103  {
104  pObj = Abc_NtkCreateLatch(pNtkNew);
105  Abc_ObjAssignName( pObj, pCur, NULL ); while ( *pCur++ );
106 
107  pNode0 = Abc_NtkCreateBi(pNtkNew);
108  Abc_ObjAssignName( pNode0, pCur, NULL ); while ( *pCur++ );
109 
110  pNode1 = Abc_NtkCreateBo(pNtkNew);
111  Abc_ObjAssignName( pNode1, pCur, NULL ); while ( *pCur++ );
112  Vec_PtrPush( vNodes, pNode1 );
113 
114  Abc_ObjAddFanin( pObj, pNode0 );
115  Abc_ObjAddFanin( pNode1, pObj );
116  }
117 
118  // get the pointer to the beginning of the node array
119  pBufferNode = (unsigned *)(pContents + (nFileSize - (2 * nAnds + nOutputs + nLatches) * sizeof(int)) );
120  // make sure we are at the place where the nodes begin
121  if ( pBufferNode != (unsigned *)pCur )
122  {
123  ABC_FREE( pContents );
124  Vec_PtrFree( vNodes );
125  Abc_NtkDelete( pNtkNew );
126  printf( "Warning: Internal reader error.\n" );
127  return NULL;
128  }
129 
130  // create the AND gates
131  pProgress = Extra_ProgressBarStart( stdout, nAnds );
132  for ( i = 0; i < nAnds; i++ )
133  {
134  Extra_ProgressBarUpdate( pProgress, i, NULL );
135  pNode0 = Abc_ObjNotCond( (Abc_Obj_t *)Vec_PtrEntry(vNodes, pBufferNode[2*i+0] >> 1), pBufferNode[2*i+0] & 1 );
136  pNode1 = Abc_ObjNotCond( (Abc_Obj_t *)Vec_PtrEntry(vNodes, pBufferNode[2*i+1] >> 1), pBufferNode[2*i+1] & 1 );
137  Vec_PtrPush( vNodes, Abc_AigAnd((Abc_Aig_t *)pNtkNew->pManFunc, pNode0, pNode1) );
138  }
139  Extra_ProgressBarStop( pProgress );
140 
141  // read the POs
142  Abc_NtkForEachCo( pNtkNew, pObj, i )
143  {
144  Num = pBufferNode[2*nAnds+i];
145  if ( Abc_ObjFanoutNum(pObj) > 0 && Abc_ObjIsLatch(Abc_ObjFanout0(pObj)) )
146  {
147  Abc_ObjSetData( Abc_ObjFanout0(pObj), (void *)(ABC_PTRINT_T)(Num & 3) );
148  Num >>= 2;
149  }
150  pNode0 = Abc_ObjNotCond( (Abc_Obj_t *)Vec_PtrEntry(vNodes, Num >> 1), Num & 1 );
151  Abc_ObjAddFanin( pObj, pNode0 );
152  }
153  ABC_FREE( pContents );
154  Vec_PtrFree( vNodes );
155 
156  // remove the extra nodes
157 // Abc_AigCleanup( (Abc_Aig_t *)pNtkNew->pManFunc );
158 
159  // check the result
160  if ( fCheck && !Abc_NtkCheckRead( pNtkNew ) )
161  {
162  printf( "Io_ReadBaf: The network check has failed.\n" );
163  Abc_NtkDelete( pNtkNew );
164  return NULL;
165  }
166  return pNtkNew;
167 
168 }
169 
170 
171 ////////////////////////////////////////////////////////////////////////
172 /// END OF FILE ///
173 ////////////////////////////////////////////////////////////////////////
174 
175 
177 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
ABC_DLL Abc_Obj_t * Abc_AigConst1(Abc_Ntk_t *pNtk)
Definition: abcAig.c:683
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 void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define Abc_NtkForEachCo(pNtk, pCo, i)
Definition: abc.h:519
ABC_DLL char * Abc_ObjAssignName(Abc_Obj_t *pObj, char *pName, char *pSuffix)
Definition: abcNames.c:68
DECLARATIONS ///.
Definition: abcAig.c:52
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
char * Extra_UtilStrsav(const char *s)
ABC_DLL void Abc_ObjAddFanin(Abc_Obj_t *pObj, Abc_Obj_t *pFanin)
Definition: abcFanio.c:84
ABC_DLL Abc_Ntk_t * Abc_NtkAlloc(Abc_NtkType_t Type, Abc_NtkFunc_t Func, int fUseMemMan)
DECLARATIONS ///.
Definition: abcNtk.c:50
static Abc_Obj_t * Abc_NtkCreateBo(Abc_Ntk_t *pNtk)
Definition: abc.h:306
void * pManFunc
Definition: abc.h:191
ABC_DLL Abc_Obj_t * Abc_AigAnd(Abc_Aig_t *pMan, Abc_Obj_t *p0, Abc_Obj_t *p1)
Definition: abcAig.c:700
DECLARATIONS ///.
int Extra_FileSize(char *pFileName)
ABC_DLL int Abc_NtkCheckRead(Abc_Ntk_t *pNtk)
Definition: abcCheck.c:77
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static Abc_Obj_t * Abc_NtkCreateLatch(Abc_Ntk_t *pNtk)
Definition: abc.h:309
static Abc_Obj_t * Abc_NtkCreatePi(Abc_Ntk_t *pNtk)
Definition: abc.h:303
void Extra_ProgressBarStop(ProgressBar *p)
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
char * pSpec
Definition: abc.h:159
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define ABC_FREE(obj)
Definition: abc_global.h:232
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
static Abc_Obj_t * Abc_ObjNotCond(Abc_Obj_t *p, int c)
Definition: abc.h:325
static void Extra_ProgressBarUpdate(ProgressBar *p, int nItemsCur, char *pString)
Definition: extra.h:243
static Abc_Obj_t * Abc_NtkCreateBi(Abc_Ntk_t *pNtk)
Definition: abc.h:305
ABC_NAMESPACE_IMPL_START Abc_Ntk_t * Io_ReadBaf(char *pFileName, int fCheck)
DECLARATIONS ///.
Definition: ioReadBaf.c:45
static Abc_Obj_t * Abc_NtkCreatePo(Abc_Ntk_t *pNtk)
Definition: abc.h:304
char * pName
Definition: abc.h:158
static Abc_Obj_t * Abc_ObjFanout0(Abc_Obj_t *pObj)
Definition: abc.h:371
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223