What is a nametable?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
What is a nametable?
by on (#95325)
As stupid as this sounds, I can't find a definition of "nametable" on Google (all I can find is "Name Tables", especially "popular name tables"). In neslib.h, there's a comment "unpack a nametable into vram". I know it's unRLEing a byte[] to VRAM, but I don't know what a nametable is. I'm also not sure what the RLE format being used is. I'd try to look it up, but I don't know of any neslib docs, and I know that there are a lot of different RLE formats (I implemented Ultima 4's RLE format a couple of weeks ago in C#) and I can't figure out how it works. Maybe it's just I'm being stupid right now, I'm not sure.

P.S. I'm glad that NES homebrew is now so easy I can replace the exchange system in Alter Ego and reset the stage when you touch an enemy despite knowing very little 6502.

by on (#95327)
In short simple words, a nametable is a small tilemap that defines a background graphics (constructs it from individual tiles).

Check out the NesDev Wiki. Here are some details on the nametables.


Here is C RLE unpacking code equivalent to the one used in the neslib:

Code:
void unrle(unsigned char *dst,const unsigned char *src)
{
   unsigned char i,tag,byte;

   tag=*src++;
   byte=0;

   while(1)
   {
      i=*src++;

      if(i==tag)
      {
         i=*src++;

         if(!i) break;

         while(i)
         {
            *dst++=byte;
            --i;
         }
      }
      else
      {
         byte=i;;
         *dst++=byte;
      }
   }
}

by on (#95329)
Thank you, Shiru. That's exactly what I needed. :)

by on (#95331)
On the NES, the background is drawn with tiles, so there must be a way for the program to specify which tiles should be drawn. A name table is just a 32x30 map that indicates which tile goes where in the screen. Each byte represents a tile index.

Each name table has a complementary attribute table after it, which specifies the palettes that each tile should use. The format used for attribute tables isn't as straightforward: each byte specifies the palettes of a 4x4 tile area.

by on (#95332)
Another way that may make better sense to people, a NameTable is a 2 dimensional array of Tile Index Numbers followed by another array of Tile Attribute Numbers. This should be easy to understand if you've programmed a game yourself using a tilemap/grid system.