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.

C/C++ > Strange behaviour with function pointers

#167286 - Kariddi - Sun Mar 08, 2009 1:35 am

Hi.
I'm experimenting with NDS programming and , for what i'm trying to do, I need to use some "function pointers".

What I'm trying to do is loading some code in the RAM , then launching that code by running the "main" of that code using a function pointer.

This is the code in main:

Code:

   consoleDemoInit();
   fatInitDefault();
   tableInit();
   iprintf("Caricamento programma\n");
   load_program(prog_str);
   main_f(0, NULL);
   iprintf("Fine main\n");


I have these pointers:
Code:

address** __table = (address**) TABLE_ADDRESS;

address prog = NULL;
main_function main_f = NULL;


Where __table is a table that should contain addresses of functions that the program I'm loading into ram should be able to use.
TABLE_ADDRESS is defined as 0x03000000 (should be Shared WRAM, right?).

The function that can be called from the loaded program is :
Code:

void funzione()
{
   iprintf("It works!!\n");
}


and tableInit() is a function that initializes the table by loading the address of "funzione()" into __table[0] :

Code:

void tableInit()
{
   __table[0] = (address*) &funzione;
}


The code of the program I'm loading into the ram simply calls "funzione()" into its main and returns.

The strange behavior is this:

When I'm running this program into an emulator everything works like should. (I've tried No$GBA and desmume)
The program is loaded into ram, the execution switch to "main_f" (that is the main of the loaded program) "funzione()" is called from the loaded program and shows "It Works!" and then exits.

When I run this program on REAL hardware (my DS) the result is that when "tableInit()" is called instead of setting __table[0] to the right value it calls "funzione()" (really strange!!) and then when the execution passes to "main_f" the screen blanks out (probably due to the fact that tableInit() didn't do what it was meant to).

Do you have any idea why on a real DS this line :
Code:

__table[0] = (address*) &funzione;


instead of setting __table[0] at the right value calls "funzione()"???

Thanks for your help

Kariddi

#167288 - Maxxie - Sun Mar 08, 2009 8:20 am

Kariddi wrote:

TABLE_ADDRESS is defined as 0x03000000 (should be Shared WRAM, right?).


Have you assigned the shared wram to the desired CPU?
It's not accessible to both CPUs at the same time. The name might confuse here.

http://nocash.emubase.de/gbatek.htm#dsmemorycontrolwram

Beware: the shared wram is used for the arm7 binary.
_________________
Trying to bring more detail into understanding the wireless hardware

#167292 - Dwedit - Sun Mar 08, 2009 8:57 am

I think Main Memory is shared, only issue with using that is the cache, you must invalidate or flush the cache if you want to exchange data that way. Probably best to use Libnds's FIFO to send messages to the ARM7.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."