Search
lxdream.org :: lxdream/src/pvr2/pvr2mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2mem.c
changeset 309:e2750808d02c
prev285:4fefedafebc6
next310:00cd8897ad5e
author nkeynes
date Sun Jan 21 11:29:17 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add method to dump vram to file in a form directly usable by testdata
view annotate diff log raw
     1 /**
     2  * $Id: pvr2mem.c,v 1.3 2007-01-21 11:29:17 nkeynes Exp $
     3  *
     4  * PVR2 (Video) VRAM handling routines (mainly for the 64-bit region)
     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 #include "pvr2.h"
    19 #include <stdio.h>
    20 #include <errno.h>
    22 extern char *video_base;
    24 void pvr2_vram64_write( sh4addr_t destaddr, char *src, uint32_t length )
    25 {
    26     int bank_flag = (destaddr & 0x04) >> 2;
    27     uint32_t *banks[2];
    28     uint32_t *dwsrc;
    29     int i;
    31     destaddr = destaddr & 0x7FFFFF;
    32     if( destaddr + length > 0x800000 ) {
    33 	length = 0x800000 - destaddr;
    34     }
    36     for( i=destaddr & 0xFFFFF000; i < destaddr + length; i+= PAGE_SIZE ) {
    37 	texcache_invalidate_page( i );
    38     }
    40     banks[0] = ((uint32_t *)(video_base + ((destaddr & 0x007FFFF8) >>1)));
    41     banks[1] = banks[0] + 0x100000;
    42     if( bank_flag ) 
    43 	banks[0]++;
    45     /* Handle non-aligned start of source */
    46     if( destaddr & 0x03 ) {
    47 	char *dest = ((char *)banks[bank_flag]) + (destaddr & 0x03);
    48 	for( i= destaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
    49 	    *dest++ = *src++;
    50 	}
    51 	bank_flag = !bank_flag;
    52     }
    54     dwsrc = (uint32_t *)src;
    55     while( length >= 4 ) {
    56 	*banks[bank_flag]++ = *dwsrc++;
    57 	bank_flag = !bank_flag;
    58 	length -= 4;
    59     }
    61     /* Handle non-aligned end of source */
    62     if( length ) {
    63 	src = (char *)dwsrc;
    64 	char *dest = (char *)banks[bank_flag];
    65 	while( length-- > 0 ) {
    66 	    *dest++ = *src++;
    67 	}
    68     }  
    69 }
    71 /**
    72  * Write an image to 64-bit vram, with a line-stride different from the line-size.
    73  * The destaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
    74  * must be multiples of 4.
    75  */
    76 void pvr2_vram64_write_stride( sh4addr_t destaddr, char *src, uint32_t line_bytes, 
    77 			       uint32_t line_stride_bytes, uint32_t line_count )
    78 {
    79     int bank_flag = (destaddr & 0x04) >> 2;
    80     uint32_t *banks[2];
    81     uint32_t *dwsrc;
    82     uint32_t line_gap;
    83     int line_gap_flag;
    84     int i,j;
    86     destaddr = destaddr & 0x7FFFF8;
    87     i = line_stride_bytes - line_bytes;
    88     line_gap_flag = i & 0x04;
    89     line_gap = i >> 3;
    90     line_bytes >>= 2;
    92     for( i=destaddr & 0xFFFFF000; i < destaddr + line_stride_bytes*line_count; i+= PAGE_SIZE ) {
    93 	texcache_invalidate_page( i );
    94     }
    96     banks[0] = (uint32_t *)(video_base + (destaddr >>1));
    97     banks[1] = banks[0] + 0x100000;
    98     if( bank_flag ) 
    99 	banks[0]++;
   101     dwsrc = (uint32_t *)src;
   102     for( i=0; i<line_count; i++ ) {
   103 	for( j=0; j<line_bytes; j++ ) {
   104 	    *banks[bank_flag]++ = *dwsrc++;
   105 	    bank_flag = !bank_flag;
   106 	}
   107 	banks[0] += line_gap;
   108 	banks[1] += line_gap;
   109 	if( line_gap_flag ) {
   110 	    banks[bank_flag]++;
   111 	    bank_flag = !bank_flag;
   112 	}
   113     }    
   114 }
   116 /**
   117  * Read an image from 64-bit vram, with a destination line-stride different from the line-size.
   118  * The srcaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
   119  * must be multiples of 4. line_stride_bytes must be >= line_bytes.
   120  * This method is used to extract a "stride" texture from vram.
   121  */
   122 void pvr2_vram64_read_stride( char *dest, uint32_t dest_line_bytes, sh4addr_t srcaddr,
   123 				   uint32_t src_line_bytes, uint32_t line_count )
   124 {
   125     int bank_flag = (srcaddr & 0x04) >> 2;
   126     uint32_t *banks[2];
   127     uint32_t *dwdest;
   128     uint32_t dest_line_gap;
   129     uint32_t src_line_gap;
   130     uint32_t line_bytes;
   131     int src_line_gap_flag;
   132     int i,j;
   134     srcaddr = srcaddr & 0x7FFFF8;
   135     if( src_line_bytes <= dest_line_bytes ) {
   136 	dest_line_gap = (dest_line_bytes - src_line_bytes) >> 2;
   137 	src_line_gap = 0;
   138 	src_line_gap_flag = 0;
   139 	line_bytes = src_line_bytes >> 2;
   140     } else {
   141 	i = (src_line_bytes - dest_line_bytes);
   142 	src_line_gap_flag = i & 0x04;
   143 	src_line_gap = i >> 3;
   144 	line_bytes = dest_line_bytes >> 2;
   145     }
   147     banks[0] = (uint32_t *)(video_base + (srcaddr>>1));
   148     banks[1] = banks[0] + 0x100000;
   149     if( bank_flag )
   150 	banks[0]++;
   152     dwdest = (uint32_t *)dest;
   153     for( i=0; i<line_count; i++ ) {
   154 	for( j=0; j<line_bytes; j++ ) {
   155 	    *dwdest++ = *banks[bank_flag]++;
   156 	    bank_flag = !bank_flag;
   157 	}
   158 	dwdest += dest_line_gap;
   159 	banks[0] += src_line_gap;
   160 	banks[1] += src_line_gap;
   161 	if( src_line_gap_flag ) {
   162 	    banks[bank_flag]++;
   163 	    bank_flag = !bank_flag;
   164 	}
   165     }
   167 }
   169 void pvr2_vram_write_invert( sh4addr_t destaddr, char *src, uint32_t length, uint32_t line_length )
   170 {
   171     char *dest = video_base + (destaddr & 0x007FFFFF);
   172     char *p = src + length - line_length;
   173     while( p >= src ) {
   174 	memcpy( dest, p, line_length );
   175 	p -= line_length;
   176 	dest += line_length;
   177     }
   178 }
   180 void pvr2_vram64_read( char *dest, sh4addr_t srcaddr, uint32_t length )
   181 {
   182     int bank_flag = (srcaddr & 0x04) >> 2;
   183     uint32_t *banks[2];
   184     uint32_t *dwdest;
   185     int i;
   187     srcaddr = srcaddr & 0x7FFFFF;
   188     if( srcaddr + length > 0x800000 )
   189 	length = 0x800000 - srcaddr;
   191     banks[0] = ((uint32_t *)(video_base + ((srcaddr&0x007FFFF8)>>1)));
   192     banks[1] = banks[0] + 0x100000;
   193     if( bank_flag )
   194 	banks[0]++;
   196     /* Handle non-aligned start of source */
   197     if( srcaddr & 0x03 ) {
   198 	char *src = ((char *)banks[bank_flag]) + (srcaddr & 0x03);
   199 	for( i= srcaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
   200 	    *dest++ = *src++;
   201 	}
   202 	bank_flag = !bank_flag;
   203     }
   205     dwdest = (uint32_t *)dest;
   206     while( length >= 4 ) {
   207 	*dwdest++ = *banks[bank_flag]++;
   208 	bank_flag = !bank_flag;
   209 	length -= 4;
   210     }
   212     /* Handle non-aligned end of source */
   213     if( length ) {
   214 	dest = (char *)dwdest;
   215 	char *src = (char *)banks[bank_flag];
   216 	while( length-- > 0 ) {
   217 	    *dest++ = *src++;
   218 	}
   219     }
   220 }
   222 void pvr2_vram64_dump_file( sh4addr_t addr, uint32_t length, gchar *filename )
   223 {
   224     uint32_t tmp[length>>2];
   225     FILE *f = fopen(filename, "wo");
   226     unsigned int i, j;
   228     if( f == NULL ) {
   229 	ERROR( "Unable to write to dump file '%s' (%s)", filename, strerror(errno) );
   230 	return;
   231     }
   232     pvr2_vram64_read( tmp, addr, length );
   233     fprintf( f, "%08X\n", addr );
   234     for( i =0; i<length>>2; i+=8 ) {
   235 	for( j=i; j<i+8; j++ ) {
   236 	    if( j < length )
   237 		fprintf( f, " %08X", tmp[j] );
   238 	    else
   239 		fprintf( f, "         " );
   240 	}
   241 	fprintf( f, "\n" );
   242     }
   243     fclose(f);
   244 }
   246 void pvr2_vram64_dump( sh4addr_t addr, uint32_t length, FILE *f ) 
   247 {
   248     char tmp[length];
   249     pvr2_vram64_read( tmp, addr, length );
   250     fwrite_dump( tmp, length, f );
   251 }
.