BASIC-DOS

A PCjs Retro-Programming Project

Project maintained by jeffpar

BASIC-DOS Technical Reference

Running in PCjs

BASIC-DOS runs on real IBM PCs and compatibles, but most people run it in PCjs, in a browser or with pc.js. This page lists every way that BASIC-DOS (or a program running on it) and PCjs work together beyond emulating the hardware. Every one of them is optional: without PCjs, BASIC-DOS behaves as it does on real hardware.

The PCjs side of each is in the pcjs repository (machines/pcx86/modules/v2 for the emulator, machines/pcx86/modules/v3 for disk images, and tools/pc for pc.js), which BASIC-DOS’s repository mirrors (see Conventions).

At a Glance

Interface Direction BASIC-DOS PCjs
Date and time PCjs supplies data at boot ddclk_init (clkdev.asm), INT_CLKDATA (pcjs.inc) ChipSet.setBDClock (chipset.js)
Disk requests Program asks PCjs INT 21h AX = PCJS_REQ or PCJS_REPLY (pcjs.inc): COMMAND’s LOAD drive: (loadDisk, editcmd.asm), a DOS statement (eg, in CDTEST.BAS); call_dos (v86dos.asm) FDC.intDOS (fdc.js), HDC.mountNamedDisc (hdc.js), DiskMenu.searchDisks (diskmenu.js)
Disk indexes PCjs searches files on disks   disks/indexes (tools/misc/diskindex.js), DiskMenu.getFiles (diskmenu.js)
Typed input PCjs watches DOS_UTL_EDITLN (INT 2Bh AH = 25h) Keyboardx86.intDOS (keyboard.js)
Mouse pointer PCjs reacts to hardware MOUSE driver (INT 33h function 1Fh) Mouse.releaseAll (mouse.js)
Memory arena PCjs Debugger reads memory CPU_ARENA, DD_LIST (bios.inc) Debuggerx86.dumpArena (debugger.js)
Disk images PCjs reads and writes disks boot sector, BPB (disk.inc), MBR (hdddev.asm) DiskInfo (diskinfo.js), HDC (hdc.js), pcjs.mbr
Claude gateway Program talks to the host over COM2 CLAUDE.BAS assets/js/claude-gateway.js, tools/claude/gateway.js
pc.js pc.js builds disks and runs tests –sys=bd entry (pc.json5) pc.js

Date and Time

A PC or PC XT has no real-time clock, so DOS normally asks for the date and time at startup. In PCjs, those machines (models before the 5170) watch for an INT 13h function 02h read into 0:7C00h, and when it returns with carry clear and the sector is a BASIC-DOS boot sector (its system file names, BASDEV COMBASDOS COM, follow the BPB at offset 2Bh), PCjs stores the host’s date and time in vectors 58h-5Bh:

Address Contents
0:160h “PCJS” (PCJS_SIG1 = 4350h, PCJS_SIG2 = 534Ah)
0:164h Day (1-31) and month (1-12), one byte each
0:166h Year (word)
0:168h Ticks since midnight (dword; 1800B0h ticks per day)
0:16Ch The BIOS tick count when they were saved (dword, from TIMER_LOW and TIMER_HIGH)

When CLOCK initializes, it adds the ticks since then (the BIOS tick count less the saved one) to the saved time, and uses the result, unless:

  • a letter was pressed at the boot message (BOOT_KEY >= ‘A’; - still sets the switch character without discarding them), which is how a user asks to be prompted;
  • the result is within 10 seconds of midnight, so the timer interrupt can’t advance the date before DOS loads; or
  • CLOCK is initializing in protected mode, where DDINT_GETVEC returns 0:0 for vectors that aren’t the ROM’s.

Either a real-time clock’s date or PCjs’s makes IOCTL_CLOCK (see CLOCK) and DOS_UTL_GETDATE with AL = 1 (see Utility Functions) return 1, and COMMAND.COM then skips the startup prompts. Nothing else in BASIC-DOS reads these vectors, and they aren’t cleared.

