Search
lxdream.org :: lxdream/src/pvr2/pvr2.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2.c
changeset 103:9b9cfc5855e0
prev100:995e42e96cc9
next106:9048bac046c3
author nkeynes
date Mon Mar 13 12:39:07 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change More rendering work in progress. Almost there now...
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@103
     2
 * $Id: pvr2.c,v 1.17 2006-03-13 12:39:07 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@103
    47
    int display_lines;
nkeynes@103
    48
    int line_time_ns;
nkeynes@103
    49
};
nkeynes@103
    50
nkeynes@103
    51
struct video_timing pal_timing = { 50, 625, 575, 32000 };
nkeynes@103
    52
struct video_timing ntsc_timing= { 60, 525, 480, 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@94
    64
    video_driver = &video_gtk_driver;
nkeynes@103
    65
    video_driver->set_display_format( 640, 480, COLFMT_RGB32 );
nkeynes@1
    66
}
nkeynes@1
    67
nkeynes@103
    68
uint32_t pvr2_line_count = 0;
nkeynes@103
    69
uint32_t pvr2_line_remainder = 0;
nkeynes@103
    70
uint32_t pvr2_irq_vpos1 = 0;
nkeynes@103
    71
uint32_t pvr2_irq_vpos2 = 0;
nkeynes@103
    72
struct video_timing *pvr2_timing = &ntsc_timing;
nkeynes@23
    73
uint32_t pvr2_time_counter = 0;
nkeynes@94
    74
uint32_t pvr2_frame_counter = 0;
nkeynes@30
    75
uint32_t pvr2_time_per_frame = 20000000;
nkeynes@23
    76
nkeynes@30
    77
uint32_t pvr2_run_slice( uint32_t nanosecs ) 
nkeynes@23
    78
{
nkeynes@103
    79
    pvr2_line_remainder += nanosecs;
nkeynes@103
    80
    while( pvr2_line_remainder >= pvr2_timing->line_time_ns ) {
nkeynes@103
    81
	pvr2_line_remainder -= pvr2_timing->line_time_ns;
nkeynes@103
    82
	pvr2_line_count++;
nkeynes@103
    83
	if( pvr2_line_count == pvr2_irq_vpos1 ) {
nkeynes@103
    84
	    asic_event( EVENT_SCANLINE1 );
nkeynes@103
    85
	} 
nkeynes@103
    86
	if( pvr2_line_count == pvr2_irq_vpos2 ) {
nkeynes@103
    87
	    asic_event( EVENT_SCANLINE2 );
nkeynes@103
    88
	}
nkeynes@103
    89
	if( pvr2_line_count == pvr2_timing->display_lines ) {
nkeynes@103
    90
	    asic_event( EVENT_RETRACE );
nkeynes@103
    91
	} else if( pvr2_line_count == pvr2_timing->total_lines ) {
nkeynes@103
    92
	    pvr2_display_frame();
nkeynes@103
    93
	    pvr2_line_count = 0;
nkeynes@103
    94
	}
nkeynes@23
    95
    }
nkeynes@30
    96
    return nanosecs;
nkeynes@23
    97
}
nkeynes@23
    98
nkeynes@1
    99
uint32_t vid_stride, vid_lpf, vid_ppl, vid_hres, vid_vres, vid_col;
nkeynes@1
   100
int interlaced, bChanged = 1, bEnabled = 0, vid_size = 0;
nkeynes@1
   101
char *frame_start; /* current video start address (in real memory) */
nkeynes@1
   102
nkeynes@103
   103
/**
nkeynes@1
   104
 * Display the next frame, copying the current contents of video ram to
nkeynes@1
   105
 * the window. If the video configuration has changed, first recompute the
nkeynes@1
   106
 * new frame size/depth.
nkeynes@1
   107
 */
nkeynes@94
   108
