Download Tonc : GBA programming in rot13

Transcript
Tonc : GBA programming in rot13
file:///E:/dev/gba/proj/tonc/bak/tonc.htm
}
return 0;
}
The code listing above contains the main demo code, the HBlank, and VCount isrs that will be routed and some sundry
items for convenience. The C master isr called hbl_grad_direct() is in another file, which will be discussed later.
First, the contents of the interrupt service routines (points 1 and 2). Both routines are pretty simple: the HBlank routine
(hbl_grad_routed()) uses the value of the scanline counter to set a color for the backdrop. At the top, REG_VCOUNT is 0,
so the color will be blue; at the bottom, it'll be 160/8=20, so it's somewhere between blue and red: purple. Now, you may
notice that the first scanline is actually red and not blue: this is because a) the HBlank interrupt occurs after the scanline
(which has caused trouble before in the DMA demo) and b) because HBlanks happen during the VBlank as well, so that the
color for line 0 is set at REG_VCOUNT=227, which will give a bright red color.
The VCount routine sets the color to red and them waits until scanline 120. At point 3, the VCount irq is set up to be
triggered at 80, so that this routine will wait for 40 scanlines until moving out of the service routine again. In the mean time,
HBlank irqs happen as they always did. Or not, of course. This is where you see what prioritized and nested interrupts actually
do. Allowing them to nest means that the HBlank interrupts can still act even though the VCount routine isn't finished yet; the
result is a single red line (fig 16.1a). Prioritized means that you can disallow the HBlank from breaking into the VCount if the
VCount interrupt has a higher priority, resulting in a red block because the next HBlank interrupt isn't handled until the 120th
line (fig 16.1b). Now, it is possible to do the prioritizing inside the isr itself, but it's and probably faster to do it in the master
isr, which is what I've done with isr_master_nest().
Point 3 is where the interrupts are set up in the first place. The call to irq_init() clears the isr table and sets up the
master isr. Its argument can be NULL, in which case the tonc's default master isr is used. The calls to irq_add() initialize the
HBlank and VCount interrupts and their service routines. If you don't supply a service routine, the switchboard will just
acknowledge the interrupt and return. There are times when this is useful, as we'll see in the next chapter. irq_add() already
takes care of both REG_IE and the IRQ bits in REG_DISPSTAT; what it doesn't do yet is set the VCount at which the interrupt
should be triggered, so this is done separately. Note that the oder in which irq_add() is called matters: the first irq called has
the highest priority.
You can switch between master service routines with irq_set_master(), as is done at point 4. While irq_add() just
fills the isr table and doesn't really select a priority, irq_set() does (point 5). The last parameter allows you to add the isr at
that priority and moves the rest out of the way. There are more options for it, but this will do for now. Note that changing the
priorities wouldn't happen often, but it's still useful to have something around that can do that.
Fig 16.1a: Gradient; nested HBlank has
priority.
Fig 16.1b: Gradient; nested VCount has
priority.
Fig 16.1c: Gradient; HBlank in master ISR in
C.
This explains most of what the demo can show. For Real Life use, irq_init() and irq_add() are pretty much all you
need, but the demo shows some other interesting things as well. Also interesting is that the result is actually a little different for
VBA, no$gba and hardware, which brings up another point: interrupts are time-critical routines, and emulating timing is rather
tricky. If something works on an emulator but not hardware, interrupts are a good place to start looking.
This almost concludes demo section, except for one thing: the direct HBlank isr in C. But to do that, we need it in ARM
code and to make it efficient, it should be in IWRAM as well. And here's how we do that.
16.6.1. Using ARM + IWRAM code
The master interrupt routines have to be ARM code. As we've always compiled to THUMB code, this would be something
new. The reason that we've always compiled to THUMB code is that the 16bit buses of the normal code sections make
ARM-code slow there. However, what we could do is put the ARM code in IWRAM, which has a 32bit bus (and no waitstates)
so that it's actually beneficial to use ARM code there.
Compiling as ARM code is actually quite simple: use -marm instead of -mthumb. The IWRAM part is what causes the
143 of 309
2007-01-23 23:41