Interrupt handlers in C

I've 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 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 these zero page locations. In addition to that, the functions that manipulate the stack pointer are not reentrant, so you have to setup a separate stack for the interrupt handler.

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

cc65/interrupt_handlers_in_c.txt · Last modified: 2012-01-21 13:34 by
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki