Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a414c7b47c6d2a9c

Jump to content

// Workers AI · dad joke modeWhat did Mode 13h say to its date? You're a pixel-fect match.

From Wikipedia, the free encyclopedia
Simulated image as displayed using VGA mode 13h resolution and color abilities (aspect ratio not corrected).
Default VGA 256-color palette

Mode 13h is the standard 256-color mode on MCGA and VGA graphics hardware introduced in 1987 with the IBM PS/2. It has a resolution of 320 × 200 pixels.[1] "13h" refers to the number of the mode in the VGA BIOS. The "h" stands for hexadecimal.

Mode 13h provides programmers with a linear 320 × 200 block of video memory, where each byte represents one pixel. This allows ease of programming at the expense of access to other useful features of the VGA hardware.

Given the aspect ratio of a 320 × 200 resolution screen for use on a 4:3 display, Mode 13h does not have square pixels.[2]

Technical details

[edit]


The VGA has 256 KiB of video memory consisting of 4 banks of 64 KiB, known as planes (or "maps" in IBM's documentation). Planar memory arrangement splits the pixels horizontally into groups of four. For any given byte in the PC's 64 KiB video memory aperture, four pixels can be accessed on screen by selecting the required plane(s) using various bit masks and "write modes". (In fact, every memory plane has its own latch and ALU; besides a direct overwrite, a CPU "write" can also lead to OR/AND/XORing the existing value with a new one.) This is more complicated for the programmer, but allows access to all of the available video memory and other benefits (see Mode X).[3]

This planar structure of most of VGA's write modes makes it not as suited to graphics operation, which is "chunky" and is most ideally done on one contiguous, linear framebuffer. Mode 13h, however, allows the programmer to access the VGA in precisely this fashion, where each consecutive address in the aperture represents a consecutive pixel on screen. Planes need not be manipulated to select the correct pixels to modify or read. It is a feature shared with the MCGA. (The limited size is also due to MCGA compatibility: a 320 × 200 256-color mode requires 64,000 bytes of display memory, which is almost all that the MCGA's 64 KiB (65,536 bytes) could provide.)[4]

Mode 13h uses a 256-color palette. Each entry has 18 bits of color, 6 per RGB channel. The color palette data is not stored in memory per se, but rather modified, read and written to by low level I/O port calls (OUT), to the DAC registers 3C8h and 3C9h.[5]

In mode 13h the screen buffer is simply the bytes in segment 0xA000 from offset 0 to 63,999.

Implementation on VGA

[edit]

On the VGA, mode 13h uses a "Chain 4" addressing-mode in which the lowest two bits of the 16-bit aperture address are used to select the plane to write to/read from. The upshot is that the programmer has a simple access model and has simultaneous access to all four planes; the downside is that now only 214 (16 KiB) of the video plane's address space are accessible (the other 2 bits being used to select planes) so only a total of 64 KiB is addressable (4 × 16 KiB). The upper 48 KiB of each plane remains unused in this mode.[6]

When selecting 320 × 200 from mode 13h, VGA internally performs scan-doubling to provide a 320 × 400 mode.[4]

The standard method to access mode 13h is via the BIOS interrupt provided by VGA firmware int 10h, which is not possible in 32-bit protected mode. Instead, the program needs to replicate what the firmware does by directly writing the VGA hardware registers or using a driver program that performs this action. Code for doing so is available.[7]

Typical usage

[edit]

The palette data was often copied from the low level ports one at a time to a spot in memory after the screen data so the colors could be saved along with the pixels they belonged to. Using the wrong palette data causes a wrong combination of colors to be saved.

A screen capture is merely a save of these 64,000 bytes, each an index of a color from a known palette of multiple colors, combined with the palette.

The bytes in segment 0xA000 after offset 64000 are not mapped to the screen. As a result they can be written to with no negative effects. One use for this is to use 64000–64767 for saving the 768-byte palette data. When written to a file these have to be read back out again and written to the color port for them to change, when an image containing a footer of color info gets loaded. A method used with this (multi-palette situation) was to have a global palette, and match the colors from an old color palette, to a new color palette. This meant changing the index of each pixel, where it would point to a near perfect match of one color from old to new color set. This also had drawbacks, as sometimes there were not enough gradients, and artifacts and such would appear.

