ioWritePla.c 13.9 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    [ioWritePla.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write the network in BENCH format.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

Alan Mishchenko committed
21
#include "ioAbc.h"
22 23

#ifdef ABC_USE_CUDD
24
#include "bdd/extrab/extraBdd.h"
25
#endif
Alan Mishchenko committed
26

27 28 29
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
30 31 32 33 34
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
35
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
36 37 38 39
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
40
  Synopsis    [Writes the network in PLA format.]
Alan Mishchenko committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WritePlaOne( FILE * pFile, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Obj_t * pNode, * pFanin, * pDriver;
    char * pCubeIn, * pCubeOut, * pCube;
    int i, k, nProducts, nInputs, nOutputs, nFanins;

    nProducts = 0;
Alan Mishchenko committed
57
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
58
    {
Alan Mishchenko committed
59
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
Alan Mishchenko committed
60 61 62 63 64 65 66 67 68 69 70
        if ( !Abc_ObjIsNode(pDriver) )
        {
            nProducts++;
            continue;
        }
        if ( Abc_NodeIsConst(pDriver) )
        {
            if ( Abc_NodeIsConst1(pDriver) )
                nProducts++;
            continue;
        }
71
        nProducts += Abc_SopGetCubeNum((char *)pDriver->pData);
Alan Mishchenko committed
72 73 74 75 76
    }

    // collect the parameters
    nInputs  = Abc_NtkCiNum(pNtk);
    nOutputs = Abc_NtkCoNum(pNtk);
Alan Mishchenko committed
77 78
    pCubeIn  = ABC_ALLOC( char, nInputs + 1 );
    pCubeOut = ABC_ALLOC( char, nOutputs + 1 );
Alan Mishchenko committed
79 80 81 82 83 84 85 86
    memset( pCubeIn,  '-', nInputs );     pCubeIn[nInputs]   = 0;
    memset( pCubeOut, '0', nOutputs );    pCubeOut[nOutputs] = 0;

    // write the header
    fprintf( pFile, ".i %d\n", nInputs );
    fprintf( pFile, ".o %d\n", nOutputs );
    fprintf( pFile, ".ilb" );
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
87
        fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanout0(pNode)) );
Alan Mishchenko committed
88 89 90
    fprintf( pFile, "\n" );
    fprintf( pFile, ".ob" );
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
91
        fprintf( pFile, " %s", Abc_ObjName(Abc_ObjFanin0(pNode)) );
Alan Mishchenko committed
92 93 94 95 96
    fprintf( pFile, "\n" );
    fprintf( pFile, ".p %d\n", nProducts );

    // mark the CI nodes
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
97
        pNode->pCopy = (Abc_Obj_t *)(ABC_PTRUINT_T)i;
Alan Mishchenko committed
98 99 100 101 102 103 104 105 106 107 108

    // write the cubes
    pProgress = Extra_ProgressBarStart( stdout, nOutputs );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        // prepare the output cube
        if ( i - 1 >= 0 )
            pCubeOut[i-1] = '0';
        pCubeOut[i] = '1';

        // consider special cases of nodes
Alan Mishchenko committed
109
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
Alan Mishchenko committed
110 111
        if ( !Abc_ObjIsNode(pDriver) )
        {
Alan Mishchenko committed
112
            assert( Abc_ObjIsCi(pDriver) );
Alan Mishchenko committed
113
            pCubeIn[(int)(ABC_PTRUINT_T)pDriver->pCopy] = '1' - Abc_ObjFaninC0(pNode);
Alan Mishchenko committed
114
            fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
Alan Mishchenko committed
115
            pCubeIn[(int)(ABC_PTRUINT_T)pDriver->pCopy] = '-';
Alan Mishchenko committed
116 117 118 119 120 121 122 123
            continue;
        }
        if ( Abc_NodeIsConst(pDriver) )
        {
            if ( Abc_NodeIsConst1(pDriver) )
                fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
            continue;
        }
