;******************************
;*
;* periodeSearch
;* 
;* r0 = periode
;* r1 = fine tune (0..7 -8..-1)
;*	
;* return
;*
;* r0 = periode with fine tune 
;* r2 = offset
;*
;* not optimized - simple sequentiel search
;*

periodeSearch

	push	r3
	push	r4
	
	movei	r3,periodeT0
	movei	r2,0	;counter
	moveih	r3,>periodeT0
	muli	r1,36	;fine tune  * 36

searchPLoop
	
	rqldi	r3,0
	nop
	nop
	ld	r4
	nop
	cmpeq	r4,r0	;same periode ????
	brts	searchPFound
	add	r4,r3,r1	;found adr + (finetune * 36)		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r2,35
	brtc	searchPLoop
	addi	r2,1	;delay slot
	addi	r3,1	;next				delay slot
	movei	r4,0	;safe value if not found	delay slot
	nop		;delay slot
	
searchPFound	

	rqldi	r4,0	;get periode with finetune
	nop
	nop
	ld	r0
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r4	;delay slot
	pop	r3	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* periodeSearchWithOffset (0..15)
;* 
;* r0 = periode
;* r1 = fine tune (0..7 -8..-1)
;* r5 = offset
;*	
;* return
;*
;* r1 = periode with fine tune 
;*
;* not optimized - simple sequentiel search
;*

periodeSearchWithOffset

	push	r3
	push	r4
	
	movei	r3,periodeT0
	movei	r2,0	;counter
	moveih	r3,>periodeT0
	muli	r1,36	;fine tune  * 36

searchPOffsetLoop
	
	rqldi	r3,0
	nop
	nop
	ld	r4
	nop
	cmpeq	r4,r0	;same periode ????
	brts	searchPOffsetFound
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r2,35
	brtc	searchPOffsetLoop
	addi	r2,1	;delay slot
	addi	r3,1	;next				delay slot
	movei	r4,0	;safe value if not found	delay slot
	nop		;delay slot
	
searchPOffsetFound	

	;r2 = counter
	
	add	r2,r2,r5	;counter + offset
	movei	r4,35
	cmploi	r2,36
	movets	r2,r2,r4	;clamp value = 0..35
	
	dexti	>periodeT0
	movei	r3,periodeT0
	nop
	add	r3,r3,r1	;adr + finetune offset
	nop
	add	r3,r3,r2	;adr + counter
	nop
	
	rqldi	r3,0
	nop
	nop
	ld	r1
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r4	;delay slot
	pop	r3	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;*********************************************************************
;* periodes
;*********************************************************************	
	
periodeT0	;36 values per table
	.data 	856,808,762,720,678,640,604,570,538,508,480,453 ; C-1,C#1,D-1,D#1,E-1,F-1,F#1,G-1,G#1,A-1,A#1,B-1 
	.data  	428,404,381,360,339,320,302,285,269,254,240,226 ; C-2 to B-2
	.data  	214,202,190,180,170,160,151,143,135,127,120,113 ; C-3 to B-3

periodeT1
	.data 	850,802,757,715,674,637,601,567,535,505,477,450
	.data 	425,401,379,357,337,318,300,284,268,253,239,225
	.data 	213,201,189,179,169,159,150,142,134,126,119,113
	
periodeT2
	.data 	844,796,752,709,670,632,597,563,532,502,474,447
	.data 	422,398,376,355,335,316,298,282,266,251,237,224
	.data 	211,199,188,177,167,158,149,141,133,125,118,112
	
periodeT3
	.data 	838,791,746,704,665,628,592,559,528,498,470,444
	.data 	419,395,373,352,332,314,296,280,264,249,235,222
	.data 	209,198,187,176,166,157,148,140,132,125,118,111
	
periodeT4
	.data 	832,785,741,699,660,623,588,555,524,495,467,441
	.data 	416,392,370,350,330,312,294,278,262,247,233,220
	.data 	208,196,185,175,165,156,147,139,131,124,117,110
	
periodeT5
	.data 	826,779,736,694,655,619,584,551,520,491,463,43
	.data 	413,390,368,347,328,309,292,276,260,245,232,219
	.data 	206,195,184,174,164,155,146,138,130,123,116,109
	
periodeT6
	.data 	820,774,730,689,651,614,580,547,516,487,460,434
	.data 	410,387,365,345,325,307,290,274,258,244,230,217
	.data 	205,193,183,172,163,154,145,137,129,122,115,109
	
periodeT7
	.data 	814,768,725,684,646,610,575,543,513,484,457,431
	.data 	407,384,363,342,323,305,288,272,256,242,228,216
	.data 	204,192,181,171,161,152,144,136,128,121,114,108
	
periodeTM8
	.data 	907,856,808,762,720,678,640,604,570,538,508,480
	.data 	453,428,404,381,360,339,320,302,285,269,254,240
	.data 	226,214,202,190,180,170,160,151,143,135,127,120
	
periodeTM7
	.data 	900,850,802,757,715,675,636,601,567,535,505,477
	.data 	450,425,401,379,357,337,318,300,284,268,253,238
	.data 	225,212,200,189,179,169,159,150,142,134,126,119
	
periodeTM6
	.data 	894,844,796,752,709,670,632,597,563,532,502,474
	.data 	447,422,398,376,355,335,316,298,282,266,251,237
	.data 	223,211,199,188,177,167,158,149,141,133,125,118
	
periodeTM5
	.data 	887,838,791,746,704,665,628,592,559,528,498,470
	.data 	444,419,395,373,352,332,314,296,280,264,249,235
	.data 	222,209,198,187,176,166,157,148,140,132,125,118
	
periodeTM4
	.data 	881,832,785,741,699,660,623,588,555,524,494,467
	.data 	441,416,392,370,350,330,312,294,278,262,247,233
	.data 	220,208,196,185,175,165,156,147,139,131,123,117
	
periodeTM3
	.data 	875,826,779,736,694,655,619,584,551,520,491,463
	.data 	437,413,390,368,347,328,309,292,276,260,245,232
	.data 	219,206,195,184,174,164,155,146,138,130,123,116
	
periodeTM2
	.data 	868,820,774,730,689,651,614,580,547,516,487,460
	.data 	434,410,387,365,345,325,307,290,274,258,244,230
	.data 	217,205,193,183,172,163,154,145,137,129,122,115
	
periodeTM1
	.data 	862,814,768,725,684,646,610,575,543,513,484,457
	.data 	431,407,384,363,342,323,305,288,272,256,242,228
	.data 	216,203,192,181,171,161,152,144,136,128,121,114	
	