Lesson 40 +40 XP

Input-Output Interface, Strobe & Handshaking, Asynchronous Serial Transfer

Input-Output Interface, Strobe & Handshaking, Asynchronous Serial Transfer

A computer talks to the outside world through peripheral devices: keyboards, displays, printers, disks. This lesson explains why each device needs an interface, how the processor addresses it, and how two units that do not share a clock can still pass data safely, in parallel (strobe and handshaking) or in serial (start and stop bits).

1. Why an I/O Interface Is Needed

A peripheral cannot be wired straight to the processor's bus, because the two differ in four ways:

  1. Signals: peripherals are electromechanical or electromagnetic devices; the CPU and memory are electronic. A conversion of signal values may be needed.
  2. Speed: a peripheral is usually much slower than the CPU, so a synchronisation mechanism is needed.
  3. Data codes and formats: a peripheral's data format (for example, serial characters) differs from the CPU's word format.
  4. Operating modes: each peripheral has its own way of working, and it must be controlled without disturbing the others.

The interface unit sits between the processor bus and the device and solves all four: it converts signals, buffers and synchronises data, converts formats and supervises the device.

2. The I/O Bus and Interface Modules

Connection of the I/O bus to input-output devicesConnection of the I/O bus to input-output devices

The I/O bus has data, address and control lines. Every interface has an address decoder that watches the address lines; when it sees its own address it connects to the bus, and the others stay disconnected. With the address the processor sends a function code (I/O command). There are four kinds:

I/O commandWhat it doesExample
Controlactivates the device and tells it what to dorewind a tape, start the disk motor
Statustests conditions in the interface and deviceis the printer ready? did a parity error occur?
Data outputthe interface takes data from the bus into its buffersend a character to the printer
Data inputthe interface puts data from the device on the busread a character from the keyboard

3. I/O Bus versus Memory Bus

The processor talks to memory as well as to I/O, and there are three ways to arrange the buses:

  1. Separate buses: one set of data, address and control lines for memory and another for I/O (used with an I/O processor, IOP).
  2. Common bus with separate control lines (isolated I/O): memory and I/O share the address and data lines, but there are separate read and write lines for each (memory read and write, I/O read and write). Special instructions (IN, OUT) drive the I/O lines.
  3. Common bus with common control lines (memory-mapped I/O): interface registers are given ordinary memory addresses, and the normal load and store instructions access them.
Isolated I/OMemory-mapped I/O
Address spaceseparate for I/O and memoryone space shared by both
Instructionsspecial IN and OUTany memory-reference instruction
Control linesseparate I/O read and I/O writethe memory read and write lines
Memory availablefull address rangepart of it is taken by I/O
ExampleIntel 8085, x86 port I/OARM, Motorola 68000
💡 Tip

Memory-mapped I/O lets every instruction that works on memory (add to, compare, test a bit) work directly on an interface register.

4. Example of an I/O Interface Unit

An I/O interface unit with two ports, a control register and a status registerAn I/O interface unit with two ports, a control register and a status register

The interface has two data ports (A and B), a control register and a status register. The CPU picks the interface with chip select (CS) and one of its four registers with the register select inputs RS1 RS0. The I/O read (RD) and I/O write (WR) inputs give the direction.

CSRS1RS0Register selected
0xxnone: the data bus is in high impedance
100Port A register
101Port B register
110Control register
111Status register

The CPU writes a control word into the control register to set the mode (for example, port A input and port B output). It reads the status register to see, for instance, that port A has new data or that port B's last byte was taken by the device.

SolvedExample 1 · Decoding the interface's addresses

The interface above must answer to the four addresses 40H to 43H on an 8-bit address bus A7..A0. Give the logic for CS, RS1 and RS0.

  1. 40H = 0100 0000 and 43H = 0100 0011: only A1 and A0 change.
  2. So RS1 = A1 and RS0 = A0.
  3. CS must be 1 when A7..A2 = 010000: CS = overline{A7} A6 overline{A5} overline{A4} overline{A3} overline{A2}.
