Getting right attribute tables with scrolling

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Getting right attribute tables with scrolling

Post by DarkMoe »

Hi ! Me again,

I've been trying to fix a bug in my emulator for 2 weeks, and I'm going crazy. You will probably laugh at my issue, since it must be very simple.

When I'm making my quadrant calculations for applying colors, it works perfectly, except on games with scrolling (only tested horizontal, but I'm sure vertical will also fail).

What I'm doing is this:

Code: Select all

int row32 = scanline % 32;
int column32 = (i + ppuX) % 32;
if (row32 <= 15) {
	if (column32 <= 15) {
        	quadrant = attrib & 0x03;			// bits 0-1
	} else {
		quadrant = (attrib & 0x0C) >> 2;		// bits 2-3
	}
} else {
	if (column32 <= 15) {
        	quadrant = (attrib & 0x30) >> 4;		// bits 4-5
	} else {
        	quadrant = (attrib & 0xC0) >> 6;		// bits 6-7
	}
}
SMB for example, sometimes will draw parts of the cloud with green colour, and some question marks blocks will also flicker different colours depending on how much I scroll.

I debugged, and looks like the issue is with my "column32" variable, that sometimes will pick the 4th quadrant instead of the third one which has the correct white color for the clouds.

How am I supposed to make this calculation correctly ? Something regarding the scrolling is throwing it off.

Thanks for your help
Attachments
Example of wrong colors
Example of wrong colors
smb wrong.jpg (30 KiB) Viewed 5302 times
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

There's a formula on the wiki for mapping a nametable memory address to its attribute, if this is helpful:
PPU scrolling # Tile and attribute fetching
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

Yes, I'm using this one:

attribute address = 0x23C0 | (v & 0x0C00) | ((v >> 4) & 0x38) | ((v >> 2) & 0x07)

and it's working ok, the issue with the calculation of this:
http://wiki.nesdev.com/w/index.php/PPU_attribute_tables

Somehow I'm not nailing down the formula for those quadrants
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

I notice in the picture you posted, the visible errors are all from the $2400 nametable region, and the $2000 nametable looks fine. Perhaps the problem has something to do with nametable selection, and not just picking a quadrant?
Attachments
smb_fceux_nametable.png
Last edited by rainwarrior on Wed Sep 07, 2016 8:39 am, edited 1 time in total.
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

Im not 100% sure, but, the clouds attribute is coming as 0x20, that means only the third quadrant is loaded with a palette different than 0, (palette 3).

So again, Im sure my quadrant logic is off. I dont know how to account for scrolling, Im using the fine scrollX to get the quadrant, but thats not working correctly.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

Also maybe worth pointing out that you can deduce quadrant from the address, just like you can deduce the attribute. You don't need to be converting from a pixel location.
  • (v & 2) is the bit that decides horizontal quadrant.
  • (v & 64) is the bit that decides vertical quadrant.
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

Ok that's really helpful ! I was looking for something like that.

Is that hex numbers 0x64 or decimal 64 ?

This is what it looks like using either
Attachments
smb wrong 2.jpg
smb wrong 2.jpg (29.43 KiB) Viewed 5290 times
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

I can't interpret that image but to explain those two values:

Every X column is 1 byte wide, so if v increments by 1, so does X. The attribute columns are 2 tiles wide, so it changes every 2 bytes. (v & 2)

Every Y row is 32 bytes wide. The attribute rows are 2 tiles tall, so it changes every two rows, 32 * 2 = 64. (v & 64)


Before you do that, though, looking at your pixel-based attempt, are you maybe just adding the scroll value when you should be subtracting it?
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

With my previous attempt , yes I tried adding and substracting, and it looked different, but still wrong.

I implemented your approach as this:

