ioReadBlifMv.c 78.8 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    [ioReadBlifMv.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read BLIF-MV file.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - January 8, 2007.]

  Revision    [$Id: ioReadBlifMv.c,v 1.00 2007/01/08 00:00:00 alanmi Exp $]

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

21 22
#include "base/abc/abc.h"
#include "misc/vec/vecPtr.h"
Alan Mishchenko committed
23
#include "ioAbc.h"
Alan Mishchenko committed
24

25 26
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
27 28 29 30 31
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define IO_BLIFMV_MAXVALUES 256
Alan Mishchenko committed
32
//#define IO_VERBOSE_OUTPUT
Alan Mishchenko committed
33 34 35 36 37

typedef struct Io_MvVar_t_ Io_MvVar_t; // parsing var
typedef struct Io_MvMod_t_ Io_MvMod_t; // parsing model
typedef struct Io_MvMan_t_ Io_MvMan_t; // parsing manager

38 39
Vec_Ptr_t *vGlobalLtlArray;

Alan Mishchenko committed
40 41 42 43 44 45 46 47 48 49 50 51 52
struct Io_MvVar_t_
{
    int                  nValues;      // the number of values 
    char **              pNames;       // the value names
};

struct Io_MvMod_t_
{
    // file lines
    char *               pName;        // .model line
    Vec_Ptr_t *          vInputs;      // .inputs lines
    Vec_Ptr_t *          vOutputs;     // .outputs lines
    Vec_Ptr_t *          vLatches;     // .latch lines
53
    Vec_Ptr_t *          vFlops;       // .flop lines
Alan Mishchenko committed
54 55 56
    Vec_Ptr_t *          vResets;      // .reset lines
    Vec_Ptr_t *          vNames;       // .names lines
    Vec_Ptr_t *          vSubckts;     // .subckt lines
Alan Mishchenko committed
57
    Vec_Ptr_t *          vShorts;      // .short lines
Alan Mishchenko committed
58
    Vec_Ptr_t *          vOnehots;     // .onehot lines
Alan Mishchenko committed
59
    Vec_Ptr_t *          vMvs;         // .mv lines
60 61
    Vec_Ptr_t *          vConstrs;     // .constraint lines
    Vec_Ptr_t *             vLtlProperties;
Alan Mishchenko committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    int                  fBlackBox;    // indicates blackbox model
    // the resulting network
    Abc_Ntk_t *          pNtk;   
    Abc_Obj_t *          pResetLatch; 
    // the parent manager
    Io_MvMan_t *         pMan;         
};

struct Io_MvMan_t_
{
    // general info about file
    int                  fBlifMv;      // the file is BLIF-MV
    int                  fUseReset;    // the reset circuitry is added
    char *               pFileName;    // the name of the file
    char *               pBuffer;      // the contents of the file
    Vec_Ptr_t *          vLines;       // the line beginnings
    // the results of reading
    Abc_Lib_t *          pDesign;      // the design under construction
    int                  nNDnodes;     // the counter of ND nodes
    // intermediate storage for models
    Vec_Ptr_t *          vModels;      // vector of models
    Io_MvMod_t *         pLatest;      // the current model
    // current processing info
    Vec_Ptr_t *          vTokens;      // the current tokens
    Vec_Ptr_t *          vTokens2;     // the current tokens
    Vec_Str_t *          vFunc;        // the local function
    // error reporting
    char                 sError[512];  // the error string generated during parsing
    // statistics 
    int                  nTablesRead;  // the number of processed tables
    int                  nTablesLeft;  // the number of dangling tables
};

