Darrel's comments

Darrel's comments on wsave variables for 14 bit core
18 Feb, 2006

When you get an interrupt, the processor could be in any bank, it depends on what it's doing when the interrupt happens.

The W and STATUS register must be saved before anything else is done, because doing anything else will change the w or STATUS register. So the W reg has to be saved in whatever bank is currently selected. And each bank (that has GP RAM) must have a location to save W (wsave1-3).

The addresses of the wsave vars are chosen to be in the exact same place in each bank, So that no matter which bank is selected, wsave points to the correct location.

When the registers are being restored, the STATUS register is always restored before the W register. And since STATUS controls the BANK, it's always left pointing to the same bank it was in before W was saved to begin with and always restores the correct wsave?.

Many 16F's also have "Access RAM", usually at address $70, but some are at a different address. Access RAM is common between all the banks. If you assign wsave to Access RAM then you don't need wsave1-3. If the chip has Access RAM, it's best to use that for wsave.

If the PIC has more than 2K of program space, then the flash memory is divided into "Pages". Since the interrupt handler in your PBP program could end up in any Page, PBP must set PCLATH to the correct page before doing a GOTO to the handler.

As before, doing anything else before saving the W register will change the W register. So PBP has to do the context saving before it can jump to your handler. That's why you only need to save context if the chip has 2k or less.

If the chip has more than 2K and you try to save context again ... you'll be saving registers that have already been changed, and PBP will crash when the wrong thing gets restored.

In either instance, you always have to restore context. Whether it was saved by you, or by PBP.

18F's are completely different. There are no "Pages" in program memory.

Page last modified on March 07, 2018, at 04:26 PM