;*======================================================================*
;*                TITLE:                  GPU.S                         *
;*                Function:               Gpu interfece routines        *
;*                                                                      *
;*                Project #:              JAGUAR                        *
;*                Programmer:             Andrew J Burgess              *
;*                                                                      *
;*              COPYRIGHT 1992 Atari Computer Corporation               *
;*          UNATHORIZED REPRODUCTION, ADAPTATION, DISTRIBUTION,         *
;*          PERFORMANCE OR DISPLAY OF THIS COMPUTER PROGRAM OR          *
;*        THE ASSOCIATED AUDIOVISUAL WORK IS STRICTLY PROHIBITED.       *
;*                            ALL RIGHTS RESERVED.                      *
;*                                                                      *
;*======================================================================*

BASE	EQU	$00D00000		;* 68000 register base address

	include "jaguar.inc"
	include "blit.inc"
	include "gpu.inc"

;
;	PUBLIC SYMBOLS
;
	.globl	gpuload
	.globl	gpurun
	.globl	gpuwait
	.globl	turtlpak
	.globl	fastdraw
	.globl	sort


;*======================================================================*
;* gpuload()	- load a GPU program into GPU RAM
;*
;*	input:
;*		a0	- 68000 address of GPU program
;*
;*	preserved:
;*		d0-d1
;*		a0-a1
;*======================================================================*
gpuload:
	movem.l	d0-d1/a0-a1,-(sp)		; save GPU address for restore

	; This code will load a gpu program into the Blitter at the address
	; specified in the header 

	move.l	#PITCH1|PIXEL16|WID256|XADDPHR,d0
	swap	d0
	move.l	d0,A1_FLAGS
	move.l	d0,A2_FLAGS
	
	; Point A1BASE to the destination
	; Read destination from header of file
	; Phrase align and find remainder

	move.l	(a0)+,d0
	or.l	#$07000000,d0
	move.l	d0,d1
	and.l	#$fffffff8,d0
	sub.l	d0,d1
	swap	d0
	move.l	d0,A1_BASE

	; Set the pixel pointer to the offset in d1

	asr.l	#1,d1		; Convert to pixels
	swap d1
	move.l	d1,A1_PIXEL

	; Find size of data to load

	move.l	(a0)+,d0
	asr.l	#1,d0		; Convert to words
	swap 	d0
	or.l	#1,d0		; Set 1 outer loop
	move.l	d0,B_COUNT

	; Set up Counters register to number of words

	; Point A2BASE to the source
	; a0 now points to the data
	; Phrase align and find remainder

	move.l	a0,d0
	move.l	d0,d2
	and.l	#$fffffff8,d0
	sub.l	d0,d2
	swap	d0
	move.l	d0,A2_BASE

	; Set the pixel pointer to the offset in d1

	asr.l	#1,d2		; Convert to pixels
	swap d2
	move.l	d2,A2_PIXEL

	; Now Turn IT ON !!!!!!!!!!!!!

	; DESTINATION = SOURCE
	; NO OUTER LOOP

	or.l	d1,d2
	beq.s	.aligned
	move.l	#SRCEN|SRCENX|LFU_AN|LFU_A,d0
	bra.s	.blit_go
.aligned:
	move.l	#SRCEN|LFU_AN|LFU_A,d0
.blit_go:
	swap	d0
	move.l	d0,B_CMD
;
;	NOTE: No Wait for BLTTER Idle - I have yet to be overrun but WARNING
;
	movem.l	(sp)+,d0-d1/a0-a1
	rts

;*======================================================================*
;* gpurun()	- tell the GPU to begin execution
;*
;*	input:
;*		a0	- 68000 address of GPU program
;*
;*	preserved:
;*		d0-d1
;*		a0
;*======================================================================*
gpurun:
	movem.l	d0-d1/a0,-(sp)		; save GPU address for restore

	move.l	#0,SEMAPHORE

	move.l	(a0)+,d0		; get GASM generated address
	swap	d0
	move.l	d0,G_PC			; load GPU PC


	move.l	G_CTRL,d0		; start the GPU
	swap	d0
	move.l	#$11,d1			; set the GPUGO bit
					; set the SINGLE_GO

	move.l	#$1011,d0
	swap	d0
	move.l	d0,G_CTRL

	movem.l	(sp)+,a0/d0-d1		; restore GPU address
	rts

;*======================================================================*
;* gpuwait()	- wait for the GPU to finish executing
;*
;*	input:
;*		None
;*
;*	preserved:
;*		d0
;*		a0
;*======================================================================*
gpuwait:
	movem.l	a0/d0,-(sp)
.gpuwt:				; wait for GPU to finish
	move.l	#SEMAPHORE,a0
	move.l	(a0),d0
	beq	.gpuwt

	movem.l	(sp)+,a0/d0
	rts

;
;	CONSTANT DATA (GPU PROGRAMS)
;
fastdraw:
	.include	"fastdraw.dat"
turtlpak:
	.include	"turtle.dat"
sort:
	.include	"sort.dat"


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

