0

I'm trying to create my own very basic OS and I'm trying to figure out how writing and reading for a disk works. I'm using this website as a reference.

Why does AL goes up to 128 if the number of sectors per track is 17? If I set AL to 20, CL to 1 and write a (512 * 19) bytes string, since sector 18 and 19 don't exist, where would the last 1024 bytes go?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Kyle
  • 23
  • 1
  • 4
  • It will go to sector 1 on the next head, then the next cylinder. Read about [c/h/s disk geometry](https://en.wikipedia.org/wiki/Cylinder-head-sector). Also, use LBA if possible. – Jester Jul 26 '19 at 22:47
  • @Jester given that SPT is 18, probably on a floppy that likely won;t support extended disk functions / LBA (although other types of drives would support it -CDROM/Hard disks etc). Multitrack wasn't ever guaranteed to work across a cylinder boundary on real hardware although emulators are far more forgiving. – Michael Petch Jul 27 '19 at 02:10
  • Although most floppy media is 18 and 9 sectors per track it is possible to have more (Microsoft's MDF format was 21 sectors per track), and the number of sectors per track on a hard disk can be higher. The limitation of 128 for number of sectors to write (or read) has more to do with the the x86 20bit segment:offset segmentation on the PC (in real mode). A segment can address 64KiB (65336 bytes). 128 * 512 (Byte per sector) = 65536. – Michael Petch Jul 27 '19 at 02:23
  • That documentation appears to be wrong. Sector numbers can be from 1 to 63 (inclusive), not 1 to 17 (inclusive). A disk with 18 sectors per track would allow a value between 1 and 18 (inclusive) – Michael Petch Jul 27 '19 at 02:23

1 Answers1

3

If you ask to write "N sectors starting with the sector at {cylinder, head, sector}" and the number of sectors you've asked to write is on a different track; then:

  • for some devices on some computers; the BIOS supports "multi-track" and will automatically switch to the next track when you reach the end of the previous track

  • for some device on some computers; the BIOS does not supports "multi-track" and will return an error when you reach the end of the previous track

Note that other (potentially better) references for that BIOS function (e.g. http://www.ctyme.com/intr/rb-0608.htm ) say nothing about the 128 sector limit; and it's likely that (in some cases) you might be able to write 255 sectors.

Also note that for floppy disks sometimes (especially for less common media formats - e.g. the "1680 KiB" format) the BIOS has no idea how many sectors there are on a track (it just shoves commands to the floppy controller and reports any errors returned by the floppy controller) and therefore can't support multi-track. For these cases, I vaguely remember some kind of "drive parameters table" in the BIOS Data Area somewhere that software (e.g. ancient MS-DOS) tampered with to correct the information the BIOS uses (using values from the "BPB" structure on the floppy disk).

There is also no notification/indication/safeguard for disk changes. This means that if the user ejects the floppy disk and puts a different floppy disk in the drive, you won't know, and your software will trash the wrong floppy disk without knowing.

Finally (due to max. size limits) for hard drives, in the 1990s the old functions became deprecated and were superseded by a newer set of disk IO functions (that use 64-bit LBA addresses and don't use CHS addressing) called "int 0x13 extensions". This didn't happen for floppy disks; so you'll end up needing different code (that uses different functions) for different types of devices.

Of course the BIOS was never really a formal standard (more like a collection of random manufacturers trying to emulate each other); and now both floppy disks and BIOS are dead (replaced by CDs and USB flash, and replaced by UEFI); so all the "retro computing horror" can be (and has been by many) forgotten; making it hard to find any up-to-date information (e.g. lists of "BIOS bugs" effecting various computers) now.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • 1
    I think it is worth noting that Multitrack isn't guaranteed to work across a cylinder boundary, but only if the tracks are on the same cylinder. Many emulators don't care, but on real hardware it use to be the general case that Multitrack reads that span cylinders would fail. – Michael Petch Jul 27 '19 at 01:14
  • 1
    @MichaelPetch: I always split my reads/writes into "single-track only" (and then had "fall back to single-sector only when there's too many errors" to increase the chance of eventual success - floppies were notoriously unreliable). – Brendan Jul 27 '19 at 01:19
  • 1
    Agreed, keeping a read to within the same track (avoiding multitrack) was usually the best bet (whether that was reading a sector at time in the worst case or computing the number of sectors remaining on the track and reading just that many) – Michael Petch Jul 27 '19 at 01:22
  • I noticed the documentation he linked to appears to be wrong. It says the value in CL (sector number) is 1 to 17. Should be 1 to 63. RBIL has it right. I owndered why he kept talking about "17" in the question. – Michael Petch Jul 27 '19 at 02:28
  • @MichaelPetch: Yeah, that HelpPC page is a little odd for multiple reasons - the "sector number from 1 to 17" would've come from a 720 KiB or 1440 KiB floppy format; but the discussion of CX in the notes ignores floppies completely and only applies to hard drives (for floppies it's an 8 bit sector number and an 8 bit cylinder number, and not a 6-bit sector number and 10-bit cylinder number). – Brendan Jul 27 '19 at 03:31
  • If it was 1440KB and 720KiB sector numbers of 1 through 18 inclusive are valid. Those numbers aren't 0 based, they are 1 based. 1 to 17 is wrong for those formats. – Michael Petch Jul 27 '19 at 03:35
  • 2
    Note: I remember "HelpPC" as a DOS utility that provided a hyper-linked help system. I thought it was abandoned in the 1990s (when Internet and HTML became common) but it looks like someone recycled the old utility's content by converting it to HTML. Of course the same happened with RBIL (the actual package comes with utility/ies to browse the raw text info and no HTML pages). – Brendan Jul 27 '19 at 03:37