Unit Ii Memory and Input / Output Management Input and Output Devices
Unit Ii Memory and Input / Output Management Input and Output Devices
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.
The following example illustrates busy-wait I/O.
Example-: Copy a string of characters to the output device:
Busy-wait I/O programming:
In this example we want to write a sequence of characters to an output device. The device
has two registers: one for the character to be written and a status register. The status
registers value is 1 when the device is busy writing and 0 when the write transaction has
completed. We will use the peek and poke functions to write the busy-wait routine in C.
The outer while loop sends the characters one at a time. The inner while loop checks the
device statusit implements the busy-wait function by repeatedly checking the device
status until the status changes to 0.
Copying characters from input to output using busy-wait I/O:
We want to repeatedly read a character from the input device and write it to the output
device. First, we need to define the addresses for the device registers:
#define IN_DATA 0x1000
#define IN_STATUS 0x1001
#define OUT_DATA 0x1100
#define OUT_STATUS 0x1101
The input device sets its status register to 1 when a new character has been read; we must
Set the status register back to 0 after the character has been read so that the device is
ready to read another character. When writing, we must set the output status register to 1
to start writing and wait for it to return to 0. We can use peek and poke to repeatedly
perform the read/write operation:
while (TRUE) { /* perform operation forever */
/* read a character into achar */
while (peek(IN_STATUS) == 0); /* wait until ready */
achar = (char)peek(IN_DATA); /* read the character */
/* write achar */
poke(OUT_DATA,achar);
poke(OUT_STATUS,1); /* turn on device */
while (peek(OUT_STATUS) != 0); /* wait until done */
}
Every character starts with a start bit (a 0) and a stop bit (a 1). The start bit allows the
receiver to recognize the start of a new character; the stop bit ensures that there will be a
transition at the start of the stop bit. The data bits are sent as high and low voltages at a
uniform rate. That rate is known as the baud rate; the period of one bit is the inverse of
the baud rate. Before transmitting or receiving data, the CPU must set the UARTs mode
registers to correspond to the data lines characteristics. The parameters for the serial port
are as follows,
the baud rate;
the number of bits per character (5 through 8);
whether parity is to be included and whether it is even or odd; and
The length of a stop bit (1, 1.5, or 2 bits).
The UART includes one 8-bit register that buffers characters between the UART and the
CPU bus. The Transmitter Ready output indicates that the transmitter is ready to accept a
data character; the Transmitter Empty signal goes high when the UART has no characters
to send. On the receiver side, the Receiver Ready pin goes high when the UART has a
character ready to be read by the CPU.