PIC18 task context is 2 bytes too long

paul_piak wrote on Friday, August 01, 2008:


I think 2 bytes can be removed from the PIC18 task context save/restore macros.
When saving context, the FSR0 register is initialized to 0, which is the address of the first byte of the .tmpdata+.mathdata sections
These 2 sections are then saved to the stack by executing an application dependent number (portCOMPILER_MANAGED_MEMORY_SIZE) of
After saving .tmpdata+.mathdata, the value of FSR0 is equal to the address of the last byte in .tmpdata+.mathdata
This address is equal to the constant portCOMPILER_MANAGED_MEMORY_SIZE
Thus, at this point the contents of FSR0 is the same for every context switch. The value is the same every time, for each task in a single application.
Effectively a constant value is saved.
By definition, constants don’t have to be saved on a stack, since they don’t unexpectedly change.

When restoring the .tmpdata+.mathdata sections from the stack, first FSR0 is initialized to the address of the last byte in .tmpdata+.mathdata
This is now done by restoring FSR0 from the task stack. But we know FSR0 will always be restored to portCOMPILER_MANAGED_MEMORY_SIZE, since that is the location of the last byte of .tmpdata+.mathdata , which will be the first byte to be restored.

Total savings: 2 bytes RAM for each task stack and 2 instructions when saving the context (restoring context will be the same number of cycles because FSR0 has to be set to the constant value that was previously on the stack, but is now portCOMPILER_MANAGED_MEMORY_SIZE)


paul_piak wrote on Thursday, August 07, 2008:

Is everybody still thinking about this?
If I’m not making sense, could someone tell me?

rtel wrote on Thursday, August 07, 2008:

Sorry for the delay … I will get around to looking at it.