mio.c 17.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
/**CFile****************************************************************

  FileName    [mio.c]

  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]

  Synopsis    [File reading/writing for technology mapping.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 18, 2003.]

  Revision    [$Id: mio.c,v 1.4 2004/08/05 18:34:51 satrajit Exp $]

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

Alan Mishchenko committed
19 20 21 22 23 24
#define _BSD_SOURCE

#ifndef WIN32
#include <unistd.h>
#endif

25
#include "base/main/main.h"
26
#include "mio.h"
27 28
#include "map/mapper/mapper.h"
#include "map/amap/amap.h"
Alan Mishchenko committed
29

30 31
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
32

Alan Mishchenko committed
33 34 35 36
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
37 38
static int Mio_CommandReadLiberty( Abc_Frame_t * pAbc, int argc, char **argv );

Alan Mishchenko committed
39 40 41
static int Mio_CommandReadLibrary( Abc_Frame_t * pAbc, int argc, char **argv );
static int Mio_CommandPrintLibrary( Abc_Frame_t * pAbc, int argc, char **argv );

Alan Mishchenko committed
42 43
static int Mio_CommandReadLibrary2( Abc_Frame_t * pAbc, int argc, char **argv );
static int Mio_CommandPrintLibrary2( Abc_Frame_t * pAbc, int argc, char **argv );
44
/*
Alan Mishchenko committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// internal version of GENLIB library
static char * pMcncGenlib[25] = {
    "GATE inv1    1   O=!a;             PIN * INV     1 999 0.9 0.0 0.9 0.0\n",
    "GATE inv2    2   O=!a;             PIN * INV     2 999 1.0 0.0 1.0 0.0\n",
    "GATE inv3    3   O=!a;             PIN * INV     3 999 1.1 0.0 1.1 0.0\n",
    "GATE inv4    4   O=!a;             PIN * INV     4 999 1.2 0.0 1.2 0.0\n",
    "GATE nand2   2   O=!(a*b);         PIN * INV     1 999 1.0 0.0 1.0 0.0\n",
    "GATE nand3   3   O=!(a*b*c);       PIN * INV     1 999 1.1 0.0 1.1 0.0\n",
    "GATE nand4   4   O=!(a*b*c*d);     PIN * INV     1 999 1.4 0.0 1.4 0.0\n",
    "GATE nor2    2   O=!(a+b);         PIN * INV     1 999 1.4 0.0 1.4 0.0\n",
    "GATE nor3    3   O=!(a+b+c);       PIN * INV     1 999 2.4 0.0 2.4 0.0\n",
    "GATE nor4    4   O=!(a+b+c+d);     PIN * INV     1 999 3.8 0.0 3.8 0.0\n",
    "GATE xora    5   O=a*!b+!a*b;      PIN * UNKNOWN 2 999 1.9 0.0 1.9 0.0\n",
    "GATE xorb    5   O=!(a*b+!a*!b);   PIN * UNKNOWN 2 999 1.9 0.0 1.9 0.0\n",
    "GATE xnora   5   O=a*b+!a*!b;      PIN * UNKNOWN 2 999 2.1 0.0 2.1 0.0\n",
    "GATE xnorb   5   O=!(!a*b+a*!b);   PIN * UNKNOWN 2 999 2.1 0.0 2.1 0.0\n",
    "GATE aoi21   3   O=!(a*b+c);       PIN * INV     1 999 1.6 0.0 1.6 0.0\n",
    "GATE aoi22   4   O=!(a*b+c*d);     PIN * INV     1 999 2.0 0.0 2.0 0.0\n",
    "GATE oai21   3   O=!((a+b)*c);     PIN * INV     1 999 1.6 0.0 1.6 0.0\n",
    "GATE oai22   4   O=!((a+b)*(c+d)); PIN * INV     1 999 2.0 0.0 2.0 0.0\n",
    "GATE buf     1   O=a;              PIN * NONINV  1 999 1.0 0.0 1.0 0.0\n",
    "GATE zero    0   O=CONST0;\n",
    "GATE one     0   O=CONST1;\n"
};
69
*/
Alan Mishchenko committed
70
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
71
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_Init( Abc_Frame_t * pAbc )
{
87
/*
Alan Mishchenko committed
88
    char * pFileTemp = "mcnc_temp.genlib";
Alan Mishchenko committed
89
    void * pLibGen;
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99
    FILE * pFile;
    int i;

    // write genlib into file
    pFile = fopen( pFileTemp, "w" );
    for ( i = 0; pMcncGenlib[i]; i++ )
        fputs( pMcncGenlib[i], pFile );
    fclose( pFile );
    // read genlib from file
    pLibGen = Mio_LibraryRead( pAbc, pFileTemp, NULL, 0 );
Alan Mishchenko committed
100
    Abc_FrameSetLibGen( pLibGen );
Alan Mishchenko committed
101 102 103
    pLibGen = Amap_LibReadAndPrepare( pFileTemp, 0, 0 );
    Abc_FrameSetLibGen2( pLibGen );

Alan Mishchenko committed
104 105 106 107 108
#ifdef WIN32
        _unlink( pFileTemp );
#else
        unlink( pFileTemp );
#endif
109
*/
Alan Mishchenko committed
110

Alan Mishchenko committed
111 112
    Cmd_CommandAdd( pAbc, "SC mapping", "read_liberty",   Mio_CommandReadLiberty,  0 ); 

Alan Mishchenko committed
113 114
    Cmd_CommandAdd( pAbc, "SC mapping", "read_library",   Mio_CommandReadLibrary,  0 ); 
    Cmd_CommandAdd( pAbc, "SC mapping", "print_library",  Mio_CommandPrintLibrary, 0 ); 
Alan Mishchenko committed
115 116 117

    Cmd_CommandAdd( pAbc, "SC mapping", "read_library2",   Mio_CommandReadLibrary2,  0 ); 
    Cmd_CommandAdd( pAbc, "SC mapping", "print_library2",  Mio_CommandPrintLibrary2, 0 ); 
Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
131
void Mio_End( Abc_Frame_t * pAbc )
Alan Mishchenko committed
132 133
{
//    Mio_LibraryDelete( s_pLib );
134 135
    Mio_LibraryDelete( (Mio_Library_t *)Abc_FrameReadLibGen() );
    Amap_LibFree( (Amap_Lib_t *)Abc_FrameReadLibGen2() );
Alan Mishchenko committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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
int Mio_CommandReadLiberty( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
    Abc_Ntk_t * pNet;
    char * FileName;
    int fVerbose;
    int c;

    pNet = Abc_FrameReadNtk(pAbc);
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    // set the defaults
    fVerbose = 1;
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "vh")) != EOF ) 
    {
        switch (c) 
        {
            case 'v':
                fVerbose ^= 1;
                break;
            case 'h':
                goto usage;
                break;
            default:
                goto usage;
        }
    }


    if ( argc != globalUtilOptind + 1 )
    {
        goto usage;
    }

    // get the input file name
    FileName = argv[globalUtilOptind];
    if ( (pFile = Io_FileOpen( FileName, "open_path", "r", 0 )) == NULL )
    {
        fprintf( pErr, "Cannot open input file \"%s\". ", FileName );
        if ( (FileName = Extra_FileGetSimilarName( FileName, ".genlib", ".lib", ".gen", ".g", NULL )) )
            fprintf( pErr, "Did you mean \"%s\"?", FileName );
        fprintf( pErr, "\n" );
        return 1;
    }
    fclose( pFile );

    if ( !Amap_LibertyParse( FileName, "temp.genlib", fVerbose ) )
        return 0;
    Cmd_CommandExecute( pAbc, "read_library temp.genlib" );
    return 0;

