You are on page 1of 30

Chapter 8 I/O Programming

Chapter 9 Trap Service Routines


Programmed I/O

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.

Synchronous vs Asynchronous I/O


Synchronous Latest value of data could be expected to be available when the program wanted it. It might be periodically updated at a known frequency. This is not typical nor usually realistic for I/O. Asynchronous Computer is generally much faster than I/O so program must wait until requested data is available or data provided has been taken. Handshaking is used to ensure that data is available or I/O device is ready.

LC-3 Memory Map


(64K of 16 bit words)
256 words 256 words

23.5 K words

39.5 K words

512 words

LC-3 has Memory Mapped I/O

LC-3 Memory Layout:


x0000 x00FF
x0020 [x0400] x0021 [x0430] x0022 [x0450] x0023 [x04A0] x0024 [x04E0] x0025 [xFD70] GETC OUT PUTS IN HALT

Trap vectors (Supports Software Interrupts)


(Read Char from Keyboard) (Write Character to Console) (Write string to Console) (Prompt, input character from Keyboard, echo character to Console) (Turn off run latch in MCR)

PUTSP (Write packed string to Console)

x0100 x01FF
x0200 x2FFF x3000 xFDFF xFE00 xFFFF
xFE02 KBDR xFE04 DSR xFE06 DDR xFFFE MCR

Interrupt Vectors (Supports Hardware Interrupts)


System Programs & Data (Operating System) User Programs Area I/O Programming Registers (Mapped I/O Regs)
(Keyboard Status Register) (Keyboard Data Register) (Display Status Register) (Display Data Register (Machine Control Register)

xFE00 KBSR [15 {Ready}, 14 {Intr enable}] [7:0{ascii data}] [15{Done}, 14{Intr enable}] [7:0{ascii data}] [15{Run latch}]

The LC-3 Computer & I/O

PSW

I/O Memory

PSW (Program Status Word):


Bits: 15 | S|

10 9 8 |Priority|

2 1 0 | N| Z| P|

LC-3 Memory Mapped I/O

Polling vs Interrrupt Driven I/O

Polling I/O
Program initiates I/O and then checks regularly to see if I/O is completed. (typically a loop in the program)

Interrupt Driven I/O


Program initiates I/O and goes to sleep until the CPU wakes up the program to say that I/O is completed. (instead of going to
sleep, the CPU may do something else before the wake up call)

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.

Keyboard Input Interface

Keyboard Input Registers


KBDR (Keyboard Data Register): Assigned to address xFE02 Data is in KBDR[7:0] Read only Register

KBSR (Keyboard Status Register):

Assigned to address xFE00

Status ready is KBSR[15]


Set to 1 when new data is ready (Set by Keyboard) Cleared when data is read (Cleared when user reads data)

Simple Polling Program for Input from Keyboard


; Waiting for a character to be entered START LDI BRzp LDI BRnzp ~ ~ KBSR KBDR .FILL .FILL xFE00 xFE02 ; Address of KBSR (Keyboard Status Register) ; Address of KBDR (Keyboard Data Register) R1, KBSR START R0, KBDR NEXT_TASK ; Test for character input ; If not here, try again

; Read character ; Go to the next task

xFE02

xFE00

Console Output Interface

Console (Monitor) Output Registers


DDR (Display Data Register): Assigned to Address xFE06

Data is in DDR[7:0]

DSR (Data Status Register):

Assigned to Address xFE04

Status done bit is DSR[15] Set to 1 when data is picked up Cleared when new data is written

Simple Polling Program to Output to Console


; Wait for write to Console done START LDI R1, DSR ; Test for output written

BRzp
STI BRnzp ~ ~ DSR DDR .FILL .FILL

START
R0, DDR NEXT_TASK

If not done, try again

; Write character ; Go to next task

xFE04 xFE06

; Address of DSR (Display Status Register) ; Address of DDR (Display Data Register)

FE06

FE04

Program to Echo from Keyboard to Monitor


; Echo keyboard input to Console output START LDI BRzp LDI R1, KBSR START R0, KBDR ; Test for input ready

ECHO

LDI BRzp STI BRnzp

R1, DSR ECHO R0, DDR NEXT_TASK

; Test for output done

KBSR KBDR DSR DDR

.FILL .FILL .FILL .FILL

xFE00 xFE02 xFE04 xFE06

; Address of KBSR ; Address of KBDR ; Address of DSR ; Address of DDR

A Complete I/O Routine to Echo a Line of Input


.orig x3000 ; Program to read and echo line from the Console ST ST ST LD L1 LDI BRzp STI R1, SaveR1 R2, SaveR2 R3, SaveR3 R2, Newline R3, DSR L1 R2, DDR ; Save registers needed ; by this routine

; Store newline Character in R2 ; Loop until Monitor is done ; Move cursor to new clean line

Loop

LEA LDR BRz LDI BRzp STI ADD BRnzp

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

A Complete I/O Routine to Echo a Line of Input (2)


Input LDI BRzp LDI LDI BRzp STI ADD BRnp R3, KBSR Input R0, KBDR R3, DSR L3 R0, DDR ; Poll until a character is typed ; Get input character

L3

; Loop until Monitor is done ; Echo input character

R0, R0, #-10 ; Test for newline (done) Input ; Loop if not done

L4

LDI BRzp STI

R3, DSR L4 R2, DDR

; Loop until Monitor is done ; Move cursor to new clean line

LD LD LD BRnzp

R1, SaveR1 R2, SaveR2 R3, SaveR3 NEXT_TASK

; Restore registers ; to original values

; Do the program's next task

A Complete I/O Routine to Echo a Line of Input (3)


SaveR1 SaveR2 SaveR3 DSR DDR KBSR KBDR .BLKW .BLKW .BLKW .FILL .FILL .FILL .FILL 1 1 1 xFE04 xFE06 xFE00 xFE02 ; Memory for registers saved

Newline .FILL
Prompt

x000A

; ASCII code for newline

.STRINGZ "Input character line> "

NEXT_TASK BRnzp NEXT_TASK .END

; Simulates next task

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

Device(s) Generating Interrupt Service Request to the CPU

Keyboard:

Monitor:

Done/Ready bit is anded with the Interrupt Enable bit to produce an interrupt request signal

Generating the LC-3 Interrupt Request to the CPU

Interrupt Vectors are: x0100 to x01FF (x0180 is for the Keyboard)

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)

