; ***********************************************************
; 	PIC MIDI Decodeur / Driver pour 32 sorties 12V 
;	16F84  V2 amélioration du timing
;	Patrice Rabby	Décembre 2008  contact@victorseraphine.com
;
;	MIDI IN:	RA2		 
; 	B0 		RB0   
; 	B1 		RB1
; 	B2 		RB2
; 	B3 		RB3
; 	B4 		RB4
; 	B5 		RB5
; 	B6 		RA6  
; 	B7 		RB7         


	LIST R=DEC			
	INCLUDE "p16f84A.inc"

	__CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON & _WDT_OFF

;	Oscillator:	XT
;	watch dog:	OFF
;	Power Up Timer:	ON
;	Code Protect:	OFF
;	clock speed:	8 MHz


; ********************* Définitions **************

BANK_OK	EQU	0x7F	; (used to eliminate bank warning)

; ********************** Macros *********************
BANK1		MACRO				; Select Bank 1
	bsf		STATUS, RP0
	endm

BANK0		MACRO				; Select Bank 0
	bcf		STATUS, RP0
	endm

; ********************** Constantes **************
	CBLOCK 0x0C

		registre1      	            ; stocke l'état des notes de 55 ( sol# ) to 62 (ré)
        	registre2           	            ; stocke l'état des notes de 63 ( mi) to 70 (la#)
        	registre3           	            ; stocke l'état des notes de 71 ( si) to 78 (fa#)
        	registre4           	            ; stocke l'état des notes de 79 ( sol) to 80 (ré)
        	compt               	      ; compteur de boucle midi routine
        	compt1                         ; compteur de boucle midi routine
        	numnote			      ; numéro de la note reçue					
        	flags					; drapeau	
        	recv						; stocke l'octet Midi					    	
        	l
        	y
        	z
        	compteur1                     ; compteur de boucle pour la tempo
        	compteur2                     ; compteur de boucle pour la tempo
        	compteur3                     ; compteur de boucle pour la tempo
;								
	ENDC

; ****************** Début ***************************** 
reset:
	org 	0
	goto	Main
	
	org 0x040
Main:
;     Configure  OPTION register
; 	PortB résistances validées    0
; 	Interrupt on RB0 rising	       0
; 	T0CS internal		            0
;	T0SE Timer0 increment	      0
; 	PSA assigned to CLOCK0
		
	movlw	B'00001000'
	BANK1
	movwf	OPTION_REG & BANK_OK
	BANK0

	clrf	PORTA					
	BANK1
	movlw	B'00000100'			; Setup PORT A en sortie sauf  RA2 (midi in)	
	movwf	TRISA & BANK_OK
	BANK0

	clrf		PORTB
	BANK1
	movlw 	B'00000000'			; Setup PORT B 0-7 as output
	movwf	TRISB & BANK_OK
	BANK0


	clrf		registre1			; RAZ des 4 registres
	clrf		registre2
	clrf		registre3
	clrf		registre4	
	

	movf registre1,0
	movwf PORTB
	bcf PORTA, 4
	nop	
	bsf PORTA,4
	
	movf registre2,0
	movwf PORTB
	bcf PORTA, 1
	nop	
	bsf PORTA,1
	
	movf registre3,0
	movwf PORTB
	bcf PORTA, 0
	nop	
	bsf PORTA,0
	
	movf registre4,0
	movwf PORTB
	bcf PORTA,3
	nop	
	bsf PORTA,3
	
    	bsf        registre1,2			; Joue 1 note au début test is OK
    	movf registre1,0
	movwf PORTB
	bcf PORTA, 4
	nop	
	bsf PORTA,4
    	call delay
    	bcf        registre1,2			
    	movf registre1,0
	movwf PORTB
	bcf PORTA, 4
	nop	
	bsf PORTA,4
	call delay


; **************** Boucle principale **********************


test1


analyse_code1					; Test si "note on"  canal 1 (code 90)
	call 	midi_in
	btfss 	flags,4
	goto 	analyse_code1		
	movlw b'10010000'
	subwf	recv,W
	btfss	STATUS,Z
	goto 	analyse_code1_80	; Si pas 90 on regarde si  "note off" canal 1 (code 80)