// static functions
static Io_MvMan_t *      Io_MvAlloc();
static void              Io_MvFree( Io_MvMan_t * p );
static Io_MvMod_t *      Io_MvModAlloc();
static void              Io_MvModFree( Io_MvMod_t * p );
static char *            Io_MvLoadFile( char * pFileName );
static void              Io_MvReadPreparse( Io_MvMan_t * p );
Alan Mishchenko committed
102
static int               Io_MvReadInterfaces( Io_MvMan_t * p );
Alan Mishchenko committed
103 104 105 106
static Abc_Lib_t *       Io_MvParse( Io_MvMan_t * p );
static int               Io_MvParseLineModel( Io_MvMod_t * p, char * pLine );
static int               Io_MvParseLineInputs( Io_MvMod_t * p, char * pLine );
static int               Io_MvParseLineOutputs( Io_MvMod_t * p, char * pLine );
107
static int               Io_MvParseLineConstrs( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
108
static int               Io_MvParseLineLatch( Io_MvMod_t * p, char * pLine );
109
static int               Io_MvParseLineFlop( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
110
static int               Io_MvParseLineSubckt( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
111
static Vec_Int_t *       Io_MvParseLineOnehot( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
112 113 114
static int               Io_MvParseLineMv( Io_MvMod_t * p, char * pLine );
static int               Io_MvParseLineNamesMv( Io_MvMod_t * p, char * pLine, int fReset );
static int               Io_MvParseLineNamesBlif( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
115
static int               Io_MvParseLineShortBlif( Io_MvMod_t * p, char * pLine );
116
static int                 Io_MvParseLineLtlProperty( Io_MvMod_t * p, char * pLine );
Alan Mishchenko committed
117 118 119 120 121 122
static int               Io_MvParseLineGateBlif( Io_MvMod_t * p, Vec_Ptr_t * vTokens );
static Io_MvVar_t *      Abc_NtkMvVarDup( Abc_Ntk_t * pNtk, Io_MvVar_t * pVar );

static int               Io_MvCharIsSpace( char s )  { return s == ' ' || s == '\t' || s == '\r' || s == '\n';  }
static int               Io_MvCharIsMvSymb( char s ) { return s == '(' || s == ')' || s == '{' || s == '}' || s == '-' || s == ',' || s == '!';  }

123 124
static Vec_Vec_t *       Io_MvExtractBoxInfo( Abc_Ntk_t * pNtk );

Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
extern void              Abc_NtkStartMvVars( Abc_Ntk_t * pNtk );

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

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

  Synopsis    [Reads the network from the BLIF or BLIF-MV file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Io_ReadBlifMv( char * pFileName, int fBlifMv, int fCheck )
{
    FILE * pFile;
    Io_MvMan_t * p;
    Abc_Ntk_t * pNtk;
Alan Mishchenko committed
147
    Abc_Lib_t * pDesign = NULL; 
Alan Mishchenko committed
148 149
    char * pDesignName;
    int RetValue, i;
150
    char * pLtlProp;
Alan Mishchenko committed
151 152 153 154 155 156 157 158 159 160 161 162 163

    // check that the file is available
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Io_ReadBlifMv(): The file is unavailable (absent or open).\n" );
        return 0;
    }
    fclose( pFile );

    // start the file reader
    p = Io_MvAlloc();
    p->fBlifMv   = fBlifMv;
Alan Mishchenko committed
164
    p->fUseReset = 1;
Alan Mishchenko committed
165 166 167 168 169 170 171 172 173 174
    p->pFileName = pFileName;
    p->pBuffer   = Io_MvLoadFile( pFileName );
    if ( p->pBuffer == NULL )
    {
        Io_MvFree( p );
        return NULL;
    }
    // set the design name
    pDesignName  = Extra_FileNameGeneric( pFileName );
    p->pDesign   = Abc_LibCreate( pDesignName );
Alan Mishchenko committed
175
    ABC_FREE( pDesignName );
Alan Mishchenko committed
176
    // free the HOP manager
177
    Hop_ManStop( (Hop_Man_t *)p->pDesign->pManFunc );
Alan Mishchenko committed
178 179 180
    p->pDesign->pManFunc = NULL;
    // prepare the file for parsing
    Io_MvReadPreparse( p );
181
    vGlobalLtlArray = Vec_PtrAlloc( 100 );
Alan Mishchenko committed
182 183 184
    // parse interfaces of each network and construct the network
    if ( Io_MvReadInterfaces( p ) )
        pDesign = Io_MvParse( p );
Alan Mishchenko committed
185 186 187 188 189 190 191 192 193 194
    if ( p->sError[0] )
        fprintf( stdout, "%s\n", p->sError );
    Io_MvFree( p );
    if ( pDesign == NULL )
        return NULL;
// pDesign should be linked to all models of the design

    // make sure that everything is okay with the network structure
    if ( fCheck )
    {
195
        Vec_PtrForEachEntry( Abc_Ntk_t *, pDesign->vModules, pNtk, i )
Alan Mishchenko committed
196 197 198
        {
            if ( !Abc_NtkCheckRead( pNtk ) )
            {
Alan Mishchenko committed
199
                printf( "Io_ReadBlifMv: The network check has failed for model %s.\n", pNtk->pName );
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209
                Abc_LibFree( pDesign, NULL );
                return NULL;
            }
        }
    }

//Abc_LibPrint( pDesign );

    // detect top-level model
    RetValue = Abc_LibFindTopLevelModels( pDesign );
210
    pNtk = (Abc_Ntk_t *)Vec_PtrEntry( pDesign->vTops, 0 );
Alan Mishchenko committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    if ( RetValue > 1 )
        printf( "Warning: The design has %d root-level modules. The first one (%s) will be used.\n",
            Vec_PtrSize(pDesign->vTops), pNtk->pName );

    // extract the master network
    pNtk->pDesign = pDesign;
    pDesign->pManFunc = NULL;

    // verify the design for cyclic dependence
    assert( Vec_PtrSize(pDesign->vModules) > 0 );
    if ( Vec_PtrSize(pDesign->vModules) == 1 )
    {
//        printf( "Warning: The design is not hierarchical.\n" );
        Abc_LibFree( pDesign, pNtk );
        pNtk->pDesign = NULL;
        pNtk->pSpec = Extra_UtilStrsav( pFileName );
    }
    else
        Abc_NtkIsAcyclicHierarchy( pNtk );

//Io_WriteBlifMv( pNtk, "_temp_.mv" );
    if ( pNtk->pSpec == NULL )
        pNtk->pSpec = Extra_UtilStrsav( pFileName );
234 235 236

    Vec_PtrForEachEntry( char *, vGlobalLtlArray, pLtlProp, i )
        Vec_PtrPush( pNtk->vLtlProperties, pLtlProp );
237
    Vec_PtrFreeP( &vGlobalLtlArray );
238 239

    pNtk->vRealPos = Io_MvExtractBoxInfo( pNtk );
Alan Mishchenko committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    return pNtk;
}

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

  Synopsis    [Allocates the BLIF parsing structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Io_MvMan_t * Io_MvAlloc()
{
    Io_MvMan_t * p;
Alan Mishchenko committed
257
    p = ABC_ALLOC( Io_MvMan_t, 1 );
Alan Mishchenko committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    memset( p, 0, sizeof(Io_MvMan_t) );
    p->vLines   = Vec_PtrAlloc( 512 );
    p->vModels  = Vec_PtrAlloc( 512 );
    p->vTokens  = Vec_PtrAlloc( 512 );
    p->vTokens2 = Vec_PtrAlloc( 512 );
    p->vFunc    = Vec_StrAlloc( 512 );
    return p;
}

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

  Synopsis    [Frees the BLIF parsing structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvFree( Io_MvMan_t * p )
{
    Io_MvMod_t * pMod;
    int i;
    if ( p->pDesign )
        Abc_LibFree( p->pDesign, NULL );
    if ( p->pBuffer )  
Alan Mishchenko committed
285
        ABC_FREE( p->pBuffer );
Alan Mishchenko committed
286 287 288 289
    if ( p->vLines )
        Vec_PtrFree( p->vLines  );
    if ( p->vModels )
    {
290
        Vec_PtrForEachEntry( Io_MvMod_t *, p->vModels, pMod, i )
Alan Mishchenko committed
291 292 293 294 295 296
            Io_MvModFree( pMod );
        Vec_PtrFree( p->vModels );
    }
    Vec_PtrFree( p->vTokens );
    Vec_PtrFree( p->vTokens2 );
    Vec_StrFree( p->vFunc );
Alan Mishchenko committed
297
    ABC_FREE( p );
Alan Mishchenko committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
}

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

  Synopsis    [Allocates the BLIF parsing structure for one model.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Io_MvMod_t * Io_MvModAlloc()
{
    Io_MvMod_t * p;
Alan Mishchenko committed
314
    p = ABC_ALLOC( Io_MvMod_t, 1 );
Alan Mishchenko committed
315 316 317 318
    memset( p, 0, sizeof(Io_MvMod_t) );
    p->vInputs  = Vec_PtrAlloc( 512 );
    p->vOutputs = Vec_PtrAlloc( 512 );
    p->vLatches = Vec_PtrAlloc( 512 );
319
    p->vFlops   = Vec_PtrAlloc( 512 );
Alan Mishchenko committed
320 321 322
    p->vResets  = Vec_PtrAlloc( 512 );
    p->vNames   = Vec_PtrAlloc( 512 );
    p->vSubckts = Vec_PtrAlloc( 512 );
Alan Mishchenko committed
323
    p->vShorts  = Vec_PtrAlloc( 512 );
Alan Mishchenko committed
324
    p->vOnehots = Vec_PtrAlloc( 512 );
Alan Mishchenko committed
325
    p->vMvs     = Vec_PtrAlloc( 512 );
326 327
    p->vConstrs = Vec_PtrAlloc( 512 );
    p->vLtlProperties = Vec_PtrAlloc( 512 );
Alan Mishchenko committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    return p;
}

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

  Synopsis    [Allocates the BLIF parsing structure for one model.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvModFree( Io_MvMod_t * p )
{
//    if ( p->pNtk )
//        Abc_NtkDelete( p->pNtk );
Alan Mishchenko committed
346
    Vec_PtrFree( p->vLtlProperties );
Alan Mishchenko committed
347 348 349
    Vec_PtrFree( p->vInputs );
    Vec_PtrFree( p->vOutputs );
    Vec_PtrFree( p->vLatches );
350
    Vec_PtrFree( p->vFlops );
Alan Mishchenko committed
351 352 353
    Vec_PtrFree( p->vResets );
    Vec_PtrFree( p->vNames );
    Vec_PtrFree( p->vSubckts );
Alan Mishchenko committed
354
    Vec_PtrFree( p->vShorts );
Alan Mishchenko committed
355
    Vec_PtrFree( p->vOnehots );
Alan Mishchenko committed
356
    Vec_PtrFree( p->vMvs );
357
    Vec_PtrFree( p->vConstrs );
Alan Mishchenko committed
358
    ABC_FREE( p );
Alan Mishchenko committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
}



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

  Synopsis    [Counts the number of given chars.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvCountChars( char * pLine, char Char )
{
    char * pCur;
    int Counter = 0;
    for ( pCur = pLine; *pCur; pCur++ )
        if ( *pCur == Char )
            Counter++;
    return Counter;
}

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

  Synopsis    [Returns the place where the arrow is hiding.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Io_MvFindArrow( char * pLine )
{
    char * pCur;
    for ( pCur = pLine; *(pCur+1); pCur++ )
        if ( *pCur == '-' && *(pCur+1) == '>' )
        {
            *pCur = ' ';
            *(pCur+1) = ' ';
            return pCur;
        }
    return NULL;
}

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

  Synopsis    [Collects the already split tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvCollectTokens( Vec_Ptr_t * vTokens, char * pInput, char * pOutput )
{
    char * pCur;
    Vec_PtrClear( vTokens );
    for ( pCur = pInput; pCur < pOutput; pCur++ )
    {
        if ( *pCur == 0 )
            continue;
        Vec_PtrPush( vTokens, pCur );
        while ( *++pCur );
    }
}

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

  Synopsis    [Splits the line into tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvSplitIntoTokens( Vec_Ptr_t * vTokens, char * pLine, char Stop )
{
    char * pCur;
    // clear spaces
    for ( pCur = pLine; *pCur != Stop; pCur++ )
        if ( Io_MvCharIsSpace(*pCur) )
            *pCur = 0;
    // collect tokens
    Io_MvCollectTokens( vTokens, pLine, pCur );
}

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

  Synopsis    [Splits the line into tokens when .default may be present.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvSplitIntoTokensMv( Vec_Ptr_t * vTokens, char * pLine )
{
    char * pCur;
    // clear spaces
    for ( pCur = pLine; *pCur != '.' || *(pCur+1) == 'd'; pCur++ )
        if ( Io_MvCharIsSpace(*pCur) )
            *pCur = 0;
    // collect tokens
    Io_MvCollectTokens( vTokens, pLine, pCur );
}

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

  Synopsis    [Splits the line into tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvSplitIntoTokensAndClear( Vec_Ptr_t * vTokens, char * pLine, char Stop, char Char )
{
    char * pCur;
    // clear spaces
    for ( pCur = pLine; *pCur != Stop; pCur++ )
        if ( Io_MvCharIsSpace(*pCur) || *pCur == Char )
            *pCur = 0;
    // collect tokens
    Io_MvCollectTokens( vTokens, pLine, pCur );
}

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

  Synopsis    [Returns the 1-based number of the line in which the token occurs.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvGetLine( Io_MvMan_t * p, char * pToken )
{
    char * pLine;
    int i;
513
    Vec_PtrForEachEntry( char *, p->vLines, pLine, i )
Alan Mishchenko committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        if ( pToken < pLine )
            return i;
    return -1;
}

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

  Synopsis    [Reads the file into a character buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Io_MvLoadFile( char * pFileName )
{
    FILE * pFile;
    int nFileSize;
    char * pContents;
535
    int RetValue;
Alan Mishchenko committed
536 537 538 539 540 541 542 543 544 545
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Io_MvLoadFile(): The file is unavailable (absent or open).\n" );
        return NULL;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    if ( nFileSize == 0 )
    {
546
        fclose( pFile );
Alan Mishchenko committed
547 548 549
        printf( "Io_MvLoadFile(): The file is empty.\n" );
        return NULL;
    }
Alan Mishchenko committed
550
    pContents = ABC_ALLOC( char, nFileSize + 10 );
Alan Mishchenko committed
551
    rewind( pFile );
552
    RetValue = fread( pContents, nFileSize, 1, pFile );
Alan Mishchenko committed
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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    fclose( pFile );
    // finish off the file with the spare .end line
    // some benchmarks suddenly break off without this line
    strcpy( pContents + nFileSize, "\n.end\n" );
    return pContents;
}

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

  Synopsis    [Prepares the parsing.]

  Description [Performs several preliminary operations:
  - Cuts the file buffer into separate lines.
  - Removes comments and line extenders.
  - Sorts lines by directives.
  - Estimates the number of objects.
  - Allocates room for the objects.
  - Allocates room for the hash table.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_MvReadPreparse( Io_MvMan_t * p )
{
    char * pCur, * pPrev;
    int i, fComment = 0;
    // parse the buffer into lines and remove comments
    Vec_PtrPush( p->vLines, p->pBuffer );
    for ( pCur = p->pBuffer; *pCur; pCur++ )
    {
        if ( *pCur == '\n' )
        {
            *pCur = 0;
//            if ( *(pCur-1) == '\r' )
//                *(pCur-1) = 0;
            fComment = 0;
            Vec_PtrPush( p->vLines, pCur + 1 );
        }
        else if ( *pCur == '#' )
            fComment = 1;
        // remove comments
        if ( fComment )
            *pCur = 0;
    }

    // unfold the line extensions and sort lines by directive
601
    Vec_PtrForEachEntry( char *, p->vLines, pCur, i )
Alan Mishchenko committed
602 603 604 605 606 607 608 609
    {
        if ( *pCur == 0 )
            continue;
        // find previous non-space character
        for ( pPrev = pCur - 2; pPrev >= p->pBuffer; pPrev-- )
            if ( !Io_MvCharIsSpace(*pPrev) )
                break;
        // if it is the line extender, overwrite it with spaces
610
        if ( pPrev >= p->pBuffer && *pPrev == '\\' )
Alan Mishchenko committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
        {
            for ( ; *pPrev; pPrev++ )
                *pPrev = ' ';
            *pPrev = ' ';
            continue;
        }
        // skip spaces at the beginning of the line
        while ( Io_MvCharIsSpace(*pCur++) );
        // parse directives
        if ( *(pCur-1) != '.' )
            continue;
        if ( !strncmp(pCur, "names", 5) || !strncmp(pCur, "table", 5) || !strncmp(pCur, "gate", 4) )
            Vec_PtrPush( p->pLatest->vNames, pCur );
        else if ( p->fBlifMv && (!strncmp(pCur, "def ", 4) || !strncmp(pCur, "default ", 8)) )
            continue;
626 627
        else if ( !strncmp( pCur, "ltlformula", 10 ) )
            Vec_PtrPush( p->pLatest->vLtlProperties, pCur );
Alan Mishchenko committed
628 629
        else if ( !strncmp(pCur, "latch", 5) )
            Vec_PtrPush( p->pLatest->vLatches, pCur );
630 631
        else if ( !strncmp(pCur, "flop", 4) )
            Vec_PtrPush( p->pLatest->vFlops, pCur );
Alan Mishchenko committed
632 633 634 635 636 637 638 639
        else if ( !strncmp(pCur, "r ", 2) || !strncmp(pCur, "reset ", 6) )
            Vec_PtrPush( p->pLatest->vResets, pCur );
        else if ( !strncmp(pCur, "inputs", 6) )
            Vec_PtrPush( p->pLatest->vInputs, pCur );
        else if ( !strncmp(pCur, "outputs", 7) )
            Vec_PtrPush( p->pLatest->vOutputs, pCur );
        else if ( !strncmp(pCur, "subckt", 6) )
            Vec_PtrPush( p->pLatest->vSubckts, pCur );
Alan Mishchenko committed
640 641
        else if ( !strncmp(pCur, "short", 5) )
            Vec_PtrPush( p->pLatest->vShorts, pCur );
Alan Mishchenko committed
642 643
        else if ( !strncmp(pCur, "onehot", 6) )
            Vec_PtrPush( p->pLatest->vOnehots, pCur );
Alan Mishchenko committed
644 645
        else if ( p->fBlifMv && !strncmp(pCur, "mv", 2) )
            Vec_PtrPush( p->pLatest->vMvs, pCur );
646 647
        else if ( !strncmp(pCur, "constraint", 10) )
            Vec_PtrPush( p->pLatest->vConstrs, pCur );
Alan Mishchenko committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
        else if ( !strncmp(pCur, "blackbox", 8) )
            p->pLatest->fBlackBox = 1;
        else if ( !strncmp(pCur, "model", 5) ) 
        {
            p->pLatest = Io_MvModAlloc();
            p->pLatest->pName = pCur;
            p->pLatest->pMan = p;
        }
        else if ( !strncmp(pCur, "end", 3) )
        {
            if ( p->pLatest )
                Vec_PtrPush( p->vModels, p->pLatest );
            p->pLatest = NULL;
        }
        else if ( !strncmp(pCur, "exdc", 4) )
        {
            fprintf( stdout, "Line %d: Skipping EXDC network.\n", Io_MvGetLine(p, pCur) );
665 666 667 668
//            break;
            if ( p->pLatest )
                Vec_PtrPush( p->vModels, p->pLatest );
            p->pLatest = NULL;
Alan Mishchenko committed
669
        }
Alan Mishchenko committed
670 671
        else if ( !strncmp(pCur, "attrib", 6) )
        {}
Alan Mishchenko committed
672 673
        else if ( !strncmp(pCur, "delay", 5) )
        {}
Alan Mishchenko committed
674 675 676
        else if ( !strncmp(pCur, "input_", 6) )
        {}
        else if ( !strncmp(pCur, "output_", 7) )
Alan Mishchenko committed
677
        {}
Alan Mishchenko committed
678
        else if ( !strncmp(pCur, "no_merge", 8) )
Alan Mishchenko committed
679
        {}
680 681
        else if ( !strncmp(pCur, "wd", 2) )
        {}
Alan Mishchenko committed
682 683
//        else if ( !strncmp(pCur, "inouts", 6) )
//        {}
Alan Mishchenko committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        else
        {
            pCur--;
            if ( pCur[strlen(pCur)-1] == '\r' )
                pCur[strlen(pCur)-1] = 0;
            fprintf( stdout, "Line %d: Skipping line \"%s\".\n", Io_MvGetLine(p, pCur), pCur );
        }
    }
}

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

  Synopsis    [Parses interfaces of the models.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
705
static int Io_MvReadInterfaces( Io_MvMan_t * p )
Alan Mishchenko committed
706 707 708
{
    Io_MvMod_t * pMod;
    char * pLine;
709
    int i, k, nOutsOld;
Alan Mishchenko committed
710
    // iterate through the models
711
    Vec_PtrForEachEntry( Io_MvMod_t *, p->vModels, pMod, i )
Alan Mishchenko committed
712 713 714
    {
        // parse the model
        if ( !Io_MvParseLineModel( pMod, pMod->pName ) )
Alan Mishchenko committed
715
            return 0;
Alan Mishchenko committed
716 717 718 719
        // add model to the design
        if ( !Abc_LibAddModel( p->pDesign, pMod->pNtk ) )
        {
            sprintf( p->sError, "Line %d: Model %s is defined twice.", Io_MvGetLine(p, pMod->pName), pMod->pName );
Alan Mishchenko committed
720
            return 0;
Alan Mishchenko committed
721 722
        }
        // parse the inputs
723
        Vec_PtrForEachEntry( char *, pMod->vInputs, pLine, k )
Alan Mishchenko committed
724
            if ( !Io_MvParseLineInputs( pMod, pLine ) )
Alan Mishchenko committed
725
                return 0;
Alan Mishchenko committed
726
        // parse the outputs
727
        Vec_PtrForEachEntry( char *, pMod->vOutputs, pLine, k )
Alan Mishchenko committed
728
            if ( !Io_MvParseLineOutputs( pMod, pLine ) )
Alan Mishchenko committed
729
                return 0;
730 731 732 733 734 735 736 737 738
        // parse the constraints
        nOutsOld = Abc_NtkPoNum(pMod->pNtk);
        Vec_PtrForEachEntry( char *, pMod->vConstrs, pLine, k )
            if ( !Io_MvParseLineConstrs( pMod, pLine ) )
                return 0;
        pMod->pNtk->nConstrs = Abc_NtkPoNum(pMod->pNtk) - nOutsOld;
        Vec_PtrForEachEntry( char *, pMod->vLtlProperties, pLine, k )
            if ( !Io_MvParseLineLtlProperty( pMod, pLine ) )
                return 0;
739 740
        // report the results
#ifdef IO_VERBOSE_OUTPUT
741 742 743 744
        if ( Vec_PtrSize(p->vModels) > 1 )
            printf( "Parsed %-32s: PI =%6d  PO =%6d  ND =%8d  FF =%6d  B =%6d\n", 
                pMod->pNtk->pName, Abc_NtkPiNum(pMod->pNtk), Abc_NtkPoNum(pMod->pNtk),
                Vec_PtrSize(pMod->vNames), Vec_PtrSize(pMod->vLatches), Vec_PtrSize(pMod->vSubckts) );
745
#endif
Alan Mishchenko committed
746
    }
Alan Mishchenko committed
747
    return 1;
Alan Mishchenko committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
}


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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Abc_Lib_t * Io_MvParse( Io_MvMan_t * p )
{
    Abc_Lib_t * pDesign;
    Io_MvMod_t * pMod;
    char * pLine;
    int i, k;
    // iterate through the models
769
    Vec_PtrForEachEntry( Io_MvMod_t *, p->vModels, pMod, i )
Alan Mishchenko committed
770
    { 
771
#ifdef IO_VERBOSE_OUTPUT
772 773
        if ( Vec_PtrSize(p->vModels) > 1 )
            printf( "Parsing model %s...\n", pMod->pNtk->pName );
774 775
#endif

Alan Mishchenko committed
776 777 778 779
        // check if there any MV lines
        if ( Vec_PtrSize(pMod->vMvs) > 0 )
            Abc_NtkStartMvVars( pMod->pNtk );
        // parse the mv lines
780
        Vec_PtrForEachEntry( char *, pMod->vMvs, pLine, k )
Alan Mishchenko committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
            if ( !Io_MvParseLineMv( pMod, pLine ) )
                return NULL;
        // if reset lines are used there should be the same number of them as latches
        if ( Vec_PtrSize(pMod->vResets) > 0 )
        {
            if ( Vec_PtrSize(pMod->vLatches) != Vec_PtrSize(pMod->vResets) )
            {
                sprintf( p->sError, "Line %d: Model %s has different number of latches (%d) and reset nodes (%d).", 
                    Io_MvGetLine(p, pMod->pName), Abc_NtkName(pMod->pNtk), Vec_PtrSize(pMod->vLatches), Vec_PtrSize(pMod->vResets) );
                return NULL;
            }
            // create binary latch with 1-data and 0-init
            if ( p->fUseReset ) 
                pMod->pResetLatch = Io_ReadCreateResetLatch( pMod->pNtk, p->fBlifMv );
        }
796 797 798 799
        // parse the flops
        Vec_PtrForEachEntry( char *, pMod->vFlops, pLine, k )
            if ( !Io_MvParseLineFlop( pMod, pLine ) )
                return NULL;
Alan Mishchenko committed
800
        // parse the latches
801
        Vec_PtrForEachEntry( char *, pMod->vLatches, pLine, k )
Alan Mishchenko committed
802 803 804 805
            if ( !Io_MvParseLineLatch( pMod, pLine ) )
                return NULL;
        // parse the reset lines
        if ( p->fUseReset )
806
            Vec_PtrForEachEntry( char *, pMod->vResets, pLine, k )
Alan Mishchenko committed
807 808 809 810 811
                if ( !Io_MvParseLineNamesMv( pMod, pLine, 1 ) )
                    return NULL;
        // parse the nodes
        if ( p->fBlifMv )
        {
812
            Vec_PtrForEachEntry( char *, pMod->vNames, pLine, k )
Alan Mishchenko committed
813 814 815 816 817
                if ( !Io_MvParseLineNamesMv( pMod, pLine, 0 ) )
                    return NULL;
        }
        else
        {
818
            Vec_PtrForEachEntry( char *, pMod->vNames, pLine, k )
Alan Mishchenko committed
819 820
                if ( !Io_MvParseLineNamesBlif( pMod, pLine ) )
                    return NULL;
821
            Vec_PtrForEachEntry( char *, pMod->vShorts, pLine, k )
Alan Mishchenko committed
822 823
                if ( !Io_MvParseLineShortBlif( pMod, pLine ) )
                    return NULL;
Alan Mishchenko committed
824 825
        }
        // parse the subcircuits
826
        Vec_PtrForEachEntry( char *, pMod->vSubckts, pLine, k )
Alan Mishchenko committed
827 828
            if ( !Io_MvParseLineSubckt( pMod, pLine ) )
                return NULL;
Alan Mishchenko committed
829 830

        // allow for blackboxes without .blackbox line
Alan Mishchenko committed
831
        if ( Abc_NtkLatchNum(pMod->pNtk) == 0 && Abc_NtkNodeNum(pMod->pNtk) == 0 && Abc_NtkBoxNum(pMod->pNtk) == 0 )
Alan Mishchenko committed
832 833
        {
            if ( pMod->pNtk->ntkFunc == ABC_FUNC_SOP )
Alan Mishchenko committed
834
            {
835
                Mem_FlexStop( (Mem_Flex_t *)pMod->pNtk->pManFunc, 0 );
Alan Mishchenko committed
836
                pMod->pNtk->pManFunc = NULL;
Alan Mishchenko committed
837
                pMod->pNtk->ntkFunc = ABC_FUNC_BLACKBOX;
Alan Mishchenko committed
838
            }
Alan Mishchenko committed
839 840
        }

Alan Mishchenko committed
841 842
        // finalize the network
        Abc_NtkFinalizeRead( pMod->pNtk );
Alan Mishchenko committed
843 844 845 846 847 848 849
        // read the one-hotness lines
        if ( Vec_PtrSize(pMod->vOnehots) > 0 )
        {
            Vec_Int_t * vLine; 
            Abc_Obj_t * pObj;
            // set register numbers
            Abc_NtkForEachLatch( pMod->pNtk, pObj, k )
Alan Mishchenko committed
850
                pObj->pNext = (Abc_Obj_t *)(ABC_PTRINT_T)k;
Alan Mishchenko committed
851 852
            // derive register
            pMod->pNtk->vOnehots = Vec_PtrAlloc( Vec_PtrSize(pMod->vOnehots) );
853
            Vec_PtrForEachEntry( char *, pMod->vOnehots, pLine, k )
Alan Mishchenko committed
854 855 856 857 858 859 860 861 862 863 864 865
            {
                vLine = Io_MvParseLineOnehot( pMod, pLine );
                if ( vLine == NULL )
                    return NULL;
                Vec_PtrPush( pMod->pNtk->vOnehots, vLine );
//                printf( "Parsed %d one-hot registers.\n", Vec_IntSize(vLine) );
            }
            // reset register numbers
            Abc_NtkForEachLatch( pMod->pNtk, pObj, k )
                pObj->pNext = NULL;
            // print the result
            printf( "Parsed %d groups of 1-hot registers: { ", Vec_PtrSize(pMod->pNtk->vOnehots) );
866
            Vec_PtrForEachEntry( Vec_Int_t *, pMod->pNtk->vOnehots, vLine, k )
Alan Mishchenko committed
867 868 869 870 871 872 873 874 875 876 877 878
                printf( "%d ", Vec_IntSize(vLine) );
            printf( "}\n" );
            printf( "The total number of 1-hot registers = %d. (%.2f %%)\n", 
                Vec_VecSizeSize( (Vec_Vec_t *)pMod->pNtk->vOnehots ), 
                100.0 * Vec_VecSizeSize( (Vec_Vec_t *)pMod->pNtk->vOnehots ) / Abc_NtkLatchNum(pMod->pNtk) );
            {
                extern void Abc_GenOneHotIntervals( char * pFileName, int nPis, int nRegs, Vec_Ptr_t * vOnehots );
                char * pFileName = Extra_FileNameGenericAppend( pMod->pMan->pFileName, "_1h.blif" );
                Abc_GenOneHotIntervals( pFileName, Abc_NtkPiNum(pMod->pNtk), Abc_NtkLatchNum(pMod->pNtk), pMod->pNtk->vOnehots );
                printf( "One-hotness condition is written into file \"%s\".\n", pFileName );
            }
        }
879 880 881 882 883 884
        if ( Vec_PtrSize(pMod->vFlops) )
        {
            printf( "Warning: The parser converted %d .flop lines into .latch lines\n", Vec_PtrSize(pMod->vFlops) );
            printf( "(information about set, reset, enable of the flops may be lost).\n" );
        }

Alan Mishchenko committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    }
    if ( p->nNDnodes )
//        printf( "Warning: The parser added %d PIs to replace non-deterministic nodes.\n", p->nNDnodes );
        printf( "Warning: The parser added %d constant 0 nodes to replace non-deterministic nodes.\n", p->nNDnodes );
    // return the network
    pDesign = p->pDesign;
    p->pDesign = NULL;
    return pDesign;
}

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

  Synopsis    [Parses the model line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineModel( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
Alan Mishchenko committed
909
    char * pToken, * pPivot;
Alan Mishchenko committed
910
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
911
    pToken = (char *)Vec_PtrEntry( vTokens, 0 );
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920 921 922 923
    assert( !strcmp(pToken, "model") );
    if ( Vec_PtrSize(vTokens) != 2 )
    {
        sprintf( p->pMan->sError, "Line %d: Model line has %d entries while it should have 2.", Io_MvGetLine(p->pMan, pToken), Vec_PtrSize(vTokens) );
        return 0;
    }
    if ( p->fBlackBox )
        p->pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_BLACKBOX, 1 );
    else if ( p->pMan->fBlifMv )
        p->pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_BLIFMV, 1 );
    else 
        p->pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_SOP, 1 );
Alan Mishchenko committed
924 925 926
//    for ( pPivot = pToken = Vec_PtrEntry(vTokens, 1); *pToken; pToken++ )
//        if ( *pToken == '/' || *pToken == '\\' )
//            pPivot = pToken+1;
927
    pPivot = pToken = (char *)Vec_PtrEntry(vTokens, 1);
Alan Mishchenko committed
928
    p->pNtk->pName = Extra_UtilStrsav( pPivot );
Alan Mishchenko committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    return 1;
}

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

  Synopsis    [Parses the inputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineInputs( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    int i;
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
949
    pToken = (char *)Vec_PtrEntry(vTokens, 0);
Alan Mishchenko committed
950
    assert( !strcmp(pToken, "inputs") );
951
    Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, 1 )
Alan Mishchenko committed
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
        Io_ReadCreatePi( p->pNtk, pToken );
    return 1;
}

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

  Synopsis    [Parses the outputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineOutputs( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    int i;
972
    p->pNtk->nRealPos = Abc_NtkPoNum(p->pNtk);
Alan Mishchenko committed
973
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
974
    pToken = (char *)Vec_PtrEntry(vTokens, 0);
Alan Mishchenko committed
975
    assert( !strcmp(pToken, "outputs") );
976
    Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, 1 )
Alan Mishchenko committed
977 978 979 980 981 982
        Io_ReadCreatePo( p->pNtk, pToken );
    return 1;
}

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

983 984 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 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
  Synopsis    [Parses the outputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineConstrs( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    int i;
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = (char *)Vec_PtrEntry(vTokens, 0);
    assert( !strcmp(pToken, "constraint") );
    Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, 1 )
        Io_ReadCreatePo( p->pNtk, pToken );
    return 1;
}

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

  Synopsis    [Parses the LTL property line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineLtlProperty( Io_MvMod_t * p, char * pLine )
{
    int i, j;
    int quoteBegin, quoteEnd;
    char keyWordLtlFormula[11];
    char *actualLtlFormula;

    //checking if the line begins with the keyword "ltlformula" and
    //progressing the pointer forword
    for( i=0; i<10; i++ )
        keyWordLtlFormula[i] = pLine[i];
    quoteBegin = i;
    keyWordLtlFormula[10] = '\0';
    assert( strcmp( "ltlformula", keyWordLtlFormula ) == 0 );
    while( pLine[i] != '"' )
        i++;
    quoteBegin = i;
    i = strlen( pLine );
    while( pLine[i] != '"' )
        i--;
    quoteEnd = i;
    actualLtlFormula = (char *)malloc( sizeof(char) * (quoteEnd - quoteBegin) );
    //printf("\nThe input ltl formula = ");
    for( i = quoteBegin + 1, j = 0; i<quoteEnd; i++, j++ )
        //printf("%c", pLine[i] );
        actualLtlFormula[j] = pLine[i];
    actualLtlFormula[j] = '\0';
    Vec_PtrPush( vGlobalLtlArray, actualLtlFormula );
    return 1;
}


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

Alan Mishchenko committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  Synopsis    [Parses the latches line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineLatch( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Obj_t * pObj, * pNet;
    char * pToken;
    int Init;
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
1066
    pToken = (char *)Vec_PtrEntry(vTokens,0);
Alan Mishchenko committed
1067 1068 1069 1070 1071 1072 1073 1074 1075
    assert( !strcmp(pToken, "latch") );
    if ( Vec_PtrSize(vTokens) < 3 )
    {
        sprintf( p->pMan->sError, "Line %d: Latch does not have input name and output name.", Io_MvGetLine(p->pMan, pToken) );
        return 0;
    }
    // create latch
    if ( p->pResetLatch == NULL )
    {
1076
        pObj = Io_ReadCreateLatch( p->pNtk, (char *)Vec_PtrEntry(vTokens,1), (char *)Vec_PtrEntry(vTokens,2) );
Alan Mishchenko committed
1077 1078 1079 1080 1081
        // get initial value
        if ( p->pMan->fBlifMv )
            Abc_LatchSetInit0( pObj );
        else
        {
Alan Mishchenko committed
1082 1083 1084
            if ( Vec_PtrSize(vTokens) > 6 )
                printf( "Warning: Line %d has .latch directive with unrecognized entries (the total of %d entries).\n", 
                    Io_MvGetLine(p->pMan, pToken), Vec_PtrSize(vTokens) ); 
Alan Mishchenko committed
1085
            if ( Vec_PtrSize(vTokens) > 3 )
1086
                Init = atoi( (char *)Vec_PtrEntryLast(vTokens) );
Alan Mishchenko committed
1087 1088
            else
                Init = 2;
1089
            if ( Init < 0 || Init > 3 )
Alan Mishchenko committed
1090
            {
Alan Mishchenko committed
1091
                sprintf( p->pMan->sError, "Line %d: Initial state of the latch is incorrect \"%s\".", Io_MvGetLine(p->pMan, pToken), (char*)Vec_PtrEntry(vTokens,3) );
Alan Mishchenko committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
                return 0;
            }
            if ( Init == 0 )
                Abc_LatchSetInit0( pObj );
            else if ( Init == 1 )
                Abc_LatchSetInit1( pObj );
            else // if ( Init == 2 )
                Abc_LatchSetInitDc( pObj );
        }
    }
    else
    {
        // get the net corresponding to the output of the latch
1105
        pNet = Abc_NtkFindOrCreateNet( p->pNtk, (char *)Vec_PtrEntry(vTokens,2) );
Alan Mishchenko committed
1106 1107 1108
        // get the net corresponding to the latch output (feeding into reset MUX)
        pNet = Abc_NtkFindOrCreateNet( p->pNtk, Abc_ObjNameSuffix(pNet, "_out") );
        // create latch
1109
        pObj = Io_ReadCreateLatch( p->pNtk, (char *)Vec_PtrEntry(vTokens,1), Abc_ObjName(pNet) );
Alan Mishchenko committed
1110
//        Abc_LatchSetInit0( pObj );
Alan Mishchenko committed
1111 1112 1113 1114 1115 1116 1117
        Abc_LatchSetInit0( pObj );
    }
    return 1;
}

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

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
  Synopsis    [Parses the latches line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineFlop( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Obj_t * pObj;
    char * pToken, * pOutput, * pInput;
    int i, Init = 2;
    assert( !p->pMan->fBlifMv );
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = (char *)Vec_PtrEntry(vTokens,0);
    assert( !strcmp(pToken, "flop") );
    // get flop output
    Vec_PtrForEachEntry( char *, vTokens, pToken, i )
        if ( pToken[0] == 'Q' && pToken[1] == '=' )
            break;
    if ( i == Vec_PtrSize(vTokens) )
    {
        sprintf( p->pMan->sError, "Line %d: Cannot find flop output.", Io_MvGetLine(p->pMan, (char *)Vec_PtrEntry(vTokens,0)) );
        return 0;
    }
    pOutput = pToken+2;
    // get flop input
    Vec_PtrForEachEntry( char *, vTokens, pToken, i )
        if ( pToken[0] == 'D' && pToken[1] == '=' )
            break;
    if ( i == Vec_PtrSize(vTokens) )
    {
        sprintf( p->pMan->sError, "Line %d: Cannot find flop input.", Io_MvGetLine(p->pMan, (char *)Vec_PtrEntry(vTokens,0)) );
        return 0;
    }
    pInput = pToken+2;
    // create latch
    pObj = Io_ReadCreateLatch( p->pNtk, pInput, pOutput );
    // get the init value
    Vec_PtrForEachEntry( char *, vTokens, pToken, i )
    {
        if ( !strncmp( pToken, "init=", 5 ) )
        {
            Init = 0;
            if ( pToken[5] == '1' )
                Init = 1;
            else if ( pToken[5] == '2' )
                Init = 2;
            else if ( pToken[5] != '0' )
            {
                sprintf( p->pMan->sError, "Line %d: Cannot read flop init value %s.", Io_MvGetLine(p->pMan, pToken), pToken );
                return 0;
            }
            break;
        }
    }
    if ( Init < 0 || Init > 2 )
    {
        sprintf( p->pMan->sError, "Line %d: Initial state of the flop is incorrect \"%s\".", Io_MvGetLine(p->pMan, pToken), (char*)Vec_PtrEntry(vTokens,3) );
        return 0;
    }
    if ( Init == 0 )
        Abc_LatchSetInit0( pObj );
    else if ( Init == 1 )
        Abc_LatchSetInit1( pObj );
    else // if ( Init == 2 )
        Abc_LatchSetInitDc( pObj );
    return 1;
}

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

Alan Mishchenko committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  Synopsis    [Parses the subckt line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineSubckt( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Ntk_t * pModel;
    Abc_Obj_t * pBox, * pNet, * pTerm;
1207 1208
    char * pToken, * pName, * pName2, ** ppNames;
    int nEquals, Last, i, k;
Alan Mishchenko committed
1209 1210 1211 1212

    // split the line into tokens
    nEquals = Io_MvCountChars( pLine, '=' );
    Io_MvSplitIntoTokensAndClear( vTokens, pLine, '\0', '=' );
1213
    pToken = (char *)Vec_PtrEntry(vTokens,0);
Alan Mishchenko committed
1214
    assert( !strcmp(pToken, "subckt") );
1215
//printf( "%d ", nEquals );
Alan Mishchenko committed
1216 1217

    // get the model for this box
1218 1219 1220 1221 1222 1223 1224 1225 1226
    pName = (char *)Vec_PtrEntry(vTokens,1);
    // skip instance name for now
    for ( pToken = pName; *pToken; pToken++ )
        if ( *pToken == '|' )
        {
            *pToken = 0;
            break;
        }
    // find the model
Alan Mishchenko committed
1227 1228 1229 1230 1231 1232
    pModel = Abc_LibFindModelByName( p->pMan->pDesign, pName );
    if ( pModel == NULL )
    {
        sprintf( p->pMan->sError, "Line %d: Cannot find the model for subcircuit %s.", Io_MvGetLine(p->pMan, pToken), pName );
        return 0;
    }
Alan Mishchenko committed
1233
/*
Alan Mishchenko committed
1234 1235 1236 1237 1238 1239 1240
    // check if the number of tokens is correct
    if ( nEquals != Abc_NtkPiNum(pModel) + Abc_NtkPoNum(pModel) )
    {
        sprintf( p->pMan->sError, "Line %d: The number of ports (%d) in .subckt differs from the sum of PIs and POs of the model (%d).", 
            Io_MvGetLine(p->pMan, pToken), nEquals, Abc_NtkPiNum(pModel) + Abc_NtkPoNum(pModel) );
        return 0;
    }
Alan Mishchenko committed
1241
*/
Alan Mishchenko committed
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    // get the names
    ppNames = (char **)Vec_PtrArray(vTokens) + 2 + p->pMan->fBlifMv;

    // create the box with these terminals
    if ( Abc_NtkHasBlackbox(pModel) )
        pBox = Abc_NtkCreateBlackbox( p->pNtk );
    else
        pBox = Abc_NtkCreateWhitebox( p->pNtk );
    pBox->pData = pModel;
    if ( p->pMan->fBlifMv )
1252
        Abc_ObjAssignName( pBox, (char *)Vec_PtrEntry(vTokens,2), NULL );
Alan Mishchenko committed
1253
    // go through formal inputs
1254
    Last = 0;
Alan Mishchenko committed
1255
    Abc_NtkForEachPi( pModel, pTerm, i )
Alan Mishchenko committed
1256 1257
    { 
        // find this terminal among the actual inputs of the subcircuit
1258
        pName2 = NULL;
Alan Mishchenko committed
1259 1260
        pName = Abc_ObjName(Abc_ObjFanout0(pTerm));
        for ( k = 0; k < nEquals; k++ )
1261 1262 1263 1264
            if ( !strcmp( ppNames[2*((k+Last)%nEquals)], pName ) )
            {
                pName2 = ppNames[2*((k+Last)%nEquals)+1];
                Last = k+Last+1;
Alan Mishchenko committed
1265
                break;
1266
            }
Alan Mishchenko committed
1267
/*
Alan Mishchenko committed
1268 1269 1270 1271 1272 1273
        if ( k == nEquals )
        {
            sprintf( p->pMan->sError, "Line %d: Cannot find PI \"%s\" of the model \"%s\" as a formal input of the subcircuit.", 
                Io_MvGetLine(p->pMan, pToken), pName, Abc_NtkName(pModel) );
            return 0;
        }
Alan Mishchenko committed
1274 1275 1276 1277
*/
        if ( k == nEquals )
        {
            Abc_Obj_t * pNode = Abc_NtkCreateNode( p->pNtk );
1278
            pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pNtk->pManFunc, " 0\n" );
Alan Mishchenko committed
1279 1280 1281 1282 1283 1284 1285
            pNet = Abc_NtkFindOrCreateNet( p->pNtk, Abc_ObjNameSuffix(pNode, "abc") );
            Abc_ObjAddFanin( pNet, pNode );
            pTerm = Abc_NtkCreateBi( p->pNtk );
            Abc_ObjAddFanin( pBox, pTerm );
            Abc_ObjAddFanin( pTerm, pNet );
            continue;
        }
1286 1287
        assert( pName2 != NULL );
  
Alan Mishchenko committed
1288
        // create the BI with the actual name
1289
        pNet = Abc_NtkFindOrCreateNet( p->pNtk, pName2 );
Alan Mishchenko committed
1290 1291 1292 1293
        pTerm = Abc_NtkCreateBi( p->pNtk );
        Abc_ObjAddFanin( pBox, pTerm );
        Abc_ObjAddFanin( pTerm, pNet );
    }
Alan Mishchenko committed
1294
    // go through formal outputs
1295
    Last = 0;
Alan Mishchenko committed
1296 1297
    Abc_NtkForEachPo( pModel, pTerm, i )
    {
Alan Mishchenko committed
1298
        // find this terminal among the actual outputs of the subcircuit
1299
        pName2 = NULL;
Alan Mishchenko committed
1300 1301
        pName = Abc_ObjName(Abc_ObjFanin0(pTerm));
        for ( k = 0; k < nEquals; k++ )
1302 1303 1304 1305
            if ( !strcmp( ppNames[2*((k+Last)%nEquals)], pName ) )
            {
                pName2 = ppNames[2*((k+Last)%nEquals)+1];
                Last = k+Last+1;
Alan Mishchenko committed
1306
                break;
1307
            }
Alan Mishchenko committed
1308
/*
Alan Mishchenko committed
1309 1310 1311 1312 1313 1314
        if ( k == nEquals )
        {
            sprintf( p->pMan->sError, "Line %d: Cannot find PO \"%s\" of the modell \"%s\" as a formal output of the subcircuit.", 
                Io_MvGetLine(p->pMan, pToken), pName, Abc_NtkName(pModel) );
            return 0;
        }
Alan Mishchenko committed
1315
*/
1316 1317
        assert( pName2 != NULL );

Alan Mishchenko committed
1318 1319
        // create the BI with the actual name
        pTerm = Abc_NtkCreateBo( p->pNtk );
1320
        pNet = Abc_NtkFindOrCreateNet( p->pNtk, k == nEquals ? Abc_ObjNameSuffix(pTerm, "abc") : pName2 );
Alan Mishchenko committed
1321 1322 1323 1324 1325 1326
        Abc_ObjAddFanin( pNet, pTerm );
        Abc_ObjAddFanin( pTerm, pBox );
    }
    return 1;
}

Alan Mishchenko committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
/**Function*************************************************************

  Synopsis    [Parses the subckt line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Vec_Int_t * Io_MvParseLineOnehot( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
//    Vec_Ptr_t * vResult;
    Vec_Int_t * vResult;
    Abc_Obj_t * pNet, * pTerm;
    char * pToken;
    int nEquals, i;

    // split the line into tokens
    nEquals = Io_MvCountChars( pLine, '=' );
    Io_MvSplitIntoTokensAndClear( vTokens, pLine, '\0', '=' );
1350
    pToken = (char *)Vec_PtrEntry(vTokens,0);
Alan Mishchenko committed
1351 1352 1353 1354 1355
    assert( !strcmp(pToken, "onehot") );

    // iterate through the register names
//    vResult = Vec_PtrAlloc( Vec_PtrSize(vTokens) );
    vResult = Vec_IntAlloc( Vec_PtrSize(vTokens) );
1356
    Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, 1 )
Alan Mishchenko committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    {
        // check if this register exists
        pNet = Abc_NtkFindNet( p->pNtk, pToken );
        if ( pNet == NULL )
        {
            sprintf( p->pMan->sError, "Line %d: Signal with name \"%s\" does not exist in the model \"%s\".", 
                Io_MvGetLine(p->pMan, pToken), pToken, Abc_NtkName(p->pNtk) );
            return NULL;
        }
        // check if this is register output net
        pTerm = Abc_ObjFanin0( pNet );
        if ( pTerm == NULL || Abc_ObjFanin0(pTerm) == NULL || !Abc_ObjIsLatch(Abc_ObjFanin0(pTerm)) )
        {
            sprintf( p->pMan->sError, "Line %d: Signal with name \"%s\" is not a register in the model \"%s\".", 
                Io_MvGetLine(p->pMan, pToken), pToken, Abc_NtkName(p->pNtk) );
            return NULL;
        }
        // save register name
//        Vec_PtrPush( vResult, Abc_ObjName(pNet) );
Alan Mishchenko committed
1376
        Vec_IntPush( vResult, (int)(ABC_PTRINT_T)Abc_ObjFanin0(pTerm)->pNext );
Alan Mishchenko committed
1377
//        printf( "%d(%d) ", (int)Abc_ObjFanin0(pTerm)->pNext, ((int)Abc_ObjFanin0(pTerm)->pData) -1 );
Alan Mishchenko committed
1378
        printf( "%d", ((int)(ABC_PTRINT_T)Abc_ObjFanin0(pTerm)->pData)-1 );
Alan Mishchenko committed
1379 1380 1381 1382 1383
    }
    printf( "\n" );
    return vResult;
}

Alan Mishchenko committed
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

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

  Synopsis    [Parses the mv line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineMv( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Obj_t * pObj;
Alan Mishchenko committed
1400
    Io_MvVar_t * pVar = NULL;
1401
    Mem_Flex_t * pFlex;
Alan Mishchenko committed
1402 1403 1404 1405 1406
    char * pName;
    int nCommas, nValues, i, k;
    // count commas and get the tokens
    nCommas = Io_MvCountChars( pLine, ',' );
    Io_MvSplitIntoTokensAndClear( vTokens, pLine, '\0', ',' );
1407
    pName = (char *)Vec_PtrEntry(vTokens,0);
Alan Mishchenko committed
1408 1409 1410 1411
    assert( !strcmp(pName, "mv") );
    // get the number of values
    if ( Vec_PtrSize(vTokens) <= nCommas + 2 )
    {
Alan Mishchenko committed
1412
        sprintf( p->pMan->sError, "Line %d: The number of values in not specified in .mv line.", Io_MvGetLine(p->pMan, pName) );
Alan Mishchenko committed
1413 1414
        return 0;
    }
1415
    nValues = atoi( (char *)Vec_PtrEntry(vTokens,nCommas+2) );
Alan Mishchenko committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    if ( nValues < 2 || nValues > IO_BLIFMV_MAXVALUES )
    {
        sprintf( p->pMan->sError, "Line %d: The number of values (%d) is incorrect (should be >= 2 and <= %d).", 
            Io_MvGetLine(p->pMan, pName), nValues, IO_BLIFMV_MAXVALUES );
        return 0;
    }
    // if there is no symbolic values, quit
    if ( nValues == 2 && Vec_PtrSize(vTokens) == nCommas + 3 )
        return 1;
    if ( Vec_PtrSize(vTokens) > nCommas + 3 && Vec_PtrSize(vTokens) - (nCommas + 3) != nValues )
    {
        sprintf( p->pMan->sError, "Line %d: Wrong number (%d) of symbolic value names (should be %d).", 
            Io_MvGetLine(p->pMan, pName), Vec_PtrSize(vTokens) - (nCommas + 3), nValues );
        return 0;
    }
    // go through variables
1432
    pFlex = (Mem_Flex_t *)Abc_NtkMvVarMan( p->pNtk );
Alan Mishchenko committed
1433 1434
    for ( i = 0; i <= nCommas; i++ )
    {
1435
        pName = (char *)Vec_PtrEntry( vTokens, i+1 );
Alan Mishchenko committed
1436 1437
        pObj = Abc_NtkFindOrCreateNet( p->pNtk, pName );
        // allocate variable
1438
        pVar = (Io_MvVar_t *)Mem_FlexEntryFetch( pFlex, sizeof(Io_MvVar_t) );
Alan Mishchenko committed
1439 1440 1441 1442 1443
        pVar->nValues = nValues;
        pVar->pNames = NULL;
        // create names
        if ( Vec_PtrSize(vTokens) > nCommas + 3 )
        {
1444
            pVar->pNames = (char **)Mem_FlexEntryFetch( pFlex, sizeof(char *) * nValues );
1445
            Vec_PtrForEachEntryStart( char *, vTokens, pName, k, nCommas + 3 )
Alan Mishchenko committed
1446
            {
1447
                pVar->pNames[k-(nCommas + 3)] = (char *)Mem_FlexEntryFetch( pFlex, strlen(pName) + 1 );
Alan Mishchenko committed
1448 1449 1450 1451 1452 1453 1454
                strcpy( pVar->pNames[k-(nCommas + 3)], pName );
            }
        }
        // save the variable
        Abc_ObjSetMvVar( pObj, pVar );
    }
    // make sure the names are unique
Alan Mishchenko committed
1455
    assert(pVar);
Alan Mishchenko committed
1456 1457 1458 1459 1460 1461
    if ( pVar->pNames )
    {
        for ( i = 0; i < nValues; i++ )
        for ( k = i+1; k < nValues; k++ )
            if ( !strcmp(pVar->pNames[i], pVar->pNames[k]) )
            {
1462
                pName = (char *)Vec_PtrEntry(vTokens,0);
Alan Mishchenko committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
                sprintf( p->pMan->sError, "Line %d: Symbolic value name \"%s\" is repeated in .mv line.", 
                    Io_MvGetLine(p->pMan, pName), pVar->pNames[i] );
                return 0;
            }
    }
    return 1;
}

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

  Synopsis    [Writes the values into the BLIF-MV representation for the node.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvWriteValues( Abc_Obj_t * pNode, Vec_Str_t * vFunc )
{
    char Buffer[10];
    Abc_Obj_t * pFanin;
    int i;
    // add the fanin number of values
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        sprintf( Buffer, "%d", Abc_ObjMvVarNum(pFanin) );
1491
        Vec_StrPrintStr( vFunc, Buffer );
Alan Mishchenko committed
1492 1493 1494 1495
        Vec_StrPush( vFunc, ' ' );
    }
    // add the node number of values
    sprintf( Buffer, "%d", Abc_ObjMvVarNum(Abc_ObjFanout0(pNode)) );
1496
    Vec_StrPrintStr( vFunc, Buffer );
Alan Mishchenko committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
    Vec_StrPush( vFunc, '\n' );
    return 1;
}

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

  Synopsis    [Translated one literal.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLiteralMv( Io_MvMod_t * p, Abc_Obj_t * pNode, char * pToken, Vec_Str_t * vFunc, int iLit )
{
    char Buffer[10];
    Io_MvVar_t * pVar;
    Abc_Obj_t * pFanin, * pNet;
    char * pCur, * pNext;
    int i;
    // consider the equality literal
    if ( pToken[0] == '=' )
    {
        // find the fanins
        Abc_ObjForEachFanin( pNode, pFanin, i )
            if ( !strcmp( Abc_ObjName(pFanin), pToken + 1 ) )
                break;
        if ( i == Abc_ObjFaninNum(pNode) )
        {
            sprintf( p->pMan->sError, "Line %d: Node name in the table \"%s\" cannot be found on .names line.", 
                Io_MvGetLine(p->pMan, pToken), pToken + 1 );
            return 0;
        }
        Vec_StrPush( vFunc, '=' );
        sprintf( Buffer, "%d", i );
1534
        Vec_StrPrintStr( vFunc, Buffer );
Alan Mishchenko committed
1535 1536 1537 1538 1539 1540
        Vec_StrPush( vFunc, (char)((iLit == -1)? '\n' : ' ') );
        return 1;
    }
    // consider regular literal
    assert( iLit < Abc_ObjFaninNum(pNode) );
    pNet = iLit >= 0 ? Abc_ObjFanin(pNode, iLit) : Abc_ObjFanout0(pNode);
1541
    pVar = (Io_MvVar_t *)Abc_ObjMvVar( pNet );
Alan Mishchenko committed
1542 1543 1544
    // if the var is absent or has no symbolic values quit
    if ( pVar == NULL || pVar->pNames == NULL )
    {
1545
        Vec_StrPrintStr( vFunc, pToken );
Alan Mishchenko committed
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
        Vec_StrPush( vFunc, (char)((iLit == -1)? '\n' : ' ') );
        return 1;
    }
    // parse the literal using symbolic values
    for ( pCur = pToken; *pCur; pCur++ )
    {
        if ( Io_MvCharIsMvSymb(*pCur) )
        {
            Vec_StrPush( vFunc, *pCur );
            continue;
        }
        // find the next MvSymb char
        for ( pNext = pCur+1; *pNext; pNext++ )
            if ( Io_MvCharIsMvSymb(*pNext) )
                break;
        // look for the value name
        for ( i = 0; i < pVar->nValues; i++ )
            if ( !strncmp( pVar->pNames[i], pCur, pNext-pCur ) )
                break;
        if ( i == pVar->nValues )
        {
            *pNext = 0;
            sprintf( p->pMan->sError, "Line %d: Cannot find value name \"%s\" among the value names of variable \"%s\".", 
                Io_MvGetLine(p->pMan, pToken), pCur, Abc_ObjName(pNet) );
            return 0;
        }
        // value name is found
        sprintf( Buffer, "%d", i );
1574
        Vec_StrPrintStr( vFunc, Buffer );
Alan Mishchenko committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
        // update the pointer
        pCur = pNext - 1;
    }
    Vec_StrPush( vFunc, (char)((iLit == -1)? '\n' : ' ') );
    return 1;
}

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

  Synopsis    [Constructs the MV-SOP cover from the file parsing info.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Io_MvParseTableMv( Io_MvMod_t * p, Abc_Obj_t * pNode, Vec_Ptr_t * vTokens2, int nInputs, int nOutputs, int iOut )
{
    Vec_Str_t * vFunc = p->pMan->vFunc;
    char * pFirst, * pToken;
    int iStart, i;
    // prepare the place for the cover
    Vec_StrClear( vFunc );
    // write the number of values
//    Io_MvWriteValues( pNode, vFunc );
    // get the first token
1603
    pFirst = (char *)Vec_PtrEntry( vTokens2, 0 );
Alan Mishchenko committed
1604 1605 1606 1607
    if ( pFirst[0] == '.' )
    {
        // write the default literal
        Vec_StrPush( vFunc, 'd' );
1608
        pToken = (char *)Vec_PtrEntry(vTokens2, 1 + iOut);
Alan Mishchenko committed
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
        if ( !Io_MvParseLiteralMv( p, pNode, pToken, vFunc, -1 ) )
            return NULL;
        iStart = 1 + nOutputs;
    }
    else
        iStart = 0;
    // write the remaining literals
    while ( iStart < Vec_PtrSize(vTokens2) )
    {
        // input literals
        for ( i = 0; i < nInputs; i++ )
        {
1621
            pToken = (char *)Vec_PtrEntry( vTokens2, iStart + i );
Alan Mishchenko committed
1622 1623 1624 1625
            if ( !Io_MvParseLiteralMv( p, pNode, pToken, vFunc, i ) )
                return NULL;
        }
        // output literal
1626
        pToken = (char *)Vec_PtrEntry( vTokens2, iStart + nInputs + iOut );
Alan Mishchenko committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
        if ( !Io_MvParseLiteralMv( p, pNode, pToken, vFunc, -1 ) )
            return NULL;
        // update the counter
        iStart += nInputs + nOutputs;
    }       
    Vec_StrPush( vFunc, '\0' );
    return Vec_StrArray( vFunc );
}

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

  Synopsis    [Adds reset circuitry corresponding to latch with pName.]

  Description [Returns the reset node's net.]
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Abc_Obj_t * Io_MvParseAddResetCircuit( Io_MvMod_t * p, char * pName )
{
    char Buffer[50];
    Abc_Obj_t * pNode, * pData0Net, * pData1Net, * pResetLONet, * pOutNet;
    Io_MvVar_t * pVar;
    // make sure the reset latch exists
    assert( p->pResetLatch != NULL );
    // get the reset net
    pResetLONet = Abc_ObjFanout0(Abc_ObjFanout0(p->pResetLatch));
    // get the output net
    pOutNet = Abc_NtkFindOrCreateNet( p->pNtk, pName );
    // get the data nets
    pData0Net = Abc_NtkFindOrCreateNet( p->pNtk, Abc_ObjNameSuffix(pOutNet, "_reset") );
    pData1Net = Abc_NtkFindOrCreateNet( p->pNtk, Abc_ObjNameSuffix(pOutNet, "_out") );
    // duplicate MV variables
    if ( Abc_NtkMvVar(p->pNtk) )
    {
1664
        pVar = (Io_MvVar_t *)Abc_ObjMvVar( pOutNet );
Alan Mishchenko committed
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
        Abc_ObjSetMvVar( pData0Net, Abc_NtkMvVarDup(p->pNtk, pVar) );
        Abc_ObjSetMvVar( pData1Net, Abc_NtkMvVarDup(p->pNtk, pVar) );
    }
    // create the node
    pNode = Abc_NtkCreateNode( p->pNtk );
    // create the output net
    Abc_ObjAddFanin( pOutNet, pNode );
    // create the function
    if ( p->pMan->fBlifMv )
    {
//        Vec_Att_t * p = Abc_NtkMvVar( pNtk );
Alan Mishchenko committed
1676
//        int nValues = Abc_ObjMvVarNum(pOutNet);
Alan Mishchenko committed
1677 1678
//        sprintf( Buffer, "2 %d %d %d\n1 - - =1\n0 - - =2\n", nValues, nValues, nValues );
        sprintf( Buffer, "1 - - =1\n0 - - =2\n" );
1679
        pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pNtk->pManFunc, Buffer );
Alan Mishchenko committed
1680 1681
    }
    else
1682
        pNode->pData = Abc_SopCreateMux( (Mem_Flex_t *)p->pNtk->pManFunc );
Alan Mishchenko committed
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
    // add nets
    Abc_ObjAddFanin( pNode, pResetLONet );
    Abc_ObjAddFanin( pNode, pData1Net );
    Abc_ObjAddFanin( pNode, pData0Net );
    return pData0Net;
}

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

  Synopsis    [Parses the nodes line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineNamesMvOne( Io_MvMod_t * p, Vec_Ptr_t * vTokens, Vec_Ptr_t * vTokens2, int nInputs, int nOutputs, int iOut, int fReset )
{
    Abc_Obj_t * pNet, * pNode;
    char * pName;
    // get the output name
1706
    pName = (char *)Vec_PtrEntry( vTokens, Vec_PtrSize(vTokens) - nOutputs + iOut );
Alan Mishchenko committed
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
    // create the node
    if ( fReset )
    {
        pNet = Abc_NtkFindNet( p->pNtk, pName );
        if ( pNet == NULL )
        {
            sprintf( p->pMan->sError, "Line %d: Latch with output signal \"%s\" does not exist.", Io_MvGetLine(p->pMan, pName), pName );
            return 0;
        }
/*
        if ( !Abc_ObjIsBo(Abc_ObjFanin0(pNet)) )
        {
            sprintf( p->pMan->sError, "Line %d: Reset line \"%s\" defines signal that is not a latch output.", Io_MvGetLine(p->pMan, pName), pName );
            return 0;
        }
*/
        // construct the reset circuit and get the reset net feeding into it
        pNet = Io_MvParseAddResetCircuit( p, pName );
        // create fanins
        pNode = Io_ReadCreateNode( p->pNtk, Abc_ObjName(pNet), (char **)(vTokens->pArray + 1), nInputs );
        assert( nInputs == Vec_PtrSize(vTokens) - 2 );
    }
    else
    {
        pNet = Abc_NtkFindOrCreateNet( p->pNtk, pName );
        if ( Abc_ObjFaninNum(pNet) > 0 )
        {
            sprintf( p->pMan->sError, "Line %d: Signal \"%s\" is defined more than once.", Io_MvGetLine(p->pMan, pName), pName );
            return 0;
        }
        pNode = Io_ReadCreateNode( p->pNtk, pName, (char **)(vTokens->pArray + 1), nInputs );
    }
    // create the cover
    pNode->pData = Io_MvParseTableMv( p, pNode, vTokens2, nInputs, nOutputs, iOut );
    if ( pNode->pData == NULL )
        return 0;
1743
    pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pNtk->pManFunc, (char *)pNode->pData );
Alan Mishchenko committed
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
//printf( "Finished parsing node \"%s\" with table:\n%s\n", pName, pNode->pData );
    return 1;
}

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

  Synopsis    [Parses the nodes line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineNamesMv( Io_MvMod_t * p, char * pLine, int fReset )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Vec_Ptr_t * vTokens2 = p->pMan->vTokens2;
    Abc_Obj_t * pNet;
    char * pName, * pFirst, * pArrow;
    int nInputs, nOutputs, nLiterals, nLines, i;
    assert( p->pMan->fBlifMv );
    // get the arrow if it is present
    pArrow = Io_MvFindArrow( pLine );
    if ( !p->pMan->fBlifMv && pArrow ) 
    {
        sprintf( p->pMan->sError, "Line %d: Multi-output node symbol (->) in binary BLIF file.", Io_MvGetLine(p->pMan, pLine) );
        return 0;
    }
    // split names line into tokens
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
    if ( fReset )
1777
        assert( !strcmp((char *)Vec_PtrEntry(vTokens,0), "r") || !strcmp((char *)Vec_PtrEntry(vTokens,0), "reset") );
Alan Mishchenko committed
1778
    else
1779
        assert( !strcmp((char *)Vec_PtrEntry(vTokens,0), "names") || !strcmp((char *)Vec_PtrEntry(vTokens,0), "table") );
Alan Mishchenko committed
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
    // find the number of inputs and outputs
    nInputs  = Vec_PtrSize(vTokens) - 2;
    nOutputs = 1;
    if ( pArrow != NULL )
    {
        for ( i = Vec_PtrSize(vTokens) - 2; i >= 1; i-- )
            if ( pArrow < (char*)Vec_PtrEntry(vTokens,i) )
            {
                nInputs--;
                nOutputs++;
            }
    }
    // split table into tokens
1793
    pName = (char *)Vec_PtrEntryLast( vTokens );
Alan Mishchenko committed
1794
    Io_MvSplitIntoTokensMv( vTokens2, pName + strlen(pName) );
1795
    pFirst = (char *)Vec_PtrEntry( vTokens2, 0 );
Alan Mishchenko committed
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
    if ( pFirst[0] == '.' )
    {
        assert( pFirst[1] == 'd' );
        nLiterals = Vec_PtrSize(vTokens2) - 1 - nOutputs;
    }
    else
        nLiterals = Vec_PtrSize(vTokens2);
    // check the number of lines
    if ( nLiterals % (nInputs + nOutputs) != 0 )
    {
        sprintf( p->pMan->sError, "Line %d: Wrong number of literals in the table of node \"%s\". (Spaces inside literals are not allowed.)", Io_MvGetLine(p->pMan, pFirst), pName );
        return 0;
    }
    // check for the ND table
    nLines = nLiterals / (nInputs + nOutputs);
    if ( nInputs == 0 && nLines > 1 )
    {
        // add the outputs to the PIs
        for ( i = 0; i < nOutputs; i++ )
        {
1816
            pName = (char *)Vec_PtrEntry( vTokens, Vec_PtrSize(vTokens) - nOutputs + i );
Alan Mishchenko committed
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
            // get the net corresponding to this node
            pNet = Abc_NtkFindOrCreateNet(p->pNtk, pName);
            if ( fReset )
            {
                assert( p->pResetLatch != NULL );
                // construct the reset circuit and get the reset net feeding into it
                pNet = Io_MvParseAddResetCircuit( p, pName );
            }
            // add the new PI node
//            Abc_ObjAddFanin( pNet, Abc_NtkCreatePi(p->pNtk) );
//            fprintf( stdout, "Io_ReadBlifMv(): Adding PI for internal non-deterministic node \"%s\".\n", pName );
            p->pMan->nNDnodes++;
            Abc_ObjAddFanin( pNet, Abc_NtkCreateNodeConst0(p->pNtk) );
        }
        return 1;
    }
    // iterate through the outputs
    for ( i = 0; i < nOutputs; i++ )
    {
        if ( !Io_MvParseLineNamesMvOne( p, vTokens, vTokens2, nInputs, nOutputs, i, fReset ) )
            return 0;
    }
    return 1;
}


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

  Synopsis    [Constructs the SOP cover from the file parsing info.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Io_MvParseTableBlif( Io_MvMod_t * p, char * pTable, int nFanins )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Vec_Str_t * vFunc = p->pMan->vFunc;
Alan Mishchenko committed
1858
    char * pProduct, * pOutput, c;
Alan Mishchenko committed
1859 1860 1861 1862 1863 1864
    int i, Polarity = -1;

    p->pMan->nTablesRead++;
    // get the tokens
    Io_MvSplitIntoTokens( vTokens, pTable, '.' );
    if ( Vec_PtrSize(vTokens) == 0 )
1865
        return Abc_SopCreateConst0( (Mem_Flex_t *)p->pNtk->pManFunc );
Alan Mishchenko committed
1866 1867
    if ( Vec_PtrSize(vTokens) == 1 )
    {
1868
        pOutput = (char *)Vec_PtrEntry( vTokens, 0 );
Alan Mishchenko committed
1869 1870
        c = pOutput[0];
        if ( (c!='0'&&c!='1'&&c!='x'&&c!='n') || pOutput[1] )
Alan Mishchenko committed
1871 1872 1873 1874
        {
            sprintf( p->pMan->sError, "Line %d: Constant table has wrong output value \"%s\".", Io_MvGetLine(p->pMan, pOutput), pOutput );
            return NULL;
        }
1875
        return pOutput[0] == '0' ? Abc_SopCreateConst0((Mem_Flex_t *)p->pNtk->pManFunc) : Abc_SopCreateConst1((Mem_Flex_t *)p->pNtk->pManFunc);
Alan Mishchenko committed
1876
    }
1877
    pProduct = (char *)Vec_PtrEntry( vTokens, 0 );
Alan Mishchenko committed
1878 1879 1880 1881 1882 1883 1884 1885 1886
    if ( Vec_PtrSize(vTokens) % 2 == 1 )
    {
        sprintf( p->pMan->sError, "Line %d: Table has odd number of tokens (%d).", Io_MvGetLine(p->pMan, pProduct), Vec_PtrSize(vTokens) );
        return NULL;
    }
    // parse the table
    Vec_StrClear( vFunc );
    for ( i = 0; i < Vec_PtrSize(vTokens)/2; i++ )
    {
1887 1888
        pProduct = (char *)Vec_PtrEntry( vTokens, 2*i + 0 );
        pOutput  = (char *)Vec_PtrEntry( vTokens, 2*i + 1 );
Alan Mishchenko committed
1889 1890 1891 1892 1893
        if ( strlen(pProduct) != (unsigned)nFanins )
        {
            sprintf( p->pMan->sError, "Line %d: Cube \"%s\" has size different from the fanin count (%d).", Io_MvGetLine(p->pMan, pProduct), pProduct, nFanins );
            return NULL;
        }
Alan Mishchenko committed
1894 1895
        c = pOutput[0];
        if ( (c!='0'&&c!='1'&&c!='x'&&c!='n') || pOutput[1] )
Alan Mishchenko committed
1896 1897 1898 1899 1900
        {
            sprintf( p->pMan->sError, "Line %d: Output value \"%s\" is incorrect.", Io_MvGetLine(p->pMan, pProduct), pOutput );
            return NULL;
        }
        if ( Polarity == -1 )
Alan Mishchenko committed
1901 1902
            Polarity = (c=='1' || c=='x');
        else if ( Polarity != (c=='1' || c=='x') )
Alan Mishchenko committed
1903 1904 1905 1906 1907
        {
            sprintf( p->pMan->sError, "Line %d: Output value \"%s\" differs from the value in the first line of the table (%d).", Io_MvGetLine(p->pMan, pProduct), pOutput, Polarity );
            return NULL;
        }
        // parse one product 
1908
        Vec_StrPrintStr( vFunc, pProduct );
Alan Mishchenko committed
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
        Vec_StrPush( vFunc, ' ' );
        Vec_StrPush( vFunc, pOutput[0] );
        Vec_StrPush( vFunc, '\n' );
    }
    Vec_StrPush( vFunc, '\0' );
    return Vec_StrArray( vFunc );
}

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

  Synopsis    [Parses the nodes line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineNamesBlif( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Obj_t * pNet, * pNode;
    char * pName;
    assert( !p->pMan->fBlifMv );
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
    // parse the mapped node
1936
    if ( !strcmp((char *)Vec_PtrEntry(vTokens,0), "gate") )
Alan Mishchenko committed
1937 1938
        return Io_MvParseLineGateBlif( p, vTokens );
    // parse the regular name line
1939 1940
    assert( !strcmp((char *)Vec_PtrEntry(vTokens,0), "names") );
    pName = (char *)Vec_PtrEntryLast( vTokens );
Alan Mishchenko committed
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    pNet = Abc_NtkFindOrCreateNet( p->pNtk, pName );
    if ( Abc_ObjFaninNum(pNet) > 0 )
    {
        sprintf( p->pMan->sError, "Line %d: Signal \"%s\" is defined more than once.", Io_MvGetLine(p->pMan, pName), pName );
        return 0;
    }
    // create fanins
    pNode = Io_ReadCreateNode( p->pNtk, pName, (char **)(vTokens->pArray + 1), Vec_PtrSize(vTokens) - 2 );
    // parse the table of this node
    pNode->pData = Io_MvParseTableBlif( p, pName + strlen(pName), Abc_ObjFaninNum(pNode) );
    if ( pNode->pData == NULL )
        return 0;
1953
    pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pNtk->pManFunc, (char *)pNode->pData );
Alan Mishchenko committed
1954 1955 1956
    return 1;
}

1957 1958 1959 1960 1961 1962 1963 1964
ABC_NAMESPACE_IMPL_END

#include "map/mio/mio.h"
#include "base/main/main.h"

ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
1965 1966
/**Function*************************************************************

Alan Mishchenko committed
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
  Synopsis    [Parses the nodes line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineShortBlif( Io_MvMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Abc_Obj_t * pNet, * pNode;
    char * pName;
    assert( !p->pMan->fBlifMv );
    Io_MvSplitIntoTokens( vTokens, pLine, '\0' );
    if ( Vec_PtrSize(vTokens) != 3 )
    {
1985
        sprintf( p->pMan->sError, "Line %d: Expecting three entries in the .short line.", Io_MvGetLine(p->pMan, (char *)Vec_PtrEntry(vTokens,0)) );
Alan Mishchenko committed
1986 1987 1988
        return 0;
    }
    // parse the regular name line
1989 1990
    assert( !strcmp((char *)Vec_PtrEntry(vTokens,0), "short") );
    pName = (char *)Vec_PtrEntryLast( vTokens );
Alan Mishchenko committed
1991 1992 1993 1994 1995 1996 1997 1998 1999
    pNet = Abc_NtkFindOrCreateNet( p->pNtk, pName );
    if ( Abc_ObjFaninNum(pNet) > 0 )
    {
        sprintf( p->pMan->sError, "Line %d: Signal \"%s\" is defined more than once.", Io_MvGetLine(p->pMan, pName), pName );
        return 0;
    }
    // create fanins
    pNode = Io_ReadCreateNode( p->pNtk, pName, (char **)(vTokens->pArray + 1), 1 );
    // parse the table of this node
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
    if ( p->pNtk->ntkFunc == ABC_FUNC_MAP )
    {
        Mio_Library_t * pGenlib; 
        Mio_Gate_t * pGate;
        // check that the library is available
        pGenlib = (Mio_Library_t *)Abc_FrameReadLibGen();
        if ( pGenlib == NULL )
        {
            sprintf( p->pMan->sError, "Line %d: The current library is not available.", Io_MvGetLine(p->pMan, pName) );
            return 0;
        }
        // get the gate
        pGate = Mio_LibraryReadBuf( pGenlib );
        if ( pGate == NULL )
        {
            sprintf( p->pMan->sError, "Line %d: Cannot find buffer gate in the library.", Io_MvGetLine(p->pMan, pName) );
            return 0;
        }
        Abc_ObjSetData( pNode, pGate );
    }
    else
        pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pNtk->pManFunc, "1 1\n" );
Alan Mishchenko committed
2022 2023 2024 2025 2026
    return 1;
}

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

Alan Mishchenko committed
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
  Synopsis    [Duplicate the MV variable.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
Io_MvVar_t * Abc_NtkMvVarDup( Abc_Ntk_t * pNtk, Io_MvVar_t * pVar )
{
2038
    Mem_Flex_t * pFlex;
Alan Mishchenko committed
2039 2040 2041 2042
    Io_MvVar_t * pVarDup;
    int i;
    if ( pVar == NULL )
        return NULL;
2043
    pFlex = (Mem_Flex_t *)Abc_NtkMvVarMan( pNtk );
Alan Mishchenko committed
2044
    assert( pFlex != NULL );
2045
    pVarDup = (Io_MvVar_t *)Mem_FlexEntryFetch( pFlex, sizeof(Io_MvVar_t) );
Alan Mishchenko committed
2046 2047 2048 2049
    pVarDup->nValues = pVar->nValues;
    pVarDup->pNames = NULL;
    if ( pVar->pNames == NULL )
        return pVarDup;
2050
    pVarDup->pNames = (char **)Mem_FlexEntryFetch( pFlex, sizeof(char *) * pVar->nValues );
Alan Mishchenko committed
2051 2052
    for ( i = 0; i < pVar->nValues; i++ )
    {
2053
        pVarDup->pNames[i] = (char *)Mem_FlexEntryFetch( pFlex, strlen(pVar->pNames[i]) + 1 );
Alan Mishchenko committed
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
        strcpy( pVarDup->pNames[i], pVar->pNames[i] );
    }
    return pVarDup;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static 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;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_MvParseLineGateBlif( Io_MvMod_t * p, Vec_Ptr_t * vTokens )
{
Alan Mishchenko committed
2093
    extern int Io_ReadBlifReorderFormalNames( Vec_Ptr_t * vTokens, Mio_Gate_t * pGate );
Alan Mishchenko committed
2094 2095 2096 2097 2098 2099
    Mio_Library_t * pGenlib; 
    Mio_Gate_t * pGate;
    Abc_Obj_t * pNode;
    char ** ppNames, * pName;
    int i, nNames;

2100
    pName = (char *)vTokens->pArray[0];
Alan Mishchenko committed
2101 2102

    // check that the library is available
2103
    pGenlib = (Mio_Library_t *)Abc_FrameReadLibGen();
Alan Mishchenko committed
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
    if ( pGenlib == NULL )
    {
        sprintf( p->pMan->sError, "Line %d: The current library is not available.", Io_MvGetLine(p->pMan, pName) );
        return 0;
    }

    // create a new node and add it to the network
    if ( vTokens->nSize < 2 )
    {
        sprintf( p->pMan->sError, "Line %d: The .gate line has less than two tokens.", Io_MvGetLine(p->pMan, pName) );
        return 0;
    }

    // get the gate
2118
    pGate = Mio_LibraryReadGateByName( pGenlib, (char *)vTokens->pArray[1] );
Alan Mishchenko committed
2119 2120
    if ( pGate == NULL )
    {
Alan Mishchenko committed
2121
        sprintf( p->pMan->sError, "Line %d: Cannot find gate \"%s\" in the library.", Io_MvGetLine(p->pMan, pName), (char*)vTokens->pArray[1] );
Alan Mishchenko committed
2122 2123 2124 2125 2126 2127 2128 2129
        return 0;
    }

    // if this is the first line with gate, update the network type
    if ( Abc_NtkNodeNum(p->pNtk) == 0 )
    {
        assert( p->pNtk->ntkFunc == ABC_FUNC_SOP );
        p->pNtk->ntkFunc = ABC_FUNC_MAP;
2130
        Mem_FlexStop( (Mem_Flex_t *)p->pNtk->pManFunc, 0 );
Alan Mishchenko committed
2131 2132 2133
        p->pNtk->pManFunc = pGenlib;
    }

Alan Mishchenko committed
2134 2135 2136 2137 2138 2139 2140
    // reorder the formal inputs to be in the same order as in the gate
    if ( !Io_ReadBlifReorderFormalNames( vTokens, pGate ) )
    {
        sprintf( p->pMan->sError, "Line %d: Mismatch in the fanins of gate \"%s\".", Io_MvGetLine(p->pMan, pName), (char*)vTokens->pArray[1] );
        return 0;
    }

Alan Mishchenko committed
2141 2142 2143
    // remove the formal parameter names
    for ( i = 2; i < vTokens->nSize; i++ )
    {
2144
        vTokens->pArray[i] = Io_ReadBlifCleanName( (char *)vTokens->pArray[i] );
Alan Mishchenko committed
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
        if ( vTokens->pArray[i] == NULL )
        {
            sprintf( p->pMan->sError, "Line %d: Invalid gate input assignment.", Io_MvGetLine(p->pMan, pName) );
            return 0;
        }
    }

    // create the node
    ppNames = (char **)vTokens->pArray + 2;
    nNames  = vTokens->nSize - 3;
    pNode   = Io_ReadCreateNode( p->pNtk, ppNames[nNames], ppNames, nNames );

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

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
/**Function*************************************************************

  Synopsis    [Box mapping procedures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_MapBoxSetPrevNext( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode;
    pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+2);
    Vec_IntWriteEntry( vMapIn, Abc_ObjId(Abc_ObjFanin0(Abc_ObjFanin0(pNode))), Id );
    pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+4);
    Vec_IntWriteEntry( vMapOut, Abc_ObjId(Abc_ObjFanin0(Abc_ObjFanin0(pNode))), Id );
}
static inline int Abc_MapBox2Next( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+4);
    return Vec_IntEntry( vMapIn, Abc_ObjId(Abc_ObjFanin0(Abc_ObjFanin0(pNode))) );
}
static inline int Abc_MapBox2Prev( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+2);
    return Vec_IntEntry( vMapOut, Abc_ObjId(Abc_ObjFanin0(Abc_ObjFanin0(pNode))) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2203
static Vec_Vec_t * Io_MvExtractBoxInfo_( Abc_Ntk_t * pNtk )
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
{
    Vec_Int_t * vMapIn, * vMapOut, * vList;
    Vec_Ptr_t * vBoxInfo, * vDrivers;
    Abc_Obj_t * pObj;
    int i, boxId;

    // quit if there is no boxes
    if ( pNtk->nRealPos == 0 || (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) % 5 != 0 )
        return NULL;

    // allocate
    vBoxInfo = Vec_PtrAlloc( 10 );
    vDrivers = Vec_PtrAlloc( Abc_NtkPoNum(pNtk) );
    vMapIn   = Vec_IntStartFull( Abc_NtkObjNumMax(pNtk) );
    vMapOut  = Vec_IntStartFull( Abc_NtkObjNumMax(pNtk) );

    // collect drivers
    Abc_NtkForEachPo( pNtk, pObj, i )
        Vec_PtrPush( vDrivers, Abc_ObjFanin0(Abc_ObjFanin0(pObj)) );

    // map box signals into box numbers            
    for ( i = pNtk->nRealPos; i < Abc_NtkPoNum(pNtk); i += 5 )
    {
//        int k;
//        for ( k = 0; k < 5; k++ )
//            printf( "%d ", Abc_ObjFanin0(Abc_ObjFanin0(Abc_ObjFanin0(Abc_NtkPo(pNtk,i+k))))->Id );
//        printf( "\n" );
        Abc_MapBoxSetPrevNext( vDrivers, vMapIn, vMapOut, i );
    }

    // find those that do not have input mapped
    for ( i = pNtk->nRealPos; i < Abc_NtkPoNum(pNtk); i += 5 )
    {
        if ( Abc_MapBox2Prev( vDrivers, vMapIn, vMapOut, i ) != ~0 )
            continue;
        // create new list
        vList = Vec_IntAlloc( 32 );
        boxId = i;
        while ( boxId != ~0 )
        {
            Vec_IntPush( vList, boxId );
            boxId = Abc_MapBox2Next( vDrivers, vMapIn, vMapOut, boxId );
        }
        Vec_PtrPush( vBoxInfo, vList );
//printf( " %d", Vec_IntSize(vList) );
    }
//printf( "\n" );
    if ( 5 * Vec_VecSizeSize((Vec_Vec_t *)vBoxInfo) != (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) )
        printf( "Mismatch in the number of boxes!!!\n" );

    // clean up
    Vec_IntFree( vMapIn );
    Vec_IntFree( vMapOut );
    Vec_PtrFree( vDrivers );
    return (Vec_Vec_t *)vBoxInfo;
}

2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
/**Function*************************************************************

  Synopsis    [Compares strings up to the first underscore.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_MvExtractCompareStr( char * pName1, char * pName2 )
{
    while ( *pName1 && *pName1 != '_' && (*pName1 == *pName2) )
        pName1++, pName2++;
    return *pName1 - *pName2;
}

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

  Synopsis    [Returns the first number of the string.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_MvExtractReadNum1( char * pName )
{
    while ( *pName && (*pName < '0' || *pName > '9') )
        pName++;
    if ( *pName == 0 )
        return -1;
    return atoi( pName );
}

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

  Synopsis    [Returns the second number of the string.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_MvExtractReadNum2( char * pName )
{
    int Counter = 0;
    while ( *pName )
    {
        Counter += (*pName++ == '_');
        if ( Counter == 2 )
            break;
    }
    if ( *pName == 0 )
        return -1;
    return atoi( pName );
}

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

  Synopsis    [Procedure used for sorting the nodes in decreasing order of levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_MvExtractCompareNames( Abc_Obj_t ** pp1, Abc_Obj_t ** pp2 )
{
    char * pName1 = (char *)(*pp1)->pCopy;
    char * pName2 = (char *)(*pp2)->pCopy;
//    int Diff = Io_MvExtractReadNum1(pName1) - Io_MvExtractReadNum1(pName2);
    int Diff = Io_MvExtractCompareStr( pName1, pName2 );
    if ( Diff < 0 )
        return -1;
    if ( Diff > 0 ) 
        return 1;
    Diff = Io_MvExtractReadNum2(pName1) - Io_MvExtractReadNum2(pName2);
    if ( Diff < 0 )
        return -1;
    if ( Diff > 0 ) 
        return 1;
    return 0; 
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Vec_Vec_t * Io_MvExtractBoxInfo( Abc_Ntk_t * pNtk )
{
    int fVerbose = 0;
    Vec_Vec_t * vBoxInfo = NULL;
    Vec_Ptr_t * vPetals;
    Abc_Obj_t * pObj;
    char * pPrev, * pThis;
    int i, Level;

    // quit if there is no boxes
    if ( pNtk->nRealPos == 0 || (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) % 5 != 0 )
        return NULL;

    // transfer names
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        pObj->pCopy = (Abc_Obj_t *)Abc_ObjName( Abc_ObjFanin0(pObj) );
        pObj->iData = i;
    }

    // collect POs
    vPetals = Vec_PtrAlloc( Abc_NtkPoNum(pNtk) );
    for ( i = pNtk->nRealPos; i < Abc_NtkPoNum(pNtk); i += 5 )
        Vec_PtrPush( vPetals, Abc_NtkPo(pNtk, i) );

    // sort by name
    qsort( (void *)Vec_PtrArray(vPetals), Vec_PtrSize(vPetals), sizeof(Abc_Obj_t *), 
        (int (*)(const void *, const void *)) Io_MvExtractCompareNames );

    // sort the POs
    Level = -1;
    pPrev = "dummy";
    vBoxInfo = Vec_VecAlloc( 32 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vPetals, pObj, i )
    {
        pThis = (char *)pObj->pCopy;
        if ( Io_MvExtractCompareStr( pPrev, pThis ) )
        {
            Level++;
            if ( fVerbose )
                printf( "\n" );
        }
        Vec_VecPushInt( vBoxInfo, Level, pObj->iData );
        if ( fVerbose )
            printf( "%s ", (char *)pObj->pCopy );
        pPrev = pThis;
    }
    if ( fVerbose )
        printf( "\n" );
    if ( 5 * Vec_VecSizeSize(vBoxInfo) != (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) )
        printf( "Mismatch in the number of boxes!!!\n" );

    // clean up
    Vec_PtrFree( vPetals );

    // remove names
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        pObj->pCopy = NULL;
        pObj->iData = 0;
    }
    return vBoxInfo;
}
2427

Alan Mishchenko committed
2428 2429 2430 2431 2432
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


2433 2434
ABC_NAMESPACE_IMPL_END