Alan Mishchenko committed
124 125

        // make sure the cover is not complemented
126
        assert( !Abc_SopIsComplement( (char *)pDriver->pData ) );
Alan Mishchenko committed
127

Alan Mishchenko committed
128 129
        // write the cubes
        nFanins = Abc_ObjFaninNum(pDriver);
130
        Abc_SopForEachCube( (char *)pDriver->pData, nFanins, pCube )
Alan Mishchenko committed
131 132
        {
            Abc_ObjForEachFanin( pDriver, pFanin, k )
Alan Mishchenko committed
133 134
            {
                pFanin = Abc_ObjFanin0Ntk(pFanin);
Alan Mishchenko committed
135 136
                assert( (int)(ABC_PTRUINT_T)pFanin->pCopy < nInputs );
                pCubeIn[(int)(ABC_PTRUINT_T)pFanin->pCopy] = pCube[k];
Alan Mishchenko committed
137
            }
Alan Mishchenko committed
138 139 140 141
            fprintf( pFile, "%s %s\n", pCubeIn, pCubeOut );
        }
        // clean the cube for future writing
        Abc_ObjForEachFanin( pDriver, pFanin, k )
Alan Mishchenko committed
142 143 144
        {
            pFanin = Abc_ObjFanin0Ntk(pFanin);
            assert( Abc_ObjIsCi(pFanin) );
Alan Mishchenko committed
145
            pCubeIn[(int)(ABC_PTRUINT_T)pFanin->pCopy] = '-';
Alan Mishchenko committed
146
        }
Alan Mishchenko committed
147 148 149 150 151 152 153 154
        Extra_ProgressBarUpdate( pProgress, i, NULL );
    }
    Extra_ProgressBarStop( pProgress );
    fprintf( pFile, ".e\n" );

    // clean the CI nodes
    Abc_NtkForEachCi( pNtk, pNode, i )
        pNode->pCopy = NULL;
Alan Mishchenko committed
155 156
    ABC_FREE( pCubeIn );
    ABC_FREE( pCubeOut );
