Input and Output

Download Report

Transcript Input and Output

Input and Output
How things get into and out of the
CPU
Computer System
CMPE12c
2
Cyrus Bazeghi
I/O Devices
Keyboard
•
•
•
•
•
•
User presses ‘A’ key -> ‘a’
ASCII code is 0x61
Keyboard sends this on wires
1 for start, 8-bits of data, 0 for stop
‘a’ is: 1011000010
Buffer at computer catches these bits
CMPE12c
3
Cyrus Bazeghi
I/O Devices
Displays
• Character display works with the
reverse process (sort of)
• Most displays are “bit mapped”
Printers
• Just like a display but now being
“printed” to paper, not a screen.
• Again, most printers are now “bit
mapped” verses character.
CMPE12c
4
Cyrus Bazeghi
I/O Devices
Hard Disk
•A spinning disk (4600, 5200, 7200, 10000+ RPM)
•2 – 240 GB and growing FAST
•Magnetic and read/write (like tape)
•Both sides
•Usually a stack of platters
•Disk access
Queuing Seek
Rotation Transfer
Depends
10ms
10ms
1ms
•Electronic speeds are in the nanoseconds (10-9 sec)
•Disk speeds are in the milliseconds (10-3 sec)
•Why use a disk?
CMPE12c
5
Cyrus Bazeghi
I/O Devices
Questions
•
•
•
•
•
•
How does CPU ask for a char to be printed?
Which printer?
Which display? Who’s?
When is printer ready for the next char?
When does keyboard have the next char?
What about the million times slower?
CMPE12c
6
Cyrus Bazeghi
MAL I/O
putc $s0 is
# address of char is in $s0
lb
$4, ($s0) # $4 char to be printed
addi $2, $0, 11 # this syscall is like:
syscall
# jal operating_system_function
getc $s0 is
addi $2, $0, 12 # this syscall is like:
syscall
# jal operating_system_function
# returns with char read in $2
CMPE12c
7
Cyrus Bazeghi
MAL I/O
•Don’t use “jal” because
•OS doesn’t trust user to provide the correct
address
•Want to switch into OS mode, where more things
are allowed
•Allowed by what?
•OS catches “syscall” and uses value in $2 to
determine what to do
•OS will not allow (or should not)
•Users to read each other’s keyboards
•Users to send infinite jobs to printers (well,
actually…)
CMPE12c
8
Cyrus Bazeghi
MAL I/O
How does the OS do I/O?
What instructions cause an I/O?
•Could have special instructions
•Hard to anticipate all possibilities
Solutions
•overload load and store
•Memory-mapped I/O
CMPE12c
9
Cyrus Bazeghi
Memory Mapped IO
• Idea is to place devices other than
RAM chips at physical address
locations.
• This way to access IO devices you
use the same load and store
instructions.
CMPE12c
10
Cyrus Bazeghi
Memory Mapped I/O
Design hardware and software to recognize
certain addresses
0x00000000
Real Memory - RAM
0xffff0000
0xffff0008
From keyboard
0xffff0010
To display
Set some labels and use those to access devices
keyboardData
displayData
CMPE12c
equ
equ
0xffff0008
0xffff0010
11
Cyrus Bazeghi
Memory Mapped I/O
CPU
MEM
Keyboard Buffer
0xffff0008
System bus
Display Buffer
0xffff0010
•Devices on bus watch for their address
•getc
operating_system_function_12:
lw
$2, KeyboardData
“return from syscall”
# getc char and put it in $2
operating_system_function_11:
sw
$2, DisplayData
“return from syscall”
# putc char, where char is in $4
•putc
•But is there a new char to read?
•But is the display done with the last char?
CMPE12c
12
Cyrus Bazeghi
Device Status
Need I/O device status to coordinate
0x00000000
Real Memory - RAM
0xffff0000
DATA from keyboard
STATUS from keyboard
0xffff0008
0xffff000c
DATA to Display
0xffff0010
0xffff0014
STATUS from Display
Set up some more labels
KeyboardStatus
DisplayStatus
equ
equ
0xffff000c
0xffff0014
Assume Status is word where MSB==1 means ready.
CMPE12c
13
Cyrus Bazeghi
Device Status
MAL OS calls
GETC
Operating_system_function_12: # getc char and put it in $2
WaitLoop12:
lw $14, KeybaordStatus
bgez $14, WaitLoop12
# keep waiting if $14 non-negative
lw $2, KeyboardData
# same as before
“return from syscall”
PUTC
Operating_system_function_11:
WaitLoop12:
lw $14, DisplayStatus
bgez $14, WaitLoop11
sw $4, DisplayData
“return from syscall”
CMPE12c
14
# putc char, where char is in $4
# keep waiting if $14 non-negative
# same as before
Cyrus Bazeghi
Device Status
HC11
Polling (non-interrupt) I/O
GETCHAR
GETCHAR:
LDAA
ANDA
BEQ
LDAA
RTS
SCSR
#$20
GETCHAR
SCDR
; status register
; rdrf bit mask
; loop if rdrf = 0
; read data
SCSR
#$80
OUTCHAR
SCDR
; load sci status register
; tdre bit
; loop intil tdre = 0
; write character to port
OUTCHAR
OUTCHAR:
LDAB
BITB
BEQ
STAA
RTS
CMPE12c
15
Cyrus Bazeghi
Device Status
• How much time is spent spinning?
• A putc or getc is less than 10 instructions, or
10ns on a modern processor
• Mechanical devices take milliseconds
• Almost all time is spent spinning
• Must do useful work while waiting
• Periodically poll devices and send
characters when ready
CMPE12c
16
Cyrus Bazeghi
Polling I/O
• The OS must check regularly (poll) for
ready devices
• Perhaps once a millisecond
• If ready, then OS services device
• Keyboard: transfer character and put on
queue
• Display: transmit character to the
graphics HW
• Problems:
• How often to poll?
• How does the OS code get run?
• What happens to the user program?
• Is there a better solution?
CMPE12c
17
Cyrus Bazeghi