IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions,...

44
IO - 1 Embedded Systems Lab./Honam University CPUs Input and output. Supervisor mode, exceptions, traps. Co-processors.

Transcript of IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions,...

Page 1: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 1 Embedded Systems Lab./Honam University

CPUs

Input and output.Supervisor mode, exceptions, traps.Co-processors.

Page 2: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 2 Embedded Systems Lab./Honam University

I/O devices

Usually includes some non-digital component.Typical digital interface to CPU:

CPU

statusreg

datareg

mec

hani

sm

Page 3: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 3 Embedded Systems Lab./Honam University

Motorola 16-bit Microcontroller

MC68HC16Z1 – 16-bit CPU, SIM, GPT, QSM, ADC, RAM. Powerful, complex modules w register banks

Intermodule Bus 24-bit address bus, 16-bit data bus; External Bus

shared with peer processors7 level Extl Interrupts8 Analog Inputs, Event capture and assertion Timer

Inputs/outputs, Synchronous and Asynchronous Serial Communication;

Page 4: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 4 Embedded Systems Lab./Honam University

Application: 8251 UART

Universal asynchronous receiver transmitter (UART) : provides serial communication.

8251 functions are integrated into standard PC interface chip.

Allows many communication parameters to be programmed.

Page 5: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 5 Embedded Systems Lab./Honam University

Serial communication

Characters are transmitted separately:

time

bit 0 bit 1 bit n-1

nochar

start stop...

Page 6: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 6 Embedded Systems Lab./Honam University

Serial communication parameters

Baud (bit) rate.Number of bits per character.Parity/no parity.Even/odd parity.Length of stop bit (1, 1.5, 2 bits).

Page 7: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 7 Embedded Systems Lab./Honam University

8251 CPU interface

CPU 8251

status(8 bit)

data(8 bit)

serialport

xmit/rcv

Page 8: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 8 Embedded Systems Lab./Honam University

Programming I/O

Two types of instructions can support I/O: special-purpose I/O instructions; memory-mapped load/store instructions.

Intel x86 provides in, out instructions. Most other CPUs use memory-mapped I/O.

I/O instructions do not preclude memory-mapped I/O.

Page 9: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 9 Embedded Systems Lab./Honam University

ARM memory-mapped I/O

Define location for device:

DEV1 EQU 0x1000Read/write code:

LDR r1,#DEV1 ; set up device adrsLDR r0,[r1] ; read DEV1LDR r0,#8 ; set up value to writeSTR r0,[r1] ; write value to device

WHY ACCESS THESE REGISTERS?

Page 10: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 10 Embedded Systems Lab./Honam University

SHARC memory mapped I/O

Device must be in external memory space (above 0x400000).

Use DM to control access:I0 = 0x400000;M0 = 0;R1 = DM(I0,M0);

Page 11: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 11 Embedded Systems Lab./Honam University

Peek and poke

Traditional HLL interfaces:int peek(char *location) {return *location; }

void poke(char *location, char newval) {(*location) = newval; }

Page 12: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 12 Embedded Systems Lab./Honam University

Busy/wait output

Simplest way to program device. Use instructions to test when device is ready.

current_char = mystring;while (*current_char != ‘\0’) {poke(OUT_CHAR,*current_char);while (peek(OUT_STATUS) != 0);current_char++;

}

Pg. 110 - Dev Addr, String

Page 13: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 13 Embedded Systems Lab./Honam University

Simultaneous busy/wait input and output

while (TRUE) {/* read */while (peek(IN_STATUS) == 0);achar = (char)peek(IN_DATA);Poke (IN_STATUS,0);/* write */poke(OUT_DATA,achar);poke(OUT_STATUS,1);while (peek(OUT_STATUS) != 0);}

Page 14: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 14 Embedded Systems Lab./Honam University

Interrupt I/O

Busy/wait is very inefficient. CPU can’t do other work while testing device. Hard to do simultaneous I/O.

