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