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 > Odd byte location read error

#108388 - dak - Wed Nov 08, 2006 11:53 am

Hi again,
I've encountered another kink in my code; I opened a file and tryed going through and extracting data. Nabbing a word from an even byte works just fine (i.e.: 0B0C comes to 3083), but when reading from an odd byte -- I get weirdness.

Lets say I have a dword: 0040 001F

And I tell the system to read from byte 1 (40 00), instead of getting 64, it reads the hex in the order of 00 40 (which will be interpreted as 16384). My guess is that it is always reading the first byte of a word and although 40 is first in this number, 00 is the first in it's own byte (thats an uber guess at the situation).

heeeelp! Should I just check for an odd byte and fix this manually?

-dak

#108411 - Lick - Wed Nov 08, 2006 4:07 pm

dak wrote:
Should I just check for an odd byte and fix this manually?

-dak


Yes.
_________________
http://licklick.wordpress.com

#108416 - masscat - Wed Nov 08, 2006 5:10 pm

To quote the ARM reference manual for the LDRH instrustion (load half word - 16bit value):
Quote:
Non halfword-aligned addresses
If the load address is not halfword-aligned, the loaded value is UNPREDICTABLE.

That is, do not do it. Read byte by byte and build the value with shifting and orring or make sure your data is aligned.

Also note the ARM is little endian (unless you have changed it). Therefore if you have the following bytes stored in memory:

Code:
Address     Byte value
0x02000000  0x12
0x02000001  0x34
0x02000002  0x56
0x02000003  0x78

Reading a 32bit value from address 0x02000000 will place the value 0x78563412 in the register.
At which end of your boiled egg do you start to eat?

#108437 - dak - Wed Nov 08, 2006 8:32 pm

thx guys - much appreciated

#108450 - dak - Wed Nov 08, 2006 10:48 pm

I just spoke to a buddy of mine, and he says that there should be compiler options to auto-magically align odd-located words. Is there any possibility that this is the case (read would help) in this situation?

#108451 - sajiimori - Wed Nov 08, 2006 11:05 pm

The compiler has no control over the contents of your files, and it has no idea what you're going to do with them. If you load your file starting at a word-aligned address, and you want to read a half-word at an odd offset into the file, then it'll break without extra care.

I'd suggest writing read16 and read32 functions that automatically handle unaligned reads.

#108465 - tepples - Thu Nov 09, 2006 4:15 am

Untested, but the functions should look somewhat like the following. Use the m series for big-endian (Motorola; network byte order) data, and use the i series for little-endian (Intel) data.
Code:
unsigned int peeki32(const unsigned char *src) {
  return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}

unsigned int peeki16(const unsigned char *src) {
  return src[0] | (src[1] << 8);
}

unsigned int peekm32(const unsigned char *src) {
  return src[3] | (src[2] << 8) | (src[1] << 16) | (src[0] << 24);
}

unsigned int peekm16(const unsigned char *src) {
  return src[1] | (src[0] << 8);
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.