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