Metatile issues...

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
d3m1ck
Posts: 4
Joined: Fri May 01, 2020 8:29 pm

Metatile issues...

Post by d3m1ck » Sun May 10, 2020 12:52 pm

Hi all! I am D3m1ck, and someone using ca65, a tool in the cc65 bundle (it's got what I need, I guess - your mileage may vary). My emulator of choice is Mednafen, which is a fun little drag-and-drop emulator that just happens to run NES, PC Engine And SNES, and thus why I am fond of any 65xxx proc.

So, I have done the Hello Mario tutorial, and also with forum resources here I have written a simple BG tile loop for filling the screen with tiles. I have ironed out my own Joypad routines (ca65 requires that if you have to work with ZP variables to AND or ADD/SUB, they have to have the # for immediate value; weird but I see why now - I hope I didn't forget in the code I am about to show). I have even learned how to use the NMI to make counters using BG tiles! I LOVE this system! It's simple and is fun to bend the thing to my will.

Speaking of bending the thing to my will, THIS stubborn ol' thing tho; this is meant to fill a screen with metatiles of 16x16 using a map and a metatable. This is in its very early stages, for I am afraid if I add too much, I will stop programming altogether. That would be sad, seeing I got this far already even with bits and pieces of Nerdy Nights.

Code: Select all

;background CHR initialization <--
;
;includes anything to be plastered onto the BG

	LDA #$20
	STA PPU_AddrHi
	STA PPU_AddrLo_2
	SEC
	SBC #$10
	STA myRAM0
	SEC
	SBC #$10
	STA PPU_AddrLo
	TAX
	
bgSTART:				;Okay, this part works by itself, but...
	LDA metaMapData, X
	ASL
	ASL
	STA metaIndex		;takes map data for metatiles, and * 4 it
	TAY
	
	LDA PPU_StatReset	;writes metatiles upper left to lower right
	LDA PPU_AddrHi		;works GREAT with Y increment!
	STA PPU_Addr16
	LDA PPU_AddrLo
	STA PPU_Addr16
	INC PPU_AddrLo
	LDA metaTileData, Y
	STA PPU_Data
	INY
	
	LDA PPU_StatReset
	LDA PPU_AddrHi
	STA PPU_Addr16
	LDA PPU_AddrLo
	STA PPU_Addr16	
	LDA metaTileData, Y
	STA PPU_Data
	INY

	LDA PPU_StatReset
	LDA PPU_AddrHi
	STA PPU_Addr16
	LDA PPU_AddrLo_2
	STA PPU_Addr16
	INC PPU_AddrLo_2
	LDA metaTileData, Y	
	STA PPU_Data
	INY
		
	LDA PPU_StatReset
	LDA PPU_AddrHi
	STA PPU_Addr16
	LDA PPU_AddrLo_2
	STA PPU_Addr16	
	LDA metaTileData, Y
	STA PPU_Data

	INX					;next tile in map data
	INC PPU_AddrLo
	INC PPU_AddrLo_2
	CPX myRAM0			;whatever value is here.
	BEQ bgNL			;will go to next line increment... 
	JMP bgSTART			;unless inequality occurs, then loops.
bgNL:	
	LDA myRAM0			;<-- From here, NOTHING works as intended.
	CLC					;The number at myRAM0 is supposed to be updated and
	ADC #$10			;tossed back into the above subroutine.
	STA myRAM0			;This way, it starts from a bookmark, writing next line.
	;LDA PPU_AddrLo		;This comparative statement doesn't work, either.
	;CMP #$e0
	;BEQ PPUHi_INC
	LDA PPU_AddrLo		;Here, I am trying to bring this low address to 32 
	CLC					;ahead by adding a value to it.
	ADC #$21
	STA PPU_AddrLo
	LDA PPU_AddrLo_2	;Same like above.
	CLC
	ADC #$21
	STA PPU_AddrLo_2	
	JMP bgSTART
PPUHi_INC:
	LDA PPU_AddrHi		;Here, I want to bring the high byte up,
	INC PPU_AddrHi		;and when it hits a threshold, it doesn't have to 
	STA PPU_AddrHi		;change anymore.
	CMP #$24
	BEQ bgDONE
	JMP bgNL
bgDONE:
I have attached files for anyone who would like to try this out, I guess.

I am still green in the horn here, as some might be able to tell clearer than others. Please be gentle... :wink:
Attachments
Colliz Includes.zip
Plain text files with .INC extension
(896 Bytes) Downloaded 21 times
Test Garden.pal
The palette set used
(32 Bytes) Downloaded 21 times
colliz.chr
Pertinent to show gfx
(8 KiB) Downloaded 21 times
colliz.asm
Main assembly file for ca65
(9.89 KiB) Downloaded 22 times

User avatar
dougeff
Posts: 2707
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Metatile issues...

Post by dougeff » Sun May 10, 2020 2:31 pm

This isn't right...

LDA PPU_AddrHi
INC PPU_AddrHi
STA PPU_AddrHi

You don't load A to use the INC command. INC directly affects the value at the address PPU_AddrHi. It doesn't affect A. You are just overwriting it with the unchanged value in A.

I think this will do what you want.

INC PPU_AddrHi
LDA PPU_AddrHi

But it doesn't look like you are actually writing a PPU address with this code... or maybe that's what PPU_Addr16 is for?

And why is this 21?

ADC #$21

Should that be #$20?
nesdoug.com -- blog/tutorial on programming for the NES

d3m1ck
Posts: 4
Joined: Fri May 01, 2020 8:29 pm

Re: Metatile issues...

Post by d3m1ck » Sun May 10, 2020 6:33 pm

Told you I have issues... Anyway, PPU_Addr16 IS, in fact, $2006. It IS supposed to be $20 and not $21 that I am adding with carry indeed like you said. It's just when I activate the bgNL portion of my code, things disappear or I get what looks like scrunched up metatiles - even with the right values. When bgNL is taken out, i. e. commented to check if the bgSTART portion of the code runs, the first row displays what it should.

So basically, I am having trouble filling the whole screen with the proper metatiles, let alone the the other three rows in the nametable.

Here's what I am trying to achieve:

I read from the metaMapData an index at a given point within 16 bytes, or row 1. This will yield four tile values to be placed after I Arithmetic Shift them left a couple times to get that value times four - the metatile index. The first tile, then I increment one nametable address for the second tile - the top row. I then have to add 16 to the next nametable address (my reasoning for using PPU_AddrLo_2 as opposed to just the Lo variety) for the third tile and then increment one to get the last.

I want bgNL (or bg NewLine) to be just that - new line. So I would preferably want to start on ppu address $2040 after I write $2000-$203f. Then after I am done with the quarter of the nametable, I want to change the high byte of the nametable using PPUHi_INC so I can then fill in the other parts of the screen until $23bf or whatever the end is.

As an aside, the commented out section there should be un-commented, but I was in the middle of trying to debug when I posted this, so that's why that's there. Sorry for that. Thanks for reminding me about the INC for PPU_AddrHi and how loading it and storing it was a fruitless effort. Changed.

User avatar
tokumaru
Posts: 11741
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Metatile issues...

Post by tokumaru » Sun May 10, 2020 7:23 pm

d3m1ck wrote:
Sun May 10, 2020 12:52 pm
My emulator of choice is Mednafen
Keep in mind that sticking to a single emulator when doing NES development is never a good idea. When you're starting out and trying to get the timing of things right, there are a lot of things that could go wrong because of subtle timing issues, that not all emulators get right. During this critical phase, it's best to stick to a few known-accurate emulator, such as Mesen or Nintendulator, which also have debugging tools that are extremely helpful.

I'm not sure if this is the case with Mednafen, but these multi-system emulators usually focus on compatibility (i.e. getting existing games - specially commercial ones - running without apparent glitches) and not necessarily on accuracy, which means they may be less strict than a real console and allow things that the real hardware wouldn't.

Pokun
Posts: 1482
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Metatile issues...

Post by Pokun » Mon May 11, 2020 3:20 am

Mednafen is quite good for systems like PC Engine (the best emulator there is for it besides Mame) where it uses its own core, but its Nes core is based on FCE Ultra and is probably (correct me if I'm wrong) quite outdated compared to Mesen, Nintendulator and puNES. Both in terms of compatibility and features and in terms of accuracy. So yeah it's best to at least test your game with one of these emulators as well from time to time to spot bugs early on. I suggest Mesen for its rich debugging features, accuracy, compatibility and overall user-friendliness.

d3m1ck
Posts: 4
Joined: Fri May 01, 2020 8:29 pm

Re: Metatile issues...

Post by d3m1ck » Mon May 11, 2020 7:22 am

I dl'd Mesen, and I like it already! Thanks! At least I don't have to worry about dl ing a certain version of an emulator (that's at YOU, Mr. FCE Ultra X). Also, I like how things are a LOT more visual! Can't wait to actually get proper use out of this!

Man, am I deprived... :wink:

d3m1ck
Posts: 4
Joined: Fri May 01, 2020 8:29 pm

Re: Metatile issues...

Post by d3m1ck » Mon May 11, 2020 7:36 am

Update: I updated my code with the fixes, and according to Mesen, it's scrolled horizontally a quarter of the way down, and not putting down ANY tiles... Hmmm... I just want something that DOESN'T scroll and has a full screen of metatiles. And my poor attribute table (didn't add to my metatile routines as I am just starting out y'know)! Where did it go?

Decommented the commented parts, changed the INC thing, and set all the $21's to $20's to get this far.

I accidentally clicked on the APU viewer, and the duty cycle is going nuts!

I think I messed up somewhere. But where?

User avatar
tokumaru
Posts: 11741
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Metatile issues...

Post by tokumaru » Mon May 11, 2020 7:51 am

d3m1ck wrote:
Mon May 11, 2020 7:36 am
according to Mesen, it's scrolled horizontally a quarter of the way down, and not putting down ANY tiles
This normally happens when you don't reset the scroll after writing data to VRAM. Using $2006 and $2007 affects the scroll, so you must remember to always reset the scroll via $2000 (name table bits) and $2005 (X scroll, Y scroll) after using VRAM.

Pokun
Posts: 1482
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Metatile issues...

Post by Pokun » Tue May 12, 2020 6:12 am

d3m1ck wrote:
Mon May 11, 2020 7:22 am
I dl'd Mesen, and I like it already! Thanks! At least I don't have to worry about dl ing a certain version of an emulator (that's at YOU, Mr. FCE Ultra X). Also, I like how things are a LOT more visual! Can't wait to actually get proper use out of this!
Going from Mednafen, which although is quite nice hardly has a usable GUI, to Mesen must be something like going from an axe to a woodcutter machine. I kind of wish Sour would make a "Mesen-PC" so that PC Engine development becomes a bit less fiddly than it is now. Mednafen do have a nice debugger, but you have to look up in the manual just to find the right key combination to open it, then the right keys to switch to different modes of it etc. I wish it was a bit more like Mame or even openMSX which both are also using window-less GUIs but are less finicky.

Sour
Posts: 815
Joined: Sun Feb 07, 2016 6:16 pm

Re: Metatile issues...

Post by Sour » Tue May 12, 2020 10:01 pm

Pokun wrote:
Tue May 12, 2020 6:12 am
I kind of wish Sour would make a "Mesen-PC" so that PC Engine development becomes a bit less fiddly than it is now.
If only I had infinite free time! Although I don't think I've ever even played a PC engine game in my life...
On the up side, I'm working on super gameboy support for Mesen-S, so you'll (eventually) get a gameboy emulator w/ good debug tools out of it as a bonus, too! :p
Though I honestly can't imagine trying to support even more hardware at this point - I'm already stretching myself pretty thin between Mesen & Mesen-S, as it is.

Pokun
Posts: 1482
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Metatile issues...

Post by Pokun » Wed May 13, 2020 7:47 am

The PC Engine is a 65x system and not that different from NES and SNES hardware-wise (I think it's a good middle ground that fills in a gap between the two), but yeah working on both Mesen and Mesen-S is probably already a lot of work. I should say it like this: I wish the PC Engine had an emulator comparable to Mesen.
If you haven't played any PC Engine games, what are you waiting for? Go play it! I made a mini guide for people looking to get into the games and the hardware. The PC Engine Mini is released, and probably a good and cheap alternative to collecting the system as it contains many of the best games.

A Super Game Boy Mesen is great news. The GB has many good emulators like BGB, GBE+ (props for emulating the obscure stuff), Sameboy and VBA-M, but it's not like for NES and I don't think there is a really good open source alternative that is comparable to BGB.

User avatar
gauauu
Posts: 698
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Metatile issues...

Post by gauauu » Wed May 13, 2020 9:38 am

Mesen is open source, right? Seems like a prime opportunity for someone else to make the PC-engine port :-)

Post Reply