The Challenge of Physical Storage
When a user program requests to open a file (e.g., MYFILE.BIN
), the OS kernel must translate that name into a list of physical disk addresses (sectors) where the data is stored.
Key Disk Terminology:
- Track: A concentric ring on the disk where data is stored.
- Sector: A small, fixed-size block of data (e.g., 512 bytes) that lives on a track.
- Directory: A list of file names, sizes, and the starting sector number for each file.
The File Allocation Table (FAT)
The File Allocation Table (FAT) is the core data structure that links a file’s starting sector to all the subsequent sectors that make up the file.
How the Kernel Uses FAT:
- File Open: The kernel searches the Directory for
MYFILE.BIN
to find its First Sector Number. - Data Read: The kernel reads the first sector. To find the second sector, the kernel looks up the First Sector Number in the FAT.
- The Chain: The value stored in the FAT entry for the first sector is the address of the Next Sector in the file’s data chain.
- End of File: The chain ends when the FAT entry contains a special code (e.g., `FFFFH′) indicating the End of File (EOF).
Low-Level I/O (The Driver)
The final step is to communicate with the disk controller hardware (the Driver). This routine handles the precise I/O commands to move the physical read/write head.
Low-Level Sector Read Routine:
DISK_CONTROLLER_PORT EQU 0B0H ; Example I/O port for the controller
READ_SECTOR:
; Assume BC = Track Number
; Assume DE = Sector Number
; Assume HL = RAM Buffer Address (where to put the 512 bytes)
; 1. Write the Track and Sector numbers to the controller registers
OUT (DISK_CONTROLLER_PORT), C ; Write Low Byte of Track (and subsequent registers)
; 2. Issue the READ command
LD A, READ_CMD_CODE
OUT (DISK_CONTROLLER_PORT), A
; 3. Poll for Completion (Wait until the controller is ready)
WAIT_LOOP:
IN A, (DISK_CONTROLLER_STATUS)
BIT 7, A ; Check the "Busy" bit
JP NZ, WAIT_LOOP ; Loop while controller is busy
; 4. Read the 512 bytes of data from the controller into the RAM buffer (HL)
CALL FAST_TRANSFER_512
RET
User Program Interface
The user program never talks to the FAT or sectors directly. It uses simple System Calls (like `RST 18H′) that pass the filename and buffer address to the OS kernel, which handles the complex translation process.