	title	Plot Driver
	name	('PLOT')

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

; Disassembled by W.Cirsovius

; Program will be called from BASIC by PLOT (X%,Y%,F%)

; Where
; X% is the column
; Y% is the line
; F% is the paint flag

XBIOS	equ	0fc5ah
ROLLER	equ	0b600h

SCR_RUN	equ	00e9h

MAX_X	equ	719

	.phase	0c000h

	ld	a,(bc)		; Fetch F%
	ld	(PntFlg),a
	ld	c,(hl)		; Fetch X%
	inc	hl
	ld	b,(hl)
	ld	hl,MAX_X
	and	a
	sbc	hl,bc		; Test in range 0..719
	ret	c
	ld	(XPOS),bc
	ex	de,hl
	ld	e,(hl)		; Fetch Y%
	inc	hl
	ld	d,(hl)
	ld	a,d
	and	a		; Test in range 0..255
	ret	nz
	sub	e		; Negate Y%
	dec	a
	ld	e,a
	ld	(YPOS),de
	di
	ld	(UsrStk),sp	; Save caller's stack
	ld	sp,LocStk	; Get local stack
	ld	bc,Plot
	call	XBIOS		; Execute function
	dw	SCR_RUN
	ld	sp,(UsrStk)	; Get back caller's stack
	ei
	ret
;
; Execute draw task
;
Plot:
	ld	hl,(YPOS)	; Get Y%
	add	hl,hl		; Times two
	ld	de,ROLLER
	add	hl,de		; Position in roller RAM
	ld	e,(hl)		; Get value from roller
	inc	hl
	ld	d,(hl)
	ld	a,e		; Build real address
	and	11111000b
	sla	a
	rl	d
	ld	b,a
	ld	a,e
	and	00000111b
	or	b
	ld	e,a
	ld	hl,(XPOS)	; Get X%
	ld	b,l
	ld	a,l
	and	11111000b
	ld	l,a
	ex	de,hl
	add	hl,de		; Position within line
	ld	a,b
	and	00000111b
	inc	a		; Get count
	ld	b,a
	xor	a		; Init bit
	scf
PosBit:
	rra			; Get bit position
	djnz	PosBit
	ld	b,a
	ld	a,(PntFlg)	; Get F%
	and	a
	jr	nz,InsertBit?	; Not erase
	ld	a,b		; Get bit
	cpl			; Negate it
	and	(hl)		; Mask
	ld	(hl),a		; Reset bit
	ret
InsertBit?:
	dec	a
	ld	a,b		; Get bit
	jr	nz,ToggleBit
	or	(hl)		; Insert it
	ld	(hl),a
	ret
ToggleBit:
	xor	(hl)		; Toggle bit
	ld	(hl),a
	ret
;
XPOS	equ	$
YPOS	equ	XPOS+2
PntFlg	equ	YPOS+2
UsrStk	equ	PntFlg+1
LocStk	equ	UsrStk+2+30

	.dephase

	end

