Search
lxdream.org :: lxdream/src/pvr2/pvr2.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2.c
changeset 127:4ba79389bb6d
prev108:565de331ccec
next133:249aeda31f02
author nkeynes
date Thu Mar 23 13:19:15 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Add a vram dump function for debugging purposes
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@127
     2
 * $Id: pvr2.c,v 1.21 2006-03-23 13:19:15 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@100
     4
 * PVR2 (Video) Core MMIO registers.
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@103
    24
#include "pvr2/pvr2.h"
nkeynes@56
    25
#include "sh4/sh4core.h"
nkeynes@1
    26
#define MMIO_IMPL
nkeynes@103
    27
#include "pvr2/pvr2mmio.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@94
    33
void pvr2_display_frame( void );
nkeynes@94
    34
nkeynes@103
    35
/**
nkeynes@103
    36
 * Current PVR2 ram address of the data (if any) currently held in the 
nkeynes@103
    37
 * OpenGL buffers.
nkeynes@103
    38
 */
nkeynes@103
    39
nkeynes@94
    40
video_driver_t video_driver = NULL;
nkeynes@94
    41
struct video_buffer video_buffer[2];
nkeynes@94
    42
int video_buffer_idx = 0;
nkeynes@15
    43
nkeynes@103
    44
struct video_timing {
nkeynes@103
    45
    int fields_per_second;
nkeynes@103
    46
    int total_lines;
nkeynes@108
    47
    int retrace_lines;
nkeynes@103
    48
    int line_time_ns;
nkeynes@103
    49
};
nkeynes@103
    50
nkeynes@108
    51
struct video_timing pal_timing = { 50, 625, 50, 32000 };
nkeynes@108
    52
struct video_timing ntsc_timing= { 60, 525, 65, 31746 };
nkeynes@103
    53
nkeynes@23
    54
struct dreamcast_module pvr2_module = { "PVR2", pvr2_init, NULL, NULL, 
nkeynes@23
    55
					pvr2_run_slice, NULL,
nkeynes@15
    56
					NULL, NULL };
nkeynes@15
    57
nkeynes@1
    58
void pvr2_init( void )
nkeynes@1
    59
{
nkeynes@1
    60
    register_io_region( &mmio_region_PVR2 );
nkeynes@85
    61
    register_io_region( &mmio_region_PVR2PAL );
nkeynes@56
    62
    register_io_region( &mmio_region_PVR2TA );
nkeynes@1
    63
    video_base = mem_get_region_by_name( MEM_REGION_VIDEO );
nkeynes@107
    64
    pvr2_render_init();
nkeynes@107
    65
    texcache_init();
nkeynes@106
    66
}
nkeynes@106
    67
nkeynes@106
    68
void video_set_driver( video_driver_t driver )
nkeynes@106
    69
{
nkeynes@106
    70
    if( video_driver != NULL && video_driver->shutdown_driver != NULL )
nkeynes@106
    71
	video_driver->shutdown_driver();
nkeynes@106
    72
nkeynes@106
    73
    video_driver = driver;
nkeynes@106
    74
    if( driver->init_driver != NULL )
nkeynes@106
    75
	driver->init_driver();
nkeynes@106
    76
    driver->set_display_format( 640, 480, COLFMT_RGB32 );
nkeynes@108
    77
    driver->set_render_format( 640, 480, COLFMT_RGB32, FALSE );
nkeynes@108
    78
    texcache_gl_init();
nkeynes@1
    79
}
nkeynes@1
    80
nkeynes@103
    81
uint32_t pvr2_line_count = 0;
nkeynes@103
    82
uint32_t pvr2_line_remainder = 0;
nkeynes@103
    83
uint32_t pvr2_irq_vpos1 = 0;
nkeynes@103
    84
uint32_t pvr2_irq_vpos2 = 0;
nkeynes@108
    85
gboolean pvr2_retrace = FALSE;
nkeynes@103
    86
struct video_timing *pvr2_timing = &ntsc_timing;
nkeynes@23
    87
uint32_t pvr2_time_counter = 0;
nkeynes@94
    88
