It is currently Tue Dec 11, 2018 10:47 am

 All times are UTC - 7 hours

 Page 1 of 2 [ 22 posts ] Go to page 1, 2  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 7:47 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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:
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.

 Attachments: File comment: Example of wrong colors smb wrong.jpg [ 30 KiB | Viewed 3193 times ]
Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:21 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:27 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:33 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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 [ 51.86 KiB | Viewed 3185 times ]

Last edited by rainwarrior on Wed Sep 07, 2016 8:39 am, edited 1 time in total.
Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:38 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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.

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:39 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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.

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:45 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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 [ 29.43 KiB | Viewed 3181 times ]
Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:53 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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?

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 8:58 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
With my previous attempt , yes I tried adding and substracting, and it looked different, but still wrong.

I implemented your approach as this:

Code:
if ((ppuV & 64) != 64) {
// 1 or 2
if ((ppuV & 2) == 2) {     QUADRANT 1
quadrant = (attrib & 0x0C) >> 2;
}
} else {
// 3 or 4
if ((ppuV & 2) == 2) {
} else {
}

So I'm only using the V register, not the X one. Now it looks like this:

 Attachments: smb wrong3.jpg [ 28.29 KiB | Viewed 3173 times ]
Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 9:05 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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?

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Wed Sep 07, 2016 10:01 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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.

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Tue Sep 13, 2016 6:43 am

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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,

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Tue Sep 13, 2016 6:37 pm

Joined: Sun Sep 19, 2004 10:59 pm
Posts: 1440
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.

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Tue Sep 13, 2016 8:33 pm

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7007
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.)

Top

 Post subject: Re: Getting right attribute tables with scrollingPosted: Thu Sep 22, 2016 12:59 pm

Joined: Sat Jun 27, 2015 1:09 pm
Posts: 70
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:
if ((ppuV & 64) != 64) {
if (((ppuV + ppuX) & 2) == 2) {
} else {
}
} else {
if (((ppuV + ppuX) & 2) == 2) {
} else {
}
}

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,

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 2 [ 22 posts ] Go to page 1, 2  Next

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki