Hardware Reference
In-Depth Information
Character available
Ready for next character
Display status
Keyboard status
Interrupt enabled
Interrupt enabled
Keyboard buffer
Display buffer
Character received
Character to display
Figure 5-30. Device registers for a simple terminal.
(interrupts will be discussed shortly). When using programmed I/O, to get input,
the CPU normally sits in a tight loop, repeatedly reading the keyboard status regis-
ter, waiting for bit 7 to go on. When this happens, the software reads in the
keyboard buffer register to get the character. Reading the keyboard data register
causes the CHARACTER AVAILABLE bit to be reset to 0.
Output works in a similar way. To write a character to the screen, the software
first reads the display status register to see if the READY bit is 1. If not, it loops
until the bit goes to 1, indicating that the device is ready to accept a character. As
soon as the terminal is ready, the software writes a character to the display buffer
register, which causes it to be transmitted to the screen, and also causes the device
to clear the READY bit in the display status register. When the character has been
displayed and the terminal is prepared to handle the next character, the READY bit
is automatically set to 1 again by the controller.
As an example of programmed I/O, consider the Java procedure of Fig. 5-31.
It is called with two parameters: a character array to be output, and the count of
characters present in the array, up to 1K. The body of the procedure is a loop that
outputs characters one at a time. For each character, first the CPU must wait until
the device is ready, then the character is output. The procedures in and out would
typically be assembly-language routines to read and write the device registers spec-
ified by the first parameter from or to the variable specified as the second parame-
ter. The implicit division by 128 by shifting gets rid of the low-order 7 bits, leav-
ing the READY bit in bit 0.
The primary disadvantage of programmed I/O is that the CPU spends most of
its time in a tight loop waiting for the device to become ready. This approach is
called busy waiting . If the CPU has nothing else to do (e.g., the CPU in a washing
machine), busy waiting may be OK (though even a simple controller often needs to
monitor multiple, concurrent events). However, if there is other work to do, such
as running other programs, busy waiting is wasteful, so a different I/O method is
needed.
The way to get rid of busy waiting is to have the CPU start the I/O device and
tell it to generate an interrupt when it is done. Looking at Fig. 5-30, we show how
 
Search WWH ::




Custom Search