|
Prerequisite Math Crash Course
Before you start hacking ANYTHING, you should know a bit about how the ROM is structured, and about bits, bytes, binary and hexidecimal numbering.
Let's dive right in. We're all familiar with the Base 10, or "decimal," system. You know, it has 10 different numbers - 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The places go up by factors of 10 - ones, tens, hundreds, thousands, and so forth. If you're not familiar with decimal numbering, then you need to sit in on a Kindergarten class and come back when you get a brain. No offense. However, you may not be quite so comfortable with binary and hexidecimal formats. They may seem scary at first, but don't be afraid.
Binary, or Base 2, has only two numbers - 0 and 1. It is the way your computer stores information - either a pulse of electricity, or not a pulse - a stream of electric 1's and 0's. Binary numbers are grouped together in blocks known as "bytes." Each byte contains eight binary digits, or "bits." 1,024 bytes is a "kilobyte," 1,048,576 is a "megabyte," 1,073,741,824 is a "gigabyte," and so on.
Diagram of a byte:
1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 Base 2 (Binary)
---+---+---+---+---+---+---+---
128| 64| 32| 16| 8 | 4 | 2 | 1 Base 10 (Decimal)
It works out in a simple fashion. Each bit represents a decimal value like I described above. A 1 in any of those bit-places designates that its value is added to the total sum of the byte's value. Let's look at the value I have above: 10111010. Looking at it, we see that the 2's, 8's, 16's, 32's, and 128's are all turned on. So, we just have to add up that sum: 2 + 8 + 16 + 32 + 128. That sum happens to be 186. Therefore, decimal 186 is equal to binary 10111010. The maximum value for any byte is 255 (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128). Now, before you run away crying, take heart. This is just here to help illustrate the concept of bytes - you won't have to do this all the time. :D
Hexidecimal, or Base 16, is even more alien to some because it contains 16 numbers. "16 NUMBERS?" you shout, incredulous. "WHAT THE ¶©§þ?" Instead of stopping at 9, it continues on. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Then, once you go past F, you count 10, 11, 12, 13... yada, yada, yada... 19, 1A, 1B, 1C, 1D, 1E, and 1F. It continues on like that. To differentiate hex numbers from decimal, we write them with 0x at the front. Like this -> 93 = 0x5D. The places in hexidecimal form increase by factors of (guess what) 16. Thus you have the ones place, the sixteens, the two-hundred-fifty-sixes, and so on. Each byte contains 2 hex digits, and the maximum any byte can be (in hex) is 0xFF. So, let's look at that byte diagram again.
Diagram of a byte (again):
1 | 0 | 1 | 1 | | 1 | 0 | 1 | 0 Base 2 (Binary)
---+---+---+---| |---+---+---+---
128| 64| 32| 16| | 8 | 4 | 2 | 1 Base 10 (Decimal)
---+---+---+---| |---+---+---+---
0x8|0x4|0x2|0x1| |0x8|0x4|0x2|0x1 Base 16 (Hexidecimal)
In order to convert from binary to hex, we have to consider the byte as two pairs of four bits. The first four bits will designate the first digit in the hex value of the byte. 0x8 + 0x2 + 0x1 = 0xB, so the first digit is B. As for the second, 0x8 + 0x2 = 0xA. Thus, the whole byte ends up being 0xBA. Unless you really want to, you don't need to manually convert any number to hex, or binary, or whatever. You have Microsoft Calculator - the one not-crap thing included with Windows! In Scientific mode, select your number type - ignoring Octal (Base 8, that is) - type in the value, and click the type you want to convert to! It's that easy. Now, let's discuss how the ROM is constructed.
The ROM is made up of a string of 3,146,240 bytes stored in a hexidecmal format. Scary. However, we can use that fact to obtain what's known as a hex address. In order to refer to a particular location in the file, we need to know where it is. We do this by counting the number of bytes into the ROM that byte is, as if each byte were a page number in a book (starting at 0x0). For example, the text in the game begins at the 335,122nd byte in the ROM - or in hex, 0x51D12. Thereby, we say that the hex address of the beginning of the text is 0x51D12. You don't have to count, as hex editors have a location readout displaying your address at all times. However, internally, it works a bit differently.
All SNES ROMs contain a short header, identifying the game, the company that made it, how big it is, and other such information. This lasts for the first 512 - or, 0x200 - bytes in the file. Now, the game will not actually read this part of the file. And on top of that, EarthBound reads the first byte after the headers as location "0xC00000". I can imagine you're going, "WHAT YOU SAY ??" right about now. Just calm down, clear your mind, and live with it. :P In order to find an SNES-format address from a standard hex address, you'll have to subtract 0x200 because it's not reading the header, and add 0xC00000 'cuz it's funky. Of course, to find the hex address from an SNES format address, you have to do the opposite (add 0x200 and subtract 0xC00000).
Now, one more basic fact to grapple with. Every time where the game stores a number - say, the amount of Hit Points a particular enemy has - it's stored in hex. The Kraken, for example, has 1,097 HP. Using our calculators, we find that 1,097 is 0x449 in hex. Since each byte has 2 hex digits, we know that that the bytes have to be [04 49]. Right? Wrong. The ROM also stored bytes in a flipped-around-backwards format which trips a lot of beginning hackers up. In this backwards format, 0x449 would be stored as the bytes [49 04]. Don't let this confuse you - you just have to read the bytes in backwards order. But DON'T read the VALUES of the bytes backward - [49 04] is not 0x4094, after all!
Well, that about wraps it up for the basic information you need to know. If you're scratching your head still, don't worry. Just give it all a chance to sink in. Knowing this stuff is a neccessary evil; but if you don't get it right off the bat, it's okay. It is required knowledge for some more advanced hacking concepts, however.
|
|
|