/* FADD221-A.C -->   This program generates Real&Imaginary 8IP 221 adder EPROM
	(7C291) contents.  This EPROM consists of two adders. Each of these 
	adders has two 2-bit inputs and a carry input and produces 3-bit 
	sum output.
	       _______
	  Cin--|      |		                 
	A(2)---| FADD |- Cin+A+B (sign extended 2-bits)
	B(2)---|      |
		------
	  Cin--|      |		
	C(2)---| FADD |- Cin+C+D (sign extended 2-bits)
	D(2)---|      |
		------
Note : The inputs are two  2-bit numbers in TWO's Complement format and a 
       carry bit. The PROM masks the MSBs and extends the 1st bit value to 
       the MSB ie., 2nd bit (i/p sign extention) and adds the two numbers. 
       Ignores the (or masks the) overflow(3rd bit), if produced upon addition. 
       Then extend the sum o/p sign bit (2nd bit) to the  3rd bit. Then it 
       produces the o/p                      

						 06 Feb 1998  TP    */

#include <stdio.h>
#include <conio.h>

main()
{
	int i, sum1, sum2, c1, c2, a1, b1, a2, b2;
	unsigned char sum;
	FILE *f1, *f2;

	f1 = fopen("FADD221A.TXT", "wt");
	f2 = fopen("FADD221A.BIN", "wb");
	clrscr();

	fprintf(f1,"               ______      8IP Card- Cy7c276 PROM  \n");
	fprintf(f1,"       c1(1)--|      |	  \n");	
	fprintf(f1,"      a1(2)---| FADD |- sum1 (sign extended 3-bits)\n");
	fprintf(f1,"      b1(2)---|      |                  \n");
	fprintf(f1,"               ------                   \n");
	fprintf(f1,"       c2(2)--|      |                  \n");
	fprintf(f1,"      a2(2)---| FADD |- sum2 (sign extended 3-bits)\n");
	fprintf(f1,"      b2(2)---|      |                  \n");
	fprintf(f1,"               ------                   \n");


	fprintf(f1," i -   a2+b2+c2= sum2 - a1+b1+c1= sum1  -  sum\n");
	for(i=0;i<1024;i++)
	{	
		/* coining the Adder1 inputs */
		c1 = i & 0x1;               /* 00 0000 0001 */
		a1 = ((i & 0x006) >>1) & 0x1; /* 00 0000 0110.,  mask MSB*/
		b1 = ((i & 0x018) >>3) & 0x1; /* 00 0001 1000.,  mask MSB */

	/*   
	   input Sign Extention  : Note: as a1,b1 are now only
	   one bit wide, the sign extention is carried out this way. 
	*/
		a1 = (a1 << 1) | a1  ;
		b1 = (b1 << 1) | b1  ;

		/* coining the Adder2 inputs */
		c2 = ((i & 0x020) >>5) & 0x1; /* 00 0010 0000 */
		a2 = ((i & 0x0c0) >>6) & 0x1; /* 00 1100 0000.,  mask MSB*/
		b2 = ((i & 0x300) >>8) & 0x1; /* 11 0000 0000.,  mask MSB */

	/*   
	   input Sign Extention 
	*/
		a2 = (a2 << 1) | a2  ;
		b2 = (b2 << 1) | b2  ;

		sum1 = ((a1 + b1) + c1) & 0x3; /*ignore the overflow */ 
		sum2 = ((a2 + b2) + c2) & 0x3; /*ignore the overflow */

	/*
	   output sign extention 
	*/
	       if((sum1 & 0x2) == 0x2)
		   sum1 = (sum1 | 0x4);
	       if((sum2 & 0x2) == 0x2)
		   sum2 = (sum2 | 0x4);

	/* Packing the two sums into one byte*/
		sum = sum1 | ((sum2 << 3) & 0x38);

		fprintf(f1,"%3x  -  %2x+%2x+%1x =%3x  - %2x+%2x+%1x = %3x  -  %3x\n", i, a2, b2, c2, sum2, a1, b1, c1, sum1,  sum);
		fwrite(&sum, sizeof(sum), 1, f2);
	}
	fcloseall();
}
