wlcWriteVer.c 26.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**CFile****************************************************************

  FileName    [wlcWriteVer.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Writes word-level Verilog.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 22, 2014.]

  Revision    [$Id: wlcWriteVer.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

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

#include "wlc.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
45 46 47 48 49 50 51 52 53 54 55 56 57
void Wlc_WriteTableOne( FILE * pFile, int nFans, int nOuts, word * pTable, int Id )
{
    int m, nMints = (1<<nFans);
//    Abc_TtPrintHexArrayRev( stdout, pTable, nMints );  printf( "\n" );
    assert( nOuts > 0 && nOuts <= 64 && (64 % nOuts) == 0 );
    fprintf( pFile, "module table%d(ind, val);\n", Id );
    fprintf( pFile, "  input  [%d:0] ind;\n", nFans-1 );
    fprintf( pFile, "  output [%d:0] val;\n", nOuts-1 );
    fprintf( pFile, "  reg    [%d:0] val;\n", nOuts-1 );
    fprintf( pFile, "  always @(ind)\n" );
    fprintf( pFile, "  begin\n" );
    fprintf( pFile, "    case (ind)\n" );
    for ( m = 0; m < nMints; m++ )
58
    fprintf( pFile, "      %d\'h%x: val = %d\'h%x;\n", nFans, m, nOuts, (unsigned)((pTable[(nOuts * m) >> 6] >> ((nOuts * m) & 63)) & Abc_Tt6Mask(nOuts)) );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    fprintf( pFile, "    endcase\n" );
    fprintf( pFile, "  end\n" );
    fprintf( pFile, "endmodule\n" );
    fprintf( pFile, "\n" );
}
void Wlc_WriteTables( FILE * pFile, Wlc_Ntk_t * p )
{
    Vec_Int_t * vNodes;
    Wlc_Obj_t * pObj, * pFanin;
    word * pTable;
    int i;
    if ( p->vTables == NULL || Vec_PtrSize(p->vTables) == 0 )
        return;
    // map tables into their nodes
    vNodes = Vec_IntStart( Vec_PtrSize(p->vTables) );
    Wlc_NtkForEachObj( p, pObj, i )
        if ( pObj->Type == WLC_OBJ_TABLE )
            Vec_IntWriteEntry( vNodes, Wlc_ObjTableId(pObj), i );
    // write tables
    Vec_PtrForEachEntry( word *, p->vTables, pTable, i )
    {
        pObj = Wlc_NtkObj( p, Vec_IntEntry(vNodes, i) );
        assert( pObj->Type == WLC_OBJ_TABLE );
        pFanin = Wlc_ObjFanin0( p, pObj );
        Wlc_WriteTableOne( pFile, Wlc_ObjRange(pFanin), Wlc_ObjRange(pObj), pTable, i );
    }
    Vec_IntFree( vNodes );
}

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

90
  Synopsis    [This was used to add POs to each node except PIs and MUXes.]
91 92 93 94 95 96 97 98

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void Wlc_WriteAddPos( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj;
    int i;
    Vec_IntClear( &p->vPos );
    Wlc_NtkForEachObj( p, pObj, i )
        if ( pObj->Type != WLC_OBJ_PI && pObj->Type != WLC_OBJ_MUX )
        {
            pObj->fIsPo = 1;
            Vec_IntPush( &p->vPos, Wlc_ObjId(p, pObj) );
        }
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
void Wlc_WriteVerIntVec( FILE * pFile, Wlc_Ntk_t * p, Vec_Int_t * vVec, int Start )
{
    char * pName;
    int LineLength  = Start;
    int NameCounter = 0;
    int AddedLength, i, iObj;
    Vec_IntForEachEntry( vVec, iObj, i )
    {
        pName = Wlc_ObjName( p, iObj );
        // get the line length after this name is written
        AddedLength = strlen(pName) + 2;
        if ( NameCounter && LineLength + AddedLength + 3 > 70 )
        { // write the line extender
            fprintf( pFile, "\n   " );
            // reset the line length
            LineLength  = Start;
            NameCounter = 0;
        }
        fprintf( pFile, " %s%s", pName, (i==Vec_IntSize(vVec)-1)? "" : "," );
        LineLength += AddedLength;
        NameCounter++;
    }
} 
147
void Wlc_WriteVerInt( FILE * pFile, Wlc_Ntk_t * p, int fNoFlops )
148 149
{
    Wlc_Obj_t * pObj;
150
    int i, k, j, iFanin;
151 152 153
    char Range[100];
    fprintf( pFile, "module %s ( ", p->pName );
    fprintf( pFile, "\n   " );
154
    if ( Wlc_NtkPiNum(p) > 0 || (fNoFlops && Wlc_NtkCiNum(p)) )
155
    {
156
        Wlc_WriteVerIntVec( pFile, p, fNoFlops ? &p->vCis : &p->vPis, 3 );
157 158
        fprintf( pFile, ",\n   " );
    }
159 160
    if ( Wlc_NtkPoNum(p) > 0 || (fNoFlops && Wlc_NtkCoNum(p))  )
        Wlc_WriteVerIntVec( pFile, p, fNoFlops ? &p->vCos : &p->vPos, 3 );
161
    fprintf( pFile, "  );\n" );
162 163 164 165
    // mark fanins of rotation shifts
    Wlc_NtkForEachObj( p, pObj, i )
        if ( pObj->Type == WLC_OBJ_ROTATE_R || pObj->Type == WLC_OBJ_ROTATE_L )
            Wlc_ObjFanin1(p, pObj)->Mark = 1;
166 167
    Wlc_NtkForEachObj( p, pObj, i )
    {
168
        int nDigits   = Abc_Base10Log(Abc_AbsInt(pObj->End)+1) + Abc_Base10Log(Abc_AbsInt(pObj->Beg)+1) + (int)(pObj->End < 0) + (int)(pObj->Beg < 0);
169 170 171 172 173
        if ( pObj->Mark ) 
        {
            pObj->Mark = 0;
            continue;
        }
174
        sprintf( Range, "%s[%d:%d]%*s", (!p->fSmtLib && Wlc_ObjIsSigned(pObj)) ? "signed ":"       ", pObj->End, pObj->Beg, 8-nDigits, "" );
175
        fprintf( pFile, "  " );
176
        if ( pObj->Type == WLC_OBJ_PI || (fNoFlops && pObj->Type == WLC_OBJ_FO) )
177
            fprintf( pFile, "input  " );
178
        else if ( pObj->fIsPo || (fNoFlops && pObj->fIsFi) )
179 180 181
            fprintf( pFile, "output " );
        else
            fprintf( pFile, "       " );
182
        if ( Wlc_ObjIsCi(pObj) || pObj->fIsPo || (fNoFlops && pObj->fIsFi) )
183
        {
184
            fprintf( pFile, "wire %s %s ;\n", Range, Wlc_ObjName(p, i) );
185 186 187 188
            if ( Wlc_ObjIsCi(pObj) )
                continue;
            Range[0] = 0;
        }
189
        if ( (pObj->fIsPo || (fNoFlops && pObj->fIsFi)) && pObj->Type != WLC_OBJ_FF )
190 191 192
        {
            if ( Wlc_ObjFaninNum(pObj) == 0 )
                continue;
193
            fprintf( pFile, "  assign                         " );
194
        }
195
        else if ( (pObj->Type == WLC_OBJ_MUX && Wlc_ObjFaninNum(pObj) > 3) || pObj->Type == WLC_OBJ_SEL )
196
            fprintf( pFile, "reg  %s ", Range );
197
        else
198
            fprintf( pFile, "wire %s ", Range );
199
        if ( pObj->Type == WLC_OBJ_TABLE )
200 201
        {
            // wire [3:0] s4972; table0 s4972_Index(s4971, s4972);
202 203
            fprintf( pFile, "%s ;              table%d", Wlc_ObjName(p, i), Wlc_ObjTableId(pObj) );
            fprintf( pFile, " s%d_Index(%s, ", i, Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
204
            fprintf( pFile, "%s)",             Wlc_ObjName(p, i) );
205
        }
206 207 208 209 210 211 212 213 214
        else if ( pObj->Type == WLC_OBJ_LUT )
        {
            // wire [3:0] s4972; LUT lut4972_Index(s4971, s4972);
            fprintf( pFile, "%s ;           LUT", Wlc_ObjName(p, i) );
            fprintf( pFile, " lut%d (%s, ", i, Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
            for ( k = 1; k < Wlc_ObjFaninNum(pObj); k++ )
                fprintf( pFile, "%s, ", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)) );
            fprintf( pFile, "%s)",             Wlc_ObjName(p, i) );
        }
215 216
        else if ( pObj->Type == WLC_OBJ_CONST )
        {
217
            fprintf( pFile, "%-16s = %d\'%sh", Wlc_ObjName(p, i), Wlc_ObjRange(pObj), Wlc_ObjIsSigned(pObj) ? "s":"" );
218 219 220 221 222 223
            if ( pObj->fXConst )
            {
                for ( k = 0; k < (Wlc_ObjRange(pObj) + 3) / 4; k++ )
                    fprintf( pFile, "x" );
            }
            else
224
                Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pObj), (Wlc_ObjRange(pObj) + 3) / 4 );
225
        }
226 227 228 229 230 231 232 233
        else if ( pObj->Type == WLC_OBJ_ROTATE_R || pObj->Type == WLC_OBJ_ROTATE_L )
        {
            //  wire [27:0] s4960 = (s57 >> 17) | (s57 << 11);
            Wlc_Obj_t * pShift = Wlc_ObjFanin1(p, pObj);
            int Num0 = *Wlc_ObjConstValue(pShift);
            int Num1 = Wlc_ObjRange(pObj) - Num0;
            assert( pShift->Type == WLC_OBJ_CONST );
            assert( Num0 > 0 && Num0 < Wlc_ObjRange(pObj) );
234
            fprintf( pFile, "%-16s = ", Wlc_ObjName(p, i) );
235
            if ( pObj->Type == WLC_OBJ_ROTATE_R )
236
                fprintf( pFile, "(%s >> %d) | (%s << %d)", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Num0, Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Num1 );
237
            else
238
                fprintf( pFile, "(%s << %d) | (%s >> %d)", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Num0, Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Num1 );
239
        }
240 241
        else if ( pObj->Type == WLC_OBJ_MUX && Wlc_ObjFaninNum(pObj) > 3 )
        {
242
            fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
243
            fprintf( pFile, "         " );
244 245 246 247 248 249 250 251 252 253 254 255
            fprintf( pFile, "always @( " );
            Wlc_ObjForEachFanin( pObj, iFanin, k )
                fprintf( pFile, "%s%s", k ? " or ":"", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)) );
            fprintf( pFile, " )\n" );
            fprintf( pFile, "           " );
            fprintf( pFile, "begin\n" );
            fprintf( pFile, "             " );
            fprintf( pFile, "case ( %s )\n", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 0)) );
            Wlc_ObjForEachFanin( pObj, iFanin, k )
            {
                if ( !k ) continue;
                fprintf( pFile, "               " );
256 257
                fprintf( pFile, "%d : %s = ", k-1, Wlc_ObjName(p, i) );
                fprintf( pFile, "%s ;\n", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)) );
