mioFunc.c 9.61 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
/**CFile****************************************************************

  FileName    [mioFunc.c]

  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]

  Synopsis    [File reading/writing for technology mapping.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - September 8, 2003.]

  Revision    [$Id: mioFunc.c,v 1.4 2004/06/28 14:20:25 alanmi Exp $]

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

#include "mioInt.h"
20 21
//#include "parse.h"
#include "exp.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


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

// these symbols (and no other) can appear in the formulas
#define MIO_SYMB_AND    '*'
32 33
#define MIO_SYMB_AND2   '&'
#define MIO_SYMB_OR     '+'
Alan Mishchenko committed
34 35
#define MIO_SYMB_OR2    '|'
#define MIO_SYMB_XOR    '^'
Alan Mishchenko committed
36 37 38 39 40 41
#define MIO_SYMB_NOT    '!'
#define MIO_SYMB_AFTNOT '\''
#define MIO_SYMB_OPEN   '('
#define MIO_SYMB_CLOSE  ')'

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
42
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
43 44 45 46
////////////////////////////////////////////////////////////////////////

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

47
  Synopsis    [Registers the cube string with the network.]
Alan Mishchenko committed
48 49 50 51 52 53 54 55

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
56
char * Mio_SopRegister( Mem_Flex_t * pMan, char * pName )
Alan Mishchenko committed
57
{
58 59 60 61 62 63
    char * pRegName;
    if ( pName == NULL ) return NULL;
    pRegName = Mem_FlexEntryFetch( pMan, strlen(pName) + 1 );
    strcpy( pRegName, pName );
    return pRegName;
}
Alan Mishchenko committed
64

65
/**Function*************************************************************
Alan Mishchenko committed
66

67
  Synopsis    [Collect the pin names in the formula.]
Alan Mishchenko committed
68

69 70 71 72 73 74 75 76 77
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_GateCollectNames( char * pFormula, char * pPinNames[] )
{
78
    char * Buffer;
79 80
    char * pTemp;
    int nPins, i;
Alan Mishchenko committed
81

82
    // save the formula as it was
83 84
    //strcpy( Buffer, pFormula );
    Buffer = Abc_UtilStrsav( pFormula );
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    // remove the non-name symbols
    for ( pTemp = Buffer; *pTemp; pTemp++ )
        if ( *pTemp == MIO_SYMB_AND  || *pTemp == MIO_SYMB_AND2   || 
             *pTemp == MIO_SYMB_OR   || *pTemp == MIO_SYMB_OR2    || 
             *pTemp == MIO_SYMB_XOR  || 
             *pTemp == MIO_SYMB_NOT  || *pTemp == MIO_SYMB_AFTNOT ||
             *pTemp == MIO_SYMB_OPEN || *pTemp == MIO_SYMB_CLOSE )
             *pTemp = ' ';

    // save the names
    nPins = 0;
    pTemp = strtok( Buffer, " " );
    while ( pTemp )
    {
        for ( i = 0; i < nPins; i++ )
            if ( strcmp( pTemp, pPinNames[i] ) == 0 )
                break;
        if ( i == nPins )
        { // cannot find this name; save it
105
            pPinNames[nPins++] = Abc_UtilStrsav(pTemp);
106 107 108 109
        }
        // get the next name
        pTemp = strtok( NULL, " " );
    }
110
    ABC_FREE( Buffer );
111 112
    return nPins;
}
Alan Mishchenko committed
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 144 145 146 147 148

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

  Synopsis    [Deriving the functionality of the gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_GateParseFormula( Mio_Gate_t * pGate )
{
    char * pPinNames[100];
    char * pPinNamesCopy[100];
    Mio_Pin_t * pPin, ** ppPin;
    int nPins, iPin, i;

    // set the maximum delay of the gate; count pins
    pGate->dDelayMax = 0.0;
    nPins = 0;
    Mio_GateForEachPin( pGate, pPin )
    {
        // set the maximum delay of the gate
        if ( pGate->dDelayMax < pPin->dDelayBlockMax )
            pGate->dDelayMax = pPin->dDelayBlockMax;
        // count the pin
        nPins++;
    }

    // check for the gate with const function
    if ( nPins == 0 )
    {
        if ( strcmp( pGate->pForm, MIO_STRING_CONST0 ) == 0 )
        {
149 150 151
//            pGate->bFunc = b0;
            pGate->vExpr = Exp_Const0();
            pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 0\n" );
152
            pGate->uTruth = 0;
Alan Mishchenko committed
153 154 155 156
            pGate->pLib->pGate0 = pGate;
        }
        else if ( strcmp( pGate->pForm, MIO_STRING_CONST1 ) == 0 )
        {
157 158 159
//            pGate->bFunc = b1;
            pGate->vExpr = Exp_Const1();
            pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 1\n" );
160
            pGate->uTruth = ~(word)0;
Alan Mishchenko committed
161 162 163 164 165 166 167
            pGate->pLib->pGate1 = pGate;
        }
        else
        {
            printf( "Cannot parse formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
            return 1;
        }
168
//        Cudd_Ref( pGate->bFunc );
Alan Mishchenko committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        return 0;
    }

    // collect the names as they appear in the formula
    nPins = Mio_GateCollectNames( pGate->pForm, pPinNames );
    if ( nPins == 0 )
    {
        printf( "Cannot read formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
        return 1;
    }

    // set the number of inputs
    pGate->nInputs = nPins;

    // consider the case when all the pins have identical pin info
    if ( strcmp( pGate->pPins->pName, "*" ) == 0 )
    {
        // get the topmost (generic) pin
        pPin = pGate->pPins;
Alan Mishchenko committed
188
        ABC_FREE( pPin->pName );
Alan Mishchenko committed
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

        // create individual pins from the generic pin
        ppPin = &pPin->pNext;
        for ( i = 1; i < nPins; i++ )
        {
            // get the new pin
            *ppPin = Mio_PinDup( pPin );
            // set its name
            (*ppPin)->pName = pPinNames[i];
            // prepare the next place in the list
            ppPin = &((*ppPin)->pNext);
        }
        *ppPin = NULL;

        // set the name of the topmost pin
        pPin->pName = pPinNames[0];
    }
    else
    {
        // reorder the variable names to appear the save way as the pins
        iPin = 0;
        Mio_GateForEachPin( pGate, pPin )
        {
            // find the pin with the name pPin->pName
            for ( i = 0; i < nPins; i++ )
            {
                if ( pPinNames[i] && strcmp( pPinNames[i], pPin->pName ) == 0 )
                {
Alan Mishchenko committed
217
                    // free pPinNames[i] because it is already available as pPin->pName
Alan Mishchenko committed
218 219
                    // setting pPinNames[i] to NULL is useful to make sure that
                    // this name is not assigned to two pins in the list
Alan Mishchenko committed
220
                    ABC_FREE( pPinNames[i] );
Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
                    pPinNamesCopy[iPin++] = pPin->pName;
                    break;
                }
                if ( i == nPins )
                {
                    printf( "Cannot find pin name \"%s\" in the formula \"%s\" of gate \"%s\".\n", 
                        pPin->pName, pGate->pForm, pGate->pName );
                    return 1;
                }
            }
        }

        // check for the remaining names
        for ( i = 0; i < nPins; i++ )
            if ( pPinNames[i] )
            {
                printf( "Name \"%s\" appears in the formula \"%s\" of gate \"%s\" but there is no such pin.\n", 
                    pPinNames[i], pGate->pForm, pGate->pName );
                return 1;
            }

        // copy the names back
        memcpy( pPinNames, pPinNamesCopy, nPins * sizeof(char *) );
    }
245
/*
Alan Mishchenko committed
246 247 248 249 250 251 252
    // expand the manager if necessary
    if ( dd->size < nPins )
    {
        Cudd_Quit( dd );
        dd = Cudd_Init( nPins + 10, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
        Cudd_zddVarsFromBddVars( dd, 2 );
    }
253
    // derive formula as the BDD
Alan Mishchenko committed
254
    pGate->bFunc = Parse_FormulaParser( stdout, pGate->pForm, nPins, 0, pPinNames, dd, dd->vars );
Alan Mishchenko committed
255 256
    if ( pGate->bFunc == NULL )
        return 1;
Alan Mishchenko committed
257
    Cudd_Ref( pGate->bFunc );
258
    // derive cover
Alan Mishchenko committed
259
    pGate->pSop = Abc_ConvertBddToSop( pGate->pLib->pMmFlex, dd, pGate->bFunc, pGate->bFunc, nPins, 0, pGate->pLib->vCube, -1 );
260 261 262 263
*/

    // derive expression 
    pGate->vExpr = Mio_ParseFormula( pGate->pForm, (char **)pPinNames, nPins );
