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 (12 years ago)
permissions -rw-r--r--
last change Add method to dump vram to file in a form directly usable by testdata
file annotate diff log raw
nkeynes@284
     1
/**
nkeynes@309
     2
 * $Id: pvr2mem.c,v 1.3 2007-01-21 11:29:17 nkeynes Exp $
nkeynes@284
     3
 *
nkeynes@284
     4
 * PVR2 (Video) VRAM handling routines (mainly for the 64-bit region)
nkeynes@284
     5
 *
nkeynes@284
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@284
     7
 *
nkeynes@284
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@284
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@284
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@284
    11
 * (at your option) any later version.
nkeynes@284
    12
 *
nkeynes@284
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@284
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@284
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@284
    16
 * GNU General Public License for more details.
nkeynes@284
    17
 */
nkeynes@284
    18
#include "pvr2.h"
nkeynes@309
    19
#include <stdio.h>
nkeynes@309
    20
#include <errno.h>
nkeynes@284
    21
nkeynes@284
    22
extern char *video_base;
nkeynes@284
    23
nkeynes@284
    24
void pvr2_vram64_write( sh4addr_t destaddr, char *src, uint32_t length )
nkeynes@284
    25
{
nkeynes@284
    26
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@284
    27
    uint32_t *banks[2];
nkeynes@284
    28
    uint32_t *dwsrc;
nkeynes@284
    29
    int i;
nkeynes@284
    30
nkeynes@284
    31
    destaddr = destaddr & 0x7FFFFF;
nkeynes@284
    32
    if( destaddr + length > 0x800000 ) {
nkeynes@284
    33
	length = 0x800000 - destaddr;
nkeynes@284
    34
    }
nkeynes@284
    35
nkeynes@284
    36
    for( i=destaddr & 0xFFFFF000; i < destaddr + length; i+= PAGE_SIZE ) {
nkeynes@284
    37
	texcache_invalidate_page( i );
nkeynes@284
    38
    }
nkeynes@284
    39
nkeynes@284
    40
    banks[0] = ((uint32_t *)(video_base + ((destaddr & 0x007FFFF8) >>1)));
nkeynes@284
    41
    banks[1] = banks[0] + 0x100000;
nkeynes@284
    42
    if( bank_flag ) 
nkeynes@284
    43
	banks[0]++;
nkeynes@284
    44
    
nkeynes@284
    45
    /* Handle non-aligned start of source */
nkeynes@284
    46
    if( destaddr & 0x03 ) {
nkeynes@284
    47
	char *dest = ((char *)banks[bank_flag]) + (destaddr & 0x03);
nkeynes@284
    48
	for( i= destaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@284
    49
	    *dest++ = *src++;
nkeynes@284
    50
	}
nkeynes@284
    51
	bank_flag = !bank_flag;
nkeynes@284
    52
    }
nkeynes@284
    53
nkeynes@284
    54
    dwsrc = (uint32_t *)src;
nkeynes@284
    55
    while( length >= 4 ) {
nkeynes@284
    56
	*banks[bank_flag]++ = *dwsrc++;
nkeynes@284
    57
	bank_flag = !bank_flag;
nkeynes@284
    58
	length -= 4;
nkeynes@284
    59
    }
nkeynes@284
    60
    
nkeynes@284
    61
    /* Handle non-aligned end of source */
nkeynes@284
    62
    if( length ) {
nkeynes@284
    63
	src = (char *)dwsrc;
nkeynes@284
    64
	char *dest = (char *)banks[bank_flag];
nkeynes@284
    65
	while( length-- > 0 ) {
nkeynes@284
    66
	    *dest++ = *src++;
nkeynes@284
    67
	}
nkeynes@284
    68
    }  
nkeynes@284
    69
}
nkeynes@284
    70
nkeynes@284
    71
/**
nkeynes@284
    72
 * Write an image to 64-bit vram, with a line-stride different from the line-size.
nkeynes@284
    73
 * The destaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
nkeynes@284
    74
 * must be multiples of 4.
nkeynes@284
    75
 */
nkeynes@284
    76
void pvr2_vram64_write_stride( sh4addr_t destaddr, char *src, uint32_t line_bytes, 
nkeynes@284
    77
			       uint32_t line_stride_bytes, uint32_t line_count )
