00001
00021 #include "abc.h"
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00037
00041
00053 char * Abc_SopRegister( Extra_MmFlex_t * pMan, char * pName )
00054 {
00055 char * pRegName;
00056 if ( pName == NULL ) return NULL;
00057 pRegName = Extra_MmFlexEntryFetch( pMan, strlen(pName) + 1 );
00058 strcpy( pRegName, pName );
00059 return pRegName;
00060 }
00061
00073 char * Abc_SopStart( Extra_MmFlex_t * pMan, int nCubes, int nVars )
00074 {
00075 char * pSopCover, * pCube;
00076 int i, Length;
00077
00078 Length = nCubes * (nVars + 3);
00079 pSopCover = Extra_MmFlexEntryFetch( pMan, Length + 1 );
00080 memset( pSopCover, '-', Length );
00081 pSopCover[Length] = 0;
00082
00083 for ( i = 0; i < nCubes; i++ )
00084 {
00085 pCube = pSopCover + i * (nVars + 3);
00086 pCube[nVars + 0] = ' ';
00087 pCube[nVars + 1] = '1';
00088 pCube[nVars + 2] = '\n';
00089 }
00090 return pSopCover;
00091 }
00092
00104 char * Abc_SopCreateConst1( Extra_MmFlex_t * pMan )
00105 {
00106 return Abc_SopRegister( pMan, " 1\n" );
00107 }
00108
00120 char * Abc_SopCreateConst0( Extra_MmFlex_t * pMan )
00121 {
00122 return Abc_SopRegister( pMan, " 0\n" );
00123 }
00124
00136 char * Abc_SopCreateAnd2( Extra_MmFlex_t * pMan, int fCompl0, int fCompl1 )
00137 {
00138 char Buffer[6];
00139 Buffer[0] = '1' - fCompl0;
00140 Buffer[1] = '1' - fCompl1;
00141 Buffer[2] = ' ';
00142 Buffer[3] = '1';
00143 Buffer[4] = '\n';
00144 Buffer[5] = 0;
00145 return Abc_SopRegister( pMan, Buffer );
00146 }
00147
00159 char * Abc_SopCreateAnd( Extra_MmFlex_t * pMan, int nVars, int * pfCompl )
00160 {
00161 char * pSop;
00162 int i;
00163 pSop = Abc_SopStart( pMan, 1, nVars );
00164 for ( i = 0; i < nVars; i++ )
00165 pSop[i] = '1' - (pfCompl? pfCompl[i] : 0);
00166 pSop[nVars + 1] = '1';
00167 return pSop;
00168 }
00169
00181 char * Abc_SopCreateNand( Extra_MmFlex_t * pMan, int nVars )
00182 {
00183 char * pSop;
00184 int i;
00185 pSop = Abc_SopStart( pMan, 1, nVars );
00186 for ( i = 0; i < nVars; i++ )
00187 pSop[i] = '1';
00188 pSop[nVars + 1] = '0';
00189 return pSop;
00190 }
00191
00203 char * Abc_SopCreateOr( Extra_MmFlex_t * pMan, int nVars, int * pfCompl )
00204 {
00205 char * pSop;
00206 int i;
00207 pSop = Abc_SopStart( pMan, 1, nVars );
00208 for ( i = 0; i < nVars; i++ )
00209 pSop[i] = '0' + (pfCompl? pfCompl[i] : 0);
00210 pSop[nVars + 1] = '0';
00211 return pSop;
00212 }
00213
00225 char * Abc_SopCreateOrMultiCube( Extra_MmFlex_t * pMan, int nVars, int * pfCompl )
00226 {
00227 char * pSop, * pCube;
00228 int i;
00229 pSop = Abc_SopStart( pMan, nVars, nVars );
00230 i = 0;
00231 Abc_SopForEachCube( pSop, nVars, pCube )
00232 {
00233 pCube[i] = '1' - (pfCompl? pfCompl[i] : 0);
00234 i++;
00235 }
00236 return pSop;
00237 }
00238
00250 char * Abc_SopCreateNor( Extra_MmFlex_t * pMan, int nVars )
00251 {
00252 char * pSop;
00253 int i;
00254 pSop = Abc_SopStart( pMan, 1, nVars );
00255 for ( i = 0; i < nVars; i++ )
00256 pSop[i] = '0';
00257 return pSop;
00258 }
00259
00271 char * Abc_SopCreateXor( Extra_MmFlex_t * pMan, int nVars )
00272 {
00273 assert( nVars == 2 );
00274 return Abc_SopRegister(pMan, "01 1\n10 1\n");
00275 }
00276
00288 char * Abc_SopCreateXorSpecial( Extra_MmFlex_t * pMan, int nVars )
00289 {
00290 char * pSop;
00291 pSop = Abc_SopCreateAnd( pMan, nVars, NULL );
00292 pSop[nVars+1] = 'x';
00293 assert( pSop[nVars+2] == '\n' );
00294 return pSop;
00295 }
00296
00308 char * Abc_SopCreateNxor( Extra_MmFlex_t * pMan, int nVars )
00309 {
00310 assert( nVars == 2 );
00311 return Abc_SopRegister(pMan, "11 1\n00 1\n");
00312 }
00313
00326 char * Abc_SopCreateMux( Extra_MmFlex_t * pMan )
00327 {
00328 return Abc_SopRegister(pMan, "11- 1\n0-1 1\n");
00329 }
00330
00342 char * Abc_SopCreateInv( Extra_MmFlex_t * pMan )
00343 {
00344 return Abc_SopRegister(pMan, "0 1\n");
00345 }
00346
00358 char * Abc_SopCreateBuf( Extra_MmFlex_t * pMan )
00359 {
00360 return Abc_SopRegister(pMan, "1 1\n");
00361 }
00362
00374 char * Abc_SopCreateFromTruth( Extra_MmFlex_t * pMan, int nVars, unsigned * pTruth )
00375 {
00376 char * pSop, * pCube;
00377 int nMints, Counter, i, k;
00378
00379 Counter = 0;
00380 nMints = (1 << nVars);
00381 for ( i = 0; i < nMints; i++ )
00382 Counter += ((pTruth[i>>5] & (1 << (i&31))) > 0);
00383
00384 assert( Counter > 0 );
00385 if ( Counter == 0 )
00386 return NULL;
00387
00388 pSop = Abc_SopStart( pMan, Counter, nVars );
00389
00390 Counter = 0;
00391 for ( i = 0; i < nMints; i++ )
00392 if ( (pTruth[i>>5] & (1 << (i&31))) > 0 )
00393 {
00394 pCube = pSop + Counter * (nVars + 3);
00395 for ( k = 0; k < nVars; k++ )
00396 pCube[k] = '0' + ((i & (1 << k)) > 0);
00397 Counter++;
00398 }
00399 return pSop;
00400 }
00401
00413 char * Abc_SopCreateFromIsop( Extra_MmFlex_t * pMan, int nVars, Vec_Int_t * vCover )
00414 {
00415 char * pSop, * pCube;
00416 int i, k, Entry, Literal;
00417 assert( Vec_IntSize(vCover) > 0 );
00418 if ( Vec_IntSize(vCover) == 0 )
00419 return NULL;
00420
00421 pSop = Abc_SopStart( pMan, Vec_IntSize(vCover), nVars );
00422
00423 Vec_IntForEachEntry( vCover, Entry, i )
00424 {
00425 pCube = pSop + i * (nVars + 3);
00426 for ( k = 0; k < nVars; k++ )
00427 {
00428 Literal = 3 & (Entry >> (k << 1));
00429 if ( Literal == 1 )
00430 pCube[k] = '0';
00431 else if ( Literal == 2 )
00432 pCube[k] = '1';
00433 else if ( Literal != 0 )
00434 assert( 0 );
00435 }
00436 }
00437 return pSop;
00438 }
00439
00451 int Abc_SopGetCubeNum( char * pSop )
00452 {
00453 char * pCur;
00454 int nCubes = 0;
00455 if ( pSop == NULL )
00456 return 0;
00457 for ( pCur = pSop; *pCur; pCur++ )
00458 nCubes += (*pCur == '\n');
00459 return nCubes;
00460 }
00461
00473 int Abc_SopGetLitNum( char * pSop )
00474 {
00475 char * pCur;
00476 int nLits = 0;
00477 if ( pSop == NULL )
00478 return 0;
00479 for ( pCur = pSop; *pCur; pCur++ )
00480 {
00481 nLits -= (*pCur == '\n');
00482 nLits += (*pCur == '0' || *pCur == '1');
00483 }
00484 return nLits;
00485 }
00486
00498 int Abc_SopGetVarNum( char * pSop )
00499 {
00500 char * pCur;
00501 for ( pCur = pSop; *pCur != '\n'; pCur++ );
00502 return pCur - pSop - 2;
00503 }
00504
00516 int Abc_SopGetPhase( char * pSop )
00517 {
00518 int nVars = Abc_SopGetVarNum( pSop );
00519 if ( pSop[nVars+1] == '0' || pSop[nVars+1] == 'n' )
00520 return 0;
00521 if ( pSop[nVars+1] == '1' || pSop[nVars+1] == 'x' )
00522 return 1;
00523 assert( 0 );
00524 return -1;
00525 }
00526
00538 int Abc_SopGetIthCareLit( char * pSop, int i )
00539 {
00540 char * pCube;
00541 int nVars;
00542 nVars = Abc_SopGetVarNum( pSop );
00543 Abc_SopForEachCube( pSop, nVars, pCube )
00544 if ( pCube[i] != '-' )
00545 return pCube[i] - '0';
00546 return -1;
00547 }
00548
00560 void Abc_SopComplement( char * pSop )
00561 {
00562 char * pCur;
00563 for ( pCur = pSop; *pCur; pCur++ )
00564 if ( *pCur == '\n' )
00565 {
00566 if ( *(pCur - 1) == '0' )
00567 *(pCur - 1) = '1';
00568 else if ( *(pCur - 1) == '1' )
00569 *(pCur - 1) = '0';
00570 else if ( *(pCur - 1) == 'x' )
00571 *(pCur - 1) = 'n';
00572 else if ( *(pCur - 1) == 'n' )
00573 *(pCur - 1) = 'x';
00574 else
00575 assert( 0 );
00576 }
00577 }
00578
00590 bool Abc_SopIsComplement( char * pSop )
00591 {
00592 char * pCur;
00593 for ( pCur = pSop; *pCur; pCur++ )
00594 if ( *pCur == '\n' )
00595 return (int)(*(pCur - 1) == '0' || *(pCur - 1) == 'n');
00596 assert( 0 );
00597 return 0;
00598 }
00599
00611 bool Abc_SopIsConst0( char * pSop )
00612 {
00613 return pSop[0] == ' ' && pSop[1] == '0';
00614 }
00615
00627 bool Abc_SopIsConst1( char * pSop )
00628 {
00629 return pSop[0] == ' ' && pSop[1] == '1';
00630 }
00631
00643 bool Abc_SopIsBuf( char * pSop )
00644 {
00645 if ( pSop[4] != 0 )
00646 return 0;
00647 if ( (pSop[0] == '1' && pSop[2] == '1') || (pSop[0] == '0' && pSop[2] == '0') )
00648 return 1;
00649 return 0;
00650 }
00651
00663 bool Abc_SopIsInv( char * pSop )
00664 {
00665 if ( pSop[4] != 0 )
00666 return 0;
00667 if ( (pSop[0] == '0' && pSop[2] == '1') || (pSop[0] == '1' && pSop[2] == '0') )
00668 return 1;
00669 return 0;
00670 }
00671
00683 bool Abc_SopIsAndType( char * pSop )
00684 {
00685 char * pCur;
00686 if ( Abc_SopGetCubeNum(pSop) != 1 )
00687 return 0;
00688 for ( pCur = pSop; *pCur != ' '; pCur++ )
00689 if ( *pCur == '-' )
00690 return 0;
00691 if ( pCur[1] != '1' )
00692 return 0;
00693 return 1;
00694 }
00695
00707 bool Abc_SopIsOrType( char * pSop )
00708 {
00709 char * pCube, * pCur;
00710 int nVars, nLits;
00711 nVars = Abc_SopGetVarNum( pSop );
00712 if ( nVars != Abc_SopGetCubeNum(pSop) )
00713 return 0;
00714 Abc_SopForEachCube( pSop, nVars, pCube )
00715 {
00716
00717 nLits = 0;
00718 for ( pCur = pCube; *pCur != ' '; pCur++ )
00719 nLits += ( *pCur != '-' );
00720 if ( nLits != 1 )
00721 return 0;
00722 }
00723 return 1;
00724 }
00725
00737 int Abc_SopIsExorType( char * pSop )
00738 {
00739 char * pCur;
00740 for ( pCur = pSop; *pCur; pCur++ )
00741 if ( *pCur == '\n' )
00742 return (int)(*(pCur - 1) == 'x' || *(pCur - 1) == 'n');
00743 assert( 0 );
00744 return 0;
00745 }
00746
00758 bool Abc_SopCheck( char * pSop, int nFanins )
00759 {
00760 char * pCubes, * pCubesOld;
00761 int fFound0 = 0, fFound1 = 0;
00762
00763
00764 for ( pCubes = pSop; *pCubes; pCubes++ )
00765 {
00766
00767 for ( pCubesOld = pCubes; *pCubes != ' '; pCubes++ );
00768
00769 if ( pCubes - pCubesOld != nFanins )
00770 {
00771 fprintf( stdout, "Abc_SopCheck: SOP has a mismatch between its cover size (%d) and its fanin number (%d).\n",
00772 pCubes - pCubesOld, nFanins );
00773 return 0;
00774 }
00775
00776 pCubes++;
00777 if ( *pCubes == '0' )
00778 fFound0 = 1;
00779 else if ( *pCubes == '1' )
00780 fFound1 = 1;
00781 else if ( *pCubes != 'x' && *pCubes != 'n' )
00782 {
00783 fprintf( stdout, "Abc_SopCheck: SOP has a strange character (%c) in the output part of its cube.\n", *pCubes );
00784 return 0;
00785 }
00786
00787 pCubes++;
00788 if ( *pCubes != '\n' )
00789 {
00790 fprintf( stdout, "Abc_SopCheck: SOP has a cube without new line in the end.\n" );
00791 return 0;
00792 }
00793 }
00794 if ( fFound0 && fFound1 )
00795 {
00796 fprintf( stdout, "Abc_SopCheck: SOP has cubes in both phases.\n" );
00797 return 0;
00798 }
00799 return 1;
00800 }
00801
00802
00814 char * Abc_SopFromTruthBin( char * pTruth )
00815 {
00816 char * pSopCover, * pCube;
00817 int nTruthSize, nVars, Digit, Length, Mint, i, b;
00818 Vec_Int_t * vMints;
00819
00820
00821 nTruthSize = strlen(pTruth);
00822 nVars = Extra_Base2Log( nTruthSize );
00823 if ( nTruthSize != (1 << (nVars)) )
00824 {
00825 printf( "String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
00826 return NULL;
00827 }
00828
00829
00830 vMints = Vec_IntAlloc( 100 );
00831 for ( i = 0; i < nTruthSize; i++ )
00832 {
00833 if ( pTruth[i] >= '0' && pTruth[i] <= '1' )
00834 Digit = pTruth[i] - '0';
00835 else
00836 {
00837 printf( "String %s does not look like a binary representation of the truth table.\n", pTruth );
00838 return NULL;
00839 }
00840 if ( Digit == 1 )
00841 Vec_IntPush( vMints, nTruthSize - 1 - i );
00842 }
00843 if ( Vec_IntSize( vMints ) == 0 || Vec_IntSize( vMints ) == nTruthSize )
00844 {
00845 Vec_IntFree( vMints );
00846 printf( "Cannot create constant function.\n" );
00847 return NULL;
00848 }
00849
00850
00851 Length = Vec_IntSize(vMints) * (nVars + 3);
00852 pSopCover = ALLOC( char, Length + 1 );
00853 pSopCover[Length] = 0;
00854 Vec_IntForEachEntry( vMints, Mint, i )
00855 {
00856 pCube = pSopCover + i * (nVars + 3);
00857 for ( b = 0; b < nVars; b++ )
00858 if ( Mint & (1 << (nVars-1-b)) )
00859
00860 pCube[b] = '1';
00861 else
00862 pCube[b] = '0';
00863 pCube[nVars + 0] = ' ';
00864 pCube[nVars + 1] = '1';
00865 pCube[nVars + 2] = '\n';
00866 }
00867 Vec_IntFree( vMints );
00868 return pSopCover;
00869 }
00870
00882 char * Abc_SopFromTruthHex( char * pTruth )
00883 {
00884 char * pSopCover, * pCube;
00885 int nTruthSize, nVars, Digit, Length, Mint, i, b;
00886 Vec_Int_t * vMints;
00887
00888
00889 nTruthSize = strlen(pTruth);
00890 nVars = Extra_Base2Log( nTruthSize ) + 2;
00891 if ( nTruthSize != (1 << (nVars-2)) )
00892 {
00893 printf( "String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
00894 return NULL;
00895 }
00896
00897
00898 vMints = Vec_IntAlloc( 100 );
00899 for ( i = 0; i < nTruthSize; i++ )
00900 {
00901 if ( pTruth[i] >= '0' && pTruth[i] <= '9' )
00902 Digit = pTruth[i] - '0';
00903 else if ( pTruth[i] >= 'a' && pTruth[i] <= 'f' )
00904 Digit = 10 + pTruth[i] - 'a';
00905 else if ( pTruth[i] >= 'A' && pTruth[i] <= 'F' )
00906 Digit = 10 + pTruth[i] - 'A';
00907 else
00908 {
00909 printf( "String %s does not look like a hexadecimal representation of the truth table.\n", pTruth );
00910 return NULL;
00911 }
00912 for ( b = 0; b < 4; b++ )
00913 if ( Digit & (1 << b) )
00914 Vec_IntPush( vMints, 4*(nTruthSize-1-i)+b );
00915 }
00916
00917
00918 Length = Vec_IntSize(vMints) * (nVars + 3);
00919 pSopCover = ALLOC( char, Length + 1 );
00920 pSopCover[Length] = 0;
00921 Vec_IntForEachEntry( vMints, Mint, i )
00922 {
00923 pCube = pSopCover + i * (nVars + 3);
00924 for ( b = 0; b < nVars; b++ )
00925
00926 if ( Mint & (1 << b) )
00927 pCube[b] = '1';
00928 else
00929 pCube[b] = '0';
00930 pCube[nVars + 0] = ' ';
00931 pCube[nVars + 1] = '1';
00932 pCube[nVars + 2] = '\n';
00933 }
00934 Vec_IntFree( vMints );
00935 return pSopCover;
00936 }
00937
00949 char * Abc_SopEncoderPos( Extra_MmFlex_t * pMan, int iValue, int nValues )
00950 {
00951 char Buffer[32];
00952 assert( iValue < nValues );
00953 sprintf( Buffer, "d0\n%d 1\n", iValue );
00954 return Abc_SopRegister( pMan, Buffer );
00955 }
00956
00968 char * Abc_SopEncoderLog( Extra_MmFlex_t * pMan, int iBit, int nValues )
00969 {
00970 char * pResult;
00971 Vec_Str_t * vSop;
00972 int v, Counter, fFirst = 1, nBits = Extra_Base2Log(nValues);
00973 assert( iBit < nBits );
00974
00975 Counter = 0;
00976 for ( v = 0; v < nValues; v++ )
00977 Counter += ( (v & (1 << iBit)) > 0 );
00978
00979 vSop = Vec_StrAlloc( 100 );
00980 Vec_StrPrintStr( vSop, "d0\n" );
00981 if ( Counter > 1 )
00982 Vec_StrPrintStr( vSop, "(" );
00983 for ( v = 0; v < nValues; v++ )
00984 if ( v & (1 << iBit) )
00985 {
00986 if ( fFirst )
00987 fFirst = 0;
00988 else
00989 Vec_StrPush( vSop, ',' );
00990 Vec_StrPrintNum( vSop, v );
00991 }
00992 if ( Counter > 1 )
00993 Vec_StrPrintStr( vSop, ")" );
00994 Vec_StrPrintStr( vSop, " 1\n" );
00995 Vec_StrPush( vSop, 0 );
00996 pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
00997 Vec_StrFree( vSop );
00998 return pResult;
00999 }
01000
01012 char * Abc_SopDecoderPos( Extra_MmFlex_t * pMan, int nValues )
01013 {
01014 char * pResult;
01015 Vec_Str_t * vSop;
01016 int i, k;
01017 assert( nValues > 1 );
01018 vSop = Vec_StrAlloc( 100 );
01019 for ( i = 0; i < nValues; i++ )
01020 {
01021 for ( k = 0; k < nValues; k++ )
01022 {
01023 if ( k == i )
01024 Vec_StrPrintStr( vSop, "1 " );
01025 else
01026 Vec_StrPrintStr( vSop, "- " );
01027 }
01028 Vec_StrPrintNum( vSop, i );
01029 Vec_StrPush( vSop, '\n' );
01030 }
01031 Vec_StrPush( vSop, 0 );
01032 pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
01033 Vec_StrFree( vSop );
01034 return pResult;
01035 }
01036
01048 char * Abc_SopDecoderLog( Extra_MmFlex_t * pMan, int nValues )
01049 {
01050 char * pResult;
01051 Vec_Str_t * vSop;
01052 int i, b, nBits = Extra_Base2Log(nValues);
01053 assert( nValues > 1 && nValues <= (1<<nBits) );
01054 vSop = Vec_StrAlloc( 100 );
01055 for ( i = 0; i < nValues; i++ )
01056 {
01057 for ( b = 0; b < nBits; b++ )
01058 {
01059 Vec_StrPrintNum( vSop, (int)((i & (1 << b)) > 0) );
01060 Vec_StrPush( vSop, ' ' );
01061 }
01062 Vec_StrPrintNum( vSop, i );
01063 Vec_StrPush( vSop, '\n' );
01064 }
01065 Vec_StrPush( vSop, 0 );
01066 pResult = Abc_SopRegister( pMan, Vec_StrArray(vSop) );
01067 Vec_StrFree( vSop );
01068 return pResult;
01069 }
01070
01074
01075