cbaReadVer.c 91.9 KB
Newer Older
1 2 3 4 5 6
/**CFile****************************************************************

  FileName    [cbaReadVer.c]

  SystemName  [ABC: Logic synthesis and verification system.]

7
  PackageName [Hierarchical word-level netlist.]
8

9
  Synopsis    [BLIF writer.]
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 29, 2014.]

  Revision    [$Id: cbaReadVer.c,v 1.00 2014/11/29 00:00:00 alanmi Exp $]

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

#include "cba.h"
#include "cbaPrs.h"

ABC_NAMESPACE_IMPL_START

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

30
static const char * s_VerTypes[PRS_VER_UNKNOWN+1] = {
31
    NULL,              // 0:  unused
32 33 34 35
    "input",           // 1:  input
    "output",          // 2:  output
    "inout",           // 3:  inout
    "wire",            // 4:  wire
36 37 38
    "reg",             // 5:  reg
    "module",          // 6:  module
    "assign",          // 7:  assign
39
    "always",          // 8:  always
40 41 42 43 44 45 46 47 48
    "function",        // 9:  function
    "defparam",        // 10: defparam
    "begin",           // 11: begin
    "end",             // 12: end
    "case",            // 13: case
    "endcase",         // 14: endcase
    "signed",          // 15: signed
    "endmodule",       // 16: endmodule
    NULL               // 17: unknown 
49 50
};

51
void Prs_NtkAddVerilogDirectives( Prs_Man_t * p )
52 53 54
{
    int i;
    for ( i = 1; s_VerTypes[i]; i++ )
55 56
        Abc_NamStrFindOrAdd( p->pStrs, (char *)s_VerTypes[i],   NULL );
    assert( Abc_NamObjNumMax(p->pStrs) == i );
57 58 59 60
}


// character recognition 
61 62 63 64 65 66 67 68 69 70 71
static inline int Prs_CharIsSpace( char c )   { return (c == ' ' || c == '\t' || c == '\r' || c == '\n');                           }
static inline int Prs_CharIsDigit( char c )   { return (c >= '0' && c <= '9');                                                      }
static inline int Prs_CharIsDigitB( char c )  { return (c == '0' || c == '1'  || c == 'x' || c == 'z');                             }
static inline int Prs_CharIsDigitH( char c )  { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');  }
static inline int Prs_CharIsChar( char c )    { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');                            }
static inline int Prs_CharIsSymb1( char c )   { return Prs_CharIsChar(c) || c == '_';                                               }
static inline int Prs_CharIsSymb2( char c )   { return Prs_CharIsSymb1(c) || Prs_CharIsDigit(c) || c == '$';                        }

static inline int Prs_ManIsChar( Prs_Man_t * p, char c )    { return p->pCur[0] == c;                        }
static inline int Prs_ManIsChar1( Prs_Man_t * p, char c )   { return p->pCur[1] == c;                        }
static inline int Prs_ManIsDigit( Prs_Man_t * p )           { return Prs_CharIsDigit(*p->pCur);              }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
// predefined primitives
typedef struct Prs_VerPrim_t_ Prs_VerPrim_t;
struct Prs_VerPrim_t_
{
    int    Type;
    char * pName;
};
static const Prs_VerPrim_t s_VerilogPrims[16] = 
{
    {CBA_BOX_BUF,  "buf"   },   
    {CBA_BOX_INV,  "not"   },   
    {CBA_BOX_AND,  "and"   },   
    {CBA_BOX_NAND, "nand"  },  
    {CBA_BOX_OR,   "or"    },    
    {CBA_BOX_NOR,  "nor"   },   
    {CBA_BOX_XOR,  "xor"   },   
    {CBA_BOX_XNOR, "xnor"  },  
    {CBA_BOX_TRI,  "bufif1"},  
    {0}
108
};
109 110 111

// predefined operator names
static const char * s_VerNames[100] = 
112
{
113
    NULL,
114
    "VERIFIC_",
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
    "add_",                  
    "mult_",                 
    "div_",                  
    "mod_",                  
    "rem_",                  
    "shift_left_",           
    "shift_right_",          
    "rotate_left_",          
    "rotate_right_",         
    "reduce_and_",           
    "reduce_or_",            
    "reduce_xor_",           
    "reduce_nand_",          
    "reduce_nor_",           
    "reduce_xnor_",          
    "LessThan_",             
    "Mux_",                  
    "Select_",               
    "Decoder_",              
    "EnabledDecoder_",       
    "PrioSelect_",           
    "DualPortRam_",          
    "ReadPort_",             
    "WritePort_",            
    "ClockedWritePort_",     
    "lut",                   
    "and_",                  
    "or_",                   
    "xor_",                  
    "nand_",                 
    "nor_",                  
    "xnor_",                 
    "buf_",                  
    "inv_",                  
    "tri_",                  
    "sub_",                  
    "unary_minus_",          
    "equal_",                
    "not_equal_",            
    "mux_",                  
    "wide_mux_",             
    "wide_select_",          
    "wide_dff_",             
    "wide_dlatch_",          
    "wide_dffrs_",           
    "wide_dlatchrs_",        
    "wide_prio_select_",     
    "pow_",                  
    "PrioEncoder_",          
164 165 166 167
    "abs_",
    "CPL_NMACROFF",
    "CPL_MACROFF",
    "CPL_FF",
168 169 170
    NULL
};

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
typedef struct Prs_VerInfo_t_ Prs_VerInfo_t;
struct Prs_VerInfo_t_
{
    int    Type;
    int    nInputs;
    char * pTypeName;
    char * pSigNames[6];
};
static const Prs_VerInfo_t s_VerInfo[100] = 
{
    {-1,              0, NULL,                     /* "PRIM_NONE"               */ {NULL}},
    {CBA_BOX_CT,      0, "VERIFIC_PWR",            /* "PRIM_PWR"                */ {"o"}},
    {CBA_BOX_CF,      0, "VERIFIC_GND",            /* "PRIM_GND"                */ {"o"}},
    {CBA_BOX_CX,      0, "VERIFIC_X",              /* "PRIM_X"                  */ {"o"}},
    {CBA_BOX_CZ,      0, "VERIFIC_Z",              /* "PRIM_Z"                  */ {"o"}},
    {CBA_BOX_INV,     1, "VERIFIC_INV",            /* "PRIM_INV"                */ {"i","o"}},
    {CBA_BOX_BUF,     1, "VERIFIC_BUF",            /* "PRIM_BUF"                */ {"i","o"}},
    {CBA_BOX_AND,     1, "VERIFIC_AND",            /* "PRIM_AND"                */ {"a0","a1","o"}},
    {CBA_BOX_NAND,    2, "VERIFIC_NAND",           /* "PRIM_NAND"               */ {"a0","a1","o"}},
    {CBA_BOX_OR,      2, "VERIFIC_OR",             /* "PRIM_OR"                 */ {"a0","a1","o"}},
    {CBA_BOX_NOR,     2, "VERIFIC_NOR",            /* "PRIM_NOR"                */ {"a0","a1","o"}},
    {CBA_BOX_XOR,     2, "VERIFIC_XOR",            /* "PRIM_XOR"                */ {"a0","a1","o"}},
    {CBA_BOX_XNOR,    2, "VERIFIC_XNOR",           /* "PRIM_XNOR"               */ {"a0","a1","o"}},
    {CBA_BOX_MUX,     3, "VERIFIC_MUX",            /* "PRIM_MUX"                */ {"c","a1","a0","o"}}, // changed order
    {-1,              0, "VERIFIC_PULLUP",         /* "PRIM_PULLUP"             */ {"o"}},
    {-1,              0, "VERIFIC_PULLDOWN",       /* "PRIM_PULLDOWN"           */ {"o"}},
    {CBA_BOX_TRI,     3, "VERIFIC_TRI",            /* "PRIM_TRI"                */ {"i","c","o"}},
    {CBA_BOX_LATCHRS, 4, "VERIFIC_DLATCHRS",       /* "PRIM_DLATCHRS"           */ {"d","s","r","gate","q"}},                  // changed order
199
    {CBA_BOX_LATCH,   4, "VERIFIC_DLATCH",         /* "PRIM_DLATCH"             */ {"d","async_val","async_cond","gate","q"}}, // changed order
200
    {CBA_BOX_DFFRS,   4, "VERIFIC_DFFRS",          /* "PRIM_DFFRS"              */ {"d","s","r","clk","q"}},                   // changed order
201
    {CBA_BOX_DFF,     4, "VERIFIC_DFF",            /* "PRIM_DFF"                */ {"d","async_val","async_cond","clk","q"}},  // changed order
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
    {-1,              2, "VERIFIC_NMOS",           /* "PRIM_NMOS"               */ {"c","d","o"}},
    {-1,              2, "VERIFIC_PMOS",           /* "PRIM_PMOS"               */ {"c","d","o"}},
    {-1,              3, "VERIFIC_CMOS",           /* "PRIM_CMOS"               */ {"d","nc","pc","o"}},
    {-1,              2, "VERIFIC_TRAN",           /* "PRIM_TRAN"               */ {"inout1","inout2","control"}},
    {CBA_BOX_ADD,     3, "VERIFIC_FADD",           /* "PRIM_FADD"               */ {"cin","a","b","o","cout"}},
    {-1,              3, "VERIFIC_RCMOS",          /* "PRIM_RCMOS"              */ {"d","nc","pc","o"}},
    {-1,              2, "VERIFIC_RNMOS",          /* "PRIM_RNMOS"              */ {"c","d","o"}},
    {-1,              2, "VERIFIC_RPMOS",          /* "PRIM_RPMOS"              */ {"c","d","o"}},
    {-1,              2, "VERIFIC_RTRAN",          /* "PRIM_RTRAN"              */ {"inout1","inout2","control"}},
    {-1,              0, "VERIFIC_HDL_ASSERTION",  /* "PRIM_HDL_ASSERTION"      */ {"condition"}},
    {CBA_BOX_ADD,     3, "add_",                   /* "OPER_ADDER"              */ {"cin","a","b","o","cout"}},
    {CBA_BOX_MUL,     2, "mult_",                  /* "OPER_MULTIPLIER"         */ {"a","b","o"}},
    {CBA_BOX_DIV,     2, "div_",                   /* "OPER_DIVIDER"            */ {"a","b","o"}},
    {CBA_BOX_MOD,     2, "mod_",                   /* "OPER_MODULO"             */ {"a","b","o"}},
    {CBA_BOX_REM,     2, "rem_",                   /* "OPER_REMAINDER"          */ {"a","b","o"}},
    {CBA_BOX_SHIL,    3, "shift_left_",            /* "OPER_SHIFT_LEFT"         */ {"cin","a","amount","o"}},
    {CBA_BOX_SHIR,    3, "shift_right_",           /* "OPER_SHIFT_RIGHT"        */ {"cin","a","amount","o"}},
    {CBA_BOX_ROTL,    2, "rotate_left_",           /* "OPER_ROTATE_LEFT"        */ {"a","amount","o"}},
    {CBA_BOX_ROTR,    2, "rotate_right_",          /* "OPER_ROTATE_RIGHT"       */ {"a","amount","o"}},
    {CBA_BOX_RAND,    1, "reduce_and_",            /* "OPER_REDUCE_AND"         */ {"a","o"}},
    {CBA_BOX_ROR,     1, "reduce_or_",             /* "OPER_REDUCE_OR"          */ {"a","o"}},
    {CBA_BOX_RXOR,    1, "reduce_xor_",            /* "OPER_REDUCE_XOR"         */ {"a","o"}},
    {CBA_BOX_RNAND,   1, "reduce_nand_",           /* "OPER_REDUCE_NAND"        */ {"a","o"}},
    {CBA_BOX_RNOR,    1, "reduce_nor_",            /* "OPER_REDUCE_NOR"         */ {"a","o"}},
    {CBA_BOX_RXNOR,   1, "reduce_xnor_",           /* "OPER_REDUCE_XNOR"        */ {"a","o"}},
    {CBA_BOX_LTHAN,   3, "LessThan_",              /* "OPER_LESSTHAN"           */ {"cin","a","b","o"}},
    {CBA_BOX_NMUX,    2, "Mux_",                   /* "OPER_NTO1MUX"            */ {"sel","data","o"}},
    {CBA_BOX_SEL,     2, "Select_",                /* "OPER_SELECTOR"           */ {"sel","data","o"}},
    {CBA_BOX_DEC,     1, "Decoder_",               /* "OPER_DECODER"            */ {"a","o"}},
    {CBA_BOX_EDEC,    2, "EnabledDecoder_",        /* "OPER_ENABLED_DECODER"    */ {"en","i","o"}},
    {CBA_BOX_PSEL,    3, "PrioSelect_",            /* "OPER_PRIO_SELECTOR"      */ {"cin","sel","data","o"}},
    {CBA_BOX_RAM,     4, "DualPortRam_",           /* "OPER_DUAL_PORT_RAM"      */ {"write_enable","write_address","write_data","read_address","read_data"}},
