NES Vertical Scrolling Screen Tear Issue

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
Sl1pm0de
Posts: 6
Joined: Sun Jan 17, 2016 1:14 pm

NES Vertical Scrolling Screen Tear Issue

Post by Sl1pm0de »

Scenario:
I have a sprite in the center of the screen. The background x and y scroll is updated by the controller. I allow to scroll infinitely in any direction. The background continues to repeat (which is what I want.) I've experimented with 4 way mirroring and loading the same nametable into each of the 4 nametable addresses so the image always repeats.

Problem:
I notice that when I scroll horizontal the image repeats with no issue but when I continue to scroll vertical a set of artifacts appears on the top of the screen that appear to be generated by the PPU and not from any graphics in my .chr file. When y reaches between 240-255 the artifacts appear. I believe it has something to do with x being from 0-255 and the size of the nametable for y is 0-239.

Question:
How can I scroll in any direction for any length of distance and have a background continue to repeat without seeing the artifacts I mention above?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: NES Vertical Scrolling Screen Tear Issue

Post by lidnariq »

nesdevwiki:PPU scrolling wrote:For this reason, a write [y] >= 240 to $2005 may appear as a "negative" scroll value, where 1 or 2 rows of attribute data will appear before the nametable's tile data is reached.
TL;DR, don't do that. On the NES for Y scrolling, 256 comes after 239.
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: NES Vertical Scrolling Screen Tear Issue

Post by Bregalad »

"Negative scrolling" is badly worded for what happens there. A more accurate description would be "scroll into the attribute table", where the AT data is displayed on the screen as NT data. I'm not saying this feaure doesn't have any applications, but so far I didn't find any so if there is any use to this it is in extremely weird/special cases.

For instance if you badly needed a 32x32 tile map and would be ready to sacrifice coulours for it, you could set all 4 BG palettes to the same values and use AT as NT, to get the required 32x32 map.

In typically scrolling case, you do not want to do that, and want to go from 239 to 0 directly.
User avatar
darryl.revok
Posts: 520
Joined: Sat Jul 25, 2015 1:22 pm

Re: NES Vertical Scrolling Screen Tear Issue

Post by darryl.revok »

Here's a good thread I found when I was dealing with this:

viewtopic.php?f=10&t=10958
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: NES Vertical Scrolling Screen Tear Issue

Post by tokumaru »

After incrementing the Y scroll, check if it's larger than 239. If so, add 16 to force a wrap to the next screen, skipping over the attribute tables. After decrementing it, check if it underflowed below 0, and subtract 16 if that's the case.
Sl1pm0de
Posts: 6
Joined: Sun Jan 17, 2016 1:14 pm

Re: NES Vertical Scrolling Screen Tear Issue

Post by Sl1pm0de »

Thanks guys. I got it to work this way during my controller reads for up and down.

Code: Select all

player_up_update:
	lda scrolly				; Load scrolly position
	sec
	sbc	#$01				; Subtract from A
	stay  scrolly				; Update scrolly position
	cmp	#$ef					; Compare scrolly to 239
	bcs	update_scrollyup		; If more than 239 update scrolly
	jmp	player_down_read		; Jump to player_down_read

update_scrollyup:
	lda	scrolly
	sec
	sbc	#$10				; Subtract 16 from scrolly position
	sta	scrolly
	rts

player_down_update:
	lda scrolly				; Load scrolly position
	clc
	adc	#$01				; Add from A
	stay  scrolly				; Update scrolly position
	cmp  #$ef					; Compare scrolly to 239
	bcs	update_scrollydown		; If more than 239 update scrolly
	jmp	player_left_read		; Jump to player_left_read

update_scrollydown:
	lda	scrolly
	clc
	adc	#$10				; Add 16 to scrolly position
	sta	scrolly
	rts
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: NES Vertical Scrolling Screen Tear Issue

Post by Bregalad »

Youre making it more complicated than needed. This is simpler :

Code: Select all

   lda scrolly
   clc
   adc scrollyspeed
   cmp #$f0
   bcc +
   sbc #$f0
+  sta scrolly
AWJ
Posts: 433
Joined: Mon Nov 10, 2008 3:09 pm

Re: NES Vertical Scrolling Screen Tear Issue

Post by AWJ »

Sl1pm0de wrote:Thanks guys. I got it to work this way during my controller reads for up and down.

Code: Select all

player_up_update:
	lda scrolly				; Load scrolly position
	sec
	sbc	#$01				; Subtract from A
	stay  scrolly				; Update scrolly position
	cmp	#$ef					; Compare scrolly to 239
	bcs	update_scrollyup		; If more than 239 update scrolly
	jmp	player_down_read		; Jump to player_down_read

update_scrollyup:
	lda	scrolly
	sec
	sbc	#$10				; Subtract 16 from scrolly position
	sta	scrolly
	rts

player_down_update:
	lda scrolly				; Load scrolly position
	clc
	adc	#$01				; Add from A
	stay  scrolly				; Update scrolly position
	cmp  #$ef					; Compare scrolly to 239
	bcs	update_scrollydown		; If more than 239 update scrolly
	jmp	player_left_read		; Jump to player_left_read

update_scrollydown:
	lda	scrolly
	clc
	adc	#$10				; Add 16 to scrolly position
	sta	scrolly
	rts
You can't use conditional branch instructions to branch to a subroutine like you're doing. Those instructions don't push a return address.
Sl1pm0de
Posts: 6
Joined: Sun Jan 17, 2016 1:14 pm

Re: NES Vertical Scrolling Screen Tear Issue

Post by Sl1pm0de »

Thanks. This ended up working. I use #$ef instead of #$f0 because you start to see the attribute table starting at #$f0 (240). I had seen the + method used before but didn't understand it. Now I do. It enabled me to jump the instruction (I'm pretty new at this).

I was adding 16 to 240 or subtracting 16 from an undersigned value (<0) to make it work before. This allowed me to skip the attribute table that was showing. Can someone help me understand why adding or subtracting (depending on the controller direction) 239 to 239 works?

Thanks

Code: Select all

player_up_update:
	lda scrolly					; Load scrolly position
	sec
	sbc	scrollspeed				; Advance the scroll
	cmp	#$ef					; Compare scrolly to 239
	bcc	+
	adc	#$ef					; Add 239 to scrolly
+	sta	scrolly					; Update scrolly position
	jmp	player_down_read		; Jump to player_down_read
	
player_down_update:
	lda scrolly					; Load scrolly position
	clc
	adc	scrollspeed				; Advance the scroll
	cmp #$ef					; Compare scrolly to 239
	bcc	+
	sbc	#$ef					; Subtract 239 to scrolly
+	sta	scrolly					; Update scrolly position
	jmp	player_left_read		; Jump to player_left_read
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: NES Vertical Scrolling Screen Tear Issue

Post by tepples »

Adding 239 is the same as subtracting 17.
Post Reply