void pvr2_display_frame( void )
nkeynes@1
   109
{
nkeynes@103
   110
    uint32_t display_addr = MMIO_READ( PVR2, DISPADDR1 );
nkeynes@103
   111
    
nkeynes@94
   112
    int dispsize = MMIO_READ( PVR2, DISPSIZE );
nkeynes@94
   113
    int dispmode = MMIO_READ( PVR2, DISPMODE );
nkeynes@103
   114
    int vidcfg = MMIO_READ( PVR2, DISPCFG );
nkeynes@94
   115
    int vid_stride = ((dispsize & DISPSIZE_MODULO) >> 20) - 1;
nkeynes@94
   116
    int vid_lpf = ((dispsize & DISPSIZE_LPF) >> 10) + 1;
nkeynes@94
   117
    int vid_ppl = ((dispsize & DISPSIZE_PPL)) + 1;
nkeynes@103
   118
    gboolean bEnabled = (dispmode & DISPMODE_DE) && (vidcfg & DISPCFG_VO ) ? TRUE : FALSE;
nkeynes@103
   119
    gboolean interlaced = (vidcfg & DISPCFG_I ? TRUE : FALSE);
nkeynes@1
   120
    if( bEnabled ) {
nkeynes@94
   121
	video_buffer_t buffer = &video_buffer[video_buffer_idx];
nkeynes@94
   122
	video_buffer_idx = !video_buffer_idx;
nkeynes@94
   123
	video_buffer_t last = &video_buffer[video_buffer_idx];
nkeynes@94
   124
	buffer->rowstride = (vid_ppl + vid_stride) << 2;
nkeynes@94
   125
	buffer->data = frame_start = video_base + MMIO_READ( PVR2, DISPADDR1 );
nkeynes@94
   126
	buffer->vres = vid_lpf;
nkeynes@94
   127
	if( interlaced ) buffer->vres <<= 1;
nkeynes@103
   128
	switch( (dispmode & DISPMODE_COL) >> 2 ) {
nkeynes@103
   129
	case 0: 
nkeynes@103
   130
	    buffer->colour_format = COLFMT_ARGB1555;
nkeynes@103
   131
	    buffer->hres = vid_ppl << 1; 
nkeynes@103
   132
	    break;
nkeynes@103
   133
	case 1: 
nkeynes@103
   134
	    buffer->colour_format = COLFMT_RGB565;
nkeynes@103
   135
	    buffer->hres = vid_ppl << 1; 
nkeynes@103
   136
	    break;
nkeynes@103
   137
	case 2:
nkeynes@103
   138
	    buffer->colour_format = COLFMT_RGB888;
nkeynes@103
   139
	    buffer->hres = (vid_ppl << 2) / 3; 
nkeynes@103
   140
	    break;
nkeynes@103
   141
	case 3: 
nkeynes@103
   142
	    buffer->colour_format = COLFMT_ARGB8888;
nkeynes@103
   143
	    buffer->hres = vid_ppl; 
nkeynes@103
   144
	    break;
nkeynes@94
   145
	}
nkeynes@94
   146
	
nkeynes@94
   147
	if( video_driver != NULL ) {
nkeynes@94
   148
	    if( buffer->hres != last->hres ||
nkeynes@94
   149
		buffer->vres != last->vres ||
nkeynes@94
   150
		buffer->colour_format != last->colour_format) {
nkeynes@103
   151
		video_driver->set_display_format( buffer->hres, buffer->vres,
nkeynes@103
   152
						  buffer->colour_format );
nkeynes@94
   153
	    }
nkeynes@103
   154
	    if( MMIO_READ( PVR2, DISPCFG2 ) & 0x08 ) { /* Blanked */
nkeynes@100
   155
		uint32_t colour = MMIO_READ( PVR2, DISPBORDER );
nkeynes@94
   156
		video_driver->display_blank_frame( colour );
nkeynes@103
   157
	    } else if( !pvr2_render_display_frame( PVR2_RAM_BASE + display_addr ) ) {
nkeynes@94
   158
		video_driver->display_frame( buffer );
nkeynes@94
   159
	    }
nkeynes@65
   160
	}
nkeynes@1
   161
    } else {
nkeynes@94
   162
	video_buffer_idx = 0;
nkeynes@94
   163
	video_buffer[0].hres = video_buffer[0].vres = 0;
nkeynes@1
   164
    }
nkeynes@94
   165
    pvr2_frame_counter++;
nkeynes@1
   166
    asic_event( EVENT_SCANLINE1 );
nkeynes@1
   167
    asic_event( EVENT_SCANLINE2 );
nkeynes@1
   168
    asic_event( EVENT_RETRACE );
nkeynes@1
   169
}
nkeynes@1
   170
