wlcShow.c 13.9 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 45 46 47 48 49 50 51
/**CFile****************************************************************

  FileName    [wlcShow.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Parses several flavors of word-level Verilog.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "wlc.h"

ABC_NAMESPACE_IMPL_START

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

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


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

  Synopsis    [Writes the graph structure of WLC for DOT.]

  Description [Useful for graph visualization using tools such as GraphViz: 
  http://www.graphviz.org/]
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkDumpDot( Wlc_Ntk_t * p, char * pFileName, Vec_Int_t * vBold )
{
    FILE * pFile;
    Wlc_Obj_t * pNode;
    int LevelMax, Prev, Level, i;

Alan Mishchenko committed
52
    if ( vBold ? (Vec_IntSize(vBold) > 2000) : (Wlc_NtkObjNum(p) > 2000) )
53
    {
Alan Mishchenko committed
54
        fprintf( stdout, "Cannot visualize WLC with more than %d nodes.\n", 2000 );
55 56 57 58 59 60 61 62 63 64 65 66 67 68
        return;
    }
    if ( (pFile = fopen( pFileName, "w" )) == NULL )
    {
        fprintf( stdout, "Cannot open the intermediate file \"%s\".\n", pFileName );
        return;
    }

    // mark the nodes
    if ( vBold )
        Wlc_NtkForEachObjVec( vBold, p, pNode, i )
            pNode->Mark = 1;

    // compute levels
69
    LevelMax = 1 + Wlc_NtkCreateLevels( p );
70 71
    if ( vBold )
        LevelMax = Wlc_NtkRemapLevels( p, vBold, LevelMax );
72 73 74 75

//    Wlc_NtkForEachObj( p, pNode, i )
//        printf( "Obj=%d Lev=%d\n", i, Wlc_ObjLevel(p, pNode) );
//    printf( "\n" );
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    // write the DOT header
    fprintf( pFile, "# %s\n",  "WLC structure generated by ABC" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "digraph WLC {\n" );
    fprintf( pFile, "size = \"7.5,10\";\n" );
//  fprintf( pFile, "ranksep = 0.5;\n" );
//  fprintf( pFile, "nodesep = 0.5;\n" );
    fprintf( pFile, "center = true;\n" );
//  fprintf( pFile, "orientation = landscape;\n" );
//  fprintf( pFile, "edge [fontsize = 10];\n" );
//  fprintf( pFile, "edge [dir = none];\n" );
    fprintf( pFile, "edge [dir = back];\n" );
    fprintf( pFile, "\n" );

    // labels on the left of the picture
    fprintf( pFile, "{\n" );
    fprintf( pFile, "  node [shape = plaintext];\n" );
    fprintf( pFile, "  edge [style = invis];\n" );
    fprintf( pFile, "  LevelTitle1 [label=\"\"];\n" );
    fprintf( pFile, "  LevelTitle2 [label=\"\"];\n" );
    // generate node names with labels
    for ( Level = LevelMax; Level >= 0; Level-- )
    {
        // the visible node name
        fprintf( pFile, "  Level%d", Level );
        fprintf( pFile, " [label = " );
        // label name
        fprintf( pFile, "\"" );
        fprintf( pFile, "\"" );
        fprintf( pFile, "];\n" );
    }

    // genetate the sequence of visible/invisible nodes to mark levels
    fprintf( pFile, "  LevelTitle1 ->  LevelTitle2 ->" );
    for ( Level = LevelMax; Level >= 0; Level-- )
    {
        // the visible node name
        fprintf( pFile, "  Level%d",  Level );
        // the connector
        if ( Level != 0 )
            fprintf( pFile, " ->" );
        else
            fprintf( pFile, ";" );
    }
    fprintf( pFile, "\n" );
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );

    // generate title box on top
    fprintf( pFile, "{\n" );
    fprintf( pFile, "  rank = same;\n" );
    fprintf( pFile, "  LevelTitle1;\n" );
    fprintf( pFile, "  title1 [shape=plaintext,\n" );
    fprintf( pFile, "          fontsize=20,\n" );
    fprintf( pFile, "          fontname = \"Times-Roman\",\n" );
    fprintf( pFile, "          label=\"" );
    fprintf( pFile, "%s", "WLC structure generated by ABC" );
    fprintf( pFile, "\\n" );
136
    fprintf( pFile, "Benchmark \\\"%s\\\" from file \\\"%s\\\". ", p->pName, p->pSpec ? Extra_FileNameWithoutPath(p->pSpec) : "unknown" );
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
//    fprintf( pFile, "Time was %s. ",  Extra_TimeStamp() );
    fprintf( pFile, "\"\n" );
    fprintf( pFile, "         ];\n" );
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );

    // generate statistics box
    fprintf( pFile, "{\n" );
    fprintf( pFile, "  rank = same;\n" );
    fprintf( pFile, "  LevelTitle2;\n" );
    fprintf( pFile, "  title2 [shape=plaintext,\n" );
    fprintf( pFile, "          fontsize=18,\n" );
    fprintf( pFile, "          fontname = \"Times-Roman\",\n" );
    fprintf( pFile, "          label=\"" );
152
    fprintf( pFile, "The word-level network contains %d nodes and spans %d levels.", Wlc_NtkObjNum(p)-Wlc_NtkCiNum(p), LevelMax-1 );
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    fprintf( pFile, "\\n" );
    fprintf( pFile, "\"\n" );
    fprintf( pFile, "         ];\n" );
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );

    // generate the COs
    fprintf( pFile, "{\n" );
    fprintf( pFile, "  rank = same;\n" );
    // the labeling node of this level
    fprintf( pFile, "  Level%d;\n",  LevelMax );
    // generate the CO nodes
    Wlc_NtkForEachCo( p, pNode, i )
    {
Alan Mishchenko committed
168 169
        if ( vBold && !pNode->Mark )
            continue;
170
        pNode = Wlc_ObjCo2PoFo(p, i);
Alan Mishchenko committed
171
        fprintf( pFile, "  NodePo%d [label = \"%s%s %d\"", Wlc_ObjId(p, pNode), Wlc_ObjName(p, Wlc_ObjId(p, pNode)), Wlc_ObjIsPo(pNode)? "":"_in",  Wlc_ObjRange(pNode) ); 
172
        fprintf( pFile, ", shape = %s", i < Wlc_NtkPoNum(p) ? "invtriangle" : "box" );
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        fprintf( pFile, ", color = coral, fillcolor = coral" );
        fprintf( pFile, "];\n" );
    }
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );

    // generate nodes of each rank
    for ( Level = LevelMax - 1; Level > 0; Level-- )
    {
        fprintf( pFile, "{\n" );
        fprintf( pFile, "  rank = same;\n" );
        // the labeling node of this level
        fprintf( pFile, "  Level%d;\n",  Level );
        Wlc_NtkForEachObj( p, pNode, i )
        {
            if ( (int)Wlc_ObjLevel(p, pNode) != Level )
                continue;
Alan Mishchenko committed
191 192
            if ( vBold && !pNode->Mark )
                continue;
193 194 195

            if ( pNode->Type == WLC_OBJ_CONST )
            {
Alan Mishchenko committed
196
                //char * pName = Wlc_ObjName(p, i);
197
                fprintf( pFile, "  Node%d [label = \"%d:%d\'h", i, i, Wlc_ObjRange(pNode) ); 
Alan Mishchenko committed
198 199 200 201 202 203 204
                if ( Wlc_ObjRange(pNode) > 64 )
                {
                    Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pNode), 16 );
                    fprintf( pFile, "..." );
                }
                else
                    Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pNode), (Wlc_ObjRange(pNode) + 3) / 4 );
205 206 207
                fprintf( pFile, "\"" ); 
            }
            else if ( pNode->Type == WLC_OBJ_BUF || pNode->Type == WLC_OBJ_MUX )
208
                fprintf( pFile, "  Node%d [label = \"%d: %d\"", i, i, Wlc_ObjRange(pNode) ); 
209
            else if ( pNode->Type >= WLC_OBJ_LOGIC_NOT && pNode->Type <= WLC_OBJ_COMP_MOREEQU )
210
                fprintf( pFile, "  Node%d [label = \"%d:%s\"", i, i, Wlc_ObjTypeName(pNode) ); 
211
            else
212
                fprintf( pFile, "  Node%d [label = \"%d:%s %d\"", i, i, Wlc_ObjTypeName(pNode), Wlc_ObjRange(pNode) ); 
213 214 215 216 217

            if ( pNode->Type == WLC_OBJ_ARI_MULTI )
                fprintf( pFile, ", shape = doublecircle" );
            else if ( pNode->Type >= WLC_OBJ_COMP_EQU && pNode->Type <= WLC_OBJ_COMP_MOREEQU )
                fprintf( pFile, ", shape = diamond" );
218
            else if ( pNode->Type == WLC_OBJ_BIT_SELECT || pNode->Type == WLC_OBJ_BIT_CONCAT || pNode->Type == WLC_OBJ_FF )
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                fprintf( pFile, ", shape = box" );
            else if ( pNode->Type == WLC_OBJ_BUF || pNode->Type == WLC_OBJ_BIT_ZEROPAD || pNode->Type == WLC_OBJ_BIT_SIGNEXT )
                fprintf( pFile, ", shape = triangle" );
            else if ( pNode->Type == WLC_OBJ_MUX )
                fprintf( pFile, ", shape = trapezium" );
            else
                fprintf( pFile, ", shape = ellipse" );

            if ( vBold ? pNode->Mark : ((pNode->Type >= WLC_OBJ_ARI_ADD && pNode->Type <= WLC_OBJ_ARI_SQUARE) || pNode->Type == WLC_OBJ_BIT_NOT) )
                fprintf( pFile, ", style = filled" );
            fprintf( pFile, "];\n" );
        }
        fprintf( pFile, "}" );
        fprintf( pFile, "\n" );
        fprintf( pFile, "\n" );
    }

    // generate the CI nodes
    fprintf( pFile, "{\n" );
    fprintf( pFile, "  rank = same;\n" );
    // the labeling node of this level
    fprintf( pFile, "  Level%d;\n",  0 );
    // generate the CI nodes
242
    Wlc_NtkForEachObj( p, pNode, i )
243
    {
244 245
        if ( !Wlc_ObjIsCi(pNode) && Wlc_ObjFaninNum(pNode) > 0 )
            continue;
Alan Mishchenko committed
246 247
        if ( vBold && !pNode->Mark )
            continue;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        if ( pNode->Type == WLC_OBJ_CONST )
        {
            //char * pName = Wlc_ObjName(p, i);
            fprintf( pFile, "  Node%d [label = \"%d:%d\'h", i, i, Wlc_ObjRange(pNode) ); 
            if ( Wlc_ObjRange(pNode) > 64 )
            {
                Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pNode), 16 );
                fprintf( pFile, "..." );
            }
            else
                Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pNode), (Wlc_ObjRange(pNode) + 3) / 4 );
            fprintf( pFile, "\"" ); 
        }
        else
        {
            fprintf( pFile, "  Node%d [label = \"%d:%s %d\"", Wlc_ObjId(p, pNode), Wlc_ObjId(p, pNode), Wlc_ObjName(p, Wlc_ObjId(p, pNode)), Wlc_ObjRange(pNode) ); 
264
            fprintf( pFile, ", shape = %s", (Vec_IntSize(&p->vFfs2) > 0 || Wlc_ObjCiId(pNode) < Wlc_NtkPiNum(p)) ? "triangle" : "box" );
265 266
            fprintf( pFile, ", color = coral, fillcolor = coral" );
        }
267 268 269 270 271 272 273 274 275
        fprintf( pFile, "];\n" );
    }
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );

    // generate invisible edges from the square down
    fprintf( pFile, "title1 -> title2 [style = invis];\n" );
    Wlc_NtkForEachCo( p, pNode, i )
276
    {
Alan Mishchenko committed
277 278
        if ( vBold && !pNode->Mark )
            continue;
279
        pNode = Wlc_ObjCo2PoFo( p, i );
280
        fprintf( pFile, "title2 -> NodePo%d [style = invis];\n", Wlc_ObjId(p, pNode) );
281
    }
282 283 284 285
    // generate invisible edges among the COs
    Prev = -1;
    Wlc_NtkForEachCo( p, pNode, i )
    {
286
        pNode = Wlc_ObjCo2PoFo( p, i );
Alan Mishchenko committed
287 288 289
        if ( vBold && !pNode->Mark )
            continue;
        if ( Prev >= 0 )
290 291 292 293 294 295 296
            fprintf( pFile, "NodePo%d -> NodePo%d [style = invis];\n", Prev, Wlc_ObjId(p, pNode) );
        Prev = Wlc_ObjId(p, pNode);
    }
    // generate invisible edges among the CIs
    Prev = -1;
    Wlc_NtkForEachCi( p, pNode, i )
    {
Alan Mishchenko committed
297 298 299
        if ( vBold && !pNode->Mark )
            continue;
        if ( Prev >= 0 )
300 301 302 303 304
            fprintf( pFile, "Node%d -> Node%d [style = invis];\n", Prev, Wlc_ObjId(p, pNode) );
        Prev = Wlc_ObjId(p, pNode);
    }

    // generate edges
305 306
    Wlc_NtkForEachCo( p, pNode, i )
    {
Alan Mishchenko committed
307 308
        if ( vBold && !pNode->Mark )
            continue;
309 310 311 312 313 314 315 316
        fprintf( pFile, "NodePo%d",  Wlc_ObjId(p, Wlc_ObjCo2PoFo(p, i)) );
        fprintf( pFile, " -> " );
        fprintf( pFile, "Node%d",  Wlc_ObjId(p, pNode) );
        fprintf( pFile, " [" );
        fprintf( pFile, "style = %s", pNode->Signed? "dotted" : "solid" );
        fprintf( pFile, "]" );
        fprintf( pFile, ";\n" );
    }
317 318 319 320 321
    Wlc_NtkForEachObj( p, pNode, i )
    {
        int k, iFanin;
        if ( Wlc_ObjIsCi(pNode) )
            continue;
Alan Mishchenko committed
322 323
        if ( vBold && !pNode->Mark )
            continue;
324
        // generate the edge from this node to the next
325
        Wlc_ObjForEachFanin( pNode, iFanin, k ) if ( iFanin )
326 327 328 329 330
        {
            fprintf( pFile, "Node%d",  i );
            fprintf( pFile, " -> " );
            fprintf( pFile, "Node%d",  iFanin );
            fprintf( pFile, " [" );
331 332 333
            fprintf( pFile, "style = %s", Wlc_NtkObj(p, iFanin)->Signed? "dotted" : "solid" );
            if ( pNode->Type == WLC_OBJ_MUX && k == 0 )
                fprintf( pFile, ", style = %s", "bold" );
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
            fprintf( pFile, "]" );
            fprintf( pFile, ";\n" );
        }
    }
    fprintf( pFile, "}" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "\n" );
    fclose( pFile );

    // unmark nodes
    if ( vBold )
        Wlc_NtkCleanMarks( p );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkShow( Wlc_Ntk_t * p, Vec_Int_t * vBold )
{
    extern void Abc_ShowFile( char * FileNameDot );
    FILE * pFile;
    char FileNameDot[200];
364 365 366 367 368 369
    char * pName = Extra_FileDesignName(p->pName);
    char * pSpec = p->pSpec ? Extra_FileDesignName(p->pSpec) : "unknown";
    sprintf( FileNameDot, "%s_%s.dot", pName, pSpec );
    ABC_FREE( pName );
    if ( strcmp(pSpec, "unknown") )
        ABC_FREE( pSpec );
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    // check that the file can be opened
    if ( (pFile = fopen( FileNameDot, "w" )) == NULL )
    {
        fprintf( stdout, "Cannot open the intermediate file \"%s\".\n", FileNameDot );
        return;
    }
    fclose( pFile );
    // generate the file
    Wlc_NtkDumpDot( p, FileNameDot, vBold );
    // visualize the file 
    Abc_ShowFile( FileNameDot );
}

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


ABC_NAMESPACE_IMPL_END