LS-DOS 6.3.1 for the Model 4 - SYS3/SYS Disassembled

Page Customization

Introduction/Summary

LS-DOS 6.3.1 SYS3/SYS Disassembly (Model 4)

SYS3/SYS is the system overlay of LS-DOS 6.3.1 that closes files and devices and turns a directory entry code back into a file specification. It is a load module of 882 bytes, 825 of them loaded in five load records covering two ranges, 1E00H-2136H and the length word at 23FEH-23FFH, with its transfer address at 1E00H. CKMOD@ on the SYS0/SYS page reads it into 1E00H whenever an SVC asks for overlay 5 and it is not already resident, and RST28 calls 1E00H with the request in Register A: 95H @CLOSE and A5H @FNAME from the SVC stubs, 85H from the COPY command, which only wants the overlay loaded, and B5H, a function that 6.3.1 has reduced to a RET (1E00H).

@CLOSE (1E12H) takes an FCB at Register Pair DE. A device FCB gets its specification back and nothing else (209DH). For a file, a short drive check of its own makes sure a diskette is turning in the drive (on a floppy drive whose DCT+4 bit 7 is clear: not on drive 0 of the release); if not, the CLOSE FAULT message asks for ENTER to try again or BREAK to give up (20C8H). The file's directory entry is read, the last buffer is written if it was changed (1E53H), and an FCB opened with UPDATE access or better has the entry brought up to date: the open bit reset, the end of file copied from the FCB, and, when the file was written to, the MOD flag, the date and, on a diskette dated in the new style, the time (1E67H). The granules that the directory gives the file beyond its end are then given back to the GAT, one at a time from the last extent, and extended directory entries left empty are given up with their HIT slots (1F1DH, 1F76H). Last, the name, extension and drive are written into the FCB as text, which closes it (200DH).

@FNAME (2064H) reads the directory entry of a DEC on a drive and writes its specification, NAME/EXT:d, into a buffer of the caller with the same routine.

The bytes are those of the LS-DOS 6.3.1 level 1H system diskette. The labels are those of the LS-DOS 6.3.1 source code of SYS3, which assembles to this file byte for byte.

Notes

