![]() |
Trying to follow C18 interrupt context code...
I'm trying to follow the code generated by the compiler for interrupt context save/restore. I am trying to understand how the interrupt routine is using the stack. By compiler convention, FSR1 is the stack pointer and FSR2 is the frame pointer. I think I follow the context save ok, but have problems following the context restore.
<code> PHP Code:
|
Re: Trying to follow C18 interrupt context code...
Bud,
I think the confusion is in the "MOVF 0xfe6, F, ACCESS" statement. The only purpose of this line is to increment FSR1 register (stack pointer) or in your notation (SP)++. Technically, it modifies the status register as well, but it's not used in this case. This line enables the code to go from pre-incrementing the stack pointer in the previous lines, to post incrementing the stack pointer in later lines. If they didn't do this, then the same register would get overwritten during the switch. It has nothing to do with actually saving any registers or the PRODH register. When restoring the context, the equivilent MOVF statment just decrements the stack pointer. Hopefully, this will make things a little clearer. Mike |
Re: Trying to follow C18 interrupt context code...
52E6 MOVF 0xfe6, F, ACCESS
This instruction, according to the PIC18F manual, should cause the FSR1L to be incremented twice - once upon the reading of the data pointed to by FSR1 through the POSTINC1 pseudo-register and once again upon writing to ram pointed to by FSR1 through POSTINC1. If the instruction was the following, then FSR1 would only be accessed on the read with the results left in the W register. In this case the FSR1 would be only incremented once. 50E6 MOVF 0xfe6, W, ACCESS This was the instruction I was expecting to see. Bud |
Re: Trying to follow C18 interrupt context code...
Lets assume that I don't have direct access to the interrupt service routine itself, but do have the ability to hook into certain interrupts to get my subroutine to run at interrupt level.
The purpose of all this is to try and have the ability to conditionally inject an event handler stack frame onto the stack. The interrupt routine would then end up returning to the event handler with interrupts off... asynchronously handle one or more events and then return to the real previous user level code. Bud |
Re: Trying to follow C18 interrupt context code...
Mike,
Thanks - I believe you are correct. I'm re-reading the PIC18 specification. Although I read the spec as indicating that an access to the POSTINC, PREINC, or POSTDEC locations cause the associated FSR to be appropriately incremented or decremented after an indirect access I think what I should have read in addition was "per instruction cycle". So a MOVF POSTINC1, F, ACCESS will do a post increment at the end of the instruction cycle and not at the end of the individual accesses. Subtle but obviously very important distinction. The MOVFF since it is two instruction cycles can increment/decrement on each access but the MOVF is only a single instruction cycle. Thanks again, Bud |
Re: Trying to follow C18 interrupt context code...
One of the other things that confused me is that the C18 stack calling convention is initially broken in the interrupt routine by using the PREINC to do stack pushes instead of the expected POSTINC I was expecting. The stack pointer should always be pointing to the next unused location on the stack.
It turns out this is true with one exception. Since there is no PREDEC instruction, the compiler to pop things off the stack must use two instructions, a MOVF POSTDEC followed by a MOVFF INDF, <reg>. Because these two instructions are non-atomic and an interrupt can catch the execution between these two instructions. Interrupt routines must assume that the stack location being currently pointed to may currently be in use and shouldn't be overwritten. Hence the interrupt stack protocol of apparently allocating an extra stack entry at the beginning of the stack manipulation. Now on to the fun stuff, figuring out how to inject an event driven handler with user level code onto the stack - <insert evil laugh here> Bud |
| All times are GMT -5. The time now is 22:34. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi