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