Need help with a loop that has 16 bit counter

Programming the 6502 microprocessor and its relatives in assembly and other languages.
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Re: Need help with a loop that has 16 bit counter

Post by teamtempest »

Well, #240 may have been a little high as a starting value for the X-register.

As for the code that didn't put down the initial 'H', I didn't see any initialization of the Y-register. Was it there and just left out of what you posted?
Movax12
Posts: 16
Joined: 09 Nov 2012

Re: Need help with a loop that has 16 bit counter

Post by Movax12 »

What's wrong with #240 ?..
Oops.
Well this code was based off of just clearing the nametable with a single value..
This change should fix it:

Code: Select all

  ; replace symbol names with your names..
    ; PPU_NAMETABLEADDRESS - where you want to write
    ; p - your pointer
    ; myData - data you want to write
    ; PPU_ADDRESS ; $2006
    ; PPU_DATA ; $2007

    ; set PPU address
    lda #>PPU_NAMETABLEADDRESS
    sta PPU_ADDRESS ; $2006
    lda #<PPU_NAMETABLEADDRESS
    sta PPU_ADDRESS

    ; nametable is 32 x 30 = 960
    ; this loop is 240 x 4 = 960
    
    lda #<mydata
    sta p
    lda #>mydata
    sta p+1

    ldy #0
    ldx #240  
  
    loop:
        lda (p), y
        sta PPU_DATA
        iny 

        lda (p), y
        sta PPU_DATA
        iny

        lda (p), y
        sta PPU_DATA
        iny

        lda (p), y
        sta PPU_DATA
        iny
       
        bne skip
        inc p+1
        skip:
    dex
    bne loop
Though I am liking barrym95838's code more at this point.

This idea does work well if you are just setting the nametable to a certian value:

Code: Select all

    ; set PPU address
    lda #>PPU_NAMETABLEADDRESS
    sta PPU_ADDRESS ; $2006
    lda #<PPU_NAMETABLEADDRESS
    sta PPU_ADDRESS

    ; nametable is 32 x 30 = 960
    ; this loop is 240 x 4 = 960
    
    lda #0
    ldx #240  
  
    loop:
        sta PPU_DATA
        sta PPU_DATA
        sta PPU_DATA
        sta PPU_DATA
    dex
    bne loop
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Need help with a loop that has 16 bit counter

Post by BigEd »

Baka94 wrote:
Unfortunately [some] code didn't work. I just got a messed up mix of 0 and blank tiles (like usually when it doesn't work). I was hoping it would have worked tough.
It's not at all unusual when programming down in assembly language that code doesn't quite work. It's worth having some tactics ready to deal with this:
- a careful manual read-through, considering each instruction's effect on registers, flags, and memory.
- single-stepping using an emulator or machine code monitor.
- modifying the misbehaving code, producing output or writing to memory so as to leave some idea of what happened when.

There are surely others too!

Hope this helps
Ed
Tor
Posts: 597
Joined: 10 Apr 2011
Location: Norway/Japan

Re: Need help with a loop that has 16 bit counter

Post by Tor »

Single-stepping (on an emulator or the hardware, if the latter is possible) is actually fun when you get down to it. Look at the registers and important memory locations after each step, and soon you'll see what happens. Very rewarding when you figure it out.

-Tor
Baka94
Posts: 12
Joined: 23 Feb 2014

Re: Need help with a loop that has 16 bit counter

Post by Baka94 »

Just letting you know that I got the 16 bit counter working. The reason why it looked like it didn't, was that I had the PPU turned on when I drew the background. Anyway, thanks for the help. I'm off to learn how to use NMI and VBlank. :)
Post Reply