analyse_code2					; Si 90 on teste le second octet qui est  le numéro de note on
	call		midi_in
	btfss 	flags,4
	goto 	analyse_code1	
	movf	recv,W				
	movwf  numnote				; on recupère le numero de note dans numnote
	
	bcf 		STATUS,C			; on envoie vers la table des notes on
	rlf	numnote,F				; Multiplie par 2 pour la table
	movlw 02
	movwf	PCLATH
	goto noteon					;call noteon
	goto test1


analyse_code1_80				; Test si "note off" canal1 (code 80)
	movlw b'10000000'
	subwf	recv,W
	btfss	STATUS,Z
	goto 	analyse_code1

analyse_code20					; Si 80 on teste le second octet qui est  le numéro de note off
	call		midi_in
	btfss 	flags,4
	goto 	analyse_code1
	movf	recv,W
	movwf  numnote				; on recupère le numero de note dans numnote

	bcf 		STATUS,C			; on envoie vers la table des notes off
	rlf	numnote,F				; Multiplie par 2 pour la table
	movlw	03
	movwf	PCLATH
	goto noteoff					;call noteoff
	goto test1

;.************************* routine MIDI *********************
midi_in
	


	btfsc	PORTA,2		;
	goto	midi_in
		
 	movlw	7 				; attendre 16 ùs pour le milieu du start bit (48 cycles)
	movwf	z


min0	
	nop						; 4 x 11= 44 cycles
	decfsz	z,F
	goto 	min0			

	movlw	8				; 46
	movwf	l				; 47
	clrf		recv				; 48 cycles ici 


min1						; attentre 32 ùs pour le milieu du MSB (96 cycles)
	movlw	14 
	movwf	z

mdy0
	nop						;4 x 18 = 72					; 
	decfsz	z,F
	goto	mdy0
	bcf 		STATUS,C
	rrf		recv,F
	btfsc	PORTA,2
	bsf		recv,7			; +23
	decfsz	l,F
	goto 	min1
	bsf 		flags,4
	movlw	18
	movwf	z

mdy1
	decfsz	z,F
	goto 	mdy1
	btfss	PORTA,2
	bcf		flags,4
	return

;****************** Validation des 74LS574 ***************
VALID5741
	movf registre1,0
	movwf PORTB
	bcf PORTA, 4
	nop	
	bsf PORTA,4
	goto test1

VALID5742
	movf registre2,0
	movwf PORTB
	bcf PORTA, 1
	nop	
	bsf PORTA,1
	goto test1

VALID5743
	movf registre3,0
	movwf PORTB
	bcf PORTA, 0
	nop	
	bsf PORTA,0
	goto test1
	
VALID5744
	movf registre4,0
	movwf PORTB
	bcf PORTA,3
	nop	
	bsf PORTA,3
	goto test1


;***************** Tempo *****************
delay
	movlw 0x10
       movwf compteur1
boucle1
       movlw 0x10
       movlw compteur2
boucle2
       movlw 0x10
       movwf compteur3
boucle3
       decfsz compteur3, f
       goto boucle3
       decfsz compteur2, f
       goto boucle2
       decfsz compteur1, f
       goto boucle1
    	return

;****************** Table des notes ON ***************
	org 0x200
