Title: Discussion
1Schedule
2Prayer an Interrupt
- 2 Nephi 328
- 8 And now, my beloved brethren, I perceive that
ye ponder still in your hearts and it grieveth
me that I must speak concerning this thing. For
if ye would hearken unto the Spirit which
teacheth a man to pray ye would know that ye must
pray for the evil spirit teacheth not a man to
pray, but teacheth him that he must not pray.
3Chapter 8 - Input/Output
4Input and Output
- Used to get information in and out of the
computer. - External devices attached to a computer are
called peripherals.
INPUT keyboard mouse scanner card
reader disk
OUTPUT monitor printer LED disk
Processing Unit
ALU
TEMP
Control Unit
PC
IR
5Introduction to I/O
- We need a method for getting information into and
out of the computer - Traps were introduced in Chapter 5
- Details are hidden in the operating system
- Input/Output used in LC-3
- Keyboard
- Display
- Concepts apply to most types of I/O
- Asynchronous versus Synchronous
6Synchronization
- Synchronous data transfer
- Data read from input / passed to output on
regular clock intervals - Asynchronous data transfer
- Data can be transferred at any time
unpredictable - Requires handshaking
- Polling vs. interrupts
7Synchronous I/O
- How do you synchronize a typist typing 60
characters a minute with a computer executing
instructions at 3 GHz? - Computer can read the keyboard data register up
to 300 million times per second (assume 10 clocks
per instruction). - Computer looks at keyboard 30 times a second
(every 10 millionth clock cycle) - What happens if typist types 29 or 31 characters
a minute? (or if typist misses the 10 millionth
clock cycle?) - I/O happens at a much, much slower rate than
instructions execute - We need to synchronize the
I/O with the computer
8Asynchronous I/O
- Handshaking
- Request - Acknowledge
Request
Sender
Receiver
Acknowledge
9Special Input-Output Instructions
- Special I/O instructions
- Needs dedicated opcode for I/O
- Used by earlier computers (DEC PDP-8)
- Most computers today are memory-mapped
- Device registers assigned addresses
- Standard load and store instructions used
- LC-3 uses memory mapped I/O
10LC-3 Address Space Divisions
x0000
OS Space
x3000
User Programming Space
xFE00
Device Registers
xFFFF
11LC-3 Memory Mapped Registers
LC-3
12Memory-Mapped I/O
- How do memory instructions access I/O registers?
READY LDI R3, KBSR read keyboard status
register KBSR .FILL xFE00
13Memory-Mapped Input
Address Control Logic determines whether MDR is
loaded from Memory or from KBSR/KBDR.
14Input from Keyboard
- When a character is typed
- its ASCII code is placed in bits 70 of
KBDR(bits 158 are always zero) - the ready bit (KBSR15) is set to one
- keyboard is disabled -- any typed characters will
be ignored - When KBDR is read
- KBSR15 is set to zero
- keyboard is enabled
15Basic Input Routine
POLL LDI R0,KBSRPtr BRzp POLL LDI
R0,KBDRPtr ... KBSRPtr .FILL xFE00KBDRPtr
.FILL xFE02
16Output to Display
- When Display is ready to display another
character - the ready bit (DSR15) is set to one
- When data is written to Display Data Register
- DSR15 is set to zero
- character in DDR70 is displayed
- any other character data written to DDR is
ignored(while DSR15 is zero)
17Basic Output Routine
POLL LDI R1,DSRPtr BRzp POLL STI
R0,DDRPtr ... DSRPtr .FILL xFE04DDRPtr .FILL
xFE06
18Memory-Mapped Output
19Keyboard Echo Routine
- By echoing character, a user gets feedback on
character typed and knows its ok to type the next
character.
POLL1 LDI R0,KBSRPtr BRzp POLL1 LDI
R0,KBDRPtrPOLL2 LDI R1,DSRPtr BRzp
POLL2 STI R0,DDRPtr ... KBSRPtr .FILL
xFE00KBDRPtr .FILL xFE02DSRPtr .FILL
xFE04DDRPtr .FILL xFE06
20Interrupt-Driven I/O - Briefly
- Everything up to now has been based on polling.
- Polling is constantly asking are you ready
yet? - Polling can be inefficient.
- Lots of instructions are just checking to see if
someone has typed a character. - Interrupt-driven I/O uses a special signal to
indicate that I/O needs to be performed. - Two elements are required
- A signal from the I/O device indicating it is
ready. - Mechanism to see if this signal is present.
21Processing the data
- Each interrupt immediately causes the CPU to jump
to a special piece of code that processes the
interrupt (interrupt service routine) - Retrieves character from the keyboard data
register. - Writes the character to the monitor.
- After code is finished, execution returns back to
the original code (as if the interrupt never
occurred).
22Interrupt-Driven I/O
- To implement an interrupt mechanism, we need
- A way for the I/O device to signal the CPU that
aninteresting event has occurred. - A way for the CPU to test whether the interrupt
signal is setand whether its priority is higher
than the current program. - Generating Signal
- Software sets "interrupt enable" bit in device
register. - When ready bit is set and IE bit is set,
interrupt is signaled.
23Priority
- Every instruction executes at a stated level of
urgency. - LC-3 8 priority levels (PL0-PL7)
- Example
- Payroll program runs at PL0.
- Nuclear power correction program runs at PL7.
- Its OK for PL7 device to interrupt PL0
program,but not the other way around. - Priority encoder selects highest-priority
device,compares to current processor priority
level,and generates interrupt signal if
appropriate.
24Testing for Interrupt Signal
- CPU looks between STORE and FETCH phases.
- If not set, continues with next instruction.
- If set, transfers control to interrupt service
routine.
More details in Chapter 10.
25Implementation of LC-3 Memory-Mapped I/O
Because of interrupt enable bits, status
registers (KBSR/DSR)must be written, as well as
read.
26Chapter 9TRAP Routines andSubroutines
27System Calls
- Certain operations require specialized
knowledgeand protection - specific knowledge of I/O device registersand
the sequence of operations needed to use them - I/O resources shared among multiple
users/programsa mistake could affect lots of
other users! - Not every programmer knows (or wants to
know)this level of detail - Provide service routines or system calls(part of
operating system) to safely and
convenientlyperform low-level, privileged
operations
28Privileged Instructions
- There are several instructions that are best
executed by a supervisor program (OS) rather than
a user program - IO instructions
- Loading of memory-mapped registers
- Resetting the clock
- Halt
- --- i.e. instructions where one program can
affect the behavior of another. - The CPU can be designed to enforce two modes of
operation - User Mode
- Privileged Mode (aka. supervisor, kernel, monitor
mode) - Only the supervisor program (OS) can execute
privileged instructions.
29Why Trap Routines
- I/O is performed through memory mapped
registers. - Two programs cannot access these registers at the
same time. - Access to I/O registers can be restricted by the
Operating System (OS) and the TRAP instruction. - A Trap is a special OS routine that can be used
to control access to I/O registers. - Also called a Service Call or a System Call.
30LC-3 TRAP Mechanism
- 1. A set of service routines.
- part of operating system -- routines start at
arbitrary addresses(convention is that system
code is below x3000) - up to 256 routines
- 2. Table of starting addresses.
- stored at x0000 through x00FF in memory
- called System Control Block in some architectures
- 3. TRAP instruction.
- used by program to transfer control to operating
system - 8-bit trap vector names one of the 256 service
routines - 4. A linkage back to the user program.
- want execution to resume immediately after the
TRAP instruction
31LC-3 Address Space Divisions
x0000
Vector Table
x00FF
Trap Functions
x3000
User Programming Space
xFE00
Device Registers
xFFFF
32Invoking a Trap Routine
TRAP x0023
1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1
33TRAP Mechanism Operation
- Lookup starting address.
- Transfer to service routine.
- Return (JMP R7).
34TRAP
35TRAP Routines/Assembler Names
36The System Control Block
37Trap Example
User Program
System Control Block
0000 0100 1010 0000
x0023
1111 0000 0010 0011
x3020
x3021
Character input service routine
x04A0
1100 0001 1100 0000
38Using the TRAP Instruction
- .ORIG x3000
- LD R2, TERM Load negative ASCII
7 LD R3, ASCII Load ASCII
differenceAGAIN TRAP x23 input
character ADD R1, R2, R0 Test for
terminate BRz EXIT Exit if done ADD R0,
R0, R3 Change to lowercase TRAP x21
Output to monitor... BRnzp AGAIN ... again
and again... - TERM .FILL xFFC9 -7ASCII .FILL x0020
lowercase bitEXIT TRAP x25 halt .END
39OUT TRAP Routine
.ORIG x0430 Starting address ST R1,
SaveR1 Save R1 TryWrite LDI R1, DSR
Status BRzp TryWrite WriteIt STI R0, DDR
Monitor ready, write char Return LD R1,
SaveR1 Restore R1 RET DSR .FILL xFE04 DDR .
FILL xFE06 SaveR1 .FILL x0000 .END
40IN TRAP Routine
.ORIG x04A0 START ST R1, SaveR1 ST R2,
SaveR2 ST R3, SaveR3 LD R2,
Newline L1 LDI R3,DSR BRzp L1 STI R2,
DDR LEA R1, Prompt Loop LDR R0, R1, 0
BRz Input L2 LDI R3, DSR BRzp L2 STI R0,
DDR ADD R1, R1, 1 BR Loop
SaveR1 .FILL x0000 SaveR2 .FILL
x0000 SaveR3 .FILL x0000 DSR .FILL
xFE04 DDR .FILL xFE06 KBSR .FILL xFE00 KBDR .FILL
xFE02 Newline .FILL x000A Prompt .STRINGZ
Input- .END
Input LDI R3, KBSR BRzp Input
LDI R0, KBDR L3 LDI R3, DSR
BRzp L3 STI R0, DDR L4 LDI
R3, DSR BRzp L4 STI R2,
DDR LD R1, SaveR1 LD
R2, SaveR2 LD R3, SaveR3
RET
41Saving and Restoring Registers
- Must save the value of a register if
- Its value will be destroyed by service routine,
and - We will need to use the value after that action.
- Who saves?
- caller of service routine?
- knows what it needs later, but may not know what
gets altered by called routine - called service routine?
- knows what it alters, but does not know what will
be needed later by calling routine
42Caller-Save versus Callee-Save
Save Registers
Save Registers
TRAP x0023
TRAP x0023
Trap Routine
Trap Routine
Restore Registers
Restore Registers
43Example Store 10 Numbers
- LEA R3, Binary LD R6, ASCII char-gtdigit
template - LD R7, COUNT initialize to 10
- AGAIN TRAP x23 Get char ADD R0, R0, R6
convert to number STR R0, R3, 0 store
number ADD R3, R3, 1 incr pointer ADD R7,
R7, -1 decr counter BRp AGAIN more?
BR NEXT - ASCII .FILL xFFD0
- COUNT .FILL 10
- Binary .BLKW 10
Whats wrong with this routine?What happens to
R7?
44Saving and Restoring Registers
- Called routine -- callee-save
- Before start, save any registers that will be
altered(unless altered value is desired by
calling program!) - Before return, restore those same registers
- Calling routine -- caller-save
- Save registers destroyed by own instructions
orby called routines (if known), if values
needed later - save R7 before TRAP
- save R0 before TRAP x23 (input character)
- Or avoid using those registers altogether
- Values are saved by storing them in memory.
45Question
- Can a service routine call another service
routine? - If so, is there anything special the calling
service routine must do?
46Halting the Computer
What instruction will restart the machine?
Clock Generator
One Machine Cycle
Master Control Register (MCR)
D15
D14
D13
D12
D11
D10
D9
D8
D7
D6
D5
D4
D3
D2
D1
D0
Bit 15 is a 0 to halt computer (need a 1 to
run).
47Halt Trap
.ORIG xFD70 ST R7, SaveR7 ST R1, SaveR1 ST R0,
SaveR0 LD R0, ASCIINL TRAP X21 LEA R0,
MESSAGE TRAP X22 PUTS LD R0, ASCIINL TRAP
X21 LDI R1, MCR LD R0, MASK AND R0, R1,
R0 STI R0, MCR
LD R1, SaveR1 LD R0, SaveR0 LD R7,
SaveR7 RET ASCIINL .FILL X000A SaveR0 .FILL
X0000 SaveR1 .FILL X0000 SaveR7 .FILL
X0000 MESSAGE .STRINGZ HALTING! MCR .FILL
XFFFF MASK .FILL X7FFF .END
48Color or No Color?
- LEA R3, Binary LD R6, ASCII
char-gtdigit template - LD R7, COUNT initialize to 10
- AGAIN TRAP x23 Get char ADD R0, R0,
R6 convert to number STR R0, R3, 0
store number ADD R3, R3, 1 incr
pointer ADD R7, R7, -1 decr counter
BRp AGAIN more? BR NEXT - ASCII .FILL xFFD0
- COUNT .FILL 10
- Binary .BLKW 10
LEA R3, Binary LD R6, ASCII
char-gtdigit template LD R7, COUNT
initialize to 10 AGAIN TRAP x23 Get char
ADD R0, R0, R6 convert to number
STR R0, R3, 0 store number ADD R3,
R3, 1 incr pointer ADD R7, R7, -1
decr counter BRp AGAIN more?
BR NEXT ASCII .FILL xFFD0 COUNT .FILL
10 Binary .BLKW 10
49Global Font-lock mode in Emacs
- ALT x
- global-font-lock-mode
50Lecture Quiz 6
- What address does label B refer to in the program
fragment below? - .orig x5000
- AND R0,R0,0
- ADD R0,R0,5
- OUT
- HALT
- .STRINGZ "Hello, world!\n"
- A .fill 12
- B .fill 0