Variants

[edit]

An alternative planar 256-color mode is available by disabling Chain 4 mode (unchaining). Video modes created using this technique are collectively called Mode X, though the original use of that term was for a VGA mode which also had square pixels. While these modes (along with very many other possible VGA modes) were not specifically documented by IBM, the VGA hardware functions and features used to implement them were documented at least as early as 1988, in the PS/2 Hardware Interface Technical Reference.

A 320 × 400 256-color mode with "chain 4"-style simple addressing can be achieved by manually writing the VGA registers.[8]

Notation

[edit]

13h refers to the number 19 written in the hexadecimal notation in a format typical of assembly code for Intel processors. In the C programming language, hexadecimal 13 is written 0x13.

In x86 assembly language the video mode is entered by setting the AL register to 13h, the AH register to 0, and then calling BIOS interrupt 10h.[1] The two 8-bit registers AL and AH comprise the 16-bit AX register.

Although 13hex is 19 in decimal, in QuickBASIC's screen mode numbering scheme it is simply screen mode 13 (not hexadecimal).[9] Many QuickBASIC screen modes have numbers which differ from the BIOS modes on which they are based.[10]

Source code examples

[edit]

Assembly code to enter Mode 13h:

mov ax, 0013h
int 10h

As an illustration of mode 13h in a video game source code, Wolfenstein 3D includes this function in a file named "ID_VL.C". It enters mode 13h by using two assembly instructions (prefixed with asm) embedded in the C source code and the switches to a custom planar mode allowing triple buffering.[11]

void    VL_SetVGAPlaneMode (void)
{
asm     mov     ax,0x13
asm     int     0x10
        VL_DePlaneVGA ();
        VGAMAPMASK(15);
        VL_SetLineWidth (40);
}

Wolfenstein 3D source code (circa 1992) which calls the BIOS to change to mode 13h.

References

[edit]
  1. 1 2 Brown, Ralf D. "Ralf Brown's Interrupt List". Retrieved 2018-08-24.
  2. ↑ Fabien Sanglard, GAME ENGINE BLACK BOOK: WOLFENSTEIN 3D, 2nd edition, Chapter Hardware, page 59, freely available at https://fabiensanglard.net/gebb/index.html
  3. ↑ "Michael Abrash's Graphics Programming Black Book Special Edition: Parallel Processing". www.phatcode.net.
  4. 1 2 "Michael Abrash's Graphics Programming Black Book Special Edition: Higher 256-Color Resolution on the VGA". www.phatcode.net.
  5. ↑ "32. Mode 13h Programming" (PDF).
  6. ↑ "VGA/SVGA Video Programming--Accessing the VGA Display Memory". web.stanford.edu.
  7. ↑ "Graphics 2: VGA and SuperVGA". For example, if we have a function, uploadRegisters, that does this, we can set Mode 13h by doing the following.
  8. ↑ [www.phatcode.net/res/224/files/html/ch31/31-02.html "Michael Abrash's Graphics Programming Black Book Special Edition: Higher 256-Color Resolution on the VGA"]. www.phatcode.net. {{cite web}}: Check |url= value (help)
  9. ↑ "Microsoft QuickBasic 4.5 Advisor". Microsoft. 1990. Archived from the original on 2009-01-05. Retrieved 2007-07-19.
  10. ↑ A major example is QuickBASIC screen mode 0, which covers all of the BIOS text modes 00h..03h. Also, QuickBASIC screen modes 1 and 2 correspond to the CGA BIOS modes 04h through 06h. Further note that QuickBASIC has modes for the Hercules graphics mode, which has no standard BIOS support at all, and for Olivetti/AT&T extended-CGA graphics modes.
  11. ↑ https://github.com/id-Software/wolf3d/blob/master/WOLFSRC/ID_VL.C#L108
[edit]