/* FADD331-A.C -->  This program generates the 8IP Card Real, Imaginary 331-Adder 
		  EPROM content CY7C291 (U21, U22).  The inputs are two 2-bit 
		  2's comp. numbers and a carry bit.  The sum produced is a sign
		  extended  3-bit 2's comp. number.


		 ______        CY7C291     
	   Cin--|      |		
Sign Ext A(3)---| Add  |- Cin+A+B (sign extended 3-bits)
  -do-   B(3)---|      |
		|______|
				   6th Feb. 1997   JR  GK  TP*/


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

main()
{
	int i, a, b, c;
	unsigned char sum;
	FILE *f1, *f2;

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

	fprintf(f1, "FADD331A.C generated this Real & Imaginary 8IP Card 331-Adder EPROM contents \n");
	fprintf(f1, "viz., CY7C291 (U21, U22).The inputs are already sign extended two 3-bit TWO's comp. numbers and\n");
	fprintf(f1, "a carry bit.  The sum produced is a sign extended 3-bit number.    16th Apr.1997   JR  GK\n\n");


	fprintf(f1,"  i  -  a +   b + c =   sum\n\n");

	for(i=0; i<128; i++)
	{	
		a = (i>>1) & 0x7;
		b = (i>>4) & 0x7;
		c = i & 0x1;

		sum = (a + b + c) & 0x7;  /* ignore the Overflow */

		if((sum & 0x4) == 0x4)  /* extend the o/p  */
		    sum = sum | 0x8;    /* sign bit        */

		fprintf(f1,"%3x - %3x + %3x + %1x = %4x \n", i, a, b, c, sum);
		fwrite(&sum, sizeof(sum), 1, f2);
	}
	fcloseall();
}
