ioReadBlif.c 32.6 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 21
/**CFile****************************************************************

  FileName    [ioReadBlif.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read BLIF files.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "io.h"
Alan Mishchenko committed
22 23
#include "main.h"
#include "mio.h"
Alan Mishchenko committed
24 25 26 27 28 29 30 31 32

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

typedef struct Io_ReadBlif_t_        Io_ReadBlif_t;   // all reading info
struct Io_ReadBlif_t_
{
    // general info about file
Alan Mishchenko committed
33 34
    char *               pFileName;    // the name of the file
    Extra_FileReader_t * pReader;      // the input file reader
Alan Mishchenko committed
35
    // current processing info
Alan Mishchenko committed
36
    Abc_Ntk_t *          pNtkCur;      // the primary network
Alan Mishchenko committed
37
    int                  LineCur;      // the line currently parsed
Alan Mishchenko committed
38
    // temporary storage for tokens
Alan Mishchenko committed
39
    Vec_Ptr_t *          vTokens;      // the current tokens
Alan Mishchenko committed
40 41
    Vec_Ptr_t *          vNewTokens;   // the temporary storage for the tokens
    Vec_Str_t *          vCubes;       // the temporary storage for the tokens
Alan Mishchenko committed
42
    // the error message
Alan Mishchenko committed
43 44
    FILE *               Output;       // the output stream
    char                 sError[1000]; // the error string generated during parsing
Alan Mishchenko committed
45 46 47 48 49 50
};

static Io_ReadBlif_t * Io_ReadBlifFile( char * pFileName );
static void Io_ReadBlifFree( Io_ReadBlif_t * p );
static void Io_ReadBlifPrintErrorMessage( Io_ReadBlif_t * p );
static Vec_Ptr_t * Io_ReadBlifGetTokens( Io_ReadBlif_t * p );
Alan Mishchenko committed
51
static char * Io_ReadBlifCleanName( char * pName );
Alan Mishchenko committed
52

Alan Mishchenko committed
53 54
static Abc_Ntk_t * Io_ReadBlifNetwork( Io_ReadBlif_t * p );
static Abc_Ntk_t * Io_ReadBlifNetworkOne( Io_ReadBlif_t * p );
Alan Mishchenko committed
55 56 57 58
static int Io_ReadBlifNetworkInputs( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
static int Io_ReadBlifNetworkOutputs( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
static int Io_ReadBlifNetworkLatch( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
static int Io_ReadBlifNetworkNames( Io_ReadBlif_t * p, Vec_Ptr_t ** pvTokens );
Alan Mishchenko committed
59
static int Io_ReadBlifNetworkGate( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
Alan Mishchenko committed
60
static int Io_ReadBlifNetworkSubcircuit( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
Alan Mishchenko committed
61 62
static int Io_ReadBlifNetworkInputArrival( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
static int Io_ReadBlifNetworkDefaultInputArrival( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens );
Alan Mishchenko committed
63
static int Io_ReadBlifNetworkConnectBoxes( Io_ReadBlif_t * p, Abc_Ntk_t * pNtkMaster );
Alan Mishchenko committed
64 65

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
66
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
67 68 69 70
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
71
  Synopsis    [Reads the (hierarchical) network from the BLIF file.]
Alan Mishchenko committed
72

Alan Mishchenko committed
73
  Description []
Alan Mishchenko committed
74 75 76 77 78 79 80 81 82
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Io_ReadBlif( char * pFileName, int fCheck )
{
    Io_ReadBlif_t * p;
Alan Mishchenko committed
83
    Abc_Ntk_t * pNtk;
Alan Mishchenko committed
84 85 86 87 88 89

    // start the file
    p = Io_ReadBlifFile( pFileName );
    if ( p == NULL )
        return NULL;

Alan Mishchenko committed
90
    // read the hierarchical network
Alan Mishchenko committed
91 92 93 94 95 96
    pNtk = Io_ReadBlifNetwork( p );
    if ( pNtk == NULL )
    {
        Io_ReadBlifFree( p );
        return NULL;
    }
Alan Mishchenko committed
97
    pNtk->pSpec = Extra_UtilStrsav( pFileName );
Alan Mishchenko committed
98
    Abc_NtkTimeInitialize( pNtk );
Alan Mishchenko committed
99 100 101
    Io_ReadBlifFree( p );

    // make sure that everything is okay with the network structure
Alan Mishchenko committed
102
    if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
Alan Mishchenko committed
103 104 105 106 107 108 109 110 111 112
    {
        printf( "Io_ReadBlif: The network check has failed.\n" );
        Abc_NtkDelete( pNtk );
        return NULL;
    }
    return pNtk;
}

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

Alan Mishchenko committed
113
  Synopsis    [Iteratively reads several networks in the hierarchical design.]
Alan Mishchenko committed
114 115 116 117 118 119 120 121

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
122
Abc_Ntk_t * Io_ReadBlifNetwork( Io_ReadBlif_t * p )
Alan Mishchenko committed
123
{
Alan Mishchenko committed
124
    Abc_Ntk_t * pNtk, * pNtkMaster;
Alan Mishchenko committed
125

Alan Mishchenko committed
126 127 128 129 130 131 132
    // read the name of the master network
    p->vTokens = Io_ReadBlifGetTokens(p);
    if ( p->vTokens == NULL || strcmp( p->vTokens->pArray[0], ".model" ) )
    {
        p->LineCur = 0;
        sprintf( p->sError, "Wrong input file format." );
        Io_ReadBlifPrintErrorMessage( p );
Alan Mishchenko committed
133
        return NULL;
Alan Mishchenko committed
134
    }
Alan Mishchenko committed
135

Alan Mishchenko committed
136 137 138
    // read networks (with EXDC) 
    pNtkMaster = NULL;
    while ( p->vTokens )
Alan Mishchenko committed
139
    {
Alan Mishchenko committed
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
        // read the network and its EXDC if present
        pNtk = Io_ReadBlifNetworkOne( p );
        if ( pNtk == NULL )
            break;
        if ( p->vTokens && strcmp(p->vTokens->pArray[0], ".exdc") == 0 )
        {
            pNtk->pExdc = Io_ReadBlifNetworkOne( p );
            Abc_NtkFinalizeRead( pNtk->pExdc );
            if ( pNtk->pExdc == NULL )
                break;
        }
        // add this network as part of the hierarchy
        if ( pNtkMaster == NULL ) // no master network so far
        {
            pNtkMaster = pNtk;
            continue;
        }
        // make sure hierarchy does not have the network with this name
        if ( pNtkMaster->tName2Model && stmm_is_member( pNtkMaster->tName2Model, pNtk->pName ) )
        {
            p->LineCur = 0;
            sprintf( p->sError, "Model %s is multiply defined in the file.", pNtk->pName );
            Io_ReadBlifPrintErrorMessage( p );
            Abc_NtkDelete( pNtk );
            Abc_NtkDelete( pNtkMaster );
            pNtkMaster = NULL;
            return NULL;
        }
        // add the network to the hierarchy
        if ( pNtkMaster->tName2Model == NULL )
            pNtkMaster->tName2Model = stmm_init_table(strcmp, stmm_strhash);
        stmm_insert( pNtkMaster->tName2Model, pNtk->pName, (char *)pNtk );
Alan Mishchenko committed
172 173
    }

Alan Mishchenko committed
174 175
    // if there is a hierarchy, connect the boxes
    if ( pNtkMaster && pNtkMaster->tName2Model )
Alan Mishchenko committed
176
    {
Alan Mishchenko committed
177
        if ( Io_ReadBlifNetworkConnectBoxes( p, pNtkMaster ) )
Alan Mishchenko committed
178
        {
Alan Mishchenko committed
179 180
            Abc_NtkDelete( pNtkMaster );
            return NULL;
Alan Mishchenko committed
181 182
        }
    }
Alan Mishchenko committed
183 184 185 186
    else
        Abc_NtkFinalizeRead( pNtkMaster );
    // return the master network
    return pNtkMaster;
Alan Mishchenko committed
187 188 189 190
}

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

Alan Mishchenko committed
191
  Synopsis    [Reads one (main or exdc) network from the BLIF file.]
Alan Mishchenko committed
192 193 194 195 196 197 198 199

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
200
Abc_Ntk_t * Io_ReadBlifNetworkOne( Io_ReadBlif_t * p )
Alan Mishchenko committed
201 202
{
    ProgressBar * pProgress;
Alan Mishchenko committed
203 204
    Abc_Ntk_t * pNtk;
    char * pDirective;
Alan Mishchenko committed
205 206
    int iLine, fTokensReady, fStatus;

Alan Mishchenko committed
207 208 209 210 211
    // make sure the tokens are present
    assert( p->vTokens != NULL );

    // create the new network
    p->pNtkCur = pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_SOP );
Alan Mishchenko committed
212
    // read the model name
Alan Mishchenko committed
213 214
    if ( strcmp( p->vTokens->pArray[0], ".model" ) == 0 )
        pNtk->pName = Extra_UtilStrsav( p->vTokens->pArray[1] );
Alan Mishchenko committed
215 216 217 218

    // read the inputs/outputs
    pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p->pReader) );
    fTokensReady = fStatus = 0;
Alan Mishchenko committed
219
    for ( iLine = 0; fTokensReady || (p->vTokens = Io_ReadBlifGetTokens(p)); iLine++ )
Alan Mishchenko committed
220 221 222 223 224 225
    {
        if ( iLine % 1000 == 0 )
        Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p->pReader), NULL );

        // consider different line types
        fTokensReady = 0;
Alan Mishchenko committed
226
        pDirective = p->vTokens->pArray[0];
Alan Mishchenko committed
227
        if ( !strcmp( pDirective, ".names" ) )
Alan Mishchenko committed
228
            { fStatus = Io_ReadBlifNetworkNames( p, &p->vTokens ); fTokensReady = 1; }
Alan Mishchenko committed
229
        else if ( !strcmp( pDirective, ".gate" ) )
Alan Mishchenko committed
230
            fStatus = Io_ReadBlifNetworkGate( p, p->vTokens );
Alan Mishchenko committed
231
        else if ( !strcmp( pDirective, ".latch" ) )
Alan Mishchenko committed
232
            fStatus = Io_ReadBlifNetworkLatch( p, p->vTokens );
Alan Mishchenko committed
233
        else if ( !strcmp( pDirective, ".inputs" ) )
Alan Mishchenko committed
234
            fStatus = Io_ReadBlifNetworkInputs( p, p->vTokens );
Alan Mishchenko committed
235
        else if ( !strcmp( pDirective, ".outputs" ) )
Alan Mishchenko committed
236
            fStatus = Io_ReadBlifNetworkOutputs( p, p->vTokens );
Alan Mishchenko committed
237
        else if ( !strcmp( pDirective, ".input_arrival" ) )
Alan Mishchenko committed
238
            fStatus = Io_ReadBlifNetworkInputArrival( p, p->vTokens );
Alan Mishchenko committed
239
        else if ( !strcmp( pDirective, ".default_input_arrival" ) )
Alan Mishchenko committed
240 241 242
            fStatus = Io_ReadBlifNetworkDefaultInputArrival( p, p->vTokens );
        else if ( !strcmp( pDirective, ".subckt" ) )
            fStatus = Io_ReadBlifNetworkSubcircuit( p, p->vTokens );
Alan Mishchenko committed
243
        else if ( !strcmp( pDirective, ".exdc" ) )
Alan Mishchenko committed
244
            break;
Alan Mishchenko committed
245
        else if ( !strcmp( pDirective, ".end" ) )
Alan Mishchenko committed
246 247
        {
            p->vTokens = Io_ReadBlifGetTokens(p);
Alan Mishchenko committed
248
            break;
Alan Mishchenko committed
249 250 251 252 253 254
        }
        else if ( !strcmp( pDirective, ".blackbox" ) )
        {
            pNtk->ntkType = ABC_NTK_BLACKBOX;
            pNtk->ntkFunc = ABC_FUNC_BLACKBOX;
        }
Alan Mishchenko committed
255 256
        else
            printf( "%s (line %d): Skipping directive \"%s\".\n", p->pFileName, 
Alan Mishchenko committed
257
                Extra_FileReaderGetLineNumber(p->pReader, 0), pDirective );
Alan Mishchenko committed
258
        if ( p->vTokens == NULL ) // some files do not have ".end" in the end
Alan Mishchenko committed
259 260 261 262 263
            break;
        if ( fStatus == 1 )
            return NULL;
    }
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
264
    return pNtk;
Alan Mishchenko committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkInputs( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    int i;
    for ( i = 1; i < vTokens->nSize; i++ )
Alan Mishchenko committed
282
        Io_ReadCreatePi( p->pNtkCur, vTokens->pArray[i] );
Alan Mishchenko committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkOutputs( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    int i;
    for ( i = 1; i < vTokens->nSize; i++ )
Alan Mishchenko committed
301
        Io_ReadCreatePo( p->pNtkCur, vTokens->pArray[i] );
Alan Mishchenko committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkLatch( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
Alan Mishchenko committed
318
    Abc_Ntk_t * pNtk = p->pNtkCur;
Alan Mishchenko committed
319
    Abc_Obj_t * pLatch;
Alan Mishchenko committed
320 321 322 323 324 325 326 327
    int ResetValue;
    if ( vTokens->nSize < 3 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "The .latch line does not have enough tokens." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
Alan Mishchenko committed
328 329
    // create the latch
    pLatch = Io_ReadCreateLatch( pNtk, vTokens->pArray[1], vTokens->pArray[2] );
Alan Mishchenko committed
330 331
    // get the latch reset value
    if ( vTokens->nSize == 3 )
Alan Mishchenko committed
332
        Abc_LatchSetInitDc( pLatch );
Alan Mishchenko committed
333 334 335 336 337 338 339 340 341 342
    else
    {
        ResetValue = atoi(vTokens->pArray[3]);
        if ( ResetValue != 0 && ResetValue != 1 && ResetValue != 2 )
        {
            p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
            sprintf( p->sError, "The .latch line has an unknown reset value (%s).", vTokens->pArray[3] );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
Alan Mishchenko committed
343 344 345 346 347 348
        if ( ResetValue == 0 )
            Abc_LatchSetInit0( pLatch );
        else if ( ResetValue == 1 )
            Abc_LatchSetInit1( pLatch );
        else if ( ResetValue == 2 )
            Abc_LatchSetInitDc( pLatch );
Alan Mishchenko committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    }
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkNames( Io_ReadBlif_t * p, Vec_Ptr_t ** pvTokens )
{
    Vec_Ptr_t * vTokens = *pvTokens;
Alan Mishchenko committed
367
    Abc_Ntk_t * pNtk = p->pNtkCur;
Alan Mishchenko committed
368 369 370
    Abc_Obj_t * pNode;
    char * pToken, Char, ** ppNames;
    int nFanins, nNames;
Alan Mishchenko committed
371 372 373 374 375 376 377 378 379

    // create a new node and add it to the network
    if ( vTokens->nSize < 2 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "The .names line has less than two tokens." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
Alan Mishchenko committed
380 381 382 383 384

    // create the node
    ppNames = (char **)vTokens->pArray + 1;
    nNames  = vTokens->nSize - 2;
    pNode   = Io_ReadCreateNode( pNtk, ppNames[nNames], ppNames, nNames );
Alan Mishchenko committed
385 386 387 388 389

    // derive the functionality of the node
    p->vCubes->nSize = 0;
    nFanins = vTokens->nSize - 2;
    if ( nFanins == 0 )
Alan Mishchenko committed
390
    {
Alan Mishchenko committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        while ( vTokens = Io_ReadBlifGetTokens(p) )
        {
            pToken = vTokens->pArray[0];
            if ( pToken[0] == '.' )
                break;
            // read the cube
            if ( vTokens->nSize != 1 )
            {
                p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
                sprintf( p->sError, "The number of tokens in the constant cube is wrong." );
                Io_ReadBlifPrintErrorMessage( p );
                return 1;
            }
            // create the cube
            Char = ((char *)vTokens->pArray[0])[0];
            Vec_StrPush( p->vCubes, ' ' );
            Vec_StrPush( p->vCubes, Char );
            Vec_StrPush( p->vCubes, '\n' );
        }
Alan Mishchenko committed
410
    }
Alan Mishchenko committed
411
    else
Alan Mishchenko committed
412
    {
Alan Mishchenko committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426
        while ( vTokens = Io_ReadBlifGetTokens(p) )
        {
            pToken = vTokens->pArray[0];
            if ( pToken[0] == '.' )
                break;
            // read the cube
            if ( vTokens->nSize != 2 )
            {
                p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
                sprintf( p->sError, "The number of tokens in the cube is wrong." );
                Io_ReadBlifPrintErrorMessage( p );
                return 1;
            }
            // create the cube
Alan Mishchenko committed
427
            Vec_StrAppend( p->vCubes, vTokens->pArray[0] );
Alan Mishchenko committed
428 429
            // check the char 
            Char = ((char *)vTokens->pArray[1])[0];
Alan Mishchenko committed
430
            if ( Char != '0' && Char != '1' && Char != 'x' && Char != 'n' )
Alan Mishchenko committed
431 432 433 434 435 436 437 438 439 440
            {
                p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
                sprintf( p->sError, "The output character in the constant cube is wrong." );
                Io_ReadBlifPrintErrorMessage( p );
                return 1;
            }
            Vec_StrPush( p->vCubes, ' ' );
            Vec_StrPush( p->vCubes, Char );
            Vec_StrPush( p->vCubes, '\n' );
        }
Alan Mishchenko committed
441
    }
Alan Mishchenko committed
442 443 444 445 446 447 448 449 450
    // if there is nothing there
    if ( p->vCubes->nSize == 0 )
    {
        // create an empty cube
        Vec_StrPush( p->vCubes, ' ' );
        Vec_StrPush( p->vCubes, '0' );
        Vec_StrPush( p->vCubes, '\n' );
    }
    Vec_StrPush( p->vCubes, 0 );
Alan Mishchenko committed
451

Alan Mishchenko committed
452 453
    // set the pointer to the functionality of the node
    Abc_ObjSetData( pNode, Abc_SopRegister(pNtk->pManFunc, p->vCubes->pArray) );
Alan Mishchenko committed
454

Alan Mishchenko committed
455 456 457 458 459
    // return the last array of tokens
    *pvTokens = vTokens;
    return 0;
}

Alan Mishchenko committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkGate( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    Mio_Library_t * pGenlib; 
    Mio_Gate_t * pGate;
    Abc_Obj_t * pNode;
    char ** ppNames;
    int i, nNames;

    // check that the library is available
Alan Mishchenko committed
480
    pGenlib = Abc_FrameReadLibGen();
Alan Mishchenko committed
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 508
    if ( pGenlib == NULL )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "The current library is not available." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }

    // create a new node and add it to the network
    if ( vTokens->nSize < 2 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "The .gate line has less than two tokens." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }

    // get the gate
    pGate = Mio_LibraryReadGateByName( pGenlib, vTokens->pArray[1] );
    if ( pGate == NULL )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Cannot find gate \"%s\" in the library.", vTokens->pArray[1] );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }

    // if this is the first line with gate, update the network type
Alan Mishchenko committed
509
    if ( Abc_NtkNodeNum(p->pNtkCur) == 0 )
Alan Mishchenko committed
510
    {
Alan Mishchenko committed
511 512 513 514
        assert( p->pNtkCur->ntkFunc == ABC_FUNC_SOP );
        p->pNtkCur->ntkFunc = ABC_FUNC_MAP;
        Extra_MmFlexStop( p->pNtkCur->pManFunc, 0 );
        p->pNtkCur->pManFunc = pGenlib;
Alan Mishchenko committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    }

    // remove the formal parameter names
    for ( i = 2; i < vTokens->nSize; i++ )
    {
        vTokens->pArray[i] = Io_ReadBlifCleanName( vTokens->pArray[i] );
        if ( vTokens->pArray[i] == NULL )
        {
            p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
            sprintf( p->sError, "Invalid gate input assignment." );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
    }

    // create the node
    ppNames = (char **)vTokens->pArray + 2;
    nNames  = vTokens->nSize - 3;
Alan Mishchenko committed
533
    pNode   = Io_ReadCreateNode( p->pNtkCur, ppNames[nNames], ppNames, nNames );
Alan Mishchenko committed
534 535 536 537 538 539

    // set the pointer to the functionality of the node
    Abc_ObjSetData( pNode, pGate );
    return 0;
}

Alan Mishchenko committed
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 577 578 579
/**Function*************************************************************

  Synopsis    [Creates a multi-input multi-output box in the hierarchical design.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkSubcircuit( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    Abc_Obj_t * pBox;
    Vec_Ptr_t * vNames;
    char * pName;
    int i;

    // create a new node and add it to the network
    if ( vTokens->nSize < 3 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "The .subcircuit line has less than three tokens." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }

    // store the names of formal/actual inputs/outputs of the box
    vNames = Vec_PtrAlloc( 10 );
    Vec_PtrForEachEntryStart( vTokens, pName, i, 1 )
        Vec_PtrPush( vNames, Abc_NtkRegisterName(p->pNtkCur, pName) );

    // create a new box and add it to the network
    pBox = Abc_NtkCreateBox( p->pNtkCur );
    // set the pointer to the node names
    Abc_ObjSetData( pBox, vNames );
    // remember the line of the file
    pBox->pCopy = (void *)Extra_FileReaderGetLineNumber(p->pReader, 0);
    return 0;
}
Alan Mishchenko committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Io_ReadBlifCleanName( char * pName )
{
    int i, Length;
    Length = strlen(pName);
    for ( i = 0; i < Length; i++ )
        if ( pName[i] == '=' )
            return pName + i + 1;
    return NULL;
}
Alan Mishchenko committed
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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkInputArrival( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    Abc_Obj_t * pNet;
    char * pFoo1, * pFoo2;
    double TimeRise, TimeFall;

    // make sure this is indeed the .inputs line
    assert( strncmp( vTokens->pArray[0], ".input_arrival", 14 ) == 0 );
    if ( vTokens->nSize != 4 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Wrong number of arguments on .input_arrival line." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
Alan Mishchenko committed
628
    pNet = Abc_NtkFindNet( p->pNtkCur, vTokens->pArray[1] );
Alan Mishchenko committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    if ( pNet == NULL )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Cannot find object corresponding to %s on .input_arrival line.", vTokens->pArray[1] );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
    TimeRise = strtod( vTokens->pArray[2], &pFoo1 );
    TimeFall = strtod( vTokens->pArray[3], &pFoo2 );
    if ( *pFoo1 != '\0' || *pFoo2 != '\0' )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Bad value (%s %s) for rise or fall time on .input_arrival line.", vTokens->pArray[2], vTokens->pArray[3] );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
    // set the arrival time
Alan Mishchenko committed
646
    Abc_NtkTimeSetArrival( p->pNtkCur, Abc_ObjFanin0(pNet)->Id, (float)TimeRise, (float)TimeFall );
Alan Mishchenko committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkDefaultInputArrival( Io_ReadBlif_t * p, Vec_Ptr_t * vTokens )
{
    char * pFoo1, * pFoo2;
    double TimeRise, TimeFall;

    // make sure this is indeed the .inputs line
    assert( strncmp( vTokens->pArray[0], ".default_input_arrival", 23 ) == 0 );
    if ( vTokens->nSize != 3 )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Wrong number of arguments on .default_input_arrival line." );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
    TimeRise = strtod( vTokens->pArray[1], &pFoo1 );
    TimeFall = strtod( vTokens->pArray[2], &pFoo2 );
    if ( *pFoo1 != '\0' || *pFoo2 != '\0' )
    {
        p->LineCur = Extra_FileReaderGetLineNumber(p->pReader, 0);
        sprintf( p->sError, "Bad value (%s %s) for rise or fall time on .default_input_arrival line.", vTokens->pArray[1], vTokens->pArray[2] );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }
    // set the arrival time
Alan Mishchenko committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    Abc_NtkTimeSetDefaultArrival( p->pNtkCur, (float)TimeRise, (float)TimeFall );
    return 0;
}

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

  Synopsis    [Prints the error message including the file name and line number.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_ReadBlifPrintErrorMessage( Io_ReadBlif_t * p )
{
    if ( p->LineCur == 0 ) // the line number is not given
        fprintf( p->Output, "%s: %s\n", p->pFileName, p->sError );
    else // print the error message with the line number
        fprintf( p->Output, "%s (line %d): %s\n", p->pFileName, p->LineCur, p->sError );
}

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

  Synopsis    [Gets the tokens taking into account the line breaks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Io_ReadBlifGetTokens( Io_ReadBlif_t * p )
{
    Vec_Ptr_t * vTokens;
    char * pLastToken;
    int i;

    // get rid of the old tokens
    if ( p->vNewTokens->nSize > 0 )
    {
        for ( i = 0; i < p->vNewTokens->nSize; i++ )
            free( p->vNewTokens->pArray[i] );
        p->vNewTokens->nSize = 0;
    }

    // get the new tokens
    vTokens = Extra_FileReaderGetTokens(p->pReader);
    if ( vTokens == NULL )
        return vTokens;

    // check if there is a transfer to another line
    pLastToken = vTokens->pArray[vTokens->nSize - 1];
    if ( pLastToken[ strlen(pLastToken)-1 ] != '\\' )
        return vTokens;

    // remove the slash
    pLastToken[ strlen(pLastToken)-1 ] = 0;
    if ( pLastToken[0] == 0 )
        vTokens->nSize--;
    // load them into the new array
    for ( i = 0; i < vTokens->nSize; i++ )
        Vec_PtrPush( p->vNewTokens, Extra_UtilStrsav(vTokens->pArray[i]) );

    // load as long as there is the line break
    while ( 1 )
    {
        // get the new tokens
        vTokens = Extra_FileReaderGetTokens(p->pReader);
        if ( vTokens->nSize == 0 )
            return p->vNewTokens;
        // check if there is a transfer to another line
        pLastToken = vTokens->pArray[vTokens->nSize - 1];
        if ( pLastToken[ strlen(pLastToken)-1 ] == '\\' )
        {
            // remove the slash
            pLastToken[ strlen(pLastToken)-1 ] = 0;
            if ( pLastToken[0] == 0 )
                vTokens->nSize--;
            // load them into the new array
            for ( i = 0; i < vTokens->nSize; i++ )
                Vec_PtrPush( p->vNewTokens, Extra_UtilStrsav(vTokens->pArray[i]) );
            continue;
        }
        // otherwise, load them and break
        for ( i = 0; i < vTokens->nSize; i++ )
            Vec_PtrPush( p->vNewTokens, Extra_UtilStrsav(vTokens->pArray[i]) );
        break;
    }
    return p->vNewTokens;
}

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

  Synopsis    [Starts the reading data structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Io_ReadBlif_t * Io_ReadBlifFile( char * pFileName )
{
    Extra_FileReader_t * pReader;
    Io_ReadBlif_t * p;

    // start the reader
    pReader = Extra_FileReaderAlloc( pFileName, "#", "\n\r", " \t" );

    if ( pReader == NULL )
        return NULL;

    // start the reading data structure
    p = ALLOC( Io_ReadBlif_t, 1 );
    memset( p, 0, sizeof(Io_ReadBlif_t) );
    p->pFileName  = pFileName;
    p->pReader    = pReader;
    p->Output     = stdout;
    p->vNewTokens = Vec_PtrAlloc( 100 );
    p->vCubes     = Vec_StrAlloc( 100 );
    return p;
}

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

  Synopsis    [Frees the data structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_ReadBlifFree( Io_ReadBlif_t * p )
{
    Extra_FileReaderFree( p->pReader );
    Vec_PtrFree( p->vNewTokens );
    Vec_StrFree( p->vCubes );
    free( p );
}


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

  Synopsis    [Connect one box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkConnectBoxesOneBox( Io_ReadBlif_t * p, Abc_Obj_t * pBox, stmm_table * tName2Model )
{
    Vec_Ptr_t * pNames;
    Abc_Ntk_t * pNtkModel;
    Abc_Obj_t * pObj, * pNet;
    char * pName, * pActual;
    int i, Length, Start;

    // get the model for this box
    pNames = pBox->pData;
    if ( !stmm_lookup( tName2Model, Vec_PtrEntry(pNames, 0), (char **)&pNtkModel ) )
    {
        p->LineCur = (int)pBox->pCopy;
        sprintf( p->sError, "Cannot find the model for subcircuit %s.", Vec_PtrEntry(pNames, 0) );
        Io_ReadBlifPrintErrorMessage( p );
        return 1;
    }

    // create the fanins of the box
    Abc_NtkForEachPi( pNtkModel, pObj, i )
        pObj->pCopy = NULL;
    Vec_PtrForEachEntryStart( pNames, pName, i, 1 )
    {
        pActual = Io_ReadBlifCleanName(pName);
        if ( pActual == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Cannot parse formal/actual name pair \"%s\".", pName );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        Length = pActual - pName - 1;
        pName[Length] = 0;
        // find the PI net with this name
        pObj = Abc_NtkFindNet( pNtkModel, pName );
        if ( pObj == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Cannot find formal input \"%s\" as an PI of model \"%s\".", pName, Vec_PtrEntry(pNames, 0) );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        // get the PI
        pObj = Abc_ObjFanin0(pObj);
        // quit if this is not a PI net
        if ( !Abc_ObjIsPi(pObj) )
        {
            pName[Length] = '=';
            Start = i;
            break;
        }
        // remember the actual name in the net
        if ( pObj->pCopy != NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Formal input \"%s\" is used more than once.", pName );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        pObj->pCopy = (void *)pActual;
        // quit if we processed all PIs
        if ( i == Abc_NtkPiNum(pNtkModel) )
        {
            Start = i+1;
            break;
        }
    }
    // create the fanins of the box
    Abc_NtkForEachPi( pNtkModel, pObj, i )
    {
        pActual = (void *)pObj->pCopy;
        if ( pActual == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Formal input \"%s\" of model %s is not driven.", pName, Vec_PtrEntry(pNames, 0) );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        pNet = Abc_NtkFindOrCreateNet( pBox->pNtk, pActual );
        Abc_ObjAddFanin( pBox, pNet );
    }
    Abc_NtkForEachPi( pNtkModel, pObj, i )
        pObj->pCopy = NULL;

    // create the fanouts of the box
    Abc_NtkForEachPo( pNtkModel, pObj, i )
        pObj->pCopy = NULL;
    Vec_PtrForEachEntryStart( pNames, pName, i, Start )
    {
        pActual = Io_ReadBlifCleanName(pName);
        if ( pActual == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Cannot parse formal/actual name pair \"%s\".", pName );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        Length = pActual - pName - 1;
        pName[Length] = 0;
        // find the PO net with this name
        pObj = Abc_NtkFindNet( pNtkModel, pName );
        if ( pObj == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Cannot find formal output \"%s\" as an PO of model \"%s\".", pName, Vec_PtrEntry(pNames, 0) );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        // get the PO
        pObj = Abc_ObjFanout0(pObj);
        if ( pObj->pCopy != NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Formal output \"%s\" is used more than once.", pName );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        pObj->pCopy = (void *)pActual;
    }
    // create the fanouts of the box
    Abc_NtkForEachPo( pNtkModel, pObj, i )
    {
        pActual = (void *)pObj->pCopy;
        if ( pActual == NULL )
        {
            p->LineCur = (int)pBox->pCopy;
            sprintf( p->sError, "Formal output \"%s\" of model %s is not driven.", pName, Vec_PtrEntry(pNames, 0) );
            Io_ReadBlifPrintErrorMessage( p );
            return 1;
        }
        pNet = Abc_NtkFindOrCreateNet( pBox->pNtk, pActual );
        Abc_ObjAddFanin( pNet, pBox );
    }
    Abc_NtkForEachPo( pNtkModel, pObj, i )
        pObj->pCopy = NULL;

    // remove the array of names, assign the pointer to the model
    Vec_PtrFree( pBox->pData );
    pBox->pData = pNtkModel;
Alan Mishchenko committed
982 983 984
    return 0;
}

Alan Mishchenko committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/**Function*************************************************************

  Synopsis    [Connect the boxes in the hierarchy of networks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkConnectBoxesOne( Io_ReadBlif_t * p, Abc_Ntk_t * pNtk, stmm_table * tName2Model )
{
    Abc_Obj_t * pBox;
    int i;
    // go through the boxes
    Abc_NtkForEachBox( pNtk, pBox, i )
        if ( Io_ReadBlifNetworkConnectBoxesOneBox( p, pBox, tName2Model ) )
            return 1;
    Abc_NtkFinalizeRead( pNtk );
    return 0;
}

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

  Synopsis    [Connect the boxes in the hierarchy of networks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadBlifNetworkConnectBoxes( Io_ReadBlif_t * p, Abc_Ntk_t * pNtkMaster )
{
    stmm_generator * gen;
    Abc_Ntk_t * pNtk;
    char * pName;
    // connect the master network
    if ( Io_ReadBlifNetworkConnectBoxesOne( p, pNtkMaster, pNtkMaster->tName2Model ) )
        return 1;
    // connect other networks
    stmm_foreach_item( pNtkMaster->tName2Model, gen, &pName, (char **)&pNtk )
        if ( Io_ReadBlifNetworkConnectBoxesOne( p, pNtk, pNtkMaster->tName2Model ) )
            return 1;
    return 0;
}
Alan Mishchenko committed
1033 1034 1035 1036 1037 1038

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