AddressRegister
40HPort A
41HPort B
42HControl
43HStatus
AnswerCS = overline{A7} A6 overline{A5} overline{A4} overline{A3} overline{A2}, RS1 = A1, RS0 = A0

5. Asynchronous Data Transfer

Inside a computer, registers share one clock (synchronous transfer). The CPU and an interface often have independent clocks, so they cannot use timing to know when data is ready. They exchange control signals instead. Two methods are used.

Strobe Control

A single control line, the strobe, marks the moment data is valid.

  • Source-initiated: the source puts data on the bus, then raises the strobe; the destination takes the data during the strobe pulse (usually on its falling edge).
  • Destination-initiated: the destination raises the strobe to ask for data; the source then places data on the bus, and the destination takes it before dropping the strobe.

The weakness: the source never learns whether the destination actually received the data (source-initiated), and the destination never learns whether the data on the bus is valid (destination-initiated).

Handshaking

Handshaking adds a second control line in the opposite direction, so each unit tells the other what it has done.

Strobe control and two-wire handshaking timingStrobe control and two-wire handshaking timing

Source-initiated handshake, step by step:

  1. The source places data on the bus and enables data valid.
  2. The destination accepts the data and enables data accepted.
  3. The source disables data valid; the bus data is now invalid.
  4. The destination disables data accepted and is ready for the next byte.

Destination-initiated handshake: the destination first enables ready for data; the source places data and enables data valid; the destination takes it and disables ready for data; the source then disables data valid.

StrobeHandshake
Control lines12 (one in each direction)
Confirmation of transfernoneeach step is acknowledged
Works with any speedonly if the timing is knownyes, it waits for the slower unit
Fault detectionnonea timeout detects a unit that does not respond

If the other unit does not answer within a set time, the waiting unit's timeout circuit signals an error. Handshaking can therefore detect a faulty or missing device.

The two units below each have their own logic and only see each other's control line. They send the three characters H, I and !:

VERILOG
// Source-initiated handshaking: the source raises "data valid" when the
// bus holds a byte; the destination raises "data accepted" after taking it.
module source_unit (
    input clk,
    input rst,
    input accepted,
    output reg valid,
    output reg [7:0] bus,
    output reg done
);
    reg [7:0] mem [0:2];
    reg [1:0] i;
    initial begin
        mem[0] = 8'h48;    // 'H'
        mem[1] = 8'h49;    // 'I'
        mem[2] = 8'h21;    // '!'
    end
    always @(posedge clk)
        if (rst) begin
            valid <= 0;
            i <= 0;
            done <= 0;
        end else if (!valid && !accepted && !done) begin
            bus <= mem[i];          // place data on the bus
            valid <= 1;             // then enable data valid
        end else if (valid && accepted) begin
            valid <= 0;             // disable data valid, bus now invalid
            if (i == 2)
                done <= 1;
            i <= i + 1'b1;
        end
endmodule

module destination_unit (
    input clk,
    input rst,
    input valid,
    input [7:0] bus,
    output reg accepted,
    output reg [7:0] latched
);
    always @(posedge clk)
        if (rst)
            accepted <= 0;
        else if (valid && !accepted) begin
            latched <= bus;         // accept data from the bus
            accepted <= 1;          // enable data accepted
        end else if (!valid && accepted)
            accepted <= 0;          // ready to accept the next byte
endmodule

module tb;
    reg clk = 0, rst = 1;
    wire valid, accepted, done;
    wire [7:0] bus, latched;
    source_unit s (clk, rst, accepted, valid, bus, done);
    destination_unit d (clk, rst, valid, bus, accepted, latched);
    always #5 clk = ~clk;
    integer t;
    initial begin
        @(posedge clk);
        #1 rst = 0;
        $display("clk valid accepted  event");
        for (t = 1; t <= 12; t = t + 1) begin
            @(posedge clk);
            #1;
            if (valid && !accepted)
                $display("%3d   %b      %b      source puts %s on the bus", t, valid, accepted, bus);
            else if (valid && accepted)
                $display("%3d   %b      %b      destination took %s", t, valid, accepted, latched);
            else if (!valid && accepted)
                $display("%3d   %b      %b      source removes data", t, valid, accepted);
            else
                $display("%3d   %b      %b      handshake complete", t, valid, accepted);
        end
        $finish;
    end
