
#define VideoBase       0x4000
#define UsableVideo     0x4040
#define VideoTopLeft	0x43c0 /*UsableVideo+UsableWidth*UsableHeight*/
#define VideoEnd		0x4400
#define ColorBase       0x4400
#define ColorEnd		0x4800
#define UsableWidth     28
#define UsableHeight    32

#define InterruptEnable 0x5000
#define SoundOn         0x5001
#define Flip            0x5003
#define CoinCounter     0x5007
#define WR0             0x5040
#define WR1             0x5050
#define WR2             0x5060
#define WR3             0x5070
#define WatchdogReset   0x50c0

#define ResetWatchdog() *(WatchdogReset) = 1;

#define IN0               0x5000
# define CREDIT                 0x80
# define COIN2                  0x40
# define COIN1                  0x20
# define DIP6                   0x10
# define DOWN                   0x08
# define RIGHT                  0x04
# define LEFT                   0x02
# define UP                     0x01
#define IN1               0x5040
# define TABLE                  0x80
# define START2                 0x40
# define START1                 0x20
# define TEST                   0x10
# define DOWN2                  0x08
# define RIGHT2                 0x04
# define LEFT2                  0x02
# define UP2                    0x01
#define Dipswitch         0x5080
# define SOLDER1                0x80
# define SOLDER2                0x40
# define DIP5                   0x20
# define DIP4                   0x10
# define DIP3                   0x08
# define DIP2                   0x04
# define DIP1                   0x02
# define DIP0                   0x01

#define WaitForOff(x) {while (x) ResetWatchdog();}
#define WaitForOn(x) {while(!(x)) ResetWatchdog();}
#define WaitForOnOff(x) {WaitForOn(x); WaitForOff(x);}

#define color(c) gColor = c;

/* global stuff for printing */
char *gScreenPtr;
char *gColorPtr;
char gColor;
#define MenuItems 7
main()
{
	int item;
	int i;
	cls();

	item = 0;
	while(1)
	{
		ResetWatchdog();
		print(6,9,0xAF,"DAVIDS@COOL@TEST");
		tab(6,10);
	    for(i=0;i<16;i++)
			printc(0xdc);
	
		
		PrintMenu(item);

		if (Up()) item--;
		if (Down()) item++;
		if (item < 0) item = MenuItems - 1;
		if (item >= MenuItems) item = 0;
		if (Player1() || Player2())
			switch(item)
			{
			case 0: ColorTest(); break;
			case 5: TestScreen(); break;
			case 6: DisplayCharSet(); break;
			default: break;
			}
	}
}

TestScreen()
{
	FillVideo();
	WaitForOn(Player1());
	cls();
}

ColorTest()
{
	char c;
	CharSet();
	c=0;
	FillColor(c);
	while(1)
	{
		ResetWatchdog();
		print(5,5,0xAF,"COLOR:");
		printc((c>>4)&0x0F);
		printc(c&0x0F);
		if(Player1()) {
		    if (Player2())
				break;
			c += 0x10;
			FillColor(c);
		}
		if(Player2()) {
			c += 0x01;
			FillColor(c);
		}
	}
	cls();
}

/* Fill up the screen with an incrementing pattern */
FillColor(inC)
char inC;
{
	char *c;
	for (c=ColorBase;c<ColorEnd;c++)
	{
		ResetWatchdog();
		*c=inC;
	}       
}

/* Fill up the screen with an incrementing pattern */
FillVideo()
{
	char *c;
	for (c = VideoBase;c<VideoEnd;c++)
	{
		ResetWatchdog();
		*c=c&0xff;
	}       
}

DisplayCharSet()
{
	cls();
	CharSet();
	WaitForOn(Player1());
	cls();
}

#define CharSetX 7
#define CharSetY 9
CharSet()
{
	int x,y;

	for (x=0;x<=0x0f;x++)
		for(y=0;y<=0x0f;y+=0x01)
		{
			ResetWatchdog();
			if (y==0)
			{
				tab(x+CharSetX,CharSetY-2);
			    printc(x);
			    tab(x+CharSetX,CharSetY-1);
			    printc(0xdc); /* horizontal */
			}

		    if (x==0)
			{
				tab(CharSetX-2,y+CharSetY);
				printc(y);
				printc(0xd2); /* vertical */
			}
			else
				tab(x+CharSetX,y+CharSetY);
			printc(y<<4|x);
		}
}

#define MenuX 6
#define MenuY 12

PrintMenu(item)
char item;
{
	char i;
	for (i=0;i<=MenuItems;i++)
	{
		ResetWatchdog();
		tab(MenuX,i+MenuY);
		color(0xAF);

		if (i==item)
			printc(0x14);
		else
			printc(0x40);
		printc(0x40);

		switch (i)
		{
		case 0: prints("COLOR@TEST"); break;
		case 1: prints("SCREEN@TEST"); break;
		case 2: prints("JOYSTICK@TEST"); break;
		case 3: prints("BUTTONS"); break;
		case 4: prints("STOP@WATCHDOGGING"); break;
		case 5: prints("TEST@SCREEN"); break;
		case 6: prints("CHARACTER@SET"); break;
		}
	}
}

cls()
{
	char *c;
	for (c=VideoBase;c<VideoEnd;c++)
	{
		ResetWatchdog();
		*c=0x40;
	}       
}

tab(x,y)
int x,y;
{
#if 0        
	gScreenPtr = UsableVideo+UsableWidth*UsableHeight - x * UsableHeight + y;
	gColorPtr = gScreenPtr+(ColorBase - VideoBase);
#else
	gScreenPtr = VideoTopLeft - x * UsableHeight + y;
	gColorPtr = gScreenPtr+(0x0400);
/*	gScreenPtr = VideoTopLeft;
	gColorPtr = gScreenPtr+(0x0400);*/
#endif
}

#define OutChar(c)     { *gScreenPtr = c; \
			*gColorPtr = gColor; \
			gScreenPtr -= UsableHeight; \
			gColorPtr  -= UsableHeight; }

print(x,y,c,s)
int x,y;
int c;
char *s;
{
	tab(x,y);
	color(c);
	prints(s);
}

printc(c)
int c;
{
	OutChar(c);
}

prints(s)
char *s;
{        
	while (*s)
		OutChar(*s++)
}

Up()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN0) & UP);
	ret = is && !was;
	was = is;
	return ret;
}
Down()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN0) & DOWN);
	ret = is && !was;
	was = is;
	return ret;
}
Left()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN0) & LEFT);
	ret = is && !was;
	was = is;
	return ret;
}
Right()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN0) & RIGHT);
	ret = is && !was;
	was = is;
	return ret;
}
Player1()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN1) & START1);
	ret = is && !was;
	was = is;
	return ret;
}
Player2()
{
	static char was;
	char is;
	char ret;
	is = !(*(IN1) & START2);
	ret = is && !was;
	was = is;
	return ret;
}
