csat_apis.c 23.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
/**CFile****************************************************************

  FileName    [csat_apis.h]

  PackageName [Interface to CSAT.]

  Synopsis    [APIs, enums, and data structures expected from the use of CSAT.]

  Author      [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 28, 2005]

  Revision    [$Id: csat_apis.h,v 1.00 2005/08/28 00:00:00 alanmi Exp $]

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

19 20
#include "base/abc/abc.h"
#include "proof/fraig/fraig.h"
Alan Mishchenko committed
21
#include "csat_apis.h"
22 23
#include "misc/st/stmm.h"
#include "base/main/main.h"
24 25 26

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
27 28 29 30 31

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

Alan Mishchenko committed
32 33 34 35 36
#define ABC_DEFAULT_CONF_LIMIT     0   // limit on conflicts
#define ABC_DEFAULT_IMP_LIMIT      0   // limit on implications


struct ABC_ManagerStruct_t
Alan Mishchenko committed
37 38 39
{
    // information about the problem
    stmm_table *          tName2Node;    // the hash table mapping names to nodes
Alan Mishchenko committed
40
    stmm_table *          tNode2Name;    // the hash table mapping nodes to names
Alan Mishchenko committed
41
    Abc_Ntk_t *           pNtk;          // the starting ABC network
Alan Mishchenko committed
42
    Abc_Ntk_t *           pTarget;       // the AIG representing the target
Alan Mishchenko committed
43
    char *                pDumpFileName; // the name of the file to dump the target network
44
    Mem_Flex_t *      pMmNames;      // memory manager for signal names
Alan Mishchenko committed
45
    // solving parameters
Alan Mishchenko committed
46 47
    int                   mode;          // 0 = resource-aware integration; 1 = brute-force SAT
    Prove_Params_t        Params;        // integrated CEC parameters
Alan Mishchenko committed
48 49 50 51 52
    // information about the target 
    int                   nog;           // the numbers of gates in the target
    Vec_Ptr_t *           vNodes;        // the gates in the target
    Vec_Int_t *           vValues;       // the values of gate's outputs in the target
    // solution
Alan Mishchenko committed
53
    CSAT_Target_ResultT * pResult;       // the result of solving the target
Alan Mishchenko committed
54 55
};

Alan Mishchenko committed
56 57 58 59 60 61
static CSAT_Target_ResultT * ABC_TargetResAlloc( int nVars );
static char * ABC_GetNodeName( ABC_Manager mng, Abc_Obj_t * pNode );

// procedures to start and stop the ABC framework
extern void  Abc_Start();
extern void  Abc_Stop();
Alan Mishchenko committed
62

Alan Mishchenko committed
63
// some external procedures
64
extern int Io_WriteBench( Abc_Ntk_t * pNtk, const char * FileName );
Alan Mishchenko committed
65 66

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
67
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Creates a new manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
81
ABC_Manager ABC_InitManager()
Alan Mishchenko committed
82
{
Alan Mishchenko committed
83 84
    ABC_Manager_t * mng;
    Abc_Start();
Alan Mishchenko committed
85
    mng = ABC_ALLOC( ABC_Manager_t, 1 );
Alan Mishchenko committed
86 87 88
    memset( mng, 0, sizeof(ABC_Manager_t) );
    mng->pNtk = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 );
    mng->pNtk->pName = Extra_UtilStrsav("csat_network");
Alan Mishchenko committed
89
    mng->tName2Node = stmm_init_table(strcmp, stmm_strhash);
Alan Mishchenko committed
90
    mng->tNode2Name = stmm_init_table(stmm_ptrcmp, stmm_ptrhash);
91
    mng->pMmNames   = Mem_FlexStart();
Alan Mishchenko committed
92 93
    mng->vNodes     = Vec_PtrAlloc( 100 );
    mng->vValues    = Vec_IntAlloc( 100 );
Alan Mishchenko committed
94 95 96 97 98
    mng->mode       = 0; // set "resource-aware integration" as the default mode
    // set default parameters for CEC
    Prove_ParamsSetDefault( &mng->Params );
    // set infinite resource limit for the final mitering
//    mng->Params.nMiteringLimitLast = ABC_INFINITY;
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    return mng;
}

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

  Synopsis    [Deletes the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
113
void ABC_ReleaseManager( ABC_Manager mng )
Alan Mishchenko committed
114
{
Alan Mishchenko committed
115 116
    CSAT_Target_ResultT * p_res = ABC_Get_Target_Result( mng,0 );
    ABC_TargetResFree(p_res);
Alan Mishchenko committed
117
    if ( mng->tNode2Name ) stmm_free_table( mng->tNode2Name );
Alan Mishchenko committed
118
    if ( mng->tName2Node ) stmm_free_table( mng->tName2Node );
119
    if ( mng->pMmNames )   Mem_FlexStop( mng->pMmNames, 0 );
Alan Mishchenko committed
120 121 122 123
    if ( mng->pNtk )       Abc_NtkDelete( mng->pNtk );
    if ( mng->pTarget )    Abc_NtkDelete( mng->pTarget );
    if ( mng->vNodes )     Vec_PtrFree( mng->vNodes );
    if ( mng->vValues )    Vec_IntFree( mng->vValues );
Alan Mishchenko committed
124 125
    ABC_FREE( mng->pDumpFileName );
    ABC_FREE( mng );
Alan Mishchenko committed
126
    Abc_Stop();
Alan Mishchenko committed
127 128 129 130 131 132
}

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

  Synopsis    [Sets solver options for learning.]

Alan Mishchenko committed
133
  Description []
Alan Mishchenko committed
134 135 136 137 138 139
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
140
void ABC_SetSolveOption( ABC_Manager mng, enum CSAT_OptionT option )
Alan Mishchenko committed
141 142 143
{
}

Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**Function*************************************************************

  Synopsis    [Sets solving mode by brute-force SAT.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void ABC_UseOnlyCoreSatSolver( ABC_Manager mng )
{
    mng->mode = 1;  // switch to "brute-force SAT" as the solving option
}
Alan Mishchenko committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

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

  Synopsis    [Adds a gate to the circuit.]

  Description [The meaning of the parameters are:
    type: the type of the gate to be added
    name: the name of the gate to be added, name should be unique in a circuit.
    nofi: number of fanins of the gate to be added;
    fanins: the name array of fanins of the gate to be added.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
175
int ABC_AddGate( ABC_Manager mng, enum GateType type, char * name, int nofi, char ** fanins, int dc_attr )
Alan Mishchenko committed
176
{
Alan Mishchenko committed
177 178 179 180
    Abc_Obj_t * pObj = NULL; // Suppress "might be used uninitialized"
    Abc_Obj_t * pFanin;
    char * pSop = NULL; // Suppress "might be used uninitialized"
    char * pNewName;
Alan Mishchenko committed
181 182
    int i;

Alan Mishchenko committed
183
    // save the name in the local memory manager
184
    pNewName = Mem_FlexEntryFetch( mng->pMmNames, strlen(name) + 1 );
Alan Mishchenko committed
185 186 187 188
    strcpy( pNewName, name );
    name = pNewName;

    // consider different cases, create the node, and map the node into the name
Alan Mishchenko committed
189 190
    switch( type )
    {
Alan Mishchenko committed
191 192
    case CSAT_BPI:
    case CSAT_BPPI:
Alan Mishchenko committed
193
        if ( nofi != 0 )
Alan Mishchenko committed
194
            { printf( "ABC_AddGate: The PI/PPI gate \"%s\" has fanins.\n", name ); return 0; }
Alan Mishchenko committed
195 196
        // create the PI
        pObj = Abc_NtkCreatePi( mng->pNtk );
Alan Mishchenko committed
197
        stmm_insert( mng->tNode2Name, (char *)pObj, name );
Alan Mishchenko committed
198
        break;
Alan Mishchenko committed
199 200 201 202 203 204 205 206 207
    case CSAT_CONST:
    case CSAT_BAND:
    case CSAT_BNAND:
    case CSAT_BOR:
    case CSAT_BNOR:
    case CSAT_BXOR:
    case CSAT_BXNOR:
    case CSAT_BINV:
    case CSAT_BBUF:
Alan Mishchenko committed
208 209 210 211 212 213
        // create the node
        pObj = Abc_NtkCreateNode( mng->pNtk );
        // create the fanins
        for ( i = 0; i < nofi; i++ )
        {
            if ( !stmm_lookup( mng->tName2Node, fanins[i], (char **)&pFanin ) )
Alan Mishchenko committed
214
                { printf( "ABC_AddGate: The fanin gate \"%s\" is not in the network.\n", fanins[i] ); return 0; }
Alan Mishchenko committed
215 216 217 218 219
            Abc_ObjAddFanin( pObj, pFanin );
        }
        // create the node function
        switch( type )
        {
Alan Mishchenko committed
220
            case CSAT_CONST:
Alan Mishchenko committed
221
                if ( nofi != 0 )
Alan Mishchenko committed
222
                    { printf( "ABC_AddGate: The constant gate \"%s\" has fanins.\n", name ); return 0; }
223
                pSop = Abc_SopCreateConst1( (Mem_Flex_t *)mng->pNtk->pManFunc );
Alan Mishchenko committed
224
                break;
Alan Mishchenko committed
225
            case CSAT_BAND:
Alan Mishchenko committed
226
                if ( nofi < 1 )
Alan Mishchenko committed
227
                    { printf( "ABC_AddGate: The AND gate \"%s\" no fanins.\n", name ); return 0; }
228
                pSop = Abc_SopCreateAnd( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi, NULL );
Alan Mishchenko committed
229
                break;
Alan Mishchenko committed
230
            case CSAT_BNAND:
Alan Mishchenko committed
231
                if ( nofi < 1 )
Alan Mishchenko committed
232
                    { printf( "ABC_AddGate: The NAND gate \"%s\" no fanins.\n", name ); return 0; }
233
                pSop = Abc_SopCreateNand( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi );
Alan Mishchenko committed
234
                break;
Alan Mishchenko committed
235
            case CSAT_BOR:
Alan Mishchenko committed
236
                if ( nofi < 1 )
Alan Mishchenko committed
237
                    { printf( "ABC_AddGate: The OR gate \"%s\" no fanins.\n", name ); return 0; }
238
                pSop = Abc_SopCreateOr( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi, NULL );
Alan Mishchenko committed
239
                break;
Alan Mishchenko committed
240
            case CSAT_BNOR:
Alan Mishchenko committed
241
                if ( nofi < 1 )
Alan Mishchenko committed
242
                    { printf( "ABC_AddGate: The NOR gate \"%s\" no fanins.\n", name ); return 0; }
243
                pSop = Abc_SopCreateNor( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi );
Alan Mishchenko committed
244
                break;
Alan Mishchenko committed
245
            case CSAT_BXOR:
Alan Mishchenko committed
246
                if ( nofi < 1 )
Alan Mishchenko committed
247
                    { printf( "ABC_AddGate: The XOR gate \"%s\" no fanins.\n", name ); return 0; }
Alan Mishchenko committed
248
                if ( nofi > 2 )
Alan Mishchenko committed
249
                    { printf( "ABC_AddGate: The XOR gate \"%s\" has more than two fanins.\n", name ); return 0; }
250
                pSop = Abc_SopCreateXor( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi );
Alan Mishchenko committed
251
                break;
Alan Mishchenko committed
252
            case CSAT_BXNOR:
Alan Mishchenko committed
253
                if ( nofi < 1 )
Alan Mishchenko committed
254
                    { printf( "ABC_AddGate: The XNOR gate \"%s\" no fanins.\n", name ); return 0; }
Alan Mishchenko committed
255
                if ( nofi > 2 )
Alan Mishchenko committed
256
                    { printf( "ABC_AddGate: The XNOR gate \"%s\" has more than two fanins.\n", name ); return 0; }
257
                pSop = Abc_SopCreateNxor( (Mem_Flex_t *)mng->pNtk->pManFunc, nofi );
Alan Mishchenko committed
258
                break;
Alan Mishchenko committed
259
            case CSAT_BINV:
Alan Mishchenko committed
260
                if ( nofi != 1 )
Alan Mishchenko committed
261
                    { printf( "ABC_AddGate: The inverter gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
262
                pSop = Abc_SopCreateInv( (Mem_Flex_t *)mng->pNtk->pManFunc );
Alan Mishchenko committed
263
                break;
Alan Mishchenko committed
264
            case CSAT_BBUF:
Alan Mishchenko committed
265
                if ( nofi != 1 )
Alan Mishchenko committed
266
                    { printf( "ABC_AddGate: The buffer gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
267
                pSop = Abc_SopCreateBuf( (Mem_Flex_t *)mng->pNtk->pManFunc );
Alan Mishchenko committed
268 269 270 271 272 273
                break;
            default :
                break;
        }
        Abc_ObjSetData( pObj, pSop );
        break;
Alan Mishchenko committed
274 275
    case CSAT_BPPO:
    case CSAT_BPO:
Alan Mishchenko committed
276
        if ( nofi != 1 )
Alan Mishchenko committed
277
            { printf( "ABC_AddGate: The PO/PPO gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
Alan Mishchenko committed
278 279
        // create the PO
        pObj = Abc_NtkCreatePo( mng->pNtk );
Alan Mishchenko committed
280
        stmm_insert( mng->tNode2Name, (char *)pObj, name );
Alan Mishchenko committed
281 282
        // connect to the PO fanin
        if ( !stmm_lookup( mng->tName2Node, fanins[0], (char **)&pFanin ) )
Alan Mishchenko committed
283
            { printf( "ABC_AddGate: The fanin gate \"%s\" is not in the network.\n", fanins[0] ); return 0; }
Alan Mishchenko committed
284 285 286
        Abc_ObjAddFanin( pObj, pFanin );
        break;
    default:
Alan Mishchenko committed
287
        printf( "ABC_AddGate: Unknown gate type.\n" );
Alan Mishchenko committed
288 289
        break;
    }
Alan Mishchenko committed
290 291

    // map the name into the node
Alan Mishchenko committed
292
    if ( stmm_insert( mng->tName2Node, name, (char *)pObj ) )
Alan Mishchenko committed
293
        { printf( "ABC_AddGate: The same gate \"%s\" is added twice.\n", name ); return 0; }
Alan Mishchenko committed
294 295 296 297 298
    return 1;
}

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

Alan Mishchenko committed
299
  Synopsis    [This procedure also finalizes construction of the ABC network.]
Alan Mishchenko committed
300

Alan Mishchenko committed
301
  Description []
Alan Mishchenko committed
302 303 304 305 306 307
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
308
void ABC_Network_Finalize( ABC_Manager mng )
Alan Mishchenko committed
309 310 311 312 313
{
    Abc_Ntk_t * pNtk = mng->pNtk;
    Abc_Obj_t * pObj;
    int i;
    Abc_NtkForEachPi( pNtk, pObj, i )
Alan Mishchenko committed
314
        Abc_ObjAssignName( pObj, ABC_GetNodeName(mng, pObj), NULL );
Alan Mishchenko committed
315
    Abc_NtkForEachPo( pNtk, pObj, i )
Alan Mishchenko committed
316
        Abc_ObjAssignName( pObj, ABC_GetNodeName(mng, pObj), NULL );
Alan Mishchenko committed
317
    assert( Abc_NtkLatchNum(pNtk) == 0 );
Alan Mishchenko committed
318
}
Alan Mishchenko committed
319

Alan Mishchenko committed
320 321 322 323 324 325 326 327
/**Function*************************************************************

  Synopsis    [Checks integraty of the manager.]

  Description [Checks if there are gates that are not used by any primary output.
  If no such gates exist, return 1 else return 0.]
               
  SideEffects []
Alan Mishchenko committed
328

Alan Mishchenko committed
329 330 331 332 333 334 335 336 337 338
  SeeAlso     []

***********************************************************************/
int ABC_Check_Integrity( ABC_Manager mng )
{
    Abc_Ntk_t * pNtk = mng->pNtk;
    Abc_Obj_t * pObj;
    int i;

    // check that there are no dangling nodes
Alan Mishchenko committed
339 340
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
Alan Mishchenko committed
341 342
        if ( i == 0 ) 
            continue;
Alan Mishchenko committed
343 344
        if ( Abc_ObjFanoutNum(pObj) == 0 )
        {
Alan Mishchenko committed
345
//            printf( "ABC_Check_Integrity: The network has dangling nodes.\n" );
Alan Mishchenko committed
346 347 348
            return 0;
        }
    }
