************************************************************************
*							*
*		    Move Pointer				*
*							*
*		    Version 1.1				*
*							*
*		  31 August, 1988		 		*
*							*
************************************************************************

**	Tabstops : variable
**	Assemble with Macro68

;date 31-Aug-88 00:13
;move pointer

;Written by Dr J. Szajman
;38 Ludbrook Ave
;South Caulfield
;Melbourne, Vic.  3162
;Australia


**	Permission is granted to purchasers of Macro68 to modify this source
**	file.  Permission is further granted to distribute the source code that
**	they have modified subject to the following conditions:
**	1.  That the original, unaltered, source is included with the
**	    distribution.
**	2.  That no charge is made for the distribution, other than the cost of
**	    media.                    (Distribution via Fred Fish is permitted)
**	3.  That my copyright notice remains as part of this and any other
**	    file adapted from this file.


;Register usage:

;	d0 = scratch register
;	d1 = scratch register
;	d3 = 
;	d4 = 
;	d5 = 
;	d6 = 
;	d7 = 

;	a0 = scratch register
;	a1 = scratch register
;	a2 = ib_FirstScreen
;	a3 = pointer to MsgPort Structure
;	a4 = pointer to IOStdReq Structure
;	a5 = pointer to InputEvent Structure
;	a6 = pointer to _ExecBase
;	a7 = stack pointer


************  Exec Equates  **********************************************

	ifnd	_AbsExecBase
_AbsExecBase		equ	4	;exec library base address
_LVOAllocMem		equ	-$C6
_LVOFreeMem		equ	-$D2
_LVOAddHead		equ	-$F0
_LVOFindTask		equ	-$126
_LVOOldOpenLibrary	equ	-$198
_LVOCloseLibrary	equ	-$19E
_LVOOpenDevice		equ	-$1BC
_LVOCloseDevice		equ	-$1C2
_LVODoIO		equ	-$1C8


************  DOS Equates  ***********************************************

_LVODelay		equ	-$C6


******  Definitions of Constants  ****************************************

NT_MSGPORT		equ	4
NT_MESSAGE		equ	5
PA_SIGNAL		equ	0
MP_SIGTASK		equ	$10
LH_SIZE			equ	$1E
IND_WRITEEVENT		equ	$0B
IECLASS_RAWMOUSE	equ	2
IECODE_LBUTTON		equ	$68
IECODE_UP_PREFIX	equ	$80
IECODE_NOBUTTON		equ	$FF
IEQUALIFIER_RELATIVEMOUSE equ	$8000
ie_Code			equ	6
ie_X			equ	$0A
ie_Y			equ	$0C
	endc
**************************************************
	exeobj
	errfile	'ram:assem.output'
	objfile	'ram:m'
	strict
**************************************************


	SECTION	main,CODE

	movem.l	d2-d7/a0-a6,-(a7)
	movea.l	(_AbsExecBase),a6

	bsr.w	d_ASCII
	bne.w	exit1
	move.l	d1,d4	;x coord
	cmp.b	#' ',d0
	bne.w	exit1
	bsr.w	d_ASCII
	bne.w	exit1
	move.l	d1,d5	;y coord
	cmp.b	#$A,d0
	beq.b	start
	cmp.b	#' ',d0
	bne.w	exit1
	bsr.w	d_ASCII
	bne.w	exit1
	move.l	d1,d6	;k coord
	cmp.b	#$A,d0
	bne.w	exit1

start	lea	(MsgPort_s),a3	;*MsgPort struct
	lea	(IOStdReq_s),a4	;*IOStdReq struct
	lea	(InputEvent_s),a5	;*InputEvent stuct

	movea.l	(_IntuitionBase),a2
	bne.b	first_screen
	lea	(int_lib),a1
	clr.l	d0
	jsr	(_LVOOldOpenLibrary,a6)
	movea.l	d0,a1
	move.l	d0,(_IntuitionBase)	;save pointer
	exg	d0,a2
	jsr	(_LVOCloseLibrary,a6)
first_screen	movea.l	(60,a2),a2	;ib_FirstScreen

findtask	movea.l	#0,a1
	jsr	(_LVOFindTask,a6)	;find own task
	move.l	d0,(MP_SIGTASK,a3)	;fill structure
	lea	(msgportlist_s),a0

	move.l	a0,(a0)	;set up msg list structure
	addq.l	#4,(a0)	;LH_HEAD
	clr.l	(4,a0)	;LH_TAIL
	move.l	a0,(8,a0)	;LH_TAILPRED

	clr.l	d0		;unit 0
	clr.l	d1		;no flags
	lea	(device_name),a0
	movea.l	a4,a1	;*IOStdReq
	jsr	(_LVOOpenDevice,a6)	;open input.device
	tst.l	d0
	beq.b	set_position
	move.l	#100,d0
	bra.w	exit1

