V86
V86 [/M:size] [program [arguments]]
Runs a real-mode program (a COM or EXE file, with .COM or .EXE added if program has no extension, found in the current directory, or if program has no drive or directory, in a directory of PATH$) in a V86 machine: memory that the program sees as an 8086 would, at addresses starting from 0, with the ROMs above it, while BASIC-DOS keeps running in protected mode. Without a program, V86 runs COMMAND.COM, so START V86 opens a real-mode command shell on a console of its own. /M:size sets the machine’s memory, in K, from 4 to 640 (eg, START V86 /M:256 opens a shell in a 256K machine, and START /B V86 /M:256 DEBUG runs DEBUG in one without switching to its console); without it, the machine has 640K. The machine uses the system’s memory only as its programs touch theirs, 64K at a time (a new shell uses about 150K), so several 640K machines can run in less memory than they add up to; a smaller size only limits what its programs can ask for. Any switches after program are the program’s own, but any other switch before it (or a size out of range) is a usage error. V86 gets PATH$ the way an external program does, from its environment, but since a session that START makes has none, START gives V86 the current PATH$ ahead of its arguments, as it does for COMMAND.COM (eg, START V86 DEBUG runs V86 PATH$="C:\;C:\EXT":DEBUG), and V86 passes that PATH$ on to its program’s environment, as PATH.
V86 machines need an 80386 (or later) running BASIC-DOS in protected mode with the 80386’s features, which is the default (unless CONFIG.SYS has a CPU=8088 or CPU=80286 line; see CPU); otherwise, V86 displays V86 mode not available. On such a machine, running a real-mode EXE file in protected mode (eg, WOLF3D at the prompt, or in a BAS or BAT file), which protected mode can’t load, runs it as START V86 MSDOS program [arguments] would: in a new session, on a console of its own that gets the focus, whose V86 machine runs only that program (under MSDOS, which provides DOS services that BASIC-DOS doesn’t), so the session and its console close when the program ends (after Press any key to return to your original prompt; see below). As with START, COMMAND doesn’t wait for the program, so a BAS or BAT file goes on with its next command, and ERRORLEVEL is 0 once the session has started. If the program’s input or output is redirected (eg, WOLF3D > LOG or WOLF3D | MORE), which a new console would replace, it fails instead, with Unable to load and its name, followed by Run MSDOS first: MSDOS opens a real-mode command shell that can run it (or V86 program runs it on the current console, with its redirection). On an 80286, it simply fails with Unable to load.
The program can do anything an 8086 program can, except I/O (other than the ports listed below), and its interrupts and exceptions go to its own vectors, which it can hook (eg, with DOS function 25h). V86 does the DOS functions that involve the machine itself (its vectors, memory blocks, PSPs, DTA, and running and ending programs, with their exit codes), and passes the rest of the DOS and BASIC-DOS functions on to BASIC-DOS, so the machine’s files, consoles, and devices are V86’s (programs that need a DOS function that BASIC-DOS doesn’t provide, eg, Load Overlay, run under MSDOS, which provides it in a V86 machine, too). So COMMAND.COM runs in it as it does in real mode, along with the programs it runs (eg, BASIC-DOS’s utilities, and tools like MASM and SYMDEB), with these differences:
- Floating-point works, through a copy of the FPU driver’s functions in the machine’s memory.
- The later programs of a pipeline (eg, MORE in
DIR | MORE) run as ordinary BASIC-DOS programs, in sessions of their own, so a real-mode program can only be the first program of a pipeline. START and KILL work as they do at any other prompt.
- MEM and FRE report the machine’s memory, and so does the memory size in its BIOS data.
- CTRL-C ends the program (unless it has hooked INT 23h), as it does in BASIC-DOS: each program starts with the default CTRL-C handler, and its parent’s is restored when it ends.
- Programs can stay resident (TSRs): INT 27h and DOS function 31h keep their memory and open files in the machine (so they last as long as the machine does; eg, run MSDOS for a shell, and then the TSR), and while a program waits for a key (DOS functions 01h, 07h, 08h, 0Ah, or 0Ch, or COMMAND’s prompt), a program that has hooked INT 28h (DOS idle) gets one about 20 times a second, which is when a TSR like MS Library’s MSL can pop up (with Alt-Left Shift).
- DEBUG works as it does in real mode, except that DM reports
? (and LN finds no block), since the machine’s memory blocks are V86’s, not the CPU driver’s.
- The common BIOS video (INT 10h), keyboard (INT 16h), and printer (INT 17h) functions work, but V86 does them with the console’s own functions rather than the ROM’s, so they work on any console (even a terminal, which can’t read characters back from its screen, so programs that do, like MSBASIC, need a video console), and with its limits: a scrolled window is always as wide as the screen (though a character written at the end of the last row doesn’t scroll it, as with the ROM), and a character that the console acts on (eg, RETURN) can’t be drawn. The functions are set mode, set cursor type and position, get cursor position, scroll up and down, read and write characters at the cursor, write teletype, and get mode; wait for a key, check for a key, and get the shift states (and on a video console, the keyboard buffer and shift states in the machine’s BIOS data stay current, for programs like MSBASIC that look there first); and print a character and get the printer’s status. The equipment (INT 11h), memory size (INT 12h), and tick count (INT 1Ah, function 00h) work, too, which the startup code of many C programs asks for, and every system function (INT 15h) returns with carry set and AH = 86h (not supported), as an older BIOS’s do, so a program that asks for the system configuration (function C0h) to see if it’s on a PS/2 goes on. On an EGA or VGA, function 1130h (get font information) returns the character height and the console’s rows less 1 (but not a font’s address), which is how some programs know there’s one (without it, they treat the adapter as a CGA, and read its status port, which ends them with a
CPU fault), and loading a text font (functions 1100h-1114h) does nothing, since the console’s font and rows are its own. The other video functions (eg, the palette functions, 10h, the graphics font functions, 1120h-1124h, the EGA information, 12h, the display combination, 1Ah, and the functionality and state information, 1Bh, which is how programs know there’s an EGA or VGA) are the adapter’s own ROM’s, which the machine runs, since its code uses only the adapter’s ports and memory (an MDA or CGA has no ROM, so on one they do nothing); in a text mode, the adapter’s palette is the console’s, so the machine can’t change it (the ROM’s writes to the adapter’s ports do nothing, and its reads get FFh). Other BIOS functions do nothing.
- The machine gets timer ticks while its programs run: an INT 08h about 18 times a second, or as often as a program sets the timer’s rate (counter 0, through ports 40h and 43h), up to about 1165 times a second, although they arrive in bursts after each of the system’s ticks (ticks that are due while its interrupts are off wait until the next of the system’s interrupts); the default INT 08h handler issues an INT 1Ch, as the ROM’s does, and the tick count in its BIOS data stays current. There are no ticks while a program waits in a DOS or BIOS function (eg, for a key).
- On a video console, a program that hooks INT 09h gets a keyboard interrupt for each scan code (key presses and releases) while its console has focus, one after each of the system’s interrupts, and reads the code from port 60h; port 61h reads what was last written to it. A program that keeps the interrupt (rather than passing it on to the previous handler) gets no keys from INT 16h or DOS until it passes one on.
- If the system has a Sound Blaster (see SND), and no other V86 machine is using it, the machine borrows it, so its programs can play music and digitized sound (eg, Wolfenstein 3D): the card’s ports and its FM synthesizer’s (388h) are the machine’s, its interrupts are the machine’s, and its DMA channel works through the DMA controller’s ports (00h-0Fh and 80h-8Fh), with the machine’s addresses. The card goes back when V86 ends (however it ends), and V86’s own handle for it is the last of its 20, so the machine’s programs get only 19 handles.
- The other I/O ports are the interrupt controller’s (20h and 21h, which read as zero; an end of interrupt does nothing), the game port (201h), the parallel ports (278h, 378h, and 3BCh), and without a Sound Blaster, its ports (210h-26Fh), an FM sound card’s (388h and 389h), and the DMA controller’s, which read as they do when there’s no such device (programs like MSBASIC read the game port on every tick). In a text mode, the CRT controller’s ports (3B4h-3BAh and 3D4h-3DAh) work, too: writing its cursor location registers (0Eh and 0Fh) moves the console’s cursor, the status port (3BAh or 3DAh) alternates between retrace and display (for programs that wait for one, eg, to avoid snow on a CGA), and its other registers and ports read as FFh, with writes that do nothing. An IN or OUT with any other port ends the program with a
CPU fault message. The speaker doesn’t sound.
- Any other interrupt that the machine’s programs don’t handle themselves, other than DOS’s, a breakpoint (INT 03h) or an INT 06h instruction (which do nothing without a debugger, as in real mode), and a few that only check whether something is installed (INT 28h, 2Ah, and 2Fh, and INT 33h, whose default vector points to an IRET, which tells programs that there’s no mouse, unless the system has a MOUSE driver, whose functions the machine’s programs then get), ends the program with a
CPU fault message, and so does an instruction that real mode allows but a V86 machine doesn’t (eg, HLT).
- A program that resets the machine, with INT 19h, the keyboard controller’s reset command (FEh at port 64h), or a jump to the ROM’s reset code (eg, to FFFF:0000, as some setup programs do when they finish), ends, along with every program that ran it (so V86 ends, too, with exit code 0), as if the machine had been restarted.
- On a video console, the console’s video memory is at its usual address (B8000h, or B0000h for a monochrome adapter), with the console’s own text page there, as page 0 would be (whichever page the console is on, and the BIOS data says that page 0 is active), so programs can draw directly, and COMMAND.COM’s graphics statements (eg, in DONKEY.BAS) work; while another console on the same adapter is in another mode (eg, after SCREEN 1), the machine’s video memory is the saved copy of its console’s screen (see SCREEN), so its programs keep drawing, or if there wasn’t enough memory to save it, a program that touches it waits until that console returns to its original mode. On an EGA or VGA, a program can also set one of their own graphics modes (0Dh-13h, eg, mode 13h for Wolfenstein 3D), with INT 10h function 00h: the machine’s video memory is then the 64K at A0000h, and the program can use the adapter’s ports (3C0h-3DFh) directly, as it would in real mode, until it sets a text mode again. In those modes, which the console doesn’t draw in, all of the other video functions (eg, writing characters, which the ROM draws) are the ROM’s, and the ROM also sets the mode for the machine (eg, its BIOS data and font vectors). On a VGA, Shift-Tab can switch away from such a console: the machine is suspended (it doesn’t run at all, so a game stops, and a borrowed Sound Blaster pauses), and the adapter’s state (its registers, palette, and all of its memory) is saved, until Shift-Tab switches back to it, which restores the state and resumes the machine (FM notes that were playing stop until the music plays its next ones). KILL or CTRL-ALT-DEL still ends a suspended program, and its console returns to its original mode. If there isn’t enough memory to save the state (about 257K), or the adapter is an EGA, Shift-Tab can’t switch away. On a terminal, the machine has no video memory.
- The machine’s BIOS data is a copy, in which only the keyboard data, the tick count, and the video mode are kept current (and in an EGA or VGA graphics mode, the ROM keeps the rest of the video data).
- Since machines use the system’s memory only as their programs touch it, the system can run out after they’ve started. A program that touches memory when there’s none left for it waits, and V86 says so: on a video console in a text mode, with
Not enough memory: waiting in a box in the middle of the machine’s screen (so it’s seen whenever that console is, and what it covers returns when the wait ends); on a terminal, with that message on a line of its own; and in a graphics mode, with a beep. The program goes on as soon as another session frees enough memory (eg, a program in it ends, or KILL ends the session), and CTRL-C ends the waiting program instead (or goes to its own CTRL-C handler). The system keeps the last 64K of free memory for the pages that DOS itself must fill for a machine’s programs, so a wait can start with a little memory still free. When there isn’t enough memory to create a machine at all, V86 fails with Unable to load and the program’s name.
ERRORLEVEL is set to the program’s exit code, or 1 if V86 couldn’t run it. When V86 is the first program of its session (eg, START V86), its console closes when it ends, so V86 first displays Press any key to return to your original prompt and waits for one, so that the program’s last screen (eg, its messages) can be read.
Source: src/os/util/v86