giaStg.c 15.8 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
/**CFile****************************************************************

  FileName    [gia.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
#include "misc/extra/extra.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
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
void Gia_ManPrintStateEncoding( Vec_Vec_t * vCodes, int nBits )
{
    char * pBuffer;
    Vec_Int_t * vVec;
    int i, k, Bit;
    pBuffer = ABC_ALLOC( char, nBits + 1 );
    pBuffer[nBits] = 0;
    Vec_VecForEachLevelInt( vCodes, vVec, i )
    {
        printf( "%6d : ", i+1 );
        memset( pBuffer, '-', nBits );
        Vec_IntForEachEntry( vVec, Bit, k )
        {
            assert( Bit < nBits );
            pBuffer[Bit] = '1';
        }
        printf( "%s\n", pBuffer );
    }
    ABC_FREE( pBuffer );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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
int Gia_ManCreateOrGate( Gia_Man_t * p, Vec_Int_t * vLits )
{
    if ( Vec_IntSize(vLits) == 0 )
        return 0;
    while ( Vec_IntSize(vLits) > 1 )
    {
        int i, k = 0, Lit1, Lit2, LitRes;
        Vec_IntForEachEntryDouble( vLits, Lit1, Lit2, i )
        {
            LitRes = Gia_ManHashOr( p, Lit1, Lit2 );
            Vec_IntWriteEntry( vLits, k++, LitRes );
        }
        if ( Vec_IntSize(vLits) & 1 )
            Vec_IntWriteEntry( vLits, k++, Vec_IntEntryLast(vLits) );
        Vec_IntShrink( vLits, k );
    }
    assert( Vec_IntSize(vLits) == 1 );
    return Vec_IntEntry(vLits, 0);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
109 110 111
Vec_Vec_t * Gia_ManAssignCodes( int kHot, int nStates, int * pnBits )
{
    Vec_Vec_t * vCodes;
Alan Mishchenko committed
112
    int s, i1, i2, i3, i4, i5, nBits;
Alan Mishchenko committed
113
    assert( nStates > 0 );
Alan Mishchenko committed
114
    assert( kHot >= 1 && kHot <= 5 );
Alan Mishchenko committed
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
    vCodes = Vec_VecStart( nStates );
    *pnBits = -1;
    if ( kHot == 1 )
    {
        for ( i1 = 0; i1 < nStates; i1++ )
            Vec_VecPushInt( vCodes, i1, i1 );
        *pnBits = nStates;
        return vCodes;
    }
    if ( kHot == 2 )
    {
        for ( nBits = kHot; nBits < ABC_INFINITY; nBits++ )
            if ( nBits * (nBits-1) / 2 >= nStates )
                break;
        *pnBits = nBits;
        s = 0;
        for ( i1 = 0; i1 < nBits; i1++ )
        for ( i2 = i1 + 1; i2 < nBits; i2++ )
        {
            Vec_VecPushInt( vCodes, s, i1 );
            Vec_VecPushInt( vCodes, s, i2 );
            if ( ++s == nStates )
                return vCodes;
        }
    }
    if ( kHot == 3 )
    {
        for ( nBits = kHot; nBits < ABC_INFINITY; nBits++ )
            if ( nBits * (nBits-1) * (nBits-2) / 6 >= nStates )
                break;       
        *pnBits = nBits;
        s = 0;
        for ( i1 = 0; i1 < nBits; i1++ )
        for ( i2 = i1 + 1; i2 < nBits; i2++ )
        for ( i3 = i2 + 1; i3 < nBits; i3++ )
        {
            Vec_VecPushInt( vCodes, s, i1 );
            Vec_VecPushInt( vCodes, s, i2 );
            Vec_VecPushInt( vCodes, s, i3 );
            if ( ++s == nStates )
                return vCodes;
        }
    }
    if ( kHot == 4 )
    {
        for ( nBits = kHot; nBits < ABC_INFINITY; nBits++ )
            if ( nBits * (nBits-1) * (nBits-2) * (nBits-3) / 24 >= nStates )
                break;       
        *pnBits = nBits;
        s = 0;
        for ( i1 = 0; i1 < nBits; i1++ )
        for ( i2 = i1 + 1; i2 < nBits; i2++ )
        for ( i3 = i2 + 1; i3 < nBits; i3++ )
        for ( i4 = i3 + 1; i4 < nBits; i4++ )
        {
            Vec_VecPushInt( vCodes, s, i1 );
            Vec_VecPushInt( vCodes, s, i2 );
            Vec_VecPushInt( vCodes, s, i3 );
            Vec_VecPushInt( vCodes, s, i4 );
            if ( ++s == nStates )
                return vCodes;
        }
    }
Alan Mishchenko committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    if ( kHot == 5 )
    {
        for ( nBits = kHot; nBits < ABC_INFINITY; nBits++ )
            if ( nBits * (nBits-1) * (nBits-2) * (nBits-3) * (nBits-4) / 120 >= nStates )
                break;       
        *pnBits = nBits;
        s = 0;
        for ( i1 = 0; i1 < nBits; i1++ )
        for ( i2 = i1 + 1; i2 < nBits; i2++ )
        for ( i3 = i2 + 1; i3 < nBits; i3++ )
        for ( i4 = i3 + 1; i4 < nBits; i4++ )
        for ( i5 = i4 + 1; i5 < nBits; i5++ )
        {
            Vec_VecPushInt( vCodes, s, i1 );
            Vec_VecPushInt( vCodes, s, i2 );
            Vec_VecPushInt( vCodes, s, i3 );
            Vec_VecPushInt( vCodes, s, i4 );
            Vec_VecPushInt( vCodes, s, i5 );
            if ( ++s == nStates )
                return vCodes;
        }
    }
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    assert( 0 );
    return NULL;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
215
Gia_Man_t * Gia_ManStgKHot( Vec_Int_t * vLines, int nIns, int nOuts, int nStates, int kHot, int fVerbose )
Alan Mishchenko committed
216
{
217
    Gia_Man_t * p, * pTemp;
Alan Mishchenko committed
218 219 220 221
    Vec_Int_t * vInMints, * vCurs, * vVec;
    Vec_Vec_t * vLitsNext, * vLitsOuts, * vCodes;
    int i, b, k, nBits, LitC, Lit;
    assert( Vec_IntSize(vLines) % 4 == 0 );
Alan Mishchenko committed
222 223

    // produce state encoding
Alan Mishchenko committed
224
    vCodes = Gia_ManAssignCodes( kHot, nStates, &nBits );
Alan Mishchenko committed
225 226 227
    assert( Vec_VecSize(vCodes) == nStates );
    if ( fVerbose )
        Gia_ManPrintStateEncoding( vCodes, nBits );
Alan Mishchenko committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    // start manager
    p = Gia_ManStart( 10000 );
    p->pName = Abc_UtilStrsav( "stg" );
    for ( i = 0; i < nIns + nBits; i++ )
        Gia_ManAppendCi(p);

    // create input minterms
    Gia_ManHashAlloc( p );
    vInMints = Vec_IntAlloc( 1 << nIns );
    for ( i = 0; i < (1 << nIns); i++ )
    {
        for ( Lit = 1, b = 0; b < nIns; b++ )
            Lit = Gia_ManHashAnd( p, Lit, Abc_Var2Lit( b+1, !((i >> b) & 1) ) );
        Vec_IntPush( vInMints, Lit );
    } 

    // create current states
    vCurs  = Vec_IntAlloc( nStates );
    Vec_VecForEachLevelInt( vCodes, vVec, i )
    {
        Lit = 1;
        Vec_IntForEachEntry( vVec, b, k )
Alan Mishchenko committed
251 252 253 254
        {
            assert( b >= 0 && b < nBits );
            Lit = Gia_ManHashAnd( p, Lit, Abc_Var2Lit(1+nIns+b, (b<kHot)) );
        }
Alan Mishchenko committed
255 256 257 258 259 260 261 262 263
        Vec_IntPush( vCurs, Lit );
    }

    // go through the lines
    vLitsNext = Vec_VecStart( nBits );
    vLitsOuts = Vec_VecStart( nOuts );
    for ( i = 0; i < Vec_IntSize(vLines); )
    {
        int iMint = Vec_IntEntry(vLines, i++);
264 265
        int iCur  = Vec_IntEntry(vLines, i++);
        int iNext = Vec_IntEntry(vLines, i++);
Alan Mishchenko committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        int iOut  = Vec_IntEntry(vLines, i++);
        assert( iMint >= 0 && iMint < (1<<nIns)  );
        assert( iCur  >= 0 && iCur  < nStates    );
        assert( iNext >= 0 && iNext < nStates    );
        assert( iOut  >= 0 && iOut  < (1<<nOuts) );
        // create condition
        LitC = Gia_ManHashAnd( p, Vec_IntEntry(vInMints, iMint), Vec_IntEntry(vCurs, iCur) );
        // update next state
//        Vec_VecPushInt( vLitsNext, iNext, LitC );
        vVec = (Vec_Int_t *)Vec_VecEntryInt( vCodes, iNext );
        Vec_IntForEachEntry( vVec, b, k )
            Vec_VecPushInt( vLitsNext, b, LitC );
        // update outputs
        for ( b = 0; b < nOuts; b++ )
            if ( (iOut >> b) & 1 ) 
                Vec_VecPushInt( vLitsOuts, b, LitC );
    }
    Vec_IntFree( vInMints );
    Vec_IntFree( vCurs );
    Vec_VecFree( vCodes );

    // create POs
Alan Mishchenko committed
288
    Vec_VecForEachLevelInt( vLitsOuts, vVec, b )
Alan Mishchenko committed
289 290 291 292
        Gia_ManAppendCo( p, Gia_ManCreateOrGate(p, vVec) );
    Vec_VecFree( vLitsOuts );

    // create next states
Alan Mishchenko committed
293 294
    Vec_VecForEachLevelInt( vLitsNext, vVec, b )
        Gia_ManAppendCo( p, Abc_LitNotCond( Gia_ManCreateOrGate(p, vVec), (b<kHot) ) );
Alan Mishchenko committed
295 296 297 298 299
    Vec_VecFree( vLitsNext );

    Gia_ManSetRegNum( p, nBits );
    Gia_ManHashStop( p );

300 301
    p = Gia_ManCleanup( pTemp = p );
    Gia_ManStop( pTemp );
Alan Mishchenko committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    assert( !Gia_ManHasDangling(p) );
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
317 318
Gia_Man_t * Gia_ManStgOneHot( Vec_Int_t * vLines, int nIns, int nOuts, int nStates )
{
319
    Gia_Man_t * p, * pTemp;
320 321
    Vec_Int_t * vInMints, * vCurs, * vVec;
    Vec_Vec_t * vLitsNext, * vLitsOuts;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    int i, b, LitC, Lit;
    assert( Vec_IntSize(vLines) % 4 == 0 );

    // start manager
    p = Gia_ManStart( 10000 );
    p->pName = Abc_UtilStrsav( "stg" );
    for ( i = 0; i < nIns + nStates; i++ )
        Gia_ManAppendCi(p);

    // create input minterms
    Gia_ManHashAlloc( p );
    vInMints = Vec_IntAlloc( 1 << nIns );
    for ( i = 0; i < (1 << nIns); i++ )
    {
        for ( Lit = 1, b = 0; b < nIns; b++ )
            Lit = Gia_ManHashAnd( p, Lit, Abc_Var2Lit( b+1, !((i >> b) & 1) ) );
        Vec_IntPush( vInMints, Lit );
    } 

    // create current states
    vCurs  = Vec_IntAlloc( nStates );
    for ( i = 0; i < nStates; i++ )
        Vec_IntPush( vCurs, Abc_Var2Lit( 1+nIns+i, !i ) );

    // go through the lines
347 348
    vLitsNext = Vec_VecStart( nStates );
    vLitsOuts = Vec_VecStart( nOuts );
349 350 351 352 353 354 355 356 357 358 359 360 361
    for ( i = 0; i < Vec_IntSize(vLines); )
    {
        int iMint = Vec_IntEntry(vLines, i++);
        int iCur  = Vec_IntEntry(vLines, i++) - 1;
        int iNext = Vec_IntEntry(vLines, i++) - 1;
        int iOut  = Vec_IntEntry(vLines, i++);
        assert( iMint >= 0 && iMint < (1<<nIns)  );
        assert( iCur  >= 0 && iCur  < nStates    );
        assert( iNext >= 0 && iNext < nStates    );
        assert( iOut  >= 0 && iOut  < (1<<nOuts) );
        // create condition
        LitC = Gia_ManHashAnd( p, Vec_IntEntry(vInMints, iMint), Vec_IntEntry(vCurs, iCur) );
        // update next state
362 363 364
//        Lit = Gia_ManHashOr( p, LitC, Vec_IntEntry(vNexts, iNext) );
//        Vec_IntWriteEntry( vNexts, iNext, Lit );
        Vec_VecPushInt( vLitsNext, iNext, LitC );
365 366 367 368
        // update outputs
        for ( b = 0; b < nOuts; b++ )
            if ( (iOut >> b) & 1 ) 
            {
369 370 371
//                Lit = Gia_ManHashOr( p, LitC, Vec_IntEntry(vOuts, b) );
//                Vec_IntWriteEntry( vOuts, b, Lit );
                Vec_VecPushInt( vLitsOuts, b, LitC );
372 373
            }
    }
374 375
    Vec_IntFree( vInMints );
    Vec_IntFree( vCurs );
376 377

    // create POs
378 379 380
    Vec_VecForEachLevelInt( vLitsOuts, vVec, i )
        Gia_ManAppendCo( p, Gia_ManCreateOrGate(p, vVec) );
    Vec_VecFree( vLitsOuts );
381 382

    // create next states
383 384 385
    Vec_VecForEachLevelInt( vLitsNext, vVec, i )
        Gia_ManAppendCo( p, Abc_LitNotCond( Gia_ManCreateOrGate(p, vVec), !i ) );
    Vec_VecFree( vLitsNext );
386 387 388 389

    Gia_ManSetRegNum( p, nStates );
    Gia_ManHashStop( p );

390 391
    p = Gia_ManCleanup( pTemp = p );
    Gia_ManStop( pTemp );
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    assert( !Gia_ManHasDangling(p) );
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManStgPrint( FILE * pFile, Vec_Int_t * vLines, int nIns, int nOuts, int nStates )
{
    int i, nDigits = Abc_Base10Log( nStates );
    assert( Vec_IntSize(vLines) % 4 == 0 );
    for ( i = 0; i < Vec_IntSize(vLines); i += 4 )
    {
        int iMint = Vec_IntEntry(vLines, i  );
        int iCur  = Vec_IntEntry(vLines, i+1) - 1;
        int iNext = Vec_IntEntry(vLines, i+2) - 1;
        int iOut  = Vec_IntEntry(vLines, i+3);
        assert( iMint >= 0 && iMint < (1<<nIns)  );
        assert( iCur  >= 0 && iCur  < nStates    );
        assert( iNext >= 0 && iNext < nStates    );
        assert( iOut  >= 0 && iOut  < (1<<nOuts) );
        Extra_PrintBinary( pFile, (unsigned *)Vec_IntEntryP(vLines, i),  nIns );
        fprintf( pFile, " %*d",  nDigits,     Vec_IntEntry(vLines,  i+1) );
        fprintf( pFile, " %*d ", nDigits,     Vec_IntEntry(vLines,  i+2) );
        Extra_PrintBinary( pFile, (unsigned *)Vec_IntEntryP(vLines, i+3), nOuts );
        fprintf( pFile, "\n" );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Gia_ManStgReadLines( char * pFileName, int * pnIns, int * pnOuts, int * pnStates )
{
    Vec_Int_t * vLines;
    char pBuffer[1000];
    char * pToken;
    int Number, nInputs = -1, nOutputs = -1, nStates = 1;
    FILE * pFile;
447 448
    if ( !strcmp(pFileName + strlen(pFileName) - 3, "aig") )
    {
Alan Mishchenko committed
449
        printf( "Input file \"%s\" has extension \"%s\".\n", pFileName, "aig" );
450 451
        return NULL;
    }
452 453 454 455 456 457 458 459 460
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\".\n", pFileName );
        return NULL;
    }
    vLines = Vec_IntAlloc( 1000 );
    while ( fgets( pBuffer, 1000, pFile ) != NULL )
    {
461 462
        if ( pBuffer[0] == '.' || pBuffer[0] == '#' )
            continue;
463
        // read condition
464
        pToken = strtok( pBuffer, " \r\n" );
465 466 467 468 469 470 471
        if ( nInputs == -1 )
            nInputs = strlen(pToken);
        else
            assert( nInputs == (int)strlen(pToken) );
        Number = Extra_ReadBinary( pToken );
        Vec_IntPush( vLines, Number );
        // read current state
472
        pToken = strtok( NULL, " \r\n" );
473
        Vec_IntPush( vLines, atoi(pToken) );
474
        nStates = Abc_MaxInt( nStates, Vec_IntEntryLast(vLines)+1 );
475
        // read next state
476
        pToken = strtok( NULL, " \r\n" );
477 478
        Vec_IntPush( vLines, atoi(pToken) );
        // read output
479
        pToken = strtok( NULL, " \r\n" );
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
        if ( nOutputs == -1 )
            nOutputs = strlen(pToken);
        else
            assert( nOutputs == (int)strlen(pToken) );
        Number = Extra_ReadBinary( pToken );
        Vec_IntPush( vLines, Number );
    }
    fclose( pFile );
    if ( pnIns )
        *pnIns = nInputs;
    if ( pnOuts )
        *pnOuts = nOutputs;
    if ( pnStates )
        *pnStates = nStates;
    return vLines;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
508
Gia_Man_t * Gia_ManStgRead( char * pFileName, int kHot, int fVerbose )
509 510 511 512 513 514 515
{
    Gia_Man_t * p;
    Vec_Int_t * vLines;
    int nIns, nOuts, nStates;
    vLines = Gia_ManStgReadLines( pFileName, &nIns, &nOuts, &nStates );
    if ( vLines == NULL )
        return NULL;
Alan Mishchenko committed
516
//    p = Gia_ManStgOneHot( vLines, nIns, nOuts, nStates );
Alan Mishchenko committed
517
    p = Gia_ManStgKHot( vLines, nIns, nOuts, nStates, kHot, fVerbose );
518 519 520 521 522 523 524 525 526 527 528
    Vec_IntFree( vLines );
    return p;
}

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


ABC_NAMESPACE_IMPL_END