I/O

Computers are not useful unless we can put data into them, and get results out.

input -- data to computer
output -- data from computer

Here is the standard computer model in a diagram:

       -----        --------
       |CPU| <----> |memory|
       -----   ^    --------
	       |
	       |
	      \ /

	     -----
	     |i/o|
	     -----

Examples of input devices:
keyboard, mouse, network, disk, scanner, camera, microphone, ??

Examples of output devices:
printer, (terminal) display, network, disk, speakers, ??

Note: We have seen only 2 of our simulator's I/O devices: the keyboard (for input), and the display (for output)

Issues that Must Be Solved:

A Real, Live, Physical Device: the Disk

Vocabulary, to form a picture of a disk:

Platter -- sort of like a phonograph record, or a CD.

Data is stored on a surface of a platter. Each platter can have one or two surfaces.

All platters are tied together and rotate around the spindle at a fixed speed.

Each surface has one or more read/write heads.

Platters are broken down into tracks. A single track is one of many concentric circles on the platter.

All the corresponding tracks on all surfaces, taken together, form a cylinder.

Each track is broken down into sectors.

How We Read or Write to a Sector

Given: the sector position on the cylinder. (looked up in a table, or calculated from the disk address).

Therefore, the time to read a sector = seek time + rotate time + read time.

How Does the OS do I/O?

There are 2 possibilities.

  1. have special I/O instructions
    input
    The OS needs to know which device, how much data, where the data is to go. These would be operands. output
    The OS needs to know which device, how much data, where the data currently is. These would be operands.

    How does the CPU know that the instruction has completed? (Is there any need to wait?)

    What happens if the device encounters an error? (Does this halt the computer?)

  2. the solution of choice

    Overload memory locations to use as communication channels.

    For example,

           address
           0x0000 0000  -|
           .             |   real memory
           .             |
           .             |
           0xffff 0000  -|
    
           0xffff 0008  - data from keyboard (Keyboard_Data)
    
           0xffff 0010  - data to display (Display_Data)
    

    Then, by reading (loading) from location 0xffff0008, data is requested from the keyboard.

    Then, by writing (storing) to location 0xffff0010, data is sent to the display.

    The syscall code within the OS must be (in essence)

         lw  $2, Keyboard_Data     # getc syscall
         return from syscall
    
    and
         sw  $2, Display_Data      # putc syscall
         return from syscall
    

    This method of using overloaded memory locations for communication with I/O devices is called memory mapped I/O.

Problems with memory-mapped I/O as currently given:

What is needed is a way to convey information about the status of I/O devices. This status information is used to coordinate and synchronize the useage of devices.

       address
       0x0000 0000  -|
       .             |   real memory
       .             |
       .             |
       0xffff 0000  -|

       0xffff 0008  - data from keyboard (Keyboard_Data)
       0xffff 000c  - STATUS from keyboard (Keyboard_Status)

       0xffff 0010  - data to display (Display_Data)
       0xffff 0014  - STATUS from display (Display_Status)

Assume that the MSB is used to tell the status of a device.
MSB = 1 means device ready
MSB = 0 means device is busy (not ready)

Note that we can check for device ready/busy by looking to see if the Status word is negative (2's comp) or not.

For the keyboard:
a 1 means that a character has been typed
a 0 means that no character is available

For the display:
a 1 means that a new character may be sent
a 0 means that the device is still disposing of a previous character

Then, the syscall code in the OS must be more like

     getc_loop: lw   $8, Keyboard_Status   # getc syscall
		bgez $8, getc_loop
                lw   $2, Keyboard_Data    
                return from syscall      # back to the user-level application


     putc_loop: lw   $8, Display_Status    # putc syscall
		bgez $8, putc_loop
                sw   $4, Display_Data
                return from syscall      # back to the user-level application

This scheme is known as busy waiting, or spin waiting. The little loop is called a spin wait loop.

Something that is not well explained (at this level) is how these status bits get set and cleared. The spin wait loop reads the status word, but does not change it.

The device (its controller) sets and clears the bit. An implied function is that the device sets the bit when it becomes ready to work on another character.

And, a load from Keyboard_Data also clears the MSB of Keyboard_Status
And, a store to Display_Data also clears the MSB of Display_Status

      -------------                           -------------
      |processor  |  <--------------------->  |  memory   |
      -------------                   |       -------------
                                      |
                                      |
                     controller       |
     -----------      ----------      |
     | display | <--> | Status |<---->|
     -----------      |  Data  |      |
                      ----------      |
                                      |
                                      |
                     controller       |
    ------------      ----------      |
    | keyboard | <--> | Status |<---->|
    ------------      |  Data  |      |
                      ----------      |
                                      |

Note that each device is "hooked up" and operates the same way with respect to the interaction between processor and memory and the device. In fact, any new device that can operate in this manner can be added to this computer system. This is an important feature.

Problems With This Programmed I/O Approach

Next Best Solution: Use Queues

Some problems are solved by the use of queues (buffers). The check for device ready is separated from the sending and receiving of characters.

For the display:

For the keyboard:

Some of the many difficulties caused by this situation:

Copyright © Karen Miller, 2006