uint32_t pvr2_frame_counter = 0;
nkeynes@30
    89
uint32_t pvr2_time_per_frame = 20000000;
nkeynes@23
    90
nkeynes@30
    91
uint32_t pvr2_run_slice( uint32_t nanosecs ) 
nkeynes@23
    92
{
nkeynes@103
    93
    pvr2_line_remainder += nanosecs;
nkeynes@103
    94
    while( pvr2_line_remainder >= pvr2_timing->line_time_ns ) {
nkeynes@103
    95
	pvr2_line_remainder -= pvr2_timing->line_time_ns;
nkeynes@103
    96
	pvr2_line_count++;
nkeynes@103
    97
	if( pvr2_line_count == pvr2_irq_vpos1 ) {
nkeynes@103
    98
	    asic_event( EVENT_SCANLINE1 );
nkeynes@103
    99
	} 
nkeynes@103
   100
	if( pvr2_line_count == pvr2_irq_vpos2 ) {
nkeynes@103
   101
	    asic_event( EVENT_SCANLINE2 );
nkeynes@103
   102
	}
nkeynes@108
   103
	if( pvr2_line_count == pvr2_timing->total_lines ) {
nkeynes@103
   104
	    asic_event( EVENT_RETRACE );
nkeynes@103
   105
	    pvr2_line_count = 0;
nkeynes@108
   106
	    pvr2_retrace = TRUE;
nkeynes@108
   107
	} else if( pvr2_line_count == pvr2_timing->retrace_lines ) {
nkeynes@108
   108
	    if( pvr2_retrace ) {
nkeynes@108
   109
		pvr2_display_frame();
nkeynes@108
   110
		pvr2_retrace = FALSE;
nkeynes@108
   111
	    }
nkeynes@103
   112
	}
nkeynes@23
   113
    }
nkeynes@30
   114
    return nanosecs;
nkeynes@23
   115
}
nkeynes@23
   116
nkeynes@1
   117
uint32_t vid_stride, vid_lpf, vid_ppl, vid_hres, vid_vres, vid_col;
nkeynes@1
   118
int interlaced, bChanged = 1, bEnabled = 0, vid_size = 0;
nkeynes@1
   119
char *frame_start; /* current video start address (in real memory) */
nkeynes@1
   120
nkeynes@103
   121
/**
nkeynes@1
   122
 * Display the next frame, copying the current contents of video ram to
nkeynes@1
   123
 * the window. If the video configuration has changed, first recompute the
nkeynes@1
   124
 * new frame size/depth.
nkeynes@1
   125
 */
nkeynes@94
   126