usage:
    fprintf( pErr, "usage: read_liberty [-vh]\n");
    fprintf( pErr, "\t         read standard cell library in Liberty format\n" );  
    fprintf( pErr, "\t         (if the library contains more than one gate\n" );  
    fprintf( pErr, "\t         with the same Boolean function, only the gate\n" );  
    fprintf( pErr, "\t         with the smallest area will be used)\n" );  
    fprintf( pErr, "\t-v     : toggle verbose printout [default = %s]\n", fVerbose? "yes": "no" );
    fprintf( pErr, "\t-h     : enable verbose output\n");
    return 1;       /* error exit */
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
226 227 228 229 230 231 232 233
int Mio_CommandReadLibrary( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
    Mio_Library_t * pLib;
    Abc_Ntk_t * pNet;
    char * FileName;
    int fVerbose;
234
    double WireDelay;
Alan Mishchenko committed
235 236
    int c;

Alan Mishchenko committed
237
    pNet = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
238 239 240 241
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    // set the defaults
242
    WireDelay   = 0.0;
Alan Mishchenko committed
243
    fVerbose = 1;
Alan Mishchenko committed
244
    Extra_UtilGetoptReset();
245
    while ( (c = Extra_UtilGetopt(argc, argv, "Wvh")) != EOF ) 
Alan Mishchenko committed
246 247 248
    {
        switch (c) 
        {
249 250 251 252 253 254 255 256 257 258 259
            case 'W':
                if ( globalUtilOptind >= argc )
                {
                    Abc_Print( -1, "Command line switch \"-W\" should be followed by a floating point number.\n" );
                    goto usage;
                }
                WireDelay = (float)atof(argv[globalUtilOptind]);
                globalUtilOptind++;
                if ( WireDelay <= 0.0 ) 
                    goto usage;
                break;
Alan Mishchenko committed
260 261 262 263 264 265 266 267 268 269 270 271
            case 'v':
                fVerbose ^= 1;
                break;
            case 'h':
                goto usage;
                break;
            default:
                goto usage;
        }
    }


Alan Mishchenko committed
272
    if ( argc != globalUtilOptind + 1 )
Alan Mishchenko committed
273 274 275 276 277
    {
        goto usage;
    }

    // get the input file name
Alan Mishchenko committed
278 279
    FileName = argv[globalUtilOptind];
    if ( (pFile = Io_FileOpen( FileName, "open_path", "r", 0 )) == NULL )
Alan Mishchenko committed
280 281 282 283 284 285 286 287 288 289
    {
        fprintf( pErr, "Cannot open input file \"%s\". ", FileName );
        if ( (FileName = Extra_FileGetSimilarName( FileName, ".genlib", ".lib", ".gen", ".g", NULL )) )
            fprintf( pErr, "Did you mean \"%s\"?", FileName );
        fprintf( pErr, "\n" );
        return 1;
    }
    fclose( pFile );

    // set the new network
290
    pLib = Mio_LibraryRead( FileName, 0, fVerbose );  
Alan Mishchenko committed
291 292 293 294 295
    if ( pLib == NULL )
    {
        fprintf( pErr, "Reading GENLIB library has failed.\n" );
        return 1;
    }
296 297 298 299
    // add the fixed number (wire delay) to all delays in the library
    if ( WireDelay != 0.0 )
        Mio_LibraryShift( pLib, WireDelay );

Alan Mishchenko committed
300
    // free the current superlib because it depends on the old Mio library
Alan Mishchenko committed
301
    if ( Abc_FrameReadLibSuper() )
Alan Mishchenko committed
302
    {
303
        Map_SuperLibFree( (Map_SuperLib_t *)Abc_FrameReadLibSuper() );
Alan Mishchenko committed
304
        Abc_FrameSetLibSuper( NULL );
Alan Mishchenko committed
305 306 307
    }

    // replace the current library
308
    Mio_LibraryDelete( (Mio_Library_t *)Abc_FrameReadLibGen() );
Alan Mishchenko committed
309
    Abc_FrameSetLibGen( pLib );
Alan Mishchenko committed
310 311

    // set the new network
312
    pLib = (Mio_Library_t *)Amap_LibReadAndPrepare( FileName, 1, 0 );  
Alan Mishchenko committed
313 314 315 316 317 318
    if ( pLib == NULL )
    {
        fprintf( pErr, "Reading GENLIB library has failed.\n" );
        return 1;
    }
    // replace the current library
319
    Amap_LibFree( (Amap_Lib_t *)Abc_FrameReadLibGen2() );
Alan Mishchenko committed
320
    Abc_FrameSetLibGen2( pLib );
Alan Mishchenko committed
321 322 323
    return 0;

usage:
324 325 326 327 328 329 330 331
    fprintf( pErr, "usage: read_library [-W float] [-vh]\n");
    fprintf( pErr, "\t           read the library from a genlib file\n" );  
    fprintf( pErr, "\t           (if the library contains more than one gate\n" );  
    fprintf( pErr, "\t           with the same Boolean function, only the gate\n" );  
    fprintf( pErr, "\t           with the smallest area will be used)\n" );  
    Abc_Print( -2, "\t-W float : wire delay (added to pin-to-pin gate delays) [default = %g]\n", WireDelay );  
    fprintf( pErr, "\t-v       : toggle verbose printout [default = %s]\n", fVerbose? "yes": "no" );
    fprintf( pErr, "\t-h       : enable verbose output\n");
Alan Mishchenko committed
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
    return 1;       /* error exit */
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_CommandReadLibrary2( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
    Mio_Library_t * pLib;
    Abc_Ntk_t * pNet;
    char * FileName;
    int fVerbose;
    int fVeryVerbose;
    int c;

    pNet = Abc_FrameReadNtk(pAbc);
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    // set the defaults
    fVerbose     = 1;
    fVeryVerbose = 0;
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "vwh")) != EOF ) 
    {
        switch (c) 
        {
            case 'v':
                fVerbose ^= 1;
                break;
            case 'w':
                fVeryVerbose ^= 1;
                break;
            case 'h':
                goto usage;
                break;
            default:
                goto usage;
        }
    }


    if ( argc != globalUtilOptind + 1 )
    {
        goto usage;
    }

    // get the input file name
    FileName = argv[globalUtilOptind];
    if ( (pFile = Io_FileOpen( FileName, "open_path", "r", 0 )) == NULL )
    {
        fprintf( pErr, "Cannot open input file \"%s\". ", FileName );
        if ( (FileName = Extra_FileGetSimilarName( FileName, ".genlib", ".lib", ".gen", ".g", NULL )) )
            fprintf( pErr, "Did you mean \"%s\"?", FileName );
        fprintf( pErr, "\n" );
        return 1;
    }
    fclose( pFile );

    // set the new network
