; Probe 03: this console's S-SMP clock against its master clock (U8). ; ; The two run from separate oscillators: the master clock (21.477 MHz NTSC) ; drives the 5A22 and PPU, a 24.576 MHz resonator drives the S-SMP and S-DSP, ; and resonators are commonly measured ~0.25 % fast (wiki/Timing). Games that ; race the two sides see this console's ratio, so an emulator needs it as a ; field, and this probe measures it. ; ; The CPU uploads a 47-byte SPC700 program through the IPL ROM. That program ; enables timer 2 (64 kHz from the S-SMP clock, divider 1), adds its 4-bit ; counter into a 24-bit total and writes the total to ports 0-2 continuously. ; The CPU reads the total at one vblank start and again 600 vblanks later ; (about 10 s). Nominal: 600 frames x 357,366 master clocks (262 lines of 1364 ; clocks, one 4-clock-short line every other frame) / 21,477,272.7 Hz x ; 64,000 Hz = 638,948 ticks = $09BFE4. scripts/probe_results.py turns the ; measured count into parts per million. ; ; The three port bytes are not written atomically, so the CPU reads them until ; two reads agree. .include "common.inc" ticks0 = $10 ; 24-bit ticks1 = $13 ; 24-bit diff = $16 ; 24-bit probe = $19 ; 3 bytes, ReadTicks scratch frames = $1C ; 16-bit FRAMES = 600 SPC_ORG = $0200 .segment "CODE" Reset: ProbeStart jsr InitPPU PrintAt 2, TitleText PrintAt 3, SubtitleText PrintAt 6, WaitText lda #$0F sta INIDISP jsr UploadSpc ldx #10 ; let the SPC700 program start and the counter settle : jsr WaitVblankStart dex bne :- jsr WaitVblankStart jsr ReadTicks lda probe sta ticks0 lda probe+1 sta ticks0+1 lda probe+2 sta ticks0+2 ldx #FRAMES : jsr WaitVblankStart dex bne :- jsr ReadTicks rep #$20 ; diff = probe - ticks0, 24-bit .a16 sec lda probe sbc ticks0 sta diff sep #$20 .a8 lda probe+2 sbc ticks0+2 sta diff+2 ; Results to battery RAM: magic, ticks (24-bit), frames (16-bit). ldx #$0000 : lda Magic,x sta RESULTS,x inx cpx #8 bne :- lda diff sta RESULTS+8 lda diff+1 sta RESULTS+9 lda diff+2 sta RESULTS+10 lda #FRAMES sta RESULTS+12 ldy #13 jsr SaveResults lda #$80 sta INIDISP PrintAt 6, TicksText lda diff+2 jsr PrintHex lda diff+1 jsr PrintHex lda diff jsr PrintHex PrintAt 8, NominalText PrintAt 11, Help1Text PrintAt 13, Help2Text PrintAt 14, Help3Text PrintAt 16, Help4Text jmp ShowAndHalt ; probe = ports 0-2, read until two reads agree. ReadTicks: lda APUIO0 sta probe lda APUIO1 sta probe+1 lda APUIO2 sta probe+2 lda APUIO0 cmp probe bne ReadTicks lda APUIO1 cmp probe+1 bne ReadTicks lda APUIO2 cmp probe+2 bne ReadTicks rts ; The IPL ROM's transfer protocol (wiki/Booting the SPC700): wait for $BBAA, ; send the destination with $CC, each byte with its index in port 0, then the ; entry point with port 1 = 0 and a port-0 value two past the last index. UploadSpc: rep #$20 .a16 : lda APUIO0 cmp #$BBAA bne :- lda #SPC_ORG sta APUIO2 sep #$20 .a8 lda #$01 sta APUIO1 lda #$CC sta APUIO0 : cmp APUIO0 bne :- ldx #$0000 @byte: lda SpcProgram,x sta APUIO1 txa ; 8-bit A: the index's low byte sta APUIO0 : cmp APUIO0 bne :- inx cpx #SpcProgramEnd - SpcProgram bne @byte rep #$20 .a16 lda #SPC_ORG sta APUIO2 sep #$20 .a8 stz APUIO1 ; 0: jump to the entry point txa clc adc #2 bne :+ inc a ; never 0 : sta APUIO0 ; no wait for the echo: the program overwrites port 0 rts .segment "RODATA" ; SPC700 machine code, assembled by hand (anomie's opcode table; spc700-python ; docs). Runs at $0200; uses direct page $00-$02 for the total. SpcProgram: .byte $8F, $01, $FC ; 0200 mov $FC,#1 timer 2 divider 1 (64 kHz) .byte $8F, $00, $00 ; 0203 mov $00,#0 .byte $8F, $00, $01 ; 0206 mov $01,#0 .byte $8F, $00, $02 ; 0209 mov $02,#0 .byte $8F, $04, $F1 ; 020C mov $F1,#$04 timer 2 on, IPL off, ports kept .byte $E4, $FF ; 020F mov a,$FF discard the first count .byte $E4, $FF ; 0211 loop: mov a,$FF ticks since last read (cleared) .byte $60 ; 0213 clrc .byte $84, $00 ; 0214 adc a,$00 .byte $C4, $00 ; 0216 mov $00,a .byte $E4, $01 ; 0218 mov a,$01 .byte $88, $00 ; 021A adc a,#0 .byte $C4, $01 ; 021C mov $01,a .byte $E4, $02 ; 021E mov a,$02 .byte $88, $00 ; 0220 adc a,#0 .byte $C4, $02 ; 0222 mov $02,a .byte $FA, $00, $F4 ; 0224 mov $F4,$00 .byte $FA, $01, $F5 ; 0227 mov $F5,$01 .byte $FA, $02, $F6 ; 022A mov $F6,$02 .byte $2F, $E2 ; 022D bra $0211 SpcProgramEnd: Magic: .byte "SNESPRB3" TitleText: .byte "SNES PROBE PACK", 0 SubtitleText:.byte "03 SMP CLOCK VS MASTER CLOCK", 0 WaitText: .byte "MEASURING, WAIT 10 SECONDS", 0 TicksText: .byte "TICKS IN 600 FRAMES $", 0 NominalText: .byte "NOMINAL ABOUT $09BFE4", 0 Help1Text: .byte "PHOTOGRAPH THIS SCREEN.", 0 Help2Text: .byte "RESET AND RUN IT 2 MORE", 0 Help3Text: .byte "TIMES, ONE PHOTO EACH.", 0 Help4Text: .byte "THE .SRM HOLDS THE LAST RUN.", 0 ProbeTail "SNES PROBE SMP CLOCK "