Tiles in Basic II - Modes 2 - 4

Let us continue with exploring Tile modes on Commander X16. We covered first two of five tile modes available in part one. Now we will focus on last three modes:
0 – 1 bit per pixel tiles– 16 colors
1 – 1 bit per pixel tiles– 256 colors
2 – 2 bits per pixel tiles – 3 colors plus transparent
3 – 4 bits per pixel tiles – 15 colors plus transparent
4  - 8 bits per pixel tiles – 255 colors plus transparent

Modes 2-4 have similar memory layout with only difference being on how many bits per pixel we use and as result how many colors can we show within single tile. The data is stored in memory in a very straightforward way. Let’s compare all three modes in a picture below:


As we see, we have wide range of possibilities for how colorful tiles we want. We have to remember that 0 in all three modes is always transparent. So in mode 2 we can use 3 colors plus transparent, in mode 3 – 15 colors plus transparent and in mode 4 we can use 255 different colors in a single tile.

Like in Modes 0 and 1 Tile Base is pointing to Tile definitions, however now we will use more than one bit per pixel. The data is stored in memory for each tile starting at the top left corner.
For example, let’s store 8x8 tile in Mode 3 and pick VRAM location $10000. Each pixel requires 4 bits (a nibble) which means that one byte can contain data for two pixels. So at location $10000 we will store first two pixels, in location $10001 we will store third and fourth pixels and so on for total of 32 bytes for single tile. Mode 2 will be probably more popular in games as it only requires 16 bytes per 8x8 tile. With 64 bytes per single 8x8 tile I don’t think Mode 4 will be very practical for games but it is there just in case.
So how do these color combinations translate into actual colors on screen? For that we have to first look at how the screen data is organized. Remember we use Map Base pointer in Layer register to point at it and even though we still use two bytes per tile(character) it is quite a bit different that in modes 0 and 1:


Offset Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
0 Tile Index (bits 0-7)
1 Palette Offset (bits 4-7) V-flip H-flip Tile Index (bits 8-9)


First difference we see is that the Tile Index (we called it Character Index before) is 10 bits long, which means we can address up to 1024 different tiles. Note that we are not storing memory locations but index to tiles. VERA “knows” in which mode we are and is calculating the memory location based on that. This gives us a lot of flexibility and only real constraint is available Video memory (128K).
Bits 2 and 3 are pretty straightforward but are very valuable. They allow us to flip vertically and/or horizontally any tile on the screen. This allows us to reuse same graphics in different color combinations and different locations and thus saving some valuable video memory and CPU because VERA does a lot of work for us.

The Palette Offset requires some more explanation. We can use Palette offset to reach all 256 colors in our palette but there are some exceptions:
Color 0 is always transparent, regardless of  how the color is defined.
The colors 16-255 are unchanged. This rule affects only Mode 4 where we have full byte available for defining color for the single pixel. In other words in Mode 4 if we set pixel value to e.g. 120 the pixel will be the color of palette in position 120, it is not affected by the Palette offset value. This makes sense since in Mode 4 we have access to the whole Palette anyway.
In order for modes 2 and 3 to take advantage of the 256 colors we have to combine the pixel value with Palette Offset. VERA documentation states that we have to add 16 x offset. That essentially means that offset are bits 4-7 to the color in palette. It is therefore perfectly matched with Mode 3 where we use 4 bits per pixel plus 4 bits from offset for 8 bit color index.
In mode 2 we have some gaps in addressing the palette and in mode 4 we might have some overlapping so it might be better to just keep palette Offset at 0. If this sounds a bit confusing let’s try to illustrate with a picture:



Let’s put this knowledge to the test. Mode 3 seems to be very appropriate for games, with plenty of colors available and moderate use of memory so let’s write a simple program to demonstrate it.

We are not focusing on the design of tiles so we will just use default single color text tiles and transform them into colorful tiles.


In lines 30 – 170 we simply parse first 40 characters and expand them from one bit per pixel to four bits per pixel. And to make the whole thing more colorful we use 01 in first row 10 in second and so on for color %1000 for eighth row (which is mostly empty for fonts).
The default font is located in address $0F800 and since it contains 256 characters with 8 bytes each it means that it occupies Video memory from $0F800 to $0FFFF. So let’s put our new tile set starting at $10000. That is why we start with memory 0 in variable M and why we use bank $1 in VPOKE command in line 130.
In Lines 210 -230 we turn on Tile Mode 3. We are using the same memory for screen memory as default starting at address $00000. In lines 230 we set the Tile Base to point to our newly generated tile set at $10000. 
In lines 310-370 we are drawing first 40 characters to screen in all 30 rows of 40x30 screen mode.
In Line 350 we are switching Palette Offset for every line from 0 to 15 and then start at 0 again.

The result is another extremely colorful screen:




Link to source

Part I – Tile Mode 0 and 1

Comments

Popular posts from this blog

Commander X16 Premium Keyboard