Interrupts allow a device to change the flow of control in the CPU. Causes subroutine call to handle device.

Page 15: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 15 Embedded Systems Lab./Honam University

Interrupt interface

CPU

statusreg

datareg

mec

hani

sm

PC

intr request

intr ack

data/address

IR

Page 16: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 16 Embedded Systems Lab./Honam University

Interrupt behavior

Based on subroutine call mechanism. Interrupt forces next instruction to be a

subroutine call to a predetermined location. Return address is saved to resume executing

foreground program.

Page 17: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 17 Embedded Systems Lab./Honam University

Interrupt physical interface

CPU and device are connected by CPU bus.CPU and device handshake:

device asserts interrupt request; CPU asserts interrupt acknowledge when it can

handle the interrupt.

Page 18: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 18 Embedded Systems Lab./Honam University

Example: character I/O handlers

void input_handler() {achar = peek(IN_DATA);gotchar = TRUE;poke(IN_STATUS,0);

}void output_handler() {}

Global Parameters to communicate with foreground program

Page 19: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 19 Embedded Systems Lab./Honam University

Example: interrupt-driven main program

main() {while (TRUE) {

if (gotchar) {poke(OUT_DATA,achar);poke(OUT_STATUS,1);gotchar = FALSE;}

}}

Foreground program: still no useful work

Page 20: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 20 Embedded Systems Lab./Honam University

Example: interrupt I/O with buffers

Queue for characters:

head tailhead tail

a

Example 3-6, Pg 114. Foreground: useful work

Last spot unused

Page 21: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 21 Embedded Systems Lab./Honam University

Buffer-based input handler

