Struct assignment problem.

support for the ARM toolchain
Post Reply
etherealpanda
Posts: 2
Joined: Fri Oct 30, 2009 5:24 pm

Struct assignment problem.

Post by etherealpanda » Fri Oct 30, 2009 5:49 pm

I've been running into a bit of an odd problem when trying to assign one struct to another. I'm working with some sample code involving putting a sprite at column 50 and row 50, but if I do this via a struct assignment, the sprite is locked in the wrong position. However, if I assign each attribute manually, it works fine.

Although I'm still somewhat of a C novice, I believe the following should be functionally equivalent.

Code: Select all

// Struct assignment:
OAM[0] = shadowOAM[0];

Code: Select all

// Attribute by attribute assignment:
OAM[0].attr0 = shadowOAM[0].attr0;
OAM[0].attr1 = shadowOAM[0].attr1;
OAM[0].attr2 = shadowOAM[0].attr2;
An interesting thing to note is that the following also doesn't work. Which seems to imply the struct assignment actually corrupts the data.

Code: Select all

OAM[0].attr0 = shadowOAM[0].attr0;
OAM[0].attr1 = shadowOAM[0].attr1;
OAM[0].attr2 = shadowOAM[0].attr2;
OAM[0] = shadowOAM[0];
The struct declarations are as follows:

Code: Select all

// Struct for interfacing with sprite registers
typedef struct { 
    unsigned short attr0; 
    unsigned short attr1; 
    unsigned short attr2; 
    unsigned short fill; 
}OBJ_ATTR;

// Start of sprite registers 
#define OAM ((OBJ_ATTR*)(0x7000000))

// Sprite registers buffer
OBJ_ATTR shadowOAM[128];
I actually compiled both versions down to assembly, and the following is the diff:

Code: Select all

@ Struct assignment version 
      mov     r3, #117440512
      ldr     r2, .L3+8
      mov     ip, #8 
      mov     r0, r3
      mov     r1, r2
      mov     r2, ip
      bl      memcpy

@ Attribute by attribute assignment
      mov     r2, #117440512
      ldr     r3, .L3+8
      ldrh    r3, [r3, #0]
      strh    r3, [r2, #0]    @ movhi
      mov     r2, #117440512
      ldr     r3, .L3+8
      ldrh    r3, [r3, #2]
      strh    r3, [r2, #2]    @ movhi
      mov     r2, #117440512
      ldr     r3, .L3+8
      ldrh    r3, [r3, #4]
      strh    r3, [r2, #4]    @ movhi

@ Reference
.L3:
        .word   Pikachu7Pal
        .word   Pikachu7Tiles
        .word   shadowOAM
        .size   main, .-main
        .comm   shadowOAM,1024,2
        .ident  "GCC: (devkitARM release 24) 4.3.2"
Both versions look right to me, though my assembly is a bit rusty. I'm assuming that memcpy is a function created by the compiler, because I didn't include string.h or call it manually.

It's possible that there is something completely obvious that I'm missing, but I wanted to ask the forum to see if you had any insight into what's causing this problem. I've attached a stripped down example of the problem including my makefile. Uncomment line 61 in main.c to replicate the problem.

Thanks in advance to anyone willing to help!

Stripped down code:
http://www.etherealpanda.com/devkitarm-code.zip

Edit:
This is code for the GBA. I'm using the visualboyadvanced emulator.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Struct assignment problem.

Post by elhobbs » Fri Oct 30, 2009 7:54 pm

the problem is most likely the memcpy call. gcc can use memcpy to copy structs like this. memcpy may choose to do 8 bit writes so you need to avoid using it where it may be writing to ds/gba memory that does not support 8 bit writes. you are probably best off using one of the dma copy functions.

etherealpanda
Posts: 2
Joined: Fri Oct 30, 2009 5:24 pm

Re: Struct assignment problem.

Post by etherealpanda » Fri Oct 30, 2009 8:06 pm

I suspected this might be the problem. Thanks a bunch!

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests