News: 11 March 2016 - Forum Rules

Author Topic: how Flag Codes work on rom hacking  (Read 2447 times)

jin299

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
how Flag Codes work on rom hacking
« on: October 29, 2012, 05:02:48 pm »
Hello, does any know, how these type of coding, is used in rom hacking?

Zoinkity

  • Hero Member
  • *****
  • Posts: 565
    • View Profile
Re: how Flag Codes work on rom hacking
« Reply #1 on: October 29, 2012, 05:52:43 pm »
Are you talking about bitflags?

Bitflags set one bit within a byte.  A byte contains 8 bits, and that's usually the basic method of storage in computers today.  Bytes can also describe numbers ranging from 0-255 through the magic of hexadecimal, and this works because each bit is a different power of 2.

Hexadecimal is a convenient way to encode several types of data in a simpler way.  For our purposes it can describe both 8 bits and 1 byte.  So, if you have eight bits:
00000001
-you can write that as one byte in hex:
01
The only complication is that you need to count to 16, so we use A for 10, B for 11, C for 12, all the way to F.

Since binary is base 2, each bit is a power of two. 00000010 is 2, 00000100 is 4, 00001000 is 8, and so forth.  These are added together to make integer numbers.  Here's a table:
Code: [Select]
00 0
01 1
02 2
04 4
08 8
10 16
20 32
40 64
80 128
equals:
FF 255

Sometimes though they aren't necessarily describing numbers but are used as flags to tell if something is on or off.  This is especially true of options; look at any save file and the options are probably going to be stored as bitflags since most are just on/off.

Funny thing is that computers usually consider the smallest piece of data they want to work with to be a byte.  So, to set or toggle a flag you'll have to OR or XOR it with it's numerical value.  To set flag 0x04, you'd OR it with 4.  You could also do something funny like OR (1<<2) if you want to complicate things.

It's incredibly useful and you'll see it all the time.  You can save a lot of space (though lose a tiny bit of CPU) by encoding things with just bits.  Likewise, because programs use them all the time you need to be familiar with it when hacking--especially with options and registers. 
As an example, the N64 uses bitflags for all its status registers.  If you want to change the graphics mode, know when data has finished loading, or check for errors, you need to look at certain bitflags in those registers. 



jin299

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: how Flag Codes work on rom hacking
« Reply #2 on: October 29, 2012, 09:28:22 pm »
Than you, that was rather informative.