	title	Fruit Zeichendarstellung
	name	('F000')

; Diese Routine gibt formatierten Text fuer das Spiel FRUIT.PAS aus.
; Die Vorbereitung erfolgt ueber das Hauptprogramm. Die Routine ist
; geschrieben zum Lauf ab Adresse 0xf000.

esc	equ	1bh

XBIOS	equ	0fc5ah		; XBIOS Adresse
SCR_RUN	equ	000e9h		; SCR_RUNTIME

ROLLER	equ	0b600h		; Roller RAM Adresse
CHRMTX	equ	0b800h		; Zeichenmatrix

MAXCOL	equ	89
MAXROW	equ	31

mtxl	equ	8

normsiz	equ	1*256+1

BIGSTYL	equ	11111111b	; Spezial-ESCape

	.phase	0f000h
;
; Tool zur Darstellung von Zeichen
;
; Routine basiert auf dem Artikel "SuperScript - 
; Abenteuer im Inneren des Joyce" aus der in PC, 6'1987
;
; Register HL zeigt auf x Position
; Register DE zeigt auf y Position
; Register BC haelt Adresse der Zeichenkette
;
; Das sind Uebergabeparameter wie in MALLARD-BASIC
;
FR_Msg:
	ld	l,(hl)		; x-Position
	ld	a,(de)
	ld	h,a		; y-Position
	dec	l
	dec	h
	ld	(xypos),hl
	ld	l,c		; Zeiger auf Stringdeskriptor
	ld	h,b
	ld	a,(hl)		; Stringlaenge
	or	a
	ret	z		; -> zurueck, falls Stringlaenge 0
	ld	c,a
	ld	b,0
	ld	(strlen),a	; Stringlaenge speichern
	inc	hl
	ld	a,(hl)
	inc	hl
	ld	h,(hl)
	ld	l,a		; hl = Stringadresse
	ld	de,string
	ldir			; String in Common-Bereich uebertragen
	ld	bc,rout		; Adresse der aufzurufenden Routine
	call	XBIOS
	dw	SCR_RUN		; Screen-Environment einschalten
	ret			; zurueck zu TURBO PASCAL
;
; Ausfuehren der Routine ueber SCR_RUNTIME
;
rout:
	ld	a,(strlen)	; Stringlaenge
	ld	b,a
	ld	hl,string	; Stringadresse
mloop:
	ld	a,(hl)		; Zeichen aus String
	cp	esc		; ESC-Code?
	jr	nz,getmat	; -> nein
	inc	hl
	dec	b
	ret	z		; -> kein weiterer Code folgt
	ld	a,(hl)		; Schriftstil holen
	ld	(style),a	; und merken
	inc	hl
	dec	b
	ret	z		; -> kein weiterer Code folgt
	ld	a,(hl)		; naechstes Zeichen
getmat:
	push	bc
	push	hl
	ld	l,a		; Zeichen
	ld	h,0
	add	hl,hl		; mal 8
	add	hl,hl
	add	hl,hl
	ld	de,CHRMTX	; plus Startadresse Zeichen-RAM
	add	hl,de		; ergibt Matrixadresse
	ld	de,matrix1
	ld	bc,mtxl
	ldir			; Matrix holen
	ld	hl,normsiz	; normale Breite/Hoehe
	ld	(width),hl	; als Vorgabe
	ld	a,(style)	; Schrifttyp holen
;
; Definition des Schrifttypen:
;
; Bit 7    6     5     4     3     2     1     0
; +-----+-----+-----+-----+-----+-----+-----+-----+
; | Ind | Exp | Hoh | Brt | Inv | Unt | Fet | Lig |
; +-----+-----+-----+-----+-----+-----+-----+-----+
;
; Alle Bits gesetzt: Schrift im Grossformat
;
	cp	BIGSTYL
	jr	z,acht		; -> Grosschrift 8x8
	rra
	call	c,light
	rra
	call	c,fett
	rra
	call	c,untstr
	rra
	call	c,invers
	rra
	call	c,breit
	rra
	call	c,hoch
	rra
	call	c,expo
	rra
	call	c,index
	call	ausg		; manipuliertes Zeichen ausgeben
nchar:
	pop	hl
	pop	bc
	inc	hl		; Zeiger auf naechstes Zeichen
	djnz	mloop
	ret
;
; Grosschrift 8x8 bearbeiten
;
acht:
	ld	hl,matrix1
	ld	b,mtxl
	ld	de,(xypos)
	ld	a,d
	sub	b
	ld	d,a		; Textpos 8 Zeilen nach oben
nreih8:
	inc	d		; naechste Reihe
	push	bc
	push	de
	push	hl
	ld	b,mtxl
	ld	c,(hl)		; Pixelreihe aus Matrix
nspal8:
	rlc	c		; naechstes Pixel
	ld	hl,matrix1
	jr	c,setch
	ld	hl,space
setch:
	call	chout		; Zeichen ausgeben
	inc	e
	ld	a,e
	djnz	nspal8
	pop	hl
	pop	de
	pop	bc
	inc	hl		; naechste Pixelreihe
	djnz	nreih8
	ld	(xypos),a
	jr	nchar		; -> naechster Buchstabe
;
; Unterstreichen ausfuehren
;
untstr:
	ld	hl,matrix1+mtxl-1; in der 8. Pixelreihe
	ld	(hl),11111111b	; unterstreichen
	ret
;
; Duennschrift ausfuehren
;
light:
	push	af		; Duennschrift
	ld	hl,matrix1	; Matrix manipulieren
	ld	b,mtxl
lloop:
	ld	a,(hl)
	srl	a
	and	(hl)
	ld	(hl),a
	inc	hl
	djnz	lloop
	pop	af
	res	0,a		; Fett sperren
	ret
