		section	a,code_c

		incdir	'work:Exsyslib/'
		include	'include/hardware/custom.i'	;define customchips
		include	'include/hardware/exsys_takeover.s' ;1st codeline!

		bsr.w	initcopper

		lea	text1,a1
		bsr.b	write20

loop		btst	#6,$bfe001
		bne.b	loop
		rts

screen_width	=	320/8	;screen width

font_spacing	=	20	;pixels
font_height	=	24	;pixels

write20		lea	font,a0
		lea	screen1,a2

read_position	moveq	#0,d0
		move.b	(a1)+,d0
		move.w	d0,x

		moveq	#0,d1
		move.b	(a1)+,d1
		move.w	d1,y

calc_dest	moveq	#0,d0
		move.w	x(pc),d0
		divu	#16,d0
		swap	d0
		move.w	d0,d2		; d2.w = xpos (byte position)
		swap	d0
		add.w	d0,d0
		ext.l	d0		; d0.w = xpos (remainder pixel offset)

		moveq	#0,d1
		move.w	y(pc),d1
		mulu	#screen_width*font_height,d1	; next position down screen
		add.w	d1,d0
		lea	(a2,d0.w),a5

read_ascii	moveq	#0,d3
		move.b	(a1)+,d3
		beq.s	wrtout
		cmp.w	#1,d3
		bne.b	noy

		add.w	#1,y
		bra.b	read_position

noy		cmp.b	#32,d3
		beq.b	next

find_font	sub.w	#65,d3
		cmp.b	#19,d3
		ble.b	nxt1
		add.w	d3,d3
		add.w	d3,d3
		lea	(a0,d3.w),a4
		lea	44*40(a4),a4
		bra.b	minterm

nxt1		cmp.b	#9,d3
		ble.b	nxt2
		add.w	d3,d3
		add.w	d3,d3
		lea	(a0,d3.w),a4
		lea	22*40(a4),a4
		bra.b	minterm

nxt2		add.w	d3,d3
		add.w	d3,d3
		lea.l	(a0,d3.w),a4

minterm		move.w	#%0000110111111100,d4	; minterm d= a + b
		ror.w	#4,d2
		or.w	d2,d4			; source 'a' shift value
		bsr.b	blit
next		add.w	#font_spacing,x
		bra.w	calc_dest

wrtout		rts


text1		dc.b	0,0,'TEST',1
		dc.b	0,1,'ABCDEFGHIJKLMNOP',1
		dc.b	0,2,'ABCDEFGHIJKLMNOP',0
		even

x		dc.w	0
y		dc.w	0

blit		btst	#6,$dff002
		btst	#6,$dff002
		bne.b	blit

		move.w	d4,$dff040		; bltcon0
		move.w	#0,$dff042		; bltcon1
		move.w	#$ffffffff,$dff044	; mask
		move.l	a4,$dff050		; a pointer
		move.l	a5,$dff04c		; b pointer
		move.l	a5,$dff054		; d pointer
		move.w	#[320-32]/8,$dff062	; b modulo
		move.w	#[320-32]/8,$dff064	; a modulo [320-32]/8
		move.w	#[320-32]/8,$dff066	; d modulo
		move.w	#[23*64+32/16],$dff058	; size
		lea	10240(a5),a5
		lea	3*23*40(a4),a4	

ready2		btst	#6,$dff002
		btst	#6,$dff002
		bne.b	ready2

		move.w	d4,$dff040		; bltcon0
		move.w	#0,$dff042		; bltcon1
		move.w	#$ffffffff,$dff044	; mask
		move.l	a4,$dff050		; a pointer
		move.l	a5,$dff04c		; b pointer
		move.l	a5,$dff054		; d pointer
		move.w	#[320-32]/8,$dff062	; b modulo
		move.w	#[320-32]/8,$dff064	; a modulo [320-32]/8
		move.w	#[320-32]/8,$dff066	; d modulo
		move.w	#[23*64+32/16],$dff058	; size
		rts


initcopper:	lea	copper(pc),a0
		bsr.s	newcopper

		lea     plane1,a0
		move.l  #screen1,d0
		move.w  d0,6(a0)
		swap    d0
		move.w  d0,2(a0)

		lea     plane2,a0
		move.l  #screen1+10240,d0
		move.w  d0,6(a0)
		swap    d0
		move.w  d0,2(a0)

		move.l	#copper,d0
		lea	adress,a0
		move.w	d0,6(a0)
		swap	d0
		move.w	d0,2(a0)
		rts
		
newcopper:	lea 	$dff000,a6		;a6=custom hardware
		move.w	#$7fff,$96(a6)		;no dma
		move.l	a0,$80(a6)		;install new copperlist ptr
		clr.w	$88(a6)			;strobe copper
		move.w	#$83c0,$96(a6)		;enable copper dma
		rts

copper		dc	$0106,$0000,$01fc,$0000
		dc	$008e,$2c81,$0090,$2cc1
		dc	$0092,$0038,$0094,$00d0
		dc	$0102,$0000,$0104,$0000
		dc	$0108,$0000,$010a,$0000

colour		dc	$0180,$0fff,$0182,$0000
		dc	$0184,$0686,$0186,$00b0
		dc	$0188,$080f,$018a,$0000
		dc	$018c,$0ff0,$018e,$0813

plane1		dc	$00e0,$0000,$00e2,$0000
plane2		dc	$00e4,$0000,$00e6,$0000
		dc	$3001,$ff00,$0100,$2200
		dc	$ffe1,$fffe,$2901,$ff00
		dc	$0100,$0200

adress		dc	$0084,$0000,$0086,$0000
		dc	$008a,$0000,$ffff,$fffe	; force restart

font		incbin	"ds:16x16_textwriter/greenfont2.bit"
screen1		ds.b	[320/8*512]
