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