Practical Session No. 10 Input &Output (I/O) I/O Devices Input/output (I/O) devices provide the means to interact with the “outside world”. An I/O device can be purely input, purely output, or both an input and output device (e.g. mouse, screen, disks). Are mostly used to communicate with the outside world, and to store data. Controller I/O devices are not directly connected to the system bus. Instead, there is usually an I/O controller that acts as an interface between the system and the I/O device. Reasons for using an I/O controller Different devices exhibit different characteristics. The processor would spend a lot of time for interaction. Controller could provide the necessary low-level commands and data for proper operation Reasons for using an I/O controller The amount of electrical power used to send signals on the system bus is very low Controllers typically contain driver hardware to send current over long cables I/O Controller Typically has 3 internal registers: ◦ Data register ◦ Command register ◦ Status register Processor interacts with an I/O device via the associated I/O controller I/O device interface to the system Communication (character output) Before the processor sends output character, it has to first check the status register of the associated controller (e.g. busy, idle, offline). The data register holds the character (e.g. to be printed). The command register determines the operation requested by the processor (e.g. send the character in the data register to the printer). Communication (character output) Sequence of operations: ◦ Wait for the controller to finish the last command; ◦ Place a character to be printed in the data register; ◦ Set the command register to initiate the transfer. I/O Ports An I/O port is the address of a register associated with an I/O controller. Two kinds of mapping: ◦ memory-mapped I/O: writing to an I/O port is similar to writing to a memory address. ◦ I/O address space: separated from the memory address space. Special I/O instructions are needed for I/O address space map. Pentium provides two instructions: in and out, to access I/O ports. I/O Ports Pentium provides 64 KB of I/O address space. This address space can be used for 8-bit,16-bit, and 32-bit I/O ports. A combination cannot be more than the I/O address space. For example, we can have 64-K 8-bit ports, 32-K 16-bit ports, 16-K 32-bit ports, or a combination of these that fits the 64-K address space. Register I/O Instructions The in instruction is used to read data from an I/O port: ◦ in accumulator, port8 (direct address) ◦ in accumulator, DX (indirect address) The out instruction to write data to an I/O port: ◦ out port8, accumulator (direct address) ◦ out DX, accumulator (indirect address) accumulator must be AL, AX, or EAX. port8 - access the first 256 ports 0..FFH. 8255 Programmable Peripheral Interface (PPI) Chip Provides three 8-bit registers to interface with I/O devices: Register ◦ ◦ ◦ ◦ Port address PA (input port) PB (output port) PC (input port) Command register 60H 61H 62H 63H 8255 Programmable Peripheral Interface (PPI) Chip Keyboard interface is provided by port PA and PB7. Keyboard sends an interrupt (to other interrupt controller) whenever a change occurs (e.g. key is pressed). A scan code of the key whose state has changed is written in PA. Keyboard waits for an acknowledge signal from CPU in PB7 Register Bit Map of 8255 Keyboard scan code if PB7 = 0 ◦ PA7 = 0 if a key is depressed ◦ PA7 = 1 if a key is released ◦ PA0–PA6 = key scan code Configuration switch 1 if PB7 = 1 PB7 — selects source for PA input ◦ 0— keyboard scan code ◦ 1— configuration switch 1 ◦ Also, 1 is used as keyboard acknowledge Keyboard Driver Uses busy wait loop. Pressing the esc key terminates the program. Waits for the PA7 bit to go low to indicate that a key is depressed. Scan code is read from PA6 to PA0 Keyboard Driver section .data ESC_KEY EQU 1BH KB_DATA EQU 60H ; ASCII code for ESC key ; 8255 port PA section .text global _start _start: key_up_loop: ;Loops until a key is pressed i.e., until PA7 = 0. ; PA7 = 1 if a key is up. in AL, KB_DATA ; read keyboard status & scan code test AL, 80H ; PA7 = 0? jnz key_up_loop ; if not, loop back Keyboard Driver and AL,7FH ; isolate the scan code ..Translate scan code to ASCII code in AL.. cmp AL,0 je key_down_loop ; ASCII code of 0 => uninterested key cmp AL,ESC_KEY je done ; ESC key---terminate program display_ch: ; char is now in AL ..Print character AL to screen.. Keyboard Driver key_down_loop: in AL,KB_DATA test AL, 80H jz key_down_loop mov AX,0C00H int 21H ; PA7 = 1? ; if not, loop back ; clear keyboard buffer ; (System interrupt) jmp key_up_loop Done: mov AX,0C00H int 21H ..Exit Program.. ; clear keyboard buffer