*********************************************************************
*
* Your init code comes here.
*
*********************************************************************

;****************************************************************************
;**
;*       Speed-optimized chunky circledraw routine, B
;*
;*  This is one of my contributions to #amycoders circledrawing-compo. This
;*  routine will participate in speed-optimizing competition. This code works
;*  with 68020+ processors, with any cache setup.
;*
;*  This is a simple bresenham circle algorithm implementation with
;*  precalculation for points of all circle sizes.
;*
;*  Code size without rts: 286 bytes. ~520K buffer
;*  Average time (dec): fast (030@59.220MHz)
;*
;*  Coded by Harry "Piru" Sintonen <sintonen@jyu.fi>.
;*  This code is copyright © 1997 by Harry Sintonen, and was written 24th
;*  september & 11th october 1997.
;*
;****************************************************************************
;*

	CNOP	0,4
_initcode
a

MAXRAD	EQU	128
CITEMS	EQU	(MAXRAD-0)*8*4	(maxrad-minrad)*8*sizeof(long)
NUMCIRCLES	EQU	128

	move.l	#offsettable,d0
	clr.b	d0
	move.l	d0,offsettablep
	move.l	d0,a0

	moveq	#0,d7

.loop	move.l	d7,d2
	movem.l	d7/a0,-(sp)
	bsr	makecircle
	movem.l	(sp)+,d7/a0
	lea	(CITEMS,a0),a0
	addq.l	#1,d7
	cmp.w	#NUMCIRCLES,d7		NUMCIRCLES circles
	bls.b	.loop

	rts


	CNOP	0,4
makecircle

;  IN: d2.l=radius
;      a0=offsettable for this circle
; OUT: circle :) d2/d4-d7/a0-a1 trashed

	tst.l	d2
	bne.b	.not_zerop

	moveq	#0,d0
	moveq	#9-1,d1
.pz	move.l	d0,(a0)+
	dbf	d1,.pz
	rts
.not_zerop
	move.l	d2,d5		2	y=r (NOTE: .l!)
	add.l	d2,d2		2
	subq.l	#3,d2		2
	neg.l	d2		2	d=3-(2*r)
	moveq	#0,d4		2	x=0

	move.l	a0,a1
	moveq	#-1,d0
	move.l	d0,(a0)+

	moveq	#0,d7			oldoffs
	sub.l	a2,a2

.loop	addq.l	#1,(a1)

	move.l	d5,d6		2
	lsl.l	#8,d6		2
	add.l	d4,d6		2
	move.l	d6,a2
	sub.l	d7,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d5,d6		2
	lsl.l	#8,d6		2
	neg.l	d6		2
	add.l	d4,d6		2
	move.l	d6,d7
	sub.l	a2,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d5,d6		2
	lsl.l	#8,d6		2
	sub.l	d4,d6		2
	move.l	d6,a2
	sub.l	d7,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d5,d6		2
	lsl.l	#8,d6		2
	neg.l	d6		2
	sub.l	d4,d6		2
	move.l	d6,d7
	sub.l	a2,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4

	move.l	d4,d6		2
	lsl.l	#8,d6		2
	add.l	d5,d6		2
	move.l	d6,a2
	sub.l	d7,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d4,d6		2
	lsl.l	#8,d6		2
	neg.l	d6		2
	add.l	d5,d6		2
	move.l	d6,d7
	sub.l	a2,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d4,d6		2
	lsl.l	#8,d6		2
	sub.l	d5,d6		2
	move.l	d6,a2
	sub.l	d7,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4
	move.l	d4,d6		2
	lsl.l	#8,d6		2
	neg.l	d6		2
	sub.l	d5,d6		2
	move.l	d6,d7
	sub.l	a2,d6
	move.l	d6,(a0)+
	;move.b	d3,0(a0,d6.l)	4

	tst.l	d2		2
	bpl.b	.skipa		2

	move.l	d4,d6		2
	lsl.l	#2,d6		2
	addq.l	#6,d6		2	
	add.l	d6,d2		2	d=d+(4*x)+6
	bra.b	.skipb		2
.skipa
	move.l	d4,d6		2
	sub.l	d5,d6		2
	add.l	d6,d6		2
	addq.l	#5,d6		2
	add.l	d6,d6		2
	add.l	d6,d2		2	d=d+4*(x-y)+10
	subq.l	#1,d5		2	y=y-1

.skipb	addq.l	#1,d4		2	x=x+1
	cmp.l	d4,d5		2
	bhs	.loop		2
b
	rts
		
	
*********************************************************************
*
* Your line routine.
* Every time you are given: A0.L : Chunky Screen (256x256x8)
*
* d0.l/d1.l  x/y
* d2.l       radius
* d3.b       color
*
* other registers are in undefined state !!
* you may trash all regs except a7
*
*********************************************************************

	CNOP	0,8
; following is my quick "nop" longword align macro ;)
mcnop4tempb	; this must be longword aligned!
mcnop4	MACRO
mcnop4temps	SET	(*-mcnop4tempb)&2
	IFGT	(mcnop4temps)
	moveq	#0,d2
	ENDC
	ENDM
_circle
c

;  IN: d0.l=xc, d1.l=yc, d2.l=radius, d3.b=color
;      a0=chunky buffer (256x256x8)
; OUT: circle :) d0-d2/a0-a1 trashed

	add.l	d2,d2			d2=r*2
	lsl.l	#8,d1			offs=yc*256
	lsl.l	#8,d2			d2=r*CITEMS/16 (=256)
	lea	$BADF00D,a1
offsettablep	EQU	*-4
	add.l	d0,d1			offs=yc*256+xc
	lea	0(a1,d2.l*8),a1		a1=offsettable+r*CITEMS
	add.l	d1,a0			a0=circle center

	move.l	(a1)+,d0		get number of offset octets

	mcnop4
.loop	add.l	(a1)+,a0		add offs 1
	move.b	d3,(a0)			put pixel 1
	add.l	(a1)+,a0		add offs 2
	move.b	d3,(a0)			put pixel 2
	add.l	(a1)+,a0		add offs 3
	move.b	d3,(a0)			put pixel 3
	add.l	(a1)+,a0		add offs 4
	move.b	d3,(a0)			put pixel 4
	add.l	(a1)+,a0		add offs 5
	move.b	d3,(a0)			put pixel 5
	add.l	(a1)+,a0		add offs 6
	move.b	d3,(a0)			put pixel 6
	add.l	(a1)+,a0		add offs 7
	move.b	d3,(a0)			put pixel 7
	add.l	(a1)+,a0		add offs 8
	move.b	d3,(a0)			put pixel 8
	dbf	d0,.loop		loop d0 times

d
	rts



;	Comment this out on Asmone for auto length-output

;	printt	"Length of your code:"
;	printv	((b-a)+(d-c))

;len	dc.l	((b-a)+(d-c))


**********************************************************************
*
* Put your tables here
*
**********************************************************************
	section yourbss,bss


	ds.b	256
offsettable	ds.b	CITEMS*(NUMCIRCLES+1)

