ioReadPla.c 10.2 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    [ioReadPla.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedure to read network from file.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

Alan Mishchenko committed
30
static Abc_Ntk_t * Io_ReadPlaNetwork( Extra_FileReader_t * p, int fZeros );
Alan Mishchenko committed
31 32

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

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

Alan Mishchenko committed
38
  Synopsis    [Reads the network from a PLA file.]
Alan Mishchenko committed
39

Alan Mishchenko committed
40
  Description []
Alan Mishchenko committed
41 42 43 44 45 46
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
47
Abc_Ntk_t * Io_ReadPla( char * pFileName, int fZeros, int fCheck )
Alan Mishchenko committed
48 49 50 51 52
{
    Extra_FileReader_t * p;
    Abc_Ntk_t * pNtk;

    // start the file
Alan Mishchenko committed
53
    p = Extra_FileReaderAlloc( pFileName, "#", "\n\r", " \t|" );
54
//    p = Extra_FileReaderAlloc( pFileName, "", "\n\r", " \t|" );
Alan Mishchenko committed
55 56 57 58
    if ( p == NULL )
        return NULL;

    // read the network
Alan Mishchenko committed
59
    pNtk = Io_ReadPlaNetwork( p, fZeros );
Alan Mishchenko committed
60 61 62 63 64
    Extra_FileReaderFree( p );
    if ( pNtk == NULL )
        return NULL;

    // make sure that everything is okay with the network structure
Alan Mishchenko committed
65
    if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
Alan Mishchenko committed
66 67 68 69 70 71 72 73 74
    {
        printf( "Io_ReadPla: The network check has failed.\n" );
        Abc_NtkDelete( pNtk );
        return NULL;
    }
    return pNtk;
}
/**Function*************************************************************

Alan Mishchenko committed
75
  Synopsis    []
Alan Mishchenko committed
76

Alan Mishchenko committed
77
  Description []
Alan Mishchenko committed
78 79 80 81 82 83
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
84
Abc_Ntk_t * Io_ReadPlaNetwork( Extra_FileReader_t * p, int fZeros )
Alan Mishchenko committed
85 86 87 88
{
    ProgressBar * pProgress;
    Vec_Ptr_t * vTokens;
    Abc_Ntk_t * pNtk;
Alan Mishchenko committed
89
    Abc_Obj_t * pTermPi, * pTermPo, * pNode;
Alan Mishchenko committed
90
    Vec_Str_t ** ppSops = NULL;
Alan Mishchenko committed
91 92 93 94
    char Buffer[100];
    int nInputs = -1, nOutputs = -1, nProducts = -1;
    char * pCubeIn, * pCubeOut;
    int i, k, iLine, nDigits, nCubes;
95

Alan Mishchenko committed
96
    // allocate the empty network
Alan Mishchenko committed
97
    pNtk = Abc_NtkStartRead( Extra_FileReaderGetFileName(p) );
Alan Mishchenko committed
98 99 100 101

    // go through the lines of the file
    nCubes = 0;
    pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
102
    for ( iLine = 0; (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)); iLine++ )
Alan Mishchenko committed
103 104 105 106
    {
        Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p), NULL );

        // if it is the end of file, quit the loop
107
        if ( strncmp( (char *)vTokens->pArray[0], ".e", 2 ) == 0 )
Alan Mishchenko committed
108 109
            break;

110 111 112 113 114 115 116 117
        // if it is the model name, get the name
        if ( strcmp( (char *)vTokens->pArray[0], ".model" ) == 0 )
        {
            ABC_FREE( pNtk->pName );
            pNtk->pName = Extra_UtilStrsav( (char *)vTokens->pArray[1] );
            continue;
        }

Alan Mishchenko committed
118 119 120 121 122
        if ( vTokens->nSize == 1 )
        {
            printf( "%s (line %d): Wrong number of token.\n", 
                Extra_FileReaderGetFileName(p), iLine+1 );
            Abc_NtkDelete( pNtk );
123 124
            Extra_ProgressBarStop( pProgress );
            ABC_FREE( ppSops );
Alan Mishchenko committed
125 126 127
            return NULL;
        }

128 129 130 131 132 133 134
        if ( strcmp( (char *)vTokens->pArray[0], ".i" ) == 0 )
            nInputs = atoi((char *)vTokens->pArray[1]);
        else if ( strcmp( (char *)vTokens->pArray[0], ".o" ) == 0 )
            nOutputs = atoi((char *)vTokens->pArray[1]);
        else if ( strcmp( (char *)vTokens->pArray[0], ".p" ) == 0 )
            nProducts = atoi((char *)vTokens->pArray[1]);
        else if ( strcmp( (char *)vTokens->pArray[0], ".ilb" ) == 0 )
Alan Mishchenko committed
135 136 137 138
        {
            if ( vTokens->nSize - 1 != nInputs )
                printf( "Warning: Mismatch between the number of PIs on the .i line (%d) and the number of PIs on the .ilb line (%d).\n", nInputs, vTokens->nSize - 1 );
            for ( i = 1; i < vTokens->nSize; i++ )
139
                Io_ReadCreatePi( pNtk, (char *)vTokens->pArray[i] );
Alan Mishchenko committed
140
        }
141
        else if ( strcmp( (char *)vTokens->pArray[0], ".ob" ) == 0 )
Alan Mishchenko committed
142 143 144 145
        {
            if ( vTokens->nSize - 1 != nOutputs )
                printf( "Warning: Mismatch between the number of POs on the .o line (%d) and the number of POs on the .ob line (%d).\n", nOutputs, vTokens->nSize - 1 );
            for ( i = 1; i < vTokens->nSize; i++ )
146
                Io_ReadCreatePo( pNtk, (char *)vTokens->pArray[i] );
Alan Mishchenko committed
147
        }
Alan Mishchenko committed
148
        else 
Alan Mishchenko committed
149 150 151 152 153 154 155 156
        {
            // check if the input/output names are given
            if ( Abc_NtkPiNum(pNtk) == 0 )
            {
                if ( nInputs == -1 )
                {
                    printf( "%s: The number of inputs is not specified.\n", Extra_FileReaderGetFileName(p) );
                    Abc_NtkDelete( pNtk );
157 158
                    Extra_ProgressBarStop( pProgress );
                    ABC_FREE( ppSops );
Alan Mishchenko committed
159 160 161 162 163 164
                    return NULL;
                }
                nDigits = Extra_Base10Log( nInputs );
                for ( i = 0; i < nInputs; i++ )
                {
                    sprintf( Buffer, "x%0*d", nDigits, i );
Alan Mishchenko committed
165
                    Io_ReadCreatePi( pNtk, Buffer );
Alan Mishchenko committed
166 167 168 169 170 171 172 173
                }
            }
            if ( Abc_NtkPoNum(pNtk) == 0 )
            {
                if ( nOutputs == -1 )
                {
                    printf( "%s: The number of outputs is not specified.\n", Extra_FileReaderGetFileName(p) );
                    Abc_NtkDelete( pNtk );
174 175
                    Extra_ProgressBarStop( pProgress );
                    ABC_FREE( ppSops );
Alan Mishchenko committed
176 177 178 179 180 181
                    return NULL;
                }
                nDigits = Extra_Base10Log( nOutputs );
                for ( i = 0; i < nOutputs; i++ )
                {
                    sprintf( Buffer, "z%0*d", nDigits, i );
Alan Mishchenko committed
182
                    Io_ReadCreatePo( pNtk, Buffer );
Alan Mishchenko committed
183 184 185 186 187 188
                }
            }
            if ( Abc_NtkNodeNum(pNtk) == 0 )
            { // first time here
                // create the PO drivers and add them
                // start the SOP covers
Alan Mishchenko committed
189
                ppSops = ABC_ALLOC( Vec_Str_t *, nOutputs );
Alan Mishchenko committed
190
                Abc_NtkForEachPo( pNtk, pTermPo, i )
Alan Mishchenko committed
191 192
                {
                    ppSops[i] = Vec_StrAlloc( 100 );
Alan Mishchenko committed
193
                    // create the node
Alan Mishchenko committed
194
                    pNode = Abc_NtkCreateNode(pNtk);
Alan Mishchenko committed
195 196 197 198 199
                    // connect the node to the PO net
                    Abc_ObjAddFanin( Abc_ObjFanin0Ntk(pTermPo), pNode );
                    // connect the node to the PI nets
                    Abc_NtkForEachPi( pNtk, pTermPi, k )
                        Abc_ObjAddFanin( pNode, Abc_ObjFanout0Ntk(pTermPi) );
Alan Mishchenko committed
200 201 202 203 204 205 206 207
                }
            }
            // read the cubes
            if ( vTokens->nSize != 2 )
            {
                printf( "%s (line %d): Input and output cubes are not specified.\n", 
                    Extra_FileReaderGetFileName(p), iLine+1 );
                Abc_NtkDelete( pNtk );
208 209
                Extra_ProgressBarStop( pProgress );
                ABC_FREE( ppSops );
Alan Mishchenko committed
210 211
                return NULL;
            }
212 213
            pCubeIn  = (char *)vTokens->pArray[0];
            pCubeOut = (char *)vTokens->pArray[1];
Alan Mishchenko committed
214 215
            if ( strlen(pCubeIn) != (unsigned)nInputs )
            {
Alan Mishchenko committed
216
                printf( "%s (line %d): Input cube length (%zu) differs from the number of inputs (%d).\n",
Alan Mishchenko committed
217 218 219 220 221 222
                    Extra_FileReaderGetFileName(p), iLine+1, strlen(pCubeIn), nInputs );
                Abc_NtkDelete( pNtk );
                return NULL;
            }
            if ( strlen(pCubeOut) != (unsigned)nOutputs )
            {
Alan Mishchenko committed
223
                printf( "%s (line %d): Output cube length (%zu) differs from the number of outputs (%d).\n",
Alan Mishchenko committed
224 225
                    Extra_FileReaderGetFileName(p), iLine+1, strlen(pCubeOut), nOutputs );
                Abc_NtkDelete( pNtk );
226 227
                Extra_ProgressBarStop( pProgress );
                ABC_FREE( ppSops );
Alan Mishchenko committed
228 229
                return NULL;
            }
Alan Mishchenko committed
230
            if ( fZeros )
Alan Mishchenko committed
231
            {
Alan Mishchenko committed
232 233 234 235
                for ( i = 0; i < nOutputs; i++ )
                {
                    if ( pCubeOut[i] == '0' )
                    {
236 237
                        Vec_StrPrintStr( ppSops[i], pCubeIn );
                        Vec_StrPrintStr( ppSops[i], " 1\n" );
Alan Mishchenko committed
238 239 240 241 242 243
                    }
                }
            }
            else
            {
                for ( i = 0; i < nOutputs; i++ )
Alan Mishchenko committed
244
                {
Alan Mishchenko committed
245 246
                    if ( pCubeOut[i] == '1' )
                    {
247 248
                        Vec_StrPrintStr( ppSops[i], pCubeIn );
                        Vec_StrPrintStr( ppSops[i], " 1\n" );
Alan Mishchenko committed
249
                    }
Alan Mishchenko committed
250 251 252 253 254 255 256 257 258 259 260
                }
            }
            nCubes++;
        }
    }
    Extra_ProgressBarStop( pProgress );
    if ( nProducts != -1 && nCubes != nProducts )
        printf( "Warning: Mismatch between the number of cubes (%d) and the number on .p line (%d).\n", 
            nCubes, nProducts );

    // add the SOP covers
Alan Mishchenko committed
261
    Abc_NtkForEachPo( pNtk, pTermPo, i )
Alan Mishchenko committed
262
    {
Alan Mishchenko committed
263
        pNode = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pTermPo) );
Alan Mishchenko committed
264 265 266
        if ( ppSops[i]->nSize == 0 )
        {
            Abc_ObjRemoveFanins(pNode);
267
            pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" );
Alan Mishchenko committed
268
            Vec_StrFree( ppSops[i] );
Alan Mishchenko committed
269 270 271
            continue;
        }
        Vec_StrPush( ppSops[i], 0 );
272
        pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, ppSops[i]->pArray );
Alan Mishchenko committed
273 274
        Vec_StrFree( ppSops[i] );
    }
Alan Mishchenko committed
275
    ABC_FREE( ppSops );
Alan Mishchenko committed
276
    Abc_NtkFinalizeRead( pNtk );
Alan Mishchenko committed
277 278 279 280 281 282 283 284 285 286
    return pNtk;
}


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



287 288
ABC_NAMESPACE_IMPL_END