110 likes | 259 Views
CMPUT229 - Fall 2003. Topic8: Input/Output Programming José Nelson Amaral. 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
E N D
CMPUT229 - Fall 2003 Topic8: Input/Output Programming José Nelson Amaral CMPUT 229 - Computer Organization and Architecture I
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
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
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
A Memory Mapped Keyboard For instance, lets consider a memory mapped keyboard that provides two read only addressed: KeyboardStatus: 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 KeyboardData: Returns the value of the keystroke Read Keyboard Assembly: Waitloop: lw $t6, KeyboardStatus bge $t6, $zero, WaitLoop lw $v0, KeyboardData CMPUT 229 - Computer Organization and Architecture I
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: DisplayStatus: 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 DisplayData: Receives the character to be displayed Write to Display Assembly: Waitloop: lw $t6, DisplayStatus bge $t6, $zero, WaitLoop sw $v0, DisplayData CMPUT 229 - Computer Organization and Architecture I
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
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. 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
no load head pointer load tail pointer Ready? return yes Queue Empty? return increment head pointer read char. from queue write char. to display return putnextchar code Read Display Status putnextchar Assembly: .data putqueue .space 256 pqtail: .word 0 pqhead: .word 0 .text putnextchar: lw $k0, DisplayStatus bge $k0, $zero, putret lw $k0, pqhead lw $k1, pqtail beq $k0, $k1, putret addi $k0, $k0, 1 andi $k0, $k0, 0x00ff sw $k0, pqhead lb $k0, putqueue($k0) sb $k0, DisplayData putret: jr $ra CMPUT 229 - Computer Organization and Architecture I
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
no Ready? return load head pointer load tail pointer increment tail pointer yes Queue Full? return read char. from keyboard put char. into queue return getnextchar code Read Keyboard Status getnextchar Assembly: .data getqueue .space 256 gqtail: .word 0 gqhead: .word 0 .text getnextchar: lw $k0, KeyboardStatus bge $k0, $zero, getret lw $k0, gqhead lw $k1, gqtail addi $k1, $k1, 1 andi $k1, $k1, 0x00ff beq $k0, $k1, getret sw $k1, gqtail lb $k0, KeyboardData sb $k0, getqueue($k1) getret: jr $ra CMPUT 229 - Computer Organization and Architecture I