Use both CPU's at the same time

Post Reply
Antoine_935
Posts: 11
Joined: Mon May 11, 2009 1:11 am

Use both CPU's at the same time

Post by Antoine_935 » Mon May 11, 2009 1:14 am

Hi there.

I'm new to nds programming, and I was looking for a way to execute code on both cpu's at a time. Kind of threads.
Is it possible ?

BTW: Thank you very much for the great work libnds and devkitARM represent !

weirdfox
Posts: 29
Joined: Thu Feb 19, 2009 8:45 am
Location: Montreal, Canada
Contact:

Re: Use both CPU's at the same time

Post by weirdfox » Mon May 11, 2009 3:35 am

Well, yes it is. BUT, communication between the two processors is quite hard...

First you have to make two programs, one compiler to run on the arm7 and one compiled to run on the arm9 (see "templates/combined" in the examples to see how to build the two programs in your nds image)

If you make your own arm7 program, you must remember to call the arm7 version of the libs that require it (dswifi, maxmod, etc. : see the default arm7 code for this)

For comunication between the CPUs, I'll let others tell you how as I'm still not sure on how to do it right... If I recall correctly, it's done by the FIFOs(maybe reading fifocommon.h cound help) using the shared ram([1] [2]), but that's all I know as I never had time to try more on multi-cpu programming on the NDS...
try, crash, debug and learn :)

Antoine_935
Posts: 11
Joined: Mon May 11, 2009 1:11 am

Re: Use both CPU's at the same time

Post by Antoine_935 » Mon May 11, 2009 10:58 am

Thank you for this helpful answer :)

I just had a look at fifocommon.h, looks interresting.
However I have a question about it: there's a callback for addresses, u32, but not for messages... what about it ?

fifoSendAddress ==> fifoSetAddressHandler
fifoSendValue32 ==> fifoSetValue32Handler
fifoSendDatamsg ==> ?

And is there any lock mechanism available (ok, I guess it's not really needed with the fifo, but that would be easier to handle, maybe) ?

WinterMute
Site Admin
Posts: 2003
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Use both CPU's at the same time

Post by WinterMute » Mon May 11, 2009 10:59 am

It really depends on what you want to do. Given that the ARM7 has only 96K of available RAM and sustained use of the shared 4meg EWRAM will impact the ARM9 performance it's generally not a good idea.

It's best to leave the ARM7 to it's assigned tasks and not attempt "threading".
Help keep devkitPro toolchains free, Donate today

Personal Blog

WinterMute
Site Admin
Posts: 2003
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Use both CPU's at the same time

Post by WinterMute » Mon May 11, 2009 11:11 am

Antoine_935 wrote:
fifoSendDatamsg ==> ?
fifoSetDatamsgHandler
And is there any lock mechanism available (ok, I guess it's not really needed with the fifo, but that would be easier to handle, maybe) ?

That's one of the things that will cause issues - if the ARM7 sits in a loop waiting for a value in EWRAM to change then it takes most of the bus & in some cases has been known to halt the ARM9 entirely. It's probably better to initiate small tasks via FIFO and make use of the handlers.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Antoine_935
Posts: 11
Joined: Mon May 11, 2009 1:11 am

Re: Use both CPU's at the same time

Post by Antoine_935 » Mon May 11, 2009 5:53 pm

It's best to leave the ARM7 to it's assigned tasks and not attempt "threading".
Yes, for a game it looks like a bad idea to do so. Moreover, if you tell so I guess I'd better trust you.

However, my case is slightly different. The program will run 2 separate tasks (ok, somewhat linked, but not much). One of them will require some time to be finished, and the other one will be a small tetris, in order to help the user wait.
This tetris should not require that much ram or processing power, but the other task will.
That's why I thought about using both CPU's.

Knowing this, is it still a bad idea, or do you think it can be done correctly ?
Given that the ARM7 has only 96K of available RAM
How can I explicitly access this ram ? Or maybe it's the default ?
fifoSetDatamsgHandler
Mmmh, so obvious... thank you :)

WinterMute
Site Admin
Posts: 2003
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Use both CPU's at the same time

Post by WinterMute » Tue May 12, 2009 12:57 am

Antoine_935 wrote:[
However, my case is slightly different. The program will run 2 separate tasks (ok, somewhat linked, but not much). One of them will require some time to be finished, and the other one will be a small tetris, in order to help the user wait.
This tetris should not require that much ram or processing power, but the other task will.
That's why I thought about using both CPU's.


Knowing this, is it still a bad idea, or do you think it can be done correctly ?
Unfortunately the ARM7 has no access to graphics hardware in DS mode and the ARM9 is the more powerful processor so dual CPU probably isn't the best solution.

Tetris isn't at all resource intensive though so you could run both on the ARM9 relatively easily. Using maxmod will offload music & sound effects to the ARM7 anyway so all you really need to worry about is the game loop itself. Given that pretty much all games consist of a main loop which waits for vblank at some point all you really need to do is get rid of the the wait & the loop then put the code into the vblank interrupt.

Instead of

Code: Select all

while(1){
  read input
  move objects
  update screen
  swiWaitForVblank();
}
you have

Code: Select all

void TetrisVblank() {
  read input
  move objects
  update screen
}
 
int main(void) {
 
  irqSet(IRQ_VBLANK, TetrisVblank);

  ...

  resource intensive stuff
  ...
}
I'd do maxmod init before starting the vblank.

Note: Avoid stdio & malloc inside interrupt code, things will break.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Antoine_935
Posts: 11
Joined: Mon May 11, 2009 1:11 am

Re: Use both CPU's at the same time

Post by Antoine_935 » Tue May 12, 2009 2:04 pm

Thank you very much for these answers :)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests