Peripheral Devices and I/O
Peripheral devices are the means of communication between the CPU and the outside world. As
human beings, we are accustomed to the interface between ourselves and the devices. For
instance, we know that to operate a keyboard, we press the keys; to operate a mouse, we place a
hand over it and move it or press its buttons. These devices must also communicate with the
CPU. The information we enter via an input device (such as a keyboard or mouse) must
somehow get past the device and into the registers on the CPU or into memory where the CPU
can gain access to it. Similarly, the information that the CPU has must make its way from the
CPU to output devices such as a printer or the monitor. For this to happen, the CPU must be able
to detect when an input device has information for it. An output device must be able to signal to
the CPU when it is ready to process more information so that the CPU can send more
information to it. In other words, these devices need the CPU to perform services for them.
A primitive, but simple means for the CPU to determine when a device needs a service is to poll
the device. In essence, the CPU must periodically check the status of each device to see if it
needs attention. Typically, the CPU checks these devices in a particular order. For example, the
CPU may check a timer to see if it is time to switch programs in a multitasking system. If the
timer does not need service, it may check the monitor next, then the keyboard, then the mouse,
etc. This check is time consuming and often useless. Many times, there is no device that needs
service. The CPU has taken time away from program execution to check on devices that are idle.
Furthermore, it may be that a device does need service, but if it is one of the last in the ordering,
the CPU wastes time checking on all the devices that are "ahead" of it before it gets to the one
that needs attention.
Hardware Interrupts
A more efficient means of servicing the needs of peripheral devices is by having a device itself
signal to the CPU that it needs service. This allows the CPU to continue executing instructions
until a device needs service. In other words, the device interrupts the fetch/execute cycle when it
needs the attention of the CPU. This is the idea behind hardware interrupts. Hardware interrupts
are exceptions caused by hardware external to the CPU, such as peripheral devices or a reset
switch. Hardware interrupts occur because the external device needs the attention of the CPU in
order to carry out some task. For example, imagine that a communications program requests
information from a modem. The modem receives one bit at a time on an incoming telephone line,
since it is a serial device. These bits are stored in a buffer (an 8-bit register) until the modem
receives a complete character, rather than sending the bits to the CPU one bit at a time. Since the
data bus can carry 16 bits at once, it would be wasteful to make eight trips, using only one bit of
the data bus for each trip. When the buffer is full, then, the CPU must assist in transferring the
buffer contents to memory. That is, once the buffer fills up, the modem's controller must
generate an interrupt to alert the CPU and let it know that it needs a service (in this case,
emptying the data buffer).
Interrupts are asynchronous. That is, they occur at any arbitrary time (whenever a device actually
needs the intervention of the CPU); they are unpredictable. Rather than have the CPU
continually check each device to determine whether the device needs the CPU to perform some
service, the CPU can continue its work until a device calls for its attention.