Updating CHR RAM during NMI causes a glitch

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
TryCatchFinally
Posts: 2
Joined: Thu Mar 17, 2016 1:09 am

Updating CHR RAM during NMI causes a glitch

Post by TryCatchFinally »

Hello,

So I'm using CHR-RAM instead of ROM in my project and I want to update a tile in the pattern table on the fly. Everything is working fine except the code is updating tiles at $1110 and $1000 when I only want to update the tile at $1110. For some reason the "LDA patternDataPoLo" seems to be causing it even though it has the same value ($#10) which I checked with fceux. if I use Immediate addressing it seems to work fine and only the tile at $1110 is being updated. I know I'm missing something here but it's driving me insane. Any pointers or tips how I can debug this?

This is what I have at the start of NMI

Code: Select all

NMI:
  LDA #$00
  STA $2003
  LDA #$02
  STA $4014       ; sprite DMA from $0200

    LDA isTileUpdate
    BEQ @SkipTileUpdate
    LDA $2002             ; read PPU status to reset the high/low latch
    LDA ppuAddressHi
    STA $2006             ; write the high byte of $2000 address
    LDA ppuAddressLo
    STA $2006             ; write the low byte of $2000 address

    LDA $2007
    TAY
    LSR
    LSR
    LSR
    LSR
    CLC
    ADC #$10
    STA patternDataPoHi
    TYA
    AND #%00001111
    ASL
    ASL
    ASL
    ASL
    STA patternDataPoLo

    LDA $2002         ; read PPU status to reset the high/low latch
    LDA patternDataPoHi
    STA $2006         ; write the high byte of $1000 address
    LDA patternDataPoLo
    ;LDA #$10 ;IF I USE THIS INSTEAD OF LDA patternDataPoLo then it works without a glitch
    STA $2006 ; write the low byte of $1000 address

    lda #%01011010
    sta $2007
@SkipTileUpdate:
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Updating CHR RAM during NMI causes a glitch

Post by tokumaru »

I'm not sure why you're using a value from VRAM to calculate the address of the pattern to change, but VRAM reads are buffered, so the first value you get must be discarded, as only values from the second read onward will be valid. Try adding another LDA $2007 immediately after the one you have and see if that changes anything.
TryCatchFinally
Posts: 2
Joined: Thu Mar 17, 2016 1:09 am

Re: Updating CHR RAM during NMI causes a glitch

Post by TryCatchFinally »

You are a genius sir. Thank you! :D

I'm just experimenting with CHR RAM at the moment and trying to do some painting. :)
The calculation is pointless I know. I'll replace it with a lookup table but for now it works.
Post Reply