Search
lxdream.org :: lxdream/src/pvr2/pvr2.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2.c
changeset 677:3ee62740ff8f
prev674:377d987db8f2
next728:4dfc293b9d96
author nkeynes
date Thu May 29 10:50:25 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Remove pvr2mmio.h include from pvr2.h (it's supposed to be moore or less private)
Move redraw function from driver into pvr2_redraw_display()
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@586
     2
 * $Id$
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@477
    20
#include <assert.h>
nkeynes@1
    21
#include "dream.h"
nkeynes@265
    22
#include "eventq.h"
nkeynes@144
    23
#include "display.h"
nkeynes@1
    24
#include "mem.h"
nkeynes@1
    25
#include "asic.h"
nkeynes@261
    26
#include "clock.h"
nkeynes@103
    27
#include "pvr2/pvr2.h"
nkeynes@677
    28
#include "pvr2/pvr2mmio.h"
nkeynes@669
    29
#include "pvr2/scene.h"
nkeynes@586
    30
#include "sh4/sh4.h"
nkeynes@1
    31
#define MMIO_IMPL
nkeynes@103
    32
#include "pvr2/pvr2mmio.h"
nkeynes@1
    33
nkeynes@502
    34
unsigned char *video_base;
nkeynes@1
    35
nkeynes@352
    36
#define MAX_RENDER_BUFFERS 4
nkeynes@352
    37
nkeynes@304
    38
#define HPOS_PER_FRAME 0
nkeynes@304
    39
#define HPOS_PER_LINECOUNT 1
nkeynes@304
    40
nkeynes@133
    41
static void pvr2_init( void );
nkeynes@133
    42
static void pvr2_reset( void );
nkeynes@133
    43
static uint32_t pvr2_run_slice( uint32_t );
nkeynes@133
    44
static void pvr2_save_state( FILE *f );
nkeynes@133
    45
static int pvr2_load_state( FILE *f );
nkeynes@265
    46
static void pvr2_update_raster_posn( uint32_t nanosecs );
nkeynes@304
    47
static void pvr2_schedule_scanline_event( int eventid, int line, int minimum_lines, int line_time_ns );
nkeynes@352
    48
static render_buffer_t pvr2_get_render_buffer( frame_buffer_t frame );
nkeynes@352
    49
static render_buffer_t pvr2_next_render_buffer( );
nkeynes@477
    50
static render_buffer_t pvr2_frame_buffer_to_render_buffer( frame_buffer_t frame );
nkeynes@265
    51
uint32_t pvr2_get_sync_status();
nkeynes@133
    52
nkeynes@94
    53
void pvr2_display_frame( void );
nkeynes@94
    54
nkeynes@477
    55
static int output_colour_formats[] = { COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGR888, COLFMT_BGRA8888 };
nkeynes@161
    56
nkeynes@133
    57
struct dreamcast_module pvr2_module = { "PVR2", pvr2_init, pvr2_reset, NULL, 
nkeynes@133
    58
					pvr2_run_slice, NULL,
nkeynes@133
    59
					pvr2_save_state, pvr2_load_state };
nkeynes@133
    60
nkeynes@103
    61
nkeynes@144
    62
display_driver_t display_driver = NULL;
nkeynes@15
    63
nkeynes@133
    64
struct pvr2_state {
nkeynes@133
    65
    uint32_t frame_count;
nkeynes@133
    66
    uint32_t line_count;
nkeynes@133
    67
    uint32_t line_remainder;
nkeynes@265
    68
    uint32_t cycles_run; /* Cycles already executed prior to main time slice */
nkeynes@304
    69
    uint32_t irq_hpos_line;
nkeynes@304
    70
    uint32_t irq_hpos_line_count;
nkeynes@304
    71
    uint32_t irq_hpos_mode;
nkeynes@304
    72
    uint32_t irq_hpos_time_ns; /* Time within the line */
nkeynes@133
    73
    uint32_t irq_vpos1;
nkeynes@133
    74
    uint32_t irq_vpos2;
nkeynes@261
    75
    uint32_t odd_even_field; /* 1 = odd, 0 = even */
nkeynes@674
    76
    int32_t palette_changed; /* TRUE if palette has changed since last render */
nkeynes@674
    77
    uint32_t padding; /* FIXME: Remove in next DST version */
nkeynes@261
    78
    /* timing */
nkeynes@261
    79
    uint32_t dot_clock;
nkeynes@261
    80
    uint32_t total_lines;
nkeynes@261
    81
    uint32_t line_size;
nkeynes@261
    82
    uint32_t line_time_ns;
nkeynes@261
    83
    uint32_t vsync_lines;
nkeynes@261
    84
    uint32_t hsync_width_ns;
nkeynes@261
    85
    uint32_t front_porch_ns;
nkeynes@261
    86
    uint32_t back_porch_ns;
nkeynes@265
    87
    uint32_t retrace_start_line;
nkeynes@265
    88
    uint32_t retrace_end_line;
nkeynes@674
    89
    int32_t interlaced;
nkeynes@133
    90
} pvr2_state;
nkeynes@15
    91
nkeynes@674
    92
static gchar *save_next_render_filename;
nkeynes@441
    93
static render_buffer_t render_buffers[MAX_RENDER_BUFFERS];
nkeynes@674
    94
static uint32_t render_buffer_count = 0;
nkeynes@441
    95
static render_buffer_t displayed_render_buffer = NULL;
nkeynes@545
    96
static uint32_t displayed_border_colour = 0;
nkeynes@133
    97
nkeynes@265
    98
/**
nkeynes@304
    99
 * Event handler for the hpos callback
nkeynes@265
   100
 */
nkeynes@304
   101
static void pvr2_hpos_callback( int eventid ) {
nkeynes@265
   102
    asic_event( eventid );
nkeynes@265
   103
    pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@304
   104
    if( pvr2_state.irq_hpos_mode == HPOS_PER_LINECOUNT ) {
nkeynes@304
   105
	pvr2_state.irq_hpos_line += pvr2_state.irq_hpos_line_count;
nkeynes@304
   106
	while( pvr2_state.irq_hpos_line > (pvr2_state.total_lines>>1) ) {
nkeynes@304
   107
	    pvr2_state.irq_hpos_line -= (pvr2_state.total_lines>>1);
nkeynes@304
   108
	}
nkeynes@304
   109
    }
nkeynes@304
   110
    pvr2_schedule_scanline_event( eventid, pvr2_state.irq_hpos_line, 1, 
nkeynes@304
   111
				  pvr2_state.irq_hpos_time_ns );
nkeynes@265
   112
}
nkeynes@265
   113
nkeynes@265
   114
/**
nkeynes@265
   115
 * Event handler for the scanline callbacks. Fires the corresponding
nkeynes@265
   116
 * ASIC event, and resets the timer for the next field.
nkeynes@265
   117
 */
nkeynes@265
   118
static void pvr2_scanline_callback( int eventid ) {
nkeynes@265
   119
    asic_event( eventid );
nkeynes@265
   120
    pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@265
   121
    if( eventid == EVENT_SCANLINE1 ) {
nkeynes@304
   122
	pvr2_schedule_scanline_event( eventid, pvr2_state.irq_vpos1, 1, 0 );
nkeynes@265
   123
    } else {
nkeynes@304
   124
	pvr2_schedule_scanline_event( eventid, pvr2_state.irq_vpos2, 1, 0 );
nkeynes@265
   125
    }
nkeynes@265
   126
}
nkeynes@265
   127
nkeynes@133
   128
static void pvr2_init( void )
nkeynes@1
   129
{
nkeynes@352
   130
    int i;
nkeynes@1
   131
    register_io_region( &mmio_region_PVR2 );
nkeynes@85
   132
    register_io_region( &mmio_region_PVR2PAL );
nkeynes@56
   133
    register_io_region( &mmio_region_PVR2TA );
nkeynes@304
   134
    register_event_callback( EVENT_HPOS, pvr2_hpos_callback );
nkeynes@265
   135
    register_event_callback( EVENT_SCANLINE1, pvr2_scanline_callback );
nkeynes@265
   136
    register_event_callback( EVENT_SCANLINE2, pvr2_scanline_callback );
nkeynes@1
   137
    video_base = mem_get_region_by_name( MEM_REGION_VIDEO );
nkeynes@133
   138
    texcache_init();
nkeynes@133
   139
    pvr2_reset();
nkeynes@214
   140
    pvr2_ta_reset();
nkeynes@674
   141
    save_next_render_filename = NULL;
nkeynes@352
   142
    for( i=0; i<MAX_RENDER_BUFFERS; i++ ) {
nkeynes@352
   143
	render_buffers[i] = NULL;
nkeynes@352
   144
    }
nkeynes@352
   145
    render_buffer_count = 0;
nkeynes@441
   146
    displayed_render_buffer = NULL;
nkeynes@545
   147
    displayed_border_colour = 0;
nkeynes@133
   148
}
nkeynes@133
   149
nkeynes@133
   150
static void pvr2_reset( void )
nkeynes@133
   151
{
nkeynes@477
   152
    int i;
nkeynes@133
   153
    pvr2_state.line_count = 0;
nkeynes@133
   154
    pvr2_state.line_remainder = 0;
nkeynes@265
   155
    pvr2_state.cycles_run = 0;
nkeynes@133
   156
    pvr2_state.irq_vpos1 = 0;
nkeynes@133
   157
    pvr2_state.irq_vpos2 = 0;
nkeynes@265
   158
    pvr2_state.dot_clock = PVR2_DOT_CLOCK;
nkeynes@265
   159
    pvr2_state.back_porch_ns = 4000;
nkeynes@337
   160
    pvr2_state.palette_changed = FALSE;
nkeynes@265
   161
    mmio_region_PVR2_write( DISP_TOTAL, 0x0270035F );
nkeynes@265
   162
    mmio_region_PVR2_write( DISP_SYNCTIME, 0x07D6A53F );
nkeynes@284
   163
    mmio_region_PVR2_write( YUV_ADDR, 0 );
nkeynes@284
   164
    mmio_region_PVR2_write( YUV_CFG, 0 );
nkeynes@133
   165
    
nkeynes@133
   166
    pvr2_ta_init();
nkeynes@133
   167
    texcache_flush();
nkeynes@477
   168
    if( display_driver ) {
nkeynes@477
   169
	display_driver->display_blank(0);
nkeynes@477
   170
	for( i=0; i<render_buffer_count; i++ ) {
nkeynes@477
   171
	    display_driver->destroy_render_buffer(render_buffers[i]);
nkeynes@477
   172
	    render_buffers[i] = NULL;
nkeynes@477
   173
	}
nkeynes@477
   174
	render_buffer_count = 0;
nkeynes@477
   175
    }
nkeynes@133
   176
}
nkeynes@133
   177
nkeynes@477
   178
void pvr2_save_render_buffer( FILE *f, render_buffer_t buffer )
nkeynes@477
   179
{
nkeynes@477
   180
    struct frame_buffer fbuf;
nkeynes@477
   181
nkeynes@477
   182
    fbuf.width = buffer->width;
nkeynes@477
   183
    fbuf.height = buffer->height;
nkeynes@477
   184
    fbuf.rowstride = fbuf.width*3;
nkeynes@477
   185
    fbuf.colour_format = COLFMT_BGR888;
nkeynes@477
   186
    fbuf.inverted = buffer->inverted;
nkeynes@477
   187
    fbuf.data = g_malloc0( buffer->width * buffer->height * 3 );
nkeynes@477
   188
    
nkeynes@477
   189
    display_driver->read_render_buffer( fbuf.data, buffer, fbuf.rowstride, COLFMT_BGR888 );
nkeynes@477
   190
    write_png_to_stream( f, &fbuf );
nkeynes@477
   191
    g_free( fbuf.data );
nkeynes@477
   192
nkeynes@477
   193
    fwrite( &buffer->rowstride, sizeof(buffer->rowstride), 1, f );
nkeynes@477
   194
    fwrite( &buffer->colour_format, sizeof(buffer->colour_format), 1, f );
nkeynes@477
   195
    fwrite( &buffer->address, sizeof(buffer->address), 1, f );
nkeynes@477
   196
    fwrite( &buffer->scale, sizeof(buffer->scale), 1, f );
nkeynes@674
   197
    int32_t flushed = (int32_t)buffer->flushed; // Force to 32-bits for save-file consistency
nkeynes@674
   198
    fwrite( &flushed, sizeof(flushed), 1, f );
nkeynes@477
   199
    
nkeynes@477
   200
}
nkeynes@477
   201
nkeynes@477
   202
render_buffer_t pvr2_load_render_buffer( FILE *f )
nkeynes@477
   203
{
nkeynes@477
   204
    frame_buffer_t frame = read_png_from_stream( f );
nkeynes@477
   205
    if( frame == NULL ) {
nkeynes@477
   206
	return NULL;
nkeynes@477
   207
    }
nkeynes@477
   208
nkeynes@477
   209
    render_buffer_t buffer = pvr2_frame_buffer_to_render_buffer(frame);
nkeynes@653
   210
    if( buffer != NULL ) {
nkeynes@674
   211
        int32_t flushed;
nkeynes@653
   212
	fread( &buffer->rowstride, sizeof(buffer->rowstride), 1, f );
nkeynes@653
   213
	fread( &buffer->colour_format, sizeof(buffer->colour_format), 1, f );
nkeynes@653
   214
	fread( &buffer->address, sizeof(buffer->address), 1, f );
nkeynes@653
   215
	fread( &buffer->scale, sizeof(buffer->scale), 1, f );
nkeynes@674
   216
	fread( &flushed, sizeof(flushed), 1, f );
nkeynes@674
   217
	buffer->flushed = (gboolean)flushed;
nkeynes@653
   218
    } else {
nkeynes@653
   219
	fseek( f, sizeof(buffer->rowstride)+sizeof(buffer->colour_format)+
nkeynes@653
   220
	       sizeof(buffer->address)+sizeof(buffer->scale)+
nkeynes@674
   221
	       sizeof(int32_t), SEEK_CUR );
nkeynes@653
   222
    }
nkeynes@477
   223
    return buffer;
nkeynes@477
   224
}
nkeynes@477
   225
nkeynes@477
   226
nkeynes@477
   227
nkeynes@477
   228
nkeynes@477
   229
void pvr2_save_render_buffers( FILE *f )
nkeynes@477
   230
{
nkeynes@477
   231
    int i;
nkeynes@674
   232
    uint32_t has_frontbuffer;
nkeynes@477
   233
    fwrite( &render_buffer_count, sizeof(render_buffer_count), 1, f );
nkeynes@477
   234
    if( displayed_render_buffer != NULL ) {
nkeynes@674
   235
	has_frontbuffer = 1;
nkeynes@674
   236
	fwrite( &has_frontbuffer, sizeof(has_frontbuffer), 1, f );
nkeynes@477
   237
	pvr2_save_render_buffer( f, displayed_render_buffer );
nkeynes@477
   238
    } else {
nkeynes@674
   239
	has_frontbuffer = 0;
nkeynes@674
   240
	fwrite( &has_frontbuffer, sizeof(has_frontbuffer), 1, f );
nkeynes@477
   241
    }
nkeynes@477
   242
nkeynes@477
   243
    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@477
   244
	if( render_buffers[i] != displayed_render_buffer && render_buffers[i] != NULL ) {
nkeynes@477
   245
	    pvr2_save_render_buffer( f, render_buffers[i] );
nkeynes@477
   246
	}
nkeynes@477
   247
    }
nkeynes@477
   248
}
nkeynes@477
   249
