The Challenge of Mass Storage
Traditional Z80 systems used slow tape drives, but modern retro systems often use SD cards or CompactFlash for fast, reliable storage. The Z80 must communicate with these devices using a low-level protocol, typically SPI (Serial Peripheral Interface).
The SPI Protocol (Simplified)
SPI is a synchronous serial protocol that sends data one bit at a time over several dedicated lines controlled by I/O ports:
Line | Direction | Purpose |
---|---|---|
CLK | Output | The Clock signal (controls timing). |
MOSI | Output | Master Out, Slave In (Data sent from Z80 to SD card). |
MISO | Input | Master In, Slave Out (Data received by Z80 from SD card). |
CS | Output | Chip Select (Activates the specific SD card). |
Bit-Banging the SPI Signal
Since the Z80 doesn’t have a native SPI controller, we must bit-bang the protocol entirely in software using fast I/O port writes and reads.
The Write (Sending a Byte): To send one byte, the Z80 must loop 8 times, checking each bit of the byte and setting the MOSI line (an output pin) high or low before toggling the CLK line.
SPI_OUT_PORT EQU 0B0H ; Example output port
SPI_CLK_BIT EQU 01H ; Bit 0 controls CLK
SPI_MOSI_BIT EQU 02H ; Bit 1 controls MOSI
SEND_SPI_BYTE:
; Assume byte to send is in the C register.
LD B, 8 ; 8 bits to send
SPI_LOOP:
RLCA ; Rotate byte (C) left, bit 7 → Carry flag
JP NC, SEND_LOW ; Jump if Carry is NOT set (bit is 0)
SEND_HIGH:
SET 1, A ; Set MOSI bit high
JR TOGGLE_CLK
SEND_LOW:
RES 1, A ; Reset MOSI bit low
TOGGLE_CLK:
OUT (SPI_OUT_PORT), A ; Send data line status
SET 0, A ; Toggle CLK high
OUT (SPI_OUT_PORT), A
RES 0, A ; Toggle CLK low (completes the clock cycle)
OUT (SPI_OUT_PORT), A
DJNZ SPI_LOOP
RET
File System Abstraction
Directly managing the sectors, clusters, and file allocation tables (FAT) on an SD card is too complex for a typical assembly program. Real-world systems use a File System Abstraction Layer (a large library in ROM or RAM) that handles the FAT/directory logic.
Programmer’s Job: The programmer simply calls a complex routine like CALL FILE_OPEN′ or
CALL FILE_READ′, passing the filename and a buffer address. The abstraction layer then executes the thousands of necessary SPI commands.