; Counters, refresh, interrupts, frame length and WRAM-to-WRAM DMA: the ; family spec is docs/probe-suite.md, "Timing". None of these has a console ; result pinned anywhere; the claims they test are anomie's and fullsnes's, ; with ares's model (ISC) beside them. ; Arm the H/V IRQ at (sync_v, sync_h) with sync_m, reset the counter ; flip-flops; the caller then WAIs (I = 1) so the next instruction is the ; one being timed. D = 0. .macro ARM_IRQ stz NMITIMEN lda sync_h sta HTIMEL lda sync_h+1 sta HTIMEH lda sync_v sta VTIMEL lda sync_v+1 sta VTIMEH lda STAT78 lda sync_m sta NMITIMEN lda TIMEUP .endmacro ; Latched counters as stored by the timing tests: H lo, H hi raw (bits 1-7 ; are PPU2 open bus), V lo, V hi raw; flip-flops reset before the latch. ; X = RES offset, advanced by 4. D = 0. TmStoreRaw4: .a8 lda OPHCT sta RES,x lda OPHCT sta RES+1,x lda OPVCT sta RES+2,x lda OPVCT sta RES+3,x inx inx inx inx rts ; --- HVFF: the $213C/$213D flip-flops and $213F (Q3), 20 bytes ------------------- ; ; F1 $213F, $2137, $213C x4, $213D x4 (8); F2 $213F, $2137, $213C, $213D, $213C, ; $213D (4); F3 $213F, $2137, $213C, $213F (stored), $213C (3); F4 $213F, ; $2137, $213C, $2137, $213C (2); F7 $213F, $213F, $2137, $213F, $213F (3). ; fullsnes: two independent flip-flops, both reset by a $213F read, which ; also clears the latch flag (bit 6). TestHvFlipFlops: .a8 lda STAT78 ; F1 lda SLHV ldx #$0000 : lda OPHCT sta RES,x inx cpx #4 bne :- : lda OPVCT sta RES,x inx cpx #8 bne :- lda STAT78 ; F2 lda SLHV lda OPHCT sta RES+8 lda OPVCT sta RES+9 lda OPHCT sta RES+10 lda OPVCT sta RES+11 lda STAT78 ; F3 lda SLHV lda OPHCT sta RES+12 lda STAT78 sta RES+13 lda OPHCT sta RES+14 lda STAT78 ; F4 lda SLHV lda OPHCT sta RES+15 lda SLHV lda OPHCT sta RES+16 lda STAT78 ; F7 lda STAT78 sta RES+17 lda SLHV lda STAT78 sta RES+18 lda STAT78 sta RES+19 rts ; --- WDMA: WRAM-to-WRAM DMA through $2180 (C7, U3, Q19), 88 bytes ------------- ; ; Channel 0, 8 bytes, BBAD = $80. Before each case $7E:3000-301F = $C0.., ; $7E:3100-311F = $30.., the A-to-B sources $10.., the B-to-A targets $AA. ; After the DMA one read of $2180 (the "probe") shows where WMADD was left. ; W1 A-to-B from ROM, WMADD $7E:3000 (control) $7E:3000-07, probe 9 ; W2 A-to-B from $7E:2100, WMADD $7E:3000 same 9 ; W3 A-to-B from $00:0B00 (low WRAM mirror) same 9 ; W4 A-to-B from $7F:2100 same 9 ; W5 B-to-A to $7E:2200, WMADD $7E:3100 $7E:2200-07, probe, $7E:3100-07 17 ; W6 B-to-A to $00:0B40, WMADD $7E:3100 $00:0B40-47, probe, $7E:3100-07 17 ; W7 B-to-A to battery RAM $70:7F00 (control) the 8 bytes, probe 9 ; W8 A-to-B from $7E:3000 to WMADD $7E:3000 (the same bytes) $7E:3000-07, probe 9 wd_case = $40 ; 16-bit wd_out = $42 ; 16-bit TestWramDma: .a8 ldx #$0000 stx wd_case stx wd_out @case: jsr WdFill rep #$20 .a16 lda wd_case asl a asl a asl a ; 8 bytes per case descriptor tax lda WdCases+1,x ; A address low 16 sta $4302 lda WdCases+4,x ; WMADD low 16 sta WMADDL lda #8 sta $4305 sep #$20 .a8 lda WdCases,x ; DMAP sta $4300 lda WdCases+3,x ; A bank sta $4304 lda WdCases+6,x ; WMADD bank bit sta WMADDH lda #$80 sta $4301 lda #$01 sta MDMAEN lda WMDATA ; the probe sta tmp ldx wd_out rep #$20 .a16 lda wd_case asl a tay sep #$20 .a8 lda WdReadBack,y ; where the 8 destination bytes are sta tmp+1 jsr WdCopy8 lda tmp sta RES,x inx lda WdSecond,y ; B-to-A cases: also the WMADD region beq :+ lda #3 ; $7E:3100 sta tmp+1 jsr WdCopy8 : stx wd_out rep #$20 .a16 inc wd_case lda wd_case cmp #8 sep #$20 .a8 beq :+ jmp @case : rts ; tmp+1 = region: 0 $7E:3000, 1 $7E:2200, 2 $00:0B40, 3 $7E:3100, 4 $70:7F00. ; Copies its 8 bytes to RES,x; X += 8. WdCopy8: .a8 phy ldy #$0000 @byte: phx tyx lda tmp+1 beq @r0 cmp #1 beq @r1 cmp #2 beq @r2 cmp #3 beq @r3 lda f:SRAM+$7F00,x bra @got @r0: lda f:$7E3000,x bra @got @r1: lda f:$7E2200,x bra @got @r2: lda f:$000B40,x bra @got @r3: lda f:$7E3100,x @got: plx sta RES,x inx iny cpy #8 bne @byte ply rts WdFill: .a8 ldx #$0000 : txa clc adc #$C0 sta f:$7E3000,x txa clc adc #$30 sta f:$7E3100,x inx cpx #$20 bne :- ldx #$0000 : txa clc adc #$10 sta f:$7E2100,x sta f:$000B00,x sta f:$7F2100,x lda #$AA sta f:$7E2200,x sta f:$000B40,x sta f:SRAM+$7F00,x inx cpx #8 bne :- rts .segment "RODATA" ; Per case: DMAP, A address (24), WMADD (24), pad. WdCases: .byte $00 .faraddr WdRomSource .faraddr $7E3000 .byte 0 .byte $00 .faraddr $7E2100 .faraddr $7E3000 .byte 0 .byte $00 .faraddr $000B00 .faraddr $7E3000 .byte 0 .byte $00 .faraddr $7F2100 .faraddr $7E3000 .byte 0 .byte $80 .faraddr $7E2200 .faraddr $7E3100 .byte 0 .byte $80 .faraddr $000B40 .faraddr $7E3100 .byte 0 .byte $80 .faraddr SRAM+$7F00 .faraddr $7E3100 .byte 0 .byte $00 .faraddr $7E3000 .faraddr $7E3000 .byte 0 WdReadBack: .word 0, 0, 0, 0, 1, 2, 4, 0 WdSecond: .word 0, 0, 0, 0, 1, 1, 0, 0 WdRomSource: .byte $10, $11, $12, $13, $14, $15, $16, $17 .segment "CODE" ; --- RFSH: the DRAM refresh and the short line (Q11, C2, C3, Q22), 412 bytes ---- ; ; A free-running sampler from the FastROM mirror with D = $2100: per unit ; `lda $37` (latch), `lda $3C` (H low), `pha`, `ldx $3C` (H high, dropped: the ; latch does not reset the flip-flop): 74 master clocks, 18.5 dots. 96 units ; run about 5.3 lines from an H+V IRQ at line 239 dot 10, so each run sees ; five refreshes (a 28-29 dot step instead of 18-19) and line 240, which is ; 4 clocks short on frames whose $213F bit 7 is 1 (anomie l.62-63). Runs: ; field 0 and field 1, each without and with an 18-clock prelude (`lda $00`; ; the branch around it makes the two 12 clocks apart). Per run $213F raw then ; 96 H lows in time order (4 x 97). Then 8 consecutive frames: H+V IRQ at ; 239/100, latch, 256 x `dex/bne` (7680 clocks, past line 240), latch: per ; frame $213F raw, H low at start, H low at end (24): one dot more on the ; short-line frames. RF_UNITS = 96 RF_BUF = $0C7F ; the sampler's stack, down to $0C20 rf_s = $50 ; 16-bit saved S rf_out = $52 ; 16-bit rf_want = $54 ; $00 or $80: the field wanted rf_pre = $56 ; 16-bit: 1 = prelude rf_frame = $58 ; 16-bit TestRefresh: .a8 GO_FAST ldx #$0000 stx rf_out stx rf_pre stz rf_want jsr RfRun lda #$80 sta rf_want jsr RfRun ldx #$0001 stx rf_pre stz rf_want jsr RfRun lda #$80 sta rf_want jsr RfRun jsr RfCross stz NMITIMEN lda TIMEUP GO_SLOW rts RfRun: .a8 @frame: lda #230 jsr AjWaitLine lda STAT78 and #$80 cmp rf_want beq :+ jsr WaitVblankEnd bra @frame : lda STAT78 ldx rf_out sta RES,x inx stx rf_out rep #$20 .a16 lda #10 sta sync_h lda #239 sta sync_v sep #$20 .a8 lda #$30 sta sync_m jsr SyncWai tsx stx rf_s ldx #RF_BUF txs ldy rf_pre pea $2100 pld sep #$10 .i8 cpy #0 beq :+ lda z:$00 : .repeat RF_UNITS lda z:$37 lda z:$3C pha ldx z:$3C .endrepeat rep #$10 .i16 DP0 ldx rf_s txs ldx rf_out ldy #RF_BUF : lda a:$0000,y sta RES,x inx dey cpy #RF_BUF - RF_UNITS bne :- stx rf_out stz NMITIMEN lda TIMEUP rts RfCross: .a8 ldx #$0000 stx rf_frame @frame: rep #$20 .a16 lda #100 sta sync_h lda #239 sta sync_v sep #$20 .a8 lda #$30 sta sync_m jsr SyncWai lda a:SLHV ldx rf_out lda STAT78 sta RES,x lda OPHCT sta RES+1,x lda OPHCT rep #$10 ldy #$0100 : dey bne :- lda a:SLHV lda STAT78 lda OPHCT sta RES+2,x lda OPHCT inx inx inx stx rf_out lda TIMEUP stz NMITIMEN jsr WaitVblankEnd rep #$20 .a16 inc rf_frame lda rf_frame cmp #8 sep #$20 .a8 bne @frame rts ; --- IRQN: where the H/V IRQ and the NMI land (Q12, Q13), 202 bytes ------------- ; ; 7a: 31 HTIMEs (IrqTimes), H+V IRQ at line 100 then 101, WAI with I = 1, ; then `lda $2137` (abs). Per HTIME: line 100 H lo, H hi raw, V lo; line ; 101 H lo, H hi raw (155). HTIME 340 and 511 are polled for two frames ; instead ($FF bytes if the flag never rises). ; 7b: 6 HTIMEs at line 100 through the IRQ vector (IrqTrampoline + a bank $00 ; handler whose first instruction latches): H lo, H hi raw, V lo (18). ; 7c: the dot-153 rule: IRQs counted over 4 frames at (V, H) = (240, 153), ; (240, 152), (240, 154), (261, 153), (261, 152), (100, 153) (6). anomie: ; 2, 4, 4, 0, 4, 4; ares irqPoll(): all 4. ; 7d: NMI: from WAI (3); then from a 128-NOP sled after an H+V IRQ at 224/250 ; (the stacked PC low minus the sled's, H lo, H hi raw, V lo: 4); then the ; same at 224/300 with a ROM-to-$2180 DMA of 16, 32, 64, 128 bytes started ; just before the sled (16). NMI_PCL = $1E40 in_i = $50 ; 16-bit in_out = $52 ; 16-bit in_prev = $54 in_count = $55 in_frames= $56 TestIrqLanding: .a8 GO_FAST ldx #$0000 stx in_out stx in_i @time: rep #$20 .a16 lda in_i asl a tax lda IrqTimes,x sta sync_h lda #100 sta sync_v sep #$20 .a8 lda #$30 sta sync_m lda sync_h+1 cmp #$01 bcc @wai ; HTIME < 256 lda sync_h cmp #$54 ; 340 and above: poll bcs @poll @wai: ARM_IRQ wai lda a:SLHV lda TIMEUP ldx in_out lda OPHCT sta RES,x lda OPHCT sta RES+1,x lda OPVCT sta RES+2,x inc sync_v ARM_IRQ wai lda a:SLHV lda TIMEUP ldx in_out lda OPHCT sta RES+3,x lda OPHCT sta RES+4,x bra @next @poll: ARM_IRQ jsr InPoll ldx in_out sta RES,x sta RES+1,x sta RES+2,x inc sync_v ARM_IRQ jsr InPoll ldx in_out sta RES+3,x sta RES+4,x @next: stz NMITIMEN rep #$20 .a16 lda in_out clc adc #5 sta in_out inc in_i lda in_i cmp #(IrqTimesEnd - IrqTimes) / 2 sep #$20 .a8 beq :+ jmp @time : jsr In7b jsr In7c jsr In7d stz NMITIMEN lda TIMEUP lda RDNMI GO_SLOW rts ; Two frames of polling $4211 bit 7: A = $FF if it never rose, else the ; latched H low. InPoll: .a8 ldy #$0008 @outer: ldx #$0000 @inner: lda TIMEUP bmi @fired dex bne @inner dey bne @outer lda #$FF rts @fired: lda a:SLHV lda OPHCT rts In7b: .a8 rep #$20 .a16 lda #.loword(IrqLand) sta IRQ_VEC stz in_i sep #$20 .a8 stz IRQ_VEC+2 @time: rep #$20 .a16 lda in_i asl a tax lda Irq7bTimes,x sta sync_h lda #100 sta sync_v sep #$20 .a8 lda #$30 sta sync_m ARM_IRQ cli wai sei stz NMITIMEN ldx in_out lda OPHCT sta RES,x lda OPHCT sta RES+1,x lda OPVCT sta RES+2,x inx inx inx stx in_out rep #$20 .a16 inc in_i lda in_i cmp #6 sep #$20 .a8 bne @time rts In7c: .a8 stz in_i stz in_i+1 @case: rep #$20 .a16 lda in_i asl a asl a tax lda Irq7cTimes,x sta sync_v lda Irq7cTimes+2,x sta sync_h sep #$20 .a8 lda #$30 sta sync_m ARM_IRQ jsr WaitVblankStart lda TIMEUP stz in_count stz in_frames lda #$80 sta in_prev @poll: lda TIMEUP bpl :+ inc in_count : lda HVBJOY and #$80 cmp in_prev beq @poll ; no change sta in_prev cmp #$00 beq @poll ; vblank ended inc in_frames lda in_frames cmp #4 bne @poll stz NMITIMEN ldx in_out lda in_count sta RES,x inx stx in_out inc in_i lda in_i cmp #6 bne @case rts In7d: .a8 rep #$20 .a16 lda #.loword(NmiLand) sta NMI_VEC sep #$20 .a8 stz NMI_VEC+2 ; N1: NMI from WAI jsr WaitVblankEnd lda RDNMI lda #$80 sta NMITIMEN wai stz NMITIMEN lda STAT78 ldx in_out lda OPHCT sta RES,x lda OPHCT sta RES+1,x lda OPVCT sta RES+2,x inx inx inx stx in_out ; N2: from the sled stz tmp ; no DMA rep #$20 .a16 lda #250 sta sync_h sep #$20 .a8 jsr InSled ; N3: the sled after a DMA of 16, 32, 64, 128 bytes lda #16 : sta tmp rep #$20 .a16 lda #300 sta sync_h sep #$20 .a8 jsr InSled lda tmp asl a bne :- rts ; tmp = DMA length (0: none). H+V IRQ at 224/sync_h, then NMI on and the ; sled. Stores the NMI's sled index, H lo, H hi raw, V lo. InSled: .a8 jsr WaitVblankEnd rep #$20 .a16 lda #224 sta sync_v sep #$20 .a8 lda #$30 sta sync_m lda #$FF sta NMI_PCL lda tmp beq :+ stz $4300 ; DMA ch0: ROM to $2180, WMADD $7E:4000 lda #$80 sta $4301 rep #$20 .a16 lda #.loword(WdRomSource) sta $4302 lda #$4000 sta WMADDL lda tmp and #$00FF sta $4305 sep #$20 .a8 stz $4304 stz WMADDH : jsr SyncWai lda TIMEUP stz NMITIMEN lda RDNMI lda #$80 sta NMITIMEN lda tmp beq InSledGo lda #$01 sta MDMAEN InSledGo: .repeat 128 nop .endrepeat stz NMITIMEN ldx in_out lda NMI_PCL sec sbc #