234 235 236
    {CBA_BOX_RAMR,    3, "ReadPort_",              /* "OPER_READ_PORT"          */ {"read_enable", "read_address", "Ram", "read_data" }},
    {CBA_BOX_RAMW,    3, "WritePort_",             /* "OPER_WRITE_PORT"         */ {"write_enable","write_address","write_data", "Ram"}},
    {CBA_BOX_RAMWC,   4, "ClockedWritePort_",      /* "OPER_CLOCKED_WRITE_PORT" */ {"clk","write_enable","write_address","write_data", "Ram"}},
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    {CBA_BOX_LUT,     1, "lut",                    /* "OPER_LUT"                */ {"i","o"}},
    {CBA_BOX_AND,     2, "and_",                   /* "OPER_WIDE_AND"           */ {"a","b","o"}},
    {CBA_BOX_OR,      2, "or_",                    /* "OPER_WIDE_OR"            */ {"a","b","o"}},
    {CBA_BOX_XOR,     2, "xor_",                   /* "OPER_WIDE_XOR"           */ {"a","b","o"}},
    {CBA_BOX_NAND,    2, "nand_",                  /* "OPER_WIDE_NAND"          */ {"a","b","o"}},
    {CBA_BOX_NOR,     2, "nor_",                   /* "OPER_WIDE_NOR"           */ {"a","b","o"}},
    {CBA_BOX_XNOR,    2, "xnor_",                  /* "OPER_WIDE_XNOR"          */ {"a","b","o"}},
    {CBA_BOX_BUF,     1, "buf_",                   /* "OPER_WIDE_BUF"           */ {"i","o"}},
    {CBA_BOX_INV,     1, "inv_",                   /* "OPER_WIDE_INV"           */ {"i","o"}},
    {CBA_BOX_TRI,     2, "tri_",                   /* "OPER_WIDE_TRI"           */ {"i","c","o"}},
    {CBA_BOX_SUB,     2, "sub_",                   /* "OPER_MINUS"              */ {"a","b","o"}},
    {CBA_BOX_MIN,     1, "unary_minus_",           /* "OPER_UMINUS"             */ {"i","o"}},
    {CBA_BOX_EQU,     2, "equal_",                 /* "OPER_EQUAL"              */ {"a","b","o"}},
    {CBA_BOX_NEQU,    2, "not_equal_",             /* "OPER_NEQUAL"             */ {"a","b","o"}},
    {CBA_BOX_MUX,     3, "mux_",                   /* "OPER_WIDE_MUX"           */ {"cond","d1","d0","o"}}, // changed order
    {CBA_BOX_NMUX,    2, "wide_mux_",              /* "OPER_WIDE_NTO1MUX"       */ {"sel","data","o"}},
    {CBA_BOX_SEL,     2, "wide_select_",           /* "OPER_WIDE_SELECTOR"      */ {"sel","data","o"}},
    {CBA_BOX_DFF,     4, "wide_dff_",              /* "OPER_WIDE_DFF"           */ {"d","async_val","async_cond","clock","q"}},
    {CBA_BOX_DFFRS,   4, "wide_dffrs_",            /* "OPER_WIDE_DFFRS"         */ {"d","set","reset","clock","q"}},
    {CBA_BOX_LATCHRS, 4, "wide_dlatchrs_",         /* "OPER_WIDE_DLATCHRS"      */ {"d","set","reset","clock","q"}},
    {CBA_BOX_LATCH,   4, "wide_dlatch_",           /* "OPER_WIDE_DLATCH"        */ {"d","async_val","async_cond","clock","q"}},
    {CBA_BOX_PSEL,    3, "wide_prio_select_",      /* "OPER_WIDE_PRIO_SELECTOR" */ {"sel","data","carry_in","o"}},
    {CBA_BOX_POW,     2, "pow_",                   /* "OPER_POW"                */ {"a","b","o"}},
    {CBA_BOX_PENC,    1, "PrioEncoder_",           /* "OPER_PRIO_ENCODER"       */ {"sel","o"}},
261
    {CBA_BOX_ABS,     1, "abs_",                   /* "OPER_ABS"                */ {"i","o"}},
262
    {CBA_BOX_DFFCPL,  4, "CPL_FF",                 /* "OPER_WIDE_DFF - 2"       */ {"d","arstval","arst","clk","q","qbar"}},
263 264 265 266
    {-1,              0, NULL,                     /* "PRIM_END"                */ {NULL}}
}; 


267
// check if it is a Verilog predefined module
268
static inline int Prs_ManIsVerilogPrim( char * pName )
269 270
{
    int i;
271 272 273
    for ( i = 0; s_VerilogPrims[i].pName; i++ )
        if ( !strcmp(pName, s_VerilogPrims[i].pName) )
            return s_VerilogPrims[i].Type;
274 275
    return 0;
}
276
// check if it is a known module
277
static inline int Prs_ManIsKnownModule( char * pName )
278
{
279
    int i, Length;
280
    for ( i = 1; s_VerNames[i]; i++ )
281 282 283 284
    {
        Length = strlen(s_VerNames[i]);
//        if ( !strncmp(pName, s_VerNames[i], Length) && (i == 1 || (pName[Length] >= '0' && pName[Length] <= '9')) )
        if ( !strncmp(pName, s_VerNames[i], Length) )
Alan Mishchenko committed
285
            return i;
286
    }
287 288
    return 0;
}
289 290 291
// check if it is a known module
static inline int Prs_ManFindType( char * pName, int * pInputs, int fOut, char *** ppNames )
{
292
    int i, Length;
293 294
    *pInputs = -1;
    for ( i = 1; s_VerInfo[i].pTypeName; i++ )
295 296 297
    {
        Length = strlen(s_VerInfo[i].pTypeName);
        if ( !strncmp(pName, s_VerInfo[i].pTypeName, Length) )
298 299 300 301 302
        {
            *pInputs = s_VerInfo[i].nInputs;
            *ppNames = (char **)s_VerInfo[i].pSigNames + (fOut ? s_VerInfo[i].nInputs : 0);
            return s_VerInfo[i].Type;
        }
303
    }
304 305
    return CBA_OBJ_BOX;
}
306 307 308 309 310 311 312 313 314 315 316 317 318

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

Alan Mishchenko committed
319
// skips Verilog comments (returns 1 if some comments were skipped)
320
static inline int Prs_ManUtilSkipComments( Prs_Man_t * p )
Alan Mishchenko committed
321
{
322
    if ( !Prs_ManIsChar(p, '/') )
Alan Mishchenko committed
323
        return 0;
324
    if ( Prs_ManIsChar1(p, '/') )
Alan Mishchenko committed
325 326
    {
        for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
327
            if ( Prs_ManIsChar(p, '\n') )
Alan Mishchenko committed
328 329
                { p->pCur++; return 1; }
    }
330
    else if ( Prs_ManIsChar1(p, '*') )
Alan Mishchenko committed
331 332
    {
        for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
333
            if ( Prs_ManIsChar(p, '*') && Prs_ManIsChar1(p, '/') )
Alan Mishchenko committed
334 335 336 337
                { p->pCur++; p->pCur++; return 1; }
    }
    return 0;
}
338
static inline int Prs_ManUtilSkipName( Prs_Man_t * p )
Alan Mishchenko committed
339
{
340
    if ( !Prs_ManIsChar(p, '\\') )
Alan Mishchenko committed
341 342
        return 0;
    for ( p->pCur++; p->pCur < p->pLimit; p->pCur++ )
343
        if ( Prs_ManIsChar(p, ' ') )
Alan Mishchenko committed
344 345 346 347
            { p->pCur++; return 1; }
    return 0;
}

