wlcMem.c 57.8 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**CFile****************************************************************

  FileName    [wlcMem.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Memory abstraction.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 22, 2014.]

  Revision    [$Id: wlcMem.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

***********************************************************************/

#include "wlc.h"
#include "sat/bsat/satStore.h"
#include "proof/pdr/pdr.h"
#include "proof/pdr/pdrInt.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  Synopsis    [Memory blasting.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkMemBlast_rec( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, int iObj, Vec_Int_t * vFanins )
{
    Wlc_Obj_t * pObj;
    int i, iFanin;
    if ( Wlc_ObjCopy(p, iObj) )
        return;
    pObj = Wlc_NtkObj( p, iObj );
    Wlc_ObjForEachFanin( pObj, iFanin, i )
        Wlc_NtkMemBlast_rec( pNew, p, iFanin, vFanins );
    if ( pObj->Type == WLC_OBJ_WRITE )
    {
        Vec_Int_t * vTemp = Vec_IntAlloc( 1 );
        Vec_Int_t * vBits = Vec_IntAlloc( 100 );
        Wlc_Obj_t * pMem  = Wlc_ObjFanin0(p, pObj);
        Wlc_Obj_t * pAddr = Wlc_ObjFanin1(p, pObj);
        Wlc_Obj_t * pData = Wlc_ObjFanin2(p, pObj);
        int DataW = Wlc_ObjRange(pData);
        int AddrW = Wlc_ObjRange(pAddr);
        int nRegs = 1 << AddrW, iObjNew, iObjDec;
        assert( nRegs * DataW == Wlc_ObjRange(pMem) );
        assert( Wlc_ObjRange(pObj) == Wlc_ObjRange(pMem) );
        // create decoder
        iObjDec = Wlc_ObjAlloc( pNew, WLC_OBJ_DEC, 0, nRegs-1, 0 );
        Vec_IntFill( vTemp, 1, Wlc_ObjCopy(p, Wlc_ObjId(p, pAddr)) );
        Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjDec), vTemp );
        // create decoder bits
        for ( i = 0; i < nRegs; i++ )
        {
            int iObj2 = Wlc_ObjAlloc( pNew, WLC_OBJ_BIT_SELECT, 0, i, i );
            Vec_IntFill( vTemp, 1, iObjDec );
            Vec_IntPushTwo( vTemp, i, i );
            Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObj2), vTemp );
            Vec_IntPush( vBits, iObj2 );
        }
        // create data words
        Vec_IntClear( vFanins );
        for ( i = 0; i < nRegs; i++ )
        {
            int iObj2 = Wlc_ObjAlloc( pNew, WLC_OBJ_BIT_SELECT, 0, i*DataW+DataW-1, i*DataW );
            Vec_IntFill( vTemp, 1, Wlc_ObjCopy(p, Wlc_ObjId(p, pMem)) );
            Vec_IntPushTwo( vTemp, i*DataW+DataW-1, i*DataW );
            Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObj2), vTemp );
            Vec_IntPush( vFanins, iObj2 );
        }
        // create MUXes of data words controlled by decoder bits
        for ( i = 0; i < nRegs; i++ )
        {
            int iObj2 = Wlc_ObjAlloc( pNew, WLC_OBJ_MUX, 0, DataW-1, 0 );
            Vec_IntFill( vTemp, 1, Vec_IntEntry(vBits, i) );
            Vec_IntPushTwo( vTemp, Wlc_ObjCopy(p, Wlc_ObjId(p, pData)), Vec_IntEntry(vFanins, i) );
            Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObj2), vTemp );
            Vec_IntWriteEntry( vFanins, i, iObj2 );
        }
        // concatenate the results
        iObjNew = Wlc_ObjAlloc( pNew, WLC_OBJ_BIT_CONCAT, 0, nRegs*DataW-1, 0 );
        Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjNew), vFanins );
        Wlc_ObjSetCopy( p, iObj, iObjNew );
        Vec_IntFree( vTemp );
        Vec_IntFree( vBits );
    }
    else if ( pObj->Type == WLC_OBJ_READ )
    {
        Vec_Int_t * vTemp = Vec_IntAlloc( 1 );
        Wlc_Obj_t * pMem  = Wlc_ObjFanin0(p, pObj);
        Wlc_Obj_t * pAddr = Wlc_ObjFanin1(p, pObj);
        int DataW = Wlc_ObjRange(pObj);
        int AddrW = Wlc_ObjRange(pAddr);
        int nRegs = 1 << AddrW, iObjNew;
        assert( nRegs * DataW == Wlc_ObjRange(pMem) );
        Vec_IntClear( vFanins );
        Vec_IntPush( vFanins, Wlc_ObjCopy(p, Wlc_ObjId(p, pAddr)) );
        for ( i = 0; i < nRegs; i++ )
        {
            int iObj2 = Wlc_ObjAlloc( pNew, WLC_OBJ_BIT_SELECT, 0, i*DataW+DataW-1, i*DataW );
            Vec_IntFill( vTemp, 1, Wlc_ObjCopy(p, Wlc_ObjId(p, pMem)) );
            Vec_IntPushTwo( vTemp, i*DataW+DataW-1, i*DataW );
            Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObj2), vTemp );
            Vec_IntPush( vFanins, iObj2 );
        }
        iObjNew = Wlc_ObjAlloc( pNew, WLC_OBJ_MUX, 0, DataW-1, 0 );
        Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjNew), vFanins );
        Wlc_ObjSetCopy( p, iObj, iObjNew );
        Vec_IntFree( vTemp );
    }
    else
        Wlc_ObjDup( pNew, p, iObj, vFanins );
}
Wlc_Ntk_t * Wlc_NtkMemBlast( Wlc_Ntk_t * p )
{
    Wlc_Ntk_t * pNew;
    Wlc_Obj_t * pObj;
    Vec_Int_t * vFanins;
    int i;
    Wlc_NtkCleanCopy( p );
    vFanins = Vec_IntAlloc( 100 );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc );
    pNew->fSmtLib = p->fSmtLib;
144
    pNew->fAsyncRst = p->fAsyncRst;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
    Wlc_NtkForEachCi( p, pObj, i )
        Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    Wlc_NtkForEachCo( p, pObj, i )
        Wlc_NtkMemBlast_rec( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    Wlc_NtkForEachCo( p, pObj, i )
        Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
    if ( p->vInits )
    pNew->vInits = Vec_IntDup( p->vInits );
    if ( p->pInits )
    pNew->pInits = Abc_UtilStrsav( p->pInits );
    Vec_IntFree( vFanins );
    if ( p->pSpec )
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    return pNew;
}


