; The multiplier and divider in flight (Q5, Q6, C4, C5, U4). The family spec ; is docs/probe-suite.md, "Multiplier and divider". Each run: the ALU ; preloaded and idle; a WAI on an H-IRQ (dot 150 by default, after the line's ; DRAM refresh); then, from the FastROM mirror (or the same code with MEMSEL ; = 0: "SLOW") with D = $4200, the write that starts the operation, k CPU ; cycles, one read. k counts the cycles strictly between the write cycle and ; the read cycle; 0 and 1 cannot be reached (the next instruction fetches ; first), so k starts at 2 (`lda dp`) and 3 (`lda abs`), NOPs adding 2 each. ; FAST against SLOW tells one step per CPU cycle from one step per so many ; master clocks. ; ; Inputs make the step count s readable: $FF x $01 gives RDMPY = 2^s - 1; ; $FFFF / $01 gives RDDIV = 2^s - 1 (shift-add and restoring division, ; as in ares sfc/cpu/timing.cpp aluEdge()). MREP replicates undisbeliever's wrmpyb-in-flight.asm (zlib, ; Marcus Rowe; no console result recorded there). MD_OUT = $1E30 ; WRAM bytes a snippet reads into md_run = $40 ; 16-bit address of the run descriptor md_end = $42 ; 16-bit end of the descriptor list md_out = $44 ; 16-bit RES offset sync_h = $46 ; 16-bit sync_v = $48 ; 16-bit sync_m = $4A ; NMITIMEN for the sync md_jump = $4C ; 16-bit snippet address (JMP (abs) reads bank $00) md_cnt = $4E ; 16-bit: bytes the snippet stored, bit 7 = it latched H ; Arm the H/V IRQ (sync_h, sync_v, sync_m) and WAI with interrupts masked; it ; returns a fixed time after the IRQ point. D = 0. The caller acknowledges. SyncWai: .a8 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 sync_m sta NMITIMEN lda TIMEUP wai rts ; Run descriptor (10 bytes): flags, WRMPYA, premul B, dividend (16), Y (the ; operand the snippet writes), A (a second operand), snippet index, HTIME (16). MD_SLOW = $01 ; MEMSEL = 0 for the run MD_PREMUL = $02 ; a WRMPYA x premul multiplication first .macro RUN pflags, pmpya, ppremul, pdividend, poperand, psecond, psnip, phtime .byte pflags, pmpya, ppremul .word pdividend .byte poperand, psecond, psnip .word phtime .endmacro ; X = first descriptor, Y = end. Each snippet's bytes go to RES in order. ; FastROM mirror; leaves MEMSEL = 1. MdRunList: .a8 stx md_run sty md_end ldx #$0000 stx md_out MdRun: ldx md_run stz WRMPYA ; idle the ALU: $00 x $00, then 20 NOPs stz WRMPYB NOPS 20 lda a:1,x sta WRMPYA lda a:0,x and #MD_PREMUL beq :+ lda a:2,x sta WRMPYB NOPS 20 : lda a:3,x sta WRDIVL lda a:4,x sta WRDIVH rep #$20 .a16 lda a:8,x sta sync_h lda #50 sta sync_v lda a:7,x ; snippet index (low byte) and #$00FF asl a tay lda MdSnips,y sta md_jump sep #$20 .a8 lda #$10 ; H-IRQ every line sta sync_m lda a:0,x and #MD_SLOW eor #MD_SLOW sta MEMSEL jsr SyncWai ldx md_run lda a:5,x tay ; Y = operand (high byte dropped by sep #$10) lda a:6,x ; A = second operand DP4200 sep #$10 .i8 jmp (md_jump) MdBack: ; X = bytes stored in MD_OUT (| $80: H latched) rep #$10 .i16 DP0 stx md_cnt lda #$01 sta MEMSEL lda TIMEUP stz NMITIMEN lda md_cnt and #$7F sta tmp stz tmp+1 ldy #$0000 ldx md_out @copy: cpy tmp beq @copied lda MD_OUT,y sta RES,x inx iny bra @copy @copied: lda md_cnt bpl :+ phx jsr ReadCounters plx lda hval sta RES,x inx : stx md_out rep #$20 .a16 lda md_run clc adc #10 sta md_run cmp md_end sep #$20 .a8 beq :+ jmp MdRun : rts ; --- Snippets ------------------------------------------------------------------ ; Write wreg (Y), k cycles, read rreg into MD_OUT. .macro MD_READ wreg, k, rreg sty z:wreg .if (k) .mod 2 = 0 NOPS ((k) - 2) / 2 lda z:rreg .else NOPS ((k) - 3) / 2 lda a:$4200 + (rreg) .endif sta a:MD_OUT ldx #1 jmp MdBack .endmacro ; wrmpyb-in-flight's RunOneTest: 16-bit store of WRMPYA, WRMPYB (starts the ; multiplication), gap g, WRMPYB = Y, 4 NOPs, read RDMPY. .macro MD_REPLICA g rep #$20 .a16 lda #$FFFF sta z:$02 .if (g) .mod 2 = 0 NOPS ((g) - 2) / 2 sty z:$03 .else NOPS ((g) - 3) / 2 sty a:$4203 .endif NOPS 4 sep #$20 .a8 ldx z:$16 stx a:MD_OUT ldx z:$17 stx a:MD_OUT+1 ldx #2 jmp MdBack .endmacro ; WRDIVB = Y starts a division, gap g, WRDIVB = A again, 12 NOPs, all four. .macro MD_RESTART g sty z:$06 .if (g) .mod 2 = 0 NOPS ((g) - 2) / 2 sta z:$06 .else NOPS ((g) - 3) / 2 sta a:$4206 .endif NOPS 12 jmp MdRead4 .endmacro .a8 ; snippets: 8-bit A and X/Y, D = $4200 .i8 MdRead4: lda z:$14 sta a:MD_OUT lda z:$15 sta a:MD_OUT+1 lda z:$16 sta a:MD_OUT+2 lda z:$17 sta a:MD_OUT+3 ldx #4 jmp MdBack ; Snippet indices: 0-35 multiply k = 2..10 x (RDDIVL, RDDIVH, RDMPYL, ; RDMPYH); 36-103 divide k = 2..18 x the same; 104-111 replica g = 2..9; ; 112-127 restart g = 2..17; 128-129 refresh probes. .repeat 9, KK .repeat 4, RG .ident(.sprintf("MdS%d", KK * 4 + RG)): MD_READ $03, KK + 2, $14 + RG .endrepeat .endrepeat .repeat 17, KK .repeat 4, RG .ident(.sprintf("MdS%d", 36 + KK * 4 + RG)): MD_READ $06, KK + 2, $14 + RG .endrepeat .endrepeat .repeat 8, GG .ident(.sprintf("MdS%d", 104 + GG)): MD_REPLICA GG + 2 .endrepeat .repeat 16, GG .ident(.sprintf("MdS%d", 112 + GG)): MD_RESTART GG + 2 .endrepeat MdS128: ; refresh probe A: RDDIVL at k = 3, then latch H sty z:$06 lda a:$4214 sta a:MD_OUT lda a:SLHV ldx #$81 jmp MdBack MdS129: ; refresh probe B: RDDIVH at k = 3 sty z:$06 lda a:$4215 sta a:MD_OUT ldx #1 jmp MdBack .i16 MdSnips: .repeat 130, IDX .addr .ident(.sprintf("MdS%d", IDX)) .endrepeat ; --- Tests --------------------------------------------------------------------- ; MULF: $FF x $01 at k = 2..10, FAST then SLOW; $FFFF / $01 at k = 2..18, ; FAST then SLOW. One byte per run, registers in the order RDDIVL, RDDIVH, ; RDMPYL, RDMPYH: 36 + 36 + 68 + 68 = 208 bytes. TestMulDivSteps: .a8 GO_FAST ldx #MulfRuns ldy #MulfRunsEnd jsr MdRunList GO_SLOW rts ; DIV0: division by zero at k = 18 for dividends $0000, $000C, $1234, $FFFF ; (16 bytes), then after a $00 x $A5 multiplication (RDDIV = $00A5 in fullsnes) ; $1234 / 0 at k = 2, 5, 9, 13, 17 (20 bytes). TestDivZero: .a8 GO_FAST ldx #Div0Runs ldy #Div0RunsEnd jsr MdRunList GO_SLOW rts ; MREP: wrmpyb-in-flight: $FF x $FF started by a 16-bit store, WRMPYB written ; again g = 2..9 cycles later with B2 = $FF, $80, $40, ..., $01, $00 (10 ; rows); RDMPYL, RDMPYH each: 160 bytes. TestMulReplica: .a8 GO_FAST ldx #MrepRuns ldy #MrepRunsEnd jsr MdRunList GO_SLOW rts ; DRST: $FFFF / $01 restarted by WRDIVB = $07 g = 2..17 cycles later; all ; four result bytes 24 cycles on: 64 bytes. TestDivRestart: .a8 GO_FAST ldx #DrstRuns ldy #DrstRunsEnd jsr MdRunList GO_SLOW rts ; MRFS: does the refresh pause step the ALU? $FFFF / $01 started a fixed time ; after an H-IRQ at HTIME = 10..41 (the run engine's own path puts the write ; about 106 dots later, so the writes straddle the refresh near dot 132), ; RDDIVL read at k = 3 then H latched (run A), RDDIVH (run B): per HTIME ; RDDIVL, H low, RDDIVH = 96 bytes. (Format 1 swept HTIME 104..135, which ; put every write after the refresh.) TestMulRefresh: .a8 GO_FAST ldx #MrfsRuns ldy #MrfsRunsEnd jsr MdRunList GO_SLOW rts .segment "RODATA" MulfRuns: .repeat 2, SPD .repeat 36, IDX RUN SPD, $FF, 0, $0000, $01, 0, IDX, 150 .endrepeat .endrepeat .repeat 2, SPD .repeat 68, IDX RUN SPD, $00, 0, $FFFF, $01, 0, 36 + IDX, 150 .endrepeat .endrepeat MulfRunsEnd: Div0Runs: .repeat 4, DV .repeat 4, RG RUN 0, $00, 0, (DV = 1) * $000C + (DV = 2) * $1234 + (DV = 3) * $FFFF, $00, 0, 36 + 16 * 4 + RG, 150 .endrepeat .endrepeat .repeat 5, II .repeat 4, RG RUN MD_PREMUL, $00, $A5, $1234, $00, 0, 36 + ((II = 0) * 0 + (II = 1) * 3 + (II = 2) * 7 + (II = 3) * 11 + (II = 4) * 15) * 4 + RG, 150 .endrepeat .endrepeat Div0RunsEnd: MrepRuns: .repeat 10, BB .repeat 8, GG RUN 0, $00, 0, $0000, (BB = 0) * $FF + (BB > 0) * (($100 >> BB) & $FF), 0, 104 + GG, 150 .endrepeat .endrepeat MrepRunsEnd: DrstRuns: .repeat 16, GG RUN 0, $00, 0, $FFFF, $01, $07, 112 + GG, 150 .endrepeat DrstRunsEnd: MrfsRuns: .repeat 32, HH RUN 0, $00, 0, $FFFF, $01, 0, 128, 10 + HH RUN 0, $00, 0, $FFFF, $01, 0, 129, 10 + HH .endrepeat MrfsRunsEnd: .segment "CODE"