Inside DOS 2 in Memory: How the File Manager Works
Atari DOS 2.0S is two files on the disk, DOS.SYS and DUP.SYS, but in memory it is a small layered system. Knowing those layers explains everyday puzzles: why MEMLO sits where it does, why a program can only open three disk files at once, why DOS from BASIC wipes low memory unless MEM.SAV exists, and why block reads are so much faster than GET loops. This guide follows a disk file request from BASIC down to the drive and checks the key addresses on a running system.
What you'll learn: the four parts of DOS and how control passes between them, the boot sector fields that load DOS.SYS, where FMS lives and how it registers itself as D:, the file control blocks, how buffers are allocated (and how to give memory back), burst I/O, and the write-verify flag.
What you need: Atari BASIC and a DOS 2.0S disk (an emulator is fine). Inside a DOS 2 Disk: VTOC, Directory, Sector Links and Deleted Files covers the disk side: the VTOC, the directory and the sector links. The test program in this guide was run in atari800 on a 64K XL with the real Atari BASIC Rev. C ROM, booting a DOS 2.0S disk and, for comparison, DOS 2.5.
1. Four layers
"DOS" is really four elements, from most visible to least:
- DUP (DUP.SYS), the Disk Utility Package: the menu. It is just an application program that calls CIO like any other. OSS replaced it with a command line in its OS/A+, and other DOSes did the same.
- CIO, Central Input/Output, in the OS ROM: it finds the device in the handler table and calls it.
- FMS, the File Management System, which is DOS.SYS proper. To CIO it is simply the handler for the device "D:".
- SIO, Serial Input/Output, in the OS ROM: it moves sectors over the serial bus.
A BASIC OPEN #1,4,0,"D:FILE" goes BASIC → CIO → FMS → SIO → drive, and the data comes back the same way. The book stresses that a program which keeps to this path (calling only CIO) works with any OS revision and any compatible DOS. That is why replacement DOSes and FujiNet or PC-link drivers can slot in underneath unchanged programs.
2. Booting DOS
The OS reads sector 1 into $0700. The first bytes tell it what to do next. Here is the boot sector of the DOS 2.0S disk we used, read on the PC:
| Offset | Address | Name | Value on our disk | Meaning |
|---|---|---|---|---|
| 0 | $700 | BFLAG | $00 | unused |
| 1 | $701 | BRCNT | $03 | boot sectors to read |
| 2–3 | $702 | BLDADR | $0700 | where to load them |
| 4–5 | $704 | BIWTARR | $1540 | initialisation address (in DUP) |
| 6–8 | $706 | JMP XBCONT | $4C $14 $07 | boot continuation at $0714 |
| 9 | $709 | SABYTE | $07 | number of file buffers |
| 10 | $70A | DRVBYT | $03 | drives 1 and 2 |
| 12–13 | $70C | SASA | $1A7C | start of buffer allocation |
| 14 | $70E | DSFLG | $01 | DOS.SYS exists, 128-byte sectors |
| 15–16 | $70F | DFLINK | 4 | first sector of DOS.SYS |
| 17 | $711 | BLDISP | 125 | offset of the sector link byte |
| 18–19 | $712 | DFLADR | $07CB | where DOS.SYS loads |
The OS loads the boot sectors and calls the code at $0706. That jumps to XBCONT, which follows DOS.SYS's sector chain from DFLINK and reads it into memory from DFLADR. The OS later calls the initialisation address, which in DOS 2.0S points into DUP.SYS's resident part at $1540. DUP initialises FMS by calling its init routine DINIT. The standard DOS 2.0S setting for SABYTE is 3; our copy had been saved with 7.
3. FMS in memory
In memory, DOS 2.0S is laid out like this:
- $0700–$1500: FMS (DOS.SYS). Key entry points: BSIO, the sector I/O routine, at $076C, and the handler vector table DFMSDH at $07CB (1995).
- $1540 up: the resident part of DUP, including the MEM.SAV logic.
- About $1A7C up: the drive and sector buffers, 128 bytes each.
- MEMLO above that: 7420 ($1CFC) with the default two drives and three file buffers.
The rest of DUP.SYS (the menu and its utilities) loads into the user area above MEMLO when you type DOS. That is why BASIC programs get overwritten unless DUP is told to save that memory to MEM.SAV first.
Our test program found the D: entry in the handler table (HATABS, from 794) pointing to 1995, DFMSDH, exactly as described. DOS 2.5 gave the same address.
4. Initialisation and buffers
When DUP calls DINIT ($07E0), FMS:
- Reads DRVBYT ($70A, 1802). Each bit 0–3 stands for drive 1–4. For each drive that is enabled and answers a status command, FMS sets aside a 128-byte drive buffer that holds that disk's VTOC (two for the 815's 256-byte sectors).
- Reads SABYTE ($709, 1801) and sets aside that many 128-byte sector buffers, one for each file open at the same time.
- Clears the file control blocks.
- Sets MEMLO just above the last buffer.
- Puts "D" into HATABS with the address of DFMSDH.
That is the whole story of "three files at once": SABYTE = 3 means three sector buffers. It also tells you how to save memory. Each drive you don't have and each buffer you don't need costs 128 bytes. We tested it: on our disk (SABYTE 7, DRVBYT 3), MEMLO was 7932. The program then POKEd both to 1 and we pressed RESET. Because DUP calls DINIT again on every RESET, FMS reallocated its buffers, and the second run reported MEMLO 7036: 896 bytes more for BASIC. That matches the standard figures exactly: 7420 for the default setting, minus 128 for each buffer removed. To make such a change permanent, write DOS again with the DUP menu's option H.
The catch: with only one sector buffer, a program that opens two disk files at once gets an error. With DRVBYT = 1, drive 2 is unusable.
5. File control blocks
For each IOCB there is a 16-byte File Control Block (FCB): FCB n belongs to IOCB n. Because both are 16 bytes, FMS reaches both with the same X register value (IOCB number × 16). The book places the FCBs at $1381 in DOS 2.0S and describes these fields:
- FCBFNO: the file's directory number (0–63) shifted left two bits. It is written into every data sector, so FMS can detect a broken chain.
- FCBOTC: how the file was opened: 4 input, 8 output, 12 update, 1 append, 2 directory.
- FCBSLT: a flag for files created by DOS 1.
- FCBFLG: $80 = may acquire new sectors (output or append), $40 = the buffered sector has been changed and must be written.
- FCBMNL: bytes per data sector (125, or 253 on an 815), or for reading, how many bytes the current sector holds.
- FCBDLN: position of the next byte within the sector.
- FCBBUF: which sector buffer this file uses.
- FCBCSN, FCBLSN, FCBSSN: current sector, next (link) sector, and the start sector of an append.
The test opens TEST.DAT for output on IOCB 3, writes 10 bytes and prints FCB 3 from 4993 + 48. Under DOS 2.0S it showed:
8 8 0 0 192 125 10 1 85 0 0 0 0 0 0 0
They read naturally against the field list: 8 is file number 2 shifted left two places (TEST.DAT is the third directory entry, after DOS.SYS and DUP.SYS), 8 is "open for output", 192 ($C0) has both flag bits set (may grow, buffer modified), 125 is the sector's data capacity, 10 is the next byte position after our ten PUTs, 1 is the buffer number, and 85 is the current sector: the first free sector after DUP.SYS, which ends at sector 84 on this disk. The extra zero at offset 3 is a field we couldn't identify from the published field descriptions. DOS 2.5 showed the same pattern in the same place (output type 8, 125, 10, buffer 1).
6. Burst I/O: why block reads fly
CIO moves data to and from a handler one byte per call. For a 1,000-byte buffer, that means 1,000 calls into FMS, each with its own setup overhead. FMS short-circuits this with burst I/O: when a GET CHARACTERS or PUT CHARACTERS request (CIO commands 7 and 11) has a buffer large enough for a whole sector, and the file is not open for update, FMS points its sector pointer straight at the caller's buffer and reads or writes whole sectors there. It saves and restores the three bytes of each sector's control information that overlap the user's data on writes. Text (record) commands never use burst mode, because they must stop at an end-of-line character.
That is the reason behind the advice in CIO and IOCBs: How Atari Input/Output Really Works to load pictures and machine code with a block read rather than a GET loop.
7. The test program
10 PRINT "SABYTE (1801): ";PEEK(1801)
20 PRINT "DRVBYT (1802): ";PEEK(1802)
30 PRINT "MEMLO (743,744): ";PEEK(743)+256*PEEK(744)
40 FOR I=0 TO 11:IF PEEK(794+I*3)=68 THEN PRINT "HATABS D: -> ";PEEK(795+I*3)+256*PEEK(796+I*3)
50 NEXT I
60 OPEN #3,8,0,"D:TEST.DAT"
70 FOR I=1 TO 10:PUT #3,65:NEXT I
80 F=4993+3*16:PRINT "FCB #3:"
90 FOR I=0 TO 15:PRINT PEEK(F+I);" ";:NEXT I:PRINT
100 CLOSE #3
110 POKE 1801,1:POKE 1802,1
Type it in or ENTER it, with a DOS 2.0S disk in drive 1 that has room for a small file. RUN it once, note the numbers, press RESET, and RUN it again to see the effect of line 110. Line 80 uses the DOS 2.0S FCB address; other DOS versions may keep their FCBs elsewhere.
8. Other useful FMS locations
- 1913 ($779): the write-verify command. Mapping the Atari gives 87 ($57, write with verify, the default) and 80 ($50, write without verify), which is faster at some risk.
- 1801 and 1802: SABYTE and DRVBYT, as above.
- DOSVEC and DOSINI (10 and 12): set up by DUP so that
DOSfrom BASIC and RESET reach it. See The RESET Key: Warm Start, Cold Start and Writing RESET-Proof Programs.
Troubleshooting
- ERROR 161 (too many files open) after reducing SABYTE: the program opens more disk files at once than there are sector buffers.
- Drive 2 stops working: DRVBYT doesn't include bit 1.
- Changes to 1801/1802 seem to do nothing: they only take effect when FMS initialises again, at RESET or boot.
- The FCB dump shows nonsense: you're not running DOS 2.0S, or the file isn't open on the IOCB you are looking at.
- DOS wipes the BASIC program: use MEM.SAV, or a DOS that keeps its menu out of user memory.
Further reading
- Bill Wilkinson (compiler), Inside Atari DOS (COMPUTE! Books, 1982): the commented source of FMS, chapters on FCBs, initialisation, burst I/O and the boot process.
- Ian Chadwick, Mapping the Atari: locations 1792–7548, with the boot sector layout, SABYTE, DRVBYT, DFMSDH and the buffer addresses.
- Atari, Disk Operating System II Reference Manual (C016347).
- On this site: Inside a DOS 2 Disk: VTOC, Directory, Sector Links and Deleted Files, Atari DOS 2.5: A Practical Guide to the Menu, RAMdisk and Utilities, Writing Your Own CIO Device Handler, Writing a Bootable Disk Program (No DOS Required).
Credits
- Picture: The test program under DOS 2.0S: buffer settings, MEMLO, the D: entry in the handler table, and the file control block of an open file. — Program authors; screenshot by the site
- Inside Atari DOS (COMPUTE! Books, 1982) — ch. 1 overview, 3 FCBs, 4 FMS initialization (DRVBYT, SABYTE, MEMLO, HATABS), 5-6 FMS entry/exit, 12 Burst I/O, 20 boot process — AtariArchives.org, 1982
- Mapping the Atari — 1792-7548 DOS area, boot sector layout, 1801 SABYTE, 1802 DRVBYT, 1900 BSIO, 1913 write verify, 1995 DFMSDH, 5440 DUP init, 7420 MEMLO — AtariArchives.org, 1995