/**Function*************************************************************

Alan Mishchenko committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  Synopsis    [Collect memory nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Wlc_NtkCollectMemSizes( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; int i;
    Vec_Int_t * vMemSizes = Vec_IntAlloc( 10 );
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) != WLC_OBJ_WRITE && Wlc_ObjType(pObj) != WLC_OBJ_READ )
            continue;
        pObj = Wlc_ObjFanin( p, pObj, 0 );
        Vec_IntPushUnique( vMemSizes, Wlc_ObjRange(pObj) );
    }
    return vMemSizes;
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// remove PIs without fanout, non-driven object and their fanouts
int Wlc_ObjCheckIsEmpty_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    int k, iFanin;
    if ( Wlc_ObjType(pObj) == 0 )
        return 1;
    if ( Wlc_ObjIsPi(pObj) )
        return Vec_IntEntry(&p->vRefs, Wlc_ObjId(p, pObj)) == 0;
    else if ( Wlc_ObjIsCi(pObj) )
        return 0;
    Wlc_ObjForEachFanin( pObj, iFanin, k )
        if ( !Wlc_ObjCheckIsEmpty_rec(p, Wlc_NtkObj(p, iFanin)) )
            return 0;
    return 1;
}
Vec_Int_t * Wlc_NtkCleanObjects( Wlc_Ntk_t * p, Vec_Int_t * vObjs )
{
    Wlc_Obj_t * pObj; int i;
    Vec_Int_t * vMemObjs = Vec_IntAlloc( 10 );
    Wlc_NtkSetRefs( p );
    Wlc_NtkForEachObjVec( vObjs, p, pObj, i )
    {
        //printf( "Considering %d (%s)\n", Wlc_ObjId(p, pObj), Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
        if ( !Wlc_ObjCheckIsEmpty_rec(p, pObj) )
            Vec_IntPush( vMemObjs, Wlc_ObjId(p, pObj) );
    }
    return vMemObjs;
}
Vec_Int_t * Wlc_NtkCollectMemory( Wlc_Ntk_t * p, int fClean )
Alan Mishchenko committed
217 218 219 220 221 222 223 224 225 226 227 228
{
    Wlc_Obj_t * pObj; int i;
    Vec_Int_t * vMemSizes = Wlc_NtkCollectMemSizes( p );
    Vec_Int_t * vMemObjs = Vec_IntAlloc( 10 );
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) == WLC_OBJ_WRITE || Wlc_ObjType(pObj) == WLC_OBJ_READ )
            Vec_IntPush( vMemObjs, i );
        else if ( Vec_IntFind(vMemSizes, Wlc_ObjRange(pObj)) >= 0 )
            Vec_IntPush( vMemObjs, i );
    }
    Vec_IntFree( vMemSizes );
229 230 231 232 233 234 235 236
    Vec_IntSort( vMemObjs, 0 );
    //Wlc_NtkPrintNodeArray( p, vMemObjs ); 
    if ( fClean )
    {
        Vec_Int_t * vTemp;
        vMemObjs = Wlc_NtkCleanObjects( p, vTemp = vMemObjs );
        Vec_IntFree( vTemp );
    }
Alan Mishchenko committed
237 238 239 240 241
    return vMemObjs;
}
void Wlc_NtkPrintMemory( Wlc_Ntk_t * p )
{
    Vec_Int_t * vMemory;
242 243
    vMemory = Wlc_NtkCollectMemory( p, 1 );
    printf( "Memory subsystem is composed of the following objects:\n" );
Alan Mishchenko committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    Wlc_NtkPrintNodeArray( p, vMemory );
    Vec_IntFree( vMemory );
}

/**Function*************************************************************

  Synopsis    [Collect fanins of memory subsystem.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Wlc_NtkCollectMemFanins( Wlc_Ntk_t * p, Vec_Int_t * vMemObjs )
{
    Wlc_Obj_t * pObj; int i, k, iFanin;
    Vec_Int_t * vMemFanins = Vec_IntAlloc( 100 );
    Wlc_NtkForEachObjVec( vMemObjs, p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
            Vec_IntPush( vMemFanins, Wlc_ObjFaninId0(pObj) );
        else if ( Wlc_ObjType(pObj) == WLC_OBJ_READ || Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
        {
            Wlc_ObjForEachFanin( pObj, iFanin, k )
                if ( k > 0 )
                    Vec_IntPush( vMemFanins, iFanin );
        }
    }
    return vMemFanins;
}

/**Function*************************************************************

  Synopsis    [Abstract memory nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Wlc_CountDcs( char * pInit )
{
    int Count = 0;
    for ( ; *pInit; pInit++ )
        Count += (*pInit == 'x' || *pInit == 'X');
    return Count;
}
int Wlc_NtkDupOneObject( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int TypeNew, Vec_Int_t * vFanins )
{
Alan Mishchenko committed
297
    int iObj = Wlc_ObjId(p, pObj);
Alan Mishchenko committed
298 299
    unsigned Type = pObj->Type;
    int iObjNew, nFanins = Wlc_ObjFaninNum(pObj);
Alan Mishchenko committed
300
    int iObjCopy = Wlc_ObjCopy(p, iObj);
Alan Mishchenko committed
301 302
    pObj->Type = TypeNew;
    pObj->nFanins = 0;
Alan Mishchenko committed
303
    iObjNew = Wlc_ObjDup( pNew, p, iObj, vFanins );
Alan Mishchenko committed
304 305 306
    pObj->Type = Type;
    pObj->nFanins = (unsigned)nFanins;
    if ( TypeNew == WLC_OBJ_FO )
Alan Mishchenko committed
307
    {
Alan Mishchenko committed
308
        Vec_IntPush( pNew->vInits, -Wlc_ObjRange(pObj) );
Alan Mishchenko committed
309 310
        Wlc_ObjSetCopy( p, iObj, iObjCopy );
    }
Alan Mishchenko committed
311 312
    return iObjNew;
}
313
void Wlc_NtkDupOneBuffer( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int iFanin, Vec_Int_t * vFanins, int fIsFi )
Alan Mishchenko committed
314 315 316
{
    int iObj = Wlc_ObjAlloc( pNew, WLC_OBJ_BUF, pObj->Signed, pObj->End, pObj->Beg );
    Wlc_Obj_t * pObjNew = Wlc_NtkObj( pNew, iObj );
317
    Vec_IntFill( vFanins, 1, iFanin );
Alan Mishchenko committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    Wlc_ObjAddFanins( pNew, pObjNew, vFanins );
    Wlc_ObjSetCo( pNew, pObjNew, fIsFi );
}

void Wlc_NtkAbsAddToNodeFrames( Vec_Int_t * vNodeFrames, Vec_Int_t * vLevel ) 
{
    int i, Entry;
    Vec_IntForEachEntry( vLevel, Entry, i )
        Vec_IntPushUnique( vNodeFrames, Entry );
    Vec_IntSort( vNodeFrames, 0 );
}
Vec_Int_t * Wlc_NtkAbsCreateFlopOutputs( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Vec_Int_t * vNodeFrames, Vec_Int_t * vFanins ) 
{
    Vec_Int_t * vNewObjs = Vec_IntAlloc( 2*Vec_IntSize(vNodeFrames) );
    Wlc_Obj_t * pObj, * pFanin = NULL;
    int i, Entry, iObj, iFrame;
    Vec_IntForEachEntry( vNodeFrames, Entry, i )
    {
Alan Mishchenko committed
336 337
        iObj   = Entry >> 11;
        iFrame = (Entry >> 1) & 0x3FF;
Alan Mishchenko committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        pObj   = Wlc_NtkObj( p, iObj );
        // address
        if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
            pFanin = Wlc_ObjFanin0(p, pObj);
        else if ( Wlc_ObjType(pObj) == WLC_OBJ_READ || Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
            pFanin = Wlc_ObjFanin1(p, pObj);
        else assert( 0 );
        Vec_IntPush( vNewObjs, Wlc_NtkDupOneObject(pNew, p, pFanin, WLC_OBJ_FO, vFanins) );
        // data
        if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
            pFanin = NULL;
        else if ( Wlc_ObjType(pObj) == WLC_OBJ_READ )
            pFanin = pObj;
        else if ( Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
            pFanin = Wlc_ObjFanin2(p, pObj);
        else assert( 0 );
        Vec_IntPush( vNewObjs, pFanin ? Wlc_NtkDupOneObject(pNew, p, pFanin, WLC_OBJ_FO, vFanins) : 0 );
    }
Alan Mishchenko committed
356
    assert( Vec_IntSize(vNewObjs) == 2 * Vec_IntSize(vNodeFrames) );
Alan Mishchenko committed
357 358
    return vNewObjs;
}
Alan Mishchenko committed
359
void Wlc_NtkAbsCreateFlopInputs( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Vec_Int_t * vNodeFrames, Vec_Int_t * vFanins, Vec_Int_t * vNewObjs, Wlc_Obj_t * pCounter, int AdderBits ) 
Alan Mishchenko committed
360
{
Alan Mishchenko committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    Wlc_Obj_t * pObj, * pFanin, * pFlop, * pCond, * pMux, * pConst;
    int i, n, Entry, Value, iObj, iFrame;
    Vec_IntForEachEntry( vNodeFrames, Entry, i )
    {
        Value  = Entry & 1;
        iObj   = Entry >> 11;
        iFrame = (Entry >> 1) & 0x3FF;
        pObj   = Wlc_NtkObj( p, iObj );
        for ( n = 0; n < 2; n++ ) // n=0 -- address   n=1 -- data
        {
            pFlop = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*i+n) );
            if ( Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
                pFanin = Wlc_ObjCopyObj( pNew, p, n ? Wlc_ObjFanin2(p, pObj) : Wlc_ObjFanin1(p, pObj) );
            else if ( Wlc_ObjType(pObj) == WLC_OBJ_READ )
                pFanin = n ? Wlc_NtkObj(pNew, Wlc_ObjCopy(p, iObj)) : Wlc_ObjCopyObj( pNew, p, Wlc_ObjFanin1(p, pObj) );
            else if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
            {
                if ( n ) continue;
                pFanin = Wlc_ObjCopyObj( pNew, p, Wlc_ObjFanin0(p, pObj) );
                if ( Value )
                {
                    Vec_IntFill( vFanins, 1, Wlc_ObjId(pNew, pFanin) );
                    pFanin = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_BIT_NOT, 0, 0, 0));
                    Wlc_ObjAddFanins( pNew, pFanin, vFanins );
                }
            }
            else assert( 0 );
            assert( Wlc_ObjRange(pFlop) == Wlc_ObjRange(pFanin) );
            // create constant
            pConst = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_CONST, 0, AdderBits-1, 0));
            Vec_IntFill( vFanins, 1, iFrame );
            Wlc_ObjAddFanins( pNew, pConst, vFanins );
            // create equality
            pCond = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_COMP_EQU, 0, 0, 0));
            Vec_IntFillTwo( vFanins, 2, Wlc_ObjId(pNew, pConst), Wlc_ObjId(pNew, pCounter) );
            Wlc_ObjAddFanins( pNew, pCond, vFanins );
            // create MUX
            pMux = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_MUX, 0, Wlc_ObjRange(pFlop)-1, 0));
            Vec_IntClear( vFanins );
            Vec_IntPush( vFanins, Wlc_ObjId(pNew, pCond) );
            Vec_IntPush( vFanins, Wlc_ObjId(pNew, pFlop) );
            Vec_IntPush( vFanins, Wlc_ObjId(pNew, pFanin) );
            Wlc_ObjAddFanins( pNew, pMux, vFanins );
            Wlc_ObjSetCo( pNew, pMux, 1 );
        }
    }
}
void Wlc_NtkAbsCreateLogic( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Vec_Int_t * vNodeFrames, Vec_Int_t * vFanins, Vec_Int_t * vNewObjs, Vec_Wec_t * vConstrs, Wlc_Obj_t * pConstrOut ) 
{
    Vec_Int_t * vTrace; 
Alan Mishchenko committed
411 412 413
    Vec_Int_t * vBitConstr = Vec_IntAlloc( 100 );
    Vec_Int_t * vLevConstr = Vec_IntAlloc( 100 );
    Wlc_Obj_t * pAddrs[2], * pDatas[2], * pCond, * pConstr = NULL;
Alan Mishchenko committed
414 415 416
    int i, k, Entry, Index[2], iFrameLast, iFrameThis;
    assert( Vec_IntSize(vNewObjs) == 2 * Vec_IntSize(vNodeFrames) );
    Vec_WecForEachLevel( vConstrs, vTrace, i )
Alan Mishchenko committed
417
    {
Alan Mishchenko committed
418 419 420
        if ( Vec_IntSize(vTrace) == 0 ) 
            continue;
        assert( Vec_IntSize(vTrace) >= 2 );
Alan Mishchenko committed
421 422
        Vec_IntClear( vLevConstr );

Alan Mishchenko committed
423 424 425 426 427
        iFrameThis = (Vec_IntEntry(vTrace, 0)  >> 1) & 0x3FF;
        iFrameLast = (Vec_IntEntryLast(vTrace) >> 1) & 0x3FF;

        Index[0] = Vec_IntFind( vNodeFrames, Vec_IntEntry(vTrace, 0) );
        Index[1] = Vec_IntFind( vNodeFrames, Vec_IntEntryLast(vTrace) );
Alan Mishchenko committed
428 429 430 431 432 433 434 435
        assert( Index[0] >= 0 && Index[1] >= 0 );

        pAddrs[0] = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*Index[0]) );
        pAddrs[1] = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*Index[1]) );

        pDatas[0] = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*Index[0]+1) );
        pDatas[1] = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*Index[1]+1) );

Alan Mishchenko committed
436 437 438 439 440 441 442 443 444 445 446
        // redirect the source in the last time frame to point to FOs
        if ( iFrameThis == iFrameLast )
        {
            pAddrs[0] = Wlc_ObjFo2Fi(pNew, pAddrs[0]);
            pDatas[0] = Wlc_ObjFo2Fi(pNew, pDatas[0]);
        }

        // redirect the sink in the last time frame to point to FOs
        pAddrs[1] = Wlc_ObjFo2Fi(pNew, pAddrs[1]);
        pDatas[1] = Wlc_ObjFo2Fi(pNew, pDatas[1]);

Alan Mishchenko committed
447 448 449 450 451 452 453 454 455 456 457 458
        // equal addresses
        pCond = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_COMP_EQU, 0, 0, 0));
        Vec_IntFillTwo( vFanins, 2, Wlc_ObjId(pNew, pAddrs[0]), Wlc_ObjId(pNew, pAddrs[1]) );
        Wlc_ObjAddFanins( pNew, pCond, vFanins );
        Vec_IntPush( vLevConstr, Wlc_ObjId(pNew, pCond) );

        // different datas
        pCond = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_COMP_NOTEQU, 0, 0, 0));
        Vec_IntFillTwo( vFanins, 2, Wlc_ObjId(pNew, pDatas[0]), Wlc_ObjId(pNew, pDatas[1]) );
        Wlc_ObjAddFanins( pNew, pCond, vFanins );
        Vec_IntPush( vLevConstr, Wlc_ObjId(pNew, pCond) );

Alan Mishchenko committed
459
        Vec_IntForEachEntryStartStop( vTrace, Entry, k, 1, Vec_IntSize(vTrace)-1 )
Alan Mishchenko committed
460
        {
Alan Mishchenko committed
461 462 463 464 465 466 467
            iFrameThis = (Entry >> 1) & 0x3FF;
            Index[0]   = Vec_IntFind( vNodeFrames, Entry );            
            assert( Index[0] >= 0 );
            pAddrs[0]  = Wlc_NtkObj( pNew, Vec_IntEntry(vNewObjs, 2*Index[0]) );
            // redirect the middle in the last time frame to point to FOs
            if ( iFrameThis == iFrameLast )
                pAddrs[0] = Wlc_ObjFo2Fi(pNew, pAddrs[0]);
Alan Mishchenko committed
468
            if ( Vec_IntEntry(vNewObjs, 2*Index[0]+1) == 0 ) // MUX
Alan Mishchenko committed
469 470
            {
                assert( Wlc_ObjType(Wlc_NtkObj(p, Entry >> 11)) == WLC_OBJ_MUX );
Alan Mishchenko committed
471
                Vec_IntPush( vLevConstr, Wlc_ObjId(pNew, pAddrs[0]) );
Alan Mishchenko committed
472
            }
Alan Mishchenko committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
            else // different addresses
            {
                assert( Wlc_ObjRange(pAddrs[0]) == Wlc_ObjRange(pAddrs[1]) );
                pCond = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_COMP_NOTEQU, 0, 0, 0));
                Vec_IntFillTwo( vFanins, 2, Wlc_ObjId(pNew, pAddrs[0]), Wlc_ObjId(pNew, pAddrs[1]) );
                Wlc_ObjAddFanins( pNew, pCond, vFanins );
                Vec_IntPush( vLevConstr, Wlc_ObjId(pNew, pCond) );
            }
        }

        // create last output
        pConstr = Wlc_NtkObj( pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_BIT_CONCAT, 0, Vec_IntSize(vLevConstr)-1, 0) );
        Wlc_ObjAddFanins( pNew, pConstr, vLevConstr );
        Vec_IntFill( vFanins, 1, Wlc_ObjId(pNew, pConstr) );
        pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_REDUCT_AND, 0, 0, 0));
        Wlc_ObjAddFanins( pNew, pConstr, vFanins );

        // save the result
        Vec_IntPush( vBitConstr, Wlc_ObjId(pNew, pConstr) );
    }
    if ( Vec_IntSize(vBitConstr) > 0 )
    {
        // create last output
        pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_BIT_CONCAT, 0, Vec_IntSize(vBitConstr)-1, 0));
        Wlc_ObjAddFanins( pNew, pConstr, vBitConstr );
        // create last output
        Vec_IntFill( vFanins, 1, Wlc_ObjId(pNew, pConstr) );
        pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_REDUCT_OR, 0, 0, 0));
        Wlc_ObjAddFanins( pNew, pConstr, vFanins );
    }
    else
    {
        pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_CONST, 0, 0, 0));
        Vec_IntFill( vFanins, 1, 0 ); // const0 - always true
        Wlc_ObjAddFanins( pNew, pConstr, vFanins );
    }
    // cleanup
    Vec_IntFree( vBitConstr );
    Vec_IntFree( vLevConstr );
Alan Mishchenko committed
512 513 514
    // add the constraint as fanin to the output
    Vec_IntFill( vFanins, 1, Wlc_ObjId(pNew, pConstr) );
    Wlc_ObjAddFanins( pNew, pConstrOut, vFanins );
Alan Mishchenko committed
515
}
Alan Mishchenko committed
516
Wlc_Ntk_t * Wlc_NtkAbstractMemory( Wlc_Ntk_t * p, Vec_Int_t * vMemObjs, Vec_Int_t * vMemFanins, int * piFirstMemPi, int * piFirstCi, int * piFirstMemCi, Vec_Wec_t * vConstrs, Vec_Int_t * vNodeFrames )
Alan Mishchenko committed
517
{
Alan Mishchenko committed
518
    Wlc_Ntk_t * pNew, * pTemp;
Alan Mishchenko committed
519 520 521
    Wlc_Obj_t * pObj, * pCounter, * pConst, * pAdder, * pConstr = NULL;
    Vec_Int_t * vNewObjs = NULL;
    Vec_Int_t * vFanins  = Vec_IntAlloc( 100 );
Alan Mishchenko committed
522
    int i, Po0, Po1, AdderBits = 16, nBits = 0;
Alan Mishchenko committed
523 524 525 526 527 528 529 530 531 532

    // mark memory nodes
    Wlc_NtkCleanMarks( p );
    Wlc_NtkForEachObjVec( vMemObjs, p, pObj, i )
        pObj->Mark = 1;

    // start new network
    Wlc_NtkCleanCopy( p );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc + 1000 );
    pNew->fSmtLib = p->fSmtLib;
533
    pNew->fAsyncRst = p->fAsyncRst;
534 535
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
Alan Mishchenko committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    pNew->vInits = Vec_IntAlloc( 100 );

    // duplicate PIs
    Wlc_NtkForEachPi( p, pObj, i )
        if ( !pObj->Mark )
        {
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
            nBits += Wlc_ObjRange(pObj);
        }

    // create new PIs
    *piFirstMemPi = nBits;
    Wlc_NtkForEachObjVec( vMemObjs, p, pObj, i )
        if ( Wlc_ObjType(pObj) == WLC_OBJ_READ )
        {
Alan Mishchenko committed
551
            Wlc_NtkDupOneObject( pNew, p, pObj, WLC_OBJ_PI, vFanins );
Alan Mishchenko committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
            nBits += Wlc_ObjRange(pObj);
        }

    // duplicate flop outputs
    *piFirstCi = nBits;
    Wlc_NtkForEachCi( p, pObj, i )
        if ( !Wlc_ObjIsPi(pObj) && !pObj->Mark )
        {
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
            Vec_IntPush( pNew->vInits, Vec_IntEntry(p->vInits, i-Wlc_NtkPiNum(p)) );
            nBits += Wlc_ObjRange(pObj);
        }

    // create flop for time-frame counter 
    pCounter = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_FO, 0, AdderBits-1, 0));
    Vec_IntPush( pNew->vInits, -AdderBits );
    nBits += AdderBits;

    // create flops for memory objects
    *piFirstMemCi = nBits;
    if ( vMemFanins )
        Wlc_NtkForEachObjVec( vMemFanins, p, pObj, i )
            Wlc_NtkDupOneObject( pNew, p, pObj, WLC_OBJ_FO, vFanins );

    // create flops for constraint objects
    if ( vConstrs )
        vNewObjs = Wlc_NtkAbsCreateFlopOutputs( pNew, p, vNodeFrames, vFanins );

    // duplicate objects
    Wlc_NtkForEachObj( p, pObj, i )
        if ( !Wlc_ObjIsCi(pObj) && !pObj->Mark )
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );

    if ( Vec_IntSize(&p->vPoPairs) )
    {
        // create miter for the PO pairs
        Vec_IntForEachEntryDouble( &p->vPoPairs, Po0, Po1, i )
        {
            int iCopy0 = Wlc_ObjCopy(p, Wlc_ObjId(p, Wlc_NtkPo(p, Po0)));
            int iCopy1 = Wlc_ObjCopy(p, Wlc_ObjId(p, Wlc_NtkPo(p, Po1)));
            int iObj = Wlc_ObjAlloc( pNew, WLC_OBJ_COMP_NOTEQU, 0, 0, 0 );
            Wlc_Obj_t * pObjNew = Wlc_NtkObj( pNew, iObj );
            Vec_IntFillTwo( vFanins, 1, iCopy0, iCopy1 );
            Wlc_ObjAddFanins( pNew, pObjNew, vFanins );
            Wlc_ObjSetCo( pNew, pObjNew, 0 );
        }
598
        printf( "Memory abstraction created %d miter outputs.\n", Wlc_NtkPoNum(pNew) );
Alan Mishchenko committed
599
        Wlc_NtkForEachCo( p, pObj, i )
600
            if ( pObj->fIsFi && !pObj->Mark )
Alan Mishchenko committed
601 602 603 604
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );

        // create constraint output
        if ( vConstrs )
Alan Mishchenko committed
605
            Wlc_ObjSetCo( pNew, (pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_BUF, 0, 0, 0))), 0 );
Alan Mishchenko committed
606 607 608 609 610 611 612 613
    }
    else
    {
        // duplicate POs
        Wlc_NtkForEachPo( p, pObj, i )
            if ( !pObj->Mark )
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
        // create constraint output
Alan Mishchenko committed
614 615
        if ( vConstrs )
            Wlc_ObjSetCo( pNew, (pConstr = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_BUF, 0, 0, 0))), 0 );
Alan Mishchenko committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
        // duplicate FIs
        Wlc_NtkForEachCo( p, pObj, i )
            if ( !Wlc_ObjIsPo(pObj) && !pObj->Mark )
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
    }

    // create time-frame counter
    pConst = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_CONST, 0, AdderBits-1, 0));
    Vec_IntFill( vFanins, 1, 1 );
    Wlc_ObjAddFanins( pNew, pConst, vFanins );

    pAdder = Wlc_NtkObj(pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_ARI_ADD, 0, AdderBits-1, 0));
    Vec_IntFillTwo( vFanins, 2, Wlc_ObjId(pNew, pCounter), Wlc_ObjId(pNew, pConst) );
    Wlc_ObjAddFanins( pNew, pAdder, vFanins );
    Wlc_ObjSetCo( pNew, pAdder, 1 );

    // create new flop inputs
    if ( vMemFanins )
        Wlc_NtkForEachObjVec( vMemFanins, p, pObj, i )
635
            Wlc_NtkDupOneBuffer( pNew, p, pObj, Wlc_ObjCopy(p, Wlc_ObjId(p, pObj)), vFanins, 1 );
Alan Mishchenko committed
636 637 638

    // create flop inputs for constraint objects
    if ( vConstrs )
Alan Mishchenko committed
639 640 641 642 643
        Wlc_NtkAbsCreateFlopInputs( pNew, p, vNodeFrames, vFanins, vNewObjs, pCounter, AdderBits );

    // create constraint logic nodes
    if ( vConstrs )
        Wlc_NtkAbsCreateLogic( pNew, p, vNodeFrames, vFanins, vNewObjs, vConstrs, pConstr );
Alan Mishchenko committed
644 645 646 647 648 649 650 651 652

    // append init states
    pNew->pInits = Wlc_PrsConvertInitValues( pNew );
    if ( p->pSpec )
        pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    //Wlc_NtkTransferNames( pNew, p );
    Vec_IntFree( vFanins );
    Vec_IntFreeP( &vNewObjs );
    Wlc_NtkCleanMarks( p );
Alan Mishchenko committed
653 654 655
    // derive topological order
    pNew = Wlc_NtkDupDfs( pTemp = pNew, 0, 1 );
    Wlc_NtkFree( pTemp );
Alan Mishchenko committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669
    return pNew;
}

/**Function*************************************************************

  Synopsis    [Derives values of memory subsystem objects under the CEX.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
670
Vec_Int_t * Wlc_NtkDeriveFirstTotal( Wlc_Ntk_t * p, Vec_Int_t * vMemObjs, Vec_Int_t * vMemFanins, int iFirstMemPi, int iFirstMemCi, int fVerbose )
Alan Mishchenko committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
    Vec_Int_t * vFirstTotal = Vec_IntStart( 3 * Vec_IntSize(vMemObjs) ); // contains pairs of (first CO bit: range) for each input/output of a node
    Wlc_Obj_t * pObj, * pFanin; int i, k, iFanin, nMemFanins = 0;
    Wlc_NtkForEachObjVec( vMemObjs, p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
        {
            //Vec_IntPush( vMemFanins, Wlc_ObjFaninId0(pObj) );
            pFanin = Wlc_ObjFanin0(p, pObj);
            assert( Wlc_ObjRange(pFanin) == 1 );
            Vec_IntWriteEntry( vFirstTotal, 3*i, (iFirstMemCi << 10) | Wlc_ObjRange(pFanin) );
            iFirstMemCi += Wlc_ObjRange(pFanin);
            nMemFanins++;
        }
        else if ( Wlc_ObjType(pObj) == WLC_OBJ_READ || Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
        {
            Wlc_ObjForEachFanin( pObj, iFanin, k )
                if ( k > 0 )
                {
                    //Vec_IntPush( vMemFanins, iFanin );
                    pFanin = Wlc_NtkObj(p, iFanin);
                    Vec_IntWriteEntry( vFirstTotal, 3*i + k, (iFirstMemCi << 10) | Wlc_ObjRange(pFanin) );
                    iFirstMemCi += Wlc_ObjRange(pFanin);
                    nMemFanins++;
                }
            if ( Wlc_ObjType(pObj) == WLC_OBJ_READ )
            {
                Vec_IntWriteEntry( vFirstTotal, 3*i + 2, (iFirstMemPi << 10) | Wlc_ObjRange(pObj) );
                iFirstMemPi += Wlc_ObjRange(pObj);
            }
        }
    }
    assert( nMemFanins == Vec_IntSize(vMemFanins) );
Alan Mishchenko committed
704 705 706 707 708 709
    if ( fVerbose )
    Vec_IntForEachEntry( vFirstTotal, iFanin, i )
    {
        printf( "Obj %5d  Fanin %5d : ", i/3, i%3 );
        printf( "%16s : %d(%d)\n", Wlc_ObjName(p, Vec_IntEntry(vMemObjs, i/3)), iFanin >> 10, iFanin & 0x3FF );
    }
Alan Mishchenko committed
710 711 712 713 714 715
    return vFirstTotal;
}
int Wlc_NtkCexResim( Gia_Man_t * pAbs, Abc_Cex_t * p, Vec_Int_t * vFirstTotal, int iBit, Vec_Wrd_t * vRes, int iFrame )
{
    Gia_Obj_t * pObj, * pObjRi, * pObjRo; 
    int k, b, Entry, First, nBits; word Value;
Alan Mishchenko committed
716
    // assuming that flop outputs have been assigned in the previous timeframe
Alan Mishchenko committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    // assign primary inputs
    Gia_ManForEachPi( pAbs, pObj, k )
        pObj->fMark0 = Abc_InfoHasBit(p->pData, iBit++);
    // simulate
    Gia_ManForEachAnd( pAbs, pObj, k )
        pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) & 
                       (Gia_ObjFanin1(pObj)->fMark0 ^ Gia_ObjFaninC1(pObj));
    // transfer to combinational outputs
    Gia_ManForEachCo( pAbs, pObj, k )
        pObj->fMark0 = Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj);
    // transfer values to flop outputs
    Gia_ManForEachRiRo( pAbs, pObjRi, pObjRo, k )
        pObjRo->fMark0 = pObjRi->fMark0;
    // at this point PIs and FOs area assigned according to this time-frame
    // collect values
    Vec_IntForEachEntry( vFirstTotal, Entry, k )
    {
        if ( Entry == 0 )
        {
Alan Mishchenko committed
736
            Vec_WrdWriteEntry( vRes, Vec_IntSize(vFirstTotal)*iFrame + k, ~(word)0 );
Alan Mishchenko committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750
            continue;
        }
        First = Entry >> 10;
        assert( First < Gia_ManCiNum(pAbs) );
        nBits = Entry & 0x3FF;
        assert( nBits <= 64 );
        Value = 0;
        for ( b = 0; b < nBits; b++ )
            if ( Gia_ManCi(pAbs, First+b)->fMark0 )
                Value |= ((word)1) << b;
        Vec_WrdWriteEntry( vRes, Vec_IntSize(vFirstTotal)*iFrame + k, Value );
    }
    return iBit;
}
Alan Mishchenko committed
751
Vec_Wrd_t * Wlc_NtkConvertCex( Vec_Int_t * vFirstTotal, Gia_Man_t * pAbs, Abc_Cex_t * pCex, int fVerbose )
Alan Mishchenko committed
752 753
{
    Vec_Wrd_t * vValues = Vec_WrdStartFull( Vec_IntSize(vFirstTotal) * (pCex->iFrame + 1) );
Alan Mishchenko committed
754
    int k, f, nBits = pCex->nRegs; word Value;
Alan Mishchenko committed
755 756 757
    Gia_ManCleanMark0(pAbs); // init FOs to zero
    for ( f = 0; f <= pCex->iFrame; f++ )
        nBits = Wlc_NtkCexResim( pAbs, pCex, vFirstTotal, nBits, vValues, f );
Alan Mishchenko committed
758 759 760 761 762 763 764 765 766
    if ( fVerbose )
    Vec_WrdForEachEntry( vValues, Value, k )
    {
        if ( k % Vec_IntSize(vFirstTotal) == 0 )
            printf( "Frame %d:\n", k/Vec_IntSize(vFirstTotal) );
        printf( "Obj %5d  Fanin %5d : ", k/3, k%3 );
        Extra_PrintBinary( stdout, (unsigned *)&Value, 32 );
        printf( "\n" );
    }
Alan Mishchenko committed
767 768 769 770 771
    return vValues;
}

/**Function*************************************************************

Alan Mishchenko committed
772
  Synopsis    [Derives the reason for failure in terms of memory values.]
Alan Mishchenko committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkTrace_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int iFrame, Vec_Int_t * vMemObjs, Vec_Wrd_t * vValues, word ValueA, Vec_Int_t * vRes )
{
    int iObj = Wlc_ObjId(p, pObj);
    int iNum = Wlc_ObjCopy( p, iObj );
    assert( iObj == Vec_IntEntry(vMemObjs, iNum) );
    assert( iFrame >= 0 );
    if ( Wlc_ObjIsPi(pObj) ) 
Alan Mishchenko committed
788 789 790 791 792 793 794 795
        Vec_IntPush( vRes, (iObj << 11) | (iFrame << 1) );
    else if ( Wlc_ObjIsCi(pObj) && iFrame == 0 ) 
    {
        int PiId = Vec_IntEntry(p->vInits, Wlc_ObjCiId(pObj)-Wlc_NtkPiNum(p) );
        int iPiObj = Wlc_ObjId( p, Wlc_NtkPi(p, PiId) );
        Vec_IntPush( vRes, (iPiObj << 11) | (iFrame << 1) );
    }
    else if ( Wlc_ObjIsCi(pObj) )
Alan Mishchenko committed
796 797 798 799 800 801
        Wlc_NtkTrace_rec( p, Wlc_ObjFo2Fi(p, pObj), iFrame - 1, vMemObjs, vValues, ValueA, vRes );
    else if ( Wlc_ObjType(pObj) == WLC_OBJ_BUF )
        Wlc_NtkTrace_rec( p, Wlc_ObjFanin0(p, pObj), iFrame, vMemObjs, vValues, ValueA, vRes );
    else if ( Wlc_ObjType(pObj) == WLC_OBJ_MUX )
    {
        int Index = 3*(iFrame*Vec_IntSize(vMemObjs) + iNum);
Alan Mishchenko committed
802
        int Value = (int)Vec_WrdEntry( vValues, Index );
803
        assert( Value == 0 || Value == 1 );
Alan Mishchenko committed
804 805
        Wlc_NtkTrace_rec( p, Value ? Wlc_ObjFanin2(p, pObj) : Wlc_ObjFanin1(p, pObj), iFrame, vMemObjs, vValues, ValueA, vRes );
        Vec_IntPush( vRes, (iObj << 11) | (iFrame << 1) | Value );
Alan Mishchenko committed
806 807 808 809 810 811
    }
    else if ( Wlc_ObjType(pObj) == WLC_OBJ_WRITE )
    {
        int Index = 3*(iFrame*Vec_IntSize(vMemObjs) + iNum);
        if ( Vec_WrdEntry(vValues, Index + 1) != ValueA ) // address 
            Wlc_NtkTrace_rec( p, Wlc_ObjFanin0(p, pObj), iFrame, vMemObjs, vValues, ValueA, vRes );
Alan Mishchenko committed
812
        Vec_IntPush( vRes, (iObj << 11) | (iFrame << 1) );
Alan Mishchenko committed
813 814 815 816 817
    }
    else assert( 0 );
}
Vec_Int_t * Wlc_NtkTrace( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int iFrame, Vec_Int_t * vMemObjs, Vec_Wrd_t * vValues )
{
Alan Mishchenko committed
818
    int iObj = Wlc_ObjId( p, pObj );
Alan Mishchenko committed
819 820 821 822 823
    int iNum = Wlc_ObjCopy( p, iObj );
    Vec_Int_t * vTrace = Vec_IntAlloc( 10 );
    assert( Wlc_ObjType(pObj) == WLC_OBJ_READ );
    assert( iObj == Vec_IntEntry(vMemObjs, iNum) );
    Wlc_NtkTrace_rec( p, Wlc_ObjFanin0(p, pObj), iFrame, vMemObjs, vValues, Vec_WrdEntry(vValues, 3*(iFrame*Vec_IntSize(vMemObjs) + iNum) + 1), vTrace );
Alan Mishchenko committed
824
    Vec_IntPush( vTrace, (iObj << 11) | (iFrame << 1) );
Alan Mishchenko committed
825 826 827 828 829
    return vTrace;
}
int Wlc_NtkTraceCheckConfict( Wlc_Ntk_t * p, Vec_Int_t * vTrace, Vec_Int_t * vMemObjs, Vec_Wrd_t * vValues )
{
    Wlc_Obj_t * pObjLast, * pObjFirst; 
Alan Mishchenko committed
830 831
    int iObjLast = Vec_IntEntryLast(vTrace) >> 11;
    int iFrmLast =(Vec_IntEntryLast(vTrace) >> 1) & 0x3FF;
Alan Mishchenko committed
832 833
    int iNumLast = Wlc_ObjCopy( p, iObjLast );
    int iIndLast = 3*(iFrmLast*Vec_IntSize(vMemObjs) + iNumLast);
Alan Mishchenko committed
834 835
    int iObjFirst = Vec_IntEntry(vTrace, 0) >> 11;
    int iFrmFirst =(Vec_IntEntry(vTrace, 0) >> 1) & 0x3FF;
Alan Mishchenko committed
836 837 838 839 840 841 842 843 844 845 846
    int iNumFirst = Wlc_ObjCopy( p, iObjFirst );
    int iIndFirst = 3*(iFrmFirst*Vec_IntSize(vMemObjs) + iNumFirst);
    assert( Vec_IntSize(vTrace) >= 2 );
    assert( iObjLast  == Vec_IntEntry(vMemObjs, iNumLast) );
    assert( iObjFirst == Vec_IntEntry(vMemObjs, iNumFirst) );
    pObjLast  = Wlc_NtkObj(p, iObjLast);
    pObjFirst = Wlc_NtkObj(p, iObjFirst);
    assert( Wlc_ObjType(pObjLast) == WLC_OBJ_READ );
    assert( Wlc_ObjType(pObjFirst) == WLC_OBJ_WRITE || Wlc_ObjIsPi(pObjFirst) );
    if ( Wlc_ObjIsPi(pObjFirst) )
        return 0;
Alan Mishchenko committed
847
    assert( Vec_WrdEntry(vValues, iIndLast + 1) == Vec_WrdEntry(vValues, iIndFirst + 1) ); // equal addr
Alan Mishchenko committed
848 849 850 851 852
    return Vec_WrdEntry(vValues, iIndLast + 2) != Vec_WrdEntry(vValues, iIndFirst + 2); // diff data
}
Vec_Int_t * Wlc_NtkFindConflict( Wlc_Ntk_t * p, Vec_Int_t * vMemObjs, Vec_Wrd_t * vValues, int nFrames )
{
    Wlc_Obj_t * pObj; int i, f, Entry;
Alan Mishchenko committed
853
    Vec_Wec_t * vTraces = Vec_WecAlloc( 100 );
Alan Mishchenko committed
854 855
    Vec_Int_t * vTrace, * vTrace1, * vTrace2;
    assert( 3 * nFrames * Vec_IntSize(vMemObjs) == Vec_WrdSize(vValues) );
Alan Mishchenko committed
856 857 858
    Wlc_NtkCleanCopy( p );
    Vec_IntForEachEntry( vMemObjs, Entry, i )
        Wlc_ObjSetCopy( p, Entry, i );
Alan Mishchenko committed
859 860 861 862 863 864
    for ( f = 0; f < nFrames; f++ )
    {
        Wlc_NtkForEachObjVec( vMemObjs, p, pObj, i )
        {
            if ( Wlc_ObjType(pObj) != WLC_OBJ_READ )
                continue;
Alan Mishchenko committed
865
            vTrace = Wlc_NtkTrace( p, pObj, f, vMemObjs, vValues );
Alan Mishchenko committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
            if ( Wlc_NtkTraceCheckConfict( p, vTrace, vMemObjs, vValues ) )
            {
                Vec_WecFree( vTraces );
                return vTrace;
            }
            Vec_WecPushLevel( vTraces );
            Vec_IntAppend( Vec_WecEntryLast(vTraces), vTrace );
            Vec_IntFree( vTrace );
        }
    }
    // check if there are any common read addresses
    Vec_WecForEachLevel( vTraces, vTrace1, i )
    Vec_WecForEachLevelStop( vTraces, vTrace2, f, i )
        if ( Vec_IntEntry(vTrace1, 0) == Vec_IntEntry(vTrace2, 0) )
        {
Alan Mishchenko committed
881 882
            int iObj1 = Vec_IntEntryLast(vTrace1) >> 11;
            int iFrm1 =(Vec_IntEntryLast(vTrace1) >> 1) & 0x3FF;
Alan Mishchenko committed
883
            int iNum1 = Wlc_ObjCopy( p, iObj1 );
Alan Mishchenko committed
884 885 886 887
            int iInd1 = 3*(iFrm1*Vec_IntSize(vMemObjs) + iNum1);

            int iObj2 = Vec_IntEntryLast(vTrace2) >> 11;
            int iFrm2 =(Vec_IntEntryLast(vTrace2) >> 1) & 0x3FF;
Alan Mishchenko committed
888
            int iNum2 = Wlc_ObjCopy( p, iObj2 );
Alan Mishchenko committed
889 890
            int iInd2 = 3*(iFrm2*Vec_IntSize(vMemObjs) + iNum2);

Alan Mishchenko committed
891 892
            assert( iObj1 == Vec_IntEntry(vMemObjs, iNum1) );
            assert( iObj2 == Vec_IntEntry(vMemObjs, iNum2) );
Alan Mishchenko committed
893 894
            if ( Vec_WrdEntry(vValues, iInd1 + 1) == Vec_WrdEntry(vValues, iInd2 + 1) &&  // equal address
                 Vec_WrdEntry(vValues, iInd1 + 2) != Vec_WrdEntry(vValues, iInd2 + 2) )   // diff data
Alan Mishchenko committed
895 896 897
            {
                vTrace = Vec_IntAlloc( 100 );
                Vec_IntPush( vTrace, Vec_IntPop(vTrace1) );
Alan Mishchenko committed
898
                Vec_IntForEachEntryStart( vTrace1, Entry, i, 1 )
Alan Mishchenko committed
899
                    Vec_IntPushUnique( vTrace, Entry );
Alan Mishchenko committed
900
                Vec_IntForEachEntryStart( vTrace2, Entry, i, 1 )
Alan Mishchenko committed
901 902 903 904 905 906 907 908
                    Vec_IntPushUnique( vTrace, Entry );
                Vec_WecFree( vTraces );
                return vTrace;
            }
        }
    Vec_WecFree( vTraces );
    return NULL;
}
Alan Mishchenko committed
909
void Wlc_NtkPrintConflict( Wlc_Ntk_t * p, Vec_Int_t * vTrace )
Alan Mishchenko committed
910
{
Alan Mishchenko committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    int Entry, i;
    printf( "Memory semantics failure trace:\n" );
    Vec_IntForEachEntry( vTrace, Entry, i )
        printf( "%3d: entry %9d : obj %5d with name %16s in frame %d\n", i, Entry, Entry >> 11, Wlc_ObjName(p, Entry>>11), (Entry >> 1) & 0x3FF );
}
void Wlc_NtkPrintCex( Wlc_Ntk_t * p, Wlc_Ntk_t * pAbs, Abc_Cex_t * pCex )
{
    Wlc_Obj_t * pObj; int i, k, f, nBits = pCex ? pCex->nRegs : 0;
    if ( pCex == NULL )
    {
        printf( "The CEX is NULL.\n" );
        return;
    }
    for ( f = 0; f <= pCex->iFrame; f++ )
    {
        printf( "Frame%02d ", f );
        Wlc_NtkForEachPi( pAbs, pObj, i )
        {
            printf( "PI%d:", i );
            //printf( "%d(%d):", nBits, Wlc_ObjRange(pObj) );
            for ( k = 0; k < Wlc_ObjRange(pObj); k++ )
                printf( "%d", Abc_InfoHasBit(pCex->pData,  nBits++) );
            printf( " " );
        }
        printf( "FF:" );
        for ( k = 0; nBits < pCex->nPis; k++ )
            printf( "%d", Abc_InfoHasBit(pCex->pData, nBits++) );
        printf( "\n" );
    }

Alan Mishchenko committed
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
}

/**Function*************************************************************

  Synopsis    [Perform abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Wlc_Ntk_t * Wlc_NtkMemAbstractTest( Wlc_Ntk_t * p )
{
    int iFirstMemPi, iFirstCi, iFirstMemCi, nDcBits;
Alan Mishchenko committed
957
    Vec_Int_t * vRefine;
958
    Vec_Int_t * vMemObjs    = Wlc_NtkCollectMemory( p, 0 );
Alan Mishchenko committed
959 960 961 962
    Vec_Int_t * vMemFanins  = Wlc_NtkCollectMemFanins( p, vMemObjs );

    Vec_Wec_t * vRefines    = Vec_WecAlloc( 100 );
    Vec_Int_t * vNodeFrames = Vec_IntAlloc( 100 );
Alan Mishchenko committed
963 964 965 966 967 968 969 970 971 972 973 974
    Wlc_Ntk_t * pNewFull;

    vRefine     = Vec_WecPushLevel(vRefines);
    Vec_IntPush( vRefine,  (11 << 11) | 0 );
    Vec_IntPush( vRefine,  (10 << 11) | 0 );
    Vec_IntPush( vRefine,  ( 8 << 11) | 0 );
    Vec_IntPush( vRefine,  ( 9 << 11) | 0 );
    Wlc_NtkAbsAddToNodeFrames( vNodeFrames, vRefine );

//    pNewFull    = Wlc_NtkAbstractMemory( p, vMemObjs, vMemFanins, &iFirstMemPi, &iFirstCi, &iFirstMemCi, NULL, NULL );
    pNewFull    = Wlc_NtkAbstractMemory( p, vMemObjs, NULL, &iFirstMemPi, &iFirstCi, &iFirstMemCi, vRefines, vNodeFrames );

Alan Mishchenko committed
975 976 977 978 979
    Vec_WecFree( vRefines );
    Vec_IntFree( vNodeFrames );

    nDcBits     = Wlc_CountDcs( pNewFull->pInits );
    printf( "iFirstMemPi = %d  iFirstCi = %d  iFirstMemCi = %d  nDcBits = %d\n", iFirstMemPi, iFirstCi, iFirstMemCi, nDcBits );
Alan Mishchenko committed
980

Alan Mishchenko committed
981 982 983 984 985
    Vec_IntFreeP( &vMemObjs );
    Vec_IntFreeP( &vMemFanins );
    return pNewFull;
}

Alan Mishchenko committed
986
int Wlc_NtkMemAbstract( Wlc_Ntk_t * p, int nIterMax, int fDumpAbs, int fPdrVerbose, int fVerbose )
Alan Mishchenko committed
987 988
{
    abctime clk = Abc_Clock();
Alan Mishchenko committed
989 990 991
    Wlc_Ntk_t * pNewFull, * pNew; Aig_Man_t * pAig, * pTempAig;
    Gia_Man_t * pAbsFull, * pAbs;
    Abc_Cex_t * pCex = NULL;  Vec_Wrd_t * vValues = NULL; 
Alan Mishchenko committed
992 993 994
    Vec_Wec_t * vRefines = Vec_WecAlloc( 100 );
    Vec_Int_t * vNodeFrames = Vec_IntAlloc( 100 );
    Vec_Int_t * vMemObjs, * vMemFanins, * vFirstTotal, * vRefine; 
Alan Mishchenko committed
995
    int RetValue = -1, iFirstMemPi, iFirstCi, iFirstMemCi, nDcBits, nIters;
Alan Mishchenko committed
996

997
    vMemObjs    = Wlc_NtkCollectMemory( p, 0 );
Alan Mishchenko committed
998
    vMemFanins  = Wlc_NtkCollectMemFanins( p, vMemObjs );
Alan Mishchenko committed
999
    pNewFull    = Wlc_NtkAbstractMemory( p, vMemObjs, vMemFanins, &iFirstMemPi, &iFirstCi, &iFirstMemCi, NULL, NULL );
Alan Mishchenko committed
1000
    nDcBits     = Wlc_CountDcs( pNewFull->pInits );
Alan Mishchenko committed
1001
    vFirstTotal = Wlc_NtkDeriveFirstTotal( p, vMemObjs, vMemFanins, iFirstMemPi, nDcBits + iFirstMemCi, fVerbose );
Alan Mishchenko committed
1002 1003 1004

    pAbsFull    = Wlc_NtkBitBlast( pNewFull, NULL );
    assert( Gia_ManPiNum(pAbsFull) == iFirstCi + nDcBits );
Alan Mishchenko committed
1005
    Wlc_NtkFree( pNewFull );
Alan Mishchenko committed
1006 1007

    // perform abstraction
Alan Mishchenko committed
1008
    for ( nIters = 0; nIters < nIterMax; nIters++ )
Alan Mishchenko committed
1009 1010 1011 1012 1013 1014 1015 1016
    {
        // set up parameters to run PDR
        Pdr_Par_t PdrPars, * pPdrPars = &PdrPars;
        Pdr_ManSetDefaultParams( pPdrPars );
        pPdrPars->fUseAbs    = 0;   // use 'pdr -t'  (on-the-fly abstraction)
        pPdrPars->fVerbose   = fVerbose;

        // derive specific abstraction
Alan Mishchenko committed
1017
        pNew = Wlc_NtkAbstractMemory( p, vMemObjs, NULL, &iFirstMemPi, &iFirstCi, &iFirstMemCi, vRefines, vNodeFrames );
Alan Mishchenko committed
1018 1019
        pAbs = Wlc_NtkBitBlast( pNew, NULL );
        // simplify the AIG
Alan Mishchenko committed
1020 1021
        //pAbs = Gia_ManSeqStructSweep( pGiaTemp = pAbs, 1, 1, 0 );
        //Gia_ManStop( pGiaTemp );
Alan Mishchenko committed
1022

Alan Mishchenko committed
1023
        // roll in the constraints
Alan Mishchenko committed
1024 1025
        pAig = Gia_ManToAigSimple( pAbs );
        Gia_ManStop( pAbs );
Alan Mishchenko committed
1026 1027 1028 1029 1030 1031 1032 1033
        pAig->nConstrs = 1;
        pAig = Saig_ManDupFoldConstrsFunc( pTempAig = pAig, 0, 0 );
        Aig_ManStop( pTempAig );
        pAbs = Gia_ManFromAigSimple( pAig );
        Aig_ManStop( pAig );

        // try to prove abstracted GIA by converting it to AIG and calling PDR
        pAig = Gia_ManToAigSimple( pAbs );
Alan Mishchenko committed
1034 1035 1036 1037
        RetValue = Pdr_ManSolve( pAig, pPdrPars );
        pCex = pAig->pSeqModel; pAig->pSeqModel = NULL;
        Aig_ManStop( pAig );

Alan Mishchenko committed
1038 1039 1040 1041 1042 1043
        if ( fVerbose )
            printf( "\nITERATIONS %d:\n", nIters );
        if ( fVerbose )
            Wlc_NtkPrintCex( p, pNew, pCex );
        Wlc_NtkFree( pNew );

Alan Mishchenko committed
1044 1045 1046
        if ( fDumpAbs )
        {
            char * pFileName = "mem_abs.aig";
1047
            Gia_AigerWrite( pAbs, pFileName, 0, 0, 0 );
Alan Mishchenko committed
1048 1049 1050
            printf( "Iteration %3d: Dumped abstraction in file \"%s\" after finding CEX in frame %d.\n", nIters, pFileName, pCex ? pCex->iFrame : -1 );
        }

Alan Mishchenko committed
1051 1052 1053 1054
        // check if proved or undecided
        if ( pCex == NULL ) 
        {
            assert( RetValue );
Alan Mishchenko committed
1055
            Gia_ManStop( pAbs );
Alan Mishchenko committed
1056 1057 1058 1059
            break;
        }

        // analyze counter-example
Alan Mishchenko committed
1060 1061
        vValues = Wlc_NtkConvertCex( vFirstTotal, pAbsFull, pCex, fVerbose );
        Gia_ManStop( pAbs );
Alan Mishchenko committed
1062 1063
        vRefine = Wlc_NtkFindConflict( p, vMemObjs, vValues, pCex->iFrame + 1 );
        Vec_WrdFree( vValues );
Alan Mishchenko committed
1064
        if ( vRefine == NULL ) // cannot refine
Alan Mishchenko committed
1065
            break;
Alan Mishchenko committed
1066
        Abc_CexFreeP( &pCex );
Alan Mishchenko committed
1067 1068

        // save refinement for the future
Alan Mishchenko committed
1069 1070
        if ( fVerbose )
            Wlc_NtkPrintConflict( p, vRefine );
Alan Mishchenko committed
1071 1072 1073 1074 1075 1076
        Vec_WecPushLevel( vRefines );
        Vec_IntAppend( Vec_WecEntryLast(vRefines), vRefine );
        Wlc_NtkAbsAddToNodeFrames( vNodeFrames, vRefine );
        Vec_IntFree( vRefine );
    }
    // cleanup
Alan Mishchenko committed
1077
    Gia_ManStop( pAbsFull );
Alan Mishchenko committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    Vec_WecFree( vRefines );
    Vec_IntFreeP( &vMemObjs );
    Vec_IntFreeP( &vMemFanins );
    Vec_IntFreeP( &vFirstTotal );
    Vec_IntFreeP( &vNodeFrames );

    // report the result
    if ( fVerbose )
        printf( "\n" );
    printf( "Abstraction " );
Alan Mishchenko committed
1088 1089
    if ( RetValue == 0 && pCex )
        printf( "resulted in a real CEX in frame %d", pCex->iFrame );
Alan Mishchenko committed
1090 1091 1092 1093 1094 1095
    else if ( RetValue == 1 )
        printf( "is successfully proved" );
    else 
        printf( "timed out" );
    printf( " after %d iterations. ", nIters );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
Alan Mishchenko committed
1096
    Abc_CexFreeP( &pCex ); // return CEX in the future
Alan Mishchenko committed
1097 1098 1099 1100
    return RetValue;
}


1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
/**Function*************************************************************

  Synopsis    [Collect visited objects.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Wlc_NtkExploreMem2_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vCollect, int nFrames )
{
    int k, iFanin, nVisited = 0;
    if ( pObj->Mark == 0 )
        return 0;
    if ( Wlc_ObjType(pObj) == WLC_OBJ_PI || (Wlc_ObjType(pObj) == WLC_OBJ_FO && nFrames == 0) )
    {
        Vec_IntPushTwo( vCollect, Wlc_ObjId(p, pObj), nFrames );
        return 1;
    }
    if ( Wlc_ObjType(pObj) == WLC_OBJ_FO )
        return Wlc_NtkExploreMem2_rec( p, Wlc_ObjFo2Fi(p, pObj), vCollect, nFrames-1 );
    Wlc_ObjForEachFanin( pObj, iFanin, k )
        nVisited += Wlc_NtkExploreMem2_rec( p, Wlc_NtkObj(p, iFanin), vCollect, nFrames );
    Vec_IntPushTwo( vCollect, Wlc_ObjId(p, pObj), nFrames );
    return nVisited + 1;
}
void Wlc_NtkExploreMem2( Wlc_Ntk_t * p, int nFrames )
{
    Wlc_Obj_t * pObj; int i, k, Entry, Frame, nVisited;
    Vec_Int_t * vCollect = Vec_IntStart( 1000 );
    Vec_Int_t * vMemObjsClean = Wlc_NtkCollectMemory( p, 1 );
    Wlc_NtkCleanMarks( p );
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
        pObj->Mark = 1;
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) != WLC_OBJ_READ )
            continue;
        Vec_IntClear( vCollect );
        nVisited = Wlc_NtkExploreMem2_rec( p, pObj, vCollect, nFrames );
        printf( "Obj %6d : ", Wlc_ObjId(p, pObj) );
        printf( "Visit = %6d  ", nVisited );
        printf( "Pair = %6d  ", Vec_IntSize(vCollect)/2 );
        if ( Vec_IntSize(vCollect)/2 < 10 )
            Vec_IntForEachEntryDouble( vCollect, Entry, Frame, k )
                printf( "%d(%d) ", Entry, Frame );
        printf( "\n" );
    }
    Vec_IntFree( vMemObjsClean );
    Vec_IntFree( vCollect );
    Wlc_NtkCleanMarks( p );
}

/**Function*************************************************************

  Synopsis    [Collect memory flops reachable.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkExploreMem_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vCollect, int nFrames )
{
    int k, iFanin;
    if ( pObj->Mark == 0 )
        return;
    if ( Wlc_ObjType(pObj) == WLC_OBJ_PI || (Wlc_ObjType(pObj) == WLC_OBJ_FO && nFrames == 0) )
    {
        Vec_IntPushUnique( vCollect, Wlc_ObjId(p, pObj) );
        return;
    }
    if ( Wlc_ObjType(pObj) == WLC_OBJ_FO )
    {
        Wlc_NtkExploreMem_rec( p, Wlc_ObjFo2Fi(p, pObj), vCollect, nFrames-1 );
        return;
    }
    Wlc_ObjForEachFanin( pObj, iFanin, k )
        Wlc_NtkExploreMem_rec( p, Wlc_NtkObj(p, iFanin), vCollect, nFrames );
}
void Wlc_NtkExploreMem( Wlc_Ntk_t * p, int nFrames )
{
    Wlc_Obj_t * pObj; int i, k, iObj;
    Vec_Int_t * vCollect = Vec_IntStart( 1000 );
    Vec_Int_t * vMemObjsClean = Wlc_NtkCollectMemory( p, 1 );
    Wlc_NtkCleanMarks( p );
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
        pObj->Mark = 1;
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
    {
        if ( Wlc_ObjType(pObj) != WLC_OBJ_READ )
            continue;
        Vec_IntClear( vCollect );
        Wlc_NtkExploreMem_rec( p, pObj, vCollect, nFrames );
        printf( "Read port %6d : ", Wlc_ObjId(p, pObj) );
        printf( "Inputs = %6d  ", Vec_IntSize(vCollect) );
        Vec_IntForEachEntry( vCollect, iObj, k )
            printf( "%d(%s) ", iObj, Wlc_ObjName(p, iObj) );
        printf( "\n" );
    }
    Vec_IntFree( vMemObjsClean );
    Vec_IntFree( vCollect );
    Wlc_NtkCleanMarks( p );
}


/**Function*************************************************************

  Synopsis    [For each READ find reachable CIs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Wlc_NtkFindReachablePiFo( Wlc_Ntk_t * p, Vec_Int_t * vMemObjsClean, int nFrames )
{
    Vec_Int_t * vRes = Vec_IntAlloc( 100 );
    Wlc_Obj_t * pObjR, * pObjI, * pObj; 
    int i, j, k, f, fFanin;
    Wlc_NtkForEachObj( p, pObj, i )
        pObj->Mark2 = 0;
    // go through read ports
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObjR, i ) if ( Wlc_ObjIsRead(pObjR) )
    {
        // go through memory CIs
        Wlc_NtkForEachObjVec( vMemObjsClean, p, pObjI, j ) if ( Wlc_ObjIsCi(pObjI) )
        {
            // propagate bit from CI to READ
            pObjI->Mark2 = 1;
            Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, k )
            {
                if ( pObj == pObjI )
                    continue;
                assert( pObj->Mark2 == 0 );
                Wlc_ObjForEachFanin( pObj, fFanin, f )
                    pObj->Mark2 |= Wlc_NtkObj(p, fFanin)->Mark2;
            }
            if ( pObjR->Mark2 )
                Vec_IntPushThree( vRes, Wlc_ObjId(p, pObjR), Wlc_ObjId(p, pObjI), -1 );
            Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, k )
                pObj->Mark2 = 0;
        }
    }
    Wlc_NtkForEachObj( p, pObj, i )
        assert( pObj->Mark2 == 0 );
    return vRes;
}
Vec_Int_t * Wlc_NtkExtractCisForThisRead( Vec_Int_t * vReachReadCi, int iRead )
{
    int k;
    Vec_Int_t * vRes = Vec_IntAlloc( 100 );
    for ( k = 0; k < Vec_IntSize(vReachReadCi)/3; k++ )
    {
        if ( iRead != Vec_IntEntry( vReachReadCi, 3*k ) )
            continue;
        Vec_IntPush( vRes, Vec_IntEntry( vReachReadCi, 3*k+1 ) );
        Vec_IntPush( vRes, Vec_IntEntry( vReachReadCi, 3*k+2 ) );
    }
    return vRes;
}
Vec_Int_t * Wlc_NtkCollectOneType( Wlc_Ntk_t * p, Vec_Int_t * vMemObjsClean, int Type1, int Type2 )
{
    Wlc_Obj_t * pObj; int i;
    Vec_Int_t * vRes = Vec_IntAlloc( 100 );
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
        if ( Wlc_ObjType(pObj) == Type1 || Wlc_ObjType(pObj) == Type2 )
            Vec_IntPush( vRes, Wlc_ObjId(p, pObj) );
    return vRes;
}

/**Function*************************************************************

  Synopsis    [Create memory constraints.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkCreateMemoryConstr( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, Vec_Int_t * vMemObjsClean, Vec_Int_t * vReachReadCi )
{
    Vec_Int_t * vReads  = Wlc_NtkCollectOneType( p, vMemObjsClean, WLC_OBJ_READ, -1 );
    Vec_Int_t * vObjCis = Wlc_NtkCollectOneType( p, vMemObjsClean, WLC_OBJ_PI, WLC_OBJ_FO );
    Vec_Int_t * vFanins = Vec_IntAlloc( 10 );
    Wlc_Obj_t * pObjR, * pObj, * pCond; 
    int i, k, iObjCi, iObjNew = -1;
    // go through read ports
    Wlc_NtkForEachObjVec( vReads, p, pObjR, i )
    {
        // ABC_READ  ( .mem_in(mem_fi1), .addr(raddr), .data(read1) ) ;
        // ABC_WRITE ( .mem_in(mem_fo1), .addr(waddr), .data(data), .mem_out(mem_fi1) ) ;
        // initialize CI related to the read port
        Vec_Int_t * vLocalCis = Wlc_NtkExtractCisForThisRead( vReachReadCi, Wlc_ObjId(p, pObjR) );
        Wlc_NtkForEachObjVec( vObjCis, p, pObj, k )
            Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), -1 );
        Vec_IntForEachEntryDouble( vLocalCis, iObjCi, iObjNew, k )
            Wlc_ObjSetCopy( p, iObjCi, iObjNew );
        Vec_IntFree( vLocalCis );
        // implement the nodes
        Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, k )
        {
            if ( Wlc_ObjIsRead(pObj) || Wlc_ObjIsCi(pObj) )
                continue;
            Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), -1 );
            if ( Wlc_ObjIsWrite(pObj) )
            {
                if ( Wlc_ObjCopy(p, Wlc_ObjFaninId0(pObj)) == -1 )
                    continue;
                if ( Wlc_ObjRange(pObjR) != Wlc_ObjRange(Wlc_ObjFanin2(p, pObj)) )
                    continue;
                // create equality
                pCond = Wlc_NtkObj( pNew, Wlc_ObjAlloc(pNew, WLC_OBJ_COMP_EQU, 0, 0, 0) );
                Vec_IntFillTwo( vFanins, 2, Wlc_ObjCopy(p, Wlc_ObjFaninId1(pObjR)), Wlc_ObjCopy(p, Wlc_ObjFaninId1(pObj)) );
                //printf( "%d : ", Wlc_ObjId(p,pObj) ), Vec_IntPrint( vFanins );
                Wlc_ObjAddFanins( pNew, pCond, vFanins );
                // create MUX
                iObjNew = Wlc_ObjAlloc( pNew, WLC_OBJ_MUX, 0, Wlc_ObjRange(pObjR)-1, 0 );
                Vec_IntFill( vFanins, 1, Wlc_ObjId(pNew, pCond) );
                Vec_IntPush( vFanins, Wlc_ObjCopy(p, Wlc_ObjFaninId2(pObj)) );
                Vec_IntPush( vFanins, Wlc_ObjCopy(p, Wlc_ObjFaninId0(pObj)) );
                Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjNew), vFanins );
            }
            else if ( Wlc_ObjIsMux(pObj) )
            {
                int iFanin0New = Wlc_ObjCopy(p, Wlc_ObjFaninId1(pObj));
                int iFanin1New = Wlc_ObjCopy(p, Wlc_ObjFaninId2(pObj));
                if ( iFanin0New == -1 || iFanin1New == -1 )
                    continue;
                //Wlc_NtkPrintNode( pNew, Wlc_NtkObj(pNew, iFanin0New) );
                assert( Wlc_ObjRange(pObjR) == Wlc_ObjRange(Wlc_NtkObj(pNew, iFanin0New)) );
                assert( Wlc_ObjRange(pObjR) == Wlc_ObjRange(Wlc_NtkObj(pNew, iFanin1New)) );
                iObjNew = Wlc_ObjAlloc( pNew, WLC_OBJ_MUX, 0, Wlc_ObjRange(pObjR)-1, 0 );
                Vec_IntFill( vFanins, 1, Wlc_ObjCopy(p, Wlc_ObjFaninId0(pObj)) );
                Vec_IntPush( vFanins, iFanin0New );
                Vec_IntPush( vFanins, iFanin1New );
                Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjNew), vFanins );
            }
            else if ( Wlc_ObjIsBuf(pObj) )
            {
                iObjNew = Wlc_ObjCopy( p, Wlc_ObjFaninId0(pObj) );
                if ( iObjNew == -1 )
                    continue;
            }
            else assert( 0 );
            Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), iObjNew );
        }
        // extract fanin
        iObjNew = Wlc_ObjCopy( p, Wlc_ObjFaninId0(pObjR) );
        assert( iObjNew != -1 );
        Vec_IntFill( vFanins, 1, iObjNew );
        // add it as buffer fanin
        iObjNew = Wlc_ObjCopy( p, Wlc_ObjId(p, pObjR) );
        assert( Wlc_NtkObj(pNew, iObjNew)->Type == WLC_OBJ_BUF );
        Wlc_ObjAddFanins( pNew, Wlc_NtkObj(pNew, iObjNew), vFanins );
    }
    Wlc_NtkForEachObjVec( vObjCis, p, pObj, k )
        Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), -1 );
    Vec_IntFree( vFanins );
    Vec_IntFree( vReads );
    Vec_IntFree( vObjCis );
}

/**Function*************************************************************

  Synopsis    [Perform abstraction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Wlc_Ntk_t * Wlc_NtkAbstractMem( Wlc_Ntk_t * p, int nFrames, int fVerbose )
{
    Vec_Int_t * vMemObjsAll   = Wlc_NtkCollectMemory( p, 0 );
    Vec_Int_t * vMemObjsClean = Wlc_NtkCollectMemory( p, 1 );
    Vec_Int_t * vReachReadCi  = Wlc_NtkFindReachablePiFo( p, vMemObjsClean, nFrames );
    Wlc_Ntk_t * pNew, * pTemp;
    Wlc_Obj_t * pObj;
    Vec_Int_t * vFanins  = Vec_IntAlloc( 100 );
    int i, Po0, Po1, iObjNew;

    assert( nFrames == 0 );

    // mark memory nodes
    Wlc_NtkCleanMarks( p );
    Wlc_NtkForEachObjVec( vMemObjsAll, p, pObj, i )
        pObj->Mark = 1;

    // start new network
    Wlc_NtkCleanCopy( p );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc + Vec_IntSize(vMemObjsClean) * nFrames * 10 );
    pNew->fSmtLib   = p->fSmtLib;
1404
    pNew->fAsyncRst = p->fAsyncRst;
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs  = p->fEasyFfs;
    pNew->vInits    = Vec_IntAlloc( 100 );

    // duplicate PIs
    Wlc_NtkForEachPi( p, pObj, i )
        if ( !pObj->Mark )
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );

    // create new PIs
    assert( Vec_IntSize(vReachReadCi) % 3 == 0 );
    for ( i = 0; i < Vec_IntSize(vReachReadCi)/3; i++ )
    {
        pObj = Wlc_NtkObj( p, Vec_IntEntry( vReachReadCi, 3*i ) );
        assert( Wlc_ObjIsRead(pObj) );
        iObjNew = Wlc_NtkDupOneObject( pNew, p, pObj, WLC_OBJ_PI, vFanins );
        Vec_IntWriteEntry( vReachReadCi, 3*i+2, iObjNew );
    }
    //Vec_IntPrint( vReachReadCi );

    // duplicate flop outputs
    Wlc_NtkForEachCi( p, pObj, i )
        if ( !Wlc_ObjIsPi(pObj) && !pObj->Mark )
        {
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
            Vec_IntPush( pNew->vInits, Vec_IntEntry(p->vInits, i-Wlc_NtkPiNum(p)) );
        }

/*
    // create flops for memory objects
    Wlc_NtkForEachObjVec( vMemFanins, p, pObj, i )
        for ( k = 0; k < nFrames; k++ )
            Wlc_NtkDupOneObject( pNew, p, pObj, WLC_OBJ_FO, vFanins );
*/

    // create buffers for read ports
    Wlc_NtkForEachObjVec( vMemObjsClean, p, pObj, i )
    {
        if ( !Wlc_ObjIsRead(pObj) )
            continue;
        iObjNew = Wlc_ObjAlloc( pNew, WLC_OBJ_BUF, pObj->Signed, pObj->End, pObj->Beg );
        Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), iObjNew );
    }

    // duplicate objects
    Wlc_NtkForEachObj( p, pObj, i )
        if ( !Wlc_ObjIsCi(pObj) && !pObj->Mark )
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );

    // create memory constraints
    Wlc_NtkCreateMemoryConstr( pNew, p, vMemObjsClean, vReachReadCi );

    // create outpus
    if ( Vec_IntSize(&p->vPoPairs) )
    {
        // create miter for the PO pairs
        Vec_IntForEachEntryDouble( &p->vPoPairs, Po0, Po1, i )
        {
            int iCopy0 = Wlc_ObjCopy(p, Wlc_ObjId(p, Wlc_NtkPo(p, Po0)));
            int iCopy1 = Wlc_ObjCopy(p, Wlc_ObjId(p, Wlc_NtkPo(p, Po1)));
            int iObj = Wlc_ObjAlloc( pNew, WLC_OBJ_COMP_NOTEQU, 0, 0, 0 );
            Wlc_Obj_t * pObjNew = Wlc_NtkObj( pNew, iObj );
            Vec_IntFillTwo( vFanins, 1, iCopy0, iCopy1 );
            Wlc_ObjAddFanins( pNew, pObjNew, vFanins );
            Wlc_ObjSetCo( pNew, pObjNew, 0 );
        }
        printf( "Memory abstraction created %d miter outputs.\n", Wlc_NtkPoNum(pNew) );
        Wlc_NtkForEachCo( p, pObj, i )
            if ( pObj->fIsFi && !pObj->Mark )
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
    }
    else
    {
        // duplicate POs
        Wlc_NtkForEachPo( p, pObj, i )
            if ( !pObj->Mark )
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
        // duplicate FIs
        Wlc_NtkForEachCo( p, pObj, i )
            if ( !Wlc_ObjIsPo(pObj) && !pObj->Mark )
                Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
    }

