
	
	.align	4
	.arm
	.text

;** change Sampleoffset


;*******************************************************************************
;*
;*  r0  = 0-5   W    Sound length; units of (64-n)/256s  (0-63)
;*  r1  = 6-7   R/W  Wave Pattern Duty                   (0-3, see below)
;*  r2  = 8-10  R/W  Envelope Step-Time; units of n/64s  (1-7, 0=No Envelope)
;*  r3  = 11    R/W  Envelope Direction                  (0=Decrease, 1=Increase)
;*  r4  = 12-15 R/W  Initial Volume of envelope          (1-15, 0=No Sound)

;*  r5  = 0-2   R/W  Number of sweep shift      (n=0-7)
;*  r6  = 3     R/W  Sweep Frequency Direction  (0=Increase, 1=Decrease)
;*  r7  = 4-6   R/W  Sweep Time; units of 7.8ms (0-7, min=7.8ms, max=54.7ms)

;*  r8  = 0-10  W    Frequency; 131072/(2048-n)Hz  (0-2047)
;*  r9  = 14    R/W  Length Flag  (1=Stop output when length in NR11 expires)
;*  r10 = 15    W    Initial      (1=Restart Sound)



set_sound_chanel_1

	mov	r12,#$4000000

	orr	r11,r5,r6,lsl #3		;SOUND1CNT_L
	orr	r11,r11,r7,lsl #4	
	strh	r11,[r12,#$60]		;SOUND1CNT_L

	orr	r11,r0,r1,lsl #6		;SOUND1CNT_H
	orr	r11,r11,r2,lsl #8	
	orr	r11,r11,r3,lsl #11
	orr	r11,r11,r4,lsl #12	
	strh	r11,[r12,#$62]		;SOUND1CNT_H
	
	orr	r11,r8,r9,lsl #14	;SOUNDCNT_X
	orr	r11,r11,r10,lsl #15
	strh	r11,[r12,#$64]		;SOUNDCNT_X
		
	bx	lr
	
get_sound_chanel_1

	mov	r12,#$4000000

	ldrh	r11,[r12,#$60]		;SOUND1CNT_L

	mov	r6,r11,lsr #3
	mov	r7,r11,lsr #4		;Sweep Time

	and	r5,r11,#$3		;Number of sweep shift
	and	r6,r6,#1		;Sweep Frequency Direction 

	ldrh	r11,[r12,#$62]		;SOUND1CNT_H
		
	mov	r1,r11,lsr #6
	mov	r2,r11,lsr #8
	mov	r3,r11,lsr #11
	mov	r4,r11,lsr #12		;Initial Volume of envelope 
	
	and	r1,r1,#$3		;Wave Pattern Duty 
	and	r2,r2,#$7		;Envelope Step-Time
	and	r3,r3,#1		;Envelope Direction
	
	ldrh	r11,[r12,#$64]		;SOUNDCNT_X
	mov	r9,r11,lsr #14
	and	r9,r9,#1
	
	bx	lr	
	
;*******************************************************************************
;*
;*  r0  = 0-5   W    Sound length; units of (64-n)/256s  (0-63)
;*  r1  = 6-7   R/W  Wave Pattern Duty                   (0-3, see below)
;*  r2  = 8-10  R/W  Envelope Step-Time; units of n/64s  (1-7, 0=No Envelope)
;*  r3  = 11    R/W  Envelope Direction                  (0=Decrease, 1=Increase)
;*  r4  = 12-15 R/W  Initial Volume of envelope          (1-15, 0=No Sound)
;*
;*  r8  = 0-10  W    Frequency; 131072/(2048-n)Hz  (0-2047)
;*  r9  = 14    R/W  Length Flag  (1=Stop output when length in NR11 expires)
;*  r10 = 15    W    Initial      (1=Restart Sound)
;*

set_sound_chanel_2

	mov	r12,#$4000000

	orr	r11,r0,r1,lsl #6		;SOUND2CNT_L
	orr	r11,r11,r2,lsl #8	
	orr	r11,r11,r3,lsl #11
	orr	r11,r11,r4,lsl #12	
	strh	r11,[r12,#$68]		;SOUND2CNT_L

	orr	r11,r8,r9,lsl #14	;SOUND2CNT_H
	orr	r11,r11,r10,lsl #15
	strh	r11,[r12,#$6c]		;SOUND2CNT_H
		
	bx	lr
	
get_sound_chanel_2

	mov	r12,#$4000000

	ldrh	r11,[r12,#$68]		;SOUND2CNT_L

	mov	r1,r11,lsr #6
	mov	r2,r11,lsr #8
	mov	r3,r11,lsr #11
	mov	r4,r11,lsr #12		;Initial Volume of envelope 
	
	and	r1,r1,#$3		;Wave Pattern Duty 
	and	r2,r2,#$7		;Envelope Step-Time
	and	r3,r3,#1		;Envelope Direction
	
	ldrh	r11,[r12,#$6c]		;SOUND2CNT_H
	mov	r9,r11,lsr #14
	and	r9,r9,#1
	
	bx	lr		

;*******************************************************************************
;*
;*  r0  = 0-5   W    Sound length; units of (64-n)/256s  (0-63)
;*  r1  = 8-10  R/W  Envelope Step-Time; units of n/64s  (1-7, 0=No Envelope)
;*  r2  = 11    R/W  Envelope Direction                  (0=Decrease, 1=Increase)
;*  r3  = 12-15 R/W  Initial Volume of envelope          (1-15, 0=No Sound)
;*
;*  r4  =  0-2   R/W  Dividing Ratio of Frequencies (r)
;*  r5  =  3     R/W  Counter Step/Width (0=15 bits, 1=7 bits)
;*  r6  =  4-7   R/W  Shift Clock Frequency (s)
;*  r7  =  8-13  -    Not used
;*  r8  =  14    R/W  Length Flag  (1=Stop output when length in NR41 expires)
;*  r9  =  15    W    Initial      (1=Restart Sound)
;*

set_sound_chanel_3

	mov	r12,#$4000000

	orr	r11,r0,r1,lsl #8
	orr	r11,r11,r2,lsl #11
	orr	r11,r11,r3,lsl #12

	strh	r11,[r12,#$78]		;SOUND4CNT_L	
	
	orr	r11,r4,r5,lsl #3
	orr	r11,r11,r6,lsl #4
	orr	r11,r11,r7,lsl #8
	orr	r11,r11,r8,lsl #14
	orr	r11,r11,r9,lsl #15

	strh	r11,[r12,#$7C]		;SOUND4CNT_H

	bx	lr
		
get_sound_chanel_3

	mov	r12,#$4000000
	ldrh	r11,[r12,#$78]		;SOUND4CNT_L	
	
	mov	r1,r11,lsr #8
	mov	r2,r11,lsr #11
	mov	r3,r11,lsr #12
	and	r1,r1,#$3
	and	r2,r2,#1
	and	r3,r3,#$7
	
	ldrh	r11,[r12,#$7C]		;SOUND4CNT_H
	
	and	r4,r11,#$7
	mov	r5,r11,lsr #3
	mov	r6,r11,lsr #4
	mov	r8,r11,lsr #14
	
	and	r5,r5,#1
	and	r6,r6,#$f
	and	r8,r8,#1
	
	bx	lr		
		
;*******************************************************************************
;*
;*  r0:0  =  5     R/W  Wave RAM Bank Number (0-1, see below)
;*  r0:1  =  6     R/W  Wave RAM Dimension   (0=One bank/32 digits, 1=Two banks/64 digits)
;*  r0:2  =  7     R/W  Sound Channel 3 Off  (0=Stop, 1=Playback)
;*
;*  r1    =  0-7   W    Sound length; units of (256-n)/256s  (0-255)
;*  r2    =  13-14 R/W  Sound Volume  (0=Mute/Zero, 1=100%, 2=50%, 3=25%)
;*  r3    =  15    R/W  Force Volume  (0=Use above, 1=Force 75% regardless of above)
;*
;*  r4    =  0-10  W    Frequency; 131072/(2048-n) Hz   (0-2047)
;*  r5:0  =  14    R/W  Length Flag  (1=Stop output when length in NR31 expires)
;*  r5:1  =  15    W    Initial      (1=Restart Sound)
;*
;*  r6    =  WRAM0
;*  r7    =  WRAM1
;*  r8    =  WRAM2
;*  r9    =  WRAM3

set_sound_chanel_4

	mov	r12,#$4000000
	
	mov	r11,r0,lsl #5
	strh	r11,[r12,#$70]		;SOUND3CNT_L
	
	orr	r11,r1,r2,lsl #13
	orr	r11,r11,r3,lsl #15
	strh	r11,[r12,#$72]		;SOUND3CNT_H
	
	orr	r11,r4,r5,lsl #14
	strh	r11,[r12,#$74]		;SOUND3CNT_X

	add	r12,r12,#$90		;WRAM0
	stmia	r12,{r6-r9}

	bx	lr		
	
get_sound_chanel_4

	mov	r12,#$4000000
	
	ldrh	r11,[r12,#$70]		;SOUND3CNT_L
	mov	r0,r11, lsr #5
	and	r0,r0,#7
	
	ldrh	r11,[r12,#$72]		;SOUND3CNT_H
	
	mov	r2,r11,lsr #13
	mov	r3,r11,lsr #15
	and	r2,r2,#$3
	
	ldrh	r11,[r12,#$74]		;SOUND3CNT_X

	mov	r5,r11,lsr #14
	
	add	r12,r12,#$90		;WRAM0
	ldmia	r12,{r6-r9}

	bx	lr	
		
	.end		