nkeynes@477
   250
gboolean pvr2_load_render_buffers( FILE *f )
nkeynes@477
   251
{
nkeynes@674
   252
    uint32_t count, has_frontbuffer;
nkeynes@674
   253
    int i;
nkeynes@477
   254
nkeynes@477
   255
    fread( &count, sizeof(count), 1, f );
nkeynes@511
   256
    if( count > MAX_RENDER_BUFFERS ) {
nkeynes@477
   257
	return FALSE;
nkeynes@477
   258
    }
nkeynes@477
   259
    fread( &has_frontbuffer, sizeof(has_frontbuffer), 1, f );
nkeynes@477
   260
    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@477
   261
	display_driver->destroy_render_buffer(render_buffers[i]);
nkeynes@477
   262
	render_buffers[i] = NULL;
nkeynes@477
   263
    }
nkeynes@477
   264
    render_buffer_count = 0;
nkeynes@477
   265
nkeynes@477
   266
    if( has_frontbuffer ) {
nkeynes@477
   267
	displayed_render_buffer = pvr2_load_render_buffer(f);
nkeynes@477
   268
	display_driver->display_render_buffer( displayed_render_buffer );
nkeynes@477
   269
	count--;
nkeynes@477
   270
    }
nkeynes@477
   271
nkeynes@477
   272
    for( i=0; i<count; i++ ) {
nkeynes@653
   273
	pvr2_load_render_buffer( f );
nkeynes@477
   274
    }
