	include "\srcAll\v1_Header.asm"

	bl @ScrInit			;Initialize Screen
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	li r6,7100h		
InfLoop:
	bl @Cls
	li r0,0100h
	a r0,r6
	
	
	mov r6,r2
	bl @ShowHexDigit
	bl @ShowHexDigit
	
	mov r6,r0
	bl @ChibiSound
	
	li r2,2000h			;Pause
Delay:		
	dec r2
	jne Delay
	
	jmp InfLoop        	;Infinite Loop
	

ChibiSound:
	li r1,8400h			;Sound port
	;MOVB R0,*R1
	ai r0,0FF00h
	
	;Silence both channels
						; 1CCTVVVV
	li r2,0FF00h		;%11111111	;Vol=15 means mute
	MOVB R2,*R1 
	li r2,0DF00h		;%11011111	
	MOVB R2,*R1 
	
	jeq silent
	
	mov r0,r3   		;Backup coppy
	
	mov r0,r2
	inv r2				;flip bits
	andi r2,04000h		;Volume
	srl r2,4			;1CCTVVVV	(Latch - Channel Type Volume)	
	mov r2,r5			;Back up for Noise
	ori r2,0D400h		;11010100
	MOVB R2,*R1 	
	
	ci r3,08000h		;Test Top bit
	jhe ChibiSoundNoise	
	
;We're making a TONE
	
					;   1CCTLLLL	(Latch - Channel Type DataL
	li r2,0CF00h	  ;%11001111	; Low Tone
	MOVB R2,*R1 
	
	mov r3,r2
	andi r2,03F00h		;0-HHHHHH
	MOVB R2,*R1 		;High Tone
	
	li r2,0FF00h		;Mute noise
	MOVB R2,*R1 	
	B *R11			;RETURN
	
ChibiSoundNoise:
	li r2,0CF00h	 	;%11001111	; Low Tone
	MOVB R2,*R1 
	
	mov r3,r2			;Set Pitch of noise (Chn2)
	andi r2,03F00h		;0-HHHHHH
	srl r2,2			;Noize frequency is a bit limited
	MOVB R2,*R1 		;High Tone
				
	li r2,0DF00h		;1CCTVVVV	(Latch - Channel Type Volume)	
	MOVB R2,*R1 		;11011111
	
	li r2,0E700h		;1CCT-MRR	(Latch - Channel Type... noise Mode (1=white) Rate (Rate 11= use Tone Channel 2)
	MOVB R2,*R1 		;11100111

	ori r5,0F000h		;1CCTVVVV	(Latch - Channel Type Volume)	
	MOVB R5,*R1			;11110000
	B *R11			;RETURN

silent:	;Mute both channels
	B *R11			;RETURN

	
	
	
;* Let's play a note 
       LI   R0,8400h;     Sound port address on the TI-99/4A 
       LI   R1,8E0Fh;     Middle A on generator 1 
       MOVB R1,*R0 
       SWPB R1 
       MOVB R1,*R0 
       LI   R2,9200h;     Volume: 4 dB below maximum 
       MOVB R2,*R0      ; Play it

	
;* Now let's make a noise (the note is still playing) 
 ;      LI   R1,>DF00     Make sure generator 3 is off 
  ;     MOVB R1,*R0 
   ;    LI   R1,>CC1A     C on generator 3 
    ;   MOVB R1,*R0 
     ;  SWPB R1 
     ;  MOVB R1,*R0 
     ;  LI   R1,>E300     Periodic noise, picking frequency from gen 3 
     ;  MOVB R1,*R0 
     ;  LI   R1,>F000     Volume: full blast
     ;  ...               Wait 

;* Turn all sounds off 
       ;LI   R1,>9F00     Start with generator 1 
;LP1    MOVB R1,*R0       Turn it off 
 ;      AI   R1,>2000     Next generator 
  ;     JNC  LP1          Carry set when >EF becomes >00
   ;    B    *R11
	   
	   
	  	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
	include "\SrcAll\monitor.asm"
	include "\SrcAll\functions.asm"

	