NES Controller / GameBoy Button Translation

Context saving isn’t for the ISR, it’s to ensure any non-atomic calculations in your main loop that haven’t completed but are interrupted have a chance to finish calculating with the correct intermediate register values restored after the interrupt returns the thread to where it was before the interrupt executed. If you don’t do this then the register values could be dirtied/corrupted from other unrelated calculations performed inside the ISR and while the calculation will still complete the value will not be correct and must be thrown out then. Not necessarily that important to worry about for games and such, but for critical multi-tasking applications it’s necessary to keep in mind.

Ah, I think I see what you mean. ISR must preserve clobbered registers, that normal functions aren’t obliged to restore on return (because an ISR might get invoked where a normal code doesn’t expect a function call). I missed that there are such registers, I somehow thought that a function must restore all used registers, in which case those push/pop instructions wouldn’t be necessary - ISR and each function it calls would just restore all registers automatically.

1 Like