// License: public domain.

#include <dos.h>
#include <conio.h>

void set_video_mode(int mode)
{
	union REGS regs;
	regs.x.ax = mode;
	int86(0x10, &regs, &regs);
}

void main()
{
	set_video_mode(0x13);
	disable();
	outpw(0x3D4, 0x0011); // Turn off write protect to CRTC registers
	outpw(0x3D4, 0x0B06); // New vertical total=525 lines, bits 0-7
	outpw(0x3D4, 0x3E07); // New vertical total=525 lines, bits 8-9
#ifdef SQUARE_PIXELS
	outpw(0x3D4, 0xB910); // Vsync start=scanline 185
	outpw(0x3D4, 0x8F12); // Vertical display end=scanline 399, bits 0-7
	outpw(0x3D4, 0xB815); // Vertical blanking start=scanline 440, bits 0-7
	outpw(0x3D4, 0xE216); // Adjust vblank end position
	outpw(0x3D4, 0x8511); // Vsync length=2 lines + turn write protect back on
#else
	outpw(0x3D4, 0x0B16); // Adjust vblank end position=scanline 524
	outpw(0x3D4, 0xD710); // Vsync start=scanline 215
	outpw(0x3D4, 0x8911); // Vsync length=2 lines + turn write protect back on
#endif
	enable();
	// fill screen with VGA palette
	unsigned char far *s = (unsigned char far *)MK_FP(0xA000, 0);
	for(unsigned int i = 0; i < 64000; ++i) s[i] = i%320;
	getch();
	// restore DOS video mode
	set_video_mode(0x03);
}
