; 
;
;Offset  Bytes 	Word 	Description
;  20     22    0	Samplename for sample 1. Pad with null bytes.
;  42      2    11	Samplelength for sample 1. Stored as number of words.
;	       		Multiply by two to get real sample length in bytes.
;  44      1    12 (up)	Lower four bits are the finetune value, stored as a signed
;	       		four bit number. The upper four bits are not used, and
;	       		should be set to zero.
;	       		Value:  Finetune:
;			 0        0
;			 1       +1
;			 2       +2
;			 3       +3
;			 4       +4
;			 5       +5
;			 6       +6
;			 7       +7
;			 8       -8
;			 9       -7
;			 A       -6
;			 B       -5
;			 C       -4
;			 D       -3
;			 E       -2
;			 F       -1
; 	
;  45      1    12 (lw)	Volume for sample 1. Range is $00-$40, or 0-64 decimal.
;  46      2    13	Repeat point for sample 1. Stored as number of words offset
;	       		from start of sample. Multiply by two to get offset in bytes.
;  48      2    14	Repeat Length for sample 1. Stored as number of words in
;	       		loop. Multiply by two to get replen in bytes.
	       
;******************************
;*
;* sampleGetLength
;*
;* r4 = sample num (0..30 are valid)
;* r5 = mod adr low
;* r6 = mod adr high
;*
;* return
;*
;* r0 = length in BYTES!!!!
;*

sampleGetLength

	push	r4
	push	r5
	push	r6
	
	addi	r5,21		;sample struct begin is 20 bytes in mod + 1 for sample length
	addtqi	r6,r6,0
	muli	r4,15		;sample struct in mod is 30 bytes long
	esadr	r6,r5
	erqld	r4
	eld	r0		;get value
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	jmpi	r7,0
	add	r0,r0,r0	;sample length from word length to byte length	delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

;******************************
;*
;* sampleGetFineTune
;*
;* r4 = sample num (0..30 are valid)
;* r5 = mod adr low
;* r6 = mod adr high
;*
;* return
;*
;* r0 = value
;*

sampleGetFineTune

	push	r4
	push	r5
	push	r6
	
	addi	r5,22		;sample struct begin is 20 bytes in mod + 2 for sample finetune
	addtqi	r6,r6,0
	muli	r4,15		;sample struct in mod is 30 bytes long
	esadr	r6,r5
	erqld	r4
	eld	r0		;get value
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	jmpi	r7,0
	lsri	r0,8	;get finetune	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* sampleGetVolume
;*
;* r4 = sample num (0..30 are valid)
;* r5 = mod adr low
;* r6 = mod adr high
;*
;* return
;*
;* r0 = value
;*

sampleGetVolume

	push	r4
	push	r5
	push	r6
	
	addi	r5,22		;sample struct begin is 20 bytes in mod + 2 for sample volume
	addtqi	r6,r6,0
	muli	r4,15		;sample struct in mod is 30 bytes long
	esadr	r6,r5
	erqld	r4
	eld	r0		;get value
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	jmpi	r7,0
	moveih	r0,0	;get volume	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* sampleGetRepeatStart
;*
;* r4 = sample num (0..30 are valid)
;* r5 = mod adr low
;* r6 = mod adr high
;*
;* return
;*
;* r0 = value
;*

sampleGetRepeatStart

	push	r4
	push	r5
	push	r6
	
	addi	r5,23		;sample struct begin is 20 bytes in mod + 3 for sample repeat start
	addtqi	r6,r6,0
	muli	r4,15		;sample struct in mod is 30 bytes long
	esadr	r6,r5
	erqld	r4
	eld	r0		;get value
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	jmpi	r7,0
	add	r0,r0,r0	;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
;******************************
;*
;* sampleGetRepeatLength
;*
;* r4 = sample num (0..30 are valid)
;* r5 = mod adr low
;* r6 = mod adr high
;*
;* return
;*
;* r0 = length in BYTES!!!!
;*

sampleGetRepeatLength

	push	r4
	push	r5
	push	r6
	
	addi	r5,24		;sample struct begin is 20 bytes in mod + 4 for sample repeat length
	addtqi	r6,r6,0
	muli	r4,15		;sample struct in mod is 30 bytes long
	esadr	r6,r5
	erqld	r4
	eld	r0		;get value
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	jmpi	r7,0
	add	r0,r0,r0	;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* sampleGetDataAddress (note: this function reads precalculated values from modStruct, not nice ... fixme or not)
;*
;* r4 = sample num (0..30 are valid) 
;*
;* return
;*
;* r0 = sample low address
;* r1 = sample high address
;*

sampleGetDataAddress

	push	r6
	push	r4
	
	dexti	>modStruct
	movei	r6,modStruct
	add	r4,r4,r4		;r4 * 2
	addi	r6,modSample0AdrLow	;goto start point
	nop
	add	r6,r6,r4
	nop
	rqldi	r6,0			;low
	rqldi	r6,1			;high
	nop
	ld	r0
	ld	r1
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r4		;delay slot
	pop	r6		;delay slot
	nop			;delay slot
	nop			;delay slot
	