noteon
	movf	numnote, w
	addwf	PCL, f
	nop
	nop	
	goto test1				;1
	nop
	goto test1				;2
	nop
	goto test1				;3
	nop
	goto test1				;4
	nop
	goto test1				;5
	nop
	goto test1				;6
	nop
	goto test1				;7
	nop
	goto test1				;8
	nop
	goto test1				;9
	nop
	goto test1				;10
	nop
	goto test1				;11
	nop
	goto test1				;12
	nop
	goto test1				;13
	nop
	goto test1				;14
	nop
	goto test1				;15
	nop
	goto test1				;16
	nop
	goto test1				;17
	nop
	goto test1				;18
	nop
	goto test1				;19
	nop
	goto test1				;20
	nop
	goto test1				;21
	nop
	goto test1				;22
	nop
	goto test1				;23
	nop
	goto test1				;24
	nop
	goto test1				;25
	nop
	goto test1				;26
	nop
	goto test1				;27
	nop
	goto test1				;28
	nop
	goto test1				;29
	nop
	goto test1				;30
	nop
	goto test1				;31
	nop
	goto test1				;32
	nop
	goto test1				;33
	nop
	goto test1				;34
	nop
	goto test1				;35
	nop
	goto test1				;36
	nop
	goto test1				;37
	nop
	goto test1				;38
	nop
	goto test1				;39
	nop
	goto test1				;40
	nop
	goto test1				;41
	nop
	goto test1				;42
	nop
	goto test1				;43
	nop
	goto test1				;44
	nop
	goto test1				;45
	nop
	goto test1				;46
	nop
	goto test1				;47
	nop
	goto test1				;48
	nop
	goto test1				;49
	nop
	goto test1				;50
	nop
	goto test1				;51
	nop
	goto test1				;52
	nop
	goto test1				;53
	nop
	goto test1				;54
	nop
	bsf		registre1,7		; 55 note 1
	goto	VALID5741
	bsf		registre1,6		; 56 note 2
	goto 	VALID5741
	bsf		registre1,5		; 57 note 3
	goto 	VALID5741
	bsf		registre1,4		; 58 note 4
	goto	VALID5741
	bsf		registre1,3		; 59 note 5
	goto	VALID5741
	bsf		registre1,2		; 60 note 6
	goto 	VALID5741
	bsf		registre1,1		; 61 note 7
	goto	VALID5741
	bsf		registre1,0		; 62 note 8
	goto	VALID5741
	bsf		registre2,7		; 63 note 9
	goto 	VALID5742
	bsf		registre2,6		; 64 note 10
	goto	VALID5742
	bsf		registre2,5		; 65 note 11
	goto 	VALID5742
	bsf		registre2,4		; 66 note 12
	goto 	VALID5742
	bsf		registre2,3		; 67 note 13
	goto 	VALID5742
	bsf		registre2,2		; 68 note 14
	goto 	VALID5742
	bsf		registre2,1		; 69 note 15
	goto 	VALID5742
	bsf		registre2,0		; 70 note 16
	goto 	VALID5742
      bsf		registre3,7		; 71 note 17
	goto 	VALID5743
	bsf		registre3,6		; 72 note 18
	goto 	VALID5743
	bsf		registre3,5		; 73 note 19
	goto 	VALID5743
	bsf		registre3,4		; 74 note 20
	goto	VALID5743
	bsf		registre3,3		; 75 note 21
	goto	VALID5743
	bsf		registre3,2		; 76 note 22
	goto	VALID5743
	bsf		registre3,1		; 77 note 23
	goto 	VALID5743
	bsf		registre3,0		; 78 note 24
	goto 	VALID5743
	bsf		registre4,7		; 79 note 25
	goto 	VALID5744
	bsf		registre4,6		; 80 note 26
	goto	VALID5744
	bsf		registre4,5		; 81 note 27
	goto	VALID5744
	bsf		registre4,4		; 82 note 28
	goto	VALID5744
	bsf		registre4,3		; 83 note 29
	goto 	VALID5744
	bsf		registre4,2		; 84 note 30
	goto 	VALID5744
	bsf		registre4,1		; 85 note 31
	goto 	VALID5744
	bsf		registre4,0		; 86 note 32
	goto 	VALID5744
	goto test1				; 87
	nop
	goto test1				; 88
	nop
	goto test1				; 89
	nop
	goto test1				; 90
	nop
	goto test1				; 91
	nop
	goto test1				; 92
	nop
	goto test1				; 93
	nop
	goto test1				; 94
	nop
	goto test1				; 95
	nop
	goto test1				; 96
	nop
	goto test1				; 97
	nop
	goto test1				; 98
	nop
	goto test1				; 99
	nop
	goto test1				; 100
	nop
	
;****************** Table des notes OFF ***************
	org 0x0300 
