24.09.2013 Views

The Stack, Subroutines, Interrupts, and Resets: Ch3 Spasov

The Stack, Subroutines, Interrupts, and Resets: Ch3 Spasov

The Stack, Subroutines, Interrupts, and Resets: Ch3 Spasov

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> <strong>Stack</strong>, <strong>Subroutines</strong>,<br />

<strong>Interrupts</strong>, <strong>and</strong> <strong>Resets</strong>:<br />

<strong>Ch3</strong> <strong>Spasov</strong><br />

A. <strong>The</strong> stack<br />

B. <strong>Subroutines</strong><br />

C. <strong>Interrupts</strong><br />

D. <strong>Resets</strong><br />

<strong>The</strong><br />

STACK<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 1<br />

<strong>The</strong> stack is a special area in memory used by the CPU to store<br />

register information or general data information during program<br />

execution<br />

<strong>The</strong> stack has a top <strong>and</strong> a bottom<br />

<strong>The</strong> SP register is the special register that controls the address<br />

in the stack<br />

<strong>The</strong> stack used is a LIFO structure that uses push <strong>and</strong> pull<br />

(pop) operations<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 2


Using the<br />

STACK<br />

push operations:<br />

PSHA, PSHB, PSHX, <strong>and</strong> PSHY<br />

pull operations<br />

PULA, PULB, PULX, <strong>and</strong> PULY<br />

Sometimes we push the register data onto the stack when we<br />

need to load the registers with other values to perform certain<br />

operations ...<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 3<br />

<strong>Stack</strong> as a Storage Buffer<br />

* Listing 3.1<br />

* SUM OF SQUARES<br />

* Demonstrate push <strong>and</strong> pull operations<br />

* Program calculates x squared plus y squared.<br />

* It gets x <strong>and</strong> y as 8-bit numbers from addresses<br />

* $1031 <strong>and</strong> $1032.<br />

* It puts the 8-bit result in ACCA.<br />

ORG $E000 ;start address of program<br />

BEGIN LDS #$FF ;You must define the stack first<br />

LDAA $1031 ;Get first data, x<br />

TAB ;<strong>and</strong> square it<br />

MUL<br />

ADCA #$00 ;round it to 8-bit result<br />

PSHA ;<strong>and</strong> save it<br />

LDAA $1032 ;Get second data, y<br />

TAB<br />

MUL<br />

;<strong>and</strong> square it<br />

ADCA #$00 ;round it to 8-bit result<br />

PULB ;retrieve first result<br />

ABA ;<strong>and</strong> add them<br />

HERE BRA HERE ;For now, stop program<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 4


Using <strong>Subroutines</strong><br />

A subroutine allows us to reuse code. It is a section of<br />

a program that may be used one or more times<br />

<strong>The</strong> main program calls subroutines to perform<br />

certain steps<br />

A data input/output process for subroutines is called<br />

parameter passing<br />

If there are not enough registers for parameter passing, a<br />

calling program can pass more parameters using stack<br />

Ex: Modify the program from the previous slide to add<br />

many squares<br />

Subroutine Example<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 5<br />

* SUM OF SQUARES: Demonstrate subroutines<br />

* Program squares all the numbers in addresses $C000 to $C07F<br />

* <strong>and</strong> puts the sum of the squares as a 16-bit result in IX.<br />

ORG $E000 ;start address<br />

* Start MAIN<br />

MAIN LDS #$CFFF ;You must define<br />

* ;the stack first squaring loop<br />

LDX #$C000 ;init. data block pointer<br />

LOOP1<br />

JSR SQUARE ;square it <strong>and</strong> point to next data<br />

PSHA ;save square<br />

CPX #$C080 ;squared all data?<br />

BNE LOOP1 ;get more if not<br />

* ;summing loop<br />

LDX #$0000 ;sum = 0<br />

LDAA #$80 ;i=$80<br />

LOOP2 PULB ;get square(i)<br />

ABX ;sum = sum + square(i)<br />

DECA ;i=i-1<br />

BNE LOOP2 ;repeat until i == 0<br />

HERE BRA HERE ;stop program<br />

* End of MAIN<br />

* ----------------------------------------------------<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 6


Subroutine Example<br />

* SUBROUTINE SQUARE<br />

