The Challenge of Parallel Data Transfer
Unlike serial communication (sending one bit at a time, Part 40), parallel communication sends an entire byte (8 bits) simultaneously across 8 dedicated data lines. This requires additional lines for handshaking to ensure the slow peripheral (the printer) is ready to receive the data.
Printer Ports on the Spectrum
The ZX Spectrum often uses an external interface (like the ZX Interface 1 or Kempston printer interface) to add parallel output capability. These interfaces typically map to two adjacent I/O ports:
- Data Port: Used to write the 8 bits of data (the character/byte).
- Status/Control Port: Used to read the printer’s status (e.g., “Busy,” “Out of Paper”) and send control signals (e.g., “Strobe”).
The Handshaking Routine
The CPU must perform a polling loop on the Status Port before and after writing data to ensure the printer does not miss any bytes.
Generic Handshaking Sequence:
PRINTER_DATA_PORT EQU 0B3H ; Example Data Port
PRINTER_STATUS_PORT EQU 0B4H ; Example Status/Control Port
SEND_CHAR:
; Assume the character to send is in the Accumulator (A).
; --- 1. Wait for Printer NOT Busy ---
WAIT_READY:
IN B, (PRINTER_STATUS_PORT) ; Read the status byte into B
BIT 7, B ; Example: Check the "Busy" bit (Bit 7)
JP NZ, WAIT_READY ; If Busy bit is 1, keep waiting
; --- 2. Write Data ---
OUT (PRINTER_DATA_PORT), A ; Send the character to the printer
; --- 3. Strobe Signal (Tell Printer Data is Ready) ---
LD A, STROBE_ON_MASK ; Set the 'Strobe' bit low (often active-low)
OUT (PRINTER_STATUS_PORT), A ; Activate the strobe
CALL DELAY_ROUTINE ; Wait a few cycles for the printer to register the strobe
LD A, STROBE_OFF_MASK ; Deactivate the strobe
OUT (PRINTER_STATUS_PORT), A ; Deactivate the strobe
RET
Character and Control Codes
The data sent to the printer is standard ASCII for normal text. However, to control formatting (line feeds, form feeds, font changes), you must send specific control codes (non-printable characters) defined by the printer manufacturer.
Example Printer Codes:
Hex Code | ASCII/Control | Purpose |
---|---|---|
0DH | CR (Carriage Return) | Moves the print head to the start of the line. |
0AH | LF (Line Feed) | Moves the paper up one line. |
0CH | FF (Form Feed) | Advances the paper to the next page. |
The printing routine must handle the complete string, including the appropriate delay and control codes between each character.