From: "Detlef Mueller" <detlef@provi.de>
Newsgroups: comp.sys.hp48
Subject: Re: line.s
Date: Wed, 18 Nov 1998 22:42:17 +0100 (CET)
Organization: Ministry of Information Retrieval
Lines: 355
Distribution: World
Message-ID: <qrgyrscebivqr.f2ngyh1.pminews@news.hamburg.pop.de>
References: <slrn7565jd.ko.mozgy@danielle.mozgy.hr>
Reply-To: "Detlef Mueller" <I.h8.spam@dont.even.try>
NNTP-Posting-Host: dmueller.hh.provi.de
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Newsreader: PMINews 2.00.1205 For OS/2 (Demonstration)
Xref: republic.btigate.com comp.sys.hp48:18739

On 18 Nov 1998 18:47:51 GMT, Mario Mikocevic Mozgy wrote:

> Well,
> I appologize for all my abusive talk here before and I'll try
> to be productive now.
> 
> Since I'm now digging through all my very old HP48 archives
> (dated to 89 or 90) I'll be posting from time to time some
> interesting ML code here. Here's first ->
> 
> oh, btw, if it is huge, complain loud, if it's uninteresting,
> complain loud, if it's lousy and sloppy code definately complain.

Hey Mozgy -- here's my version. Competition is open, which one is
faster ? Anyone w/ an even faster/shorter version ?

Bye,
	Detlef

P.S.	Took two days of my life to write it... :-)


******************************************************************************
** Name:	Line - draw/clear/toggle line from x1,y1 to x2,y2 in a GROB
**
** Category:	GROB/Display
**
** Abstract:	Should replace internal limited LINE commands, uses std. DDA
**
** Entry:	(grob) #x1 #y1 #x2 #y2 on stack. #x1 must be < #x2.
**		RPL vars in CPU, P = 0
**
** Exit:	RPL vars in RAM/CPU, P = 0
**
** Error Exits: none
**
** Alters:	CPU - A, B, C, D, D0, D1, R0, R1, R2, R3, R4, P, CRY
**		RAM - (grob and) coordinates popped of data stack
**
** Calls:	=POP# =SAVPTR (=PopASavptr) =GETPTRLOOP
**
** Stk Levels:	1
**
** Notes:	Algorithm (in C syntax):
**
**		void line ( int x1, int y1, int x2, int y2 )
**		{
**		    int  ys = 1, dx, dy, e, c, f = FALSE ;
**
**		    if ( (dx = x2 - x1) < 0 )
**			return ;
**
**		    if ( (dy = y2 - y1) < 0 )
**			dy = -dy, ys = -ys ;
**
**		    if ( ! dx )
**			f = TRUE, c = dy, dx = 1, dy = 0 ;
**		    else
**		    if ( ! dy )
**			c = dx, dx = 1, dy = 0 ;
**		    else
**		    if ( dx == dy )
**			c = dx, dx = dy = 0 ;
**		    else
**		    {
**			if ( dy > dx )
**			    f = TRUE, e = dx, dx = dy, dy = e ;
**
**			c = dx ;
**		    }
**
**		    for ( e = dx >> 1 ; ; plot( x1, y1 ) )
**		    {
**			if ( --c < 0 )
**			    break ;
**
**			e += dy ;
**
**			if ( f )
**			{
**			    if ( e >= dx )
**				++x1, e -= dx ;
**
**			    y1 += ys ;
**			}
**			else
**			{
**			    if ( e >= dx )
**				y1 += ys, e -= dx ;
**
**			    ++x1 ;
**			}
**		    }
**		}
**
**		The routine works only on VDISP yet, assuming x-size = 131
**		(real 136 - 34 nibb/line).
**		!!! BANG TYPE !!!
**
** History:
**
**	Date		Prog	Modification
**	--------	----	----------------------------------------------
**	05/20/92	DM	Implemented as test for QIX - Q&D !!!
**	07/10/92	DM	add some doc ;-)
**	07/11/92	DM	review, add special cases, add cond. ass.
******************************************************************************

CODE

Toggle	EQU	1		\  Conditional assembly flags, set only
On	EQU	0		|- one of them to 1 at a time !
Off	EQU	0		/

sFlag	EQU	5

******************************************************************************
* CPU			A	B	C	D	D0	D1	P
*			    R0	    R1	    R2	    R3	    R4	    RSTK
*	Pop parameter, calculate and check dx
******************************************************************************

	GOSUB	Pop#	y2
	R3=A					    y2
	GOSUB	Pop#	x2
	R2=A				    x2
	GOSUB	Pop#	y1
	R1=A			    y1
	GOSUB	Pop#	x1
	R0=A		    x1
	GOSBVL	=PopASavptr  ->grob *1*

	D0=A						->grob

	A=R0		x1
	C=R2				x2
	C=C-A	A			dx = x2 - x1
	GONC	dxok
	GOTO	Exit	** Sorry, invalid order
dxok	R2=C				    dx

******************************************************************************
* CPU			xxx	xxx	xxx	xxx	->grob	xxx
*			    x1	    y1	    dx	    y2	    xxx
*	Calculate ystep, pointer to grob and initial bit mask
******************************************************************************

	D0=D0+	15	** Skip prlg, len, y		->x

	C=DAT0	A			x			*2*
	C=C+CON A,7	** Force byte boundary			*2*
	CSRB.F	A						*2*
	CSRB.F	A						*2*
	CSRB.F	A						*2*
	C=C+C	A			nibb/line		*2*

	D=C	A				ys
	D0=D0+	5	** Skip x			->pix
	A=R1		y1