void input_handler() {char achar;if (full_buffer()) error = 1;else { achar = peek(IN_DATA); add_char(achar); }poke(IN_STATUS,0);if (nchars == 1)

{ poke(OUT_DATA,remove_char(); poke(OUT_STATUS,1); }} Only to start off

character outputting

Page 22: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 22 Embedded Systems Lab./Honam University

I/O sequence diagram

:foreground :input :output :queue

empty

a

empty

b

bc

c

Page 23: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 23 Embedded Systems Lab./Honam University

Debugging interrupt code

What if you forget to change registers? Foreground program can exhibit mysterious bugs. Bugs will be hard to repeat---depend on interrupt

timing.

Book Example 3-7, Page 119: Interrupt Handler changes value of j in the foreground matrix program. HOW? Is it time sensitive? Can we overcome the problem?

Page 24: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 24 Embedded Systems Lab./Honam University

Priorities and vectors

Two mechanisms allow us to make interrupts more specific: Priorities determine what interrupt gets CPU first. Vectors determine what code is called for each type

of interrupt.

Mechanisms are orthogonal: most CPUs provide both.

Page 25: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 25 Embedded Systems Lab./Honam University

Prioritized interrupts

CPU

device 1 device 2 device n

R0 R1 .. Rn

interruptacknowledge IR0 IR1

IRn

Page 26: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 26 Embedded Systems Lab./Honam University

Interrupt prioritization

Masking: interrupt with priority lower than current priority is not recognized until pending interrupt is complete.

Non-maskable interrupt (NMI): highest-priority, never masked. Often used for power-down.

Page 27: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 27 Embedded Systems Lab./Honam University

Example: Prioritized I/O

:interrupts :foreground :A :B :C

B

A,B

C

A

Page 28: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 28 Embedded Systems Lab./Honam University

Interrupt vectors

Allow different devices to be handled by different code.

Interrupt vector table:

handler 0

handler 1

handler 2

handler 3

Interruptvector

table head

Page 29: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 29 Embedded Systems Lab./Honam University

Interrupt vector acquisition

:CPU :device

receiverequest

receiveack

receivevector

Page 30: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 30 Embedded Systems Lab./Honam University

Generic interrupt mechanism

intr?N

YAssume priority selection is

handled before this point.

Nignore

Y

ack

vector?Y

Y

Ntimeout?

Ybus error

call table[vector]

intr priority > current priority?

continueexecution

Page 31: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 31 Embedded Systems Lab./Honam University

Interrupt sequence

CPU acknowledges request.Device sends vector.CPU calls handler.Software processes request.CPU restores state to foreground program.

Page 32: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 32 Embedded Systems Lab./Honam University

Sources of interrupt overhead

Handler execution time. Interrupt mechanism overhead.Register save/restore.Pipeline-related penalties.Cache-related penalties.

Page 33: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 33 Embedded Systems Lab./Honam University

ARM interrupts

ARM7 supports two types of interrupts: Fast interrupt requests (FIQs). Interrupt requests (IRQs).

Interrupt table starts at location 0.

Page 34: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 34 Embedded Systems Lab./Honam University

ARM interrupt procedure

CPU actions: Save PC. Copy CPSR to SPSR. Force bits in CPSR to record interrupt. Force PC to vector.

Handler responsibilities: Restore proper PC. Restore CPSR from SPSR. Clear interrupt disable flags.

Page 35: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 35 Embedded Systems Lab./Honam University

ARM interrupt latency

Worst-case latency to respond to interrupt is 27 cycles: Two cycles to synchronize external request. Up to 20 cycles to complete current instruction. Three cycles for data abort. Two cycles to enter interrupt handling state.

Page 36: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 36 Embedded Systems Lab./Honam University

SHARC interrupt structure

Interrupts are vectored and prioritized.Priorities are fixed: reset highest, user SW

interrupt 3 lowest.Vectors are also fixed. Vector is offset in vector

table. Table starts at 0x20000 in internal memory, 0x40000 in external memory.v

Page 37: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 37 Embedded Systems Lab./Honam University

SHARC interrupt sequence

Start: must be executing or IDLE/IDLE16.

1. Output appropriate interrupt vector address.

2. Push PC value onto PC stack.

3. Set bit in interrupt latch register.

4. Set IMASKP to current nesting state.

Page 38: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 38 Embedded Systems Lab./Honam University

SHARC interrupt return

Initiated by RTI instruction.

1. Return to address at top of PC stack.

2. Pop PC stack.

3. Pop status stack if appropriate.

4. Clear bits in interrupt latch register and IMASKP.

Page 39: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 39 Embedded Systems Lab./Honam University

SHARC interrupt performance

Three stages of response: 1 cycle: synchronization and latching; 1 cycle: recognition; 2 cycles: brancing to vector.

Total latency: 3 cycles.

Multiprocessor vector interrupts have 6 cycle latency.

Page 40: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 40 Embedded Systems Lab./Honam University

Supervisor mode

May want to provide protective barriers between programs. Avoid memory corruption.

Need supervisor mode to manage the various programs.

SHARC does not have a supervisor mode.

Page 41: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 41 Embedded Systems Lab./Honam University

ARM supervisor mode

Use SWI instruction to enter supervisor mode, similar to subroutine:SWI CODE_1

Sets PC to 0x08. Argument to SWI is passed to supervisor mode

code.Saves CPSR in SPSR.

Page 42: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 42 Embedded Systems Lab./Honam University

Exception

Exception: internally detected error.Exceptions are synchronous with instructions

but unpredictable.Build exception mechanism on top of interrupt

mechanism.Exceptions are usually prioritized and

vectorized.

Page 43: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 43 Embedded Systems Lab./Honam University

Trap

Trap (software interrupt): an exception generated by an instruction. Call supervisor mode.

ARM uses SWI instruction for traps.SHARC offers three levels of software

interrupts. Called by setting bits in IRPTL register.

Page 44: IO - 1Embedded Systems Lab./Honam University CPUs zInput and output. zSupervisor mode, exceptions, traps. zCo-processors.

IO - 44 Embedded Systems Lab./Honam University

Co-processor

Co-processor: added function unit that is called by instruction. Floating-point units are often structured as co-

processors.

ARM allows up to 16 designer-selected co-processors. Floating-point co-processor uses units 1 and 2.