



Note	EQU 078h
MidiB   EQU 079h
V0      EQU 07Ch


ORG 00H
JMP START

ORG 0023H
JMP SERI



START:  MOV PCON,#0
        MOV TMOD,#00100001B ;T1 8bit autoreload/T0 16bit
        MOV TL1,#255        ;Set load/reload
        MOV TH1,#255        ;
        MOV TL0,#0
        MOV TH0,#0
        SETB TR1
        MOV SCON,#01010000B
        
        SETB EA
        SETB ES


RUN:
        JMP RUN




SERI:
        PUSH ACC
        
        JB RI,LER
        JMP MidiEnd
LER:
        MOV A,SBUF
        MOV V0,A				;Buffers SBUF
        CLR RI

        ANL A,#11110000b

        ;=======CAUTION=========
        CJNE A,#090h,$+6
        LJMP PNoteOn
        
        CJNE A,#080h,$+6
        LJMP PNoteOff
        
        JB ACC.0, MidiEnd		;END if is another midi command (ie. RT)
                                        ;a bit buggy but should work
        
        JMP ProcessMidiData
        
        
PNoteOn:	;Process Note On Command
        MOV MidiB,#1
        JMP MidiEnd

PNoteOff:	;Process Note Off Command
        MOV MidiB,#5
        JMP MidiEnd


ProcessMidiData:
        MOV A,MidiB
        
        CJNE A,#1,$+6
        LJMP PNoteOnNote
        
        CJNE A,#2,$+6
        LJMP PNoteOnVelocity
        
        CJNE A,#5,$+6
        LJMP PNoteOffNote
        
        CJNE A,#6,$+6
        LJMP PNoteOffVelocity
        
        JMP MidiEnd ;Not Listed



PNoteOnNote:
        MOV Note,V0
        
        MOV MidiB,#2
        JMP MidiEnd


PNoteOnVelocity:
        MOV A,V0
        
        JZ VELZERO
        ;VELOCITY NOT ZERO
        
        ;Note On Routine====
        
        JMP PNoteOnVelocityEnd
        


VELZERO:;VELOCITY = ZERO (same as note off)
		
        ;;Note On Routine when velocity=zero


PNoteOnVelocityEnd:
        MOV MidiB,#0
        JMP MidiEnd


PNoteOffNote:
        MOV Note,V0

        MOV MidiB,#6
        JMP MidiEnd


PNoteOffVelocity:
        MOV MidiB,#0
        
        ;Note Off Routine====
        
        JMP MidiEnd


MidiEnd:
		CLR TI
        CLR RI
		POP ACC
        RETI

END