Alan Mishchenko committed
349 350 351 352 353 354 355

    // make sure everything is okay with the network structure
    if ( !Abc_NtkDoCheck( pNtk ) )
    {
        printf( "ABC_Check_Integrity: The internal network check has failed.\n" );
        return 0;
    }
Alan Mishchenko committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369
    return 1;
}

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

  Synopsis    [Sets time limit for solving a target.]

  Description [Runtime: time limit (in second).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
370
void ABC_SetTimeLimit( ABC_Manager mng, int runtime )
Alan Mishchenko committed
371
{
Alan Mishchenko committed
372
//    printf( "ABC_SetTimeLimit: The resource limit is not implemented (warning).\n" );
Alan Mishchenko committed
373 374 375 376 377 378 379 380 381 382 383 384 385
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
386
void ABC_SetLearnLimit( ABC_Manager mng, int num )
Alan Mishchenko committed
387
{
Alan Mishchenko committed
388
//    printf( "ABC_SetLearnLimit: The resource limit is not implemented (warning).\n" );
Alan Mishchenko committed
389 390 391 392 393 394 395 396 397 398 399 400 401
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
402
void ABC_SetLearnBacktrackLimit( ABC_Manager mng, int num )
Alan Mishchenko committed
403
{
Alan Mishchenko committed
404
//    printf( "ABC_SetLearnBacktrackLimit: The resource limit is not implemented (warning).\n" );
Alan Mishchenko committed
405 406 407 408 409 410 411 412 413 414 415 416 417
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
418
void ABC_SetSolveBacktrackLimit( ABC_Manager mng, int num )
Alan Mishchenko committed
419
{
Alan Mishchenko committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    mng->Params.nMiteringLimitLast = num;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void ABC_SetSolveImplicationLimit( ABC_Manager mng, int num )
{
//    printf( "ABC_SetSolveImplicationLimit: The resource limit is not implemented (warning).\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
450
void ABC_SetTotalBacktrackLimit( ABC_Manager mng, ABC_UINT64_T num )
Alan Mishchenko committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
{
    mng->Params.nTotalBacktrackLimit = num;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
466
void ABC_SetTotalInspectLimit( ABC_Manager mng, ABC_UINT64_T num )
Alan Mishchenko committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480
{
    mng->Params.nTotalInspectLimit = num;
}
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
481
ABC_UINT64_T ABC_GetTotalBacktracksMade( ABC_Manager mng )
Alan Mishchenko committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
    return mng->Params.nTotalBacktracksMade;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
497
ABC_UINT64_T ABC_GetTotalInspectsMade( ABC_Manager mng )
Alan Mishchenko committed
498 499
{
    return mng->Params.nTotalInspectsMade;
Alan Mishchenko committed
500 501 502 503 504 505 506 507 508 509 510 511 512
}

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

  Synopsis    [Sets the file name to dump the structurally hashed network used for solving.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
513
void ABC_EnableDump( ABC_Manager mng, char * dump_file )
Alan Mishchenko committed
514
{
Alan Mishchenko committed
515
    ABC_FREE( mng->pDumpFileName );
Alan Mishchenko committed
516
    mng->pDumpFileName = Extra_UtilStrsav( dump_file );
Alan Mishchenko committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
}

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

  Synopsis    [Adds a new target to the manager.]

  Description [The meaning of the parameters are:
    nog: number of gates that are in the targets,
    names: name array of gates,
    values: value array of the corresponding gates given in "names" to be solved. 
    The relation of them is AND.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
534
int ABC_AddTarget( ABC_Manager mng, int nog, char ** names, int * values )
Alan Mishchenko committed
535 536 537 538
{
    Abc_Obj_t * pObj;
    int i;
    if ( nog < 1 )
Alan Mishchenko committed
539
        { printf( "ABC_AddTarget: The target has no gates.\n" ); return 0; }
Alan Mishchenko committed
540 541 542 543 544 545 546 547
    // clear storage for the target
    mng->nog = 0;
    Vec_PtrClear( mng->vNodes );
    Vec_IntClear( mng->vValues );
    // save the target
    for ( i = 0; i < nog; i++ )
    {
        if ( !stmm_lookup( mng->tName2Node, names[i], (char **)&pObj ) )
Alan Mishchenko committed
548
            { printf( "ABC_AddTarget: The target gate \"%s\" is not in the network.\n", names[i] ); return 0; }
Alan Mishchenko committed
549 550
        Vec_PtrPush( mng->vNodes, pObj );
        if ( values[i] < 0 || values[i] > 1 )
Alan Mishchenko committed
551
            { printf( "ABC_AddTarget: The value of gate \"%s\" is not 0 or 1.\n", names[i] ); return 0; }
Alan Mishchenko committed
552 553 554 555 556 557 558 559 560 561 562
        Vec_IntPush( mng->vValues, values[i] );
    }
    mng->nog = nog;
    return 1;
}

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

  Synopsis    [Initialize the solver internal data structure.]

  Description [Prepares the solver to work on one specific target
Alan Mishchenko committed
563
  set by calling ABC_AddTarget before.]
Alan Mishchenko committed
564 565 566 567 568 569
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
570
void ABC_SolveInit( ABC_Manager mng )
Alan Mishchenko committed
571 572 573 574
{
    // check if the target is available
    assert( mng->nog == Vec_PtrSize(mng->vNodes) );
    if ( mng->nog == 0 )
Alan Mishchenko committed
575
        { printf( "ABC_SolveInit: Target is not specified by ABC_AddTarget().\n" ); return; }
Alan Mishchenko committed
576

Alan Mishchenko committed
577
    // free the previous target network if present
Alan Mishchenko committed
578 579 580
    if ( mng->pTarget ) Abc_NtkDelete( mng->pTarget );

    // set the new target network
Alan Mishchenko committed
581 582
//    mng->pTarget = Abc_NtkCreateTarget( mng->pNtk, mng->vNodes, mng->vValues );
    mng->pTarget = Abc_NtkStrash( mng->pNtk, 0, 1, 0 );
Alan Mishchenko committed
583 584 585 586 587 588 589 590 591 592 593 594 595
}

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

  Synopsis    [Currently not implemented.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
596
void ABC_AnalyzeTargets( ABC_Manager mng )
Alan Mishchenko committed
597 598 599 600 601
{
}

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

Alan Mishchenko committed
602
  Synopsis    [Solves the targets added by ABC_AddTarget().]
Alan Mishchenko committed
603 604 605 606 607 608 609 610

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
611
enum CSAT_StatusT ABC_Solve( ABC_Manager mng )
Alan Mishchenko committed
612
{
Alan Mishchenko committed
613
    Prove_Params_t * pParams = &mng->Params;
Alan Mishchenko committed
614 615 616 617
    int RetValue, i;

    // check if the target network is available
    if ( mng->pTarget == NULL )
Alan Mishchenko committed
618
        { printf( "ABC_Solve: Target network is not derived by ABC_SolveInit().\n" ); return UNDETERMINED; }
Alan Mishchenko committed
619

Alan Mishchenko committed
620 621
    // try to prove the miter using a number of techniques
    if ( mng->mode )
Alan Mishchenko committed
622
        RetValue = Abc_NtkMiterSat( mng->pTarget, (ABC_INT64_T)pParams->nMiteringLimitLast, (ABC_INT64_T)0, 0, NULL, NULL );
Alan Mishchenko committed
623 624 625
    else
//        RetValue = Abc_NtkMiterProve( &mng->pTarget, pParams ); // old CEC engine
        RetValue = Abc_NtkIvyProve( &mng->pTarget, pParams ); // new CEC engine
Alan Mishchenko committed
626

Alan Mishchenko committed
627
    // analyze the result
Alan Mishchenko committed
628
    mng->pResult = ABC_TargetResAlloc( Abc_NtkCiNum(mng->pTarget) );
Alan Mishchenko committed
629 630 631 632 633
    if ( RetValue == -1 )
        mng->pResult->status = UNDETERMINED;
    else if ( RetValue == 1 )
        mng->pResult->status = UNSATISFIABLE;
    else if ( RetValue == 0 )
Alan Mishchenko committed
634
    {
Alan Mishchenko committed
635 636 637
        mng->pResult->status = SATISFIABLE;
        // create the array of PI names and values
        for ( i = 0; i < mng->pResult->no_sig; i++ )
Alan Mishchenko committed
638
        {
Alan Mishchenko committed
639 640
            mng->pResult->names[i]  = Extra_UtilStrsav( ABC_GetNodeName(mng, Abc_NtkCi(mng->pNtk, i)) ); 
            mng->pResult->values[i] = mng->pTarget->pModel[i];
Alan Mishchenko committed
641
        }
Alan Mishchenko committed
642
        ABC_FREE( mng->pTarget->pModel );
Alan Mishchenko committed
643
    }
Alan Mishchenko committed
644
    else assert( 0 );
Alan Mishchenko committed
645 646 647 648 649 650 651 652 653 654 655 656

    // delete the target
    Abc_NtkDelete( mng->pTarget );
    mng->pTarget = NULL;
    // return the status
    return mng->pResult->status;
}

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

  Synopsis    [Gets the solve status of a target.]

Alan Mishchenko committed
657
  Description [TargetID: the target id returned by ABC_AddTarget().]
Alan Mishchenko committed
658 659 660 661 662 663
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
664
CSAT_Target_ResultT * ABC_Get_Target_Result( ABC_Manager mng, int TargetID )
Alan Mishchenko committed
665 666 667 668 669 670
{
    return mng->pResult;
}

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

Alan Mishchenko committed
671
  Synopsis    [Dumps the original network into the BENCH file.]
Alan Mishchenko committed
672

Alan Mishchenko committed
673
  Description [This procedure should be modified to dump the target.]
Alan Mishchenko committed
674 675 676
               
  SideEffects []

Alan Mishchenko committed
677
  SeeAlso     [] 
Alan Mishchenko committed
678 679

***********************************************************************/
Alan Mishchenko committed
680
void ABC_Dump_Bench_File( ABC_Manager mng )
Alan Mishchenko committed
681
{
Alan Mishchenko committed
682
    Abc_Ntk_t * pNtkTemp, * pNtkAig;
683
    const char * pFileName;
Alan Mishchenko committed
684
 
Alan Mishchenko committed
685
    // derive the netlist
Alan Mishchenko committed
686 687 688
    pNtkAig = Abc_NtkStrash( mng->pNtk, 0, 0, 0 );
    pNtkTemp = Abc_NtkToNetlistBench( pNtkAig );
    Abc_NtkDelete( pNtkAig );
Alan Mishchenko committed
689
    if ( pNtkTemp == NULL ) 
Alan Mishchenko committed
690
        { printf( "ABC_Dump_Bench_File: Dumping BENCH has failed.\n" ); return; }
Alan Mishchenko committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    pFileName = mng->pDumpFileName? mng->pDumpFileName: "abc_test.bench";
    Io_WriteBench( pNtkTemp, pFileName );
    Abc_NtkDelete( pNtkTemp );
}



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

  Synopsis    [Allocates the target result.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
709
CSAT_Target_ResultT * ABC_TargetResAlloc( int nVars )
Alan Mishchenko committed
710
{
Alan Mishchenko committed
711
    CSAT_Target_ResultT * p;
Alan Mishchenko committed
712
    p = ABC_ALLOC( CSAT_Target_ResultT, 1 );
Alan Mishchenko committed
713
    memset( p, 0, sizeof(CSAT_Target_ResultT) );
Alan Mishchenko committed
714
    p->no_sig = nVars;
Alan Mishchenko committed
715 716
    p->names = ABC_ALLOC( char *, nVars );
    p->values = ABC_ALLOC( int, nVars );
Alan Mishchenko committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    memset( p->names, 0, sizeof(char *) * nVars );
    memset( p->values, 0, sizeof(int) * nVars );
    return p;
}

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

  Synopsis    [Deallocates the target result.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
733
void ABC_TargetResFree( CSAT_Target_ResultT * p )
Alan Mishchenko committed
734 735 736
{
    if ( p == NULL )
        return;
Alan Mishchenko committed
737 738 739 740 741
    if( p->names )
    {
        int i = 0;
        for ( i = 0; i < p->no_sig; i++ )
        {
Alan Mishchenko committed
742
            ABC_FREE(p->names[i]);
Alan Mishchenko committed
743 744
        }
    }
Alan Mishchenko committed
745 746 747
    ABC_FREE( p->names );
    ABC_FREE( p->values );
    ABC_FREE( p );
Alan Mishchenko committed
748 749
}

Alan Mishchenko committed
750 751 752 753 754 755 756 757 758 759 760
/**Function*************************************************************

  Synopsis    [Dumps the target AIG into the BENCH file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
761
char * ABC_GetNodeName( ABC_Manager mng, Abc_Obj_t * pNode )
Alan Mishchenko committed
762 763 764 765 766 767 768 769 770
{
    char * pName = NULL;
    if ( !stmm_lookup( mng->tNode2Name, (char *)pNode, (char **)&pName ) )
    {
        assert( 0 );
    }
    return pName;
}

Alan Mishchenko committed
771

Alan Mishchenko committed
772 773 774 775 776
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


777 778
ABC_NAMESPACE_IMPL_END