Context Information Saving on microcontrollers

microcontroller

I make a search for context information saving method in microcontrollers (especially PIC), and the only thing i found is a patent of the "tamper resistant microprocessor". According to the patent a microprocessor save a contex information for one program whose execution is to be interrupted and the execution of that program can be restarted by restoring the execution state from the saved context information.

link: http://www.google.com/patents/US20050166069

Is this what i am searching for? And if this is correct and i have understand correct this method is used for example in interrupts when you save the registers values that you need and probably the interrupt routine can change and when you return to the normal execution of the program you take back the saved values? Or when you pop a value to the stack that indicates an address that start a piece of code and when you return from a call instruction you retrieve this address??

Best Answer

You ask for "context saving", but you don't seem to know what that term means in your context?

The meaning I am most familiar with is in the context of interrupts and task switching, where everything that the main program or a task relies on is saved in RAM, to be restored later. In most cases this amounts to pushing all registers on the stack, so they can be popped later.

Things can get difficult when there is context outside the CPU registers that can be used by the interrupt (or by other tasks), so it must be saved too. Think for instance of floating point coprocessor registers.

On a the old 12 and 14 bit core PICs context saving for an interrupt is a bit tricky, but it is explained in the datasheet, better read it there. Note that on these chips various memory-mapped registers can be context too, like the indirection register. If your interrupt routine uses such registers they must probably be saved (and restored) too.

Real context swapping for tasking switching is not possible on these PICs, because the stack can not be changed. There are some dirty tricks that achieve the same effect (like not using the hardware stack at all), but at a cost.

The 18F PICs and IIRC the enhanced midrange chips too have a stack that can be read and written, so real context switching is possible, but it is tedious. If you want multitasking, better look for a CPU that has a memory-mapped stack. (Nowadays a Cortex would be an obvious choice.)