nkeynes@477
   275
    return TRUE;
nkeynes@477
   276
}
nkeynes@477
   277
    
nkeynes@477
   278
nkeynes@133
   279
static void pvr2_save_state( FILE *f )
nkeynes@133
   280
{
nkeynes@477
   281
    pvr2_save_render_buffers( f );
nkeynes@133
   282
    fwrite( &pvr2_state, sizeof(pvr2_state), 1, f );
nkeynes@193
   283
    pvr2_ta_save_state( f );
nkeynes@295
   284
    pvr2_yuv_save_state( f );
nkeynes@133
   285
}
nkeynes@133
   286
nkeynes@133
   287
static int pvr2_load_state( FILE *f )
nkeynes@133
   288
{
nkeynes@477
   289
    if( !pvr2_load_render_buffers(f) )
nkeynes@477
   290
	return 1;
nkeynes@153
   291
    if( fread( &pvr2_state, sizeof(pvr2_state), 1, f ) != 1 )
nkeynes@153
   292
	return 1;
nkeynes@295
   293
    if( pvr2_ta_load_state(f) ) {
nkeynes@295
   294
	return 1;
nkeynes@295
   295
    }
nkeynes@295
   296
    return pvr2_yuv_load_state(f);
nkeynes@133
   297
}
nkeynes@133
   298
nkeynes@265
   299
/**
nkeynes@265
   300
 * Update the current raster position to the given number of nanoseconds,
nkeynes@265
   301
 * relative to the last time slice. (ie the raster will be adjusted forward
nkeynes@265
   302
 * by nanosecs - nanosecs_already_run_this_timeslice)
nkeynes@265
   303
 */
nkeynes@265
   304
static void pvr2_update_raster_posn( uint32_t nanosecs )
nkeynes@265
   305
{
nkeynes@265
   306
    uint32_t old_line_count = pvr2_state.line_count;
nkeynes@265
   307
    if( pvr2_state.line_time_ns == 0 ) {
nkeynes@265
   308
	return; /* do nothing */
nkeynes@265
   309
    }
nkeynes@265
   310
    pvr2_state.line_remainder += (nanosecs - pvr2_state.cycles_run);
nkeynes@265
   311
    pvr2_state.cycles_run = nanosecs;
nkeynes@265
   312
    while( pvr2_state.line_remainder >= pvr2_state.line_time_ns ) {
nkeynes@265
   313
	pvr2_state.line_count ++;
nkeynes@265
   314
	pvr2_state.line_remainder -= pvr2_state.line_time_ns;
nkeynes@265
   315
    }
nkeynes@265
   316
nkeynes@265
   317
    if( pvr2_state.line_count >= pvr2_state.total_lines ) {
nkeynes@265
   318
	pvr2_state.line_count -= pvr2_state.total_lines;
nkeynes@265
   319
	if( pvr2_state.interlaced ) {
nkeynes@265
   320
	    pvr2_state.odd_even_field = !pvr2_state.odd_even_field;
nkeynes@265
   321
	}
nkeynes@265
   322
    }
nkeynes@265
   323
    if( pvr2_state.line_count >= pvr2_state.retrace_end_line &&
nkeynes@265
   324
	(old_line_count < pvr2_state.retrace_end_line ||
nkeynes@265
   325
	 old_line_count > pvr2_state.line_count) ) {
nkeynes@337
   326
	pvr2_state.frame_count++;
nkeynes@265
   327
	pvr2_display_frame();
nkeynes@265
   328
    }
nkeynes@265
   329
}
nkeynes@265
   330
nkeynes@133
   331
static uint32_t pvr2_run_slice( uint32_t nanosecs ) 
nkeynes@133
   332
{
nkeynes@265
   333
    pvr2_update_raster_posn( nanosecs );
nkeynes@265
   334
    pvr2_state.cycles_run = 0;
nkeynes@133
   335
    return nanosecs;
nkeynes@133
   336
}
nkeynes@133
   337
nkeynes@133
   338
int pvr2_get_frame_count() 
nkeynes@133
   339
{
nkeynes@133
   340
    return pvr2_state.frame_count;
nkeynes@106
   341
}
nkeynes@106
   342
nkeynes@677
   343
void pvr2_redraw_display()
nkeynes@477
   344
{
nkeynes@677
   345
    if( display_driver != NULL ) {
nkeynes@677
   346
        if( displayed_render_buffer == NULL ) {
nkeynes@677
   347
            display_driver->display_blank(displayed_border_colour);
nkeynes@677
   348
        } else {
nkeynes@677
   349
            display_driver->display_render_buffer(displayed_render_buffer);
nkeynes@677
   350
        }
nkeynes@677
   351
    }
nkeynes@545
   352
}
nkeynes@545
   353
nkeynes@295
   354
gboolean pvr2_save_next_scene( const gchar *filename )
nkeynes@295
   355
{
nkeynes@674
   356
    if( save_next_render_filename != NULL ) {
nkeynes@674
   357
	g_free( save_next_render_filename );
nkeynes@295
   358
    } 
nkeynes@674
   359
    save_next_render_filename = g_strdup(filename);
nkeynes@295
   360
    return TRUE;
nkeynes@295
   361
}
nkeynes@295
   362
nkeynes@295
   363
nkeynes@295
   364
nkeynes@103
   365
/**
nkeynes@1
   366
 * Display the next frame, copying the current contents of video ram to
nkeynes@1
   367
 * the window. If the video configuration has changed, first recompute the
nkeynes@1
   368
 * new frame size/depth.
nkeynes@1
   369
 */
nkeynes@94
   370
