00001
00019 #include "mapperInt.h"
00020
00024
00025 static int Map_LibraryReadFile( Map_SuperLib_t * pLib, FILE * pFile );
00026 static Map_Super_t * Map_LibraryReadGate( Map_SuperLib_t * pLib, char * pBuffer, int nVars );
00027 static int Map_LibraryTruthVerify( Map_SuperLib_t * pLib, Map_Super_t * pGate );
00028 static void Map_LibraryComputeTruth( Map_SuperLib_t * pLib, char * pFormula, unsigned uTruthRes[] );
00029 static void Map_LibraryComputeTruth_rec( Map_SuperLib_t * pLib, char * pFormula, unsigned uTruthsIn[][2], unsigned uTruthRes[] );
00030 static void Map_LibraryPrintClasses( Map_SuperLib_t * p );
00031
00035
00047 int Map_LibraryRead( Map_SuperLib_t * pLib, char * pFileName )
00048 {
00049 FILE * pFile;
00050 int Status;
00051
00052 assert( pLib->pGenlib == NULL );
00053 pFile = fopen( pFileName, "r" );
00054 if ( pFile == NULL )
00055 {
00056 printf( "Cannot open input file \"%s\".\n", pFileName );
00057 return 0;
00058 }
00059 Status = Map_LibraryReadFile( pLib, pFile );
00060 fclose( pFile );
00061
00062 return Status;
00063 }
00064
00065
00077 int Map_LibraryReadFile( Map_SuperLib_t * pLib, FILE * pFile )
00078 {
00079 ProgressBar * pProgress;
00080 char pBuffer[2000];
00081 FILE * pFileGen;
00082 Map_Super_t * pGate;
00083 char * pTemp, * pLibName;
00084 int nCounter, nGatesTotal;
00085 unsigned uCanon[2];
00086
00087
00088 while ( fgets( pBuffer, 5000, pFile ) != NULL )
00089 {
00090
00091 for ( pTemp = pBuffer; *pTemp == ' ' || *pTemp == '\r' || *pTemp == '\n'; pTemp++ );
00092
00093 if ( *pTemp != 0 && *pTemp != '#' )
00094 break;
00095 }
00096
00097
00098 pLibName = strtok( pTemp, " \t\r\n" );
00099 if ( strcmp( pLibName, "GATE" ) == 0 )
00100 {
00101 printf( "The input file \"%s\" looks like a GENLIB file and not a supergate library file.\n", pLib->pName );
00102 return 0;
00103 }
00104 pFileGen = fopen( pLibName, "r" );
00105 if ( pFileGen == NULL )
00106 {
00107 printf( "Cannot open the GENLIB file \"%s\".\n", pLibName );
00108 return 0;
00109 }
00110 fclose( pFileGen );
00111
00112
00113 pLib->pGenlib = Mio_LibraryRead( Abc_FrameGetGlobalFrame(), pLibName, 0, 0 );
00114 if ( pLib->pGenlib == NULL )
00115 {
00116 printf( "Cannot read GENLIB file \"%s\".\n", pLibName );
00117 return 0;
00118 }
00119
00120
00121 fscanf( pFile, "%d\n", &pLib->nVarsMax );
00122 if ( pLib->nVarsMax < 2 || pLib->nVarsMax > 10 )
00123 {
00124 printf( "Suspicious number of variables (%d).\n", pLib->nVarsMax );
00125 return 0;
00126 }
00127
00128
00129 fscanf( pFile, "%d\n", &nGatesTotal );
00130 if ( nGatesTotal < 1 || nGatesTotal > 10000000 )
00131 {
00132 printf( "Suspicious number of gates (%d).\n", nGatesTotal );
00133 return 0;
00134 }
00135
00136
00137 nCounter = 0;
00138 pProgress = Extra_ProgressBarStart( stdout, nGatesTotal );
00139 while ( fgets( pBuffer, 5000, pFile ) != NULL )
00140 {
00141 for ( pTemp = pBuffer; *pTemp == ' ' || *pTemp == '\r' || *pTemp == '\n'; pTemp++ );
00142 if ( pTemp[0] == '\0' )
00143 continue;
00144
00145 pGate = Map_LibraryReadGate( pLib, pTemp, pLib->nVarsMax );
00146 assert( pGate->Num == nCounter + 1 );
00147
00148 for ( pTemp = pGate->pFormula; *pTemp; pTemp++ )
00149 pGate->nGates += (*pTemp == '(');
00150
00151 assert( Map_LibraryTruthVerify(pLib, pGate) );
00152
00153
00154 pGate->nPhases = Map_CanonComputeSlow( pLib->uTruths, pLib->nVarsMax, pLib->nVarsMax, pGate->uTruth, pGate->uPhases, uCanon );
00155
00156 Map_SuperTableInsertC( pLib->tTableC, uCanon, pGate );
00157
00158 Extra_ProgressBarUpdate( pProgress, ++nCounter, NULL );
00159 }
00160 Extra_ProgressBarStop( pProgress );
00161 pLib->nSupersAll = nCounter;
00162 if ( nCounter != nGatesTotal )
00163 printf( "The number of gates read (%d) is different what the file says (%d).\n", nGatesTotal, nCounter );
00164 return 1;
00165 }
00166
00178 Map_Super_t * Map_LibraryReadGate( Map_SuperLib_t * pLib, char * pBuffer, int nVars )
00179 {
00180 Map_Super_t * pGate;
00181 char * pTemp;
00182 int i;
00183
00184
00185 pGate = (Map_Super_t *)Extra_MmFixedEntryFetch( pLib->mmSupers );
00186 memset( pGate, 0, sizeof(Map_Super_t) );
00187
00188
00189 pTemp = strtok( pBuffer, " " );
00190 pGate->Num = atoi(pTemp);
00191
00192
00193 pTemp = strtok( NULL, " " );
00194 if ( pLib->nVarsMax < 6 )
00195 {
00196 pGate->uTruth[0] = Extra_ReadBinary(pTemp);
00197 pGate->uTruth[1] = 0;
00198 }
00199 else
00200 {
00201 pGate->uTruth[0] = Extra_ReadBinary(pTemp+32);
00202 pTemp[32] = 0;
00203 pGate->uTruth[1] = Extra_ReadBinary(pTemp);
00204 }
00205
00206
00207 pTemp = strtok( NULL, " " );
00208 pGate->tDelayMax.Rise = (float)atof(pTemp);
00209 pGate->tDelayMax.Fall = pGate->tDelayMax.Rise;
00210
00211
00212 for ( i = 0; i < nVars; i++ )
00213 {
00214 pTemp = strtok( NULL, " " );
00215 pGate->tDelaysR[i].Rise = (float)atof(pTemp);
00216 pGate->tDelaysF[i].Fall = pGate->tDelaysR[i].Rise;
00217 }
00218
00219
00220 pTemp = strtok( NULL, " " );
00221 pGate->Area = (float)atof(pTemp);
00222
00223
00224 pTemp = strtok( NULL, " \r\n" );
00225 if ( strlen(pTemp) == 0 )
00226 printf( "A gate name is empty.\n" );
00227
00228
00229 pGate->pFormula = Extra_MmFlexEntryFetch( pLib->mmForms, strlen(pTemp) + 1 );
00230 strcpy( pGate->pFormula, pTemp );
00231
00232
00233 pTemp = strtok( NULL, " \n\0" );
00234 if ( pTemp != NULL )
00235 printf( "The following trailing symbols found \"%s\".\n", pTemp );
00236 return pGate;
00237 }
00238
00251 char * Map_LibraryReadFormulaStep( char * pFormula, char * pStrings[], int * pnStrings )
00252 {
00253 char * pName, * pPar1, * pPar2, * pCur;
00254 int nStrings, CountPars;
00255
00256
00257 for ( pName = pFormula; *pName && *pName == ' '; pName++ );
00258 assert( *pName );
00259
00260 for ( pPar1 = pName; *pPar1 && *pPar1 != '('; pPar1++ );
00261 if ( *pPar1 == 0 )
00262 {
00263 *pnStrings = 0;
00264 return pName;
00265 }
00266
00267 assert( *pPar1 == '(' );
00268 *pPar1 = 0;
00269
00270 for ( CountPars = 1, pPar2 = pPar1 + 1; *pPar2 && CountPars; pPar2++ )
00271 if ( *pPar2 == '(' )
00272 CountPars++;
00273 else if ( *pPar2 == ')' )
00274 CountPars--;
00275 pPar2--;
00276 assert( CountPars == 0 );
00277
00278 assert( *pPar2 == ')' );
00279 *pPar2 = 0;
00280
00281 nStrings = 0;
00282 pCur = pPar1 + 1;
00283 while ( 1 )
00284 {
00285
00286 pStrings[ nStrings++ ] = pCur;
00287
00288 for ( CountPars = 0; *pCur && (CountPars || *pCur != ','); pCur++ )
00289 if ( *pCur == '(' )
00290 CountPars++;
00291 else if ( *pCur == ')' )
00292 CountPars--;
00293 if ( *pCur == 0 )
00294 break;
00295 assert( *pCur == ',' );
00296 *pCur = 0;
00297 pCur++;
00298 }
00299
00300 *pnStrings = nStrings;
00301 return pName;
00302 }
00303
00304
00316 int Map_LibraryTruthVerify( Map_SuperLib_t * pLib, Map_Super_t * pGate )
00317 {
00318 unsigned uTruthRes[2];
00319 Map_LibraryComputeTruth( pLib, pGate->pFormula, uTruthRes );
00320 if ( uTruthRes[0] != pGate->uTruth[0] || uTruthRes[1] != pGate->uTruth[1] )
00321 return 0;
00322 return 1;
00323 }
00324
00325
00339 void Map_LibraryComputeTruth( Map_SuperLib_t * pLib, char * pFormula, unsigned uTruthRes[] )
00340 {
00341 char Buffer[1000];
00342 strcpy( Buffer, pFormula );
00343 Map_LibraryComputeTruth_rec( pLib, Buffer, pLib->uTruths, uTruthRes );
00344 }
00345
00359 void Map_LibraryComputeTruth_rec( Map_SuperLib_t * pLib, char * pFormula, unsigned uTruthsIn[][2], unsigned uTruthRes[] )
00360 {
00361 Mio_Gate_t * pMioGate;
00362 char * pGateName, * pStrings[6];
00363 unsigned uTruthsFanins[6][2];
00364 int nStrings, i;
00365
00366
00367
00368 pGateName = Map_LibraryReadFormulaStep( pFormula, pStrings, &nStrings );
00369 if ( nStrings == 0 )
00370 {
00371 assert( pGateName[0] - 'a' < pLib->nVarsMax );
00372 uTruthRes[0] = uTruthsIn[pGateName[0] - 'a'][0];
00373 uTruthRes[1] = uTruthsIn[pGateName[0] - 'a'][1];
00374 return;
00375 }
00376
00377 for ( i = 0; i < nStrings; i++ )
00378 Map_LibraryComputeTruth_rec( pLib, pStrings[i], uTruthsIn, uTruthsFanins[i] );
00379
00380 pMioGate = Mio_LibraryReadGateByName( pLib->pGenlib, pGateName );
00381 if ( pMioGate == NULL )
00382 printf( "A supergate contains gate \"%s\" that is not in \"%s\".\n", pGateName, Mio_LibraryReadName(pLib->pGenlib) );
00383
00384 Mio_DeriveTruthTable( pMioGate, uTruthsFanins, nStrings, pLib->nVarsMax, uTruthRes );
00385 }
00386
00398 void Map_LibraryPrintSupergate( Map_Super_t * pGate )
00399 {
00400 printf( "%5d : ", pGate->nUsed );
00401 printf( "%5d ", pGate->Num );
00402 printf( "A = %5.2f ", pGate->Area );
00403 printf( "D = %5.2f ", pGate->tDelayMax );
00404 printf( "%s", pGate->pFormula );
00405 printf( "\n" );
00406 }
00407
00408
00420 void Map_LibraryPrintClasses( Map_SuperLib_t * p )
00421 {
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 }
00444
00448
00449