* calculates the square of an 8-bit number<br />

* as a rounded 8-bit normalized result<br />

* data pointer (IX) increments<br />

* calling registers:<br />

* IX = address of data to be squared<br />

* return registers:<br />

* ACCA = 8-bit square<br />

* IX=IX+1<br />

* ACCB is affected<br />

* others unaffected<br />

SQUARE<br />

LDAA $0,X<br />

TAB<br />

MUL<br />

ADCA #$00 ;round it to 8-bit result<br />

INX<br />

RTS<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 7<br />

* -----------------------------------------------------<br />

Process of Calling <strong>Subroutines</strong><br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 8


Example: Subroutine Using the <strong>Stack</strong><br />

* Listing 3.6<br />

* Demonstrate a convention for using subroutines<br />

* using a modification of Subroutine SQUARE<br />

* (See Listing 3.2). Program squares all the numbers<br />

* in addresses $C000 to $C07F<br />

ORG $E000 ;start address<br />

* Start MAIN<br />

MAIN LDS #$FF ;You must define the stack first<br />

* ;squaring loop<br />

LDX #$C000 ;init. data block pointer<br />

* ;IX is the calling register<br />

LOOP1<br />

JSR SQUARE2 ;square it <strong>and</strong> note that<br />

* ;original IX value returned<br />

INX ;point to next data<br />

CPX #$C080 ;squared all data?<br />

BNE LOOP1 ;get more if not<br />

HERE<br />

* End of MAIN<br />

BRA HERE ;stop program<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 9<br />

Example: Subroutine Using the <strong>Stack</strong><br />

* SUBROUTINE SQUARE2<br />

* calculates the square of an 8-bit number as a rounded 8-bit result<br />

* data pointer (IX) increments<br />

* calling registers:IX = address of data to be squared<br />

* return registers: IX = address of 8-bit square<br />

* CCR affected; others unaffected<br />

SQUARE2<br />

PSHA ;preserve registers<br />

PSHB<br />

* ;note that following instructions modify<br />

* ;ACCA <strong>and</strong> ACCB<br />

LDAA $0,X ;get data to square<br />

TAB ;copy it to ACCB<br />

MUL ;square it<br />

ADCA #$00 ;round it to 8-bit result<br />

STAA $0,X ;store result<br />

PULB ;restore registers<br />

PULA<br />

RTS ;return, note that ACCA <strong>and</strong> ACCB<br />

* ;contain their original values<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 10


Example: Subroutine Using the <strong>Stack</strong><br />

<strong>The</strong> number of pushes must be equal with the number of pulls<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 11<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 12


ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 13<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 14


ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 15<br />

Typical Program Execution<br />

Without interrupts the program executes continuously<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 16


<strong>Resets</strong><br />

A reset is a special type of interrupt<br />

Unlike an interrupt, it does not return to the interrupted<br />

program<br />

A reset stops execution of the application program to do a<br />

special function, such as reinitializing the registers <strong>and</strong><br />

memory<br />

<strong>Resets</strong><br />

<strong>and</strong><br />

<strong>Interrupts</strong><br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 17<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 18


<strong>The</strong> 68HC11 has four possible types of<br />

reset<br />

External RESET^ pin<br />

Power-on reset (POR)<br />

Computer operating<br />

properly (COP)<br />

Clock monitor reset<br />

Types 1 <strong>and</strong> 2 are external resets<br />

<strong>and</strong> types 3 <strong>and</strong> 4 are fault<br />

tolerant resets or internal resets.<br />

Other differences between resets <strong>and</strong> interrupts are:<br />

•Reset exception is immediately recognized asynchronously<br />

to the clock<br />

•MCU stays in the reset state as long as reset signal is active<br />

•An interrupt is only sampled by the CPU at the end of instr. seq.<br />

Interrupt<br />

External<br />

RESET<br />

Q: How does MCU knows<br />

the source of RESET?<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 19<br />

An interrupt suspends the execution of an application to<br />

do something else – it is triggered by an interrupt request<br />

An interrupt service routine (ISR) is initiated<br />

RTI instruction has to be at the end of the ISR to load registers<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 20


Interrupt Vector<br />

All resets <strong>and</strong> interrupts<br />

use vectors<br />

A vector indicates the<br />

start address of reset or<br />

interrupt routines<br />

