1 /**CFile****************************************************************
2 
3   FileName    [plaSimple.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [SOP manager.]
8 
9   Synopsis    [Scalable SOP transformations.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - March 18, 2015.]
16 
17   Revision    [$Id: plaSimple.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "pla.h"
22 
23 ABC_NAMESPACE_IMPL_START
24 
25 ////////////////////////////////////////////////////////////////////////
26 ///                        DECLARATIONS                              ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 ////////////////////////////////////////////////////////////////////////
30 ///                     FUNCTION DEFINITIONS                         ///
31 ////////////////////////////////////////////////////////////////////////
32 
33 /**Function*************************************************************
34 
35   Synopsis    [Dump PLA manager into a BLIF file.]
36 
37   Description []
38 
39   SideEffects []
40 
41   SeeAlso     []
42 
43 ***********************************************************************/
Pla_ManDumpPla(Pla_Man_t * p,char * pFileName)44 void Pla_ManDumpPla( Pla_Man_t * p, char * pFileName )
45 {
46     // find the number of original variables
47     //int nVarsInit = Pla_ManDivNum(p) ? Vec_IntCountZero(&p->vDivs) : Pla_ManInNum(p);
48     FILE * pFile = fopen( pFileName, "wb" );
49     if ( pFile == NULL )
50         printf( "Cannot open file \"%s\" for writing.\n", pFileName );
51     else
52     {
53         //char * pLits = "-01?";
54         Vec_Str_t * vStr;
55         Vec_Int_t * vCube;
56         int i, k, Lit;
57         // comment
58         fprintf( pFile, "# PLA file written via PLA package in ABC on " );
59         fprintf( pFile, "%s", Extra_TimeStamp() );
60         fprintf( pFile, "\n\n" );
61         // header
62         fprintf( pFile, ".i %d\n", Pla_ManInNum(p) );
63         fprintf( pFile, ".o %d\n", 1 );
64         fprintf( pFile, ".p %d\n", Vec_WecSize(&p->vCubeLits) );
65         // SOP
66         vStr = Vec_StrStart( Pla_ManInNum(p) + 1 );
67         Vec_WecForEachLevel( &p->vCubeLits, vCube, i )
68         {
69             if ( !Vec_IntSize(vCube) )
70                 continue;
71             for ( k = 0; k < Pla_ManInNum(p); k++ )
72                 Vec_StrWriteEntry( vStr, k, '-' );
73             Vec_IntForEachEntry( vCube, Lit, k )
74                 Vec_StrWriteEntry( vStr, Abc_Lit2Var(Lit), (char)(Abc_LitIsCompl(Lit) ? '0' : '1') );
75             fprintf( pFile, "%s 1\n", Vec_StrArray(vStr) );
76         }
77         Vec_StrFree( vStr );
78         fprintf( pFile, ".e\n\n" );
79         fclose( pFile );
80         printf( "Written file \"%s\".\n", pFileName );
81     }
82 }
Pla_ManDumpBlif(Pla_Man_t * p,char * pFileName)83 void Pla_ManDumpBlif( Pla_Man_t * p, char * pFileName )
84 {
85     // find the number of original variables
86     int nVarsInit = Pla_ManDivNum(p) ? Vec_IntCountZero(&p->vDivs) : Pla_ManInNum(p);
87     FILE * pFile = fopen( pFileName, "wb" );
88     if ( pFile == NULL )
89         printf( "Cannot open file \"%s\" for writing.\n", pFileName );
90     else
91     {
92         //char * pLits = "-01?";
93         Vec_Str_t * vStr;
94         Vec_Int_t * vCube;
95         int i, k, Lit, Div;
96         // comment
97         fprintf( pFile, "# BLIF file written via PLA package in ABC on " );
98         fprintf( pFile, "%s", Extra_TimeStamp() );
99         fprintf( pFile, "\n\n" );
100         // header
101         fprintf( pFile, ".model %s\n", Pla_ManName(p) );
102         fprintf( pFile, ".inputs" );
103         for ( i = 0; i < nVarsInit; i++ )
104             fprintf( pFile, " i%d", i );
105         fprintf( pFile, "\n" );
106         fprintf( pFile, ".outputs o" );
107         fprintf( pFile, "\n" );
108         // SOP
109         fprintf( pFile, ".names" );
110         for ( i = 0; i < Pla_ManInNum(p); i++ )
111             fprintf( pFile, " i%d", i );
112         fprintf( pFile, " o\n" );
113         vStr = Vec_StrStart( Pla_ManInNum(p) + 1 );
114         Vec_WecForEachLevel( &p->vCubeLits, vCube, i )
115         {
116             for ( k = 0; k < Pla_ManInNum(p); k++ )
117                 Vec_StrWriteEntry( vStr, k, '-' );
118             Vec_IntForEachEntry( vCube, Lit, k )
119                 Vec_StrWriteEntry( vStr, Abc_Lit2Var(Lit), (char)(Abc_LitIsCompl(Lit) ? '0' : '1') );
120             fprintf( pFile, "%s 1\n", Vec_StrArray(vStr) );
121         }
122         Vec_StrFree( vStr );
123         // divisors
124         Vec_IntForEachEntryStart( &p->vDivs, Div, i, nVarsInit )
125         {
126             int pLits[3] = { (Div >> 2) & 0x3FF, (Div >> 12) & 0x3FF, (Div >> 22) & 0x3FF };
127             fprintf( pFile, ".names" );
128             fprintf( pFile, " i%d", Abc_Lit2Var(pLits[0]) );
129             fprintf( pFile, " i%d", Abc_Lit2Var(pLits[1]) );
130             if ( (Div & 3) == 3 ) // MUX
131                 fprintf( pFile, " i%d", Abc_Lit2Var(pLits[2]) );
132             fprintf( pFile, " i%d\n", i );
133             if ( (Div & 3) == 1 ) // AND
134                 fprintf( pFile, "%d%d 1\n", !Abc_LitIsCompl(pLits[0]), !Abc_LitIsCompl(pLits[1]) );
135             else if ( (Div & 3) == 2 ) // XOR
136             {
137                 assert( !Abc_LitIsCompl(pLits[0]) );
138                 assert( !Abc_LitIsCompl(pLits[1]) );
139                 fprintf( pFile, "10 1\n01 1\n" );
140             }
141             else if ( (Div & 3) == 3 ) // MUX
142             {
143                 assert( !Abc_LitIsCompl(pLits[1]) );
144                 assert( !Abc_LitIsCompl(pLits[2]) );
145                 fprintf( pFile, "%d-0 1\n-11 1\n", !Abc_LitIsCompl(pLits[0]) );
146             }
147             else assert( 0 );
148         }
149         fprintf( pFile, ".end\n\n" );
150         fclose( pFile );
151         printf( "Written file \"%s\".\n", pFileName );
152     }
153 }
154 
155 /**Function*************************************************************
156 
157   Synopsis    [Transforms truth table into an SOP manager.]
158 
159   Description []
160 
161   SideEffects []
162 
163   SeeAlso     []
164 
165 ***********************************************************************/
Pla_ManExpendDirNum(word * pOn,int nBits,int iMint,int * pVars)166 int Pla_ManExpendDirNum( word * pOn, int nBits, int iMint, int * pVars )
167 {
168     int v, nVars = 0;
169     for ( v = 0; v < nBits; v++ )
170         if ( Pla_TtGetBit(pOn, iMint ^ (1 << v)) )
171             pVars[nVars++] = v;
172     return nVars;
173 }
Pla_PrintBinary(word * pT,int nBits)174 void Pla_PrintBinary( word * pT, int nBits )
175 {
176     int v;
177     for ( v = 0; v < nBits; v++ )
178         printf( "%d", Pla_TtGetBit(pT, v) );
179     printf( "\n" );
180 }
Pla_ManFxMinimize(word * pOn,int nVars)181 Vec_Wrd_t * Pla_ManFxMinimize( word * pOn, int nVars )
182 {
183     int i, v, iMint, iVar, nMints = (1 << nVars);
184     int nWords = Abc_Bit6WordNum( nMints );
185     Vec_Wrd_t * vCubes = Vec_WrdAlloc( 1000 );
186     word * pDc = ABC_CALLOC( word, nWords );
187     int Count[32] = {0};
188     int Cubes[32] = {0};
189     Vec_Int_t * vStore = Vec_IntAlloc( 1000 );
190 
191     // count the number of expansion directions
192     for ( i = 0; i < nMints; i++ )
193         if ( Pla_TtGetBit(pOn, i) && !Pla_TtGetBit(pDc, i) )
194         {
195             int pDirs[32], nDirs = Pla_ManExpendDirNum(pOn, nVars, i, pDirs);
196             Count[nDirs]++;
197             if ( nDirs == 0 )
198             {
199                 Pla_TtSetBit(pDc, i);
200                 Cubes[0]++;
201                 // save
202                 Vec_IntPushTwo( vStore, i, -1 );
203                 continue;
204             }
205             if ( nDirs == 1 )
206             {
207                 //Pla_PrintBinary( (word *)&i, nVars );
208                 //printf( " %d \n", pDirs[0] );
209 
210                 Pla_TtSetBit(pDc, i);
211                 Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]));
212                 Cubes[1]++;
213                 // save
214                 Vec_IntPushTwo( vStore, i, pDirs[0] );
215                 continue;
216             }
217             if ( nDirs == 2 && Pla_TtGetBit(pOn, i ^ (1 << pDirs[0]) ^ (1 << pDirs[1])) )
218             {
219                 assert( 0 );
220                 Pla_TtSetBit(pDc, i);
221                 Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]));
222                 Pla_TtSetBit(pDc, i ^ (1 << pDirs[1]));
223                 Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]) ^ (1 << pDirs[1]));
224                 Cubes[2]++;
225                 continue;
226             }
227         }
228 
229     // go through the remaining cubes
230     for ( i = 0; i < nMints; i++ )
231         if ( Pla_TtGetBit(pOn, i) && !Pla_TtGetBit(pDc, i) )
232         {
233             int pDirs[32], nDirs = Pla_ManExpendDirNum(pOn, nVars, i, pDirs);
234             // find direction, which is not taken
235             for ( v = 0; v < nDirs; v++ )
236                 if ( Pla_TtGetBit(pOn, i ^ (1 << pDirs[v])) && !Pla_TtGetBit(pDc, i ^ (1 << pDirs[v])) )
237                     break;
238             // if there is no open directions, use any one
239             if ( v == nDirs )
240                 v = 0;
241             // mark this one
242             Pla_TtSetBit(pDc, i);
243             Pla_TtSetBit(pDc, i ^ (1 << pDirs[v]));
244             Cubes[10]++;
245             // save
246             Vec_IntPushTwo( vStore, i, pDirs[v] );
247             continue;
248         }
249 
250     printf( "\n" );
251     printf( "Truth = %d. ", Pla_TtCountOnes(pOn, nWords) );
252     printf( "Cover = %d. ", Pla_TtCountOnes(pDc, nWords) );
253     printf( "\n" );
254 
255     printf( "Count: " );
256     for ( i = 0; i < 16; i++ )
257         if ( Count[i] )
258             printf( "%d=%d ", i, Count[i] );
259     printf( "\n" );
260 
261     printf( "Cubes: " );
262     for ( i = 0; i < 16; i++ )
263         if ( Cubes[i] )
264             printf( "%d=%d ", i, Cubes[i] );
265     printf( "\n" );
266 
267 /*
268     // extract cubes one at a time
269     for ( i = 0; i < nMints; i++ )
270         if ( Pla_TtGetBit(pOn, i) )
271         {
272             word Cube = 0;
273             for ( v = 0; v < nVars; v++ )
274                 if ( (i >> v) & 1 )
275                     Pla_CubeSetLit( &Cube, v, PLA_LIT_ONE );
276                 else
277                     Pla_CubeSetLit( &Cube, v, PLA_LIT_ZERO );
278             Vec_WrdPush( vCubes, Cube );
279         }
280 */
281     Vec_IntForEachEntryDouble( vStore, iMint, iVar, i )
282     {
283         word Cube = 0;
284         for ( v = 0; v < nVars; v++ )
285         {
286             if ( v == iVar )
287                 continue;
288             if ( (iMint >> v) & 1 )
289                 Pla_CubeSetLit( &Cube, v, PLA_LIT_ONE );
290             else
291                 Pla_CubeSetLit( &Cube, v, PLA_LIT_ZERO );
292         }
293         Vec_WrdPush( vCubes, Cube );
294     }
295     Vec_IntFree( vStore );
296 
297     // collect the minterms
298     ABC_FREE( pDc );
299     return vCubes;
300 }
Pla_ManFxPrepare(int nVars)301 Pla_Man_t * Pla_ManFxPrepare( int nVars )
302 {
303     Pla_Man_t * p; char Buffer[1000];
304     Vec_Bit_t * vFunc = Pla_ManPrimesTable( nVars );
305     Vec_Wrd_t * vSop = Pla_ManFxMinimize( (word *)Vec_BitArray(vFunc), nVars );
306     word Cube, * pCube = &Cube; int i, k, Lit;
307     sprintf( Buffer, "primes%02d", nVars );
308     p = Pla_ManAlloc( Buffer, nVars, 1, Vec_WrdSize(vSop) );
309     Vec_WecInit( &p->vCubeLits, Pla_ManCubeNum(p) );
310     Vec_WecInit( &p->vOccurs, 2*Pla_ManInNum(p) );
311     Vec_WrdForEachEntry( vSop, Cube, i )
312         Pla_CubeForEachLit( nVars, pCube, Lit, k )
313             if ( Lit != PLA_LIT_DASH )
314             {
315                 Lit = Abc_Var2Lit( k, Lit == PLA_LIT_ZERO );
316                 Vec_WecPush( &p->vCubeLits, i, Lit );
317                 Vec_WecPush( &p->vOccurs, Lit, i );
318             }
319     Vec_BitFree( vFunc );
320     Vec_WrdFree( vSop );
321     return p;
322 }
Pla_ManFxPerformSimple(int nVars)323 int Pla_ManFxPerformSimple( int nVars )
324 {
325     char Buffer[100];
326     Pla_Man_t * p = Pla_ManFxPrepare( nVars );
327     sprintf( Buffer, "primesmin%02d.pla", nVars );
328     Pla_ManDumpPla( p, Buffer );
329     Pla_ManFree( p );
330     return 1;
331 }
332 
333 ////////////////////////////////////////////////////////////////////////
334 ///                       END OF FILE                                ///
335 ////////////////////////////////////////////////////////////////////////
336 
337 
338 ABC_NAMESPACE_IMPL_END
339 
340