170 likes | 349 Views
Computer Science 210 Computer Organization. Input and Output. Connecting to the Outside World. We can now Compute with data in registers Transfer data between registers and memory Make choices and repeat instructions But how do we
E N D
Computer Science 210Computer Organization Input and Output
Connecting to the Outside World • We can now • Compute with data in registers • Transfer data between registers and memory • Make choices and repeat instructions • But how do we • Transfer data between the system and the outside world • Translate it to a form that people can enter or receive
I/O Device Characteristics • Types of I/O devices characterized by: • behavior: input, output, storage • input: keyboard, motion detector, network interface • output: monitor, printer, network interface • storage: disk, CD-ROM, flash stick, etc. • data rate: how fast can data be transferred? • keyboard: 100 bytes/sec • disk: 30 MB/s • network: 1 Mb/s - 1 Gb/s
I/O Controller • Control/Status Registers • CPU tells device what to do -- write to control register • CPU checks whether task is done -- read status register • Data Registers • CPU transfers data to/from device • Device electronics • performs actual operation • pixels to screen, bits to/from disk, characters from keyboard Graphics Controller Control/Status CPU Electronics display Output Data
The Interface to the Devices • How are device registers identified? • Memory-mapped vs. special instructions • How is timing of transfer managed? • Asynchronous vs. synchronous • Who controls transfer? • CPU (polling) vs. device (interrupts)
Memory-Mapped vs Instructions • Instructions • designate opcode(s) for I/O • register and operation encoded in instruction • Memory-mapped • assign a memory address to each device register • use data movement instructions (LD/ST)for control and data transfer
Transfer Timing • Synchronous • data supplied at a fixed, predictable rate • CPU reads/writes every X cycles • Asynchronous • data rate less predictable • CPU must synchronize with device,so that it doesn’t miss data or write too quickly
Transfer Control • Polling • The CPU keeps checking the status register until new data arrives OR device ready for next data • “Are we there yet? Are we there yet? Are we there yet?” • Interrupts • The device sends a special signal to the CPU whennew data arrives OR device ready for next data • CPU can be performing other tasks instead of polling device. • “Wake me when we get there.”
I/O in LC-3 • Memory-mapped I/O(Table A.3) • Asynchronous devices • synchronized through status registers • Polling and Interrupts • the details of interrupts will be discussed in Chapter 10
Keyboard Input • When a character is typed: • its ASCII code is placed in bits [7:0] of KBDR (bits [15:8] are always zero) • the “ready bit” (KBSR[15]) is set to one • keyboard is disabled -- any typed characters will be ignored • When KBDR is read: • KBSR[15] is set to zero • keyboard is enabled keyboard data 15 8 7 0 KBDR 15 14 0 ready bit KBSR
Basic Input Routine POLL LDI R0, KBSRptr BRzp POLL LDI R0, KBDRptr ... KBSRptr .FILL xFE00KBDRptr .FILL xFE02 new char? NO Polling YES readcharacter Operand of LDI is an address of a datum that’s another address
Data Path for Memory-Mapped Input Address Control Logic determines whether MDR is loaded from Memory or from KBSR/KBDR.
Monitor Output • When Monitor is ready to display another character: • the “ready bit” (DSR[15]) is set to one • When data is written to Display Data Register: • DSR[15] is set to zero • character in DDR[7:0] is displayed • any other character data written to DDR is ignored (while DSR[15] is zero) output data 15 8 7 0 DDR 15 14 0 ready bit DSR
Basic Output Routine POLL LDI R1, DSRptr BRzp POLL STI R0, DDRptr ... DSRptr .FILL xFE04DDRptr .FILL xFE06 screen ready? NO Polling YES writecharacter Operand of LDI is an address of a datum that’s another address
;; File: ascii.asm ;; Author: Ken Lambert ;; This program outputs ASCII values 0-127. ;; Pseudocode design: ; for ch in range 0..127 ; loop while display status >= 0 ; print ch .ORIG x3000 ;; Register usage: ; R1 = contents of display status register ; R0 = ch to send to display data register ; R2 = #-127, the negation of the last ASCII value ; R3 = utility storage ; Main program code AND R0, R0, #0; Initialize ch LD R2, LASTCH; Initialize last character CHLOOP ADD R3, R0, R2; Loop while ch - last character != 0 BRz ENDMAIN POLL LDI R1, DSR; Poll for negative display status register (ready bit = 1) BRzp POLL STI R0, DDR ; Display is ready, so output the character ADD R0, R0, #1; Increment the ASCII value BR CHLOOP ENDMAIN HALT ; Main program data DSR .FILL xFE04; Address of the display status register DDR .FILL xFE06; Address of the display data register LASTCH .FILL #-127; The last ASCII value, negated .END
Keyboard Echo Routine • Usually, input character is also printed to screen. • User gets feedback on character typed and knows it’s ok to type the next character. new char? INPUT LDI R0, KBSRptr BRzp INPUT LDI R0, KBDRptrECHO LDI R1, DSRptr BRzp ECHO STI R0, DDRptr ... KBSRptr .FILL xFE00KBDRptr .FILL xFE02DSRptr .FILL xFE04DDRptr .FILL xFE06 NO YES readcharacter screen ready? NO YES writecharacter
;; This program echoes the keys pressed until the return key is entered. ;; Pseudocode design: ; ; loop ; loop while keyboard status >= 0 ; get the next character from the keyboard ; loop while display status >= 0 ; print the character ; if the character == return ; break .ORIG x3000 ;; Register usage: ; R1 = contents of status register ; R0 = contents of data register ; R2 = the newline character ; R3 = temporary working storage ; Main program code LD R2, RT ; Initialize the return character INPOLL LDI R1, KBSR ; Poll for negative keyboard status register (ready bit = 1) BRzp INPOLL LDI R0, KBDR ; Keyboard is ready, so input the character OUTPOLL LDI R1, DSR ; Poll for negative display status register (ready bit = 1) BRzp OUTPOLL STI R0, DDR ; Display is ready, so echo the character ADD R3, R0, R2 ; Quit if character = return BRz ENDMAIN BR INPOLL ; Return to input poll ENDMAIN HALT ; Main program data KBSR .FILL XFE00 ; Address of the keyboard status register KBDR .FILL XFE02 ; Address of the keyboard data register DSR .FILL xFE04 ; Address of the display status register DDR .FILL xFE06 ; Address of the display data register RT .FILL x-000D ; The return character (negated) .END