void pvr2_display_frame( void )
nkeynes@1
   127
{
nkeynes@103
   128
    uint32_t display_addr = MMIO_READ( PVR2, DISPADDR1 );
nkeynes@103
   129
    
nkeynes@94
   130
    int dispsize = MMIO_READ( PVR2, DISPSIZE );
nkeynes@94
   131
    int dispmode = MMIO_READ( PVR2, DISPMODE );
nkeynes@103
   132
    int vidcfg = MMIO_READ( PVR2, DISPCFG );
nkeynes@94
   133
    int vid_stride = ((dispsize & DISPSIZE_MODULO) >> 20) - 1;
nkeynes@94
   134
    int vid_lpf = ((dispsize & DISPSIZE_LPF) >> 10) + 1;
nkeynes@94
   135
    int vid_ppl = ((dispsize & DISPSIZE_PPL)) + 1;
nkeynes@103
   136
    gboolean bEnabled = (dispmode & DISPMODE_DE) && (vidcfg & DISPCFG_VO ) ? TRUE : FALSE;
nkeynes@103
   137
    gboolean interlaced = (vidcfg & DISPCFG_I ? TRUE : FALSE);
nkeynes@1
   138
    if( bEnabled ) {
nkeynes@94
   139
	video_buffer_t buffer = &video_buffer[video_buffer_idx];
nkeynes@94
   140
	video_buffer_idx = !video_buffer_idx;
nkeynes@94
   141
	video_buffer_t last = &video_buffer[video_buffer_idx];
nkeynes@94
   142
	buffer->rowstride = (vid_ppl + vid_stride) << 2;
nkeynes@94
   143
	buffer->data = frame_start = video_base + MMIO_READ( PVR2, DISPADDR1 );
nkeynes@94
   144
	buffer->vres = vid_lpf;
nkeynes@94
   145
	if( interlaced ) buffer->vres <<= 1;
nkeynes@103
   146
	switch( (dispmode & DISPMODE_COL) >> 2 ) {
nkeynes@103
   147
	case 0: 
nkeynes@103
   148
	    buffer->colour_format = COLFMT_ARGB1555;
nkeynes@103
   149
	    buffer->hres = vid_ppl << 1; 
nkeynes@103
   150
	    break;
nkeynes@103
   151
	case 1: 
nkeynes@103
   152
	    buffer->colour_format = COLFMT_RGB565;
nkeynes@103
   153
	    buffer->hres = vid_ppl << 1; 
nkeynes@103
   154
	    break;
nkeynes@103
   155
	case 2:
nkeynes@103
   156
	    buffer->colour_format = COLFMT_RGB888;
nkeynes@103
   157
	    buffer->hres = (vid_ppl << 2) / 3; 
nkeynes@103
   158
	    break;
nkeynes@103
   159
	case 3: 
nkeynes@103
   160
	    buffer->colour_format = COLFMT_ARGB8888;
nkeynes@103
   161
	    buffer->hres = vid_ppl; 
nkeynes@103
   162
	    break;
nkeynes@94
   163
	}
nkeynes@94
   164
	
nkeynes@94
   165
	if( video_driver != NULL ) {
nkeynes@94
   166
	    if( buffer->hres != last->hres ||
nkeynes@94
   167
		buffer->vres != last->vres ||
nkeynes@94
   168
		buffer->colour_format != last->colour_format) {
nkeynes@103
   169
		video_driver->set_display_format( buffer->hres, buffer->vres,
nkeynes@103
   170
						  buffer->colour_format );
nkeynes@94
   171
	    }
nkeynes@103
   172
	    if( MMIO_READ( PVR2, DISPCFG2 ) & 0x08 ) { /* Blanked */
nkeynes@100
   173
		uint32_t colour = MMIO_READ( PVR2, DISPBORDER );
nkeynes@94
   174
		video_driver->display_blank_frame( colour );
nkeynes@103
   175
	    } else if( !pvr2_render_display_frame( PVR2_RAM_BASE + display_addr ) ) {
nkeynes@94
   176
		video_driver->display_frame( buffer );
nkeynes@94
   177
	    }
nkeynes@65
   178
	}
nkeynes@1
   179
    } else {
nkeynes@94
   180
	video_buffer_idx = 0;
nkeynes@94
   181
	video_buffer[0].hres = video_buffer[0].vres = 0;
nkeynes@1
   182
    }
nkeynes@94
   183
    pvr2_frame_counter++;
nkeynes@1
   184
    asic_event( EVENT_SCANLINE1 );
nkeynes@1
   185
    asic_event( EVENT_SCANLINE2 );
nkeynes@1
   186
    asic_event( EVENT_RETRACE );
nkeynes@1
   187
}
nkeynes@1
   188
nkeynes@1
   189