;
; Fett ausfuehren
;
fett:
	push	af
	ld	hl,matrix1	; Matrix manipulieren
	ld	b,mtxl		; 8 Pixelreihen
floop:
	ld	a,(hl)
	srl	a
	or	(hl)
	ld	(hl),a
	inc	hl
	djnz	floop
	pop	af
	ret
;
; Invers ausfuehren
;
invers:
	push	af
	ld	hl,matrix1
	ld	b,mtxl
iloop:
	ld	a,(hl)
	cpl			; Pixelreihe invertieren
	ld	(hl),a
	inc	hl
	djnz	iloop
	pop	af
	ret
;
; Breitschrift ausfuehren
;
breit:
	push	af
	ld	a,2
	ld	(width),a
	ld	ix,matrix1
	ld	c,mtxl		; 8 Pixelreihen
nrow:
	ld	b,mtxl		; 8 Pixel verdoppeln
	ld	a,(ix+0)	; Pixelreihe
npix:
	rra			; Pixel ins Carry
	rr	(ix+0)
	rr	(ix+2*mtxl)
	sra	(ix+0)		; Pixel verdoppeln
	rr	(ix+2*mtxl)
	djnz	npix
	inc	ix		; Zeiger auf naechste Pixelreihe
	dec	c
	jr	nz,nrow
	pop	af
	ret
;
; Hochschrift ausfuehren
;
hoch:
	ld	a,2
	ld	(hight),a
	ld	de,matrix1+mtxl-1
	call	expand		; Pixelreihen verdoppeln
	ld	a,(width)
	dec	a		; doppelte Breite?
	ret	z		; -> nein
	ld	de,matrix3+mtxl-1; Adresse der zweiten Haelfte
expand:
	ld	hl,mtxl
	add	hl,de
	ld	b,mtxl		; 8 Pixelreihen
nhi:
	ld	a,(de)		; Pixelreihe
	dec	de
	ld	(hl),a		; verdoppeln
	dec	hl
	ld	(hl),a
	dec	hl
	djnz	nhi
	xor	a		; Exponent/Index sperren
	ret
;
; Index ausfuehren
;
index:
	xor	a		; Hoehe 0 fuer Index
	jr	indexp
;
; Exponent ausfuehren
;
expo:
	ld	a,2		; Hoehe 2 fuer Exponent
indexp:
	ld	(hight),a	; merken
	ld	de,matrix2+mtxl-1
	call	trans		; Matrix verschieben
	ld	a,(width)
	dec	a		; doppelte Breite?
	ret	z		; -> nein
	ld	de,matrix4+mtxl-1; Adresse der rechten Haelfte
trans:
	call	clear		; 4 Pixelreihen loeschen
	ld	hl,-mtxl/2
	add	hl,de
	ld	bc,mtxl
	lddr			; Matrix um 4 Pixelreihen verschieben
clear:
	xor	a
	ld	(de),a
	dec	de
	ld	(de),a
	dec	de
	ld	(de),a
	dec	de
	ld	(de),a
	dec	de
	ret
;
; Neue Matrix ausgeben
;
ausg:
	ld	hl,matrix1	; manipulierte Matritzen ausgeben
	ld	bc,(width)	; b=Hoehe, c=Breite
	ld	de,(xypos)	; Textkoordinaten
nspalt:
	push	de
	push	hl
	dec	d
	dec	b
	dec	b
	call	z,chout		; oben f. Exp./doppelte Hoehe
	inc	d
	call	chout		; Mitte
	inc	d
	inc	b
	inc	b
	call	z,chout		; unten fuer Index
	pop	hl
	ld	de,mtxl+mtxl	; Adresse der rechten Haelfte
	add	hl,de		; berechnen
	pop	de
	inc	e		; naechste Textspalte
	dec	c
	jr	nz,nspalt	; -> falls doppelte Breite
	ld	(xypos),de	; neue Textkoordinaten merken
	ret
;
; Matrix in den Bildschirm kopieren in Position x (Reg E) und y (Reg D)
;
chout:
	ld	a,e		; Spalte
	cp	MAXCOL+1	; groesser 89?
	ret	nc		; -> ja
	ld	a,d		; Reihe
	cp	MAXROW+1	; groesser 31?
	ret	nc		; -> ja
	push	bc
	push	de
	push	hl
	ld	d,0
	rlca			; reihe * 16
	rla
	rla
	rla
	ld	l,a
	ld	a,d
	adc	a,HIGH ROLLER
	ld	h,a		; hl = b600+Reihe*16
	ld	a,(hl)		; Wert aus Roller-RAM
	inc	hl
	ld	h,(hl)
	ld	l,a
	ex	de,hl		; nach de
	add	hl,hl		; Spalte * 4
	add	hl,hl
	add	hl,de		; + Wert aus Roller-RAM
	add	hl,hl		; * 2 = Screenadresse
	ex	de,hl		; nach de
	pop	hl		; Matrixadresse
	ld	bc,mtxl		; 8 Pixelreihen
	ldir			; ins Video-RAM
	pop	de
	pop	bc
	ret
;
style:
	db	0		; ESCape Zeichen
strlen:
	db	0		; Laenge des Strings
xypos:
	dw	0		; Cursorposition
width:
	db	0		; Breite
hight:
	db	0		; Hoehe
space:
	db	0,0,0,0,0,0,0,0	; Leerzeichen
matrix1:
	db	0		; Kopie der Matrix beginnt (8 Bytes)
matrix2	equ	matrix1+mtxl	; Zweite Matrix beginnt
matrix3	equ	matrix2+mtxl	; Dritte Matrix beginnt
matrix4	equ	matrix3+mtxl	; Vierte Matrix beginnt
string	equ	matrix4+mtxl	; String beginnt

	.dephase

	end

