ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
abcGen.c
Go to the documentation of this file.
1
20
21#include "base/abc/abc.h"
22#include "aig/miniaig/miniaig.h"
23
25
29
33
45void Abc_WriteHalfAdder( FILE * pFile )
46{
47 int fNaive = 0;
48 fprintf( pFile, ".model HA\n" );
49 fprintf( pFile, ".inputs a b\n" );
50 fprintf( pFile, ".outputs s cout\n" );
51 if ( fNaive )
52 {
53 fprintf( pFile, ".names a b s\n" );
54 fprintf( pFile, "10 1\n" );
55 fprintf( pFile, "01 1\n" );
56 fprintf( pFile, ".names a b cout\n" );
57 fprintf( pFile, "11 1\n" );
58 }
59 else
60 {
61 fprintf( pFile, ".names a b cout\n" );
62 fprintf( pFile, "11 1\n" );
63 fprintf( pFile, ".names a b and1_\n" );
64 fprintf( pFile, "00 1\n" );
65 fprintf( pFile, ".names cout and1_ s\n" );
66 fprintf( pFile, "00 1\n" );
67 }
68 fprintf( pFile, ".end\n" );
69 fprintf( pFile, "\n" );
70}
71void Abc_WriteFullAdder( FILE * pFile )
72{
73 int fNaive = 0;
74 fprintf( pFile, ".model FA\n" );
75 fprintf( pFile, ".inputs a b cin\n" );
76 fprintf( pFile, ".outputs s cout\n" );
77 if ( fNaive )
78 {
79/*
80 fprintf( pFile, ".names a b k\n" );
81 fprintf( pFile, "10 1\n" );
82 fprintf( pFile, "01 1\n" );
83 fprintf( pFile, ".names k cin s\n" );
84 fprintf( pFile, "10 1\n" );
85 fprintf( pFile, "01 1\n" );
86 fprintf( pFile, ".names a b cin cout\n" );
87 fprintf( pFile, "11- 1\n" );
88 fprintf( pFile, "1-1 1\n" );
89 fprintf( pFile, "-11 1\n" );
90*/
91 fprintf( pFile, ".names a b s0\n" );
92 fprintf( pFile, "10 1\n" );
93 fprintf( pFile, "01 1\n" );
94 fprintf( pFile, ".names a b c0\n" );
95 fprintf( pFile, "11 1\n" );
96 fprintf( pFile, ".names s0 cin s\n" );
97 fprintf( pFile, "10 1\n" );
98 fprintf( pFile, "01 1\n" );
99 fprintf( pFile, ".names s0 cin c1\n" );
100 fprintf( pFile, "11 1\n" );
101 fprintf( pFile, ".names c0 c1 cout\n" );
102 fprintf( pFile, "00 0\n" );
103 }
104 else
105 {
106 fprintf( pFile, ".names a b and1\n" );
107 fprintf( pFile, "11 1\n" );
108 fprintf( pFile, ".names a b and1_\n" );
109 fprintf( pFile, "00 1\n" );
110 fprintf( pFile, ".names and1 and1_ xor\n" );
111 fprintf( pFile, "00 1\n" );
112
113 fprintf( pFile, ".names cin xor and2\n" );
114 fprintf( pFile, "11 1\n" );
115 fprintf( pFile, ".names cin xor and2_\n" );
116 fprintf( pFile, "00 1\n" );
117 fprintf( pFile, ".names and2 and2_ s\n" );
118 fprintf( pFile, "00 1\n" );
119
120 fprintf( pFile, ".names and1 and2 cout\n" );
121 fprintf( pFile, "00 0\n" );
122 }
123 fprintf( pFile, ".end\n" );
124 fprintf( pFile, "\n" );
125}
126void Abc_WriteAdder( FILE * pFile, int nVars )
127{
128 int i, nDigits = Abc_Base10Log( nVars );
129
130 assert( nVars > 0 );
131 fprintf( pFile, ".model ADD%d\n", nVars );
132
133 fprintf( pFile, ".inputs" );
134 for ( i = 0; i < nVars; i++ )
135 fprintf( pFile, " a%0*d", nDigits, i );
136 for ( i = 0; i < nVars; i++ )
137 fprintf( pFile, " b%0*d", nDigits, i );
138 fprintf( pFile, "\n" );
139
140 fprintf( pFile, ".outputs" );
141 for ( i = 0; i <= nVars; i++ )
142 fprintf( pFile, " s%0*d", nDigits, i );
143 fprintf( pFile, "\n" );
144
145 fprintf( pFile, ".names c\n" );
146 if ( nVars == 1 )
147 fprintf( pFile, ".subckt FA a=a0 b=b0 cin=c s=y0 cout=s1\n" );
148 else
149 {
150 fprintf( pFile, ".subckt FA a=a%0*d b=b%0*d cin=c s=s%0*d cout=%0*d\n", nDigits, 0, nDigits, 0, nDigits, 0, nDigits, 0 );
151 for ( i = 1; i < nVars-1; i++ )
152 fprintf( pFile, ".subckt FA a=a%0*d b=b%0*d cin=%0*d s=s%0*d cout=%0*d\n", nDigits, i, nDigits, i, nDigits, i-1, nDigits, i, nDigits, i );
153 fprintf( pFile, ".subckt FA a=a%0*d b=b%0*d cin=%0*d s=s%0*d cout=s%0*d\n", nDigits, i, nDigits, i, nDigits, i-1, nDigits, i, nDigits, i+1 );
154 }
155 fprintf( pFile, ".end\n" );
156 fprintf( pFile, "\n" );
157 Abc_WriteFullAdder( pFile );
158}
159void Abc_GenAdder( char * pFileName, int nVars )
160{
161 FILE * pFile;
162 assert( nVars > 0 );
163 pFile = fopen( pFileName, "w" );
164 fprintf( pFile, "# %d-bit ripple-carry adder generated by ABC on %s\n", nVars, Extra_TimeStamp() );
165 Abc_WriteAdder( pFile, nVars );
166 fclose( pFile );
167}
168
180void Abc_WriteMulti( FILE * pFile, int nVars )
181{
182 int i, k, nDigits = Abc_Base10Log( nVars ), nDigits2 = Abc_Base10Log( 2*nVars );
183
184 assert( nVars > 0 );
185 fprintf( pFile, ".model Multi%d\n", nVars );
186
187 fprintf( pFile, ".inputs" );
188 for ( i = 0; i < nVars; i++ )
189 fprintf( pFile, " a%0*d", nDigits, i );
190 for ( i = 0; i < nVars; i++ )
191 fprintf( pFile, " b%0*d", nDigits, i );
192 fprintf( pFile, "\n" );
193
194 fprintf( pFile, ".outputs" );
195 for ( i = 0; i < 2*nVars; i++ )
196 fprintf( pFile, " m%0*d", nDigits2, i );
197 fprintf( pFile, "\n" );
198
199 for ( i = 0; i < 2*nVars; i++ )
200 fprintf( pFile, ".names x%0*d_%0*d\n", nDigits, 0, nDigits2, i );
201 for ( k = 0; k < nVars; k++ )
202 {
203 for ( i = 0; i < 2 * nVars; i++ )
204 if ( i >= k && i < k + nVars )
205 fprintf( pFile, ".names b%0*d a%0*d y%0*d_%0*d\n11 1\n", nDigits, k, nDigits, i-k, nDigits, k, nDigits2, i );
206 else
207 fprintf( pFile, ".names y%0*d_%0*d\n", nDigits, k, nDigits2, i );
208 fprintf( pFile, ".subckt ADD%d", 2*nVars );
209 for ( i = 0; i < 2*nVars; i++ )
210 fprintf( pFile, " a%0*d=x%0*d_%0*d", nDigits2, i, nDigits, k, nDigits2, i );
211 for ( i = 0; i < 2*nVars; i++ )
212 fprintf( pFile, " b%0*d=y%0*d_%0*d", nDigits2, i, nDigits, k, nDigits2, i );
213 for ( i = 0; i <= 2*nVars; i++ )
214 fprintf( pFile, " s%0*d=x%0*d_%0*d", nDigits2, i, nDigits, k+1, nDigits2, i );
215 fprintf( pFile, "\n" );
216 }
217 for ( i = 0; i < 2 * nVars; i++ )
218 fprintf( pFile, ".names x%0*d_%0*d m%0*d\n1 1\n", nDigits, k, nDigits2, i, nDigits2, i );
219 fprintf( pFile, ".end\n" );
220 fprintf( pFile, "\n" );
221 Abc_WriteAdder( pFile, 2*nVars );
222}
223void Abc_GenMulti( char * pFileName, int nVars )
224{
225 FILE * pFile;
226 assert( nVars > 0 );
227 pFile = fopen( pFileName, "w" );
228 fprintf( pFile, "# %d-bit multiplier generated by ABC on %s\n", nVars, Extra_TimeStamp() );
229 Abc_WriteMulti( pFile, nVars );
230 fclose( pFile );
231}
232
244void Abc_WriteComp( FILE * pFile )
245{
246 fprintf( pFile, ".model Comp\n" );
247 fprintf( pFile, ".inputs a b\n" );
248 fprintf( pFile, ".outputs x y\n" );
249 fprintf( pFile, ".names a b x\n" );
250 fprintf( pFile, "11 1\n" );
251 fprintf( pFile, ".names a b y\n" );
252 fprintf( pFile, "1- 1\n" );
253 fprintf( pFile, "-1 1\n" );
254 fprintf( pFile, ".end\n" );
255 fprintf( pFile, "\n" );
256}
257void Abc_WriteLayer( FILE * pFile, int nVars, int fSkip1 )
258{
259 int i;
260 fprintf( pFile, ".model Layer%d\n", fSkip1 );
261 fprintf( pFile, ".inputs" );
262 for ( i = 0; i < nVars; i++ )
263 fprintf( pFile, " x%02d", i );
264 fprintf( pFile, "\n" );
265 fprintf( pFile, ".outputs" );
266 for ( i = 0; i < nVars; i++ )
267 fprintf( pFile, " y%02d", i );
268 fprintf( pFile, "\n" );
269 if ( fSkip1 )
270 {
271 fprintf( pFile, ".names x00 y00\n" );
272 fprintf( pFile, "1 1\n" );
273 i = 1;
274 }
275 else
276 i = 0;
277 for ( ; i + 1 < nVars; i += 2 )
278 fprintf( pFile, ".subckt Comp a=x%02d b=x%02d x=y%02d y=y%02d\n", i, i+1, i, i+1 );
279 if ( i < nVars )
280 {
281 fprintf( pFile, ".names x%02d y%02d\n", i, i );
282 fprintf( pFile, "1 1\n" );
283 }
284 fprintf( pFile, ".end\n" );
285 fprintf( pFile, "\n" );
286}
287
299void Abc_GenSorter( char * pFileName, int nVars )
300{
301 FILE * pFile;
302 int i, k, Counter, nDigits;
303
304 assert( nVars > 1 );
305
306 pFile = fopen( pFileName, "w" );
307 fprintf( pFile, "# %d-bit sorter generated by ABC on %s\n", nVars, Extra_TimeStamp() );
308 fprintf( pFile, ".model Sorter%02d\n", nVars );
309
310 fprintf( pFile, ".inputs" );
311 for ( i = 0; i < nVars; i++ )
312 fprintf( pFile, " x%02d", i );
313 fprintf( pFile, "\n" );
314
315 fprintf( pFile, ".outputs" );
316 for ( i = 0; i < nVars; i++ )
317 fprintf( pFile, " y%02d", i );
318 fprintf( pFile, "\n" );
319
320 Counter = 0;
321 nDigits = Abc_Base10Log( (nVars-2)*nVars );
322 if ( nVars == 2 )
323 fprintf( pFile, ".subckt Comp a=x00 b=x01 x=y00 y=y01\n" );
324 else
325 {
326 fprintf( pFile, ".subckt Layer0" );
327 for ( k = 0; k < nVars; k++ )
328 fprintf( pFile, " x%02d=x%02d", k, k );
329 for ( k = 0; k < nVars; k++ )
330 fprintf( pFile, " y%02d=%0*d", k, nDigits, Counter++ );
331 fprintf( pFile, "\n" );
332 Counter -= nVars;
333 for ( i = 1; i < 2*nVars-2; i++ )
334 {
335 fprintf( pFile, ".subckt Layer%d", (i&1) );
336 for ( k = 0; k < nVars; k++ )
337 fprintf( pFile, " x%02d=%0*d", k, nDigits, Counter++ );
338 for ( k = 0; k < nVars; k++ )
339 fprintf( pFile, " y%02d=%0*d", k, nDigits, Counter++ );
340 fprintf( pFile, "\n" );
341 Counter -= nVars;
342 }
343 fprintf( pFile, ".subckt Layer%d", (i&1) );
344 for ( k = 0; k < nVars; k++ )
345 fprintf( pFile, " x%02d=%0*d", k, nDigits, Counter++ );
346 for ( k = 0; k < nVars; k++ )
347 fprintf( pFile, " y%02d=y%02d", k, k );
348 fprintf( pFile, "\n" );
349 }
350 fprintf( pFile, ".end\n" );
351 fprintf( pFile, "\n" );
352
353 Abc_WriteLayer( pFile, nVars, 0 );
354 Abc_WriteLayer( pFile, nVars, 1 );
355 Abc_WriteComp( pFile );
356 fclose( pFile );
357}
358
370void Abc_WriteCell( FILE * pFile )
371{
372 fprintf( pFile, ".model cell\n" );
373 fprintf( pFile, ".inputs px1 px2 py1 py2 x y\n" );
374 fprintf( pFile, ".outputs fx fy\n" );
375 fprintf( pFile, ".names x y a\n" );
376 fprintf( pFile, "11 1\n" );
377 fprintf( pFile, ".names px1 a x nx\n" );
378 fprintf( pFile, "11- 1\n" );
379 fprintf( pFile, "0-1 1\n" );
380 fprintf( pFile, ".names py1 a y ny\n" );
381 fprintf( pFile, "11- 1\n" );
382 fprintf( pFile, "0-1 1\n" );
383 fprintf( pFile, ".names px2 nx fx\n" );
384 fprintf( pFile, "10 1\n" );
385 fprintf( pFile, "01 1\n" );
386 fprintf( pFile, ".names py2 ny fy\n" );
387 fprintf( pFile, "10 1\n" );
388 fprintf( pFile, "01 1\n" );
389 fprintf( pFile, ".end\n" );
390 fprintf( pFile, "\n" );
391}
392
404void Abc_GenMesh( char * pFileName, int nVars )
405{
406 FILE * pFile;
407 int i, k;
408
409 assert( nVars > 0 );
410
411 pFile = fopen( pFileName, "w" );
412 fprintf( pFile, "# %dx%d mesh generated by ABC on %s\n", nVars, nVars, Extra_TimeStamp() );
413 fprintf( pFile, ".model mesh%d\n", nVars );
414
415 for ( i = 0; i < nVars; i++ )
416 for ( k = 0; k < nVars; k++ )
417 {
418 fprintf( pFile, ".inputs" );
419 fprintf( pFile, " p%d%dx1", i, k );
420 fprintf( pFile, " p%d%dx2", i, k );
421 fprintf( pFile, " p%d%dy1", i, k );
422 fprintf( pFile, " p%d%dy2", i, k );
423 fprintf( pFile, "\n" );
424 }
425 fprintf( pFile, ".inputs" );
426 for ( i = 0; i < nVars; i++ )
427 fprintf( pFile, " v%02d v%02d", 2*i, 2*i+1 );
428 fprintf( pFile, "\n" );
429
430 fprintf( pFile, ".outputs" );
431 fprintf( pFile, " fx00" );
432 fprintf( pFile, "\n" );
433
434 for ( i = 0; i < nVars; i++ ) // horizontal
435 for ( k = 0; k < nVars; k++ ) // vertical
436 {
437 fprintf( pFile, ".subckt cell" );
438 fprintf( pFile, " px1=p%d%dx1", i, k );
439 fprintf( pFile, " px2=p%d%dx2", i, k );
440 fprintf( pFile, " py1=p%d%dy1", i, k );
441 fprintf( pFile, " py2=p%d%dy2", i, k );
442 if ( k == nVars - 1 )
443 fprintf( pFile, " x=v%02d", i );
444 else
445 fprintf( pFile, " x=fx%d%d", i, k+1 );
446 if ( i == nVars - 1 )
447 fprintf( pFile, " y=v%02d", nVars+k );
448 else
449 fprintf( pFile, " y=fy%d%d", i+1, k );
450 // outputs
451 fprintf( pFile, " fx=fx%d%d", i, k );
452 fprintf( pFile, " fy=fy%d%d", i, k );
453 fprintf( pFile, "\n" );
454 }
455 fprintf( pFile, ".end\n" );
456 fprintf( pFile, "\n" );
457 fprintf( pFile, "\n" );
458
459 Abc_WriteCell( pFile );
460 fclose( pFile );
461}
462
463
475void Abc_WriteKLut( FILE * pFile, int nLutSize )
476{
477 int i, iVar, iNext, nPars = (1 << nLutSize);
478 fprintf( pFile, "\n" );
479 fprintf( pFile, ".model lut%d\n", nLutSize );
480 fprintf( pFile, ".inputs" );
481 for ( i = 0; i < nPars; i++ )
482 fprintf( pFile, " p%02d", i );
483 fprintf( pFile, "\n" );
484 fprintf( pFile, ".inputs" );
485 for ( i = 0; i < nLutSize; i++ )
486 fprintf( pFile, " i%d", i );
487 fprintf( pFile, "\n" );
488 fprintf( pFile, ".outputs o\n" );
489 fprintf( pFile, ".names n01 o\n" );
490 fprintf( pFile, "1 1\n" );
491 // write internal MUXes
492 iVar = 0;
493 iNext = 2;
494 for ( i = 1; i < nPars; i++ )
495 {
496 if ( i == iNext )
497 {
498 iNext *= 2;
499 iVar++;
500 }
501 if ( iVar == nLutSize - 1 )
502 fprintf( pFile, ".names i%d p%02d p%02d n%02d\n", iVar, 2*(i-nPars/2), 2*(i-nPars/2)+1, i );
503 else
504 fprintf( pFile, ".names i%d n%02d n%02d n%02d\n", iVar, 2*i, 2*i+1, i );
505 fprintf( pFile, "01- 1\n" );
506 fprintf( pFile, "1-1 1\n" );
507 }
508 fprintf( pFile, ".end\n" );
509 fprintf( pFile, "\n" );
510}
511
523void Abc_GenFpga( char * pFileName, int nLutSize, int nLuts, int nVars )
524{
525 int fGenerateFunc = 1;
526 FILE * pFile;
527 int nVarsLut = (1 << nLutSize); // the number of LUT variables
528 int nVarsLog = Abc_Base2Log( nVars + nLuts - 1 ); // the number of encoding vars
529 int nVarsDeg = (1 << nVarsLog); // the number of LUT variables (total)
530 int nParsLut = nLuts * (1 << nLutSize); // the number of LUT params
531 int nParsVar = nLuts * nLutSize * nVarsLog; // the number of var params
532 int i, j, k;
533
534 assert( nVars > 0 );
535
536 pFile = fopen( pFileName, "w" );
537 fprintf( pFile, "# Structure with %d %d-LUTs for %d-var function generated by ABC on %s\n", nLuts, nLutSize, nVars, Extra_TimeStamp() );
538 fprintf( pFile, ".model struct%dx%d_%d\n", nLuts, nLutSize, nVars );
539
540 fprintf( pFile, ".inputs" );
541 for ( i = 0; i < nParsLut; i++ )
542 {
543// if ( i % (1 << nLutSize) == 0 && i != (nLuts - 1) * (1 << nLutSize) )
544// continue;
545 fprintf( pFile, " pl%02d", i );
546 }
547 fprintf( pFile, "\n" );
548
549 fprintf( pFile, ".inputs" );
550 for ( i = 0; i < nParsVar; i++ )
551 fprintf( pFile, " pv%02d", i );
552 fprintf( pFile, "\n" );
553
554 fprintf( pFile, ".inputs" );
555 for ( i = 0; i < nVars; i++ )
556 fprintf( pFile, " v%02d", i );
557 fprintf( pFile, "\n" );
558
559 fprintf( pFile, ".outputs" );
560// fprintf( pFile, " v%02d", nVars + nLuts - 1 );
561 fprintf( pFile, " out" );
562 fprintf( pFile, "\n" );
563 fprintf( pFile, ".names Gnd\n" );
564 fprintf( pFile, " 0\n" );
565
566 // generate function
567 if ( fGenerateFunc )
568 {
569 fprintf( pFile, ".names v%02d func out\n", nVars + nLuts - 1 );
570 fprintf( pFile, "00 1\n11 1\n" );
571 fprintf( pFile, ".names" );
572 for ( i = 0; i < nVars; i++ )
573 fprintf( pFile, " v%02d", i );
574 fprintf( pFile, " func\n" );
575 for ( i = 0; i < nVars; i++ )
576 fprintf( pFile, "1" );
577 fprintf( pFile, " 1\n" );
578 }
579 else
580 fprintf( pFile, ".names v%02d out\n1 1\n", nVars + nLuts - 1 );
581
582 // generate LUTs
583 for ( i = 0; i < nLuts; i++ )
584 {
585 fprintf( pFile, ".subckt lut%d", nLutSize );
586 // generate config parameters
587 for ( k = 0; k < nVarsLut; k++ )
588 fprintf( pFile, " p%02d=pl%02d", k, i * nVarsLut + k );
589 // generate the inputs
590 for ( k = 0; k < nLutSize; k++ )
591 fprintf( pFile, " i%d=s%02d", k, i * nLutSize + k );
592 // generate the output
593 fprintf( pFile, " o=v%02d", nVars + i );
594 fprintf( pFile, "\n" );
595 }
596
597 // generate LUT inputs
598 for ( i = 0; i < nLuts; i++ )
599 {
600 for ( j = 0; j < nLutSize; j++ )
601 {
602 fprintf( pFile, ".subckt lut%d", nVarsLog );
603 // generate config parameters
604 for ( k = 0; k < nVarsDeg; k++ )
605 {
606 if ( k < nVars + nLuts - 1 && k < nVars + i )
607 fprintf( pFile, " p%02d=v%02d", k, k );
608 else
609 fprintf( pFile, " p%02d=Gnd", k );
610 }
611 // generate the inputs
612 for ( k = 0; k < nVarsLog; k++ )
613 fprintf( pFile, " i%d=pv%02d", k, (i * nLutSize + j) * nVarsLog + k );
614 // generate the output
615 fprintf( pFile, " o=s%02d", i * nLutSize + j );
616 fprintf( pFile, "\n" );
617 }
618 }
619
620 fprintf( pFile, ".end\n" );
621 fprintf( pFile, "\n" );
622
623 // generate LUTs
624 Abc_WriteKLut( pFile, nLutSize );
625 if ( nVarsLog != nLutSize )
626 Abc_WriteKLut( pFile, nVarsLog );
627 fclose( pFile );
628}
629
641void Abc_GenOneHot( char * pFileName, int nVars )
642{
643 FILE * pFile;
644 int i, k, Counter, nDigitsIn, nDigitsOut;
645 pFile = fopen( pFileName, "w" );
646 fprintf( pFile, "# One-hotness condition for %d vars generated by ABC on %s\n", nVars, Extra_TimeStamp() );
647 fprintf( pFile, ".model 1hot_%dvars\n", nVars );
648 fprintf( pFile, ".inputs" );
649 nDigitsIn = Abc_Base10Log( nVars );
650 for ( i = 0; i < nVars; i++ )
651 fprintf( pFile, " i%0*d", nDigitsIn, i );
652 fprintf( pFile, "\n" );
653 fprintf( pFile, ".outputs" );
654 nDigitsOut = Abc_Base10Log( nVars * (nVars - 1) / 2 );
655 for ( i = 0; i < nVars * (nVars - 1) / 2; i++ )
656 fprintf( pFile, " o%0*d", nDigitsOut, i );
657 fprintf( pFile, "\n" );
658 Counter = 0;
659 for ( i = 0; i < nVars; i++ )
660 for ( k = i+1; k < nVars; k++ )
661 {
662 fprintf( pFile, ".names i%0*d i%0*d o%0*d\n", nDigitsIn, i, nDigitsIn, k, nDigitsOut, Counter );
663 fprintf( pFile, "11 0\n" );
664 Counter++;
665 }
666 fprintf( pFile, ".end\n" );
667 fprintf( pFile, "\n" );
668 fclose( pFile );
669}
670
682void Abc_GenOneHotIntervals( char * pFileName, int nPis, int nRegs, Vec_Ptr_t * vOnehots )
683{
684 Vec_Int_t * vLine;
685 FILE * pFile;
686 int i, j, k, iReg1, iReg2, Counter, Counter2, nDigitsIn, nDigitsOut;
687 pFile = fopen( pFileName, "w" );
688 fprintf( pFile, "# One-hotness with %d vars and %d regs generated by ABC on %s\n", nPis, nRegs, Extra_TimeStamp() );
689 fprintf( pFile, "# Used %d intervals of 1-hot registers: { ", Vec_PtrSize(vOnehots) );
690 Counter = 0;
691 Vec_PtrForEachEntry( Vec_Int_t *, vOnehots, vLine, k )
692 {
693 fprintf( pFile, "%d ", Vec_IntSize(vLine) );
694 Counter += Vec_IntSize(vLine) * (Vec_IntSize(vLine) - 1) / 2;
695 }
696 fprintf( pFile, "}\n" );
697 fprintf( pFile, ".model 1hot_%dvars_%dregs\n", nPis, nRegs );
698 fprintf( pFile, ".inputs" );
699 nDigitsIn = Abc_Base10Log( nPis+nRegs );
700 for ( i = 0; i < nPis+nRegs; i++ )
701 fprintf( pFile, " i%0*d", nDigitsIn, i );
702 fprintf( pFile, "\n" );
703 fprintf( pFile, ".outputs" );
704 nDigitsOut = Abc_Base10Log( Counter );
705 for ( i = 0; i < Counter; i++ )
706 fprintf( pFile, " o%0*d", nDigitsOut, i );
707 fprintf( pFile, "\n" );
708 Counter2 = 0;
709 Vec_PtrForEachEntry( Vec_Int_t *, vOnehots, vLine, k )
710 {
711 Vec_IntForEachEntry( vLine, iReg1, i )
712 Vec_IntForEachEntryStart( vLine, iReg2, j, i+1 )
713 {
714 fprintf( pFile, ".names i%0*d i%0*d o%0*d\n", nDigitsIn, nPis+iReg1, nDigitsIn, nPis+iReg2, nDigitsOut, Counter2 );
715 fprintf( pFile, "11 0\n" );
716 Counter2++;
717 }
718 }
719 assert( Counter == Counter2 );
720 fprintf( pFile, ".end\n" );
721 fprintf( pFile, "\n" );
722 fclose( pFile );
723}
724
726
727#include "aig/aig/aig.h"
728
730
731
743void Abc_GenRandom( char * pFileName, int nPis )
744{
745 FILE * pFile;
746 unsigned * pTruth;
747 int i, b, w, nWords = Abc_TruthWordNum( nPis );
748 int nDigitsIn;
749 //Aig_ManRandom( 1 );
750 pTruth = ABC_ALLOC( unsigned, nWords );
751 for ( w = 0; w < nWords; w++ )
752 pTruth[w] = Aig_ManRandom( 0 );
753 pFile = fopen( pFileName, "w" );
754 fprintf( pFile, "# Random function with %d inputs generated by ABC on %s\n", nPis, Extra_TimeStamp() );
755 fprintf( pFile, ".model rand%d\n", nPis );
756 fprintf( pFile, ".inputs" );
757 nDigitsIn = Abc_Base10Log( nPis );
758 for ( i = 0; i < nPis; i++ )
759 fprintf( pFile, " i%0*d", nDigitsIn, i );
760 fprintf( pFile, "\n" );
761 fprintf( pFile, ".outputs f\n" );
762 fprintf( pFile, ".names" );
763 nDigitsIn = Abc_Base10Log( nPis );
764 for ( i = 0; i < nPis; i++ )
765 fprintf( pFile, " i%0*d", nDigitsIn, i );
766 fprintf( pFile, " f\n" );
767 for ( i = 0; i < (1<<nPis); i++ )
768 if ( Abc_InfoHasBit(pTruth, i) )
769 {
770 for ( b = nPis-1; b >= 0; b-- )
771 fprintf( pFile, "%d", (i>>b)&1 );
772 fprintf( pFile, " 1\n" );
773 }
774 fprintf( pFile, ".end\n" );
775 fprintf( pFile, "\n" );
776 fclose( pFile );
777 ABC_FREE( pTruth );
778}
779
780
792void Abc_GenFsmCond( Vec_Str_t * vCond, int nPis, int Prob )
793{
794 int i, Rand;
795 Vec_StrClear( vCond );
796 for ( i = 0; i < nPis; i++ )
797 {
798 Rand = Aig_ManRandom( 0 );
799 if ( Rand % 100 > Prob )
800 Vec_StrPush( vCond, '-' );
801 else if ( Rand & 1 )
802 Vec_StrPush( vCond, '1' );
803 else
804 Vec_StrPush( vCond, '0' );
805 }
806 Vec_StrPush( vCond, '\0' );
807}
808void Abc_GenFsm( char * pFileName, int nPis, int nPos, int nStates, int nLines, int ProbI, int ProbO )
809{
810 FILE * pFile;
811 Vec_Wrd_t * vStates;
812 Vec_Str_t * vCond;
813 int i, iState, iState2;
814 int nDigits = Abc_Base10Log( nStates );
815 Aig_ManRandom( 1 );
816 vStates = Vec_WrdAlloc( nLines );
817 vCond = Vec_StrAlloc( 1000 );
818 for ( i = 0; i < nStates; )
819 {
820 iState = Aig_ManRandom( 0 ) % nStates;
821 if ( iState == i )
822 continue;
823 Vec_WrdPush( vStates, ((word)i << 32) | iState );
824 i++;
825 }
826 for ( ; i < nLines; )
827 {
828 iState = Aig_ManRandom( 0 ) % nStates;
829 iState2 = Aig_ManRandom( 0 ) % nStates;
830 if ( iState2 == iState )
831 continue;
832 Vec_WrdPush( vStates, ((word)iState << 32) | iState2 );
833 i++;
834 }
835 Vec_WrdSort( vStates, 0 );
836 // write the file
837 pFile = fopen( pFileName, "w" );
838 fprintf( pFile, "# This random FSM was generated by ABC on %s\n", Extra_TimeStamp() );
839 fprintf( pFile, "# Command line was: \"genfsm -I %d -O %d -S %d -L %d -P %d -Q %d %s\"\n", nPis, nPos, nStates, nLines, ProbI, ProbO, pFileName );
840 fprintf( pFile, "# FSM has %d inputs, %d outputs, %d states, and %d products\n", nPis, nPos, nStates, nLines );
841 fprintf( pFile, ".i %d\n", nPis );
842 fprintf( pFile, ".o %d\n", nPos );
843 fprintf( pFile, ".p %d\n", nLines );
844 fprintf( pFile, ".s %d\n", nStates );
845 for ( i = 0; i < nLines; i++ )
846 {
847 Abc_GenFsmCond( vCond, nPis, ProbI );
848 fprintf( pFile, "%s ", Vec_StrArray(vCond) );
849 fprintf( pFile, "%0*d ", nDigits, (int)(Vec_WrdEntry(vStates, i) >> 32) );
850 fprintf( pFile, "%0*d ", nDigits, (int)(Vec_WrdEntry(vStates, i)) );
851 if ( nPos > 0 )
852 {
853 Abc_GenFsmCond( vCond, nPos, ProbO );
854 fprintf( pFile, "%s", Vec_StrArray(vCond) );
855 }
856 fprintf( pFile, "\n" );
857 }
858 fprintf( pFile, ".e" );
859 fprintf( pFile, "\n" );
860 fclose( pFile );
861 Vec_WrdFree( vStates );
862 Vec_StrFree( vCond );
863}
864
876void Abc_AdderTree( FILE * pFile, int nArgs, int nBits )
877{
878 int i, k, nDigits = Abc_Base10Log( nBits ), Log2 = Abc_Base2Log( nArgs );
879 assert( nArgs > 1 && nBits > 1 );
880 fprintf( pFile, "module adder_tree_%d_%d (\n ", nArgs, nBits );
881 for ( i = 0; i < nBits; i++, fprintf(pFile, "\n ") )
882 for ( k = 0; k < nArgs; k++ )
883 fprintf( pFile, " i%0*d_%0*d,", nDigits, k, nDigits, nBits-1-i );
884 fprintf( pFile, " z\n" );
885 fprintf( pFile, " );\n" );
886 for ( i = 0; i < nBits; i++ )
887 {
888 fprintf( pFile, " input" );
889 for ( k = 0; k < nArgs; k++ )
890 fprintf( pFile, " i%0*d_%0*d%s", nDigits, k, nDigits, nBits-1-i, k==nArgs-1 ? "":"," );
891 fprintf( pFile, ";\n" );
892 }
893 fprintf( pFile, " output [%d:0] z;\n", nBits+Log2-1 );
894 for ( i = 0; i < nArgs; i++ )
895 {
896 fprintf( pFile, " wire [%d:0] t%d = {", nBits-1, i );
897 for ( k = 0; k < nBits; k++ )
898 fprintf( pFile, " i%0*d_%0*d%s", nDigits, i, nDigits, nBits-1-k, k==nBits-1 ? "":"," );
899 fprintf( pFile, " };\n" );
900 }
901 for ( i = 0; i < nArgs-1; i++ )
902 fprintf( pFile, " wire [%d:0] s%d = t%d + %s%d;\n", nBits+Log2-1, i+1, i+1, i ? "s":"t", i );
903 fprintf( pFile, " assign z = s%d;\n", nArgs-1 );
904 fprintf( pFile, "endmodule\n\n" );
905}
906void Abc_GenAdderTree( char * pFileName, int nArgs, int nBits )
907{
908 FILE * pFile = fopen( pFileName, "w" );
909 fprintf( pFile, "// %d-argument %d-bit adder-tree generated by ABC on %s\n", nArgs, nBits, Extra_TimeStamp() );
910 Abc_AdderTree( pFile, nArgs, nBits );
911 fclose( pFile );
912}
913
914
926int Abc_GenSignedBoothPP( Gia_Man_t * p, int a, int b, int c, int d, int e )
927{
928/*
929 abc> lutexact -I 5 -N 7 -g F335ACC0
930
931 05 = 4'b0110( d e )
932 06 = 4'b0110( c d )
933 07 = 4'b0100( a 06 )
934 08 = 4'b1000( b 06 )
935 09 = 4'b0100( 05 07 )
936 10 = 4'b0110( 08 09 )
937 11 = 4'b0110( d 10 )
938*/
939 int n05 = Gia_ManHashXor( p, d, e );
940 int n06 = Gia_ManHashXor( p, c, d );
941 int n07 = Gia_ManHashAnd( p, a, Abc_LitNot(n06) );
942 int n08 = Gia_ManHashAnd( p, b, n06 );
943 int n09 = Gia_ManHashAnd( p, n05, Abc_LitNot(n07) );
944 int n10 = Gia_ManHashXor( p, n08, n09 );
945 int n11 = Gia_ManHashXor( p, d, n10 );
946 return n11;
947}
948Gia_Man_t * Abc_GenSignedBoothPPTest( int nArgA, int nArgB )
949{
950 Gia_Man_t * pNew; int i, iLit;
951 pNew = Gia_ManStart( 1000 );
952 pNew->pName = Abc_UtilStrsav( "booth" );
953 for ( i = 0; i < 5; i++ )
954 Gia_ManAppendCi(pNew);
955 iLit = Abc_GenSignedBoothPP( pNew, 2, 4, 6, 8, 10 );
956 Gia_ManAppendCo(pNew, iLit);
957 return pNew;
958}
959
960/*
961// parametrized implementation of signed Booth multiplier
962module booth #(
963 parameter N = 4 // bit-width of input a
964 ,parameter M = 4 // bit-width of input b
965)(
966 input [N-1:0] a // input data
967 ,input [M-1:0] b // input data
968 ,output [N+M-1:0] z // output data
969);
970
971 localparam TT = 32'hF335ACC0;
972 localparam W = N+M+1;
973 localparam L = (M+1)/2;
974
975 wire [W-1:0] data1[L:0];
976 wire [W-1:0] data2[L:0];
977
978 assign data2[0] = data1[0];
979 assign z = data2[L][N+M-1:0];
980
981 wire [N+1:0] a2 = {a[N-1], a, 1'b0};
982 wire [M+1:0] b2 = {b[M-1], b, 1'b0};
983
984 genvar j;
985 generate
986 for ( j = 0; j < W; j = j + 1 ) begin : J
987 assign data1[0][j] = (j%2 == 0 && j/2 < L) ? b2[j+2] : 1'b0;
988 end
989 endgenerate
990
991 genvar k, i0, i1, i2;
992 generate
993 for ( k = 0; k < 2*L; k = k + 2 ) begin : K
994
995 for ( i0 = 0; i0 < k; i0 = i0 + 1 ) begin : I0
996 assign data1[k/2+1][i0] = 1'b0;
997 end
998
999 for ( i1 = 0; i1 <= N; i1 = i1 + 1 ) begin : I1
1000 wire [4:0] in = {b2[k+2], b2[k+1], b2[k], a2[i1+1], a2[i1]};
1001 assign data1[k/2+1][k+i1] = (k > 0 && i1 == N) ? ~TT[in] : TT[in];
1002 end
1003
1004 assign data1[k/2+1][k+N+1] = k > 0 ? 1'b1 : data1[k/2+1][k+N];
1005 for ( i2 = k+N+2; i2 < W; i2 = i2 + 1 ) begin : I2
1006 assign data1[k/2+1][i2] = (k > 0 || i2 > k+N+2)? 1'b0 : ~data1[k/2+1][k+N];
1007 end
1008
1009 assign data2[k/2+1] = data2[k/2] + data1[k/2+1];
1010
1011 end
1012 endgenerate
1013
1014endmodule
1015*/
1016
1017Gia_Man_t * Abc_GenSignedBooth( int nArgN, int nArgM )
1018{
1019 int nWidth = nArgN + nArgM + 1;
1020 int Length = (nArgM + 1) / 2;
1021 int i, k, iLit;
1022
1023 Vec_Int_t * vPPs = Vec_IntAlloc( nWidth * (Length + 1) );
1024 Vec_Int_t * vArgN = Vec_IntAlloc( nArgN + 2 );
1025 Vec_Int_t * vArgM = Vec_IntAlloc( nArgM + 2 );
1026 int * pArgN = Vec_IntArray( vArgN );
1027 int * pArgM = Vec_IntArray( vArgM );
1028
1029 Gia_Man_t * pTemp, * pNew;
1030 pNew = Gia_ManStart( 1000 );
1031 pNew->pName = Abc_UtilStrsav( "booth" );
1032
1033 Vec_IntPush( vArgN, 0 );
1034 for ( i = 0; i < nArgN; i++ )
1035 Vec_IntPush( vArgN, Gia_ManAppendCi(pNew) );
1036 Vec_IntPush( vArgN, Vec_IntEntryLast(vArgN) );
1037
1038 Vec_IntPush( vArgM, 0 );
1039 for ( i = 0; i < nArgM; i++ )
1040 Vec_IntPush( vArgM, Gia_ManAppendCi(pNew) );
1041 Vec_IntPush( vArgM, Vec_IntEntryLast(vArgM) );
1042
1043 for ( i = 0; i < nWidth; i++ )
1044 Vec_IntPush( vPPs, (i%2 == 0 && i/2 < Length) ? pArgM[i+2] : 0 );
1045
1046 Gia_ManHashAlloc( pNew );
1047 for ( k = 0; k < 2*Length; k += 2 )
1048 {
1049 for ( i = 0; i < k; i++ )
1050 Vec_IntPush( vPPs, 0 );
1051 for ( i = 0; i <= nArgN; i++ )
1052 {
1053 iLit = Abc_GenSignedBoothPP( pNew, pArgN[i], pArgN[i+1], pArgM[k], pArgM[k+1], pArgM[k+2] );
1054 Vec_IntPush( vPPs, Abc_LitNotCond( iLit, k > 0 && i == nArgN ) );
1055 }
1056 iLit = Vec_IntEntryLast(vPPs);
1057 Vec_IntPush( vPPs, k > 0 ? 1 : iLit );
1058 for ( i = k+nArgN+2; i < nWidth; i++ )
1059 Vec_IntPush( vPPs, (k > 0 || i > k+nArgN+2) ? 0 : Abc_LitNot(iLit) );
1060 }
1061 Gia_ManHashStop( pNew );
1062
1063 for ( k = 0; k <= Length; k++ )
1064 for ( i = 0; i < nArgN+nArgM; i++ )
1065 Gia_ManAppendCo( pNew, Vec_IntEntry(vPPs, k*(nArgN+nArgM+1) + i) );
1066 Vec_IntFree( vPPs );
1067 Vec_IntFree( vArgN );
1068 Vec_IntFree( vArgM );
1069
1070 pNew = Gia_ManCleanup( pTemp = pNew );
1071 Gia_ManStop( pTemp );
1072 return pNew;
1073}
1074Mini_Aig_t * Abc_GenSignedBoothMini( int nArgN, int nArgM )
1075{
1076 extern Mini_Aig_t * Gia_ManToMiniAig( Gia_Man_t * pGia );
1077 Gia_Man_t * pGia = Abc_GenSignedBooth( nArgN, nArgM );
1078 Mini_Aig_t * pMini = Gia_ManToMiniAig( pGia );
1079 Gia_ManStop( pGia );
1080 return pMini;
1081}
1082
1083
1095void Abc_WriteBoothPartialProducts( FILE * pFile, int nVars )
1096{
1097 Mini_Aig_t * p = Abc_GenSignedBoothMini( nVars, nVars );
1098 int i, nNodes = Mini_AigNodeNum(p);
1099 int nDigits = Abc_Base10Log( nVars );
1100 int nDigits2 = Abc_Base10Log( 2*nVars );
1101 int nDigits3 = Abc_Base10Log( nNodes );
1102 int nOut = 0;
1103 fprintf( pFile, ".names pp%0*d\n", nDigits3, 0 );
1104 for ( i = 1; i < nNodes; i++ )
1105 {
1106 if ( Mini_AigNodeIsPi( p, i ) )
1107 {
1108 if ( i > 0 && i <= nVars )
1109 fprintf( pFile, ".names a%0*d pp%0*d\n1 1\n", nDigits, i-1, nDigits3, i );
1110 else if ( i > nVars && i <= 2*nVars )
1111 fprintf( pFile, ".names b%0*d pp%0*d\n1 1\n", nDigits, i-1-nVars, nDigits3, i );
1112 else assert( 0 );
1113 }
1114 else if ( Mini_AigNodeIsPo( p, i ) )
1115 {
1116 int Lit = Mini_AigNodeFanin0( p, i );
1117 fprintf( pFile, ".names pp%0*d y%0*d_%0*d\n%d 1\n", nDigits3, Abc_Lit2Var(Lit), nDigits, nOut/(2*nVars), nDigits2, nOut%(2*nVars), !Abc_LitIsCompl(Lit) );
1118 nOut++;
1119 }
1120 else if ( Mini_AigNodeIsAnd( p, i ) )
1121 {
1122 int Lit0 = Mini_AigNodeFanin0( p, i );
1123 int Lit1 = Mini_AigNodeFanin1( p, i );
1124 fprintf( pFile, ".names pp%0*d pp%0*d pp%0*d\n%d%d 1\n",
1125 nDigits3, Abc_Lit2Var(Lit0), nDigits3, Abc_Lit2Var(Lit1), nDigits3, i, !Abc_LitIsCompl(Lit0), !Abc_LitIsCompl(Lit1) );
1126 }
1127 else assert( 0 );
1128 }
1129 Mini_AigStop( p );
1130}
1131void Abc_WriteBooth( FILE * pFile, int nVars )
1132{
1133 int i, k, nDigits = Abc_Base10Log( nVars ), nDigits2 = Abc_Base10Log( 2*nVars );
1134 int Length = 1+(nVars + 1)/2;
1135
1136 assert( nVars > 0 );
1137 fprintf( pFile, ".model Multi%d\n", nVars );
1138
1139 fprintf( pFile, ".inputs" );
1140 for ( i = 0; i < nVars; i++ )
1141 fprintf( pFile, " a%0*d", nDigits, i );
1142 for ( i = 0; i < nVars; i++ )
1143 fprintf( pFile, " b%0*d", nDigits, i );
1144 fprintf( pFile, "\n" );
1145
1146 fprintf( pFile, ".outputs" );
1147 for ( i = 0; i < 2*nVars; i++ )
1148 fprintf( pFile, " m%0*d", nDigits2, i );
1149 fprintf( pFile, "\n" );
1150
1151 Abc_WriteBoothPartialProducts( pFile, nVars );
1152
1153 for ( i = 0; i < 2*nVars; i++ )
1154 fprintf( pFile, ".names x%0*d_%0*d\n", nDigits, 0, nDigits2, i );
1155 for ( k = 0; k < Length; k++ )
1156 {
1157 fprintf( pFile, ".subckt ADD%d", 2*nVars );
1158 for ( i = 0; i < 2*nVars; i++ )
1159 fprintf( pFile, " a%0*d=x%0*d_%0*d", nDigits2, i, nDigits, k, nDigits2, i );
1160 for ( i = 0; i < 2*nVars; i++ )
1161 fprintf( pFile, " b%0*d=y%0*d_%0*d", nDigits2, i, nDigits, k, nDigits2, i );
1162 for ( i = 0; i <= 2*nVars; i++ )
1163 fprintf( pFile, " s%0*d=x%0*d_%0*d", nDigits2, i, nDigits, k+1, nDigits2, i );
1164 fprintf( pFile, "\n" );
1165 }
1166 for ( i = 0; i < 2 * nVars; i++ )
1167 fprintf( pFile, ".names x%0*d_%0*d m%0*d\n1 1\n", nDigits, k, nDigits2, i, nDigits2, i );
1168 fprintf( pFile, ".end\n" );
1169 fprintf( pFile, "\n" );
1170 Abc_WriteAdder( pFile, 2*nVars );
1171}
1172void Abc_GenBooth( char * pFileName, int nVars )
1173{
1174 FILE * pFile;
1175 assert( nVars > 0 );
1176 pFile = fopen( pFileName, "w" );
1177 fprintf( pFile, "# %d-bit signed Booth multiplier generated by ABC on %s\n", nVars, Extra_TimeStamp() );
1178 Abc_WriteBooth( pFile, nVars );
1179 fclose( pFile );
1180}
1181
1182
1194void Abc_GenGraph( char * pFileName, int nPis )
1195{
1196 FILE * pFile;
1197 int i, a, b, w, nDigitsIn, nWords = Abc_TruthWordNum( nPis*(nPis-1)/2 );
1198 unsigned * pTruth = ABC_CALLOC( unsigned, nWords );
1199 unsigned char M[10][10] = {{0}}, C[100][2] = {{0}}, nVars = 0;
1200 assert( nPis <= 8 );
1201 for ( a = 0; a < nPis; a++ )
1202 for ( b = a+1; b < nPis; b++ )
1203 C[nVars][0] = a, C[nVars][1] = b, nVars++;
1204 for ( i = 0; i < (1<<nVars); i++ )
1205 {
1206 int fChanges = 1;
1207 for ( w = 0; w < nVars; w++ )
1208 M[C[w][0]][C[w][1]] = M[C[w][1]][C[w][0]] = (i >> w) & 1;
1209 while ( fChanges && !M[0][1] ) {
1210 fChanges = 0;
1211 for ( a = 0; a < nPis; a++ )
1212 for ( b = 0; b < nPis; b++ )
1213 if ( M[a][b] )
1214 for ( w = 0; w < nPis; w++ )
1215 if ( M[b][w] && !M[a][w] )
1216 M[a][w] = 1, fChanges = 1;
1217 }
1218 if ( M[0][1] )
1219 Abc_InfoSetBit(pTruth, i);
1220 }
1221 pFile = fopen( pFileName, "w" );
1222 fprintf( pFile, "# Function with %d inputs generated by ABC on %s\n", nVars, Extra_TimeStamp() );
1223 fprintf( pFile, ".model fun%d\n", nVars );
1224 fprintf( pFile, ".inputs" );
1225 nDigitsIn = Abc_Base10Log( nVars );
1226 for ( i = 0; i < nVars; i++ )
1227 fprintf( pFile, " i%0*d", nDigitsIn, i );
1228 fprintf( pFile, "\n" );
1229 fprintf( pFile, ".outputs f\n" );
1230 fprintf( pFile, ".names" );
1231 nDigitsIn = Abc_Base10Log( nVars );
1232 for ( b = nVars-1; b >= 0; b-- )
1233 fprintf( pFile, " i%0*d", nDigitsIn, b );
1234 fprintf( pFile, " f\n" );
1235 for ( i = 0; i < (1<<nVars); i++ )
1236 if ( Abc_InfoHasBit(pTruth, i) )
1237 {
1238 for ( b = nVars-1; b >= 0; b-- )
1239 fprintf( pFile, "%d", (i>>b)&1 );
1240 fprintf( pFile, " 1\n" );
1241 }
1242 fprintf( pFile, ".end\n" );
1243 fprintf( pFile, "\n" );
1244 fclose( pFile );
1245 ABC_FREE( pTruth );
1246}
1247
1259void Abc_GenComp63a4( FILE * pFile )
1260{
1261 fprintf( pFile, ".model C63a\n" );
1262 fprintf( pFile, ".inputs x0 x1 x2 x3 x4 x5\n" );
1263 fprintf( pFile, ".outputs z0 z1 z2\n" );
1264 fprintf( pFile, ".names x1 x2 x3 x0 n10\n" );
1265 fprintf( pFile, "--00 1\n" );
1266 fprintf( pFile, "-0-0 1\n" );
1267 fprintf( pFile, "0--0 1\n" );
1268 fprintf( pFile, "000- 1\n" );
1269 fprintf( pFile, ".names x4 x5 n13 n10 z0\n" );
1270 fprintf( pFile, "--00 1\n" );
1271 fprintf( pFile, "-1-0 1\n" );
1272 fprintf( pFile, "1--0 1\n" );
1273 fprintf( pFile, "111- 1\n" );
1274 fprintf( pFile, ".names x1 x2 x3 x0 n13\n" );
1275 fprintf( pFile, "-110 1\n" );
1276 fprintf( pFile, "1-10 1\n" );
1277 fprintf( pFile, "11-0 1\n" );
1278 fprintf( pFile, "-001 1\n" );
1279 fprintf( pFile, "0-01 1\n" );
1280 fprintf( pFile, "00-1 1\n" );
1281 fprintf( pFile, ".names x4 x5 n13 n16 z1\n" );
1282 fprintf( pFile, "1-00 1\n" );
1283 fprintf( pFile, "0-10 1\n" );
1284 fprintf( pFile, "-101 1\n" );
1285 fprintf( pFile, "-011 1\n" );
1286 fprintf( pFile, ".names x1 x2 x3 x4 n16\n" );
1287 fprintf( pFile, "1000 1\n" );
1288 fprintf( pFile, "0100 1\n" );
1289 fprintf( pFile, "0010 1\n" );
1290 fprintf( pFile, "1110 1\n" );
1291 fprintf( pFile, "0001 1\n" );
1292 fprintf( pFile, "1101 1\n" );
1293 fprintf( pFile, "1011 1\n" );
1294 fprintf( pFile, "0111 1\n" );
1295 fprintf( pFile, ".names x5 n16 z2\n" );
1296 fprintf( pFile, "10 1\n" );
1297 fprintf( pFile, "01 1\n" );
1298 fprintf( pFile, ".end\n\n" );
1299}
1300void Abc_GenComp63a6( FILE * pFile )
1301{
1302 fprintf( pFile, ".model C63a\n" );
1303 fprintf( pFile, ".inputs x0 x1 x2 x3 x4 x5\n" );
1304 fprintf( pFile, ".outputs z0 z1 z2\n" );
1305 fprintf( pFile, ".names x1 x2 x3 x4 x5 x0 z0\n" );
1306 fprintf( pFile, "---111 1\n" );
1307 fprintf( pFile, "--1-11 1\n" );
1308 fprintf( pFile, "--11-1 1\n" );
1309 fprintf( pFile, "-1--11 1\n" );
1310 fprintf( pFile, "-1-1-1 1\n" );
1311 fprintf( pFile, "-11--1 1\n" );
1312 fprintf( pFile, "-1111- 1\n" );
1313 fprintf( pFile, "1---11 1\n" );
1314 fprintf( pFile, "1--1-1 1\n" );
1315 fprintf( pFile, "1-1--1 1\n" );
1316 fprintf( pFile, "1-111- 1\n" );
1317 fprintf( pFile, "11---1 1\n" );
1318 fprintf( pFile, "11-11- 1\n" );
1319 fprintf( pFile, "111-1- 1\n" );
1320 fprintf( pFile, "1111-- 1\n" );
1321 fprintf( pFile, ".names x1 x2 x3 x4 x5 x0 z1\n" );
1322 fprintf( pFile, "-00001 1\n" );
1323 fprintf( pFile, "-00110 1\n" );
1324 fprintf( pFile, "-01010 1\n" );
1325 fprintf( pFile, "-01100 1\n" );
1326 fprintf( pFile, "-10010 1\n" );
1327 fprintf( pFile, "-10100 1\n" );
1328 fprintf( pFile, "-11000 1\n" );
1329 fprintf( pFile, "-11111 1\n" );
1330 fprintf( pFile, "0-0001 1\n" );
1331 fprintf( pFile, "0-0110 1\n" );
1332 fprintf( pFile, "0-1010 1\n" );
1333 fprintf( pFile, "0-1100 1\n" );
1334 fprintf( pFile, "00-001 1\n" );
1335 fprintf( pFile, "00-110 1\n" );
1336 fprintf( pFile, "000-01 1\n" );
1337 fprintf( pFile, "0000-1 1\n" );
1338 fprintf( pFile, "1-0010 1\n" );
1339 fprintf( pFile, "1-0100 1\n" );
1340 fprintf( pFile, "1-1000 1\n" );
1341 fprintf( pFile, "1-1111 1\n" );
1342 fprintf( pFile, "11-000 1\n" );
1343 fprintf( pFile, "11-111 1\n" );
1344 fprintf( pFile, "111-11 1\n" );
1345 fprintf( pFile, "1111-1 1\n" );
1346 fprintf( pFile, ".names x1 x2 x3 x4 x5 z2\n" );
1347 fprintf( pFile, "00001 1\n" );
1348 fprintf( pFile, "00010 1\n" );
1349 fprintf( pFile, "00100 1\n" );
1350 fprintf( pFile, "00111 1\n" );
1351 fprintf( pFile, "01000 1\n" );
1352 fprintf( pFile, "01011 1\n" );
1353 fprintf( pFile, "01101 1\n" );
1354 fprintf( pFile, "01110 1\n" );
1355 fprintf( pFile, "10000 1\n" );
1356 fprintf( pFile, "10011 1\n" );
1357 fprintf( pFile, "10101 1\n" );
1358 fprintf( pFile, "10110 1\n" );
1359 fprintf( pFile, "11001 1\n" );
1360 fprintf( pFile, "11010 1\n" );
1361 fprintf( pFile, "11100 1\n" );
1362 fprintf( pFile, "11111 1\n" );
1363 fprintf( pFile, ".end\n\n" );
1364}
1365void Abc_GenAdder4( FILE * pFile, int nBits, int nLutSize )
1366{
1367 int i, n;
1368 fprintf( pFile, ".model A%02d_4x\n", nBits );
1369 for ( n = 0; n < 4; n++ ) {
1370 fprintf( pFile, ".inputs" );
1371 for ( i = 0; i < nBits; i++ )
1372 fprintf( pFile, " %c%02d", 'a'+n, i );
1373 fprintf( pFile, "\n" );
1374 }
1375 fprintf( pFile, ".outputs" );
1376 for ( i = 0; i < nBits; i++ )
1377 fprintf( pFile, " s%02d", i );
1378 fprintf( pFile, "\n" );
1379 fprintf( pFile, ".names v00\n" );
1380 fprintf( pFile, ".names w00\n" );
1381 for ( i = 0; i < nBits; i++ ) {
1382 fprintf( pFile, ".subckt C63a" );
1383 fprintf( pFile, " x0=w%02d", i );
1384 fprintf( pFile, " x1=v%02d", i );
1385 fprintf( pFile, " x2=a%02d", i );
1386 fprintf( pFile, " x3=b%02d", i );
1387 fprintf( pFile, " x4=c%02d", i );
1388 fprintf( pFile, " x5=d%02d", i );
1389 fprintf( pFile, " z0=w%02d", i+1 );
1390 fprintf( pFile, " z1=v%02d", i+1 );
1391 fprintf( pFile, " z2=s%02d", i );
1392 fprintf( pFile, "\n" );
1393 }
1394 fprintf( pFile, ".end\n\n" );
1395 if ( nLutSize == 4 )
1396 Abc_GenComp63a4( pFile );
1397 else if ( nLutSize == 6 )
1398 Abc_GenComp63a6( pFile );
1399 else assert( 0 );
1400}
1401void Abc_WriteAdder2( FILE * pFile, int nVars )
1402{
1403 int i;
1404 assert( nVars > 0 );
1405 fprintf( pFile, ".model A%02d\n", nVars );
1406 fprintf( pFile, ".inputs c\n" );
1407 fprintf( pFile, ".inputs" );
1408 for ( i = 0; i < nVars; i++ )
1409 fprintf( pFile, " a%02d", i );
1410 fprintf( pFile, "\n" );
1411 fprintf( pFile, ".inputs" );
1412 for ( i = 0; i < nVars; i++ )
1413 fprintf( pFile, " b%02d", i );
1414 fprintf( pFile, "\n" );
1415 fprintf( pFile, ".outputs" );
1416 for ( i = 0; i <= nVars; i++ )
1417 fprintf( pFile, " s%02d", i );
1418 fprintf( pFile, "\n" );
1419 fprintf( pFile, ".names c t00\n1 1\n" );
1420 for ( i = 0; i < nVars; i++ )
1421 fprintf( pFile, ".subckt FA a=a%02d b=b%02d cin=t%02d s=s%02d cout=t%02d\n", i, i, i, i, i+1 );
1422 fprintf( pFile, ".names t%02d s%02d\n1 1\n", nVars, nVars );
1423 fprintf( pFile, ".end\n\n" );
1424 Abc_WriteFullAdder( pFile );
1425}
1426void Abc_GenAdder4test( FILE * pFile, int nBits )
1427{
1428 int i, n;
1429 fprintf( pFile, ".model A%02d_4x\n", nBits );
1430 for ( n = 0; n < 4; n++ ) {
1431 fprintf( pFile, ".inputs" );
1432 for ( i = 0; i < nBits; i++ )
1433 fprintf( pFile, " %c%02d", 'a'+n, i );
1434 fprintf( pFile, "\n" );
1435 }
1436 fprintf( pFile, ".outputs" );
1437 for ( i = 0; i < nBits; i++ )
1438 fprintf( pFile, " o%02d", i );
1439 fprintf( pFile, "\n" );
1440
1441 fprintf( pFile, ".names zero\n" );
1442 fprintf( pFile, ".subckt A%02d c=zero", nBits );
1443 fprintf( pFile, " \\\n" );
1444 for ( i = 0; i < nBits; i++ )
1445 fprintf( pFile, " a%0d=a%02d", i, i );
1446 fprintf( pFile, " \\\n" );
1447 for ( i = 0; i < nBits; i++ )
1448 fprintf( pFile, " b%0d=b%02d", i, i );
1449 fprintf( pFile, " \\\n" );
1450 for ( i = 0; i <= nBits; i++ )
1451 fprintf( pFile, " s%0d=t%02d", i, i );
1452 fprintf( pFile, "\n" );
1453
1454 fprintf( pFile, ".subckt A%02d c=zero", nBits );
1455 fprintf( pFile, " \\\n" );
1456 for ( i = 0; i < nBits; i++ )
1457 fprintf( pFile, " a%0d=c%02d", i, i );
1458 fprintf( pFile, " \\\n" );
1459 for ( i = 0; i < nBits; i++ )
1460 fprintf( pFile, " b%0d=t%02d", i, i );
1461 fprintf( pFile, " \\\n" );
1462 for ( i = 0; i <= nBits; i++ )
1463 fprintf( pFile, " s%0d=u%02d", i, i );
1464 fprintf( pFile, "\n" );
1465
1466 fprintf( pFile, ".subckt A%02d c=zero", nBits );
1467 fprintf( pFile, " \\\n" );
1468 for ( i = 0; i < nBits; i++ )
1469 fprintf( pFile, " a%0d=d%02d", i, i );
1470 fprintf( pFile, " \\\n" );
1471 for ( i = 0; i < nBits; i++ )
1472 fprintf( pFile, " b%0d=u%02d", i, i );
1473 fprintf( pFile, " \\\n" );
1474 for ( i = 0; i <= nBits; i++ )
1475 fprintf( pFile, " s%0d=o%02d", i, i );
1476 fprintf( pFile, "\n" );
1477
1478 fprintf( pFile, ".end\n\n" );
1479 Abc_WriteAdder( pFile, nBits );
1480}
1481void Abc_WriteWeight( FILE * pFile, int Num, int nBits, int Weight )
1482{
1483 int i;
1484 fprintf( pFile, ".model W%02d\n", Num );
1485 fprintf( pFile, ".inputs i\n" );
1486 fprintf( pFile, ".outputs" );
1487 for ( i = 0; i < nBits; i++ )
1488 fprintf( pFile, " o%02d", i );
1489 fprintf( pFile, "\n" );
1490 for ( i = 0; i < nBits; i++ )
1491 if ( (Weight >> i) & 1 )
1492 fprintf( pFile, ".names i o%02d\n1 1\n", i );
1493 else
1494 fprintf( pFile, ".names o%02d\n", i );
1495 fprintf( pFile, ".end\n\n" );
1496}
1497
1498Vec_Int_t * Abc_GenTreeFindGroups( char * pTree, int iPos )
1499{
1500 Vec_Int_t * vRes = NULL;
1501 int Counter = 1;
1502 assert( pTree[iPos] == '(' );
1503 while ( pTree[++iPos] ) {
1504 if ( pTree[iPos] == '(' ) {
1505 if ( Counter++ == 1 ) {
1506 if ( vRes == NULL )
1507 vRes = Vec_IntAlloc( 4 );
1508 Vec_IntPush( vRes, iPos );
1509 }
1510 }
1511 if ( pTree[iPos] == ')' )
1512 Counter--;
1513 if ( Counter == 0 )
1514 return vRes;
1515 }
1516 assert( 0 );
1517 return NULL;
1518}
1519int Abc_GenTree_rec( FILE * pFile, int nBits, char * pTree, int iPos, int * pSig, int * pUsed )
1520{
1521 Vec_Int_t * vGroups = Abc_GenTreeFindGroups( pTree, iPos );
1522 if ( vGroups == NULL )
1523 return atoi(pTree+iPos+1);
1524 int i, g, Group;
1525 Vec_IntForEachEntry( vGroups, Group, g ) {
1526 Group = Abc_GenTree_rec( pFile, nBits, pTree, Group, pSig, pUsed );
1527 Vec_IntWriteEntry( vGroups, g, Group );
1528 }
1529 if ( Vec_IntSize(vGroups) == 3 )
1530 Vec_IntPush(vGroups, 0);
1531 if ( Vec_IntSize(vGroups) == 4 )
1532 fprintf( pFile, ".subckt A%02d_4x", nBits ), *pUsed = 1;
1533 else if ( Vec_IntSize(vGroups) == 2 )
1534 fprintf( pFile, ".subckt A%02d c=zero", nBits );
1535 else assert( 0 );
1536 Vec_IntForEachEntry( vGroups, Group, g ) {
1537 fprintf( pFile, " \\\n" );
1538 for ( i = 0; i < nBits; i++ )
1539 fprintf( pFile, " %c%02d=%02d_%02d", 'a'+g, i, Group, i );
1540 }
1541 fprintf( pFile, " \\\n" );
1542 for ( i = 0; i < nBits; i++ )
1543 fprintf( pFile, " s%02d=%02d_%02d", i, *pSig, i );
1544 fprintf( pFile, "\n\n" );
1545 return (*pSig)++;
1546}
1547void Abc_GenThreshAdder( FILE * pFile, int nBits, int A, int B, int S, int fOne )
1548{
1549 if ( A > B ) ABC_SWAP( int, A, B ); int i;
1550 fprintf( pFile, ".subckt A%02d c=%s", nBits, fOne ? "one" : "zero" );
1551 fprintf( pFile, " \\\n" );
1552 for ( i = 0; i < nBits; i++ )
1553 fprintf( pFile, " a%02d=%02d_%02d", i, A, i );
1554 fprintf( pFile, " \\\n" );
1555 for ( i = 0; i < nBits; i++ )
1556 fprintf( pFile, " b%02d=%02d_%02d", i, B, i );
1557 fprintf( pFile, " \\\n" );
1558 for ( i = 0; i <= nBits; i++ )
1559 fprintf( pFile, " s%02d=%02d_%02d", i, S, i );
1560 fprintf( pFile, "\n" );
1561}
1562void Abc_GenThresh( char * pFileName, int nBits, Vec_Int_t * vNums, int nLutSize, char * pArch )
1563{
1564 FILE * pFile = fopen( pFileName, "w" );
1565 int c, i, k, Temp, iPrev = 1, nNums = 1, nSigs = 1, fUsed = 0;
1566 fprintf( pFile, "# %d-bit threshold function with %d variables generated by ABC on %s\n",
1567 nBits, Vec_IntSize(vNums)-1, Extra_TimeStamp() );
1568 fprintf( pFile, "# Weights:" );
1569 Vec_IntForEachEntryStop( vNums, Temp, i, Vec_IntSize(vNums)-1 )
1570 fprintf( pFile, " %d", Temp );
1571 fprintf( pFile, "\n# Threshold: %d\n", Vec_IntEntryLast(vNums) );
1572 fprintf( pFile, ".model TF%d_%d\n", Vec_IntSize(vNums)-1, nBits );
1573 fprintf( pFile, ".inputs" );
1574 for ( i = 0; i < Vec_IntSize(vNums)-1; i++ )
1575 fprintf( pFile, " x%02d", i );
1576 fprintf( pFile, "\n" );
1577 fprintf( pFile, ".outputs F\n" );
1578 for ( i = 0; i < nBits; i++ )
1579 fprintf( pFile, ".names %02d_%02d\n", 0, i );
1580 fprintf( pFile, ".names zero\n" );
1581 fprintf( pFile, ".names one\n 1\n" );
1582 Vec_IntForEachEntry( vNums, Temp, k ) {
1583 fprintf( pFile, ".subckt W%02d", k );
1584 if ( k < Vec_IntSize(vNums)-1 )
1585 fprintf( pFile, " i=x%02d", k );
1586 else
1587 fprintf( pFile, " i=one" );
1588 for ( i = 0; i < nBits; i++ )
1589 fprintf( pFile, " o%02d=%02d_%02d", i, nSigs, i );
1590 fprintf( pFile, "\n" );
1591 nSigs++;
1592 }
1593 fprintf( pFile, "\n" );
1594 if ( pArch == NULL )
1595 {
1596 Vec_IntForEachEntryStart( vNums, Temp, k, 1 ) {
1597 Abc_GenThreshAdder( pFile, nBits, iPrev, k+1, nSigs, k == Vec_IntSize(vNums)-1 );
1598 iPrev = nSigs++;
1599 }
1600 fprintf( pFile, ".names %02d_%02d F\n0 1\n", iPrev, nBits-1 );
1601 }
1602 else
1603 {
1604 Vec_Str_t * vArch = Vec_StrAlloc( 100 );
1605 for ( c = 0; c < strlen(pArch); c++ ) {
1606 if ( pArch[c] == '(' || pArch[c] == ')' ) {
1607 Vec_StrPush( vArch, pArch[c] );
1608 continue;
1609 }
1610 Temp = pArch[c] >= '0' && pArch[c] <= '9' ? pArch[c] - '0' : pArch[c] - 'A' + 10;
1611 assert( Temp > 0 );
1612 if ( Temp == 1 ) {
1613 if ( nNums + Temp == Vec_IntSize(vNums) )
1614 Abc_GenThreshAdder( pFile, nBits, nNums, nNums+1, iPrev = nSigs++, 1 );
1615 else
1616 iPrev = nNums++;
1617 }
1618 else {
1619 int kLast = 0;
1620 assert( nNums + Temp <= Vec_IntSize(vNums) );
1621 if ( nNums + Temp == Vec_IntSize(vNums) )
1622 kLast = Temp++;
1623 iPrev = nNums++;
1624 for ( k = 1; k < Temp; k++ ) {
1625 Abc_GenThreshAdder( pFile, nBits, iPrev, nNums++, nSigs, k == kLast );
1626 iPrev = nSigs++;
1627 }
1628 fprintf( pFile, "\n" );
1629 }
1630 Vec_StrPrintF( vArch, "(%d)", iPrev );
1631 }
1632 Vec_StrPush( vArch, '\0' );
1633 Temp = Abc_GenTree_rec( pFile, nBits, Vec_StrArray(vArch), 0, &nSigs, &fUsed );
1634 fprintf( pFile, ".names %02d_%02d F\n0 1\n", Temp, nBits-1 );
1635 Vec_StrFree( vArch );
1636 }
1637 fprintf( pFile, ".end\n\n" );
1638 Vec_IntForEachEntry( vNums, Temp, k )
1639 Abc_WriteWeight( pFile, k, nBits, k == Vec_IntSize(vNums)-1 ? ~Temp : Temp );
1640 Abc_WriteAdder2( pFile, nBits );
1641 if ( fUsed )
1642 Abc_GenAdder4( pFile, nBits, nLutSize == 4 ? 4 : 6 );
1643 fclose( pFile );
1644}
1645
1657
1658// Based on the paper: E. Demenkov, A. Kojevnikov, A. Kulikov, and G. Yaroslavtsev,
1659// "New upper bounds on the Boolean circuit complexity of symmetric functions".
1660// Information Processing Letters, Vol 110(7), March 2010, Pages 264-267.
1661// https://grigory.us/files/publications/2010_upper_bounds_symmetric_ipl.pdf
1662
1663void Abc_WriteMDFA( FILE * pFile )
1664{
1665 fprintf( pFile, ".model MDFA\n" );
1666 fprintf( pFile, ".inputs z x1 y1 x2 y2\n" );
1667 fprintf( pFile, ".outputs s c1 c2\n" );
1668 fprintf( pFile, ".names x1 z g1\n" );
1669 fprintf( pFile, "10 1\n" );
1670 fprintf( pFile, "01 1\n" );
1671 fprintf( pFile, ".names y1 g1 g2\n" );
1672 fprintf( pFile, "00 0\n" );
1673 fprintf( pFile, ".names y1 z g3\n" );
1674 fprintf( pFile, "10 1\n" );
1675 fprintf( pFile, "01 1\n" );
1676 fprintf( pFile, ".names g2 g3 g4\n" );
1677 fprintf( pFile, "10 1\n" );
1678 fprintf( pFile, "01 1\n" );
1679 fprintf( pFile, ".names x2 g3 g5\n" );
1680 fprintf( pFile, "10 1\n" );
1681 fprintf( pFile, "01 1\n" );
1682 fprintf( pFile, ".names g3 y2 g6\n" );
1683 fprintf( pFile, "10 1\n" );
1684 fprintf( pFile, "01 1\n" );
1685 fprintf( pFile, ".names g5 y2 g7\n" );
1686 fprintf( pFile, "10 1\n" );
1687 fprintf( pFile, ".names g2 g7 g8\n" );
1688 fprintf( pFile, "10 1\n" );
1689 fprintf( pFile, "01 1\n" );
1690 fprintf( pFile, ".names g6 s\n" );
1691 fprintf( pFile, "1 1\n" );
1692 fprintf( pFile, ".names g4 c1\n" );
1693 fprintf( pFile, "1 1\n" );
1694 fprintf( pFile, ".names g8 c2\n" );
1695 fprintf( pFile, "1 1\n" );
1696 fprintf( pFile, ".end\n" );
1697}
1698
1699void Abc_GenAT( char * pFileName, Vec_Int_t * vNums )
1700{
1701 word Sum = 0; int i, k, Num, nBits = 0;
1702 Vec_IntForEachEntry( vNums, Num, i )
1703 Sum += ((word)1 << i) * Num;
1704 while ( Sum )
1705 nBits++, Sum >>= 1;
1706
1707 Vec_Int_t * vTemp; int nFAs = 0, nHAs = 0, nItem = 0;
1708 Vec_Wec_t * vItems = Vec_WecStart( nBits );
1709 Vec_IntForEachEntry( vNums, Num, i )
1710 for ( k = 0; k < Num; k++ )
1711 Vec_WecPush( vItems, i, nItem++ );
1712
1713 FILE * pFile = fopen( pFileName, "w" );
1714 fprintf( pFile, "# %d-bit %d-input adder tree generated by ABC on %s\n", nBits, nItem, Extra_TimeStamp() );
1715 fprintf( pFile, "# Profile:" );
1716 Vec_IntForEachEntry( vNums, Num, i )
1717 fprintf( pFile, " %d", Num );
1718 fprintf( pFile, "\n" );
1719
1720 fprintf( pFile, ".model AT%d_%d\n", nItem, nBits );
1721 Vec_WecForEachLevel( vItems, vTemp, i ) {
1722 if ( Vec_IntSize(vTemp) == 0 )
1723 continue;
1724 fprintf( pFile, ".inputs" );
1725 Vec_IntForEachEntry( vTemp, Num, k )
1726 fprintf( pFile, " %02d", Num );
1727 fprintf( pFile, "\n" );
1728 }
1729 fprintf( pFile, ".outputs" );
1730 for ( k = 0; k < nBits; k++ )
1731 fprintf( pFile, " o%02d", k );
1732 fprintf( pFile, "\n\n" );
1733
1734 assert( nItem == Vec_IntSum(vNums) );
1735 Vec_WecForEachLevel( vItems, vTemp, i ) {
1736 fprintf( pFile, "# Rank %d:\n", i );
1737 Vec_IntForEachEntry( vTemp, Num, k ) {
1738 if ( Vec_IntSize(vTemp) < 2 )
1739 continue;
1740 while ( Vec_IntSize(vTemp) > 2 ) {
1741 int i1 = Vec_IntPop(vTemp);
1742 int i2 = Vec_IntPop(vTemp);
1743 int i3 = Vec_IntPop(vTemp);
1744 int i4 = nItem++;
1745 int i5 = nItem++;
1746 fprintf( pFile, ".subckt FA a=%02d b=%02d cin=%02d s=%02d cout=%02d\n", i3, i2, i1, i4, i5 ); nFAs++;
1747 Vec_IntPush( vTemp, i4 );
1748 if ( i+1 < Vec_WecSize(vItems) )
1749 Vec_WecPush( vItems, i+1, i5 );
1750 }
1751 if ( Vec_IntSize(vTemp) == 2 ) {
1752 int i1 = Vec_IntPop(vTemp);
1753 int i2 = Vec_IntPop(vTemp);
1754 int i4 = nItem++;
1755 int i5 = nItem++;
1756 fprintf( pFile, ".subckt HA a=%02d b=%02d s=%02d cout=%02d\n", i2, i1, i4, i5 ); nHAs++;
1757 Vec_IntPush( vTemp, i4 );
1758 if ( i+1 < Vec_WecSize(vItems) )
1759 Vec_WecPush( vItems, i+1, i5 );
1760 }
1761 assert( Vec_IntSize(vTemp) == 1 );
1762 }
1763 }
1764 Vec_WecForEachLevel( vItems, vTemp, i )
1765 if ( Vec_IntSize(vTemp) == 0 )
1766 fprintf( pFile, ".names o%02d\n", i );
1767 else if ( Vec_IntSize(vTemp) == 1 )
1768 fprintf( pFile, ".names %02d o%02d\n1 1\n", Vec_IntEntry(vTemp, 0), i );
1769 else assert( 0 );
1770 fprintf( pFile, ".end\n\n" );
1771 Abc_WriteHalfAdder( pFile );
1772 Abc_WriteFullAdder( pFile );
1773 printf( "Created %d-bit %d-input AT with %d FAs and %d HAs.\n", nBits, Vec_IntSum(vNums), nFAs, nHAs );
1774 fclose( pFile );
1775 Vec_WecFree( vItems );
1776}
1777void Abc_GenATDual( char * pFileName, Vec_Int_t * vNums )
1778{
1779 word Sum = 0; int i, k, Num, nBits = 0, Iter = 0, fUsed = 0;
1780 Vec_IntForEachEntry( vNums, Num, i )
1781 Sum += ((word)1 << i) * Num;
1782 while ( Sum )
1783 nBits++, Sum >>= 1;
1784
1785 Vec_Int_t * vTemp; int nFAs = 0, nHAs = 0, nXors = 0, nItem = 1;
1786 Vec_Wec_t * vItems = Vec_WecStart( nBits );
1787 Vec_IntForEachEntry( vNums, Num, i )
1788 for ( k = 0; k < Num; k++ )
1789 Vec_WecPush( vItems, i, nItem++ );
1790
1791 FILE * pFile = fopen( pFileName, "w" );
1792 fprintf( pFile, "# %d-bit %d-input adder tree generated by ABC on %s\n", nBits, Vec_IntSum(vNums), Extra_TimeStamp() );
1793 fprintf( pFile, "# Profile:" );
1794 Vec_IntForEachEntry( vNums, Num, i )
1795 fprintf( pFile, " %d", Num );
1796 fprintf( pFile, "\n" );
1797
1798 fprintf( pFile, ".model AT%d_%d\n", nItem, nBits );
1799 Vec_WecForEachLevel( vItems, vTemp, i ) {
1800 if ( Vec_IntSize(vTemp) == 0 )
1801 continue;
1802 fprintf( pFile, ".inputs" );
1803 Vec_IntForEachEntry( vTemp, Num, k )
1804 fprintf( pFile, " %02d", Num );
1805 fprintf( pFile, "\n" );
1806 }
1807 fprintf( pFile, ".outputs" );
1808 for ( k = 0; k < nBits; k++ )
1809 fprintf( pFile, " o%02d", k );
1810 fprintf( pFile, "\n\n" );
1811
1812 fprintf( pFile, ".names %02d\n", 0 );
1813 while ( Vec_WecMaxLevelSize(vItems) > 2 )
1814 {
1815 fprintf( pFile, "# Iter %d:\n", Iter++ );
1816 Vec_Wec_t * vItems2 = Vec_WecStart( nBits );
1817 Vec_WecForEachLevel( vItems, vTemp, i ) {
1818 while ( Vec_IntSize(vTemp) > 3 ) {
1819 int i0 = Vec_IntEntry(vTemp, 0); Vec_IntDrop(vTemp, 0);
1820 int i1 = Vec_IntEntry(vTemp, 0); Vec_IntDrop(vTemp, 0);
1821 int i2 = Vec_IntEntry(vTemp, 0); Vec_IntDrop(vTemp, 0);
1822 int i3 = Vec_IntEntry(vTemp, 0); Vec_IntDrop(vTemp, 0);
1823 int i4 = (Vec_IntSize(vTemp) > 0 && Vec_IntEntryLast(vTemp) > 0) ? Vec_IntPop(vTemp) : 0;
1824 assert( (i0 < 0) == (i1 < 0) );
1825 assert( (i2 < 0) == (i3 < 0) );
1826 if ( i1 > 0 )
1827 fprintf( pFile, ".names %02d %02d %02d\n01 1\n10 1\n", i0, i1, nItem ), i1 = nItem++, nXors++;
1828 else
1829 i1 = -i1, i0 = -i0;
1830 if ( i3 > 0 )
1831 fprintf( pFile, ".names %02d %02d %02d\n01 1\n10 1\n", i2, i3, nItem ), i3 = nItem++, nXors++;
1832 else
1833 i3 = -i3, i2 = -i2;
1834 int o0 = nItem++;
1835 int o1 = nItem++;
1836 int o2 = nItem++;
1837 fprintf( pFile, ".subckt MDFA z=%02d x1=%02d y1=%02d x2=%02d y2=%02d s=%02d c1=%02d c2=%02d\n", i4, i0, i1, i2, i3, o0, o1, o2 ); nFAs += 2, fUsed = 1;
1838 Vec_WecPush( vItems2, i, o0 );
1839 if ( i+1 < Vec_WecSize(vItems2) ) {
1840 Vec_WecPush( vItems2, i+1, -o1 );
1841 Vec_WecPush( vItems2, i+1, -o2 );
1842 }
1843 }
1844 if ( Vec_IntSize(vTemp) == 3 ) {
1845 int i2 = Vec_IntPop(vTemp);
1846 int i1 = Vec_IntPop(vTemp);
1847 int i0 = Vec_IntPop(vTemp);
1848 assert( (i0 < 0) == (i1 < 0) );
1849 assert( i2 > 0 );
1850 if ( i1 < 0 )
1851 fprintf( pFile, ".names %02d %02d %02d\n01 1\n10 1\n", -i0, -i1, nItem ), i0 = -i0, i1 = nItem++, nXors++;
1852 int o0 = nItem++;
1853 int o1 = nItem++;
1854 fprintf( pFile, ".subckt FA a=%02d b=%02d cin=%02d s=%02d cout=%02d\n", i0, i1, i2, o0, o1 ); nFAs++;
1855 Vec_WecPush( vItems2, i, o0 );
1856 if ( i+1 < Vec_WecSize(vItems2) )
1857 Vec_WecPush( vItems2, i+1, o1 );
1858 }
1859 if ( Vec_IntSize(vTemp) == 2 ) {
1860 int i1 = Vec_IntPop(vTemp);
1861 int i0 = Vec_IntPop(vTemp);
1862 assert( (i0 < 0) == (i1 < 0) );
1863 if ( i1 < 0 ) {
1864 Vec_IntInsert( Vec_WecEntry(vItems2, i), 0, i1 );
1865 Vec_IntInsert( Vec_WecEntry(vItems2, i), 0, i0 );
1866 }
1867 else {
1868 Vec_WecPush( vItems2, i, i0 );
1869 Vec_WecPush( vItems2, i, i1 );
1870 }
1871 }
1872 if ( Vec_IntSize(vTemp) == 1 ) {
1873 int i0 = Vec_IntPop(vTemp);
1874 assert( i0 > 0 );
1875 Vec_WecPush( vItems2, i, i0 );
1876 }
1877 assert( Vec_IntSize(vTemp) == 0 );
1878 }
1879 Vec_WecFree( vItems );
1880 vItems = vItems2;
1881 }
1882 Vec_WecForEachLevel( vItems, vTemp, i ) {
1883 if ( Vec_IntSize(vTemp) == 2 ) {
1884 int i1 = Vec_IntPop(vTemp);
1885 int i0 = Vec_IntPop(vTemp);
1886 assert( (i0 < 0) == (i1 < 0) );
1887 if ( i1 < 0 )
1888 fprintf( pFile, ".names %02d %02d %02d\n01 1\n10 1\n", -i0, -i1, nItem ), i0 = -i0, i1 = nItem++, nXors++;
1889 Vec_IntPush( vTemp, i0 );
1890 Vec_IntPush( vTemp, i1 );
1891 }
1892 if ( Vec_IntSize(vTemp) == 1 ) {
1893 int i0 = Vec_IntPop(vTemp);
1894 assert( i0 > 0 );
1895 Vec_IntPush( vTemp, i0 );
1896 Vec_IntPush( vTemp, 0 );
1897 }
1898 if ( Vec_IntSize(vTemp) == 0 ) {
1899 Vec_IntPush( vTemp, 0 );
1900 Vec_IntPush( vTemp, 0 );
1901 }
1902 assert( Vec_IntSize(vTemp) == 2 );
1903 }
1904 int cin = 0;
1905 Vec_WecForEachLevel( vItems, vTemp, i ) {
1906 int i1 = Vec_IntPop(vTemp);
1907 int i0 = Vec_IntPop(vTemp);
1908 assert( i0 >= 0 && i1 >= 0 );
1909 fprintf( pFile, ".subckt FA a=%02d b=%02d cin=%02d s=o%02d cout=%02d\n", i0, i1, cin, i, nItem ); nFAs++;
1910 cin = nItem++;
1911 }
1912 fprintf( pFile, ".end\n\n" );
1913 Abc_WriteFullAdder( pFile );
1914 if ( fUsed )
1915 Abc_WriteMDFA( pFile );
1916 printf( "Created %d-bit %d-input AT with %d FAs, %d HAs, and %d XORs.\n", nBits, Vec_IntSum(vNums), nFAs, nHAs, nXors );
1917 fclose( pFile );
1918 Vec_WecFree( vItems );
1919}
1920
1924
1925
1927
void Abc_WriteCell(FILE *pFile)
Definition abcGen.c:370
Vec_Int_t * Abc_GenTreeFindGroups(char *pTree, int iPos)
Definition abcGen.c:1498
ABC_NAMESPACE_IMPL_START void Abc_WriteHalfAdder(FILE *pFile)
DECLARATIONS ///.
Definition abcGen.c:45
void Abc_GenFsmCond(Vec_Str_t *vCond, int nPis, int Prob)
Definition abcGen.c:792
Mini_Aig_t * Abc_GenSignedBoothMini(int nArgN, int nArgM)
Definition abcGen.c:1074
Gia_Man_t * Abc_GenSignedBoothPPTest(int nArgA, int nArgB)
Definition abcGen.c:948
void Abc_WriteBooth(FILE *pFile, int nVars)
Definition abcGen.c:1131
void Abc_GenOneHotIntervals(char *pFileName, int nPis, int nRegs, Vec_Ptr_t *vOnehots)
Definition abcGen.c:682
void Abc_GenGraph(char *pFileName, int nPis)
Definition abcGen.c:1194
int Abc_GenSignedBoothPP(Gia_Man_t *p, int a, int b, int c, int d, int e)
Definition abcGen.c:926
void Abc_GenComp63a4(FILE *pFile)
Definition abcGen.c:1259
void Abc_GenAdder4test(FILE *pFile, int nBits)
Definition abcGen.c:1426
void Abc_GenFpga(char *pFileName, int nLutSize, int nLuts, int nVars)
Definition abcGen.c:523
void Abc_WriteAdder2(FILE *pFile, int nVars)
Definition abcGen.c:1401
void Abc_GenMesh(char *pFileName, int nVars)
Definition abcGen.c:404
void Abc_GenAdder(char *pFileName, int nVars)
Definition abcGen.c:159
void Abc_GenAT(char *pFileName, Vec_Int_t *vNums)
Definition abcGen.c:1699
void Abc_GenThresh(char *pFileName, int nBits, Vec_Int_t *vNums, int nLutSize, char *pArch)
Definition abcGen.c:1562
void Abc_GenComp63a6(FILE *pFile)
Definition abcGen.c:1300
void Abc_GenOneHot(char *pFileName, int nVars)
Definition abcGen.c:641
void Abc_GenSorter(char *pFileName, int nVars)
Definition abcGen.c:299
void Abc_WriteBoothPartialProducts(FILE *pFile, int nVars)
Definition abcGen.c:1095
void Abc_WriteFullAdder(FILE *pFile)
Definition abcGen.c:71
void Abc_GenFsm(char *pFileName, int nPis, int nPos, int nStates, int nLines, int ProbI, int ProbO)
Definition abcGen.c:808
void Abc_AdderTree(FILE *pFile, int nArgs, int nBits)
Definition abcGen.c:876
void Abc_WriteKLut(FILE *pFile, int nLutSize)
Definition abcGen.c:475
void Abc_WriteMulti(FILE *pFile, int nVars)
Definition abcGen.c:180
void Abc_WriteLayer(FILE *pFile, int nVars, int fSkip1)
Definition abcGen.c:257
int Abc_GenTree_rec(FILE *pFile, int nBits, char *pTree, int iPos, int *pSig, int *pUsed)
Definition abcGen.c:1519
void Abc_GenBooth(char *pFileName, int nVars)
Definition abcGen.c:1172
void Abc_GenThreshAdder(FILE *pFile, int nBits, int A, int B, int S, int fOne)
Definition abcGen.c:1547
void Abc_GenATDual(char *pFileName, Vec_Int_t *vNums)
Definition abcGen.c:1777
void Abc_GenAdder4(FILE *pFile, int nBits, int nLutSize)
Definition abcGen.c:1365
void Abc_GenMulti(char *pFileName, int nVars)
Definition abcGen.c:223
void Abc_WriteWeight(FILE *pFile, int Num, int nBits, int Weight)
Definition abcGen.c:1481
void Abc_WriteComp(FILE *pFile)
Definition abcGen.c:244
void Abc_WriteMDFA(FILE *pFile)
Definition abcGen.c:1663
Gia_Man_t * Abc_GenSignedBooth(int nArgN, int nArgM)
Definition abcGen.c:1017
void Abc_GenAdderTree(char *pFileName, int nArgs, int nBits)
Definition abcGen.c:906
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_START void Abc_GenRandom(char *pFileName, int nPis)
Definition abcGen.c:743
void Abc_WriteAdder(FILE *pFile, int nVars)
Definition abcGen.c:126
int nWords
Definition abcNpn.c:127
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
unsigned Aig_ManRandom(int fReset)
Definition aigUtil.c:1170
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
struct Vec_Str_t_ Vec_Str_t
Definition bblif.c:46
Cube * p
Definition exorList.c:222
char * Extra_TimeStamp()
Mini_Aig_t * Gia_ManToMiniAig(Gia_Man_t *pGia)
Definition giaMini.c:118
void Gia_ManStop(Gia_Man_t *p)
Definition giaMan.c:82
void Gia_ManHashAlloc(Gia_Man_t *p)
Definition giaHash.c:105
Gia_Man_t * Gia_ManStart(int nObjsMax)
FUNCTION DEFINITIONS ///.
Definition giaMan.c:57
int Gia_ManHashXor(Gia_Man_t *p, int iLit0, int iLit1)
Definition giaHash.c:668
struct Gia_Man_t_ Gia_Man_t
Definition gia.h:96
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition giaScl.c:84
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
Definition giaHash.c:576
void Gia_ManHashStop(Gia_Man_t *p)
Definition giaHash.c:149
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
word M(word f1, word f2, int n)
Definition kitPerm.c:240
struct Mini_Aig_t_ Mini_Aig_t
BASIC TYPES ///.
Definition miniaig.h:48
char * pName
Definition gia.h:99
#define assert(ex)
Definition util_old.h:213
int strlen()
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54
#define Vec_IntForEachEntryStop(vVec, Entry, i, Stop)
Definition vecInt.h:58
#define Vec_IntForEachEntryStart(vVec, Entry, i, Start)
Definition vecInt.h:56
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition vecPtr.h:55
#define Vec_WecForEachLevel(vGlob, vVec, i)
MACRO DEFINITIONS ///.
Definition vecWec.h:55
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.
Definition vecWec.h:42
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.
Definition vecWrd.h:42