/*
 *	LIFE - do a generation of life to the screen
 */

#include <stdio.h>
#include <osbind.h>

extern char *lifetab;		/* from lifetab.c */

/* How big a boundary a screen buffer has to be to work with Physbase */
#define ALIGN 512	/* probably an overestimate */

main(argc,argv)
char **argv;
{
	int i;
	long l;
	int *scrbase;
	char *screen2;		/* Second screen buffer */

	scrbase = (int *) Logbase();

	screen2 = (char *) Malloc(32000L+ALIGN);
	if (screen2 == NULL) {
		fputs("Out of memory.\n", stderr);
		exit(1);
	}
	/* Align the pointer */
	screen2 = (char *)(
		((long)screen2 + (long)ALIGN - 1L) & ~((long)ALIGN-1L)
		);

	fputs("Precomputing lookup tables...", stdout);
	fflush(stdout);
	inittables();
	
	v_curoff();	/* Turn flashing cursor off */
	
	while (1) {
		generate(scrbase, screen2);
		Setscreen(-1L, screen2, -1);	/* flip screens at vblank */
		generate(screen2, scrbase);
		Setscreen(-1L, scrbase, -1);
		/* must leave PhysBase pointing at the original screen at exit */

		if (Bconstat(2)) break;		/* exit on keypress */
	}

	putchar('\r');

	v_curon();	/* Turn the nasty thing back on again */

	exit(0);
}

/* turn the text cursor off */
v_curoff()
{
	xbios(21, 0, 0);
}

/* turn the text cursor on */
v_curon()
{
	xbios(21, 1, 0);	/* I don't know what default flash rate is */
}
