ioWriteEqn.c 7.24 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    [ioWriteEqn.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write equation representation of the network.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: ioWriteEqn.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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static void Io_NtkWriteEqnOne( FILE * pFile, Abc_Ntk_t * pNtk );
Alan Mishchenko committed
31 32 33
static void Io_NtkWriteEqnCis( FILE * pFile, Abc_Ntk_t * pNtk );
static void Io_NtkWriteEqnCos( FILE * pFile, Abc_Ntk_t * pNtk );
static int Io_NtkWriteEqnCheck( Abc_Ntk_t * pNtk );
Alan Mishchenko committed
34 35

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
36
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Writes the logic network in the equation format.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_WriteEqn( Abc_Ntk_t * pNtk, char * pFileName )
{
    FILE * pFile;

Alan Mishchenko committed
54
    assert( Abc_NtkIsAigNetlist(pNtk) );
Alan Mishchenko committed
55 56 57
    if ( Abc_NtkLatchNum(pNtk) > 0 )
        printf( "Warning: only combinational portion is being written.\n" );

Alan Mishchenko committed
58 59 60 61
    // check that the names are fine for the EQN format
    if ( !Io_NtkWriteEqnCheck(pNtk) )
        return;

Alan Mishchenko committed
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
    // start the output stream
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Io_WriteEqn(): Cannot open the output file \"%s\".\n", pFileName );
        return;
    }
    fprintf( pFile, "# Equations for \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );

    // write the equations for the network
    Io_NtkWriteEqnOne( pFile, pNtk );
    fprintf( pFile, "\n" );
    fclose( pFile );
}

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

  Synopsis    [Write one network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_NtkWriteEqnOne( FILE * pFile, Abc_Ntk_t * pNtk )
{
Alan Mishchenko committed
90
    Vec_Vec_t * vLevels;
Alan Mishchenko committed
91
    ProgressBar * pProgress;
Alan Mishchenko committed
92 93
    Abc_Obj_t * pNode, * pFanin;
    int i, k;
Alan Mishchenko committed
94 95 96

    // write the PIs
    fprintf( pFile, "INORDER =" );
Alan Mishchenko committed
97
    Io_NtkWriteEqnCis( pFile, pNtk );
Alan Mishchenko committed
98 99 100 101
    fprintf( pFile, ";\n" );

    // write the POs
    fprintf( pFile, "OUTORDER =" );
Alan Mishchenko committed
102
    Io_NtkWriteEqnCos( pFile, pNtk );
Alan Mishchenko committed
103 104 105
    fprintf( pFile, ";\n" );

    // write each internal node
Alan Mishchenko committed
106
    vLevels = Vec_VecAlloc( 10 );
Alan Mishchenko committed
107 108 109 110
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
Alan Mishchenko committed
111 112 113
        fprintf( pFile, "%s = ", Abc_ObjName(Abc_ObjFanout0(pNode)) );
        // set the input names
        Abc_ObjForEachFanin( pNode, pFanin, k )
114
            Hop_IthVar((Hop_Man_t *)pNtk->pManFunc, k)->pData = Abc_ObjName(pFanin);
Alan Mishchenko committed
115
        // write the formula
116
        Hop_ObjPrintEqn( pFile, (Hop_Obj_t *)pNode->pData, vLevels, 0 );
Alan Mishchenko committed
117
        fprintf( pFile, ";\n" );
Alan Mishchenko committed
118 119
    }
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
120
    Vec_VecFree( vLevels );
Alan Mishchenko committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134
}


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

  Synopsis    [Writes the primary input list.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
135
void Io_NtkWriteEqnCis( FILE * pFile, Abc_Ntk_t * pNtk )
Alan Mishchenko committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
    Abc_Obj_t * pTerm, * pNet;
    int LineLength;
    int AddedLength;
    int NameCounter;
    int i;

    LineLength  = 9;
    NameCounter = 0;

    Abc_NtkForEachCi( pNtk, pTerm, i )
    {
        pNet = Abc_ObjFanout0(pTerm);
        // get the line length after this name is written
        AddedLength = strlen(Abc_ObjName(pNet)) + 1;
        if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
        { // write the line extender
            fprintf( pFile, " \n" );
            // reset the line length
            LineLength  = 0;
            NameCounter = 0;
        }
        fprintf( pFile, " %s", Abc_ObjName(pNet) );
        LineLength += AddedLength;
        NameCounter++;
    }
}

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

  Synopsis    [Writes the primary input list.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
175
void Io_NtkWriteEqnCos( FILE * pFile, Abc_Ntk_t * pNtk )
Alan Mishchenko committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
    Abc_Obj_t * pTerm, * pNet;
    int LineLength;
    int AddedLength;
    int NameCounter;
    int i;

    LineLength  = 10;
    NameCounter = 0;

    Abc_NtkForEachCo( pNtk, pTerm, i )
    {
        pNet = Abc_ObjFanin0(pTerm);
        // get the line length after this name is written
        AddedLength = strlen(Abc_ObjName(pNet)) + 1;
        if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
        { // write the line extender
            fprintf( pFile, " \n" );
            // reset the line length
            LineLength  = 0;
            NameCounter = 0;
        }
        fprintf( pFile, " %s", Abc_ObjName(pNet) );
        LineLength += AddedLength;
        NameCounter++;
    }
}

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

Alan Mishchenko committed
206
  Synopsis    [Make sure the network does not have offending names.]
Alan Mishchenko committed
207 208

  Description []
Alan Mishchenko committed
209
  
Alan Mishchenko committed
210 211 212 213 214
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
215
int Io_NtkWriteEqnCheck( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
216
{
Alan Mishchenko committed
217
    Abc_Obj_t * pObj;
Alan Mishchenko committed
218
    char * pName = NULL;
Alan Mishchenko committed
219 220
    int i, k, Length;
    int RetValue = 1;
Alan Mishchenko committed
221

Alan Mishchenko committed
222 223
    // make sure the network does not have proper names, such as "0" or "1" or containing parantheses
    Abc_NtkForEachObj( pNtk, pObj, i )
Alan Mishchenko committed
224
    {
Alan Mishchenko committed
225 226 227 228 229
        pName = Nm_ManFindNameById(pNtk->pManName, i);
        if ( pName == NULL )
            continue;
        Length = strlen(pName);
        if ( pName[0] == '0' || pName[0] == '1' )
Alan Mishchenko committed
230
        {
Alan Mishchenko committed
231 232
            RetValue = 0;
            break;
Alan Mishchenko committed
233
        }
Alan Mishchenko committed
234 235 236 237 238
        for ( k = 0; k < Length; k++ )
            if ( pName[k] == '(' || pName[k] == ')' || pName[k] == '!' || pName[k] == '*' || pName[k] == '+' )
            {
                RetValue = 0;
                break;
Alan Mishchenko committed
239
            }
Alan Mishchenko committed
240 241
        if ( k < Length )
            break;
Alan Mishchenko committed
242
    }
Alan Mishchenko committed
243 244 245 246 247 248
    if ( RetValue == 0 )
    {
        printf( "The network cannot be written in the EQN format because object %d has name \"%s\".\n", i, pName );
        printf( "Consider renaming the objects using command \"short_names\" and trying again.\n" );
    }
    return RetValue;
Alan Mishchenko committed
249 250 251 252 253 254 255
}

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


256 257
ABC_NAMESPACE_IMPL_END