Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic8: Input/Output Programming José Nelson Amaral.

Similar presentations


Presentation on theme: "CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic8: Input/Output Programming José Nelson Amaral."— Presentation transcript:

1 CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic8: Input/Output Programming José Nelson Amaral

2 CMPUT 229 - Computer Organization and Architecture I2 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.

3 CMPUT 229 - Computer Organization and Architecture I3 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.

4 CMPUT 229 - Computer Organization and Architecture I4 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.

5 CMPUT 229 - Computer Organization and Architecture I5 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

6 CMPUT 229 - Computer Organization and Architecture I6 A Memory Mapped Display 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 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:

7 CMPUT 229 - Computer Organization and Architecture I7 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!!!

8 CMPUT 229 - Computer Organization and Architecture I8 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).

9 CMPUT 229 - Computer Organization and Architecture I9 putnextchar code putnextchar Assembly:.data putqueue.space256 pqtail:.word0 pqhead:.word0.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 Ready? Read Display Status load head pointer load tail pointer Queue Empty? increment head pointer read char. from queue write char. to display return no return yes

10 CMPUT 229 - Computer Organization and Architecture I10 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.

11 CMPUT 229 - Computer Organization and Architecture I11 getnextchar code getnextchar Assembly:.data getqueue.space256 gqtail:.word0 gqhead:.word0.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 Ready? Read Keyboard Status load head pointer load tail pointer increment tail pointer Queue Full? read char. from keyboard put char. into queue return no return yes


Download ppt "CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic8: Input/Output Programming José Nelson Amaral."

Similar presentations


Ads by Google