void pvr2_display_frame( void )
nkeynes@1
   371
{
nkeynes@197
   372
    int dispmode = MMIO_READ( PVR2, DISP_MODE );
nkeynes@261
   373
    int vidcfg = MMIO_READ( PVR2, DISP_SYNCCFG );
nkeynes@335
   374
    gboolean bEnabled = (dispmode & DISPMODE_ENABLE) && (vidcfg & DISPCFG_VO ) ? TRUE : FALSE;
nkeynes@352
   375
nkeynes@352
   376
    if( display_driver == NULL ) {
nkeynes@352
   377
	return; /* can't really do anything much */
nkeynes@352
   378
    } else if( !bEnabled ) {
nkeynes@352
   379
	/* Output disabled == black */
nkeynes@545
   380
	displayed_render_buffer = NULL;
nkeynes@545
   381
	displayed_border_colour = 0;
nkeynes@352
   382
	display_driver->display_blank( 0 ); 
nkeynes@352
   383
    } else if( MMIO_READ( PVR2, DISP_CFG2 ) & 0x08 ) { 
nkeynes@352
   384
	/* Enabled but blanked - border colour */
nkeynes@545
   385
	displayed_border_colour = MMIO_READ( PVR2, DISP_BORDER );
nkeynes@441
   386
	displayed_render_buffer = NULL;
nkeynes@545
   387
	display_driver->display_blank( displayed_border_colour );
nkeynes@352
   388
    } else {
nkeynes@352
   389
	/* Real output - determine dimensions etc */
nkeynes@352
   390
	struct frame_buffer fbuf;
nkeynes@352
   391
	uint32_t dispsize = MMIO_READ( PVR2, DISP_SIZE );
nkeynes@352
   392
	int vid_stride = (((dispsize & DISPSIZE_MODULO) >> 20) - 1);
nkeynes@352
   393
	int vid_ppl = ((dispsize & DISPSIZE_PPL)) + 1;
nkeynes@352
   394
nkeynes@352
   395
	fbuf.colour_format = output_colour_formats[(dispmode & DISPMODE_COLFMT) >> 2];
nkeynes@352
   396
	fbuf.width = vid_ppl << 2 / colour_formats[fbuf.colour_format].bpp;
nkeynes@352
   397
	fbuf.height = ((dispsize & DISPSIZE_LPF) >> 10) + 1;
nkeynes@352
   398
	fbuf.size = vid_ppl << 2 * fbuf.height;
nkeynes@352
   399
	fbuf.rowstride = (vid_ppl + vid_stride) << 2;
nkeynes@352
   400
nkeynes@352
   401
	/* Determine the field to display, and deinterlace if possible */
nkeynes@352
   402
	if( pvr2_state.interlaced ) {
nkeynes@352
   403
	    if( vid_ppl == vid_stride ) { /* Magic deinterlace */
nkeynes@352
   404
		fbuf.height = fbuf.height << 1;
nkeynes@352
   405
		fbuf.rowstride = vid_ppl << 2;
nkeynes@352
   406
		fbuf.address = MMIO_READ( PVR2, DISP_ADDR1 );
nkeynes@352
   407
	    } else { 
nkeynes@352
   408
		/* Just display the field as is, folks. This is slightly tricky -
nkeynes@352
   409
		 * we pick the field based on which frame is about to come through,
nkeynes@352
   410
		 * which may not be the same as the odd_even_field.
nkeynes@352
   411
		 */
nkeynes@352
   412
		gboolean oddfield = pvr2_state.odd_even_field;
nkeynes@352
   413
		if( pvr2_state.line_count >= pvr2_state.retrace_start_line ) {
nkeynes@352
   414
		    oddfield = !oddfield;
nkeynes@352
   415
		}
nkeynes@352
   416
		if( oddfield ) {
nkeynes@352
   417
		    fbuf.address = MMIO_READ( PVR2, DISP_ADDR1 );
nkeynes@352
   418
		} else {
nkeynes@352
   419
		    fbuf.address = MMIO_READ( PVR2, DISP_ADDR2 );
nkeynes@352
   420
		}
nkeynes@337
   421
	    }
nkeynes@352
   422
	} else {
nkeynes@352
   423
	    fbuf.address = MMIO_READ( PVR2, DISP_ADDR1 );
nkeynes@335
   424
	}
nkeynes@352
   425
	fbuf.address = (fbuf.address & 0x00FFFFFF) + PVR2_RAM_BASE;
nkeynes@477
   426
	fbuf.inverted = FALSE;
nkeynes@477
   427
	fbuf.data = video_base + (fbuf.address&0x00FFFFFF);
nkeynes@352
   428
nkeynes@352
   429
	render_buffer_t rbuf = pvr2_get_render_buffer( &fbuf );
nkeynes@477
   430
	if( rbuf == NULL ) {
nkeynes@477
   431
	    rbuf = pvr2_frame_buffer_to_render_buffer( &fbuf );
nkeynes@477
   432
	}
nkeynes@441
   433
	displayed_render_buffer = rbuf;
nkeynes@352
   434
	if( rbuf != NULL ) {
nkeynes@352
   435
	    display_driver->display_render_buffer( rbuf );
nkeynes@65
   436
	}
nkeynes@1
   437
    }
nkeynes@1
   438
}
nkeynes@1
   439
nkeynes@197
   440
/**
nkeynes@197
   441
 * This has to handle every single register individually as they all get masked 
nkeynes@197
   442
 * off differently (and its easier to do it at write time)
nkeynes@197
   443
 */
nkeynes@1
   444
void mmio_region_PVR2_write( uint32_t reg, uint32_t val )
nkeynes@1
   445
{
nkeynes@1
   446
    if( reg >= 0x200 && reg < 0x600 ) { /* Fog table */
nkeynes@1
   447
        MMIO_WRITE( PVR2, reg, val );
nkeynes@1
   448
        return;
nkeynes@1
   449
    }
nkeynes@1
   450
    
nkeynes@1
   451
    switch(reg) {
nkeynes@189
   452
    case PVRID:
nkeynes@189
   453
    case PVRVER:
nkeynes@261
   454
    case GUNPOS: /* Read only registers */
nkeynes@189
   455
	break;
nkeynes@197
   456
    case PVRRESET:
nkeynes@197
   457
	val &= 0x00000007; /* Do stuff? */
nkeynes@197
   458
	MMIO_WRITE( PVR2, reg, val );
nkeynes@197
   459
	break;
nkeynes@295
   460
    case RENDER_START: /* Don't really care what value */
nkeynes@674
   461
	if( save_next_render_filename != NULL ) {
nkeynes@674
   462
	    if( pvr2_render_save_scene(save_next_render_filename) == 0 ) {
nkeynes@674
   463
		INFO( "Saved scene to %s", save_next_render_filename);
nkeynes@295
   464
	    }
nkeynes@674
   465
	    g_free( save_next_render_filename );
nkeynes@674
   466
	    save_next_render_filename = NULL;
nkeynes@295
   467
	}
nkeynes@653
   468
	pvr2_scene_read();
nkeynes@352
   469
	render_buffer_t buffer = pvr2_next_render_buffer();
nkeynes@373
   470
	if( buffer != NULL ) {
nkeynes@653
   471
	    pvr2_scene_render( buffer );
nkeynes@373
   472
	}
nkeynes@352
   473
	asic_event( EVENT_PVR_RENDER_DONE );
nkeynes@189
   474
	break;
nkeynes@191
   475
    case RENDER_POLYBASE:
nkeynes@191
   476
    	MMIO_WRITE( PVR2, reg, val&0x00F00000 );
nkeynes@191
   477
    	break;
nkeynes@191
   478
    case RENDER_TSPCFG:
nkeynes@191
   479
    	MMIO_WRITE( PVR2, reg, val&0x00010101 );
nkeynes@191
   480
    	break;
nkeynes@197
   481
    case DISP_BORDER:
nkeynes@191
   482
    	MMIO_WRITE( PVR2, reg, val&0x01FFFFFF );
nkeynes@191
   483
    	break;
nkeynes@197
   484
    case DISP_MODE:
nkeynes@191
   485
    	MMIO_WRITE( PVR2, reg, val&0x00FFFF7F );
nkeynes@191
   486
    	break;
nkeynes@191
   487
    case RENDER_MODE:
nkeynes@191
   488
    	MMIO_WRITE( PVR2, reg, val&0x00FFFF0F );
nkeynes@191
   489
    	break;
nkeynes@191
   490
    case RENDER_SIZE:
nkeynes@191
   491
    	MMIO_WRITE( PVR2, reg, val&0x000001FF );
nkeynes@191
   492
    	break;
nkeynes@197
   493
    case DISP_ADDR1:
nkeynes@189
   494
	val &= 0x00FFFFFC;
nkeynes@189
   495
	MMIO_WRITE( PVR2, reg, val );
nkeynes@265
   496
	pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@108
   497
	break;
nkeynes@197
   498
    case DISP_ADDR2:
nkeynes@191
   499
    	MMIO_WRITE( PVR2, reg, val&0x00FFFFFC );
nkeynes@337
   500
	pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@191
   501
    	break;
nkeynes@197
   502
    case DISP_SIZE:
nkeynes@191
   503
    	MMIO_WRITE( PVR2, reg, val&0x3FFFFFFF );
nkeynes@191
   504
    	break;
nkeynes@191
   505
    case RENDER_ADDR1:
nkeynes@191
   506
    case RENDER_ADDR2:
nkeynes@191
   507
    	MMIO_WRITE( PVR2, reg, val&0x01FFFFFC );
nkeynes@191
   508
    	break;
nkeynes@191
   509
    case RENDER_HCLIP:
nkeynes@191
   510
	MMIO_WRITE( PVR2, reg, val&0x07FF07FF );
nkeynes@189
   511
	break;
nkeynes@191
   512
    case RENDER_VCLIP:
nkeynes@191
   513
	MMIO_WRITE( PVR2, reg, val&0x03FF03FF );
nkeynes@189
   514
	break;
nkeynes@197
   515
    case DISP_HPOSIRQ:
nkeynes@191
   516
	MMIO_WRITE( PVR2, reg, val&0x03FF33FF );
nkeynes@304
   517
	pvr2_state.irq_hpos_line = val & 0x03FF;
nkeynes@304
   518
	pvr2_state.irq_hpos_time_ns = 2000000*((val>>16)&0x03FF)/pvr2_state.dot_clock;
nkeynes@304
   519
	pvr2_state.irq_hpos_mode = (val >> 12) & 0x03;
nkeynes@304
   520
	switch( pvr2_state.irq_hpos_mode ) {
nkeynes@304
   521
	case 3: /* Reserved - treat as 0 */
nkeynes@304
   522
	case 0: /* Once per frame at specified line */
nkeynes@304
   523
	    pvr2_state.irq_hpos_mode = HPOS_PER_FRAME;
nkeynes@304
   524
	    break;
nkeynes@304
   525
	case 2: /* Once per line - as per-line-count */
nkeynes@304
   526
	    pvr2_state.irq_hpos_line = 1;
nkeynes@304
   527
	    pvr2_state.irq_hpos_mode = 1;
nkeynes@304
   528
	case 1: /* Once per N lines */
nkeynes@304
   529
	    pvr2_state.irq_hpos_line_count = pvr2_state.irq_hpos_line;
nkeynes@304
   530
	    pvr2_state.irq_hpos_line = (pvr2_state.line_count >> 1) + 
nkeynes@304
   531
		pvr2_state.irq_hpos_line_count;
nkeynes@304
   532
	    while( pvr2_state.irq_hpos_line > (pvr2_state.total_lines>>1) ) {
nkeynes@304
   533
		pvr2_state.irq_hpos_line -= (pvr2_state.total_lines>>1);
nkeynes@304
   534
	    }
nkeynes@304
   535
	    pvr2_state.irq_hpos_mode = HPOS_PER_LINECOUNT;
nkeynes@304
   536
	}
nkeynes@304
   537
	pvr2_schedule_scanline_event( EVENT_HPOS, pvr2_state.irq_hpos_line, 0,
nkeynes@304
   538
					  pvr2_state.irq_hpos_time_ns );
nkeynes@189
   539
	break;
nkeynes@197
   540
    case DISP_VPOSIRQ:
nkeynes@189
   541
	val = val & 0x03FF03FF;
nkeynes@189
   542
	pvr2_state.irq_vpos1 = (val >> 16);
nkeynes@133
   543
	pvr2_state.irq_vpos2 = val & 0x03FF;
nkeynes@265
   544
	pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@304
   545
	pvr2_schedule_scanline_event( EVENT_SCANLINE1, pvr2_state.irq_vpos1, 0, 0 );
nkeynes@304
   546
	pvr2_schedule_scanline_event( EVENT_SCANLINE2, pvr2_state.irq_vpos2, 0, 0 );
nkeynes@189
   547
	MMIO_WRITE( PVR2, reg, val );
nkeynes@103
   548
	break;
nkeynes@197
   549
    case RENDER_NEARCLIP:
nkeynes@197
   550
	MMIO_WRITE( PVR2, reg, val & 0x7FFFFFFF );
nkeynes@197
   551
	break;
nkeynes@191
   552
    case RENDER_SHADOW:
nkeynes@191
   553
	MMIO_WRITE( PVR2, reg, val&0x000001FF );
nkeynes@191
   554
	break;
nkeynes@191
   555
    case RENDER_OBJCFG:
nkeynes@191
   556
    	MMIO_WRITE( PVR2, reg, val&0x003FFFFF );
nkeynes@191
   557
    	break;
nkeynes@191
   558
    case RENDER_TSPCLIP:
nkeynes@191
   559
    	MMIO_WRITE( PVR2, reg, val&0x7FFFFFFF );
nkeynes@191
   560
    	break;
nkeynes@197
   561
    case RENDER_FARCLIP:
nkeynes@197
   562
	MMIO_WRITE( PVR2, reg, val&0xFFFFFFF0 );
nkeynes@197
   563
	break;
nkeynes@191
   564
    case RENDER_BGPLANE:
nkeynes@191
   565
    	MMIO_WRITE( PVR2, reg, val&0x1FFFFFFF );
nkeynes@191
   566
    	break;
nkeynes@191
   567
    case RENDER_ISPCFG:
nkeynes@191
   568
    	MMIO_WRITE( PVR2, reg, val&0x00FFFFF9 );
nkeynes@191
   569
    	break;
nkeynes@197
   570
    case VRAM_CFG1:
nkeynes@197
   571
	MMIO_WRITE( PVR2, reg, val&0x000000FF );
nkeynes@197
   572
	break;
nkeynes@197
   573
    case VRAM_CFG2:
nkeynes@197
   574
	MMIO_WRITE( PVR2, reg, val&0x003FFFFF );
nkeynes@197
   575
	break;
nkeynes@197
   576
    case VRAM_CFG3:
nkeynes@197
   577
	MMIO_WRITE( PVR2, reg, val&0x1FFFFFFF );
nkeynes@197
   578
	break;
nkeynes@197
   579
    case RENDER_FOGTBLCOL:
nkeynes@197
   580
    case RENDER_FOGVRTCOL:
nkeynes@197
   581
	MMIO_WRITE( PVR2, reg, val&0x00FFFFFF );
nkeynes@197
   582
	break;
nkeynes@197
   583
    case RENDER_FOGCOEFF:
nkeynes@197
   584
	MMIO_WRITE( PVR2, reg, val&0x0000FFFF );
nkeynes@197
   585
	break;
nkeynes@197
   586
    case RENDER_CLAMPHI:
nkeynes@197
   587
    case RENDER_CLAMPLO:
nkeynes@197
   588
	MMIO_WRITE( PVR2, reg, val );
nkeynes@197
   589
	break;
nkeynes@261
   590
    case RENDER_TEXSIZE:
nkeynes@261
   591
	MMIO_WRITE( PVR2, reg, val&0x00031F1F );
nkeynes@197
   592
	break;
nkeynes@261
   593
    case RENDER_PALETTE:
nkeynes@261
   594
	MMIO_WRITE( PVR2, reg, val&0x00000003 );
nkeynes@261
   595
	break;
nkeynes@653
   596
    case RENDER_ALPHA_REF:
nkeynes@653
   597
	MMIO_WRITE( PVR2, reg, val&0x000000FF );
nkeynes@653
   598
	break;
nkeynes@261
   599
	/********** CRTC registers *************/
nkeynes@197
   600
    case DISP_HBORDER:
nkeynes@197
   601
    case DISP_VBORDER:
nkeynes@197
   602
	MMIO_WRITE( PVR2, reg, val&0x03FF03FF );
nkeynes@197
   603
	break;
nkeynes@261
   604
    case DISP_TOTAL:
nkeynes@261
   605
	val = val & 0x03FF03FF;
nkeynes@261
   606
	MMIO_WRITE( PVR2, reg, val );
nkeynes@265
   607
	pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@261
   608
	pvr2_state.total_lines = (val >> 16) + 1;
nkeynes@261
   609
	pvr2_state.line_size = (val & 0x03FF) + 1;
nkeynes@261
   610
	pvr2_state.line_time_ns = 1000000 * pvr2_state.line_size / pvr2_state.dot_clock;
nkeynes@265
   611
	pvr2_state.retrace_end_line = 0x2A;
nkeynes@265
   612
	pvr2_state.retrace_start_line = pvr2_state.total_lines - 6;
nkeynes@304
   613
	pvr2_schedule_scanline_event( EVENT_SCANLINE1, pvr2_state.irq_vpos1, 0, 0 );
nkeynes@304
   614
	pvr2_schedule_scanline_event( EVENT_SCANLINE2, pvr2_state.irq_vpos2, 0, 0 );
nkeynes@304
   615
	pvr2_schedule_scanline_event( EVENT_HPOS, pvr2_state.irq_hpos_line, 0, 
nkeynes@304
   616
					  pvr2_state.irq_hpos_time_ns );
nkeynes@261
   617
	break;
nkeynes@261
   618
    case DISP_SYNCCFG:
nkeynes@261
   619
	MMIO_WRITE( PVR2, reg, val&0x000003FF );
nkeynes@261
   620
	pvr2_state.interlaced = (val & 0x0010) ? TRUE : FALSE;
nkeynes@261
   621
	break;
nkeynes@261
   622
    case DISP_SYNCTIME:
nkeynes@261
   623
	pvr2_state.vsync_lines = (val >> 8) & 0x0F;
nkeynes@269
   624
	pvr2_state.hsync_width_ns = ((val & 0x7F) + 1) * 2000000 / pvr2_state.dot_clock;
nkeynes@197
   625
	MMIO_WRITE( PVR2, reg, val&0xFFFFFF7F );
nkeynes@197
   626
	break;
nkeynes@197
   627
    case DISP_CFG2:
nkeynes@197
   628
	MMIO_WRITE( PVR2, reg, val&0x003F01FF );
nkeynes@197
   629
	break;
nkeynes@197
   630
    case DISP_HPOS:
nkeynes@261
   631
	val = val & 0x03FF;
nkeynes@261
   632
	pvr2_state.front_porch_ns = (val + 1) * 1000000 / pvr2_state.dot_clock;
nkeynes@261
   633
	MMIO_WRITE( PVR2, reg, val );
nkeynes@197
   634
	break;
nkeynes@197
   635
    case DISP_VPOS:
nkeynes@197
   636
	MMIO_WRITE( PVR2, reg, val&0x03FF03FF );
nkeynes@197
   637
	break;
nkeynes@261
   638
nkeynes@261
   639
	/*********** Tile accelerator registers ***********/
nkeynes@261
   640
    case TA_POLYPOS:
nkeynes@261
   641
    case TA_LISTPOS:
nkeynes@261
   642
	/* Readonly registers */
nkeynes@197
   643
	break;
nkeynes@189
   644
    case TA_TILEBASE:
nkeynes@193
   645
    case TA_LISTEND:
nkeynes@189
   646
    case TA_LISTBASE:
nkeynes@191
   647
	MMIO_WRITE( PVR2, reg, val&0x00FFFFE0 );
nkeynes@189
   648
	break;
nkeynes@191
   649
    case RENDER_TILEBASE:
nkeynes@189
   650
    case TA_POLYBASE:
nkeynes@189
   651
    case TA_POLYEND:
nkeynes@191
   652
	MMIO_WRITE( PVR2, reg, val&0x00FFFFFC );
nkeynes@189
   653
	break;
nkeynes@189
   654
    case TA_TILESIZE:
nkeynes@191
   655
	MMIO_WRITE( PVR2, reg, val&0x000F003F );
nkeynes@189
   656
	break;
nkeynes@189
   657
    case TA_TILECFG:
nkeynes@191
   658
	MMIO_WRITE( PVR2, reg, val&0x00133333 );
nkeynes@189
   659
	break;
nkeynes@261
   660
    case TA_INIT:
nkeynes@261
   661
	if( val & 0x80000000 )
nkeynes@261
   662
	    pvr2_ta_init();
nkeynes@261
   663
	break;
nkeynes@261
   664
    case TA_REINIT:
nkeynes@261
   665
	break;
nkeynes@261
   666
	/**************** Scaler registers? ****************/
nkeynes@335
   667
    case RENDER_SCALER:
nkeynes@261
   668
	MMIO_WRITE( PVR2, reg, val&0x0007FFFF );
nkeynes@261
   669
	break;
nkeynes@261
   670
nkeynes@197
   671
    case YUV_ADDR:
nkeynes@284
   672
	val = val & 0x00FFFFF8;
nkeynes@284
   673
	MMIO_WRITE( PVR2, reg, val );
nkeynes@284
   674
	pvr2_yuv_init( val );
nkeynes@197
   675
	break;
nkeynes@197
   676
    case YUV_CFG:
nkeynes@197
   677
	MMIO_WRITE( PVR2, reg, val&0x01013F3F );
nkeynes@284
   678
	pvr2_yuv_set_config(val);
nkeynes@197
   679
	break;
nkeynes@261
   680
nkeynes@261
   681
	/**************** Unknowns ***************/
nkeynes@261
   682
    case PVRUNK1:
nkeynes@261
   683
    	MMIO_WRITE( PVR2, reg, val&0x000007FF );
nkeynes@261
   684
    	break;
nkeynes@261
   685
    case PVRUNK2:
nkeynes@261
   686
	MMIO_WRITE( PVR2, reg, val&0x00000007 );
nkeynes@100
   687
	break;
nkeynes@261
   688
    case PVRUNK3:
nkeynes@261
   689
	MMIO_WRITE( PVR2, reg, val&0x000FFF3F );
nkeynes@261
   690
	break;
nkeynes@261
   691
    case PVRUNK5:
nkeynes@261
   692
	MMIO_WRITE( PVR2, reg, val&0x0000FFFF );
nkeynes@261
   693
	break;
nkeynes@197
   694
    case PVRUNK7:
nkeynes@197
   695
	MMIO_WRITE( PVR2, reg, val&0x00000001 );
nkeynes@197
   696
	break;
nkeynes@1
   697
    }
nkeynes@1
   698
}
nkeynes@1
   699