void mmio_region_PVR2_write( uint32_t reg, uint32_t val )
nkeynes@1
   190
{
nkeynes@1
   191
    if( reg >= 0x200 && reg < 0x600 ) { /* Fog table */
nkeynes@1
   192
        MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   193
        /* I don't want to hear about these */
nkeynes@1
   194
        return;
nkeynes@1
   195
    }
nkeynes@1
   196
    
nkeynes@1
   197
    INFO( "PVR2 write to %08X <= %08X [%s: %s]", reg, val, 
nkeynes@1
   198
          MMIO_REGID(PVR2,reg), MMIO_REGDESC(PVR2,reg) );
nkeynes@108
   199
nkeynes@108
   200
    MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   201
   
nkeynes@1
   202
    switch(reg) {
nkeynes@108
   203
    case DISPADDR1:
nkeynes@108
   204
	if( pvr2_retrace ) {
nkeynes@108
   205
	    pvr2_display_frame();
nkeynes@108
   206
	    pvr2_retrace = FALSE;
nkeynes@108
   207
	}
nkeynes@108
   208
	break;
nkeynes@103
   209
    case VPOS_IRQ:
nkeynes@103
   210
	pvr2_irq_vpos1 = (val >> 16) & 0x03FF;
nkeynes@103
   211
	pvr2_irq_vpos2 = val & 0x03FF;
nkeynes@103
   212
	break;
nkeynes@100
   213
    case TAINIT:
nkeynes@100
   214
	if( val & 0x80000000 )
nkeynes@100
   215
	    pvr2_ta_init();
nkeynes@100
   216
	break;
nkeynes@65
   217
    case RENDSTART:
nkeynes@65
   218
	if( val == 0xFFFFFFFF )
nkeynes@65
   219
	    pvr2_render_scene();
nkeynes@65
   220
	break;
nkeynes@1
   221
    }
nkeynes@1
   222
}
nkeynes@1
   223
nkeynes@1
   224
MMIO_REGION_READ_FN( PVR2, reg )
nkeynes@1
   225
{
nkeynes@1
   226
    switch( reg ) {
nkeynes@1
   227
        case BEAMPOS:
nkeynes@2
   228
            return sh4r.icount&0x20 ? 0x2000 : 1;
nkeynes@1
   229
        default:
nkeynes@1
   230
            return MMIO_READ( PVR2, reg );
nkeynes@1
   231
    }
nkeynes@1
   232
}
nkeynes@19
   233
nkeynes@85
   234
MMIO_REGION_DEFFNS( PVR2PAL )
nkeynes@85
   235
nkeynes@19
   236
void pvr2_set_base_address( uint32_t base ) 
nkeynes@19
   237
{
nkeynes@19
   238
    mmio_region_PVR2_write( DISPADDR1, base );
nkeynes@19
   239
}
nkeynes@56
   240
nkeynes@56
   241
nkeynes@65
   242
nkeynes@98
   243
nkeynes@56
   244
int32_t mmio_region_PVR2TA_read( uint32_t reg )
nkeynes@56
   245
{
nkeynes@56
   246
    return 0xFFFFFFFF;
nkeynes@56
   247
}
nkeynes@56
   248
nkeynes@56
   249
void mmio_region_PVR2TA_write( uint32_t reg, uint32_t val )
nkeynes@56
   250
{
nkeynes@100
   251
    pvr2_ta_write( &val, sizeof(uint32_t) );
nkeynes@56
   252
}
nkeynes@56
   253
nkeynes@85
   254
nkeynes@103
   255
void pvr2_vram64_write( sh4addr_t destaddr, char *src, uint32_t length )
nkeynes@103
   256
{
nkeynes@103
   257
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@103
   258
    uint32_t *banks[2];
nkeynes@103
   259
    uint32_t *dwsrc;
nkeynes@103
   260
    int i;
nkeynes@65
   261
nkeynes@103
   262
    destaddr = destaddr & 0x7FFFFF;
nkeynes@103
   263
    if( destaddr + length > 0x800000 ) {
nkeynes@103
   264
	length = 0x800000 - destaddr;
nkeynes@103
   265
    }
nkeynes@103
   266
nkeynes@103
   267
    for( i=destaddr & 0xFFFFF000; i < destaddr + length; i+= PAGE_SIZE ) {
nkeynes@103
   268
	texcache_invalidate_page( i );
nkeynes@103
   269
    }
nkeynes@103
   270
nkeynes@108
   271
    banks[0] = ((uint32_t *)(video_base + ((destaddr & 0x007FFFF8) >>1)));
nkeynes@103
   272
    banks[1] = banks[0] + 0x100000;
nkeynes@108
   273
    if( bank_flag ) 
nkeynes@108
   274
	banks[0]++;
nkeynes@103
   275
    
nkeynes@103
   276
    /* Handle non-aligned start of source */
nkeynes@103
   277
    if( destaddr & 0x03 ) {
nkeynes@103
   278
	char *dest = ((char *)banks[bank_flag]) + (destaddr & 0x03);
nkeynes@103
   279
	for( i= destaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@103
   280
	    *dest++ = *src++;
nkeynes@103
   281
	}
nkeynes@103
   282
	bank_flag = !bank_flag;
nkeynes@103
   283
    }
nkeynes@103
   284
nkeynes@103
   285
    dwsrc = (uint32_t *)src;
nkeynes@103
   286
    while( length >= 4 ) {
nkeynes@103
   287
	*banks[bank_flag]++ = *dwsrc++;
nkeynes@103
   288
	bank_flag = !bank_flag;
nkeynes@103
   289
	length -= 4;
nkeynes@103
   290
    }
nkeynes@103
   291
    
nkeynes@103
   292
    /* Handle non-aligned end of source */
nkeynes@103
   293
    if( length ) {
nkeynes@103
   294
	src = (char *)dwsrc;
nkeynes@103
   295
	char *dest = (char *)banks[bank_flag];
nkeynes@103
   296
	while( length-- > 0 ) {
nkeynes@103
   297
	    *dest++ = *src++;
nkeynes@103
   298
	}
nkeynes@103
   299
    }  
nkeynes@103
   300
nkeynes@103
   301
}
nkeynes@103
   302
