FinalZero wrote:You still want 0 written to both of those PPU registers while the PPU is still warming up.
But I thought I was writing to them after the PPU has already warmed up.
I apologize for my mistake. I always thought those registers started with an unknown state, which isn't true. Even if they did start at something unknown, writes to them are also totally ignored for the first few frames.
What tokumaru posted is what I was describing, but
the wiki says you don't need to.
I'll probably still do it to be safe, because I am paranoid.
I don't understand yours. You push a bunch of stuff onto the stack only to pop it off in the very same routine after only incrementing nmis.
Exactly. I explained why it does that in the comments, but Tepples makes a good point. The NMI I posted currently
doesn't affect the status registers, but the idea would be that you add on to it once you're passed this hurdle.
Here's a real world example of why the pushing and popping is done.
Imagine this code is in your main routine:
Code: Select all
lda #$FF
ldx #$00
loop:
sta $0200,x
inx
inx
inx
inx
;*
bne loop
;We can continue only when X is exactly 0. I've used code like to remove all sprites from the screen (by putting their y position below the screen).
Now imagine this is your nmi routine. It interrupts at some point during the loop. Say... where that * is.
Code: Select all
nmi:
inc nmis
lda #$20;This line will cause a #$20 to be written instead of #$FF
sta $2006
ldx #$03;This line will cause an infinite loop.
sta $2006
ldy #$00
sta $2007
;
rti
When that code returns you're in an infinite loop, because the nmi changed X to something that makes completing the loop impossible.
Can (4*Z+3)%256 ever equal 0?
(Z = the number of times we have looped)
No. But (4*Z+0)%256 can be 0 which would allow your main loop to continue.
We push the registers to the stack in the NMI because it can interrupt our code at ANY time. After they're pushed, we can safely change them in the NMI, and restore them when we're done for when we return.
The inc nmis part might make sense now too. Because the opposite happens than this example. The nmi not changing that variable KEEPS us in an infinite loop when it should break us out of one.
Is that clear? If not I'll try a different approach. Tepples also did an explanation of game loops where you can avoid pushing and pulling if you like.
I know. My math routines use a stack based on the zeropage with x as the index register, as its stack pointer. That's what I meant by "stack pointer" here.
My bad. I'm a new guy in this thread, so I might have missed where that was mentioned. I'm just trying to make sure you're not doing anything you don't understand.