;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Jaguar Example Source Code
; Jaguar Workshop Series #1
; Copyright (c)1994 Atari Corp.
; ALL RIGHTS RESERVED
;
; Program: hv.cof      - Minimum time object list update
;  Module: hv_list.s   - Object List Refresh and Initialization
;
		.include        "jaguar.inc"
		.include        "hv.inc"

		.globl		InitLister
		.globl		UpdateList

		.extern		InitMoveVars

		.extern		main_obj_list
		.extern		bra2
		.extern		bm1
		.extern		bm2
		.extern		gpu1
		.extern		stop1
		.extern		bmp_highl1
		.extern		bmp_lowl1
		.extern		bmp_height2
		.extern		bmp_highl2
		.extern		bmp_lowl2
		.extern		scl_lowl2
		.extern		reflect1
		.extern		count_x1
		.extern		count_y1
		.extern		x_motion1
		.extern		y_motion1
		.extern		x_pos1
		.extern		y_pos1
		.extern		x_min1
		.extern		x_max1
		.extern		y_min1
		.extern		y_max1
		.extern		upd_freqx1
		.extern		upd_freqy1
		.extern		reflect2
		.extern		count_x2
		.extern		count_y2
		.extern		x_motion2
		.extern		y_motion2
		.extern		x_pos2
		.extern		y_pos2
		.extern		x_min2
		.extern		x_max2
		.extern		y_min2
		.extern		y_max2
		.extern		upd_freqx2
		.extern		upd_freqy2
		.extern		bmp_hscl2
		.extern		bmp_vscl2
		.extern		img_adr1
		.extern		img_adr2
		.extern		a_hdb
		.extern		a_hde
		.extern		a_vdb
		.extern		a_vde
		.extern		width
		.extern		height

		.extern		JAGPIC
		.extern		chopper
		.extern		stillife
		.extern		image1
		.extern		image2
		.extern		image3

		.text

CopyPicture:
        movem.l d0/a0-a1, -(sp)

				lea.l JAGPIC, a0
        lea.l image1, a1
        move.w #(BMP_PHRASES*2*BMP_HEIGHT-1), d0
.loop1:
				move.l (a0)+, (a1)+
        dbra d0, .loop1

				lea.l stillife, a0
        lea.l image2, a1
        move.w #(BMP_PHRASES*2*BMP_HEIGHT-1), d0
.loop2:
				move.l (a0)+, (a1)+
        dbra d0, .loop2

				lea.l chopper, a0
        lea.l image3, a1
        move.w #(BMP_PHRASES*2*BMP_HEIGHT-1), d0
.loop3:
				move.l (a0)+, (a1)+
        dbra d0, .loop3

        movem.l (sp)+, d0/a0-a1
        rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; InitLister: Initialize Object List Processor List
;
; Returns: Pre-word-swapped address of object list in d0.l
;
InitLister:
		movem.l d1-d7/a0-a2,-(sp)

		jsr CopyPicture

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write first BRANCH object (branch if YPOS > a_vde )

		move.l	#main_obj_list,a0
		move.l	#BRANCHOBJ,d0
		move.l	#stop1,d2
		move.w	a_vde,bra_ypo
		move.l	#O_BRLT,bra_con
		jsr			build_obj			; Build a branch object at main_obj_list

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write second BRANCH object (branch if YPOS < a_vdb)   

		move.l	#bra2,a0
		move.l	#BRANCHOBJ,d0
		move.l	#stop1,d2
		move.w	a_vdb,bra_ypo
		move.l	#O_BRGT,bra_con
		jsr			build_obj			; Build a branch object at bra2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write first BITMAP object

		move.l	#bm1,a0
		move.l	#bmp_highl1,a1
		move.l	#bmp_lowl1,a2

		move.l	#O_TRANS,bmp_bit
		move.l	#BMP_PHRASES,bmp_phr

		move.l	#BITOBJ,d0
		move.l	#bm2,d2
		move.w	#150,d3
		move.w	#150,x_pos1
		move.w	#150,d4
		move.w	#150,y_pos1
		move.l	#BMP_HEIGHT,d5
		move.l	#image1,d6
		move.l	#image1,img_adr1
		move.l	#O_DEPTH16|O_NOGAP,d7
		jsr			build_obj			; Build a bitmap object at bm1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write a SCALED BITMAP object

		move.l	#bm2,a0
		move.l	#bmp_highl2,a1
		move.l	#bmp_lowl2,a2
		move.l	#scl_lowl2,a3

		move.l	#O_TRANS,bmp_bit
		move.l	#BMP_PHRASES,bmp_phr

		move.l	#SCBITOBJ,d0
		move.l	#gpu1,d2
		move.w	#180,d3
		move.w	#180,x_pos2
		move.w	#180,d4
		move.w	#180,y_pos2
		move.w	#$0020,scl_hsc
		move.w	#$0020,bmp_hscl2
		move.w	#$0020,scl_vsc
		move.w	#$0020,bmp_vscl2
		move.l	#BMP_HEIGHT-1,d5
		move.l	#image2,d6
		move.l	#image2,img_adr2
		move.l	#O_DEPTH16|O_NOGAP,d7
		jsr			build_obj			; Build a scaled bitmap object at bm2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write a GPU Interrupt Object

		move.l	#gpu1,a0
		move.l	#GPUOBJ,d0
		jsr			build_obj			; Build a gpu object at gpu1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write a STOP object at end of list

		move.l	#stop1,a0
		move.l	#STOPOBJ,d0
		jsr			build_obj			; Build a stop object at stop1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Now return swapped list pointer in D0                      

		move.l	#main_obj_list,d0  
		swap		d0

		movem.l	(sp)+,d1-d7/a0-a2
		rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; UpdateList: Update list fields destroyed by the object processor.
