;ALSEQ.GEN VER 1.2
;E.L. JUNI 1987,REV.3-8-87
;BASIC-VERSJON, HITBIT
;-----------------------------------
;SENDER SYSEX ALL SEQUENCE DUMP
;  REQUEST
;  MOTTAR SEQUENCE DUMP ALERT,SJEKKER
;  DATALENGDE,SENDER EVT. ACCEPT MESS
;  MOTTAR SEQUENCE DATA,LAGRER FRA
;  ALLSEQ TIL ALLSEQ+DATALENGDE
;
;SENDER SYSEX ALL SEQUENCE DUMP ALERT,
;  VENTER PAA ACCEPT MESSAGE FRA ESQ,
;  SENDER EVT. ALL SEQ DUMP PACKET,
;  ELLER SKRIVER FEILMELDING HVIS
;  ESQ NEKTER.
;
;SKRIVER UT ALLE SONG NAVN
;
;
;-----------------------------------
;ADRESSER:
STARTA	EQU  #8800	;STARTADRESSE
ALLSEQ	EQU	 #8B00	;LAGERADRESSE
INTGR	EQU	 #F7F8	;PARAMETER->BASIC
MID_SR	EQU	 #3FF6	;MIDI.GEN
MID_DR	EQU  #3FF5	;MIDI.GEN
EXSLOT	EQU  #FFFF
VRAMST	EQU	 #0		;VRAMSTART
PPIA	EQU  #A8	;PORT A
;
;
;ROM BIOS RUTINER:
POSIT	EQU  #C6	;FLYTT CURSOR
LDIRVM	EQU	 #5C
CHGET	EQU  #9F
CHPUT	EQU	 #A2
BEEP	EQU  #C0
ENASLT	EQU  #24		;?UBRUKT?
;
;
;
;
;MIDI:
SYSEX	EQU	 #F0
MAXSIZE	EQU	 #55	;H0BYT SEQLENGDE
TRPOLEN	EQU	 #0132	;TRACK/POINTER
					;	INFO LENGDE
;FEILKODER:
TOOBIG	EQU	 #F1   ;SEQ FOR LANG
MIDIERR	EQU	 #F0   ;MIDI ERROR
WRNGCOD	EQU	 #F2   ;GAL KODE
NOMEMOR	EQU	 #F5   ;FOR LITE MEMORY
NOFREE	EQU	 #FD   ;INGEN LEDIG SEQ.
;
;MASKER:
SLMAPM	EQU  #A1	;HITBIT
SLMAPR	EQU  #A0	;HITBIT
;
;DIVERSE:
STAPOS	EQU	 #0601	;CSR STARTPOSISJON
HOME	EQU	 #0101	;  0VERST/VENSTRE
;-----------------------------------
;-----------------------------------
		DEFB #FE	;MSX-DISK
		DEFW STARTA ;BLOAD
		DEFW ENDA	;HEADER
		DEFW STARTA ;
		ORG  STARTA
		JP	 ALSLST	;U9 ALLSEQ LOAD
		JP	 ALSDST	;U0 ALLSEQ DUMP
		JP	 ALSDIS	;U1 SONGS DISPLAY
;-----------------------------------
DBFAD	DEFW ALLSEQ		;DATA BUF ADR
DLEN	DEFW 102		;DATA LENGTH
JOB		DEFB 2			;KODE/JOB
SAVESP	DEFW 0			;SP SAVE AREA
		;
SXHEAD	DEFB #F0	;SX STATUS BYTE
		DEFB #0F	;ENSONIQ I.D.
		DEFB #02	;ESQ I.D.
MCHAN	DEFB #00	;MIDI KANAL
REQUEST DEFB #0		;PROG.DUMP REQUEST
EOX		DEFB #F7	;END OF EXCLUSIVE
;-----------------------------------
ALSALRT	LD	 A,3	;ALL SEQ ALERT
		JR	 SXSTOUT
ACCEPT	LD	 A,4	;SEQ ACCEPT
		JR	 SXOUT
REJECT	LD	 A,5	;SEQ REJECT
		JR	 SXOUT
ALSDUMP	LD	 A,6	;ALL SEQ DUMP
		JR	 SXSTOUT
ALSREQ	LD	 A,11	;ALL SEQ REQUEST
		JR	 SXOUT
		;
SXSTOUT	LD	 BC,5
		JR	 SXUT
SXOUT	LD	 BC,6
SXUT	LD	 (REQUEST),A
		LD	 HL,SXHEAD
		CALL OMLP
		RET
;-----------------------------------
;
;-----------------------------------
;ALL SEQUENCES ESQ->CX
ALSDST	CALL START
		CALL RESET
		CALL ALSREQ		;ALSEQDUMPREQ
		LD	 A,3		;ALS ALERT
		CALL INSIZE2	;VENT ALERT
		JR	 NC,DMPSEQ
		CALL REJECT
		JR	 SEQEXIT
