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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/**CFile****************************************************************
FileName [plaSimple.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [SOP manager.]
Synopsis [Scalable SOP transformations.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - March 18, 2015.]
Revision [$Id: plaSimple.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]
***********************************************************************/
#include "pla.h"
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Dump PLA manager into a BLIF file.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Pla_ManDumpPla( Pla_Man_t * p, char * pFileName )
{
// find the number of original variables
int nVarsInit = Pla_ManDivNum(p) ? Vec_IntCountZero(&p->vDivs) : Pla_ManInNum(p);
FILE * pFile = fopen( pFileName, "wb" );
if ( pFile == NULL )
printf( "Cannot open file \"%s\" for writing.\n", pFileName );
else
{
//char * pLits = "-01?";
Vec_Str_t * vStr;
Vec_Int_t * vCube;
int i, k, Lit;
// comment
fprintf( pFile, "# PLA file written via PLA package in ABC on " );
fprintf( pFile, "%s", Extra_TimeStamp() );
fprintf( pFile, "\n\n" );
// header
fprintf( pFile, ".i %d\n", Pla_ManInNum(p) );
fprintf( pFile, ".o %d\n", 1 );
fprintf( pFile, ".p %d\n", Vec_WecSize(&p->vCubeLits) );
// SOP
vStr = Vec_StrStart( Pla_ManInNum(p) + 1 );
Vec_WecForEachLevel( &p->vCubeLits, vCube, i )
{
if ( !Vec_IntSize(vCube) )
continue;
for ( k = 0; k < Pla_ManInNum(p); k++ )
Vec_StrWriteEntry( vStr, k, '-' );
Vec_IntForEachEntry( vCube, Lit, k )
Vec_StrWriteEntry( vStr, Abc_Lit2Var(Lit), (char)(Abc_LitIsCompl(Lit) ? '0' : '1') );
fprintf( pFile, "%s 1\n", Vec_StrArray(vStr) );
}
Vec_StrFree( vStr );
fprintf( pFile, ".e\n\n" );
fclose( pFile );
printf( "Written file \"%s\".\n", pFileName );
}
}
void Pla_ManDumpBlif( Pla_Man_t * p, char * pFileName )
{
// find the number of original variables
int nVarsInit = Pla_ManDivNum(p) ? Vec_IntCountZero(&p->vDivs) : Pla_ManInNum(p);
FILE * pFile = fopen( pFileName, "wb" );
if ( pFile == NULL )
printf( "Cannot open file \"%s\" for writing.\n", pFileName );
else
{
//char * pLits = "-01?";
Vec_Str_t * vStr;
Vec_Int_t * vCube;
int i, k, Lit, Div;
// comment
fprintf( pFile, "# BLIF file written via PLA package in ABC on " );
fprintf( pFile, "%s", Extra_TimeStamp() );
fprintf( pFile, "\n\n" );
// header
fprintf( pFile, ".model %s\n", Pla_ManName(p) );
fprintf( pFile, ".inputs" );
for ( i = 0; i < nVarsInit; i++ )
fprintf( pFile, " i%d", i );
fprintf( pFile, "\n" );
fprintf( pFile, ".outputs o" );
fprintf( pFile, "\n" );
// SOP
fprintf( pFile, ".names" );
for ( i = 0; i < Pla_ManInNum(p); i++ )
fprintf( pFile, " i%d", i );
fprintf( pFile, " o\n" );
vStr = Vec_StrStart( Pla_ManInNum(p) + 1 );
Vec_WecForEachLevel( &p->vCubeLits, vCube, i )
{
for ( k = 0; k < Pla_ManInNum(p); k++ )
Vec_StrWriteEntry( vStr, k, '-' );
Vec_IntForEachEntry( vCube, Lit, k )
Vec_StrWriteEntry( vStr, Abc_Lit2Var(Lit), (char)(Abc_LitIsCompl(Lit) ? '0' : '1') );
fprintf( pFile, "%s 1\n", Vec_StrArray(vStr) );
}
Vec_StrFree( vStr );
// divisors
Vec_IntForEachEntryStart( &p->vDivs, Div, i, nVarsInit )
{
int pLits[3] = { (Div >> 2) & 0x3FF, (Div >> 12) & 0x3FF, (Div >> 22) & 0x3FF };
fprintf( pFile, ".names" );
fprintf( pFile, " i%d", Abc_Lit2Var(pLits[0]) );
fprintf( pFile, " i%d", Abc_Lit2Var(pLits[1]) );
if ( (Div & 3) == 3 ) // MUX
fprintf( pFile, " i%d", Abc_Lit2Var(pLits[2]) );
fprintf( pFile, " i%d\n", i );
if ( (Div & 3) == 1 ) // AND
fprintf( pFile, "%d%d 1\n", !Abc_LitIsCompl(pLits[0]), !Abc_LitIsCompl(pLits[1]) );
else if ( (Div & 3) == 2 ) // XOR
{
assert( !Abc_LitIsCompl(pLits[0]) );
assert( !Abc_LitIsCompl(pLits[1]) );
fprintf( pFile, "10 1\n01 1\n" );
}
else if ( (Div & 3) == 3 ) // MUX
{
assert( !Abc_LitIsCompl(pLits[1]) );
assert( !Abc_LitIsCompl(pLits[2]) );
fprintf( pFile, "%d-0 1\n-11 1\n", !Abc_LitIsCompl(pLits[0]) );
}
else assert( 0 );
}
fprintf( pFile, ".end\n\n" );
fclose( pFile );
printf( "Written file \"%s\".\n", pFileName );
}
}
/**Function*************************************************************
Synopsis [Transforms truth table into an SOP manager.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Pla_ManExpendDirNum( word * pOn, int nBits, int iMint, int * pVars )
{
int v, nVars = 0;
for ( v = 0; v < nBits; v++ )
if ( Pla_TtGetBit(pOn, iMint ^ (1 << v)) )
pVars[nVars++] = v;
return nVars;
}
void Pla_PrintBinary( word * pT, int nBits )
{
int v;
for ( v = 0; v < nBits; v++ )
printf( "%d", Pla_TtGetBit(pT, v) );
printf( "\n" );
}
Vec_Wrd_t * Pla_ManFxMinimize( word * pOn, int nVars )
{
int i, v, iMint, iVar, nMints = (1 << nVars);
int nWords = Abc_Bit6WordNum( nMints );
Vec_Wrd_t * vCubes = Vec_WrdAlloc( 1000 );
word * pDc = ABC_CALLOC( word, nWords );
int Count[32] = {0};
int Cubes[32] = {0};
Vec_Int_t * vStore = Vec_IntAlloc( 1000 );
// count the number of expansion directions
for ( i = 0; i < nMints; i++ )
if ( Pla_TtGetBit(pOn, i) && !Pla_TtGetBit(pDc, i) )
{
int pDirs[32], nDirs = Pla_ManExpendDirNum(pOn, nVars, i, pDirs);
Count[nDirs]++;
if ( nDirs == 0 )
{
Pla_TtSetBit(pDc, i);
Cubes[0]++;
// save
Vec_IntPushTwo( vStore, i, -1 );
continue;
}
if ( nDirs == 1 )
{
//Pla_PrintBinary( (word *)&i, nVars );
//printf( " %d \n", pDirs[0] );
Pla_TtSetBit(pDc, i);
Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]));
Cubes[1]++;
// save
Vec_IntPushTwo( vStore, i, pDirs[0] );
continue;
}
if ( nDirs == 2 && Pla_TtGetBit(pOn, i ^ (1 << pDirs[0]) ^ (1 << pDirs[1])) )
{
assert( 0 );
Pla_TtSetBit(pDc, i);
Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]));
Pla_TtSetBit(pDc, i ^ (1 << pDirs[1]));
Pla_TtSetBit(pDc, i ^ (1 << pDirs[0]) ^ (1 << pDirs[1]));
Cubes[2]++;
continue;
}
}
// go through the remaining cubes
for ( i = 0; i < nMints; i++ )
if ( Pla_TtGetBit(pOn, i) && !Pla_TtGetBit(pDc, i) )
{
int pDirs[32], nDirs = Pla_ManExpendDirNum(pOn, nVars, i, pDirs);
// find direction, which is not taken
for ( v = 0; v < nDirs; v++ )
if ( Pla_TtGetBit(pOn, i ^ (1 << pDirs[v])) && !Pla_TtGetBit(pDc, i ^ (1 << pDirs[v])) )
break;
// if there is no open directions, use any one
if ( v == nDirs )
v = 0;
// mark this one
Pla_TtSetBit(pDc, i);
Pla_TtSetBit(pDc, i ^ (1 << pDirs[v]));
Cubes[10]++;
// save
Vec_IntPushTwo( vStore, i, pDirs[v] );
continue;
}
printf( "\n" );
printf( "Truth = %d. ", Pla_TtCountOnes(pOn, nWords) );
printf( "Cover = %d. ", Pla_TtCountOnes(pDc, nWords) );
printf( "\n" );
printf( "Count: " );
for ( i = 0; i < 16; i++ )
if ( Count[i] )
printf( "%d=%d ", i, Count[i] );
printf( "\n" );
printf( "Cubes: " );
for ( i = 0; i < 16; i++ )
if ( Cubes[i] )
printf( "%d=%d ", i, Cubes[i] );
printf( "\n" );
/*
// extract cubes one at a time
for ( i = 0; i < nMints; i++ )
if ( Pla_TtGetBit(pOn, i) )
{
word Cube = 0;
for ( v = 0; v < nVars; v++ )
if ( (i >> v) & 1 )
Pla_CubeSetLit( &Cube, v, PLA_LIT_ONE );
else
Pla_CubeSetLit( &Cube, v, PLA_LIT_ZERO );
Vec_WrdPush( vCubes, Cube );
}
*/
Vec_IntForEachEntryDouble( vStore, iMint, iVar, i )
{
word Cube = 0;
for ( v = 0; v < nVars; v++ )
{
if ( v == iVar )
continue;
if ( (iMint >> v) & 1 )
Pla_CubeSetLit( &Cube, v, PLA_LIT_ONE );
else
Pla_CubeSetLit( &Cube, v, PLA_LIT_ZERO );
}
Vec_WrdPush( vCubes, Cube );
}
Vec_IntFree( vStore );
// collect the minterms
ABC_FREE( pDc );
return vCubes;
}
Pla_Man_t * Pla_ManFxPrepare( int nVars )
{
Pla_Man_t * p; char Buffer[1000];
Vec_Bit_t * vFunc = Pla_ManPrimesTable( nVars );
Vec_Wrd_t * vSop = Pla_ManFxMinimize( (word *)Vec_BitArray(vFunc), nVars );
word Cube, * pCube = &Cube; int i, k, Lit;
sprintf( Buffer, "primes%02d", nVars );
p = Pla_ManAlloc( Buffer, nVars, 1, Vec_WrdSize(vSop) );
Vec_WecInit( &p->vCubeLits, Pla_ManCubeNum(p) );
Vec_WecInit( &p->vOccurs, 2*Pla_ManInNum(p) );
Vec_WrdForEachEntry( vSop, Cube, i )
Pla_CubeForEachLit( nVars, pCube, Lit, k )
if ( Lit != PLA_LIT_DASH )
{
Lit = Abc_Var2Lit( k, Lit == PLA_LIT_ZERO );
Vec_WecPush( &p->vCubeLits, i, Lit );
Vec_WecPush( &p->vOccurs, Lit, i );
}
Vec_BitFree( vFunc );
Vec_WrdFree( vSop );
return p;
}
int Pla_ManFxPerformSimple( int nVars )
{
char Buffer[100];
Pla_Man_t * p = Pla_ManFxPrepare( nVars );
sprintf( Buffer, "primesmin%02d.pla", nVars );
Pla_ManDumpPla( p, Buffer );
Pla_ManFree( p );
return 1;
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|