clear nametable then copy into ppu

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
Electronoob
Posts: 11
Joined: Mon Sep 29, 2014 3:28 pm

clear nametable then copy into ppu

Post by Electronoob » Fri Oct 18, 2019 10:01 am

This is what I put together in order to clear the nametable then copy over some bytes. I'm wondering what improvements could be made by someone who actually knows what they are doing.

Code: Select all

.PROC LoadBackgroundNametable
    ;first clear nametable ram
    lda _PPUSTATUS
    lda #$20
    sta _PPUADDR
    lda #$00
    sta _PPUADDR
    ldy #$00
  loadBackgroundNametableClearRAMLoopY:
    ldx #$00
  loadBackgroundNametableClearRAMLoopX:
    lda #$24
    sta _PPUDATA
    inx
    cpx #30
    bne loadBackgroundNametableClearRAMLoopX
    iny
    cpy #32
    bne loadBackgroundNametableClearRAMLoopY
    ; begin copying nametable bytes
    lda _PPUSTATUS
    lda #$20
    sta _PPUADDR
    lda #$00
    sta _PPUADDR
    ldx #$00
  loadBackgroundNametableLoop:
    lda _NAMETABLE, x
    sta _PPUDATA
    inx
    cpx #32    ; copy 32 bytes 
    bne loadBackgroundNametableLoop

    rts

.ENDPROC

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

Re: clear nametable then copy into ppu

Post by rainwarrior » Fri Oct 18, 2019 11:23 am

1. You seem to be setting rows of 30 tiles to 0 at a time? NES' background is 32 x 30, not 30 x 32. Not that it matters, since either way multiplies to 960, but conceptually this looks strange.

2. You're not setting the attribute bytes which determine which colours those tiles use. Normally when clearing the nametable you want to write a full 1024 bytes of 0 or whatever fill value you're using.

A common way to do that is have X count 256 times in an inner loop and and Y count 4 times in an outer loop (4 * 256 = 1024).

It's okay to think of it as 32 x 30 tiles + 64 extra bytes of attribute, and fine to write code that treats it that way, but it's often more expedient just to think of it as one contiguous block of memory.
Last edited by rainwarrior on Fri Oct 18, 2019 12:06 pm, edited 1 time in total.

User avatar
NOOPr
Posts: 68
Joined: Tue Feb 27, 2018 10:41 am
Location: Brazil
Contact:

Re: clear nametable then copy into ppu

Post by NOOPr » Fri Oct 18, 2019 11:27 am

A short version for clear only the name table:

Code: Select all

   lda #CLEAR_VALUE
   ldx #0
   ldy #>($3C0)
clear_page:
   sta PPUDATA
   inx
   bne clear_page
   dey
   bne clear_page
   ldy #<($3C0)
clear_frag:
   sta PPUDATA
   dey
   bne clear_frag
or to clear NT within Attribute Table:

Code: Select all

   lda #CLEAR_VALUE
   ldx #0
   ldy #4 ; 4x256
clear_all:
   sta PPUDATA
   inx
   bne clear_all
   dey
   bne clear_all
If you gonna redraw the entire name table theres no need to clear it first.

User avatar
Electronoob
Posts: 11
Joined: Mon Sep 29, 2014 3:28 pm

Re: clear nametable then copy into ppu

Post by Electronoob » Sat Oct 19, 2019 1:38 am

thank you both, that was really helpful to read through. there's something amusing to me about how much more succinct and elegant your solutions are.

i think from now on i will take the advice of treating as a 1024 byte contiguous block of memory instead of how i was handling it before.

Post Reply