	title	Plot and Draw Driver
	name	('PLOTDRAW')

; This is the machine part of a BASIC-program written
; by Lawrence Simons,
; published in 8000 Plus, June 1989

; Disassembled by W.Cirsovius

lf600	equ	0f600h

ROLLER	equ	0b600h		; Start of roller RAM

MAX_X	equ	719
;
; Bank selections
;
_SCBNK	equ	81h		; Screen bank
_TPBNK	equ	85h		; TPA bank
;
; Bank I/O ports
;
_SEL1	equ	0f1h		; Select memory bank 4000H-7FFFH
_SEL2	equ	0f2h		; Select memory bank 8000H-BFFFH

;PLOT(X%,Y%,F%)		F300
;DRAW(X%,Y%,F%)		F303
;CIRCLE(X%,Y%,R%,F%)	F306

	.phase	0f300h

PLOT:
	jp	goPLOT
DRAW:
	jp	goDRAW
CIRCLE:
	jp	goCIRCLE
;
; RET codes:
; 0	Success
; 1	Plot pixel position out of range
; 2	Coordinates in draw line out of range
;
lf309:
	db	-1		; RET code
X0:
	dw	-1		; X0
Y0:
	dw	-1		; Y0
;
; Init plot
; ENTRY	Reg HL points to RET code address
; EXIT	Accu' holds setting:
;	1 Unplot
;	2 Plot
;	3 Toggle
;	Reg HL' points to status location
;
InitPlot:
	exx
	pop	bc		; Get caller
	ld	hl,0
	add	hl,sp		; Copy caller's stack
	ld	sp,lf600	; Set local stack
	push	hl		; Save caller's stack
	push	bc		; Bring back caller
	ld	hl,lf309	; Init return address
	exx
	ld	c,(hl)		; Get value of F%
	inc	hl
	ld	b,(hl)
	ld	a,b
	or	c		; Test zero - unplot
	jr	z,lf32d
	dec	bc		; Test one - plot
	ld	a,b
	or	c
	ld	a,2-1
	jr	z,lf32d
	ld	a,3-1		; Init toggle
lf32d:
	inc	a
	ex	af,af'
	ret
;
; PLOT(X%,Y%,F%)
;
goPLOT:
	push	hl
	pop	ix		; Copy address of X%
	push	de
	pop	iy		; Copy address of Y%
	ld	h,b		; Copy address of F%
	ld	l,c
	call	InitPlot	; Init plot
	call	doPlot		; Plot, unplot or toggle pixel
	pop	hl
	ld	sp,hl
	ret
;
; Plot, unplot or toggle pixel
;
doPlot:
	exx
	ld	(hl),1		; Init RET code
	exx
	ld	c,(ix+0)	; Fetch horizontal position
	ld	b,(ix+1)
	ld	e,(iy+0)	; Fetch vertical position
	ld	d,(iy+1)
	ld	(X0),bc		; Save X0
	ld	(Y0),de		; Save Y0
	ld	hl,MAX_X
	and	a
	sbc	hl,bc		; Test in horizontal range
	ret	c		; Nope
	ld	a,d
	and	a		; Test in vertical range
	scf
	ret	nz		; Nope
	exx
	ld	(hl),0		; Change RET code to success
	exx
	sub	e		; Build 256-vertical position
	dec	a
	ld	e,a		; Save it
	ex	de,hl
;
; Overlay video memory
;
	di
	ld	a,_SCBNK
	out	(_SEL1),a	; Select video bank
	inc	a
	out	(_SEL2),a
	add	hl,hl
	ld	de,ROLLER
	add	hl,de		; Position in roller RAM
	ld	e,(hl)		; Fetch address of current line
	inc	hl
	ld	d,(hl)
	ex	de,hl
	ld	a,l
	and	00000111b	; Mask bits
	ld	e,a
	add	hl,hl
	ld	a,l
	and	11110000b
	or	e
	ld	l,a
	ld	e,c
	ld	a,c
	and	11111000b
	ld	c,a
	add	hl,bc		; Build pixel address
	ld	a,e
	and	00000111b	; Extract bit position
	inc	a
	ld	b,a		; Set for count
	ld	a,00000001b	; Init bit position
