;*****************************
;*** PROGRAM : ToneDial    ***
;***                       ***
;***  AUTHOR : Mike Archer ***
;***                       ***
;*** (C) Dec 1999          ***
;*****************************

		;Open the dos library
			MOVE.L	4.w,a6
			LEA		DosName,a1
			JSR		_LVOOldOpenLibrary(a6)
			MOVE.L	d0,DosBase
			
		;Check the dos library version
			MOVE.L	d0,a0
			CMP.W	#37,20(a0)
			BLO		NoDos
			
		;Call Read Args to pre-parse the command line
			MOVE.L	DosBase,a6
			MOVE.L	#Template,d1
			MOVE.L	#Array,d2
			MOVEQ.L	#0,d3
			JSR		_LVOReadArgs(a6)
			MOVE.L	d0,RdArgs
			BEQ		Error
			
		;Set up the hardware registers ready to produce sound.
			MOVE.L	#SineWave,d0
			MOVE.L	d0,$DFF0B0		;Address of AudioData
			MOVE.W	#8,$DFF0B4		;Length of AudioData (8 Words)
			MOVE.W	#0,$DFF0B6		;cycles
			MOVE.L	d0,$DFF0C0
			MOVE.W	#8,$DFF0C4
			MOVE.W	#0,$DFF0C6
			MOVE.W	#$10,$DFF09E
			MOVE.W	#$8206,$DFF096	;Enable DMA 
			
		;Get the start address of the number to dial.
			MOVE.L	Array,a2
			
Repeat:		
		;Check for NULL byte (end of number)
			TST.B	(a2)
			BEQ		Done

		;Calculate the tone to use
			MOVE.B	(a2)+,d0
			
			CMP.B	#'0',d0		;Is digit a '0' ?
			BNE		NotZero
			ADD.B	#11,d0
			BRA		CalcTone
			
NotZero:	CMP.B	#'*',d0		;Is digit a '*' ?
			BNE		NotStar
			ADD.B	#16,d0
			BRA		CalcTone
			
NotStar:	CMP.B	#'#',d0		;Is digit a '#' ?
			BNE		NotHash
			ADD.B	#25,d0
			BRA		CalcTone
			
NotHash:	CMP.B	#'0',d0		;If <'0' then not a valid digit
			BLO.S	Repeat
			
			CMP.B	#'9',d0		;Is it <='9' ?
			BLS		CalcTone
			
			AND.B	#$5f,d0		;Make uppercase
			
			CMP.B	#'A',d0		;If <'A' then not a vlaid letter
			BLO.S	Repeat

			CMP.B	#'D',d0		;IF >'D' then not a valid letter
			BHI.S	Repeat
			
		;Calculate the row + column for a letter
			SUB.B	#$41,d0
			MOVE.B	d0,d2		;d2 = Row
			MOVE.B	#3,d1		;d1 = Column
			BRA		GetFreq
			
		;Calculate the row + column for a digit or "#" or "*"
CalcTone:	SUB.B	#$31,d0
			DIVU	#3,d0
			MOVE.W	d0,d2		;d2 = Row
			SWAP	d0			;Get division remainder
			MOVE.W	d0,d1		;d1 = Column
		
		;Get the Tone Frequencies from the tables	
GetFreq:	ASL.W	#1,d1
			AND.L	#$FF,d1
			ASL.w	#1,d2
			AND.L	#$FF,d2
			LEA		Tones1,a0
			LEA		Tones2,a1
			MOVE.W	(a0,d1.w),$DFF0B6	;cycle times
			MOVE.W	(a1,d2.w),$DFF0C6
			MOVE.W	#$38,$DFF0B8		;Volumes on
			MOVE.W	#$38,$DFF0C8
			
		;Wait for the specified ToneTime (default 50 vbls)
			MOVE.L	ToneTime,d0
ToneLoop:	MOVE.L	$DFF004,d5
			AND.L	#$0001FF00,d5
			CMP.L	#$00003000,d5
			BNE.S	ToneLoop
			SUBQ.L	#1,d0
			BNE.S	ToneLoop

		;Turn off the volumes
			MOVE.W	#0,$DFF0B8
			MOVE.W	#0,$DFF0C8
			
		;Wait for the specified GapTime (Default 25 vbls)
			MOVE.L	GapTime,d0
GapLoop:	MOVE.L	$DFF004,d5
			AND.L	#$0001FF00,d5
			CMP.L	#$00003000,d5
			BNE.S	GapLoop
			SUBQ.L	#1,d0
			BNE.S	GapLoop

		;Get the next digit in the loop
			BRA		Repeat
			
Done:		
		;Free the RdArgs structure
			MOVE.L	DosBase,a6
			MOVE.L	RdArgs,d1
			JSR		_LVOFreeArgs(a6)
			
		;Close the dos library
Exit:		MOVE.L	4.w,a6
			MOVE.L	DosBase,a1
			JSR		_LVOCloseLibrary(a6)
			
		;Return to the CLI
NoDos:		CLR.L	d0
			RTS
						
Error:		MOVE.L	DosBase,a6
			MOVE.L	#TextPtr,d1
			JSR		_LVOPutStr(a6)
			
			BRA.S	Exit
				
			
				SECTION	data,data
				
DosBase:	DC.L	0
RdArgs:		DC.L	0
ToneTime:	DC.L	100
GapTime:	DC.L	50
Array:		DC.L	0
DosName:	DC.B	"dos.library",0
Template:	DC.B	"NUMBER/A/F",0

TextPtr:	DC.B	10,$9B,"32mToneDial V1.0",$9B,"33m          ©1999 by Mike Archer.",10,10
			DC.B	$9B,"31mThis program will dial the number given as a parameter",10
			DC.B	" using the standard tones for DTMF phone systems.",10,10
			DC.B    "(Valid digits are 0123456789ABCD#*)",10,10,0
			
Version:	DC.B	"$VER: ToneDial V1.0",0


Tones1:		DC.W	$B9,$A7,$97,$89,0
Tones2:		DC.W	$140,$122,$106,$ED,0

				SECTION	SoundSample,DATA_C

SineWave:	DC.B	$00,$26,$47,$5C,$64,$5C,$47,$26
			DC.B	$00,$DA,$B9,$A4,$9C,$A4,$B9,$DA
