;******************************************************************************
;            (C) Copyright 1992-1995, Radius, Inc. and Atari Corp.
; Secret and confidential                                   All rights reserved
;******************************************************************************
; This source code and any compilation or derivative thereof is the joint
; property of Radius, Inc. and Atari Corp.  This code is provided pursuant
; to a Software License Agreement and/or Software Development Contract.  This
; code is the proprietary information of Radius, Inc. and Atari Corp. and is
; confidential in nature.  Its use and dissemination by any party other than
; Atari Corp. are strictly limited by the confidential information provisions
; of the Agreement or Contract referenced above.
;******************************************************************************
; Program:  player.cof 	- Cinepak Player Demo
;  Module: startup.s	- Hardware initialization/License screen display
;
; Revision History:
;  1/12/95 - SDS: Modified from MOU.COF sources.
;  2/28/95 - SDS: Optimized some code from MOU.COF.
;  3/14/95 - SDS: Old code preserved old value from INT1 and OR'ed the
;                 video interrupt enable bit. Trouble is that could cause
;                 pending interrupts to persist. Now it just stuffs the value.
;  3/29/94 - SDS: Modified (only a little!) for no logo and Cinepak olist
;                 setup.
;******************************************************************************
; 100 23 May 95 JPE     Moved equates to 'player.inc' to resolve conflicts.
; 101 08 Nov 95 JPE     Official legal banner added (no other changes).
;******************************************************************************

		.include 'jaguar.inc'
		.include 'memory.inc'
		.include 'player.inc'
; Globals
		.globl	gSetOLP
		.globl	olp2set

		.globl  a_vdb
		.globl  a_vde
		.globl  a_hdb
		.globl  a_hde
		.globl  width
		.globl  height
		.globl	listbuf
		.globl	bmpupdate
; Externals
		.extern	_start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Program Entry Point Follows...

		.text

		move.l  #$70007,G_END		; big-endian mode
		move.l  #$70007,D_END
		move.w  #$FFFF,VI       	; disable video interrupts

		move.l  #INITSTACK,a7   	; Setup a stack
			
		jsr 	InitVideo      		; Setup our video registers.
		jsr 	InitLister     		; Initialize Object Display List

;;; Sneaky trick to cause display to popup at first VB
		move.l	#$0,listbuf+BITMAP_OFF
		move.l	#$C,listbuf+BITMAP_OFF+4

	     	jmp 	_start			; Jump to main code
						; OLP swapped is in D0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Procedure: InitVideo (same as in vidinit.s)
;            Build values for hdb, hde, vdb, and vde and store them.
;
						
InitVideo:
		movem.l d0-d6,-(sp)     
			
		move.w  CONFIG,d0      		 ; Also is joystick register
		andi.w  #VIDTYPE,d0    		 ; 0 = PAL, 1 = NTSC
		beq 	palvals

		move.w  #NTSC_HMID,d2
		move.w  #NTSC_WIDTH,d0

		move.w  #NTSC_VMID,d6
		move.w  #NTSC_HEIGHT,d4

		bra 	calc_vals
palvals:
		move.w  #PAL_HMID,d2
		move.w  #PAL_WIDTH,d0

		move.w  #PAL_VMID,d6
		move.w  #PAL_HEIGHT,d4

calc_vals:
		move.w  d0,width
		move.w  d4,height

		move.w  d0,d1
		asr 	#1,d1         	 	; Width/2

		sub.w   d1,d2         	  	; Mid - Width/2
		add.w   #4,d2         	  	; (Mid - Width/2)+4

		sub.w   #1,d1         	  	; Width/2 - 1
		ori.w   #$400,d1      	  	; (Width/2 - 1)|$400
		
		move.w  d1,a_hde
		move.w  d1,HDE

		move.w  d2,a_hdb
		move.w  d2,HDB1
		move.w  d2,HDB2

		move.w  d6,d5
		sub.w   d4,d5
		move.w  d5,a_vdb

		add.w   d4,d6
		move.w  d6,a_vde

		move.w  a_vdb,VDB
		move.w  #$FFFF,VDE
			
		move.l  #0,BORD1        	; Black border
		move.w  #0,BG           	; Init line buffer to black
			
		movem.l (sp)+,d0-d6
		rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; InitLister: Initialize Object List Processor List
;
;    Returns: Pre-word-swapped address of current object list in d0.l
;
;  Registers: d0.l/d1.l - Phrase being built
;             d2.l/d3.l - Work Registers
;             a0.l      - Roving object list pointer
;
;----------------------------------------------------------------------
; List being built looks like:
;
;  -------- Dummy Phrases <- Quad-Phrase Boundary (in-case using scaled)		
;  --------
;  --------
;  -------- GPU Int Obj
;  -------- STOP Obj
;  -------- Br = VDE (to GPU Int)
;  -------- Br < VDB (to Stop)
;  -------- Br > VDE (to Stop)
;  -------- Scaled or Normal Bitmap (to Stop)
;  --------
;  -------- (maybe)