nkeynes@1
   171
void mmio_region_PVR2_write( uint32_t reg, uint32_t val )
nkeynes@1
   172
{
nkeynes@1
   173
    if( reg >= 0x200 && reg < 0x600 ) { /* Fog table */
nkeynes@1
   174
        MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   175
        /* I don't want to hear about these */
nkeynes@1
   176
        return;
nkeynes@1
   177
    }
nkeynes@1
   178
    
nkeynes@1
   179
    INFO( "PVR2 write to %08X <= %08X [%s: %s]", reg, val, 
nkeynes@1
   180
          MMIO_REGID(PVR2,reg), MMIO_REGDESC(PVR2,reg) );
nkeynes@1
   181
   
nkeynes@1
   182
    switch(reg) {
nkeynes@103
   183
    case VPOS_IRQ:
nkeynes@103
   184
	pvr2_irq_vpos1 = (val >> 16) & 0x03FF;
nkeynes@103
   185
	pvr2_irq_vpos2 = val & 0x03FF;
nkeynes@103
   186
	break;
nkeynes@100
   187
    case TAINIT:
nkeynes@100
   188
	if( val & 0x80000000 )
nkeynes@100
   189
	    pvr2_ta_init();
nkeynes@100
   190
	break;
nkeynes@65
   191
    case RENDSTART:
nkeynes@65
   192
	if( val == 0xFFFFFFFF )
nkeynes@65
   193
	    pvr2_render_scene();
nkeynes@65
   194
	break;
nkeynes@1
   195
    }
nkeynes@1
   196
    MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   197
}
nkeynes@1
   198
nkeynes@1
   199
MMIO_REGION_READ_FN( PVR2, reg )
nkeynes@1
   200
{
nkeynes@1
   201
    switch( reg ) {
nkeynes@1
   202
        case BEAMPOS:
nkeynes@2
   203
            return sh4r.icount&0x20 ? 0x2000 : 1;
nkeynes@1
   204
        default:
nkeynes@1
   205
            return MMIO_READ( PVR2, reg );
nkeynes@1
   206
    }
nkeynes@1
   207
}
nkeynes@19
   208
nkeynes@85
   209
MMIO_REGION_DEFFNS( PVR2PAL )
nkeynes@85
   210
nkeynes@19
   211
void pvr2_set_base_address( uint32_t base ) 
nkeynes@19
   212
{
nkeynes@19
   213
    mmio_region_PVR2_write( DISPADDR1, base );
nkeynes@19
   214
}
nkeynes@56
   215
nkeynes@56
   216
nkeynes@65
   217
nkeynes@98
   218
nkeynes@56
   219
int32_t mmio_region_PVR2TA_read( uint32_t reg )
nkeynes@56
   220
{
nkeynes@56
   221
    return 0xFFFFFFFF;
nkeynes@56
   222
}
nkeynes@56
   223
nkeynes@56
   224
void mmio_region_PVR2TA_write( uint32_t reg, uint32_t val )
nkeynes@56
   225
{
nkeynes@100
   226
    pvr2_ta_write( &val, sizeof(uint32_t) );
nkeynes@56
   227
}
nkeynes@56
   228
nkeynes@85
   229
nkeynes@103
   230
