00001
00021 #include "retInt.h"
00022
00026
00027 static int Abc_NtkRetimeVerifyModel( Abc_Ntk_t * pNtkCone, Vec_Int_t * vValues, int * pModel );
00028
00032
00044 Vec_Int_t * Abc_NtkRetimeInitialValues( Abc_Ntk_t * pNtkCone, Vec_Int_t * vValues, int fVerbose )
00045 {
00046 Vec_Int_t * vSolution;
00047 Abc_Ntk_t * pNtkMiter, * pNtkLogic;
00048 int RetValue, clk;
00049 if ( pNtkCone == NULL )
00050 return Vec_IntDup( vValues );
00051
00052 pNtkLogic = Abc_NtkDup( pNtkCone );
00053 Abc_NtkToAig( pNtkLogic );
00054
00055 pNtkMiter = Abc_NtkCreateTarget( pNtkLogic, pNtkLogic->vCos, vValues );
00056 if ( fVerbose )
00057 printf( "The miter for initial state computation has %d AIG nodes. ", Abc_NtkNodeNum(pNtkMiter) );
00058
00059 clk = clock();
00060 RetValue = Abc_NtkMiterSat( pNtkMiter, (sint64)500000, (sint64)50000000, 0, NULL, NULL );
00061 if ( fVerbose )
00062 { PRT( "SAT solving time", clock() - clk ); }
00063
00064 if ( RetValue == 1 )
00065 printf( "Abc_NtkRetimeInitialValues(): The problem is unsatisfiable. DC latch values are used.\n" );
00066 else if ( RetValue == -1 )
00067 printf( "Abc_NtkRetimeInitialValues(): The SAT problem timed out. DC latch values are used.\n" );
00068 else if ( !Abc_NtkRetimeVerifyModel( pNtkCone, vValues, pNtkMiter->pModel ) )
00069 printf( "Abc_NtkRetimeInitialValues(): The computed counter-example is incorrect.\n" );
00070
00071 vSolution = RetValue? NULL : Vec_IntAllocArray( pNtkMiter->pModel, Abc_NtkPiNum(pNtkLogic) );
00072 pNtkMiter->pModel = NULL;
00073 Abc_NtkDelete( pNtkMiter );
00074 Abc_NtkDelete( pNtkLogic );
00075 return vSolution;
00076 }
00077
00089 int Abc_ObjSopSimulate( Abc_Obj_t * pObj )
00090 {
00091 char * pCube, * pSop = pObj->pData;
00092 int nVars, Value, v, ResOr, ResAnd, ResVar;
00093 assert( pSop && !Abc_SopIsExorType(pSop) );
00094
00095 ResOr = 0;
00096 nVars = Abc_SopGetVarNum(pSop);
00097 Abc_SopForEachCube( pSop, nVars, pCube )
00098 {
00099 ResAnd = 1;
00100 Abc_CubeForEachVar( pCube, Value, v )
00101 {
00102 if ( Value == '0' )
00103 ResVar = 1 ^ ((int)Abc_ObjFanin(pObj, v)->pCopy);
00104 else if ( Value == '1' )
00105 ResVar = (int)Abc_ObjFanin(pObj, v)->pCopy;
00106 else
00107 continue;
00108 ResAnd &= ResVar;
00109 }
00110 ResOr |= ResAnd;
00111 }
00112
00113 if ( !Abc_SopGetPhase(pSop) )
00114 ResOr ^= 1;
00115 return ResOr;
00116 }
00117
00129 int Abc_NtkRetimeVerifyModel( Abc_Ntk_t * pNtkCone, Vec_Int_t * vValues, int * pModel )
00130 {
00131 Vec_Ptr_t * vNodes;
00132 Abc_Obj_t * pObj;
00133 int i, Counter = 0;
00134 assert( Abc_NtkIsSopLogic(pNtkCone) );
00135
00136 Abc_NtkForEachPi( pNtkCone, pObj, i )
00137 pObj->pCopy = (void *)pModel[i];
00138
00139 vNodes = Abc_NtkDfs( pNtkCone, 0 );
00140 Vec_PtrForEachEntry( vNodes, pObj, i )
00141 pObj->pCopy = (void *)Abc_ObjSopSimulate( pObj );
00142 Vec_PtrFree( vNodes );
00143
00144 Abc_NtkForEachPo( pNtkCone, pObj, i )
00145 pObj->pCopy = Abc_ObjFanin0(pObj)->pCopy;
00146 Abc_NtkForEachPo( pNtkCone, pObj, i )
00147 Counter += (Vec_IntEntry(vValues, i) != (int)pObj->pCopy);
00148 if ( Counter > 0 )
00149 printf( "%d outputs (out of %d) have a value mismatch.\n", Counter, Abc_NtkPoNum(pNtkCone) );
00150 return 1;
00151 }
00152
00164 void Abc_NtkRetimeTranferToCopy( Abc_Ntk_t * pNtk )
00165 {
00166 Abc_Obj_t * pObj;
00167 int i;
00168 Abc_NtkForEachObj( pNtk, pObj, i )
00169 if ( Abc_ObjIsLatch(pObj) )
00170 pObj->pCopy = (void *)Abc_LatchIsInit1(pObj);
00171 }
00172
00184 void Abc_NtkRetimeTranferFromCopy( Abc_Ntk_t * pNtk )
00185 {
00186 Abc_Obj_t * pObj;
00187 int i;
00188 Abc_NtkForEachObj( pNtk, pObj, i )
00189 if ( Abc_ObjIsLatch(pObj) )
00190 pObj->pData = (void *)(pObj->pCopy? ABC_INIT_ONE : ABC_INIT_ZERO);
00191 }
00192
00204 Vec_Int_t * Abc_NtkRetimeCollectLatchValues( Abc_Ntk_t * pNtk )
00205 {
00206 Vec_Int_t * vValues;
00207 Abc_Obj_t * pObj;
00208 int i;
00209 vValues = Vec_IntAlloc( Abc_NtkLatchNum(pNtk) );
00210 Abc_NtkForEachObj( pNtk, pObj, i )
00211 if ( Abc_ObjIsLatch(pObj) )
00212 Vec_IntPush( vValues, Abc_LatchIsInit1(pObj) );
00213 return vValues;
00214 }
00215
00227 void Abc_NtkRetimeInsertLatchValues( Abc_Ntk_t * pNtk, Vec_Int_t * vValues )
00228 {
00229 Abc_Obj_t * pObj;
00230 int i, Counter = 0;
00231 Abc_NtkForEachObj( pNtk, pObj, i )
00232 if ( Abc_ObjIsLatch(pObj) )
00233 pObj->pCopy = (void *)Counter++;
00234 Abc_NtkForEachObj( pNtk, pObj, i )
00235 if ( Abc_ObjIsLatch(pObj) )
00236 pObj->pData = (void *)(vValues? (Vec_IntEntry(vValues,(int)pObj->pCopy)? ABC_INIT_ONE : ABC_INIT_ZERO) : ABC_INIT_DC);
00237 }
00238
00250 Abc_Ntk_t * Abc_NtkRetimeBackwardInitialStart( Abc_Ntk_t * pNtk )
00251 {
00252 Abc_Ntk_t * pNtkNew;
00253 Abc_Obj_t * pObj;
00254 int i;
00255
00256 pNtkNew = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 );
00257
00258 Abc_NtkForEachObj( pNtk, pObj, i )
00259 if ( Abc_ObjIsLatch(pObj) )
00260 pObj->pCopy = Abc_NtkCreatePo(pNtkNew);
00261 return pNtkNew;
00262 }
00263
00275 void Abc_NtkRetimeBackwardInitialFinish( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew, Vec_Int_t * vValuesOld, int fVerbose )
00276 {
00277 Vec_Int_t * vValuesNew;
00278 Abc_Obj_t * pObj;
00279 int i;
00280
00281 Abc_NtkForEachObj( pNtk, pObj, i )
00282 if ( Abc_ObjIsLatch(pObj) )
00283 Abc_ObjAddFanin( pObj->pCopy, Abc_NtkCreatePi(pNtkNew) );
00284
00285 Abc_NtkAddDummyPiNames( pNtkNew );
00286 Abc_NtkAddDummyPoNames( pNtkNew );
00287
00288 if ( !Abc_NtkCheck( pNtkNew ) )
00289 fprintf( stdout, "Abc_NtkRetimeBackwardInitialFinish(): Network check has failed.\n" );
00290
00291 vValuesNew = Abc_NtkRetimeInitialValues( pNtkNew, vValuesOld, fVerbose );
00292
00293 Abc_NtkRetimeInsertLatchValues( pNtk, vValuesNew );
00294 if ( vValuesNew ) Vec_IntFree( vValuesNew );
00295 }
00296
00297
00310 void Abc_NtkCycleInitStateSop( Abc_Ntk_t * pNtk, int nFrames, int fVerbose )
00311 {
00312 Vec_Ptr_t * vNodes;
00313 Abc_Obj_t * pObj;
00314 int i, f;
00315 assert( Abc_NtkIsSopLogic(pNtk) );
00316 srand( 0x12341234 );
00317
00318 Abc_NtkForEachPi( pNtk, pObj, i )
00319 pObj->pCopy = (void *)(rand() & 1);
00320 Abc_NtkForEachLatch( pNtk, pObj, i )
00321 pObj->pCopy = (void *)Abc_LatchIsInit1(pObj);
00322
00323 vNodes = Abc_NtkDfs( pNtk, 0 );
00324 for ( f = 0; f < nFrames; f++ )
00325 {
00326
00327 Vec_PtrForEachEntry( vNodes, pObj, i )
00328 pObj->pCopy = (void *)Abc_ObjSopSimulate( pObj );
00329
00330 Abc_NtkForEachCo( pNtk, pObj, i )
00331 pObj->pCopy = Abc_ObjFanin0(pObj)->pCopy;
00332
00333 Abc_NtkForEachPi( pNtk, pObj, i )
00334 pObj->pCopy = (void *)(rand() & 1);
00335
00336 Abc_NtkForEachLatch( pNtk, pObj, i )
00337 Abc_ObjFanout0(pObj)->pCopy = Abc_ObjFanin0(pObj)->pCopy;
00338 }
00339 Vec_PtrFree( vNodes );
00340
00341 Abc_NtkForEachLatch( pNtk, pObj, i )
00342 pObj->pData = (void *)(Abc_ObjFanout0(pObj)->pCopy ? ABC_INIT_ONE : ABC_INIT_ZERO);
00343 }
00344
00348
00349