00001
00021 #include "io.h"
00022
00026
00027 static Abc_Ntk_t * Io_ReadBenchNetwork( Extra_FileReader_t * p );
00028
00032
00044 Abc_Ntk_t * Io_ReadBench( char * pFileName, int fCheck )
00045 {
00046 Extra_FileReader_t * p;
00047 Abc_Ntk_t * pNtk;
00048
00049
00050 p = Extra_FileReaderAlloc( pFileName, "#", "\n\r", " \t,()=" );
00051 if ( p == NULL )
00052 return NULL;
00053
00054
00055 pNtk = Io_ReadBenchNetwork( p );
00056 Extra_FileReaderFree( p );
00057 if ( pNtk == NULL )
00058 return NULL;
00059
00060
00061 if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
00062 {
00063 printf( "Io_ReadBench: The network check has failed.\n" );
00064 Abc_NtkDelete( pNtk );
00065 return NULL;
00066 }
00067 return pNtk;
00068 }
00069
00081 Abc_Ntk_t * Io_ReadBenchNetwork( Extra_FileReader_t * p )
00082 {
00083 ProgressBar * pProgress;
00084 Vec_Ptr_t * vTokens;
00085 Abc_Ntk_t * pNtk;
00086 Abc_Obj_t * pNode, * pNet;
00087 Vec_Str_t * vString;
00088 unsigned uTruth[8];
00089 char * pType, ** ppNames, * pString;
00090 int iLine, nNames, nDigits, fLutsPresent = 0;
00091
00092
00093 pNtk = Abc_NtkStartRead( Extra_FileReaderGetFileName(p) );
00094
00095
00096 vString = Vec_StrAlloc( 100 );
00097 pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
00098 for ( iLine = 0; vTokens = Extra_FileReaderGetTokens(p); iLine++ )
00099 {
00100 Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p), NULL );
00101
00102 if ( vTokens->nSize == 1 )
00103 {
00104 printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
00105 Vec_StrFree( vString );
00106 Abc_NtkDelete( pNtk );
00107 return NULL;
00108 }
00109
00110
00111 if ( strncmp( vTokens->pArray[0], "INPUT", 5 ) == 0 )
00112 Io_ReadCreatePi( pNtk, vTokens->pArray[1] );
00113 else if ( strncmp( vTokens->pArray[0], "OUTPUT", 5 ) == 0 )
00114 Io_ReadCreatePo( pNtk, vTokens->pArray[1] );
00115 else
00116 {
00117
00118 pType = vTokens->pArray[1];
00119 if ( strncmp(pType, "DFF", 3) == 0 )
00120 {
00121 pNode = Io_ReadCreateLatch( pNtk, vTokens->pArray[2], vTokens->pArray[0] );
00122
00123 if ( pType[3] == '0' )
00124 Abc_LatchSetInit0( pNode );
00125 else if ( pType[3] == '1' )
00126 Abc_LatchSetInit1( pNode );
00127 else
00128 Abc_LatchSetInitDc( pNode );
00129 }
00130 else if ( strcmp(pType, "LUT") == 0 )
00131 {
00132 fLutsPresent = 1;
00133 ppNames = (char **)vTokens->pArray + 3;
00134 nNames = vTokens->nSize - 3;
00135
00136 if ( nNames > 8 )
00137 {
00138 printf( "%s: Currently cannot read truth tables with more than 8 inputs (%d).\n", Extra_FileReaderGetFileName(p), nNames );
00139 Vec_StrFree( vString );
00140 Abc_NtkDelete( pNtk );
00141 return NULL;
00142 }
00143
00144 pString = vTokens->pArray[2];
00145 if ( strncmp( pString, "0x", 2 ) )
00146 {
00147 printf( "%s: The LUT signature (%s) does not look like a hexadecimal beginning with \"0x\".\n", Extra_FileReaderGetFileName(p), pString );
00148 Vec_StrFree( vString );
00149 Abc_NtkDelete( pNtk );
00150 return NULL;
00151 }
00152 pString += 2;
00153
00154 nDigits = (1 << nNames) / 4;
00155 if ( nDigits == 0 )
00156 nDigits = 1;
00157 if ( strlen(pString) < (unsigned)nDigits )
00158 {
00159 Vec_StrFill( vString, nDigits - strlen(pString), '0' );
00160 Vec_StrPrintStr( vString, pString );
00161 Vec_StrPush( vString, 0 );
00162 pString = Vec_StrArray( vString );
00163 }
00164
00165 if ( !Extra_ReadHexadecimal( uTruth, pString, nNames ) )
00166 {
00167 printf( "%s: Reading hexadecimal number (%s) has failed.\n", Extra_FileReaderGetFileName(p), pString );
00168 Vec_StrFree( vString );
00169 Abc_NtkDelete( pNtk );
00170 return NULL;
00171 }
00172
00173 if ( Extra_TruthIsConst0(uTruth, nNames) )
00174 {
00175 pNode = Io_ReadCreateNode( pNtk, vTokens->pArray[0], ppNames, 0 );
00176 Abc_ObjSetData( pNode, Abc_SopRegister( pNtk->pManFunc, " 0\n" ) );
00177 }
00178 else if ( Extra_TruthIsConst1(uTruth, nNames) )
00179 {
00180 pNode = Io_ReadCreateNode( pNtk, vTokens->pArray[0], ppNames, 0 );
00181 Abc_ObjSetData( pNode, Abc_SopRegister( pNtk->pManFunc, " 1\n" ) );
00182 }
00183 else
00184 {
00185
00186 pNode = Io_ReadCreateNode( pNtk, vTokens->pArray[0], ppNames, nNames );
00187 assert( nNames > 0 );
00188 if ( nNames > 1 )
00189 Abc_ObjSetData( pNode, Abc_SopCreateFromTruth(pNtk->pManFunc, nNames, uTruth) );
00190 else if ( pString[0] == '2' )
00191 Abc_ObjSetData( pNode, Abc_SopCreateBuf(pNtk->pManFunc) );
00192 else if ( pString[0] == '1' )
00193 Abc_ObjSetData( pNode, Abc_SopCreateInv(pNtk->pManFunc) );
00194 else
00195 {
00196 printf( "%s: Reading truth table (%s) of single-input node has failed.\n", Extra_FileReaderGetFileName(p), pString );
00197 Vec_StrFree( vString );
00198 Abc_NtkDelete( pNtk );
00199 return NULL;
00200 }
00201 }
00202 }
00203 else
00204 {
00205
00206 ppNames = (char **)vTokens->pArray + 2;
00207 nNames = vTokens->nSize - 2;
00208 pNode = Io_ReadCreateNode( pNtk, vTokens->pArray[0], ppNames, nNames );
00209
00210 if ( strcmp(pType, "AND") == 0 )
00211 Abc_ObjSetData( pNode, Abc_SopCreateAnd(pNtk->pManFunc, nNames, NULL) );
00212 else if ( strcmp(pType, "OR") == 0 )
00213 Abc_ObjSetData( pNode, Abc_SopCreateOr(pNtk->pManFunc, nNames, NULL) );
00214 else if ( strcmp(pType, "NAND") == 0 )
00215 Abc_ObjSetData( pNode, Abc_SopCreateNand(pNtk->pManFunc, nNames) );
00216 else if ( strcmp(pType, "NOR") == 0 )
00217 Abc_ObjSetData( pNode, Abc_SopCreateNor(pNtk->pManFunc, nNames) );
00218 else if ( strcmp(pType, "XOR") == 0 )
00219 Abc_ObjSetData( pNode, Abc_SopCreateXor(pNtk->pManFunc, nNames) );
00220 else if ( strcmp(pType, "NXOR") == 0 || strcmp(pType, "XNOR") == 0 )
00221 Abc_ObjSetData( pNode, Abc_SopCreateNxor(pNtk->pManFunc, nNames) );
00222 else if ( strncmp(pType, "BUF", 3) == 0 )
00223 Abc_ObjSetData( pNode, Abc_SopCreateBuf(pNtk->pManFunc) );
00224 else if ( strcmp(pType, "NOT") == 0 )
00225 Abc_ObjSetData( pNode, Abc_SopCreateInv(pNtk->pManFunc) );
00226 else if ( strncmp(pType, "MUX", 3) == 0 )
00227 Abc_ObjSetData( pNode, Abc_SopRegister(pNtk->pManFunc, "1-0 1\n-11 1\n") );
00228 else if ( strncmp(pType, "gnd", 3) == 0 )
00229 Abc_ObjSetData( pNode, Abc_SopRegister( pNtk->pManFunc, " 0\n" ) );
00230 else if ( strncmp(pType, "vdd", 3) == 0 )
00231 Abc_ObjSetData( pNode, Abc_SopRegister( pNtk->pManFunc, " 1\n" ) );
00232 else
00233 {
00234 printf( "Io_ReadBenchNetwork(): Cannot determine gate type \"%s\" in line %d.\n", pType, Extra_FileReaderGetLineNumber(p, 0) );
00235 Vec_StrFree( vString );
00236 Abc_NtkDelete( pNtk );
00237 return NULL;
00238 }
00239 }
00240 }
00241 }
00242 Extra_ProgressBarStop( pProgress );
00243 Vec_StrFree( vString );
00244
00245
00246 if ( (pNet = Abc_NtkFindNet( pNtk, "gnd" )) )
00247 {
00248 if ( Abc_ObjFaninNum(pNet) == 0 )
00249 Io_ReadCreateConst( pNtk, "gnd", 0 );
00250 }
00251 if ( (pNet = Abc_NtkFindNet( pNtk, "1" )) )
00252 {
00253 if ( Abc_ObjFaninNum(pNet) == 0 )
00254 {
00255 printf( "Io_ReadBenchNetwork(): Adding constant 0 fanin to non-driven net \"1\".\n" );
00256 Io_ReadCreateConst( pNtk, "1", 0 );
00257 }
00258 }
00259
00260 if ( (pNet = Abc_NtkFindNet( pNtk, "vdd" )) )
00261 {
00262 if ( Abc_ObjFaninNum(pNet) == 0 )
00263 Io_ReadCreateConst( pNtk, "vdd", 1 );
00264 }
00265 if ( (pNet = Abc_NtkFindNet( pNtk, "2" )) )
00266 {
00267 if ( Abc_ObjFaninNum(pNet) == 0 )
00268 {
00269 printf( "Io_ReadBenchNetwork(): Adding constant 1 fanin to non-driven net \"2\".\n" );
00270 Io_ReadCreateConst( pNtk, "2", 1 );
00271 }
00272 }
00273
00274 Abc_NtkFinalizeRead( pNtk );
00275
00276
00277 if ( fLutsPresent )
00278 {
00279 if ( !Abc_NtkToBdd(pNtk) )
00280 {
00281 printf( "Io_ReadBenchNetwork(): Converting to BDD has failed.\n" );
00282 Abc_NtkDelete( pNtk );
00283 return NULL;
00284 }
00285 if ( !Abc_NtkToSop(pNtk, 0) )
00286 {
00287 printf( "Io_ReadBenchNetwork(): Converting to SOP has failed.\n" );
00288 Abc_NtkDelete( pNtk );
00289 return NULL;
00290 }
00291 }
00292 return pNtk;
00293 }
00294
00295
00307 void Io_ReadBenchInit( Abc_Ntk_t * pNtk, char * pFileName )
00308 {
00309 char pBuffer[1000];
00310 FILE * pFile;
00311 char * pToken;
00312 Abc_Obj_t * pObj;
00313 int Num;
00314 pFile = fopen( pFileName, "r" );
00315 if ( pFile == NULL )
00316 {
00317 printf( "Io_ReadBenchInit(): Failed to open file \"%s\".\n", pFileName );
00318 return;
00319 }
00320 while ( fgets( pBuffer, 999, pFile ) )
00321 {
00322 pToken = strtok( pBuffer, " \n\t\r" );
00323
00324 Num = Nm_ManFindIdByName( pNtk->pManName, pToken, ABC_OBJ_BO );
00325 if ( Num < 0 )
00326 {
00327 printf( "Io_ReadBenchInit(): Cannot find register with output %s.\n", pToken );
00328 continue;
00329 }
00330 pObj = Abc_ObjFanin0( Abc_NtkObj( pNtk, Num ) );
00331 if ( !Abc_ObjIsLatch(pObj) )
00332 {
00333 printf( "Io_ReadBenchInit(): The signal is not a register output %s.\n", pToken );
00334 continue;
00335 }
00336
00337 pToken = strtok( NULL, " \n\t\r" );
00338 if ( pToken[0] == '0' )
00339 Abc_LatchSetInit0( pObj );
00340 else if ( pToken[0] == '1' )
00341 Abc_LatchSetInit1( pObj );
00342 else if ( pToken[0] == '2' )
00343 Abc_LatchSetInitDc( pObj );
00344 else
00345 {
00346 printf( "Io_ReadBenchInit(): The signal %s has unknown initial value (%s).\n",
00347 Abc_ObjName(Abc_ObjFanout0(pObj)), pToken );
00348 continue;
00349 }
00350 }
00351 fclose( pFile );
00352 }
00353
00354
00358
00359
00360