DMPSEQ	CALL COPYLEN
		CALL ADDLEN
		CALL WRITLEN	;FOR BASIC
		CALL ACCEPT
		LD	 HL,ALLSEQ+2
		LD	 (DBFAD),HL
		CALL SXJUMP
		CALL IMIDI
SEQEXIT	CALL BACK
		CALL ERROR		;EVT.ERROR?
		RET				;TIL BASIC
;-----------------------------------
;-----------------------------------
;INSIZE	LESER 1 SEQ DUMP ALERT PACKET
;LAGRER SEQSIZE I DLEN OG LAGRER
;FEILKODE HVIS SIZE > MAXSIZE
INSIZE	LD	 A,7		;1 SEQ ALERT
INSIZE2	LD	 (JOB),A	;		KODE
		CALL SXJUMP		;LES SXHEADER
		LD	 HL,DLEN
		LD	 BC,2
		CALL IMPDLP
		CALL INMD		;LES EOX
		;
		LD	 A,(DLEN+1)	;H0BYT LENGDE
		CP	 MAXSIZE	;LEN>MAXSIZE?
		CCF
		RET	 NC
		LD	 A,TOOBIG	;FEILMELDING
		LD	 (JOB),A
		RET				;TIL SEQDST
;-----------------------------------
;
;
;
;-----------------------------------
;WRITLEN LESER DATALENGDE,
; REGNER UT SLUTTADRESSE
; OG SKRIVER DEN TIL INTGR (BASIC/
; MASKINKODE PARAMETER PASSING)
WRITLEN	LD	 BC,(DLEN)	;HENT DATALEN
		LD	 HL,ALLSEQ	;STARTADRESSE
		ADD	 HL,BC		;ENDADRESSE
		INC	 HL
		INC	 HL
		LD	 (INTGR),HL	;TIL BASIC
		RET
;-----------------------------------
;
;
;
;
;
;-----------------------------------
;COPYLEN KOPIERER DATALENGDE FRA DLEN
;TIL ALLSEQ,OG BYTTER OM H0Y/LAV BYTE
COPYLEN	LD	 BC,(DLEN)
		LD	 HL,ALLSEQ
		LD	 (HL),B		;H0BYTE F0RST
		INC	 HL
		LD	 (HL),C
		RET
;-----------------------------------
;ADDERER TRACK/POINTER INFO LENGDE
;	TIL (DLEN)
ADDLEN	LD	 HL,TRPOLEN	;TRACK/POINTER
		LD	 BC,(DLEN)	;	INFO LEN
		ADD	 HL,BC
		LD	 (DLEN),HL
		RET
;-----------------------------------
;
;
;-----------------------------------
ALSLST	LD	 HL,ALLSEQ
		LD	 (DBFAD),HL
		CALL START
		CALL RESET
		CALL UTSIZE
		LD	 A,4		;ACCEPT CODE
		LD	 (JOB),A
		CALL SXJUMP		;VENT SVAR
		JR	 Z,SEQLOAD	;JA,AKSEPTERT
		ADD	 A,#F0		;FEILKODE
		LD	 (HL),A		;TIL (JOB)
		JR	 SEQLEX
SEQLOAD CALL ADDLEN
		CALL ALSDUMP
		LD	 HL,(DBFAD)
		INC	 HL
		INC	 HL
		LD	 BC,(DLEN)
		CALL OMLPESQ
SEQLEX	CALL BACK
		CALL ERROR
		RET				;SEQ CX->ESQ
;-----------------------------------
;UTSIZE	KOPIERER SEQSIZE FRA ALLSEQ
;TIL DLEN (SNUR H0Y/LAV BYTE),
;OG SENDER 1 SEQ DUMP ALERT PACKET
UTSIZE	LD	 HL,(ALLSEQ)
		LD	 DE,DLEN
		LD	 A,H		;LABYTE I H!
		LD	 (DE),A
		INC	 DE
		LD	 A,L		;H0BY I L!
		LD	 (DE),A
		CALL ALSALRT	;ALERT PACKET
		LD	 HL,DLEN	;SKRIV DATA-
		LD	 BC,2		;	LENGDE
		CALL OMLPESQ	;
		RET
;-----------------------------------
		;
		;
		;
		;
		;
		;
		;
		;
		;
		;
		;
		;
		;
		;
;-----------------------------------
OMLPESQ	CALL OPMDESQ
		INC  HL
		DEC  BC
		LD	 A,B
		OR	 C
		JR	 NZ,OMLPESQ
		LD	 HL,EOX
		CALL OPMD
		RET				;TIL CPLST
;	-	-	-	-	-	-	-
;
;
;
OPMDESQ	LD	 D,(HL)		;SAVE DATA
		LD	 A,(MID_SR)
		BIT  0,A
		JR	 Z,OPMDESQ
		RRD				;LANIB(HL)->A
		AND	 #0F	   ;MASK BIT 7654
		LD	 (MID_DR),A	;SEND UT
