Introduction
A .nib file is what the Disk II drive reads off a floppy, not what the file system stores on it: the nibbles, the disk bytes, of each of the 35 tracks, 6,656 of them a track, 232,960 bytes in all. It keeps what a
sector image throws away: the volume number in every address field, the gaps, the order the sectors were written in, and checksums and sectors that are damaged or missing. Disks with
non-standard formats can therefore be kept, in the simplest cases, which a sector image cannot hold. The same extension is used for Cocoa interface files, which are not this format.
A track
Every disk byte has its high bit set, and no two zero bits in a row (so that the drive can keep time). The track below is a 16-sector one; a 13-sector track has the same shape with another data field. Between the fields are gaps of $FF bytes. On the disk they are written as 10-bit self-sync bytes ($FF and two zero bits); in a .nib file they are plain $FF bytes. A track is a ring: reading wraps from its last byte to its first, and a field may straddle the join. A DOS 3.3 track of 16 sectors is about 6,400 nibbles (the tracks built for the tests here are 6,384: 48 sync bytes, then 16 sectors of 396) and the file pads the rest of the 6,656 with $FF.
Each sector is two fields:
The address field
| Bytes | Meaning |
|---|---|
| 3 | Prologue D5 AA 96 (D5 AA B5 for a 13-sector disk) |
| 2 | Volume number, 4-and-4 encoded |
| 2 | Track number, 4-and-4 |
| 2 | Sector number, 4-and-4: the physical position on the track (0 to 15), not the DOS or ProDOS logical number |
| 2 | Checksum: volume xor track xor sector, 4-and-4 |
| 3 | Epilogue DE AA EB (the last byte is often not read) |
4-and-4 puts one byte in two disk bytes: first (v >> 1) | $AA (the odd bits), then v | $AA (the even bits); a reader combines them with ((a << 1) | 1) & b. Volume 254 is FF FE.
The data field
| Bytes | Meaning |
|---|---|
| 3 | Prologue D5 AA AD |
| 342 | The sector's 256 bytes in the 6-and-2 encoding |
| 1 | Checksum |
| 3 | Epilogue DE AA EB |
6-and-2. A disk byte carries only 6 bits (only 64 bytes have no two adjacent zeros and the high bit set, the table 96 97 9A 9B 9D 9E 9F A6 A7 AB AC AD AE AF B2 B3 …FF), so 256 bytes are split into the top 6 bits of each (256 values) and the low 2 bits of each, packed 3 to a value (86 values). On the disk the 86 packed values come first. Value m of the 86 holds the low two bits (bit 0 and bit 1 swapped) of bytes m, m + 86 and m + 172, those of byte m in its lowest bits. The 256 six-bit values follow in byte order. Each of the 342 values is written as the exclusive-or of itself and the one before it, and the 343rd byte is the last value on its own: the checksum.
What was compared. The 64-byte table was read out of the KEGS emulator's source (to_disk_byte in iwm.c), and the encoding and decoding were compared with a port of KEGS's own routines (sector_to_partial_nib and the data part of iwm_denib_track525): the viewer's encoder is decoded by that port, and the viewer's decoder reads what the port encodes, on 40 test sectors including all zeros and all $FF. A NIB built from the real DOS 3.3 System Master disk with the viewer's encoder was also decoded by the port: all 560 sectors came back as the disk's own sectors. That comparison is of built files only: no real 16-sector .nib file was available, so real gap lengths, sync patterns and half-tracks of a 16-sector disk were not seen. The two real .nib files that were available are 13-sector disks, below.
13-sector disks: 5-and-3
DOS 3 up to DOS 3.2.1 put 13 sectors on a track (35 × 13 × 256 = 116,480 bytes, the size of a .d13 sector image) with the older 5-and-3 encoding; DOS 3.3 changed the disk controller's PROM so that the 6-and-2 encoding became possible and the sectors 16. A 13-sector track is recognised by its address field, D5 AA B5 (6-and-2 is D5 AA 96): the address field is the same as before (volume, track, sector and checksum in 4-and-4; sector 0 to 12). The data field is:
| Bytes | Meaning |
|---|---|
| 3 | Prologue D5 AA AD |
| 410 | The sector's 256 bytes in the 5-and-3 encoding: 410 five-bit values, each written as a disk byte from the 32-byte table below |
| 1 | Checksum |
| 3 | Epilogue DE AA EB |
The table of the 32 disk bytes for the values 0 to 31 is AB AD AE AF B5 B6 B7 BA BB BD BE BF D6 D7 DA DB DD DE DF EA EB ED EE EF F5 F6 F7 FA FB FD FE FF: no two zero bits in a row, the high bit set, and none of the bytes $D5 and $AA, which are reserved for the prologues. 256 × 8 = 2,048 bits make 409.6 five-bit values, so 410 are written (Beneath Apple DOS, the chapter on the disk's data encoding: 410 disk bytes for a 256-byte sector, the translate table in its figure 3.19). The running exclusive-or of the values, as in 6-and-2 (each disk byte is the exclusive-or of two neighbouring values, and the last disk byte is the final running value), is the checksum rule Beneath Apple DOS describes for both encodings.
Where the bits go. The book gives this only as a figure, which the text we could read does not carry (the OCR of it is garbled). The arrangement here was found from real data and then checked, not copied from a source: the DOS image on tracks 0 to 2 of a real 13-sector WOZ 2 master disk was compared with the same disk as a .d13, trying every arrangement of the secondary values, and the one that matched was then used on every sector of that disk (455 of 455 sectors decode with good address and data checksums, a VTOC of release 2, volume 254 and a readable catalogue). Call the 410 values after the running exclusive-or v[0] to v[409]; let S be v[0 to 153] (the three-bit part) and T be v[154 to 409] (the five-bit part: the top five bits of the 256 bytes). For a group j from 0 to 50, with a = S[j + 1], b = S[j + 52] and c = S[j + 103]:
| Byte | Top five bits | Low three bits |
|---|---|---|
| 5j | T[50 − j] | c >> 2 |
| 5j + 1 | T[101 − j] | b >> 2 |
| 5j + 2 | T[152 − j] | a >> 2 |
| 5j + 3 | T[153 − j] | bit 1 of c, b, a (c is the high bit) |
| 5j + 4 | T[204 − j] | bit 0 of c, b, a (c is the high bit) |
| 255 | T[255] | S[0] & 7 |
The sectors of a 13-sector disk are kept in physical order, which is the order a .d13 holds them in: DOS 3.2 has no sector skew of its own, so there is no DOS order and ProDOS order to choose between.
What was compared. Two real 13-sector .nib files, both from an INIT of a 13-sector disk, and the 13-sector WOZ master. In both .nib files the address fields of all 455 sectors are present, but INIT wrote data fields for only 50 of them (tracks 0 to 2, track 17 and part of track 18): the viewer decodes exactly those 50, all with good checksums, reads a DOS 3.2 VTOC (volume 254, 35 tracks, 13 sectors) and the catalogue entries HELLO and HIYA, and lists the 405 unwritten sectors as problems of their tracks. The ten empty catalogue sectors of the first file equal those of the blank DOS 3.2 .d13, and the two files agree on 37 of the 39 sectors of the DOS image on tracks 0 to 2 (that DOS image differs from the one in the blank .d13, so the .d13 was not a reference for tracks 0 to 2 of these files). A decoder written separately (scripts/real-files/decode53.mjs), the viewer's own encoder and a second encoder written for the test all agree with the viewer's decoder on 200 random sectors.
In the explorer
The tree has the 35 tracks and in each the fields in the order they are: gaps, address fields with their six parts, data fields with prologue, 342 encoded values, checksum and epilogue, named by the sector and the file it belongs to. A field that straddles the end of the track is shown in two parts. A checksum that is wrong, a missing field and a duplicate sector are problems of the track. The explorer takes the sectors by their physical numbers, builds the disk in DOS order and in ProDOS order, and keeps the order in which a file system reads (a tie goes to ProDOS order for a ProDOS disk and DOS order for the rest, since both give the same disk). The table of tracks lists the sectors decoded of each; the disk shown after that is the one a sector image would give. A track of 5-and-3 sectors is read the same way (13 sectors, D5 AA B5 address fields) and the table says (5-and-3) next to its count; a disk with 5-and-3 tracks is built as 35 tracks of 13 sectors in physical order (116,480 bytes) and read with the DOS 3.2 VTOC and catalogue. A sector whose address field is there but whose data field was never written shows as a problem, not as zeros. A sector whose field starts in the second lap of the stream (the stream begins inside it) is found there.
Not described
Known, not yet covered
- Reference: the 5-and-3 bit arrangement is derived from real disks and checked on them (455 sectors of one disk, 50 of each of two more) but it is not quoted from a specification: the figure of Beneath Apple DOS that shows it could not be read as text, and no other source of the arrangement was found.
- Viewer: 36-track and 40-track
.nibfiles, half-track images and 2IMG format 2 are not read; only the 232,960-byte form is. - Viewer: disks protected by non-standard fields (other prologues, data checksums that are meant to fail) show the sectors that decode and list the others.
Unknown
- Reference and viewer: the only real
.nibfiles here are the two 13-sector disks above, both mostly unwritten; no real 16-sector.nibis available, so every reading of 6-and-2 tracks is of built files.
Variants
- Apple II sector image (.DSK, .DO, .PO) – the sectors with no header, in DOS or ProDOS order
- 2IMG disk image – a 64-byte header in front of the sectors
- NIB nibble image – the nibbles of each track as the drive reads them
- WOZ disk image – the bit stream of every track, in chunks
Links
Software
- AppleWin – Apple II emulator that loads .nib files (Windows)
- CiderPress II – tool to view and convert Apple II disk images (Windows, macOS, Linux)