nkeynes@284
    78
{
nkeynes@284
    79
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@284
    80
    uint32_t *banks[2];
nkeynes@284
    81
    uint32_t *dwsrc;
nkeynes@284
    82
    uint32_t line_gap;
nkeynes@284
    83
    int line_gap_flag;
nkeynes@284
    84
    int i,j;
nkeynes@284
    85
nkeynes@284
    86
    destaddr = destaddr & 0x7FFFF8;
nkeynes@284
    87
    i = line_stride_bytes - line_bytes;
nkeynes@284
    88
    line_gap_flag = i & 0x04;
nkeynes@284
    89
    line_gap = i >> 3;
nkeynes@284
    90
    line_bytes >>= 2;
nkeynes@284
    91
nkeynes@284
    92
    for( i=destaddr & 0xFFFFF000; i < destaddr + line_stride_bytes*line_count; i+= PAGE_SIZE ) {
nkeynes@284
    93
	texcache_invalidate_page( i );
nkeynes@284
    94
    }
nkeynes@284
    95
nkeynes@284
    96
    banks[0] = (uint32_t *)(video_base + (destaddr >>1));
nkeynes@284
    97
    banks[1] = banks[0] + 0x100000;
nkeynes@284
    98
    if( bank_flag ) 
nkeynes@284
    99
	banks[0]++;
nkeynes@284
   100
    
nkeynes@284
   101
    dwsrc = (uint32_t *)src;
nkeynes@284
   102
    for( i=0; i<line_count; i++ ) {
nkeynes@284
   103
	for( j=0; j<line_bytes; j++ ) {
nkeynes@284
   104
	    *banks[bank_flag]++ = *dwsrc++;
nkeynes@284
   105
	    bank_flag = !bank_flag;
nkeynes@284
   106
	}
nkeynes@285
   107
	banks[0] += line_gap;
nkeynes@285
   108
	banks[1] += line_gap;
nkeynes@284
   109
	if( line_gap_flag ) {
nkeynes@285
   110
	    banks[bank_flag]++;
nkeynes@284
   111
	    bank_flag = !bank_flag;
nkeynes@284
   112
	}
nkeynes@284
   113
    }    
nkeynes@284
   114
}
nkeynes@284
   115
nkeynes@284
   116
/**
nkeynes@284
   117
 * Read an image from 64-bit vram, with a destination line-stride different from the line-size.
nkeynes@284
   118
 * The srcaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
nkeynes@284
   119
 * must be multiples of 4. line_stride_bytes must be >= line_bytes.
nkeynes@284
   120
 * This method is used to extract a "stride" texture from vram.
nkeynes@284
   121
 */
nkeynes@284
   122
void pvr2_vram64_read_stride( char *dest, uint32_t dest_line_bytes, sh4addr_t srcaddr,
nkeynes@284
   123
				   uint32_t src_line_bytes, uint32_t line_count )
nkeynes@284
   124
{
nkeynes@284
   125
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@284
   126
    uint32_t *banks[2];
nkeynes@284
   127
    uint32_t *dwdest;
nkeynes@284
   128
    uint32_t dest_line_gap;
nkeynes@284
   129
    uint32_t src_line_gap;
nkeynes@284
   130
    uint32_t line_bytes;
nkeynes@284
   131
    int src_line_gap_flag;
nkeynes@284
   132
    int i,j;
nkeynes@284
   133
nkeynes@284
   134
    srcaddr = srcaddr & 0x7FFFF8;
nkeynes@284
   135
    if( src_line_bytes <= dest_line_bytes ) {
nkeynes@284
   136
	dest_line_gap = (dest_line_bytes - src_line_bytes) >> 2;
nkeynes@284
   137
	src_line_gap = 0;
nkeynes@284
   138
	src_line_gap_flag = 0;
nkeynes@284
   139
	line_bytes = src_line_bytes >> 2;
nkeynes@284
   140
    } else {
nkeynes@284
   141
	i = (src_line_bytes - dest_line_bytes);
nkeynes@284
   142
	src_line_gap_flag = i & 0x04;
nkeynes@284
   143
	src_line_gap = i >> 3;
nkeynes@284
   144
	line_bytes = dest_line_bytes >> 2;
nkeynes@284
   145
    }
nkeynes@284
   146
	
nkeynes@284
   147
    banks[0] = (uint32_t *)(video_base + (srcaddr>>1));
nkeynes@284
   148
    banks[1] = banks[0] + 0x100000;
nkeynes@284
   149
    if( bank_flag )
nkeynes@284
   150
	banks[0]++;
nkeynes@284
   151
    
nkeynes@284
   152
    dwdest = (uint32_t *)dest;
nkeynes@284
   153
    for( i=0; i<line_count; i++ ) {
nkeynes@284
   154
	for( j=0; j<line_bytes; j++ ) {
nkeynes@284
   155
	    *dwdest++ = *banks[bank_flag]++;
nkeynes@284
   156
	    bank_flag = !bank_flag;
nkeynes@284
   157
	}
nkeynes@284
   158
	dwdest += dest_line_gap;
nkeynes@284
   159
	banks[0] += src_line_gap;
nkeynes@284
   160
	banks[1] += src_line_gap;
nkeynes@284
   161
	if( src_line_gap_flag ) {
nkeynes@284
   162
	    banks[bank_flag]++;
nkeynes@284
   163
	    bank_flag = !bank_flag;
nkeynes@284
   164
	}
nkeynes@284
   165
    }
nkeynes@284
   166
    
nkeynes@284
   167
}
nkeynes@284
   168