Disk Requests

A program can ask PCjs to put a disk in a drive, as if the user had chosen it from the page’s drive list, with an INT 21h function that DOS doesn’t have (its equates are in pcjs.inc):

Register Value
AX 7F00h (PCJS_REQ), or 7F01h (PCJS_REPLY) for a reply
BX:CX “PCJS” (BX = PCJS_SIG1 = 4350h, CX = PCJS_SIG2 = 534Ah)
DS:DX the request: a string of up to 255 characters, ending with a null or a CR
ES:DI PCJS_REPLY only: a buffer for the reply
SI PCJS_REPLY only: the buffer’s size

COMMAND’s LOAD drive: [disk] (see LOAD) makes a PCJS_REPLY request of LOAD and its arguments, displays the reply, and reports any error; it’s loadDisk in editcmd.asm, outside COMMAND’s core, which cmdLoad reaches through genUpper when LOAD’s argument is just a drive. In BASIC, the DOS statement can also make a request (without a reply); eg:

DOS &H7F00, &H4350, &H534A, "LOAD D: PC-SIG Library CD-ROM 8th Edition (April 1990)"

The only request is LOAD drive: [disk]. Drives A: and B: (and any other diskette drives) belong to the FDC component; any other letter is matched against the drives of other controllers (eg, a CD-ROM drive, whose letter is the one after the hard disks; see HDC.getDriveLetter()). The disk is found in the drive’s lists (the drop-down menus’ lists, all of which PCjs fetches if it has to; see DiskMenu.searchDisks()):

  • Without a disk, the reply is the name of the drive’s current disk (or None).
  • A number picks that disk from the drive’s last list of matches.
  • None removes the disk, and a disk containing / is a path or URL.
  • A disk whose name matches exactly (in any case) is loaded.
  • Otherwise, every word must appear in a disk’s name, in any order (a quoted phrase is one word). A word with a wildcard (* or ?), or that looks like an 8.3 file name with a letter (eg, ADVENT.EXE, but not 2.00), is a file word instead: PCjs looks for disks with a matching file in the list’s disk index, and the other words must appear in those disks’ names. If no disk has the file, the file words are treated as ordinary words.

PCjs handles the interrupt itself, and DOS never sees it:

  • While PCjs is still looking for the disk (eg, fetching a disk list or index) or loading it, it backs IP up to the INT, so the program executes it again; interrupts are taken in between, so other sessions keep running.
  • If one disk matched and was loaded (or removed), or the request had no disk, carry is clear and AX = 0, and a reply describes the disk: its name alone for a request with no disk, or Loaded "name" in X: (followed for a file search by the file’s path and date) or Removed the disk in X:.
  • If several disks matched, none is loaded, carry is clear, and AX = their number; a reply lists them, numbered, as many as fit (followed by “and n more” if some didn’t), and then how to load one (Use "LOAD [drive] [number]" to load one of the numbered disks above), and they become the drive’s list for a later LOAD drive: n.
  • If no disk matched or it couldn’t be loaded, carry is set and AX = 2 (ERR_NOFILE), which the DOS statement reports as BASIC error 53; if there’s no such drive, AX = 15 (ERR_BADDRIVE), error 77.

A reply is a null-terminated string of lines, each ending with CR LF, cut to fit the buffer (SI includes the null), with any character above 7Eh replaced by ?. Without PCjs, or for any request that isn’t a LOAD (or that lacks the signature), the INT goes to DOS, which fails function 7Fh with ERR_INVALID (BASIC’s “Illegal function call”; COMMAND’s LOAD reports “Unable to load X”). So a program can’t tell “no PCjs” from “a request this PCjs doesn’t understand”.

