;MAP.GEN
;E.L.  23-3-89
;BASIC-VERSJON, HITBIT
;VERSION 1.3
;-----------------------------------
;ADRESSER:
STARTA	EQU  #9F00	;STARTADRESSE
DATABUF	EQU  #AFFF
MAPST	EQU	#B000  ;MAP START
MIDICON	EQU	MAPST+128	;MIDIKANALER
CURPCB	EQU	#AF9A
INTGR	EQU	#F7F8	;PARAMETER->BASIC
MID_SR	EQU	#3FF6	;MIDI.GEN
MID_DR	EQU  #3FF5	;MIDI.GEN
EXSLOT	EQU  #FFFF
VRAMST	EQU	#0		;V-RAM START
PPIA		EQU	#A8		;PORT A
;
;ROM BIOS RUTINER:
POSIT	EQU  #C6	;FLYTT CURSOR
LDIRVM	EQU	#5C
LDIRMV	EQU	#59
CHGET	EQU  #9F
CHPUT	EQU	#A2
BEEP		EQU	#C0
ENASLT	EQU	#24		;?UBRUKT?
;
;MIDI:
SYSEX	EQU	#F0
;
;FEILKODER:
MIDIERR	EQU	#F0   ;MIDI ERROR
;
;ANDRE KODER:
HOME		EQU	#0101	;0VERST/VENSTRE
;
;MASKER:
SLMAPM	EQU  #A1	;HITBIT
SLMAPR	EQU  #A0	;HITBIT
;
;-----------------------------------
		;
		;
		DEFB #FE	;MSX-DISK
		DEFW STARTA ;BLOAD
		DEFW ENDA	;HEADER
		DEFW STARTA ;
		ORG  STARTA
		;
		JP	 MSTART
		JP	 MAPRES	;MAP RESET
		JP	 VELGST	;VELG FIL
		JP	 THRUST	;MIDI THRU
		JP	 SETMAP	;SET MAP VIA MIDI
		JP	 SETMC	;SET MIDI CH
		;
		;
		;
		;
		;
		;
		;
		;
		;
DBFAD	DEFW CURPCB
DLEN	DEFW 102
JOB		DEFB 2		;KODE/JOB
SAVESP	DEFW 0		;SP SAVE AREA
;
;
;
;-----------------------------------
;SYSTEM EXCLUSIVE HEADER:
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
;
;
;
;
;
;-----------------------------------
ERROR	LD	 A,(JOB)	;HENT KODE
		BIT	 7,A		;>7F?
		RET	 Z			;INGEN FEIL
		CP	 MIDIERR
		JR	 Z,MDERR
		RET
;	-	-	-	-	-	-	-
MDERR	LD	 HL,MDFEIL
		CALL SKRIV
		RET
		;	-	-	-	-	-
MDFEIL	DEFB 10
		DEFM "MIDI error"
NOTFRO	DEFB 15
		DEFM "MIDI note no.: "
NOTTO	DEFB 16
		DEFM " mapped to no.: "
;
;
;
;-----------------------------------
;SKRIV.LIB
;UTSKRIFT TIL SKJERMEN
SKRIV	LD	B,(HL)		;LENGDE
		INC	HL
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
;	-	-	-	-	-
CRLF		LD	A,#D		;CARR.RETURN
		CALL	CHPUT
		LD	A,#A		;LINE FEED
		CALL	CHPUT
		RET
;
;
;-----------------------------------
;
MSTART	CALL	START
		CALL	RESET
INPLOOP	CALL	INMD
		LD	HL,DATABUF
INPLP	LD	(HL),D
		LD	A,(MIDICON) ;9n
		CP	D		;NOTE ON?
		JR	Z,KONOTON
		SUB	#10	;8n
		CP	D		;NOTE OFF?
		JR	Z,KONOTOF
		LD	A,#E0	;PITCH WHEEL CH 1?
		CP	D
		JR	Z,MEND	;RETURN TO BASIC
		CALL	OPMD
		JR	INPLOOP
MEND		CALL	BACK
		RET
;
; - - - - - - - - - - - -
;
KONOTON	LD	A,(MIDICON+1);NOTE ON
		JR	KONOTE
KONOTOF	LD	A,(MIDICON+1)
		SUB	#10		;NOTE OFF
KONOTE	LD	(HL),A
		CALL	OPMD
		CALL	INMD		;GET NOTE
NOTELP	LD	HL,MAPST
		LD	L,D		;MAP
		CALL	OPMD
		CALL	INMD		;GET VELOCITY
		LD	HL,DATABUF
		LD	(HL),D
		CALL	OPMD
		CALL	INMD
		BIT	7,D		;STATUS BYTE?
		JR	Z,NOTELP
		JR	INPLP