402
    pLib = (Mio_Library_t *)Amap_LibReadAndPrepare( FileName, fVerbose, fVeryVerbose );  
Alan Mishchenko committed
403 404 405 406 407 408 409
    if ( pLib == NULL )
    {
        fprintf( pErr, "Reading GENLIB library has failed.\n" );
        return 1;
    }

    // replace the current library
410
    Amap_LibFree( (Amap_Lib_t *)Abc_FrameReadLibGen2() );
Alan Mishchenko committed
411 412 413 414 415 416 417 418 419 420 421
    Abc_FrameSetLibGen2( pLib );
    return 0;

usage:
    fprintf( pErr, "usage: read_library2 [-vh]\n");
    fprintf( pErr, "\t         read the library from a genlib file\n" );  
    fprintf( pErr, "\t         (if the library contains more than one gate\n" );  
    fprintf( pErr, "\t         with the same Boolean function, only the gate\n" );  
    fprintf( pErr, "\t         with the smallest area will be used)\n" );  
    fprintf( pErr, "\t-v     : toggle verbose printout [default = %s]\n", fVerbose? "yes": "no" );
    fprintf( pErr, "\t-w     : toggle detailed printout [default = %s]\n", fVeryVerbose? "yes": "no" );
Alan Mishchenko committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    fprintf( pErr, "\t-h     : enable verbose output\n");
    return 1;       /* error exit */
}


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

  Synopsis    [Command procedure to read LUT libraries.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_CommandPrintLibrary( Abc_Frame_t * pAbc, int argc, char **argv )
{
440
    FILE * pOut, * pErr, * pFile;
Alan Mishchenko committed
441
    Abc_Ntk_t * pNet;
442
    char * FileName;
Alan Mishchenko committed
443 444 445
    int fVerbose;
    int c;

Alan Mishchenko committed
446
    pNet = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
447 448 449 450 451
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    // set the defaults
    fVerbose = 1;
Alan Mishchenko committed
452 453
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "vh")) != EOF ) 
Alan Mishchenko committed
454 455 456 457 458 459 460 461 462 463 464 465 466
    {
        switch (c) 
        {
            case 'v':
                fVerbose ^= 1;
                break;
            case 'h':
                goto usage;
                break;
            default:
                goto usage;
        }
    }
