*-------------------------------------------------------*
*	16 bit colour index handling			*	
*-------------------------------------------------------*
	include		"..\..\vdi.inc"

NOVA		equ	1	; 1 - byte swap 16 bit colour value (NOVA etc)

red_bits	equ	5	; 5 for all normal 16 bit hardware
green_bits	equ	6	; 6 for Falcon TC and NOVA 16 bit, 5 for NOVA 15 bit
				; (I think 15 bit Falcon TC disregards the green LSB)
blue_bits	equ	5	; 5 for all normal 16 bit hardware


	xdef		set_colours
	xdef		_get_colour

	xref		_true_colour_mode


* Never called
* Just here to make sure no bad linking is done
dummy:
	move.w	_true_colour_mode,d0
	rts

* In:	d0		fore- and background colour index
*	a0		VDI struct
* Out:	d0		fore- and background colour values
_get_colour:
	movem.l		d1/a2,-(a7)
	and.l		#$00ff00ff,d0
	ifne	colour_struct_size!=16
	move.l		d0,d1
	mulu		#colour_struct_size,d0
	swap		d1
	mulu		#colour_struct_size,d1
	swap		d0
	move.w		d1,d0
	swap		d0
	else
	lsl.l		#4,d0
	endc

	move.l		vwk_palette(a0),d1
	bne		.local_palette
	move.l		vwk_real_address(a0),a2
	move.l		wk_screen_palette_colours(a2),d1
.local_palette:
	move.l		d1,a2
	move.w		colour_real(a2,d0.w),d0
	swap		d0
	move.w		colour_real(a2,d0.w),d0
	swap		d0
	movem.l		(a7)+,d1/a2
	rts


* In:	d0		number of entries and start entry
*	a0		VDI struct
*	a1		Requested colour values (3 word/entry)
*	a2		VDI palette array
* XXX:	d0-d3/a1-a2
set_colours:
	move.w		d0,d1
	swap		d0
	mulu		#colour_struct_size,d1
	add.w		d1,a2
	bra		.loop_end
.loop:
	move.w		(a1)+,d3
	move.w		d3,(a2)+
	move.w		d3,4(a2)		; Not at all correct
	mulu		#(1<<red_bits)-1,d3
	add.l		#500,d3
	divu		#1000,d3
	move.w		d3,d2
	lsl.w		#green_bits,d2

	move.w		(a1)+,d3
	move.w		d3,(a2)+
	move.w		d3,4(a2)
	mulu		#(1<<green_bits)-1,d3
	add.l		#500,d3
	divu		#1000,d3
;	add.w		d3,d3
	or.b		d3,d2
	lsl.w		#blue_bits,d2

	move.w		(a1)+,d3
	move.w		d3,(a2)+
	move.w		d3,4(a2)
	mulu		#(1<<blue_bits)-1,d3
	add.l		#500,d3
	divu		#1000,d3
	or.b		d3,d2

	ifne	NOVA
	rol.w		#8,d2
	endc

	move.w		d2,6(a2)
	add.w		#colour_struct_size-colour_hw,a2
.loop_end:
	dbra		d0,.loop

	rts

	end
