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.

Coding > preemptive multitasking experiment

#9034 - sajiimori - Sat Jul 26, 2003 9:51 pm

I don't know if anybody has done this before on GBA, but I'm attempting to construct a simple preemptive multitasking environment, and I wanted to see if I could get some feedback.

The basic idea is to set up TIMER0 to trigger interrupts upon overflow, and have the interrupt handler save the current CPU state and load the state for the next thread.

The problem is that the BIOS saves the CPU state upon entering an interrupt, and restores it upon exiting. This behavior is incompatable with the idea of writing an interrupt handler whose purpose is to change the CPU state, such as a task switcher.

I can think of two possible solutions:

1) Don't return to the BIOS. Just re-enable interrupts and jump to the new thread. I'm concerned that the BIOS does other cleanup work, besides restoring the CPU state, such as stack unwinding. If so, triggering an interrupt multiple times without ever returning to the BIOS could result in a stack overflow or other bad things.

2) Hijack the CPU state saved in the BIOS and replace it with the state of the new thread, so the BIOS will "return" to a location of my choice. I'm biased toward this idea simply for its coolness factor, but I don't really know how to do it. I would have to know the location and format of the saved state.

It's unfortunate that the BIOS is inflexible regarding this behavior. Any ideas on working around it?

#9048 - Geno - Sun Jul 27, 2003 3:07 am

Read the CowBite and GBATek specifications on the GBA.
_________________
Out of Order.

#9087 - torne - Mon Jul 28, 2003 10:41 am

You don't need to return to the BIOS (though you will need to pop the things it pushed in order to avoid running out of IRQ stack). The GBATEK doc tells you what the BIOS interrupt handler does: after returning from the user interrupt handler, it only does
Code:
ldmfd  r13!,r0-r3,r12,r14
subs   r15,r14,4h


So, you will need to pop those values (or increment SP to throw them away)., and then return into your next thread by writing to PC with S set.

Torne

#9231 - sajiimori - Thu Jul 31, 2003 3:04 am

Great, thanks!