467 468 469 470 471
    if ( Abc_FrameReadLibGen() == NULL )
    {
        printf( "Library is not available.\n" );
        return 1;
    }
472 473
    FileName = argv[globalUtilOptind];
    if ( argc == globalUtilOptind + 1 )
Alan Mishchenko committed
474
    {
475 476 477 478 479 480 481 482 483
        pFile = fopen( FileName, "w" );
        if ( pFile == NULL )
        {
            printf( "Error! Cannot open file \"%s\" for writing the library.\n", FileName );
            return 1;
        }
        Mio_WriteLibrary( pFile, (Mio_Library_t *)Abc_FrameReadLibGen(), 0 );
        fclose( pFile );
        printf( "The current GENLIB library is written into file \"%s\".\n", FileName );
Alan Mishchenko committed
484
    }
485 486 487 488
    else if ( argc == globalUtilOptind )
        Mio_WriteLibrary( stdout, (Mio_Library_t *)Abc_FrameReadLibGen(), 0 );
    else
        goto usage;
Alan Mishchenko committed
489 490 491
    return 0;

usage:
492
    fprintf( pErr, "\nusage: print_library [-vh] <file>\n");
Alan Mishchenko committed
493
    fprintf( pErr, "\t          print the current genlib library\n" );  
494
    fprintf( pErr, "\t-v      : toggles enabling of verbose output [default = %s]\n", fVerbose? "yes" : "no" );
