CMPUT 229 Fall 2003 Topic 8 InputOutput Programming

  • Slides: 11
Download presentation
CMPUT 229 - Fall 2003 Topic 8: Input/Output Programming José Nelson Amaral CMPUT 229

CMPUT 229 - Fall 2003 Topic 8: Input/Output Programming José Nelson Amaral CMPUT 229 - Computer Organization and Architecture I 1

Reading Material The slides for this topic are based on the Chapter 11 of

Reading Material The slides for this topic are based on the Chapter 11 of the following book: Goodman, James and Miller Karen, A Programmer’s View of Computer Architecture with Assembly Language Examples from the MIPS RISC Architecure, Oxford University Press, 1993. CMPUT 229 - Computer Organization and Architecture I 2

Memory Mapped I/O In machines that use memory mapped I/O, a region of the

Memory Mapped I/O In machines that use memory mapped I/O, a region of the memory space is a collection of communication channels to I/O devices. Loads and stores from/to these addresses is used to communicate with the I/O hardware. Special care must be taken when writing to a memory mapped I/O device, because the device may not be available sometimes. For instance: - an input device only can supply data after a character is typed; - an output device may require some time to dispose of one character before it can accept the next. CMPUT 229 - Computer Organization and Architecture I 3

Memory Mapped I/O (cont. ) Tipically an I/O device has an address that can

Memory Mapped I/O (cont. ) Tipically an I/O device has an address that can be read by the processor to indicate whether the device is ready or busy. Usually two types of addresses are provided for the processor to communicate with the I/O device: read only address: the processor can read data from this address but cannot write to it. write only address: the processor can send data to this address but cannot read from it. CMPUT 229 - Computer Organization and Architecture I 4

A Memory Mapped Keyboard For instance, lets consider a memory mapped keyboard that provides

A Memory Mapped Keyboard For instance, lets consider a memory mapped keyboard that provides two read only addressed: Keyboard. Status: When this address is read, the bit 31 indicates if the keyboard is ready: bit 31 = 1: keyboard is ready bit 31 = 0: keyboard is not ready Keyboard. Data: Returns the value of the keystroke Read Keyboard Assembly: Waitloop: lw bge lw $t 6, Keyboard. Status $t 6, $zero, Wait. Loop $v 0, Keyboard. Data CMPUT 229 - Computer Organization and Architecture I 5

A Memory Mapped Display In an output device, such as a display, the processor

A Memory Mapped Display In an output device, such as a display, the processor needs to ensure that the first character was processed before the second can be sent: Lets consider that a display provides the following addresses: Display. Status: When this address is read, the bit 31 indicates if the keyboard is ready: bit 31 = 1: display is ready bit 31 = 0: display is busy Display. Data: Receives the character to be displayed Write to Display Assembly: Waitloop: lw bge sw $t 6, Display. Status $t 6, $zero, Wait. Loop $v 0, Display. Data CMPUT 229 - Computer Organization and Architecture I 6

Busy-Waiting or Spin. Waiting In both previous examples the processor is busy-waiting, or spin-waiting

Busy-Waiting or Spin. Waiting In both previous examples the processor is busy-waiting, or spin-waiting on the I/O devices. The problem is that the I/O devices are much slower than the processor, thus it is likely that the processor will be tight up with the I/O for long periods of time. Another problem is that an input device might have a character ready while the processor is busy doing other things. You would be ignored by a computer that has nothing better to do!!! CMPUT 229 - Computer Organization and Architecture I 7

A Very Simple Asynchronous I/O In the interest of simplicity, we will define a

A Very Simple Asynchronous I/O In the interest of simplicity, we will define a very simple asynchronous I/O system. In this system, when a string has to be printed to a display, the procedure printstring is called. But printstring does not send any character to the display. Instead, it adds the string to a queue. printstring then calls another procedure, putnextchar checks if the display is ready, if it is putnextchar prints the first character and returns immediately. If the display is not ready, putnextchar simply returns. The limitation in this system is that putnextchar has to be called periodically (a polling mechanism). CMPUT 229 - Computer Organization and Architecture I 8

putnextchar code Read Display Status Ready? no return load head pointer load tail pointer

putnextchar code Read Display Status Ready? no return load head pointer load tail pointer Queue Empty? yes increment head pointer read char. from queue write char. to display return putnextchar Assembly: . data putqueue. space 256 pqtail: . word 0 pqhead: . word 0. text putnextchar: lw $k 0, Display. Status bge $k 0, $zero, putret lw $k 0, pqhead lw $k 1, pqtail beq $k 0, $k 1, putret addi $k 0, 1 andi $k 0, 0 x 00 ff sw $k 0, pqhead lb $k 0, putqueue($k 0) sb $k 0, Display. Data putret: jr $ra return CMPUT 229 - Computer Organization and Architecture I 9

A simple Keyboard Interface Similarly, we can design a simple keyboard interface. A getnextchar

A simple Keyboard Interface Similarly, we can design a simple keyboard interface. A getnextchar rotine is called regularly to get characters from the keyboard and store in a getqueue. When a function foo needs something from the keyboard, foo would get it from the getqueue. If the getqueue is empty, then foo can spin-wait calling getnextchar. CMPUT 229 - Computer Organization and Architecture I 10

getnextchar code Read Keyboard Status Ready? no return load head pointer load tail pointer

getnextchar code Read Keyboard Status Ready? no return load head pointer load tail pointer increment tail pointer Queue Full? yes read char. from keyboard put char. into queue return getnextchar Assembly: . data getqueue. space 256 gqtail: . word 0 gqhead: . word 0. text getnextchar: lw $k 0, Keyboard. Status bge $k 0, $zero, getret lw $k 0, gqhead lw $k 1, gqtail addi $k 1, 1 andi $k 1, 0 x 00 ff beq $k 0, $k 1, getret sw $k 1, gqtail lb $k 0, Keyboard. Data sb $k 0, getqueue($k 1) getret: jr $ra return CMPUT 229 - Computer Organization and Architecture I 11