abc/src/base/io/ioReadAiger.c

367 lines
12 KiB
C
Raw Normal View History

2007-09-06 17:01:00 +02:00
/**CFile****************************************************************
2008-01-31 05:01:00 +01:00
FileName [ioReadAiger.c]
2007-09-06 17:01:00 +02:00
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Command processing package.]
Synopsis [Procedures to read binary AIGER format developed by
Armin Biere, Johannes Kepler University (http://fmv.jku.at/)]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - December 16, 2006.]
2008-01-31 05:01:00 +01:00
Revision [$Id: ioReadAiger.c,v 1.00 2006/12/16 00:00:00 alanmi Exp $]
2007-09-06 17:01:00 +02:00
***********************************************************************/
2008-05-08 17:01:00 +02:00
#include "ioAbc.h"
2007-09-06 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
2008-01-21 17:01:00 +01:00
/**Function*************************************************************
Synopsis [Extracts one unsigned AIG edge from the input buffer.]
Description [This procedure is a slightly modified version of Armin Biere's
procedure "unsigned decode (FILE * file)". ]
SideEffects [Updates the current reading position.]
SeeAlso []
***********************************************************************/
2008-01-31 05:01:00 +01:00
unsigned Io_ReadAigerDecode( char ** ppPos )
2008-01-21 17:01:00 +01:00
{
unsigned x = 0, i = 0;
unsigned char ch;
// while ((ch = getnoneofch (file)) & 0x80)
while ((ch = *(*ppPos)++) & 0x80)
x |= (ch & 0x7f) << (7 * i++);
return x | (ch << (7 * i));
}
/**Function*************************************************************
Synopsis [Decodes the encoded array of literals.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2008-01-31 05:01:00 +01:00
Vec_Int_t * Io_WriteDecodeLiterals( char ** ppPos, int nEntries )
2008-01-21 17:01:00 +01:00
{
Vec_Int_t * vLits;
int Lit, LitPrev, Diff, i;
vLits = Vec_IntAlloc( nEntries );
2008-01-31 05:01:00 +01:00
LitPrev = Io_ReadAigerDecode( ppPos );
2008-01-21 17:01:00 +01:00
Vec_IntPush( vLits, LitPrev );
for ( i = 1; i < nEntries; i++ )
{
// Diff = Lit - LitPrev;
// Diff = (Lit < LitPrev)? -Diff : Diff;
// Diff = ((2 * Diff) << 1) | (int)(Lit < LitPrev);
2008-01-31 05:01:00 +01:00
Diff = Io_ReadAigerDecode( ppPos );
2008-01-21 17:01:00 +01:00
Diff = (Diff & 1)? -(Diff >> 1) : Diff >> 1;
Lit = Diff + LitPrev;
Vec_IntPush( vLits, Lit );
LitPrev = Lit;
}
return vLits;
}
2007-09-06 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Reads the AIG in the binary AIGER format.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2008-01-31 05:01:00 +01:00
Abc_Ntk_t * Io_ReadAiger( char * pFileName, int fCheck )
2007-09-06 17:01:00 +02:00
{
2008-01-31 05:01:00 +01:00
ProgressBar * pProgress;
2007-09-06 17:01:00 +02:00
FILE * pFile;
2008-01-31 05:01:00 +01:00
Vec_Ptr_t * vNodes, * vTerms;
2008-01-21 17:01:00 +01:00
Vec_Int_t * vLits = NULL;
2008-01-31 05:01:00 +01:00
Abc_Obj_t * pObj, * pNode0, * pNode1;
Abc_Ntk_t * pNtkNew;
int nTotal, nInputs, nOutputs, nLatches, nAnds, nFileSize, iTerm, nDigits, i;
char * pContents, * pDrivers, * pSymbols, * pCur, * pName, * pType;
2007-09-06 17:01:00 +02:00
unsigned uLit0, uLit1, uLit;
// read the file into the buffer
2008-01-31 05:01:00 +01:00
nFileSize = Extra_FileSize( pFileName );
2007-09-06 17:01:00 +02:00
pFile = fopen( pFileName, "rb" );
pContents = ALLOC( char, nFileSize );
fread( pContents, nFileSize, 1, pFile );
fclose( pFile );
// check if the input file format is correct
2008-01-21 17:01:00 +01:00
if ( strncmp(pContents, "aig", 3) != 0 || (pContents[3] != ' ' && pContents[3] != '2') )
2007-09-06 17:01:00 +02:00
{
fprintf( stdout, "Wrong input file format.\n" );
return NULL;
}
2008-01-31 05:01:00 +01:00
// allocate the empty AIG
pNtkNew = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
pName = Extra_FileNameGeneric( pFileName );
pNtkNew->pName = Extra_UtilStrsav( pName );
pNtkNew->pSpec = Extra_UtilStrsav( pFileName );
free( pName );
2007-09-06 17:01:00 +02:00
// read the file type
pCur = pContents; while ( *pCur++ != ' ' );
// read the number of objects
nTotal = atoi( pCur ); while ( *pCur++ != ' ' );
// read the number of inputs
nInputs = atoi( pCur ); while ( *pCur++ != ' ' );
// read the number of latches
nLatches = atoi( pCur ); while ( *pCur++ != ' ' );
// read the number of outputs
nOutputs = atoi( pCur ); while ( *pCur++ != ' ' );
// read the number of nodes
nAnds = atoi( pCur ); while ( *pCur++ != '\n' );
// check the parameters
if ( nTotal != nInputs + nLatches + nAnds )
{
fprintf( stdout, "The paramters are wrong.\n" );
return NULL;
}
// prepare the array of nodes
vNodes = Vec_PtrAlloc( 1 + nInputs + nLatches + nAnds );
2008-01-31 05:01:00 +01:00
Vec_PtrPush( vNodes, Abc_ObjNot( Abc_AigConst1(pNtkNew) ) );
2007-09-06 17:01:00 +02:00
// create the PIs
2008-01-31 05:01:00 +01:00
for ( i = 0; i < nInputs; i++ )
2007-09-06 17:01:00 +02:00
{
2008-01-31 05:01:00 +01:00
pObj = Abc_NtkCreatePi(pNtkNew);
2007-09-06 17:01:00 +02:00
Vec_PtrPush( vNodes, pObj );
}
// create the POs
2008-01-31 05:01:00 +01:00
for ( i = 0; i < nOutputs; i++ )
2007-09-06 17:01:00 +02:00
{
2008-01-31 05:01:00 +01:00
pObj = Abc_NtkCreatePo(pNtkNew);
2007-09-06 17:01:00 +02:00
}
// create the latches
2008-01-31 05:01:00 +01:00
nDigits = Extra_Base10Log( nLatches );
2007-09-06 17:01:00 +02:00
for ( i = 0; i < nLatches; i++ )
{
2008-01-31 05:01:00 +01:00
pObj = Abc_NtkCreateLatch(pNtkNew);
Abc_LatchSetInit0( pObj );
pNode0 = Abc_NtkCreateBi(pNtkNew);
pNode1 = Abc_NtkCreateBo(pNtkNew);
Abc_ObjAddFanin( pObj, pNode0 );
Abc_ObjAddFanin( pNode1, pObj );
2007-09-06 17:01:00 +02:00
Vec_PtrPush( vNodes, pNode1 );
// assign names to latch and its input
2008-01-31 05:01:00 +01:00
// Abc_ObjAssignName( pObj, Abc_ObjNameDummy("_L", i, nDigits), NULL );
// printf( "Creating latch %s with input %d and output %d.\n", Abc_ObjName(pObj), pNode0->Id, pNode1->Id );
2007-09-06 17:01:00 +02:00
}
2008-01-31 05:01:00 +01:00
2008-01-21 17:01:00 +01:00
if ( pContents[3] == ' ' ) // standard AIGER
{
2008-01-31 05:01:00 +01:00
// remember the beginning of latch/PO literals
pDrivers = pCur;
2008-01-21 17:01:00 +01:00
// scroll to the beginning of the binary data
for ( i = 0; i < nLatches + nOutputs; )
if ( *pCur++ == '\n' )
i++;
}
else // modified AIGER
{
2008-01-31 05:01:00 +01:00
vLits = Io_WriteDecodeLiterals( &pCur, nLatches + nOutputs );
2008-01-21 17:01:00 +01:00
}
2007-09-06 17:01:00 +02:00
// create the AND gates
2008-01-31 05:01:00 +01:00
pProgress = Extra_ProgressBarStart( stdout, nAnds );
2007-09-06 17:01:00 +02:00
for ( i = 0; i < nAnds; i++ )
{
2008-01-31 05:01:00 +01:00
Extra_ProgressBarUpdate( pProgress, i, NULL );
2007-09-06 17:01:00 +02:00
uLit = ((i + 1 + nInputs + nLatches) << 1);
2008-01-31 05:01:00 +01:00
uLit1 = uLit - Io_ReadAigerDecode( &pCur );
uLit0 = uLit1 - Io_ReadAigerDecode( &pCur );
2007-09-06 17:01:00 +02:00
// assert( uLit1 > uLit0 );
2008-01-31 05:01:00 +01:00
pNode0 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), uLit0 & 1 );
pNode1 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit1 >> 1), uLit1 & 1 );
2007-09-06 17:01:00 +02:00
assert( Vec_PtrSize(vNodes) == i + 1 + nInputs + nLatches );
2008-01-31 05:01:00 +01:00
Vec_PtrPush( vNodes, Abc_AigAnd(pNtkNew->pManFunc, pNode0, pNode1) );
2007-09-06 17:01:00 +02:00
}
2008-01-31 05:01:00 +01:00
Extra_ProgressBarStop( pProgress );
2007-09-06 17:01:00 +02:00
// remember the place where symbols begin
pSymbols = pCur;
// read the latch driver literals
2008-01-31 05:01:00 +01:00
pCur = pDrivers;
2008-01-21 17:01:00 +01:00
if ( pContents[3] == ' ' ) // standard AIGER
2007-09-06 17:01:00 +02:00
{
2008-01-31 05:01:00 +01:00
Abc_NtkForEachLatchInput( pNtkNew, pObj, i )
2008-01-21 17:01:00 +01:00
{
uLit0 = atoi( pCur ); while ( *pCur++ != '\n' );
2008-01-31 05:01:00 +01:00
pNode0 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );//^ (uLit0 < 2) );
Abc_ObjAddFanin( pObj, pNode0 );
2008-01-21 17:01:00 +01:00
}
// read the PO driver literals
2008-01-31 05:01:00 +01:00
Abc_NtkForEachPo( pNtkNew, pObj, i )
2008-01-21 17:01:00 +01:00
{
uLit0 = atoi( pCur ); while ( *pCur++ != '\n' );
2008-01-31 05:01:00 +01:00
pNode0 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );//^ (uLit0 < 2) );
Abc_ObjAddFanin( pObj, pNode0 );
2008-01-21 17:01:00 +01:00
}
2007-09-06 17:01:00 +02:00
}
2008-01-21 17:01:00 +01:00
else
2007-09-06 17:01:00 +02:00
{
2008-01-21 17:01:00 +01:00
// read the latch driver literals
2008-01-31 05:01:00 +01:00
Abc_NtkForEachLatchInput( pNtkNew, pObj, i )
2008-01-21 17:01:00 +01:00
{
uLit0 = Vec_IntEntry( vLits, i );
2008-01-31 05:01:00 +01:00
pNode0 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );
Abc_ObjAddFanin( pObj, pNode0 );
2008-01-21 17:01:00 +01:00
}
// read the PO driver literals
2008-01-31 05:01:00 +01:00
Abc_NtkForEachPo( pNtkNew, pObj, i )
2008-01-21 17:01:00 +01:00
{
2008-01-31 05:01:00 +01:00
uLit0 = Vec_IntEntry( vLits, i+Abc_NtkLatchNum(pNtkNew) );
pNode0 = Abc_ObjNotCond( Vec_PtrEntry(vNodes, uLit0 >> 1), (uLit0 & 1) );
Abc_ObjAddFanin( pObj, pNode0 );
2008-01-21 17:01:00 +01:00
}
Vec_IntFree( vLits );
2007-09-06 17:01:00 +02:00
}
2008-01-31 05:01:00 +01:00
2007-09-06 17:01:00 +02:00
// read the names if present
pCur = pSymbols;
if ( *pCur != 'c' )
{
int Counter = 0;
while ( pCur < pContents + nFileSize && *pCur != 'c' )
{
// get the terminal type
pType = pCur;
if ( *pCur == 'i' )
2008-01-31 05:01:00 +01:00
vTerms = pNtkNew->vPis;
2007-09-06 17:01:00 +02:00
else if ( *pCur == 'l' )
2008-01-31 05:01:00 +01:00
vTerms = pNtkNew->vBoxes;
2007-09-06 17:01:00 +02:00
else if ( *pCur == 'o' )
2008-01-31 05:01:00 +01:00
vTerms = pNtkNew->vPos;
2007-09-06 17:01:00 +02:00
else
{
fprintf( stdout, "Wrong terminal type.\n" );
return NULL;
}
// get the terminal number
iTerm = atoi( ++pCur ); while ( *pCur++ != ' ' );
// get the node
if ( iTerm >= Vec_PtrSize(vTerms) )
{
fprintf( stdout, "The number of terminal is out of bound.\n" );
return NULL;
}
pObj = Vec_PtrEntry( vTerms, iTerm );
if ( *pType == 'l' )
2008-01-31 05:01:00 +01:00
pObj = Abc_ObjFanout0(pObj);
2007-09-06 17:01:00 +02:00
// assign the name
pName = pCur; while ( *pCur++ != '\n' );
// assign this name
*(pCur-1) = 0;
2008-01-31 05:01:00 +01:00
Abc_ObjAssignName( pObj, pName, NULL );
2007-09-06 17:01:00 +02:00
if ( *pType == 'l' )
{
2008-01-31 05:01:00 +01:00
Abc_ObjAssignName( Abc_ObjFanin0(pObj), Abc_ObjName(pObj), "L" );
Abc_ObjAssignName( Abc_ObjFanin0(Abc_ObjFanin0(pObj)), Abc_ObjName(pObj), "_in" );
2007-09-06 17:01:00 +02:00
}
// mark the node as named
2008-01-31 05:01:00 +01:00
pObj->pCopy = (Abc_Obj_t *)Abc_ObjName(pObj);
2007-09-06 17:01:00 +02:00
}
// assign the remaining names
2008-01-31 05:01:00 +01:00
Abc_NtkForEachPi( pNtkNew, pObj, i )
2007-09-06 17:01:00 +02:00
{
if ( pObj->pCopy ) continue;
2008-01-31 05:01:00 +01:00
Abc_ObjAssignName( pObj, Abc_ObjName(pObj), NULL );
2007-09-06 17:01:00 +02:00
Counter++;
}
2008-01-31 05:01:00 +01:00
Abc_NtkForEachLatchOutput( pNtkNew, pObj, i )
2007-09-06 17:01:00 +02:00
{
if ( pObj->pCopy ) continue;
2008-01-31 05:01:00 +01:00
Abc_ObjAssignName( pObj, Abc_ObjName(pObj), NULL );
Abc_ObjAssignName( Abc_ObjFanin0(pObj), Abc_ObjName(pObj), "L" );
Abc_ObjAssignName( Abc_ObjFanin0(Abc_ObjFanin0(pObj)), Abc_ObjName(pObj), "_in" );
2007-09-06 17:01:00 +02:00
Counter++;
}
2008-01-31 05:01:00 +01:00
Abc_NtkForEachPo( pNtkNew, pObj, i )
2007-09-06 17:01:00 +02:00
{
if ( pObj->pCopy ) continue;
2008-01-31 05:01:00 +01:00
Abc_ObjAssignName( pObj, Abc_ObjName(pObj), NULL );
2007-09-06 17:01:00 +02:00
Counter++;
}
2008-01-31 05:01:00 +01:00
// if ( Counter )
// printf( "Io_ReadAiger(): Added %d default names for nameless I/O/register objects.\n", Counter );
2007-09-06 17:01:00 +02:00
}
else
{
2008-01-31 05:01:00 +01:00
// printf( "Io_ReadAiger(): I/O/register names are not given. Generating short names.\n" );
Abc_NtkShortNames( pNtkNew );
2007-09-06 17:01:00 +02:00
}
2008-01-31 05:01:00 +01:00
// read the name of the model if given
2008-05-08 17:01:00 +02:00
if ( *pCur == 'c' && pCur < pContents + nFileSize )
2008-01-31 05:01:00 +01:00
{
if ( !strncmp( pCur + 2, ".model", 6 ) )
{
char * pTemp;
for ( pTemp = pCur + 9; *pTemp && *pTemp != '\n'; pTemp++ );
*pTemp = 0;
free( pNtkNew->pName );
pNtkNew->pName = Extra_UtilStrsav( pCur + 9 );
}
}
2007-09-06 17:01:00 +02:00
// skipping the comments
free( pContents );
Vec_PtrFree( vNodes );
// remove the extra nodes
2008-01-31 05:01:00 +01:00
Abc_AigCleanup( pNtkNew->pManFunc );
2007-09-06 17:01:00 +02:00
// check the result
2008-01-31 05:01:00 +01:00
if ( fCheck && !Abc_NtkCheckRead( pNtkNew ) )
2007-09-06 17:01:00 +02:00
{
2008-01-31 05:01:00 +01:00
printf( "Io_ReadAiger: The network check has failed.\n" );
Abc_NtkDelete( pNtkNew );
2007-09-06 17:01:00 +02:00
return NULL;
}
2008-01-31 05:01:00 +01:00
return pNtkNew;
2007-09-06 17:01:00 +02:00
}
2008-01-31 05:01:00 +01:00
2007-09-06 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////