;
UpdateList:
		movem.l	d0-d2/a0,-(sp)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 1. Bitmap
;
		move.l	#bm1,a0


		move.l	bmp_highl1,d1		; Grab low longword of phrase
		andi.l	#$00000FFF,d1		; Strip old adress of picture
		move.l	img_adr1,d2			; Adress of picture
		lsl.l		#8,d2
		or.l		d2,d1
		move.l	d1,(a0)					; Store high longword of phrase 1

		move.l	bmp_lowl1,d0		; Grab low longword of phrase
		andi.l	#$FFFFC007,d0		; Strip old YPOS
		clr.l		d1
		move.w	y_pos1,d1				; Update YPOS from internal var
		lsl.w		#3,d1						; Shift into bits 13-3
		or.w		d1,d0
		move.l	d0,4(a0)				; Store low longword of phrase 1

		move.l	12(a0),d0				; Low Phrase 2 -> d0.l
		andi.l	#$FFFFF000,d0		; Extract XPOS
		move.w	x_pos1,d1		    ; Grab new XPOS
		or.w		d1,d0						; store XPOS
		move.l	d0,12(a0)		    ; d0.l -> Low Phrase 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2. Scaled Bitmap
;
		move.l	#bm2,a0

		move.l	bmp_highl2,d1		; Grab low longword of phrase
		andi.l	#$00000FFF,d1		; Strip old adress of picture
		move.l	img_adr2,d2			; Adress of picture
		lsl.l		#8,d2
		or.l		d2,d1
		move.l	d1,(a0)					; Store high longword of phrase 1

		move.l	bmp_lowl2,d0		; Grab low longword of phrase
		andi.l	#$FFFFC007,d0		; Strip old YPOS
		clr.l		d1
		move.w	y_pos2,d1				; Update YPOS from internal var
		lsl.w		#3,d1						; Shift into bits 13-3
		or.w		d1,d0
		move.l	d0,4(a0)				; Store low longword of phrase 1

		move.l	12(a0),d0				; Low Phrase 2 -> d0.l
		andi.l	#$FFFFF000,d0		; Extract XPOS
		move.w	x_pos2,d1		    ; Grab new XPOS
		or.w		d1,d0						; store XPOS
		move.l	d0,12(a0)		    ; d0.l -> Low Phrase 2

		move.l	scl_lowl2,d0		; Low Long of Phr 3 containing H/VSCALE

		andi.l	#$FFFF0000,d0		; Strip away old
		or.w		bmp_hscl2,d0
		move.w	bmp_vscl2,d1
		lsl.w		#8,d1
		or.w		d1,d0
		move.l	d0,20(a0)				; Put in new

		move.w	#$101,INT1			; Signal we're done
		move.w	#$0,INT2

		movem.l	(sp)+,d0-d2/a0
		rte

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
build_obj:
		movem.l	d0-d7/a0-a3,-(sp)

