Search
lxdream.org :: lxdream/src/pvr2/pvr2.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2.c
changeset 265:5daf59b7f31b
prev261:93fdb2a70e18
next269:e41f9b1490d1
author nkeynes
date Wed Jan 10 08:38:02 2007 +0000 (17 years ago)
permissions -rw-r--r--
last change Fix last tile not actually being rendered
file annotate diff log raw
1.1 --- a/src/pvr2/pvr2.c Wed Jan 03 09:01:51 2007 +0000
1.2 +++ b/src/pvr2/pvr2.c Wed Jan 10 08:38:02 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: pvr2.c,v 1.34 2007-01-03 09:01:51 nkeynes Exp $
1.6 + * $Id: pvr2.c,v 1.35 2007-01-06 04:06:36 nkeynes Exp $
1.7 *
1.8 * PVR2 (Video) Core module implementation and MMIO registers.
1.9 *
1.10 @@ -18,6 +18,7 @@
1.11 #define MODULE pvr2_module
1.12
1.13 #include "dream.h"
1.14 +#include "eventq.h"
1.15 #include "display.h"
1.16 #include "mem.h"
1.17 #include "asic.h"
1.18 @@ -34,6 +35,10 @@
1.19 static uint32_t pvr2_run_slice( uint32_t );
1.20 static void pvr2_save_state( FILE *f );
1.21 static int pvr2_load_state( FILE *f );
1.22 +static void pvr2_update_raster_posn( uint32_t nanosecs );
1.23 +static void pvr2_schedule_line_event( int eventid, int line );
1.24 +static void pvr2_schedule_scanline_event( int eventid, int line );
1.25 +uint32_t pvr2_get_sync_status();
1.26
1.27 void pvr2_display_frame( void );
1.28
1.29 @@ -60,10 +65,10 @@
1.30 uint32_t frame_count;
1.31 uint32_t line_count;
1.32 uint32_t line_remainder;
1.33 + uint32_t cycles_run; /* Cycles already executed prior to main time slice */
1.34 uint32_t irq_vpos1;
1.35 uint32_t irq_vpos2;
1.36 uint32_t odd_even_field; /* 1 = odd, 0 = even */
1.37 - gboolean retrace;
1.38
1.39 /* timing */
1.40 uint32_t dot_clock;
1.41 @@ -74,6 +79,8 @@
1.42 uint32_t hsync_width_ns;
1.43 uint32_t front_porch_ns;
1.44 uint32_t back_porch_ns;
1.45 + uint32_t retrace_start_line;
1.46 + uint32_t retrace_end_line;
1.47 gboolean interlaced;
1.48 struct video_timing timing;
1.49 } pvr2_state;
1.50 @@ -81,20 +88,39 @@
1.51 struct video_buffer video_buffer[2];
1.52 int video_buffer_idx = 0;
1.53
1.54 +/**
1.55 + * Event handler for the retrace callback (fires on line 0 normally)
1.56 + */
1.57 +static void pvr2_retrace_callback( int eventid ) {
1.58 + asic_event( eventid );
1.59 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.60 + pvr2_schedule_line_event( EVENT_RETRACE, 0 );
1.61 +}
1.62 +
1.63 +/**
1.64 + * Event handler for the scanline callbacks. Fires the corresponding
1.65 + * ASIC event, and resets the timer for the next field.
1.66 + */
1.67 +static void pvr2_scanline_callback( int eventid ) {
1.68 + asic_event( eventid );
1.69 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.70 + if( eventid == EVENT_SCANLINE1 ) {
1.71 + pvr2_schedule_scanline_event( eventid, pvr2_state.irq_vpos1 );
1.72 + } else {
1.73 + pvr2_schedule_scanline_event( eventid, pvr2_state.irq_vpos2 );
1.74 + }
1.75 +}
1.76 +
1.77 static void pvr2_init( void )
1.78 {
1.79 register_io_region( &mmio_region_PVR2 );
1.80 register_io_region( &mmio_region_PVR2PAL );
1.81 register_io_region( &mmio_region_PVR2TA );
1.82 + register_event_callback( EVENT_RETRACE, pvr2_retrace_callback );
1.83 + register_event_callback( EVENT_SCANLINE1, pvr2_scanline_callback );
1.84 + register_event_callback( EVENT_SCANLINE2, pvr2_scanline_callback );
1.85 video_base = mem_get_region_by_name( MEM_REGION_VIDEO );
1.86 texcache_init();
1.87 - pvr2_state.dot_clock = 27069;
1.88 - pvr2_state.total_lines = pal_timing.total_lines;
1.89 - pvr2_state.line_time_ns = pal_timing.line_time_ns;
1.90 - pvr2_state.front_porch_ns = 12000;
1.91 - pvr2_state.back_porch_ns = 4000;
1.92 - pvr2_state.hsync_width_ns = 4000;
1.93 - pvr2_state.vsync_lines = 5;
1.94 pvr2_reset();
1.95 pvr2_ta_reset();
1.96 }
1.97 @@ -103,10 +129,14 @@
1.98 {
1.99 pvr2_state.line_count = 0;
1.100 pvr2_state.line_remainder = 0;
1.101 + pvr2_state.cycles_run = 0;
1.102 pvr2_state.irq_vpos1 = 0;
1.103 pvr2_state.irq_vpos2 = 0;
1.104 - pvr2_state.retrace = FALSE;
1.105 pvr2_state.timing = ntsc_timing;
1.106 + pvr2_state.dot_clock = PVR2_DOT_CLOCK;
1.107 + pvr2_state.back_porch_ns = 4000;
1.108 + mmio_region_PVR2_write( DISP_TOTAL, 0x0270035F );
1.109 + mmio_region_PVR2_write( DISP_SYNCTIME, 0x07D6A53F );
1.110 video_buffer_idx = 0;
1.111
1.112 pvr2_ta_init();
1.113 @@ -127,33 +157,41 @@
1.114 return pvr2_ta_load_state(f);
1.115 }
1.116
1.117 +/**
1.118 + * Update the current raster position to the given number of nanoseconds,
1.119 + * relative to the last time slice. (ie the raster will be adjusted forward
1.120 + * by nanosecs - nanosecs_already_run_this_timeslice)
1.121 + */
1.122 +static void pvr2_update_raster_posn( uint32_t nanosecs )
1.123 +{
1.124 + uint32_t old_line_count = pvr2_state.line_count;
1.125 + if( pvr2_state.line_time_ns == 0 ) {
1.126 + return; /* do nothing */
1.127 + }
1.128 + pvr2_state.line_remainder += (nanosecs - pvr2_state.cycles_run);
1.129 + pvr2_state.cycles_run = nanosecs;
1.130 + while( pvr2_state.line_remainder >= pvr2_state.line_time_ns ) {
1.131 + pvr2_state.line_count ++;
1.132 + pvr2_state.line_remainder -= pvr2_state.line_time_ns;
1.133 + }
1.134 +
1.135 + if( pvr2_state.line_count >= pvr2_state.total_lines ) {
1.136 + pvr2_state.line_count -= pvr2_state.total_lines;
1.137 + if( pvr2_state.interlaced ) {
1.138 + pvr2_state.odd_even_field = !pvr2_state.odd_even_field;
1.139 + }
1.140 + }
1.141 + if( pvr2_state.line_count >= pvr2_state.retrace_end_line &&
1.142 + (old_line_count < pvr2_state.retrace_end_line ||
1.143 + old_line_count > pvr2_state.line_count) ) {
1.144 + pvr2_display_frame();
1.145 + }
1.146 +}
1.147 +
1.148 static uint32_t pvr2_run_slice( uint32_t nanosecs )
1.149 {
1.150 - pvr2_state.line_remainder += nanosecs;
1.151 - while( pvr2_state.line_remainder >= pvr2_state.line_time_ns ) {
1.152 - pvr2_state.line_remainder -= pvr2_state.line_time_ns;
1.153 -
1.154 - pvr2_state.line_count++;
1.155 - if( pvr2_state.line_count == pvr2_state.total_lines ) {
1.156 - asic_event( EVENT_RETRACE );
1.157 - pvr2_state.line_count = 0;
1.158 - pvr2_state.retrace = TRUE;
1.159 - }
1.160 -
1.161 - if( pvr2_state.line_count == pvr2_state.irq_vpos1 ) {
1.162 - asic_event( EVENT_SCANLINE1 );
1.163 - }
1.164 - if( pvr2_state.line_count == pvr2_state.irq_vpos2 ) {
1.165 - asic_event( EVENT_SCANLINE2 );
1.166 - }
1.167 -
1.168 - if( pvr2_state.line_count == pvr2_state.timing.retrace_lines ) {
1.169 - if( pvr2_state.retrace ) {
1.170 - pvr2_display_frame();
1.171 - pvr2_state.retrace = FALSE;
1.172 - }
1.173 - }
1.174 - }
1.175 + pvr2_update_raster_posn( nanosecs );
1.176 + pvr2_state.cycles_run = 0;
1.177 return nanosecs;
1.178 }
1.179
1.180 @@ -273,9 +311,10 @@
1.181 case DISP_ADDR1:
1.182 val &= 0x00FFFFFC;
1.183 MMIO_WRITE( PVR2, reg, val );
1.184 - if( pvr2_state.retrace ) {
1.185 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.186 + if( pvr2_state.line_count >= pvr2_state.retrace_start_line ||
1.187 + pvr2_state.line_count < pvr2_state.retrace_end_line ) {
1.188 pvr2_display_frame();
1.189 - pvr2_state.retrace = FALSE;
1.190 }
1.191 break;
1.192 case DISP_ADDR2:
1.193 @@ -301,6 +340,9 @@
1.194 val = val & 0x03FF03FF;
1.195 pvr2_state.irq_vpos1 = (val >> 16);
1.196 pvr2_state.irq_vpos2 = val & 0x03FF;
1.197 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.198 + pvr2_schedule_scanline_event( EVENT_SCANLINE1, pvr2_state.irq_vpos1 );
1.199 + pvr2_schedule_scanline_event( EVENT_SCANLINE2, pvr2_state.irq_vpos2 );
1.200 MMIO_WRITE( PVR2, reg, val );
1.201 break;
1.202 case RENDER_NEARCLIP:
1.203 @@ -359,9 +401,15 @@
1.204 case DISP_TOTAL:
1.205 val = val & 0x03FF03FF;
1.206 MMIO_WRITE( PVR2, reg, val );
1.207 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.208 pvr2_state.total_lines = (val >> 16) + 1;
1.209 pvr2_state.line_size = (val & 0x03FF) + 1;
1.210 pvr2_state.line_time_ns = 1000000 * pvr2_state.line_size / pvr2_state.dot_clock;
1.211 + pvr2_state.retrace_end_line = 0x2A;
1.212 + pvr2_state.retrace_start_line = pvr2_state.total_lines - 6;
1.213 + pvr2_schedule_line_event( EVENT_RETRACE, 0 );
1.214 + pvr2_schedule_scanline_event( EVENT_SCANLINE1, pvr2_state.irq_vpos1 );
1.215 + pvr2_schedule_scanline_event( EVENT_SCANLINE2, pvr2_state.irq_vpos2 );
1.216 break;
1.217 case DISP_SYNCCFG:
1.218 MMIO_WRITE( PVR2, reg, val&0x000003FF );
1.219 @@ -456,50 +504,82 @@
1.220 * 12 Horizontal sync off
1.221 * 13 Vertical sync off
1.222 * Note this method is probably incorrect for anything other than straight
1.223 - * interlaced PAL, and needs further testing.
1.224 + * interlaced PAL/NTSC, and needs further testing.
1.225 */
1.226 uint32_t pvr2_get_sync_status()
1.227 {
1.228 - uint32_t tmp = pvr2_state.line_remainder + sh4r.slice_cycle;
1.229 - uint32_t line = pvr2_state.line_count + (tmp / pvr2_state.line_time_ns);
1.230 - uint32_t remainder = tmp % pvr2_state.line_time_ns;
1.231 - uint32_t field = pvr2_state.odd_even_field;
1.232 - uint32_t result;
1.233 + pvr2_update_raster_posn(sh4r.slice_cycle);
1.234 + uint32_t result = pvr2_state.line_count;
1.235
1.236 - if( line >= pvr2_state.total_lines ) {
1.237 - line -= pvr2_state.total_lines;
1.238 - if( pvr2_state.interlaced ) {
1.239 - field == 1 ? 0 : 1;
1.240 - }
1.241 - }
1.242 -
1.243 - result = line;
1.244 -
1.245 - if( field ) {
1.246 + if( pvr2_state.odd_even_field ) {
1.247 result |= 0x0400;
1.248 }
1.249 - if( (line & 0x01) == field ) {
1.250 - if( remainder > pvr2_state.hsync_width_ns ) {
1.251 + if( (pvr2_state.line_count & 0x01) == pvr2_state.odd_even_field ) {
1.252 + if( pvr2_state.line_remainder > pvr2_state.hsync_width_ns ) {
1.253 result |= 0x1000; /* !HSYNC */
1.254 }
1.255 - if( line >= pvr2_state.vsync_lines ) {
1.256 - if( remainder > pvr2_state.front_porch_ns ) {
1.257 + if( pvr2_state.line_count >= pvr2_state.vsync_lines ) {
1.258 + if( pvr2_state.line_remainder > pvr2_state.front_porch_ns ) {
1.259 result |= 0x2800; /* Display active */
1.260 } else {
1.261 result |= 0x2000; /* Front porch */
1.262 }
1.263 }
1.264 } else {
1.265 - if( remainder < (pvr2_state.line_time_ns - pvr2_state.back_porch_ns) &&
1.266 - line >= pvr2_state.vsync_lines ) {
1.267 + if( pvr2_state.line_remainder < (pvr2_state.line_time_ns - pvr2_state.back_porch_ns) &&
1.268 + pvr2_state.line_count >= pvr2_state.vsync_lines ) {
1.269 result |= 0x3800; /* Display active */
1.270 } else {
1.271 result |= 0x1000; /* Back porch */
1.272 }
1.273 }
1.274 +
1.275 return result;
1.276 }
1.277
1.278 +/**
1.279 + * Schedule an event for the start of the given line. If the line is actually
1.280 + * the current line, schedules it for the next field.
1.281 + * The raster position should be updated before calling this method.
1.282 + */
1.283 +static void pvr2_schedule_line_event( int eventid, int line )
1.284 +{
1.285 + uint32_t time;
1.286 + if( line <= pvr2_state.line_count ) {
1.287 + time = (pvr2_state.total_lines - pvr2_state.line_count + line) * pvr2_state.line_time_ns
1.288 + - pvr2_state.line_remainder;
1.289 + } else {
1.290 + time = (line - pvr2_state.line_count) * pvr2_state.line_time_ns - pvr2_state.line_remainder;
1.291 + }
1.292 +
1.293 + if( line < pvr2_state.total_lines ) {
1.294 + event_schedule( eventid, time );
1.295 + } else {
1.296 + event_cancel( eventid );
1.297 + }
1.298 +}
1.299 +
1.300 +/**
1.301 + * Schedule a "scanline" event. This actually goes off at
1.302 + * 2 * line in even fields and 2 * line + 1 in odd fields.
1.303 + * Otherwise this behaves as per pvr2_schedule_line_event().
1.304 + * The raster position should be updated before calling this
1.305 + * method.
1.306 + */
1.307 +static void pvr2_schedule_scanline_event( int eventid, int line )
1.308 +{
1.309 + uint32_t field = pvr2_state.odd_even_field;
1.310 + if( line <= pvr2_state.line_count && pvr2_state.interlaced ) {
1.311 + field = !field;
1.312 + }
1.313 +
1.314 + line <<= 1;
1.315 + if( field ) {
1.316 + line += 1;
1.317 + }
1.318 + pvr2_schedule_line_event( eventid, line );
1.319 +}
1.320 +
1.321 MMIO_REGION_READ_FN( PVR2, reg )
1.322 {
1.323 switch( reg ) {
.