noteoff
	movf	numnote, w
	addwf	PCL, f
	nop
	nop	
	goto test1				; 1
	nop
	goto test1				; 2
	nop
	goto test1				; 3
	nop
	goto test1				; 4
	nop
	goto test1				; 5
	nop
	goto test1				; 6
	nop
	goto test1				; 7
	nop
	goto test1				; 8
	nop
	goto test1				; 9
	nop
	goto test1				; 10
	nop
	goto test1				; 11
	nop
	goto test1				; 12
	nop
	goto test1				; 13
	nop
	goto test1				; 14
	nop
	goto test1				; 15
	nop
	goto test1				; 16
	nop
	goto test1				; 17
	nop
	goto test1				; 18
	nop
	goto test1				; 19
	nop
	goto test1				; 20
	nop
	goto test1				; 21
	nop
	goto test1				; 22
	nop
	goto test1				; 23
	nop
	goto test1				; 24
	nop
	goto test1				; 25
	nop
	goto test1				; 26
	nop
	goto test1				; 27
	nop
	goto test1				; 28
	nop
	goto test1				; 29
	nop
	goto test1				; 30
	nop
	goto test1				; 31
	nop 
	goto test1				; 32
	nop
	goto test1				; 33
	nop
	goto test1				; 34
	nop
	goto test1				; 35
	nop
	goto test1				; 36
	nop
	goto test1				; 37
	nop
	goto test1				; 38
	nop
	goto test1				; 39
	nop
	goto test1				; 40
	nop
	goto test1				; 41
	nop
	goto test1				; 42
	nop
	goto test1				; 43
	nop
	goto test1				; 44
	nop
	goto test1				; 45
	nop
	goto test1				; 46
	nop
	goto test1				; 47
	nop
	goto test1				; 48
	nop
	goto test1				; 49
	nop
	goto test1				; 50
	nop
	goto test1				; 51
	nop
	goto test1				; 52
	nop
	goto test1				; 53
	nop
	goto test1				; 54
	nop
	bcf		registre1,7		; 55 note 1
	goto 	VALID5741
	bcf		registre1,6		; 56 note 2
	goto 	VALID5741
	bcf		registre1,5		; 57 note 3
	goto 	VALID5741
	bcf		registre1,4		; 58 note 4
	goto 	VALID5741
	bcf		registre1,3		; 59 note 5
	goto 	VALID5741
	bcf		registre1,2		; 60 note 6
	goto 	VALID5741
	bcf		registre1,1		; 61 note 7
	goto	VALID5741
	bcf		registre1,0		; 62 note 8
	goto 	VALID5741
	bcf		registre2,7		; 63 note 9
	goto	VALID5742
	bcf		registre2,6		; 64 note 10
	goto 	VALID5742
	bcf		registre2,5		; 65 note 11
	goto 	VALID5742
	bcf		registre2,4		; 66 note 12
	goto 	VALID5742
	bcf		registre2,3		; 67 note 13
	goto	VALID5742
	bcf		registre2,2		; 68 note 14
	goto 	VALID5742
	bcf		registre2,1		; 69 note 15
	goto 	VALID5742
	bcf		registre2,0		; 70 note 16
	goto 	VALID5742
	bcf		registre3,7		; 71 note 17
	goto 	VALID5743
	bcf		registre3,6		; 72 note 18
	goto 	VALID5743
	bcf		registre3,5		; 73 note 19
	goto 	VALID5743
	bcf		registre3,4		; 74 note 20
	goto 	VALID5743
	bcf		registre3,3		; 75 note 21
	goto 	VALID5743
	bcf		registre3,2		; 76 note 22
	goto 	VALID5743
	bcf		registre3,1		; 77 note 23
	goto 	VALID5743
	bcf		registre3,0		; 78 note 24
	goto 	VALID5743
	bcf		registre4,7		; 79 note 25
	goto 	VALID5744
	bcf		registre4,6		; 80 note 26
	goto 	VALID5744
	bcf		registre4,5		; 81 note 27
	goto 	VALID5744
	bcf		registre4,4		; 82 note 28
	goto 	VALID5744
	bcf		registre4,3		; 83 note 29
	goto 	VALID5744
	bcf		registre4,2		; 84 note 30
	goto 	VALID5744
	bcf		registre4,1		; 85 note 31
	goto 	VALID5744
	bcf		registre4,0		; 86 note 32
	goto 	VALID5744
	goto test1				; 87
	nop
	goto test1				; 88
	nop
	goto test1				; 89
	nop
	goto test1				; 90
	nop
	goto test1				; 91
	nop
	goto test1				; 92
	nop
	goto test1				; 93
	nop
	goto test1				; 94
	nop
	goto test1				; 95
	nop
	goto test1				; 96
	nop
	goto test1				; 97
	nop
	goto test1				; 98
	nop
	goto test1				; 99
	nop
	goto test1				; 100
	nop
	END