nkeynes@261
   700
/**
nkeynes@261
   701
 * Calculate the current read value of the syncstat register, using
nkeynes@261
   702
 * the current SH4 clock time as an offset from the last timeslice.
nkeynes@261
   703
 * The register reads (LSB to MSB) as:
nkeynes@261
   704
 *     0..9  Current scan line
nkeynes@261
   705
 *     10    Odd/even field (1 = odd, 0 = even)
nkeynes@261
   706
 *     11    Display active (including border and overscan)
nkeynes@261
   707
 *     12    Horizontal sync off
nkeynes@261
   708
 *     13    Vertical sync off
nkeynes@261
   709
 * Note this method is probably incorrect for anything other than straight
nkeynes@265
   710
 * interlaced PAL/NTSC, and needs further testing. 
nkeynes@261
   711
 */
nkeynes@261
   712
uint32_t pvr2_get_sync_status()
nkeynes@261
   713
{
nkeynes@265
   714
    pvr2_update_raster_posn(sh4r.slice_cycle);
nkeynes@265
   715
    uint32_t result = pvr2_state.line_count;
nkeynes@261
   716
nkeynes@265
   717
    if( pvr2_state.odd_even_field ) {
nkeynes@261
   718
	result |= 0x0400;
nkeynes@261
   719
    }
nkeynes@265
   720
    if( (pvr2_state.line_count & 0x01) == pvr2_state.odd_even_field ) {
nkeynes@265
   721
	if( pvr2_state.line_remainder > pvr2_state.hsync_width_ns ) {
nkeynes@261
   722
	    result |= 0x1000; /* !HSYNC */
nkeynes@261
   723
	}
nkeynes@265
   724
	if( pvr2_state.line_count >= pvr2_state.vsync_lines ) {
nkeynes@265
   725
	    if( pvr2_state.line_remainder > pvr2_state.front_porch_ns ) {
nkeynes@261
   726
		result |= 0x2800; /* Display active */
nkeynes@261
   727
	    } else {
nkeynes@261
   728
		result |= 0x2000; /* Front porch */
nkeynes@261
   729
	    }
nkeynes@261
   730
	}
nkeynes@261
   731
    } else {
nkeynes@269
   732
	if( pvr2_state.line_count >= pvr2_state.vsync_lines ) {
nkeynes@269
   733
	    if( pvr2_state.line_remainder < (pvr2_state.line_time_ns - pvr2_state.back_porch_ns)) {
nkeynes@269
   734
		result |= 0x3800; /* Display active */
nkeynes@269
   735
	    } else {
nkeynes@269
   736
		result |= 0x3000;
nkeynes@269
   737
	    }
nkeynes@261
   738
	} else {
nkeynes@261
   739
	    result |= 0x1000; /* Back porch */
nkeynes@261
   740
	}
nkeynes@261
   741
    }
nkeynes@261
   742
    return result;
nkeynes@261
   743
}
nkeynes@261
   744
