NESpix - Tileset/Pixel Art Editor

A place where you can keep others updated about your NES-related projects through screenshots, videos or information in general.

Moderator: Moderators

User avatar
miau
Posts: 188
Joined: Mon Nov 06, 2006 9:34 am
Location: Potsdam, Germany
Contact:

Re: NESpix - Tileset/Pixel Art Editor

Post by miau »

Movax12 wrote:miau, I took a look at your source for any inspiration. I was wondering if you would be willing to explain your NMI routine's cmd priority/task queue system. What does it allow you to do? And/or, if you would be willing to explain the source with a bit more detail I would appreciate it. I have something I think is similar and would like to compare.
Some parts of the vblank code are from when I originally started NES development so it's a bit awkward, but here is how it should work:

- have a stack with addresses of functions to jsr to during vblank ("vblank commands")
- push a command with Vblank.cmd.exec
- the stack pointer does not advance automatically, the same function gets called once everytime the ppu enters vblank until it calls Vblank.cmd.end
- Vblank.cmd.replace changes the address at the top of the stack, making the supplied address the next command to be executed and discarding the current one

...except that's not quite how it works?! There's a high priority version of Vblank.cmd.exec and I don't recall why it is even needed with the stack-based approach. At this point, I'd rather rewrite the whole thing properly than spend any more time looking at it, heh.
Also, I probably should have used a queue instead of a stack, that would have been more logical and intuitive. Then again, queues can be a bit fiddly to implement and a priority system could be costly cpu-wise.

In any case, here's a usage example for the current Vblank.cmd interface:

Code: Select all

.proc letsDoSomethingOverMultipleVblanks ;can be called outside of vblank/NMI
    Vblank.cmd.exec firstVblank
    rts
firstVblank:
    ;do stuff
    Vblank.cmd.replace secondVblank
    rts
secondVblank:
    ;do other stuff
    Vblank.cmd.replace thirdVblank
    rts
thirdVblank:
    ;do other stuff
    jsr Vblank.cmd.end
    rts
.endproc
And another one:

Code: Select all

.proc letsGonnaDoSomethingOverMultipleVblanksAgain ;can be called outside of vblank/NMI
    .bss
    counter: .res 1
    .code
    lda #32
    sta counter
    Vblank.cmd.exec vblank
    rts
vblank:
    ;do stuff once per vblank for 32 vblanks
    ;...like redrawing one row of the nametable per vblank
    dec counter
    bne @doNotEndYet
        jsr Vblank.cmd.end
    @doNotEndYet:
    rts
.endproc
How are you handling Vblank/NMI tasks?
If there's anything else you'd like to know, please point me to it.
User avatar
Movax12
Posts: 541
Joined: Sun Jan 02, 2011 11:50 am

Re: NESpix - Tileset/Pixel Art Editor

Post by Movax12 »

There are some similarities to how I do it, and you have given me some things to think about.
For me, I have a list of addresses to be accessed in NMI. To try to speed things up I store these addresses at $0100 and switch the stack pointer to point to these addresses when running these commands. Every RTS executes the next routine in the list (fast!). This makes it fairly quick to access anything that wants to run in vblank. The last address in the stack always must point to returnFromChain.

The parameter whichVectorList exists because I have three lists of routines to execute. (High priority PPU update routines, call always routines (even in missed frame), and routines that can be called at the end of NMI that don't need vblank time.)

Code: Select all

privatefunc startChain, {whichVectorList a}
    
    ; save normal stack:
    tsx
    stx realStack
    ; transfer and save new temp stack:
    _ x := whichVectorList
    stx currentNMIstack
    txs
    rts ; load the CPU program counter with the first hooked vector.
endfunc

privatefunc returnFromChain
    ldx realStack
    txs
    return currentNMIstack
endfunc
User avatar
miau
Posts: 188
Joined: Mon Nov 06, 2006 9:34 am
Location: Potsdam, Germany
Contact:

Re: NESpix - Tileset/Pixel Art Editor

Post by miau »

Well, that gives me things to think about.

Having multiple lists and using actual stack addresses is neat, I think I will adapt that for other projects.
Post Reply