;MIDI.GEN
;FRA YAHAMA G0TEBORG
;		.Z80			;????
;		CSEG			;????
;
STARTA	EQU  #9000
BLOKK	EQU  #A000
;
MID_SR	EQU	 #3FF6
MID_DR	EQU  #3FF5
PPIA	EQU  #A8
EXSLOT	EQU  #FFFF
SLMAPM	EQU  #A1		;HITBIT VERSJON
SLMAPR	EQU  #A0		;HITBIT
ENASLT	EQU  #24		;?UBRUKT?
;
;
		DEFB #FE		;BASIC
		DEFW STARTA		;BLOAD
		DEFW ENDA		;HEADER
		DEFW STARTA
		;
		ORG  STARTA
		JP	 START
		
;
;
;INTERFACE TO BASIC
;
DBFAD	DEFW BLOKK		;DATA BUF ADR
DLEN	DEFW 204		;DATA LENGTH
JOB		DEFB #2			;JOB SELECT
SAVESP	DEFW #0			;SP SAVE AREA
;
;
;CHANGE SLOT
;
START	DI
		LD	 (SAVESP),SP
;		IN	 A,(PPIA)
;		OR	 #C0		;SET BIT 7&6
;		OUT  (PPIA),A
;		LD	 A,(EXSLOT)
;		CPL
;		OR	 SLMAPM
;		LD	 (EXSLOT),A
		IN	 A,(PPIA)
		OR	 SLMAPM
		OUT  (PPIA),A
		;
		CALL RESET
		;
;JOB SELECT
		;
		LD	 A,(JOB)
		CP	 3
		CALL Z,RESET
		CP	 2
		CALL Z,IMIDI	;INPUT DATA
		CP	 1
		CALL Z,OMIDI	;OUTPUT DATA
		CP	 5
		CALL Z,OIMIDI	;OUT/IN
		CP	 4
		CALL Z,SUMCHK	;SUM CHECK
		CP	 6
		CALL Z,O1MIDI	;OUT 1 BYTE
		;
		;
;BACK TO BASIC
		;
BACK	LD	 A,SLMAPR
		OUT  (PPIA),A
		LD	 SP,(SAVESP)
		EI
		RET
		;
		;
;RESET MIDI
		;
RESET	LD	 A,#80
		LD	 (MID_SR),A
		LD	 A,#05
		LD	 (MID_SR),A
		XOR  A
		RET
		;
		;
;INPUT DATA FROM MIDI
		;
IMIDI	LD	 HL,(DBFAD)	;SET BUFADRESS
IMSLP	CALL INMD
		RET  C
		LD	 A,D
		CP	 #F0		;SYS EX?
		JR	 NZ,IMSLP
		LD	 (HL),A
		INC  HL
		LD	 B,5
IMSLP2	CALL INMD
		RET  C
		LD	 (HL),D
		INC  HL
		DJNZ IMSLP2
		LD	 IX,(DBFAD)
		LD	 A,(IX+4)
		LD	 D,A
		LD	 A,(IX+5)
		SRL  D
		JR	 NC,IMSNX
		ADD  A,#80
IMSNX	LD	 C,A
		LD	 B,D
		LD	 (DLEN),BC
		INC	 BC
		INC  BC
		;
	;	LD	 BC,(DLEN)
IMLP	CALL INMD
		RET  C
		LD	 (HL),D
		INC  HL
		DEC  BC
		LD	 A,B
		OR	 C
		JR	 NZ,IMLP
		RET
		;
		;
;INPUT MIDI TO D-REG
;DATA:D		X:A
		;
INMD	LD	 A,(MID_SR)
		BIT	 5,A		;?????????
		JR	 NZ,FE
		BIT  1,A
		JR	 Z,INMD
		LD	 A,(MID_DR)
		LD	 D,A
		AND  #F8
		CP	 #F8		;CANCEL
		JR	 Z,INMD		; READ TIME
		AND  A			;NO ERROR
		RET
		;
FE		LD	 A,#F0
		LD	 (JOB),A
		SCF
		RET				;ERROR
		;
		;
;OUTPUT DATA TO MIDI
		;
OMIDI	LD	 HL,(DBFAD)
		LD	 BC,(DLEN)
OMLP	CALL OPMD
		INC  HL
		DEC  BC
		LD	 A,B
		OR	 C
		JR	 NZ,OMLP
		RET
		;
OPMD	LD	 A,(MID_SR)
		BIT  0,A
		JR	 Z,OPMD
		LD	 A,(HL)
		LD	 (MID_DR),A
		RET
		;
		;
;SUM CHECK
		;
SUMCHK	LD	 HL,(DBFAD)
		LD	 BC,(DLEN)
		LD	 D,0
SULP	LD	 A,(HL)
		ADD  A,D
		LD	 D,A
		INC  HL
		DEC  BC
		LD	 A,B
		OR	 C
		JR	 NZ,SULP
		LD	 A,D
		LD	 (JOB),A
		RET
		;
		;
;REQUEST SEND & RECEIVE DATA
		;
OIMIDI	CALL OMIDI
		CALL IMIDI
		RET
		;
		;
;OUTPUT 1 BYTE DATA TO MIDI
		;
O1MIDI	INC  HL
		INC  HL
		CALL OPMD
		RET
		;
		;
ENDA	NOP
		;
		;