endmodule

Each byte takes the four handshake steps, and the destination never reads before data valid nor the source change data before data accepted:

OUTPUT
clk valid accepted  event
  1   1      0      source puts H on the bus
  2   1      1      destination took H
  3   0      1      source removes data
  4   0      0      handshake complete
  5   1      0      source puts I on the bus
  6   1      1      destination took I
  7   0      1      source removes data
  8   0      0      handshake complete
  9   1      0      source puts ! on the bus
 10   1      1      destination took !
 11   0      1      source removes data
 12   0      0      handshake complete

6. Asynchronous Serial Transfer

Serial transfer sends one bit at a time on a single line. In synchronous serial transfer the two units share a clock. In asynchronous serial transfer they do not, so each character is framed with special bits:

  • The line rests at 1 when idle.
  • A start bit (always 0) marks the start of a character. Its falling edge starts the receiver's timing.
  • The character bits follow (5 to 8 bits, LSB first, sometimes with a parity bit).
  • One or more stop bits (always 1) end the character; the line then stays at 1 until the next start bit.

Asynchronous serial transmission of one characterAsynchronous serial transmission of one character

The receiver knows the bit rate, so after the start bit it samples each bit in the middle of its time. If the bit where the stop bit should be is 0, it reports a framing error.

The baud rate is the number of bits sent per second.

SolvedExample 2 · Terminal with 11-bit characters (Mano)

A terminal sends 10 characters per second, each with 1 start bit, 8 information bits and 2 stop bits. Find the baud rate and the bit time.

  1. Bits per character = 1 + 8 + 2 = 11.
  2. Baud rate = 10 × 11 = 110 bits per second.
  3. Bit time = 1 / 110 s = 9.09 ms.
Answer110 baud, 9.09 ms per bit
SolvedExample 3 · Characters per second and efficiency

A line runs at 1200 baud. Each character has 1 start bit, 7 data bits, 1 parity bit and 1 stop bit. How many characters per second can it carry, and what fraction of the bits is data?

  1. Bits per character = 1 + 7 + 1 + 1 = 10.
  2. Characters per second = 1200 / 10 = 120.
  3. Efficiency = 7 / 10 = 70%.
Answer120 characters per second, 70% data bits
SolvedExample 4 · Time to send a file

How long does it take to send a 2 KB file over a 9600-baud line with 10-bit frames (1 start, 8 data, 1 stop)?

  1. Characters = 2 × 1024 = 2048.
  2. Bits = 2048 × 10 = 20480.
  3. Time = 20480 / 9600 = 2.13 s.
Answerabout 2.13 seconds

The transmitter below loads a frame {stop, stop, data, start} into a shift register and shifts it out one bit per bit time; the receiver waits for the start bit and shifts the data back in:

