summaryrefslogtreecommitdiffstats
path: root/src/aig/ntl/ntlReadBlif.c
blob: feb8e488ad4e262d5568be129d69f6887c98fbce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/**CFile****************************************************************

  FileName    [ntlReadBlif.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read BLIF file.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ntl.h"

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

typedef struct Ioa_ReadMod_t_ Ioa_ReadMod_t;   // parsing model
typedef struct Ioa_ReadMan_t_ Ioa_ReadMan_t;   // parsing manager

struct Ioa_ReadMod_t_
{
    // file lines
    char *               pFirst;       // .model line
    Vec_Ptr_t *          vInputs;      // .inputs lines
    Vec_Ptr_t *          vOutputs;     // .outputs lines
    Vec_Ptr_t *          vLatches;     // .latch lines
    Vec_Ptr_t *          vNames;       // .names lines
    Vec_Ptr_t *          vSubckts;     // .subckt lines
    int                  fBlackBox;    // indicates blackbox model
    // the resulting network
    Ntl_Mod_t *          pNtk;   
    // the parent manager
    Ioa_ReadMan_t *      pMan;         
};

struct Ioa_ReadMan_t_
{
    // general info about file
    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
    Ntl_Man_t *          pDesign;      // the design under construction
    // intermediate storage for models
    Vec_Ptr_t *          vModels;      // vector of models
    Ioa_ReadMod_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 Ioa_ReadMan_t *   Ioa_ReadAlloc();
static void              Ioa_ReadFree( Ioa_ReadMan_t * p );
static Ioa_ReadMod_t *   Ioa_ReadModAlloc();
static void              Ioa_ReadModFree( Ioa_ReadMod_t * p );
static char *            Ioa_ReadLoadFile( char * pFileName );
static void              Ioa_ReadReadPreparse( Ioa_ReadMan_t * p );
static void              Ioa_ReadReadInterfaces( Ioa_ReadMan_t * p );
static Ntl_Man_t *       Ioa_ReadParse( Ioa_ReadMan_t * p );
static int               Ioa_ReadParseLineModel( Ioa_ReadMod_t * p, char * pLine );
static int               Ioa_ReadParseLineInputs( Ioa_ReadMod_t * p, char * pLine );
static int               Ioa_ReadParseLineOutputs( Ioa_ReadMod_t * p, char * pLine );
static int               Ioa_ReadParseLineLatch( Ioa_ReadMod_t * p, char * pLine );
static int               Ioa_ReadParseLineSubckt( Ioa_ReadMod_t * p, char * pLine );
static int               Ioa_ReadParseLineNamesBlif( Ioa_ReadMod_t * p, char * pLine );

static int               Ioa_ReadCharIsSpace( char s )   { return s == ' ' || s == '\t' || s == '\r' || s == '\n';             }
static int               Ioa_ReadCharIsSopSymb( char s ) { return s == '0' || s == '1' || s == '-' || s == '\r' || s == '\n';  }

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

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

  Synopsis    [Reads the network from the BLIF file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ioa_ReadBlif( char * pFileName, int fCheck )
{
    FILE * pFile;
    Ioa_ReadMan_t * p;
    Ntl_Mod_t * pNtk;
    Ntl_Man_t * pDesign;
    int i;

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

    // start the file reader
    p = Ioa_ReadAlloc();
    p->pFileName = pFileName;
    p->pBuffer   = Ioa_ReadLoadFile( pFileName );
    if ( p->pBuffer == NULL )
    {
        Ioa_ReadFree( p );
        return NULL;
    }
    // set the design name
    p->pDesign = Ntl_ManAlloc( pFileName );
    // prepare the file for parsing
    Ioa_ReadReadPreparse( p );
    // parse interfaces of each network
    Ioa_ReadReadInterfaces( p );
    // construct the network
    pDesign = Ioa_ReadParse( p );
    if ( p->sError[0] )
        fprintf( stdout, "%s\n", p->sError );
    if ( pDesign == NULL )
        return NULL;
    p->pDesign = NULL;
    Ioa_ReadFree( p );
// pDesign should be linked to all models of the design

    // make sure that everything is okay with the network structure
    if ( fCheck )
    {
        // check individual models
        Vec_PtrForEachEntry( pDesign->vModels, pNtk, i )
        {
            if ( !Ntl_ModelCheck( pNtk ) )
            {
                printf( "Ioa_ReadBlif: The network check has failed for network %s.\n", pNtk->pName );
                Ntl_ManFree( pDesign );
                return NULL;
            }
        }
        // check the hierarchy
        if ( !Ntl_ManCheck( pDesign ) )
        {
            printf( "Ioa_ReadBlif: The hierarchy check has failed for design %s.\n", pDesign->pName );
            Ntl_ManFree( pDesign );
            return NULL;
        }

    }
//Ioa_WriteBlif( pDesign, "_temp_.blif" );
    return pDesign;
}

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

  Synopsis    [Allocates the BLIF parsing structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Ioa_ReadMan_t * Ioa_ReadAlloc()
{
    Ioa_ReadMan_t * p;
    p = ALLOC( Ioa_ReadMan_t, 1 );
    memset( p, 0, sizeof(Ioa_ReadMan_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 Ioa_ReadFree( Ioa_ReadMan_t * p )
{
    Ioa_ReadMod_t * pMod;
    int i;
    if ( p->pDesign )
        Ntl_ManFree( p->pDesign );
    if ( p->pBuffer )  
        free( p->pBuffer );
    if ( p->vLines )
        Vec_PtrFree( p->vLines );
    if ( p->vModels )
    {
        Vec_PtrForEachEntry( p->vModels, pMod, i )
            Ioa_ReadModFree( pMod );
        Vec_PtrFree( p->vModels );
    }
    Vec_PtrFree( p->vTokens );
    Vec_PtrFree( p->vTokens2 );
    Vec_StrFree( p->vFunc );
    free( p );
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Ioa_ReadMod_t * Ioa_ReadModAlloc()
{
    Ioa_ReadMod_t * p;
    p = ALLOC( Ioa_ReadMod_t, 1 );
    memset( p, 0, sizeof(Ioa_ReadMod_t) );
    p->vInputs  = Vec_PtrAlloc( 512 );
    p->vOutputs = Vec_PtrAlloc( 512 );
    p->vLatches = Vec_PtrAlloc( 512 );
    p->vNames   = Vec_PtrAlloc( 512 );
    p->vSubckts = Vec_PtrAlloc( 512 );
    return p;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Ioa_ReadModFree( Ioa_ReadMod_t * p )
{
    Vec_PtrFree( p->vInputs );
    Vec_PtrFree( p->vOutputs );
    Vec_PtrFree( p->vLatches );
    Vec_PtrFree( p->vNames );
    Vec_PtrFree( p->vSubckts );
    free( p );
}



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

  Synopsis    [Counts the number of given chars.]

  Description []
  
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Collects the already split tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Ioa_ReadCollectTokens( 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 Ioa_ReadSplitIntoTokens( Vec_Ptr_t * vTokens, char * pLine, char Stop )
{
    char * pCur;
    // clear spaces
    for ( pCur = pLine; *pCur != Stop; pCur++ )
        if ( Ioa_ReadCharIsSpace(*pCur) )
            *pCur = 0;
    // collect tokens
    Ioa_ReadCollectTokens( vTokens, pLine, pCur );
}

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

  Synopsis    [Splits the line into tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadGetLine( Ioa_ReadMan_t * p, char * pToken )
{
    char * pLine;
    int i;
    Vec_PtrForEachEntry( p->vLines, pLine, i )
        if ( pToken < pLine )
            return i;
    return -1;
}

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

  Synopsis    [Reads the file into a character buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Ioa_ReadLoadFile( char * pFileName )
{
    FILE * pFile;
    int nFileSize;
    char * pContents;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Ioa_ReadLoadFile(): The file is unavailable (absent or open).\n" );
        return NULL;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    if ( nFileSize == 0 )
    {
        printf( "Ioa_ReadLoadFile(): The file is empty.\n" );
        return NULL;
    }
    pContents = ALLOC( char, nFileSize + 10 );
    rewind( pFile );
    fread( pContents, nFileSize, 1, pFile );
    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 Ioa_ReadReadPreparse( Ioa_ReadMan_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
    Vec_PtrForEachEntry( p->vLines, pCur, i )
    {
        if ( *pCur == 0 )
            continue;
        // find previous non-space character
        for ( pPrev = pCur - 2; pPrev >= p->pBuffer; pPrev-- )
            if ( !Ioa_ReadCharIsSpace(*pPrev) )
                break;
        // if it is the line extender, overwrite it with spaces
        if ( *pPrev == '\\' )
        {
            for ( ; *pPrev; pPrev++ )
                *pPrev = ' ';
            *pPrev = ' ';
            continue;
        }
        // skip spaces at the beginning of the line
        while ( Ioa_ReadCharIsSpace(*pCur++) );
        // parse directives
        if ( *(pCur-1) != '.' )
            continue;
        if ( !strncmp(pCur, "names", 5) )
            Vec_PtrPush( p->pLatest->vNames, pCur );
        else if ( !strncmp(pCur, "latch", 5) )
            Vec_PtrPush( p->pLatest->vLatches, 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 );
        else if ( !strncmp(pCur, "blackbox", 8) )
            p->pLatest->fBlackBox = 1;
        else if ( !strncmp(pCur, "model", 5) ) 
        {
            p->pLatest = Ioa_ReadModAlloc();
            p->pLatest->pFirst = 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", Ioa_ReadGetLine(p, pCur) );
            break;
        }
        else
        {
            pCur--;
            if ( pCur[strlen(pCur)-1] == '\r' )
                pCur[strlen(pCur)-1] = 0;
            fprintf( stdout, "Line %d: Skipping line \"%s\".\n", Ioa_ReadGetLine(p, pCur), pCur );
        }
    }
}

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

  Synopsis    [Parses interfaces of the models.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Ioa_ReadReadInterfaces( Ioa_ReadMan_t * p )
{
    Ioa_ReadMod_t * pMod;
    char * pLine;
    int i, k;
    // iterate through the models
    Vec_PtrForEachEntry( p->vModels, pMod, i )
    {
        // parse the model
        if ( !Ioa_ReadParseLineModel( pMod, pMod->pFirst ) )
            return;
        // parse the inputs
        Vec_PtrForEachEntry( pMod->vInputs, pLine, k )
            if ( !Ioa_ReadParseLineInputs( pMod, pLine ) )
                return;
        // parse the outputs
        Vec_PtrForEachEntry( pMod->vOutputs, pLine, k )
            if ( !Ioa_ReadParseLineOutputs( pMod, pLine ) )
                return;
    }
}


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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Ntl_Man_t * Ioa_ReadParse( Ioa_ReadMan_t * p )
{
    Ntl_Man_t * pDesign;
    Ioa_ReadMod_t * pMod;
    char * pLine;
    int i, k;
    // iterate through the models
    Vec_PtrForEachEntry( p->vModels, pMod, i )
    {
        // parse the latches
        Vec_PtrForEachEntry( pMod->vLatches, pLine, k )
            if ( !Ioa_ReadParseLineLatch( pMod, pLine ) )
                return NULL;
        // parse the nodes
        Vec_PtrForEachEntry( pMod->vNames, pLine, k )
            if ( !Ioa_ReadParseLineNamesBlif( pMod, pLine ) )
                return NULL;
        // parse the subcircuits
        Vec_PtrForEachEntry( pMod->vSubckts, pLine, k )
            if ( !Ioa_ReadParseLineSubckt( pMod, pLine ) )
                return NULL;
        // finalize the network
        Ntl_ModelFixNonDrivenNets( pMod->pNtk );
    }
    // return the network
    pDesign = p->pDesign;
    p->pDesign = NULL;
    return pDesign;
}

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

  Synopsis    [Parses the model line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadParseLineModel( Ioa_ReadMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    Ioa_ReadSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = Vec_PtrEntry( vTokens, 0 );
    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.", Ioa_ReadGetLine(p->pMan, pToken), Vec_PtrSize(vTokens) );
        return 0;
    }
    p->pNtk = Ntl_ModelAlloc( p->pMan->pDesign, Vec_PtrEntry(vTokens, 1) );
    return 1;
}

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

  Synopsis    [Parses the inputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadParseLineInputs( Ioa_ReadMod_t * p, char * pLine )
{
    Ntl_Net_t * pNet;
    Ntl_Obj_t * pObj;
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    int i;
    Ioa_ReadSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = Vec_PtrEntry(vTokens, 0);
    assert( !strcmp(pToken, "inputs") );
    Vec_PtrForEachEntryStart( vTokens, pToken, i, 1 )
    {
        pObj = Ntl_ModelCreatePi( p->pNtk );
        pNet = Ntl_ModelFindOrCreateNet( p->pNtk, pToken );
        if ( !Ntl_ModelSetNetDriver( pObj, pNet ) )
        {
            sprintf( p->pMan->sError, "Line %d: Net %s already has a driver.", Ioa_ReadGetLine(p->pMan, pToken), pNet->pName );
            return 0;
        }
    }
    return 1;
}

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

  Synopsis    [Parses the outputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadParseLineOutputs( Ioa_ReadMod_t * p, char * pLine )
{
    Ntl_Net_t * pNet;
    Ntl_Obj_t * pObj;
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    char * pToken;
    int i;
    Ioa_ReadSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = Vec_PtrEntry(vTokens, 0);
    assert( !strcmp(pToken, "outputs") );
    Vec_PtrForEachEntryStart( vTokens, pToken, i, 1 )
    {
        pNet = Ntl_ModelFindOrCreateNet( p->pNtk, pToken );
        pObj = Ntl_ModelCreatePo( p->pNtk, pNet );
    }
    return 1;
}

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

  Synopsis    [Parses the latches line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadParseLineLatch( Ioa_ReadMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Ntl_Net_t * pNetLi, * pNetLo;
    Ntl_Obj_t * pObj;
    char * pToken, * pNameLi, * pNameLo;
    int Init, Class;
    Ioa_ReadSplitIntoTokens( vTokens, pLine, '\0' );
    pToken = Vec_PtrEntry(vTokens,0);
    assert( !strcmp(pToken, "latch") );
    if ( Vec_PtrSize(vTokens) < 3 )
    {
        sprintf( p->pMan->sError, "Line %d: Latch does not have input name and output name.", Ioa_ReadGetLine(p->pMan, pToken) );
        return 0;
    }
    // create latch
    pNameLi = Vec_PtrEntry( vTokens, 1 );
    pNameLo = Vec_PtrEntry( vTokens, 2 );
    pNetLi  = Ntl_ModelFindOrCreateNet( p->pNtk, pNameLi );
    pNetLo  = Ntl_ModelFindOrCreateNet( p->pNtk, pNameLo );
    pObj    = Ntl_ModelCreateLatch( p->pNtk );
    pObj->pFanio[0] = pNetLi;
    if ( !Ntl_ModelSetNetDriver( pObj, pNetLo ) )
    {
        sprintf( p->pMan->sError, "Line %d: Net %s already has a driver.", Ioa_ReadGetLine(p->pMan, pToken), pNetLo->pName );
        return 0;
    }
    // get initial value
    if ( Vec_PtrSize(vTokens) > 3 )
        Init = atoi( Vec_PtrEntry(vTokens,Vec_PtrSize(vTokens)-1) );
    else
        Init = 2;
    if ( Init < 0 || Init > 2 )
    {
        sprintf( p->pMan->sError, "Line %d: Initial state of the latch is incorrect \"%s\".", Ioa_ReadGetLine(p->pMan, pToken), Vec_PtrEntry(vTokens,3) );
        return 0;
    }
    // get the register class
    if ( Vec_PtrSize(vTokens) == 6 )
        Class = atoi( Vec_PtrEntry(vTokens,3) );
    else
        Class = 0;
    if ( Class < 0 || Class > (1<<24) )
    {
        sprintf( p->pMan->sError, "Line %d: Class of the latch is incorrect \"%s\".", Ioa_ReadGetLine(p->pMan, pToken), Vec_PtrEntry(vTokens,4) );
        return 0;
    }
    pObj->LatchId = (Class << 2) | Init;
    // get the clock
    if ( Vec_PtrSize(vTokens) == 5 || Vec_PtrSize(vTokens) == 6 )
    {
        pToken = Vec_PtrEntry(vTokens,Vec_PtrSize(vTokens)-2);
        pNetLi = Ntl_ModelFindOrCreateNet( p->pNtk, pToken );
        pObj->pFanio[1] = pNetLi;
    }
    return 1;
}

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

  Synopsis    [Parses the subckt line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Ioa_ReadParseLineSubckt( Ioa_ReadMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Ntl_Mod_t * pModel;
    Ntl_Obj_t * pBox, * pTerm;
    Ntl_Net_t * pNet;
    char * pToken, * pName, ** ppNames;
    int nEquals, i, k;

    // split the line into tokens
    nEquals = Ioa_ReadCountChars( pLine, '=' );
    Ioa_ReadSplitIntoTokensAndClear( vTokens, pLine, '\0', '=' );
    pToken = Vec_PtrEntry(vTokens,0);
    assert( !strcmp(pToken, "subckt") );

    // get the model for this box
    pName = Vec_PtrEntry(vTokens,1);
    pModel = Ntl_ManFindModel( p->pMan->pDesign, pName );
    if ( pModel == NULL )
    {
        sprintf( p->pMan->sError, "Line %d: Cannot find the model for subcircuit %s.", Ioa_ReadGetLine(p->pMan, pToken), pName );
        return 0;
    }

    // check if the number of tokens is correct
    if ( nEquals != Ntl_ModelPiNum(pModel) + Ntl_ModelPoNum(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).", 
            Ioa_ReadGetLine(p->pMan, pToken), nEquals, Ntl_ModelPiNum(pModel) + Ntl_ModelPoNum(pModel) );
        return 0;
    }

    // get the names
    ppNames = (char **)Vec_PtrArray(vTokens) + 2;

    // create the box with these terminals
    pBox = Ntl_ModelCreateBox( p->pNtk, Ntl_ModelPiNum(pModel), Ntl_ModelPoNum(pModel) );
    pBox->pImplem = pModel;
    Ntl_ModelForEachPi( pModel, pTerm, i )
    {
        // find this terminal among the formal inputs of the subcircuit
        pName = Ntl_ObjFanout0(pTerm)->pName;
        for ( k = 0; k < nEquals; k++ )
            if ( !strcmp( ppNames[2*k], pName ) )
                break;
        if ( k == nEquals )
        {
            sprintf( p->pMan->sError, "Line %d: Cannot find PI \"%s\" of the model \"%s\" as a formal input of the subcircuit.", 
                Ioa_ReadGetLine(p->pMan, pToken), pName, pModel->pName );
            return 0;
        }
        // create the BI with the actual name
        pNet = Ntl_ModelFindOrCreateNet( p->pNtk, ppNames[2*k+1] );
        Ntl_ObjSetFanin( pBox, pNet, i );
    }
    Ntl_ModelForEachPo( pModel, pTerm, i )
    {
        // find this terminal among the formal outputs of the subcircuit
        pName = Ntl_ObjFanin0(pTerm)->pName;
        for ( k = 0; k < nEquals; k++ )
            if ( !strcmp( ppNames[2*k], pName ) )
                break;
        if ( k == nEquals )
        {
            sprintf( p->pMan->sError, "Line %d: Cannot find PO \"%s\" of the model \"%s\" as a formal output of the subcircuit.", 
                Ioa_ReadGetLine(p->pMan, pToken), pName, pModel->pName );
            return 0;
        }
        // create the BI with the actual name
        pNet = Ntl_ModelFindOrCreateNet( p->pNtk, ppNames[2*k+1] );
        Ntl_ObjSetFanout( pBox, pNet, i );
    }
    return 1;
}


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

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

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Ioa_ReadParseTableBlif( Ioa_ReadMod_t * p, char * pTable, int nFanins )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Vec_Str_t * vFunc = p->pMan->vFunc;
    char * pProduct, * pOutput;
    int i, Polarity = -1;

    p->pMan->nTablesRead++;
    // get the tokens
    Ioa_ReadSplitIntoTokens( vTokens, pTable, '.' );
    if ( Vec_PtrSize(vTokens) == 0 )
        return Ntl_ManStoreSop( p->pMan->pDesign, " 0\n" );
    if ( Vec_PtrSize(vTokens) == 1 )
    {
        pOutput = Vec_PtrEntry( vTokens, 0 );
        if ( ((pOutput[0] - '0') & 0x8E) || pOutput[1] )
        {
            sprintf( p->pMan->sError, "Line %d: Constant table has wrong output value \"%s\".", Ioa_ReadGetLine(p->pMan, pOutput), pOutput );
            return NULL;
        }
        return Ntl_ManStoreSop( p->pMan->pDesign, (pOutput[0] == '0') ? " 0\n" : " 1\n" );
    }
    pProduct = Vec_PtrEntry( vTokens, 0 );
    if ( Vec_PtrSize(vTokens) % 2 == 1 )
    {
        sprintf( p->pMan->sError, "Line %d: Table has odd number of tokens (%d).", Ioa_ReadGetLine(p->pMan, pProduct), Vec_PtrSize(vTokens) );
        return NULL;
    }
    // parse the table
    Vec_StrClear( vFunc );
    for ( i = 0; i < Vec_PtrSize(vTokens)/2; i++ )
    {
        pProduct = Vec_PtrEntry( vTokens, 2*i + 0 );
        pOutput  = Vec_PtrEntry( vTokens, 2*i + 1 );
        if ( strlen(pProduct) != (unsigned)nFanins )
        {
            sprintf( p->pMan->sError, "Line %d: Cube \"%s\" has size different from the fanin count (%d).", Ioa_ReadGetLine(p->pMan, pProduct), pProduct, nFanins );
            return NULL;
        }
        if ( ((pOutput[0] - '0') & 0x8E) || pOutput[1] )
        {
            sprintf( p->pMan->sError, "Line %d: Output value \"%s\" is incorrect.", Ioa_ReadGetLine(p->pMan, pProduct), pOutput );
            return NULL;
        }
        if ( Polarity == -1 )
            Polarity = pOutput[0] - '0';
        else if ( Polarity != pOutput[0] - '0' )
        {
            sprintf( p->pMan->sError, "Line %d: Output value \"%s\" differs from the value in the first line of the table (%d).", Ioa_ReadGetLine(p->pMan, pProduct), pOutput, Polarity );
            return NULL;
        }
        // parse one product 
        Vec_StrAppend( vFunc, pProduct );
        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 Ioa_ReadParseLineNamesBlif( Ioa_ReadMod_t * p, char * pLine )
{
    Vec_Ptr_t * vTokens = p->pMan->vTokens;
    Ntl_Obj_t * pNode;
    Ntl_Net_t * pNetOut, * pNetIn;
    char * pNameOut, * pNameIn;
    int i;
    Ioa_ReadSplitIntoTokens( vTokens, pLine, '\0' );
    // parse the mapped node
//    if ( !strcmp(Vec_PtrEntry(vTokens,0), "gate") )
//        return Ioa_ReadParseLineGateBlif( p, vTokens );
    // parse the regular name line
    assert( !strcmp(Vec_PtrEntry(vTokens,0), "names") );
    pNameOut = Vec_PtrEntryLast( vTokens );
/*
    if ( strcmp( pNameOut, "18434" ) == 0 )
    {
        int x = 0;
    }
*/
    pNetOut = Ntl_ModelFindOrCreateNet( p->pNtk, pNameOut );
    // create fanins
    pNode = Ntl_ModelCreateNode( p->pNtk, Vec_PtrSize(vTokens) - 2 );
    for ( i = 0; i < Vec_PtrSize(vTokens) - 2; i++ )
    {
        pNameIn = Vec_PtrEntry(vTokens, i+1);
        pNetIn = Ntl_ModelFindOrCreateNet( p->pNtk, pNameIn );
        Ntl_ObjSetFanin( pNode, pNetIn, i );
    }
    if ( !Ntl_ModelSetNetDriver( pNode, pNetOut ) )
    {
        sprintf( p->pMan->sError, "Line %d: Signal \"%s\" is defined more than once.", Ioa_ReadGetLine(p->pMan, pNameOut), pNameOut );
        return 0;
    }
    // parse the table of this node
    pNode->pSop = Ioa_ReadParseTableBlif( p, pNameOut + strlen(pNameOut), pNode->nFanins );
    if ( pNode->pSop == NULL )
        return 0;
    pNode->pSop = Ntl_ManStoreSop( p->pNtk->pMan, pNode->pSop );
    return 1;
}


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