; Probe 02: the H/V counter latch by $2137 read against WRIO bit 7 (U5). ; ; Two ways to latch OPHCT/OPVCT: reading SLHV ($2137) while WRIO bit 7 is 1, ; or writing WRIO ($4201) bit 7 from 1 to 0. fullsnes and the emulators put ; the two latch points up to a dot apart and disagree where. This probe makes ; both accesses from the same place: an H-IRQ at a fixed dot wakes a WAI with ; interrupts disabled (no handler, so the wake-up timing repeats), and the very ; next instruction is either `lda $2137` or `stz $4201`. Both are absolute ; 4-cycle instructions whose fourth cycle is the access, both to 6-clock I/O, ; so the access lands on the same master clock either way. Any difference in ; the latched H is the latch delay difference. ; ; 16 samples each, interleaved A, B, A, B so drift hits both alike. Shown as ; two columns of latched H (9-bit, hex); the V of each is saved to battery RAM ; with them. Spread inside a column is wake-up jitter (the CPU's 6/8/12-clock ; cycles against the 4-clock dot); the gap between the columns is the answer. .include "common.inc" SAMPLES = 16 HDOT = 40 ; H-IRQ dot: early in the line, before DRAM refresh sample = $10 ; 16-bit loop index hval = $12 ; 16-bit vval = $14 ; 16-bit ; WRAM sample buffers: per sample, H then V, 16-bit each. BUF_A = $0300 BUF_B = $0380 .segment "CODE" Reset: ProbeStart jsr InitPPU lda #$0F ; display on: the counters run either way, but this is the normal state sta INIDISP lda #HDOT sta HTIMEH lda #$10 ; H-IRQ only, every line sta NMITIMEN ldx #$0000 @loop: stx sample ; A: latch by reading $2137 lda #$80 sta WRIO lda TIMEUP ; acknowledge wai lda SLHV jsr ReadCounters ldx sample rep #$20 .a16 txa asl a asl a tax lda hval sta BUF_A,x lda vval sta BUF_A+2,x sep #$20 .a8 ; B: latch by WRIO bit 7 going 1 -> 0 lda #$80 sta WRIO lda TIMEUP wai stz WRIO jsr ReadCounters lda #$80 sta WRIO ldx sample rep #$20 .a16 txa asl a asl a tax lda hval sta BUF_B,x lda vval sta BUF_B+2,x sep #$20 .a8 ldx sample inx cpx #SAMPLES bne @loop stz NMITIMEN lda TIMEUP ; Battery RAM: magic, then A's 16 (H, V) pairs, then B's. ldx #$0000 : lda Magic,x sta RESULTS,x inx cpx #8 bne :- ldx #$0000 : lda BUF_A,x sta RESULTS+8,x lda BUF_B,x sta RESULTS+8+SAMPLES*4,x inx cpx #SAMPLES*4 bne :- ldy #8 + SAMPLES*8 jsr SaveResults lda #$80 sta INIDISP PrintAt 1, TitleText PrintAt 2, SubtitleText PrintAt 4, HeadText ldx #$0000 @row: stx sample rep #$20 .a16 txa clc adc #6 ; screen row 6 + i asl a asl a asl a asl a asl a clc adc #MAP + 4 sta VMADD lda sample asl a asl a tax sep #$20 .a8 lda BUF_A+1,x jsr PutNibble lda BUF_A,x jsr PrintHex ldy #Gap jsr PrintHere lda BUF_B+1,x jsr PutNibble lda BUF_B,x jsr PrintHex ldx sample inx cpx #SAMPLES bne @row PrintAt 23, Help1Text PrintAt 24, Help2Text jmp ShowAndHalt ; Read the latched counters into hval/vval (9 bits each). Reading STAT78 first ; resets the low/high flip-flops of OPHCT and OPVCT. ReadCounters: lda STAT78 lda OPHCT sta hval lda OPHCT and #$01 sta hval+1 lda OPVCT sta vval lda OPVCT and #$01 sta vval+1 rts .segment "RODATA" Magic: .byte "SNESPRB2" TitleText: .byte "SNES PROBE PACK", 0 SubtitleText:.byte "02 H/V LATCH: 2137 VS WRIO", 0 HeadText: .byte " H BY 2137 H BY WRIO", 0 Gap: .byte " ", 0 Help1Text: .byte "PHOTOGRAPH THIS SCREEN.", 0 Help2Text: .byte "RESET, RUN AGAIN, PHOTO.", 0 ProbeTail "SNES PROBE HV LATCH "