void pvr2_vram64_write( sh4addr_t destaddr, char *src, uint32_t length )
nkeynes@103
   231
{
nkeynes@103
   232
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@103
   233
    uint32_t *banks[2];
nkeynes@103
   234
    uint32_t *dwsrc;
nkeynes@103
   235
    int i;
nkeynes@65
   236
nkeynes@103
   237
    destaddr = destaddr & 0x7FFFFF;
nkeynes@103
   238
    if( destaddr + length > 0x800000 ) {
nkeynes@103
   239
	length = 0x800000 - destaddr;
nkeynes@103
   240
    }
nkeynes@103
   241
nkeynes@103
   242
    for( i=destaddr & 0xFFFFF000; i < destaddr + length; i+= PAGE_SIZE ) {
nkeynes@103
   243
	texcache_invalidate_page( i );
nkeynes@103
   244
    }
nkeynes@103
   245
nkeynes@103
   246
    banks[0] = ((uint32_t *)(video_base + (destaddr>>3)));
nkeynes@103
   247
    banks[1] = banks[0] + 0x100000;
nkeynes@103
   248
    
nkeynes@103
   249
    /* Handle non-aligned start of source */
nkeynes@103
   250
    if( destaddr & 0x03 ) {
nkeynes@103
   251
	char *dest = ((char *)banks[bank_flag]) + (destaddr & 0x03);
nkeynes@103
   252
	for( i= destaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@103
   253
	    *dest++ = *src++;
nkeynes@103
   254
	}
nkeynes@103
   255
	bank_flag = !bank_flag;
nkeynes@103
   256
    }
nkeynes@103
   257
nkeynes@103
   258
    dwsrc = (uint32_t *)src;
nkeynes@103
   259
    while( length >= 4 ) {
nkeynes@103
   260
	*banks[bank_flag]++ = *dwsrc++;
nkeynes@103
   261
	bank_flag = !bank_flag;
nkeynes@103
   262
	length -= 4;
nkeynes@103
   263
    }
nkeynes@103
   264
    
nkeynes@103
   265
    /* Handle non-aligned end of source */
nkeynes@103
   266
    if( length ) {
nkeynes@103
   267
	src = (char *)dwsrc;
nkeynes@103
   268
	char *dest = (char *)banks[bank_flag];
nkeynes@103
   269
	while( length-- > 0 ) {
nkeynes@103
   270
	    *dest++ = *src++;
nkeynes@103
   271
	}
nkeynes@103
   272
    }  
nkeynes@103
   273
nkeynes@103
   274
}
nkeynes@103
   275
nkeynes@103
   276
void pvr2_vram64_read( char *dest, sh4addr_t srcaddr, uint32_t length )
nkeynes@103
   277
{
nkeynes@103
   278
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@103
   279
    uint32_t *banks[2];
nkeynes@103
   280
    uint32_t *dwdest;
nkeynes@103
   281
    int i;
nkeynes@103
   282
nkeynes@103
   283
    srcaddr = srcaddr & 0x7FFFFF;
nkeynes@103
   284
    if( srcaddr + length > 0x800000 )
nkeynes@103
   285
	length = 0x800000 - srcaddr;
nkeynes@103
   286
nkeynes@103
   287
    banks[0] = ((uint32_t *)(video_base + (srcaddr>>3)));
nkeynes@103
   288
    banks[1] = banks[0] + 0x100000;
nkeynes@103
   289
    
nkeynes@103
   290
    /* Handle non-aligned start of source */
nkeynes@103
   291
    if( srcaddr & 0x03 ) {
nkeynes@103
   292
	char *src = ((char *)banks[bank_flag]) + (srcaddr & 0x03);
nkeynes@103
   293
	for( i= srcaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@103
   294
	    *dest++ = *src++;
nkeynes@103
   295
	}
nkeynes@103
   296
	bank_flag = !bank_flag;
nkeynes@103
   297
    }
nkeynes@103
   298
nkeynes@103
   299
    dwdest = (uint32_t *)dest;
nkeynes@103
   300
    while( length >= 4 ) {
nkeynes@103
   301
	*dwdest++ = *banks[bank_flag]++;
nkeynes@103
   302
	bank_flag = !bank_flag;
nkeynes@103
   303
	length -= 4;
nkeynes@103
   304
    }
nkeynes@103
   305
    
nkeynes@103
   306
    /* Handle non-aligned end of source */
nkeynes@103
   307
    if( length ) {
nkeynes@103
   308
	dest = (char *)dwdest;
nkeynes@103
   309
	char *src = (char *)banks[bank_flag];
nkeynes@103
   310
	while( length-- > 0 ) {
nkeynes@103
   311
	    *dest++ = *src++;
nkeynes@103
   312
	}
nkeynes@103
   313
    }
nkeynes@103
   314
}
.