258 259 260 261 262 263 264
            }
            fprintf( pFile, "             " );
            fprintf( pFile, "endcase\n" );
            fprintf( pFile, "           " );
            fprintf( pFile, "end\n" );
            continue;
        }
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        else if ( pObj->Type == WLC_OBJ_SEL )
        {
            fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
            fprintf( pFile, "         " );
            fprintf( pFile, "always @( " );
            Wlc_ObjForEachFanin( pObj, iFanin, k )
                fprintf( pFile, "%s%s", k ? " or ":"", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)) );
            fprintf( pFile, " )\n" );
            fprintf( pFile, "           " );
            fprintf( pFile, "begin\n" );
            fprintf( pFile, "             " );
            fprintf( pFile, "case ( %s )\n", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 0)) );
            Wlc_ObjForEachFanin( pObj, iFanin, k )
            {
                if ( !k ) continue;
                fprintf( pFile, "               " );
                fprintf( pFile, "%d\'b", Wlc_ObjFaninNum(pObj)-1 );
                for ( j = Wlc_ObjFaninNum(pObj)-1; j > 0; j-- )
                    fprintf( pFile, "%d", (int)(j==k) );
                fprintf( pFile, " : %s = ", Wlc_ObjName(p, i) );
                fprintf( pFile, "%s ;\n", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)) );
            }