InitLister:
		movem.l d1-d3/a0,-(sp)		; Save registers
			
		lea     listbuf,a0
		move.l  a0,d2           	; Copy

; GPU Interrupt Object
		clr.l	(a0)+
		move.l	#GPUOBJ,(a0)+

; STOP Object
		clr.l	(a0)+
		move.l	#STOPOBJ,(a0)+

; Write first BRANCH object (branch if YPOS = a_vde)
		move.l	d2,d0			; Address of GPU obj copies
		move.l	d2,d1

		lsr.l	#8,d0			; Shift high half into place
		lsr.l	#3,d0
		
		swap	d1			; Place low half correctly
		clr.w	d1
		lsl.l	#5,d1

		move.w	a_vde,d3
		andi.w	#$FFFE,d3
		lsl.w	#3,d3
		or.w	d3,d1
		ori.l	#O_BREQ|BRANCHOBJ,d1

		move.l	d0,(a0)+		; Store it
		move.l	d1,(a0)+

; Write second BRANCH object (branch if a_vdb > YPOS)

		move.l	d2,d0			; High half
		add.l	#8,d0			; Add 8 (STOP object)
		move.l	d0,d1			; Copy for low half

		lsr.l	#8,d0			; Shift high half into place
		lsr.l	#3,d0
		
		swap	d1			; Place low half correctly
		clr.w	d1
		lsl.l	#5,d1

		or.l  #(BRANCHOBJ|O_BRGT),d1  ; $4000 = VC < YPOS
								
		move.w  a_vdb,d3                ; for YPOS
		lsl.w   #3,d3                   ; Make it bits 13-3
		or.w    d3,d1

		move.l	d0,(a0)+
		move.l	d1,(a0)+

; Write third branch object (branch if a_vde < YPOS)   
; Note: LINK address is the same so preserve it

		andi.l  #$FF000007,d1           ; Mask off CC and YPOS
		ori.l   #O_BRLT,d1      	; $8000 = VC > YPOS
		move.w  a_vde,d3                ; for YPOS
		lsl.w   #3,d3                   ; Make it bits 13-3
		or.w    d3,d1

		move.l	d0,(a0)+
		move.l	d1,(a0)+

; Write a standard BITMAP object
		andi.l	#$7FF,d0		; Leave valid LINK
		andi.l	#$FF000000,d1		; Zeros TYPE

	.if (USESCALED)
		ori.l  #((NLINES-1)<<14)|SCBITOBJ,d1
	.else
		ori.l  #NLINES<<14,d1       	; Height of image
	.endif

		move.w  height,d3           	; Center bitmap vertically
		sub.w   #NLINES,d3
		add.w   a_vdb,d3
		andi.w  #$FFFE,d3               ; Must be even
		lsl.w   #3,d3
		or.w    d3,d1                   ; Stuff YPOS in low phrase

		move.l	#SCREEN_BASE,d3
		lsl.l	#8,d3
		or.l	d3,d0
	 
		move.l	d0,(a0)+
		move.l	d1,(a0)+
		movem.l	d0-d1,bmpupdate

; Second Phrase of Bitmap
	.if (USESCALED)
		move.l	#O_RELEASE|((BMP_PHRASES*1)>>4),d0
	.else
		move.l	#(BMP_PHRASES*1)>>4,d0	; Only part of top LONG is IWIDTH
	.endif
		move.l  #O_DEPTH16|O_2GAP,d1   ; Bit Depth = 16-bit, Contiguous data

		move.w  width,d3            	; Get width in clocks
		lsr.w   #2,d3               	; /4 Pixel Divisor
		sub.w   #NPIXELS,d3
		lsr.w   #1,d3
		or.w    d3,d1

		ori.l	#((BMP_PHRASES*3)<<18)|((BMP_PHRASES*1)<<28),d1	; DWIDTH|IWIDTH

		move.l	d0,(a0)+
		move.l	d1,(a0)+

; Third-phrase for Scaled Bitmap???
	.if (USESCALED)
		clr.l	(a0)+
		move.l	#$1F2020,(a0)+
	.endif

; Now return object list pointer in D0                      
		move.l  #listbuf,d0
		add.l	#$10,d0  

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

		.bss
		.qphrase

		.ds.l	(2*3)
listbuf:    	.ds.l   LISTSIZE*2  		; Object List
bmpupdate:  	.ds.l   2       		; One Phrase of Bitmap for Refresh

a_hdb:  	.ds.w   1
a_hde:      	.ds.w   1
a_vdb:      	.ds.w   1
a_vde:      	.ds.w   1
width:      	.ds.w   1
height:     	.ds.w   1

		.end

