;Photo Harp 2
;PentaTonic
;with TestMode

	LIST	P=PIC16F84
	LIST	R=DECIMAL
	include	"p16f84.inc"
	__config _CP_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC

note_vel equ 0x10
tx_reg   equ 0x11
tx_lcnt  equ 0x12
tx_wcnt  equ 0x13
out_b    equ 0x14
wcnt1    equ 0x15
wcnt2    equ 0x16
wcnt3    equ 0x17
key1_old equ 0x18
key1_new equ 0x19
key2_old equ 0x1A
key2_new equ 0x1B
key3_old equ 0x1C
key3_new equ 0x1D
key4_old equ 0x1E
key4_new equ 0x1F
key_sw   equ 0x20
key_num  equ 0x21
key_cnt  equ 0x22


	org 0
	goto	start
	retfie
	retfie
	retfie
	retfie
start
	bcf 	STATUS,RP0
	movlw	0xFF
	movwf	out_b
	movwf	PORTB

	bsf 	STATUS,RP0
	movlw	0xC1
	movwf	TRISB
	bcf 	STATUS,RP0

	btfss	PORTA,4
	goto	test_loop

	clrf	key1_old
	clrf	key1_new
	clrf	key2_old
	clrf	key2_new
	clrf	key3_old
	clrf	key3_new
	clrf	key4_old
	movlw	0x01
	movwf	key4_new

loop1
	clrf	key_num
key1
	movlw	0xEF
	movwf	out_b
	movwf	PORTB
	call	wait_on
	movfw	key1_new
	movwf	key1_old
	movfw	PORTA
	movwf	key1_new
	movlw	0xFF
	movwf	out_b
	movwf	PORTB
key1_on
	comf	key1_old,w
	andwf	key1_new,w
	movwf	key_sw
	movlw	64
	movwf	note_vel
	call	key_event
	movlw	4
	subwf	key_num
key1_off
	comf	key1_new,w
	andwf	key1_old,w
	movwf	key_sw
	movlw	0
	movwf	note_vel
	call	key_event
key2
	movlw	0xF7
	movwf	out_b
	movwf	PORTB
	call	wait_on
	movfw	key2_new
	movwf	key2_old
	movfw	PORTA
	movwf	key2_new
	movlw	0xFF
	movwf	out_b
	movwf	PORTB
key2_on
	comf	key2_old,w
	andwf	key2_new,w
	movwf	key_sw
	movlw	64
	movwf	note_vel
	call	key_event
	movlw	4
	subwf	key_num
key2_off
	comf	key2_new,w
	andwf	key2_old,w
	movwf	key_sw
	movlw	0
	movwf	note_vel
	call	key_event
key3
	movlw	0xFB
	movwf	out_b
	movwf	PORTB
	call	wait_on
	movfw	key3_new
	movwf	key3_old
	movfw	PORTA
	movwf	key3_new
	movlw	0xFF
	movwf	out_b
	movwf	PORTB
key3_on
	comf	key3_old,w
	andwf	key3_new,w
	movwf	key_sw
	movlw	64
	movwf	note_vel
	call	key_event
	movlw	4
	subwf	key_num
key3_off
	comf	key3_new,w
	andwf	key3_old,w
	movwf	key_sw
	movlw	0
	movwf	note_vel
	call	key_event
key4
	movlw	0xFD
	movwf	out_b
	movwf	PORTB
	call	wait_on
	movfw	key4_new
	movwf	key4_old
	movfw	PORTA
	movwf	key4_new
	movlw	0xFF
	movwf	out_b
	movwf	PORTB
key4_on
	comf	key4_old,w
	andwf	key4_new,w
	movwf	key_sw
	movlw	64
	movwf	note_vel
	call	key_event
	movlw	4
	subwf	key_num
key4_off
	comf	key4_new,w
	andwf	key4_old,w
	movwf	key_sw
	movlw	0
	movwf	note_vel
	call	key_event
	goto	loop1

key_event
	movlw	4
	movwf	key_cnt
key_loop
	btfsc	key_sw,3
	call	note_event
	rlf 	key_sw
	incf	key_num
	decfsz	key_cnt
	goto	key_loop
	return

note_event
	movlw	0x90
	call	Midi_Out
	movfw	key_num
	movwf	EEADR
	bsf 	STATUS,RP0
	bsf 	EECON1,RD
	bcf 	STATUS,RP0
	movfw	EEDATA
	call	Midi_Out
	movfw	note_vel
	call	Midi_Out
	return

Midi_Out
	movwf	tx_reg
	bcf 	out_b,5		;start bit
	movfw	out_b
	movwf	PORTB
	call	midi_wait
	nop
	nop
	movlw	8
	movwf	tx_lcnt
midi_loop
	btfsc	tx_reg,0
	bsf 	out_b,5
	btfss	tx_reg,0
	bcf 	out_b,5
	movfw	out_b
	movwf	PORTB
	call	midi_wait
	rrf 	tx_reg
	decfsz	tx_lcnt
	goto	midi_loop
midi_stop_bit
	nop
	nop
	nop
	nop
	bsf 	out_b,5		;stop bit
	movfw	out_b
	movwf	PORTB
	call	midi_wait
	nop
	return

midi_wait				;Xtal=10MHz
	nop
	nop
	movlw	21
	movwf	tx_wcnt
midi_wait_loop
	decfsz	tx_wcnt
	goto	midi_wait_loop
	return

wait_on
	movlw	10
	movwf	wcnt1
wlp1
	clrf	wcnt2
wlp2
	nop
	nop
	decfsz	wcnt2
	goto	wlp2
	decfsz	wcnt1
	goto	wlp1
	return

;test mode

test_loop
	movlw	0xEF
	movwf	PORTB
	call	kwait
	movlw	0xF7
	movwf	PORTB
	call	kwait
	movlw	0xFB
	movwf	PORTB
	call	kwait
	movlw	0xFD
	movwf	PORTB
	call	kwait
	goto	test_loop

kwait
kwait_hi
	btfss	PORTA,4
	goto	kwait_hi
kwait_lo
	btfsc	PORTA,4
	goto	kwait_lo
	movlw	100
	movwf	wcnt1
kwlp1
	clrf	wcnt2
kwlp2
	nop
	nop
	decfsz	wcnt2
	goto	kwlp2
	decfsz	wcnt1
	goto	kwlp1
	btfsc	PORTA,4
	goto	kwait_lo
	return

;EEPROM
	org 0x2100
	de	50	; 0
	de	60	; 4
	de	69	; 8
	de	79	;12
	de	52	; 1
	de	62	; 5
	de	71	; 9
	de	81	;13
	de	55	; 2
	de	64	; 6
	de	74	;10
	de	83	;14
	de	57	; 3
	de	67	; 7
	de	76	;11
	de	85	;15

	END
