ioWriteCnf.c 3.75 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8
/**CFile****************************************************************

  FileName    [ioWriteCnf.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

Alan Mishchenko committed
9
  Synopsis    [Procedures to output CNF of the miter cone.]
Alan Mishchenko committed
10 11 12 13 14 15 16 17 18 19 20

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: ioWriteCnf.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

Alan Mishchenko committed
21
#include "ioAbc.h"
22
#include "sat/bsat/satSolver.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
30 31
 
static Abc_Ntk_t * s_pNtk = NULL;
Alan Mishchenko committed
32 33

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
34
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
35 36 37 38 39 40 41 42 43 44 45 46 47
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Write the miter cone into a CNF file for the SAT solver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
48
int Io_WriteCnf( Abc_Ntk_t * pNtk, char * pFileName, int fAllPrimes )
Alan Mishchenko committed
49
{
Alan Mishchenko committed
50 51 52 53 54 55
    sat_solver * pSat;
    if ( Abc_NtkIsStrash(pNtk) )
        printf( "Io_WriteCnf() warning: Generating CNF by applying heuristic AIG to CNF conversion.\n" );
    else
        printf( "Io_WriteCnf() warning: Generating CNF by convering logic nodes into CNF clauses.\n" );
    if ( Abc_NtkPoNum(pNtk) != 1 )
Alan Mishchenko committed
56
    {
Alan Mishchenko committed
57
        fprintf( stdout, "Io_WriteCnf(): Currently can only process the miter (the network with one PO).\n" );
Alan Mishchenko committed
58 59
        return 0;
    }
Alan Mishchenko committed
60
    if ( Abc_NtkLatchNum(pNtk) != 0 )
Alan Mishchenko committed
61
    {
Alan Mishchenko committed
62
        fprintf( stdout, "Io_WriteCnf(): Currently can only process the miter for combinational circuits.\n" );
Alan Mishchenko committed
63 64
        return 0;
    }
Alan Mishchenko committed
65
    if ( Abc_NtkNodeNum(pNtk) == 0 )
Alan Mishchenko committed
66
    {
Alan Mishchenko committed
67
        fprintf( stdout, "The network has no logic nodes. No CNF file is generaled.\n" );
Alan Mishchenko committed
68 69
        return 0;
    }
Alan Mishchenko committed
70 71 72
    // convert to logic BDD network
    if ( Abc_NtkIsLogic(pNtk) )
        Abc_NtkToBdd( pNtk );
Alan Mishchenko committed
73
    // create solver with clauses
74
    pSat = (sat_solver *)Abc_NtkMiterSatCreate( pNtk, fAllPrimes );
Alan Mishchenko committed
75 76 77 78 79
    if ( pSat == NULL )
    {
        fprintf( stdout, "The problem is trivially UNSAT. No CNF file is generated.\n" );
        return 1;
    }        
Alan Mishchenko committed
80
    // write the clauses
Alan Mishchenko committed
81 82 83
    s_pNtk = pNtk;
    Sat_SolverWriteDimacs( pSat, pFileName, 0, 0, 1 );
    s_pNtk = NULL;
Alan Mishchenko committed
84
    // free the solver
Alan Mishchenko committed
85
    sat_solver_delete( pSat );
Alan Mishchenko committed
86 87 88
    return 1;
}

Alan Mishchenko committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/**Function*************************************************************

  Synopsis    [Output the mapping of PIs into variable numbers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_WriteCnfOutputPiMapping( FILE * pFile, int incrementVars )
{
    extern Vec_Int_t * Abc_NtkGetCiSatVarNums( Abc_Ntk_t * pNtk );
    Abc_Ntk_t * pNtk = s_pNtk;
    Vec_Int_t * vCiIds;
    Abc_Obj_t * pObj;
    int i;
    vCiIds = Abc_NtkGetCiSatVarNums( pNtk );
    fprintf( pFile, "c PI variable numbers: <PI_name> <SAT_var_number>\n" );
    Abc_NtkForEachCi( pNtk, pObj, i )
        fprintf( pFile, "c %s %d\n", Abc_ObjName(pObj), Vec_IntEntry(vCiIds, i) + (int)(incrementVars > 0) );
    Vec_IntFree( vCiIds );
}
Alan Mishchenko committed
113 114 115 116 117 118

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


119 120
ABC_NAMESPACE_IMPL_END