
;------------------------------------------------------------------------
; Example of using some of the harlequin device driver functions.  This
; code does the following:
;    1. Opens the first available screen closest to 832x576
;    2. Clears the screen
;    3. Sets the FgPen to red
;    3. Draws a rectangle
;
; Mick Tinker  Nov 1990
; mtinker@cix.compulink.co.uk
;
;------------------------------------------------------------------------

; Assembled with DevPac 2

	incdir	"inc:"
	include	"exec/exec_lib.i"
	include	"exec/ports.i"
	include	"exec/lists.i"
	include	"devices/harlequin.i"
	include	"asmsupp.i"
			
	
main:
;
; Init msgport
;
	lea	portname,a0
	moveq	#0,d0
	jsr	CreatePort
	lea	ioreq,a0
	move.l	d0,MN_REPLYPORT(a0)
	beq	error
;
; Init ioreq and open device
;
	lea	ioreq,a1
	lea	devname,a0
	moveq	#0,d0
	moveq	#0,d1
	move.l	4.w,a6
	CALLEXEC	OpenDevice
	tst.l	d0
	bne	error2
;
; Open screen
;
	lea	newscreen,a0
	lea	ioreq,a1
	move.l	a0,io_Data(a1)
	move.w	#HQCMD_OPENSCREEN,IO_COMMAND(a1)
	CALLEXEC	DoIO
	tst.l	d0
	bne.s	openfail
;
; HClearScreen
;
	lea	ioreq,a1
	move.w	#HQCMD_CLEARSCREEN,IO_COMMAND(a1)
	CALLEXEC	DoIO	
;
; Ensure screen on and at front
;
	lea	ioreq,a1
	move.w	#HQCMD_SCREENFUNCTION,IO_COMMAND(a1)
	move.w	#SCREEN_ON|SCREEN_FRONT,io_Mask(a1)
	CALLEXEC	DoIO
;
; Fill Rect
;
	lea	ioreq,a1
	move.l	io_Screen(a1),a0
	move.l	#$ff000000,HS_FgPen(a0)
	move.w	#HQCMD_RECTFILL,IO_COMMAND(a1)
	move.w	#100,io_DestX(a1)
	move.w	#100,io_DestY(a1)
	move.w	#400,io_SizeX(a1)
	move.w	#400,io_SizeY(a1)
	CALLEXEC	DoIO
	
;	
; Close screen
;
	lea	ioreq,a1
	move.w	#HQCMD_CLOSESCREEN,IO_COMMAND(a1)
	CALLEXEC	DoIO	
;
; Close device
;	
openfail
	lea	ioreq,a1
	CALLEXEC	CloseDevice
error2
	lea	ioreq,a0
	move.l	MN_REPLYPORT(a0),a0
	jsr	DeletePort
error	rts	

ioreq		dcb.b	iohq_SIZE,0	
portname	dc.b	"test.port",0
devname		HQDEVNAME
newscreen	dc.w	0,0,832,576,HINTERLACE|HNEAREST,-1,0,0


;struct MsgPort *CreatePort(name,pri)
;	d0      =            a0  d0

CreatePort:
	move.l	a0,.name
	move.w	d0,.pri
	moveq.l	#-1,d0
	CALLEXEC	AllocSignal
	move.w	d0,.sigbitnumber
	cmp.w	#-1,d0
	bne	.skip4
	move.l	#0,d0
	rts
.skip4	move.l	#65537,d1
	move.l	#34,d0
	CALLEXEC	AllocMem
	move.l	d0,.msgport
	bne	.skip6
	move.l	#0,d0
	move.w	.sigbitnumber,d0
	CALLEXEC	FreeSignal
	move.l	#0,d0
	rts
.skip6	move.l	.msgport,a0
	move.l	.name,10(a0)
	move.b	.pri+1,9(a0)
	move.b	#4,8(a0)
	clr.b	14(a0)
	move.b	.sigbitnumber+1,15(a0)
	lea	0,a1
	CALLEXEC	FindTask
	move.l	.msgport,a0
	move.l	d0,16(a0)
	tst.l	.name
	beq	.skip7
	move.l	a0,a1
	CALLEXEC	AddPort
	bra	.skip8
.skip7	move.l	.msgport,a0
	lea	20(a0),a0
	NEWLIST	a0
.skip8	move.l	.msgport,d0
	rts

.sigbitnumber	ds.w	1
.msgport	ds.l	1
.name		ds.l	1
.pri		ds.w	1


; DeletePort(port)
;             a0
;-------------------------
DeletePort:
;-------------------------
	move.l	a0,.port
	tst.l	10(a0)
	beq	.skip4
	move.l	a0,a1
	CALLEXEC	RemPort
.skip4
	move.l	.port,a0
	move.b	#255,8(a0)
	move.l	#-1,20(a0)
	move.l	#0,d0
	move.b	15(a0),d0
	CALLEXEC	FreeSignal
	move.l	.port,a1
	move.l	#34,d0
	CALLEXEC	FreeMem
	rts

.port	ds.l	1
	
