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.

DS development > PACKED attribute

#158845 - ingramb - Thu Jun 19, 2008 9:37 am

It seems that any struct declared as packed is only accessed byte by byte, even if it is aligned properly. I had some structs declared as PACKED "just to be safe", and noticed a problem with some code using slot2 ram. So a struct like this:

struct PACKED MyStruct {
u32 a;
u32 b;
u16 c;
u16 d;
u8 e;
u8 f;
u8 g;
u8 i;
};

will generate byte instructions even when accessing a-d.

Not really a question in here, just a heads up =)

[EDIT]
See here for more info: http://digitalvampire.org/blog/index.php/2006/07/31/why-you-shouldnt-use-__attribute__packed/

#158848 - silent_code - Thu Jun 19, 2008 10:55 am

You could put an union in and merge this structure with another anonymous structure which consists of 16bit or 32bit members.
That way you could r/w the second structure when accessing external RAM and work with the regular structure in internal RAM.


BUT WAIT: Doesn't externam RAM allow only byte reads/writes anyway? Then that may be nonsense, what I posted here... Although you could union-merge your structure with an u8 array of the same size and then work with it when accessing external RAM... you will have to see for yourself what works. (I can't help much more, because the RAM pack I have is for NDSl and I only have a phatty, so I can't use it. :^C )


Just look at libnds' SpriteEntry structure.

I hope this is correct and it helps. ;^D

This will compile, but I haven't tested it:
Code:
struct PACKED MyStruct
{
   u32 a;
   u32 b;
   u16 c;
   u16 d;
   union
   {
      struct
      {
         u8 e;
         u8 f;
         u8 g;
         u8 i;
      };
      struct
      {
         u16 ef;
         u16 gi;
      };
   };
};


EDIT: This specific structure may not need packing at all, if you use the union approach, but I'm not 100% sure. Though, it simply looks like 4 32bit members, which could be easily put into an union (that would be done in a different structure layout, not the one I posted above.) :^)

PS: Why does i come after g, instead of h? ;?p
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158850 - tepples - Thu Jun 19, 2008 12:26 pm

silent_code wrote:
BUT WAIT: Doesn't externam RAM allow only byte reads/writes anyway?

SLOT-2 RAM in the SRAM space (0x0A000000-0x0A00FFFF on DS or 0x0E000000-0x0E00FFFF on GBA) uses only byte reads and byte writes. SLOT-2 RAM in the GBA ROM space (0x08000000-0x09FFFFFF) uses only 16-bit or larger writes.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#158855 - silent_code - Thu Jun 19, 2008 3:14 pm

@ tepples: Let me guess, it's manufacturer dependent which option is implemented, right?
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158858 - Dwedit - Thu Jun 19, 2008 4:43 pm

Nope, just 8-bit writes if it's the 64K SRAM area, and 16-bit writes if it's the 32MB ROM area.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#158861 - silent_code - Thu Jun 19, 2008 6:27 pm

Duh, I'm a stupid! I was in a rush to get out and later it occured to me. Thanks for clearing it up, anyway. :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158901 - tepples - Fri Jun 20, 2008 12:47 pm

silent_code wrote:
@ tepples: Let me guess, it's manufacturer dependent which option is implemented, right?

Many cards designed to run GBA programs implement both options: the GBA ROM area for running GBA programs and the (usually battery- or capacitor-backed) GBA SRAM area for saving persistent data between runs.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.