User Tools

Site Tools


The code below initializes the TOD 50/60 Hz pre-scaler of CIA based on the detected AC line frequency.

The idea is to use the horizontal retrace frequency (15.6-16 KHz) as base to measure the TOD tick interval. If it takes longer then a threshold the 50 Hz pre-scaler is used otherwise the 60 Hz one.


  • Independent of CPU clock so it works on SuperCPU
  • Does not hang on DC only powered machines
  • No need to switch off screen or sprites
  • Does not mess with timers or anything else

Of course timer NMIs should be avoided during calibration. If interrupts are already disabled it can be shortened by 3 bytes.

tod_cia		= $dc08			; or $dd08

tod_calibrate	ldy #255
		jsr tod_prescale	; use 50 Hz

		php			; no interrupts
		sei			; while measuring
		jsr tod_measure
		jsr tod_measure
tod_prescale	lda tod_cia+6
		asl a
		cpy #204		; threshold
		ror a
		sta tod_cia+6		; set pre-scaler

tod_measure	ldy #0
		sty tod_cia
tod_lp2		lda #64
tod_lp		cpx $d012
		beq tod_lp
		ldx $d012
		lsr a
		bne tod_lp
		lda tod_cia
		bne tod_done
		bne tod_lp2
tod_done	rts
base/tod_calibration.txt · Last modified: 2019-05-19 08:04 by soci