nkeynes@284
   169
void pvr2_vram_write_invert( sh4addr_t destaddr, char *src, uint32_t length, uint32_t line_length )
nkeynes@284
   170
{
nkeynes@284
   171
    char *dest = video_base + (destaddr & 0x007FFFFF);
nkeynes@284
   172
    char *p = src + length - line_length;
nkeynes@284
   173
    while( p >= src ) {
nkeynes@284
   174
	memcpy( dest, p, line_length );
nkeynes@284
   175
	p -= line_length;
nkeynes@284
   176
	dest += line_length;
nkeynes@284
   177
    }
nkeynes@284
   178
}
nkeynes@284
   179
nkeynes@284
   180
void pvr2_vram64_read( char *dest, sh4addr_t srcaddr, uint32_t length )
nkeynes@284
   181
{
nkeynes@284
   182
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@284
   183
    uint32_t *banks[2];
nkeynes@284
   184
    uint32_t *dwdest;
nkeynes@284
   185
    int i;
nkeynes@284
   186
nkeynes@284
   187
    srcaddr = srcaddr & 0x7FFFFF;
nkeynes@284
   188
    if( srcaddr + length > 0x800000 )
nkeynes@284
   189
	length = 0x800000 - srcaddr;
nkeynes@284
   190
nkeynes@284
   191
    banks[0] = ((uint32_t *)(video_base + ((srcaddr&0x007FFFF8)>>1)));
nkeynes@284
   192
    banks[1] = banks[0] + 0x100000;
nkeynes@284
   193
    if( bank_flag )
nkeynes@284
   194
	banks[0]++;
nkeynes@284
   195
    
nkeynes@284
   196
    /* Handle non-aligned start of source */
nkeynes@284
   197
    if( srcaddr & 0x03 ) {
nkeynes@284
   198
	char *src = ((char *)banks[bank_flag]) + (srcaddr & 0x03);
nkeynes@284
   199
	for( i= srcaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@284
   200
	    *dest++ = *src++;
nkeynes@284
   201
	}
nkeynes@284
   202
	bank_flag = !bank_flag;
nkeynes@284
   203
    }
nkeynes@284
   204
nkeynes@284
   205
    dwdest = (uint32_t *)dest;
nkeynes@284
   206
    while( length >= 4 ) {
nkeynes@284
   207
	*dwdest++ = *banks[bank_flag]++;
nkeynes@284
   208
	bank_flag = !bank_flag;
nkeynes@284
   209
	length -= 4;
nkeynes@284
   210
    }
nkeynes@284
   211
    
nkeynes@284
   212
    /* Handle non-aligned end of source */
nkeynes@284
   213
    if( length ) {
nkeynes@284
   214
	dest = (char *)dwdest;
nkeynes@284
   215
	char *src = (char *)banks[bank_flag];
nkeynes@284
   216
	while( length-- > 0 ) {
nkeynes@284
   217
	    *dest++ = *src++;
nkeynes@284
   218
	}
nkeynes@284
   219
    }
nkeynes@284
   220
}
nkeynes@284
   221
nkeynes@309
   222
void pvr2_vram64_dump_file( sh4addr_t addr, uint32_t length, gchar *filename )
nkeynes@309
   223
{
nkeynes@309
   224
    uint32_t tmp[length>>2];
nkeynes@309
   225
    FILE *f = fopen(filename, "wo");
nkeynes@309
   226
    unsigned int i, j;
nkeynes@309
   227
nkeynes@309
   228
    if( f == NULL ) {
nkeynes@309
   229
	ERROR( "Unable to write to dump file '%s' (%s)", filename, strerror(errno) );
nkeynes@309
   230
	return;
nkeynes@309
   231
    }
nkeynes@309
   232
    pvr2_vram64_read( tmp, addr, length );
nkeynes@309
   233
    fprintf( f, "%08X\n", addr );
nkeynes@309
   234
    for( i =0; i<length>>2; i+=8 ) {
nkeynes@309
   235
	for( j=i; j<i+8; j++ ) {
nkeynes@309
   236
	    if( j < length )
nkeynes@309
   237
		fprintf( f, " %08X", tmp[j] );
nkeynes@309
   238
	    else
nkeynes@309
   239
		fprintf( f, "         " );
nkeynes@309
   240
	}
nkeynes@309
   241
	fprintf( f, "\n" );
nkeynes@309
   242
    }
nkeynes@309
   243
    fclose(f);
nkeynes@309
   244
}
nkeynes@309
   245
nkeynes@284
   246
void pvr2_vram64_dump( sh4addr_t addr, uint32_t length, FILE *f ) 
nkeynes@284
   247
{
nkeynes@284
   248
    char tmp[length];
nkeynes@284
   249
    pvr2_vram64_read( tmp, addr, length );
nkeynes@284
   250
    fwrite_dump( tmp, length, f );
nkeynes@284
   251
}
.