Skip to content

Instantly share code, notes, and snippets.

@juj
Created January 11, 2023 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juj/34306e6da02a8a7043e393f01e013f24 to your computer and use it in GitHub Desktop.
Save juj/34306e6da02a8a7043e393f01e013f24 to your computer and use it in GitHub Desktop.
How to reprogram VGA registers to a 320x200@60hz mode with either square pixels or 5:6 pixels.
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment