Search
lxdream.org :: lxdream/src/pvr2/pvr2.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2.c
changeset 65:9f124c245fc6
prev56:3224dceaf2a3
next85:71e239d20c5d
author nkeynes
date Tue Jan 03 12:21:45 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement blanking (maybe, untested)
Quick hack to generate appropriate events on ta list completion
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@65
     2
 * $Id: pvr2.c,v 1.12 2006-01-03 12:21:45 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * PVR2 (Video) MMIO and supporting functions.
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@35
    18
#define MODULE pvr2_module
nkeynes@31
    19
nkeynes@1
    20
#include "dream.h"
nkeynes@1
    21
#include "video.h"
nkeynes@1
    22
#include "mem.h"
nkeynes@1
    23
#include "asic.h"
nkeynes@1
    24
#include "pvr2.h"
nkeynes@56
    25
#include "sh4/sh4core.h"
nkeynes@1
    26
#define MMIO_IMPL
nkeynes@1
    27
#include "pvr2.h"
nkeynes@1
    28
nkeynes@1
    29
char *video_base;
nkeynes@1
    30
nkeynes@15
    31
void pvr2_init( void );
nkeynes@30
    32
uint32_t pvr2_run_slice( uint32_t );
nkeynes@23
    33
void pvr2_next_frame( void );
nkeynes@15
    34
nkeynes@23
    35
struct dreamcast_module pvr2_module = { "PVR2", pvr2_init, NULL, NULL, 
nkeynes@23
    36
					pvr2_run_slice, NULL,
nkeynes@15
    37
					NULL, NULL };
nkeynes@15
    38
nkeynes@1
    39
void pvr2_init( void )
nkeynes@1
    40
{
nkeynes@1
    41
    register_io_region( &mmio_region_PVR2 );
nkeynes@56
    42
    register_io_region( &mmio_region_PVR2TA );
nkeynes@1
    43
    video_base = mem_get_region_by_name( MEM_REGION_VIDEO );
nkeynes@1
    44
}
nkeynes@1
    45
nkeynes@23
    46
uint32_t pvr2_time_counter = 0;
nkeynes@30
    47
uint32_t pvr2_time_per_frame = 20000000;
nkeynes@23
    48
nkeynes@30
    49
uint32_t pvr2_run_slice( uint32_t nanosecs ) 
nkeynes@23
    50
{
nkeynes@30
    51
    pvr2_time_counter += nanosecs;
nkeynes@30
    52
    while( pvr2_time_counter >= pvr2_time_per_frame ) {
nkeynes@23
    53
	pvr2_next_frame();
nkeynes@23
    54
	pvr2_time_counter -= pvr2_time_per_frame;
nkeynes@23
    55
    }
nkeynes@30
    56
    return nanosecs;
nkeynes@23
    57
}
nkeynes@23
    58
nkeynes@1
    59
uint32_t vid_stride, vid_lpf, vid_ppl, vid_hres, vid_vres, vid_col;
nkeynes@1
    60
int interlaced, bChanged = 1, bEnabled = 0, vid_size = 0;
nkeynes@1
    61
char *frame_start; /* current video start address (in real memory) */
nkeynes@1
    62
nkeynes@1
    63
/*
nkeynes@1
    64
 * Display the next frame, copying the current contents of video ram to
nkeynes@1
    65
 * the window. If the video configuration has changed, first recompute the
nkeynes@1
    66
 * new frame size/depth.
nkeynes@1
    67
 */
nkeynes@1
    68
void pvr2_next_frame( void )
nkeynes@1
    69
{
nkeynes@1
    70
    if( bChanged ) {
nkeynes@1
    71
        int dispsize = MMIO_READ( PVR2, DISPSIZE );
nkeynes@1
    72
        int dispmode = MMIO_READ( PVR2, DISPMODE );
nkeynes@1
    73
        int vidcfg = MMIO_READ( PVR2, VIDCFG );
nkeynes@1
    74
        vid_stride = ((dispsize & DISPSIZE_MODULO) >> 20) - 1;
nkeynes@1
    75
        vid_lpf = ((dispsize & DISPSIZE_LPF) >> 10) + 1;
nkeynes@1
    76
        vid_ppl = ((dispsize & DISPSIZE_PPL)) + 1;
nkeynes@1
    77
        vid_col = (dispmode & DISPMODE_COL);
nkeynes@1
    78
        frame_start = video_base + MMIO_READ( PVR2, DISPADDR1 );
nkeynes@1
    79
        interlaced = (vidcfg & VIDCFG_I ? 1 : 0);
nkeynes@1
    80
        bEnabled = (dispmode & DISPMODE_DE) && (vidcfg & VIDCFG_VO ) ? 1 : 0;
nkeynes@1
    81
        vid_size = (vid_ppl * vid_lpf) << (interlaced ? 3 : 2);
nkeynes@1
    82
        vid_hres = vid_ppl;
nkeynes@1
    83
        vid_vres = vid_lpf;
nkeynes@1
    84
        if( interlaced ) vid_vres <<= 1;
nkeynes@1
    85
        switch( vid_col ) {
nkeynes@1
    86
            case MODE_RGB15:
nkeynes@1
    87
            case MODE_RGB16: vid_hres <<= 1; break;
nkeynes@1
    88
            case MODE_RGB24: vid_hres *= 3; break;
nkeynes@1
    89
            case MODE_RGB32: vid_hres <<= 2; break;
nkeynes@1
    90
        }
nkeynes@1
    91
        vid_hres >>= 2;
nkeynes@1
    92
        video_update_size( vid_hres, vid_vres, vid_col );
nkeynes@1
    93
        bChanged = 0;
nkeynes@1
    94
    }
nkeynes@1
    95
    if( bEnabled ) {
nkeynes@65
    96
	if( MMIO_READ( PVR2, VIDCFG2 ) & 0x08 ) {
nkeynes@65
    97
	    /* Blanked */
nkeynes@65
    98
	    uint32_t colour = MMIO_READ( PVR2, BORDERCOL );
nkeynes@65
    99
	    video_fill( colour );
nkeynes@65
   100
	} else {
nkeynes@65
   101
	    /* Assume bit depths match for now... */
nkeynes@65
   102
	    memcpy( video_data, frame_start, vid_size );
nkeynes@65
   103
	}
nkeynes@1
   104
    } else {
nkeynes@1
   105
        memset( video_data, 0, vid_size );
nkeynes@1
   106
    }
nkeynes@1
   107
    video_update_frame();
nkeynes@1
   108
    asic_event( EVENT_SCANLINE1 );
nkeynes@1
   109
    asic_event( EVENT_SCANLINE2 );
nkeynes@1
   110
    asic_event( EVENT_RETRACE );
nkeynes@1
   111
}
nkeynes@1
   112
nkeynes@1
   113
void mmio_region_PVR2_write( uint32_t reg, uint32_t val )
nkeynes@1
   114
{
nkeynes@1
   115
    if( reg >= 0x200 && reg < 0x600 ) { /* Fog table */
nkeynes@1
   116
        MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   117
        /* I don't want to hear about these */
nkeynes@1
   118
        return;
nkeynes@1
   119
    }
nkeynes@1
   120
    
nkeynes@1
   121
    INFO( "PVR2 write to %08X <= %08X [%s: %s]", reg, val, 
nkeynes@1
   122
          MMIO_REGID(PVR2,reg), MMIO_REGDESC(PVR2,reg) );
nkeynes@1
   123
   
nkeynes@1
   124
    switch(reg) {
nkeynes@65
   125
    case DISPSIZE: bChanged = 1;
nkeynes@65
   126
    case DISPMODE: bChanged = 1;
nkeynes@65
   127
    case DISPADDR1: bChanged = 1;
nkeynes@65
   128
    case DISPADDR2: bChanged = 1;
nkeynes@65
   129
    case VIDCFG: bChanged = 1;
nkeynes@65
   130
	break;
nkeynes@65
   131
    case RENDSTART:
nkeynes@65
   132
	if( val == 0xFFFFFFFF )
nkeynes@65
   133
	    pvr2_render_scene();
nkeynes@65
   134
	break;
nkeynes@1
   135
    }
nkeynes@1
   136
    MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   137
}
nkeynes@1
   138
nkeynes@1
   139
MMIO_REGION_READ_FN( PVR2, reg )
nkeynes@1
   140
{
nkeynes@1
   141
    switch( reg ) {
nkeynes@1
   142
        case BEAMPOS:
nkeynes@2
   143
            return sh4r.icount&0x20 ? 0x2000 : 1;
nkeynes@1
   144
        default:
nkeynes@1
   145
            return MMIO_READ( PVR2, reg );
nkeynes@1
   146
    }
nkeynes@1
   147
}
nkeynes@19
   148
