plaRead.c 7.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 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 175 176 177 178 179
/**CFile****************************************************************

  FileName    [plaRead.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SOP manager.]

  Synopsis    [Scalable SOP transformations.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - March 18, 2015.]

  Revision    [$Id: plaRead.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

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

#include "pla.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Pla_ReadFile( char * pFileName, char ** ppLimit )
{
    char * pBuffer;
    int nFileSize, RetValue;
    FILE * pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open input file.\n" );
        return NULL;
    }
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile );  
    // move the file current reading position to the beginning
    rewind( pFile ); 
    // load the contents of the file into memory
    pBuffer = ABC_ALLOC( char, nFileSize + 16 );
    pBuffer[0] = '\n';
    RetValue = fread( pBuffer+1, nFileSize, 1, pFile );
    fclose( pFile );
    // terminate the string with '\0'
    pBuffer[nFileSize + 1] = '\n';
    pBuffer[nFileSize + 2] = '\0';
    *ppLimit = pBuffer + nFileSize + 3;
    return pBuffer;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pla_ReadPlaRemoveComments( char * pBuffer, char * pLimit )
{
    char * pTemp; 
    for ( pTemp = pBuffer; pTemp < pLimit; pTemp++ )
        if ( *pTemp == '#' )
            while ( *pTemp && *pTemp != '\n' )
                *pTemp++ = ' ';
}
int Pla_ReadPlaHeader( char * pBuffer, char * pLimit, int * pnIns, int * pnOuts, int * pnCubes, int * pType )
{
    char * pTemp; 
    *pType = PLA_FILE_FD;
    *pnIns = *pnOuts = *pnCubes = -1;
    for ( pTemp = pBuffer; pTemp < pLimit; pTemp++ )
    {
        if ( *pTemp != '.' )
            continue;
        if ( !strncmp(pTemp, ".i ", 3) )
            *pnIns = atoi( pTemp + 3 );
        else if ( !strncmp(pTemp, ".o ", 3) )
            *pnOuts = atoi( pTemp + 3 );
        else if ( !strncmp(pTemp, ".p ", 3) )
            *pnCubes = atoi( pTemp + 3 );
        else if ( !strncmp(pTemp, ".e ", 3) )
            break;
        else if ( !strncmp(pTemp, ".type ", 6) )
        {
            char Buffer[100];
            *pType = PLA_FILE_NONE;
            sscanf( pTemp+6, "%s", Buffer );
            if ( !strcmp(Buffer, "f") )
                *pType = PLA_FILE_F;
            else if ( !strcmp(Buffer, "fr") )
                *pType = PLA_FILE_FR;
            else if ( !strcmp(Buffer, "fd") )
                *pType = PLA_FILE_FD;
            else if ( !strcmp(Buffer, "fdr") )
                *pType = PLA_FILE_FDR;
        }
    }
    if ( *pnIns <= 0 )
        printf( "The number of inputs (.i) should be positive.\n" );
    if ( *pnOuts <= 0 )
        printf( "The number of outputs (.o) should be positive.\n" );
    return *pnIns > 0 && *pnOuts > 0;
}
Vec_Str_t * Pla_ReadPlaBody( char * pBuffer, char * pLimit, Pla_File_t Type )
{
    char * pTemp;
    Vec_Str_t * vLits;
    vLits = Vec_StrAlloc( 10000 );
    for ( pTemp = pBuffer; pTemp < pLimit; pTemp++ )
    {
        if ( *pTemp == '.' )
            while ( *pTemp && *pTemp != '\n' )
                pTemp++;
        if ( *pTemp == '0' )
            Vec_StrPush( vLits, (char)PLA_LIT_ZERO );
        else if ( *pTemp == '1' )
            Vec_StrPush( vLits, (char)PLA_LIT_ONE );
        else if ( *pTemp == '-' || *pTemp == '2' )
            Vec_StrPush( vLits, (char)PLA_LIT_DASH );
        else if ( *pTemp == '~' ) // no meaning
        {
            if ( Type == PLA_FILE_F || Type == PLA_FILE_FD )
                Vec_StrPush( vLits, (char)PLA_LIT_ZERO );
            else if ( Type == PLA_FILE_FR )
                Vec_StrPush( vLits, (char)PLA_LIT_DASH );
            else if ( Type == PLA_FILE_FDR )
                Vec_StrPush( vLits, (char)PLA_LIT_FULL );
            else assert( 0 );
        }
    }
    return vLits;
}
void Pla_ReadAddBody( Pla_Man_t * p, Vec_Str_t * vLits )
{
    word * pCubeIn, * pCubeOut; 
    int i, k, Lit, Count = 0; 
    int nCubesReal = Vec_StrSize(vLits) / (p->nIns + p->nOuts);
    assert( Vec_StrSize(vLits) % (p->nIns + p->nOuts) == 0 );
    if ( nCubesReal != Pla_ManCubeNum(p) )
    {
        printf( "Warning: Declared number of cubes (%d) differs from the actual (%d).\n", 
            Pla_ManCubeNum(p), nCubesReal );
        if ( nCubesReal < Pla_ManCubeNum(p) )
            Vec_IntShrink( &p->vCubes, nCubesReal );
        else
        {
            assert( nCubesReal > Pla_ManCubeNum(p) );
            Vec_IntFillNatural( &p->vCubes, nCubesReal );
            Vec_WrdFillExtra( &p->vInBits,  nCubesReal * p->nInWords,  0 );
            Vec_WrdFillExtra( &p->vOutBits, nCubesReal * p->nOutWords, 0 );
        }
    }
    Pla_ForEachCubeInOut( p, pCubeIn, pCubeOut, i )
    {
        Pla_CubeForEachLit( p->nIns, pCubeIn, Lit, k )
180
            Pla_CubeSetLit( pCubeIn, k, (Pla_Lit_t)Vec_StrEntry(vLits, Count++) );
181
        Pla_CubeForEachLit( p->nOuts, pCubeOut, Lit, k )
182
            Pla_CubeSetLit( pCubeOut, k, (Pla_Lit_t)Vec_StrEntry(vLits, Count++) );
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
    assert( Count == Vec_StrSize(vLits) );
}
Pla_Man_t * Pla_ReadPla( char * pFileName )
{   
    Pla_Man_t * p;
    Vec_Str_t * vLits;
    int nIns, nOuts, nCubes, Type;
    char * pBuffer, * pLimit;   
    pBuffer = Pla_ReadFile( pFileName, &pLimit );
    if ( pBuffer == NULL )
        return NULL;
    Pla_ReadPlaRemoveComments( pBuffer, pLimit );
    if ( Pla_ReadPlaHeader( pBuffer, pLimit, &nIns, &nOuts, &nCubes, &Type ) )
    {
198
        vLits = Pla_ReadPlaBody( pBuffer, pLimit, (Pla_File_t)Type );
199 200 201 202 203
        if ( Vec_StrSize(vLits) % (nIns + nOuts) == 0 )
        {
            if ( nCubes == -1 )
                nCubes = Vec_StrSize(vLits) / (nIns + nOuts);
            p = Pla_ManAlloc( pFileName, nIns, nOuts, nCubes );
204
            p->Type = (Pla_File_t)Type;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            Pla_ReadAddBody( p, vLits );
            Vec_StrFree( vLits );
            ABC_FREE( pBuffer );
            return p;
        }
        printf( "Literal count is incorrect (in = %d; out = %d; lit = %d).\n", nIns, nOuts, Vec_StrSize(vLits) );
        Vec_StrFree( vLits );
    }
    ABC_FREE( pBuffer );
    return NULL;
}


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


ABC_NAMESPACE_IMPL_END