PCjs sees the request only when an INT 21h instruction executes, through a CPU notification (CPUx86.addIntNotify), and it reads the string through DS (and writes a reply through ES), so the request works in real mode and from ring 3 in protected mode (DS and ES can be selectors). In a V86 machine, whose IOPL is 0, the program’s INT faults to CPU before PCjs is notified, but V86.COM passes the function on to BASIC-DOS (see call_dos in v86dos.asm) with an INT 21h of its own, with the same registers and a selector for the machine’s DS, and that’s the one PCjs answers (and repeats, while it waits). PCjs never sees an interrupt that’s simulated (eg, PUSHF and a far call), or a V86 machine’s INT that its host doesn’t repeat.

PCjs keeps each pending request by its function and text, so different requests (eg, from two sessions) can be pending at once; a finished request’s result waits for the program to repeat the INT (for up to 10 seconds). Two sessions making the same request at the same time share it.

TESTS\MISC\CDTEST.BAS uses these requests to load a CD-ROM and blank diskettes before it runs every program on the disc (see Testing with CDTEST).

Disk Indexes

File searches use indexes of the files on every disk in the lists, which tools/misc/diskindex.js builds in disks/indexes, and which each list names with an @files property (eg, diskettes.json’s is /disks/indexes/diskette-files.json; a relative name is relative to the list):

{"disks": [disk paths], "dirs": [directory names], "files": {"NAME.EXT": [disk, dir, date, ...]}}

Each file name maps to triples: the index of a disk in disks, the index of a directory in dirs (eg, GAMES/ADV, without a leading slash; "" is the root), and the file’s date as a number (YYYYMMDD, or 0 if it’s unknown). An index that would be too large to fetch at once is sharded: its files is replaced by "@shards": "folder/", and the folder has a file for each first character (A-Z, 0-9, or _ for any other), with its own dirs and files; a search fetches only the shards that its file words need (so a word that begins with a wildcard can’t search a sharded index).

node tools/misc/diskindex.js diskettes reads every diskette image in the diskette lists (skipping volume labels and directories), and node tools/misc/diskindex.js discs reads the directories of every disc in the CD-ROM list with HTTP Range requests (caching each disc’s directories in tools/pc/disks/tmp-cdindex, so a later run reads only new discs). To keep the CD-ROM index small (about 28Mb, or 7.5Mb compressed, with no shard over 2.7Mb), it has only EXE, COM, BAS, and ZIP files (DISC_EXTS in diskindex.js), and only the discs that have any; the diskette index has every file.

Typed Input

A machine on a page can have an autoType string (eg, DATE\rTIME\rDIR\r on the preview pages), which PCjs types for the user once the machine is ready for input. PCjs learns that from the line-input functions: INT 21h AH = 0Ah (DOS buffered input), and INT 2Bh AH = 25h (DOS_UTL_EDITLN), which COMMAND.COM uses instead. When either executes, the Keyboard component starts typing (or calls a callback that’s waiting for the first prompt). As with disk requests, only an INT instruction that executes is seen.

Mouse Pointer

The MOUSE driver turns a serial mouse’s power off (DTR and RTS off) when no program is using it, and back on with any INT 33h call other than function 1Fh (see MOUSE). PCjs’s serial mouse treats power off as “done”, and releases the browser’s pointer if it locked it; it captures the pointer again only when the mouse is powered on. This is ordinary hardware behavior, so any mouse driver that does the same gets it, but BASIC-DOS is why PCjs has it: without it, the pointer would stay captured after a program that used the mouse ended.

Memory Arena

The PCjs Debugger’s d dos command, with no MCB address, lists BASIC-DOS’s memory the way DEBUG.COM’s DM command does (see Physical Memory Arena). It reads these locations, which Debuggerx86.BASICDOS defines, and which must match BASIC-DOS’s:

Name Value BASIC-DOS definition
DD_LIST 0:50Ah bios.inc: the first device driver (to name system blocks by driver)
CPU_ARENA 0:514h bios.inc: the real-mode address of the arena: an MCB (offset 0) or the 80386 driver’s PMB table (nonzero offset)
LIN_BASE C0000000h the linear address of physical memory when paging is on
PMB_USED 01h cpu.inc: PMBTYPE_USED
OWNER_SYS 08h dosapi.inc: MCBOWNER_SYSTEM
TYPES FREE, CPU, RSVD, SYS, ARR, CODE, ENV, FUNC, STR, TEXT, VAR, DATA dbgdev.asm: MA_TYPES