/*
    // create new flop inputs
    Wlc_NtkForEachObjVec( vMemFanins, p, pObj, i )
        for ( k = 0; k < nFrames; k++ )
            Wlc_NtkDupOneBuffer( pNew, p, pObj, Wlc_ObjCopy(p, Wlc_ObjId(p, pObj))-(nFrames-1-k), vFanins, 1 );
*/

    // append init states
    pNew->pInits = Wlc_PrsConvertInitValues( pNew );
    if ( p->pSpec )
        pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    if ( Wlc_NtkHasNameId(p) )
        Wlc_NtkTransferNames( pNew, p );
    Vec_IntFree( vFanins );
    Vec_IntFree( vMemObjsAll );
    Vec_IntFree( vMemObjsClean );
    Vec_IntFree( vReachReadCi );
    Wlc_NtkCleanMarks( p );
    // derive topological order
//printf( "Objects before = %d.\n", Wlc_NtkObjNum(pNew) );
    pNew = Wlc_NtkDupDfs( pTemp = pNew, 0, 1 );
    Wlc_NtkFree( pTemp );
//printf( "Objects after = %d.\n", Wlc_NtkObjNum(pNew) );
    return pNew;
}



Alan Mishchenko committed
1516 1517 1518 1519 1520 1521 1522
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END