It is currently Sun Dec 10, 2017 8:59 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Wed Sep 07, 2016 7:47 am 
Offline

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.

Thanks for your help


Attachments:
File comment: Example of wrong colors
smb wrong.jpg
smb wrong.jpg [ 30 KiB | Viewed 1886 times ]
Top
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:21 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:27 am 
Offline

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
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:33 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
smb_fceux_nametable.png [ 51.86 KiB | Viewed 1878 times ]


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

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
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:39 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:45 am 
Offline

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
smb wrong 2.jpg [ 29.43 KiB | Viewed 1874 times ]
Top
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:53 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
 Profile  
 
PostPosted: Wed Sep 07, 2016 8:58 am 
Offline

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 & 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 1866 times ]
Top
 Profile  
 
PostPosted: Wed Sep 07, 2016 9:05 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
 Profile  
 
PostPosted: Wed Sep 07, 2016 10:01 am 
Offline

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
 Profile  
 
PostPosted: Tue Sep 13, 2016 6:43 am 
Offline

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
 Profile  
 
PostPosted: Tue Sep 13, 2016 6:37 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 10:59 pm
Posts: 1393
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
 Profile  
 
PostPosted: Tue Sep 13, 2016 8:33 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5885
Location: Canada
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
 Profile  
 
PostPosted: Thu Sep 22, 2016 12:59 pm 
Offline

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) {
            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,


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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 5 guests


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

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group