smssimplenoise equ 1

;                                     ;smstranspose equ 1

;                                     

;                                     chibioctive:
chibioctive:
		;   e     f     g      a     b     c     d   
	   dc.w $0020,$0030,$0040,$0080,$00c0,$0100,$0140	;0
;       ;   41.2  43.6   49    55   61.7  65.4  73.4

;                                     	ifndef smstranspose
	ifnd smstranspose
	   dc.w $0180,$01c0,$0200,$0240,$0280,$02c0,$0300	;1
	endif
 	  ;   82.4  87.3  98    110   123    130   146
	dc.w $0340,$0380,$03c0,$03aa,$1d5e,$295a,$4161		;2
   	  ;   164   174    196   220 	246   261   293
	dc.w $562a,$6024,$717c,$80db,$8e9d,$9516,$a0a5		;3
      ;   329   349   392   440   493   523    587
	dc.w $ab17,$afc3,$b8a2,$c076,$c746,$ca8f,$d060		;4
   	  ;   659  698   783   880   987   1046  1174
	dc.w $d566,$d7de,$dc17,$e01a,$e3af,$e520,$e801		;5
	  ;   1318  1396  1567  1760  1975  2093  2349
	dc.w $eaae,$ebeb,$ee11,$f003,$f1c3,$f2b2,$f3d2		;6
	dc.w $f400,$f600,$f800,$facc,$fc0,$fe00,$ffff		;7

;d3=volume (0-255) 
;d6=channel num (0-127 unused channels will wrap around) / top bit=noise
;d2=pitch (0-65535)

chibisoundpro_set:

	btst #7,d6
	bne chibisoundpro_noise
	move.l #channelmask,a1
	clr.l d0
	move.b d6,d0
	and.b #%00000011,d0
	move.b (a1,d0),d1				;Channel mask
	bra chibisoundpro_notnoise
chibisoundpro_noise:
;                                     	ifdef smssimplenoise
	ifd smssimplenoise

		move.l #channelnoise,a1
		clr.l d0
		move.b d6,d0
		and.b #%00000011,d0

		move.b #1,(a1,d0)		;mark noise as on

		move.w d2,d0
		and.w #%1100000000000000,d0
		rol.w #2,d0
		beq noisezero
		subq.b #1,d0
noisezero:
		or.b #%11100100,d0	;whitenoise speed 0-2 (chn3)
		move.b d0,$C00011
		move.b d3,d0
		lsr.b #4,d0
		eor.b #%11111111,d0	;set volume (chn3)
		move.b d0,$C00011
		rts

	else
		move.b #%01000000,d1		;noise channel uses tone 2
	endif

chibisoundpro_notnoise:
;                                     	ifdef smssimplenoise
	ifd smssimplenoise
;                                     		push bc
	
		move.l #channelnoise,a1
		clr.l d0
		move.b d6,d0
		and.b #%00000011,d0
		cmp.b #0,(a1,d0)

		beq noisestilloff
;                                     			ld a,%11111111
	move.b #%11111111,d4	;noise was on, now off
	move.b d4,$C00011

	clr.b (a1,d0)

noisestilloff:
	
	endif

	move.b d3,d0
	beq silent

	not.w d2
	lsr.w #2,d2

	move.b d2,d0
	lsr.b #4,d0				;Low Tone
	
	or.b #%10000000,d0      ;1cctllll	(latch - channel type datal
	or.b d1,d0				;channelnum
	move.b d0,$C00011

	move.w d2,d0
	lsr.w #8,d0				;Low Tone	;0-hhhhhh
	and.b #%00111111,d0	;high tone

	move.b d0,$C00011

	move.b d3,d0
	lsr.b #4,d0	
	move.b d0,d4

	ifnd smssimplenoise
		btst #7,d6		;we're done if there is no noise
		bne noiseon

	endif

	eor.b #%10011111,d0	;set volume
	or.b d1,d0	;channelnum
	move.b d0,$C00011
	
	ifnd smssimplenoise
		rts

noiseon:
		move.l #%11011111,d0	;1cctvvvv	(latch - channel type volume)	
		move.b d0,$C00011

		move.l #%11100111,d0;1cct-mrr	(latch - channel type... noise mode (1=white) rate (rate 11= use tone channel 2)
		move.b d0,$C00011

		move.b d4,d0
		eor.b #%11111111,d0	;set volume of noise
		move.b d0,$C00011
	endif
chibisoundpro_init:
chibisoundpro_update:
	rts

silent:			;mute both channels	;1cctvvvv

	move.b #%10011111,d0

	or.b d1,d0	;channelnum
	move.b d0,$C00011
	rts

	align 4
channelmask:
	dc.b %00000000,%00100000,%01000000,%00000000

channelnoise equ chibisoundram+64

