1 /**CFile****************************************************************
2 
3   FileName    [ioWriteEqn.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Command processing package.]
8 
9   Synopsis    [Procedures to write equation representation of the network.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: ioWriteEqn.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "ioAbc.h"
22 
23 ABC_NAMESPACE_IMPL_START
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 ///                        DECLARATIONS                              ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 static void Io_NtkWriteEqnOne( FILE * pFile, Abc_Ntk_t * pNtk );
31 static void Io_NtkWriteEqnCis( FILE * pFile, Abc_Ntk_t * pNtk );
32 static void Io_NtkWriteEqnCos( FILE * pFile, Abc_Ntk_t * pNtk );
33 static int Io_NtkWriteEqnCheck( Abc_Ntk_t * pNtk );
34 
35 ////////////////////////////////////////////////////////////////////////
36 ///                     FUNCTION DEFINITIONS                         ///
37 ////////////////////////////////////////////////////////////////////////
38 
39 /**Function*************************************************************
40 
41   Synopsis    [Writes the logic network in the equation format.]
42 
43   Description []
44 
45   SideEffects []
46 
47   SeeAlso     []
48 
49 ***********************************************************************/
Io_WriteEqn(Abc_Ntk_t * pNtk,char * pFileName)50 void Io_WriteEqn( Abc_Ntk_t * pNtk, char * pFileName )
51 {
52     FILE * pFile;
53 
54     assert( Abc_NtkIsAigNetlist(pNtk) );
55     if ( Abc_NtkLatchNum(pNtk) > 0 )
56         printf( "Warning: only combinational portion is being written.\n" );
57 
58     // check that the names are fine for the EQN format
59     if ( !Io_NtkWriteEqnCheck(pNtk) )
60         return;
61 
62     // start the output stream
63     pFile = fopen( pFileName, "w" );
64     if ( pFile == NULL )
65     {
66         fprintf( stdout, "Io_WriteEqn(): Cannot open the output file \"%s\".\n", pFileName );
67         return;
68     }
69     fprintf( pFile, "# Equations for \"%s\" written by ABC on %s\n", pNtk->pName, Extra_TimeStamp() );
70 
71     // write the equations for the network
72     Io_NtkWriteEqnOne( pFile, pNtk );
73     fprintf( pFile, "\n" );
74     fclose( pFile );
75 }
76 
77 /**Function*************************************************************
78 
79   Synopsis    [Write one network.]
80 
81   Description []
82 
83   SideEffects []
84 
85   SeeAlso     []
86 
87 ***********************************************************************/
Io_NtkWriteEqnOne(FILE * pFile,Abc_Ntk_t * pNtk)88 void Io_NtkWriteEqnOne( FILE * pFile, Abc_Ntk_t * pNtk )
89 {
90     Vec_Vec_t * vLevels;
91     ProgressBar * pProgress;
92     Abc_Obj_t * pNode, * pFanin;
93     int i, k;
94 
95     // write the PIs
96     fprintf( pFile, "INORDER =" );
97     Io_NtkWriteEqnCis( pFile, pNtk );
98     fprintf( pFile, ";\n" );
99 
100     // write the POs
101     fprintf( pFile, "OUTORDER =" );
102     Io_NtkWriteEqnCos( pFile, pNtk );
103     fprintf( pFile, ";\n" );
104 
105     // write each internal node
106     vLevels = Vec_VecAlloc( 10 );
107     pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
108     Abc_NtkForEachNode( pNtk, pNode, i )
109     {
110         Extra_ProgressBarUpdate( pProgress, i, NULL );
111         fprintf( pFile, "%s = ", Abc_ObjName(Abc_ObjFanout0(pNode)) );
112         // set the input names
113         Abc_ObjForEachFanin( pNode, pFanin, k )
114             Hop_IthVar((Hop_Man_t *)pNtk->pManFunc, k)->pData = Abc_ObjName(pFanin);
115         // write the formula
116         Hop_ObjPrintEqn( pFile, (Hop_Obj_t *)pNode->pData, vLevels, 0 );
117         fprintf( pFile, ";\n" );
118     }
119     Extra_ProgressBarStop( pProgress );
120     Vec_VecFree( vLevels );
121 }
122 
123 
124 /**Function*************************************************************
125 
126   Synopsis    [Writes the primary input list.]
127 
128   Description []
129 
130   SideEffects []
131 
132   SeeAlso     []
133 
134 ***********************************************************************/
Io_NtkWriteEqnCis(FILE * pFile,Abc_Ntk_t * pNtk)135 void Io_NtkWriteEqnCis( FILE * pFile, Abc_Ntk_t * pNtk )
136 {
137     Abc_Obj_t * pTerm, * pNet;
138     int LineLength;
139     int AddedLength;
140     int NameCounter;
141     int i;
142 
143     LineLength  = 9;
144     NameCounter = 0;
145 
146     Abc_NtkForEachCi( pNtk, pTerm, i )
147     {
148         pNet = Abc_ObjFanout0(pTerm);
149         // get the line length after this name is written
150         AddedLength = strlen(Abc_ObjName(pNet)) + 1;
151         if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
152         { // write the line extender
153             fprintf( pFile, " \n" );
154             // reset the line length
155             LineLength  = 0;
156             NameCounter = 0;
157         }
158         fprintf( pFile, " %s", Abc_ObjName(pNet) );
159         LineLength += AddedLength;
160         NameCounter++;
161     }
162 }
163 
164 /**Function*************************************************************
165 
166   Synopsis    [Writes the primary input list.]
167 
168   Description []
169 
170   SideEffects []
171 
172   SeeAlso     []
173 
174 ***********************************************************************/
Io_NtkWriteEqnCos(FILE * pFile,Abc_Ntk_t * pNtk)175 void Io_NtkWriteEqnCos( FILE * pFile, Abc_Ntk_t * pNtk )
176 {
177     Abc_Obj_t * pTerm, * pNet;
178     int LineLength;
179     int AddedLength;
180     int NameCounter;
181     int i;
182 
183     LineLength  = 10;
184     NameCounter = 0;
185 
186     Abc_NtkForEachCo( pNtk, pTerm, i )
187     {
188         pNet = Abc_ObjFanin0(pTerm);
189         // get the line length after this name is written
190         AddedLength = strlen(Abc_ObjName(pNet)) + 1;
191         if ( NameCounter && LineLength + AddedLength + 3 > IO_WRITE_LINE_LENGTH )
192         { // write the line extender
193             fprintf( pFile, " \n" );
194             // reset the line length
195             LineLength  = 0;
196             NameCounter = 0;
197         }
198         fprintf( pFile, " %s", Abc_ObjName(pNet) );
199         LineLength += AddedLength;
200         NameCounter++;
201     }
202 }
203 
204 /**Function*************************************************************
205 
206   Synopsis    [Make sure the network does not have offending names.]
207 
208   Description []
209 
210   SideEffects []
211 
212   SeeAlso     []
213 
214 ***********************************************************************/
Io_NtkWriteEqnCheck(Abc_Ntk_t * pNtk)215 int Io_NtkWriteEqnCheck( Abc_Ntk_t * pNtk )
216 {
217     Abc_Obj_t * pObj;
218     char * pName = NULL;
219     int i, k, Length;
220     int RetValue = 1;
221 
222     // make sure the network does not have proper names, such as "0" or "1" or containing parentheses
223     Abc_NtkForEachObj( pNtk, pObj, i )
224     {
225         pName = Nm_ManFindNameById(pNtk->pManName, i);
226         if ( pName == NULL )
227             continue;
228         Length = strlen(pName);
229         if ( pName[0] == '0' || pName[0] == '1' )
230         {
231             RetValue = 0;
232             break;
233         }
234         for ( k = 0; k < Length; k++ )
235             if ( pName[k] == '(' || pName[k] == ')' || pName[k] == '!' || pName[k] == '*' || pName[k] == '+' )
236             {
237                 RetValue = 0;
238                 break;
239             }
240         if ( k < Length )
241             break;
242     }
243     if ( RetValue == 0 )
244     {
245         printf( "The network cannot be written in the EQN format because object %d has name \"%s\".\n", i, pName );
246         printf( "Consider renaming the objects using command \"short_names\" and trying again.\n" );
247     }
248     return RetValue;
249 }
250 
251 ////////////////////////////////////////////////////////////////////////
252 ///                       END OF FILE                                ///
253 ////////////////////////////////////////////////////////////////////////
254 
255 
256 ABC_NAMESPACE_IMPL_END
257 
258