A vector address is a 2byte<br />

memory location<br />

that stores a vector<br />

Typical Motorola<br />

processors use a single<br />

area of memory to store<br />

the vectors => known as<br />

the vector table<br />

We can divide the<br />

interrupt sources into<br />

four groups:<br />

interrupts from on-chip<br />

resources<br />

external interrupts<br />

software interrupts<br />

<strong>and</strong> reset exceptions<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 21<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 22


CPU Interrupt<br />

Processing<br />

<strong>The</strong> interrupt process<br />

consists of hardware that<br />

detects an event <strong>and</strong> signals<br />

the CPU<br />

<strong>The</strong> CPU changes the<br />

program flow to the Interrupt<br />

Service Routine<br />

<strong>The</strong> process in the side<br />

diagram is called exception<br />

processing because the CPU<br />

will execute the next<br />

instruction except when one<br />

of the interrupts is pending<br />

An interrupt is said to be<br />

pending when it is enabled,<br />

its event has been detected,<br />

<strong>and</strong> it has not been serviced<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 23<br />

Pending Interrupt Detection<br />

Most interrupts are detected by the on-chip resource<br />

Only IRQ <strong>and</strong> XIRQ are detected directly by the CPU<br />

<strong>The</strong> CPU checks the interrupt signals from all sources<br />

after it had finished executing each instruction<br />

<strong>The</strong> interrupt signals from on-chip resources are<br />

implemented either as event flags that must be<br />

cleared explicitly or as automatic event flags<br />

IRQ <strong>and</strong> XIRQ are active-low signals, so these<br />

sources are serviced as long as the interrupt signal is<br />

low<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 24


Saving Context<br />

Once a pending interrupt is<br />

detected, CPU11, 12 saves<br />

the context by pushing all the<br />

CPU registers onto the stack<br />

All registers must be saved<br />

because the interrupt is<br />

asynchronous to the<br />

program flow <strong>and</strong> we cannot<br />

predict when it will occur<br />

At the end of ISR the RTI<br />

instruction restores the<br />

context of the interrupted<br />

program<br />

Set Interrupt Masks<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 25<br />

After context save, CPU sets the appropriate<br />

masks<br />

if the interrupt source is one controlled by the I<br />

mask, then I will be set automatically<br />

if the interrupt source is the XIRQ, then both X <strong>and</strong><br />

I masks are set<br />

This is done to prevent nested interrupts<br />

before the system is ready<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 26


Vectors<br />

Next step is to get the interrupt service routine<br />

An interrupt vector is two fixed memory locations that<br />

contain the address of the ISR for a given interrupt<br />

<strong>The</strong> CPU 11 interrupt vectors are stored at addresses<br />

shown in slide 22<br />

So, for example, to load the interrupt vector for a<br />

service routine you could do:<br />

ORG $vector<br />

FDB ISRname ; form double byte<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 27<br />

Multiple <strong>Interrupts</strong> <strong>and</strong> Priority<br />

When a system has more<br />

than one interrupt source<br />

enabled, it must be able to<br />

h<strong>and</strong>le the condition when<br />

more than one interrupt is<br />

pending at the same time<br />

Since the CPU can only<br />

h<strong>and</strong>le a single interrupt at a<br />

time, a priority is assigned to<br />

each source<br />

<strong>The</strong> CPU then services the<br />

highest priority pending<br />

interrupt first<br />

If the ISR does not reset the<br />

I bit, only nonmaskable<br />

interrupts or resets can<br />

interrupt this service routine<br />

<strong>The</strong> ISR could clear I bit to<br />

allow itself to be interrupted<br />

Execution of the RTI restores<br />

the original condition of the I<br />

bit then the lower-priority<br />

interrupt can be serviced<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 28


Software <strong>Interrupts</strong><br />

A software interrupt is an<br />

instruction that initiates the<br />

interrupt process<br />

In the CPU11 <strong>and</strong> CPU12<br />

the software interrupt<br />

instruction is SWI<br />

Software interrupts should<br />

be used exclusively for<br />

debugging tools such as<br />

software breakpoints in<br />

debug monitors <strong>and</strong><br />

emulators<br />

SWI is a single byte opcode<br />

instruction<br />

When a breakpoint is<br />

reached in a debug monitor<br />

