Updating attributes already in the attribute table

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

User avatar
rainwarrior
Posts: 7812
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Updating attributes already in the attribute table

Post by rainwarrior » Mon May 02, 2016 3:22 pm

To JoeGTake2:

I don't see anything directly wrong with your code logic (aside from the missing quadrant selection, of course), but I will ask why the addr variable in your example appears to be big endian, and the attributeToChangeAddress is little endian? Is that intentional? Also it doesn't match the name attributeAddressToChange used later; i'm assuming they're supposed to be the same variable?

What specifically was wrong with your code when you tested it? Did it appear to do nothing? Did you put a breakpoint on that $2007 write to see what was going through?

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

Re: Updating attributes already in the attribute table

Post by dougeff » Mon May 02, 2016 3:56 pm

OMG, addr+1 is the small byte in your code? Now it makes sense to me. Ok.
nesdoug.com -- blog/tutorial on programming for the NES

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

Re: Updating attributes already in the attribute table

Post by dougeff » Mon May 02, 2016 8:01 pm

Here's a mixture of the original code and mine...
NOTE:addr is the low byte, addr+1 is the high byte

Also NOTE: new_bits = 0-3 = the palette of a given 16x16 block (for anyone out there who doesn't know).

Code: Select all

    LDA addr
    LSR
    LSR
    CMP #$20
    AND #%00000111
    STA temp

    LDA addr+1
    ROL
    ASL
    ASL
    ASL
    ORA #$C0
    ORA temp
    STA attrib_addr

    LDA addr+1
    ORA #$03
    STA attrib_addr+1


	Bit $2002
	Lda attrib_addr+1
	Sta $2006
	Lda attrib_addr
	Sta $2006
	Lda $2007 ;junk
	Lda $2007 ;data
	Sta temp

	Lda addr
	And #$40 ;top or bottom?
	BNE BOTTOM

TOP:
	Lda addr
	And #02
	BNE TOP_R

TOP_L:
	Lda temp ;our data
	And #$fc ; mask out bits
	ora new_bits ;in right 2 bit position
	Sta temp
	Jmp END

TOP_R:
	Lda temp ;our data
	And #$f3 ; mask out bits
	Sta temp
	Lda new_bits ;in right 2 bit position
	Asl a
	Asl a
	Ora temp
	Sta temp
	Jmp END

BOTTOM:
	Lda addr
	And #02
	BNE BOT_R

BOT_L:
	Lda temp ;our data
	And #$cf ; mask out bits
	Sta temp
	Lda new_bits ;in right 2 bit position
	Asl a
	Asl a
	Asl a
	Asl a
	Ora temp
	Sta temp
	Jmp END

BOT_R:
	Lda temp ;our data
	And #$3f ; mask out bits
	Sta temp
	Lda new_bits ;in right 2 bit position
	lsr a
	Ror a
	Ror a
	Ora temp
	Sta temp

END: ;put new data in attrib table
	Lda attrib_addr+1
	Sta $2006
	Lda attrib_addr
	Sta $2006
	Lda temp
	Sta $2007
Tested. It works.
nesdoug.com -- blog/tutorial on programming for the NES

JoeGtake2
Posts: 333
Joined: Tue Jul 01, 2014 4:02 pm

Re: Updating attributes already in the attribute table

Post by JoeGtake2 » Tue May 03, 2016 8:12 am

Thanks so much for all the help and explanation. This is *close* to working.

It seemed to give a false positive. For instance, I directly loaded 20 84 respectively into the addr bytes and ran the routine. It worked exactly as expected. But then dropped 20 - 86 into the bytes, and got the same output as 20 84...anomalous, for sure.

For kicks, I tried 21 84, that also worked. As did 21 44. And 20 c4. But in some instances, changing the input value had similar anomalous effects.

After much playing to see what direct values i got to work and which ones didn't, it seems the bottom rows behave as expected, but the top rows do not. I'm playing with it a bit, but that said...anything stick out?

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

Re: Updating attributes already in the attribute table

Post by dougeff » Tue May 03, 2016 9:52 am

It worked for me.

You'll need to either post the code or the ROM, or perhaps step through the code in a debugger.
nesdoug.com -- blog/tutorial on programming for the NES

JoeGtake2
Posts: 333
Joined: Tue Jul 01, 2014 4:02 pm

Re: Updating attributes already in the attribute table

Post by JoeGtake2 » Tue May 03, 2016 9:59 am

Sorry - I debugged. It absolutely works. corrupted variable on my end...fixed and now flawless. :-)