OPMDES2	LD	 A,(MID_SR)
		BIT  0,A
		JR	 Z,OPMDES2
		RRD				;LANIB(HL)->A
		AND	 #0F
		LD	 (MID_DR),A	;SEND UT
		LD	 (HL),D		;HENT DATA
		RET				;TIL OMLPESQ
;-----------------------------------
;
;
;-----------------------------------
ALSDIS	LD	 HL,ALLSEQ+#B3
		LD	 B,2		;2*5 SONGS
S2DISLP	PUSH BC
		LD	 B,5		;MELLOMROM
		LD	 A," "
S1DISLP	CALL CHPUT
		DJNZ S1DISLP
		LD	 B,5	;5 F0RSTE SONGS
SODISLP	PUSH BC
		LD	 B,6		;6 BOKSTAVER
		CALL SKRIVUT
		POP	 BC
		DJNZ SODISLP
		POP	 BC
		DJNZ S2DISLP
		RET
;
;
;
;
;
;-----------------------------------
SKRIVUT	LD	 A,#20		;MELLOMROM
		CALL CHPUT
SKRIVLP	LD	 A,(HL)		;HENT TEGN
		CP	 #20		;CTRL CHAR?
		JR	 NC,GREIT	;NEI,SKRIV
		LD	 A,#20		;JA,SKRIV " "
GREIT	CALL CHPUT		;SKRIV DET
		INC	 HL			;NESTE TEGN
		DJNZ SKRIVLP
		RET
;-----------------------------------
ERROR	LD	 A,(JOB)	;HENT KODE
		BIT	 7,A		;>7F?
		RET	 Z			;INGEN FEIL
		CP	 TOOBIG
		JR	 Z,TOOBERR
		CP	 WRNGCOD
		JR	 Z,CODEERR
		CP	 MIDIERR
		JR	 Z,MDERR
		CP	 NOMEMOR
		JR	 Z,MEMERR
		CP	 NOFREE
		JR	 Z,SEQERR
		JR	 CODEERR  ;TEST TEST TEST
		RET
;	-	-	-	-	-	-	-
;FEILKODER:
;TOOBIG		#F1   ;SEQ FOR LANG
;MIDIERR	#F0   ;MIDI ERROR
;WRNGCOD	#F2   ;GAL KODE
;NOMEMOR	#F5   ;FOR LITE MEMORY
;NOFREE		#FD   ;INGEN LEDIG SEQ.
TOOBERR	LD	 HL,FORSTOR
		LD	 B,18
		CALL SKRIVLP
		RET
MDERR	LD	 HL,MDFEIL
		LD	 B,10
		CALL SKRIVLP
		RET
CODEERR LD	 HL,GALKODE
		LD	 B,10
		CALL SKRIVLP
		RET
MEMERR	LD	 HL,FULLMEM
		LD	 B,17
		CALL SKRIVLP
		RET
SEQERR	LD	 HL,FULLSEQ
		LD	 B,16
		CALL SKRIVLP
		RET
FORSTOR	DEFM "Sequence too large"
MDFEIL	DEFM "MIDI error"
GALKODE DEFM "Wrong code"
FULLMEM	DEFM "Not enough memory"
FULLSEQ	DEFM "No free sequence"
;-----------------------------------
;-----------------------------------
;MIDI.GEN (FRA YAMAHA,G0TEBORG)
		;
;CHANGE SLOT:
		;
START	DI
		LD	 (SAVESP),SP
		IN	 A,(PPIA)
		OR	 SLMAPM
		OUT  (PPIA),A
		RET
		;
;	-	-	-	-	-	-	-
;BACK TO MAIN PROGRAM:
		;
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:
SXJUMP	CALL INMD
		RET  C
		LD	 A,D
		CP	 SYSEX		;SYS EX?
		JR	 NZ,SXJUMP
		LD	 B,4
SXLOOP	CALL INMD
		RET  C
		DJNZ SXLOOP
		LD	 HL,JOB		;HENT
		LD	 A,D		;	KODE
		CP	 (HL)	;RIKTIG MESSAGE?
		RET
		;
;	-	-	-	-	-	-	-
IMIDI	LD	 HL,(DBFAD)	;SET BUFADRESS
		LD	 BC,(DLEN)	;OG LENGDE
IMPDLP	CALL INMD		;HENT DATA
		RET  C			;
		LD	 A,D		;LANIB A TIL
		RRD				;H0NIB (HL)
		CALL INMD		;NEXT NIBBLE
		RET  C
		LD	 A,D
		RRD
		INC  HL
		DEC  BC			;TELL NED
		LD	 A,B		;BC=0?
		OR	 C
		JR	 NZ,IMPDLP
		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		; REALTIME
		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
;	-	-	-	-	-	-	-
;
		;			;END MIDI.GEN
		;
ENDA	NOP
		;
