; The auto-joypad family: undisbeliever's 14 auto-joypad tests (snes-test-roms ; ac6ef800, src/hardware-tests/auto-joypad/, zlib licence, Marcus Rowe), redone ; as CPU measurements to battery RAM. The plan and the reasoning behind every ; offset is the family spec (docs/probe-suite.md, "Auto-joypad"). Only one of ; his tests carries a console result: clear-autojoy-after-autojoy-active.asm ; l.133-139, a 1-CHIP SFC printing `0001 0001` with B held at a 156-162 clock ; code delay (U11). The tester holds B on pad 1 for every test but AJT1. ; ; Like his ROMs, the timed code runs from the FastROM mirror (bank $80, MEMSEL ; = 1) with D = $4200, so `bit z:$12` reads HVBJOY in 18 master clocks and the ; detect loop is 36 clocks a pass; the IRQ handlers run from bank $00 SlowROM, ; entered through IrqTrampoline. Delays are built in master clocks from nop ; (12), rep #0 (18), bit a:$0000 (26, WRAM), sta f:WRAM (32), sty a:WRAM (34, ; 16-bit index), FastROM, as his are. Every wait outside a timed window has a ; timeout; byte 0 of each result counts the timeouts. AJ_FIRED = $1E20 ; IRQ handler count JOY2L = $421A aj_vt = $40 ; 16-bit: the line AjWaitV waits for aj_timeouts= $42 aj_trial = $44 ; 16-bit aj_out = $46 ; 16-bit RES offset aj_pass = $48 ; 16-bit aj_var = $4A ; 16-bit variant index (T6) aj_row = $4C ; 16-bit row index (T6) ; Detect the flag rising, wait P, clear the enable, then the polls (T2). .macro AJ_CLEAR_SNIP P .local loop loop: bit z:$12 beq loop DELAY P stz z:$00 jmp AjClPolls .endmacro ; Detect, wait P, clear, wait D, enable again, then count (T3, T4). .macro AJ_BLIP_SNIP P, D, count .local loop loop: bit z:$12 beq loop DELAY P stz z:$00 DELAY D sta z:$00 jmp count .endmacro ; An IRQ handler (bank $00 SlowROM, D = $4200, A = $01 from the count loop): ; the operative access, then the latch, the count, the acknowledge. .macro AJ_IRQ op, operand op operand bit a:SLHV inc a:AJ_FIRED bit z:$11 rti .endmacro ; --- Shared -------------------------------------------------------------------- ; Wait until the latched V equals aj_vt (D = 0). Counts a timeout after ~8 ; frames. AjWaitV: .a8 ldx #20000 @loop: lda SLHV lda STAT78 lda OPVCT sta tmp lda OPVCT and #$01 sta tmp+1 rep #$20 .a16 lda tmp cmp aj_vt sep #$20 .a8 beq @ok dex bne @loop inc aj_timeouts @ok: rts ; A = line; then AjWaitV. AjWaitLine: .a8 sta aj_vt stz aj_vt+1 bra AjWaitV AjWaitFlagSet: .a8 ldx #$FFFF : lda HVBJOY and #$01 bne @ok dex bne :- inc aj_timeouts @ok: rts AjWaitFlagClear: .a8 ldx #$FFFF : lda HVBJOY and #$01 beq @ok dex bne :- inc aj_timeouts @ok: rts ; tmp = 16 reads of JOYSER0, first bit in bit 15, no latch pulse ; (_modify-autojoy-during-autojoy.inc l.268-279). AjManual: .a8 lda #$01 sta tmp stz tmp+1 : lda JOYSER0 lsr a rol tmp rol tmp+1 bcc :- rts ; RES[X..] = tmp (16), then JOY1 (16); X += 4. AjStoreManualJoy: rep #$20 .a16 lda tmp sta RES,x lda JOY1L sta RES+2,x inx inx inx inx sep #$20 .a8 rts ; RES[X..] = the latched H (lo, hi bit 0) and V (lo); X += 3. AjStoreLatch: .a8 phx jsr ReadCounters plx lda hval sta RES,x lda hval+1 sta RES+1,x lda vval sta RES+2,x inx inx inx rts ; --- AJT1: the dots where $4212 bit 0 rises and falls -------------------------- ; ; 16 trials, one every 2 frames so the start moves across its 256-clock grid. ; Per trial 6 bytes: H lo, H hi, V lo at the rise, then at the fall, each ; latched 36 clocks after the HVBJOY read that saw the change. TestAjTiming: .a8 GO_FAST stz aj_timeouts lda #$01 sta NMITIMEN ldx #$0001 stx aj_out stz aj_trial stz aj_trial+1 @trial: lda #200 jsr AjWaitLine DP4200 lda #$01 : bit z:$12 beq :- lda a:SLHV DP0 ldx aj_out jsr AjStoreLatch stx aj_out DP4200 lda #$01 : bit z:$12 bne :- lda a:SLHV DP0 ldx aj_out jsr AjStoreLatch stx aj_out jsr WaitVblankStart inc aj_trial lda aj_trial cmp #16 bne @trial jmp AjEnd ; Leave: auto-joypad off, timeouts to RES+0, back to bank $00. AjEnd: .a8 sei stz NMITIMEN lda TIMEUP stz JOYSER0 lda aj_timeouts sta RES+0 GO_SLOW rts ; --- AJCL: clear the enable a delay after the read starts (T2, U11) ------------- ; ; Header (16 bytes from RES+1): JOY1 after a normal read, a manual read after ; a JOYSER0 pulse, JOY2, then 10 reserved. Then 2 passes of the 28 delays in ; AjClSnipsMclk: per trial 5 bytes: the first of 31 polls (30 clocks apart) ; to see the flag clear (1-31, $FF none), the manual read and JOY1 after 600 ; dex/bne (18000 clocks). TestAjClear: .a8 GO_FAST stz aj_timeouts jsr AjHeader ldx #17 stx aj_out stz aj_pass stz aj_pass+1 AjClPass: stz aj_trial stz aj_trial+1 AjClTrial: lda #200 jsr AjWaitLine lda #$01 sta NMITIMEN rep #$20 .a16 lda aj_trial asl a tax sep #$20 .a8 DP4200 lda #$01 jmp (AjClSnips,x) AjClDone: ; A = poll index ldx #600 : dex bne :- DP0 ldx aj_out sta RES,x inx jsr AjManual jsr AjStoreManualJoy stx aj_out stz NMITIMEN inc aj_trial lda aj_trial cmp #(AjClSnipsEnd - AjClSnips) / 2 bne AjClTrial inc aj_pass lda aj_pass cmp #2 bne AjClPass jmp AjEnd AjHeader: .a8 lda #$01 sta NMITIMEN jsr WaitVblankStart jsr AjWaitFlagSet jsr AjWaitFlagClear rep #$20 .a16 lda JOY1L sta RES+1 lda JOY2L sta RES+5 sep #$20 .a8 stz NMITIMEN lda #$01 sta JOYSER0 stz JOYSER0 jsr AjManual rep #$20 .a16 lda tmp sta RES+3 sep #$20 .a8 rts ; The polls after the clear: stubs for polls 1-16 sit before them and 17-31 ; after, so every beq reaches its stub. AjClHits1: .repeat 16, I .ident(.sprintf("AjClHit%d", I)): lda #I + 1 jmp AjClDone .endrepeat AjClPolls: .repeat 31, I bit z:$12 beq .ident(.sprintf("AjClHit%d", I)) .endrepeat lda #$FF jmp AjClDone .repeat 15, I .ident(.sprintf("AjClHit%d", I + 16)): lda #I + 17 jmp AjClDone .endrepeat ; --- AJBG, AJBP: blip the enable (T3, T4) -------------------------------------- ; ; AJBG: clear at once after the flag rises, enable again D clocks later ; (AjBgSnipsMclk), count the flag over 600 polls. AJBP: clear P clocks after ; the rise (AjBpSnipsMclk), enable at once, count over 200 polls. 2 passes; ; per trial 3 bytes: the count (saturating at $FF), JOY1. TestAjBlipGap: .a8 GO_FAST rep #$20 .a16 lda #AjBgSnips sta aj_var lda #(AjBgSnipsEnd - AjBgSnips) / 2 sta aj_row sep #$20 .a8 bra AjBlip TestAjBlipPre: .a8 GO_FAST rep #$20 .a16 lda #AjBpSnips sta aj_var lda #(AjBpSnipsEnd - AjBpSnips) / 2 sta aj_row sep #$20 .a8 AjBlip: stz aj_timeouts ldx #$0001 stx aj_out stz aj_pass stz aj_pass+1 AjBlPass: stz aj_trial stz aj_trial+1 AjBlTrial: lda #200 jsr AjWaitLine lda #$01 sta NMITIMEN rep #$20 .a16 lda aj_trial asl a clc adc aj_var tax lda a:$0000,x ; the snippet's address, from the table sta tmp sep #$20 .a8 ldy #$0000 DP4200 lda #$01 jmp (tmp) ; JMP (abs) reads the pointer from bank $00 AjCount600: ldx #600 bra AjCount AjCount200: ldx #200 AjCount: bit z:$12 beq :+ iny : dex bne AjCount DP0 cpy #$0100 bcc :+ ldy #$00FF : tya ldx aj_out sta RES,x jsr AjWaitFlagClear ldx aj_out rep #$20 .a16 lda JOY1L sta RES+1,x txa clc adc #3 sta aj_out sep #$20 .a8 stz NMITIMEN rep #$20 .a16 inc aj_trial lda aj_trial cmp aj_row sep #$20 .a8 bne AjBlTrial inc aj_pass lda aj_pass cmp #2 bne AjBlPass jmp AjEnd ; --- AJLT: does a blip re-latch the pad? (T5) ---------------------------------- ; ; Right after the rise: clear the enable, read JOYSER0 twice, enable again ; (blip-autojoy-latches-joypad-test.asm l.169-178). 16 trials of 4 bytes: the ; two raw JOYSER0 bytes, JOY1. TestAjLatch: .a8 GO_FAST stz aj_timeouts ldx #$0001 stx aj_out stz aj_trial stz aj_trial+1 @trial: lda #200 jsr AjWaitLine stz JOYSER0 lda #$01 sta NMITIMEN DP4200 sep #$10 .i8 : bit z:$12 beq :- stz z:$00 ldx a:JOYSER0 ldy a:JOYSER0 sta z:$00 rep #$10 .i16 DP0 phx phy jsr AjWaitFlagClear ldx aj_out pla ; Y low: the second read sta RES+1,x pla pla ; X low: the first read sta RES,x pla rep #$20 .a16 lda JOY1L sta RES+2,x txa clc adc #4 sta aj_out sep #$20 .a8 stz NMITIMEN inc aj_trial lda aj_trial cmp #16 bne @trial jmp AjEnd ; --- AJIR: an IRQ during the read writes NMITIMEN or touches JOYSER0 (T6) ------- ; ; Six variants (AjIrVariants) x the 15 (V, H) rows of AjIrRows. Per row: a ; poison frame (the latch held high through the read: JOY1 = $FFFF with B ; held, so a read that does not run is visible), then the test frame: from ; line 210, the H+V IRQ armed, 600 polls of the flag counted while it fires; ; the handler makes its access and latches H/V. Per variant 2 bytes (JOY1 ; after the first poison), then per row 9: count (16), JOY1, manual read, H lo, ; H hi (bit 7 set if the IRQ did not fire), V lo. TestAjIrq: .a8 GO_FAST stz aj_timeouts ldx #$0001 stx aj_out stz aj_var stz aj_var+1 @variant: stz aj_row stz aj_row+1 @row: ; poison frame lda #200 jsr AjWaitLine lda #$01 sta JOYSER0 sta NMITIMEN jsr AjWaitFlagSet jsr AjWaitFlagClear stz JOYSER0 stz NMITIMEN lda aj_row bne :+ rep #$20 .a16 lda JOY1L ldx aj_out sta RES,x inx inx stx aj_out sep #$20 .a8 : ; test frame lda #210 jsr AjWaitLine rep #$20 .a16 lda aj_var asl a asl a tax ; X = variant * 4 lda AjIrVariants,x sta IRQ_VEC lda aj_row asl a asl a tay ; Y = row * 4 lda AjIrRows,y sta VTIMEL lda AjIrRows+2,y sta HTIMEL sep #$20 .a8 lda #^AjIrqBlip sta IRQ_VEC+2 lda AjIrVariants+2,x sta JOYSER0 lda AjIrVariants+3,x ora #$30 sta NMITIMEN lda TIMEUP stz AJ_FIRED ldx #600 ldy #$0000 lda #$01 DP4200 cli @poll: bit z:$12 beq :+ iny : dex bne @poll sei stz z:$00 lda z:$11 DP0 ldx aj_out rep #$20 .a16 tya sta RES,x lda JOY1L sta RES+2,x sep #$20 .a8 jsr AjManual ldx aj_out rep #$20 .a16 lda tmp sta RES+4,x sep #$20 .a8 stz JOYSER0 rep #$20 ; the offset passes 255: 16-bit arithmetic .a16 txa clc adc #6 tax sep #$20 .a8 jsr AjStoreLatch lda AJ_FIRED bne :+ lda RES-2,x ; H hi: the IRQ never fired ora #$80 sta RES-2,x : stx aj_out rep #$20 .a16 inc aj_row lda aj_row cmp #(AjIrRowsEnd - AjIrRows) / 4 sep #$20 .a8 beq :+ jmp @row : rep #$20 .a16 inc aj_var lda aj_var cmp #(AjIrVariantsEnd - AjIrVariants) / 4 sep #$20 .a8 beq :+ jmp @variant : jmp AjEnd ; The handlers (bank $00 SlowROM, via IrqTrampoline). AjIrqBlip: stz z:$00 sta z:$00 bit a:SLHV inc a:AJ_FIRED bit z:$11 rti AjIrqClear: AJ_IRQ stz, z:$00 AjIrqEnable: AJ_IRQ sta, z:$00 AjIrqLatchClr: AJ_IRQ stz, a:JOYSER0 AjIrqLatchSet: AJ_IRQ sta, a:JOYSER0 AjIrqRead: AJ_IRQ bit, a:JOYSER0 .segment "RODATA" ; Per variant: handler (16, bank $00), JOYSER0 before the frame, auto-joypad ; enabled before the IRQ. AjIrVariants: .word AjIrqBlip .byte 0, 1 .word AjIrqClear .byte 0, 1 .word AjIrqEnable .byte 0, 0 .word AjIrqLatchClr .byte 1, 1 .word AjIrqLatchSet .byte 0, 1 .word AjIrqRead .byte 0, 1 AjIrVariantsEnd: ; (V, H) of the IRQ: first and last are controls (the IRQ must not fire ; during the polls), the rest straddle the read's start and middle. AjIrRows: .word 0, 0 .word 224, 200 .word 224, 308 .word 225, 0 .word 225, 16 .word 225, 32 .word 225, 48 .word 225, 64 .word 225, 80 .word 225, 96 .word 225, 192 .word 226, 20 .word 227, 20 .word 228, 20 .word 229, 0 AjIrRowsEnd: .segment "CODE" ; Generated delay snippets (see the family notes above). Clear sweep: AjCl0: AJ_CLEAR_SNIP 0 AjCl1: AJ_CLEAR_SNIP 64 AjCl2: AJ_CLEAR_SNIP 128 AjCl3: AJ_CLEAR_SNIP 192 AjCl4: AJ_CLEAR_SNIP 256 AjCl5: AJ_CLEAR_SNIP 384 AjCl6: AJ_CLEAR_SNIP 512 AjCl7: AJ_CLEAR_SNIP 140 AjCl8: AJ_CLEAR_SNIP 142 AjCl9: AJ_CLEAR_SNIP 144 AjCl10: AJ_CLEAR_SNIP 146 AjCl11: AJ_CLEAR_SNIP 148 AjCl12: AJ_CLEAR_SNIP 150 AjCl13: AJ_CLEAR_SNIP 152 AjCl14: AJ_CLEAR_SNIP 154 AjCl15: AJ_CLEAR_SNIP 156 AjCl16: AJ_CLEAR_SNIP 158 AjCl17: AJ_CLEAR_SNIP 160 AjCl18: AJ_CLEAR_SNIP 162 AjCl19: AJ_CLEAR_SNIP 164 AjCl20: AJ_CLEAR_SNIP 166 AjCl21: AJ_CLEAR_SNIP 168 AjCl22: AJ_CLEAR_SNIP 170 AjCl23: AJ_CLEAR_SNIP 172 AjCl24: AJ_CLEAR_SNIP 174 AjCl25: AJ_CLEAR_SNIP 176 AjCl26: AJ_CLEAR_SNIP 178 AjCl27: AJ_CLEAR_SNIP 180 ; Blip gap sweep (pre-delay 0): AjBg0: AJ_BLIP_SNIP 0, 0, AjCount600 AjBg1: AJ_BLIP_SNIP 0, 32, AjCount600 AjBg2: AJ_BLIP_SNIP 0, 64, AjCount600 AjBg3: AJ_BLIP_SNIP 0, 96, AjCount600 AjBg4: AJ_BLIP_SNIP 0, 128, AjCount600 AjBg5: AJ_BLIP_SNIP 0, 160, AjCount600 AjBg6: AJ_BLIP_SNIP 0, 192, AjCount600 AjBg7: AJ_BLIP_SNIP 0, 196, AjCount600 AjBg8: AJ_BLIP_SNIP 0, 200, AjCount600 AjBg9: AJ_BLIP_SNIP 0, 202, AjCount600 AjBg10: AJ_BLIP_SNIP 0, 204, AjCount600 AjBg11: AJ_BLIP_SNIP 0, 206, AjCount600 AjBg12: AJ_BLIP_SNIP 0, 208, AjCount600 AjBg13: AJ_BLIP_SNIP 0, 210, AjCount600 AjBg14: AJ_BLIP_SNIP 0, 214, AjCount600 AjBg15: AJ_BLIP_SNIP 0, 220, AjCount600 AjBg16: AJ_BLIP_SNIP 0, 230, AjCount600 AjBg17: AJ_BLIP_SNIP 0, 256, AjCount600 ; Blip pre-delay sweep (gap 0): AjBp0: AJ_BLIP_SNIP 0, 0, AjCount200 AjBp1: AJ_BLIP_SNIP 120, 0, AjCount200 AjBp2: AJ_BLIP_SNIP 126, 0, AjCount200 AjBp3: AJ_BLIP_SNIP 132, 0, AjCount200 AjBp4: AJ_BLIP_SNIP 138, 0, AjCount200 AjBp5: AJ_BLIP_SNIP 144, 0, AjCount200 AjBp6: AJ_BLIP_SNIP 150, 0, AjCount200 AjBp7: AJ_BLIP_SNIP 156, 0, AjCount200 AjBp8: AJ_BLIP_SNIP 162, 0, AjCount200 AjBp9: AJ_BLIP_SNIP 168, 0, AjCount200 AjBp10: AJ_BLIP_SNIP 174, 0, AjCount200 AjBp11: AJ_BLIP_SNIP 180, 0, AjCount200 AjBp12: AJ_BLIP_SNIP 186, 0, AjCount200 AjBp13: AJ_BLIP_SNIP 192, 0, AjCount200 AjBp14: AJ_BLIP_SNIP 198, 0, AjCount200 AjBp15: AJ_BLIP_SNIP 204, 0, AjCount200 AjBp16: AJ_BLIP_SNIP 210, 0, AjCount200 AjBp17: AJ_BLIP_SNIP 216, 0, AjCount200 AjBp18: AJ_BLIP_SNIP 252, 0, AjCount200 AjClSnips: .addr AjCl0 .addr AjCl1 .addr AjCl2 .addr AjCl3 .addr AjCl4 .addr AjCl5 .addr AjCl6 .addr AjCl7 .addr AjCl8 .addr AjCl9 .addr AjCl10 .addr AjCl11 .addr AjCl12 .addr AjCl13 .addr AjCl14 .addr AjCl15 .addr AjCl16 .addr AjCl17 .addr AjCl18 .addr AjCl19 .addr AjCl20 .addr AjCl21 .addr AjCl22 .addr AjCl23 .addr AjCl24 .addr AjCl25 .addr AjCl26 .addr AjCl27 AjClSnipsEnd: AjClSnipsMclk: .word 0 .word 64 .word 128 .word 192 .word 256 .word 384 .word 512 .word 140 .word 142 .word 144 .word 146 .word 148 .word 150 .word 152 .word 154 .word 156 .word 158 .word 160 .word 162 .word 164 .word 166 .word 168 .word 170 .word 172 .word 174 .word 176 .word 178 .word 180 AjBgSnips: .addr AjBg0 .addr AjBg1 .addr AjBg2 .addr AjBg3 .addr AjBg4 .addr AjBg5 .addr AjBg6 .addr AjBg7 .addr AjBg8 .addr AjBg9 .addr AjBg10 .addr AjBg11 .addr AjBg12 .addr AjBg13 .addr AjBg14 .addr AjBg15 .addr AjBg16 .addr AjBg17 AjBgSnipsEnd: AjBgSnipsMclk: .word 0 .word 32 .word 64 .word 96 .word 128 .word 160 .word 192 .word 196 .word 200 .word 202 .word 204 .word 206 .word 208 .word 210 .word 214 .word 220 .word 230 .word 256 AjBpSnips: .addr AjBp0 .addr AjBp1 .addr AjBp2 .addr AjBp3 .addr AjBp4 .addr AjBp5 .addr AjBp6 .addr AjBp7 .addr AjBp8 .addr AjBp9 .addr AjBp10 .addr AjBp11 .addr AjBp12 .addr AjBp13 .addr AjBp14 .addr AjBp15 .addr AjBp16 .addr AjBp17 .addr AjBp18 AjBpSnipsEnd: AjBpSnipsMclk: .word 0 .word 120 .word 126 .word 132 .word 138 .word 144 .word 150 .word 156 .word 162 .word 168 .word 174 .word 180 .word 186 .word 192 .word 198 .word 204 .word 210 .word 216 .word 252