348
// skip any number of spaces and comments
349
static inline int Prs_ManUtilSkipSpaces( Prs_Man_t * p )
350
{
Alan Mishchenko committed
351
    while ( p->pCur < p->pLimit )
352
    {
353
        while ( Prs_CharIsSpace(*p->pCur) ) 
354
            p->pCur++;
Alan Mishchenko committed
355
        if ( !*p->pCur )
356 357
            return Prs_ManErrorSet(p, "Unexpectedly reached end-of-file.", 1);
        if ( !Prs_ManUtilSkipComments(p) )
Alan Mishchenko committed
358
            return 0;
359
    }
360
    return Prs_ManErrorSet(p, "Unexpectedly reached end-of-file.", 1);
361 362
}
// skip everything including comments until the given char
363
static inline int Prs_ManUtilSkipUntil( Prs_Man_t * p, char c )
364
{
Alan Mishchenko committed
365
    while ( p->pCur < p->pLimit )
366
    {
367
        if ( Prs_ManIsChar(p, c) )
368
            return 1;
369
        if ( Prs_ManUtilSkipComments(p) )
Alan Mishchenko committed
370
            continue;
371
        if ( Prs_ManUtilSkipName(p) )
Alan Mishchenko committed
372
            continue;
373 374 375 376 377
        p->pCur++;
    }
    return 0;
}
// skip everything including comments until the given word
378
static inline int Prs_ManUtilSkipUntilWord( Prs_Man_t * p, char * pWord )
379 380
{
    char * pPlace = strstr( p->pCur, pWord );
Alan Mishchenko committed
381 382 383
    if ( pPlace == NULL )  return 1;
    p->pCur = pPlace + strlen(pWord);
    return 0;
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
// detect two symbols on the same line
static inline int Prs_ManUtilDetectTwo( Prs_Man_t * p, char Sym1, char Sym2 )
{
    char * pTemp;
    for ( pTemp = p->pCur; *pTemp != ';'; pTemp++ )
        if ( *pTemp == Sym1 && *pTemp == Sym2 )
            return 1;
    return 0;
}
// find closing paren
static inline char * Prs_ManFindClosingParenthesis( Prs_Man_t * p, char Open, char Close )
{
    char * pTemp;
    int Counter = 0;
    int fNotName = 1;
    assert( Prs_ManIsChar(p, Open) );
    for ( pTemp = p->pCur; *pTemp; pTemp++ )
    {
        if ( fNotName )
        {
            if ( *pTemp == Open )
                Counter++;
            if ( *pTemp == Close )
                Counter--;
            if ( Counter == 0 )
                return pTemp;
        }
        if ( *pTemp == '\\' )
            fNotName = 0;
        else if ( !fNotName && *pTemp == ' ' )
            fNotName = 1;
    }
    return NULL;
}
419 420 421 422 423 424 425 426 427 428 429 430

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
431
static inline int Prs_ManReadName( Prs_Man_t * p )
432 433
{
    char * pStart = p->pCur;
434
    if ( Prs_ManIsChar(p, '\\') ) // escaped name
435 436
    {
        pStart = ++p->pCur;
437
        while ( !Prs_ManIsChar(p, ' ') ) 
Alan Mishchenko committed
438 439
            p->pCur++;
    }    
440
    else if ( Prs_CharIsSymb1(*p->pCur) ) // simple name
Alan Mishchenko committed
441 442
    {
        p->pCur++;
443
        while ( Prs_CharIsSymb2(*p->pCur) ) 
444 445
            p->pCur++;
    }
Alan Mishchenko committed
446 447
    else 
        return 0;
448
    return Abc_NamStrFindOrAddLim( p->pStrs, pStart, p->pCur, NULL );
Alan Mishchenko committed
449
}
450 451 452 453 454 455 456
static inline int Prs_ManReadNameList( Prs_Man_t * p, Vec_Int_t * vTemp, char LastSymb )
{
    Vec_IntClear( vTemp );
    while ( 1 )
    {
        int Item = Prs_ManReadName(p);
        if ( Item == 0 )                    return Prs_ManErrorSet(p, "Cannot read name in the list.", 0);
457 458 459
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 1.", 0);
        if ( Item == PRS_VER_WIRE  )
            continue;
460 461 462 463
        Vec_IntPush( vTemp, Item );
        if ( Prs_ManIsChar(p, LastSymb) )   break;
        if ( !Prs_ManIsChar(p, ',') )       return Prs_ManErrorSet(p, "Expecting comma in the list.", 0);
        p->pCur++;
464
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 2.", 0);
465 466 467 468
    }
    return 1;
}
static inline int Prs_ManReadConstant( Prs_Man_t * p )
Alan Mishchenko committed
469 470
{
    char * pStart = p->pCur;
471 472
    assert( Prs_ManIsDigit(p) );
    while ( Prs_ManIsDigit(p) ) 
Alan Mishchenko committed
473
        p->pCur++;
474 475
    if ( !Prs_ManIsChar(p, '\'') )
        return Abc_NamStrFindOrAddLim( p->pFuns, pStart, p->pCur, NULL );
Alan Mishchenko committed
476
    p->pCur++;
477 478
    if ( Prs_ManIsChar(p, 's') )
        p->pCur++;
479
    if ( Prs_ManIsChar(p, 'b') )
480
    {
Alan Mishchenko committed
481
        p->pCur++;
482
        while ( Prs_CharIsDigitB(*p->pCur) ) 
483 484 485 486 487 488 489 490 491
        {
            if ( *p->pCur == '0' )
                p->pNtk->fHasC0s = 1;
            else if ( *p->pCur == '1' )
                p->pNtk->fHasC1s = 1;
            else if ( *p->pCur == 'x' )
                p->pNtk->fHasCXs = 1;
            else if ( *p->pCur == 'z' )
                p->pNtk->fHasCZs = 1;
492
            p->pCur++;
493
        }
Alan Mishchenko committed
494
    }
495
    else if ( Prs_ManIsChar(p, 'h') )
Alan Mishchenko committed
496
    {
497
        p->pCur++;
498
        p->pNtk->fHasC0s = 1;
499
        while ( Prs_CharIsDigitH(*p->pCur) ) 
500 501 502
        {
            if ( *p->pCur != '0' )
                p->pNtk->fHasC1s = 1;
Alan Mishchenko committed
503
            p->pCur++;
504
        }
505
    }
506
    else if ( Prs_ManIsChar(p, 'd') )
507
    {
Alan Mishchenko committed
508
        p->pCur++;
509
        p->pNtk->fHasC0s = 1;
510
        while ( Prs_ManIsDigit(p) ) 
511 512 513
        {
            if ( *p->pCur != '0' )
                p->pNtk->fHasC1s = 1;
514
            p->pCur++;
515
        }
516
    }
517
    else                                    return Prs_ManErrorSet(p, "Cannot read radix of constant.", 0);
518
    return Abc_NamStrFindOrAddLim( p->pFuns, pStart, p->pCur, NULL );
519
}
520
static inline int Prs_ManReadRange( Prs_Man_t * p )
521
{
522
    int Left, Right;
523
    assert( Prs_ManIsChar(p, '[') );
524
    p->pCur++;
525
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 3.", 0);
526
    if ( !Prs_ManIsDigit(p) )               return Prs_ManErrorSet(p, "Cannot read digit in range specification.", 0);
527 528 529
    Left = Right = atoi(p->pCur);
    while ( Prs_ManIsDigit(p) ) 
        p->pCur++;
530
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 4.", 0);
531
    if ( Prs_ManIsChar(p, ':') )
532
    {
533
        p->pCur++;
534
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 5.", 0);
535
        if ( !Prs_ManIsDigit(p) )           return Prs_ManErrorSet(p, "Cannot read digit in range specification.", 0);
536 537 538
        Right = atoi(p->pCur);
        while ( Prs_ManIsDigit(p) ) 
            p->pCur++;
539
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 6.", 0);
540
    }
541
    if ( !Prs_ManIsChar(p, ']') )           return Prs_ManErrorSet(p, "Cannot read closing brace in range specification.", 0);
542
    p->pCur++;
543
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 6a.", 0);
544
    return Hash_Int2ManInsert( p->vHash, Left, Right, 0 );
545
}
546
static inline int Prs_ManReadConcat( Prs_Man_t * p, Vec_Int_t * vTemp2 )
547
{
548 549
    extern int Prs_ManReadSignalList( Prs_Man_t * p, Vec_Int_t * vTemp, char LastSymb, int fAddForm );
    assert( Prs_ManIsChar(p, '{') );
Alan Mishchenko committed
550
    p->pCur++;
551
    if ( !Prs_ManReadSignalList( p, vTemp2, '}', 0 ) ) return Prs_ManErrorSet(p, "Error number 7.", 0);
Alan Mishchenko committed
552
    // check final
553
    assert( Prs_ManIsChar(p, '}') );
Alan Mishchenko committed
554 555
    p->pCur++;
    // return special case
556 557 558 559
    assert( Vec_IntSize(vTemp2) > 0 );
    if ( Vec_IntSize(vTemp2) == 1 )
        return Vec_IntEntry(vTemp2, 0);
    return Abc_Var2Lit2( Prs_NtkAddConcat(p->pNtk, vTemp2), CBA_PRS_CONCAT );
Alan Mishchenko committed
560
}
561
static inline int Prs_ManReadSignal( Prs_Man_t * p )
Alan Mishchenko committed
562
{
563
    int Item;
564
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 8.", 0);
565
    if ( Prs_ManIsDigit(p) )
Alan Mishchenko committed
566
    {
567
        Item = Prs_ManReadConstant(p);
568
        if ( Item == 0 )                    return 0;
569
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 10.", 0);
570 571 572 573
        return Abc_Var2Lit2( Item, CBA_PRS_CONST );
    }
    if ( Prs_ManIsChar(p, '{') )
    {
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
        if ( Prs_CharIsDigit(p->pCur[1]) )
        {
            p->pCur++;
            if ( Prs_ManIsDigit(p) )
            {
                int i, Num = atoi(p->pCur);
                while ( Prs_ManIsDigit(p) )
                    p->pCur++;
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 10.", 0);
                assert( Prs_ManIsChar(p, '{') );
                p->pCur++;
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 10.", 0);
                Item = Prs_ManReadSignal( p );
                assert( Prs_ManIsChar(p, '}') );
                p->pCur++;
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 10.", 0);
                // add to concat all, expect the last one
                assert( p->fUsingTemp2 );
                for ( i = 0; i < Num-1; i++ )
                    Vec_IntPush( &p->vTemp2, Item );
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 10.", 0);
                assert( Prs_ManIsChar(p, '}') );
                p->pCur++;
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 10.", 0);
                return Item;
            }
        }
601 602 603 604
        if ( p->fUsingTemp2 )               return Prs_ManErrorSet(p, "Cannot read nested concatenations.", 0);
        p->fUsingTemp2 = 1;
        Item = Prs_ManReadConcat(p, &p->vTemp2);
        p->fUsingTemp2 = 0;
605
        if ( Item == 0 )                    return 0;
606
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 12.", 0);
607
        return Item;
Alan Mishchenko committed
608 609 610
    }
    else
    {
611
        Item = Prs_ManReadName( p );
612
        if ( Item == 0 )                    return 1; // no actual name               
613
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 14.", 0);
614 615 616
        if ( Prs_ManIsChar(p, '[') )
        {
            int Range = Prs_ManReadRange(p);
617 618
            if ( Range == 0 )               return Prs_ManErrorSet(p, "Error number 15.", 0);
            if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 16.", 0);
619 620 621
            return Abc_Var2Lit2( Prs_NtkAddSlice(p->pNtk, Item, Range), CBA_PRS_SLICE );
        }
        return Abc_Var2Lit2( Item, CBA_PRS_NAME );
Alan Mishchenko committed
622 623
    }
}
624
int Prs_ManReadSignalList( Prs_Man_t * p, Vec_Int_t * vTemp, char LastSymb, int fAddForm )
Alan Mishchenko committed
625 626 627 628
{
    Vec_IntClear( vTemp );
    while ( 1 )
    {
629 630 631 632 633 634 635
        int Item = Prs_ManReadSignal(p);
        if ( Item == 0 )                    return Prs_ManErrorSet(p, "Cannot read signal in the list.", 0);
        if ( fAddForm )
            Vec_IntPush( vTemp, 0 );
        Vec_IntPush( vTemp, Item );
        if ( Prs_ManIsChar(p, LastSymb) )   break;
        if ( !Prs_ManIsChar(p, ',') )       return Prs_ManErrorSet(p, "Expecting comma in the list.", 0);
Alan Mishchenko committed
636 637 638 639
        p->pCur++;
    }
    return 1;
}
640
static inline int Prs_ManReadSignalList2( Prs_Man_t * p, Vec_Int_t * vTemp )
Alan Mishchenko committed
641
{
642
    int FormId, ActItem;
Alan Mishchenko committed
643
    Vec_IntClear( vTemp );
644 645
    assert( Prs_ManIsChar(p, '.') );
    while ( Prs_ManIsChar(p, '.') )
Alan Mishchenko committed
646 647
    {
        p->pCur++;
648 649
        FormId = Prs_ManReadName( p );
        if ( FormId == 0 )                  return Prs_ManErrorSet(p, "Cannot read formal name of the instance.", 0);
650
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 17.", 0);
651
        if ( !Prs_ManIsChar(p, '(') )       return Prs_ManErrorSet(p, "Cannot read \"(\" in the instance.", 0);
Alan Mishchenko committed
652
        p->pCur++;
653
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 17.", 0);
654
        ActItem = Prs_ManReadSignal( p );
655
        if ( ActItem == 0 )                 return Prs_ManErrorSet(p, "Cannot read actual name of an instance.", 0);
656
        if ( !Prs_ManIsChar(p, ')') )       return Prs_ManErrorSet(p, "Cannot read \")\" in the instance.", 0);
Alan Mishchenko committed
657
        p->pCur++;
658 659
        if ( ActItem != 1 )
            Vec_IntPushTwo( vTemp, FormId, ActItem );
660
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 18.", 0);
661 662
        if ( Prs_ManIsChar(p, ')') )        break;
        if ( !Prs_ManIsChar(p, ',') )       return Prs_ManErrorSet(p, "Expecting comma in the instance.", 0);
Alan Mishchenko committed
663
        p->pCur++;
664
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 19.", 0);
Alan Mishchenko committed
665 666
    }
    assert( Vec_IntSize(vTemp) > 0 );
667
    assert( Vec_IntSize(vTemp) % 2 == 0 );
Alan Mishchenko committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681
    return 1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
682
static inline int Prs_ManReadFunction( Prs_Man_t * p )
683
{
684 685 686 687 688 689 690 691 692 693 694
    // this is a hack to read functions produced by ABC Verilog writer
    p->FuncNameId = p->FuncRangeId = 0;
    if ( Prs_ManUtilSkipUntilWord( p, "_func_" ) ) return Prs_ManErrorSet(p, "Cannot find \"_func_\" keyword.", 0);
    p->pCur -= 6;
    p->FuncNameId = Prs_ManReadName( p );          
    if ( p->FuncNameId == 0 )                      return Prs_ManErrorSet(p, "Error number 30a.", 0);
    if ( Prs_ManUtilSkipUntilWord( p, "input" ) )  return Prs_ManErrorSet(p, "Cannot find \"input\" keyword.", 0);
    if ( Prs_ManUtilSkipSpaces(p) )                return Prs_ManErrorSet(p, "Error number 30b.", 0);
    if ( Prs_ManIsChar(p, '[') )
        p->FuncRangeId = Prs_ManReadRange(p);
    else if ( Prs_ManReadName(p) == PRS_VER_SIGNED ) 
695
    {
696 697 698
        if ( Prs_ManUtilSkipSpaces(p) )            return Prs_ManErrorSet(p, "Error number 30c.", 0);
        if ( Prs_ManIsChar(p, '[') )
            p->FuncRangeId = Prs_ManReadRange(p);
699
    }
700
    if ( Prs_ManUtilSkipUntilWord( p, "endfunction" ) ) return Prs_ManErrorSet(p, "Cannot find \"endfunction\" keyword.", 0);
701 702
    return 1;
}
703
static inline int Prs_ManReadAlways( Prs_Man_t * p )
704
{
705 706 707
    // this is a hack to read always-statement representing case-statement
    int iToken;
    char * pClose; 
708
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23.", 0);
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
    if ( !Prs_ManIsChar(p, '@') )           return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    p->pCur++;
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    if ( !Prs_ManIsChar(p, '(') )           return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    pClose = Prs_ManFindClosingParenthesis( p, '(', ')' );
    if ( pClose == NULL )
        return Prs_ManErrorSet(p, "Expecting closing parenthesis 1.", 0); 
    p->pCur = pClose;
    if ( !Prs_ManIsChar(p, ')') )           return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    p->pCur++;
    // read begin
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    iToken = Prs_ManReadName( p );
    if ( iToken != PRS_VER_BEGIN )          return Prs_ManErrorSet(p, "Cannot read \"begin\" keyword.", 0);
    // read case
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    iToken = Prs_ManReadName( p );
    if ( iToken != PRS_VER_CASE )           return Prs_ManErrorSet(p, "Cannot read \"case\" keyword.", 0);
    // read control
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    if ( !Prs_ManIsChar(p, '(') )           return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    p->pCur++;
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    iToken = Prs_ManReadSignal( p );
    if ( iToken == 0 )                      return Prs_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    if ( !Prs_ManIsChar(p, ')') )           return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    p->pCur++;
    // save control
    Vec_IntClear( &p->vTemp3 );
    Vec_IntPushTwo( &p->vTemp3, 0, 0 );  // output will go here
    Vec_IntPushTwo( &p->vTemp3, 0, iToken );
    // read conditions
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    if ( !Prs_ManIsDigit(p) )               return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
    while ( Prs_ManIsDigit(p) )
    {
        while ( Prs_ManIsDigit(p) )
            p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
        if ( !Prs_ManIsChar(p, ':') )       return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
        p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
        // read output
        iToken = Prs_ManReadSignal( p );
        if ( iToken == 0 )                  return Prs_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
        if ( !Prs_ManIsChar(p, '=') )       return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
        p->pCur++;
        // save output
        Vec_IntWriteEntry( &p->vTemp3, 1, iToken );
        // read input
        iToken = Prs_ManReadSignal( p );
        if ( iToken == 0 )                  return Prs_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
        if ( !Prs_ManIsChar(p, ';') )       return Prs_ManErrorSet(p, "Cannot parse always statement.", 0);
        p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
        // save input
        Vec_IntPushTwo( &p->vTemp3, 0, iToken );
    }
    // read endcase
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    iToken = Prs_ManReadName( p );
    if ( iToken != PRS_VER_ENDCASE )        return Prs_ManErrorSet(p, "Cannot read \"endcase\" keyword.", 0);
    // read end
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23a.", 0);
    iToken = Prs_ManReadName( p );
    if ( iToken != PRS_VER_END )            return Prs_ManErrorSet(p, "Cannot read \"end\" keyword.", 0);
    // save binary operator
    Prs_NtkAddBox( p->pNtk, CBA_BOX_NMUX, 0, &p->vTemp3 );
    return 1;
}
/*
static inline int Prs_ManReadExpression( Prs_Man_t * p, int OutItem )
{
    int InItem, fCompl = 0, fCompl2 = 0, Oper = 0;
    // read output name
787
    if ( Prs_ManIsChar(p, '~') ) 
Alan Mishchenko committed
788 789 790 791
    { 
        fCompl = 1; 
        p->pCur++; 
    }
792 793 794 795
    // write output name
    Vec_IntClear( &p->vTemp );
    Vec_IntPush( &p->vTemp, 0 );
    Vec_IntPush( &p->vTemp, OutItem );
Alan Mishchenko committed
796
    // read first name
797 798 799 800
    InItem = Prs_ManReadSignal( p );
    if ( InItem == 0 )                      return Prs_ManErrorSet(p, "Cannot read first input name in the assign-statement.", 0);
    Vec_IntPush( &p->vTemp, 0 );
    Vec_IntPush( &p->vTemp, InItem );
Alan Mishchenko committed
801
    // check unary operator
802
    if ( Prs_ManIsChar(p, ';') )
Alan Mishchenko committed
803
    {
804 805
        Oper = fCompl ? CBA_BOX_INV : CBA_BOX_BUF;
        Prs_NtkAddBox( p->pNtk, Oper, 0, &p->vTemp );
Alan Mishchenko committed
806 807
        return 1;
    }
808 809 810 811 812
    if ( Prs_ManIsChar(p, '&') ) 
        Oper = CBA_BOX_AND;
    else if ( Prs_ManIsChar(p, '|') ) 
        Oper = CBA_BOX_OR;
    else if ( Prs_ManIsChar(p, '^') ) 
813
        Oper = CBA_BOX_XOR;
814 815 816
    else if ( Prs_ManIsChar(p, '?') ) 
        Oper = CBA_BOX_MUX;
    else                                    return Prs_ManErrorSet(p, "Unrecognized operator in the assign-statement.", 0);
Alan Mishchenko committed
817
    p->pCur++; 
818 819 820 821 822 823
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
    if ( Prs_ManIsChar(p, '~') ) 
    { 
        fCompl2 = 1; 
        p->pCur++; 
    }
Alan Mishchenko committed
824
    // read second name
825 826 827 828
    InItem = Prs_ManReadSignal( p );
    if ( InItem == 0 )                      return Prs_ManErrorSet(p, "Cannot read second input name in the assign-statement.", 0);
    Vec_IntPush( &p->vTemp, 0 );
    Vec_IntPush( &p->vTemp, InItem );
Alan Mishchenko committed
829
    // read third argument
830
    if ( Oper == CBA_BOX_MUX )
Alan Mishchenko committed
831 832
    {
        assert( fCompl == 0 ); 
833
        if ( !Prs_ManIsChar(p, ':') )       return Prs_ManErrorSet(p, "Expected colon in the MUX assignment.", 0);
Alan Mishchenko committed
834 835
        p->pCur++; 
        // read third name
836 837 838 839 840
        InItem = Prs_ManReadSignal( p );
        if ( InItem == 0 )                  return Prs_ManErrorSet(p, "Cannot read third input name in the assign-statement.", 0);
        Vec_IntPush( &p->vTemp, 0 );
        Vec_IntPush( &p->vTemp, InItem );
        if ( !Prs_ManIsChar(p, ';') )       return Prs_ManErrorSet(p, "Expected semicolon at the end of the assign-statement.", 0);
Alan Mishchenko committed
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
    else
    {
        // figure out operator
        if ( Oper == CBA_BOX_AND )
        {
            if ( fCompl && !fCompl2 )
                Oper = CBA_BOX_SHARPL;
            else if ( !fCompl && fCompl2 )
                Oper = CBA_BOX_SHARP;
            else if ( fCompl && fCompl2 )
                Oper = CBA_BOX_NOR;
        }
        else if ( Oper == CBA_BOX_OR )
        {
            if ( fCompl && fCompl2 )
                Oper = CBA_BOX_NAND;
            else assert( !fCompl && !fCompl2 );
        }
        else if ( Oper == CBA_BOX_XOR )
        {
            if ( fCompl && !fCompl2 )
                Oper = CBA_BOX_XNOR;
            else assert( !fCompl && !fCompl2 );
        }
    }
867
    // save binary operator
868
    Prs_NtkAddBox( p->pNtk, Oper, 0, &p->vTemp );
Alan Mishchenko committed
869
    return 1;
870
}
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 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 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
*/
static inline int Prs_ManReadExpression( Prs_Man_t * p, int OutItem )
{
    char * pClose;
    int Item, Type = CBA_OBJ_NONE;
    int fRotating = 0;

    // write output name
    Vec_IntClear( &p->vTemp );
    Vec_IntPush( &p->vTemp, 0 );
    Vec_IntPush( &p->vTemp, OutItem );
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
    if ( Prs_ManIsChar(p, '(') )
    {
        // THIS IS A HACK TO DETECT rotating shifters:  try to find both << and >> on the same line
        if ( Prs_ManUtilDetectTwo(p, '>', '>') && Prs_ManUtilDetectTwo(p, '<', '<') )
            fRotating = 1;
        pClose = Prs_ManFindClosingParenthesis( p, '(', ')' );
        if ( pClose == NULL )
            return Prs_ManErrorSet(p, "Expecting closing parenthesis 1.", 0); 
        *p->pCur = *pClose = ' ';
    }
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
    // read constant or concatenation
    if ( Prs_ManIsDigit(p) || Prs_ManIsChar(p, '{') )
    {
        Item = Prs_ManReadSignal( p );
        // write constant
        Vec_IntPush( &p->vTemp, 0 );
        Vec_IntPush( &p->vTemp, Item );
        Type = CBA_BOX_BUF;
    }
    else if ( Prs_ManIsChar(p, '!') || Prs_ManIsChar(p, '~') || Prs_ManIsChar(p, '@') ||
              Prs_ManIsChar(p, '&') || Prs_ManIsChar(p, '|') || Prs_ManIsChar(p, '^') || Prs_ManIsChar(p, '-') )
    {
        if ( Prs_ManIsChar(p, '!') )
            Type = CBA_BOX_LNOT;
        else if ( Prs_ManIsChar(p, '~') )
            Type = CBA_BOX_INV;
        else if ( Prs_ManIsChar(p, '@') )
            Type = CBA_BOX_SQRT;
        else if ( Prs_ManIsChar(p, '&') )
            Type = CBA_BOX_RAND;
        else if ( Prs_ManIsChar(p, '|') )
            Type = CBA_BOX_ROR;
        else if ( Prs_ManIsChar(p, '^') )
            Type = CBA_BOX_RXOR;
        else if ( Prs_ManIsChar(p, '-') )
            Type = CBA_BOX_MIN;
        else assert( 0 );
        p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
        // skip parentheses
        if ( Prs_ManIsChar(p, '(') )
        {
            pClose = Prs_ManFindClosingParenthesis( p, '(', ')' );
            if ( pClose == NULL )
                return Prs_ManErrorSet(p, "Expecting closing parenthesis 2.", 0); 
            *p->pCur = *pClose = ' ';
        }
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
        // read first name
        Item = Prs_ManReadSignal( p );
        if ( Item == 0 )                        return Prs_ManErrorSet(p, "Cannot read name after a unary operator.", 0);
        Vec_IntPush( &p->vTemp, 0 );
        Vec_IntPush( &p->vTemp, Item );
    }
    else
    {
        // read first name
        Item = Prs_ManReadSignal( p );
        if ( Item == 0 )                        return Prs_ManErrorSet(p, "Cannot read name after a binary operator.", 0);
        // check if this is a recent function
        if ( Abc_Lit2Var2(Item) == p->FuncNameId )
        {
            int Status, nInputs, RangeSize;
            if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 24.", 0);
            if ( !Prs_ManIsChar(p, '(') )       return Prs_ManErrorSet(p, "Error number 24.", 0);
            p->pCur++;
            Status = Prs_ManReadSignalList( p, &p->vTemp, ')', 1 );
            nInputs = Vec_IntSize(&p->vTemp)/2;
            RangeSize = p->FuncRangeId ? Ptr_NtkRangeSize(p->pNtk, p->FuncRangeId) : 1;
            p->FuncNameId = p->FuncRangeId = 0;
            if ( Status == 0 )                  return 0;
            if ( nInputs == 1 )
                Type = CBA_BOX_DEC;
            else if ( nInputs == RangeSize + 1 )
                Type = CBA_BOX_SEL;
            else if ( nInputs == (1 << RangeSize) + 1 )
                Type = CBA_BOX_NMUX;
            else                                return Prs_ManErrorSet(p, "Cannot determine word-level operator.", 0);
            p->pCur++;
            if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 24.", 0);
            // save word-level operator
            Vec_IntInsert( &p->vTemp, 0, 0 );
            Vec_IntInsert( &p->vTemp, 1, OutItem );
            Prs_NtkAddBox( p->pNtk, Type, 0, &p->vTemp );
            return 1;
        }
        Vec_IntPush( &p->vTemp, 0 );
        Vec_IntPush( &p->vTemp, Item );
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 24.", 0);
        assert( !Prs_ManIsChar(p, '[') );

        // get the next symbol
        if ( Prs_ManIsChar(p, ',') || Prs_ManIsChar(p, ';') )
            Type = CBA_BOX_BUF;
        else if ( Prs_ManIsChar(p, '?') )
        {
            p->pCur++;
            Item = Prs_ManReadSignal( p );
            if ( Item == 0 )                    return 0;
            Vec_IntPush( &p->vTemp, 0 );
            Vec_IntPush( &p->vTemp, Item );
            if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 24.", 0);
            if ( !Prs_ManIsChar(p, ':') )       return Prs_ManErrorSet(p, "MUX lacks the colon symbol (:).", 0);

            p->pCur++;
            Item = Prs_ManReadSignal( p );
            if ( Item == 0 )                    return 0;
            Vec_IntPush( &p->vTemp, 0 );
            Vec_IntPush( &p->vTemp, Item );
            if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 24.", 0);
            assert( Vec_IntSize(&p->vTemp) == 8 );
            //ABC_SWAP( int, Vec_IntArray(&p->vTemp)[3], Vec_IntArray(&p->vTemp)[5] );
            Type = CBA_BOX_MUX;
        }
        else 
        {
                 if ( p->pCur[0] == '>' && p->pCur[1] == '>' && p->pCur[2] != '>' )  p->pCur += 2, Type = fRotating ? CBA_BOX_ROTR : CBA_BOX_SHIR;
            else if ( p->pCur[0] == '>' && p->pCur[1] == '>' && p->pCur[2] == '>' )  p->pCur += 3, Type = CBA_BOX_SHIRA;      
            else if ( p->pCur[0] == '<' && p->pCur[1] == '<' && p->pCur[2] != '<' )  p->pCur += 2, Type = fRotating ? CBA_BOX_ROTL : CBA_BOX_SHIL;
            else if ( p->pCur[0] == '<' && p->pCur[1] == '<' && p->pCur[2] == '<' )  p->pCur += 3, Type = CBA_BOX_SHILA;      
            else if ( p->pCur[0] == '&' && p->pCur[1] != '&'                      )  p->pCur += 1, Type = CBA_BOX_AND;       
            else if ( p->pCur[0] == '|' && p->pCur[1] != '|'                      )  p->pCur += 1, Type = CBA_BOX_OR;        
            else if ( p->pCur[0] == '^' && p->pCur[1] != '^'                      )  p->pCur += 1, Type = CBA_BOX_XOR;       
            else if ( p->pCur[0] == '&' && p->pCur[1] == '&'                      )  p->pCur += 2, Type = CBA_BOX_LAND;     
            else if ( p->pCur[0] == '|' && p->pCur[1] == '|'                      )  p->pCur += 2, Type = CBA_BOX_LOR;      
            else if ( p->pCur[0] == '=' && p->pCur[1] == '='                      )  p->pCur += 2, Type = CBA_BOX_EQU;      
            else if ( p->pCur[0] == '!' && p->pCur[1] == '='                      )  p->pCur += 2, Type = CBA_BOX_NEQU;      
            else if ( p->pCur[0] == '<' && p->pCur[1] != '='                      )  p->pCur += 1, Type = CBA_BOX_LTHAN;     
            else if ( p->pCur[0] == '>' && p->pCur[1] != '='                      )  p->pCur += 1, Type = CBA_BOX_MTHAN;     
            else if ( p->pCur[0] == '<' && p->pCur[1] == '='                      )  p->pCur += 2, Type = CBA_BOX_LETHAN;
            else if ( p->pCur[0] == '>' && p->pCur[1] == '='                      )  p->pCur += 2, Type = CBA_BOX_METHAN;  
            else if ( p->pCur[0] == '+'                                           )  p->pCur += 1, Type = CBA_BOX_ADD;       
            else if ( p->pCur[0] == '-'                                           )  p->pCur += 1, Type = CBA_BOX_SUB;       
            else if ( p->pCur[0] == '*' && p->pCur[1] != '*'                      )  p->pCur += 1, Type = CBA_BOX_MUL;     
            else if ( p->pCur[0] == '/'                                           )  p->pCur += 1, Type = CBA_BOX_DIV;        
            else if ( p->pCur[0] == '%'                                           )  p->pCur += 1, Type = CBA_BOX_MOD;   
            else if ( p->pCur[0] == '*' && p->pCur[1] == '*'                      )  p->pCur += 2, Type = CBA_BOX_POW;
            else return Prs_ManErrorSet(p, "Unsupported operation.", 0);

            Item = Prs_ManReadSignal( p );
            if ( Item == 0 )                    return 0;
            Vec_IntPush( &p->vTemp, 0 );
            Vec_IntPush( &p->vTemp, Item );
            // for adder insert carry-in
            if ( Type == CBA_BOX_ADD )
                Vec_IntInsert( &p->vTemp, 2, 0 );
            if ( Type == CBA_BOX_ADD )
                Vec_IntInsert( &p->vTemp, 3, 0 );
        }
    }
    if ( Prs_ManUtilSkipSpaces(p) )                              return Prs_ManErrorSet(p, "Error number 24.", 0);
    // make sure there is nothing left there
    if ( fRotating )
    {
        Prs_ManUtilSkipUntilWord(p, ";");
        p->pCur--;
    }
    else if ( !Prs_ManIsChar(p, ',') && !Prs_ManIsChar(p, ';') ) return Prs_ManErrorSet(p, "Trailing symbols on this line.", 0);
    // save binary operator
    Prs_NtkAddBox( p->pNtk, Type, 0, &p->vTemp );
    return 1;
}
static inline int Prs_ManReadDeclaration( Prs_Man_t * p, int Type )
{
    int i, Item = 0, NameId, RangeId = 0, fSigned = 0;
    Vec_Int_t * vNames[4]  = { &p->pNtk->vInputs,  &p->pNtk->vOutputs,  &p->pNtk->vInouts,  &p->pNtk->vWires };
    Vec_Int_t * vNamesR[4] = { &p->pNtk->vInputsR, &p->pNtk->vOutputsR, &p->pNtk->vInoutsR, &p->pNtk->vWiresR };
    assert( Type >= PRS_VER_INPUT && Type <= PRS_VER_WIRE );
    // read first word
    if ( Prs_ManUtilSkipSpaces(p) )                                       return Prs_ManErrorSet(p, "Error number 20.", 0);
    if ( Prs_ManIsChar(p, '[') && !(RangeId = Prs_ManReadRange(p)) )      return Prs_ManErrorSet(p, "Error number 21.", 0);
    Item = Prs_ManReadName(p);
    if ( Item == PRS_VER_SIGNED )
    {
        fSigned = 1;
        if ( Prs_ManUtilSkipSpaces(p) )                                   return Prs_ManErrorSet(p, "Error number 20.", 0);
        if ( Prs_ManIsChar(p, '[') && !(RangeId = Prs_ManReadRange(p)) )  return Prs_ManErrorSet(p, "Error number 21.", 0);
        Item = Prs_ManReadName(p);
    }
    if ( Item == PRS_VER_WIRE )
    {
        if ( Prs_ManUtilSkipSpaces(p) )                                   return Prs_ManErrorSet(p, "Error number 20.", 0);
        if ( Prs_ManIsChar(p, '[') && !(RangeId = Prs_ManReadRange(p)) )  return Prs_ManErrorSet(p, "Error number 21.", 0);
        Item = Prs_ManReadName(p);
    }
    // read variable names
    Vec_IntClear( &p->vTemp3 );
    while ( 1 )
    {
        if ( Item == 0 )                    return Prs_ManErrorSet(p, "Cannot read name in the list.", 0);
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 22a", 0);
        if ( Item == PRS_VER_WIRE  )
            continue;
        Vec_IntPush( &p->vTemp3, Item );
        if ( Prs_ManIsChar(p, '=') )
        {
            if ( Type == PRS_VER_INPUT )    return Prs_ManErrorSet(p, "Input cannot be defined", 0);
            p->pCur++;
            if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 23.", 0);
            if ( !Prs_ManReadExpression(p, Abc_Var2Lit2(Item, CBA_PRS_NAME)) ) 
                return 0;
        }
        if ( Prs_ManIsChar(p, ';') )   
            break;
        if ( !Prs_ManIsChar(p, ',') )       return Prs_ManErrorSet(p, "Expecting comma in the list.", 0);
        p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 22b.", 0);
        Item = Prs_ManReadName(p);
    }
    Vec_IntForEachEntry( &p->vTemp3, NameId, i )
    {
        Vec_IntPush( vNames[Type - PRS_VER_INPUT],  NameId );
        Vec_IntPush( vNamesR[Type - PRS_VER_INPUT], Abc_Var2Lit(RangeId, fSigned) );
        if ( Type < PRS_VER_WIRE )
            Vec_IntPush( &p->pNtk->vOrder, Abc_Var2Lit2(NameId, Type) );
    }
    return 1;
}
1102
static inline int Prs_ManReadInstance( Prs_Man_t * p, int Func )
1103
{
1104
    int InstId, Status;
1105 1106
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 25.", 0);
    if ( Prs_ManIsChar(p, '#') )
1107
    {
1108 1109 1110 1111
        p->pCur++;
        while ( Prs_ManIsDigit(p) )
            p->pCur++;
        if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 25.", 0);
1112 1113
    }
    if ( (InstId = Prs_ManReadName(p)) )
1114
        if (Prs_ManUtilSkipSpaces(p))       return Prs_ManErrorSet(p, "Error number 26.", 0);
1115
    if ( !Prs_ManIsChar(p, '(') )           return Prs_ManErrorSet(p, "Expecting \"(\" in module instantiation.", 0);
Alan Mishchenko committed
1116
    p->pCur++;
1117
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 27.", 0);
1118 1119 1120
    if ( Prs_ManIsChar(p, '.') ) // box
        Status = Prs_ManReadSignalList2(p, &p->vTemp);
    else  // node
1121
    {
1122 1123
        //char * s = Abc_NamStr(p->pStrs, Func);
        // translate elementary gate
1124
        int iFuncNew = Prs_ManIsVerilogPrim(Abc_NamStr(p->pStrs, Func));
1125
        if ( iFuncNew == 0 )                return Prs_ManErrorSet(p, "Cannot find elementary gate.", 0);
Alan Mishchenko committed
1126
        Func = iFuncNew;
1127
        Status = Prs_ManReadSignalList( p, &p->vTemp, ')', 1 );
1128
    }
1129
    if ( Status == 0 )                      return Prs_ManErrorSet(p, "Error number 28.", 0);
1130 1131
    assert( Prs_ManIsChar(p, ')') );
    p->pCur++;
1132
    if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 29.", 0);
1133 1134 1135 1136 1137 1138 1139 1140
    if ( !Prs_ManIsChar(p, ';') )           return Prs_ManErrorSet(p, "Expecting semicolon in the instance.", 0);
    // add box 
    Prs_NtkAddBox( p->pNtk, Func, InstId, &p->vTemp );
    return 1;
}
static inline int Prs_ManReadArguments( Prs_Man_t * p )
{
    int iRange = 0, iType = -1;
1141 1142
    Vec_Int_t * vSigs[3]  = { &p->pNtk->vInputs,  &p->pNtk->vOutputs,  &p->pNtk->vInouts  };
    Vec_Int_t * vSigsR[3] = { &p->pNtk->vInputsR, &p->pNtk->vOutputsR, &p->pNtk->vInoutsR };
1143 1144
    assert( Prs_ManIsChar(p, '(') );
    p->pCur++;
1145
    if ( Prs_ManUtilSkipSpaces(p) )             return Prs_ManErrorSet(p, "Error number 30.", 0);
1146 1147
    if ( Prs_ManIsChar(p, ')') )
        return 1;
1148 1149
    while ( 1 )
    {
1150
        int fEscape = Prs_ManIsChar(p, '\\');
1151
        int iName = Prs_ManReadName( p );
1152
        int fSigned = 0;
1153 1154
        if ( iName == 0 )                       return Prs_ManErrorSet(p, "Error number 31.", 0);
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 32.", 0);
1155
        if ( iName >= PRS_VER_INPUT && iName <= PRS_VER_INOUT && !fEscape ) // declaration
1156 1157 1158 1159 1160
        {
            iType = iName;
            if ( Prs_ManIsChar(p, '[') )
            {
                iRange = Prs_ManReadRange(p);
1161 1162
                if ( iRange == 0 )              return Prs_ManErrorSet(p, "Error number 33.", 0);
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 34.", 0);
1163
            }
1164
            iName = Prs_ManReadName( p );
1165
            if ( iName == 0 )                   return Prs_ManErrorSet(p, "Error number 35.", 0);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
            if ( iName == PRS_VER_SIGNED )
            {
                fSigned = 1;
                if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 32.", 0);
                if ( Prs_ManIsChar(p, '[') )
                {
                    iRange = Prs_ManReadRange(p);
                    if ( iRange == 0 )              return Prs_ManErrorSet(p, "Error number 33.", 0);
                    if ( Prs_ManUtilSkipSpaces(p) ) return Prs_ManErrorSet(p, "Error number 34.", 0);
                }
                iName = Prs_ManReadName( p );
                if ( iName == 0 )               return Prs_ManErrorSet(p, "Error number 35.", 0);
            }
1179 1180 1181
        }
        if ( iType > 0 )
        {
1182
            Vec_IntPush( vSigs[iType - PRS_VER_INPUT], iName );
1183
            Vec_IntPush( vSigsR[iType - PRS_VER_INPUT], Abc_Var2Lit(iRange, fSigned) );
1184
            Vec_IntPush( &p->pNtk->vOrder, Abc_Var2Lit2(iName, iType) );
1185
        }
1186
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 36.", 0);
1187 1188 1189 1190
        if ( Prs_ManIsChar(p, ')') )
            break;
        if ( !Prs_ManIsChar(p, ',') )           return Prs_ManErrorSet(p, "Expecting comma in the instance.", 0);
        p->pCur++;
1191
        if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 36.", 0);
1192 1193 1194
    }
    // check final
    assert( Prs_ManIsChar(p, ')') );
Alan Mishchenko committed
1195
    return 1;
1196 1197
}
// this procedure can return:
Alan Mishchenko committed
1198
// 0 = reached end-of-file; 1 = successfully parsed; 2 = recognized as primitive; 3 = failed and skipped; 4 = error (failed and could not skip)
1199
static inline int Prs_ManReadModule( Prs_Man_t * p )
1200
{
1201
    int iToken, Status, fAlways = 0;
1202 1203
    if ( p->pNtk != NULL )                  return Prs_ManErrorSet(p, "Parsing previous module is unfinished.", 4);
    if ( Prs_ManUtilSkipSpaces(p) )
Alan Mishchenko committed
1204
    { 
1205
        Prs_ManErrorClear( p );       
Alan Mishchenko committed
1206 1207
        return 0; 
    }
1208
    // read keyword
1209 1210 1211 1212 1213 1214 1215 1216 1217
    while ( Prs_ManIsChar(p, '`') )
    {
        Prs_ManUtilSkipUntilWord(p, "\n");
        if ( Prs_ManUtilSkipSpaces(p) )
        { 
            Prs_ManErrorClear( p );       
            return 0; 
        }
    }
1218 1219 1220
    iToken = Prs_ManReadName( p );
    if ( iToken != PRS_VER_MODULE )         return Prs_ManErrorSet(p, "Cannot read \"module\" keyword.", 4);
    if ( Prs_ManUtilSkipSpaces(p) )         return 4;
1221
    // read module name
1222 1223
    iToken = Prs_ManReadName( p );
    if ( iToken == 0 )                      return Prs_ManErrorSet(p, "Cannot read module name.", 4);
1224
    if ( Prs_ManIsKnownModule(Abc_NamStr(p->pStrs, iToken)) )
Alan Mishchenko committed
1225
    {
1226 1227 1228
        if ( Prs_ManUtilSkipUntilWord( p, "endmodule" ) ) return Prs_ManErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
        //printf( "Warning! Skipped known module \"%s\".\n", Abc_NamStr(p->pStrs, iToken) );
        Vec_IntPush( &p->vKnown, iToken );
Alan Mishchenko committed
1229 1230
        return 2;
    }
1231
    Prs_ManInitializeNtk( p, iToken, 1 );
1232
    // skip arguments
1233 1234 1235
    if ( Prs_ManUtilSkipSpaces(p) )         return 4;
    if ( !Prs_ManIsChar(p, '(') )           return Prs_ManErrorSet(p, "Cannot find \"(\" in the argument declaration.", 4);
    if ( !Prs_ManReadArguments(p) )         return 4;
1236 1237
    assert( *p->pCur == ')' );
    p->pCur++;
1238
    if ( Prs_ManUtilSkipSpaces(p) )         return 4;
1239
    // read declarations and instances
1240
    while ( Prs_ManIsChar(p, ';') || fAlways )
1241
    {
1242 1243
        if ( !fAlways ) p->pCur++;
        fAlways = 0;
1244 1245 1246
        if ( Prs_ManUtilSkipSpaces(p) )     return 4;
        iToken = Prs_ManReadName( p );
        if ( iToken == PRS_VER_ENDMODULE )
1247
        {
1248 1249
            Vec_IntPush( &p->vSucceeded, p->pNtk->iModuleName );
            Prs_ManFinalizeNtk( p );
Alan Mishchenko committed
1250
            return 1;
1251
        }
1252 1253
        if ( iToken >= PRS_VER_INPUT && iToken <= PRS_VER_REG ) // declaration
            Status = Prs_ManReadDeclaration( p, iToken == PRS_VER_REG ? PRS_VER_WIRE : iToken );
1254 1255
        else if ( iToken == PRS_VER_REG || iToken == PRS_VER_DEFPARAM ) // unsupported keywords
            Status = Prs_ManUtilSkipUntil( p, ';' );
Alan Mishchenko committed
1256 1257
        else // read instance
        {
1258
            if ( iToken == PRS_VER_ASSIGN )
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
            {
                // read output name
                int OutItem = Prs_ManReadSignal( p );
                if ( OutItem == 0 )                     return Prs_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
                if ( !Prs_ManIsChar(p, '=') )           return Prs_ManErrorSet(p, "Expecting \"=\" in assign-statement.", 0);
                p->pCur++;
                if ( Prs_ManUtilSkipSpaces(p) )         return Prs_ManErrorSet(p, "Error number 23.", 0);
                // read expression
                while ( 1 )
                {
                    if ( !Prs_ManReadExpression(p, OutItem) )    return 0;
                    if ( Prs_ManIsChar(p, ';') )
                        break;
                    assert( Prs_ManIsChar(p, ',') );
                    p->pCur++;
                    if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23a.", 0);
                    // read output name
                    OutItem = Prs_ManReadSignal( p );
                    if ( OutItem == 0 )                 return Prs_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
                    if ( !Prs_ManIsChar(p, '=') )       return Prs_ManErrorSet(p, "Expecting \"=\" in assign-statement.", 0);
                    p->pCur++;
                    if ( Prs_ManUtilSkipSpaces(p) )     return Prs_ManErrorSet(p, "Error number 23.", 0);
                }
            }
            else if ( iToken == PRS_VER_ALWAYS )
                Status = Prs_ManReadAlways(p), fAlways = 1;
            else if ( iToken == PRS_VER_FUNCTION )
                Status = Prs_ManReadFunction(p), fAlways = 1;
Alan Mishchenko committed
1287
            else
1288
                Status = Prs_ManReadInstance( p, iToken );
Alan Mishchenko committed
1289 1290
            if ( Status == 0 )
            {
1291 1292
                return 4;

1293
                if ( Prs_ManUtilSkipUntilWord( p, "endmodule" ) ) return Prs_ManErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
Alan Mishchenko committed
1294
                //printf( "Warning! Failed to parse \"%s\". Adding module \"%s\" as blackbox.\n", 
1295 1296
                //    Abc_NamStr(p->pStrs, iToken), Abc_NamStr(p->pStrs, p->pNtk->iModuleName) );
                Vec_IntPush( &p->vFailed, p->pNtk->iModuleName );
Alan Mishchenko committed
1297
                // cleanup
1298 1299 1300 1301 1302 1303 1304
                Vec_IntErase( &p->pNtk->vWires );
                Vec_IntErase( &p->pNtk->vWiresR );
                Vec_IntErase( &p->pNtk->vSlices );
                Vec_IntErase( &p->pNtk->vConcats );
                Vec_IntErase( &p->pNtk->vBoxes );
                Vec_IntErase( &p->pNtk->vObjs );
                p->fUsingTemp2 = 0;
Alan Mishchenko committed
1305
                // add
1306 1307
                Prs_ManFinalizeNtk( p );
                Prs_ManErrorClear( p );
Alan Mishchenko committed
1308 1309 1310
                return 3;
            }
        }
1311 1312
        if ( !Status )                      return 4;
        if ( Prs_ManUtilSkipSpaces(p) )     return 4;
1313
    }
1314
    return Prs_ManErrorSet(p, "Cannot find \";\" in the module definition.", 4);
1315
}
1316
static inline int Prs_ManReadDesign( Prs_Man_t * p )
1317 1318 1319
{
    while ( 1 )
    {
1320
        int RetValue = Prs_ManReadModule( p );
Alan Mishchenko committed
1321 1322 1323 1324 1325
        if ( RetValue == 0 ) // end of file
            break;
        if ( RetValue == 1 ) // successfully parsed
            continue;
        if ( RetValue == 2 ) // recognized as primitive
1326
            continue;
Alan Mishchenko committed
1327
        if ( RetValue == 3 ) // failed and skipped
1328
            continue;
Alan Mishchenko committed
1329
        if ( RetValue == 4 ) // error
1330 1331 1332
            return 0;
        assert( 0 );
    }
Alan Mishchenko committed
1333
    return 1;
1334 1335
}

Alan Mishchenko committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1347
void Prs_ManPrintModules( Prs_Man_t * p )
Alan Mishchenko committed
1348 1349 1350
{
    char * pName; int i; 
    printf( "Succeeded parsing %d models:\n", Vec_IntSize(&p->vSucceeded) );
1351
    Prs_ManForEachNameVec( &p->vSucceeded, p, pName, i )
Alan Mishchenko committed
1352 1353 1354
        printf( " %s", pName );
    printf( "\n" );
    printf( "Skipped %d known models:\n", Vec_IntSize(&p->vKnown) );
1355
    Prs_ManForEachNameVec( &p->vKnown, p, pName, i )
Alan Mishchenko committed
1356 1357 1358
        printf( " %s", pName );
    printf( "\n" );
    printf( "Skipped %d failed models:\n", Vec_IntSize(&p->vFailed) );
1359
    Prs_ManForEachNameVec( &p->vFailed, p, pName, i )
Alan Mishchenko committed
1360 1361 1362
        printf( " %s", pName );
    printf( "\n" );
}
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1375
Vec_Ptr_t * Prs_ManReadVerilog( char * pFileName )
1376
{
1377 1378
    Vec_Ptr_t * vPrs = NULL;
    Prs_Man_t * p = Prs_ManAlloc( pFileName );
1379 1380
    if ( p == NULL )
        return NULL;
1381 1382 1383 1384
    Abc_NamStrFindOrAdd( p->pFuns, "1\'b0", NULL );
    Abc_NamStrFindOrAdd( p->pFuns, "1\'b1", NULL );
    Abc_NamStrFindOrAdd( p->pFuns, "1\'bx", NULL );
    Abc_NamStrFindOrAdd( p->pFuns, "1\'bz", NULL );
1385
    Prs_NtkAddVerilogDirectives( p );
1386
    Prs_ManReadDesign( p );   
1387
    Prs_ManPrintModules( p );
1388 1389 1390 1391
    if ( Prs_ManErrorPrint(p) )
        ABC_SWAP( Vec_Ptr_t *, vPrs, p->vNtks );
    Prs_ManFree( p );
    return vPrs;
1392
}
Alan Mishchenko committed
1393

1394
void Prs_ManReadVerilogTest( char * pFileName )
1395
{
Alan Mishchenko committed
1396
    abctime clk = Abc_Clock();
1397
    Vec_Ptr_t * vPrs = Prs_ManReadVerilog( pFileName );
1398 1399
    if ( !vPrs ) return;
    printf( "Finished reading %d networks. ", Vec_PtrSize(vPrs) );
Alan Mishchenko committed
1400
    printf( "NameIDs = %d. ", Abc_NamObjNumMax(Prs_ManNameMan(vPrs)) );
1401
    printf( "Memory = %.2f MB. ", 1.0*Prs_ManMemory(vPrs)/(1<<20) );
Alan Mishchenko committed
1402
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
1403
    Prs_ManWriteVerilog( Extra_FileNameGenericAppend(pFileName, "_out.v"), vPrs );
1404
//    Abc_NamPrint( Prs_ManNameMan(vPrs) );
1405
    Prs_ManVecFree( vPrs );
1406 1407
}

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Prs_CreateVerilogFindFon( Cba_Ntk_t * p, int NameId )
{
    int iFon = Cba_NtkGetMap( p, NameId );
    if ( iFon )
        return iFon;
    printf( "Network \"%s\": Signal \"%s\" is not driven.\n", Cba_NtkName(p), Cba_NtkStr(p, NameId) );
    return 0;
}
1427
int Prs_CreateSlice( Cba_Ntk_t * p, int iFon, Prs_Ntk_t * pNtk, int Range )
1428 1429
{
    int iObj, iFonNew, NameId;
1430
    assert( Cba_FonIsReal(iFon) );
1431
    // check existing slice
1432
    NameId = Cba_NtkNewStrId( p, Cba_ManGetSliceName(p, iFon, Range) );
1433 1434 1435 1436 1437 1438 1439 1440
    iFonNew = Cba_NtkGetMap( p, NameId );
    if ( iFonNew )
        return iFonNew;
    // create slice
    iObj = Cba_ObjAlloc( p, CBA_BOX_SLICE, 1, 1 );
    Cba_ObjSetName( p, iObj, NameId );
    Cba_ObjSetFinFon( p, iObj, 0, iFon );
    iFonNew = Cba_ObjFon0(p, iObj);
1441
    Cba_FonSetRange( p, iFonNew, Range );
1442 1443 1444 1445 1446 1447 1448
    Cba_FonSetName( p, iFonNew, NameId );
    Cba_NtkSetMap( p, NameId, iFonNew );
    return iFonNew;        
}
int Prs_CreateCatIn( Cba_Ntk_t * p, Prs_Ntk_t * pNtk, int Con )
{
    extern int Prs_CreateSignalIn( Cba_Ntk_t * p, Prs_Ntk_t * pNtk, int Sig );
1449
    int i, Sig, iObj, iFon, NameId, nBits = 0;
1450 1451
    Vec_Int_t * vSigs = Prs_CatSignals(pNtk, Con);
    // create input concatenation
1452
    iObj = Cba_ObjAlloc( p, CBA_BOX_CONCAT, Vec_IntSize(vSigs), 1 );
1453
    iFon = Cba_ObjFon0(p, iObj);
1454 1455 1456
    //sprintf( Buffer, "_icc%d_", iObj );
    //NameId = Cba_NtkNewStrId( p, Buffer );
    NameId = Cba_NtkNewStrId( p, "_icc%d_", iObj );
1457 1458 1459 1460 1461 1462 1463 1464 1465
    Cba_FonSetName( p, iFon, NameId );
    Cba_NtkSetMap( p, NameId, iFon );
    // set inputs
    Vec_IntForEachEntry( vSigs, Sig, i )
    {
        iFon = Prs_CreateSignalIn( p, pNtk, Sig );
        if ( iFon )
            Cba_ObjSetFinFon( p, iObj, i, iFon );
        if ( iFon )
1466
            nBits += Cba_FonRangeSize( p, iFon );
1467 1468
    }
    iFon = Cba_ObjFon0(p, iObj);
1469
    Cba_FonSetRange( p, iFon, Cba_NtkHashRange(p, nBits-1, 0) );
1470 1471 1472 1473
    return Cba_ObjFon0(p, iObj);
}
int Prs_CreateSignalIn( Cba_Ntk_t * p, Prs_Ntk_t * pNtk, int Sig )
{
1474
    int iFon, Value = Abc_Lit2Var2( Sig );
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
    Prs_ManType_t Type = (Prs_ManType_t)Abc_Lit2Att2( Sig );
    if ( !Sig ) return 0;
    if ( Type == CBA_PRS_NAME )
        return Prs_CreateVerilogFindFon( p, Cba_NtkNewStrId(p, Prs_NtkStr(pNtk, Value)) );
    if ( Type == CBA_PRS_CONST )
        return Cba_FonFromConst( Value );
    if ( Type == CBA_PRS_SLICE )
    {
        iFon =  Prs_CreateVerilogFindFon( p, Cba_NtkNewStrId(p, Prs_NtkStr(pNtk, Prs_SliceName(pNtk, Value))) );
        if ( !iFon )
            return 0;
1486
        return Prs_CreateSlice( p, iFon, pNtk, Prs_SliceRange(pNtk, Value) );
1487 1488 1489 1490
    }
    assert( Type == CBA_PRS_CONCAT );
    return Prs_CreateCatIn( p, pNtk, Value );
}
1491
int Prs_CreateRange( Cba_Ntk_t * p, int iFon, int NameId )
1492
{
1493
    int RangeId = -Cba_NtkGetMap(p, NameId);
1494
    if ( RangeId < 0 ) // this variable is already created
1495
        return Cba_FonRangeSize( p, -RangeId );
1496 1497 1498 1499 1500
    Cba_NtkUnsetMap( p, NameId );
    Cba_NtkSetMap( p, NameId, iFon );
    if ( RangeId == 0 )
        return 1;
    assert( RangeId > 0 );
1501
    Cba_FonSetRangeSign( p, iFon, RangeId );
1502
    return Cba_FonRangeSize( p, iFon );
1503
}
1504 1505
void Prs_CreateSignalOut( Cba_Ntk_t * p, int iFon, Prs_Ntk_t * pNtk, int Sig )
{
1506 1507 1508
    int i, iFonNew, NameOut, RangeOut, NameId, RangeId, RangeSize, nBits = 0; 
    Prs_ManType_t SigType = (Prs_ManType_t)Abc_Lit2Att2( Sig );
    int SigValue = Abc_Lit2Var2( Sig );
1509
    if ( !Sig ) return;
1510
    if ( SigType == CBA_PRS_NAME )
1511
    {
1512 1513 1514
        NameId = SigValue;
        if ( !strncmp(Cba_NtkStr(p, NameId), "Open_", 5) )
            return;
1515
        Cba_FonSetName( p, iFon, NameId );
1516
        Prs_CreateRange( p, iFon, NameId );
1517 1518
        return;
    }
1519 1520 1521 1522 1523 1524
    // create name for this fan
    NameOut = Cba_NtkNewStrId( p, "_occ%d_", iFon );
    Cba_FonSetName( p, iFon, NameOut );
    Cba_NtkSetMap( p, NameOut, iFon );
    // consider special cases
    if ( SigType == CBA_PRS_SLICE )
1525
    {
1526 1527 1528 1529 1530
        NameId  = Prs_SliceName(pNtk, SigValue);
        RangeId = Prs_SliceRange(pNtk, SigValue);
        nBits   = Cba_NtkRangeSize(p, RangeId);
        // save this slice
        Vec_IntPushThree( &p->vArray0, NameId, RangeId, iFon );
1531
    }
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 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 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
    else if ( SigType == CBA_PRS_CONCAT )
    {
        Vec_Int_t * vSigs = Prs_CatSignals(pNtk, SigValue);
        Vec_IntReverseOrder( vSigs );
        Vec_IntForEachEntry( vSigs, Sig, i )
        {
            SigType = (Prs_ManType_t)Abc_Lit2Att2( Sig );
            SigValue = Abc_Lit2Var2( Sig );
            if ( SigType == CBA_PRS_NAME )
            {
                int iObjBuf, iFonBuf;
                // create buffer
                NameId = SigValue;
                if ( !strncmp(Cba_NtkStr(p, NameId), "Open_", 5) )
                {
                    nBits++;
                    continue;
                }
                iObjBuf   = Cba_ObjAlloc( p, CBA_BOX_BUF, 1, 1 );
                iFonBuf   = Cba_ObjFon0(p, iObjBuf);
                Cba_FonSetName( p, iFonBuf, NameId );
                RangeSize = Prs_CreateRange( p, iFonBuf, NameId );
                RangeOut  = Cba_NtkHashRange(p, nBits+RangeSize-1, nBits);
                // create slice
                iFonNew   = Prs_CreateSlice( p, iFon, pNtk, RangeOut );
                Cba_ObjSetFinFon( p, iObjBuf, 0, iFonNew );
            }
            else if ( SigType == CBA_PRS_SLICE )
            {
                NameId    = Prs_SliceName(pNtk, SigValue);
                RangeId   = Prs_SliceRange(pNtk, SigValue);
                RangeSize = Cba_NtkRangeSize(p, RangeId);
                RangeOut  = Cba_NtkHashRange(p, nBits+RangeSize-1, nBits);
                // create slice
                iFonNew   = Prs_CreateSlice( p, iFon, pNtk, RangeOut );
                // save this slice
                Vec_IntPushThree( &p->vArray0, NameId, RangeId, iFonNew );
            }
            else assert( 0 );
            // increment complete range
            nBits += RangeSize;
        }
        Vec_IntReverseOrder( vSigs );
    }
    else assert( 0 );
    // set the range for the output
    Cba_FonHashRange( p, iFon, nBits-1, 0 );
}

void Prs_CreateOutConcat( Cba_Ntk_t * p, int * pSlices, int nSlices )
{
    Vec_Int_t * vBits = &p->vArray1;
    int NameId    = pSlices[0];
    int RangeId   = -Cba_NtkGetMap(p, NameId);
    int LeftId    = Cba_NtkRangeLeft( p, RangeId );
    int RightId   = Cba_NtkRangeRight( p, RangeId );
    int BotId     = Abc_MinInt( LeftId, RightId );
    int TopId     = Abc_MaxInt( LeftId, RightId );
1590
    int i, k, iObj, iFon, nParts, Prev, nBits;
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
    assert( RangeId > 0 );
    Vec_IntFill( vBits, Abc_MaxInt(LeftId, RightId) + 1, 0 );
    // fill up with slices
    for ( i = 0; i < nSlices; i++ )
    {
        int Range = pSlices[3*i+1];
        int iFon  = pSlices[3*i+2];
        int Left  = Cba_NtkRangeLeft( p, Range );
        int Right = Cba_NtkRangeRight( p, Range );
        int Bot   = Abc_MinInt( Left, Right );
        int Top   = Abc_MaxInt( Left, Right );
1602 1603
        assert( NameId == pSlices[3*i+0] && iFon > 0 );
        assert( BotId <= Bot && Top <= TopId );
1604 1605 1606 1607 1608 1609 1610
        for ( k = Bot; k <= Top; k++ )
        {
            assert( Vec_IntEntry(vBits, k) == 0 );
            Vec_IntWriteEntry( vBits, k, iFon );
        }
    }
    // check how many parts we have
1611
    Prev = -1; nParts = 0; 
1612 1613 1614 1615 1616 1617 1618
    Vec_IntForEachEntryStartStop( vBits, iFon, i, BotId, TopId+1 )
    {
        if ( Prev != iFon )
            nParts++;
        Prev = iFon;
    }
    // create new concatenation
1619
    iObj = Cba_ObjAlloc( p, CBA_BOX_CONCAT, nParts, 1 );
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    iFon = Cba_ObjFon0(p, iObj);
    Cba_FonSetName( p, iFon, NameId );
    Prs_CreateRange( p, iFon, NameId );
    // set inputs
    k = 0; Prev = -1; nBits = 0;
    Vec_IntForEachEntryStartStop( vBits, iFon, i, BotId, TopId+1 )
    {
        if ( Prev == -1 || Prev == iFon )
            nBits++;
        else
        {
            if ( Prev == 0 ) // create constant
                Prev = Cba_ManNewConstZero( p, nBits );
            assert( nBits == Cba_FonRangeSize(p, Prev) );
            Cba_ObjSetFinFon( p, iObj, nParts-1-k++, Prev );
            nBits = 1;
        }
        Prev = iFon;         
    }
    assert( nBits == Cba_FonRangeSize(p, Prev) );
    Cba_ObjSetFinFon( p, iObj, nParts-1-k++, Prev );
    assert( k == nParts );
1642
}
1643

1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
// looks at multi-bit signal; if one bit is repeated, returns this bit; otherwise, returns -1
int Prs_CreateBitSignal( Prs_Ntk_t * pNtk, int Sig )
{
    Vec_Int_t * vSigs;
    int i, SigTemp, SigOne = -1, Value = Abc_Lit2Var2( Sig );
    Prs_ManType_t Type = (Prs_ManType_t)Abc_Lit2Att2( Sig );
    if ( Type == CBA_PRS_NAME || Type == CBA_PRS_SLICE )
        return -1;
    if ( Type == CBA_PRS_CONST )
    {
        int fOnly0 = 1, fOnly1 = 1;
        char * pConst = Prs_NtkConst(pNtk, Value);
        pConst = strchr( pConst, '\'' ) + 1;
        assert( *pConst == 'b' );
        while ( *++pConst )
            if ( *pConst == '0' )
                fOnly1 = 0;
            else if ( *pConst == '1' )
                fOnly0 = 0;
        if ( fOnly0 )
            return Abc_Var2Lit2( 1, CBA_PRS_CONST ); // const0
        if ( fOnly1 )
            return Abc_Var2Lit2( 2, CBA_PRS_CONST ); // const1
        return -1;
    }
    assert( Type == CBA_PRS_CONCAT );
    vSigs = Prs_CatSignals( pNtk, Value );
    Vec_IntForEachEntry( vSigs, SigTemp, i )
    {
        Value = Abc_Lit2Var2( SigTemp );
        Type = (Prs_ManType_t)Abc_Lit2Att2( SigTemp );
        if ( Type != CBA_PRS_NAME )
            return -1;
        if ( SigOne == -1 )
            SigOne = Value;
        else if ( SigOne != Value )
            return -1;
    }
    assert( SigOne >= 0 );
    return Abc_Var2Lit2( SigOne, CBA_PRS_NAME );
}
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696

int Prs_CreateFlopSetReset( Cba_Ntk_t * p, Prs_Ntk_t * pNtk, Vec_Int_t * vBox, int * pIndexSet, int * pIndexRst, int * pBitSet, int * pBitRst )
{
    int iSigSet = -1, iSigRst = -1;
    int IndexSet = -1, IndexRst = -1;
    int FormId, ActId, k;
    // mark set and reset
    Cba_NtkCleanMap2( p );
    Cba_NtkSetMap2( p, Cba_NtkStrId(p, "set"), 1 );
    Cba_NtkSetMap2( p, Cba_NtkStrId(p, "reset"), 2 );
    // check the inputs
    Vec_IntForEachEntryDouble( vBox, FormId, ActId, k )
1697
        if ( Cba_NtkGetMap2(p, FormId) == 1 ) // set
1698
            iSigSet = ActId, IndexSet = k+1;
1699
        else if ( Cba_NtkGetMap2(p, FormId) == 2 ) // reset
1700 1701 1702 1703 1704 1705 1706 1707
            iSigRst = ActId, IndexRst = k+1;
    assert( iSigSet >= 0 && iSigRst >= 0 );
    if ( pIndexSet ) *pBitSet = 0;
    if ( pIndexRst ) *pBitRst = 0;
    if ( pBitSet )   *pBitSet = 0;
    if ( pBitRst )   *pBitRst = 0;
    if ( iSigSet == -1 || iSigRst == -1 )
        return 0;
1708 1709 1710 1711
    iSigSet = Prs_CreateBitSignal(pNtk, iSigSet);
    iSigRst = Prs_CreateBitSignal(pNtk, iSigRst);
    if ( iSigSet == -1 || iSigRst == -1 )
        return 0;
1712 1713 1714 1715 1716 1717
    if ( pIndexSet ) *pIndexSet = IndexSet;
    if ( pIndexRst ) *pIndexRst = IndexRst;
    if ( pBitSet )   *pBitSet = iSigSet;
    if ( pBitRst )   *pBitRst = iSigRst;
    return 1;
}
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
char * Prs_CreateDetectRamPort( Prs_Ntk_t * pNtk, Vec_Int_t * vBox, int NameRamId )
{
    int i, FormId, ActId;
    Vec_IntForEachEntryDouble( vBox, FormId, ActId, i )
        if ( FormId == NameRamId )
            return Abc_NamStr(pNtk->pStrs, Abc_Lit2Var2(ActId));
    return NULL;
}
int Prs_CreateGetMemSize( char * pName )
{
    char * pPtr1 = strchr( pName, '_' );
    char * pPtr2 = strchr( pPtr1+1, '_' );
    int Num1 = atoi( pPtr1 + 1 );
    int Num2 = atoi( pPtr2 + 1 );
    assert( Num1 + Abc_Base2Log(Num2) < 32 );
    return (1 << Num1) * Num2;
}
1735 1736
Vec_Ptr_t * Prs_CreateDetectRams( Prs_Ntk_t * pNtk )
{
1737 1738 1739 1740 1741
    Vec_Ptr_t * vAllRams = NULL, * vRam; 
    Vec_Int_t * vBox, * vBoxCopy; 
    char * pNtkName, * pRamName;
    int NameRamId = Abc_NamStrFind( pNtk->pStrs, "Ram" );
    int i, k, fWrite;
1742 1743 1744 1745 1746
    Prs_NtkForEachBox( pNtk, vBox, i )
    {
        if ( Prs_BoxIsNode(pNtk, i) ) // node
            continue;
        pNtkName = Prs_NtkStr(pNtk, Prs_BoxNtk(pNtk, i));
1747 1748
        fWrite = !strncmp(pNtkName, "ClockedWritePort_", strlen("ClockedWritePort_"));
        if ( fWrite || !strncmp(pNtkName, "ReadPort_", strlen("ReadPort_")) )
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
            pRamName = Prs_CreateDetectRamPort( pNtk, vBox, NameRamId ); assert( pRamName );
            if ( vAllRams == NULL )
                vAllRams = Vec_PtrAlloc( 4 );
            Vec_PtrForEachEntry( Vec_Ptr_t *, vAllRams, vRam, k )
                if ( pRamName == (char *)Vec_PtrEntry(vRam, 0) )
                {
                    if ( fWrite )
                    {
                        vBoxCopy = Vec_IntDup(vBox);
                        Vec_IntPush( vBoxCopy, i );
                        Vec_PtrPush( vRam, vBoxCopy );
                    }
                    break;
                }
            if ( k < Vec_PtrSize(vAllRams) )
                continue;
            vRam = Vec_PtrAlloc( 4 );
            Vec_PtrPush( vRam, pRamName );
            Vec_PtrPush( vRam, Abc_Int2Ptr(Prs_CreateGetMemSize(pNtkName)) );
            if ( fWrite )
            {
                vBoxCopy = Vec_IntDup(vBox);
                Vec_IntPush( vBoxCopy, i );
                Vec_PtrPush( vRam, vBoxCopy );
            }
            Vec_PtrPush( vAllRams, vRam );
1776 1777
        }
    }
1778
    return vAllRams;
1779
}
1780 1781
void Prs_CreateVerilogPio( Cba_Ntk_t * p, Prs_Ntk_t * pNtk )
{
1782
    int i, NameId, RangeId, iObj, iFon;
1783 1784 1785
    Cba_NtkCleanObjFuncs( p );
    Cba_NtkCleanObjNames( p );
    Cba_NtkCleanFonNames( p );
1786
    Cba_NtkCleanFonRanges( p );
1787 1788 1789 1790 1791 1792 1793 1794
    // create inputs
    Cba_NtkCleanMap( p );
    assert( Vec_IntSize(&pNtk->vInouts) == 0 );
    Vec_IntForEachEntryTwo( &pNtk->vInputs, &pNtk->vInputsR, NameId, RangeId, i )
    {
        iObj = Cba_ObjAlloc( p, CBA_OBJ_PI, 0, 1 );
        Cba_ObjSetName( p, iObj, NameId ); // direct name
        iFon = Cba_ObjFon0(p, iObj);
1795
        Cba_FonSetRangeSign( p, iFon, RangeId );
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
        Cba_FonSetName( p, iFon, NameId );
        Cba_NtkSetMap( p, NameId, iObj );
    }
    // create outputs
    Vec_IntForEachEntryTwo( &pNtk->vOutputs, &pNtk->vOutputsR, NameId, RangeId, i )
    {
        iObj = Cba_ObjAlloc( p, CBA_OBJ_PO, 1, 0 );
        Cba_ObjSetName( p, iObj, NameId ); // direct name
        Cba_NtkSetMap( p, NameId, iObj );
    }
    // create order
    Vec_IntForEachEntry( &pNtk->vOrder, NameId, i )
    {
        iObj = Prs_CreateVerilogFindFon( p, Abc_Lit2Var2(NameId) ); // labeled name
        if ( iObj )
            Vec_IntPush( &p->vOrder, iObj );
    }
}
int Prs_CreateVerilogNtk( Cba_Ntk_t * p, Prs_Ntk_t * pNtk )
{
1816 1817
    Vec_Int_t * vBox2Obj = Vec_IntStart( Prs_NtkBoxNum(pNtk) );
    Vec_Int_t * vBox;  Vec_Ptr_t * vAllRams, * vRam;
1818
    int i, k, iObj, iTerm, iFon, FormId, ActId, RangeId, NameId, Type;
1819 1820 1821 1822
    // map inputs
    Cba_NtkCleanMap( p );
    Cba_NtkForEachPi( p, iObj, i )
        Cba_NtkSetMap( p, Cba_ObjName(p, iObj), Cba_ObjFon0(p, iObj) );
1823 1824 1825 1826 1827 1828 1829

    // map wire names into their rangeID
    Vec_IntForEachEntryTwo( &pNtk->vWires, &pNtk->vWiresR, NameId, RangeId, i )
        Cba_NtkSetMap( p, NameId, -RangeId );
    Vec_IntForEachEntryTwo( &pNtk->vOutputs, &pNtk->vOutputsR, NameId, RangeId, i )
        Cba_NtkSetMap( p, NameId, -RangeId );

1830
    // collect RAMs and create boxes
1831 1832 1833
    vAllRams = Prs_CreateDetectRams( pNtk );
    if ( vAllRams )
    Vec_PtrForEachEntry( Vec_Ptr_t *, vAllRams, vRam, i )
1834
    {
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
        char * pRamName = (char *)Vec_PtrEntry( vRam, 0 );
        int MemSize = Abc_Ptr2Int( (char *)Vec_PtrEntry( vRam, 1 ) );
        //char Buffer[1000]; sprintf( Buffer, "%s_box", pRamName );
        //NameId = Cba_NtkNewStrId( p, Buffer ); 
        NameId = Cba_NtkNewStrId( p, "%s_box", pRamName );
        // create RAM object
        iObj = Cba_ObjAlloc( p, CBA_BOX_RAMBOX, Vec_PtrSize(vRam)-2, 1 );
        Cba_ObjSetName( p, iObj, NameId );
        iFon = Cba_ObjFon0(p, iObj);
        NameId = Cba_NtkNewStrId( p, pRamName ); 
        Cba_FonSetName( p, iFon, NameId );
1846
        Prs_CreateRange( p, iFon, NameId );
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
        assert( Cba_FonLeft(p, iFon) <= MemSize-1 );
        assert( Cba_FonRight(p, iFon) == 0 );
        //Cba_VerificSaveLineFile( p, iObj, pNet->Linefile() );
        // create write ports feeding into this object
        Vec_PtrForEachEntryStart( Vec_Int_t *, vRam, vBox, k, 2 )
        {
            int iObjNew = Cba_ObjAlloc( p, CBA_BOX_RAMWC, 4, 1 );
            int Line = Vec_IntPop( vBox );
            Vec_IntWriteEntry( vBox2Obj, Line, iObjNew );
            if ( Prs_BoxName(pNtk, Line) )
                Cba_ObjSetName( p, iObjNew, Prs_BoxName(pNtk, Line) );
            //Cba_VerificSaveLineFile( p, iObjNew, pInst->Linefile() );
            // connect output
            iFon = Cba_ObjFon0(p, iObjNew);
1861
            Cba_FonSetRange( p, iFon, Cba_NtkHashRange(p, MemSize-1, 0) );
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
            //sprintf( Buffer, "%s_wp%d", pRamName, k-2 );
            //NameId = Cba_NtkNewStrId( p, Buffer ); 
            NameId = Cba_NtkNewStrId( p, "%s_wp%d", pRamName, k-2 );
            Cba_FonSetName( p, iFon, NameId );
            Cba_NtkSetMap( p, NameId, iFon );
            // connet to RAM object
            Cba_ObjSetFinFon( p, iObj, (k++)-2, iFon );
            Vec_IntFree( vBox );
        }
        Vec_PtrFree( vRam );
1872
    }
1873 1874
    Vec_PtrFreeP( &vAllRams );

1875
    // create objects
1876
    Vec_IntClear( &p->vArray0 );
1877 1878 1879 1880
    Prs_NtkForEachBox( pNtk, vBox, i )
    {
        if ( Prs_BoxIsNode(pNtk, i) ) // node
        {
1881 1882
            Type = Prs_BoxNtk(pNtk, i);
            iObj = Cba_ObjAlloc( p, Type, Prs_BoxIONum(pNtk, i)-1, Type == CBA_BOX_ADD ? 2 : 1 );
1883 1884 1885 1886 1887
            Prs_CreateSignalOut( p, Cba_ObjFon0(p, iObj), pNtk, Vec_IntEntry(vBox, 1) ); // node output
        }
        else // box
        {
            Cba_Ntk_t * pBox = NULL; int nInputs, nOutputs = 1;
Alan Mishchenko committed
1888
            char ** pOutNames = NULL, * pNtkName = Prs_NtkStr(pNtk, Prs_BoxNtk(pNtk, i));
1889
            Type = Prs_ManFindType( pNtkName, &nInputs, 1, &pOutNames );
1890 1891
            if ( Type == CBA_BOX_RAMWC )
                continue;
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
            if ( Type == CBA_OBJ_BOX )
            {
                pBox = Cba_ManNtkFind( p->pDesign, pNtkName );
                if ( pBox == NULL )
                {
                    printf( "Fatal error: Cannot find module \"%s\".\n", pNtkName );
                    continue;
                }
                nInputs = Cba_NtkPiNum(pBox);
                nOutputs = Cba_NtkPoNum(pBox);
            }
1903
            else if ( Type == CBA_BOX_ADD || Type == CBA_BOX_DFFCPL )
1904 1905
                nOutputs = 2;
            else if ( Type == CBA_BOX_NMUX )
1906 1907 1908 1909 1910 1911 1912
            {
                if ( !strncmp(pNtkName, "wide_mux_", strlen("wide_mux_")) )
                    nInputs = 1 + (1 << atoi(pNtkName+strlen("wide_mux_")));
                else if ( !strncmp(pNtkName, "Mux_", strlen("Mux_")) )
                    nInputs = 1 + (1 << atoi(pNtkName+strlen("Mux_")));
                else assert( 0 );
            }
1913
            else if ( Type == CBA_BOX_SEL )
1914 1915 1916 1917 1918 1919 1920
            {
                if ( !strncmp(pNtkName, "wide_select_", strlen("wide_select_")) )
                    nInputs = 1 + atoi(pNtkName+strlen("wide_select_"));
                else if ( !strncmp(pNtkName, "Select_", strlen("Select_")) )
                    nInputs = 1 + atoi(pNtkName+strlen("Select_"));
                else assert( 0 );
            }
1921
            else if ( (Type == CBA_BOX_DFFRS || Type == CBA_BOX_LATCHRS) && !strncmp(pNtkName, "wide_", strlen("wide_")) && !Prs_CreateFlopSetReset(p, pNtk, vBox, NULL, NULL, NULL, NULL) )
1922
                nInputs = atoi(pNtkName+strlen(Type == CBA_BOX_DFFRS ? "wide_dffrs_" : "wide_latchrs_")), nOutputs = 1, Type = CBA_BOX_CONCAT;
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
            // create object
            iObj = Cba_ObjAlloc( p, Type, nInputs, nOutputs );
            if ( pBox ) Cba_ObjSetFunc( p, iObj, Cba_NtkId(pBox) );
            // mark PO objects
            Cba_NtkCleanMap2( p );
            if ( pBox )
                Cba_NtkForEachPo( pBox, iTerm, k )
                    Cba_NtkSetMap2( p, Cba_ObjName(pBox, iTerm), k+1 );
            else
                for ( k = 0; k < nOutputs; k++ )
                    Cba_NtkSetMap2( p, Cba_NtkStrId(p, pOutNames[k]), k+1 );
            // map box fons
            Vec_IntForEachEntryDouble( vBox, FormId, ActId, k )
                if ( Cba_NtkGetMap2(p, FormId) )
                {
                    iFon = Cba_ObjFon(p, iObj, Cba_NtkGetMap2(p, FormId)-1);
                    Prs_CreateSignalOut( p, iFon, pNtk, ActId );
                }
        }
1942
        Vec_IntWriteEntry( vBox2Obj, i, iObj );
1943 1944
        if ( Prs_BoxName(pNtk, i) )
            Cba_ObjSetName( p, iObj, Prs_BoxName(pNtk, i) );
1945
        //Cba_VerificSaveLineFile( p, iObj, pInst->Linefile() );
1946 1947
    }

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    // create concatenations for split signals
    if ( Vec_IntSize(&p->vArray0) )
    {
        int Prev = -1, Index = 0;
        Vec_IntSortMulti( &p->vArray0, 3, 0 );
        Vec_IntForEachEntryTriple( &p->vArray0, NameId, RangeId, iFon, i )
        {
            if ( Prev != -1 && Prev != NameId )
                Prs_CreateOutConcat( p, Vec_IntArray(&p->vArray0) + Index, (i - Index)/3 ), Index = i;
            Prev = NameId;         
        }
        Prs_CreateOutConcat( p, Vec_IntArray(&p->vArray0) + Index, (i - Index)/3 ), Index = i;
        //Cba_VerificSaveLineFile( p, iObj, pInst->Linefile() );
    }

1963 1964 1965
    // connect objects
    Prs_NtkForEachBox( pNtk, vBox, i )
    {
1966
        iObj = Vec_IntEntry( vBox2Obj, i );
1967 1968
        if ( Prs_BoxIsNode(pNtk, i) ) // node
        {
1969
            Type = Prs_BoxNtk(pNtk, i);
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
            Vec_IntForEachEntryDoubleStart( vBox, FormId, ActId, k, 2 )
            {
                iFon = Prs_CreateSignalIn( p, pNtk, ActId );
                if ( iFon )
                    Cba_ObjSetFinFon( p, iObj, k/2-1, iFon ); 
            }
        }
        else // box
        {
            int nInputs = -1;
Alan Mishchenko committed
1980
            char ** pInNames = NULL, * pNtkName = Prs_NtkStr(pNtk, Prs_BoxNtk(pNtk, i));
1981
            Type = Prs_ManFindType( pNtkName, &nInputs, 0, &pInNames );
1982
            if ( (Type == CBA_BOX_DFFRS || Type == CBA_BOX_LATCHRS) && !strncmp(pNtkName, "wide_", strlen("wide_")) )
1983
            {
1984 1985 1986 1987 1988 1989 1990 1991 1992
                int IndexSet = -1, IndexRst = -1,  iBitSet = -1, iBitRst = -1;
                int Status = Prs_CreateFlopSetReset( p, pNtk, vBox, &IndexSet, &IndexRst, &iBitSet, &iBitRst );
                if ( Status )
                {
                    Vec_IntWriteEntry( vBox, IndexSet, iBitSet );
                    Vec_IntWriteEntry( vBox, IndexRst, iBitRst );
                    // updated box should be fine
                }
                else
1993
                {
1994
                    int w, Width = atoi( pNtkName + strlen(Type == CBA_BOX_DFFRS ? "wide_dffrs_" : "wide_latchrs_") );
1995
                    assert( Cba_ObjType(p, iObj) == CBA_BOX_CONCAT );
1996 1997 1998 1999 2000 2001 2002
                    // prepare inputs
                    assert( nInputs >= 0 );
                    Cba_NtkCleanMap2( p );
                    for ( k = 0; k < nInputs; k++ )
                        Cba_NtkSetMap2( p, Cba_NtkStrId(p, pInNames[k]), k+1 );
                    // create bit-level objects
                    for ( w = 0; w < Width; w++ )
2003 2004 2005 2006 2007
                    {
                        // create bit-level flop
                        int iObjNew = Cba_ObjAlloc( p, Type, 4, 1 );
                        if ( Prs_BoxName(pNtk, i) )
                        {
2008
                            NameId = Cba_NtkNewStrId( p, "%s[%d]", Prs_NtkStr(pNtk, Prs_BoxName(pNtk, i)), w );
2009 2010 2011 2012 2013 2014
                            Cba_ObjSetName( p, iObjNew, NameId );
                        }
                        //Cba_VerificSaveLineFile( p, iObjNew, pInst->Linefile() );
                        // set output fon
                        iFon = Cba_ObjFon0(p, iObjNew);
                        {
2015
                            NameId = Cba_NtkNewStrId( p, "%s[%d]", Cba_FonNameStr(p, Cba_ObjFon0(p, iObj)), w );
2016 2017 2018 2019 2020
                            Cba_FonSetName( p, iFon, NameId );
                        }
                        // no need to map this name because it may be produced elsewhere
                        //Cba_NtkSetMap( p, NameId, iFon );
                        // add the flop
2021
                        Cba_ObjSetFinFon( p, iObj, Width-1-w, iFon );
2022 2023 2024 2025 2026 2027 2028 2029
                        // create bit-level flops
                        Vec_IntForEachEntryDouble( vBox, FormId, ActId, k )
                            if ( Cba_NtkGetMap2(p, FormId) )
                            {
                                int Index = Cba_NtkGetMap2(p, FormId)-1;
                                iFon = Prs_CreateSignalIn( p, pNtk, ActId );  assert( iFon );
                                // create bit-select node for data/set/reset (but not for clock)
                                if ( Index < 3 ) // not clock
2030
                                    iFon = Prs_CreateSlice( p, iFon, pNtk, 0 );
2031 2032 2033
                                Cba_ObjSetFinFon( p, iObjNew, Index, iFon );
                            }
                    }
2034 2035 2036
                    continue;
                }
            }
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
            assert( Type == Cba_ObjType(p, iObj) );
            //assert( nInputs == -1 || nInputs == Cba_ObjFinNum(p, iObj) );
            // mark PI objects
            Cba_NtkCleanMap2( p );
            if ( Type == CBA_OBJ_BOX )
            {
                Cba_Ntk_t * pBox = Cba_ObjNtk(p, iObj);
                assert( Cba_NtkPiNum(pBox) == Cba_ObjFinNum(p, iObj) );
                assert( Cba_NtkPoNum(pBox) == Cba_ObjFonNum(p, iObj) );
                Cba_NtkForEachPi( pBox, iTerm, k )
                    Cba_NtkSetMap2( p, Cba_ObjName(pBox, iTerm), k+1 );
            }
            else
            {
                assert( nInputs >= 0 );
                for ( k = 0; k < nInputs; k++ )
                    Cba_NtkSetMap2( p, Cba_NtkStrId(p, pInNames[k]), k+1 );
            }
2055 2056 2057 2058
            // connect box fins
            Vec_IntForEachEntryDouble( vBox, FormId, ActId, k )
                if ( Cba_NtkGetMap2(p, FormId) )
                {
2059 2060 2061
                    int Index = Cba_NtkGetMap2(p, FormId)-1;
                    int nBits = Cba_ObjFinNum(p, iObj);
                    assert( Index < nBits );
2062 2063
                    iFon = Prs_CreateSignalIn( p, pNtk, ActId );
                    if ( iFon )
2064
                        Cba_ObjSetFinFon( p, iObj, Index, iFon );
2065 2066 2067 2068 2069
                }
            // special cases
            if ( Type == CBA_BOX_NMUX || Type == CBA_BOX_SEL )
            {
                int FonCat = Cba_ObjFinFon( p, iObj, 1 );
2070
                int nBits  = Cba_FonRangeSize( p, FonCat );
2071 2072
                int nParts = Cba_ObjFinNum(p, iObj) - 1;
                int Slice  = nBits / nParts;
2073 2074
                int nFins = Cba_ObjFinNum(p, iObj);
                assert( Cba_ObjFinNum(p, iObj) >= 2 );
2075
                assert( Slice * nParts == nBits );
2076
                assert( nFins == 1 + nParts );
2077
                Cba_ObjCleanFinFon( p, iObj, 1 );
2078 2079 2080 2081 2082 2083 2084 2085
                // create buffer for the constant
                if ( FonCat < 0 ) 
                {
                    int iObjNew = Cba_ObjAlloc( p, CBA_BOX_BUF, 1, 1 );
                    Cba_ObjSetFinFon( p, iObjNew, 0, FonCat );
                    FonCat = Cba_ObjFon0( p, iObjNew );
                    NameId = Cba_NtkNewStrId( p, "_buf_const_%d", iObjNew );
                    Cba_FonSetName( p, FonCat, NameId );
2086
                    Cba_FonSetRange( p, FonCat, Cba_NtkHashRange(p, nBits-1, 0) );
2087
                }
2088 2089
                for ( k = 0; k < nParts; k++ )
                {
2090 2091
//                    iFon = Prs_CreateSlice( p, FonCat, pNtk, Cba_NtkHashRange(p, (nParts-1-k)*Slice+Slice-1, (nParts-1-k)*Slice) );
                    iFon = Prs_CreateSlice( p, FonCat, pNtk, Cba_NtkHashRange(p, k*Slice+Slice-1, k*Slice) );
2092 2093 2094 2095
                    Cba_ObjSetFinFon( p, iObj, k+1, iFon );
                }
            }
        }
2096 2097 2098 2099 2100 2101 2102 2103
        // if carry-in is not supplied, use constant 0
        if ( Type == CBA_BOX_ADD && Cba_ObjFinFon(p, iObj, 0) == 0 )
            Cba_ObjSetFinFon( p, iObj, 0, Cba_FonFromConst(1) );
        // if set or reset are not supplied, use constant 0
        if ( Type == CBA_BOX_DFFRS && Cba_ObjFinFon(p, iObj, 1) == 0 )
            Cba_ObjSetFinFon( p, iObj, 1, Cba_FonFromConst(1) );
        if ( Type == CBA_BOX_DFFRS && Cba_ObjFinFon(p, iObj, 2) == 0 )
            Cba_ObjSetFinFon( p, iObj, 2, Cba_FonFromConst(1) );
2104
    }
2105
    Vec_IntFree( vBox2Obj );
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
    // connect outputs
    Vec_IntForEachEntryTwo( &pNtk->vOutputs, &pNtk->vOutputsR, NameId, RangeId, i )
    {
        iObj = Cba_NtkPo( p, i );
        assert( NameId == Cba_ObjName(p, iObj) ); // direct name
        iFon = Prs_CreateVerilogFindFon( p, NameId );
        if ( !iFon ) 
            continue;
        Cba_ObjSetFinFon( p, iObj, 0, iFon );
        if ( RangeId )
        {
2117 2118
            assert( Cba_NtkRangeLeft(p, Abc_Lit2Var(RangeId))  == Cba_FonLeft(p, iFon) );
            assert( Cba_NtkRangeRight(p, Abc_Lit2Var(RangeId)) == Cba_FonRight(p, iFon) );
2119 2120 2121 2122
        }
    }
    return 0;
}
2123 2124
Cba_Man_t * Prs_ManBuildCbaVerilog( char * pFileName, Vec_Ptr_t * vDes )
{
2125 2126 2127 2128 2129 2130
    Prs_Ntk_t * pPrsNtk; int i, fError = 0;
    Prs_Ntk_t * pPrsRoot = Prs_ManRoot(vDes);
    // start the manager
    Abc_Nam_t * pStrs = Abc_NamRef(pPrsRoot->pStrs);
    Abc_Nam_t * pFuns = Abc_NamRef(pPrsRoot->pFuns);
    Abc_Nam_t * pMods = Abc_NamStart( 100, 24 );
2131
    Cba_Man_t * p = Cba_ManAlloc( pFileName, Vec_PtrSize(vDes), pStrs, pFuns, pMods, Hash_IntManRef(pPrsRoot->vHash) );
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
    // initialize networks
    Vec_PtrForEachEntry( Prs_Ntk_t *, vDes, pPrsNtk, i )
    {
        Cba_Ntk_t * pNtk = Cba_NtkAlloc( p, Prs_NtkId(pPrsNtk), Prs_NtkPiNum(pPrsNtk), Prs_NtkPoNum(pPrsNtk), Prs_NtkObjNum(pPrsNtk), 100, 100 );
        Prs_CreateVerilogPio( pNtk, pPrsNtk );
        Cba_NtkAdd( p, pNtk );
    }
    // create networks
    Vec_PtrForEachEntry( Prs_Ntk_t *, vDes, pPrsNtk, i )
    {
2142
        printf( "Building module \"%s\"...\n", Prs_NtkName(pPrsNtk) );
2143 2144 2145 2146 2147 2148 2149 2150 2151
        fError = Prs_CreateVerilogNtk( Cba_ManNtk(p, i+1), pPrsNtk );
        if ( fError )
            break;
    }
    if ( fError )
        printf( "Quitting because of errors.\n" );
    else
        Cba_ManPrepareSeq( p );
    return p;
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cba_Man_t * Cba_ManReadVerilog( char * pFileName )
{
    Cba_Man_t * p = NULL;
    Vec_Ptr_t * vDes = Prs_ManReadVerilog( pFileName );
    if ( vDes && Vec_PtrSize(vDes) )
        p = Prs_ManBuildCbaVerilog( pFileName, vDes );
    if ( vDes )
        Prs_ManVecFree( vDes );
    return p;
}
2175 2176 2177 2178 2179 2180 2181 2182

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


ABC_NAMESPACE_IMPL_END