With an MCB segment (d dos seg), it walks an ordinary DOS MCB chain instead.

In protected mode, the Debugger treats an INT 20h as a Windows VxD service call (INT 20h followed by a service ID), and sets a temporary breakpoint there so that it can label the call once Windows patches it. It does so only in 32-bit code, where VxD calls are made, so that it doesn’t confuse the INT 20h that ends a 16-bit BASIC-DOS program in protected mode with one; otherwise, the breakpoint would stop the machine whenever other code later ran at that address.

Disk Images

PCjs builds, reads, and writes disk images (diskinfo.js, used by both the emulator and pc.js), and it knows these BASIC-DOS formats:

  • Boot sectors: a BASIC-DOS boot sector starts with a near JMP to a CLD and has the OEM name “PCJS.ORG” (BPB_OEM in disk.inc); older ones start with the CLD itself. When PCjs writes one (eg, building a hard disk image), it fills in all four HIDDENSECS bytes (BASIC-DOS uses DOS 2.x’s boot drive byte as part of them), and BASIC-DOS’s extended BPB fields after the standard ones: DRIVE (24h), CYLSECS (25h), LBAROOT (27h), and LBADATA (29h), ending at 2Bh, where the system file names begin.
  • The pc.js MBR (pcjs.mbr) has “PCJS” at offset 199h and two drive parameter tables, which it installs at boot (INT 41h and INT 46h), so a machine can boot a hard disk whose geometry isn’t one of its controller’s drive types (eg, the demos’ 615x4x17 20Mb disk on a PC XT). HDD gets the geometry from INT 13h AH = 08h, so it needs nothing else. The MBR that HDD’s set_part writes is modeled on it, without the tables.
  • PCJS controller types: an HDC whose type starts with PCJS (eg, PCJS-XT) adopts a disk image’s geometry instead of warning that it doesn’t match the drive type.
  • CD-ROM drives identify themselves as “PCJS CD-ROM” (ATA IDENTIFY) and “PCJS.ORG” (SCSI INQUIRY). CDC doesn’t check either; it waits up to a minute for a command, since a disc’s data may still be arriving over the network (see CDD).

Claude Gateway

CLAUDE.BAS (see CLAUDE) sends questions through COM2 (9600 baud, 8 data bits, no parity, 1 stop bit) to a gateway, which asks Claude and returns the answer in chunks. The protocol uses only ASCII control codes that never appear in the text:

From Bytes Meaning
PC ENQ End any answer in progress, and reply with ACK
PC text EOT A question: a parameter line (the console’s columns, and NEW to start a new conversation), CR, and the question
PC ACK Send the next chunk
Gateway ACK The reply to ENQ
Gateway text ETB A chunk of the answer (at most 100 bytes); wait for ACK
Gateway text EOT The last chunk
Gateway NAK (anywhere in a chunk) the rest of the answer is an error message

Chunks keep the COM driver’s input buffer (128 bytes by default) from overflowing. The gateway converts answers to code page 437 with CR LF line endings, wraps them to the console’s width, and never sends CTRL-C, CTRL-Q, or CTRL-S, which the COM driver handles itself.

The gateway is assets/js/claude-gateway.js. On a demo page that includes it, connectPage() binds it to the machine’s COM2 (the SerialPort component’s exports.bind) and gets an API key from the page’s #claudekey= fragment or a prompt; in Node, tools/claude/gateway.js runs it on stdin and stdout, and pc.js’s --com2="command" connects a command to COM2. Unlike a disk request, this is ordinary serial I/O, so CLAUDE.BAS works the same on a real PC connected to another computer with a null-modem cable.

pc.js

