From 6345832dba505bc60f78e5d165ca29d3842cbbd6 Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Thu, 3 Feb 2022 18:45:11 -0800 Subject: Improving truth table handling. --- src/base/abc/abc.h | 3 +++ src/base/abc/abcNtk.c | 46 ++++++++++++++++++++++++++++++++++ src/base/abc/abcSop.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/base/io/io.c | 43 ++++++++++++++++++-------------- src/base/wln/wlnRtl.c | 26 +++++++++++++++++++ 5 files changed, 166 insertions(+), 21 deletions(-) diff --git a/src/base/abc/abc.h b/src/base/abc/abc.h index e7f6c0ad..eea66f29 100644 --- a/src/base/abc/abc.h +++ b/src/base/abc/abc.h @@ -784,6 +784,7 @@ extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateMffc( Abc_Ntk_t * pNtk, Abc_Obj_t extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateTarget( Abc_Ntk_t * pNtk, Vec_Ptr_t * vRoots, Vec_Int_t * vValues ); extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateFromNode( Abc_Ntk_t * pNtk, Abc_Obj_t * pNode ); extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateWithNode( char * pSop ); +extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateWithNodes( Vec_Ptr_t * vSops ); extern ABC_DLL void Abc_NtkDelete( Abc_Ntk_t * pNtk ); extern ABC_DLL void Abc_NtkFixNonDrivenNets( Abc_Ntk_t * pNtk ); extern ABC_DLL void Abc_NtkMakeComb( Abc_Ntk_t * pNtk, int fRemoveLatches ); @@ -920,6 +921,8 @@ extern ABC_DLL int Abc_SopIsExorType( char * pSop ); extern ABC_DLL int Abc_SopCheck( char * pSop, int nFanins ); extern ABC_DLL char * Abc_SopFromTruthBin( char * pTruth ); extern ABC_DLL char * Abc_SopFromTruthHex( char * pTruth ); +extern ABC_DLL Vec_Ptr_t * Abc_SopFromTruthsBin( char * pTruth ); +extern ABC_DLL Vec_Ptr_t * Abc_SopFromTruthsHex( char * pTruth ); extern ABC_DLL char * Abc_SopEncoderPos( Mem_Flex_t * pMan, int iValue, int nValues ); extern ABC_DLL char * Abc_SopEncoderLog( Mem_Flex_t * pMan, int iBit, int nValues ); extern ABC_DLL char * Abc_SopDecoderPos( Mem_Flex_t * pMan, int nValues ); diff --git a/src/base/abc/abcNtk.c b/src/base/abc/abcNtk.c index f8e40b41..63353344 100644 --- a/src/base/abc/abcNtk.c +++ b/src/base/abc/abcNtk.c @@ -1270,6 +1270,52 @@ Abc_Ntk_t * Abc_NtkCreateWithNode( char * pSop ) return pNtkNew; } +/**Function************************************************************* + + Synopsis [Creates the network composed of one node with the given SOP.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +Abc_Ntk_t * Abc_NtkCreateWithNodes( Vec_Ptr_t * vSop ) +{ + Abc_Ntk_t * pNtkNew; + Abc_Obj_t * pFanin, * pNode, * pNodePo; + Vec_Ptr_t * vNames; + int i, k, nVars; char Buffer[10]; + char * pSop = (char *)Vec_PtrEntry(vSop, 0); + // start the network + pNtkNew = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 ); + pNtkNew->pName = Extra_UtilStrsav("ex"); + // create PIs + Vec_PtrPush( pNtkNew->vObjs, NULL ); + nVars = Abc_SopGetVarNum( pSop ); + vNames = Abc_NodeGetFakeNames( nVars ); + for ( i = 0; i < nVars; i++ ) + Abc_ObjAssignName( Abc_NtkCreatePi(pNtkNew), (char *)Vec_PtrEntry(vNames, i), NULL ); + Abc_NodeFreeNames( vNames ); + // create the node, add PIs as fanins, set the function + Vec_PtrForEachEntry( char *, vSop, pSop, i ) + { + pNode = Abc_NtkCreateNode( pNtkNew ); + Abc_NtkForEachPi( pNtkNew, pFanin, k ) + Abc_ObjAddFanin( pNode, pFanin ); + pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtkNew->pManFunc, pSop ); + // create the only PO + pNodePo = Abc_NtkCreatePo(pNtkNew); + Abc_ObjAddFanin( pNodePo, pNode ); + sprintf( Buffer, "F%d", i ); + Abc_ObjAssignName( pNodePo, Buffer, NULL ); + } + if ( !Abc_NtkCheck( pNtkNew ) ) + fprintf( stdout, "Abc_NtkCreateWithNode(): Network check has failed.\n" ); + return pNtkNew; +} + /**Function************************************************************* Synopsis [Deletes the Ntk.] diff --git a/src/base/abc/abcSop.c b/src/base/abc/abcSop.c index b91214af..a560a249 100644 --- a/src/base/abc/abcSop.c +++ b/src/base/abc/abcSop.c @@ -907,6 +907,41 @@ int Abc_SopCheck( char * pSop, int nFanins ) return 1; } +/**Function************************************************************* + + Synopsis [] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Abc_SopCheckReadTruth( Vec_Ptr_t * vRes, char * pToken, int fHex ) +{ + char * pBase; int nVars; + int Log2 = Abc_Base2Log( strlen(pToken) ); + if ( (1 << Log2) != (int)strlen(pToken) ) + { + printf( "The truth table length (%d) is not power-of-2.\n", strlen(pToken) ); + Vec_PtrFreeData( vRes ); + Vec_PtrShrink( vRes, 0 ); + return 0; + } + if ( Vec_PtrSize(vRes) == 0 ) + return 1; + pBase = (char *)Vec_PtrEntry( vRes, 0 ); + nVars = Abc_SopGetVarNum( pBase ); + if ( nVars != Log2+2*fHex ) + { + printf( "Truth table #1 has %d vars while truth table #%d has %d vars.\n", nVars, Vec_PtrSize(vRes)+1, Log2+2*fHex ); + Vec_PtrFreeData( vRes ); + Vec_PtrShrink( vRes, 0 ); + return 0; + } + return 1; +} /**Function************************************************************* @@ -964,8 +999,8 @@ char * Abc_SopFromTruthBin( char * pTruth ) { pCube = pSopCover + i * (nVars + 3); for ( b = 0; b < nVars; b++ ) - if ( Mint & (1 << (nVars-1-b)) ) -// if ( Mint & (1 << b) ) +// if ( Mint & (1 << (nVars-1-b)) ) + if ( Mint & (1 << b) ) pCube[b] = '1'; else pCube[b] = '0'; @@ -976,6 +1011,21 @@ char * Abc_SopFromTruthBin( char * pTruth ) Vec_IntFree( vMints ); return pSopCover; } +Vec_Ptr_t * Abc_SopFromTruthsBin( char * pTruth ) +{ + Vec_Ptr_t * vRes = Vec_PtrAlloc( 10 ); + char * pCopy = Abc_UtilStrsav(pTruth); + char * pToken = strtok( pCopy, " \r\n\t|" ); + while ( pToken ) + { + if ( !Abc_SopCheckReadTruth( vRes, pToken, 0 ) ) + break; + Vec_PtrPush( vRes, Abc_SopFromTruthBin(pToken) ); + pToken = strtok( NULL, " \r\n\t|" ); + } + ABC_FREE( pCopy ); + return vRes; +} /**Function************************************************************* @@ -1058,6 +1108,21 @@ char * Abc_SopFromTruthHex( char * pTruth ) Vec_IntFree( vMints ); return pSopCover; } +Vec_Ptr_t * Abc_SopFromTruthsHex( char * pTruth ) +{ + Vec_Ptr_t * vRes = Vec_PtrAlloc( 10 ); + char * pCopy = Abc_UtilStrsav(pTruth); + char * pToken = strtok( pCopy, " \r\n\t|" ); + while ( pToken ) + { + if ( !Abc_SopCheckReadTruth( vRes, pToken, 1 ) ) + break; + Vec_PtrPush( vRes, Abc_SopFromTruthHex(pToken) ); + pToken = strtok( NULL, " \r\n\t|" ); + } + ABC_FREE( pCopy ); + return vRes; +} /**Function************************************************************* diff --git a/src/base/io/io.c b/src/base/io/io.c index 5cf74ef9..72fd25bf 100644 --- a/src/base/io/io.c +++ b/src/base/io/io.c @@ -1115,7 +1115,7 @@ int IoCommandReadTruth( Abc_Frame_t * pAbc, int argc, char ** argv ) { Abc_Ntk_t * pNtk; char * pStr = NULL; - char * pSopCover; + Vec_Ptr_t * vSops; int fHex = 1; int fFile = 0; int c; @@ -1145,25 +1145,23 @@ int IoCommandReadTruth( Abc_Frame_t * pAbc, int argc, char ** argv ) pStr = Extra_FileReadContents( argv[globalUtilOptind] ); else pStr = argv[globalUtilOptind]; - while ( pStr[ strlen(pStr) - 1 ] == '\n' || pStr[ strlen(pStr) - 1 ] == '\r' ) - pStr[ strlen(pStr) - 1 ] = '\0'; // convert truth table to SOP if ( fHex ) - pSopCover = Abc_SopFromTruthHex(pStr); + vSops = Abc_SopFromTruthsHex(pStr); else - pSopCover = Abc_SopFromTruthBin(pStr); + vSops = Abc_SopFromTruthsBin(pStr); if ( fFile ) ABC_FREE( pStr ); - if ( pSopCover == NULL || pSopCover[0] == 0 ) + if ( Vec_PtrSize(vSops) == 0 ) { - ABC_FREE( pSopCover ); + Vec_PtrFreeFree( vSops ); fprintf( pAbc->Err, "Reading truth table has failed.\n" ); return 1; } - pNtk = Abc_NtkCreateWithNode( pSopCover ); - ABC_FREE( pSopCover ); + pNtk = Abc_NtkCreateWithNodes( vSops ); + Vec_PtrFreeFree( vSops ); if ( pNtk == NULL ) { fprintf( pAbc->Err, "Deriving the network has failed.\n" ); @@ -1176,9 +1174,9 @@ int IoCommandReadTruth( Abc_Frame_t * pAbc, int argc, char ** argv ) usage: fprintf( pAbc->Err, "usage: read_truth [-xfh] \n" ); - fprintf( pAbc->Err, "\t creates network with node having given truth table\n" ); - fprintf( pAbc->Err, "\t-x : toggles between bin and hex representation [default = %s]\n", fHex? "hex":"bin" ); - fprintf( pAbc->Err, "\t-f : toggles reading truth table from file [default = %s]\n", fFile? "yes": "no" ); + fprintf( pAbc->Err, "\t creates network with node(s) having given truth table(s)\n" ); + fprintf( pAbc->Err, "\t-x : toggles between bin and hex notation [default = %s]\n", fHex? "hex":"bin" ); + fprintf( pAbc->Err, "\t-f : toggles reading truth table(s) from file [default = %s]\n", fFile? "yes": "no" ); fprintf( pAbc->Err, "\t-h : prints the command summary\n" ); fprintf( pAbc->Err, "\ttruth : truth table with most signficant bit first (e.g. 1000 for AND(a,b))\n" ); fprintf( pAbc->Err, "\tfile : file name with the truth table\n" ); @@ -3250,19 +3248,23 @@ int IoCommandWriteTruths( Abc_Frame_t * pAbc, int argc, char **argv ) word * pTruth; int nBytes; int fReverse = 0; - int fBinary = 0; + int fHex = 1; + int fBinaryFile = 0; int c, i; Extra_UtilGetoptReset(); - while ( ( c = Extra_UtilGetopt( argc, argv, "rbh" ) ) != EOF ) + while ( ( c = Extra_UtilGetopt( argc, argv, "rxbh" ) ) != EOF ) { switch ( c ) { case 'r': fReverse ^= 1; break; + case 'x': + fHex ^= 1; + break; case 'b': - fBinary ^= 1; + fBinaryFile ^= 1; break; case 'h': goto usage; @@ -3300,19 +3302,22 @@ int IoCommandWriteTruths( Abc_Frame_t * pAbc, int argc, char **argv ) Gia_ManForEachCo( pAbc->pGia, pObj, i ) { pTruth = Gia_ObjComputeTruthTable( pAbc->pGia, pObj ); - if ( fBinary ) + if ( fBinaryFile ) fwrite( pTruth, nBytes, 1, pFile ); - else + else if ( fHex ) Extra_PrintHex( pFile, (unsigned *)pTruth, Gia_ManPiNum(pAbc->pGia) ), fprintf( pFile, "\n" ); + else + Extra_PrintBinary( pFile, (unsigned *)pTruth, 1 << Gia_ManPiNum(pAbc->pGia) ), fprintf( pFile, "\n" ); } fclose( pFile ); return 0; usage: - fprintf( pAbc->Err, "usage: &write_truths [-rbh] \n" ); + fprintf( pAbc->Err, "usage: &write_truths [-rxbh] \n" ); fprintf( pAbc->Err, "\t writes truth tables of each PO of GIA manager into a file\n" ); fprintf( pAbc->Err, "\t-r : toggle reversing bits in the truth table [default = %s]\n", fReverse? "yes":"no" ); - fprintf( pAbc->Err, "\t-b : toggle using binary format [default = %s]\n", fBinary? "yes":"no" ); + fprintf( pAbc->Err, "\t-x : toggle writing in the hex notation [default = %s]\n", fHex? "yes":"no" ); + fprintf( pAbc->Err, "\t-b : toggle using binary file format [default = %s]\n", fBinaryFile? "yes":"no" ); fprintf( pAbc->Err, "\t-h : print the help massage\n" ); fprintf( pAbc->Err, "\tfile : the name of the file to write\n" ); return 1; diff --git a/src/base/wln/wlnRtl.c b/src/base/wln/wlnRtl.c index df53ebe0..97cc9714 100644 --- a/src/base/wln/wlnRtl.c +++ b/src/base/wln/wlnRtl.c @@ -77,6 +77,32 @@ void Rtl_NtkCleanFile( char * pFileName ) fclose( pFile2 ); } +void Rtl_NtkCleanFile2( char * pFileName ) +{ + char * pBuffer, * pFileName2 = "_temp__.v"; + FILE * pFile = fopen( pFileName, "rb" ); + FILE * pFile2; + if ( pFile == NULL ) + { + printf( "Cannot open file \"%s\" for reading.\n", pFileName ); + return; + } + pFile2 = fopen( pFileName2, "wb" ); + if ( pFile2 == NULL ) + { + fclose( pFile ); + printf( "Cannot open file \"%s\" for writing.\n", pFileName2 ); + return; + } + pBuffer = ABC_ALLOC( char, MAX_LINE ); + while ( fgets( pBuffer, MAX_LINE, pFile ) != NULL ) + if ( !strstr(pBuffer, "//") ) + fputs( pBuffer, pFile2 ); + ABC_FREE( pBuffer ); + fclose( pFile ); + fclose( pFile2 ); +} + char * Wln_GetYosysName() { char * pYosysName = NULL; -- cgit v1.2.3