;		a0=Adress of object to process
;		a1=Adress of high long of (s)bmpobj phrase 1 for later update
;		a2=Adress of low long of (s)bmpobj phrase 1 for later update
;		a3=Adress of low long of scaleobj phrase 3 for later update
;		d0=Object type (0-4) and low long of phrase 1
;		d1=High long of phrase 1
;		d2=Link adress to next object
;		d3=X-Position of bitmap (.w)
;		d4=Y-Position of bitmap (.w)
;		d5=Bitmap height in pixel
;		d6=Adress of bitmap
;		d7=Bitmap depth, pitch (O_TRANS, O_NOGAP)

		move.l	d0,obj_typ

		cmp.l		#0,d0					; Bitmap object
		beq.w		.build_bitmap
		cmp.l		#1,d0					; Scaled bitmap  object (begins like bitmap)
		beq.w		.build_bitmap
		cmp.l		#2,d0					; GPU object
		beq.w		.build_gpu
		cmp.l		#3,d0					; Branch object
		beq.w		.build_branch
		cmp.l		#4,d0					; Stop object
		beq.w		.build_stop

		jmp			.all_done

.build_bitmap:
		clr.l   d1

		jsr			format_link		; Stuff in LINK address in d0 and d1

		and.w		#$FFFE,d4			; Y-Position must be even

		lsl.w		#3,d4
		or.w		d4,d0

		lsl.l		#8,d5					; Bitmap height
		lsl.l		#6,d5
		or.l		d5,d0

		lsl.l		#8,d6					; Adress of picture
		or.l		d6,d1

		move.l	d1,(a0)+
		move.l	d1,(a1)
		move.l	d0,(a0)+
		move.l	d0,(a2)

		clr.l		d0						; Now Phrase

		or.w		d3,d0					; X-Position
		or.l		d7,d0					; Depth...

		move.l	bmp_bit,d1		; Goon with phrase 2

		move.l	bmp_phr,d4		; DWIDTH
		move.l	d4,d3

		lsl.l		#8,d4
		lsl.l		#8,d4
		lsl.l		#2,d4
		or.l		d4,d0

		lsl.l		#8,d4					; IWIDTH Bits 28-31
		lsl.l		#2,d4
		or.l		d4,d0

		lsr.l		#4,d3					; IWIDTH Bits 37-32
		or.l		d3,d1

		move.l  d1,(a0)+
		move.l  d0,(a0)+

		move.l	obj_typ,d0

		cmp.l		#1,d0					; Scaled bitmap  object
		beq.w		.build_scale

		jmp			.all_done

.build_scale:
		clr.l		(a0)+					; High long
		clr.l		d0

		move.w	scl_vsc,d0		; Vscale
		lsl.w	#8,d0
		or.w	scl_hsc,d0			; Hscale

		move.l	d0,(a0)				; Low long
		move.l	d0,(a3)

		jmp			.all_done

.build_gpu:
		clr.l		d1

		move.l	d1,(a0)+
		move.l	d0,(a0)

		jmp			.all_done

.build_branch:
		clr.l		d1

		or.l		bra_con,d0		; Branch condition

		jsr			format_link		; Stuff in LINK address in d0 and d1

		move.w	bra_ypo,d3		; YPOS of branch object
		lsl.w		#3,d3					; Make it bits 13-3
		or.w		d3,d0

		move.l	d1,(a0)+
		move.l	d0,(a0)

		jmp			.all_done

.build_stop:
		clr.l		d1

		or.l		#O_STOPINTS,d0

		move.l	d1,(a0)+
		move.l	d0,(a0)

.all_done:
		movem.l	(sp)+,d0-d7/a0-a3
		rts


format_link:
		movem.l	d2-d3,-(sp)

		andi.l	#$3FFFF8,d2		; Ensure alignment/valid address
		move.l	d2,d3

		swap		d2						; Put bits 10-3 in bits 31-24
		clr.w		d2
		lsl.l		#5,d2
		or.l		d2,d0

		lsr.l		#8,d3					; Put bits 21-11 in bits 42-32
		lsr.l		#3,d3
		or.l		d3,d1

		movem.l	(sp)+,d2-d3
		rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Some variables
;
		.bss
		.phrase

obj_typ:		ds.l	1
bra_ypo:		ds.w	1
bra_con:		ds.l	1
bmp_bit:		ds.l	1
bmp_phr:		ds.l	1
scl_vsc:		ds.w	1
scl_hsc:		ds.w	1

		.end