How do I byteswap a SNES rom?

A place that you can discuss reproduction of classic titles or "licensed-for-reproduction" homebrew for personal use.

Moderators: B00daW, Moderators

Forum rules
1. NO BLATANT PIRACY. This includes reproducing homebrew less than 10 years old, with the exception of free software.
2. No advertising your reproductions, with the exception of free software.
3. Be nice. See RFC 1855 if you aren't sure what this means.
Post Reply
Nidhoegger
Posts: 19
Joined: Sun Jun 14, 2009 6:35 am

How do I byteswap a SNES rom?

Post by Nidhoegger »

Hi,

I am trying to make a repro of a romhack to play on real hardware. I found the FeRAM project on github. The only thing I am struggling with is that it states the rom needs to be byteswapped. What does that mean? How is it done? I am thankful for all points in the right direction.

Thanks!

Nidhoegger
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: How do I byteswap a SNES rom?

Post by lidnariq »

Whether the ROM needs to be byteswapped depends on your programmer.

16-bit ROMs can be assumed to need to be programmed little-endian or big-endian, and the programmer just needs to agree with whatever's using the 'PROM afterwards.

Your programmer should have this functionality built-in. Otherwise, almost any hex editor will also be usable.
User avatar
jeffythedragonslayer
Posts: 344
Joined: Thu Dec 09, 2021 12:29 pm

Re: How do I byteswap a SNES rom?

Post by jeffythedragonslayer »

This example visualizes how byteswapping switches adjacent characters around:

https://www.reddit.com/r/emulation/comm ... lained_for

Oh and FYI, we do have a dedicated SNES section on this forum.
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: How do I byteswap a SNES rom?

Post by Fiskbit »

This repro subforum is for SNES, as well.
Nidhoegger
Posts: 19
Joined: Sun Jun 14, 2009 6:35 am

Re: How do I byteswap a SNES rom?

Post by Nidhoegger »

jeffythedragonslayer wrote: Sat Mar 04, 2023 2:04 am This example visualizes how byteswapping switches adjacent characters around:

https://www.reddit.com/r/emulation/comm ... lained_for

Oh and FYI, we do have a dedicated SNES section on this forum.
When trying to post in the SNES Section is explicitly redirects me to here for questions about repros. Thank you for the Link, ill have a look at it.

EDIT: No Idea if something is wrong with my Browser, but I only see a blank space where I guess the visualizationshould be...
Bavi_H
Posts: 193
Joined: Sun Mar 03, 2013 1:52 am
Location: Texas, USA
Contact:

Re: How do I byteswap a SNES rom?

Post by Bavi_H »

WHAT DOES BYTE SWAPPING MEAN?

The following diagram shows the hex values in a sample 6-byte file before and after byte swapping it:

Code: Select all

Before: 0A 0B 0C 0D 0E 0F
          ×     ×     ×
 After: 0B 0A 0D 0C 0F 0E

MAYBE JUST TRY BOTH WAYS

If the ROM programming process is quick and reprogramming a ROM is not harmful, then maybe you can just try both ways: program the ROM with the original file and if that doesn't work try byte swapping the file then reprogramming the ROM with that. One way should work. See the bottom of this post for a way to byte swap the file on a Windows computer.

If reprogramming the ROM is not feasible or you want to understand when byte swapping the file is required, read the next section.


IS BYTE SWAPPING REQUIRED?

To learn what little-endian and big-endian mean, look at the Wikipedia article Endianness, especially the example diagram. The example in the article is 32 bits (like hex 0A0B0C0D), but the cases in this thread will be 16 bits (like hex 0A0B).
lidnariq wrote: Fri Mar 03, 2023 4:44 pmWhether the ROM needs to be byteswapped depends on your programmer [...] little-endian or big-endian [...] the programmer just needs to agree with whatever's using the 'PROM afterwards.
More specifically, we can tell if byte swapping the file is needed once we know both of the following things:
A. What endianness the ROM programmer is expecting the file to be in, and
B. What endianness the SNES FeRAM Cart is using to the present the physical 16-bit ROM as an 8-bit ROM to the CPU.

For A, Presumably the ROM programmer used to write the 16-bit ROM will have some documentation that indicates if it reads from files using little-endian or big-endian ordering. Nidhoegger, if you can't tell what file endianness your ROM programmer needs, I guess you can tell us your ROM programmer's brand and model and we can search online for more information.

But for B, can anyone tell if the SNES FeRAM Cart "converts" the physical 16-bit ROM into a virtual 8-bit ROM using little-ending or big-endian ordering?

As lidnariq says, if the ROM programmer has an option to choose the endianness of the file, then that can be used instead of byte swapping the file. Just choose whichever endianness the circuit is using.