Code: Select all

                if ((ppuV & 64) != 64) {
			// 1 or 2
			if ((ppuV & 2) == 2) {     QUADRANT 1
				quadrant = attrib & 0x03;
			} else {           QUADRANT 2
				quadrant = (attrib & 0x0C) >> 2;
			}
		} else {
			// 3 or 4
			if ((ppuV & 2) == 2) {
				quadrant = (attrib & 0x30) >> 4;		// QUADRANT 3
			} else {
				quadrant = (attrib & 0xC0) >> 6;		// QUADRANT 4
			}
So I'm only using the V register, not the X one. Now it looks like this:
Attachments
smb wrong3.jpg
smb wrong3.jpg (28.29 KiB) Viewed 5282 times
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

You're doing something on a per-pixel basis instead of per-tile. V only changes on a tile, it should be impossible to see an attribute split halfway through a tile like in your screenshot. Are you somehow using a different V for your attributes than you are to fetch the tile, changing it on every pixel?
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

mmm, looks like I'm not changing V on a pixel basis.

However, I found something suspicious with the tile fetching.

So, according to that wiki, I'm storing first and second tiles on PPU cycles 328 and 336 of previous scanline (same for attributes), and then tile 3 at 8 cycles of current scanline, tile 4 at 16 cycles, etc.

This is how I'm getting the current tile for drawing:
tile = arrayOfTiles[(currentPixel + ppuX) / 8];

That is, if I'm rendering the third pixel of the scanline, and ppuX = 7, then (2 + 7) / 8 = 1, fetch the secondTile, and do the same with the attribute.

If I remove ppuX from there, I get correct attributes, but scrolling is super trippy.

I'm close, but something is still missing.
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

Im going crazy.

No matter what combination I use, when I get correct colours, the scrolling is glitched, and when scrolling works, I get those incorrect colors in some tiles.

Any tips on what should I check ?

I'm caching the first and second tiles on video cycles 328 and 336 of the previous line, and the rest on cycles 8, 16, etc of the current line.

How does X register account for tile fetching ? .. Im using (currentDot + X) / 8 = Index of my array of prefetched tiles,

Thanks,
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Getting right attribute tables with scrolling

Post by Quietust »

DarkMoe wrote:How does X register account for tile fetching ? .. Im using (currentDot + X) / 8 = Index of my array of prefetched tiles
The upper 5 bits of the X scroll register affect tile fetching (combined with Y scroll and the 2 nametable select bits), and they're incremented (just the upper 5 bits) after each tile.

The bottom 3 bits of the X scroll register, on the other hand, never change - the only effect they have is on which tile pixels get drawn to the screen, effectively shifting the entire scanline left by up to 7 pixels.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Getting right attribute tables with scrolling

Post by rainwarrior »

DarkMoe wrote:How does X register account for tile fetching ? .. Im using (currentDot + X) / 8 = Index of my array of prefetched tiles
You should be fetching the tiles using the PPU address, which should increment (after also fetching the attribute), then at the end of each scanline reload the PPU address and increase it by 1 line.

There should be no way that attributes and tiles get out of synch with each other; they should both be fetched based on the same PPU address, and cached together however you're doing that. Don't do one thing for tiles and a different thing for attributes.

(There's more precise information about the timing on the wiki but they're less important than solving the primary problem you're having of the tiles not being synchronized with their attributes.)
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Getting right attribute tables with scrolling

Post by DarkMoe »

Narrowed it down to quadrant fetching.

When there's fine X scroll involved, quadrant number and attributes data don't match.

So, I guess I need to add that fine x scroll field to quadrant logic ?

My current logic:

Code: Select all

                if ((ppuV & 64) != 64) {
			if (((ppuV + ppuX) & 2) == 2) {
				quadrantPalette = (attrib & 0x03);			// quadrant 1
			} else {
				quadrantPalette = (attrib & 0x0C) >> 2;		// quadrant 2
			}
		} else {
			if (((ppuV + ppuX) & 2) == 2) {
				quadrantPalette = (attrib & 0x30) >> 4;		// quadrant 3
                        } else {
				quadrantPalette = (attrib & 0xC0) >> 6;		// quadrant 4
			}
		}
Still can't get it right, looks like I'm getting quadrant 4 for those white clouds, when I should be getting quadrant 3. Only happens when fine x scroll > 0 (at x scroll = 7, the entire tile is wrongly colored)

Any idea ? Thanks,
Post Reply