
		;   WAKEUP.ASM
		;

		section __MERGED,DATA

		xdef	_VTask
		xdef	_VMask

_VTask		dc.l	0
_VMask		dc.l	0

MiscRes 	dc.l	0
CIARes		dc.l	0
CIBRes		dc.l	0
MiscName	dc.b	'misc.resource',0
CIAName 	dc.b	'ciaa.resource',0
CIBName 	dc.b	'ciab.resource',0

		;   ICR interrupt

		section text,code

		xref	_SysBase
		xref	_LVOSignal
		xref	_LVOOpenResource
		xdef	_VInt

_VInt:		move.l	_SysBase,A6
		move.l	_VTask,A1
		move.l	_VMask,D0
		jsr	_LVOSignal(A6)
		moveq.l #1,D0		; clear interrupt.
		rts

		;   VAddICRVector(cia#, intnum, int)
		;   VRemICRVector(cia#, intnum, int)
		;
		;   cia# : 0 = ciaa, 1 = ciab

		xdef	_VAddICRVector
		xdef	_VRemICRVector
		xdef	_VSetICR
		xdef	_VAbleICRA	    ; enable/disable ints for CIAA.
		xref	_LVOAbleICR
		xref	_LVORemICRVector
		xref	_LVOAddICRVector
		xref	_LVOSetICR

_VAbleICRA:	move.l	A6,-(sp)
		move.l	CIARes,D0
		bne	.vaica10
		bsr	GetCIAResource	    ; D0 = 0, get resource for ciaa
.vaica10	move.l	D0,A6		    ; assume it works <sigh>.
		move.l	4+4(sp),D0          ; D0 = mask
		jsr	_LVOAbleICR(A6)
		move.l	(sp)+,A6
		rts

_VSetICR:
		move.l	A6,-(sp)
		move.l	4+4(sp),D0
		bsr	GetCIAResource
		tst.l	D0
		beq	.badres
		move.l	4+8(sp),D0      ; mask
		jsr	_LVOSetICR(A6)
		move.l	(sp)+,A6
		rts

_VAddICRVector:
		move.l	A6,-(sp)
		move.l	4+4(sp),D0
		bsr	GetCIAResource
		tst.l	D0
		beq	.badres
		move.l	4+8(sp),D0
		move.l	4+12(sp),A1
		jsr	_LVOAddICRVector(A6)
		move.l	(sp)+,A6
		rts

_VRemICRVector:
		move.l	A6,-(sp)
		move.l	4+4(sp),D0
		bsr	GetCIAResource
		tst.l	D0
		beq	.badres
		move.l	4+8(sp),D0
		move.l	4+12(sp),A1
		jsr	_LVORemICRVector(A6)
		move.l	(sp)+,A6
		rts

.badres 	moveq.l #-1,D0
		move.l	(sp)+,A6
		rts

		;   Retrieve CIAA (D0 = 0) or CIAB (D0 != 0) resource

GetCIAResource:
		move.l	_SysBase,A6
		tst.l	D0
		bne	.gcr100
		move.l	CIARes,D0
		bne	.gcrrts
		lea	CIAName,A1
		jsr	_LVOOpenResource(A6)
		move.l	D0,CIARes
		bra	.gcrrts

.gcr100 	move.l	CIBRes,D0
		bne	.gcrrts
		lea	CIBName,A1
		jsr	_LVOOpenResource(A6)
		move.l	D0,CIBRes

.gcrrts 	move.l	D0,A6
		rts


 ;
 ;  EXEC resource support
 ;			       4(sp)  8(sp)
 ;	AutoAllocMiscResource(resno, value)
 ;	    resno:  MR_SERIALPORT, SERIALBITS, PARALLELPORT, PARALLELBITS
 ;	    value:  -1 to allocate, 0 to check
 ;
 ;	    returns 0 on success
 ;
 ;	AutoFreeMiscResource(resno)
 ;			      4(sp)
 ;	    Free a misc resource you allocated
 ;
 ;	No need to open the misc.resource

		xdef	_AutoAllocMiscResource
		xdef	_AutoFreeMiscResource

_AutoAllocMiscResource:
		move.l	A6,-(sp)
		bsr	OpenMiscResource
		beq	amfail
		move.l	8(sp),D0
		move.l	12(sp),A1
		jsr	-6(A6)
		bra	amret
amfail		moveq.l #-1,D0
amret		move.l	(sp)+,A6
		rts

_AutoFreeMiscResource:
		move.l	A6,-(sp)
		bsr	OpenMiscResource
		beq	fmret
		move.l	8(sp),D0
		jsr	-12(A6)
fmret		move.l	(sp)+,A6
		rts

OpenMiscResource:
		move.l	MiscRes,D0
		bne	.omr10
		move.l	4,A6
		lea.l	MiscName,A1
		jsr	_LVOOpenResource(A6)
.omr10		move.l	D0,A6
		tst.l	D0
		rts

		END