You guys rock. Glad to know I wasn't *too* far off. Can't thank you guys enough for your constant feedback!

sdm2
Posts: 12
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Updating attributes already in the attribute table

Post by sdm2 » Sun May 31, 2020 5:26 am

I have a problem with the reverse of the code - that is, I would like to save the value of the block's palette from the given PPU address.
So in general, I'd like to reverse the operation of existing code.

EDIT: ok i think it works (demo.nes - button A place new block palette, button B copies the existing block palette)

Code: Select all

	LDA PPU_BLOCK_AddrLO
	LSR A
	LSR A
	CMP #$20
	AND #%00000111
	STA temp

	LDA PPU_BLOCK_AddrHI
	ROL A
	ASL A
	ASL A
	ASL A
	ORA #$C0
	ORA temp
	STA PPU_ATTR_AddrLO

	LDA PPU_BLOCK_AddrHI
	ORA #$03
	STA PPU_ATTR_AddrHI

	BIT $2002

	LDA PPU_ATTR_AddrHI
	STA $2006
	LDA PPU_ATTR_AddrLO
	STA $2006
	LDA $2007		;junk
	LDA $2007		;data
	STA temp

	LDA PPU_BLOCK_AddrLO
	AND #$40		;up or down?
	BNE BOTTOM

TOP:
	LDA PPU_BLOCK_AddrLO
	AND #02
	BNE TOP_R

TOP_L:

;	LDA temp		;our data
;	AND #$fc		;mask out bits
;	ORA palette		;in right 2 bit position
;	STA temp

	;copy temp bits0,1 (000000xx) to palette bit0,1
	
	lda temp
	and #%00000011
	sta palette

	JMP END

TOP_R:

;	LDA temp		;our data
;	AND #$f3		;mask out bits
;	STA temp
;	LDA palette		;in right 2 bit position
;	ASL A
;	ASL A
;	ORA temp
;	STA temp

	;copy temp bits2,3 (0000xx00) to palette bit0,1
	
	lsr temp
	lsr temp
	lda temp
	and #%00000011
	sta palette

	JMP END

BOTTOM:

	LDA PPU_BLOCK_AddrLO
	AND #$02
	BNE BOT_R

BOT_L:

;	LDA temp		;our data
;	AND #$cf		;mask out bits
;	STA temp
;	LDA palette		;in right 2 bit position
;	ASL A
;	ASL A
;	ASL A
;	ASL A
;	ORA temp
;	STA temp

	;copy temp bits4,5 (00xx0000) to palette bit0,1
	
	lsr temp
	lsr temp
	lsr temp
	lsr temp
	lda temp
	and #%00000011
	sta palette

	JMP END

BOT_R:

;	LDA temp		;our data
;	AND #$3f		;mask out bits
;	STA temp
;	LDA palette		;in right 2 bit position
;	LSR A
;	ROR A
;	ROR A
;	ORA temp
;	STA temp

	;copy temp bits6,7 (xx000000) to palette bit0,1
	
	lsr temp
	lsr temp
	lsr temp
	lsr temp
	lsr temp
	lsr temp
	lda temp
	and #%00000011
	sta palette

END:				;put new data in attrib table (not used)

;	LDA PPU_ATTR_AddrHI
;	STA $2006
;	LDA PPU_ATTR_AddrLO
;	STA $2006
;	LDA temp
;	STA $2007

Attachments
demo.nes
(128.02 KiB) Downloaded 10 times

Post Reply