If the ROM programmer requires one kind of endianness but the circuit expects the other type of endianness, then you should byte swap the file before the ROM programmer reads it.

Take a look at the following images for the four possible correct cases.

Legend for the images:

(1) the file: (a) without byte swapping, (b) after byte swapping.
(2) the ROM programmer interprets the file: (a) in little-endian order, (b) in big-endian order.
(3) the physical ROM.
(4) the circuit presents the physical ROM's values: (a) in little-endian order, (b) in big-endian order.
(5) what the CPU sees.

1-little.png
1-little.png (11.56 KiB) Viewed 1894 times
2-big.png
3-swap-big-little.png
4-swap-little-big.png


HOW TO BYTE SWAP A FILE

Here's a way to byte swap a file on a Windows computer:

1. Download the file binutils-2.32-1-mingw32-bin.tar.xz

2. Use 7-zip to open the file, find the objcopy.exe file inside and extract it.

3. In a Command Prompt, enter the the following command:

objcopy -I binary -O binary --reverse-bytes=2 inputfile.bin outputfile.bin

where inputfile.bin is the original file and outputfile.bin will be the byte-swapped file.

Lidnariq also suggests almost any hex editor can do this byte swapping. But in my experience with two hex editors on Windows (XVI32 and HxD), they don't have byte swapping functions. And for a past project I remember searching for a hex editor that could swap the endianness of a file but didn't find one. However, I was only focused on finding tools for Windows and my search was very quick.

Lidnariq (or anyone else), can you suggest any specific hex editors (or any other tools) that can byte swap a file?
Markfrizb
Posts: 607
Joined: Sun Dec 02, 2012 8:17 am
Location: East Texas

Re: How do I byteswap a SNES rom?

Post by Markfrizb »

Lidnariq also suggests almost any hex editor can do this byte swapping. But in my experience with two hex editors on Windows (XVI32 and HxD), they don't have byte swapping functions. And for a past project I remember searching for a hex editor that could swap the endianness of a file but didn't find one.
Winhex is a good one and it does byte swap (and other swaps too) easily.

I’m unclear why you need/want byte swap to begin with? I’ve NEVER had to byteswap anything for a repo. If we were talking about Genesis carts, then yes, byte swapping makes sense for a repo to run on OEM carts.
Bavi_H
Posts: 193
Joined: Sun Mar 03, 2013 1:52 am
Location: Texas, USA
Contact:

Re: How do I byteswap a SNES rom?

Post by Bavi_H »

Markfrizb wrote: Sat Mar 04, 2023 5:39 pmI’m unclear why you need/want byte swap to begin with? I’ve NEVER had to byteswap anything for a repo.
This particular cart (the SNES FeRAM Cart) is built with a 16-bit ROM -- that is, each address in the ROM chip outputs 16 data bits -- but the SNES expects 8 data bits, and files on computers also store values in byte units. As you probably know, any time you have to break down a multi-byte value into individual bytes, there's two ways you can order the bytes. If the writing process orders the bytes one way and the reading process orders the bytes the other way, you'll need to compensate somehow, such as byte swapping the source file before using it to burn the ROM.

If the cart was built on a ROM that outputs 8 data bits, then the processes would be based on bytes all the way through and no byte ordering differences could happen.
Markfrizb
Posts: 607
Joined: Sun Dec 02, 2012 8:17 am
Location: East Texas

Re: How do I byteswap a SNES rom?

Post by Markfrizb »

Bavi_H wrote: Sat Mar 04, 2023 10:25 pm
Markfrizb wrote: Sat Mar 04, 2023 5:39 pmI’m unclear why you need/want byte swap to begin with? I’ve NEVER had to byteswap anything for a repo.
This particular cart (the SNES FeRAM Cart) is built with a 16-bit ROM -- that is, each address in the ROM chip outputs 16 data bits -- but the SNES expects 8 data bits, and files on computers also store values in byte units. As you probably know, any time you have to break down a multi-byte value into individual bytes, there's two ways you can order the bytes. If the writing process orders the bytes one way and the reading process orders the bytes the other way, you'll need to compensate somehow, such as byte swapping the source file before using it to burn the ROM.

If the cart was built on a ROM that outputs 8 data bits, then the processes would be based on bytes all the way through and no byte ordering differences could happen.
If I understand correctly, this cart when it was being designed, the authors pinned the rom such that a “non-native” programming of the rom would be needed? Basically a mistake but easily corrected by byteswapping. I have never used that cart pcb before so I didn’t realize its issue. We make our own carts so I never looked into this one.
Post Reply