2Motivation I/O devices make computers useful to humans. Input devices may be arbitrarily slow and must have a way to tell the computer that they are ready to supply data.Output devices must have a way of refusing more data until they are ready to receive more.
3General Categories of I/O User interface devices. Input devices detect changes and output devices changes the physical world.Input devices: mouse, keyboardOutput devices: terminal display, printerMass storage devices. These devices hold large quantities of data. For example, disks and tape drives.Gateways and networks. Computers communicate with each other and these communication exhibit delays relative to computer speed. Networks can be treated as if they were conventional I/O devices.
4The KeyboardThe keyboard detects when keys are pressed and sends a designated sequence of characters to the computer. A table is used to map the keys to characters.Some keys like SHIFT, CTRL, ALT, and META either change the mapping of the keyboard or modify the effect of other keys.The bits are usually sent serially at a rate given in bits per second (bit rate).
5The keyboard must have a way of indicating to the computer that it has a character to send. This is done using extra bits called start bit and stop bit. The start bit signals the beginning of a transmission and the stop bit signals its end.The computer then knows that data is going to follow after receiving the start bit. The following sequence of bits are saved to an input register. Both start and stop bits are stripped off at the receiving end.In MAL, we can access the input via $v0 or $2.
6The Video DisplayEach time a printable character is sent to the display it reacts by displaying the character in the position where the cursor was, and moving the cursor one position to the right.A terminal consists of a display unit and a keyboard. Thus it is both an input and an output device.When cr is received, the cursor moves to beginning of the current line and does not scroll.When nl is received, scrolling is initiated but the cursor does not move. Combining both moves the cursor to the beginning of the next line.
7Hard DiskThe disk must first be informed where the data is to be read or written. The disk’s read/write arm seeks to the proper cylinder and rotates to the correct sector.When the read/write arm is in proper position it then informs the computer that it is ready to receive or transmit.At this point it can either transmit data or write data.
8Hardware: I/O instructions The hardware implements special instructions to read from/write to I/O devices.Perhaps lwio (load word I/O) and swioThe devices have a separate address space from memorylwio $s0, 16 and lw $s0, 16 access different datalwio $s0, 16 reads the next word from device 16lw $s0, 16 reads the word stored in memory location 16.MIPS does NOT use special I/O instructions
9Memory-Mapped I/OThe hardware can be designed in such a way that some memory addresses are not really memory at all but a collection of communication channels to I/O devices.In memory-mapped I/O, load and store from/to the communication channels provide a means of performing I/O, i.e., load and store instructions with an I/O address are treated as I/O operations.lw $2, KeyboardDatasw $5, DisplayDatacommunicationchannels
10MIPS RISC architecture uses memory-mapped I/O MIPS RISC architecture uses memory-mapped I/O. Note that the hardware must support this.Memory-mapped I/O is one method by which the processor and I/O devices communicate via a common bus. In this case, one common bus for memory and I/O are used with common control lines.The I/O devices unlike memory may unavailable. If a device is not ready to accept or transmit data then it is said to be not ready or busy.The processor communicates with I/O using two addresses: one for data exchange and the other to obtain status of the I/O device.
12Memory mapped addresses in SPIM Define the memory mapped constants like this:.data 0xffff0000keyboard_control: .space 4keyboard_data: .space 4display_control: .space 4display_data: .space 4
13Programmed I/O In programmed I/O, the CPU stays in a loop until the I/O unit indicates that is ready for data transfer orif the CPU has issued a command to the I/O moduleit must wait until the operation is complete.wait: lw $14,keyboardstatusbeqz $14,waitlw $2,keyboardDatawait: lw $14,displaystatussw $2,displayData1 = ready0 = busymost significant bit
14Direct Memory Access (DMA) A controller is a device that has registers and can execute routines on its own given necessary parameters.A DMA controller is given by the CPU the starting address into which a block of data will be transferred and the amount of data to be transferred. It also has a control register and a status register.The DMA controller requests memory cycle from the memory controller. During a DMA transfer the CPU executes from cache while control of the memory bus is given to the DMA controller
15A code a DMA controller might execute .datacount: .word 1024begin_file: .space 1024.textinitialize:lw $15,count #number of charactersla $16,begin_file #address where to storewaitloop:lw $14,diskstatusbeqz $14, waitlooplb $2,diskdata #read a charactersb $2,($16) #store into the arraysub $15,1 #decrement counteradd $16,1 #adjust array pointerbgtz $15,waitloop
16Exception MechanismInstead of spin-waiting or doing a regular check whether an I/O device is ready, the I/O device can just inform the CPU that it is ready to receive or transmit data. The I/O device sends an interrupt signal to the system.In this mechanism, control is transferred to a different program which saves the current state of the interrupted program. The requests are then serviced and control is given back to the interrupted program. This mechanism is called an exception.
17An interrupt is one class of exception. An interrupt can occur at any time.Hardware and software are needed to support interrupt handling. The hardware must choose the appropriate time in which to interrupt the executing program and transfers control to an exception handler.The exception handler must save the current state of the interrupted program.The exception handler also determines which event has caused the exception and decides what should be done based on it.
18Since an exception handler can be invoked anytime, an exception handler can not have parameters nor it can return values.It must also save register values being used by the interrupted program and restore them before returning control to the interrupted program.
19Software: Interrupt Driven I/O Exceptions come in two varietiesInterrupts are generated by hardwareI/O deviceClockPower downTraps are generated by code executionDivision by zeroIllegal memory addressSystem call
20How interrupt driven I/O works User code/dataKernel dataDisplay.text.la $a0, Ali $v0, 4syscall.dataA:.asciiz “cat”Output buffer
21How interrupt driven I/O works User code/dataKernel dataDisplay.text.la $a0, Ali $v0, 4syscall.dataA:.asciiz “cat”Output bufferc a t
22How interrupt driven I/O works User code/dataKernel dataDisplay.text.la $a0, Ali $v0, 4syscall.dataA:.asciiz “cat”Output buffercc a t
23How interrupt driven I/O works User code/dataKernel dataDisplay.text.la $a0, Ali $v0, 4syscall.dataA:.asciiz “cat”Output buffercac a t
24How interrupt driven I/O works User code/dataKernel dataDisplay.text.la $a0, Ali $v0, 4syscall.dataA:.asciiz “cat”Output buffercatc a t
25Role of the Operating System The operating system is a program that allocates and controls the use of all system resources: the processor, memory, and I/O devices.Since there are many processes that can run concurrently, the operating system uses interrupt to allocate the processor to different processes periodically -- allowing processes to share processing time with each other.The exception handler plus other codes used to decide what process should be executed next is called the kernel.
26In saving the current values of registers in MIPS RISC architecture, addresses needed to be formed to store them into memory.The formation of addresses requires the use of registers -- thus $26 and $27 are reserved for the operating system so that when an interrupt is to be serviced there is no need to save the contents of these two registers.The other type of exception is a trap. A trap occurs when an event happens as a direct result of executing a program, e.g. caused by an overflow or an attempt to access a memory outside of the legal range.