lf394:
	rrca			; Position to right place
	djnz	lf394
	ex	af,af'
	ld	b,a		; Get pixel mode
	ex	af,af'
	djnz	lf3a1
	cpl			; Mask for unplot
	and	(hl)
	ld	(hl),a
	jr	lf3a9
lf3a1:
	djnz	lf3a7
	or	(hl)		; Set for plot
	ld	(hl),a
	jr	lf3a9
lf3a7:
	xor	(hl)		; Toggle bit
	ld	(hl),a
;
; Reset video memory
;
lf3a9:
	ld	a,_TPBNK
	out	(_SEL1),a	; Reset TPA bank
	inc	a
	out	(_SEL2),a
	ei
	and	a
	ret
;
; DRAW(X%,Y%,F%)
;
goDRAW:
	push	hl
	pop	ix		; Copy address of X%
	push	de
	pop	iy		; Copy address of Y%
	ld	h,b		; Copy address of F%
	ld	l,c
	call	InitPlot	; Init plot
	call	lf3c6		; Draw line to new coordinates
	exx
	ld	(hl),c		; Save resulting code
	pop	hl
	ld	sp,hl
	ret
;
; Draw line
; EXIT	Reg C' holds resulting code
;
lf3c6:
	ld	l,(ix+0)	; Load new horizontal point
	ld	h,(ix+1)
	ld	bc,(X0)		; Load last horizontal point X0
	and	a
	sbc	hl,bc		; Build difference
	ex	de,hl
	ld	l,(iy+0)	; Load new vertical point
	ld	h,(iy+1)
	ld	bc,(Y0)		; Load last vertical point Y0
	and	a
	sbc	hl,bc		; Build difference, too
	exx
	ld	c,2		; Init RET code
	exx
	ld	a,d
	or	e		; Verify not zero
	or	h
	or	l
	ret	z		; Error if so
	ld	c,1		; Init direction
	bit	7,d		; Test horizontal sign
	jr	z,lf3f9
	ld	c,-1		; Change direction
	ld	a,d
	cpl			; Negate horizontal difference
	ld	d,a
	ld	a,e
	cpl
	ld	e,a
	inc	de
lf3f9:
	ld	b,1		; Init direction
	bit	7,h		; Test vertical sign
	jr	z,lf408
	ld	b,-1		; Change direction
	ld	a,h
	cpl			; Negate vertical difference
	ld	h,a
	ld	a,l
	cpl
	ld	l,a
	inc	hl
;
; Directions calculated are in Regs B (vertical) or C (horizontal):
; +1 : Positive direction
; -1 : Negative direction
;
lf408:
	ld	(lf592),bc	; Save direction
	and	a
	sbc	hl,de		; Vertical - horizontal
	jr	c,lf41d
	add	hl,de
	ld	(lf596),de
	ld	(lf598),hl
	ld	c,0
	jr	lf427
lf41d:
	add	hl,de
	ld	(lf596),hl
	ld	(lf598),de
	ld	b,0
lf427:
	ld	(lf594),bc
	ld	hl,(lf598)
	ld	b,h
	ld	c,l
	srl	h
	rr	l
	exx
	ld	c,0		; Set success RET code
	exx
lf438:
	push	bc
	ld	bc,(lf596)
	add	hl,bc
	ld	bc,(lf598)
	and	a
	sbc	hl,bc
	jr	c,lf44d
	ld	de,(lf592)	; Get direction
	jr	lf452
lf44d:
	add	hl,bc
	ld	de,(lf594)
lf452:
	push	hl
	ld	hl,(X0)		; Load X0
	ld	c,e
	ld	a,e
	rla
	sbc	a,a
	ld	b,a
	add	hl,bc
	ld	(ix+0),l
	ld	(ix+1),h
	ld	hl,(Y0)		; Load Y0
	ld	c,d
	ld	a,d
	rla
	sbc	a,a
	ld	b,a
	add	hl,bc
	ld	(iy+0),l
	ld	(iy+1),h
	call	doPlot		; Plot, unplot or toggle pixel
	exx
	ld	a,(hl)
	or	c
	ld	c,a
	exx
	pop	hl
	pop	bc
	dec	bc
	ld	a,b
	or	c
	jr	nz,lf438
	ret
