Programmed I/O - Interrupts - Interrupt Driven I/O - Trap Service Routines
Programmed I/O - Interrupts - Interrupt Driven I/O - Trap Service Routines
Interrupts
Interrupt Driven I/O Trap Service Routines
Input/Output Addressing
Memory Mapped I/O
A section of the memory address space is reserved for I/O Registers rather than general memory locations. Think of it as pseudo memory. The same instructions are used for general programming and I/O programming.
Non-Memory Mapped I/O There is a separate address space for I/O programming, and an entirely separate set of I/O Instructions.
23.5 K words
39.5 K words
512 words
x0100 x01FF
x0200 x2FFF x3000 xFDFF xFE00 xFFFF
xFE02 KBDR xFE04 DSR xFE06 DDR xFFFE MCR
xFE00 KBSR [15 {Ready}, 14 {Intr enable}] [7:0{ascii data}] [15{Done}, 14{Intr enable}] [7:0{ascii data}] [15{Run latch}]
PSW
I/O Memory
10 9 8 |Priority|
2 1 0 | N| Z| P|
Polling I/O
Program initiates I/O and then checks regularly to see if I/O is completed. (typically a loop in the program)
Handshaking Communications
A Paradigm Used in Asynchronous Communications Typical sequence:
Initiate I/O Request (send signal to I/O device) Loop checking for I/O complete (check for returned signal) Continue Program (perhaps Initiate another I/O Request) Etc.
xFE02
xFE00
Data is in DDR[7:0]
Status done bit is DSR[15] Set to 1 when data is picked up Cleared when new data is written
BRzp
STI BRnzp ~ ~ DSR DDR .FILL .FILL
START
R0, DDR NEXT_TASK
xFE04 xFE06
; Address of DSR (Display Status Register) ; Address of DDR (Display Data Register)
FE06
FE04
ECHO
; Store newline Character in R2 ; Loop until Monitor is done ; Move cursor to new clean line
Loop
R1, Prompt R0, R1, #0 Input R3, DSR L2 R0, DDR R1, R1, #1 Loop
; Load starting address of prompt string ; Get prompt character ; Branch to Loop on null (0) ; Loop until Monitor is done ; Write prompt character ; Increment Prompt pointer ; Go to get next prompt character
L2
L3
R0, R0, #-10 ; Test for newline (done) Input ; Loop if not done
L4
LD LD LD BRnzp
Newline .FILL
Prompt
x000A
I/O Interrupts
Requirements for a device to interrupt the processor
The device must have the right to request service The I/O device must want service The device request must be at a higher priority than what is being done by the processor or is being requested by other devices The processor must be completed with the present instruction execution
Keyboard:
Monitor:
Done/Ready bit is anded with the Interrupt Enable bit to produce an interrupt request signal
Servicing an Interrupt
The following process is followed to service an interrupt:
The CPU enters the Supervisor State
(PSW Bit 15 set to 0)
The context of the present program is saved (PC, PSW, SP) (Why?) The device provides the address of location in the interrupt service routine table where the pointer to the service routine should reside. (x0180 is the Interrupt Vector for the
Keyboard Interrupt Service Routine)
The Supervisor loads the address of the service routine into the PC The service routine is executed ending with an RTI
(Any registers used will be saved and restored)
Trap Routines
Trap Routines
Trap Instruction: TRAP x 1111 0000 trap vector F0xx
[PC ] R7
Jump to routine at trap vector address Return: RET 1100 000 111 000000 [R7] PC (JMP R7) C1C0
Some Conventions (For the LC-3): Data passed (read & write) in R0, Error messages returned in R5
Trap Process
1)
Execute
Trap Vectors are at memory locations [0000:00FF] Trap Vectors contain addresses of Pre written (?) Service Routines
2)
3) 4) 5)
[PC] is stored in R7
Address of Trap Service Routine loaded into PC Service Routine Program executed Trap service routine program ends with an RET ([R7] loaded into PC)
TRAP x21
; out.asm ; .ORIG x0430 ST
(Output Character)
OUT
R1, SaveR1
; System call starting address ; R1 will be used to poll the DSR ; hardware
; ; Write the character ; TryWrite LDI R1, DSR BRzp TryWrite WriteIt STI R0, DDR ; ; Return from trap ; Return LD R1, SaveR1 RET ; DSR .FILL xFE04 DDR .FILL xFE06 SaveR1 .BLKW 1 .END
; Restore registers ; Return from trap (JMP R7, actually) ; Address of display status register ; Address of display data register ; Save area for R1
R1,SaveR1 R2,SaveR2 R3,SaveR3 R2,Newline R3,DSR L1 R2,DDR R1,Prompt R0,R1,#0 Input R3,DSR L2 R0,DDR R1,R1,#1 Loop R3,KBSR Input R0,KBDR R3,DSR L3 R0,DDR
; Save the register values ; that are used so that they ; can be restored before RET
; Check DDR --
is it free?
; Move cursor to new clean line ; ; ; ; Prompt is starting address of prompt string Get next prompt character Check for end of prompt string
; SaveR1 SaveR2 SaveR3 DSR DDR KBSR KBDR Newline Prompt .END
.BLKW 1 .BLKW 1 .BLKW 1 .FILL xFE04 .FILL xFE06 .FILL xFE00 .FILL xFE02 .FILL x000A ; newline (ASCII) .STRINGZ "Input a character>"
L3
; L4
; Move cursor to new clean line ; Service routine done, restore ; original values in registers. ; Return from trap (i.e., JMP R7)
; halt.asm Halts the program ; .ORIG xFD70 ; Where this routine resides ST R7, SaveR7 ST R1, SaveR1 ; R1: a temp for MC register ST R0, SaveR0 ; R0 is used as working space ; print message that machine is halting LD TRAP LEA TRAP LD TRAP R0, ASCIINewLine x21 R0, Message x22 R0, ASCIINewLine x21
; ; clear bit 15 at xFFFE to stop the machine ; LDI R1, MCR ; Load MC register into R1 LD R0, MASK ; R0 = x7FFF AND R0, R1, R0 ; Mask to clear the top bit STI R0, MCR ; Store R0 into MC register ; ; return from HALT routine. ; (how can this routine return if the machine is halted above?) ; LD R1, SaveR1 ; Restore registers LD R0, SaveR0 LD R7, SaveR7 RET ; JMP R7, actually ; ; Some constants ; ASCIINewLine .FILL x000A SaveR0 .BLKW 1 SaveR1 .BLKW 1 SaveR7 .BLKW 1 Message .STRINGZ "Halting the machine." MCR .FILL xFFFE ; Address of MCR MASK .FILL x7FFF ; Mask to clear the top bit .END