The context of the original program is reloaded, and


The original program resumes

Trap Routines

Programming Trap Service Routines


Writing the code for them

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 vector - Service Routine Addresses

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

Trap Vector Routine

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

; Get status ; Bit 15 on says display is ready ; Write character

; Restore registers ; Return from trap (JMP R7, actually) ; Address of display status register ; Address of display data register ; Save area for R1

TRAP x23 IN Trap Service Routine


(Input Character)
; in.asm ; .ORIG x04A0 START ST ST ST ; LD L1 LDI BRzp STI ; LEA
Loop L2 LDR BRz LDI BRzp STI ADD BRnzp ; Input LDI BRzp LDI LDI BRzp STI

Service Routine for Keyboard Input

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>"

; Write next character of ; prompt string ; Increment Prompt pointer

; Has a character been typed? ; Load it into R0

L3

; Echo input character ; to the monitor

; L4

LDI BRzp STI LD LD LD RET

R3,DSR L4 R2,DDR R1,SaveR1 R2,SaveR2 R3,SaveR3

; 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

TRAP x25 HALT Service Routine

; ; 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

TRAP 22 PUTS Trap Service Routine (Output a Character String)


; puts.asm Output a Character String ; This service routine writes a NULL-terminated string to the console. ; It services the PUTS service call (TRAP x22). ; Inputs: R0 is a pointer to the string to print. ; Context Information: R0, R1, and R3 are saved, and R7 is lost ; in the jump to this routine ; .ORIG x0450 ; Where this Service Routine resides ST R7, SaveR7 ; Save R7 for later return ST R0, SaveR0 ; Save other registers that are used by this routine ST R1, SaveR1 ST R3, SaveR3 ; ; Loop through each character in the array ; ; Loop LDR R1, R0, #0 ; Retrieve the character(s) BRz Return ; If it is 0, done L2 LDI R3,DSR BRzp L2 STI R1, DDR ; Write the character ADD R0, R0, #1 ; Increment pointer BRnzp Loop ; Do it all over again ; ; Return from the request for service call Return LD R3, SaveR3 ; Restore Registers LD R1, SaveR1 LD R0, SaveR0 LD R7, SaveR7 RET ; ; Register locations DSR .FILL xFE04 DDR .FILL xFE06 SaveR0 SaveR1 SaveR3 SaveR7 .FILL .FILL .FILL .FILL .END x0000 x0000 x0000 x0000

You might also like