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