00001
00021 #include "io.h"
00022
00026
00030
00042 char * Io_ReadDsdFindEnd( char * pCur )
00043 {
00044 char * pEnd;
00045 int nParts = 0;
00046 assert( *pCur == '(' );
00047 for ( pEnd = pCur; *pEnd; pEnd++ )
00048 {
00049 if ( *pEnd == '(' )
00050 nParts++;
00051 else if ( *pEnd == ')' )
00052 nParts--;
00053 if ( nParts == 0 )
00054 return pEnd;
00055 }
00056 return NULL;
00057 }
00058
00070 int Io_ReadDsdStrSplit( char * pCur, char * pParts[], int * pTypeXor )
00071 {
00072 int fAnd = 0, fXor = 0, fPri = 0, nParts = 0;
00073 assert( *pCur );
00074
00075 while ( 1 )
00076 {
00077
00078 pParts[nParts++] = pCur;
00079
00080 if ( *pCur == '!' )
00081 pCur++;
00082
00083 if ( *pCur >= 'a' && *pCur <= 'z' )
00084 pCur++;
00085 else
00086 {
00087
00088 while ( (*pCur >= '0' && *pCur <= '9') || (*pCur >= 'A' && *pCur <= 'F') )
00089 pCur++;
00090
00091 if ( *pCur != '(' )
00092 {
00093 printf( "Cannot find the opening paranthesis.\n" );
00094 break;
00095 }
00096
00097 pCur = Io_ReadDsdFindEnd( pCur );
00098 if ( pCur == NULL )
00099 {
00100 printf( "Cannot find the closing paranthesis.\n" );
00101 break;
00102 }
00103 pCur++;
00104 }
00105
00106 if ( *pCur == 0 )
00107 break;
00108
00109 if ( *pCur != '*' && *pCur != '+' && *pCur != ',' )
00110 {
00111 printf( "Wrong separating symbol.\n" );
00112 break;
00113 }
00114
00115 fAnd |= (*pCur == '*');
00116 fXor |= (*pCur == '+');
00117 fPri |= (*pCur == ',');
00118 *pCur++ = 0;
00119 }
00120
00121 if ( fAnd + fXor + fPri > 1 )
00122 {
00123 printf( "Different types of separating symbol ennPartsed.\n" );
00124 return 0;
00125 }
00126 *pTypeXor = fXor;
00127 return nParts;
00128 }
00129
00141 Abc_Obj_t * Io_ReadDsd_rec( Abc_Ntk_t * pNtk, char * pCur, char * pSop )
00142 {
00143 Abc_Obj_t * pObj, * pFanin;
00144 char * pEnd, * pParts[32];
00145 int i, nParts, TypeExor;
00146
00147
00148 if ( *pCur == '!' )
00149 {
00150 pObj = Io_ReadDsd_rec( pNtk, pCur + 1, NULL );
00151 return Abc_NtkCreateNodeInv( pNtk, pObj );
00152 }
00153 if ( *pCur == '(' )
00154 {
00155 assert( pCur[strlen(pCur)-1] == ')' );
00156 pCur[strlen(pCur)-1] = 0;
00157 nParts = Io_ReadDsdStrSplit( pCur+1, pParts, &TypeExor );
00158 if ( nParts == 0 )
00159 {
00160 Abc_NtkDelete( pNtk );
00161 return NULL;
00162 }
00163 pObj = Abc_NtkCreateNode( pNtk );
00164 if ( pSop )
00165 {
00166
00167 for ( i = 0; i < nParts; i++ )
00168 {
00169 pFanin = Io_ReadDsd_rec( pNtk, pParts[i], NULL );
00170 if ( pFanin == NULL )
00171 return NULL;
00172 Abc_ObjAddFanin( pObj, pFanin );
00173 }
00174 }
00175 else
00176 {
00177 for ( i = 0; i < nParts; i++ )
00178 {
00179 pFanin = Io_ReadDsd_rec( pNtk, pParts[i], NULL );
00180 if ( pFanin == NULL )
00181 return NULL;
00182 Abc_ObjAddFanin( pObj, pFanin );
00183 }
00184 }
00185 if ( pSop )
00186 pObj->pData = Abc_SopRegister( pNtk->pManFunc, pSop );
00187 else if ( TypeExor )
00188 pObj->pData = Abc_SopCreateXorSpecial( pNtk->pManFunc, nParts );
00189 else
00190 pObj->pData = Abc_SopCreateAnd( pNtk->pManFunc, nParts, NULL );
00191 return pObj;
00192 }
00193 if ( *pCur >= 'a' && *pCur <= 'z' )
00194 {
00195 assert( *(pCur+1) == 0 );
00196 return Abc_NtkPi( pNtk, *pCur - 'a' );
00197 }
00198
00199
00200 pEnd = pCur;
00201 while ( (*pEnd >= '0' && *pEnd <= '9') || (*pEnd >= 'A' && *pEnd <= 'F') )
00202 pEnd++;
00203 if ( *pEnd != '(' )
00204 {
00205 printf( "Cannot find the end of hexidecimal truth table.\n" );
00206 return NULL;
00207 }
00208
00209
00210 *pEnd = 0;
00211 pSop = Abc_SopFromTruthHex( pCur );
00212 *pEnd = '(';
00213 pObj = Io_ReadDsd_rec( pNtk, pEnd, pSop );
00214 free( pSop );
00215 return pObj;
00216 }
00217
00229 Abc_Ntk_t * Io_ReadDsd( char * pForm )
00230 {
00231 Abc_Ntk_t * pNtk;
00232 Abc_Obj_t * pObj, * pTop;
00233 Vec_Ptr_t * vNames;
00234 char * pCur, * pFormCopy;
00235 int i, nInputs;
00236
00237
00238 nInputs = 0;
00239 for ( pCur = pForm; *pCur; pCur++ )
00240 if ( *pCur >= 'a' && *pCur <= 'z' )
00241 nInputs = ABC_MAX( nInputs, *pCur - 'a' );
00242 nInputs++;
00243
00244
00245 pNtk = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 );
00246 pNtk->pName = Extra_UtilStrsav( "dsd" );
00247
00248
00249 vNames = Abc_NodeGetFakeNames( nInputs );
00250 for ( i = 0; i < nInputs; i++ )
00251 Abc_ObjAssignName( Abc_NtkCreatePi(pNtk), Vec_PtrEntry(vNames, i), NULL );
00252 Abc_NodeFreeNames( vNames );
00253
00254
00255
00256 pCur = pFormCopy = ALLOC( char, 3 * strlen(pForm) + 10 );
00257 *pCur++ = '(';
00258 for ( ; *pForm; pForm++ )
00259 if ( *pForm == '(' )
00260 {
00261 *pCur++ = '(';
00262 *pCur++ = '(';
00263 }
00264 else if ( *pForm == ')' )
00265 {
00266 *pCur++ = ')';
00267 *pCur++ = ')';
00268 }
00269 else if ( *pForm == ',' )
00270 {
00271 *pCur++ = ')';
00272 *pCur++ = ',';
00273 *pCur++ = '(';
00274 }
00275 else
00276 *pCur++ = *pForm;
00277 *pCur++ = ')';
00278 *pCur = 0;
00279
00280
00281 pObj = Io_ReadDsd_rec( pNtk, pFormCopy, NULL );
00282 free( pFormCopy );
00283 if ( pObj == NULL )
00284 return NULL;
00285
00286
00287 pTop = Abc_NtkCreatePo(pNtk);
00288 Abc_ObjAssignName( pTop, "F", NULL );
00289 Abc_ObjAddFanin( pTop, pObj );
00290
00291
00292 if ( !Abc_NtkCheck( pNtk ) )
00293 {
00294 fprintf( stdout, "Io_ReadDsd(): Network check has failed.\n" );
00295 Abc_NtkDelete( pNtk );
00296 return NULL;
00297 }
00298 return pNtk;
00299 }
00300
00301
00302
00306
00307
00308