#resize TRUE
#jumptomain NONE

#define SCREENSIZE 64000
#define MEMBLOCKSIZE SCREENSIZE/16

word screen;
word buffer;

void setrgbpalette(char color,r,g,b);

main()
{
	@SETVIDEOMODE(byte 0x13);
	setcolortable();
	screen=GETMEM(MEMBLOCKSIZE);
	CLEARBLOCK();
	buffer=GETMEM(MEMBLOCKSIZE);
	CLEARBLOCK();
	fillscreen();
	WHILE(!KBHIT()){
		trueblurscreen();
		copybufferscreen();
		copyvirtualscreen();
	}
	@BIOSREADKEY();
	fillscreen();
	WHILE(!KBHIT()){
		fastblurscreen();
		copyvirtualscreen();
	}
	@SETVIDEOMODE(byte 0x3);
	@BIOSREADKEY();
}

void setrgbpalette(char color,r,g,b)
{
	DX=0x3C8;
	outp(color,DX);
	DX++;
	outp(r,DX);
	outp(g,DX);
	outp(b,DX);
}

void setcolortable()
{
	FOR(CL=0;CL<=63;CL++){
		setrgbpalette(CL,CL,0,0);
		setrgbpalette(CL+64,63,CL,0);
		setrgbpalette(CL+128,63,63,CL);
		setrgbpalette(CL+192,63,63,63);
	}
}

void CLEARBLOCK()
{
	ES=AX;
	EAX=0;
	DI=0;
	CX=16000;
	$rep $stosd
}

void fillscreen()
{
	ES=screen;
	DI=321;
	FOR(SI=1;SI<199;SI++){
		CX=318;
		AL=255;
		$rep $stosb
		DI++;
		DI++;
	}
}

void trueblurscreen()
{
	ES=screen;
	FS=buffer;
	DI=321;
	FOR(SI=1;SI<199;SI++){
		FOR(BX=1;BX<319;BX++,DI++){
			FSBYTE[DI]=int ESBYTE[DI-321]+ESBYTE[DI-320]+ESBYTE[DI-319]+
				ESBYTE[DI-1]+ESBYTE[DI+1]+ESBYTE[DI+319]+ESBYTE[DI+320]+
				ESBYTE[DI+321]>>3;
		}
		DI++;
		DI++;
	}
}

void fastblurscreen()
{
	ES=screen;
	DI=321;
	FOR(SI=1;SI<199;SI++){
		FOR(BX=1;BX<319;BX++,DI++){
			ESBYTE[DI]=int ESBYTE[DI-320]+ESBYTE[DI-1]+ESBYTE[DI+1]+ESBYTE[DI+320]>>2;
		}
		DI++;
		DI++;
	}
}

void copybufferscreen()
{
	ES=screen;
	$push DS
	DS=buffer;
	SI=0;
	DI=0;
	CX=16000;
	$rep $movsd
	$pop DS
}

void copyvirtualscreen()
{
	@WAITVSYNC();
	copyfard(0xA000,0,screen,0,16000);
}