;-----------------------------------
;SET MAP VIA MIDI
SETMAP	LD	HL,NOTFRO
		CALL	SKRIV
		CALL	NEWMAP	;GET NOTE NO.
		BIT	7,D	;STATUS BYTE?
		RET	NZ		;TIL BASIC
		LD	A,D
		LD	(DATABUF),A;SAVE NOTE NO.
		CALL	BINDEC3;SKRIV NOTE NO.
		LD	HL,NOTTO
		CALL	SKRIV
		CALL	NEWMAP
		BIT	7,D
		RET	NZ
		LD	A,(DATABUF)
		LD	HL,MAPST
		LD	L,A
		LD	(HL),D
		LD	A,D
		CALL	BINDEC3
		CALL	CRLF
		JR	SETMAP
;-----------------------------------
;
NEWMAP	CALL	START
		CALL	RESET
NEWLOOP	CALL	INMD
		LD	A,D
		CP	#E0	;PITCH WHEEL CH 1?
		JR	Z,NEWEND;RETURN TO BASIC
		CP	#90	;NOTE ON CH 1?
		JR	NZ,NEWLOOP
		CALL	INMD	;GET NOTE NO.
NEWEND	CALL	BACK
		RET
;
;
;
;
;
;
;
;-----------------------------------
;MIDI THRU
THRUST	CALL	START
		CALL	RESET
THRLOOP	CALL	INMD
		LD	HL,DATABUF
		LD	(HL),D
		LD	A,D
		CP	#EF  ;PITCH WHEEL CH 16?
		JR	Z,THREND	;RETURN TO BASIC
		CALL	OPMD
		JR	THRLOOP
THREND	CALL	BACK
		RET
;
;
;
;
;
;
;
;-----------------------------------
;SET MIDI CHANNELS
SETMC	INC	 HL
		INC	 HL
		LD	 A,(HL)
		OR	 #90
		LD	 (MIDICON),A
		INC	 HL
		LD	 A,(HL)
		OR	 #90
		LD	 (MIDICON+1),A
		RET
;-----------------------------------
;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 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
;-----------------------------------
;
;
;
;-----------------------------------
SELESC	CALL CHGET		;VENT
		CP	 24			;SELECT?
		JR	 Z,SELRET
		CP	 27			;ESCAPE?
		JR	 NZ,SELESC
		SCF				;SETT C-FLAGG
SELRET	RET
;-----------------------------------
;MAP RESET 1:1
MAPRES	LD	 HL,MAPST+129
		LD	 (HL),#90	;NOTE ON CH 1
		DEC	 HL
		LD	 (HL),#90
		DEC	 HL
		LD	 B,127		;BLOKKLENGDE
RESLOOP	LD	 (HL),B
		DEC	 HL
		DJNZ RESLOOP
		RET
;-----------------------------------
VELGST	CP	 3		;STRING?
		RET	 NZ		;HVIS IKKE,RETURN
		PUSH DE
		EX	 DE,HL	;STR LEN TIL HL
		LD	 C,(HL)	;	OG TIL BC
		LD	 B,0
		PUSH BC
		INC  HL
		LD	 E,(HL)	;STR ADR LABYT
		INC	 HL
		LD	 D,(HL)	;STR ADR H0BYT
		PUSH DE
		LD	 HL,HOME
		CALL VELGFIL
		CALL HL2ADR
		POP	 DE
		POP	 BC
		CALL LDIRMV
		POP	 DE
		RET
;-----------------------------------
;
;
;
;
;
;
;
;-----------------------------------
;CSRMOVE.LIB
;DISPLAY CURSOR,FLYTT CSR MED PILTASTER
;'RETURN' FOR AA AVLESE POSISJON
H0YRF	POP  HL
		LD	 A,H		;CSR LENGST
		CP	 20			; TIL H0YRE?
		JR	 NC,SAMMF	;IKKE FLYTT
		ADD	 A,13
		LD	 H,A
		JR	 SAMMF
VENSTRF	POP	 HL
		LD	 A,H
		CP	 5
		JR	 C,SAMMF
		SUB	 13
		LD	 H,A
		JR	 SAMMF
CSROPF	POP  HL
		LD	 A,L
		CP	 2
		JR	 C,SAMMF
		DEC	 A
		LD	 L,A
		JR	 SAMMF
CSRNEF	POP  HL
		LD	 A,L
		CP	 24
		JR	 NC,SAMMF
		INC	 A
		LD	 L,A
SAMMF	PUSH HL
		CALL POSIT
		JR	 CSRLPF
;	-	-	-	-	-	-	-
VELGFIL	PUSH HL			; START-
		CALL POSIT		; POSISJON
