ioWritePla.c 6.12 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
/**CFile****************************************************************

  FileName    [ioWritePla.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write the network in BENCH format.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

Alan Mishchenko committed
21
#include "ioAbc.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static int    Io_WritePlaOne( FILE * pFile, Abc_Ntk_t * pNtk );

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
33
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
34 35 36 37
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
38
  Synopsis    [Writes the network in PLA format.]
Alan Mishchenko committed
39 40 41 42 43 44 45 46 47 48 49 50 51

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WritePla( Abc_Ntk_t * pNtk, char * pFileName )
{
    Abc_Ntk_t * pExdc;
    FILE * pFile;

Alan Mishchenko committed
52
    assert( Abc_NtkIsSopNetlist(pNtk) );
Alan Mishchenko committed
53
    assert( Abc_NtkLevel(pNtk) == 1 );
Alan Mishchenko committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Io_WritePla(): Cannot open the output file.\n" );
        return 0;
    }
    fprintf( pFile, "# Benchmark \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
    // write the network
    Io_WritePlaOne( pFile, pNtk );
    // write EXDC network if it exists
    pExdc = Abc_NtkExdc( pNtk );
    if ( pExdc )
        printf( "Io_WritePla: EXDC is not written (warning).\n" );
    // finalize the file
    fclose( pFile );
    return 1;
}

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

Alan Mishchenko committed
75
  Synopsis    [Writes the network in PLA format.]
Alan Mishchenko committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WritePlaOne( FILE * pFile, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Obj_t * pNode, * pFanin, * pDriver;
    char * pCubeIn, * pCubeOut, * pCube;
    int i, k, nProducts, nInputs, nOutputs, nFanins;

    nProducts = 0;
Alan Mishchenko committed
92
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
93
    {
Alan Mishchenko committed
94
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
Alan Mishchenko committed
95 96 97 98 99 100 101 102 103 104 105
        if ( !Abc_ObjIsNode(pDriver) )
        {
            nProducts++;
            continue;
        }
        if ( Abc_NodeIsConst(pDriver) )
        {
            if ( Abc_NodeIsConst1(pDriver) )
                nProducts++;
            continue;
        }
106
        nProducts += Abc_SopGetCubeNum((char *)pDriver->pData);
Alan Mishchenko committed
107 108 109 110 111
    }

    // collect the parameters
    nInputs  = Abc_NtkCiNum(pNtk);
    nOutputs = Abc_NtkCoNum(pNtk);
Alan Mishchenko committed
112 113
    pCubeIn  = ABC_ALLOC( char, nInputs + 1 );
    pCubeOut = ABC_ALLOC( char, nOutputs + 1 );
Alan Mishchenko committed
114 115 116 117 118 119 120 121
    memset( pCubeIn,  '-', nInputs );     pCubeIn[nInputs]   = 0;
    memset( pCubeOut, '0', nOutputs );    pCubeOut[nOutputs] = 0;

    // write the header
    fprintf( pFile, ".i %d\n", nInputs );
    fprintf( pFile, ".o %d\n", nOutputs );
    fprintf( pFile, ".ilb" );
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
122
        fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanout0(pNode)) );
Alan Mishchenko committed
123 124 125
    fprintf( pFile, "\n" );
    fprintf( pFile, ".ob" );
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
126
        fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanin0(pNode)) );
Alan Mishchenko committed
127 128 129 130 131
    fprintf( pFile, "\n" );
    fprintf( pFile, ".p %d\n", nProducts );

    // mark the CI nodes
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
132
        pNode->pCopy = (Abc_Obj_t *)(ABC_PTRUINT_T)i;
Alan Mishchenko committed
133 134 135 136 137 138 139 140 141 142 143

    // write the cubes
    pProgress = Extra_ProgressBarStart( stdout, nOutputs );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        // prepare the output cube
        if ( i - 1 >= 0 )
            pCubeOut[i-1] = '0';
        pCubeOut[i] = '1';

        // consider special cases of nodes
Alan Mishchenko committed
144
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
Alan Mishchenko committed
145 146
        if ( !Abc_ObjIsNode(pDriver) )
        {
Alan Mishchenko committed
147
            assert( Abc_ObjIsCi(pDriver) );
Alan Mishchenko committed
148
            pCubeIn[(int)(ABC_PTRUINT_T)pDriver->pCopy] = '1' - Abc_ObjFaninC0(pNode);
Alan Mishchenko committed
149
            fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
Alan Mishchenko committed
150
            pCubeIn[(int)(ABC_PTRUINT_T)pDriver->pCopy] = '-';
Alan Mishchenko committed
151 152 153 154 155 156 157 158
            continue;
        }
        if ( Abc_NodeIsConst(pDriver) )
        {
            if ( Abc_NodeIsConst1(pDriver) )
                fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
            continue;
        }
Alan Mishchenko committed
159 160

        // make sure the cover is not complemented
161
        assert( !Abc_SopIsComplement( (char *)pDriver->pData ) );
Alan Mishchenko committed
162

Alan Mishchenko committed
163 164
        // write the cubes
        nFanins = Abc_ObjFaninNum(pDriver);
165
        Abc_SopForEachCube( (char *)pDriver->pData, nFanins, pCube )
Alan Mishchenko committed
166 167
        {
            Abc_ObjForEachFanin( pDriver, pFanin, k )
Alan Mishchenko committed
168 169
            {
                pFanin = Abc_ObjFanin0Ntk(pFanin);
Alan Mishchenko committed
170 171
                assert( (int)(ABC_PTRUINT_T)pFanin->pCopy < nInputs );
                pCubeIn[(int)(ABC_PTRUINT_T)pFanin->pCopy] = pCube[k];
Alan Mishchenko committed
172
            }
Alan Mishchenko committed
173 174 175 176
            fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
        }
        // clean the cube for future writing
        Abc_ObjForEachFanin( pDriver, pFanin, k )
Alan Mishchenko committed
177 178 179
        {
            pFanin = Abc_ObjFanin0Ntk(pFanin);
            assert( Abc_ObjIsCi(pFanin) );
Alan Mishchenko committed
180
            pCubeIn[(int)(ABC_PTRUINT_T)pFanin->pCopy] = '-';
Alan Mishchenko committed
181
        }
Alan Mishchenko committed
182 183 184 185 186 187 188 189
        Extra_ProgressBarUpdate( pProgress, i, NULL );
    }
    Extra_ProgressBarStop( pProgress );
    fprintf( pFile, ".e\n" );

    // clean the CI nodes
    Abc_NtkForEachCi( pNtk, pNode, i )
        pNode->pCopy = NULL;
Alan Mishchenko committed
190 191
    ABC_FREE( pCubeIn );
    ABC_FREE( pCubeOut );
Alan Mishchenko committed
192 193 194 195 196 197 198 199 200
    return 1;
}


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


201 202
ABC_NAMESPACE_IMPL_END