View
220
Download
0
Tags:
Embed Size (px)
Citation preview
1
Input and Output Input and Output
Patt and Patel Ch. 8Patt and Patel Ch. 8
2
Computer SystemComputer System
3
• So far, we’ve learned how to:So far, we’ve learned how to:– compute with values in registers– load data from memory to registers– store data from registers to memory
• But where does data in memory come from?But where does data in memory come from?
• And how does data get out of the system so And how does data get out of the system so thatthathumans can use it?humans can use it?
I/O: Connecting to Outside I/O: Connecting to Outside WorldWorld
4
I/O: Connecting to the Outside WorldI/O: Connecting to the Outside World
Types of I/O devices characterized by: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
– data rate: how fast can data be transferred?• keyboard: 100 bytes/sec• disk: 30 MB/s or more• network: 1 Mb/s - 1 Gb/s
5
I/O Devices I/O Devices
KeyboardKeyboard
• User presses ‘A’ key -> ‘a’User presses ‘A’ key -> ‘a’• ASCII code is 0x61ASCII code is 0x61• Keyboard sends this on wiresKeyboard sends this on wires• 1 for start, 8-bits of data, 0 for stop1 for start, 8-bits of data, 0 for stop• ‘‘a’ is: 1011000010a’ is: 1011000010• Buffer at computer catches these bitsBuffer at computer catches these bits
6
DisplaysDisplays
• Character display works with the Character display works with the reverse process (sort of)reverse process (sort of)
• Most displays today are “bit mapped”Most displays today are “bit mapped”
I/O Devices I/O Devices
PrintersPrinters
• Just like a display but now being Just like a display but now being “printed” to paper, not a screen.“printed” to paper, not a screen.
• Again, most printers are now “bit Again, most printers are now “bit mapped” verses character.mapped” verses character.
7
I/O Devices I/O Devices
Hard DiskHard Disk
• A spinning disk (4600, 5200, 7200, 10000+ RPM)A spinning disk (4600, 5200, 7200, 10000+ RPM)• 20 – 400 GB and growing FAST20 – 400 GB and growing FAST• Magnetic and read/write (like tape)Magnetic and read/write (like tape)• Both sides Both sides • Usually a stack of plattersUsually a stack of platters• Disk accessDisk access
• Electronic speeds are in the nanoseconds (10-9 sec)Electronic speeds are in the nanoseconds (10-9 sec)• Disk speeds are in the milliseconds (10-3 sec)Disk speeds are in the milliseconds (10-3 sec)• Why use a disk?Why use a disk?
QueuingQueuing SeekSeek RotationRotation TransferTransfer
DependDependss
10ms10ms 10ms10ms 1ms1ms
8
I/O Devices I/O Devices
QuestionsQuestions
• How does CPU ask for a char to be printed?How does CPU ask for a char to be printed?• Which printer?Which printer?• Which display? Who’s?Which display? Who’s?• When is printer ready for the next char?When is printer ready for the next char?• When does keyboard have the next char?When does keyboard have the next char?• What about the million times slower?What about the million times slower?
9
LC-3 I/OLC-3 I/O
Uses the “TRAP” instruction to invoke Uses the “TRAP” instruction to invoke the Operating System, which in turns the Operating System, which in turns talks to the hardware.talks to the hardware.
User specifies the type of operation User specifies the type of operation desired by giving a code to the OS.desired by giving a code to the OS.
Ex. “TRAP x20” gets a character from Ex. “TRAP x20” gets a character from the keyboard.the keyboard.
10
LC-3 I/O LC-3 I/O
LC-3 I/OLC-3 I/O
• Don’t use “JSR” becauseDon’t use “JSR” because– OS doesn’t trust user to provide the correct address– Want to switch into OS mode, where more things
are allowed
• CPU sees the “TRAP” instruction and uses the CPU sees the “TRAP” instruction and uses the trap vector to determine where to go in the OS trap vector to determine where to go in the OS code.code.
• OS will not allow (or should not)OS will not allow (or should not)– Users to read each other’s keyboards– Users to access all memory or disk locations
11
I/O ControllerI/O ControllerControl/Status RegistersControl/Status Registers
– CPU tells device what to do -- write to control register– CPU checks whether task is done -- read status register
Data RegistersData Registers
– CPU transfers data to/from device
Device electronicsDevice electronics
– performs actual operation• pixels to screen, bits to/from disk, characters from
keyboard
Graphics ControllerControl/Status
Output Data ElectronicsCPU display
12
Programming InterfaceProgramming InterfaceHow are device registers identified?How are device registers identified?
– Memory-mapped vs. special instructions
How is timing of transfer managed?How is timing of transfer managed?
– Asynchronous vs. synchronous
Who controls transfer?Who controls transfer?
– CPU (polling) vs. device (interrupts)
13
Memory-Mapped vs. I/O Memory-Mapped vs. I/O InstructionsInstructions
Special InstructionsSpecial Instructions
– designate opcode(s) for I/O– register and operation encoded in instruction
Memory-mappedMemory-mapped
– assign a memory address to each device register
– use data movement instructions (LD/ST)for control and data transfer
14
Which is better?Which is better?
• What is the problem with having What is the problem with having special instructions for IO?special instructions for IO?
• What happens if a new device is What happens if a new device is created?created?
Memory mapped is much more flexibleMemory mapped is much more flexible
and expandable.and expandable.
Memory-MappedMemory-Mapped vs. I/O Instructionsvs. I/O Instructions
15
Memory Mapped IOMemory Mapped IO
• Idea is to place devices other than Idea is to place devices other than RAM chips at physical address RAM chips at physical address locations.locations.
• This way to access IO devices you This way to access IO devices you use the same load and store use the same load and store instructions.instructions.
16
Memory Mapped I/O Memory Mapped I/O
Design hardware and software to recognize Design hardware and software to recognize certain addressescertain addresses
Real Memory - RAMReal Memory - RAM
0x000000000x00000000
0xffff00000xffff0000
0xffff00080xffff0008
0xffff00100xffff0010
From keyboardFrom keyboard
To displayTo display
17
Memory Mapped I/O Memory Mapped I/O
CPU MEM
Keyboard Buffer0xffff0008
Display Buffer0xffff0010
• Devices on bus watch for their addressDevices on bus watch for their address• But is there a new char to read?But is there a new char to read?• But is the display done with the last char?But is the display done with the last char?
System busSystem bus
18
Need I/O device status to Need I/O device status to coordinatecoordinate
0x000000000x00000000
0xffff00000xffff0000
0xffff00080xffff00080xffff000c0xffff000c
DATA from keyboardDATA from keyboardSTATUS from keyboardSTATUS from keyboard
0xffff00100xffff00100xffff00140xffff0014
DATA to DisplayDATA to Display
STATUS from DisplaySTATUS from Display
Real Memory - RAMReal Memory - RAM
Memory Mapped I/O Memory Mapped I/O
19
Transfer TimingTransfer TimingI/O events generally happen much slower I/O events generally happen much slower than CPU cycles.than CPU cycles.
SynchronousSynchronous
– data supplied at a fixed, predictable rate– CPU reads/writes every X cycles
AsynchronousAsynchronous
– data rate less predictable– CPU must synchronize with device, so that it
doesn’t miss data or write too quickly
20
Transfer ControlTransfer Control
Who determines when the next data transfer Who determines when the next data transfer occurs?occurs?PollingPolling
– CPU keeps checking status register until new data arrives OR device ready for next data
– “Are we there yet? Are we there yet? Are we there yet?”
InterruptsInterrupts
– Device sends a special signal to CPU when new data arrives OR device ready for next data
– CPU can be performing other tasks instead of polling device.– “Wake me when we get there.”
21
LC-3 LC-3 Memory-mapped I/OMemory-mapped I/O (Table A.3) (Table A.3)
Asynchronous devicesAsynchronous devices
– synchronized through status registersPollingPolling and and InterruptsInterrupts
– the details of interrupts is in chapter 10
LocationLocation I/O RegisterI/O Register FunctionFunction
xFE00xFE00 Keyboard Status Reg Keyboard Status Reg (KBSR)(KBSR)
Bit [15] is one when keyboard Bit [15] is one when keyboard has received a new character.has received a new character.
xFE02xFE02 Keyboard Data Reg (KBDR)Keyboard Data Reg (KBDR) Bits [7:0] contain the last Bits [7:0] contain the last character typed on keyboard.character typed on keyboard.
xFE04xFE04 Display Status Register Display Status Register (DSR)(DSR)
Bit [15] is one when device Bit [15] is one when device ready to display another char ready to display another char on screen.on screen.
xFE06xFE06 Display Data Register Display Data Register (DDR)(DDR)
Character written to bits [7:0] Character written to bits [7:0] will be displayed on screen.will be displayed on screen.
22
Input from KeyboardInput from KeyboardWhen a character is typed: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:When KBDR is read:
– KBSR[15] is set to zero– keyboard is enabled
KBDR15 8 7 0
KBSR1514 0
keyboard data
ready bit
23
Basic Input RoutineBasic Input Routine
newchar?
readcharacter
YES
NO
Polling
POLL LDI R0, KBSRPtr BRzp POLL LDI R0, KBDRPtr
...
KBSRPtr .FILL xFE00KBDRPtr .FILL xFE02
24
Simple Implementation: Simple Implementation: Memory-Mapped InputMemory-Mapped Input
Address Control Logicdetermines whether MDR is loaded from
Memory or from KBSR/KBDR.
25
Output to MonitorOutput to Monitor
When Monitor is ready to display another character: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: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)
DDR15 8 7 0
DSR1514 0
output data
ready bit
26
Basic Output RoutineBasic Output Routine
screenready?
writecharacter
YES
NO
Polling
POLL LDI R1, DSRPtrBRzp POLLSTI R0, DDRPtr
...
DSRPtr .FILL xFE04DDRPtr .FILL xFE06
27
Simple Implementation: Simple Implementation: Memory-Mapped OutputMemory-Mapped Output
Sets LD.DDRor selects
DSR as input.
28
Keyboard Echo RoutineKeyboard Echo RoutineUsually, input character is also printed to screen.Usually, input character is also printed to screen.
– User gets feedback on character typedand knows its ok to type the next character.
newchar?
readcharacter
YES
NO
screenready?
writecharacter
YES
NO
POLL1 LDI R0, KBSRPtrBRzp POLL1LDI R0, KBDRPtr
POLL2 LDI R1, DSRPtrBRzp POLL2STI R0, DDRPtr
...
KBSRPtr .FILL xFE00KBDRPtr .FILL xFE02DSRPtr .FILL xFE04DDRPtr .FILL xFE06
29
Polling (non-interrupt) I/OPolling (non-interrupt) I/O
GETCHARGETCHARLDAALDAA SCSRSCSR ; status register; status registerANDAANDA #0x20#0x20 ; rdrf bit mask; rdrf bit maskBEQBEQ GETCHARGETCHAR ; loop if rdrf = 0; loop if rdrf = 0LDAALDAA SCDRSCDR ; read data; read dataRTSRTS
OUTCHAROUTCHARLDABLDAB SCSRSCSR ; load sci status register; load sci status registerBITBBITB #0x80#0x80 ; tdre bit; tdre bitBEQBEQ OUTCHAROUTCHAR ; loop intil tdre = 0; loop intil tdre = 0STAASTAA SCDRSCDR ; write character to port; write character to portRTSRTS
HC11HC11
30
• How much time is spent spinning?How much time is spent spinning?
– A PUTC or GETC is less than 10 instructions, or ~10ns on a modern processor
• Mechanical devices take millisecondsMechanical devices take milliseconds
– Almost all time is spent spinning– Must do useful work while waiting
• Periodically poll devices and send Periodically poll devices and send characters when readycharacters when ready
PollingPolling
31
• The OS must check regularly (poll) The OS must check regularly (poll) for ready devicesfor ready devices
– Perhaps once a millisecond– If ready, then OS services device
• Keyboard: transfer character and Keyboard: transfer character and put on queueput on queue
• Display: transmit character to the Display: transmit character to the graphics HWgraphics HW
Polling I/OPolling I/O
32
Problems:– How often to poll?– How does the OS code get run?– What happens to the user program?
Is there a better solution?
Polling I/O
33
Questions?Questions?
34