Switching to CHR-RAM for the first time. A few questions...

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
Sogona
Posts: 186
Joined: Thu Jul 23, 2015 7:54 pm
Location: USA
Contact:

Switching to CHR-RAM for the first time. A few questions...

Post by Sogona »

So I'm at a point in the development of my current project where I'm going to need more than 8kb of CHR data. I'm using MMC1, which can obviously only swap 4k banks. This is more than the amount of tiles I need to switch out at a given time, so CHR-RAM seems like the better option (i.e SNROM)

The wiki has a good article, but really only covers switching entire pages of CHR data, so I don't really know go about doing something like, say, load a specific tile in a specific spot in the PPU, i.e for animation.

I feel like this is way easier than I think it is and that I'm just missing something, but how do you guys organize and load your CHR data? Are there any other techniques I should know about?
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Switching to CHR-RAM for the first time. A few questions

Post by rainwarrior »

Sogona wrote:I don't really know go about doing something like, say, load a specific tile in a specific spot in the PPU, i.e for animation.
Every tile is 16 bytes of data. You write to CHR RAM through $2007, just like nametables, only the address is in the $0000-1FFF range. (Also like nametables, only do when rendering is off or during vblank.)

Example code to update tile $63: (found at address $0630, kind of easy to multiply by 16 bytes in hex.)

Code: Select all

	lda $2002
	lda #$06
	sta $2006
	lda #$30
	sta $2006
	ldy #0
	:
		lda (data), Y
		sta $2007
		iny
		cpy #16
		bcc :-
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Switching to CHR-RAM for the first time. A few questions

Post by tokumaru »

The MMC1 will not help you with animating single tiles, since it can only swap 256 tiles (4KB) at a time (no existing mapper will switch units smaller than 1KB, or 64 tiles), so you're stuck with manually writing 16 bytes per tile to $2007 if you really need small updates. You can of course write tiles progressively to a hidden 4KB page across several vblanks and swap that in when you're done updating all the tiles you need.

Another thing you can do with this setup is draw different animation frames to each 4KB banks beforehand (tiles that don't animate will look the same across all banks), and switch between pages as the game runs to animate the background. This way you can easily do simple animations like water running, leaves blowing, coins spinning, and all sorts of things that make places look more dynamic.
User avatar
Sogona
Posts: 186
Joined: Thu Jul 23, 2015 7:54 pm
Location: USA
Contact:

Re: Switching to CHR-RAM for the first time. A few questions

Post by Sogona »

rainwarrior wrote:
Sogona wrote:I don't really know go about doing something like, say, load a specific tile in a specific spot in the PPU, i.e for animation.
Every tile is 16 bytes of data. You write to CHR RAM through $2007, just like nametables, only the address is in the $0000-1FFF range. (Also like nametables, only do when rendering is off or during vblank.)

Example code to update tile $63: (found at address $0630, kind of easy to multiply by 16 bytes in hex.)

Code: Select all

	lda $2002
	lda #$06
	sta $2006
	lda #$30
	sta $2006
	ldy #0
	:
		lda (data), Y
		sta $2007
		iny
		cpy #16
		bcc :-
Yeah, this makes sense, I just don't know how to go about abstracting it for use with any given tile.

Do most people treat animated tiles like regular animations? Like:

Code: Select all

AnimatedTile0:
  .dw AnimatedTile0_0, AnimatedTile0_1, AnimatedTile0_2, AnimatedTie0_3

AnimatedTile0_1:
  .db ;16 bytes of tile data
And then just have another table of where in the CHR-RAM to store each tile? I was trying to avoid hard-codng, but it kinda seems inevitable.
Post Reply