Re: [cc65] Interrupts and break

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2001-09-30 20:01:25


Hi!

On Sun, Sep 30, 2001 at 06:50:08PM +0200, Daniel Berntsson wrote:
> I've tried to use a timer interrupt to check the joysticks and update
> some structs and sprites. (on the c64, or well, vice) Unfortunately, as
> soon as I try to use any not totally trivial functions in my (probably
> wrongly set up) interrupt handler the program crashes or goes weird. I
> haven't found any mentions of how to do interrupts in C or cc65 and no
> good examples anywhere. It would be nice if someone could tell me or
> point me to the correct cc65 way of doing this.

As any non trivial 6502 program, the code generated by cc65 makes use of the
zero page. Zero page locations are 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.

As you can see, it cannot be done without some assembler code. For many
applications, the wrapper code 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 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
break handler. It contains all code that would also be needed for an interrupt
handler (saving registers and zero page locations, calling a C function).

> And another thing; When the brk_handler function returns, it returns to
> the BRK instruction and not the next one. So it will be called again and
> again and.. Is this really how it is supposed to be?

Yes, it is supposed to work like that. The break handler was thought to be
part of a debugger (probably the most common usage). A debugger uses the BRK
instruction to insert breakpoints, and - if a break occurs - it needs the
address of the BRK instruction to locate the breakpoint in the active
breakpoint list.

Because the break handler does not know, what to skip (just the break? the
instruction that was replaced by the break? the two bytes that are skipped by
the 6502 on occurrance of a break?), it is best to not skip anything. Having
the PC point to the break instruction itself, the application (which has more
knowledge) can handle the situation.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:42 CET