287 288 289 290 291 292 293
            fprintf( pFile, "               " );
            fprintf( pFile, "default" );
            fprintf( pFile, " : %s = ", Wlc_ObjName(p, i) );
            fprintf( pFile, "%d\'b", Wlc_ObjRange(pObj) );
            for ( j = Wlc_ObjRange(pObj)-1; j >= 0; j-- )
                fprintf( pFile, "%d", 0 );
            fprintf( pFile, " ;\n" );
294 295 296 297 298 299
            fprintf( pFile, "             " );
            fprintf( pFile, "endcase\n" );
            fprintf( pFile, "           " );
            fprintf( pFile, "end\n" );
            continue;
        }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        else if ( pObj->Type == WLC_OBJ_DEC )
        {
            int nRange = Wlc_ObjRange(Wlc_ObjFanin0(p, pObj));
            assert( (1 << nRange) == Wlc_ObjRange(pObj) );
            fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
            for ( k = 0; k < Wlc_ObjRange(pObj); k++ )
            {
                fprintf( pFile, "         " );
                fprintf( pFile, "wire " );
                fprintf( pFile, "%s_", Wlc_ObjName(p, i) );
                for ( j = 0; j < nRange; j++ )
                    fprintf( pFile, "%d", (k >> (nRange-1-j)) & 1 );
                fprintf( pFile, " = " );
                for ( j = 0; j < nRange; j++ )
                    fprintf( pFile, "%s%s%s[%d]", 
                        j ? " & ":"", ((k >> (nRange-1-j)) & 1) ? " ":"~", 
                        Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 0)), nRange-1-j );
                fprintf( pFile, " ;\n" );
            }
            fprintf( pFile, "         " );
            fprintf( pFile, "assign %s = { ", Wlc_ObjName(p, i) );
            for ( k = Wlc_ObjRange(pObj)-1; k >= 0; k-- )
            {
                fprintf( pFile, "%s%s_", k < Wlc_ObjRange(pObj)-1 ? ", ":"", Wlc_ObjName(p, i) );
                for ( j = 0; j < nRange; j++ )
                    fprintf( pFile, "%d", (k >> (nRange-1-j)) & 1 );
            }
            fprintf( pFile, " } ;\n" );
            continue;
        }
