;*======================================================================*
;*                TITLE:                  VIDINIT.S                     *
;*                Function:               Video initialization and      *
;*                                              VBLANK                  *
;*                                                                      *
;*                Project #:              JAGUAR                        *
;*                                                                      *
;*                Programmer:             Leonard Tramiel               *
;*                                        Andrew J Burgess              *
;*                                                                      *
;*                                                                      *
;*              COPYRIGHT 1992,1993 Atari U.S. Corporation              *
;*          UNATHORIZED REPRODUCTION, ADAPTATION, DISTRIBUTION,         *
;*          PERFORMANCE OR DISPLAY OF THIS COMPUTER PROGRAM OR          *
;*        THE ASSOCIATED AUDIOVISUAL WORK IS STRICTLY PROHIBITED.       *
;*                            ALL RIGHTS RESERVED.                      *
;*                                                                      *
;*======================================================================*

	.title	"VIDINIT.S - video initialization and VBLANK routines"

BASE	EQU	$00D00000		;* 68000 register base address	*
BAR_ON	EQU	0

	.include	"jaguar.inc"
	.include	"defs.inc"

	.globl	VideoIni
	.globl	VBlank
	.globl	WaitForSync

	.globl	frameCnt
	.globl	frameRate
	.globl	rateCnt

	.extern	CopyObjList


;*======================================================================*
;*	VIDEO DEFINES
;*======================================================================*
; Note that the horizontal timing numbers are for NTSC
; in units of 100 ps

vclk		EQU	834
ivclk		EQU	(vclk*3)/2

horiz_per	EQU	317778
sync_per	EQU	46050
eq_per		EQU	23500
front_porch	EQU	17450
line_blank	EQU	109500
disp_width	EQU	320	;this is in pixels
disp_height	EQU	200	;this is in lines

; Horizontal computations

n_hp		EQU	horiz_per/ivclk
n_hbb		EQU	((horiz_per-sync_per-front_porch)/ivclk)+$400
n_hbe		EQU	(line_blank-sync_per-front_porch)/ivclk
n_hs		EQU	((horiz_per-sync_per)/ivclk)+$400
n_hvs		EQU	(horiz_per-(2*sync_per))/ivclk
n_heq		EQU	(horiz_per-eq_per)/ivclk
n_hde		EQU	((disp_width/2)-1)+$400
n_hdb1		EQU	((n_hp)-(disp_width/2))
n_hdb2		EQU	n_hdb1

n_vmode		EQU	$c1

; Vertical

n_vp		EQU	523
n_vee		EQU	6
n_vbe		EQU	40
n_vdb		EQU	n_vbe+26
n_vde		EQU	n_vdb+(disp_height*2)
n_vbb		EQU	n_vde+40
n_vs		EQU	n_vp-10
n_veb		EQU	n_vs-n_vee

;*======================================================================*
;*	VideoIni()	- Initialize Video to 320x200 NTSC
;*
;*	preserved:
;*		none
;*======================================================================*

VideoIni:
	move.w	#0,VMODE	; Stop the Video (I want to get off)

	clr.w	frameCnt
	clr.w	frameRate
	clr.w	rateCnt
	clr.w	sync

	move.w	#n_hp,HP
	move.w	#n_hbb,HBB
	move.w	#n_hbe,HBE
	move.w	#n_hs,HS
	move.w	#n_hvs,HVS
	move.w	#n_heq,HEQ
	move.w	#n_hde,HDE
	move.w	#n_hdb1,HDB1
	move.w	#n_hdb2,HDB2

	move.w	#n_vp,VP
	move.w	#n_vee,VEE
	move.w	#n_vbe,VBE
	move.w	#n_vdb,VDB
	move.w	#n_vde,VDE
	move.w	#n_vbb,VBB
	move.w	#n_veb,VEB
	move.w	#n_vs,VS

	;===============================================================
	; While we are at it let's set the object processor to a stop
	; object so that the system can run reliably
	;
	;	move.l	#defobj,d0
	;	and.l	#$ffffffc0,d0	; Assure even phrase alignment
	;	move.w	d0,OLPLO
	;	swap	d0
	;	move.w	d0,OLPHI
	;===============================================================

	move.w	#0,ODP		; Clear any garbage address bits

	move.w	#$0,BG		; Also lets set up some default colors 
	move.l	#RGB_DKGREEN,BORD1

	move.l	#R30Hz,frameRate
	rts


;*======================================================================*
;*	VBlank()	- VBlank interrupt
;*
;*	preserved:
;*		d0-d1
;*		a0-a1
;*======================================================================*

VBlank:
	movem.l	d0-d1/a0-a1,-(sp)

	jsr	CopyObjList

	addq.w	#1,rateCnt
	addq.w	#1,frameCnt
	move.l	frameRate,d0
	move.l	rateCnt,d1
	cmp.w	d1,d0
;	bne	.waitframe
	addq.w	#1,sync
	clr.w	rateCnt

.waitframe:
	move.w	#$101,INT1
	move.w	#0,INT2

	movem.l	(sp)+,d0-d1/a0-a1

	rte

;*======================================================================*
;*	WaitForSync()	- wait for video - objlist display sync
;*
;*	preserved:
;*		d0
;*======================================================================*

WaitForSync:
	movem.l	d0,-(sp)

	move.w	sync,d0
.wait:	cmp.w	sync,d0
	beq	.wait

	movem.l	(sp)+,d0
	rts

;========================================================================
;	USEFUL VARIABLES
;========================================================================
	.bss

	.even
sync:				;* interrupt synchronization
	ds.w	1

frameCnt:			;* frame counter
	ds.w	1

frameRate:			;* frame rate specifier
	ds.w	1

rateCnt:			;* frame rate count
	ds.w	1

;*======================================================================*
;*				EOF
;*======================================================================*

