Romhacking.net

Romhacking => Newcomer's Board => Topic started by: petfriendamy on April 17, 2019, 12:56:09 am

Title: Rearranging tiles?
Post by: petfriendamy on April 17, 2019, 12:56:09 am
Hello! So this is probably a super noobish question, but I've been experimenting with a translation hack of a GBC game called Star Sweep. I've been able to retexture most of the menus into English without much trouble, but I'm running into a bit of an issue with the way the text itself is spaced out. Since a lot of the Japanese words use fewer characters than the English equivalents, the programmers opted to leave blank tiles in the middle of some words, which comes out very awkward when changed into English.

(https://i.postimg.cc/ZKNfmLp3/weird.png)

I figured the easiest fix for this would be to rearrange the tiles so that the blank ones are on the sides of the words, instead of in the middle. Unfortunately, I've not had much luck figuring out how to do that. Is there a recommended tool for moving tiles around? Thanks in advance!
Title: Re: Rearranging tiles?
Post by: FAST6191 on April 18, 2019, 11:53:19 am
Depending upon whether those are sprites or backgrounds it falls under background maps or sprites (OAM)
http://bgb.bircd.org/pandocs.htm#vrambackgroundmaps

It can be a simple table in the ROM (more for later systems, where they might even have dedicated formats for it, but far from unknown on older ones), or it can need you to figure out where it comes from with a debugger and go from there. If you want to start by taking a section of that memory as detailed in the link above and then searching for that in the ROM you might get where you need to be.
As for telling which of the two it is then most debuggers will have a view sprites/oam option, and a view backgrounds/maps option so that narrows that, failing that there is probably a disable layer option and that will also narrow it in short order.
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 19, 2019, 08:05:21 pm
As it turns out, both—"View" and "Delete" are background tiles, while "Turn" and "Drop" are sprites. :P

But yeah, I'll have to look into it in more detail. Thanks!
Title: Re: Rearranging tiles?
Post by: 4l3j4ndr0 on April 20, 2019, 02:31:19 pm
I can help you. Can be edited whit hexcode. Send me the file to make it.
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 20, 2019, 10:33:15 pm
Though I would like to learn how to do it myself, I would definitely appreciate the help! I'll send you a message.
Title: Re: Rearranging tiles?
Post by: FAST6191 on April 21, 2019, 07:26:48 am
"Can be edited whit hexcode"
So can anything. It is usually pointlessly long winded and hard though so we tend to seek better methods unless it is really short. Similarly if you know what goes for this game then unless it is some complicated assembly driven animation (rare for a menu but not entirely unheard of) you can surely tell the OP.

Anyway did searching the ROM for the contents, or snippets thereof*, of the OAM/BG mapping sections when such things were on screen not get you anywhere? There can occasionally be some complicated animations done and make it not resemble the ROM but still worth a try.

*static parts of the screen are often good as they are static. Active menu parts like this that have highlights and colour changes as part of their use could be excluded from the deal if the devs did some odd, though perfectly valid, things to achieve their effects.

Failing that time to bust out the debugger and learn some tracing (for the GB/GBC I think we are still suggesting BGB Debugger). You should be able to get by with a light understanding of things -- you are mainly only looking for where it pulls it from, which may involve a second stage where you trace back from an intermediate location. There should not be an any tricky conditionals, loops and whatever else you have to understand like you might if you were reverse engineering a damage calculation in an RPG or something, or perhaps to keep it with the text hacking them then coding in a variable width font. Even if there is some kind of animation you still don't have to understand it all as much as direct the results to whatever tiles one space before or after that you want them in.

The following is for the GBA but most of the terms are the same (the GBA being a direct successor and all that), and frankly it is very similar for most systems out there
https://www.romhacking.net/documents/361/
General flow will probably be get to the part before you load that screen (whether it is press start or some previous menu I don't know, or much care. Very rarely a game might keep it in memory as some kind of cache but that still means you go back to the screen before that comes in even further back in the game). Probably want to make a savestate here as well. Then set up a breakpoint session (probably a BPW aka breakpoint on write) on the area of OAM and/or BG mapping that you care to observe. I would suggest one at a time but some might do it all at once.
Go into the screen. Emulator should halt and say "this did this". Hopefully it is a nice DMA or whatever ROM read the game uses directly from the ROM somewhere but if you have to follow it back up the chain to something that does touch other memory, or the ROM, then such is what it takes to be a hacker here. In a lot of cases the maps will be right next to the graphics data itself or with all the other maps.
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 21, 2019, 04:29:05 pm
To be honest, a lot of this is going over my head at the moment, so I'm not quite sure how to do what you're suggesting. I did figure out a couple of things from messing around in BGB, though:

1) I figured out how to directly edit the VRAM to rearrange the background tiles. Not that helpful since it only stays that way while in the menu, but it's something.

2) I've managed to get it to breakpoint at the moment when the tiles are loaded into VRAM, but I'm not sure how to get BGB to output anything meaningful (or how to read what it's outputting as the case may be).
Title: Re: Rearranging tiles?
Post by: Psyklax on April 21, 2019, 05:42:49 pm
I've managed to get it to breakpoint at the moment when the tiles are loaded into VRAM, but I'm not sure how to get BGB to output anything meaningful (or how to read what it's outputting as the case may be).

You're on the right track. :) The problem is that you'll need to understand a bit of Z80 assembly (or whatever the modified CPU is in the GB). See, you've found where the tiles go in VRAM, and you've managed to get the emulator to break at the moment they - or at least the first byte - are loaded. So, the question is: where did that bit of data actually come from? This comes from learning to read assembly, plus knowing a little of how the particular system has its memory laid out.

I'm not looking at the game right now so I can't give a good answer, but if I were to guess, I'd say that the value in question was probably pulled directly from the ROM and put in one of the registers, then an instruction stored it from that register to the place in VRAM. I can't remember if the GB CPU has direct access to the VRAM, but I think it probably does.

You know what, my vagueness is bothering me. I'm gonna spend five minutes looking at the game myself and will then edit this post. :D

EDIT: wow, that was easier than I thought. :) So the top menu which you renamed "View" is in the VRAM starting at $984A, and it gets that number from $7392 in the ROM portion of the address space - which corresponds to $27392 in the actual ROM (because BGB says it's ROM9, so $4000 x $9 = $24000 + $3392 = $27392). I'll let you mess around with the numbers stored there - but don't forget to make a backup! ;)
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 21, 2019, 07:50:36 pm
EDIT: wow, that was easier than I thought. :) So the top menu which you renamed "View" is in the VRAM starting at $984A, and it gets that number from $7392 in the ROM portion of the address space - which corresponds to $27392 in the actual ROM (because BGB says it's ROM9, so $4000 x $9 = $24000 + $3392 = $27392). I'll let you mess around with the numbers stored there - but don't forget to make a backup! ;)
Hey, it worked!

(https://i.postimg.cc/zfsLbgt9/Screen-Shot-2019-04-21-at-5-44-12-PM.png)

Can I ask how exactly you found that number?

EDIT: Actually, I found a really easy way to do it—I realized that the block of data you pointed me to was literally 1-to-1 with what appears in the VRAM, so I took some of those VRAM values and searched for them in the ROM with my hex editor, and boom, there it was! (I also realize now that that's probably what FAST6191 was trying to tell me how to do and I misunderstood that somehow, lol.)

I'm still not sure how to find and reposition the OAM sprites, though. Any tips on that?
Title: Re: Rearranging tiles?
Post by: Psyklax on April 22, 2019, 01:13:34 am
Can I ask how exactly you found that number?

Um, I told you. :) Ah, how EXACTLY? Well, I'm not looking right now, but basically I did a write breakpoint to that part of VRAM, and when it stopped at that point, I looked at the main window for the instructions and registers. If I remember right, this one loaded the data from the A register to a location in HL, and going back one or two instructions, it loaded A from a location in the DE register, so I looked at DE and there was the ROM location. Of course, it helps to know assembly to figure this stuff out. :)

As for the sprites, that's another issue which might be slightly more work, and as I said, I'm not looking right now. Maybe later...
Title: Re: Rearranging tiles?
Post by: FAST6191 on April 22, 2019, 05:31:36 am
Hey, it worked!

[ image ]

Can I ask how exactly you found that number?

EDIT: Actually, I found a really easy way to do it—I realized that the block of data you pointed me to was literally 1-to-1 with what appears in the VRAM, so I took some of those VRAM values and searched for them in the ROM with my hex editor, and boom, there it was! (I also realize now that that's probably what FAST6191 was trying to tell me how to do and I misunderstood that somehow, lol.)

I'm still not sure how to find and reposition the OAM sprites, though. Any tips on that?
Nice. Also yeah I was trying to say search the ROM for the data as it was loaded into the things governing maps, or if there is animation involved a smaller portion of it, preferably a static part. Will not always work (sometimes it is generated on the fly, sometimes there are animations, sometime there is compression...) but definitely worth a shot.
It also works for most other things within a game -- consoles don't have the most processing power and it is not like people edit the games after the fact (the Read Only part of ROM/CD-ROM tending to come into play there) so devs tend to make things into a format the system understands. Works for pretty much everything, save perhaps music which is usually more tricky, so if you can find it in RAM then search the ROM for it.

As for OAM/sprites you can start with the same way you just did the BG stuff -- it might have the snippet that controls its location in the ROM as well. Find the value locating it in the OAM and search the ROM for that.
Failing that set a breakpoint on the OAM section and see what is writing to it for that particular sprite. Each character or couple of letters might be. The tile viewers and/or OAM viewer should tell you what belongs to what so you can narrow things down. As you are only looking to control the x-axis location of the sprite it should not be so bad.
Title: Re: Rearranging tiles?
Post by: Psyklax on April 22, 2019, 04:46:19 pm
Well, I've got it. :D It's always fun for me to try new things like this, and I'm glad I can help. So, let's see...

First thing I did was open the OAM viewer and found the first sprite, and set a breakpoint for it. Turns out the GB has an OAM DMA command which takes the contents of somewhere and blasts it to the OAM section. In this case it takes $C0XX - i.e. the work RAM, which makes sense since you can change RAM unlike ROM. So I set a breakpoint for that part of the work RAM and found that it was getting the number I needed (the X position) from $5BED, which is ROMC, so $4000 x $C + $1BED = $31BED. Go there in the ROM file, and you'll see $18. The game takes $60 and adds this to it, which results in the final X position of $78. So if you change it to $10, and change $31BF1 from $20 to $18, the sprites for one of the options will shift left 8 pixels. :) If you look ahead a little, you'll see the other $18 and $20 for the other sprites, so change those too.

Hope you have no problems with this! And don't forget to experiment with it to see what you can do! ;)
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 22, 2019, 06:23:48 pm
It worked! And it even keeps the X-position when you switch the controls, which is really convenient.

(https://i.postimg.cc/j28CSv8K/fixed.png)

I feel like I need to learn assembly because I'm having a hard time tracing these hex positions myself, haha. At any rate, I'm super grateful for the help!

(I think my next big hurdle is going to be finding and editing the text from story mode, but that might be getting a bit off-topic. :P)
Title: Re: Rearranging tiles?
Post by: 4l3j4ndr0 on April 24, 2019, 10:26:17 pm
I'm confused, maybe I lost something.

"View" is in the VRAM starting at $984A, and it gets that number from $7392 in the ROM portion of the address space - which corresponds to $27392 in the actual ROM

"View" is in the VRAM starting at $984A, True, is in there
 and it gets that number from $7392 in the ROM portion of the address space How do you get 7392?

- which corresponds to $27392 in the actual ROM.

BGB says it's ROM9, so $4000 x $9 = $24000 + $3392 = $27392) where do you get 3392?

Can you please explain to me, I want to learn.

I have a tile on $8810 that mean bank 8?
Title: Re: Rearranging tiles?
Post by: Psyklax on April 25, 2019, 02:02:23 am
How do you get 7392?

I said how in another post: the instructions in the game and the registers at that moment told me. The game loads the correct value from an address in the DE register, which was $7392 at that moment.

where do you get 3392?

The Game Boy uses a Z80 processor which has a 16-bit address bus. It can access up to $FFFF, and on the GB the first half of that (up to $7FFF) is reserved for the ROM. That's not a lot so the system natively used bank switching - unlike the NES which needs an extra chip in the cartridge to do it. For flexibility it uses two 8KB banks, one up to $3FFF and the other up to $7FFF.

Helpfully, BGB tells us which bank is in place at any one time. In this example, the game reads from ROM9, and each bank is $4000. This happens to be the second bank in use, though, so we take away $4000 from the address (since it doesn't start from zero). Thus $4000 x 9 + $3392. :)

I have a tile on $8810 that mean bank 8?

I don't understand, I think you may have mixed up VRAM with the CPU address space. Read what I said earlier in this post. :)
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 26, 2019, 12:30:58 pm
How do you get 7392?
After studying the assembly code a bit, I think I get how this works. Feel free to correct me if I'm wrong.

If you set a write breakpoint on 984A, then enter the options menu, BGB will stop here:

(https://i.postimg.cc/mZm8Wcbx/Screen-Shot-2019-04-22-at-10-28-52-AM.png)

The highlighted line at the top says "ldi (h1),a", which means that it's taking the value stored at A and assigning it to the memory location stored in H1—which, if you look in the top right, you'll see h1=984A. Scroll up a little and you should see "ld a,(de)". Again, this means that the value stored in the memory address DE is being stored in A, and if you look in the top right, you'll see de=7392, as well as rom=9 which Psyklax explained.

So the value stored at $7392 (ROM 9) is placed in A, and then that value is placed in $984A.
Title: Re: Rearranging tiles?
Post by: Psyklax on April 26, 2019, 03:59:45 pm
So the value stored at $7392 (ROM 9) is placed in A, and then that value is placed in $984A.

You got it! ;D

Just one correction, though: it's not "h1", that's HL. See, CPUs use registers to store numbers, either to do calculations with or to use as an index. The Z80 in the Game Boy has a bunch of registers: A, B, C, D, E, F, H, and L. They are 8-bit registers, so they can hold just one byte. However, as you can see, they're paired up, so they can behave like 16-bit registers, and become AF BC DE and HL. The A register - both on this CPU and the 6502 in the NES - is called the Accumulator, and is the one that most arithmetic operations are performed on. The others are usually used for counters and addresses. Despite being paired with F, A can only work alone because F is used to store different flags, so essentially A is 8-bit - the main reason this is called an "8-bit console".

As for DE and HL, they're usually used for addresses. No surprise that in this example, DE is holding the address in the ROM to get the data from, and HL is holding the address in VRAM that needs to be written to, while A is used for the data itself. It's really quite straightforward, but if you ever get into hacking the NES, it's even easier: it only has three main registers, A, X and Y. A is the same as on the Z80, while X and Y are 8-bit registers used for addresses (ie you can't do any arithmetic on them). It sounds limited, but it makes up for it by having the first 256 bytes of memory (from $00 to $FF) be called "zero page" and has special instructions dedicated to it, so that's used instead of the registers. Some of those instructions allow 16-bit addresses on the zero page to be used, so in practice it's not really handicapped. And for a hacker, it can be easier keeping track of actual RAM rather than the registers, so I prefer the 6502.

So yeah, assembly is really not that hard, once you get the hang of it. Keep on experimenting and you'll be amazed at what you can achieve. :)
Title: Re: Rearranging tiles?
Post by: petfriendamy on April 26, 2019, 05:43:27 pm
Oh, oops. That L really looked like a 1, lol.

But yeah, thank you so much for the help!