00001
00021 #include "io.h"
00022
00026
00030
00042 Io_FileType_t Io_ReadFileType( char * pFileName )
00043 {
00044 char * pExt;
00045 if ( pFileName == NULL )
00046 return IO_FILE_NONE;
00047 pExt = Extra_FileNameExtension( pFileName );
00048 if ( pExt == NULL )
00049 return IO_FILE_NONE;
00050 if ( !strcmp( pExt, "aig" ) )
00051 return IO_FILE_AIGER;
00052 if ( !strcmp( pExt, "baf" ) )
00053 return IO_FILE_BAF;
00054 if ( !strcmp( pExt, "blif" ) )
00055 return IO_FILE_BLIF;
00056 if ( !strcmp( pExt, "bench" ) )
00057 return IO_FILE_BENCH;
00058 if ( !strcmp( pExt, "cnf" ) )
00059 return IO_FILE_CNF;
00060 if ( !strcmp( pExt, "dot" ) )
00061 return IO_FILE_DOT;
00062 if ( !strcmp( pExt, "edif" ) )
00063 return IO_FILE_EDIF;
00064 if ( !strcmp( pExt, "eqn" ) )
00065 return IO_FILE_EQN;
00066 if ( !strcmp( pExt, "gml" ) )
00067 return IO_FILE_GML;
00068 if ( !strcmp( pExt, "list" ) )
00069 return IO_FILE_LIST;
00070 if ( !strcmp( pExt, "mv" ) )
00071 return IO_FILE_BLIFMV;
00072 if ( !strcmp( pExt, "pla" ) )
00073 return IO_FILE_PLA;
00074 if ( !strcmp( pExt, "v" ) )
00075 return IO_FILE_VERILOG;
00076 return IO_FILE_UNKNOWN;
00077 }
00078
00090 Abc_Ntk_t * Io_ReadNetlist( char * pFileName, Io_FileType_t FileType, int fCheck )
00091 {
00092 FILE * pFile;
00093 Abc_Ntk_t * pNtk;
00094 if ( FileType == IO_FILE_NONE || FileType == IO_FILE_UNKNOWN )
00095 {
00096 fprintf( stdout, "The generic file reader requires a known file extension.\n" );
00097 return NULL;
00098 }
00099
00100 pFile = fopen( pFileName, "r" );
00101 if ( pFile == NULL )
00102 {
00103 fprintf( stdout, "Cannot open input file \"%s\". ", pFileName );
00104 if ( pFileName = Extra_FileGetSimilarName( pFileName, ".blif", ".bench", ".pla", ".baf", ".aig" ) )
00105 fprintf( stdout, "Did you mean \"%s\"?", pFileName );
00106 fprintf( stdout, "\n" );
00107 return NULL;
00108 }
00109 fclose( pFile );
00110
00111 if ( FileType == IO_FILE_AIGER || FileType == IO_FILE_BAF )
00112 {
00113 if ( FileType == IO_FILE_AIGER )
00114 pNtk = Io_ReadAiger( pFileName, fCheck );
00115 else
00116 pNtk = Io_ReadBaf( pFileName, fCheck );
00117 if ( pNtk == NULL )
00118 {
00119 fprintf( stdout, "Reading AIG from file has failed.\n" );
00120 return NULL;
00121 }
00122 return pNtk;
00123 }
00124
00125 if ( FileType == IO_FILE_BLIF )
00126
00127 pNtk = Io_ReadBlifMv( pFileName, 0, fCheck );
00128 else if ( Io_ReadFileType(pFileName) == IO_FILE_BLIFMV )
00129 pNtk = Io_ReadBlifMv( pFileName, 1, fCheck );
00130 else if ( FileType == IO_FILE_BENCH )
00131 pNtk = Io_ReadBench( pFileName, fCheck );
00132 else if ( FileType == IO_FILE_EDIF )
00133 pNtk = Io_ReadEdif( pFileName, fCheck );
00134 else if ( FileType == IO_FILE_EQN )
00135 pNtk = Io_ReadEqn( pFileName, fCheck );
00136 else if ( FileType == IO_FILE_PLA )
00137 pNtk = Io_ReadPla( pFileName, fCheck );
00138 else if ( FileType == IO_FILE_VERILOG )
00139 pNtk = Io_ReadVerilog( pFileName, fCheck );
00140 else
00141 {
00142 fprintf( stderr, "Unknown file format.\n" );
00143 return NULL;
00144 }
00145 if ( pNtk == NULL )
00146 {
00147 fprintf( stdout, "Reading network from file has failed.\n" );
00148 return NULL;
00149 }
00150 if ( Abc_NtkBlackboxNum(pNtk) || Abc_NtkWhiteboxNum(pNtk) )
00151 fprintf( stdout, "Warning: The network contains hierarchy.\n" );
00152 return pNtk;
00153 }
00154
00155
00167 Abc_Ntk_t * Io_Read( char * pFileName, Io_FileType_t FileType, int fCheck )
00168 {
00169 Abc_Ntk_t * pNtk, * pTemp;
00170
00171 pNtk = Io_ReadNetlist( pFileName, FileType, fCheck );
00172 if ( pNtk == NULL )
00173 return NULL;
00174 if ( !Abc_NtkIsNetlist(pNtk) )
00175 return pNtk;
00176
00177 assert( Abc_NtkIsNetlist(pNtk) );
00178 if ( Abc_NtkWhiteboxNum(pNtk) > 0 )
00179 {
00180 pNtk = Abc_NtkFlattenLogicHierarchy( pTemp = pNtk );
00181 Abc_NtkDelete( pTemp );
00182 if ( pNtk == NULL )
00183 {
00184 fprintf( stdout, "Flattening logic hierarchy has failed.\n" );
00185 return NULL;
00186 }
00187 }
00188
00189 if ( Abc_NtkBlackboxNum(pNtk) > 0 )
00190 {
00191 printf( "Hierarchy reader converted %d instances of blackboxes.\n", Abc_NtkBlackboxNum(pNtk) );
00192 pNtk = Abc_NtkConvertBlackboxes( pTemp = pNtk );
00193 Abc_NtkDelete( pTemp );
00194 if ( pNtk == NULL )
00195 {
00196 fprintf( stdout, "Converting blackboxes has failed.\n" );
00197 return NULL;
00198 }
00199 }
00200
00201 if ( Io_ReadFileType(pFileName) == IO_FILE_BLIFMV )
00202 {
00203
00204
00205 pNtk = Abc_NtkStrashBlifMv( pTemp = pNtk );
00206 Abc_NtkDelete( pTemp );
00207 if ( pNtk == NULL )
00208 {
00209 fprintf( stdout, "Converting BLIF-MV to AIG has failed.\n" );
00210 return NULL;
00211 }
00212 return pNtk;
00213 }
00214
00215 pNtk = Abc_NtkToLogic( pTemp = pNtk );
00216 Abc_NtkDelete( pTemp );
00217 if ( pNtk == NULL )
00218 {
00219 fprintf( stdout, "Converting netlist to logic network after reading has failed.\n" );
00220 return NULL;
00221 }
00222 return pNtk;
00223 }
00224
00236 void Io_Write( Abc_Ntk_t * pNtk, char * pFileName, Io_FileType_t FileType )
00237 {
00238 Abc_Ntk_t * pNtkTemp, * pNtkCopy;
00239
00240 if ( pNtk == NULL )
00241 {
00242 fprintf( stdout, "Empty network.\n" );
00243 return;
00244 }
00245
00246 if ( FileType == IO_FILE_NONE || FileType == IO_FILE_UNKNOWN )
00247 {
00248 fprintf( stdout, "The generic file writer requires a known file extension.\n" );
00249 return;
00250 }
00251
00252 if ( FileType == IO_FILE_AIGER || FileType == IO_FILE_BAF )
00253 {
00254 if ( !Abc_NtkIsStrash(pNtk) )
00255 {
00256 fprintf( stdout, "Writing this format is only possible for structurally hashed AIGs.\n" );
00257 return;
00258 }
00259 if ( FileType == IO_FILE_AIGER )
00260 Io_WriteAiger( pNtk, pFileName, 1 );
00261 else
00262 Io_WriteBaf( pNtk, pFileName );
00263 return;
00264 }
00265
00266 if ( FileType == IO_FILE_CNF )
00267 {
00268 Io_WriteCnf( pNtk, pFileName, 0 );
00269 return;
00270 }
00271 if ( FileType == IO_FILE_DOT )
00272 {
00273 Io_WriteDot( pNtk, pFileName );
00274 return;
00275 }
00276 if ( FileType == IO_FILE_GML )
00277 {
00278 Io_WriteGml( pNtk, pFileName );
00279 return;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289 if ( FileType == IO_FILE_PLA )
00290 {
00291 if ( Abc_NtkLevel(pNtk) > 1 )
00292 {
00293 fprintf( stdout, "PLA writing is available for collapsed networks.\n" );
00294 return;
00295 }
00296 if ( Abc_NtkIsComb(pNtk) )
00297 pNtkTemp = Abc_NtkToNetlist( pNtk );
00298 else
00299 {
00300 fprintf( stdout, "Latches are writen into the PLA file at PI/PO pairs.\n" );
00301 pNtkCopy = Abc_NtkDup( pNtk );
00302 Abc_NtkMakeComb( pNtkCopy );
00303 pNtkTemp = Abc_NtkToNetlist( pNtk );
00304 Abc_NtkDelete( pNtkCopy );
00305 }
00306 if ( !Abc_NtkToSop( pNtk, 1 ) )
00307 return;
00308 }
00309 else if ( FileType == IO_FILE_BENCH )
00310 {
00311 if ( !Abc_NtkIsStrash(pNtk) )
00312 {
00313 fprintf( stdout, "Writing traditional BENCH is available for AIGs only (use \"write_bench\").\n" );
00314 return;
00315 }
00316 pNtkTemp = Abc_NtkToNetlistBench( pNtk );
00317 }
00318 else
00319 pNtkTemp = Abc_NtkToNetlist( pNtk );
00320
00321 if ( pNtkTemp == NULL )
00322 {
00323 fprintf( stdout, "Converting to netlist has failed.\n" );
00324 return;
00325 }
00326
00327 if ( FileType == IO_FILE_BLIF )
00328 {
00329 if ( !Abc_NtkHasSop(pNtkTemp) && !Abc_NtkHasMapping(pNtkTemp) )
00330 Abc_NtkToSop( pNtkTemp, 0 );
00331 Io_WriteBlif( pNtkTemp, pFileName, 1 );
00332 }
00333 else if ( FileType == IO_FILE_BLIFMV )
00334 {
00335 if ( !Abc_NtkConvertToBlifMv( pNtkTemp ) )
00336 return;
00337 Io_WriteBlifMv( pNtkTemp, pFileName );
00338 }
00339 else if ( FileType == IO_FILE_BENCH )
00340 Io_WriteBench( pNtkTemp, pFileName );
00341 else if ( FileType == IO_FILE_PLA )
00342 Io_WritePla( pNtkTemp, pFileName );
00343 else if ( FileType == IO_FILE_EQN )
00344 {
00345 if ( !Abc_NtkHasAig(pNtkTemp) )
00346 Abc_NtkToAig( pNtkTemp );
00347 Io_WriteEqn( pNtkTemp, pFileName );
00348 }
00349 else if ( FileType == IO_FILE_VERILOG )
00350 {
00351 if ( !Abc_NtkHasAig(pNtkTemp) && !Abc_NtkHasMapping(pNtkTemp) )
00352 Abc_NtkToAig( pNtkTemp );
00353 Io_WriteVerilog( pNtkTemp, pFileName );
00354 }
00355 else
00356 fprintf( stderr, "Unknown file format.\n" );
00357 Abc_NtkDelete( pNtkTemp );
00358 }
00359
00371 void Io_WriteHie( Abc_Ntk_t * pNtk, char * pBaseName, char * pFileName )
00372 {
00373 Abc_Ntk_t * pNtkTemp, * pNtkResult, * pNtkBase = NULL;
00374
00375 if ( pNtk == NULL )
00376 {
00377 fprintf( stdout, "Empty network.\n" );
00378 return;
00379 }
00380
00381
00382 assert( Abc_NtkIsStrash(pNtk) || Abc_NtkIsLogic(pNtk) );
00383 if ( Io_ReadFileType(pBaseName) == IO_FILE_BLIF )
00384 pNtkBase = Io_ReadBlifMv( pBaseName, 0, 1 );
00385 else if ( Io_ReadFileType(pBaseName) == IO_FILE_BLIFMV )
00386 pNtkBase = Io_ReadBlifMv( pBaseName, 1, 1 );
00387 else if ( Io_ReadFileType(pBaseName) == IO_FILE_VERILOG )
00388 pNtkBase = Io_ReadVerilog( pBaseName, 1 );
00389 else
00390 fprintf( stderr, "Unknown input file format.\n" );
00391 if ( pNtkBase == NULL )
00392 return;
00393
00394
00395 if ( Abc_NtkWhiteboxNum(pNtkBase) > 0 )
00396 {
00397 pNtkBase = Abc_NtkFlattenLogicHierarchy( pNtkTemp = pNtkBase );
00398 if ( pNtkBase == NULL )
00399 return;
00400 Abc_NtkDelete( pNtkTemp );
00401 }
00402
00403
00404 if ( Io_ReadFileType(pBaseName) == IO_FILE_BLIFMV )
00405 {
00406 if ( Abc_NtkBlackboxNum(pNtkBase) > 0 )
00407 {
00408 printf( "Hierarchy writer does not support BLIF-MV with blackboxes.\n" );
00409 Abc_NtkDelete( pNtkBase );
00410 return;
00411 }
00412
00413 assert( !Abc_NtkIsNetlist(pNtk) );
00414 pNtkResult = Abc_NtkToNetlist( pNtk );
00415 if ( !Abc_NtkConvertToBlifMv( pNtkResult ) )
00416 return;
00417
00418 pNtkResult = Abc_NtkInsertBlifMv( pNtkBase, pNtkTemp = pNtkResult );
00419 Abc_NtkDelete( pNtkTemp );
00420 }
00421 else if ( Abc_NtkBlackboxNum(pNtkBase) > 0 )
00422 {
00423
00424 pNtkResult = Abc_NtkToNetlist( pNtk );
00425 pNtkResult = Abc_NtkInsertNewLogic( pNtkBase, pNtkTemp = pNtkResult );
00426 Abc_NtkDelete( pNtkTemp );
00427 if ( pNtkResult )
00428 printf( "Hierarchy writer reintroduced %d instances of blackboxes.\n", Abc_NtkBlackboxNum(pNtkBase) );
00429 }
00430 else
00431 {
00432 printf( "Warning: The output network does not contain blackboxes.\n" );
00433 pNtkResult = Abc_NtkToNetlist( pNtk );
00434 }
00435 Abc_NtkDelete( pNtkBase );
00436 if ( pNtkResult == NULL )
00437 return;
00438
00439
00440 if ( Io_ReadFileType(pFileName) == IO_FILE_BLIF )
00441 {
00442 if ( !Abc_NtkHasSop(pNtkResult) && !Abc_NtkHasMapping(pNtkResult) )
00443 Abc_NtkToSop( pNtkResult, 0 );
00444 Io_WriteBlif( pNtkResult, pFileName, 1 );
00445 }
00446 else if ( Io_ReadFileType(pFileName) == IO_FILE_VERILOG )
00447 {
00448 if ( !Abc_NtkHasAig(pNtkResult) && !Abc_NtkHasMapping(pNtkResult) )
00449 Abc_NtkToAig( pNtkResult );
00450 Io_WriteVerilog( pNtkResult, pFileName );
00451 }
00452 else if ( Io_ReadFileType(pFileName) == IO_FILE_BLIFMV )
00453 {
00454 Io_WriteBlifMv( pNtkResult, pFileName );
00455 }
00456 else
00457 fprintf( stderr, "Unknown output file format.\n" );
00458
00459 Abc_NtkDelete( pNtkResult );
00460 }
00461
00473 Abc_Obj_t * Io_ReadCreatePi( Abc_Ntk_t * pNtk, char * pName )
00474 {
00475 Abc_Obj_t * pNet, * pTerm;
00476
00477 pNet = Abc_NtkFindNet( pNtk, pName );
00478 if ( pNet )
00479 printf( "Warning: PI \"%s\" appears twice in the list.\n", pName );
00480 pNet = Abc_NtkFindOrCreateNet( pNtk, pName );
00481
00482 pTerm = Abc_NtkCreatePi( pNtk );
00483 Abc_ObjAddFanin( pNet, pTerm );
00484 return pTerm;
00485 }
00486
00498 Abc_Obj_t * Io_ReadCreatePo( Abc_Ntk_t * pNtk, char * pName )
00499 {
00500 Abc_Obj_t * pNet, * pTerm;
00501
00502 pNet = Abc_NtkFindNet( pNtk, pName );
00503 if ( pNet && Abc_ObjFaninNum(pNet) == 0 )
00504 printf( "Warning: PO \"%s\" appears twice in the list.\n", pName );
00505 pNet = Abc_NtkFindOrCreateNet( pNtk, pName );
00506
00507 pTerm = Abc_NtkCreatePo( pNtk );
00508 Abc_ObjAddFanin( pTerm, pNet );
00509 return pTerm;
00510 }
00511
00523 Abc_Obj_t * Io_ReadCreateAssert( Abc_Ntk_t * pNtk, char * pName )
00524 {
00525 Abc_Obj_t * pNet, * pTerm;
00526
00527 pNet = Abc_NtkFindNet( pNtk, pName );
00528 if ( pNet && Abc_ObjFaninNum(pNet) == 0 )
00529 printf( "Warning: Assert \"%s\" appears twice in the list.\n", pName );
00530 pNet = Abc_NtkFindOrCreateNet( pNtk, pName );
00531
00532 pTerm = Abc_NtkCreateAssert( pNtk );
00533 Abc_ObjAddFanin( pTerm, pNet );
00534 return pTerm;
00535 }
00536
00548 Abc_Obj_t * Io_ReadCreateLatch( Abc_Ntk_t * pNtk, char * pNetLI, char * pNetLO )
00549 {
00550 Abc_Obj_t * pLatch, * pTerm, * pNet;
00551
00552 pNet = Abc_NtkFindOrCreateNet( pNtk, pNetLI );
00553
00554 pTerm = Abc_NtkCreateBi( pNtk );
00555 Abc_ObjAddFanin( pTerm, pNet );
00556
00557 pLatch = Abc_NtkCreateLatch( pNtk );
00558 Abc_ObjAddFanin( pLatch, pTerm );
00559
00560 pTerm = Abc_NtkCreateBo( pNtk );
00561 Abc_ObjAddFanin( pTerm, pLatch );
00562
00563 pNet = Abc_NtkFindOrCreateNet( pNtk, pNetLO );
00564 Abc_ObjAddFanin( pNet, pTerm );
00565
00566 Abc_ObjAssignName( pLatch, pNetLO, "L" );
00567 return pLatch;
00568 }
00569
00581 Abc_Obj_t * Io_ReadCreateResetLatch( Abc_Ntk_t * pNtk, int fBlifMv )
00582 {
00583 Abc_Obj_t * pLatch, * pNode;
00584 Abc_Obj_t * pNetLI, * pNetLO;
00585
00586
00587 pNetLI = Abc_NtkCreateNet( pNtk );
00588 pNetLO = Abc_NtkCreateNet( pNtk );
00589 Abc_ObjAssignName( pNetLI, Abc_ObjName(pNetLI), NULL );
00590 Abc_ObjAssignName( pNetLO, Abc_ObjName(pNetLO), NULL );
00591 pLatch = Io_ReadCreateLatch( pNtk, Abc_ObjName(pNetLI), Abc_ObjName(pNetLO) );
00592
00593 Abc_LatchSetInit0( pLatch );
00594
00595
00596
00597 pNode = Abc_NtkCreateNodeConst1( pNtk );
00598 Abc_ObjAddFanin( Abc_ObjFanin0(Abc_ObjFanin0(pLatch)), pNode );
00599 return pLatch;
00600 }
00601
00613 Abc_Obj_t * Io_ReadCreateNode( Abc_Ntk_t * pNtk, char * pNameOut, char * pNamesIn[], int nInputs )
00614 {
00615 Abc_Obj_t * pNet, * pNode;
00616 int i;
00617
00618 pNode = Abc_NtkCreateNode( pNtk );
00619
00620 for ( i = 0; i < nInputs; i++ )
00621 {
00622 pNet = Abc_NtkFindOrCreateNet( pNtk, pNamesIn[i] );
00623 Abc_ObjAddFanin( pNode, pNet );
00624 }
00625
00626 pNet = Abc_NtkFindOrCreateNet( pNtk, pNameOut );
00627 Abc_ObjAddFanin( pNet, pNode );
00628 return pNode;
00629 }
00630
00642 Abc_Obj_t * Io_ReadCreateConst( Abc_Ntk_t * pNtk, char * pName, bool fConst1 )
00643 {
00644 Abc_Obj_t * pNet, * pTerm;
00645 pTerm = fConst1? Abc_NtkCreateNodeConst1(pNtk) : Abc_NtkCreateNodeConst0(pNtk);
00646 pNet = Abc_NtkFindNet(pNtk, pName); assert( pNet );
00647 Abc_ObjAddFanin( pNet, pTerm );
00648 return pTerm;
00649 }
00650
00662 Abc_Obj_t * Io_ReadCreateInv( Abc_Ntk_t * pNtk, char * pNameIn, char * pNameOut )
00663 {
00664 Abc_Obj_t * pNet, * pNode;
00665 pNet = Abc_NtkFindNet(pNtk, pNameIn); assert( pNet );
00666 pNode = Abc_NtkCreateNodeInv(pNtk, pNet);
00667 pNet = Abc_NtkFindNet(pNtk, pNameOut); assert( pNet );
00668 Abc_ObjAddFanin( pNet, pNode );
00669 return pNode;
00670 }
00671
00683 Abc_Obj_t * Io_ReadCreateBuf( Abc_Ntk_t * pNtk, char * pNameIn, char * pNameOut )
00684 {
00685 Abc_Obj_t * pNet, * pNode;
00686 pNet = Abc_NtkFindNet(pNtk, pNameIn); assert( pNet );
00687 pNode = Abc_NtkCreateNodeBuf(pNtk, pNet);
00688 pNet = Abc_NtkFindNet(pNtk, pNameOut); assert( pNet );
00689 Abc_ObjAddFanin( pNet, pNode );
00690 return pNet;
00691 }
00692
00693
00707 FILE * Io_FileOpen( const char * FileName, const char * PathVar, const char * Mode, int fVerbose )
00708 {
00709 char * t = 0, * c = 0, * i;
00710 extern char * Abc_FrameReadFlag( char * pFlag );
00711
00712 if ( PathVar == 0 )
00713 {
00714 return fopen( FileName, Mode );
00715 }
00716 else
00717 {
00718 if ( c = Abc_FrameReadFlag( (char*)PathVar ) )
00719 {
00720 char ActualFileName[4096];
00721 FILE * fp = 0;
00722 t = Extra_UtilStrsav( c );
00723 for (i = strtok( t, ":" ); i != 0; i = strtok( 0, ":") )
00724 {
00725 #ifdef WIN32
00726 _snprintf ( ActualFileName, 4096, "%s/%s", i, FileName );
00727 #else
00728 snprintf ( ActualFileName, 4096, "%s/%s", i, FileName );
00729 #endif
00730 if ( ( fp = fopen ( ActualFileName, Mode ) ) )
00731 {
00732 if ( fVerbose )
00733 fprintf ( stdout, "Using file %s\n", ActualFileName );
00734 free( t );
00735 return fp;
00736 }
00737 }
00738 free( t );
00739 return 0;
00740 }
00741 else
00742 {
00743 return fopen( FileName, Mode );
00744 }
00745 }
00746 }
00747
00751
00752