Alan Mishchenko committed
157 158 159
    return 1;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/**Function*************************************************************

  Synopsis    [Writes the network in PLA format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WritePla( Abc_Ntk_t * pNtk, char * pFileName )
{
    Abc_Ntk_t * pExdc;
    FILE * pFile;

    assert( Abc_NtkIsSopNetlist(pNtk) );
    assert( Abc_NtkLevel(pNtk) == 1 );

    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Io_WritePla(): Cannot open the output file.\n" );
        return 0;
    }
    fprintf( pFile, "# Benchmark \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
    // write the network
    Io_WritePlaOne( pFile, pNtk );
    // write EXDC network if it exists
    pExdc = Abc_NtkExdc( pNtk );
    if ( pExdc )
        printf( "Io_WritePla: EXDC is not written (warning).\n" );
    // finalize the file
    fclose( pFile );
    return 1;
}

197
#ifdef ABC_USE_CUDD
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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

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

  Synopsis    [Writes the network in PLA format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteMoPlaOneInt( FILE * pFile, Abc_Ntk_t * pNtk, DdManager * dd, Vec_Ptr_t * vFuncs )
{
    Abc_Obj_t * pNode;
    DdNode * bOnset, * bOffset, * bCube, * bFunc, * bTemp, * zCover;
    int i, k, nInputs, nOutputs;
    int nCubes, fPhase;

    assert( Vec_PtrSize(vFuncs) == Abc_NtkCoNum(pNtk) );
    assert( dd->size == Abc_NtkCiNum(pNtk) );
    assert( dd->size <= 1000 );

    // collect the parameters
    nInputs   = Abc_NtkCiNum(pNtk);
    nOutputs  = Abc_NtkCoNum(pNtk);
    assert( nOutputs > 1 );

    // create extra variables
    for ( i = 0; i < nOutputs; i++ )
        Cudd_bddNewVarAtLevel( dd, i );
    assert( dd->size == nInputs + nOutputs );

    // create ON and OFF sets
    bOnset = Cudd_ReadLogicZero( dd );  Cudd_Ref(bOnset);
    bOffset = Cudd_ReadLogicZero( dd ); Cudd_Ref(bOffset);
    for ( i = 0; i < nOutputs; i++ )
    {
        bFunc = (DdNode *)Vec_PtrEntry(vFuncs, i);
        // create onset
        bCube = Cudd_bddAnd( dd, Cudd_bddIthVar(dd, nInputs+i), bFunc );  Cudd_Ref(bCube);
        for ( k = 0; k < nOutputs; k++ ) 
            if ( k != i )
            {
                bCube = Cudd_bddAnd( dd, bTemp = bCube, Cudd_Not(Cudd_bddIthVar(dd, nInputs+k)) );  Cudd_Ref(bCube);
                Cudd_RecursiveDeref( dd, bTemp );
            }
        bOnset = Cudd_bddOr( dd, bTemp = bOnset, bCube );   Cudd_Ref(bOnset);
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bCube );
        // create offset
        bCube = Cudd_bddAnd( dd, Cudd_bddIthVar(dd, nInputs+i), Cudd_Not(bFunc) );  Cudd_Ref(bCube);
        bOffset = Cudd_bddOr( dd, bTemp = bOffset, bCube );                         Cudd_Ref(bOffset);
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bCube );

        printf( "Trying %d output.\n", i );
        printf( "Onset = %d nodes.\n", Cudd_DagSize(bOnset) );
        printf( "Offset = %d nodes.\n", Cudd_DagSize(bOffset) );
    }

    Cudd_zddVarsFromBddVars( dd, 2 );

    // derive ISOP
    {
        extern int Abc_CountZddCubes( DdManager * dd, DdNode * zCover );
        DdNode * bCover, * zCover0, * zCover1;
        int nCubes0, nCubes1;
        // get the ZDD of the negative polarity
        bCover = Cudd_zddIsop( dd, bOffset, Cudd_Not(bOnset), &zCover0 );
        Cudd_Ref( zCover0 );
        Cudd_Ref( bCover );
        Cudd_RecursiveDeref( dd, bCover );
        nCubes0 = Abc_CountZddCubes( dd, zCover0 );

        // get the ZDD of the positive polarity
        bCover = Cudd_zddIsop( dd, bOnset, Cudd_Not(bOffset), &zCover1 );
        Cudd_Ref( zCover1 );
        Cudd_Ref( bCover );
        Cudd_RecursiveDeref( dd, bCover );
        nCubes1 = Abc_CountZddCubes( dd, zCover1 );

        // compare the number of cubes
        if ( nCubes1 <= nCubes0 )
        { // use positive polarity
            nCubes = nCubes1;
            zCover = zCover1;
            Cudd_RecursiveDerefZdd( dd, zCover0 );
            fPhase = 1;
        }
        else
        { // use negative polarity
            nCubes = nCubes0;
            zCover = zCover0;
            Cudd_RecursiveDerefZdd( dd, zCover1 );
            fPhase = 0;
        }
    }
    Cudd_RecursiveDeref( dd, bOnset );
    Cudd_RecursiveDeref( dd, bOffset );
    Cudd_RecursiveDerefZdd( dd, zCover );
    printf( "Cover = %d nodes.\n", Cudd_DagSize(zCover) );
    printf( "ISOP = %d\n", nCubes );

    // write the header
    fprintf( pFile, ".i %d\n", nInputs );
    fprintf( pFile, ".o %d\n", nOutputs );
    fprintf( pFile, ".ilb" );
    Abc_NtkForEachCi( pNtk, pNode, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".ob" );
    Abc_NtkForEachCo( pNtk, pNode, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".p %d\n", nCubes );


    fprintf( pFile, ".e\n" );
    return 1;
}

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

  Synopsis    [Writes the network in PLA format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteMoPlaOneIntMinterms( FILE * pFile, Abc_Ntk_t * pNtk, DdManager * dd, Vec_Ptr_t * vFuncs )
{
    int pValues[1000];
    Abc_Obj_t * pNode;
    int i, k, nProducts, nInputs, nOutputs;
    assert( Vec_PtrSize(vFuncs) == Abc_NtkCoNum(pNtk) );
    assert( dd->size == Abc_NtkCiNum(pNtk) );
    assert( dd->size <= 1000 );

    // collect the parameters
    nInputs   = Abc_NtkCiNum(pNtk);
    nOutputs  = Abc_NtkCoNum(pNtk);
    nProducts = (1 << nInputs);

    // write the header
    fprintf( pFile, ".i %d\n", nInputs );
    fprintf( pFile, ".o %d\n", nOutputs );
    fprintf( pFile, ".ilb" );
    Abc_NtkForEachCi( pNtk, pNode, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".ob" );
    Abc_NtkForEachCo( pNtk, pNode, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".p %d\n", nProducts );

    // iterate through minterms
    for ( k = 0; k < nProducts; k++ )
    {
        for ( i = 0; i < nInputs; i++ )
            fprintf( pFile, "%c", '0' + (pValues[i] = ((k >> i) & 1)) );
        fprintf( pFile, " " );
        for ( i = 0; i < nOutputs; i++ )
            fprintf( pFile, "%c", '0' + (Cudd_ReadOne(dd) == Cudd_Eval(dd, (DdNode *)Vec_PtrEntry(vFuncs, i), pValues)) );
        fprintf( pFile, "\n" );
    }

    fprintf( pFile, ".e\n" );
    return 1;
}

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

  Synopsis    [Writes the network in PLA format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteMoPlaOne( FILE * pFile, Abc_Ntk_t * pNtk )
{
    int fVerbose = 1;
    DdManager * dd;
    DdNode * bFunc;
    Vec_Ptr_t * vFuncsGlob;
    Abc_Obj_t * pObj;
    int i;
    assert( Abc_NtkIsStrash(pNtk) );
    dd = (DdManager *)Abc_NtkBuildGlobalBdds( pNtk, 10000000, 1, 1, fVerbose );
    if ( dd == NULL )
        return 0;
    if ( fVerbose )
        printf( "Shared BDD size = %6d nodes.\n", Cudd_ReadKeys(dd) - Cudd_ReadDead(dd) );

    // complement the global functions
    vFuncsGlob = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pObj, i )
        Vec_PtrPush( vFuncsGlob, Abc_ObjGlobalBdd(pObj) );

    // consider minterms
    Io_WriteMoPlaOneIntMinterms( pFile, pNtk, dd, vFuncsGlob );
    Abc_NtkFreeGlobalBdds( pNtk, 0 );

    // cleanup
    Vec_PtrForEachEntry( DdNode *, vFuncsGlob, bFunc, i )
        Cudd_RecursiveDeref( dd, bFunc );
    Vec_PtrFree( vFuncsGlob );
    Extra_StopManager( dd );
    return 1;
}

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

  Synopsis    [Writes the network in PLA format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteMoPla( Abc_Ntk_t * pNtk, char * pFileName )
{
    FILE * pFile;
    assert( Abc_NtkIsStrash(pNtk) );
    if ( Abc_NtkCiNum(pNtk) > 16 )
    {
        printf( "Cannot write multi-output PLA for more than 16 inputs.\n" );
        return 0;
    }    
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Io_WritePla(): Cannot open the output file.\n" );
        return 0;
    }
    fprintf( pFile, "# Benchmark \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
    Io_WriteMoPlaOne( pFile, pNtk );
    fclose( pFile );
    return 1;
}
Alan Mishchenko committed
447

448 449 450 451 452 453
#else

int Io_WriteMoPla( Abc_Ntk_t * pNtk, char * pFileName ) { return 1; }

#endif

Alan Mishchenko committed
454 455 456 457 458
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


459 460
ABC_NAMESPACE_IMPL_END