yupferris/rustendo64

Endianness related operations in the standard library

pmarcelll opened this issue · 4 comments

Hi, I watched your video on Youtube, and I remembered that the standard library has endianness related functions. They are implemented by all integer types.
i hope this helps...

Thanks for the tip!

I did a quick skim and found from_be, from_le, swap_bytes etc, but afaik those wouldn't help in our case; perhaps I'm missing something?

I don't think I mentioned this in the videos (in fact I think I got a bit overwhelmed by details and mentioned the opposite by mistake), but my understanding of << and >> is that they don't actually shift in a particular direction related to endianness; x >> 2 is always equivalent to x / 4 regardless of endianness of the host CPU. What we need is a way to deserialize bytes that we know are in big-endian order into a u32 on our host CPU; beyond that we can just think of the value as all 32/64 bits consecutively in big-endian form and do all the bit arithmetic we please. Endianness of the host CPU should only matter if we're interpreting bytes that are to from/written from the CPU; for example, casting a u8 pointer to a u32 pointer and dereferencing.

like it got mention in the stream you should use the byteorder crate then you land up with something like this
BigEndian::read_u32(&self.pif_rom[rel_addr as usize..])

Yes, I was planning on using ByteOrder for that deserialization, but as I was unfamiliar with the library on the last stream, I just went for manual reading/shifting to get to code execution faster :)

Thanks for providing that little sample snippet; you saved me lots of time digging through documentation to figure out what we'd need! We'll clean up this bit of code in the next session.

emoon commented

Yeah I can recommend the byteorder create as well :) I use it for parsing Amiga executables (which are also big endian)