nkeynes@265
   745
/**
nkeynes@265
   746
 * Schedule a "scanline" event. This actually goes off at
nkeynes@265
   747
 * 2 * line in even fields and 2 * line + 1 in odd fields.
nkeynes@265
   748
 * Otherwise this behaves as per pvr2_schedule_line_event().
nkeynes@265
   749
 * The raster position should be updated before calling this
nkeynes@265
   750
 * method.
nkeynes@304
   751
 * @param eventid Event to fire at the specified time
nkeynes@304
   752
 * @param line Line on which to fire the event (this is 2n/2n+1 for interlaced
nkeynes@304
   753
 *  displays). 
nkeynes@304
   754
 * @param hpos_ns Nanoseconds into the line at which to fire.
nkeynes@265
   755
 */
nkeynes@304
   756
static void pvr2_schedule_scanline_event( int eventid, int line, int minimum_lines, int hpos_ns )
nkeynes@265
   757
{
nkeynes@265
   758
    uint32_t field = pvr2_state.odd_even_field;
nkeynes@265
   759
    if( line <= pvr2_state.line_count && pvr2_state.interlaced ) {
nkeynes@265
   760
	field = !field;
nkeynes@265
   761
    }
nkeynes@304
   762
    if( hpos_ns > pvr2_state.line_time_ns ) {
nkeynes@304
   763
	hpos_ns = pvr2_state.line_time_ns;
nkeynes@304
   764
    }
nkeynes@265
   765
nkeynes@265
   766
    line <<= 1;
nkeynes@265
   767
    if( field ) {
nkeynes@265
   768
	line += 1;
nkeynes@265
   769
    }
nkeynes@274
   770
    
nkeynes@274
   771
    if( line < pvr2_state.total_lines ) {
nkeynes@274
   772
	uint32_t lines;
nkeynes@274
   773
	uint32_t time;
nkeynes@274
   774
	if( line <= pvr2_state.line_count ) {
nkeynes@274
   775
	    lines = (pvr2_state.total_lines - pvr2_state.line_count + line);
nkeynes@274
   776
	} else {
nkeynes@274
   777
	    lines = (line - pvr2_state.line_count);
nkeynes@274
   778
	}
nkeynes@274
   779
	if( lines <= minimum_lines ) {
nkeynes@274
   780
	    lines += pvr2_state.total_lines;
nkeynes@274
   781
	}
nkeynes@304
   782
	time = (lines * pvr2_state.line_time_ns) - pvr2_state.line_remainder + hpos_ns;
nkeynes@274
   783
	event_schedule( eventid, time );
nkeynes@274
   784
    } else {
nkeynes@274
   785
	event_cancel( eventid );
nkeynes@274
   786
    }
nkeynes@265
   787
}
nkeynes@265
   788
nkeynes@1
   789
MMIO_REGION_READ_FN( PVR2, reg )
nkeynes@1
   790
{
nkeynes@1
   791
    switch( reg ) {
nkeynes@261
   792
        case DISP_SYNCSTAT:
nkeynes@261
   793
            return pvr2_get_sync_status();
nkeynes@1
   794
        default:
nkeynes@1
   795
            return MMIO_READ( PVR2, reg );
nkeynes@1
   796
    }
nkeynes@1
   797
}
nkeynes@19
   798
nkeynes@337
   799
MMIO_REGION_WRITE_FN( PVR2PAL, reg, val )
nkeynes@337
   800
{
nkeynes@337
   801
    MMIO_WRITE( PVR2PAL, reg, val );
nkeynes@337
   802
    pvr2_state.palette_changed = TRUE;
nkeynes@337
   803
}
nkeynes@337
   804
nkeynes@337
   805
void pvr2_check_palette_changed()
nkeynes@337
   806
{
nkeynes@337
   807
    if( pvr2_state.palette_changed ) {
nkeynes@337
   808
	texcache_invalidate_palette();
nkeynes@337
   809
	pvr2_state.palette_changed = FALSE;
nkeynes@337
   810
    }
nkeynes@337
   811
}
nkeynes@337
   812
nkeynes@337
   813
MMIO_REGION_READ_DEFFN( PVR2PAL );
nkeynes@85
   814
nkeynes@19
   815
void pvr2_set_base_address( uint32_t base ) 
nkeynes@19
   816
{
nkeynes@197
   817
    mmio_region_PVR2_write( DISP_ADDR1, base );
nkeynes@19
   818
}
nkeynes@56
   819
nkeynes@56
   820
nkeynes@65
   821
nkeynes@98
   822
nkeynes@56
   823
int32_t mmio_region_PVR2TA_read( uint32_t reg )
nkeynes@56
   824
{
nkeynes@56
   825
    return 0xFFFFFFFF;
nkeynes@56
   826
}
nkeynes@56
   827
nkeynes@56
   828
void mmio_region_PVR2TA_write( uint32_t reg, uint32_t val )
nkeynes@56
   829
{
nkeynes@433
   830
    pvr2_ta_write( (unsigned char *)&val, sizeof(uint32_t) );
nkeynes@56
   831
}
nkeynes@56
   832
nkeynes@352
   833
/**
nkeynes@352
   834
 * Find the render buffer corresponding to the requested output frame
nkeynes@352
   835
 * (does not consider texture renders). 
nkeynes@352
   836
 * @return the render_buffer if found, or null if no such buffer.
nkeynes@352
   837
 *
nkeynes@352
   838
 * Note: Currently does not consider "partial matches", ie partial
nkeynes@352
   839
 * frame overlap - it probably needs to do this.
nkeynes@352
   840
 */
nkeynes@352
   841
render_buffer_t pvr2_get_render_buffer( frame_buffer_t frame )
nkeynes@352
   842
{
nkeynes@352
   843
    int i;
nkeynes@352
   844
    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@352
   845
	if( render_buffers[i] != NULL && render_buffers[i]->address == frame->address ) {
nkeynes@352
   846
	    return render_buffers[i];
nkeynes@352
   847
	}
nkeynes@352
   848
    }
nkeynes@352
   849
    return NULL;
nkeynes@352
   850
}
nkeynes@352
   851
nkeynes@352
   852
/**
nkeynes@477
   853
 * Allocate a render buffer with the requested parameters.
nkeynes@477
   854
 * The order of preference is:
nkeynes@352
   855
 *   1. An existing buffer with the same address. (not flushed unless the new
nkeynes@352
   856
 * size is smaller than the old one).
nkeynes@352
   857
 *   2. An existing buffer with the same size chosen by LRU order. Old buffer
nkeynes@352
   858
 *       is flushed to vram.
nkeynes@352
   859
 *   3. A new buffer if one can be created.
nkeynes@352
   860
 *   4. The current display buff
nkeynes@352
   861
 * Note: The current display field(s) will never be overwritten except as a last
nkeynes@352
   862
 * resort.
nkeynes@352
   863
 */
nkeynes@477
   864
render_buffer_t pvr2_alloc_render_buffer( sh4addr_t render_addr, int width, int height )
nkeynes@352
   865
{
nkeynes@477
   866
    int i;
nkeynes@352
   867
    render_buffer_t result = NULL;
nkeynes@352
   868
nkeynes@352
   869
    /* Check existing buffers for an available buffer */
nkeynes@352
   870
    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@352
   871
	if( render_buffers[i]->width == width && render_buffers[i]->height == height ) {
nkeynes@352
   872
	    /* needs to be the right dimensions */
nkeynes@352
   873
	    if( render_buffers[i]->address == render_addr ) {
nkeynes@441
   874
		if( displayed_render_buffer == render_buffers[i] ) {
nkeynes@441
   875
		    /* Same address, but we can't use it because the
nkeynes@441
   876
		     * display has it. Mark it as unaddressed for later.
nkeynes@477
   877
		     */
nkeynes@441
   878
		    render_buffers[i]->address = -1;
nkeynes@441
   879
		} else {
nkeynes@441
   880
		    /* perfect */
nkeynes@441
   881
		    result = render_buffers[i];
nkeynes@441
   882
		    break;
nkeynes@441
   883
		}
nkeynes@441
   884
	    } else if( render_buffers[i]->address == -1 && result == NULL && 
nkeynes@441
   885
		       displayed_render_buffer != render_buffers[i] ) {
nkeynes@352
   886
		result = render_buffers[i];
nkeynes@352
   887
	    }
nkeynes@441
   888
	    
nkeynes@352
   889
	} else if( render_buffers[i]->address == render_addr ) {
nkeynes@352
   890
	    /* right address, wrong size - if it's larger, flush it, otherwise 
nkeynes@352
   891
	     * nuke it quietly */
nkeynes@352
   892
	    if( render_buffers[i]->width * render_buffers[i]->height >
nkeynes@352
   893
		width*height ) {
nkeynes@352
   894
		pvr2_render_buffer_copy_to_sh4( render_buffers[i] );
nkeynes@352
   895
	    }
nkeynes@433
   896
	    render_buffers[i]->address = -1;
nkeynes@352
   897
	}
nkeynes@352
   898
    }
nkeynes@352
   899
nkeynes@352
   900
    /* Nothing available - make one */
nkeynes@352
   901
    if( result == NULL ) {
nkeynes@352
   902
	if( render_buffer_count == MAX_RENDER_BUFFERS ) {
nkeynes@352
   903
	    /* maximum buffers reached - need to throw one away */
nkeynes@352
   904
	    uint32_t field1_addr = MMIO_READ( PVR2, DISP_ADDR1 );
nkeynes@352
   905
	    uint32_t field2_addr = MMIO_READ( PVR2, DISP_ADDR2 );
nkeynes@352
   906
	    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@352
   907
		if( render_buffers[i]->address != field1_addr &&
nkeynes@441
   908
		    render_buffers[i]->address != field2_addr &&
nkeynes@441
   909
		    render_buffers[i] != displayed_render_buffer ) {
nkeynes@352
   910
		    /* Never throw away the current "front buffer(s)" */
nkeynes@352
   911
		    result = render_buffers[i];
nkeynes@477
   912
		    if( !result->flushed ) {
nkeynes@477
   913
			pvr2_render_buffer_copy_to_sh4( result );
nkeynes@477
   914
		    }
nkeynes@352
   915
		    if( result->width != width || result->height != height ) {
nkeynes@352
   916
			display_driver->destroy_render_buffer(render_buffers[i]);
nkeynes@352
   917
			result = display_driver->create_render_buffer(width,height);
nkeynes@352
   918
			render_buffers[i] = result;
nkeynes@352
   919
		    }
nkeynes@352
   920
		    break;
nkeynes@352
   921
		}
nkeynes@352
   922
	    }
nkeynes@352
   923
	} else {
nkeynes@352
   924
	    result = display_driver->create_render_buffer(width,height);
nkeynes@352
   925
	    if( result != NULL ) { 
nkeynes@352
   926
		render_buffers[render_buffer_count++] = result;
nkeynes@352
   927
	    }
nkeynes@352
   928
	}
nkeynes@352
   929
    }
nkeynes@352
   930
nkeynes@477
   931
    if( result != NULL ) {
nkeynes@477
   932
	result->address = render_addr;
nkeynes@477
   933
    }
nkeynes@352
   934
    return result;
nkeynes@352
   935
}
nkeynes@352
   936
nkeynes@352
   937
/**
nkeynes@477
   938
 * Allocate a render buffer based on the current rendering settings
nkeynes@477
   939
 */
nkeynes@477
   940
render_buffer_t pvr2_next_render_buffer()
nkeynes@477
   941
{
nkeynes@477
   942
    render_buffer_t result = NULL;
nkeynes@477
   943
    uint32_t render_addr = MMIO_READ( PVR2, RENDER_ADDR1 );
nkeynes@477
   944
    uint32_t render_mode = MMIO_READ( PVR2, RENDER_MODE );
nkeynes@477
   945
    uint32_t render_scale = MMIO_READ( PVR2, RENDER_SCALER );
nkeynes@477
   946
    uint32_t render_stride = MMIO_READ( PVR2, RENDER_SIZE ) << 3;
nkeynes@477
   947
nkeynes@477
   948
    if( render_addr & 0x01000000 ) { /* vram64 */
nkeynes@477
   949
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
nkeynes@477
   950
    } else { /* vram32 */
nkeynes@477
   951
	render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
nkeynes@477
   952
    }
nkeynes@477
   953
nkeynes@653
   954
    int width = pvr2_scene_buffer_width();
nkeynes@653
   955
    int height = pvr2_scene_buffer_height();
nkeynes@477
   956
    int colour_format = pvr2_render_colour_format[render_mode&0x07];
nkeynes@477
   957
nkeynes@477
   958
    result = pvr2_alloc_render_buffer( render_addr, width, height );
nkeynes@477
   959
    /* Setup the buffer */
nkeynes@477
   960
    if( result != NULL ) {
nkeynes@477
   961
	result->rowstride = render_stride;
nkeynes@477
   962
	result->colour_format = colour_format;
nkeynes@477
   963
	result->scale = render_scale;
nkeynes@477
   964
	result->size = width * height * colour_formats[colour_format].bpp;
nkeynes@477
   965
	result->flushed = FALSE;
nkeynes@477
   966
	result->inverted = TRUE; // render buffers are inverted normally
nkeynes@477
   967
    }
nkeynes@477
   968
    return result;
nkeynes@477
   969
}
nkeynes@477
   970
nkeynes@477
   971
static render_buffer_t pvr2_frame_buffer_to_render_buffer( frame_buffer_t frame )
nkeynes@477
   972
{
nkeynes@477
   973
    render_buffer_t result = pvr2_alloc_render_buffer( frame->address, frame->width, frame->height );
nkeynes@477
   974
    if( result != NULL ) {
nkeynes@477
   975
	int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@477
   976
	result->rowstride = frame->rowstride;
nkeynes@477
   977
	result->colour_format = frame->colour_format;
nkeynes@477
   978
	result->scale = 0x400;
nkeynes@477
   979
	result->size = frame->width * frame->height * bpp;
nkeynes@477
   980
	result->flushed = TRUE;
nkeynes@477
   981
	result->inverted = frame->inverted;
nkeynes@477
   982
	display_driver->load_frame_buffer( frame, result );
nkeynes@477
   983
    }
nkeynes@477
   984
    return result;
nkeynes@477
   985
}
nkeynes@477
   986
    
nkeynes@477
   987
nkeynes@477
   988
/**
nkeynes@352
   989
 * Invalidate any caching on the supplied address. Specifically, if it falls
nkeynes@352
   990
 * within any of the render buffers, flush the buffer back to PVR2 ram.
nkeynes@352
   991
 */
nkeynes@352
   992
gboolean pvr2_render_buffer_invalidate( sh4addr_t address, gboolean isWrite )
nkeynes@352
   993
{
nkeynes@352
   994
    int i;
nkeynes@352
   995
    address = address & 0x1FFFFFFF;
nkeynes@352
   996
    for( i=0; i<render_buffer_count; i++ ) {
nkeynes@352
   997
	uint32_t bufaddr = render_buffers[i]->address;
nkeynes@352
   998
	if( bufaddr != -1 && bufaddr <= address && 
nkeynes@352
   999
	    (bufaddr + render_buffers[i]->size) > address ) {
nkeynes@352
  1000
	    if( !render_buffers[i]->flushed ) {
nkeynes@352
  1001
		pvr2_render_buffer_copy_to_sh4( render_buffers[i] );
nkeynes@352
  1002
		render_buffers[i]->flushed = TRUE;
nkeynes@352
  1003
	    }
nkeynes@352
  1004
	    if( isWrite ) {
nkeynes@352
  1005
		render_buffers[i]->address = -1; /* Invalid */
nkeynes@352
  1006
	    }
nkeynes@352
  1007
	    return TRUE; /* should never have overlapping buffers */
nkeynes@352
  1008
	}
nkeynes@352
  1009
    }
nkeynes@352
  1010
    return FALSE;
nkeynes@352
  1011
}
.