**********************  MBPress.asm  ******************************

; by Steve Anderson on Jan 28, 1992
; using the Dvpac Assembler

; Purpose:
;  - little CLI command to detect mouse button presses for
;    gameport 1 (unit 0)

; Return:
;  - 0 for no button pressed
;  - 1 (or 001b) for lmb
;  - 2 (or 010b) for rmb
;  - 4 (or 100b) for mmb
;  - 5 (or 101b) for both lmb + mmb (OR the individual results)
;  - ...
;  - 20 or 21 for error conditions

; Linking:
;  blink MBPress.o lib lib:small.lib ND SC SD


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


	INCLUDE 	"exec/types.i"
	INCLUDE 	"exec/exec_lib.i"
	INCLUDE 	"hardware/cia.i"
	INCLUDE 	"hardware/custom.i"
	INCLUDE 	"resources/potgo_lib.i"


	XREF	_ciaa
	XREF	_custom
	XREF	_potinp


SETBITS	equ	$FFFF		;which POTGO bits to set (masked later)
UNSETBITS	equ	$0000		;which POTGO bits to clear
LBIT	equ	(1<<0)		;bit mask for Lmb return value
RBIT	equ	(1<<1)		;bit mask for Rmb return value
MBIT	equ	(1<<2)		;bit mask for Mmb return value
OUTLY	equ	(1<<11)		;bit mask for Rmb enable (port 1, unit 0)
DATLY	equ	(1<<10)		;bit mask for Rmb data
OUTLX	equ	(1<<9)		;bit mask for Mmb enable
DATLX	equ	(1<<8)		;bit mask for Mmb data
DATLY_POS	equ	10		;bit position for Rmb data
DATLX_POS	equ	8		;bit position for Mmb data
RES_FAIL	equ	20		;return value if can't open resource	
ALOC_FAIL	equ	21		;return value if can't allocate bits


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


Start:	movem.l	d2-d7/a2-a6,-(sp)	;save registers per convention
	moveq.l	#0,d0		;clear d0 before using
	moveq.l	#0,d1		;clear d1 before using
	moveq.l	#0,d2		;clear d2, used to store return value

Prep1:	lea.l	_ciaa,a4		;base address of CIAA
	move.b	ciapra(a4),d1	;move CIAAPRA bits into d1

Lmb:	btst	#CIAB_GAMEPORT0,d1	;check left button
	bne.s	Prep2		;if not pressed, continue checking
	or.b	#LBIT,d2		;if pressed, set bit 0 of return value

Prep2:	lea.l	PotgoName(pc),a1	;load a1 with resource name string
	CALLEXEC	OpenResource	;open 'potgo.resource'
	move.l	d0,d7		;stash resource base
	bne.s	Prep3		;resource ok, so continue preps
	moveq.l	#RES_FAIL,d2	;return error value
	bra	Done		;halt prematurely

Prep3:	exg.l	d7,a6		;put resource base in a6
	moveq.l	#0,d0		;clear d0
	or.w	#OUTLY,d0		;or OUTLY bit
	or.w	#DATLY,d0		;or DATLY bit
	or.w	#OUTLX,d0		;or OUTLX bit
	or.w	#DATLX,d0		;or DATLX bit
	jsr	_LVOAllocPotBits(a6);call resource preparation function
	tst.l	d0		;check whether bits allocated
	beq.s	Prep4		;bits allocated, so continue preps
	jsr	_LVOFreePotBits(a6)	;free any bits that did allocate
	moveq.l	#ALOC_FAIL,d2	;return error value
	bra	Done		;halt prematurely
	
Prep4:	move.l	d0,d1		;move bit mask to proper register
	move.l	#SETBITS,d0	;prepare to set all masked potgo bits
	jsr	_LVOWritePotgo(a6)	;set chosen potgo bits			
	lea.l	_potinp,a4	;address of POTINP register
	move.w	(a4),d0		;move current POTbits to d0 for testing

Rmb:	btst.l	#DATLY_POS,d0	;test POTINP with bit DATLY_POS
	bne.s	Mmb		;if not pressed, continue checking
	or.b	#RBIT,d2		;if pressed, set bit 1 of return value
		
Mmb:	btst.l	#DATLX_POS,d0	;test POTINP with bit DATLX_POS
	bne.s	Unprep		;if not pressed, let's cleanup and exit
	or.b	#MBIT,d2		;if pressed, set bit 2 of return value

Unprep:	move.l	#UNSETBITS,d0	;prepare to clear all masked potgo bits
	jsr	_LVOWritePotgo(a6)	;clear chosen potgo bits
	move.l	d1,d0		;move bit mask to proper register
	jsr	_LVOFreePotBits(a6)	;free any allocated potgo bits

Done:	move.l	d2,d0		;move return value to d0 for CLI
	movem.l	(sp)+,d2-d7/a2-a6	;restore the registers we saved
	rts			;this takes us back to DOS


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


vers:	dc.b	0,'$VER: MBPress 1.0',0
PotgoName:
	dc.b	'potgo.resource',0
	ds.w	0
	
	END

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