set_position	move.w	#IECODE_NOBUTTON,(ie_Code,a5)
	movea.l	a4,a1
	move.w	d4,(ie_X,a5)	;x coord
	move.w	d5,(ie_Y,a5)	;y coord
	move.l	($10,a2),d0	;current position
	sub.w	d0,(ie_X,a5)
	swap	d0
	asl.w	#1,d0
	sub.w	d0,(ie_Y,a5)
	jsr	(_LVODoIO,a6)	;do I/O
	move.l	d6,d3	;select left button?
	beq.b	exit2

	subq.l	#1,d3
	cmp.l	#2,d3
	bgt.b	exit2
loop	move.w	#IECODE_LBUTTON,(ie_Code,a5)
	clr.l	(ie_X,a5)	;clear (x,y)
	movea.l	a4,a1
	jsr	(_LVODoIO,a6)	;down button
	move.w	#IECODE_UP_PREFIX+IECODE_LBUTTON,(ie_Code,a5)
	movea.l	a4,a1
	jsr	(_LVODoIO,a6)	;release left button
	dbf	d3,loop

exit2	movea.l	a4,a1
	jsr	(_LVOCloseDevice,a6)
	clr.l	d0
exit1	movem.l	(a7)+,d2-d7/a0-a6
	rts




;Convert ASCII decimal numbers to HEX.
;entry-	a0 - pointer to decimal string
;exit -	d0 - delimeter
;	d1 - HEX number
;used -	d0,d1

d_ASCII	clr.l	d0
	clr.l	d1
d_ASCII_lp1	move.b	(a0)+,d0
	cmp.b	#' ',d0
	bgt.b	d_ASCII_jp1
	cmp.b	d0,d0	;set zero flag
	rts
d_ASCII_jp1	cmpi.b	#'0',d0
	blt.b	d_ASCII_exit
	cmpi.b	#'9',d0
	bgt.b	d_ASCII_exit
	mulu.w	#10,d1
	sub.l	#$30,d0	;strip ASCII
	add.l	d0,d1
	bra.b	d_ASCII_lp1
d_ASCII_exit	move.l	#5,d0	;error exit
	rts


*******************  Message Port Structure **************************

	cnop	0,2		;word align

MsgPort_s	dc.l	0		;LN_SUCC
	dc.l	0		;LN_PREDcd
	dc.b	NT_MSGPORT	;LN_TYPE
	dc.b	0		;LN_PRI
	dc.l	0		;*LN_NAME - 0 or pointer to 'name'
	dc.b	PA_SIGNAL	;MP_FLAGS
	dc.b	0		;MP_SIGBIT
	dc.l	0		;MP_SIGTASK
msgportlist_s	dcb.b	LH_SIZE,0	;list structure


*******************  InputEvent Structure ***************************

InputEvent_s	dc.l	0		;ie_NextEvent
	dc.b	IECLASS_RAWMOUSE	;ie_Class
	dc.b	0		;ie_SubClass
	dc.w	0		;ie_Code
	dc.w	IEQUALIFIER_RELATIVEMOUSE ;ie_Qualifier
	dc.w	0		;ie_X
	dc.w	0		;ie_Y

TimeStamp_s	dc.l	0		;TV_SECS
	dc.l	0			;TV_MICRO


*******************  IoStdReq Structure  *****************************

IOStdReq_s	dc.l	0		;ln_Succ
	dc.l	0		;ln_Pred
	dc.b	NT_MESSAGE	;ln_Type
	dc.b	0		;ln_Pri
	dc.l	0		;*ln_Name
	dc.l	MsgPort_s	;*mn_ReplyPort
	dc.w	0		;mn_Length

	dc.l	0		;*io_Device
	dc.l	0		;*io_unit
	dc.w	IND_WRITEEVENT;	;io_Command
	dc.b	0		;io_Flags
	dc.b	0		;io_error

	dc.l	0		;io_Actual
	dc.l	$16		;io_Length
	dc.l	InputEvent_s	;*io_Data
	dc.l	0		;io_Offset

*********************************************************************

_IntuitionBase	dc.l	0

device_name	dc.b	'input.device',0
int_lib	dc.b	'intuition.library',0

	end
