00001
00021 #include "io.h"
00022
00026
00027 static Abc_Ntk_t * Io_ReadPlaNetwork( Extra_FileReader_t * p );
00028
00032
00044 Abc_Ntk_t * Io_ReadPla( 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_ReadPlaNetwork( p );
00056 Extra_FileReaderFree( p );
00057 if ( pNtk == NULL )
00058 return NULL;
00059
00060
00061 if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
00062 {
00063 printf( "Io_ReadPla: The network check has failed.\n" );
00064 Abc_NtkDelete( pNtk );
00065 return NULL;
00066 }
00067 return pNtk;
00068 }
00080 Abc_Ntk_t * Io_ReadPlaNetwork( Extra_FileReader_t * p )
00081 {
00082 ProgressBar * pProgress;
00083 Vec_Ptr_t * vTokens;
00084 Abc_Ntk_t * pNtk;
00085 Abc_Obj_t * pTermPi, * pTermPo, * pNode;
00086 Vec_Str_t ** ppSops;
00087 char Buffer[100];
00088 int nInputs = -1, nOutputs = -1, nProducts = -1;
00089 char * pCubeIn, * pCubeOut;
00090 int i, k, iLine, nDigits, nCubes;
00091
00092
00093 pNtk = Abc_NtkStartRead( Extra_FileReaderGetFileName(p) );
00094
00095
00096 nCubes = 0;
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
00103 if ( strcmp( vTokens->pArray[0], ".e" ) == 0 )
00104 break;
00105
00106 if ( vTokens->nSize == 1 )
00107 {
00108 printf( "%s (line %d): Wrong number of token.\n",
00109 Extra_FileReaderGetFileName(p), iLine+1 );
00110 Abc_NtkDelete( pNtk );
00111 return NULL;
00112 }
00113
00114 if ( strcmp( vTokens->pArray[0], ".i" ) == 0 )
00115 nInputs = atoi(vTokens->pArray[1]);
00116 else if ( strcmp( vTokens->pArray[0], ".o" ) == 0 )
00117 nOutputs = atoi(vTokens->pArray[1]);
00118 else if ( strcmp( vTokens->pArray[0], ".p" ) == 0 )
00119 nProducts = atoi(vTokens->pArray[1]);
00120 else if ( strcmp( vTokens->pArray[0], ".ilb" ) == 0 )
00121 {
00122 if ( vTokens->nSize - 1 != nInputs )
00123 printf( "Warning: Mismatch between the number of PIs on the .i line (%d) and the number of PIs on the .ilb line (%d).\n", nInputs, vTokens->nSize - 1 );
00124 for ( i = 1; i < vTokens->nSize; i++ )
00125 Io_ReadCreatePi( pNtk, vTokens->pArray[i] );
00126 }
00127 else if ( strcmp( vTokens->pArray[0], ".ob" ) == 0 )
00128 {
00129 if ( vTokens->nSize - 1 != nOutputs )
00130 printf( "Warning: Mismatch between the number of POs on the .o line (%d) and the number of POs on the .ob line (%d).\n", nOutputs, vTokens->nSize - 1 );
00131 for ( i = 1; i < vTokens->nSize; i++ )
00132 Io_ReadCreatePo( pNtk, vTokens->pArray[i] );
00133 }
00134 else
00135 {
00136
00137 if ( Abc_NtkPiNum(pNtk) == 0 )
00138 {
00139 if ( nInputs == -1 )
00140 {
00141 printf( "%s: The number of inputs is not specified.\n", Extra_FileReaderGetFileName(p) );
00142 Abc_NtkDelete( pNtk );
00143 return NULL;
00144 }
00145 nDigits = Extra_Base10Log( nInputs );
00146 for ( i = 0; i < nInputs; i++ )
00147 {
00148 sprintf( Buffer, "x%0*d", nDigits, i );
00149 Io_ReadCreatePi( pNtk, Buffer );
00150 }
00151 }
00152 if ( Abc_NtkPoNum(pNtk) == 0 )
00153 {
00154 if ( nOutputs == -1 )
00155 {
00156 printf( "%s: The number of outputs is not specified.\n", Extra_FileReaderGetFileName(p) );
00157 Abc_NtkDelete( pNtk );
00158 return NULL;
00159 }
00160 nDigits = Extra_Base10Log( nOutputs );
00161 for ( i = 0; i < nOutputs; i++ )
00162 {
00163 sprintf( Buffer, "z%0*d", nDigits, i );
00164 Io_ReadCreatePo( pNtk, Buffer );
00165 }
00166 }
00167 if ( Abc_NtkNodeNum(pNtk) == 0 )
00168 {
00169
00170
00171 ppSops = ALLOC( Vec_Str_t *, nOutputs );
00172 Abc_NtkForEachPo( pNtk, pTermPo, i )
00173 {
00174 ppSops[i] = Vec_StrAlloc( 100 );
00175
00176 pNode = Abc_NtkCreateNode(pNtk);
00177
00178 Abc_ObjAddFanin( Abc_ObjFanin0Ntk(pTermPo), pNode );
00179
00180 Abc_NtkForEachPi( pNtk, pTermPi, k )
00181 Abc_ObjAddFanin( pNode, Abc_ObjFanout0Ntk(pTermPi) );
00182 }
00183 }
00184
00185 if ( vTokens->nSize != 2 )
00186 {
00187 printf( "%s (line %d): Input and output cubes are not specified.\n",
00188 Extra_FileReaderGetFileName(p), iLine+1 );
00189 Abc_NtkDelete( pNtk );
00190 return NULL;
00191 }
00192 pCubeIn = vTokens->pArray[0];
00193 pCubeOut = vTokens->pArray[1];
00194 if ( strlen(pCubeIn) != (unsigned)nInputs )
00195 {
00196 printf( "%s (line %d): Input cube length (%d) differs from the number of inputs (%d).\n",
00197 Extra_FileReaderGetFileName(p), iLine+1, strlen(pCubeIn), nInputs );
00198 Abc_NtkDelete( pNtk );
00199 return NULL;
00200 }
00201 if ( strlen(pCubeOut) != (unsigned)nOutputs )
00202 {
00203 printf( "%s (line %d): Output cube length (%d) differs from the number of outputs (%d).\n",
00204 Extra_FileReaderGetFileName(p), iLine+1, strlen(pCubeOut), nOutputs );
00205 Abc_NtkDelete( pNtk );
00206 return NULL;
00207 }
00208 for ( i = 0; i < nOutputs; i++ )
00209 {
00210 if ( pCubeOut[i] == '1' )
00211 {
00212 Vec_StrAppend( ppSops[i], pCubeIn );
00213 Vec_StrAppend( ppSops[i], " 1\n" );
00214 }
00215 }
00216 nCubes++;
00217 }
00218 }
00219 Extra_ProgressBarStop( pProgress );
00220 if ( nProducts != -1 && nCubes != nProducts )
00221 printf( "Warning: Mismatch between the number of cubes (%d) and the number on .p line (%d).\n",
00222 nCubes, nProducts );
00223
00224
00225 Abc_NtkForEachPo( pNtk, pTermPo, i )
00226 {
00227 pNode = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pTermPo) );
00228 if ( ppSops[i]->nSize == 0 )
00229 {
00230 Abc_ObjRemoveFanins(pNode);
00231 pNode->pData = Abc_SopRegister( pNtk->pManFunc, " 0\n" );
00232 Vec_StrFree( ppSops[i] );
00233 continue;
00234 }
00235 Vec_StrPush( ppSops[i], 0 );
00236 pNode->pData = Abc_SopRegister( pNtk->pManFunc, ppSops[i]->pArray );
00237 Vec_StrFree( ppSops[i] );
00238 }
00239 free( ppSops );
00240 Abc_NtkFinalizeRead( pNtk );
00241 return pNtk;
00242 }
00243
00244
00248
00249
00250