Romhacking.net

Romhacking => Newcomer's Board => Topic started by: Spriteiscoolio on May 31, 2011, 12:43:18 pm

Title: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on May 31, 2011, 12:43:18 pm
I'll try to explain my problem as well as I can. I've already received some suggestions on the IRC channel but I'm coming here for any extra help I can get.

My problem is that in the game I'm trying to edit, I'm trying to make it in the vein on old school RPGs where an icon appears before a skill or item, like so. Here's what it looks like on this particular screen.

(http://i1089.photobucket.com/albums/i351/spriteiscoolio/RPGMakerGBK_01.png)

I've made white magic icons. Looks kinda nice in my opinion. My problem is...

(http://i1089.photobucket.com/albums/i351/spriteiscoolio/RPGMakerGBK_03.png)

This happens whenever I go to the skill menu. The grey color of the icon turns completely black.. I'm thinking that it's the fact that the palette doesn't include that grey color. But how do I add it in? I've been suggested corruption tools but I'm not sure how to work them. Sorry if this is totally obvious. I feel like I'm being completely impatient and looking for an easy way out, but I'm willing to try anything and learn how to fix this.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on May 31, 2011, 01:50:56 pm
As a first step, try running the game in B/W Game Boy mode (most GB emulators should have a toggle for this somewhere in their settings). If the icon still appears solid black, then the problem has something to do with how the game copies the tiles to VRAM for that particular print routine - it might be copying the same byte twice instead of properly copying both bytes of the tile. This'll take some ASM hacking to fix, but it shouldn't be difficult.

If it shows up properly, then yes, it's a palette issue. I don't know much about GB palettes, but the general idea should be simple: find the appropriate palette in memory, set a write breakpoint to its location, then when the game breaks (pauses on the write), trace the code and figure out where the palette data is coming from.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on May 31, 2011, 02:15:05 pm
As a first step, try running the game in B/W Game Boy mode (most GB emulators should have a toggle for this somewhere in their settings). If the icon still appears solid black, then the problem has something to do with how the game copies the tiles to VRAM for that particular print routine - it might be copying the same byte twice instead of properly copying both bytes of the tile. This'll take some ASM hacking to fix, but it shouldn't be difficult.

I should have specified it's a Game Boy Color game...that might make things different. All of the methods I've tried however still end up with the icon appearing black.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on May 31, 2011, 04:59:45 pm
GBC-exclusive, or GBC-compatible? There's a difference. Run the game in GB-only mode and see if the game runs, or if you get an error message.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on May 31, 2011, 05:05:52 pm
GBC-exclusive, or GBC-compatible? There's a difference. Run the game in GB-only mode and see if the game runs, or if you get an error message.

GBC exclusive. I get an error message that says "This game can only be played on a Game Boy Color".
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on May 31, 2011, 08:58:24 pm
Right, then. BGB's debugger has a palette viewer. That'd be a good place to start. I'm assuming that your icons display as BG tiles rather than OBJ sprites, correct?
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on May 31, 2011, 09:07:56 pm
Yes, they are. I'm actually taking the easy way out and overwriting font graphics to make icons easier to implement.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on May 31, 2011, 10:32:49 pm
Good, good. Like I said, use BGB and its debugger's VRAM viewer to check which BG palette the offending tiles use. Then check the palette data and see what colors are actually in the palette. (The left column is the BG palettes, numbered from 0; BG 7 is the one on the bottom.)
Title: Re: Here\'s something I don\'t understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on May 31, 2011, 10:50:20 pm
I get it! This makes pinpointing the colors a lot easier. The palette my game is using for the icons is BG0, and the colors are 7FFF and three 0000s.

Now, how would I use this new found knowledge to pinpoint it in a palette editor like SNESPal? I've looked around on google and some sources have told me to enter it the RGB values into a hex editor, but I think I'm missing the point.

Thank you very much for all your help so far as well.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on May 31, 2011, 11:53:59 pm
The obvious way, of course, would be to simply hex search the ROM for that sequence of palette bytes - it might be 7F FF 00 00 00 00 00 00, or it might be FF 7F 00 00 00 00 00 00 if the palette values are stored in little-endian format (probably this, but it pays to try both ways). Otherwise, you'll have to catch the game in the act of updating the palette. Is BG0 ever not White-Black-Black-Black?
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 12:14:36 am
The obvious way, of course, would be to simply hex search the ROM for that sequence of palette bytes - it might be 7F FF 00 00 00 00 00 00, or it might be FF 7F 00 00 00 00 00 00 if the palette values are stored in little-endian format (probably this, but it pays to try both ways). Otherwise, you'll have to catch the game in the act of updating the palette. Is BG0 ever not White-Black-Black-Black?

I wasn't able to find anything with that byte string (It still selected something but it wasn't the exact string I put in. Is this normal? Should I continue from here? It has 84 instances of this same string no matter what I put in. I'm not sure how to input it either - 32 bit float? 64 bit unsigned? I really don't even get what I'm supposed to preform my search terms with) but yes, the BG0 does change. It's usually something like

In game menu: 7FFF 0000 0000 0000
Overworld/Towns: 7FFF 0000 00EF 027B
Dungeons: 7FFF 0000 00AD 1279
Title Screen: 7FFF 0000 56B5 7FFF

Or is that not what you meant
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 12:40:22 am
That's exactly what I meant. :3

So we can catch the game in the act of loading the menu-specific palette and change it to whatever we like. In theory.

I'm doing my research, but I find the process of writing to palette RAM inscrutable, to say the least. If you can find the palette data in the ROM itself, then that's super: we just change it and call it a day. In the meantime, I'll see if I can't figure out how we can find its location with an ASM trace.

EDIT: 64-bit unsigned should give you the best results. 64 bits = 8 bytes, and unsigned means it takes all the bits as literal numeric value rather than interpreting the highest bit as a sign (if it's 1, then the number is treated as negative). And since this is technically an integer value, definitely don't have it look for a float.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 12:48:25 am
All right, now I know a little bit more on how to search on this, so I'll try my hand and see what I can find. I'm not sure if I'll actually be able to find the palette data, but it's better me to try than to not if you ask me. Hmm...I'm actually not getting that fantastic of results. I'm still not sure what to start searching for.

I wish I could be a better help...I'm just not experienced with these tools and how to use them properly yet.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 07:11:17 pm
Okay. It's taken me some effort, but I've figured out what you need to look for.

Palette data is written to palette RAM using the registers FF68, FF69, FF6A and FF6B. Registers are special memory values that allow the game software to access certain hardware functions - controller input, for instance.

In this case, we're primarily interested in FF69, which writes data to the BG palette RAM. Run the game in BGB, wait until just before the game loads the menu palette, and then use "access breakpoints" under the Debug menu in the debugger to set a write breakpoint for FF69 (type FF69 in the "addr range" field, make sure "on write" is checked and "on read" is unchecked, and click "Add"). This should catch the game in the act of writing the menu palette to palette RAM. Now we can find out where the palette data is coming from: hopefully, it'll be somewhere in the ROM (addresses 0000-7FFF), but if not, look at the RAM address indicated and see if you can find it there. We can then repeat the process, only this time, we'll set a write breakpoint for the RAM location of the palette in order to find out where it's coming from.
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 07:24:24 pm
All right, I got it. And I did it twice just to make sure I did it right. This is what I got out of it. Is this what we're looking for?

(http://oi53.tinypic.com/mbgu9g.jpg)
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 08:07:18 pm
Dandy. And I've got some good news for you! It looks like the palette data is stored in the ROM!

I'm assuming this sequence repeats itself at least once:

Code: [Select]
inc de
ld a,(de)
ld (ff00+69),a

In English:

1. Increment (add 1 to) the two-byte register value "de". This is the address for reading palette data from the ROM.
2. Load 1 byte of the palette data from the ROM at the address stored in "de" (in this case, 7F22) into the register value "a".
3. Load register FF69 with the value stored in "a".

Are you following?

See the ROM viewer at the bottom? We need to know which bank it's looking for palette data in. Once we know that, we can calculate the file address. Scroll down to 7F22 and note the number after "ROM" on the left. That tells us the bank. Just to be sure, look to see if "7F FF" or "FF 7F" is at or near that address, followed by a string of zeroes. That's the palette data we're trying to edit. Got it? Good. We're almost done. :3
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 08:26:06 pm
Holy crap, I'm actually taking this in and understanding it. Well, getting a better grasp of the methods of palette editing, that is Is this what you meant?

(http://oi56.tinypic.com/hx52d2.jpg)
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 08:37:27 pm
Direct hit. Now, if my math is correct, you should find that data in the ROM at 83F22.

Try changing the zeroes after that FF 7F - just random values will do - and see if the palette changes next time you run the game. If the colors are different, then we've found the palette data. Now we need to change it.

Find the palette the game uses for the menu tiles where the icons display properly and use that. Remember, the values are little-endian - the second byte goes first. For example, if you wanted to replace the palette with this one you mentioned earlier:

Quote
Title Screen: 7FFF 0000 56B5 7FFF

You'd change "FF 7F 00 00 00 00 00 00" to "FF 7F 00 00 B5 56 FF 7F".
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 08:57:58 pm
Well, something happened...the palette is FUBAR. I used the title screen values. But at least now we know we can edit it more directly, right?

(http://oi54.tinypic.com/2ilyjba.jpg)
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 09:01:08 pm
Peculiar...and slightly alarming. Are all the palettes like this now?
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 09:05:21 pm
No, just when I go into the menu screen. Everything else is untouched. Could it have something to do with this? That color was a part of the title screen palette...

http://oi52.tinypic.com/2vi2omx.jpg

Theoretically, this palette data should have been the fix.  :(

EDIT: And it was! It seems that I accidentally didn't use little endian format and it screwed up the colors. But once I plugged in those colors, I played the game and it came up looking like this:

http://i1089.photobucket.com/albums/i351/spriteiscoolio/2-1.png


And once I loaded it up, I went into my palette editor and went to the address you gave me, and simply added in the greys, and...it works! My menus now have greys! :D I can't even begin to thank you for all the time and effort you've saved me, and I even learned how to find out this stuff for myself in the future. It's been a pleasure.

(http://i1089.photobucket.com/albums/i351/spriteiscoolio/RPGMakerGBK-Copy2.png)
(http://i1089.photobucket.com/albums/i351/spriteiscoolio/RPGMakerGBK.png)
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Ryusui on June 01, 2011, 09:38:08 pm
Glad I could help. You've been an awesome student. :3

This was a big help:

http://www.romhacking.net/docs/544/

The process is similar on any platform where palette RAM is in its own separate block, but this filled in the details - it's all on page 66.

Just so I know, which game is this you're working on? Are you fan translating a game or making a hack for a game that's already available in English? (If the former, a word of advice: your numbers need to be shifted down a pixel. They're out of line with the letters. ^_^;)
Title: Re: Here's something I don't understand about Game Boy palettes...(huge noob inside)
Post by: Spriteiscoolio on June 01, 2011, 09:43:38 pm
The game is RPG Tsukuru GB, and this was pretty much my only addition - adding icons to the game. I'm using Magecraft's translation as a base (This is mostly a personal project but if I were to release it I'd try to get his/thier permission to use his translation)

http://www.gamefaqs.com/gbc/577001-rpg-tsukuru-gb
http://www.romhacking.net/trans/42/

Also, holy crap that document is nice. I'm going to be skimming this a lot. (also you're right about those numbers.)
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Ryusui on June 01, 2011, 10:12:02 pm
Need an English "RPG Maker GB" logo? Or any English-language graphics at all, really. I'm good at that kinda thing.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on June 01, 2011, 10:21:08 pm
Need an English "RPG Maker GB" logo? Or any English-language graphics at all, really. I'm good at that kinda thing.

The rom already has the logo in English...kinda shoddy looking, but I'm okay with it. Thanks for the offer, though...I feel kinda bad though, you've helped me so much, I feel like I have to give back to the community now. xD And...I dunno! I've been meaning to translate a game, but considering my experience it might not be a good idea. I'd be able to translate the game but I wouldn't actually be able to put the translation in...kind of frustrating.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Ryusui on June 01, 2011, 10:59:08 pm
Translation's the hard part, actually - unless the script's compressed or something crazy like that. Are you familiar with using a command prompt?
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on June 01, 2011, 11:02:16 pm
Not at all. This project was my first foray beyond GUI based hacking.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Ryusui on June 01, 2011, 11:26:40 pm
Then things might get a little complicated. The major script dump/insertion tools are command line-based, but with batch files, you can automate the process a great deal. Do you know how to make a table file?
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on June 01, 2011, 11:34:04 pm
I've looked at documents telling me how but I don't really comprehend it.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Ryusui on June 01, 2011, 11:48:25 pm
The table is a file that tells your hex editor, script dumper and script inserter which hex values correspond to which characters. Sometimes you get lucky and the text data is stored in a recognized format, like ASCII or Shift-JIS, but for most older titles, it's purely ad hoc and requires some sleuthing to figure out.

The general form of a table looks like this:

Code: [Select]
6D=く
6E=け
6F=こ
70=さ
71=し
72=す
73=せ

That is, your tools will recognize that these hex values are used to represent the corresponding characters in the game's text data. This is just a sample, mind: the exact values will differ from game to game.

A good first step is to find the game's font. Assuming it's not compressed, it should be arranged in the same order as the game's table - this simplifies the math involved in converting character values to ROM addresses. Once you've figured that out, you can use relative searching to find patterns matching the game's text (again, provided the script isn't compressed) and thus work out the beginnings of your table.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: KingMike on June 02, 2011, 01:16:25 am
I think it's safe to say you're not going to find Magecraft or whatever the one member's name was.
The wonderful people at the Whirlpool :P flamed the hell out of him a decade ago for "stealing" a more experienced member's project (flaming wouldn't be tolerated here).

More relevant to this topic, it would not surprise me if this was stored with event data between the text. Definitely not beginner friendly, and I imagine it's a good reason why nobody's finished translations of the SNES games.
I tried myself to do RPG Maker 1 for SNES. Take a look in the editor at the things an NPC can do: walk in each direction, turn in each direction, yes/no prompts, etc. Each of those has a hex value. And even for the "text" opcode, there's probably more control bytes for text length, windows, etc. Even if you made a custom text dumper/inserter that could expand and relocate the event data, I'd guess that due to the editable nature, you'd still be restricted in how much you could expand the text before crashing by making the data take up too much SRAM.
(I'd thought about doing DTE for RPGM SNES, but then I realized that'd make editing the text input windows a real problem)
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on June 02, 2011, 02:20:35 am
I think it's safe to say you're not going to find Magecraft or whatever the one member's name was.
The wonderful people at the Whirlpool :P flamed the hell out of him a decade ago for "stealing" a more experienced member's project (flaming wouldn't be tolerated here).

More relevant to this topic, it would not surprise me if this was stored with event data between the text. Definitely not beginner friendly, and I imagine it's a good reason why nobody's finished translations of the SNES games.
I tried myself to do RPG Maker 1 for SNES. Take a look in the editor at the things an NPC can do: walk in each direction, turn in each direction, yes/no prompts, etc. Each of those has a hex value. And even for the "text" opcode, there's probably more control bytes for text length, windows, etc. Even if you made a custom text dumper/inserter that could expand and relocate the event data, I'd guess that due to the editable nature, you'd still be restricted in how much you could expand the text before crashing by making the data take up too much SRAM.
(I'd thought about doing DTE for RPGM SNES, but then I realized that'd make editing the text input windows a real problem)
So, would it be okay for me to release a patch with the custom icons that includes his translation? I'm seeing this as sort of a grey area, I wouldn't want to be seen as taking credit for other's work. If I can't I'll just reinsert the icons into the unpatched Japanese version and release it like that. And to be honest, I can't see myself doing a full translation anytime soon. I figure that if I can translate at least the main menu, I can be satisfied for a while.

The table is a file that tells your hex editor, script dumper and script inserter which hex values correspond to which characters. Sometimes you get lucky and the text data is stored in a recognized format, like ASCII or Shift-JIS, but for most older titles, it's purely ad hoc and requires some sleuthing to figure out.

The general form of a table looks like this:

Code: [Select]
6D=く
6E=け
6F=こ
70=さ
71=し
72=す
73=せ

That is, your tools will recognize that these hex values are used to represent the corresponding characters in the game's text data. This is just a sample, mind: the exact values will differ from game to game.

A good first step is to find the game's font. Assuming it's not compressed, it should be arranged in the same order as the game's table - this simplifies the math involved in converting character values to ROM addresses. Once you've figured that out, you can use relative searching to find patterns matching the game's text (again, provided the script isn't compressed) and thus work out the beginnings of your table.

Okay, this makes it a lot easier to understand. So, once I find one character, I just keep going up by one byte until the table is done? I'll see what I can do before I try to come back here for help again. I'm probably also going to try to work with a game that already has Roman characters instead of having to add them myself.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Ryusui on June 02, 2011, 02:29:07 am
There might be some gaps in the table, but yeah, once you've found a text pattern, it's like solving a substitution cipher.
Title: Re: Here's something I don't understand about Game Boy palettes...(SOLVED)
Post by: Spriteiscoolio on June 02, 2011, 03:01:32 pm
This is actually not too bad. I'm sure the next few steps will start to be though.