#12957 - maximAL - Tue Dec 02, 2003 4:41 pm
i just experienced that my bitfields do some really strange things O_o
take this simple example:
Code: |
struct Bla
{
vu8 A1:4;
vu8 A2:4;
vu8 B1:4;
vu8 B2:4;
}__attribute__((packed));
//...
Bla *Blubb=(Bla*)0x7000000;
Blubb->A1=1;
|
actually, this should result in 0x0001, right? but it always sets 0x0101! this happens with all values, they are somehow "duplicated" into both bytes!?
#12960 - tepples - Tue Dec 02, 2003 6:11 pm
I see you're trying to write to OAM. Byte writes do not work properly with palette memory, VRAM, or OAM, and GCC may be generating byte writes for bitfield code. I'd suggest not using bitfields because they have an even more sinister downside, namely that different versions of a compiler may lay out bitfields in different ways. If you're trying to write to OAM, use macros to build up values.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#12963 - maximAL - Tue Dec 02, 2003 7:28 pm
hmm, if thats all, i'll simply use a shadow register to manipulate the values und copy it over in one piece.
edit:
mmmkay, i think i got a serious problem here...using bitfields always seems to produce crap. i simply used a second object of the same type, set the bits und copied it via a pointer to the proper register - but obviously the data always gets fucked up in some weird way?! strange, because i think i tried this some time ago and it seemed to work...
#12969 - sajiimori - Tue Dec 02, 2003 9:40 pm
Bitfields in structs are not guaranteed to be packed in the smallest possible space. For instance, a struct with 2 fields of 4 bits each might not be packed into a single byte. Go with tepples' suggestion and use macros.
#12971 - maximAL - Tue Dec 02, 2003 9:47 pm
the __attribute__((packed)) should ensure that it is packed right...should
is it worth trying the DevKitAdv r5 beta 3? at the moment i'm still using the r4...
#12973 - sajiimori - Tue Dec 02, 2003 10:03 pm
AFAIK, the 'packed' attribute only suppresses padding of subword-sized (e.g. char, short) fields to word boundaries. Besides, if it happened to work on only one version of DevkitAdvance, that would only prove tepples' point that you can't rely on bitfields to be arranged in any particular order.