ntlWriteBlif.c 24.1 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    [ntlWriteBlif.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write BLIF files.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

Alan Mishchenko committed
21 22
// The code in this file is developed in collaboration with Mark Jarvin of Toronto.

Alan Mishchenko committed
23 24 25
#include "ntl.h"
#include "ioa.h"

Alan Mishchenko committed
26
#include <stdarg.h>
Alan Mishchenko committed
27 28
#include "bzlib.h"
#include "zlib.h"
Alan Mishchenko committed
29

30 31 32
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
33 34 35 36
#ifdef _WIN32
#define vsnprintf _vsnprintf
#endif

Alan Mishchenko committed
37 38 39 40 41 42 43
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
44
 
Alan Mishchenko committed
45 46 47 48 49 50 51 52 53 54 55
/**Function*************************************************************

  Synopsis    [Writes one model into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
56
void Ntl_ManWriteBlifModel( FILE * pFile, Ntl_Mod_t * pModel, int fMain )
Alan Mishchenko committed
57 58 59
{
    Ntl_Obj_t * pObj;
    Ntl_Net_t * pNet;
Alan Mishchenko committed
60
    float Delay;
Alan Mishchenko committed
61
    int i, k;
Alan Mishchenko committed
62
    fprintf( pFile, ".model %s\n", pModel->pName );
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71
    if ( pModel->attrWhite || pModel->attrBox || pModel->attrComb || pModel->attrKeep )
    {
        fprintf( pFile, ".attrib" );
        fprintf( pFile, " %s", pModel->attrWhite? "white": "black" );
        fprintf( pFile, " %s", pModel->attrBox?   "box"  : "logic" );
        fprintf( pFile, " %s", pModel->attrComb?  "comb" : "seq"   );
//        fprintf( pFile, " %s", pModel->attrKeep?  "keep" : "sweep" );
        fprintf( pFile, "\n" );
    }
Alan Mishchenko committed
72 73
    if ( pModel->attrNoMerge )
        fprintf( pFile, ".no_merge\n" );
Alan Mishchenko committed
74 75 76 77 78 79 80 81
    fprintf( pFile, ".inputs" );
    Ntl_ModelForEachPi( pModel, pObj, i )
        fprintf( pFile, " %s", Ntl_ObjFanout0(pObj)->pName );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".outputs" );
    Ntl_ModelForEachPo( pModel, pObj, i )
        fprintf( pFile, " %s", Ntl_ObjFanin0(pObj)->pName );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
82 83 84 85 86 87
    // write delays
    if ( pModel->vDelays )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vDelays); i += 3 )
        {
            fprintf( pFile, ".delay" );
Alan Mishchenko committed
88 89 90 91
            if ( Vec_IntEntry(pModel->vDelays,i) != -1 )
                fprintf( pFile, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vDelays,i)))->pName );
            if ( Vec_IntEntry(pModel->vDelays,i+1) != -1 )
                fprintf( pFile, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vDelays,i+1)))->pName );
Alan Mishchenko committed
92 93 94 95
            fprintf( pFile, " %.3f", Aig_Int2Float(Vec_IntEntry(pModel->vDelays,i+2)) );
            fprintf( pFile, "\n" );
        }
    }
Alan Mishchenko committed
96
    if ( pModel->vTimeInputs )
Alan Mishchenko committed
97
    {
Alan Mishchenko committed
98
        for ( i = 0; i < Vec_IntSize(pModel->vTimeInputs); i += 2 )
Alan Mishchenko committed
99
        {
Alan Mishchenko committed
100 101 102 103 104 105 106
            if ( fMain )
                fprintf( pFile, ".input_arrival" );
            else
                fprintf( pFile, ".input_required" );
            if ( Vec_IntEntry(pModel->vTimeInputs,i) != -1 )
                fprintf( pFile, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vTimeInputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeInputs,i+1));
Alan Mishchenko committed
107 108 109 110 111 112 113 114 115
            if ( Delay == -TIM_ETERNITY )
                fprintf( pFile, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                fprintf( pFile, " inf" );
            else
                fprintf( pFile, " %.3f", Delay );
            fprintf( pFile, "\n" );
        }
    }
Alan Mishchenko committed
116
    if ( pModel->vTimeOutputs )
Alan Mishchenko committed
117
    {
Alan Mishchenko committed
118
        for ( i = 0; i < Vec_IntSize(pModel->vTimeOutputs); i += 2 )
Alan Mishchenko committed
119
        {
Alan Mishchenko committed
120 121 122 123 124 125 126
            if ( fMain )
                fprintf( pFile, ".output_required" );
            else
                fprintf( pFile, ".output_arrival" );
            if ( Vec_IntEntry(pModel->vTimeOutputs,i) != -1 )
                fprintf( pFile, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vTimeOutputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeOutputs,i+1));
Alan Mishchenko committed
127 128 129 130 131 132 133 134 135 136
            if ( Delay == -TIM_ETERNITY )
                fprintf( pFile, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                fprintf( pFile, " inf" );
            else
                fprintf( pFile, " %.3f", Delay );
            fprintf( pFile, "\n" );
        }
    }
    // write objects
Alan Mishchenko committed
137 138 139 140 141 142 143 144 145
    Ntl_ModelForEachObj( pModel, pObj, i )
    {
        if ( Ntl_ObjIsNode(pObj) )
        {
            fprintf( pFile, ".names" );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                fprintf( pFile, " %s", pNet->pName );
            fprintf( pFile, " %s\n", Ntl_ObjFanout0(pObj)->pName );
            fprintf( pFile, "%s", pObj->pSop );
Alan Mishchenko committed
146 147
            if ( *pObj->pSop == '\"' )
                fprintf( pFile, "\n" );
Alan Mishchenko committed
148 149 150 151 152 153
        }
        else if ( Ntl_ObjIsLatch(pObj) )
        {
            fprintf( pFile, ".latch" );
            fprintf( pFile, " %s", Ntl_ObjFanin0(pObj)->pName );
            fprintf( pFile, " %s", Ntl_ObjFanout0(pObj)->pName );
Alan Mishchenko committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            assert( pObj->LatchId.regType == 0 || pObj->LatchId.regClass == 0 );
            if ( pObj->LatchId.regType )
            {
                if ( pObj->LatchId.regType == 1 )
                    fprintf( pFile, " fe" );
                else if ( pObj->LatchId.regType == 2 )
                    fprintf( pFile, " re" );
                else if ( pObj->LatchId.regType == 3 )
                    fprintf( pFile, " ah" );
                else if ( pObj->LatchId.regType == 4 )
                    fprintf( pFile, " al" );
                else if ( pObj->LatchId.regType == 5 )
                    fprintf( pFile, " as" );
                else
                    assert( 0 );
            }
            else if ( pObj->LatchId.regClass )
                fprintf( pFile, " %d", pObj->LatchId.regClass );
            if ( pObj->pClock )
                fprintf( pFile, " %s", pObj->pClock->pName );
            fprintf( pFile, " %d", pObj->LatchId.regInit );
Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            fprintf( pFile, "\n" );
        }
        else if ( Ntl_ObjIsBox(pObj) )
        {
            fprintf( pFile, ".subckt %s", pObj->pImplem->pName );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                fprintf( pFile, " %s=%s", Ntl_ModelPiName(pObj->pImplem, k), pNet->pName );
            Ntl_ObjForEachFanout( pObj, pNet, k )
                fprintf( pFile, " %s=%s", Ntl_ModelPoName(pObj->pImplem, k), pNet->pName );
            fprintf( pFile, "\n" );
        }
    }
    fprintf( pFile, ".end\n\n" );
}

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

Alan Mishchenko committed
192
  Synopsis    [Writes the netlist into the BLIF file.]
Alan Mishchenko committed
193 194 195 196 197 198 199 200

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
201
void Ntl_ManWriteBlif_old( Ntl_Man_t * p, char * pFileName )
Alan Mishchenko committed
202 203 204 205 206 207 208 209
{
    FILE * pFile;
    Ntl_Mod_t * pModel;
    int i;
    // start the output stream
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
210
        fprintf( stdout, "Ntl_ManWriteBlif(): Cannot open the output file \"%s\".\n", pFileName );
Alan Mishchenko committed
211 212
        return;
    }
213
    fprintf( pFile, "# Benchmark \"%s\" written by ABC-8 on %s\n", p->pName, Aig_TimeStamp() );
Alan Mishchenko committed
214 215
    // write the models
    Ntl_ManForEachModel( p, pModel, i )
216
        Ntl_ManWriteBlifModel( pFile, pModel, i==0 );
Alan Mishchenko committed
217 218 219 220
    // close the file
    fclose( pFile );
}

Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230 231
/**Function*************************************************************

  Synopsis    [Writes the logic network into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
232
void Ntl_ManWriteBlifLogic( Nwk_Man_t * pNtk, Ntl_Man_t * p, char * pFileName )
Alan Mishchenko committed
233 234 235
{
    Ntl_Man_t * pNew;
    pNew = Ntl_ManInsertNtk( p, pNtk );
236
    Ntl_ManWriteBlif( pNew, pFileName );
Alan Mishchenko committed
237 238 239
    Ntl_ManFree( pNew );
}

Alan Mishchenko committed
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

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

  Synopsis    [Procedure to write data into BZ2 file.]

  Description [Based on the vsnprintf() man page.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
typedef struct bz2file {
  FILE   * f;
  BZFILE * b;
  char   * buf;
  int      nBytes;
  int      nBytesMax;
} bz2file;

int fprintfBz2(bz2file * b, char * fmt, ...) {
    if (b->b) {
        char * newBuf;
        int bzError;
        va_list ap;
        while (1) {
            va_start(ap,fmt);
            b->nBytes = vsnprintf(b->buf,b->nBytesMax,fmt,ap);
            va_end(ap);
            if (b->nBytes > -1 && b->nBytes < b->nBytesMax)
                break;
            if (b->nBytes > -1)
                b->nBytesMax = b->nBytes + 1;
            else
                b->nBytesMax *= 2;
Alan Mishchenko committed
276
            if ((newBuf = ABC_REALLOC( char,b->buf,b->nBytesMax )) == NULL)
Alan Mishchenko committed
277 278 279 280 281 282
                return -1;
            else
                b->buf = newBuf;
        }
        BZ2_bzWrite( &bzError, b->b, b->buf, b->nBytes );
        if (bzError == BZ_IO_ERROR) {
283
            fprintf( stdout, "Ntl_ManWriteBlif(): I/O error writing to compressed stream.\n" );
Alan Mishchenko committed
284 285 286 287 288 289 290 291 292 293 294 295 296
            return -1;
        }
        return b->nBytes;
    } else {
        int n;
        va_list ap;
        va_start(ap,fmt);
        n = vfprintf( b->f, fmt, ap);
        va_end(ap);
        return n;
    }
}

Alan Mishchenko committed
297 298 299 300 301 302 303 304 305 306 307
/**Function*************************************************************

  Synopsis    [Writes one model into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
308
void Ntl_ManWriteBlifModelGz( gzFile pFile, Ntl_Mod_t * pModel, int fMain )
Alan Mishchenko committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{
    Ntl_Obj_t * pObj;
    Ntl_Net_t * pNet;
    float Delay;
    int i, k;
    gzprintf( pFile, ".model %s\n", pModel->pName );
    if ( pModel->attrWhite || pModel->attrBox || pModel->attrComb || pModel->attrKeep )
    {
        gzprintf( pFile, ".attrib" );
        gzprintf( pFile, " %s", pModel->attrWhite? "white": "black" );
        gzprintf( pFile, " %s", pModel->attrBox?   "box"  : "logic" );
        gzprintf( pFile, " %s", pModel->attrComb?  "comb" : "seq"   );
//        gzprintf( pFile, " %s", pModel->attrKeep?  "keep" : "sweep" );
        gzprintf( pFile, "\n" );
    }
Alan Mishchenko committed
324 325
    if ( pModel->attrNoMerge )
        gzprintf( pFile, ".no_merge\n" );    
Alan Mishchenko committed
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
    gzprintf( pFile, ".inputs" );
    Ntl_ModelForEachPi( pModel, pObj, i )
        gzprintf( pFile, " %s", Ntl_ObjFanout0(pObj)->pName );
    gzprintf( pFile, "\n" );
    gzprintf( pFile, ".outputs" );
    Ntl_ModelForEachPo( pModel, pObj, i )
        gzprintf( pFile, " %s", Ntl_ObjFanin0(pObj)->pName );
    gzprintf( pFile, "\n" );
    // write delays
    if ( pModel->vDelays )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vDelays); i += 3 )
        {
            gzprintf( pFile, ".delay" );
            if ( Vec_IntEntry(pModel->vDelays,i) != -1 )
                gzprintf( pFile, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vDelays,i)))->pName );
            if ( Vec_IntEntry(pModel->vDelays,i+1) != -1 )
                gzprintf( pFile, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vDelays,i+1)))->pName );
            gzprintf( pFile, " %.3f", Aig_Int2Float(Vec_IntEntry(pModel->vDelays,i+2)) );
            gzprintf( pFile, "\n" );
        }
    }
    if ( pModel->vTimeInputs )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vTimeInputs); i += 2 )
        {
            if ( fMain )
                gzprintf( pFile, ".input_arrival" );
            else
                gzprintf( pFile, ".input_required" );
            if ( Vec_IntEntry(pModel->vTimeInputs,i) != -1 )
                gzprintf( pFile, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vTimeInputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeInputs,i+1));
            if ( Delay == -TIM_ETERNITY )
                gzprintf( pFile, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                gzprintf( pFile, " inf" );
            else
                gzprintf( pFile, " %.3f", Delay );
            gzprintf( pFile, "\n" );
        }
    }
    if ( pModel->vTimeOutputs )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vTimeOutputs); i += 2 )
        {
            if ( fMain )
                gzprintf( pFile, ".output_required" );
            else
                gzprintf( pFile, ".output_arrival" );
            if ( Vec_IntEntry(pModel->vTimeOutputs,i) != -1 )
                gzprintf( pFile, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vTimeOutputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeOutputs,i+1));
            if ( Delay == -TIM_ETERNITY )
                gzprintf( pFile, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                gzprintf( pFile, " inf" );
            else
                gzprintf( pFile, " %.3f", Delay );
            gzprintf( pFile, "\n" );
        }
    }
    // write objects
    Ntl_ModelForEachObj( pModel, pObj, i )
    {
        if ( Ntl_ObjIsNode(pObj) )
        {
            gzprintf( pFile, ".names" );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                gzprintf( pFile, " %s", pNet->pName );
            gzprintf( pFile, " %s\n", Ntl_ObjFanout0(pObj)->pName );
            gzprintf( pFile, "%s", pObj->pSop );
Alan Mishchenko committed
398 399
            if ( *pObj->pSop == '\"' )
                gzprintf( pFile, "\n" );
Alan Mishchenko committed
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 447 448 449 450 451 452
        }
        else if ( Ntl_ObjIsLatch(pObj) )
        {
            gzprintf( pFile, ".latch" );
            gzprintf( pFile, " %s", Ntl_ObjFanin0(pObj)->pName );
            gzprintf( pFile, " %s", Ntl_ObjFanout0(pObj)->pName );
            assert( pObj->LatchId.regType == 0 || pObj->LatchId.regClass == 0 );
            if ( pObj->LatchId.regType )
            {
                if ( pObj->LatchId.regType == 1 )
                    gzprintf( pFile, " fe" );
                else if ( pObj->LatchId.regType == 2 )
                    gzprintf( pFile, " re" );
                else if ( pObj->LatchId.regType == 3 )
                    gzprintf( pFile, " ah" );
                else if ( pObj->LatchId.regType == 4 )
                    gzprintf( pFile, " al" );
                else if ( pObj->LatchId.regType == 5 )
                    gzprintf( pFile, " as" );
                else
                    assert( 0 );
            }
            else if ( pObj->LatchId.regClass )
                gzprintf( pFile, " %d", pObj->LatchId.regClass );
            if ( pObj->pClock )
                gzprintf( pFile, " %s", pObj->pClock->pName );
            gzprintf( pFile, " %d", pObj->LatchId.regInit );
            gzprintf( pFile, "\n" );
        }
        else if ( Ntl_ObjIsBox(pObj) )
        {
            gzprintf( pFile, ".subckt %s", pObj->pImplem->pName );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                gzprintf( pFile, " %s=%s", Ntl_ModelPiName(pObj->pImplem, k), pNet->pName );
            Ntl_ObjForEachFanout( pObj, pNet, k )
                gzprintf( pFile, " %s=%s", Ntl_ModelPoName(pObj->pImplem, k), pNet->pName );
            gzprintf( pFile, "\n" );
        }
    }
    gzprintf( pFile, ".end\n\n" );
}

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

  Synopsis    [Writes the logic network into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
453
void Ntl_ManWriteBlifGz( Ntl_Man_t * p, char * pFileName )
Alan Mishchenko committed
454 455 456 457 458 459 460 461 462
{
    Ntl_Mod_t * pModel;
    int i;
    gzFile pFile;

    // start the output stream
    pFile = gzopen( pFileName, "wb" ); // if pFileName doesn't end in ".gz" then this acts as a passthrough to fopen
    if ( pFile == NULL )
    {
463
        fprintf( stdout, "Ntl_ManWriteBlif(): Cannot open the output file \"%s\".\n", pFileName );
Alan Mishchenko committed
464 465 466
        return;
    }

467
    gzprintf( pFile, "# Benchmark \"%s\" written by ABC-8 on %s\n", p->pName, Aig_TimeStamp() );
Alan Mishchenko committed
468 469
    // write the models
    Ntl_ManForEachModel( p, pModel, i )
470
        Ntl_ManWriteBlifModelGz( pFile, pModel, i==0 );
Alan Mishchenko committed
471 472 473 474
    // close the file
    gzclose( pFile );
}

Alan Mishchenko committed
475 476 477 478 479 480 481 482 483 484 485 486

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

  Synopsis    [Writes one model into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
487
void Ntl_ManWriteBlifModelBz2( bz2file * b, Ntl_Mod_t * pModel, int fMain )
Alan Mishchenko committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
{
    Ntl_Obj_t * pObj;
    Ntl_Net_t * pNet;
    float Delay;
    int i, k;
    fprintfBz2( b, ".model %s\n", pModel->pName );
    if ( pModel->attrWhite || pModel->attrBox || pModel->attrComb || pModel->attrKeep )
    {
        fprintfBz2( b, ".attrib" );
        fprintfBz2( b, " %s", pModel->attrWhite? "white": "black" );
        fprintfBz2( b, " %s", pModel->attrBox?   "box"  : "logic" );
        fprintfBz2( b, " %s", pModel->attrComb?  "comb" : "seq"   );
//        fprintfBz2( b, " %s", pModel->attrKeep?  "keep" : "sweep" );
        fprintfBz2( b, "\n" );
    }
Alan Mishchenko committed
503 504
    if ( pModel->attrNoMerge )
        fprintfBz2( b, ".no_merge\n" );    
Alan Mishchenko committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    fprintfBz2( b, ".inputs" );
    Ntl_ModelForEachPi( pModel, pObj, i )
        fprintfBz2( b, " %s", Ntl_ObjFanout0(pObj)->pName );
    fprintfBz2( b, "\n" );
    fprintfBz2( b, ".outputs" );
    Ntl_ModelForEachPo( pModel, pObj, i )
        fprintfBz2( b, " %s", Ntl_ObjFanin0(pObj)->pName );
    fprintfBz2( b, "\n" );
    // write delays
    if ( pModel->vDelays )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vDelays); i += 3 )
        {
            fprintfBz2( b, ".delay" );
            if ( Vec_IntEntry(pModel->vDelays,i) != -1 )
                fprintfBz2( b, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vDelays,i)))->pName );
            if ( Vec_IntEntry(pModel->vDelays,i+1) != -1 )
                fprintfBz2( b, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vDelays,i+1)))->pName );
            fprintfBz2( b, " %.3f", Aig_Int2Float(Vec_IntEntry(pModel->vDelays,i+2)) );
            fprintfBz2( b, "\n" );
        }
    }
    if ( pModel->vTimeInputs )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vTimeInputs); i += 2 )
        {
            if ( fMain )
                fprintfBz2( b, ".input_arrival" );
            else
                fprintfBz2( b, ".input_required" );
            if ( Vec_IntEntry(pModel->vTimeInputs,i) != -1 )
                fprintfBz2( b, " %s", Ntl_ObjFanout0(Ntl_ModelPi(pModel, Vec_IntEntry(pModel->vTimeInputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeInputs,i+1));
            if ( Delay == -TIM_ETERNITY )
                fprintfBz2( b, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                fprintfBz2( b, " inf" );
            else
                fprintfBz2( b, " %.3f", Delay );
            fprintfBz2( b, "\n" );
        }
    }
    if ( pModel->vTimeOutputs )
    {
        for ( i = 0; i < Vec_IntSize(pModel->vTimeOutputs); i += 2 )
        {
            if ( fMain )
                fprintfBz2( b, ".output_required" );
            else
                fprintfBz2( b, ".output_arrival" );
            if ( Vec_IntEntry(pModel->vTimeOutputs,i) != -1 )
                fprintfBz2( b, " %s", Ntl_ObjFanin0(Ntl_ModelPo(pModel, Vec_IntEntry(pModel->vTimeOutputs,i)))->pName );
            Delay = Aig_Int2Float(Vec_IntEntry(pModel->vTimeOutputs,i+1));
            if ( Delay == -TIM_ETERNITY )
                fprintfBz2( b, " -inf" );
            else if ( Delay == TIM_ETERNITY )
                fprintfBz2( b, " inf" );
            else
                fprintfBz2( b, " %.3f", Delay );
            fprintfBz2( b, "\n" );
        }
    }
    // write objects
    Ntl_ModelForEachObj( pModel, pObj, i )
    {
        if ( Ntl_ObjIsNode(pObj) )
        {
            fprintfBz2( b, ".names" );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                fprintfBz2( b, " %s", pNet->pName );
            fprintfBz2( b, " %s\n", Ntl_ObjFanout0(pObj)->pName );
            fprintfBz2( b, "%s", pObj->pSop );
Alan Mishchenko committed
577 578
            if ( *pObj->pSop == '\"' )
                fprintfBz2( b, "\n" );
Alan Mishchenko committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
        }
        else if ( Ntl_ObjIsLatch(pObj) )
        {
            fprintfBz2( b, ".latch" );
            fprintfBz2( b, " %s", Ntl_ObjFanin0(pObj)->pName );
            fprintfBz2( b, " %s", Ntl_ObjFanout0(pObj)->pName );
            assert( pObj->LatchId.regType == 0 || pObj->LatchId.regClass == 0 );
            if ( pObj->LatchId.regType )
            {
                if ( pObj->LatchId.regType == 1 )
                    fprintfBz2( b, " fe" );
                else if ( pObj->LatchId.regType == 2 )
                    fprintfBz2( b, " re" );
                else if ( pObj->LatchId.regType == 3 )
                    fprintfBz2( b, " ah" );
                else if ( pObj->LatchId.regType == 4 )
                    fprintfBz2( b, " al" );
                else if ( pObj->LatchId.regType == 5 )
                    fprintfBz2( b, " as" );
                else
                    assert( 0 );
            }
            else if ( pObj->LatchId.regClass )
                fprintfBz2( b, " %d", pObj->LatchId.regClass );
            if ( pObj->pClock )
                fprintfBz2( b, " %s", pObj->pClock->pName );
            fprintfBz2( b, " %d", pObj->LatchId.regInit );
            fprintfBz2( b, "\n" );
        }
        else if ( Ntl_ObjIsBox(pObj) )
        {
            fprintfBz2( b, ".subckt %s", pObj->pImplem->pName );
            Ntl_ObjForEachFanin( pObj, pNet, k )
                fprintfBz2( b, " %s=%s", Ntl_ModelPiName(pObj->pImplem, k), pNet->pName );
            Ntl_ObjForEachFanout( pObj, pNet, k )
                fprintfBz2( b, " %s=%s", Ntl_ModelPoName(pObj->pImplem, k), pNet->pName );
            fprintfBz2( b, "\n" );
        }
    }
    fprintfBz2( b, ".end\n\n" );
}

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

  Synopsis    [Writes the logic network into the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
632
void Ntl_ManWriteBlif( Ntl_Man_t * p, char * pFileName )
Alan Mishchenko committed
633 634 635 636
{
    Ntl_Mod_t * pModel;
    int i, bzError;
    bz2file b;
637
    if ( p->pNal && !Ntl_FileIsType(pFileName, ".blif", ".blif.gz", ".blif.bz2") )
Alan Mishchenko committed
638 639 640 641
    {
        p->pNalW( p, pFileName );
        return;
    }
Alan Mishchenko committed
642 643 644
    // write the GZ file
    if (!strncmp(pFileName+strlen(pFileName)-3,".gz",3)) 
    {
645
        Ntl_ManWriteBlifGz( p, pFileName );
Alan Mishchenko committed
646 647 648
        return;
    }

Alan Mishchenko committed
649 650
    memset(&b,0,sizeof(b));
    b.nBytesMax = (1<<12);
Alan Mishchenko committed
651
    b.buf = ABC_ALLOC( char,b.nBytesMax );
Alan Mishchenko committed
652 653 654 655 656

    // start the output stream
    b.f = fopen( pFileName, "wb" ); 
    if ( b.f == NULL )
    {
657
        fprintf( stdout, "Ntl_ManWriteBlif(): Cannot open the output file \"%s\".\n", pFileName );
Alan Mishchenko committed
658
        ABC_FREE(b.buf);
Alan Mishchenko committed
659 660 661 662 663 664
        return;
    }
    if (!strncmp(pFileName+strlen(pFileName)-4,".bz2",4)) {
        b.b = BZ2_bzWriteOpen( &bzError, b.f, 9, 0, 0 );
        if ( bzError != BZ_OK ) {
            BZ2_bzWriteClose( &bzError, b.b, 0, NULL, NULL );
665
            fprintf( stdout, "Ntl_ManWriteBlif(): Cannot start compressed stream.\n" );
Alan Mishchenko committed
666
            fclose( b.f );
Alan Mishchenko committed
667
            ABC_FREE(b.buf);
Alan Mishchenko committed
668 669 670 671
            return;
        }
    }

672
    fprintfBz2( &b, "# Benchmark \"%s\" written by ABC-8 on %s\n", p->pName, Aig_TimeStamp() );
Alan Mishchenko committed
673 674
    // write the models
    Ntl_ManForEachModel( p, pModel, i )
675
        Ntl_ManWriteBlifModelBz2( &b, pModel, i==0 );
Alan Mishchenko committed
676 677 678 679
    // close the file
    if (b.b) {
        BZ2_bzWriteClose( &bzError, b.b, 0, NULL, NULL );
        if (bzError == BZ_IO_ERROR) {
680
            fprintf( stdout, "Ntl_ManWriteBlif(): I/O error closing compressed stream.\n" );
Alan Mishchenko committed
681
            fclose( b.f );
Alan Mishchenko committed
682
            ABC_FREE(b.buf);
Alan Mishchenko committed
683 684 685 686
            return;
        }
    }
    fclose( b.f );
Alan Mishchenko committed
687
    ABC_FREE(b.buf);
Alan Mishchenko committed
688 689 690
}


Alan Mishchenko committed
691 692 693 694 695
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


696 697
ABC_NAMESPACE_IMPL_END