*-------------------------------------------------------*
*	fVDI->driver interface (C functions)		*	
*-------------------------------------------------------*

	include		"..\vdi.inc"


	xdef		_c_line
	xdef		_c_set_pixel
	xdef		_c_get_pixel
	xdef		_c_expand
	xdef		_c_fill
	xdef		_c_blit
	xdef		_c_text
	xdef		_c_set_palette
	xdef		_c_initialize_palette

	xref		_c_line_draw,_c_write_pixel,_c_read_pixel,_c_expand_area
	xref		_c_fill_area,_c_blit_area,_c_text_area,_c_set_colours


	dc.b		"set_pixel",0
*---------
* Set a coloured pixel
* c_write_pixel(Virtual *vwk, MFDB *mfdb, long x, long y, long colour)
* In:	a0	VDI struct, destination MFDB
*	d0	pixel colour
*	d1	x
*	d2	y
*---------
_c_set_pixel:
c_set_pixel:
	movem.l		d0-d2/a0-a2,-(sp)

	ext.l		d1
	ext.l		d2
	move.l		d0,-(a7)
	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		4(a0),-(a7)
	move.l		(a0),-(a7)
	jsr		_c_write_pixel
	add.w		#20,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"get_pixel",0
*---------
* Get a coloured pixel
* c_read_pixel(Virtual *vwk, MFDB *mfdb, long x, long y)
* In:	a0	VDI struct, source MFDB
*	d1	x
*	d2	y
* Out:	d0	pixel colour
*---------
_c_get_pixel:
c_get_pixel:
	movem.l		d1-d2/a0-a2,-(sp)

	ext.l		d1
	ext.l		d2
	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		4(a0),-(a7)
	move.l		(a0),-(a7)
	jsr		_c_read_pixel
	add.w		#16,a7

	movem.l		(sp)+,d1-d2/a0-a2
	rts


	dc.b		"line"
*---------
* Draw a colored line between 2 points
* c_draw_line(Virtual *vwk, long x1, long y1, long x2, long y2, long colour)
* In:	a0	VDI struct
*	d0	line colour
*	d1	x1
*	d2	y1
*	d3	x2
*	d4	y2
*---------
_c_line:
c_line:
	movem.l		d0-d2/a0-a2,-(sp)

	ext.l		d1
	ext.l		d2
	move.l		d0,-(a7)
	move.w		d4,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d3,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		a0,-(a7)
	jsr		_c_line_draw
	add.w		#24,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"expand"
*---------
* Expand a monochrome area to multiple bitplanes
* c_expand_area(Virtual *vwk, MFDB *src, long src_x, long src_y, MFDB *dst, long dst_x, long dst_y, long w, long h, long operation, long colour)
* In:	a0	VDI struct, destination MFDB, VDI struct, source MFDB
*	d0	colours
*	d1-d2	x1,y1 source
*	d3-d6	x1,y1 x2,y2 destination
*	d7	logic operation
*---------
_c_expand:
c_expand:
	movem.l		d0-d2/a0-a2,-(a7)

	ext.l		d1
	ext.l		d2
	move.l		d0,-(a7)
	ext.l		d7
	move.l		d7,-(a7)

	move.w		d6,d0
	sub.w		d4,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d5,d0
	sub.w		d3,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)

	move.w		d4,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d3,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.l		4(a0),-(a7)
	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		12(a0),-(a7)
	move.l		(a0),-(a7)

	jsr		_c_expand_area
	add.w		#44,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"fill"
*---------
* Fill a multiple bitplane area using a monochrome pattern
* c_fill_area(Virtual *vwk, long x, long y, long w, long h, short *pattern, long colour)
* In:	a0	VDI struct, destination MFDB, VDI struct, source MFDB
*	d0	colours
*	d1-d4	x1,y1 x2,y2 destination
*	d5	pattern address
*---------
_c_fill:
c_fill:
	movem.l		d0-d2/a0-a2,-(sp)

	ext.l		d1
	ext.l		d2
	move.l		d0,-(a7)
	move.l		d5,-(a7)

	move.w		d4,d0
	sub.w		d2,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d3,d0
	sub.w		d1,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)

	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		a0,-(a7)

	jsr		_c_fill_area
	add.w		#28,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"blit"
*---------
* Blit an area
* c_blit_area(Virtual *vwk, MFDB *src, long src_x, long src_y, MFDB *dst, long dst_x, long dst_y, long w, long h, long operation)
* In:	a0	VDI struct, destination MFDB, VDI struct, source MFDB
*	d0	logic operation
*	d1-d2	x1,y1 source
*	d3-d6	x1,y1 x2,y2 destination
*---------
_c_blit:
c_blit:
	movem.l		d0-d2/a0-a2,-(sp)

	ext.l		d1
	ext.l		d2
	ext.l		d0
	move.l		d0,-(a7)

	move.w		d6,d0
	sub.w		d4,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d5,d0
	sub.w		d3,d0
	addq.w		#1,d0
	ext.l		d0
	move.l		d0,-(a7)

	move.w		d4,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.w		d3,d0
	ext.l		d0
	move.l		d0,-(a7)
	move.l		4(a0),-(a7)
	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		12(a0),-(a7)
	move.l		(a0),-(a7)

	jsr		_c_blit_area
	add.w		#40,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"text"
*---------
* Draw some text
* c_text_area(Virtual *vwk, short *text, long length, long dst_x, long dst_y)
* In:	a0	VDI struct
*	a1	string address
*	d0	string length
*	d1	x1,y1 destination
*---------
_c_text:
c_text:
	movem.l		d2,-(sp)

	ext.l		d0
	move.w		d1,d2
	swap		d1
	ext.l		d1
	ext.l		d2

	move.l		d2,-(a7)
	move.l		d1,-(a7)
	move.l		d0,-(a7)
	move.l		a1,-(a7)
	move.l		a0,-(a7)

	jsr		_c_text_area
	add.w		#20,a7

	movem.l		(sp)+,d2
	rts


	dc.b		"set_palette",0
*---------
* Set palette colours
* c_set_colours(Virtual *vwk, long start, long entries, short requested[3][], Colour palette[])
* In:	a0	VDI struct
*	d0	number of entries, start entry
*	a1	requested colour values (3 word/entry)
*	a2	colour palette
*---------
_c_set_palette:
c_set_palette:
	movem.l		d0-d2/a0-a2,-(sp)

	move.l		a2,-(a7)
	move.l		a1,-(a7)
	move.l		d0,d1
	swap		d1
	ext.l		d1
	move.l		d1,-(a7)
	ext.l		d0
	move.l		d0,-(a7)
	move.l		a0,-(a7)

	jsr		_c_set_colours
	add.w		#20,a7

	movem.l		(sp)+,d0-d2/a0-a2
	rts


	dc.b		"initialize_palette"
*---------
* Set palette colours
* initialize_palette(Virtual *vwk, long start, long entries, short requested[][3], Colour palette[])
* To be called from C
*---------
_c_initialize_palette:
	jmp		_c_set_colours		; Exactly the same parameters

	end
