Wednesday, December 30, 2009

More with Interrupts

Today I did more with software interrupts.

One of the things that SWI is used for is to run more than one program or more than one thread on a system. Today, I re-purposed my stack to be a program swap stack. In this example, when execution reaches the interrupt, it shifts to an area of code that saves the registers and does something else for a while, then returns to the next place in execution.


; Software interrupt example.
;
;
;


AREA HEAP, NOINIT, READWRITE, ALIGN=3

Heap_Size EQU 0x500 ; Size of heap. 0x500, from my earlier project
Stack_Size EQU 0x400 ; Size of stack. 0x400
Manager_Offset EQU 0x100 ; Size of ordinary heap. Manager lives on top of it.

Heap_Memory SPACE Heap_Size ; Allocate memroy for the heap.
Stack_Memory SPACE Stack_Size ; Allocate memory for the stack.


Heap_Manager EQU (Heap_Memory + Manager_Offset)

AREA HelloWorld, CODE, READONLY ;
EXPORT mystart ; Export this code to the project

; Exception handlers.


Vectors B Reset_Handler
Undef_Handler B Undef_Handler
B SWI_Handler


Reset_Handler B mystart

SWI_Handler
STMFD sp!, {R4, R7, R9-R11, R14} ; Push test values and Link Register (R14) onto stack

MOV R4, #0xb ; Overwrite test values in registers
MOV R7, #0xb
MOV R9, #0xb
MOV R10, #0xb
MOV R11, #0xb

LDMFD sp!, {R4, R7, R9-R11, R14} ; Pop test values off stack.

BX R14

; Test code to see if the SWI works as expected.
mystart
LDR sp, =Stack_Memory ; Set stack memory. This points to the base of the stack.
ADD sp, sp, #Stack_Size ; Because this is a decrementing stack, we need to point to the top of the stack.

MOV R4, #0xA ; Load test values into registers
MOV R7, #0xB
MOV R9, #0xC
MOV R10, #0xD
MOV R11, #0xE
SWI #02

stop
BAL stop ; gives me a loop to be caught in

END ; end.


Of course, to do anything interesting with this, there'd have to be a thread manager at SWI_Handler and it could not be implemented as a stack. If it was a stack, the program components would all have to be nested.

No comments:

Post a Comment