/* ADD221-A.C -->   This program generates Power 8IPCard 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)---| Add  |- Cin+A+B (3-bits)
	B(2)---|      |
		------
	  Cin--|      |		
	C(2)---| Add  |- Cin+C+D (3-bits)
	D(2)---|      |
		------



Program to add two 5-bit numbers (2,2,1 & 2,2,1) */
/* 		             			 16th Apr. 1997    */

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

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

	f1 = fopen("ADD221-A.TXT", "wt");
	f2 = fopen("ADD221-A.BIN", "wb");
	clrscr();
	for(i=0;i<1024;i++)
	{	
		/* coining the Adder1 inputs */
		c1 = i & 0x001;               /* 00 0000 0001 */
		a1 = ((i & 0x006) >>1) & 0x3; /* 00 0000 0110 */
		b1 = ((i & 0x018) >>3) & 0x3; /* 00 0001 1000 */

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

		sum1 = ((a1 + b1) + c1) & 0x7;
		sum2 = ((a2 + b2) + c2) & 0x7;

		sum = sum1 | ((sum2 << 3) & 0x38);

       //	printf("sum of %x and %x is = %x \n", (i & 0x001f), ((i>>5) & 0x001f), sum);
		fprintf(f1,"%3x -  %2x+%2x+%1x = %3x - %2x+%2x+%1x = %3x -  %3x\n", i, a1, b1, c1, sum1, a2, b2, c2, sum2, sum);
		fwrite(&sum, sizeof(sum), 1, f2);
	}
	fcloseall();
}