264
//    Mio_ParseFormulaTruthTest( pGate->pForm, (char **)pPinNames, nPins );
265 266 267 268 269 270
    // derive cover
    pGate->pSop = Mio_LibDeriveSop( nPins, pGate->vExpr, pGate->pLib->vCube );
    pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, pGate->pSop );
    // derive truth table
    if ( nPins <= 6 )
        pGate->uTruth = Exp_Truth6( nPins, pGate->vExpr, NULL );
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
    // verify
    if ( pGate->nInputs <= 6 )
    {
        extern word Abc_SopToTruth( char * pSop, int nInputs );
        word t2 = Abc_SopToTruth( pGate->pSop, nPins );
        if ( pGate->uTruth != t2 )
        {
            printf( "%s\n", pGate->pForm );
            Exp_Print( nPins, pGate->vExpr );
            printf( "Verification failed!\n" );
        }
    }
*/
Alan Mishchenko committed
286 287 288 289 290
    return 0;
}

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

291
  Synopsis    [Deriving the functionality of the gates.]
Alan Mishchenko committed
292 293 294 295 296 297 298 299

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
300
int Mio_LibraryParseFormulas( Mio_Library_t * pLib )
Alan Mishchenko committed
301
{
302
    Mio_Gate_t * pGate;
Alan Mishchenko committed
303

304 305 306 307
    // count the gates
    pLib->nGates = 0;
    Mio_LibraryForEachGate( pLib, pGate )
        pLib->nGates++;        
Alan Mishchenko committed
308

309 310 311 312 313
    // for each gate, derive its function
    Mio_LibraryForEachGate( pLib, pGate )
        if ( Mio_GateParseFormula( pGate ) )
            return 1;
    return 0;
Alan Mishchenko committed
314 315 316 317 318 319 320
}

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


321 322
ABC_NAMESPACE_IMPL_END