pc.js runs PCjs machines in Node, and builds their disks. With --sys=bd, the “bd” entry in pc.json5 builds a BASIC-DOS 2.x disk from src: the boot sector (BOOT1.COM), CONFIG.SYS (src/configs/console/serial/fpe’s, which puts the console on COM1), and BASDEV.COM and BASDOS.COM (HIDDEN, SYSTEM, and READONLY) and COMMAND.COM (SYSTEM and READONLY, so DIR still lists it). For BASIC-DOS, pc.js also:

  • marks its helper programs HIDDEN and SYSTEM (BASIC-DOS runs hidden files), so DIR doesn’t list them;
  • adds its startup commands (from --commands, --test, and the current drive and directory) to the first SHELL line, separated by ` : `, as long as the command tail fits in a PSP (126 characters) and no IF precedes the last command; otherwise it puts them in AUTOEXEC.BAT (starting with @ECHO OFF) and adds that to the SHELL line, since BASIC-DOS doesn’t run AUTOEXEC.BAT itself;
  • writes program paths with /, and with no path for a program in the root;
  • replaces the boot message with “Hard disk loading…” when building a hard disk (as SYS and FORMAT do);
  • accepts FAT16 volumes, which BASIC-DOS 2.x supports (DOS needed 3.00);
  • with --floppy, removes a PC XT’s hard disk controller, whose empty drive would make the BASIC-DOS boot sector wait for a key;
  • with --serial, translates the terminal’s arrow, function, and editing keys to the control characters that BASIC-DOS treats as their aliases (see the scan code table in condev.asm); and
  • when it saves a disk after running faster than real time (eg, --speed), scales file dates that are in the future back into the time since the machine loaded, keeping their order, so MAKE doesn’t rebuild everything.

pc.js’s own programs, which it puts on the disks it builds, end with “PCJS” after an INT, which pc.js intercepts:

Program Interrupt Action
QUIT.COM INT 19h at offset 102h Quit pc.js; QUIT /S [drive:] file saves the drive as a disk image first, and QUIT /R reboots
LOAD.COM INT 20h at offset 102h Run a pc.js load command
Helper programs (eg, LS.COM) INT 20h Run the host command of the same name in the current drive’s directory (not in a browser)

Conventions

These are the conventions that the interfaces above follow, for adding others:

  • Optional: everything must still work without PCjs (eg, the clock prompts as on real hardware, and a request fails with ERR_INVALID), and the user should be able to decline what PCjs supplies (eg, a letter at the boot message).
  • The signature: “PCJS” is the same 4 bytes everywhere (4350h and 534Ah as words): the clock data, BX:CX in a request, after the INT in pc.js’s programs, and in pcjs.mbr; BASIC-DOS’s boot sectors use “PCJS.ORG”.
  • Two kinds of channel: an INT 21h request (function 7Fh) reaches only PCjs, and can do things only an emulator can (eg, change disks); a serial port (like the Claude gateway’s) reaches anything on the other end of a cable, so a real PC can use it too.
  • Requests are text, so a new verb needs no new registers. FDC handles the only verb (LOAD), so a verb that isn’t about disks needs a handler of its own (eg, in another component), which must return true (pass the INT on) for requests it doesn’t recognize.
  • Data PCjs supplies at boot goes in low memory that BASIC-DOS owns at that point (vectors 58h-5Bh) with a signature, and its equates go in pcjs.inc, with the other equates for PCjs interfaces.
  • PCjs sees only executed INT instructions, so a V86 program’s request reaches it only because V86.COM repeats the call (see Disk Requests).
  • Constants that PCjs keeps (Debuggerx86.BASICDOS, the boot sector signature in ChipSet.setBDClock, and DiskInfo’s extended BPB) must change when BASIC-DOS’s definitions do.
  • Both repositories: emulator changes go in pcjs (machines/pcx86 or tools/pc) and in BASIC-DOS’s copy, and the site needs the compiled pcx86.js rebuilt.

« BASIC-DOS vs. DOS Extenders

Copyright (c) 2020-2026 Jeff Parsons Released under MIT License