News: 11 March 2016 - Forum Rules

Author Topic: Tilemap Editing?  (Read 7723 times)

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Tilemap Editing?
« on: June 26, 2013, 02:39:40 pm »
Don't think I've.. actually done something like this before.

What exactly is a 'Tilemap'?  Is that what dictates how certain tiles go where, like their placement?
How would one find something like that?  I'm not really quite sure where I'd even begin to look.

Are Tilemaps difficult to modify?
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Tilemap Editing?
« Reply #1 on: June 26, 2013, 03:19:45 pm »
A tilemap is indeed a table that states what tile to show at each spot in the map. Some formats allow additional data, such as mirroring flags and palette selection.

Tilemaps are often found right next to the tileset when they are used for a single screen. Otherwise then they are usually in some game specific format.

A tilemap is sorta like a bitmap, the difference is that it paints with tiles, not pixels. As such, they look fairly similar in a hexeditor.

Also be on the look out for pointless tilemaps, a few games use them for no good reason. Those are just increasing numbers. That is, each spot on the tilemap has it's own tile in the tileset. Very wasteful if the tileset has multiple tiles with the same pixeldata. Not to mention that the code to generate the tilemap on the fly at runtime is likely smaller.

Gideon Zhi

  • Discord Staff
  • Hero Member
  • *****
  • Posts: 3536
    • View Profile
    • Aeon Genesis
Re: Tilemap Editing?
« Reply #2 on: June 27, 2013, 12:29:53 am »
What exactly is a 'Tilemap'?  Is that what dictates how certain tiles go where, like their placement?
How would one find something like that?  I'm not really quite sure where I'd even begin to look.

In the context of the SNES, it can be thought of as a grid of tile indexes. Each background has its own tilemap, and each byte-pair in the tilemap tells the system which tile to display, which palette to assign it, whether it's horizontally or vertically mirrored. The first entry in the tilemap corresponds to the upper-left tile in the background, and they go across then down.

