; SNAIL 1.0
; ATARI DISK DRIVE TACHOMETER
; (C)1983 ANALOG COMPUTING
;
; SYNTAX: X=USR(ML,B1,B2)
; ML=addr of this routine
; B1=addr of dummy disk buffer
; B2=addr of 32-byte work buffer
;
; Program equates
;
BASIC	= $D4 	; BASIC return addr
POINTER	= $CB 	; pointer to workbuffer
TEMP	= $CD 	; temporary variable
SYSTIM	= $14 	; system jiffy timer
CONSOLE	= $D01F 	; console key status
DISKIO	= $E453 	; disk handler entry
DBUF	= $0304 	; disk buffer addr
;
	PLA  	; # arguments
	PLA  	; msb of disk buffer addr
	STA DBUF+1
	PLA  	; lsb
	STA DBUF
	PLA  	; msb of work buffer addr
	STA POINTER+1
	PLA  	; lsb
	STA POINTER
	CLD  	; clear decimal mode
	LDA #$00
	STA BASIC
	STA BASIC+1 	; clear BASIC return
	JSR DISKIO 	; bring drive up to speed!
;
; Read sector #1 32 times
;
	LDA #$1F
	STA TEMP 	; initialize index
AGAIN
	LDA #$00
	STA SYSTIM 	; reset timer
	JSR DISKIO 	; read sector
	LDA SYSTIM 	; obtain access time
	LDY TEMP 	; and index
	STA (POINTER),Y 	; save in buffer
	DEY 
	BMI SUM 	; exit if buffer full
	STY TEMP 	; otherwise update index
	LDA CONSOLE
	CMP #$06 	; START key pressed?
	BNE AGAIN 	; no; get another sector
	RTS  	; otherwise exit
;
; Add up the timer readings
;
; BASIC return addr serves double duty
; as a temporary sum buffer
;
SUM
	LDY #$1F 	; initialize index
NEXTADD
	CLC 
	LDA (POINTER),Y
	ADC BASIC
	STA BASIC
	BCC OKAY
	INC BASIC+1
OKAY
	DEY 
	BPL NEXTADD 	; keep adding
	RTS  	; give sum to BASIC
