/* FADD441A.C -->  This program generates the Real, Imaginary 8IP Card 441-Adder EPROM contents
		   CY7C291 (U33).  The inputs are two Sign extended 4-bit numbers and a
		   carry bit.  The sum produced is a sign extended 5-bit number.

	Note : 	When the Subgroup is enabled this PROM logic ignores one of the 
		adder inputs corresponding to G1 to G4(Slot0 to Slot3) outputs.
		Thus allowing to produce Subgroup sum outputs.
			      

		   ______        CY7C291(U33)
	     Cin--|      |		
sign ext.  A(4)---| Add  |- Cin+A+B (sign extended 5-bits)
  -do-     B(4)---|      |
	       ___|      |
	      ³   |______|
	    SubG


	The input to the PROM is organized as shown below :

	   X XXXX XXXX X
	   |   |    |  |_____ carry
     SubGroup  |  input a
	    input b
	
				   07 FEB 1998  TP JR */


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

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

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

	fprintf(f1, "FADD441A.C generated this real, Imaginary 8IP Card 441-Adder EPROM contents \n");
	fprintf(f1, "viz., CY7C291 (U33).The inputs are two sign extended 4-bit numbers and\n");
	fprintf(f1, "a carry bit.  The sum produced is a sign extended 5-bit number.    07 FEB 1998   TP JR\n\n");


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

	for(i=0; i<1024; i++)
	{	
		a = (i>>1) & 0xf;
		b = (i>>5) & 0xf;
		c = i & 0x1;
		s = (i >> 9) & 0x1;  /* SubGroup enable bit */

		if (s==0x0)	/* if SubGroup not selected */
		    sum = (a + b + c) & 0xf;  /* overflow is ignored */

		if (s == 0x1)	/* if SubGroup selected */
		    sum = b;
		
		if((sum & 0x8) == 0x8)    /* extend the sign */
		     sum = sum | 0x10 ;  /* bit to the MSB */ 

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