nkeynes@103
   303
void pvr2_vram64_read( char *dest, sh4addr_t srcaddr, uint32_t length )
nkeynes@103
   304
{
nkeynes@103
   305
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@103
   306
    uint32_t *banks[2];
nkeynes@103
   307
    uint32_t *dwdest;
nkeynes@103
   308
    int i;
nkeynes@103
   309
nkeynes@103
   310
    srcaddr = srcaddr & 0x7FFFFF;
nkeynes@103
   311
    if( srcaddr + length > 0x800000 )
nkeynes@103
   312
	length = 0x800000 - srcaddr;
nkeynes@103
   313
nkeynes@108
   314
    banks[0] = ((uint32_t *)(video_base + ((srcaddr&0x007FFFF8)>>1)));
nkeynes@103
   315
    banks[1] = banks[0] + 0x100000;
nkeynes@108
   316
    if( bank_flag )
nkeynes@108
   317
	banks[0]++;
nkeynes@103
   318
    
nkeynes@103
   319
    /* Handle non-aligned start of source */
nkeynes@103
   320
    if( srcaddr & 0x03 ) {
nkeynes@103
   321
	char *src = ((char *)banks[bank_flag]) + (srcaddr & 0x03);
nkeynes@103
   322
	for( i= srcaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@103
   323
	    *dest++ = *src++;
nkeynes@103
   324
	}
nkeynes@103
   325
	bank_flag = !bank_flag;
nkeynes@103
   326
    }
nkeynes@103
   327
nkeynes@103
   328
    dwdest = (uint32_t *)dest;
nkeynes@103
   329
    while( length >= 4 ) {
nkeynes@103
   330
	*dwdest++ = *banks[bank_flag]++;
nkeynes@103
   331
	bank_flag = !bank_flag;
nkeynes@103
   332
	length -= 4;
nkeynes@103
   333
    }
nkeynes@103
   334
    
nkeynes@103
   335
    /* Handle non-aligned end of source */
nkeynes@103
   336
    if( length ) {
nkeynes@103
   337
	dest = (char *)dwdest;
nkeynes@103
   338
	char *src = (char *)banks[bank_flag];
nkeynes@103
   339
	while( length-- > 0 ) {
nkeynes@103
   340
	    *dest++ = *src++;
nkeynes@103
   341
	}
nkeynes@103
   342
    }
nkeynes@103
   343
}
nkeynes@127
   344
nkeynes@127
   345
void pvr2_vram64_dump( sh4addr_t addr, uint32_t length, FILE *f ) 
nkeynes@127
   346
{
nkeynes@127
   347
    char tmp[length];
nkeynes@127
   348
    pvr2_vram64_read( tmp, addr, length );
nkeynes@127
   349
    fwrite_dump( tmp, length, f );
nkeynes@127
   350
}
.