gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > libnds internals - IRQ

#128835 - fogzot - Tue May 15, 2007 8:27 pm

First of all let me say that I am a real newbie here and if this is not the right place to post just redirect me somewhere else. Then let me thank all the people that reverse enginered the DS and especially libnds authors: reading its source code is better than a reference guide (and makes me go back to 6510 assembly and C64 times.) Anyway, here is the question:

I've noted that IRQ handlers are implemented in libnds as an array of (mask, handler) pairs and I don't understand why. Woulnd't a simple array of handlers, indexed by IRQ number be simpler and faster? It seems that this is done to allow for multi-bit masks (i.e., more than one IRQ in the same mask) but I don't see an use for that. Is there some code out there that uses that feature? Is it a really feature?

Thank you for your time, and thank you again.

#128957 - wintermute - Thu May 17, 2007 2:28 am

The mask & handler pair approach allows for pseudo prioritisation of interrupts so that the order you install handlers in determines the order in which they are tested. It also allows for few, widely spaced interrupts to be tested quickly in comparison to the more standard array of handlers approach.

For example, you might want an hblank interrupt to take as few cycles as possible to get to the handler - in this case installing that handler first will cause it to be tested first rather than checking vblank flag then hblank flag.

If you had just a vblank handler and a timer 3 irq installed then only 2 comparisons would be required before entering the timer3 handler rather than 7 with the standard array approach.

When I wrote this code originally I had assumed that there might possibly be a situation where multiple interrupts would require servicing. If both a vblank irq and a gamepak irq were fired then you would most likely prefer that the gamepak irq be processed first. I keep meaning to test for simultaneous irqs but never manage to get around to it - I have noticed some effects which seem to indicate that the hardware does it's own prioritisation on gba at least and prevents multiple bits being set on entry to the dispatcher. This doesn't really make a lot of sense to me though.

Ultimately the gba/nds interrupt code is as flexible as I currently know how to make it so that most users don't need to know all the ins and outs of interrupt handling.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#129022 - fogzot - Thu May 17, 2007 11:09 pm

I understand (an indexed table requires a loop anyway, to convert from the bitmask to the index); thank you for the explanation.