The date of a change goes into the entry in two forms. DIR+1 bits 3-0 take the month and DIR+2 the day (bits 7-3) and the year less 1980 modulo 8 (bits 2-0), the old style, which can tell only eight years apart. When the bit of YFLAG$ for the drive is set (the diskette's GAT+CDH bit 3, read by @CKDRV), DIR+18 takes the hours (bits 7-3) and the high three bits of the minutes, and DIR+19 the low three bits of the minutes (bits 7-5) and the full year less 1980 (bits 4-0, 1980 to 2011): the field that held the user password hash in earlier versions. With no date set, the month, the day and the year are 0. The MOD flag (DIR+1 bit 6) is set only when the FCB shows a write (FCB+0 bit 2); DIR+1 bit 4, which the TRM says marks a change made with no date kept, is neither set nor reset.

Only an FCB opened with UPDATE access or better (FCB+0 bit 6) changes the directory at close. It resets the open bit in DIR+1 and writes the entry back on every close, even when nothing else changed and whether or not the open bit had been written to the disk at the open (SYS2/SYS writes it only with NFLAG$ bit 0). A file set up with the CREATE command (DIR+1 bit 7) keeps all its granules.

A close returns Z with Register A = 00H and every register but Register Pair AF as it was. It returns NZ with the error in Register A when a directory, HIT or GAT sector cannot be read or written or the last buffer cannot be written, with error 32 when the CLOSE FAULT message is answered with BREAK, and with error 38 for an FCB that is neither an open file nor a device; after every error but 38 the FCB stays open. A file whose directory entry is found no longer in use returns Z with Register A = 11H and is not closed (B22). @FNAME returns Register Pairs DE and HL as they were, not Register Pair HL at the entry as the TRM says, changes Register B, ends the text with 03H, not the 0DH of the TRM, and gives error 16 for a DEC whose sector is past the directory; it does not look at whether the entry is in use.

There are no hidden instructions. Four operands are changed while SYS3 runs: 1EEAH (the bit number of BIT n,A for the drive's bit of YFLAG$), 1FE5H (the DEC of the entry before an FXDE that is given up), 205CH (the drive digit of the text) and 20C6H (the bit number of RES n,B for a granule). The jump at 1EB4H is never reached, and the error 15 path at 2004H (a write protected diskette after the entry was written) returns success without writing the GAT.

Bugs (listed on the LS-DOS reference page): B21, CLOSE reads the directory entry before it writes the last buffer and keeps its pointer into SBUFF$; when that write (byte I/O) has to look up or allocate a granule in an extended directory entry that lies in another directory sector, SBUFF$ is left holding that sector, and CLOSE changes the bytes at the file's place in it, writes the whole sector over the sector of the file's own entry, which destroys the entries there, gives back granules by the extents of whatever entry sits at that place, which can be another file's, and puts that entry's name into the FCB (1E61H); B22, a file whose directory entry is no longer in use is not closed: the last buffer is lost and the FCB stays open, but Z is returned (1E5EH, latent); B23, after the CLOSE FAULT message ENTER checks only that a diskette is in the drive, not that it is the one the file was open on, so a wrong diskette gets the last buffer and the directory and GAT changes of another (20EBH, limit); B24, two FCBs can have the same file open with UPDATE access, because the open bit is not written to the disk, and the one closed last puts its own end of file back into the entry and gives back the granules past it, so what the other FCB added is lost (1E8EH, latent).

Variables

Memory Used by SYS3/SYS

Address
Size
Contents
002EH-002FH
2 bytes
TIME$+1, TIME$+2
The minutes and the hours of the clock, read for the time field of the new style of dating (1EF5H).
0033H-0035H
3 bytes
DATE$
The year less 1900, the day and the month, read for the date of a change (1EC1H, 1EC9H, 1ED1H); all 00H when no date was set.
0074H
1 byte
KFLAG$
Bit 0, the BREAK bit, tested and reset through @CKBRKC while the CLOSE FAULT message waits for a key (20D0H, 20DCH).
0082H
1 byte
YFLAG$
One bit for each drive, set when its diskette uses the new style of dating; tested for the file's drive (1EE6H).
0470H-04BFH
80 bytes
DCT$
The drive code table entry of the file's drive: DCT+3 bit 3 hard drive, DCT+4 bits 4 and 7 alien and no index test, DCT+5 the current cylinder (1E29H-1E3CH); DCT+4 bit 5 two sided (1F91H, 207BH); DCT+7 heads and sectors (2068H); DCT+8 granules per track and sectors per granule (1F54H, 1F85H).
1D00H-1DFFH
256 bytes
SBUFF$
The directory sector of the file's entry (1E58H, 2019H), of its FXDEs (1F41H), of the entry before an FXDE that is given up (1FE6H) and of an entry asked for by @FNAME (2094H), and the HIT (1FD9H).
1EEAH
1 byte
BIT operand
The second byte of BIT n,A, 47H + 8 x drive (1EE3H).
1FE5H
1 byte
STUFDEC operand
The DEC of the entry before an FXDE that is given up, from the FXDE's DIR+1 (1FD6H).
205CH
1 byte
RCVN5 operand
The drive as an ASCII digit for the text of the specification (2025H).
20C6H
1 byte
CALC1 operand
The second byte of RES n,B, 80H + 8 x the granule (20C2H).
2300H-23FFH
256 bytes
DIRBUF$
The GAT of the file's drive, read when granules are to be given back (1F70H), changed bit by bit (1FAAH) and written back (2009H).
23FEH-23FFH
2 bytes
Overlay length
0337H, the length of SYS3 read by SYSTEM (SYSRES).

Routines of Other Files Called

AddressRoutine
052DH@DSPLY (BOOT/SYS): shows the CLOSE FAULT message (20CDH).
0553H@CKBRKC (BOOT/SYS): tests and resets the BREAK bit (20D0H, 20DCH).
0635H@KBD (BOOT/SYS): scans the keyboard (20D3H).
06E3H@DIV16 (BOOT/SYS): the ERN divided by the sectors per granule (1F61H).
13A2HRWRIT@ (SYS0/SYS): writes the last buffer if it was changed (1E61H).
1566H, 1568HLNKFCB@ and CKOPEN@ (SYS0/SYS): save the registers, point Index Register IX at the FCB and plant RESTREG (20A2H; 1E18H).
1803H@DIRWR (SYS0/SYS): writes a directory sector (1F18H, 1FCCH, 1FF5H, 1FFFH).
1874H, 1875H@GATRD and @GATWR (SYS0/SYS): read and write the GAT (1F70H; 2009H).
1897H, 1898H@HITRD and @HITWR (SYS0/SYS): read and write the HIT (1FD9H; 1FE0H).
18BBH@DIRRD (SYS0/SYS): reads the directory sector of an entry (1E58H, 1F41H, 1FE6H, 2019H, 2094H).
190AH, 1927H@MUL8 and @DIV8 (SYS0/SYS): sectors per cylinder (2075H); a granule turned into a cylinder and a granule in it (1F9AH).
19D0H, 19D4H@SEEK and @RSLCT (SYS0/SYS): a seek to the current cylinder and the controller status (1E41H; 1E23H, 1E47H).
1A1EH, 1A2BH@GTDCT and @DCTBYT (SYS0/SYS): the drive code table entry and one of its fields (1E20H; 1F54H, 1F85H, 1F91H, 2068H, 207BH).

Disassembly

1E00H - SYS3: Entry and the Function Dispatcher

SYS3/SYS is loaded at 1E00H by CKMOD@ (1A7FH on the SYS0/SYS page) whenever an SVC asks for overlay 5 and it is not already resident, and RST28 calls 1E00H with Register A holding the request. Bits 6-4 of the request pick the function: 95H @CLOSE and A5H @FNAME come from the SVC stubs at 1999H and 199CH; B5H was the customer service number check of earlier versions and is only a RET in 6.3.1. Request 85H only loads the overlay and returns (the COPY library command sends it to have SYS3 in memory).

1E00SYS3
AND 70H E6 70
SYS3: mask Register A (the request handed over by RST28: 85H, 95H, A5H or B5H) with 70H, keeping bits 6-4, the function number times 10H.
1E02
RET Z C8
Return at once if Register A is 00H: function 0 (request 85H) only gets SYS3 loaded; no SVC stub sends it, the COPY command in SYS6/SYS does (GETSYS3).
1E03
CP 10H FE 10
Compare Register A with 10H, the function of @CLOSE (request 95H, SVC 60, from the stub at 1999H).
1E05
If Register A is 10H, jump relative to 1E12H (CLOSE) to close the file or device whose FCB is at Register Pair DE.
1E07
CP 20H FE 20
Compare Register A with 20H, the function of @FNAME (request A5H, SVC 80, from the stub at 199CH).
1E09
If Register A is 20H, jump to 2064H (FNAME) to fetch the file specification of directory entry Register B on drive Register C into the buffer at Register Pair DE.
1E0C
CP 30H FE 30
Compare Register A with 30H, function 3 (request B5H), the customer service number check of the ID command in earlier versions.
1E0E
If Register A is 30H, jump to 2136H (DOID), which in 6.3.1 is a single RET: the Z flag of the CP and Register A = 30H go back to the caller.
1E11
RET C9
Return with the NZ flag of the CP and Register A = the function times 10H (40H to 70H): requests C5H to F5H have no function in SYS3 and do nothing.

1E12H - CLOSE: @CLOSE and the Drive Check

@CLOSE (SVC 60, request 95H) has Register Pair DE at an FCB. An FCB whose first byte has bit 7 clear is not an open file: CLOSDEV (209DH) closes a device FCB (10H) and refuses anything else with error 38. For an open file CKOPEN@ saves the registers and points Index Register IX at the FCB; then a short check of its own makes sure the drive holds a diskette before the directory is touched: the controller must not be busy, and for a floppy drive that is not marked hard, alien or without an index test, a seek to the current cylinder must be followed within 48 status reads by a moment without the index pulse. A drive that fails goes to HOLDUP (20C8H), which shows the CLOSE FAULT message and waits for ENTER (try again) or BREAK (give up with error 32). In the release drive 0 has that bit set in its DCT+4, so a close on drive 0 never looks for the diskette.

1E12CLOSE
LD A,(DE) 1A
CLOSE: load Register A with the first byte of the FCB at Register Pair DE: bit 7 is set in an open file's FCB+0; a device FCB holds 10H, a closed FCB the first character of a file specification.
1E13
BIT 7,A CB 7F
Test bit 7 of Register A, the open file bit of FCB+0.
1E15
If bit 7 is clear (no open file), jump to 209DH (CLOSDEV), which closes a device FCB or returns error 38.
1E18
Call CKOPEN@ at 1568H (on the SYS0/SYS page): with FCB+0 bit 7 set it saves Register Pairs HL, DE, BC and Index Register IX, points Index Register IX at the FCB and plants RESTREG (1589H), so that every RET below gives the caller its registers back with only Register Pair AF changed.
1E1B
LD C,(IX+06H) DD 4E 06
Load Register C with FCB+6 (Index Register IX+06H), the drive number of the open file, for the disk SVCs and @DIRRD below.
1E1E
PUSH IY FD E5
Save Index Register IY (the caller's) on the stack, because @GTDCT loads it with the drive code table entry; SAWBLK (1E53H) restores it, or ABRT at 20E3H when the close is given up.
1E20
Call @GTDCT at 1A1EH (on the SYS0/SYS page) to point Index Register IY at the drive code table entry of drive Register C (DCT$ 0470H + 10 x drive).
1E23CKAGN
CKAGN: call @RSLCT at 19D4H (on the SYS0/SYS page), driver function 7 for drive Register C: the floppy driver waits until the controller is not busy and returns Z with the status in Register A; a disabled drive returns NZ with Register A = 20H. HOLDUP (20EBH) comes back here after ENTER.
1E26
If @RSLCT returned NZ, jump to 20C8H (HOLDUP) to show the CLOSE FAULT message and wait for ENTER or BREAK.
1E29
BIT 3,(IY+03H) FD CB 03 5E
Test bit 3 of DCT+3 (Index Register IY+03H): set for a hard drive, which has no index pulse to test.
1E2D
If the drive is a hard drive, jump relative to 1E53H (SAWBLK) without the diskette test.
1E2F
BIT 4,(IY+04H) FD CB 04 66
Test bit 4 of DCT+4 (Index Register IY+04H): set for an alien drive (a driver of another kind than the floppy driver).
1E33
If the drive is alien, jump relative to 1E53H (SAWBLK) without the diskette test.
1E35
BIT 7,(IY+04H) FD CB 04 7E
Test bit 7 of DCT+4 (Index Register IY+04H), the bit that shortens the index test of @CKDRV (drive 0's DCT+4 is C1H in the release, so it is set there).
1E39
If bit 7 is set, jump relative to 1E53H (SAWBLK) without the diskette test. @CKDRV with this bit still waits for the index pulse to end and skips only the wait for a new one; CLOSE skips its whole test, so on drive 0 of the release it never looks for a diskette.
1E3B
PUSH DE D5
Save Register Pair DE (the FCB) on the stack, because the seek below takes its cylinder and sector in Register Pair DE.
1E3C
LD D,(IY+05H) FD 56 05
Load Register D with DCT+5 (Index Register IY+05H), the cylinder the drive's head is on, so that the seek does not move the head.
1E3F
LD E,00H 1E 00
Load Register E with 00H, sector 0, which keeps the seek on side 0.
1E41
Call @SEEK at 19D0H (on the SYS0/SYS page), driver function 6 for drive Register C: the floppy driver selects the drive and gives the controller a Seek command to cylinder Register D, so that the status register shows the type I bits, among them the index pulse in bit 1. Its return code is not looked at.
1E44
POP DE D1
Restore Register Pair DE (the FCB) from the stack.
1E45
LD B,30H 06 30
Load Register B with 30H (48), the number of status reads allowed to see the index pulse go off.
1E47BLACK
BLACK: call @RSLCT at 19D4H (on the SYS0/SYS page) to wait until the Seek command has ended and read the controller status of drive Register C into Register A; @RSLCT keeps Register Pair BC.
1E4A
BIT 1,A CB 4F
Test bit 1 of Register A, the index pulse bit of the type I status: 1 while the index hole is under the sensor, and all the time when the drive has no diskette.
1E4C
If bit 1 is clear (the sensor is dark: a diskette is turning in the drive), jump relative to 1E53H (SAWBLK) to go on with the close.
1E4E
Decrement Register B and loop back to 1E47H (BLACK) until 48 status reads have all shown the index bit set.
1E50
Jump to 20C8H (HOLDUP): the index bit never went off, so there is no diskette in the drive (or its door is open); the CLOSE FAULT message is shown.

1E53H - SAWBLK: Read the Directory Entry and Write the Last Buffer

The directory entry of the file (its DEC is FCB+7) is read into SBUFF$ at 1D00H. If the entry is no longer in use the close stops here and returns Z without writing anything. Otherwise RWRIT@ writes the last buffer of the file when it was changed in byte I/O. Bug B21: the entry is read before that write, and Register Pair HL is kept pointing into SBUFF$. When the last buffer is written in byte I/O, its granule is not among the FCB's extent records and the extent that holds or gets it is in an extended directory entry (FXDE) in another directory sector, RWRIT@ leaves that sector in SBUFF$ (unless the exclamation mark option made WEOF1 read the FPDE again): everything below then changes the bytes at the file's place in the wrong sector, writes them over the file's own directory sector (1F18H), gives back granules by the extents of whatever entry is at that place, and puts that entry's name into the FCB.

1E53SAWBLK
POP IY FD E1
SAWBLK: restore Index Register IY (the caller's) from the stack; the drive code table entry is no longer needed.
1E55
LD B,(IX+07H) DD 46 07
Load Register B with FCB+7 (Index Register IX+07H), the DEC of the file's primary directory entry (the FPDE): bits 7-5 the entry in the sector, bits 4-0 the directory sector less 2.
1E58
Call @DIRRD at 18BBH (on the SYS0/SYS page) to read the directory sector of entry Register B on drive Register C into SBUFF$ (1D00H-1DFFH): Register Pair HL = the entry (1D00H + bits 7-5 of the DEC x 32); Register A = 11H in both cases, with Z on success and NZ (error 17, Directory read error) on failure.
1E5B
RET NZ C0
Return NZ with error 17 (Directory read error) in Register A if the directory sector could not be read; the FCB stays open.
1E5C
BIT 4,(HL) CB 66
Test bit 4 of DIR+0 (Register Pair HL), the in use bit of the entry.
1E5E
RET Z C8
Return Z if the entry is not in use (the file has been removed while this FCB was open): Register A still holds 11H from @DIRRD, the last buffer is not written, the directory is not changed and the FCB stays open with no file specification put back (B22).
1E5F
PUSH HL E5
Save Register Pair HL (the entry in SBUFF$) on the stack, for the directory work after the buffer write.
1E60
PUSH BC C5
Save Register Pair BC (Register B the DEC, Register C the drive) on the stack.
1E61
Call RWRIT@ at 13A2H (on the SYS0/SYS page): when FCB+1 shows byte I/O with a changed buffer (bits 7 and 4) and the NRN is not 0, it writes the buffer to sector NRN less 1 of the file, sets FCB+0 bit 2 (written) and moves the ERN when the file grows; otherwise it returns Z with Register A = 00H at once. Its errors include 37 (no write access, or UPDATE access at the end of the file). When the sector's granule is not among the FCB's five extent records, IOREC reads the file's directory entries through SBUFF$ (CYL_GRN), allocating the granule if it is new (the GAT through DIRBUF$, the HIT through SBUFF$ for a new FXDE); with the exclamation mark option (FCB+1 bit 3) WEOF1 reads and writes the FPDE's sector too (B21).
1E64
POP BC C1
Restore Register Pair BC (the DEC in Register B, the drive in Register C) from the stack.
1E65
POP HL E1
Restore Register Pair HL from the stack: the address of the entry in SBUFF$ as it was before RWRIT@, which is the file's entry only while SBUFF$ still holds its sector (B21).
1E66
RET NZ C0
Return NZ with the error of RWRIT@ in Register A (a write error, 37, or an error of the allocation such as 27 or 30) if the last buffer could not be written; the FCB stays open, and an allocation done before the error stays on the disk.

1E67H - CLOSE: the End of File, the MOD Flag and the Date

Only an FCB opened with UPDATE access or better (FCB+0 bit 6, set by OPNCB in SYS2/SYS) has close authority; any other close goes straight to RCVN0 (200DH) and leaves the directory as it is. With close authority the open bit (DIR+1 bit 5) is reset, the EOF offset (DIR+3) and the ERN (DIR+20, DIR+21) are taken from the FCB, and a file that was written to (FCB+0 bit 2) gets the MOD flag (DIR+1 bit 6) and the date of the change: the month in DIR+1, the day and the year less 1980 modulo 8 in DIR+2, and on a diskette with the new style of dating (its bit of YFLAG$) the time and the full year in DIR+18 and DIR+19. The entry is then written back.

1E67
BIT 6,(IX+00H) DD CB 00 76
Test bit 6 of FCB+0 (Index Register IX+00H), the close authority bit: set by OPNCB when the file was opened with UPDATE access or better.
1E6B
If the FCB has no close authority, jump to 200DH (RCVN0): the directory is not changed, the file specification is only put back into the FCB.
1E6E
INC L 2C
Increment Register L: Register Pair HL points to DIR+1 of the entry in SBUFF$.
1E6F
RES 5,(HL) CB AE
Reset bit 5 of DIR+1 (Register Pair HL), the file open bit, in the copy of the entry: it is written back below whatever NFLAG$ bit 0 said at the open.
1E71
INC L 2C
Increment Register L to DIR+2, the date byte, on the way to the EOF offset.
1E72
INC L 2C
Increment Register L to DIR+3, the EOF offset of the entry.
1E73
LD A,(IX+08H) DD 7E 08
Load Register A with FCB+8 (Index Register IX+08H), the EOF offset of the open file: the byte position in the last sector at which the file ends.
1E76
PUSH HL E5
Save Register Pair HL (DIR+3) on the stack, so that CLOS1 can store the new EOF offset there.
1E77
CP (HL) BE
Compare Register A (the FCB's EOF offset) with DIR+3 (Register Pair HL).
1E78
If the EOF offset has moved, jump relative to 1E8EH (CLOS1) to copy the EOF offset and the ERN into the entry.
1E7A
LD A,11H 3E 11
Load Register A with 11H (17), the distance from DIR+3 to DIR+20.
1E7C
ADD A,L 85
Add Register L (the low byte of DIR+3) to Register A.
1E7D
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+20, the low byte of the ERN.
1E7E
LD A,(IX+0CH) DD 7E 0C
Load Register A with FCB+12 (Index Register IX+0CH), the low byte of the FCB's ending record number.
1E81
CP (HL) BE
Compare Register A with DIR+20 (Register Pair HL).
1E82
If the low byte of the ERN has moved, jump relative to 1E8EH (CLOS1).
1E84
INC L 2C
Increment Register L to DIR+21, the high byte of the ERN.
1E85
LD A,(IX+0DH) DD 7E 0D
Load Register A with FCB+13 (Index Register IX+0DH), the high byte of the FCB's ending record number.
1E88
CP (HL) BE
Compare Register A with DIR+21 (Register Pair HL).
1E89
If the high byte of the ERN has moved, jump relative to 1E8EH (CLOS1).
1E8B
POP AF F1
Discard the saved DIR+3 from the stack into Register Pair AF: the EOF offset and the ERN are unchanged.
1E8C
Jump relative to 1EA8H (CLOS2) with Register Pair HL at DIR+21.
1E8ECLOS1
POP HL E1
CLOS1: restore Register Pair HL (DIR+3) from the stack; the end of file of the FCB differs from that of the entry and replaces it below, even when the entry's is the later one (B24).
1E8F
LD A,(IX+08H) DD 7E 08
Load Register A with FCB+8 (Index Register IX+08H), the EOF offset of the open file.
1E92
LD (HL),A 77
Store Register A into DIR+3 (Register Pair HL), the EOF offset of the entry.
1E93
LD A,11H 3E 11
Load Register A with 11H (17), the distance from DIR+3 to DIR+20.
1E95
ADD A,L 85
Add Register L (the low byte of DIR+3) to Register A.
1E96
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+20.
1E97
LD A,(IX+0CH) DD 7E 0C
Load Register A with FCB+12 (Index Register IX+0CH), the low byte of the ending record number.
1E9A
LD (HL),A 77
Store Register A into DIR+20 (Register Pair HL).
1E9B
INC L 2C
Increment Register L to DIR+21, the high byte of the ending record number.
1E9C
LD A,(IX+0DH) DD 7E 0D
Load Register A with FCB+13 (Index Register IX+0DH), the high byte of the ending record number.
1E9F
LD (HL),A 77
Store Register A into DIR+21 (Register Pair HL): the entry now has the end of file of the FCB.
1EA0
BIT 2,(IX+00H) DD CB 00 56
Test bit 2 of FCB+0 (Index Register IX+00H), set at 13E1H in RWRIT@ (on the SYS0/SYS page) whenever a sector of the file has been written.
1EA4
If the file was written to, jump relative to 1EB6H (CLOS3) to set the MOD flag and the date.
1EA6
Otherwise jump relative to 1F17H (CLOS5) to write the entry back without a new date. Only WRCHAR and YESEOF move the end of file of an FCB, and each leads to a write that sets bit 2, so this path is taken when the entry was changed through another FCB of the same file: this FCB's older end of file is put back (B24).
1EA8CLOS2
BIT 2,(IX+00H) DD CB 00 56
CLOS2: test bit 2 of FCB+0 (Index Register IX+00H), the written bit, for the entry whose end of file did not move.
1EAC
If the file was written to, jump relative to 1EB6H (CLOS3) to set the MOD flag and the date.
1EAE
BIT 6,(IX+00H) DD CB 00 76
Test bit 6 of FCB+0 (Index Register IX+00H), the close authority bit, which is always set here because 1E6BH went to RCVN0 without it.
1EB2
Jump relative to 1F17H (CLOS5) to write the entry back (with its open bit reset); this jump is always taken.
1EB4
Jump relative to 1F1DH (CLOS6) without writing the entry. No path reaches this instruction: the test at 1EAEH always finds bit 6 set.
1EB6CLOS3
PUSH HL E5
CLOS3: save Register Pair HL (DIR+21) on the stack.
1EB7
LD A,L 7D
Load Register A with Register L, the low byte of DIR+21.
1EB8
AND E0H E6 E0
Mask Register A with E0H, keeping bits 7-5: the start of the 32 byte entry in SBUFF$.
1EBA
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+0.
1EBB
INC L 2C
Increment Register L to DIR+1, the flags and the month of the entry.
1EBC
SET 6,(HL) CB F6
Set bit 6 of DIR+1 (Register Pair HL), the MOD flag: the file has changed since its last backup (only BACKUP resets it).
1EBE
LD DE,0033H 11 33 00LD DE,DATE$
Point Register Pair DE to 0033H, the year byte of DATE$ (0033H year less 1900, 0034H day, 0035H month; all 00H when no date was set).
1EC1
LD A,(DE) 1A
Load Register A with the year at 0033H (Register Pair DE): 80 to 111 for 1980 to 2011, 00H for no date.
1EC2
PUSH AF F5
Save Register Pair AF (the year) on the stack for the time field below.
1EC3
PUSH BC C5
Save Register Pair BC (Register B the DEC, Register C the drive) on the stack, because Register B is used for the date.
1EC4
INC DE 13
Increment Register Pair DE to 0034H, the day of DATE$.
1EC5
INC L 2C
Increment Register L to DIR+2, where the day and the year go.
1EC6
AND 07H E6 07
Mask Register A (the year) with 07H, keeping bits 2-0: the year modulo 8, which is also the year less 1980 modulo 8 (1980 is a multiple of 8).
1EC8
LD B,A 47
Copy Register A into Register B: the three bits of the year for DIR+2.
1EC9
LD A,(DE) 1A
Load Register A with the day at 0034H (Register Pair DE), 1 to 31.
1ECA
RLCA 07
Rotate Register A left one bit: the day starts to move into bits 7-3.
1ECB
RLCA 07
Rotate Register A left a second time, toward bits 7-3.
1ECC
RLCA 07
Rotate Register A left a third time: the day is in bits 7-3, bits 2-0 are 0.
1ECD
OR B B0
OR Register B (the year bits) into Register A: bits 7-3 the day, bits 2-0 the year less 1980 modulo 8.
1ECE
LD (HL),A 77
Store Register A into DIR+2 (Register Pair HL), the day and the year of the change.
1ECF
DEC L 2D
Decrement Register L back to DIR+1, where the month goes.
1ED0
INC DE 13
Increment Register Pair DE to 0035H, the month of DATE$.
1ED1
LD A,(DE) 1A
Load Register A with the month at 0035H (Register Pair DE), 1 to 12.
1ED2
LD B,A 47
Copy Register A into Register B for the merge.
1ED3
LD A,(HL) 7E
Load Register A with DIR+1 (Register Pair HL): bits 7-4 the flags (7 created, 6 MOD, 5 open, 4 date not kept), bits 3-0 the month of the last change.
1ED4
AND F0H E6 F0
Mask Register A with F0H, keeping the four flag bits and clearing the old month; bit 4 is kept as it was.
1ED6
OR B B0
OR Register B (the new month) into Register A.
1ED7
LD (HL),A 77
Store Register A into DIR+1 (Register Pair HL): the flags and the month of this change (0 when no date was set, which the TRM reads as date not set).
1ED8
LD A,C 79
Load Register A with Register C, the drive number.
1ED9
AND 07H E6 07
Mask Register A with 07H, keeping the drive 0 to 7.
1EDB
LD B,A 47
Copy Register A into Register B, the loop count.
1EDC
INC B 04
Increment Register B so that the loop below runs drive + 1 times.
1EDD
LD A,3FH 3E 3F
Load Register A with 3FH: with 08H added drive + 1 times it becomes 47H + 8 x drive, the second byte of BIT n,A with n = the drive.
1EDFCLOS3A
ADD A,08H C6 08
CLOS3A: add 08H to Register A, the next bit number in the BIT opcode.
1EE1
Decrement Register B and loop back to 1EDFH (CLOS3A) until Register A = 47H + 8 x drive.
1EE3
LD (1EEAH),A 32 EA 1ELD (CLOS3B),A
Store Register A into 1EEAH, the second byte of the BIT instruction at 1EE9H, which then tests the bit of YFLAG$ that belongs to the file's drive.
1EE6
LD A,(0082H) 3A 82 00LD A,(YFLAG$)
Load Register A with YFLAG$ (0082H): bit n set when the diskette in drive n uses the new style of directory dating (GAT+CDH bit 3), as @CKDRV found it.
1EE9
BIT 0,A CB 47
Test bit n of Register A, n being the drive stored into 1EEAH at 1EE3H (BIT 0,A in the file).
1EEB
If the diskette is dated in the new style, jump relative to 1EF1H (CLOS3C) to store the time and the full year.
1EED
POP BC C1
Old style: restore Register Pair BC (the DEC and the drive) from the stack.
1EEE
POP AF F1
Discard the saved year into Register Pair AF: an old style diskette keeps the user password hash in DIR+18 and DIR+19.
1EEF
Jump relative to 1F16H (CLOS4) to write the entry back.
1EF1CLOS3C
LD A,L 7D
CLOS3C: load Register A with Register L, the low byte of DIR+1.
1EF2
ADD A,11H C6 11
Add 11H (17) to Register A: DIR+1 + 17 = DIR+18, the time field of the new style of dating.
1EF4
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+18.
1EF5
LD DE,(002EH) ED 5B 2E 00
Load Register Pair DE with the word at 002EH: Register E = the minutes (002EH), Register D = the hours (002FH) of TIME$ (002DH seconds, 002EH minutes, 002FH hours).
1EF9
LD A,D 7A
Load Register A with Register D, the hours, 0 to 23.
1EFA
LD D,E 53
Copy Register E (the minutes, 0 to 59) into Register D.
1EFB
LD E,00H 1E 00
Load Register E with 00H: Register Pair DE = the minutes x 256.
1EFD
LD B,03H 06 03
Load Register B with 03H, the number of bits to shift.
1EFFCLOSD1
SRL D CB 3A
CLOSD1: shift Register D right one bit, bit 0 into the Carry flag.
1F01
RR E CB 1B
Rotate Register E right through the Carry flag: the bit from Register D goes into bit 7 of Register E.
1F03
Decrement Register B and loop back to 1EFFH (CLOSD1) three times: Register D = the minutes divided by 8 (bits 2-0), Register E = the minutes modulo 8 in bits 7-5.
1F05
RLCA 07
Rotate Register A (the hours) left one bit, toward bits 7-3.
1F06
RLCA 07
Rotate Register A left a second time, toward bits 7-3.
1F07
RLCA 07
Rotate Register A left a third time: the hours are in bits 7-3.
1F08
OR D B2
OR Register D (the minutes divided by 8) into Register A.
1F09
LD D,A 57
Copy Register A into Register D: bits 7-3 the hours, bits 2-0 the high three bits of the minutes, the byte for DIR+18.
1F0A
POP BC C1
Restore Register Pair BC (the DEC and the drive) from the stack.
1F0B
POP AF F1
Restore Register Pair AF from the stack: Register A = the year from 0033H.
1F0C
OR A B7
OR Register A with itself to test for 00H, no date set.
1F0D
If no date was set, jump relative to 1F13H (CLOSD2) with year bits 0 in Register E.
1F0F
SUB 50H D6 50
Subtract 50H (80) from Register A: the year less 1980, 0 to 31 for 1980 to 2011.
1F11
OR E B3
OR Register E (the low three bits of the minutes in bits 7-5) into Register A: bits 4-0 the year less 1980.
1F12
LD E,A 5F
Copy Register A into Register E, the byte for DIR+19.
1F13CLOSD2
LD (HL),D 72
CLOSD2: store Register D (the hours and the high bits of the minutes) into DIR+18 (Register Pair HL).
1F14
INC HL 23
Increment Register Pair HL to DIR+19, the second byte of the time field.
1F15
LD (HL),E 73
Store Register E (the low bits of the minutes and the year less 1980) into DIR+19 (Register Pair HL).
1F16CLOS4
POP HL E1
CLOS4: restore Register Pair HL (DIR+21) from the stack.
1F17CLOS5
PUSH HL E5
CLOS5: save Register Pair HL (DIR+21) on the stack, because @DIRWR changes it.
1F18
Call @DIRWR at 1803H (on the SYS0/SYS page) to write SBUFF$ back to the directory sector of DEC Register B on drive Register C, with a verify and a second try: Z on success, NZ with error 18 (Directory write error) or 15 (Write protected disk).
1F1B
POP HL E1
Restore Register Pair HL (DIR+21) from the stack.
1F1C
RET NZ C0
Return NZ with the error in Register A if the entry could not be written; the FCB stays open.

1F1DH - CLOS6: Count the Granules the Directory Gives the File

Unless the file was set up with the CREATE command (DIR+1 bit 7), the close gives back the granules that lie past its end. CLOS6 adds up the granules of every extent of the entry, following the link (FEH and a DEC in the fifth field) into each extended directory entry (FXDE). The number of granules the file needs is its ERN rounded up to whole granules. When the directory gives the file more than that, the GAT is read and BAKUP (1FB7H) starts taking granules off at the end.

1F1DCLOS6
INC L 2C
CLOS6: increment Register L from DIR+21 to DIR+22, the cylinder byte of the first extent field.
1F1E
PUSH HL E5
Save Register Pair HL (DIR+22) on the stack, the first extent field, for the count below.
1F1F
LD A,L 7D
Load Register A with Register L, the low byte of DIR+22.
1F20
SUB 15H D6 15
Subtract 15H (21) from Register A: DIR+22 less 21 is DIR+1.
1F22
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+1.
1F23
BIT 7,(HL) CB 7E
Test bit 7 of DIR+1 (Register Pair HL), the CREATED bit set by the CREATE library command.
1F25
POP HL E1
Restore Register Pair HL (DIR+22) from the stack; the POP keeps the flags.
1F26
If the file was CREATEd, jump to 200DH (RCVN0): its space is kept even past the end of file.
1F29
LD DE,0000H 11 00 00
Load Register Pair DE with 0000H, the count of granules the extents give the file.
1F2CCLOS7
LD A,(HL) 7E
CLOS7: load Register A with the cylinder byte of the extent field at Register Pair HL: FFH for an unused field, FEH for the link to an FXDE, else the cylinder where the extent starts.
1F2D
INC L 2C
Increment Register L to the second byte of the field (the allocation byte, or the DEC of the FXDE).
1F2E
CP FEH FE FE
Compare Register A with FEH, the link mark; FFH marks an unused field.
1F30
If Register A is FEH or FFH, jump relative to 1F3EH (CLOS8): the last extent of this entry has been counted.
1F32
LD A,(HL) 7E
Load Register A with the allocation byte of the extent (Register Pair HL): bits 7-5 the starting granule in the cylinder, bits 4-0 the number of granules less 1.
1F33
INC L 2C
Increment Register L to the next extent field.
1F34
AND 1FH E6 1F
Mask Register A with 1FH, keeping the number of granules less 1.
1F36
INC A 3C
Increment Register A: the number of granules in the extent, 1 to 32.
1F37
ADD A,E 83
Add Register E (the count so far, low byte) to Register A.
1F38
LD E,A 5F
Copy Register A into Register E, the low byte of the count of granules.
1F39
If there is no carry, jump relative to 1F2CH (CLOS7) to the next extent field.
1F3B
INC D 14
Increment Register D, the high byte of the count, for the carry out of Register E.
1F3C
Jump relative to 1F2CH (CLOS7) to the next extent field.
1F3ECLOS8
CLOS8: if the field was FFH (the NZ of the CP with FEH), jump relative to 1F4BH (CLOS9): Register Pair DE holds every granule of the file.
1F40
LD B,(HL) 46
The field is FEH, a link: load Register B with its second byte at Register Pair HL, the DEC of the FXDE that holds the next extents.
1F41
Call @DIRRD at 18BBH (on the SYS0/SYS page) to read the directory sector of the FXDE (DEC Register B, drive Register C) into SBUFF$: Register Pair HL = the FXDE.
1F44
RET NZ C0
Return NZ with error 17 (Directory read error) in Register A if the FXDE cannot be read; the entry has been written, the FCB stays open.
1F45
LD A,L 7D
Load Register A with Register L, the low byte of the FXDE's address in SBUFF$.
1F46
ADD A,16H C6 16
Add 16H (22) to Register A: the FXDE's first extent field, at the same place as in a primary entry.
1F48
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to the FXDE+22.
1F49
Jump relative to 1F2CH (CLOS7) to count the FXDE's extents.
1F4BCLOS9
PUSH HL E5
CLOS9: save Register Pair HL (the allocation byte of the FFH field after the last extent) on the stack.
1F4C
LD L,(IX+0CH) DD 6E 0C
Load Register L with FCB+12 (Index Register IX+0CH), the low byte of the ending record number.
1F4F
LD H,(IX+0DH) DD 66 0D
Load Register H with FCB+13 (Index Register IX+0DH): Register Pair HL = the ERN, the number of sectors the file uses.
1F52
LD A,08H 3E 08
Load Register A with 08H, the field DCT+8 (granules per track less 1 in bits 7-5, sectors per granule less 1 in bits 4-0).
1F54
Call @DCTBYT at 1A2BH (on the SYS0/SYS page) to load Register A with DCT+8 of drive Register C; Register Pair HL is kept.
1F57
AND 1FH E6 1F
Mask Register A with 1FH: the number of sectors per granule less 1 (05H on the release diskette).
1F59
PUSH AF F5
Save Register Pair AF (the sectors per granule less 1) on the stack.
1F5A
ADD A,L 85
Add Register L (the low byte of the ERN) to Register A, to round the ERN up to the next whole granule.
1F5B
LD L,A 6F
Copy Register A into Register L, the low byte of the rounded ERN.
1F5C
If there is no carry, jump relative to 1F5FH (CLOS10).
1F5E
INC H 24
Increment Register H for the carry: Register Pair HL = the ERN plus the sectors per granule less 1.
1F5FCLOS10
POP AF F1
CLOS10: restore Register Pair AF: Register A = the sectors per granule less 1.
1F60
INC A 3C
Increment Register A: the number of sectors per granule, the divisor.
1F61
Call @DIV16 at 06E3H (on the BOOT/SYS page) to divide Register Pair HL by Register A: Register Pair HL = the number of granules the file needs, Register A = the remainder.
1F64
XOR A AF
Clear Register A and the Carry flag for the subtraction.
1F65
EX DE,HL EB
Exchange Register Pair DE with Register Pair HL: Register Pair HL = the granules the directory gives, Register Pair DE = the granules needed.
1F66
SBC HL,DE ED 52
Subtract Register Pair DE (needed) from Register Pair HL (given) with the borrow (0): Register Pair HL = the granules too many; Z when the same, Carry when the file needs more than it has.
1F68
EX DE,HL EB
Exchange Register Pair DE with Register Pair HL: Register Pair DE = the excess granules; the flags are kept.
1F69
POP HL E1
Restore Register Pair HL from the stack: the allocation byte of the FFH field after the last extent; the POP keeps the flags.
1F6A
If no granule is too many, jump to 200DH (RCVN0).
1F6D
If the file needs more granules than its extents give (a broken entry), jump to 200DH (RCVN0) and leave it.
1F70
Call @GATRD at 1874H (on the SYS0/SYS page) to read the GAT of drive Register C into 2300H-23FFH (DIRBUF$): Z on success (Register A = 14H), NZ with error 20 (GAT read error).
1F73
RET NZ C0
Return NZ with error 20 (GAT read error) in Register A if the GAT cannot be read; the entry has been written, the FCB stays open.
1F74
Jump relative to 1FB7H (BAKUP) to mark the FFH field and step back to the last extent.

1F76H - CLOS11: Give One Granule Back

CLOS11 finds the last granule of the extent whose allocation byte is at Register Pair HL: the starting granule plus the count less 1, turned into cylinders and a granule by the number of granules per cylinder (doubled on a two sided diskette). The granule's bit in the GAT byte of that cylinder is reset (CALCBIT, 20BBH) and the extent is made one granule shorter. An extent left empty is marked unused (FFH FFH) and the walk steps back one field; when it steps back past the first field of an FXDE, that FXDE is given up too (BAKUP).

1F76CLOS11
PUSH DE D5
CLOS11: save Register Pair DE (the number of granules still to give back) on the stack.
1F77
LD A,(HL) 7E
Load Register A with the allocation byte of the extent at Register Pair HL.
1F78
AND E0H E6 E0
Mask Register A with E0H, keeping bits 7-5, the starting granule in the cylinder.
1F7A
RLCA 07
Rotate Register A left one bit: bits 7-5 start to move into bits 2-0.
1F7B
RLCA 07
Rotate Register A left a second time, moving the starting granule on toward bits 2-0.
1F7C
RLCA 07
Rotate Register A left a third time: the starting granule is in bits 2-0.
1F7D
LD E,A 5F
Copy Register A into Register E, the starting granule.
1F7E
LD A,(HL) 7E
Load Register A with the allocation byte of the extent at Register Pair HL again.
1F7F
AND 1FH E6 1F
Mask Register A with 1FH, keeping the number of granules less 1.
1F81
ADD A,E 83
Add Register E (the starting granule) to Register A.
1F82
LD E,A 5F
Copy Register A into Register E: the last granule of the extent, counted from the start of its first cylinder.
1F83
LD A,08H 3E 08
Load Register A with 08H, the field DCT+8 (granules per track less 1 in bits 7-5).
1F85
Call @DCTBYT at 1A2BH (on the SYS0/SYS page) to load Register A with DCT+8 of drive Register C.
1F88
RLCA 07
Rotate Register A left one bit: bits 7-5 (granules per track less 1) start to move into bits 2-0.
1F89
RLCA 07
Rotate Register A left a second time, moving the granules per track on toward bits 2-0.
1F8A
RLCA 07
Rotate Register A left a third time: the granules per track less 1 are in bits 2-0.
1F8B
AND 07H E6 07
Mask Register A with 07H, keeping the granules per track less 1 (02H on the release diskette).
1F8D
INC A 3C
Increment Register A: the number of granules per track.
1F8E
LD D,A 57
Copy Register A into Register D to keep it while DCT+4 is read.
1F8F
LD A,04H 3E 04
Load Register A with 04H, the field DCT+4, whose bit 5 says whether the diskette is two sided.
1F91
Call @DCTBYT at 1A2BH (on the SYS0/SYS page) to load Register A with DCT+4 of drive Register C.
1F94
BIT 5,A CB 6F
Test bit 5 of Register A, set when the diskette is two sided.
1F96
LD A,D 7A
Load Register A with Register D, the granules per track; the LD keeps the flags.
1F97
If the diskette is one sided, jump relative to 1F9AH with the granules per cylinder = the granules per track.
1F99
RLCA 07
Rotate Register A left one bit: twice the granules per track, the granules per cylinder of a two sided diskette.
1F9A
Call @DIV8 at 1927H (on the SYS0/SYS page) to divide Register E (the last granule) by Register A (granules per cylinder): Register A = the cylinders past the extent's first one, Register E = the granule in that cylinder.
1F9D
DEC L 2D
Decrement Register L to the cylinder byte of the extent field.
1F9E
ADD A,(HL) 86
Add the starting cylinder (Register Pair HL) to Register A: the cylinder that holds the extent's last granule.
1F9F
LD D,A 57
Copy Register A into Register D, the cylinder.
1FA0
PUSH HL E5
Save Register Pair HL (the cylinder byte of the extent) on the stack.
1FA1
PUSH BC C5
Save Register Pair BC (Register B the DEC of the entry in SBUFF$, Register C the drive) on the stack.
1FA2
LD H,23H 26 23
Load Register H with 23H, the page of the GAT at 2300H.
1FA4
LD L,D 6A
Copy Register D (the cylinder) into Register L: Register Pair HL points to the GAT byte of the cylinder, one bit for each of its granules.
1FA5
LD B,(HL) 46
Load Register B with the GAT byte (Register Pair HL).
1FA6
LD A,E 7B
Load Register A with Register E, the granule in the cylinder.
1FA7
Call CALCBIT at 20BBH to reset the bit of granule Register A in Register B.
1FAA
LD (HL),B 70
Store Register B into the GAT byte (Register Pair HL): the granule is free again.
1FAB
POP BC C1
Restore Register Pair BC (the DEC and the drive) from the stack.
1FAC
POP HL E1
Restore Register Pair HL (the cylinder byte of the extent) from the stack.
1FAD
INC L 2C
Increment Register L to the allocation byte of the extent.
1FAE
DEC (HL) 35
Decrement the allocation byte (Register Pair HL): one granule fewer. When the count was 0 (one granule) the borrow runs into the starting granule bits and the count bits become 1FH.
1FAF
LD A,(HL) 7E
Load Register A with the new allocation byte (Register Pair HL).
1FB0
INC A 3C
Increment Register A: the count bits become the number of granules left, or 20H from 1FH.
1FB1
AND 1FH E6 1F
Mask Register A with 1FH: 00H when the extent has no granule left (the count was 0 before the DEC).
1FB3
POP DE D1
Restore Register Pair DE (the granules still to give back) from the stack.
1FB4
DEC DE 1B
Decrement Register Pair DE for the granule just given back; the 16 bit DEC keeps the flags of the AND.
1FB5
If the extent still has granules, jump relative to 1FFAH (CLOS12) to see whether more are to be given back.
1FB7BAKUP
LD (HL),FFH 36 FF
BAKUP: store FFH into the allocation byte of the field at Register Pair HL: the extent is empty (or, from 1F74H, the field after the last extent is marked FFH again).
1FB9
DEC L 2D
Decrement Register L to the cylinder byte of the field.
1FBA
LD (HL),FFH 36 FF
Store FFH into the cylinder byte (Register Pair HL): the field is unused.
1FBC
DEC L 2D
Decrement Register L to the allocation byte of the field before it.
1FBD
LD A,L 7D
Load Register A with Register L, the position of Register Pair HL in SBUFF$.
1FBE
AND 1FH E6 1F
Mask Register A with 1FH, keeping the offset in the 32 byte entry.
1FC0
CP 15H FE 15
Compare Register A with 15H: DIR+21, one byte before the first extent field, means every extent of this entry is now unused.
1FC2
If the walk is still inside the extent fields, jump relative to 1FFAH (CLOS12).
1FC4
XOR L AD
Exclusive OR Register L into Register A (15H): bits 4-0 of the result are 0, so Register A = the start of the entry.
1FC5
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+0 of the entry.
1FC6
BIT 7,(HL) CB 7E
Test bit 7 of DIR+0 (Register Pair HL), set in an FXDE.
1FC8
If the entry is the primary entry (FPDE), jump relative to 1FFAH (CLOS12): it is kept even with no extent.
1FCA
LD (HL),00H 36 00
Store 00H into DIR+0 (Register Pair HL) of the FXDE: the entry is no longer in use.
1FCC
Call @DIRWR at 1803H (on the SYS0/SYS page) to write the FXDE's sector (DEC Register B, drive Register C) from SBUFF$.
1FCF
RET NZ C0
Return NZ with the error (18 or 15) in Register A if the sector cannot be written; the GAT is not written.
1FD0
LD A,B 78
Load Register A with Register B, the DEC of the FXDE given up.
1FD1
AND E0H E6 E0
Mask Register A with E0H, the position of the entry in its sector.
1FD3
INC A 3C
Increment Register A: the position of DIR+1, which in an FXDE holds the DEC of the entry it extends.
1FD4
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+1 of the FXDE in SBUFF$ (Register H is still 1DH).
1FD5
LD A,(HL) 7E
Load Register A with DIR+1 (Register Pair HL), the DEC of the entry before it in the chain (the FPDE or an earlier FXDE).
1FD6
LD (1FE5H),A 32 E5 1F
Store Register A into 1FE5H, the operand of LD B,nn at STUFDEC (1FE4H).
1FD9
Call @HITRD at 1897H (on the SYS0/SYS page) to read the HIT of drive Register C into 1D00H-1DFFH (SBUFF$): Register Pair HL = 1D00H.
1FDC
RET NZ C0
Return NZ with error 22 (HIT read error) in Register A if the HIT cannot be read.
1FDD
LD L,B 68
Copy Register B (the DEC of the FXDE) into Register L: Register Pair HL points to its HIT slot, the slot number being the DEC.
1FDE
LD (HL),00H 36 00
Store 00H into the HIT slot (Register Pair HL): the slot is free.
1FE0
Call @HITWR at 1898H (on the SYS0/SYS page) to write the HIT of drive Register C back from 1D00H.
1FE3
RET NZ C0
Return NZ with error 23 (HIT write error) in Register A if the HIT cannot be written.
1FE4STUFDEC
LD B,00H 06 00
STUFDEC: load Register B with the DEC of the entry before the FXDE, stored into 1FE5H at 1FD6H (00H in the file).
1FE6
Call @DIRRD at 18BBH (on the SYS0/SYS page) to read that entry's sector into SBUFF$: Register Pair HL = the entry.
1FE9
RET NZ C0
Return NZ with error 17 (Directory read error) in Register A if it cannot be read.
1FEA
LD A,B 78
Load Register A with Register B, the DEC of that entry.
1FEB
OR 1FH F6 1F
OR Register A with 1FH: bits 7-5 the entry and bits 4-0 all set, the position of DIR+31.
1FED
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+31, the DEC of the FXDE just given up.
1FEE
LD (HL),FFH 36 FF
Store FFH into DIR+31 (Register Pair HL): the entry no longer links to it.
1FF0
DEC L 2D
Decrement Register L to DIR+30, the link byte FEH.
1FF1
LD (HL),FFH 36 FF
Store FFH into DIR+30 (Register Pair HL): the entry has no FXDE any more.
1FF3
DEC L 2D
Decrement Register L to DIR+29, the allocation byte of the entry's fourth extent, where the walk goes on.
1FF4
PUSH HL E5
Save Register Pair HL (DIR+29) on the stack, because @DIRWR changes it.
1FF5
Call @DIRWR at 1803H (on the SYS0/SYS page) to write the entry's sector (DEC Register B, drive Register C) back.
1FF8
POP HL E1
Restore Register Pair HL (DIR+29) from the stack.
1FF9
RET NZ C0
Return NZ with the error (18 or 15) in Register A if the sector cannot be written.
1FFACLOS12
LD A,D 7A
CLOS12: load Register A with Register D, the high byte of the granules still to give back.
1FFB
OR E B3
OR Register E into Register A to test Register Pair DE for zero.
1FFC
If granules are still to be given back, jump to 1F76H (CLOS11) for the next one, at the extent at Register Pair HL.
1FFF
Call @DIRWR at 1803H (on the SYS0/SYS page) to write the sector of the last entry changed (DEC Register B, drive Register C) from SBUFF$.
2002
If the write succeeded, jump relative to 2009H (CLOS13) to write the GAT.
2004
CP 0FH FE 0F
Compare Register A (the error) with 0FH, error 15 (Write protected disk).
2006
RET NZ C0
Return NZ with error 18 (Directory write error) in Register A for any other write failure; the GAT is not written.
2007
For a write protected diskette jump relative to 200DH (RCVN0) and return success without writing the GAT. The entry was already written at 1F18H, so a write protected diskette cannot get here unless the tab was put on during the close.
2009CLOS13
CLOS13: call @GATWR at 1875H (on the SYS0/SYS page) to write the GAT of drive Register C back from 2300H, with a verify: Z on success, NZ with error 21 (GAT write error).
200C
RET NZ C0
Return NZ with error 21 (GAT write error) in Register A if the GAT cannot be written: the entries no longer show the granules, but the GAT on the disk still has them allocated.

200DH - RCVN0 and RCVNAM: Put the File Specification Back

Every close of a file ends here. The directory sector of the file's FPDE is read again unless SBUFF$ already holds it (the last DEC in Register B is in the same sector), and RCVNAM writes the name, the extension and the drive into the FCB as text: NAME/EXT:d and 03H, at most 15 bytes. The first byte becomes a character with bit 7 clear, so the FCB is closed, and the specification can be given to @OPEN again; the password is not put back. @FNAME (2064H) calls RCVNAM at 2020H for a buffer of its caller.

200DRCVN0
LD A,(IX+07H) DD 7E 07
RCVN0: load Register A with FCB+7 (Index Register IX+07H), the DEC of the file's FPDE.
2010
LD C,(IX+06H) DD 4E 06
Load Register C with FCB+6 (Index Register IX+06H), the file's drive.
2013
XOR B A8
Exclusive OR Register B (the DEC of the entry whose sector was last read or written, or the FPDE's own DEC) into Register A.
2014
AND 1FH E6 1F
Mask Register A with 1FH, keeping the directory sector bits of the two DECs: Z when both entries lie in the same sector, which is taken to mean that SBUFF$ holds the FPDE's sector already (not true after B21, where Register B is the FPDE's DEC but SBUFF$ holds another sector).
2016
LD B,(IX+07H) DD 46 07
Load Register B with FCB+7 (Index Register IX+07H), the DEC of the FPDE, for the read and for RCVNAM; the LD keeps the flags.
2019
If SBUFF$ holds another directory sector, call @DIRRD at 18BBH (on the SYS0/SYS page) to read the FPDE's sector (DEC Register B, drive Register C) into SBUFF$.
201C
RET NZ C0
Return NZ with error 17 (Directory read error) in Register A if the directory sector cannot be read; the FCB stays open.
201D
PUSH IX DD E5
Save Index Register IX (the FCB) on the stack.
201F
POP DE D1
Restore it into Register Pair DE: Register Pair DE = the FCB, where the specification text goes.
2020RCVNAM
LD A,C 79
RCVNAM: load Register A with Register C, the drive (entry of @FNAME with Register Pair DE at its caller's buffer, Register B the DEC, Register C the drive).
2021
AND 07H E6 07
Mask Register A with 07H, keeping the drive number 0 to 7.
2023
OR 30H F6 30
OR Register A with 30H: the drive number as an ASCII digit, 0 to 7.
2025
LD (205CH),A 32 5C 20
Store Register A into 205CH, the operand of LD A,nn at RCVN5 (205BH), for the drive digit of the text.
2028
LD H,1DH 26 1D
Load Register H with 1DH, the page of SBUFF$ (1D00H-1DFFH).
202A
LD A,B 78
Load Register A with Register B, the DEC of the entry.
202B
AND E0H E6 E0
Mask Register A with E0H, keeping bits 7-5: the start of the entry in its sector.
202D
OR 05H F6 05
OR Register A with 05H: the position of DIR+5, the first character of the name.
202F
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+5 of the entry in SBUFF$.
2030
PUSH HL E5
Save Register Pair HL (DIR+5) on the stack, to find the extension from it after the name.
2031
LD B,08H 06 08
Load Register B with 08H, the length of the name field.
2033RCVN1
LD A,(HL) 7E
RCVN1: load Register A with the next character of the name (Register Pair HL).
2034
CP 20H FE 20
Compare Register A with 20H, the space that pads the name field on the right.
2036
If the character is a space, jump relative to 203DH (RCVN2): the name has ended.
2038
LD (DE),A 12
Store the character in Register A into the text at Register Pair DE.
2039
INC HL 23
Increment Register Pair HL to the next character of the name field.
203A
INC DE 13
Increment Register Pair DE to the next byte of the text.
203B
Decrement Register B and loop back to 2033H (RCVN1) for at most 8 characters.
203DRCVN2
POP HL E1
RCVN2: restore Register Pair HL (DIR+5) from the stack.
203E
LD A,L 7D
Load Register A with Register L, the low byte of DIR+5.
203F
ADD A,08H C6 08
Add 08H to Register A: DIR+13, the first character of the extension field.
2041
LD L,A 6F
Copy Register A into Register L: Register Pair HL points to DIR+13.
2042
LD A,(HL) 7E
Load Register A with the first character of the extension (Register Pair HL).
2043
CP 20H FE 20
Compare Register A with 20H, a space: a blank extension field.
2045
If the extension is blank, jump relative to 2057H (RCVN4): the text gets no slash.
2047
LD A,2FH 3E 2F
Load Register A with 2FH, the slash that comes before an extension.
2049
LD (DE),A 12
Store the slash (Register A) into the text at Register Pair DE.
204A
INC DE 13
Increment Register Pair DE to the next byte of the text.
204B
LD B,03H 06 03
Load Register B with 03H, the length of the extension field.
204DRCVN3
LD A,(HL) 7E
RCVN3: load Register A with the next character of the extension (Register Pair HL).
204E
CP 20H FE 20
Compare Register A with 20H, a space, which pads the extension field on the right.
2050
If the character is a space, jump relative to 2057H (RCVN4): the extension has ended.
2052
LD (DE),A 12
Store the character in Register A into the text at Register Pair DE.
2053
INC HL 23
Increment Register Pair HL to the next character of the extension field.
2054
INC DE 13
Increment Register Pair DE to the next byte of the text.
2055
Decrement Register B and loop back to 204DH (RCVN3) for at most 3 characters.
2057RCVN4
LD A,3AH 3E 3A
RCVN4: load Register A with 3AH, the colon that comes before the drive.
2059
LD (DE),A 12
Store the colon (Register A) into the text at Register Pair DE.
205A
INC DE 13
Increment Register Pair DE to the next byte of the text.
205BRCVN5
LD A,00H 3E 00
RCVN5: load Register A with the drive digit stored into 205CH at 2025H (00H in the file).
205D
LD (DE),A 12
Store the drive digit (Register A) into the text at Register Pair DE.
205E
INC DE 13
Increment Register Pair DE to the next byte of the text.
205F
LD A,03H 3E 03
Load Register A with 03H (ETX), which ends a file specification for @FSPEC and @OPEN.
2061
LD (DE),A 12
Store 03H (Register A) into the text at Register Pair DE: the text is complete. The TRM says @FNAME ends it with 0DH; the code writes 03H.
2062
XOR A AF
Clear Register A to 00H and set the Z flag: success.
2063
RET C9
Return Z with Register A = 00H: for @CLOSE through RESTREG, which gives the caller Register Pairs HL, DE, BC and Index Register IX back; for @FNAME to FNAM2 (209AH).

2064H - FNAME: @FNAME, the File Specification of a Directory Entry

@FNAME (SVC 80, request A5H) has Register B = a DEC, Register C = a drive and Register Pair DE at a buffer of 15 bytes. It works out the number of directory sectors of the drive from its drive code table (sectors per cylinder less the GAT and the HIT), refuses a DEC whose sector bits lie past them with error 16, reads the entry's sector and lets RCVNAM write NAME/EXT:d and 03H into the buffer. Register Pairs DE and HL come back as they were (the TRM says Register Pair HL points to the entry), Register B is changed. The entry is not checked: a free entry or an FXDE gives whatever its name field holds.

2064FNAME
PUSH HL E5
FNAME: save Register Pair HL (the caller's) on the stack.
2065
PUSH DE D5
Save Register Pair DE (the caller's buffer) on the stack.
2066
LD A,07H 3E 07
Load Register A with 07H, the field DCT+7 (bits 7-5 the heads less 1, bits 4-0 the highest sector number on a track).
2068
Call @DCTBYT at 1A2BH (on the SYS0/SYS page) to load Register A with DCT+7 of drive Register C (11H for the floppy drives of the release).
206B
LD D,A 57
Copy Register A into Register D, to keep both parts.
206C
AND 1FH E6 1F
Mask Register A with 1FH, keeping the highest sector number.
206E
LD E,A 5F
Copy Register A into Register E, the highest sector number on a track.
206F
INC E 1C
Increment Register E: the number of sectors on a track (18 on the release diskette).
2070
XOR D AA
Exclusive OR Register D into Register A: the sector bits cancel out, Register A = bits 7-5 of DCT+7, the heads less 1.
2071
RLCA 07
Rotate Register A left one bit: bits 7-5 start to move into bits 2-0.
2072
RLCA 07
Rotate Register A left a second time, moving the heads less 1 on toward bits 2-0.
2073
RLCA 07
Rotate Register A left a third time: the heads less 1 are in bits 2-0.
2074
INC A 3C
Increment Register A: the number of heads (1 for a floppy drive).
2075
Call @MUL8 at 190AH (on the SYS0/SYS page) to multiply Register A by Register E: Register A = the sectors per track times the heads, the sectors on a cylinder of one side; Registers D and E are changed.
2078
LD E,A 5F
Copy Register A into Register E, the sectors per cylinder of one side.
2079
LD A,04H 3E 04
Load Register A with 04H, the field DCT+4, whose bit 5 says whether the diskette is two sided.
207B
Call @DCTBYT at 1A2BH (on the SYS0/SYS page) to load Register A with DCT+4 of drive Register C.
207E
BIT 5,A CB 6F
Test bit 5 of Register A, set when the diskette is two sided.
2080
LD A,E 7B
Load Register A with Register E, the sectors per cylinder of one side; the LD keeps the flags.
2081
If the diskette is one sided, jump relative to 2084H (ONESID).
2083
ADD A,A 87
Add Register A to itself: twice the sectors, for the two sides of the cylinder.
2084ONESID
SUB 02H D6 02
ONESID: subtract 02H from Register A for the GAT and the HIT (sectors 0 and 1 of the directory cylinder): the number of directory entry sectors (16 on the release diskette).
2086
LD D,A 57
Copy Register A into Register D, the number of directory sectors.
2087
LD A,B 78
Load Register A with Register B, the DEC asked for.
2088
AND 1FH E6 1F
Mask Register A with 1FH, keeping bits 4-0, the directory sector less 2.
208A
CP D BA
Compare Register A with Register D, the number of directory sectors.
208B
If the sector exists, jump relative to 2092H (FNAM1).
208D
LD A,10H 3E 10
Load Register A with 10H, error 16 (Illegal logical file number).
208F
OR A B7
OR Register A with itself to set the NZ flag for the error.
2090
Jump relative to 209AH (FNAM2) to return the error.
2092FNAM1
POP DE D1
FNAM1: restore Register Pair DE (the caller's buffer) from the stack.
2093
PUSH DE D5
Save Register Pair DE on the stack again for FNAM2.
2094
Call @DIRRD at 18BBH (on the SYS0/SYS page) to read the directory sector of DEC Register B on drive Register C into SBUFF$: Z on success, NZ with error 17 (Directory read error); Register A = 11H either way.
2097
If the sector was read, call RCVNAM at 2020H to write the name, the extension and the drive into the buffer at Register Pair DE; it returns Z with Register A = 00H.
209AFNAM2
POP DE D1
FNAM2: restore Register Pair DE (the caller's buffer) from the stack.
209B
POP HL E1
Restore Register Pair HL (the caller's) from the stack.
209C
RET C9
Return: Z with Register A = 00H and the specification in the buffer, or NZ with error 16 or 17 in Register A.

209DH - CLOSDEV: Close a Device

An FCB that is not an open file is closed here only if it is a device FCB, 10H (routed) as DEVOPEN in SYS2/SYS builds it; anything else returns error 38 (File not open). The device itself is not told anything: the FCB just gets its specification back, an asterisk and the two letters DEVOPEN kept at FCB+6 and FCB+7, ended by 03H.

209DCLOSDEV
CP 10H FE 10
CLOSDEV: compare Register A (FCB+0, loaded at 1E12H) with 10H, the first byte of an FCB routed to a device.
209F
LD A,26H 3E 26
Load Register A with 26H, error 38 (File not open); the LD keeps the flags of the CP.
20A1
RET NZ C0
Return NZ with error 38 if the FCB is neither an open file nor an open device; no register but Register Pair AF is changed.
20A2
Call LNKFCB@ at 1566H (on the SYS0/SYS page), which skips the open test: it saves Register Pairs HL, DE, BC and Index Register IX, points Index Register IX at the FCB and plants RESTREG for the return.
20A5
LD C,(IX+06H) DD 4E 06
Load Register C with FCB+6 (Index Register IX+06H), the first letter of the device name kept there by DEVOPEN.
20A8
LD B,(IX+07H) DD 46 07
Load Register B with FCB+7 (Index Register IX+07H), the second letter of the device name.
20AB
LD (IX+00H),2AH DD 36 00 2A
Store 2AH, the asterisk of a device specification, into FCB+0 (Index Register IX+00H): bit 7 clear, the FCB is closed.
20AF
LD (IX+01H),C DD 71 01
Store Register C (the first letter) into FCB+1 (Index Register IX+01H).
20B2
LD (IX+02H),B DD 70 02
Store Register B (the second letter) into FCB+2 (Index Register IX+02H).
20B5
LD (IX+03H),03H DD 36 03 03
Store 03H (ETX) into FCB+3 (Index Register IX+03H), which ends the device specification of an asterisk and two letters.
20B9
XOR A AF
Clear Register A to 00H and set the Z flag: the device FCB is closed.
20BA
RET C9
Return Z with Register A = 00H through RESTREG, which gives the caller Register Pairs HL, DE, BC and Index Register IX back.

20BBH - CALCBIT: Reset a Granule's Bit

CALCBIT builds the instruction RES n,B at 20C5H for the granule number n in Register A and runs it, so that the granule's bit in the GAT byte held in Register B is reset.

20BBCALCBIT
AND 07H E6 07
CALCBIT: mask Register A (the granule in the cylinder) with 07H, the bit number 0 to 7.
20BD
RLCA 07
Rotate Register A left one bit: the bit number starts to move into bits 5-3.
20BE
RLCA 07
Rotate Register A left a second time, moving the bit number on toward bits 5-3.
20BF
RLCA 07
Rotate Register A left a third time: the bit number is in bits 5-3, the place it has in a CB opcode.
20C0
OR 80H F6 80
OR Register A with 80H: 80H + 8 x n, the second byte of RES n,B.
20C2
LD (20C6H),A 32 C6 20
Store Register A into 20C6H, the second byte of the instruction at 20C5H.
20C5CALC1
RES 0,B CB 80
CALC1: reset bit n of Register B (the GAT byte), n being the bit stored into 20C6H at 20C2H (RES 0,B in the file).
20C7
RET C9
Return to 1FAAH with the granule's bit reset in Register B.

20C8H - HOLDUP: the CLOSE FAULT Message

When the drive check of CLOSE fails, HOLDUP shows the message at 20EEH and waits for a key. ENTER goes back to CKAGN (1E23H) to check the drive again; BREAK gives up the close with Register A = 20H (error 32, Illegal drive number) and the NZ flag, leaving the FCB open. Every other key is ignored. Nothing checks that the diskette put back is the one the file was open on (B23).

20C8HOLDUP
PUSH HL E5
HOLDUP: save Register Pair HL on the stack (the caller's, as CLOSE received it).
20C9
PUSH DE D5
Save Register Pair DE (the FCB) on the stack.
20CA
LD HL,20EEH 21 EE 20LD HL,HOLDUP$
Point Register Pair HL to 20EEH (HOLDUP$), the CLOSE FAULT message.
20CD
Call @DSPLY at 052DH (on the BOOT/SYS page) to show the message at Register Pair HL on the video display, up to and with its 0DH.
20D0
Call @CKBRKC at 0553H (on the BOOT/SYS page) to reset the BREAK bit of KFLAG$ (0074H) if it is set, so that only a BREAK pressed from now on counts.
20D3WAITING
WAITING: call @KBD at 0635H (on the BOOT/SYS page) to scan the keyboard: Z with the key in Register A, NZ when no key is waiting.
20D6
If no key was pressed, jump relative to 20D3H (WAITING) and scan again.
20D8
CP 0DH FE 0D
Compare Register A (the key) with 0DH, the ENTER key.
20DA
If ENTER was pressed, jump relative to 20E9H (TRYNOW) to check the drive again.
20DC
Call @CKBRKC at 0553H (on the BOOT/SYS page): NZ when the BREAK bit of KFLAG$ is set (it is then reset), Z otherwise.
20DF
If the key was not BREAK, jump relative to 20D3H (WAITING) and ignore it.
20E1ABRT
POP DE D1
ABRT: restore Register Pair DE from the stack.
20E2
POP HL E1
Restore Register Pair HL from the stack (the caller's, as CLOSE received it).
20E3
POP IY FD E1
Restore Index Register IY (the caller's), saved at 1E1EH, from the stack.
20E5
LD A,20H 3E 20
Load Register A with 20H, error 32 (Illegal drive number), the code the TRM gives for a close given up with BREAK.
20E7
OR A B7
OR Register A with itself to set the NZ flag.
20E8
RET C9
Return NZ with error 32 in Register A through RESTREG; the FCB stays open and its last buffer is not written.
20E9TRYNOW
POP DE D1
TRYNOW: restore Register Pair DE from the stack.
20EA
POP HL E1
Restore Register Pair HL from the stack; Index Register IY's saved copy stays on the stack.
20EB
Jump to 1E23H (CKAGN) to check the drive again. Nothing checks that the diskette now in the drive is the one the file was opened on (B23).
20EEHOLDUP$
DEFB 0AH 0A
HOLDUP$, 20EEH: 0AH, a line feed, so that the message starts on a new line.
20EF-2112
DEFM "** CLOSE FAULT ** Drive not ready, " 2A 2A 20 43 4C 4F 53 45 20 46 41 55 4C 54 20 2A 2A 20 20 44 72 69 76 65 20 6E 6F 74 20 72 65 61 64 79 2C 20
20EFH-2112H: two asterisks, a space, CLOSE FAULT, a space and two asterisks, then two spaces and Drive not ready with a comma and a space after it, 36 characters.
2113-2134
DEFM "<ENTER> to retry, <BREAK> to abort" 3C 45 4E 54 45 52 3E 20 74 6F 20 72 65 74 72 79 2C 20 3C 42 52 45 41 4B 3E 20 74 6F 20 61 62 6F 72 74
2113H-2134H: the text that tells the user the two keys HOLDUP takes, 34 characters.
2135
DEFB 0DH 0D
2135H: 0DH, which ends the message for @DSPLY and is shown as a new line.

2136H - DOID, Free Space and the Overlay Length

DOID was function 3 of SYS3 in earlier versions: it checked and showed the customer service number for the ID library command. 6.3.1 assembles only its RET (`@BLD631`), and the ID command shows No service contract! instead. The rest of the overlay area is not loaded, and the length of SYS3 at 23FEH ends the file as in the other system overlays.

2136DOID
RET C9
DOID: return (function 3, request B5H) with the Z flag and Register A = 30H from the CP at 1E0CH. The last byte of the code of SYS3/SYS.
2137-23FD
DEFS 711
2137H-23FDH: the rest of the overlay area, not loaded by SYS3/SYS; it holds what was there before. DIRBUF$ (2300H-23FFH), where @GATRD reads the GAT for CLOSE (1F70H), lies in it and overlaps the length word at 23FEH, which each load of SYS3 writes again.
23FE-23FF
DEFW 0337H 37 03
23FEH-23FFH = 0337H (823): the length of SYS3, 1E00H to 2136H (LAST less 1E00H). SYSTEM (SYSRES) reads it when it copies the overlay into high memory to keep it resident, and stores it back when it brings the copy into 1E00H again.