the monitor program<br />

substitutes the instruction at<br />

that address with an SWI<br />

When the monitor is finished<br />

the SWI opcode is replaced<br />

with the original opcode<br />

Another use for SWI is as a<br />

subroutine that preserves the<br />

registers automatically<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 29<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 30


Software <strong>Interrupts</strong><br />

Another instructions that are used in the<br />

interrupt process are WRI <strong>and</strong> STOP<br />

<strong>The</strong>se instructions are useful when the<br />

program can be in a wait mode while waiting<br />

for an interrupt<br />

in the wait mode the 68HC11 can reduce its power<br />

consumption<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 31<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 32


ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 33<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 34


* PIOC configured as shown in Figure 9.13<br />

* <strong>and</strong> all other required initialization has been done.<br />

ORG $100<br />

LDY #PTR ;initialize data pointer<br />

CLI ;enable interrupts<br />

REPEAT<br />

WAI ;wait for falling STRA<br />

* ;to cause interrupt<br />

LDAA 0,Y ;send out data when it occurs<br />

STAA PORTB,X<br />

* ;note, MCU also pulses STRB low for 2 E-cycles<br />

INY ;repeat for next data transfer<br />

BRA REPEAT<br />

RIRQ<br />

* This is the interrupt h<strong>and</strong>ler routine for parallel I/O<br />

* Note it has the same vector ($FFF2,F3) as IRQ<br />

LDAA PIOC,X ;these two instructions clear STAF<br />

LDAA PORTCL,X<br />

RTI<br />

PTR EQU $180<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 35<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 36


STOP<br />

Instruction<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 37<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 38


St<strong>and</strong>ard<br />

Definitions<br />

External <strong>Interrupts</strong><br />

<strong>The</strong>re are 2 external interrupt<br />

pins on the CPU11 <strong>and</strong> 12<br />

IRQ – is an active-low,<br />

maskable interrupt signal<br />

request<br />

XIRQ – is an active-low,<br />

pseudo-nonmaskable<br />

interrupt request<br />

By default, when you power<br />

up the MPU or RESET it,<br />

XIRQ is masked, that is:<br />

<strong>The</strong> X bit in CCR is set<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 39<br />

To unmask XIRQ, you use<br />

the TAP instruction to clear<br />

bit X<br />

Once the program has<br />

cleared it, executing another<br />

TAP will not set X again<br />

<strong>The</strong> only way to set X is to<br />

RESET the 68HX11<br />

TPA ;CCR -> A<br />

ANDA #$BF; reset bit 6 (X)<br />

TAP ;A -> CCR<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 40


68HC11<br />

<strong>Interrupts</strong><br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 41<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 42


Changing Priority<br />

HPRIO – highest priority<br />

interrupt register<br />

may be read at any time<br />

but may only be written<br />

under special<br />

circumstances<br />

interrupts obey a fixed<br />

hardware-priority circuit to<br />

resolve simultaneous<br />

requests<br />

However, an I-bit-related<br />

interrupt source may be<br />

elevated to the highest I-bit<br />

priority position in the<br />

resolution circuit<br />

<strong>The</strong> first six interrupts are not<br />

masked by the I bit in the<br />

CCR <strong>and</strong> have a fixed<br />

priority:<br />

reset, clock monitor fail,<br />

COP fail, illegal opcode<br />

<strong>and</strong> XIRQ<br />

HPRIO may only be written<br />

while the I-bit related<br />

interrupts are inhibited (I=1)<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 43<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 44


External<br />

Interrupt<br />

Design<br />

Approach<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 45<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 46


ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 47<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 48


How to<br />

Establish<br />

the<br />

Vector<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 49<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 50


IRQ Interrupt Request<br />

Interrupt<br />

Polling<br />

Using<br />

Linked<br />

Lists<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 51<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 52


6811 Assembly Structure for Interrupt<br />

Polling using Linked Lists<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 53<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 54


Periodic<br />

Polling<br />

Plus<br />

Real<br />

Time <strong>Interrupts</strong><br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 55<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 56


6811 Registers<br />

Used to<br />

Configure the<br />

RTI<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 57<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 58


68HC11 Assembly Language Implementation of a Periodic Interrupt<br />

Using RTI<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 59<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 60


ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 61<br />

ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 62

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!