To get a feel for it, take a ZSNES savestate of a game with, for example, some text on the screen. Figure out which background layer the text is stored in (probably BG3), then open the savestate up in vsnes. Bring up the "info" tab in the scene viewer to determine the starting address for the tilemap in the layer you want (vsnes lists this as "tilemap base.") Add 0xC13 to compensate for the zsnes savestate header (or just open it up in windhex) and 0x20000 (since vram starts at 0x20C13 in the savestate, or 0x20000 if you're ignoring the header) and jump to that address in a hex editor. The tilemap will be laid bare for you, and changing it SHOULD change what's displayed on the screen.

It won't always, though. Depends on how your game updates its background layers. Some will copy an entire 0x800-byte tilemap to RAM every time NMI fires (or every few times), others only update specific bytes at specific times. You'll have to determine how your game handles your tilemaps, as changing what gets displayed on screen is a matter of figuring out how it gets to the screen in the first place.

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Re: Tilemap Editing?
« Reply #3 on: June 27, 2013, 03:10:14 am »
I think it just ended up leading me back to the original issue I was having.

The tilemap base starts at 'F800', or so it says.  Add C13 and get 10413.  Add 20,000 and get 30413.  Modifying that in the current savestate I have did absolutely nothing.

Found out that the actual data junk is down at 30920.  Altering that will alter what letters appear on lines.  Though that just loops me back to the original issue I had in actually locating the Tilemap base in the actual ROM, as that's what needs to be edited.  Though I did find a storage in RAM with some similar data, but it doesn't update at all so I hit a dead end.
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

LostTemplar

  • Hero Member
  • *****
  • Posts: 910
    • View Profile
    • au-ro-ra.net
Re: Tilemap Editing?
« Reply #4 on: June 27, 2013, 03:45:11 am »
Many games alter the tilemap base with HDMA or other means mid-frame, meaning the base vSNES displays will be incorrect or only partly correct.

If you know where the tilemap actually resides in VRAM, just turn on the DMA tracer and look from where it gets transferred and set a write breakpoint. If it's done via regular VRAM writing instead of DMA it's more tricky; but you could try to copy maybe 0x20-30 bytes from the VRAM tilemap and look if you find a copy in WRAM. I'm not sure but if no$sns has VRAM breakpoints you could try those.

Tilemaps are very seldom stored verbatim in ROM; instead, they're usually created on-the-fly (exceptions include e.g. title screens and other big bitmaps), so you probably won't find it in the ROM. You'll have to locate the routine (or rather routines) that build(s) the tilemap at run-time.

Gideon Zhi

  • Discord Staff
  • Hero Member
  • *****
  • Posts: 3536
    • View Profile
    • Aeon Genesis
Re: Tilemap Editing?
« Reply #5 on: June 27, 2013, 05:39:33 am »
Tilemaps are very seldom stored verbatim in ROM; instead, they're usually created on-the-fly (exceptions include e.g. title screens and other big bitmaps), so you probably won't find it in the ROM. You'll have to locate the routine (or rather routines) that build(s) the tilemap at run-time.

This, especially where text is concerned. It likely adds incremental new data to the tilemap each time it draws a new character.

Trax

  • RHDN Supporter
  • Hero Member
  • *****
  • Posts: 619
    • View Profile
    • Trax ROM Hacking
Re: Tilemap Editing?
« Reply #6 on: June 27, 2013, 07:25:56 am »
In other words, the present discussion boils down to the concept of data compression. Everything must ultimately come from ROM. If there's text to display on screen, even if, at the code level, text tiles are fed to the VRAM one by one, the data must come from somewhere, either as litteral data or as the result of a decompression algorithm...

The challenge is to find out the location of the raw data, and how it's decompressed...

Gideon Zhi

  • Discord Staff
  • Hero Member
  • *****
  • Posts: 3536
    • View Profile
    • Aeon Genesis
Re: Tilemap Editing?
« Reply #7 on: June 27, 2013, 12:12:08 pm »
In other words, the present discussion boils down to the concept of data compression. Everything must ultimately come from ROM. If there's text to display on screen, even if, at the code level, text tiles are fed to the VRAM one by one, the data must come from somewhere, either as litteral data or as the result of a decompression algorithm...

Uh, no. In the case of text, it's usually generated dynamically/mathematically by the routine that draws the text to the screen. If you're dealing with a fixed width font, and if we wanted to abstract this into higher-level code, it'd look more like "tilePosition+=2"

If we're talking 8x8 vwf, it'd look something like this:
Code: [Select]
int spillover = (oldwidth+newwidth) % 8;
for (int i = 0; i < spillover; i++){
  tilePosition+=2;
  tileIndex+=1;
  updateTilemap(tilePosition, tileIndex);
}
...

void updateTilemap(int pos, int tileData){
  while(!nmi){
    int i = 0;
  }
  vram[pos]=tileData;
}

Real quick, kind of dirty, pretty ugly, but that's the basic concept. Nothing to do with compression at all. No idea where you go that from.

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Re: Tilemap Editing?
« Reply #8 on: June 27, 2013, 01:13:03 pm »
There's no VWF currently.  It's just a flat out 16x16 FWF.  Though that's altered now to be 8x16.  It takes the text, finds the graphics needed and directly stores it to VRAM.  Runs back to the issue I had before.  I have everything labeled out but it's just every third character it draws the letter onto the next line, then comes back up draws two more characters, back down etc..

So if what's being said is the case, it's probably going to be in the area where I had to set it to load half the font size and such.  Though I've gone through and tried to figure out how it does the locations, and it just adds 08 bytes each time for the spacing.

It basically goes like:
Code: [Select]
6100  6108  |  6120  6128  | ...
6110  6118  |  6130  6138  | ...

Though I've gone through the code forwards and backwards multiple times and I'm not entirely sure how it's throwing it out there like that.  In VRAM it's exactly right, all the letters are at where they should be, just once it hits layer 3 on screen it's completely whacked.
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

Gideon Zhi

  • Discord Staff
  • Hero Member
  • *****
  • Posts: 3536
    • View Profile
    • Aeon Genesis
Re: Tilemap Editing?
« Reply #9 on: June 27, 2013, 03:34:04 pm »
Those are your VRAM addresses. 6100, 6108, 6110, and 6118 each correspond to the start of four consecutive 2bpp 8x8 tiles. Note that... eurgh, not sure exactly how to describe this, but the SNES's vram architecture is treated with a 2-byte value as being the "base" unit, so in order to calculate the address in a savestate, you need to double each of those values (C200, C210, C220, C230).

For further hopeful-explanation, here's an example vram dump:



Note in this that "M" is in the first tile, while the second contains "io:" - note also that the lower half of the letters occurs on the second line of tiles. In this case, M is comprised of tiles 00 and 10, while io: is comprised of tiles 10 and 11. Using your addresses, the M woulc be at C200 and C300, while the io: would be at C210 and C310. Note further that most games (including this one) assemble their bitmaps differently; I deliberately used this format to try to illustrate how tile addresses translate to tilemap indexes.

Does that help at all?

Nightcrawler

  • Hero Member
  • *****
  • Posts: 5795
    • View Profile
    • Nightcrawler's Translation Corporation
Re: Tilemap Editing?
« Reply #10 on: June 27, 2013, 04:01:10 pm »
Note that... eurgh, not sure exactly how to describe this, but the SNES's vram architecture is treated with a 2-byte value as being the "base" unit, so in order to calculate the address in a savestate, you need to double each of those values (C200, C210, C220, C230).

The term you need is word based addressing. SNES VRAM can only be word addressed via $2116/$2117. Since we're dealing with byte addressing rather than words in savestate hex editing or VSNES VRAM viewing, you need to double those values to find the effective VRAM locations.

It's done like this because in the hardware, VRAM is stored across two chips addressed at the same time. Then, $2118 goes to one chip and $2119 goes to the other.
TransCorp - Over 20 years of community dedication.
Dual Orb 2, Wozz, Emerald Dragon, Tenshi No Uta, Glory of Heracles IV SFC/SNES Translations

Trax

  • RHDN Supporter
  • Hero Member
  • *****
  • Posts: 619
    • View Profile
    • Trax ROM Hacking
Re: Tilemap Editing?
« Reply #11 on: June 27, 2013, 04:45:38 pm »
Quote
Uh, no. In the case of text, it's usually generated dynamically/mathematically by the routine that draws the text to the screen. If you're dealing with a fixed width font, and if we wanted to abstract this into higher-level code, it'd look more like "tilePosition+=2"

Yeah, I see what you mean. Since the tile size was not specified, and it's not about any specific game, I presumed a 8x8 tile size for layout. But now, assuming there could be DTE involved as well, of course it's another pair of sleeves...

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Re: Tilemap Editing?
« Reply #12 on: June 28, 2013, 02:34:13 am »
Those are your VRAM addresses. 6100, 6108, 6110, and 6118 each correspond to the start of four consecutive 2bpp 8x8 tiles. Note that... eurgh, not sure exactly how to describe this, but the SNES's vram architecture is treated with a 2-byte value as being the "base" unit, so in order to calculate the address in a savestate, you need to double each of those values (C200, C210, C220, C230).

For further hopeful-explanation, here's an example vram dump:



Note in this that "M" is in the first tile, while the second contains "io:" - note also that the lower half of the letters occurs on the second line of tiles. In this case, M is comprised of tiles 00 and 10, while io: is comprised of tiles 10 and 11. Using your addresses, the M woulc be at C200 and C300, while the io: would be at C210 and C310. Note further that most games (including this one) assemble their bitmaps differently; I deliberately used this format to try to illustrate how tile addresses translate to tilemap indexes.

Does that help at all?

No no, I completely understand that and it's very clear.  But it's whenever it hits a certain letter, it displays it incorrectly on the screen.  The letter itself is perfect, but the position is completely wrong, yet the position of it in VRAM is written how it SHOULD be displaying on screen.
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Tilemap Editing?
« Reply #13 on: June 28, 2013, 06:59:43 am »
Hblank shenanigans?

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Re: Tilemap Editing?
« Reply #14 on: June 28, 2013, 01:04:40 pm »
Quite possibly, I don't honestly know where the cause is coming from, but it's extremely frustrating as it's holding up progress significantly.

Edit:

Image 1: What it currently does.
Image 2: How it displays in VRAM.
Image 3: What it SHOULD be doing.

Code: [Select]
$80/B8AF 69 20 00    ADC #$0020              A:0000 X:0002 Y:0420 P:envmxdIZc - VRAM storage of each letter Currently this is set to 08 so the letters are side by side.  Change this to 20 and the letters appear like so.

- AKA the way it SHOULD appear but with wayyyy too much spacing inbetween.
« Last Edit: June 28, 2013, 01:34:26 pm by justin3009 »
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

LostTemplar

  • Hero Member
  • *****
  • Posts: 910
    • View Profile
    • au-ro-ra.net
Re: Tilemap Editing?
« Reply #15 on: June 29, 2013, 03:03:36 am »
You probably need to skip #$10 every #$10 bytes (:= 2 letters). Or better, rewrite the routine that does the tilemap building. According to the other thread, one 16x16 character was built like this

Code: [Select]
0 1
2 3

where 0...3 represents the order of the 8x8 tiles in VRAM. So, when you only increase by #$08 (:= one 8x8 tile), you get to position 0 (your A), then 1 (your B), then 2 (:= next line; your C)! If you wanted to do a nice 8x16 font, you'd have to either a) interleave top and bottom (render top to 0, then bottom to 2, top to 1, bottom to 3, and so on) or b) rewrite the tilemap such that it's

Code: [Select]
0 2
1 3

Well, that's how I think it works, judging from this and the other thread.
« Last Edit: June 29, 2013, 03:13:16 am by LostTemplar »

justin3009

  • Hero Member
  • *****
  • Posts: 1666
  • Welp
    • View Profile
Re: Tilemap Editing?
« Reply #16 on: June 30, 2013, 04:48:35 pm »
I wonder if what I posted in the other thread was the tilemap building.  I'll have to try it when I get home from work tomorrow.

Edit: I'm trying to figure out how exactly to rewrite this.  It's a bit.. confusing :|
« Last Edit: July 04, 2013, 03:27:16 pm by justin3009 »
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'