nesdev.com
https://forums.nesdev.com/

blargg's Branch Timing Test
https://forums.nesdev.com/viewtopic.php?f=3&t=10707
Page 1 of 1

Author:  WedNESday [ Thu Nov 21, 2013 7:22 am ]
Post subject:  blargg's Branch Timing Test

In regards to 2-branch_timing.nes, all I get are 64 0's printed across the screen and an error message.

I've tested my branch code manually and its branching and timing are perfect (even the dummy read on the 4th cycle).

Can anybody (pref. blargg) explain to me how the test works?

Author:  blargg [ Thu Nov 21, 2013 12:34 pm ]
Post subject:  Re: blargg's Branch Timing Test

Any timing test must use some kind of timer, so if your branches are flawless, then it's probably the timer.

Looking at source/2-branch_timing.s:

; Verifies timing of branch instructions
;
; Runs branch instruction in loop that counts iterations
; until APU length counter expires. Moves the loop around
; in memory to trigger page cross/no cross cases.

So it uses the APU's length counter to do the timing. Searching for APU yields this:
Code:
    ; Synchronize with APU length counter
    setb SNDMODE,$40
    setb SNDCHN,$01
    setb $4000,$10
    setb $4001,$7F
    setb $4002,$FF
    setb $4003,$18
    lda #$01
:   and SNDCHN
    bne :-

setb, what's that? There's a readme.txt in the source/ directory:
Code:
Macros
------
Some macros are used to make common operations more convenient, defined
in common/macros.inc. The left is equivalent to the right:

    Macro               Equivalent
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
...
   
    setb addr,byte      lda #byte
                        sta addr

Then it does the length counter setup described above, runs the instruction, and converts the number of iterations the branch loop ran to a cycle count:
Code:
    ; Setup length counter
    setb $4003,$18
   
    delay 29830-7120
   
    ; Run instruction
    setb temp,0
    pla
    jmp (addr)
   
raw_to_cycles: ; entry i is lowest value that qualifies for i cycles
    .byte 250, 241, 233, 226, 219, 213, 206, 201, 195, 190, 0

; Jumps here when instruction has been timed
instr_done:
    ; Convert iteration count to cycle count
    lda temp
    ldy #-1
:   iny
    cmp raw_to_cycles,y
    blt :-


So your APU length counter handling might be wrong.

Author:  WedNESday [ Thu Nov 21, 2013 1:22 pm ]
Post subject:  Re: blargg's Branch Timing Test

I don't yet emulate the APU timer so that must be it, thanks.

Author:  blargg [ Thu Nov 21, 2013 4:10 pm ]
Post subject:  Re: blargg's Branch Timing Test

The length counter for a sound channel? It doesn't use the APU frame interrupt or anything like that.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/