00001
00022 #include "ioa.h"
00023
00027
00028 unsigned Ioa_ReadAigerDecode( char ** ppPos );
00029
00033
00045 Aig_Man_t * Ioa_ReadAiger( char * pFileName, int fCheck )
00046 {
00047 Bar_Progress_t * pProgress;
00048 FILE * pFile;
00049 Vec_Ptr_t * vNodes, * vDrivers;
00050 Aig_Obj_t * pObj, * pNode0, * pNode1;
00051 Aig_Man_t * pManNew;
00052 int nTotal, nInputs, nOutputs, nLatches, nAnds, nFileSize, i;
00053 char * pContents, * pDrivers, * pSymbols, * pCur, * pName;
00054 unsigned uLit0, uLit1, uLit;
00055
00056
00057 nFileSize = Ioa_FileSize( pFileName );
00058 pFile = fopen( pFileName, "rb" );
00059 pContents = ALLOC( char, nFileSize );
00060 fread( pContents, nFileSize, 1, pFile );
00061 fclose( pFile );
00062
00063
00064 if ( strncmp(pContents, "aig", 3) != 0 )
00065 {
00066 fprintf( stdout, "Wrong input file format.\n" );
00067 return NULL;
00068 }
00069
00070
00071 pCur = pContents; while ( *pCur++ != ' ' );
00072
00073 nTotal = atoi( pCur ); while ( *pCur++ != ' ' );
00074
00075 nInputs = atoi( pCur ); while ( *pCur++ != ' ' );
00076
00077 nLatches = atoi( pCur ); while ( *pCur++ != ' ' );
00078
00079 nOutputs = atoi( pCur ); while ( *pCur++ != ' ' );
00080
00081 nAnds = atoi( pCur ); while ( *pCur++ != '\n' );
00082
00083 if ( nTotal != nInputs + nLatches + nAnds )
00084 {
00085 fprintf( stdout, "The paramters are wrong.\n" );
00086 return NULL;
00087 }
00088
00089
00090 pManNew = Aig_ManStart( nAnds );
00091 pName = Ioa_FileNameGeneric( pFileName );
00092 pManNew->pName = Aig_UtilStrsav( pName );
00093
00094 free( pName );
00095
00096
00097 vNodes = Vec_PtrAlloc( 1 + nInputs + nLatches + nAnds );
00098 Vec_PtrPush( vNodes, Aig_ManConst0(pManNew) );
00099
00100
00101 for ( i = 0; i < nInputs + nLatches; i++ )
00102 {
00103 pObj = Aig_ObjCreatePi(pManNew);
00104 Vec_PtrPush( vNodes, pObj );
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114 pManNew->nRegs = nLatches;
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 pDrivers = pCur;
00134
00135
00136 for ( i = 0; i < nLatches + nOutputs; )
00137 if ( *pCur++ == '\n' )
00138 i++;
00139
00140
00141 pProgress = Bar_ProgressStart( stdout, nAnds );
00142 for ( i = 0; i < nAnds; i++ )
00143 {
00144 Bar_ProgressUpdate( pProgress, i, NULL );
00145 uLit = ((i + 1 + nInputs + nLatches) << 1);
00146 uLit1 = uLit - Ioa_ReadAigerDecode( &pCur );
00147 uLit0 = uLit1 - Ioa_ReadAigerDecode( &pCur );
00148
00149 pNode0 = Aig_NotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), uLit0 & 1 );
00150 pNode1 = Aig_NotCond( Vec_PtrEntry(vNodes, uLit1 >> 1), uLit1 & 1 );
00151 assert( Vec_PtrSize(vNodes) == i + 1 + nInputs + nLatches );
00152 Vec_PtrPush( vNodes, Aig_And(pManNew, pNode0, pNode1) );
00153 }
00154 Bar_ProgressStop( pProgress );
00155
00156
00157 pSymbols = pCur;
00158
00159
00160 vDrivers = Vec_PtrAlloc( nLatches + nOutputs );
00161 pCur = pDrivers;
00162
00163 for ( i = 0; i < nLatches; i++ )
00164 {
00165 uLit0 = atoi( pCur ); while ( *pCur++ != '\n' );
00166 pNode0 = Aig_NotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );
00167
00168 Vec_PtrPush( vDrivers, pNode0 );
00169 }
00170
00171
00172 for ( i = 0; i < nOutputs; i++ )
00173 {
00174 uLit0 = atoi( pCur ); while ( *pCur++ != '\n' );
00175 pNode0 = Aig_NotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );
00176
00177 Vec_PtrPush( vDrivers, pNode0 );
00178 }
00179
00180
00181
00182 for ( i = 0; i < nOutputs; i++ )
00183 Aig_ObjCreatePo( pManNew, Vec_PtrEntry(vDrivers, nLatches + i) );
00184 for ( i = 0; i < nLatches; i++ )
00185 Aig_ObjCreatePo( pManNew, Vec_PtrEntry(vDrivers, i) );
00186 Vec_PtrFree( vDrivers );
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 free( pContents );
00267 Vec_PtrFree( vNodes );
00268
00269
00270 Aig_ManCleanup( pManNew );
00271
00272
00273 if ( fCheck && !Aig_ManCheck( pManNew ) )
00274 {
00275 printf( "Ioa_ReadAiger: The network check has failed.\n" );
00276 Aig_ManStop( pManNew );
00277 return NULL;
00278 }
00279 return pManNew;
00280 }
00281
00282
00295 unsigned Ioa_ReadAigerDecode( char ** ppPos )
00296 {
00297 unsigned x = 0, i = 0;
00298 unsigned char ch;
00299
00300
00301 while ((ch = *(*ppPos)++) & 0x80)
00302 x |= (ch & 0x7f) << (7 * i++);
00303
00304 return x | (ch << (7 * i));
00305 }
00306
00307
00311
00312