A computer system has three basic components:
- CPU
- Memory
- Input-Output (I/O) devices (or, peripherals)
Up until now, we have mainly concerned with the first two of these components, namely the CPU and the memory, and their interfacing. In this chapter we will turn our attention to the third component, namely the I/O devices.
Introduction to Peripherals
In order to communicate with the external world, a CPU/memory system must interact with an outside device:
CPU “reads/writes”
- If the CPU/memory system is to display some results to the user, it must be connected to a 7-segment display or a VGA/HDMI monitor.
- If data is to be entered, a switchboard or keypad must be connected to the CPU/memory system.
- An electric motor can be connected to a CPU/memory system. Then the CPU can control the motor’s position or speed.
- A printer can be connected to a CPU/memory system. It can be used by the CPU to print messages.
- An IDE disk or USB storage device can be connected to a CPU/memory system to store/read data to/from main memory.
- A thermometer can be connected to a CPU/memory system for the CPU to read the temperature.
- A tachometer can be connected to a CPU/memory system for the CPU to read the speed of a vehicle.
Without any connections to peripherals, a CPU/memory system is quite useless. If is deaf and blind, as it cannot interact with the outside world. On the other hand, with connections to peripherals, it can receive demands from outside world and respond intelligently. Let us give some examples:
- If a CPU/memory system is connected to a keypad and a set of 7-segment displays, we will have a pocket calculator. The user can enter mathematical operations from the keypad and reads the results from the 7-segment displays. Actually, we will build such a calculator very shortly.
- If a CPU/memory system is connected to a keyboard and a VGA monitor, we will have the hardware for a minimal computer (no, hard disk is not strictly required. Actually some early computers like Commodore 64, Sinclair or Atari didn’t have one.) We will build one such computer also. Hint: the hard part is not constructing the hardware but writing an operating system for it.
- A CPU/memory system can be connected to a temperature sensor and an electric motor with a fan on its shaft. The CPU will constantly monitor the temperature and turns on the electric motor if the temperature goes above a certain threshold. It will do otherwise if the temperature falls below a threshold. Also, the rotation speed of the fan may be made dependent on the temperature.
- In an electric car, a CPU/memory system can be connected to a tachometer and an electric motor of the car. If the car’s speed drops under a given threshold (this generally happens when the car climbs an incline), the CPU may increase the voltage on the electric motor to speed up the car. Conversely, when the car’s speed exceeds a given threshold (this generally happens when the car goes down a slope), the CPU may decrease the voltage on the electric motor to slow down the car. This way, we obtain a cruise control system.
Host Adapters: Or, how does the CPU sees a peripheral device?
How does a cpu “reads” a thermometer or “commands” an electric motor to turn at a certain speed? Apparently there should be some hardware that stands between the CPU and the relevant peripheral and translate them to each other. Such components are called as “host adapters” or “host controllers”. “Host” here indicates the part of a computer system to which peripherals are attached. For us, “host” will denote the CPU/memory system which we have already designed in the previous chapters. Note that the word “host” can take slightly different meanings in other contexts.
To understand the interaction between a CPU and a peripheral, we have to notice that this interaction takes two forms:
- CPU can send data/commands to a peripheral. For example, it can send a number to a 7-segment display to be displayed.
- CPU can read data from a peripheral: CPU can read the temperature from the temperature sensor, or currently pressed key from the keypad.
The main “magic” of a host adapter is to make the CPU see the peripheral as a series of memory locations with certain definite addresses.
- When the CPU wants to send a command/data to a peripheral, it just writes this data to a certain memory address with a usual LD instruction. This “memory location” is actually not a real memory location but a register in the host adapter. Then, host adapter converts this data into a format which can be understood by the peripheral and drives the peripheral with it.
- When a peripheral has ready data, it sends it to the host controller. Host controller converts it to a binary number and puts it into one of its registers. CPU reads it from there by a regular LD instruction.
This technique is known as “memory mapped input output (MMIO)”.
We will first try to understand how MMIO works in software without going into any hardware details. This can be best illustrated with some examples:
Up till now, the exact nature of how cpu “reads” an electric motor or thermometer remained in the dark.
main computer usb port — host adapter is behind.