summaryrefslogtreecommitdiffstats
path: root/src/base/io/ioWriteBench.c
blob: 0b7591c05f531dbfd8a50d8e3ef77ce78a63edb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**CFile****************************************************************

  FileName    [ioWriteBench.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to write the network in BENCH format.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: ioWriteBench.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "io.h"

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

static int    Io_WriteBenchOne( FILE * pFile, Abc_Ntk_t * pNtk );
static int    Io_WriteBenchOneNode( FILE * pFile, Abc_Obj_t * pNode );
static char * Io_BenchNodeName( Abc_Obj_t * pObj, int fPhase );
static char * Io_BenchNodeNameInv( Abc_Obj_t * pObj );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Writes the network in BENCH format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteBench( Abc_Ntk_t * pNtk, char * pFileName )
{
    Abc_Ntk_t * pExdc;
    FILE * pFile;
    assert( Abc_NtkIsAig(pNtk) );
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
        fprintf( stdout, "Io_WriteBench(): Cannot open the output file.\n" );
        return 0;
    }
    fprintf( pFile, "# Benchmark \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
    // write the network
    Io_WriteBenchOne( pFile, pNtk );
    // write EXDC network if it exists
    pExdc = Abc_NtkExdc( pNtk );
    if ( pExdc )
    {
        printf( "Io_WriteBench: EXDC is not written (warning).\n" );
//        fprintf( pFile, "\n" );
//        fprintf( pFile, ".exdc\n" );
//        Io_LogicWriteOne( pFile, pExdc );
    }
    // finalize the file
    fclose( pFile );
    return 1;
}

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

  Synopsis    [Writes the network in BENCH format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteBenchOne( FILE * pFile, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Obj_t * pNode;
    int i;

    assert( Abc_NtkIsLogicSop(pNtk) || Abc_NtkIsAig(pNtk) );

    // write the PIs/POs/latches
    Abc_NtkForEachPi( pNtk, pNode, i )
        fprintf( pFile, "INPUT(%s)\n", Abc_NtkNamePi(pNtk,i) );
    Abc_NtkForEachPo( pNtk, pNode, i )
        fprintf( pFile, "OUTPUT(%s)\n", Abc_NtkNamePo(pNtk,i) );
    Abc_NtkForEachLatch( pNtk, pNode, i )
        fprintf( pFile, "%-11s = DFF(%s)\n", 
            Abc_NtkNameLatch(pNtk,i), Abc_NtkNameLatchInput(pNtk,i) );

    // set the node names 
    Abc_NtkCleanCopy( pNtk );
    Abc_NtkForEachCi( pNtk, pNode, i )
        pNode->pCopy = (Abc_Obj_t *)Abc_NtkNameCi(pNtk,i);

    // write intervers for COs appearing in negative polarity
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        if ( Abc_AigNodeIsUsedCompl(pNode) )
            fprintf( pFile, "%-11s = NOT(%s)\n", 
                Io_BenchNodeNameInv(pNode), 
                Abc_NtkNameCi(pNtk,i) );
    }

    // write internal nodes
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkNodeNum(pNtk) );
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        if ( Abc_NodeIsConst(pNode) )
            continue;
        Io_WriteBenchOneNode( pFile, pNode );
    }
    Extra_ProgressBarStop( pProgress );

    // write buffers for CO
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        fprintf( pFile, "%-11s = BUFF(%s)\n", 
            (i < Abc_NtkPoNum(pNtk))? Abc_NtkNamePo(pNtk,i) : 
                Abc_NtkNameLatchInput( pNtk, i-Abc_NtkPoNum(pNtk) ), 
            Io_BenchNodeName( Abc_ObjFanin0(pNode), !Abc_ObjFaninC0(pNode) ) );
    }
    Abc_NtkCleanCopy( pNtk );
    return 1;
}


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

  Synopsis    [Writes the network in BENCH format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_WriteBenchOneNode( FILE * pFile, Abc_Obj_t * pNode )
{
    assert( Abc_ObjIsNode(pNode) );
    // write the AND gate
    fprintf( pFile, "%-11s",       Io_BenchNodeName( pNode, 1 ) );
    fprintf( pFile, " = AND(%s, ", Io_BenchNodeName( Abc_ObjFanin0(pNode), !Abc_ObjFaninC0(pNode) ) ); 
    fprintf( pFile, "%s)\n",       Io_BenchNodeName( Abc_ObjFanin1(pNode), !Abc_ObjFaninC1(pNode) ) );

    // write the inverter if necessary
    if ( Abc_AigNodeIsUsedCompl(pNode) )
    {
        fprintf( pFile, "%-11s = NOT(",   Io_BenchNodeName( pNode, 0 ) );
        fprintf( pFile, "%s)\n",          Io_BenchNodeName( pNode, 1 ) );
    }
    return 1;
}

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

  Synopsis    [Returns the name of an internal AIG node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Io_BenchNodeName( Abc_Obj_t * pObj, int fPhase )
{
    static char Buffer[500];
    if ( pObj->pCopy ) // PIs and latches
    {
        sprintf( Buffer, "%s%s", (char *)pObj->pCopy, (fPhase? "":"_c") );
        return Buffer;
    }
    assert( Abc_ObjIsNode(pObj) );
    if ( Abc_NodeIsConst(pObj) ) // constant node
    {
        if ( fPhase )
            sprintf( Buffer, "%s", "vdd" );
        else
            sprintf( Buffer, "%s", "gnd" );
        return Buffer;
    }
    // internal nodes
    sprintf( Buffer, "%s%s", Abc_ObjName(pObj), (fPhase? "":"_c") );
    return Buffer;
}

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

  Synopsis    [Returns the name of an internal AIG node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Io_BenchNodeNameInv( Abc_Obj_t * pObj )
{
    static char Buffer[500];
    sprintf( Buffer, "%s%s", Abc_ObjName(pObj), "_c" );
    return Buffer;
}

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