FADEN97 ASM;
;
; Faden-Demo FADEN97.KCC
; ______________________
;
;
; - Spiegelung horizontal+vertikal
; - LINE-Unterprogramm nur 1x
; - Speicherbereich w{hlbar
; - regeneriert Pixelinhalt
;
;--------------------------------------
 
ORG	EQU 4000H ;Speicherbereich
 
PV1	EQU	0F003H
CRT	EQU 00H
KBDZ	EQU 0EH
AHEX	EQU 1CH
CRLF	EQU 2CH
MULT	EQU 41H
MENU	EQU 46H
 
MEM	EQU ORG   ;Koordinatenspeicher
 
X1	EQU ORG+100H ;Koordinaten
X2	EQU ORG+101H
Y1	EQU ORG+102H
Y2	EQU ORG+103H
X3	EQU ORG+104H
X4	EQU ORG+105H
Y3	EQU ORG+106H
Y4	EQU ORG+107H
 
ZUF1	EQU ORG+108H ;Zufallszahlen
ZUF2	EQU ORG+109H
ZUF3	EQU ORG+10AH
ZUF4	EQU ORG+10BH
 
ZEIGER	EQU ORG+10CH ;Zeiger
GRENZE	EQU ORG+10DH ;Grenze
HMIRR	EQU ORG+10EH ;hor. Spiegel
VMIRR	EQU ORG+10FH ;vert. Spiegel
 
	ORG ORG+110H
;
; Erzeugung einer Zufallszahl
; (im Bereich von 3 bis 13)
;
RND:	EXX
	LD	C,15
	LD	A,(ZAHL)
	INC	A
	LD	D,A
	CALL	PV1	; Zahl*15
	DB	MULT
	ADD	A
	ADD	B
	LD	C,A
	LD	A,R
	ADD	L
	LD	(ZAHL),A
	LD	B,11
RND1:	SUB	B
	CP	B
	JR	NC,RND1
	ADD	A,3
	EXX
	RET
 
ZAHL	DB	0	; Ablage
 
;======================================
 
	DW	7F7FH
	DB	'ZUFALL'
	DB	1
	CALL	RND
	CALL	PV1
	DB	AHEX
	CALL	PV1
	DB	CRLF
	RET
;
; ----- Programmschleife -----
;
; neue Betr{ge erzeugen,
; wenn Rand erreicht ist
;
FADEN:	LD	HL,X1
	LD	DE,ZUF1
	LD	B,4	;4 Werte
NEU:	LD	A,M
	AND	A
	JR	NZ,NEU1
	CALL	RND
	LD	(D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L
	LD	C,M
	CALL	LINE	;2. Linie
NOSP3:	LD	A,(VMIRR)
	AND	A
	JR	Z,NOSP4
	LD	A,(ZEIGER)
	LD	L,A
	LD	H,MEM/256
	INC	HL
	INC	HL
	LD	E,M
	INC	HL
	LD	C,M
	INC	HL
	LD	D,M
	INC	HL
	LD	B,M
	CALL	LINE	;3. Linie
	LD	A,(HMIRR)
	AND	A
	JR	Z,NOSP4
	LD	A,(ZEIGER)
	LD	L,A
	LD	H,MEM/256
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	LD	D,M
	INC	HL
	LD	B,M
	INC	HL
	LD	E,M
	INC	HL
	LD	C,M
	CALL	LINE	;4. Linie
NOSP4:
;
; Koordinaten f}r n{chste Linie
; aus Zufallszahlen berechnen
;
	LD	HL,ZUF1
	LD	DE,X1
	LD	B,4	;4 Werte
CALC:	LD	A,(DE)
	ADD	M
	BIT	7,M
	JR	NZ,CALC2
	JR	NC,CALC3
	LD	A,-1
	JR	CALC3
CALC2:	JR	C,CALC3
	XOR	A
CALC3:	LD	(DE),A
	INC	HL
	INC	DE
	DJNZ	CALC
;
; Tastaturabfrage/Auswertung
;
	XOR	A
	CALL	PV1
	DB	KBDZ
	AND	A
	JP	Z,FADEN
	CP	3
	RET	Z	;Ende
	CP	20H
	CALL	Z,PAUSE
	CP	'+'
	JR	Z,PLUS
	CP	'-'
	JR	Z,MINUS
	AND	5FH	;upcase
	CP	'H'
	JR	Z,HSWI
	CP	'V'
	JR	Z,VSWI
	JP	FADEN
;
; Unterbrechung
;
PAUSE:	CALL	PV1
	DB	KBDZ
	CP	20H
	JR	Z,PAUSE
	AND	A
	RET	NZ
	JR	PAUSE
;
; zwei Linien mehr
;
PLUS:	LD	A,(GRENZE)
	AND	A
	JP	Z,FADEN
	ADD	16
SET1:	LD	(GRENZE),A
SET2:	XOR	A
	LD	(ZEIGER),A
	CALL	MCLR
	CALL	CLS
	JP	FADEN
;
; zwei Linien weniger
;
MINUS:	LD	A,(GRENZE)
	SUB	16
	JP	Z,FADEN
	JR	SET1
;
; hor. Spiegelung ein/aus
;
HSWI:	LD	A,(HMIRR)
	XOR	1
	LD	(HMIRR),A
	JR	SET2
;
; vert. Spiegelung ein/aus
;
VSWI:	LD	A,(VMIRR)
	XOR	1
	LD	(VMIRR),A
	JR	SET2
 
;======================================
 
	DW	7F7FH
	DB	'FADEN'
	DB	1
	LD	HL,8000H
	LD	DE,5800H
	LD	BC,2800H
	LDIR		;Pixel retten
	CALL	CLS
	LD	HL,MEM
	LD	DE,MEM+1
	LD	BC,10FH
	LD	M,0	;L|schen
	LDIR		;MEM+VAR
	LD	A,90H
	LD	(GRENZE),A
	CALL	FADEN
	LD	HL,5800H
	LD	DE,8000H
	LD	BC,2800H
	LDIR		;Pixel regen.
	RET
;
; Pixel-CLS
;
CLS:	LD	HL,8000H
	LD	DE,8001H
	LD	BC,27FFH
	LD	M,0
	LDIR
	RET
;
; Koordinatenspeicher l|schen
;
MCLR:	LD	HL,MEM
	LD	DE,MEM+1
	LD	BC,0FFH
	LD	M,0
	LDIR
	RET
;
; Linie setzen:
;	D=X1	E=Y1
;	B=X2	C=Y2
;
LINES:	DB	21H
	NOP
	OR	M
	JR	LINE0
	;
LINER:	DB	21H
	CPL
	AND	M
LINE0:	LD	(LIEND),HL
	LD	(PUNKT2),HL
LINE:	LD	L,0
	LD	A,B
	SUB	D
	JR	NC,LINE2
	SET	0,L
	NEG
LINE2:	LD	H,A
	LD	A,C
	SUB	E
	JR	Z,LINE3
	JR	NC,LINE4
	NEG
	SET	1,L
LINE4:	LD	B,L
	LD	L,A
	CP	H
	JR	C,LINE5
	LD	A,H
	LD	H,L
	LD	L,A
	SET	2,B
LINE5:	LD	A,B
	EXX
	ADD	A
	LD	B,A
	LD	HL,LITAB1
	ADD	L
	LD	L,A
	LD	A,M
	LD	(UP1+1),A
	INC	HL
	LD	A,M
	LD	(UP1+2),A
	LD	HL,LITAB2
	LD	A,B
	AND	0F7H
	ADD	L
	LD	L,A
	LD	A,M
	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