* File Name: vdp.asm * * Copyright (c) 1994 Sega of America, Inc. All Rights Reserved. * ALL ROUTINES IN THIS FILE ARE BASED ON ROUTINES FOUND IN LOGODEMO * (part of the Genesis Sample code) * * Author(s): Unni Pillai * Date: 6-03-94 * Description: This file contains general purpose routines used * the VDP on the MD side. * * History: * *---------------------------------------------------------------------* nolist section .text xref FlyBackFlag include "z80.h" ;z80 hardware equates include "vdp.h" ;vdp hardware equates and masks include "vdp.i" ;references to VDP variables include "general.mac" ;general purpose macros list *-------------------------------------------------------------------- * _SetVDPRegs: Sets the VDP registers * * void _SetVDPRegs( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVDPRegs dc.l 0 SetVDPRegs: moveq #19-1,d0 ; registers lea VDPSlaves,a0 ; point to register slaves lea VDPControl,a1 ; point to VDP control port .sr_lp: move.w (a0)+,(a1) ; set register dbra d0,.sr_lp ; do all registers sequentially rts *-------------------------------------------------------------------- * _SetVDPDMA: Sets VDP DMA regs * * void _SetVDPDMA( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVDPDMA dc.l 0 SetVDPDMA moveq #5-1,d0 ;regs 19..23 lea DMASlaves,a0 ; point to register slaves lea VDPControl,a1 ; point to VDP Control Port .sd_lp move.w (a0)+,(a1) ; set register dbra d0,.sd_lp rts *-------------------------------------------------------------------- * _SetVRAMWrite: Prepares VDP for write VRAM * * void _SetVRAMWrite( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVRAMWrite dc.l 0 SetVRAMWrite: move.l d0,-(sp) or.w #CD0,d0 swap d0 move.w 2(sp),d0 rol.w #2,d0 and.l #($FFFF-CD1)<<16+%11,d0 move.l d0,VDPControl move.l (sp)+,d0 rts *-------------------------------------------------------------------- * _SetVRAMRead: Prepares VDP for read from VRAM * * void _SetVRAMRead( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVRAMRead dc.l 0 SetVRAMRead: move.l d0,-(sp) swap d0 move.w 2(sp),d0 rol.w #2,d0 and.l #($FFFF-CD1-CD0)<<16+%11,d0 move.l d0,VDPControl move.l (sp)+,d0 rts *-------------------------------------------------------------------- * _SetCRAMWrite: Prepares VDP for write to CRAM * * void _SetCRAMWrite( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetCRAMWrite dc.l 0 SetCRAMWrite: and.w #$7f,d0 or.w #CD1+CD0,d0 move.w d0,VDPControl move.w #0,VDPControl and.w #$7f,d0 rts *-------------------------------------------------------------------- * _SetCRAMRead: Prepares VDP for read CRAM * * void _SetCRAMRead( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetCRAMRead dc.l 0 SetCRAMRead: and.w #$7f,d0 move.w d0,VDPControl move.w #CD3,VDPControl rts *-------------------------------------------------------------------- * _SetVSRAMWrite: Prepares VDP for write to Vertical Scroll * * void _SetVSRAMWrite( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVSRAMWrite dc.l 0 SetVSRAMWrite: and.w #$3f,d0 or.w #CD0,d0 move.w d0,VDPControl move.w #CD2,VDPControl and.w #$3f,d0 rts *-------------------------------------------------------------------- * _SetVSRARead: Prepares VDP for read Vertical Scroll * * void _SetVSRAMRead( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef SetVSRAMRead dc.l 0 SetVSRAMRead: and.w #$3f,d0 move.w d0,VDPControl move.w #CD2,VDPControl rts *----------------------------------------------------------------------- * _BlackOut: Blacks Out the display screen by setting color palette to 0 * * void _BlackOut( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef BlackOut dc.l 0 BlackOut: moveq #0,d0 ; CRAM address 0 bsr SetCRAMWrite ; write to CRAM moveq #64*2/4-1,d1 ; 128 bytes acessed as long words .bo_lp: move.l d0,VDPData dbra d1,.bo_lp rts *-------------------------------------------------------------------- * _WaitForDMA: Waits for DMA to finish * * void _WaitForDMA( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef WaitForDMA dc.l 0 WaitForDMA: move.w VDPControl,d3 ; read VDP status btst.l #1,d3 ; check DMA status bne.b WaitForDMA ; wait for DMA not busy rts *------------------------------------------------------------------------* *;* clear vram without dma ??????????? * *;------------------------------------------------------------------------* *;entry:- *;always starts at 0 & clears vram *; *; xdef ClearVRAM2 dc.l 0 ClearVRAM2: move.w #$8124,VDPControl ;turn display off move.w #$8F02,VDPControl ;set VDP autoincrement to 2 (fill w/words) move.w #0,d0 ;words bsr SetVRAMWrite ;set VRAM address for write move.w #$10000/4/4-1,d1 ;adjust for dbra CV2: move.l #0,VDPData ;clear long word move.l #0,VDPData ;clear long word move.l #0,VDPData ;clear long word move.l #0,VDPData ;clear long word dbra d1,CV2 ;until done move.w #$8164,VDPControl ;turn display on rts *-------------------------------------------------------------------- * _ClearVRAM: Clears VRAM with DMA * * void _ClearVRAM( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef ClearVRAM dc.l 0 ClearVRAM: or.b #M1,VDP_ModeSet2 ; enable DMA move.b VDP_AutoInc,d7 ; get current autoinc move.b #1,VDP_AutoInc ; VRAM is byte wide st VDP_DMALenLo st VDP_DMALenHi move.b #DMD1,VDP_DMASrcHi ; VRAM fill bsr SetVDPRegs bsr SetVDPDMA move.l #$40000000,VDPControl move.l #$40000080,VDPControl ; address 0 move.w #0,VDPData ; clear value and.b #$ff-M1,VDP_ModeSet2 ; flag save as DISABLE DMA move.b d7,VDP_AutoInc ; set slave autoinc to previous value bsr WaitForDMA ; wait until DMA finished bra SetVDPRegs *-------------------------------------------------------------------- * _ClearVSRAM: Clears Vertical Scroll * * void _ClearVSRAM( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef ClearVSRAM dc.l 0 ClearVSRAM: moveq #0,d0 ; start address bsr SetVSRAMWrite moveq #80/4-1,d1 ; 80bytes i.e 20 long words lea VDPData,a0 ; point to VDP data .cv_lp: move.l d0,(a0) ; clear scrolls dbra d1,.cv_lp rts *-------------------------------------------------------------------- * _ClearHSRAM: Clears Horizontal Scrolls * * void _ClearHSRAM( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef ClearHSRAM dc.l 0 ClearHSRAM: move.w #HScroll,d0 ; address of scroll table in VRAM bsr SetVRAMWrite moveq #0,d0 ; clear d0 move.w #1024/4-1,d1 ; 1kbytes i.e 256 long words lea VDPData,a0 .ch_lp: move.l d0,(a0) ; clear scrolls dbra d1,.ch_lp rts *-------------------------------------------------------------------- * _CopyToVRAM: Copies to VRAM using DMA * * void _CopyToVRAM( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef CopyToVRAM dc.l 0 CopyToVRAM: move.w sr,-(sp) ; save status DI ; disable interrupts Z80Di ; grab Z80 st Z80NoROM ; flag ROM accesses NOT permitted Z80Ei ; allow Z80 to continue move.w (sp)+,sr ; reset old interrupt status or.b #M1,VDP_ModeSet2 ; enable DMA move.b VDP_AutoInc,d7 ; get current autoinc move.b #2,VDP_AutoInc ; VRAM is byte wide lsr.w #1,d1 ; convert length to words move.b d1,VDP_DMALenLo ; \ move.w d1,-(sp) ; > length (byte aligned word in VDP slaves) move.b (sp)+,VDP_DMALenHi ; / move.l a0,d1 ; get address lsr.l #1,d1 ; convert to word address move.b d1,VDP_DMASrcLo ;\ move.w d1,-(sp) ; \ move.b (sp)+,VDP_DMASrcMid ; \ source address (byte aligned in VDP slaves) swap d1 ; / and.b #$7f,d1 ; / clear DMD1 move.b d1,VDP_DMASrcHi ;/ move.w d0,DMATemp ; save VRAM address bsr SetVDPRegs ; copy DMA and AutoInc slaves to VDP bsr SetVDPDMA ; copy DMA slaves to VDP move.w DMATemp,d0 ; get VRAM address and.w #$FFFF-CD1,d0 ; clear CD1 or.w #CD0,d0 ; set CD0 lea VDPControl,a0 ; control port pointer move.w d0,(a0) ; set first word of address move.w DMATemp,d0 ; get VRAM address again move.l DMATrigger(pc),DMATemp ; move trigger code to work RAM rol.w #2,d0 ; move bits 14/15 to bits 0/1 and.w #%11,d0 ; mask them (clear CD2 and other bits) or.w #$80,d0 ; set bit 7 jsr DMATemp ; call the trigger (set second word of address) and.b #$FF-M1,VDP_ModeSet2 ; flag slave as DISABLE DMA move.b d7,VDP_AutoInc ; set slave autoinc back to previous bsr SetVDPRegs ; set up the VDP (AutoInc and M1). No need to WAIT for DMA! move.w sr,-(sp) ; save status DI ; disable interrupts Z80Di ; grab Z80 sf Z80NoROM ; flag ROM accesses permitted Z80Ei ; allow Z80 to continue move.w (sp)+,sr ; reset old interrupt status rts DMATrigger: move.w d0,(a0) ;\ this code is moved to and executed in work RAM rts ; / *-------------------------------------------------------------------- * _CopyToVRAM2: Copies to VRAM without DMA * * void _CopyToVRAM2( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef CopyToVRAM2 dc.l 0 CopyToVRAM2: bsr SetVRAMWrite ;set VRAM address for write lsr.w #1,d1 ;words subq.w #1,d1 ;adjust for dbra .ctv_load: move.w (a0)+,VDPData ;load word dbra d1,.ctv_load ;until done rts *-------------------------------------------------------------------- * _ClearScrolls: Clears both Vertical and Horizontal Scrolls * * void _ClearScrolls( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef ClearScrolls dc.l 0 ClearScrolls: bsr ClearVSRAM bsr ClearHSRAM rts *-------------------------------------------------------------------- * _ColorLoad: Loads Color Palette onto PALETTE * * void _ColorLoad( ) * in: a2 source address of COLOR data in ROM * d0 destination addresss in CRAM * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- * xref COLOR xdef ColorLoad dc.l 0 ColorLoad: * moveq #0,d0 bsr SetCRAMWrite * lea COLOR(pc),a2 moveq #64-1,d0 .ld_color: move.w (a2)+,(a0) dbra d0,.ld_color rts *-------------------------------------------------------------------- * _Char_Load: Loads ASCII character set onto top of VRAM * * void _Char_Load( ) * in: a2 source address of character data in ROM * d0 destination address in VRAM * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef Char_Load dc.l 0 Char_Load: bsr SetVRAMWrite move.w #$100-1,d0 load_char_00: moveq #8-1,d1 load_char_01: moveq #8-1,d2 move.b (a2)+,d3 load_char_02: lsr.b #1,d3 roxr.l #1,d4 lsr.l #3,d4 dbra d2,load_char_02 move.l d4,(a0) dbra d1,load_char_01 dbra d0,load_char_00 rts *-------------------------------------------------------------------- * _LoadChars: Loads ASCII character set onto top of VRAM * * void _LoadChars( ) * in: a2 source address of character data in ROM * d0 destination address in VRAM * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/14/94 created *---------------------------------------------------------------------- xdef LoadChars dc.l 0 LoadChars lsl.w #5,d0 ;make VRAM address bsr SetVRAMWrite ;set write to VRAM lsl.w #3,d1 ;bytes to expand subq.w #1,d1 ;adjust for dbra lea VDPData,a1 ;point at data port and.w #$f,d2 and.w #$f,d3 ;force colours into range mulu #%1000100010001,d2 mulu #%1000100010001,d3 ;duplicate nybble into all 4 nybbles lc_bytes move.b (a0),d0 ;get byte and.w #$f0,d0 ;mask top nybble lsr.w #3,d0 ;make index bsr lc_donybble ;expand nybble move.b (a0)+,d0 ;reget byte (and move data ptr) and.w #$0f,d0 ;mask bottom nybble add.w d0,d0 ;make mask bsr lc_donybble ;expand nybble dbra d1,lc_bytes ;expand all rts xdef lc_donybble dc.l 0 lc_donybble move.w lc_nybble4(pc,d0.w),d4 ;expand a nybble move.w d4,d0 ;duplicate not.w d0 ;make background mask and.w d2,d4 ;put in colour and.w d3,d0 ;put in other colour or.w d4,d0 ;combine move.w d0,(a1) ;write expanded word to VRAM rts xdef lc_nybble4 dc.l 0 lc_nybble4 dc.w $0000,$000F,$00F0,$00FF dc.w $0F00,$0F0F,$0FF0,$0FFF dc.w $F000,$F00F,$F0F0,$F0FF dc.w $FF00,$FF0F,$FFF0,$FFFF *-------------------------------------------------------------------- * _VerticalGrid: Loads character set using vertical grid * for display on Screen * * void _VerticalGrid( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/02/94 created *---------------------------------------------------------------------- xdef VerticalGrid dc.l 0 VerticalGrid: add.w d0,d0 ;scale x position to words move.w CellMapCols,d7 ;get cell map width add.w d7,d7 ;convert to bytes mulu d7,d1 ;scale y position to lines add.w d1,d0 ;add line to x offset add.w d5,d0 ;create cell map address of top left cell move.w d4,d1 ;copy first cell number move.w d3,d6 ;copy height subq.w #1,d6 ;adjust height for dbra subq.w #1,d2 ;adjust width for dbra vg_rows move.w d2,d5 ;get width count bsr SetVRAMWrite ;set up left most address (cannot use a macro here!) add.w d7,d0 ;update address for next line down cell map vg_cols move.w d1,VDPData ;set cell map add.w d3,d1 ;next horizontal character number dbra d5,vg_cols ;do a row addq.w #1,d4 ;make next sequential character below left most character move.w d4,d1 ;get it dbra d6,vg_rows ;do all rows rts ; simple really! *-------------------------------------------------------------------- * _JumpIn: Displays immediately * * void _JumpIn( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/13/94 created *---------------------------------------------------------------------- xdef JumpIn dc.l 0 JumpIn: moveq #0,d0 ;first colour bsr SetCRAMWrite ;set write to colour RAM lea PalBuffer,a0 ;point at slaves moveq #64-1,d0 ;number of colours (4*16 colour palettes) .ji_lp move.w (a0)+,VDPData ;move data to CRAM dbra d0,.ji_lp ;move all rts *-------------------------------------------------------------------- * _SetPalette: constructs palette slave buffer * * void _SetPalette( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/13/94 created *---------------------------------------------------------------------- xdef SetPalette dc.l 0 SetPalette: bra .sp_get ;jump to start .sp_set lea PalBuffer,a1 ;point at slave buffer add.w d0,d0 ;2*colour add.w d0,a1 ;index correct slave move.w (a0)+,d0 ;get count of values subq.w #1,d0 ;for dbra .sp_lp move.w (a0)+,(a1)+ ;copy value to slave buffer dbra d0,.sp_lp ;do values .sp_get move.w (a0)+,d0 ;get flag/address bpl.s .sp_set ;adress if not -ve rts ;stop *-------------------------------------------------------------------- * _FadeIn: displays gradually over a couple of frames * * void _FadeIn( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/17/94 *---------------------------------------------------------------------- xdef FadeIn dc.l 0 FadeIn lea PalBuffer,a0 ;point to slaves FadeAnyIn move.w d0,d7 ;maximum frame moveq #1,d6 ;first frame moveq #1,d5 ;direction bra.s f_join ;do it FadeOut lea PalBuffer,a0 ;point to slaves move.w d0,d7 ;maximum frame move.w d0,d6 subq.w #1,d6 ;first frame moveq #-1,d5 ;direction moveq #0,d0 ;set address of palettes (in CRAM) bsr SetCRAMRead ;set read of palette moveq #64-1,d0 ;words f_get move.w VDPData,(a0)+ ;read palette dbra d0,f_get ;read all lea -128(a0),a0 ;point to buffer f_join move.w d7,d4 subq.w #1,d4 ;frames to do (adjusted for dbra) f_fade bsr WaitFrame ;synchronize to video moveq #0,d0 ;address to write (in CRAM) bsr SetCRAMWrite ;set write palette moveq #64-1,d1 ;every colour f_set move.b (a0)+,d0 ;1st byte of data bsr f_nib ;do nybble move.b d0,-(sp) move.w (sp)+,d0 ;byte swap move.b (a0)+,d0 ;2nd byte of data ror.b #4,d0 ;top nibble bsr f_nib ;do nybble ror.b #4,d0 ;bottom nibble bsr f_nib ;do nybble move.w d0,VDPData ;set colour dbra d1,f_set ;do all colours lea -128(a0),a0 ;point to buffer again add.w d5,d6 ;next frame dbra d4,f_fade ;fade rts f_nib move.w d0,d3 and.w #$f,d0 ;get nibble eor.w d0,d3 ;clear nibble mulu d6,d0 divu d7,d0 ;scale for current frame and.w #$e,d0 ;discard bottom bit or.w d3,d0 ;include other nibbles rts *---------------------------------------------------------------------------- * _WaitFrame: waits for VBL * * void _WaitFrame( ) * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/13/94 created *---------------------------------------------------------------------- xdef WaitFrame dc.l 0 WaitFrame sf FlyBackFlag ;clear flag .wf_lp tst.b FlyBackFlag ;has interrupt set it yet beq.s .wf_lp ;wait until it has rts *---------------------------------------------------------------------------- * _WaitFrames: waits for #of VBL * * void _WaitFrames( ) * in d0 #of frames to wait * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/19/94 created *---------------------------------------------------------------------- xdef WaitFrames dc.l 0 WaitFrames subq.w #1,d0 .wfs_lp bsr WaitFrame dbra d0,.wfs_lp rts *---------------------------------------------------------------------------- * _LoadCellMap: * * void _LoadCellMap( ) * in d0 #of frames to wait * * MODS * name date comments * ---- -------- ------------------------------------- * UP 06/19/94 created *---------------------------------------------------------------------- xdef LoadCellMap dc.l 0 LoadCellMap add.w d0,d0 ;scale x position to words move.w CellMapCols,d7 ;get cell map width add.w d7,d7 ;convert to bytes mulu d7,d1 ;scale y position to lines add.w d1,d0 ;add line to x offset add.w d4,d0 ;create cell map address of top left cell subq.w #1,d2 ;adjust width for dbra subq.w #1,d3 ;adjust height for dbra lea VDPData,a1 ;point to VDP data port .lc_rows move.w d2,d5 ;get width bsr SetVRAMWrite ;set address add.w d7,d0 ;get next line address .lc_cols move.w (a0)+,(a1) ;put data in cell map dbra d5,.lc_cols ;do row dbra d3,.lc_rows ;do all rows rts