330 331 332 333 334 335 336 337 338 339 340 341 342
        else if ( pObj->Type == WLC_OBJ_ARI_ADDSUB )
        {
            // out = mode ? a+b+cin : a-b-cin
            fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
            fprintf( pFile, "         " );
            fprintf( pFile, "assign " );
            fprintf( pFile, "%s = %s ? %s + %s + %s : %s - %s - %s ;\n", 
                        Wlc_ObjName(p, i), Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)),
                        Wlc_ObjName(p, Wlc_ObjFaninId2(pObj)), Wlc_ObjName(p, Wlc_ObjFaninId(pObj,3)), Wlc_ObjName(p, Wlc_ObjFaninId1(pObj)),
                        Wlc_ObjName(p, Wlc_ObjFaninId2(pObj)), Wlc_ObjName(p, Wlc_ObjFaninId(pObj,3)), Wlc_ObjName(p, Wlc_ObjFaninId1(pObj)) 
                   );
            continue;
        }
343 344
        else if ( pObj->Type == WLC_OBJ_READ || pObj->Type == WLC_OBJ_WRITE )
        {
345 346 347 348 349 350
            if ( p->fMemPorts )
            {
                fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
                fprintf( pFile, "         " );
                fprintf( pFile, "%s (", pObj->Type == WLC_OBJ_READ ? "ABC_READ" : "ABC_WRITE" );
                Wlc_ObjForEachFanin( pObj, iFanin, k )
351
                    fprintf( pFile, " .%s(%s),", k==0 ? "mem_in" : (k==1 ? "addr": "data"), Wlc_ObjName(p, iFanin) );
352 353 354 355 356 357 358 359 360 361 362 363 364 365
                fprintf( pFile, " .%s(%s) ) ;\n", pObj->Type == WLC_OBJ_READ ? "data" : "mem_out", Wlc_ObjName(p, i) );
                continue;
            }
            else
            {
                int nBitsMem  = Wlc_ObjRange( Wlc_ObjFanin(p, pObj, 0) );
                //int nBitsAddr = Wlc_ObjRange( Wlc_ObjFanin(p, pObj, 1) );
                int nBitsDat  = pObj->Type == WLC_OBJ_READ ? Wlc_ObjRange(pObj) : Wlc_ObjRange(Wlc_ObjFanin(p, pObj, 2));
                int Depth     = nBitsMem / nBitsDat;
                assert( nBitsMem % nBitsDat == 0 );
                fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
                fprintf( pFile, "         " );
                fprintf( pFile, "%s_%d (", pObj->Type == WLC_OBJ_READ ? "CPL_MEM_READ" : "CPL_MEM_WRITE", Depth );
                Wlc_ObjForEachFanin( pObj, iFanin, k )
366
                    fprintf( pFile, " .%s(%s),", k==0 ? "mem_data_in" : (k==1 ? "addr_in": "data_in"), Wlc_ObjName(p, iFanin) );
367 368 369 370 371 372
                fprintf( pFile, " .%s(%s) ) ;\n", "data_out", Wlc_ObjName(p, i) );
                continue;
            }
        }
        else if ( pObj->Type == WLC_OBJ_FF )
        {
373 374 375 376
            fprintf( pFile, "%s ;\n", Wlc_ObjName(p, i) );
            continue;
        }
        else 
