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++ > Putting a C++ member function into IWRAM

#27319 - gb_feedback - Sat Oct 09, 2004 2:24 pm

First a short intro:
I'm using a fairly old version of devkitadv via Visual Studio 6 to develop.
I prefer to use C++ normally as I'm used to it, but also have several working apps for the GBA which use C.
I'm currently writing another pogoshell plug-in / standalone application for displaying GIF files. This is supposed to handle animated GIFs too and is intended to work at a reasonable speed. I'm using a full-screen short excerpt from an animated movie at 12 fps as a sample. Currently it works functionally but is too slow.
So I intended to transfer the speed critical part, to IWRAM as I have so many times before.
However previously I have been using C for this, as in the following example:

In the header file

Code:
 #define CODE_IN_IWRAM __attribute__ ((section (".iwram"), long_call))
extern void ResetGba(void) CODE_IN_IWRAM;


In the .c file which is compiled for ARM (and interworking):

Code:
 CODE_IN_IWRAM void ResetGba(void)
{
...
}


When calling this from the THUMB (and interworking) compiled module:

Code:
 ...
   ResetGba();
...


And this has always worked fine. Now, for this new project, I have been using C++, so I tried:-

In the header file:

Code:
 #define CODE_IN_IWRAM __attribute__ ((section (".iwram"), long_call))

class CGif
{
...
public:
   bool GifDecompressImage (GIFINFO* gifInfo)  CODE_IN_IWRAM;
...
};


In the .cpp file which is compiled for ARM (and interworking):

Code:
CODE_IN_IWRAM bool CGif::GifDecompressImage (GIFINFO* gifInfo)
{
...
}


When calling this from the THUMB (and interworking) compiled module:
Code:

...
   m_gifFunc.GifDecompressImage (&m_gifInfo);
...



Well, as you've realised by now, it doesn't work - just crashes (goes off and executes at some non-existant address).

I realise I've got a lot of work ahead to sort this out as my understanding of ARM assembly code and GCC are minimal (I've just been using them for the results!). But it would be nice to know before I start if anyone else has moved a C++ class member function into IWRAM and successfully called it, or am I asking the impossible?
Remember, the code worked just before I moved the function into IWRAM...

Any ideas, guys?
_________________
http://www.bookreader.co.uk/

#27324 - sajiimori - Sat Oct 09, 2004 6:00 pm

If you don't end up finding a direct solution, you could always make the member function be a wrapper for a plain C function.

#27325 - gb_feedback - Sat Oct 09, 2004 6:47 pm

Thanks for the reply. I was going to wait till I'd fully tracked this down before I posted again. The problem with the wrapper is that the data is so nicely buried in the class that to make a C function would have spoiled the 'elegance' (such as it is). But you're right basically. I would just have re-written the whole thing in C and swallowed my pride. However I've been single stepping through the program with no$gba and noticed that it crashes close to my dma clearScreen routine. I just 'ifed' that out of the code, and, low and behold, it sprang into life.

No idea yet why my clearScreen suddenly failed, but the conclusion is at least that the basic approach to moving a C++ member function into IWRAM, outlined in the original question does at least work. Might be useful to someone...
_________________
http://www.bookreader.co.uk/

#27326 - sajiimori - Sat Oct 09, 2004 7:49 pm

I'm glad you've got it figured out.

The class is still well-encapsulated if the plain C function is file static. The interface needn't show any signs of trickery, unless the wrapper method absolutely has to be inlined.

#27395 - tepples - Tue Oct 12, 2004 5:47 am

sajiimori wrote:
The class is still well-encapsulated if the plain C function is file static.

But then of course you can't compile the ROM code as Thumb instructions and the IWRAM code as ARM instructions, as the current version of GCC doesn't have any mechanism to specify both Thumb and ARM instructions in one module.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#27400 - sajiimori - Tue Oct 12, 2004 6:47 am

...

Make a friend class!! lol

#27408 - wintermute - Tue Oct 12, 2004 1:28 pm

sajiimori wrote:
...

Make a friend class!! lol


there's no reason why you can't put some member functions in separate source files.

#27420 - sajiimori - Tue Oct 12, 2004 6:27 pm

Oh yeah. I'm so confused! ;_; lol