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 5orpusha. - Assembler: NASM-style syntax, with labels,
.locallabels,equ,db/dw/dd,times,%includeand expressions. - Video: mode 13h, 320×200 with 256 colours, through
int 10hand the palette ports3C7h–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_retraceto 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
.COMfile. Start withorg 100h, and end withretorint 20h. - Keyboard: click the screen first, then
int 16hand port60hsee your keys. hltsleeps until the next vertical retrace, andint 3pauses 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.
| Routine | Inputs | What it does |
|---|---|---|
vga_init | Enter mode 13h and find the back buffer | |
vga_done | Return to text mode | |
target_screen, target_buffer | Choose where the drawing routines write | |
put_pixel | BX, DX, AL | Plot a point, ignoring anything off-screen |
get_pixel | BX, DX | Return the colour in AL |
draw_line | BX, DX to SI, DI, AL | Bresenham line |
draw_ellipse | BX, DX, radii CX (horizontal) and SI (vertical), AL | Bresenham-style ellipse |
draw_circle | BX, DX, radius CX, AL | An ellipse with equal radii |
flood_fill | BX, DX, AL | Paint fill of the area around a point |
clear_buffer | AL | Fill the whole target |
copy_buffer | Copy the back buffer to the screen | |
wait_retrace | Wait for the next vertical retrace | |
present | wait_retrace, then copy_buffer | |
set_palette_entry | index AL, red AH, green BL, blue BH | Change one colour (components are 0–63) |
get_palette_entry | index AL | Return red AH, green BL, blue BH |
load_palette | DS:SI | Set all 256 colours from 768 bytes |
save_palette | ES:DI | Store all 256 colours into 768 bytes |
random | Return a pseudo-random number in AX | |
key_pressed, wait_key | Check for a key (ZF clear if one is waiting), or wait for one |
Further reading
- The 8086 Family User’s Manual (Intel, October 1979) is the original reference for every instruction, flag and addressing mode.
It is a 63 MB scanned PDF.
- A more succinct guide to 8086 instructions can be found under this old Intro to Computers course: 8086 Family Users Manual
- Intel Op Codes from the PC Game Programmer’s Encyclopedia is a much shorter plain-text list of the instructions but the site is often down.
- The NASM manual covers the assembler syntax used here.
- Michael Abrash’s Graphics Programming Black Book, chapter 35, covers Bresenham’s line algorithm on the VGA in depth.
- Alois Zingl’s A Rasterizing Algorithm for Drawing Curves is the source of the ellipse routine in
vga.inc. - The PC Game Programmer’s Encyclopedia is I first learned this, including Denthor’s mode 13h tutorial and Mark Feldman’s Bresenham’s Line and Circle Algorithms.