Skip to content

Interrupt handlers in C

greg-king5 edited this page Apr 21, 2021 · 3 revisions

"I have tried to use a timer interrupt to do some work in the background. Unfortunately, as soon as I try to use any not totally trivial functions in my interrupt handler, the program crashes or goes weird."

As with any non-trivial 6502 program, the code generated by cc65 makes use of the zero page. Zero page locations are also used for the argument stack, and as temporary storage for most of the runtime support functions. So, when writing interrupt handlers in C, it is necessary to save the CPU registers plus those zero page locations. In addition to that, the functions that manipulate the stack pointer are not reentrant, so you have to set up a separate stack for the interrupt handler.

That means that interrupt handlers cannot be done without some Assembly code. For many applications, the wrapper code written in Assembly is more than what has to be done in the interrupt handler itself, so I usually do suggest to think about writing the interrupt handler completely in Assembly (provided it is rather short). If you don't want to do that, for some reason or another, you may want to have a look at the function set_irq() declared in the 6502.h header.

Clone this wiki locally