Alan Mishchenko committed
495
    fprintf( pErr, "\t-h      : print the command usage\n");
496
    fprintf( pErr, "\t<file>  : optional file name to write the library\n");
Alan Mishchenko committed
497 498
    return 1;       /* error exit */
}
Alan Mishchenko committed
499 500 501 502 503 504 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


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

  Synopsis    [Command procedure to read LUT libraries.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_CommandPrintLibrary2( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pOut, * pErr;
    Abc_Ntk_t * pNet;
    int fPrintAll;
    int fVerbose;
    int c;

    pNet = Abc_FrameReadNtk(pAbc);
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    // set the defaults
    fPrintAll = 0;
    fVerbose = 1;
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "avh")) != EOF ) 
    {
        switch (c) 
        {
            case 'a':
                fPrintAll ^= 1;
                break;
            case 'v':
                fVerbose ^= 1;
                break;
            case 'h':
                goto usage;
                break;
            default:
                goto usage;
        }
    }


    if ( argc != globalUtilOptind )
    {
        goto usage;
    }

    // set the new network
553
    Amap_LibPrintSelectedGates( (Amap_Lib_t *)Abc_FrameReadLibGen2(), fPrintAll );
Alan Mishchenko committed
554 555 556 557 558 559 560 561 562 563 564
    return 0;

usage:
    fprintf( pErr, "\nusage: print_library2 [-avh]\n");
    fprintf( pErr, "\t          print gates used for area-oriented tech-mapping\n" );  
    fprintf( pErr, "\t-a      : toggles printing all gates [default = %s]\n", (fPrintAll? "yes" : "no") );
    fprintf( pErr, "\t-v      : toggles enabling of verbose output [default = %s]\n", (fVerbose? "yes" : "no") );
    fprintf( pErr, "\t-h      : print the command usage\n");
    return 1;       /* error exit */
}

Alan Mishchenko committed
565 566 567 568 569
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


570 571
ABC_NAMESPACE_IMPL_END