nkeynes@19
   149
void pvr2_set_base_address( uint32_t base ) 
nkeynes@19
   150
{
nkeynes@19
   151
    mmio_region_PVR2_write( DISPADDR1, base );
nkeynes@19
   152
}
nkeynes@56
   153
nkeynes@56
   154
nkeynes@65
   155
void pvr2_render_scene( void )
nkeynes@65
   156
{
nkeynes@65
   157
    /* Actual rendering goes here :) */
nkeynes@65
   158
    asic_event( EVENT_PVR_RENDER_DONE );
nkeynes@65
   159
    DEBUG( "Rendered frame %d", video_frame_count );
nkeynes@65
   160
}
nkeynes@65
   161
nkeynes@65
   162
/** Tile Accelerator */
nkeynes@65
   163
nkeynes@65
   164
struct tacmd {
nkeynes@65
   165
    uint32_t command;
nkeynes@65
   166
    uint32_t param1;
nkeynes@65
   167
    uint32_t param2;
nkeynes@65
   168
    uint32_t texture;
nkeynes@65
   169
    float alpha;
nkeynes@65
   170
    float red;
nkeynes@65
   171
    float green;
nkeynes@65
   172
    float blue;
nkeynes@65
   173
};
nkeynes@65
   174
nkeynes@56
   175
int32_t mmio_region_PVR2TA_read( uint32_t reg )
nkeynes@56
   176
{
nkeynes@56
   177
    return 0xFFFFFFFF;
nkeynes@56
   178
}
nkeynes@56
   179
nkeynes@56
   180
char pvr2ta_remainder[8];
nkeynes@56
   181
nkeynes@56
   182
void mmio_region_PVR2TA_write( uint32_t reg, uint32_t val )
nkeynes@56
   183
{
nkeynes@65
   184
    DEBUG( "Direct write to TA %08X", val );
nkeynes@56
   185
}
nkeynes@56
   186
nkeynes@56
   187
void pvr2ta_write( char *buf, uint32_t length )
nkeynes@56
   188
{
nkeynes@65
   189
    int i;
nkeynes@65
   190
    struct tacmd *cmd_list = (struct tacmd *)buf;
nkeynes@65
   191
    int count = length >> 5;
nkeynes@65
   192
    unsigned int lasttype = 0;
nkeynes@65
   193
    for( i=0; i<count; i++ ){
nkeynes@65
   194
	unsigned int type = (cmd_list[i].command >> 24) & 0xFF;
nkeynes@65
   195
	DEBUG( "PVR2 cmd: %08X %08X %08X", cmd_list[i].command, cmd_list[i].param1, cmd_list[i].param2 );
nkeynes@65
   196
	if( type == 0 ) {
nkeynes@65
   197
	    /* End of list */
nkeynes@65
   198
	    switch( lasttype ) {
nkeynes@65
   199
	    case 0x80: /* Opaque polys */
nkeynes@65
   200
		asic_event( EVENT_PVR_OPAQUE_DONE );
nkeynes@65
   201
		break;
nkeynes@65
   202
	    case 0x81: /* Opaque poly modifier */
nkeynes@65
   203
		asic_event( EVENT_PVR_OPAQUEMOD_DONE );
nkeynes@65
   204
		break;
nkeynes@65
   205
	    case 0x82: /* Transparent polys */
nkeynes@65
   206
		asic_event( EVENT_PVR_TRANS_DONE );
nkeynes@65
   207
		break;
nkeynes@65
   208
	    case 0x83: /* Transparent poly modifier */
nkeynes@65
   209
		asic_event( EVENT_PVR_TRANSMOD_DONE );
nkeynes@65
   210
		break;
nkeynes@65
   211
	    case 0x84: /* Punchthrough */
nkeynes@65
   212
		asic_event( EVENT_PVR_PUNCHOUT_DONE );
nkeynes@65
   213
		break;
nkeynes@65
   214
	    }
nkeynes@65
   215
	} else {
nkeynes@65
   216
	    lasttype = type;
nkeynes@65
   217
	}
nkeynes@65
   218
    }
nkeynes@56
   219
}
nkeynes@65
   220
.