*			** unsigned C *= A (org. HP)
	B=0	A		0
	BCEX	A	#	#'      0
Mul2	B=B-1	P
	GOC	Mul3
	C=C+A	A
	GOTO	Mul2
Mul3	ASL	A
	P=P+1
	?P#	5
	GOYES	Mul2
	P=	0

	A=R0		x1		y1 * nibb/line
	ASRB.F	A
	ASRB.F	A			/ 4
	A=A+C	A	offs = x1 / 4 + y1 * nibb/line

	CD0EX				->pix
	C=C+A	A			+= offs
	D0=C						->nibb
*			** Now some magic
	A=R0		x1
	LC(1)	3			3
	C=C&A	B			x1 % 4
	C=C+CON B,13			+= 13
	P=C	0
	LC(4)	#1248
	P=C	0
	C=P	1
	P=	0
	B=C	B		B:mask

    IFNE    Off
	B=-B-1	B		OFF mask
    ENDIF

******************************************************************************
* CPU			xxx	mask	xxx	ys	->1st nibb xxx
*			    xxx     y1	    dx	    y2	    xxx
*	Calculate dy, check for special cases dx = 0, dy = 0 or dx = dy
******************************************************************************

	A=R1		y1
	C=R3				y2
	C=C-A	A			dy = y2 - y1
	GONC	tstdxy
	D=-D	A				-ys
	C=-C	A			-dy

tstdxy	ST=0	sFlag
	A=R2		dx

	?C=A	A
	GOYES	dxeqdy

	?A=0	A
	GOYES	dxeq0	** dx = 0

	?C#0	A
	GOYES	dxgtdy? ** dy <> 0

************************** dy = 0, set up x steps
	D1=A							cnt = dx
	GONC	dx1dy0	** BET

************************** dx = 0, set up y steps
dxeq0	ST=1	sFlag
	D1=C							cnt = dy
dx1dy0	A=0	A
	A=A+1	A	dx = 1
	C=0	A			dy = 0
	GONC	dxdycok ** BET

************************** dx = dy, set up x&y steps
dxeqdy	D1=A							cnt = dx
	A=0	A	dx = 0
	C=0	A			dy = 0
	GOC	dxdycok ** BET


Exit	GOVLNG	=GETPTRLOOP

******************************************************************************
* CPU			dx	mask	dy	ys	->1st nibb xxx
*			    xxx     xxx     xxx     xxx     xxx
*	On normal case, check for dy >= dx, calculate e
******************************************************************************

dxgtdy? ?C<A	A
	GOYES	dxdyok

	ST=1	sFlag	** dy > dx, swap dx, dy
	ACEX	A	dy		dx

dxdyok	D1=A							cnt = dx

dxdycok R2=A				    dx
	R3=C					   dy

	ASRB.F	A	dx / 2
	R4=A						    e

******************************************************************************
* CPU			xxx	mask	xxx	ys	->pix	cnt	0
*			    xxx     xxx     dx	    dy	    e
*	The show begins...
******************************************************************************

plot	A=DAT0	1	** Read nibble

    IFNE    On
	A=A!B	B	** Set bit
    ENDIF
    IFNE    Off
	A=A&B	B	** Clear bit
    ENDIF
    IFNE    Toggle
	C=A	B
	A=A!B	B
	C=C&B	B
	A=A-C	B
    ENDIF

	DAT0=A	1	** Write nibble

	D1=D1-	1						--cnt
	GOC	Exit

	A=R4.F	A	e
	C=R3.F	A			dy
	A=A+C	A	e += dy
	R4=A.F	A					    e
	C=R2.F	A	dx

	?ST=1	sFlag
	GOYES	allwy

	?A<C	A
	GOYES	xstep	** e >= dx

	A=A-C	A			e -= dx
	R4=A.F	A					    e
	CD0EX				->pix
	C=C+D	A			->next row
	D0=C						->pix

xstep	B=B+B	B		mask <<= 1
    IFNE    (On)+(Toggle)
	GONC	plot
	LC(2)	#11			init. mask
    ENDIF
    IFNE    Off
	GOC	plot
	LC(2)	#EE
    ENDIF
	B=C	B		mask
	D0=D0+	1					->nxt nibb
	GONC	plot	** BET

allwy	?A<C	A
	GOYES	ystep	** e >= dx

	A=A-C	A			e -= dx
	R4=A.F	A					    e

	B=B+B	B		mask <<= 1
    IFNE    (On)+(Toggle)
	GONC	ystep
	LC(2)	#11			init. mask
    ENDIF
    IFNE    Off
	GOC	ystep
	LC(2)	#EE
    ENDIF
	B=C	B		mask
	D0=D0+	1					->nxt nibb

ystep	CD0EX				->pix
	C=C+D	A			->next row
	D0=C						->pix
	GOTO	plot

Pop#	GOVLNG	=POP#

ENDCODE

--
`Let's get some cards and play Magic'                     Detlef Mueller
 -- Gex                                               detlef (@) provi (.) de
                                                    http://www.provi.de/~detlef

         !!! ATTN !!! To reach my via e-mail, do _NOT_ auto-reply !!!

