IO.pdf
(
226 KB
)
Pobierz
AoA.book
The I/O Subsystem
Chapter Seven
7.1
Chapter Overview
A
typical program does three basic activities: input, computation, and output. In this section we will discuss
the other two activities beyond computation: input and output or I/O.
This chapter concentrates on low-level
CPU I/O rather than high level file or character I/O.
This chapter discusses how the CPU transfers bytes of data
to and from the outside world.
This chapter discusses the mechanisms and performance issues behind the I/O.
7.2
Connecting a CPU to the Outside World
Most I/O devices interface to the CPU in a fashion quite similar to memory
. Indeed, many devices appear to
the CPU as though they were memory devices.
T
o output data to the outside world the CPU simply stores data
into a "memory" location and the data magically appears on some connectors external to the computer
. Simi
-
larly
, to input data from some external device, the CPU simply transfers data from a "memory" location into the
CPU; this "memory" location holds the value found on the pins of some external connector
.
An output port
is a device that looks like a memory cell to the computer but contains connections to the out
-
side world.
An I/O port typically uses a latch rather than a flip-flop to implement the memory cell.
When the
CPU writes to the address associated with the latch, the latch device captures the data and makes it available on a
set of wires external to the CPU and memory system (see
Figure 7.1
). Note that output ports can be write-only
,
or read/write.
The port in
Figure 7.1
, for example, is a write-only port. Since the outputs on the latch do not loop
back to the CPU
s data bus, the CPU cannot read the data the latch contains. Both the address decode and write
control lines must be active for the latch to operate; when reading from the latch
s address the decode line is
active, but the write control line is not.
CPU write control line
W
L
a
t
c
h
Address decode line
En
Data Bus from CPU
Data
Data to outside world
Figure 7.1
A Typical Output Port
Figure 7.2
shows how to create a
read/write input/output port.
The data written to the output port loops back
to a transparent latch.
Whenever the CPU reads the decoded address the read and decode lines are active and this
activates the lower latch.
This places the data previously written to the output port on the CPU
s data bus, allow
-
ing the CPU to read that data.
A
read-only (input) port is simply the lower half of
Figure 7.2
; the system ignores
any data written to an input port.
Page
327
CPU write control line
W
L
a
t
c
h
Address decode line
En
Data Bus from CPU
Data
CPU read control line
Data to outside world
R
L
a
t
c
h
Address decode line
En
Data Bus to CPU
Data
Figure 7.2
An Output Port that Supports Read/Write Access
Note that the port in
Figure 7.2
is not an input port.
Although the CPU can read this data, this port or
ganiza
-
tion simply lets the CPU read the data it previously wrote to the port.
The data appearing on an external connec
-
tor is an output port (only). One could create a (read-only) input port by using the lower half of the circuit in
Figure 7.2
.
The input to the latch would appear on the CPU
s data bus whenever the CPU reads the latch data.
perfect example of an output port is a parallel printer port.
The CPU typically writes an
ASCII character to
a byte-wide output port that connects to the DB-25F connector on the back of the computer
s case.
cable trans
-
mits this data to the printer where an input port (to the printer) receives the data.
A
processor inside the printer
typically converts this
ASCII character to a sequence of dots it prints on the paper
.
Generally
, a given peripheral device will use more than a single I/O port.
typical PC parallel printer inter
-
face, for example, uses three ports: a read/write port, an input port, and an output port.
The read/write port is the
data port (it is read/write to allow the CPU to read the last
ASCII character it wrote to the printer port).
The input
port returns control signals from the printer; these signals indicate whether the printer is ready to accept another
character
, is of
f-line, is out of paper
, etc.
The output port transmits control information to the printer such as
whether data is available to print.
The first thing to learn about the input/output subsystem is that I/O in a typical computer system is radically
dif
ferent than I/O in a typical high level programming language. In a real computer system you will rarely find
machine instructions that behave like
writeln
,
cout
,
printf
, or even the HLA
stdin
and
stdout
statements. In fact,
most input/output instructions behave exactly like the 80x86
s MOV
instruction.
T
o send data to an output
device, the CPU simply moves that data to a special memory location.
T
o read data from an input device, the
CPU simply moves data from the address of that device into the CPU. Other than there are usually more wait
states associated with a typical peripheral device than actual memory
, the input or output operation looks very
similar to a memory read or write operation.
Page
328
A
A
A
7.3
Read-Only, Write-Only, Read/Write, and Dual I/O Ports
W
e can classify input/output ports into four categories based on the CPU
s ability to read and write data at a
given port address.
These four categories are read-only ports, write-only ports, read/write ports, and dual I/O
ports.
A
read-only port is (obviously) an input port. If the CPU can only read the data from the port, then that port
is providing data appearing on lines external to the CPU.
The system typically ignores any attempt to write data
to a read-only port
1
.
good example of a read-only port is the status port on a PC
s parallel printer interface.
Reading data from this port lets you test the current condition of the printer
.
The system ignores any data written
to this port.
A write-only port is always an output port. Writing data to such a port presents the data for use by an external
device. Attempting to read data from a write-only port generally returns garbage (i.e., whatever values that just
happen to be on the data bus at that time). You generally cannot depend on the meaning of any value read from a
write-only port.
A read/write port is an output port as far as the outside world is concerned. However, the CPU can read as
well as write data to such a port. Whenever the CPU reads data from a read/write port, it reads the data that was
last written to the port. Reading the port does not affect the data the external peripheral device sees, reading the
port is a simple convenience for the programmer so that s/he doesn t have to save the value last written to the
port should they want to retrieve the value.
A dual I/O port is also a read/write port, but reading the port reads data from some external device while writ-
ing data to the port transmits data to a different external device. Figure 7.3 shows how you could interface such
a device to the system. Note that the input and output ports are actually a read-only and a write-only port that
share the same address. Reading the address accesses one port while writing to the address accesses the other
port. Essentially, this port arrangement uses the R/W control line(s) as an extra address bit when selecting these
ports.
1. Note, however, that some devices may fail if you attempt to write to their corresponding input ports, so it s never a good
idea to write data to a read-only port.
Page
329
A
CPU write control line
W
L
a
t
c
h
Address decode line
En
Data to the
outside world
Data Bus from CPU
Data
Data Bus
CPU read control line
R
L
a
t
c
h
Address decode line
En
Data from the
outside world
Data Bus to CPU
Data
Figure 7.3
An Input and an Output Device That Share the Same Address (a Dual I/O Port)
These examples may leave you with the impression that the CPU always reads and writes data to peripheral
devices using data on the data bus (that is, whatever data the CPU places on the data bus when it writes to an out-
put port is the data actually written to that output port). While this is generally true for input ports (that is, the
CPU transfers input data across the data bus when reading data from the input port), this isn t necessarily true for
output ports. In fact, a very common output mechanism is simply accessing a port. Figure 7.4 provides a very
simple example. In this circuit, an address decoder decodes two separate addresses. Any access (read or write)
to the first address sets the output line high; any read or write of the second address clears the output line. Note
that this circuit ignores the data on the CPU s data lines. It is not important whether the CPU reads or writes data
to these addresses, nor is the data written of any consequence. The only thing that matters is that the CPU access
one of these two addresses.
Address decode line #1
S
Q
Single bit output
to the outside
world.
Address decode line #2
R
S/R
Flip-Flop
Figure 7.4
Outputting Data to a Port by Simply Accessing That Port
Another possible way to connect an output port to the CPU is to use a D flip-flop and connect the read/write
status lines to the D input on the flip-flop. Figure 7.5 shows how you could design such a device. In this dia-
gram any read of the selected port sets the output bit to zero while a write to this output port sets the output bit to
one.
Page 330
Address decode line #1
Clk
Q
Single bit output
to the outside
world.
Read control line
(active low)
D
D
Flip-Flop
Figure 7.5
Outputting Data Using the Read/Write Control as the Data to Output
There are a wide variety of ways you can connect external devices to the CPU. This section only provides a
few examples as a sampling of what is possible. In the real world, there are an amazing number of different ways
that engineers connect external devices to the CPU. Unless otherwise noted, the rest of this chapter will assume
that the CPU reads and writes data to an external device using the data bus. This is not to imply that this is the
only type of I/O that one could use in a given example.
7.4 I/O (Input/Output) Mechanisms
There are three basic forms of input and output that a typical computer system will use: I/O-mapped I/O,
memory-mapped I/O, and direct memory access (DMA). I/O-mapped input/output uses special instructions to
transfer data between the computer system and the outside world; memory-mapped I/O uses special memory
locations in the normal address space of the CPU to communicate with real-world devices; DMA is a special
form of memory-mapped I/O where the peripheral device reads and writes data in memory without going
through the CPU. Each I/O mechanism has its own set of advantages and disadvantages, we will discuss these in
this section.
7.4.1 Memory Mapped Input/Output
A memory mapped peripheral device is connected to the CPU s address and data lines exactly like memory,
so whenever the CPU reads or writes the address associated with the peripheral device, the CPU transfers data to
or from the device. This mechanism has several benefits and only a few disadvantages.
The principle advantage of a memory-mapped I/O subsystem is that the CPU can use any instruction that
accesses memory to transfer data between the CPU and a memory-mapped I/O device. The MOV instruction is
the one most commonly used to send and receive data from a memory-mapped I/O device, but any instruction
that reads or writes data in memory is also legal. For example, if you have an I/O port that is read/write, you can
use the ADD instruction to read the port, add data to the value read, and then write data back to the port.
Of course, this feature is only usable if the port is a read/write port (or the port is readable and you ve speci-
fied the port address as the source operand of your ADD instruction). If the port is read-only or write-only, an
instruction that reads memory, modifies the value, and then writes the modified value back to memory will be of
little use. You should use such read/modify/write instructions only with read/write ports (or dual I/O ports if
such an operation makes sense).
Nevertheless, the fact that you can use any instruction that accesses memory to manipulate port data is often
a big advantage since you can operate on the data with a single instruction rather than first moving the data into
the CPU, manipulating the data, and then writing the data back to the I/O port.
Page 331
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
IntroductionToProcedures.pdf
(400 KB)
AoAIX.pdf
(1811 KB)
AdvancedArithmetic.pdf
(882 KB)
AoATOC2.pdf
(765 KB)
AdvancedControlStructures.pdf
(302 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin