Calling a C Function from an ISR

Question: How do I call a C function from inside an ISR with ImageCraft compiler?

 

Answer:

When the processor enters an ISR, the flag register is reset.  This resets the page mode also to 0.  A function written in C will follow the native page mode (Mode 3 for Imagecraft).  So, you have to switch to Native paging mode before calling the function.  Also, there is no guarantee about which virtual registers or page mode registers the C function is going to change.  So, you will have to save and restore all the page mode registers, and the virtual registers before and after calling the function. 

 

For example, if you would like to call a C function "MyCFunction", the code should look like this.

 

; Switch to native paging

RAM_SET_NATIVE_PAGING

 

; Save paging registers

REG_PRESERVE CUR_PP

REG_PRESERVE MVR_PP

REG_PRESERVE MVW_PP

REG_PRESERVE IDX_PP

 

; Save virtual registers

RAM_SETPAGE_CUR >__r0  ; Set CUR_PP to point to the virtual registers page

mov A,[__r0]

push A

mov A,[__r1]

push A

mov A,[__r2]

push A

 

; Call the C function

lcall _MyCFunction

 

; Restore virtual registers

RAM_SETPAGE_CUR >__r0  ; Set CUR_PP to point to the virtual registers page

pop A

mov [__r2],A

pop A

mov [__r1],A

pop A

mov [__r0],A

 

; Restore paging registers
REG_RESTORE IDX_PP
REG_RESTORE MVW_PP
REG_RESTORE MVR_PP
REG_RESTORE CUR_PP
reti