Networking is organized into layers. The Link Layer (Layer 2 of the OSI model) is the lowest level of communication, responsible for sending and receiving frames (raw data packets) between two connected devices (e.g., two computers on a local network).

Goal: To wrap user data with a small header and footer so the receiver knows where the packet begins, where it ends, and whether it’s valid.

Defining the Frame Structure

Our simple protocol needs three main parts: a Header, the Payload (data), and a Footer (for error checking).

Field Size (Bytes) Purpose
Start Marker 1 A unique byte (e.g., 0AAH) that signals the start of a frame.
Length 2 The 16-bit size of the payload.
Payload Variable The actual user data being sent (e.g., ASCII text, game state).
Checksum 1 A simple error-checking value (Footer).

Sending a Frame (The SIO Driver)

The transmit routine uses the SIO chip (Part 63) to send each byte of the frame sequentially.

The Steps:

  1. Calculate Checksum: Generate the Checksum (e.g., summing all payload bytes) and store it.
  2. Wait for Ready: Poll the SIO’s Transmit Ready (TxRDY) flag.
  3. Transmit: Send the Header, then the Payload, then the Checksum, waiting for TxRDY after each byte.
SEND_FRAME:
    CALL CALC_CHECKSUM     ; Generate the error byte
    
    LD   A, START_MARKER    ; A ← 0AAH
    CALL SIO_TX_BYTE       ; Send the Start Marker

    ; ... (Code to send Length High, Length Low)

    CALL TRANSMIT_PAYLOAD  ; Loop through all data bytes
    
    LD   A, CHECKSUM_VALUE
    CALL SIO_TX_BYTE       ; Send the Checksum
    RET

Receiving a Frame (The SIO Interrupt)

The receiver relies on a Receive Interrupt from the SIO to read incoming data.

The Logic:

  1. Wait for Start: The receiver discards all incoming bytes until it sees the Start Marker (`0AAH′).
  2. Read Header: Reads the 2-byte Length field to know how much data to expect.
  3. Read Payload: Reads the specified number of payload bytes into a buffer, calculating a running checksum of the payload data simultaneously.
  4. Verify: After the payload, the receiver reads the final Checksum byte and compares it to its own calculated checksum.
  5. Error Handling: If the checksums do not match, the frame is corrupted and must be discarded or requested again.