1 /**CFile****************************************************************
2 
3   FileName    [exorUtil.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Exclusive sum-of-product minimization.]
8 
9   Synopsis    [Utilities.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: exorUtil.c,v 1.0 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 ////////////////////////////////////////////////////////////////////////
22 ///                                                                  ///
23 ///                  Implementation of EXORCISM - 4                  ///
24 ///              An Exclusive Sum-of-Product Minimizer               ///
25 ///               Alan Mishchenko  <alanmi@ee.pdx.edu>               ///
26 ///                                                                  ///
27 ////////////////////////////////////////////////////////////////////////
28 ///                                                                  ///
29 ///                        Utility Functions                         ///
30 ///                                                                  ///
31 ///       1) allocating memory for and creating the ESOP cover       ///
32 ///       2) writing the resultant cover into an ESOP PLA file       ///
33 ///                                                                  ///
34 ///  Ver. 1.0. Started - July 15, 2000. Last update - July 20, 2000  ///
35 ///  Ver. 1.4. Started -  Aug 10, 2000. Last update -  Aug 10, 2000  ///
36 ///  Ver. 1.5. Started -  Aug 19, 2000. Last update -  Aug 19, 2000  ///
37 ///  Ver. 1.7. Started -  Sep 20, 2000. Last update -  Sep 23, 2000  ///
38 ///                                                                  ///
39 ////////////////////////////////////////////////////////////////////////
40 ///   This software was tested with the BDD package "CUDD", v.2.3.0  ///
41 ///                          by Fabio Somenzi                        ///
42 ///                  http://vlsi.colorado.edu/~fabio/                ///
43 ////////////////////////////////////////////////////////////////////////
44 
45 #include "exor.h"
46 
47 ABC_NAMESPACE_IMPL_START
48 
49 ////////////////////////////////////////////////////////////////////////
50 ///                      EXTERNAL VARIABLES                         ////
51 ////////////////////////////////////////////////////////////////////////
52 
53 // information about the options, the function, and the cover
54 extern cinfo g_CoverInfo;
55 
56 ////////////////////////////////////////////////////////////////////////
57 ///                        EXTERNAL FUNCTIONS                        ///
58 ////////////////////////////////////////////////////////////////////////
59 
60 // Cube Cover Iterator
61 // starts an iterator that traverses all the cubes in the ring
62 extern Cube* IterCubeSetStart();
63 // returns the next cube in the ring
64 extern Cube* IterCubeSetNext();
65 
66 // retrieves the variable from the cube
67 extern varvalue GetVar( Cube* pC, int Var );
68 
69 ////////////////////////////////////////////////////////////////////////
70 ///                      FUNCTION DECLARATIONS                       ///
71 ////////////////////////////////////////////////////////////////////////
72 
73 ///////////////////////////////////////////////////////////////////
74 ////////////       Cover Service Procedures       /////////////////
75 ///////////////////////////////////////////////////////////////////
76 
CountLiterals()77 int CountLiterals()
78 {
79     Cube* p;
80     int LitCounter = 0;
81     for ( p = IterCubeSetStart( ); p; p = IterCubeSetNext() )
82         LitCounter += p->a;
83     return LitCounter;
84 }
85 
CountLiteralsCheck()86 int CountLiteralsCheck()
87 {
88     Cube* p;
89     int Value, v;
90     int LitCounter = 0;
91     int LitCounterControl = 0;
92 
93     for ( p = IterCubeSetStart( ); p; p = IterCubeSetNext() )
94     {
95         LitCounterControl += p->a;
96 
97         assert( p->fMark == 0 );
98 
99         // write the input variables
100         for ( v = 0; v < g_CoverInfo.nVarsIn; v++ )
101         {
102             Value = GetVar( p, v );
103             if ( Value == VAR_NEG )
104                 LitCounter++;
105             else if ( Value == VAR_POS )
106                 LitCounter++;
107             else if ( Value != VAR_ABS )
108             {
109                 assert(0);
110             }
111         }
112     }
113 
114     if ( LitCounterControl != LitCounter )
115         printf( "Warning! The recorded number of literals (%d) differs from the actual number (%d)\n", LitCounterControl, LitCounter );
116     return LitCounter;
117 }
118 
CountQCost()119 int CountQCost()
120 {
121     Cube* p;
122     int QCost = 0;
123     int QCostControl = 0;
124     for ( p = IterCubeSetStart( ); p; p = IterCubeSetNext() )
125     {
126         QCostControl += p->q;
127         QCost += ComputeQCostBits( p );
128     }
129 //    if ( QCostControl != QCost )
130 //        printf( "Warning! The recorded number of literals (%d) differs from the actual number (%d)\n", QCostControl, QCost );
131     return QCost;
132 }
133 
134 
WriteTableIntoFile(FILE * pFile)135 void WriteTableIntoFile( FILE * pFile )
136 // nCubesAlloc is the number of allocated cubes
137 {
138     int v, w;
139     Cube * p;
140     int cOutputs;
141     int nOutput;
142     int WordSize;
143 
144     for ( p = IterCubeSetStart( ); p; p = IterCubeSetNext() )
145     {
146         assert( p->fMark == 0 );
147 
148         // write the input variables
149         for ( v = 0; v < g_CoverInfo.nVarsIn; v++ )
150         {
151             int Value = GetVar( p, v );
152             if ( Value == VAR_NEG )
153                 fprintf( pFile, "0" );
154             else if ( Value == VAR_POS )
155                 fprintf( pFile, "1" );
156             else if ( Value == VAR_ABS )
157                 fprintf( pFile, "-" );
158             else
159                 assert(0);
160         }
161         fprintf( pFile, " " );
162 
163         // write the output variables
164         cOutputs = 0;
165         nOutput = g_CoverInfo.nVarsOut;
166         WordSize = 8*sizeof( unsigned );
167         for ( w = 0; w < g_CoverInfo.nWordsOut; w++ )
168             for ( v = 0; v < WordSize; v++ )
169             {
170                 if ( p->pCubeDataOut[w] & (1<<v) )
171                     fprintf( pFile, "1" );
172                 else
173                     fprintf( pFile, "0" );
174                 if ( ++cOutputs == nOutput )
175                     break;
176             }
177         fprintf( pFile, "\n" );
178     }
179 }
180 
181 
WriteResultIntoFile(char * pFileName)182 int WriteResultIntoFile( char * pFileName )
183 // write the ESOP cover into the PLA file <NewFileName>
184 {
185     FILE * pFile;
186     time_t ltime;
187     char * TimeStr;
188 
189     pFile = fopen( pFileName, "w" );
190     if ( pFile == NULL )
191     {
192         fprintf( pFile, "\n\nCannot open the output file\n" );
193         return 1;
194     }
195 
196     // get current time
197     time( &ltime );
198     TimeStr = asctime( localtime( &ltime ) );
199     // get the number of literals
200     g_CoverInfo.nLiteralsAfter = CountLiteralsCheck();
201     g_CoverInfo.QCostAfter = CountQCost();
202     fprintf( pFile, "# EXORCISM-4 output for command line arguments: " );
203     fprintf( pFile, "\"-Q %d -V %d\"\n", g_CoverInfo.Quality, g_CoverInfo.Verbosity );
204     fprintf( pFile, "# Minimization performed %s", TimeStr );
205     fprintf( pFile, "# Initial statistics: " );
206     fprintf( pFile, "Cubes = %d  Literals = %d  QCost = %d\n", g_CoverInfo.nCubesBefore, g_CoverInfo.nLiteralsBefore, g_CoverInfo.QCostBefore );
207     fprintf( pFile, "# Final   statistics: " );
208     fprintf( pFile, "Cubes = %d  Literals = %d  QCost = %d\n", g_CoverInfo.nCubesInUse, g_CoverInfo.nLiteralsAfter, g_CoverInfo.QCostAfter );
209     fprintf( pFile, "# File reading and reordering time = %.2f sec\n", TICKS_TO_SECONDS(g_CoverInfo.TimeRead) );
210     fprintf( pFile, "# Starting cover generation time   = %.2f sec\n", TICKS_TO_SECONDS(g_CoverInfo.TimeStart) );
211     fprintf( pFile, "# Pure ESOP minimization time      = %.2f sec\n", TICKS_TO_SECONDS(g_CoverInfo.TimeMin) );
212     fprintf( pFile, ".i %d\n", g_CoverInfo.nVarsIn );
213     fprintf( pFile, ".o %d\n", g_CoverInfo.nVarsOut );
214     fprintf( pFile, ".p %d\n", g_CoverInfo.nCubesInUse );
215     fprintf( pFile, ".type esop\n" );
216     WriteTableIntoFile( pFile );
217     fprintf( pFile, ".e\n" );
218     fclose( pFile );
219     return 0;
220 }
221 
222 ///////////////////////////////////////////////////////////////////
223 ////////////              End of File             /////////////////
224 ///////////////////////////////////////////////////////////////////
225 
226 
227 ABC_NAMESPACE_IMPL_END
228 
229