377
        {
378
            fprintf( pFile, "%-16s = ", Wlc_ObjName(p, i) );
379
            if ( pObj->Type == WLC_OBJ_BUF )
380
                fprintf( pFile, "%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
381
            else if ( pObj->Type == WLC_OBJ_MUX )
382 383 384 385 386
            {
                fprintf( pFile, "%s ? ", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
                fprintf( pFile, "%s : ", Wlc_ObjName(p, Wlc_ObjFaninId2(pObj)) );
                fprintf( pFile, "%s",    Wlc_ObjName(p, Wlc_ObjFaninId1(pObj)) );
            }
387
            else if ( pObj->Type == WLC_OBJ_ARI_MINUS )
388
                fprintf( pFile, "-%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
389
            else if ( pObj->Type == WLC_OBJ_BIT_NOT )
390
                fprintf( pFile, "~%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
391
            else if ( pObj->Type == WLC_OBJ_LOGIC_NOT )
392
                fprintf( pFile, "!%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
393
            else if ( pObj->Type == WLC_OBJ_REDUCT_AND )
394
                fprintf( pFile, "&%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
395
            else if ( pObj->Type == WLC_OBJ_REDUCT_OR )
396
                fprintf( pFile, "|%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
397
            else if ( pObj->Type == WLC_OBJ_REDUCT_XOR )
398
                fprintf( pFile, "^%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
399 400 401 402 403 404
            else if ( pObj->Type == WLC_OBJ_REDUCT_NAND )
                fprintf( pFile, "~&%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
            else if ( pObj->Type == WLC_OBJ_REDUCT_NOR )
                fprintf( pFile, "~|%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
            else if ( pObj->Type == WLC_OBJ_REDUCT_NXOR )
                fprintf( pFile, "~^%s", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
405
            else if ( pObj->Type == WLC_OBJ_BIT_SELECT )
406
                fprintf( pFile, "%s [%d:%d]", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Wlc_ObjRangeEnd(pObj), Wlc_ObjRangeBeg(pObj) );
407
            else if ( pObj->Type == WLC_OBJ_BIT_SIGNEXT )
408
                fprintf( pFile, "{ {%d{%s[%d]}}, %s }", Wlc_ObjRange(pObj) - Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1, Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
409
            else if ( pObj->Type == WLC_OBJ_BIT_ZEROPAD )
410
                fprintf( pFile, "{ {%d{1\'b0}}, %s }", Wlc_ObjRange(pObj) - Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) );
411 412 413 414 415 416 417
            else if ( pObj->Type == WLC_OBJ_BIT_CONCAT )
            {
                fprintf( pFile, "{" );
                Wlc_ObjForEachFanin( pObj, iFanin, k )
                    fprintf( pFile, " %s%s", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)), k == Wlc_ObjFaninNum(pObj)-1 ? "":"," );
                fprintf( pFile, " }" );
            }
418
            else
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            {
                fprintf( pFile, "%s ", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 0)) );
                if ( pObj->Type == WLC_OBJ_SHIFT_R )
                    fprintf( pFile, ">>" );
                else if ( pObj->Type == WLC_OBJ_SHIFT_RA )
                    fprintf( pFile, ">>>" );
                else if ( pObj->Type == WLC_OBJ_SHIFT_L )
                    fprintf( pFile, "<<" );
                else if ( pObj->Type == WLC_OBJ_SHIFT_LA )
                    fprintf( pFile, "<<<" );
                else if ( pObj->Type == WLC_OBJ_BIT_AND )
                    fprintf( pFile, "&" );
                else if ( pObj->Type == WLC_OBJ_BIT_OR )
                    fprintf( pFile, "|" );
                else if ( pObj->Type == WLC_OBJ_BIT_XOR )
                    fprintf( pFile, "^" );
435 436 437 438
                else if ( pObj->Type == WLC_OBJ_BIT_NAND )
                    fprintf( pFile, "~&" );
                else if ( pObj->Type == WLC_OBJ_BIT_NOR )
                    fprintf( pFile, "~|" );
439 440
                else if ( pObj->Type == WLC_OBJ_BIT_NXOR )
                    fprintf( pFile, "~^" );
441 442
                else if ( pObj->Type == WLC_OBJ_LOGIC_IMPL )
                    fprintf( pFile, "=>" );
443 444 445 446
                else if ( pObj->Type == WLC_OBJ_LOGIC_AND )
                    fprintf( pFile, "&&" );
                else if ( pObj->Type == WLC_OBJ_LOGIC_OR )
                    fprintf( pFile, "||" );
447 448
                else if ( pObj->Type == WLC_OBJ_LOGIC_XOR )
                    fprintf( pFile, "^^" );
449 450
                else if ( pObj->Type == WLC_OBJ_COMP_EQU )
                    fprintf( pFile, "==" );
451
                else if ( pObj->Type == WLC_OBJ_COMP_NOTEQU )
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                    fprintf( pFile, "!=" );
                else if ( pObj->Type == WLC_OBJ_COMP_LESS )
                    fprintf( pFile, "<" );
                else if ( pObj->Type == WLC_OBJ_COMP_MORE )
                    fprintf( pFile, ">" );
                else if ( pObj->Type == WLC_OBJ_COMP_LESSEQU )
                    fprintf( pFile, "<=" );
                else if ( pObj->Type == WLC_OBJ_COMP_MOREEQU )
                    fprintf( pFile, ">=" );
                else if ( pObj->Type == WLC_OBJ_ARI_ADD )
                    fprintf( pFile, "+" );
                else if ( pObj->Type == WLC_OBJ_ARI_SUB )
                    fprintf( pFile, "-" );
                else if ( pObj->Type == WLC_OBJ_ARI_MULTI )
                    fprintf( pFile, "*" );
                else if ( pObj->Type == WLC_OBJ_ARI_DIVIDE )
468
                    fprintf( pFile, "/" );
469 470
                else if ( pObj->Type == WLC_OBJ_ARI_REM )
                    fprintf( pFile, "%%" );
471
                else if ( pObj->Type == WLC_OBJ_ARI_MODULUS )
Alan Mishchenko committed
472
                    fprintf( pFile, "%%" );
473 474
                else if ( pObj->Type == WLC_OBJ_ARI_POWER )
                    fprintf( pFile, "**" );
475 476
                else if ( pObj->Type == WLC_OBJ_ARI_SQRT )
                    fprintf( pFile, "@" );
477 478
                else if ( pObj->Type == WLC_OBJ_ARI_SQUARE )
                    fprintf( pFile, "#" );
479 480
                else 
                {
481 482
                    //assert( 0 );
                    printf( "Failed to write node \"%s\" with unknown operator type (%d).\n", Wlc_ObjName(p, i), pObj->Type );
483 484 485
                    fprintf( pFile, "???\n" );
                    continue;
                }
486
                fprintf( pFile, " %s", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 1)) );
487 488
                if ( Wlc_ObjFaninNum(pObj) == 3 && pObj->Type == WLC_OBJ_ARI_ADD ) 
                    fprintf( pFile, " + %s", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 2)) );
489 490
            }
        }
491
        fprintf( pFile, " ;%s\n", (p->fSmtLib && Wlc_ObjIsSigned(pObj)) ? " // signed SMT-LIB operator" : "" );
492
    }
493 494
    iFanin = 0;
    assert( !p->vInits || Wlc_NtkFfNum(p) == Vec_IntSize(p->vInits) );
495
    if ( !fNoFlops )
496
    {
497 498
        if ( p->vInits )
        Wlc_NtkForEachCi( p, pObj, i )
499
        {
500 501 502 503 504 505 506 507 508 509 510 511 512
            int nDigits   = Abc_Base10Log(pObj->End+1) + 1;
            char * pName  = Wlc_ObjName(p, Wlc_ObjId(p, pObj));
            assert( i == Wlc_ObjCiId(pObj) );
            if ( pObj->Type == WLC_OBJ_PI )
                continue;
            sprintf( Range, "       [%d:%d]%*s", Wlc_ObjRange(pObj) - 1, 0, 8-nDigits, "" );
            fprintf( pFile, "         " );
            fprintf( pFile, "wire %s ", Range );
            fprintf( pFile, "%s_init%*s = ", pName, 11 - (int)strlen(pName), "" );
            if ( Vec_IntEntry(p->vInits, i-Wlc_NtkPiNum(p)) > 0 )
                fprintf( pFile, "%s", Wlc_ObjName(p, Wlc_ObjId(p, Wlc_NtkPi(p, Vec_IntEntry(p->vInits, i-Wlc_NtkPiNum(p))))));
            else
            {
513
                if ( p->pInits[iFanin] == 'x' || p->pInits[iFanin] == 'X' )
514 515 516 517 518 519 520 521 522 523 524
                {
                    fprintf( pFile, "%d\'h", Wlc_ObjRange(pObj) );
                    for ( k = 0; k < (Wlc_ObjRange(pObj) + 3) / 4; k++ )
                        fprintf( pFile, "x" );
                }
                else
                {
                    fprintf( pFile, "%d\'b", Wlc_ObjRange(pObj) );
                    for ( k = Wlc_ObjRange(pObj)-1; k >= 0; k-- )
                        fprintf( pFile, "%c", p->pInits[iFanin + k] );
                }
525 526 527
            }
            fprintf( pFile, ";\n" );
            iFanin += Wlc_ObjRange(pObj);
528
        }
529 530 531 532 533 534
        Wlc_NtkForEachCi( p, pObj, i )
        {
            assert( i == Wlc_ObjCiId(pObj) );
            if ( pObj->Type == WLC_OBJ_PI )
                continue;
            fprintf( pFile, "         " );
535 536 537 538 539 540 541 542 543 544
            if ( p->fEasyFfs )
            {
                fprintf( pFile, "ABC_DFF" );
                fprintf( pFile, " reg%d (",        i );
                fprintf( pFile, " .q(%s),",      Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
                fprintf( pFile, " .d(%s),",      Wlc_ObjName(p, Wlc_ObjId(p, Wlc_ObjFo2Fi(p, pObj))) );
                if ( p->vInits )
                    fprintf( pFile, " .init(%s_init)", Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
                fprintf( pFile, " ) ;\n" );
            }
545
            else
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
            {
                fprintf( pFile, "CPL_FF" );
                if ( Wlc_ObjRange(pObj) > 1 )
                    fprintf( pFile, "#%d%*s", Wlc_ObjRange(pObj), 4 - Abc_Base10Log(Wlc_ObjRange(pObj)+1), "" );
                else
                    fprintf( pFile, "     " );
                fprintf( pFile, " reg%d (",       i );
                fprintf( pFile, " .q(%s),",      Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
                fprintf( pFile, " .qbar()," );
                fprintf( pFile, " .d(%s),",      Wlc_ObjName(p, Wlc_ObjId(p, Wlc_ObjFo2Fi(p, pObj))) );
                fprintf( pFile, " .clk(%s),",    "1\'b0" );
                fprintf( pFile, " .arst(%s),",   "1\'b0" );
                if ( p->vInits )
                    fprintf( pFile, " .arstval(%s_init)", Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
                else
                    fprintf( pFile, " .arstval(%s)", "1\'b0" );
                fprintf( pFile, " ) ;\n" );
            }
564 565
        }
        assert( !p->vInits || iFanin == (int)strlen(p->pInits) );
566
    }
567 568 569 570 571 572 573 574 575 576 577 578
    // write DFFs in the end
    fprintf( pFile, "\n" );
    Wlc_NtkForEachFf2( p, pObj, i )
    {
        char * pInNames[8] = {"d", "clk", "reset", "set", "enable", "async", "sre", "init"};
        fprintf( pFile, "         " );
        fprintf( pFile, "%s (", "ABC_DFFRSE" );
        Wlc_ObjForEachFanin( pObj, iFanin, k )
            if ( iFanin ) fprintf( pFile, " .%s(%s),", pInNames[k], Wlc_ObjName(p, iFanin) );
        fprintf( pFile, " .%s(%s) ) ;\n", "q", Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
    }
    fprintf( pFile, "\n" );
579 580
    fprintf( pFile, "endmodule\n\n" );
} 
581
void Wlc_WriteVer( Wlc_Ntk_t * p, char * pFileName, int fAddCos, int fNoFlops )
582 583 584 585 586 587 588 589
{
    FILE * pFile;
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Wlc_WriteVer(): Cannot open the output file \"%s\".\n", pFileName );
        return;
    }
590
    fprintf( pFile, "// Benchmark \"%s\" from file \"%s\" written by ABC on %s\n", p->pName, p->pSpec ? p->pSpec : "unknown", Extra_TimeStamp() );
591
    fprintf( pFile, "\n" );
592
    Wlc_WriteTables( pFile, p );
593 594
    if ( fAddCos )
        Wlc_WriteAddPos( p );
595
    Wlc_WriteVerInt( pFile, p, fNoFlops );
596 597 598 599 600 601 602 603 604 605 606
    fprintf( pFile, "\n" );
    fclose( pFile );
}

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


ABC_NAMESPACE_IMPL_END