VERILOG
// Asynchronous serial transmitter: 1 start bit (0), 8 data bits sent
// LSB first, 2 stop bits (1). The line rests at 1 between characters.
module serial_tx (
    input clk,                 // one tick per bit time
    input load,
    input [7:0] char,
    output line,
    output busy
);
    reg [10:0] frame;
    reg [3:0] count;
    assign line = frame[0];
    assign busy = (count != 0);
    initial begin
        frame = 11'h7FF;       // idle: line high
        count = 0;
    end
    always @(posedge clk)
        if (load && !busy) begin
            frame <= {2'b11, char, 1'b0};   // stop, stop, data, start
            count <= 11;
        end else if (busy) begin
            frame <= {1'b1, frame[10:1]};   // shift out, fill with 1s
            count <= count - 1'b1;
        end
endmodule

// Receiver: waits for the falling edge of the start bit, then shifts in
// 8 data bits and checks the stop bit (framing error if it is 0).
module serial_rx (
    input clk,
    input line,
    output reg [7:0] char,
    output reg ready,
    output reg frame_err
);
    reg [3:0] count = 0;
    reg [7:0] shift;
    always @(posedge clk) begin
        ready <= 0;
        if (count == 0) begin
            if (!line) count <= 1;          // start bit found
        end else if (count <= 8) begin
            shift <= {line, shift[7:1]};    // LSB arrives first
            count <= count + 1'b1;
        end else begin                      // first stop bit
            char <= shift;
            frame_err <= !line;
            ready <= 1;
            count <= 0;
        end
    end
endmodule

module tb;
    reg clk = 0, load = 0;
    reg [7:0] c = "C";
    wire line, busy, ready, ferr;
    wire [7:0] got;
    serial_tx tx (clk, load, c, line, busy);
    serial_rx rx (clk, line, got, ready, ferr);
    always #5 clk = ~clk;
    integer k;
    reg seen = 0;
    always @(posedge clk)
        if (ready)
            seen <= 1;            // remember the one-clock ready pulse
    initial #2000 $finish;        // safety stop
    initial begin
        $display("sending '%s' = %b", c, c);
        @(negedge clk) load = 1;
        @(negedge clk) load = 0;
        $write("line: ");
        for (k = 0; k < 11; k = k + 1) begin
            $write("%b", line);
            @(negedge clk);
        end
        $display("  (start, 8 data bits LSB first, 2 stop)");
        while (!seen) @(negedge clk);
        $display("received '%s' = %b, framing error = %b", got, got, ferr);
        $finish;
    end
endmodule

The line carries 0 (start), then 1100 0010 (the bits of 'C' from LSB to MSB), then 11 (stop):

OUTPUT
sending 'C' = 01000011
line: 01100001011  (start, 8 data bits LSB first, 2 stop)
received 'C' = 01000011, framing error = 0

7. Asynchronous Communication Interface (UART)

A UART (universal asynchronous receiver-transmitter) is an interface chip that does the serial-to-parallel work:

  • Transmitter: the CPU writes a byte into the transmitter register; it moves to a shift register that adds the start and stop bits and shifts them out on the serial line.
  • Receiver: a shift register collects the incoming bits after the start bit; the complete byte moves to the receiver register for the CPU to read.
  • Status register: flags such as transmitter register empty, receiver register full, parity error, framing error and overrun (a new character arrived before the CPU read the last one).
  • Control register: sets the number of bits, parity, stop bits and baud rate.

A FIFO buffer (first-in, first-out) is often placed between a fast and a slow unit. The input side writes at its own rate and the output side reads at its own rate, and the bytes come out in the same order they went in.

💡 Tip

Key points to remember

  • Interfaces fix differences in signals, speed, data formats and operating modes.
  • Four I/O commands: control, status, data output, data input.
  • Isolated I/O: separate address space and IN/OUT instructions. Memory-mapped: shared space, normal instructions.
  • Strobe: one line, no acknowledgment. Handshake: two lines, every step acknowledged, timeout detects faults.
  • Serial frame: start bit 0, data LSB first, stop bit(s) 1. Baud = bits per second.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Why is an I/O interface needed? Explain the four types of I/O commands. (5 Marks)
  • Q2. Differentiate between isolated I/O and memory-mapped I/O. (5 Marks)
  • Q3. Explain asynchronous data transfer using strobe control and handshaking, with timing diagrams. What is the drawback of strobe control? (10 Marks)
  • Write: source- and destination-initiated versions of both, and the four handshake steps.
  • Q4. Explain asynchronous serial transfer with a neat diagram. A terminal sends 10 characters/s with 11 bits each: find the baud rate. (5 Marks)
  • Q5. Draw the block diagram of an I/O interface unit and explain the use of CS, RS1 and RS0. (5 Marks)