Mode 13h playground

This is an in-browser emulator of an 8086, with 1 megabyte of memory and a VGA card that supports mode 13h. Edit the assembly file, press Run, and it is assembled and executed: the pixels you see are the bytes in the memory page starting at A000:0000.

Loading…

What is included in the emulator

  • CPU: the full documented 8086 instruction set. Nothing from the 80186 onwards, so no shl ax, 4, push 5 or pusha.
  • Assembler: NASM-style syntax, with labels, .local labels, equ, db/dw/dd, times, %include and expressions.
  • Video: mode 13h, 320×200 with 256 colours, through int 10h and the palette ports 3C7h–3C9h.
  • Mode X: turn chain-4 off through the sequencer and the card’s full 256K appears as four planes, with the map mask, read map, write modes and latches working as on a real VGA. The CRTC start address (for page flipping) and the 240-line setting work too. Split screen, pixel panning and the 400- and 480-line modes are not emulated. The Mode X page flipping example shows the setup.
  • Two buffers: the screen at segment A000h, and a back buffer in the 64K above your program.
  • Timing: a fixed 60 frames a second, or 70, which is what a real VGA did in this mode, or 30 to slow things down. The CPU itself is not slowed down. It runs as fast as your browser allows.
  • Clock cycles: every instruction is charged the clock cycles Intel published for the 8086, including the effective address time for memory operands. The status line shows how many cycles the program used per second and per frame. For scale, an original 4.77 MHz PC had about 79,500 cycles in a 60 Hz frame. The figures leave out the time a real 8086 spent fetching instruction bytes, so real hardware was somewhat slower still. Use wait_retrace to wait for the frame to be drawn.
  • Raster effects: each scanline is read from video memory as the beam reaches it, so changing the palette while the frame is drawing can have interesting effects.
  • Program format: a .COM file. Start with org 100h, and end with ret or int 20h.
  • Keyboard: click the screen first, then int 16h and port 60h see your keys.
  • hlt sleeps until the next vertical retrace, and int 3 pauses and shows the registers.

The library

vga.inc is ordinary source in the second tab; read it, change it, or ignore it and write your own. Coordinates go in BX (x) and DX (y), the colour in AL, and every routine preserves the registers it does not return a value in.

RoutineInputsWhat it does
vga_initEnter mode 13h and find the back buffer
vga_doneReturn to text mode
target_screen, target_bufferChoose where the drawing routines write
put_pixelBX, DX, ALPlot a point, ignoring anything off-screen
get_pixelBX, DXReturn the colour in AL
draw_lineBX, DX to SI, DI, ALBresenham line
draw_ellipseBX, DX, radii CX (horizontal) and SI (vertical), ALBresenham-style ellipse
draw_circleBX, DX, radius CX, ALAn ellipse with equal radii
flood_fillBX, DX, ALPaint fill of the area around a point
clear_bufferALFill the whole target
copy_bufferCopy the back buffer to the screen
wait_retraceWait for the next vertical retrace
presentwait_retrace, then copy_buffer
set_palette_entryindex AL, red AH, green BL, blue BHChange one colour (components are 0–63)
get_palette_entryindex ALReturn red AH, green BL, blue BH
load_paletteDS:SISet all 256 colours from 768 bytes
save_paletteES:DIStore all 256 colours into 768 bytes
randomReturn a pseudo-random number in AX
key_pressed, wait_keyCheck for a key (ZF clear if one is waiting), or wait for one

Further reading