CSRLPF	CALL CHGET
		CP	 13			;RETURN?
		JR	 Z,TILBAKF
		CP	 28			;H0YRE?
		JR	 Z,H0YRF
		CP	 29			;VENSTRE?
		JR	 Z,VENSTRF
		CP	 30			;OPP?
		JR	 Z,CSROPF
		CP	 31			;NED?
		JR	 Z,CSRNEF
		JR	 CSRLPF
;	-	-	-	-	-	-	-
TILBAKF POP  HL
		RET				;TIL MAIN PRG
;-----------------------------------
;
;
;
;
;
;-----------------------------------
HL2ADR	LD	 DE,VRAMST
		EX	 DE,HL	;0-STILL HL,
		PUSH DE		;  KOORD. TIL DE
		LD	 B,E	;Y-KOORDINAT
		LD	 DE,40	;LINJELENGDE
FINNY	ADD	 HL,DE	;ADDER 40
		DJNZ FINNY	; Y-1 GANGER
		SBC	 HL,DE	;
		POP	 DE
		LD	 B,D	;X-KOORDINAT
FINNX	INC	 HL		;ADDER 1
		DJNZ FINNX	; X-1 GANGER
		DEC	 HL
		RET			;MED RIKTIG ADRESSE
;-----------------------------------
DECIBUF DEFS 7	;BIN->DESIMAL BUFFER
;-----------------------------------
;BN2DEC	Z80 ALS P.178
;KONV. 16-BIT BIN TIL ASCII-STRING
;INN:BIN I DE,ADR.TIL UTBUFFER I HL
;UT :BUFFER ADR. AV HL:
;	 1.BYTE:LENGDE, 2.BYTE EVT."-",
;	 ASCII-SIFRE
;REGISTRE:AF,BC,DE,HL
		;
		;
BN2DEC	LD	 (BUFPTR),HL
		EX	 DE,HL
		LD	 A,0
		LD	 (CURLEN),A
		LD	 A,H
		LD	 (NGFLAG),A
		OR	 A
		JP	 P,CNVERT
		EX	 DE,HL
		LD	 HL,0
		OR	 A
		SBC	 HL,DE
CNVERT	LD	 E,0
		LD	 B,16
		OR	 A
DVLOOP	RL	 L
		RL	 H
		RL	 E
		LD	 A,E
		SUB	 10
		CCF
		JR	 NC,DECCNT
		LD	 E,A
DECCNT	DJNZ DVLOOP
		RL	 L
		RL	 H
CHINS	LD	 A,E
		ADD	 A,"0"
		CALL INSERT
		LD	 A,H
		OR	 L
		JR	 NZ,CNVERT
EXIT	LD	 A,(NGFLAG)
		OR	 A
		JP	 P,POS
		LD	 A,"-"
		CALL INSERT
POS		EX	 DE,HL	;FEIL I BOKA?
		RET
		;
INSERT	PUSH HL
		PUSH AF
		LD	 HL,(BUFPTR)
		LD	 D,H
		LD	 E,L
		INC	 DE
		LD	 (BUFPTR),DE
		LD	 A,(CURLEN)
		OR	 A
		JR	 Z,EXITMR
		LD	 C,A
		LD	 B,0
		LDDR
EXITMR	LD	 A,(CURLEN)
		INC	 A
		LD	 (CURLEN),A
		LD	 (HL),A
		EX	 DE,HL
		POP	 AF
		LD	 (HL),A
		POP	 HL
		RET
		;
BUFPTR	DEFS	 2
CURLEN	DEFS	 1
NGFLAG	DEFS	 1
		;
;-----------------------------------
;BINDEC.LIB
;KONV.8-BITS BIN TIL 3-SIFRET ASCII
;OG SKRIVER UT TIL SKJERMEN
BINDEC3 LD	 B,#FF		;100-SIFFER
D100LP	INC	 B			;NULLSTILLES
		SUB	 100		;A<100 ?
		JR	 NC,D100LP	;HVIS IKKE,
		ADD	 A,100		;+100
		CALL ASC2DE		;SKRIV 100SIF
BINDEC2 LD	 B,#FF		;10-SIFFER
D10LP	INC  B			;NULLSTILLES
		SUB  10			;A<10 ?
		JR	 NC,D10LP	;HVIS IKKE,
		ADD  A,10		;+10
		CALL ASC2DE		;SKRIV 10SIF
BINDEC1 LD	 B,A		;1-SIFRET
		CALL ASC2DE		;SKRIVES
		RET
; - - - - - - - - - - - -
ASC2DE	LD	 C,A		;SAVE A
		LD	 A,B		;SIFF FRA B
		ADD  A,"0"		;KONV TIL ASC
		CALL CHPUT		;OG SKRIV UT
		LD	 A,C		;HENT A
		RET
;-----------------------------------
ENDA	NOP
;-----------------------------------
