;******************************************************************************
;            (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.
;******************************************************************************
; 100 22 May 95 JPE     Initial revision (incorporates preliminary subtitle
;                       code from Dave Schwartz).
; 101 23 May 95 DMS     Let the subtitle flicker to reduce overhead
; 102 23 May 95 DMS     Make subtitle time placement variable
; 103 24 May 95 DMS     Allow multiple subtitles per track
; 104 22 Jun 95 DMS     Continue debug
; 105 23 Jun 95 DMS     Merge in V204's inits
; 106 23 Jun 95 DMS     Hold off track init until after 2d subtitle
; 107 27 Jun 95 DMS     Bug fixes and final subtitles
; 108 28 Jun 95 DMS     Color the hot subtitles that actually branch
; 110 30 Jun 95 DMS     Add ABC buttons on controller
; 111 30 Jun 95 DMS     Switch to disc 07JUL95C
; 112 12 Jul 95 DMS     Switch to disc 11JUL95D
; 113 12 Jul 95 DMS     Fix colors and missing subtitles
; 114 12 Jul 95 DMS     Use 4 loop passes; 2 dummy, 2 hot
; 115 25 Aug 95 JPE     Uses computed value for A1/A2_PIXEL, not zero.
; 116 14 Sep 95 JPE     Modified for new interface. New fonts added.
; 117 03 Oct 95 JPE     Clean up string alignment. Clear subtitle by over-
;                       writing with black. Use prototype atom structure for
;                       management of subtitles. Move joypad routine to new
;                       module. Add routine to clear only viewed buffers.
; 118 19 Oct 95 JPE     Added centering capability to Print() routine. Moved
;                       subtitle and print functions to new module 'auxil.s'.
; 119 08 Nov 95 JPE     ClearOnScreen() now clears subtitle area.
; 120 01 Dec 95 JPE     Initial value of blitIndex changed from 1 to 0.
;******************************************************************************

        .nlist
        .include    'memory.inc'
        .include    'jaguar.inc'
        .include    'player.inc'
        .list

;==============================================================================
;                               InitBlit
;==============================================================================
;   Subroutine to precompute the following blitter variables:
;
;       blitCount
;       blitPixel
;       blitStep
;
;   Also sets up the value of 'dest', which is the address of the buffer used
;   by the Cinepak decompressor.
;
;   Input:  d0 = width of movie (pixels)
;           d1 = height of movie (lines)
;
;   Output: None
;==============================================================================

InitBlit:
        movem.l d2-d4,-(a7)             ; Save used registers

        move.l  #NLINES,d4              ; Total number of active lines
        sub.l   d1,d4                   ; Subtract lines in movie
        move.l  d4,titleLines           ; Number left for subtitle

        move    d1,d2                   ; Lines of movie
        swap    d2                      ; Move to high word
        move    d0,d2                   ; Pixels per line
        move.l  d2,blitCount            ; Blitter B_COUNT

        move.l  d0,d2                   ; Form Blitter STEP regs
        neg     d2
        ori.l   #$10000,d2
        move.l  d2,blitStep

        move.l  #NLINES,d2              ; Find true Vert center
        sub.l   d4,d2
        sub.l   d1,d2                   ; d2 = (d2-d1)/2
        lsr.l   #1,d2

        move.l  d2,d3                   ; Copy
        mulu    #(NPIXELS*2),d3         ; Mult * bytes per row

        swap    d2                      ; Put in high word

        move    #NPIXELS,d2             ; Find true Horz center
        sub     d0,d2                   ; d2 = (d2-d0)/2
        lsr     #1,d2

        clr.l   d4
        move    d2,d4                   ; # Pixel Offset
        lsl.l   #1,d4                   ; * 2 (for bytes)

        add.l   d4,d3
        mulu    #3,d3
        add.l   #SCREEN_BASE,d3
        move.l  d3,dest

        move.l  d2,blitPixel            ; Built Blitter A1/A2_PIXEL

        movem.l (a7)+,d2-d4             ; Restore used registers
        rts

;==============================================================================
;                                DoBlit
;==============================================================================
;   Copies video frame from Cinepak decompression buffer to one of two ping-
;   pong display buffers. Toggles the buffer index prior to the copy and
;   updates the 'scrbuf' address used by the GPU to set up the object list.
;
;   Input:  None.
;
;   Output: None.
;==============================================================================

DoBlit:
        movem.l d0-d1/a0,-(a7)          ; Save used registers

        move.l  blitCount,d1            ; Usual number of lines

        tst     blitTitle               ; Do we have to copy subtitle?
        beq.s   GetScreen               ; No, proceed

        subi    #1,blitTitle            ; Yes, decrement counter
        move.l  titleLines,d1           ; Extra lines in subtitle
        swap    d1                      ; Move to high word
        add.l   blitCount,d1            ; Adjusted count

GetScreen:
        movea.l #SCREEN_BASE,a0         ; Base of triple buffer
        eori    #1,blitIndex            ; Toggle index
        move    blitIndex,d0            ; Get index
        lsl     #3,d0                   ; Convert to phrase offset
        lea     $8(a0,d0),a0            ; Base of target buffer

        move.l  a0,A1_BASE              ; Do normal phrase screen copy
        move.l  #SCREEN_BASE,A2_BASE
        move.l  #PIXEL16|XADDPHR|PITCH3|WID320,A1_FLAGS
        move.l  #PIXEL16|XADDPHR|PITCH3|WID320,A2_FLAGS
        move.l  blitPixel,A1_PIXEL
        move.l  blitPixel,A2_PIXEL
        move.l  blitStep,A1_STEP
        move.l  blitStep,A2_STEP
        move.l  d1,B_COUNT
        move.l  #SRCEN|UPDA1|UPDA2|LFU_REPLACE,B_CMD

Wait4Blit:
        move.l  B_CMD,d0                ; Ensure blit has finished
        btst.l  #0,d0
        beq.s   Wait4Blit

        move.l  a0,scrbuf               ; Store buffer for op vframe

        movem.l (a7)+,d0-d1/a0          ; Restore used registers
        rts

;==============================================================================
;                                Clear
;==============================================================================
;   Clears off-screen and on-screen display buffers, including subtitle area.
;
;   Input:  None
;
;   Output: None
;==============================================================================

Clear:
        movem.l d0-d2,-(sp)             ; Save used registers

        move.l  #PITCH1,d0              ; Single buffering
        move.l  #SCREEN_BASE,d1         ; Base of buffer

        move    #(ROWBYTES*NLINES/512)*3,d2     ; *2 for two buffers

        bsr     BlitZero                ; Clear buffer

        movem.l (sp)+,d0-d2             ; Restore used registers
        rts

;==============================================================================
;                            ClearOnScreen
;==============================================================================
;   Clears only the on-screen display buffers. The buffer used by the Cinepak
;   decompressor is unmodified!!
;
;   Input:  None
;
;   Output: None
;==============================================================================

ClearOnScreen:
        movem.l d0-d2,-(sp)             ; Save used registers

        move.l  #PITCH3,d0                  ; Triple buffering
        move    #(ROWBYTES*NLINES/512)*1,d2 ; *2 for two buffers

        move.l  #SCREEN_BASE+$8,d1      ; Base of first buffer
        bsr     BlitZero                ; Clear it

        move.l  #SCREEN_BASE+$10,d1     ; Base of second buffer
        bsr     BlitZero                ; Clear it

        movem.l (sp)+,d0-d2             ; Restore used registers
        rts                             ; Exit

BlitZero:
        move.l  d0,-(sp)                ; Save used register

        ori.l   #PIXEL16|WID256|XADDPHR,d0
        move.l  d0,A1_FLAGS

        move.l  d1,A1_BASE              ; Point A1BASE to destination
        move.l  #0,A1_PIXEL             ; Set the pixel point to 0,0
        move.l  #0,A1_CLIP              ; No clipping (must set anyway)

        move    #1,d0
        swap    d0
        move    #(-256),d0
        move.l  d0,A1_STEP

        move    d2,d0
        swap    d0
        move    #256,d0
        move.l  d0,B_COUNT

        move.l  #$0,B_PATD
        move.l  #$0,B_PATD+4

        move.l  #PATDSEL|UPDA1,d0
        move.l  d0,B_CMD

dowa:
        move.l  B_CMD,d0
        andi    #1,d0
        beq.s   dowa

        move.l  (sp)+,d0                ; Restore used register
        rts

;==============================================================================
;                              BlockMove
;==============================================================================
;   Moves a block of data from one memory location to another.
;
;   Input:  d0 = byte count
;           a0 = source address
;           a1 = destination address
;
;   Output: None
;==============================================================================

BlockMove:
        move.l  d0,-(sp)                ; Save used register

        add.l   #3,d0                   ; Round to higher longword
        lsr.l   #2,d0                   ; Convert bytes to longs

        move.l  a1,A1_BASE              ; Destination
        move.l  a0,A2_BASE              ; Source
    
        move.l  #XADDPHR|PIXEL32|WID2048|PITCH1,A1_FLAGS
        move.l  #XADDPHR|PIXEL32|WID2048|PITCH1,A2_FLAGS

        move.l  #0,A1_PIXEL
        move.l  #0,A2_PIXEL

        or.l    #$10000,d0
        move.l  d0,B_COUNT

        move.l  #SRCEN|UPDA1|UPDA2|LFU_REPLACE,B_CMD

w1:
        move.l  B_CMD,d0
        andi    #1,d0
        beq.s   w1

        move.l  (sp)+,d0                ; Restore used register
        rts

        .long

scrbuf:         dc.l    0       ; Address of buffer to display (Read by GPU as long)
dest:           dc.l    0

blitTitle:      dc.w    0       ; Blit title to screen buffers
blitIndex:      dc.w    0       ; For double-buffer blit

        .bss

titleLines:     ds.l    1
blitCount:      ds.l    1       ; Blit B_COUNT value (precomputed)
blitPixel:      ds.l    1       ; Blit A1/A2_PIXEL value (precomputed)
blitStep:       ds.l    1       ; Blit A1/A2_STEP value (precomputed)

        .globl  InitBlit
        .globl  DoBlit
        .globl  Clear
        .globl  ClearOnScreen
        .globl  BlockMove
        .globl  dest
        .globl  scrbuf
        .globl  blitIndex
        .globl  blitTitle

        .end                           ; end of file
