CPU: Programming input and output:
The basic techniques for I/O programming can be understood relatively independent of the instruction set. In this section, we cover the basics of I/O programming and place them in the contexts of both the ARM and C55x.
We begin by discussing the basic characteristics of I/O devices so that we can understand the requirements they place on programs that communicate with them.
1. Input and Output Devices:
Input and output devices usually have some analog or non electronic component for instance, a disk drive has a rotating disk and analog read/write electronics. But the digital logic in the device that is most closely connected to the CPU very strongly resembles the logic you would expect in any computer system.
Figure 1.18 shows the structure of a typical I/O device and its relationship to the CPU.The interface between the CPU and the device’s internals (e.g.,the rotating disk and read/write electronics in a disk drive) is a set of registers. The CPU talks to the device by reading and writing the registers.
Devices typically have several registers:
· Data registers hold values that are treated as data by the device, such as the data read or
written by a disk.
Status registers provide information about the device’s operation, such as whether the current transaction has completed.
Some registers may be read-only, such as a status register that indicates when the device is done, while others may be readable or writable.
2. Input and Output Primitives:
Microprocessors can provide programming support for input and output in two ways: I/O instructions and memory-mapped I/O.
Some architectures, such as the Intel x86, provide special instructions (in and out in the case of the Intel x86) for input and output. These instructions provide a separate address space for I/O devices.
But the most common way to implement I/O is by memory mapping even CPUs that provide I/O instructions can also implement memory-mapped I/O.
As the name implies, memory-mapped I/O provides addresses for the registers in each I/O device. Programs use the CPU’s normal read and write instructions to communicate with the devices.
3. Busy-Wait I/O:
The most basic way to use devices in a program is busy-wait I/O. Devices are typically slower than the CPU and may require many cycles to complete an operation. If the CPU is performing multiple operations on a single device, such as writing several characters to an output device, then it must wait for one operation to complete before starting the next one. (If we try to start writing the second character before the device has finished with the first one, for example, the device will probably never print the first character.) Asking an I/O device whether it is finished by reading its status register is often called polling.