Learn Electronics

Byte Swapping (BIOS Files)

5min

A common requirement when programming EEPROM BIOS chips is if the console requires the bytes to be swapped.

Let's use a Mega CD as an example.

Here is the Mega CD 1 BIOS in its normal (little-endian) format, usable by emulators.

Mega CD BIOS
Mega CD BIOS
īģŋ

Notice each hexadecimal value (FF for example) represents 256 possible values, so 8 bits also known as 1 byte.

That means each 2 digits in hex is 1 byte.

Big Endian vs Little Endian

The Mega CD reads the BIOS as 16 bit values (so 2 bytes), and the Mega CD reads them in big-endian format. Whereas PC and all emulators are little-endian.

In short, it just refers to the order that bytes are read in.

On PC and most systems, it is little-endian. This means for a 16 bit format (the 2 bytes making up the 16 bits) lets say FD 00, are read by PC in little endian as FD00 (the least significant byte being 00, the Most significant byte being FD).

If we put that FD00 into the Windows Calculator (as Windows is Little-Endian) it will show the value 64,768.

Windows Calculator
Windows Calculator
īģŋ

However, the Mega CD is Big-Endian, so it would ready the first byte FD as the LSB (least significant byte) and 00 as the MSB (most significant byte). This would be the equivalent of 00 FD, which is 253, not 64,768.

Windows Calculator Big Endian
Windows Calculator Big Endian
īģŋ

Byte Swapping

So the goal of the above, is to flip every 2 bytes around before we program the EEPROM, so the little-endian BIOS file becomes big-endian.

If we notice the first row is FF FF FD 00 00 00 04 26 and so on, the groups would be FFFF FD00 0000 0426 and so on.

When we byte-swap, we flip each byte around in each group of two.

So FF and FF are the same, FD 00 however would be swapped, to become 00 FD, and the 04 26 would become 26 04.

In short, take every 2 bytes, and flip them.

Let's group them visually first.

Mega CD BIOS Close Up
Mega CD BIOS Close Up
īģŋ

Now let's flip the bytes around.

This is basically converting little-endian format to big-endian.

Byte Swapped Mega CD BIOS Close Up
Byte Swapped Mega CD BIOS Close Up
īģŋ

Notice the FD00 has become 00FD.

This is more obvious when you look at the embedded text in the file.

Notice how the SEGA text is flipped every byte, so instead of SE it is ES, then instead of GA it is AG.

Document image
īģŋ
Byte Swapped Mega CD BIOS Text
Byte Swapped Mega CD BIOS Text
īģŋ

How To Byte Swap

If you have the BIOS file you want to byte swap, you must first find out what format the system reads the data in (8 bit, 16 bit, 32 bit) and then use a hex editor tool such as HexToolkit to byte swap.

In HexToolkit select the interpretation to the correct size and click Byte Swap then Export.

I personally use Hex Editor Neo, open the file, select Operations > Byte Swap > Words.

Byte Swap
Byte Swap
īģŋ

You must select the format size in order to flip the MSB and LSB sections correctly.

For example, 16 bit systems are 2 bytes, so one byte in each two is swapped, also known as Word swapping (as a WORD is 2 bytes long).

Byte swapping (8 bit) is swapping the upper 4 and lower 4 bits of a single byte.

Double Word swapping (32 bit) swaps 2 bytes at a time, so FFAA BBCC becomes BBCC FFAA. If you Word swapped (instead of Double Word swapped) a 32 bit format file, it would incorrectly swap every 2 bytes and become AA FF CC BB.

So you see the importance of understanding not only byte swapping but the end format the file is being read in (what chunks of data at a time, 8/16/32/64 bit).