;
; CIRCLE(X%,Y%,R%,F%)
;
goCIRCLE:
	push	hl
	pop	ix		; Copy address of X%
	push	de
	pop	iy		; Copy address of Y%
	ld	h,b		; Copy address of R% and F%
	ld	l,c
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	ex	de,hl
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	ld	(lf59e),bc
	ex	de,hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	ex	de,hl
	call	InitPlot	; Init plot
	exx
	ld	(hl),4		; Init RET code
	exx
	ld	hl,(lf59e)
	ld	a,h
	and	a
	ret	nz
	exx
	inc	(hl)
	exx
	or	l
	ret	z
	call	lf572
	ld	(lf5a0),hl
	ld	hl,(lf59e)
	call	lf581
	ld	l,(iy+0)
	ld	h,(iy+1)
	ld	(lf59c),hl
	ld	(Y0),hl		; Save Y0
	ld	l,(ix+0)
	ld	h,(ix+1)
	ld	(lf59a),hl
	add	hl,bc
	ld	(X0),hl		; Save X0
	exx
	ld	b,0
	ld	e,1
	exx
lf4d8:
	call	lf4fa
	exx
	inc	e
	ld	a,e
	exx
	cp	5
	jr	c,lf4d8
	ld	hl,(lf59a)
	ld	(ix+0),l
	ld	(ix+1),h
	ld	hl,(lf59c)
	ld	(iy+0),l
	ld	(iy+1),h
	exx
	ld	(hl),b
	pop	hl
	ld	sp,hl
	ret
lf4fa:
	ld	a,(lf59e)
	ld	b,a
lf4fe:
	push	bc
	dec	b
	exx
	ld	a,e
	exx
	rrca
	jr	c,lf50b
	ld	a,(lf59e)
	sub	b
	ld	b,a
lf50b:
	ld	l,b
	ld	h,0
	push	hl
	call	lf572
	ex	de,hl
	ld	hl,(lf5a0)
	and	a
	sbc	hl,de
	call	lf55d
	ex	de,hl
	ld	hl,(lf59c)
	exx
	ld	a,e
	exx
	cp	3
	jr	nc,lf52a
	add	hl,de
	jr	lf52d
lf52a:
	and	a
	sbc	hl,de
lf52d:
	ld	(iy+0),l
	ld	(iy+1),h
	pop	hl
	call	lf581
	ld	hl,(lf59a)
	exx
	ld	a,e
	exx
	cp	1
	jr	z,lf54a
	cp	4
	jr	z,lf54a
	and	a
	sbc	hl,bc
	jr	lf54b
lf54a:
	add	hl,bc
lf54b:
	ld	(ix+0),l
	ld	(ix+1),h
	call	lf3c6		; Draw line
	exx
	ld	a,c
	or	b
	ld	b,a
	exx
	pop	bc
	djnz	lf4fe
	ret
;
;
;
lf55d:
	ld	a,h
	or	l
	ret	z
	ex	de,hl
	ld	hl,0
lf564:
	inc	hl
	push	hl
	call	lf572
	and	a
	sbc	hl,de
	pop	hl
	jr	c,lf564
	ret	z
	dec	hl
	ret
;
;
;
lf572:
	ld	h,l
	ld	l,0
	ld	c,h
	ld	b,l
	ld	a,8
lf579:
	add	hl,hl
	jr	nc,lf57d
	add	hl,bc
lf57d:
	dec	a
	jr	nz,lf579
	ret
;
;
;
lf581:
	ld	d,h
	ld	e,l
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,de
	srl	h
	rr	l
	srl	h
	rr	l
	ld	b,h
	ld	c,l
	ret
;
; Direction code
;
lf592:
	db	-1
	db	-1
lf594:
	rst	38h
	rst	38h
lf596:
	rst	38h
	rst	38h
lf598:
	rst	38h
	rst	38h
lf59a:
	rst	38h
	rst	38h
lf59c:
	rst	38h
	rst	38h
lf59e:
	rst	38h
	rst	38h
lf5a0:

	.dephase

	end

