Search
lxdream.org :: lxdream/src/pvr2/pvr2mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/pvr2mem.c
changeset 327:00d55a462af3
prev325:5717ae5d4746
next333:e29561c998f5
author nkeynes
date Thu Jan 25 11:46:35 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Cleanup framebuffer formats by adding a central source for the information
file annotate diff log raw
nkeynes@284
     1
/**
nkeynes@327
     2
 * $Id: pvr2mem.c,v 1.7 2007-01-25 11:46:35 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@325
    19
#include "asic.h"
nkeynes@309
    20
#include <stdio.h>
nkeynes@309
    21
#include <errno.h>
nkeynes@284
    22
nkeynes@284
    23
extern char *video_base;
nkeynes@284
    24
nkeynes@325
    25
void pvr2_dma_write( sh4addr_t destaddr, char *src, uint32_t count )
nkeynes@325
    26
{
nkeynes@325
    27
    int region;
nkeynes@325
    28
nkeynes@325
    29
    switch( destaddr & 0x13800000 ) {
nkeynes@325
    30
    case 0x10000000:
nkeynes@325
    31
    case 0x12000000:
nkeynes@325
    32
	pvr2_ta_write( src, count );
nkeynes@325
    33
	break;
nkeynes@325
    34
    case 0x11000000:
nkeynes@325
    35
    case 0x11800000:
nkeynes@325
    36
	region = MMIO_READ( ASIC, PVRDMARGN1 );
nkeynes@325
    37
	if( region == 0 ) {
nkeynes@325
    38
	    pvr2_vram64_write( destaddr, src, count );
nkeynes@325
    39
	} else {
nkeynes@325
    40
	    char *dest = mem_get_region(destaddr);
nkeynes@325
    41
	    memcpy( dest, src, count );
nkeynes@325
    42
	}
nkeynes@325
    43
	break;
nkeynes@325
    44
    case 0x10800000:
nkeynes@325
    45
    case 0x12800000:
nkeynes@325
    46
	pvr2_yuv_write( src, count );
nkeynes@325
    47
	break;
nkeynes@325
    48
    case 0x13000000:
nkeynes@325
    49
    case 0x13800000:
nkeynes@325
    50
	region = MMIO_READ( ASIC, PVRDMARGN2 );
nkeynes@325
    51
	if( region == 0 ) {
nkeynes@325
    52
	    pvr2_vram64_write( destaddr, src, count );
nkeynes@325
    53
	} else {
nkeynes@325
    54
	    char *dest = mem_get_region(destaddr);
nkeynes@325
    55
	    memcpy( dest, src, count );
nkeynes@325
    56
	}
nkeynes@325
    57
    }	    
nkeynes@325
    58
}
nkeynes@325
    59
nkeynes@284
    60
void pvr2_vram64_write( sh4addr_t destaddr, char *src, uint32_t length )
nkeynes@284
    61
{
nkeynes@284
    62
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@284
    63
    uint32_t *banks[2];
nkeynes@284
    64
    uint32_t *dwsrc;
nkeynes@284
    65
    int i;
nkeynes@284
    66
nkeynes@284
    67
    destaddr = destaddr & 0x7FFFFF;
nkeynes@284
    68
    if( destaddr + length > 0x800000 ) {
nkeynes@284
    69
	length = 0x800000 - destaddr;
nkeynes@284
    70
    }
nkeynes@284
    71
nkeynes@284
    72
    for( i=destaddr & 0xFFFFF000; i < destaddr + length; i+= PAGE_SIZE ) {
nkeynes@284
    73
	texcache_invalidate_page( i );
nkeynes@284
    74
    }
nkeynes@284
    75
nkeynes@284
    76
    banks[0] = ((uint32_t *)(video_base + ((destaddr & 0x007FFFF8) >>1)));
nkeynes@284
    77
    banks[1] = banks[0] + 0x100000;
nkeynes@284
    78
    if( bank_flag ) 
nkeynes@284
    79
	banks[0]++;
nkeynes@284
    80
    
nkeynes@284
    81
    /* Handle non-aligned start of source */
nkeynes@284
    82
    if( destaddr & 0x03 ) {
nkeynes@284
    83
	char *dest = ((char *)banks[bank_flag]) + (destaddr & 0x03);
nkeynes@284
    84
	for( i= destaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@284
    85
	    *dest++ = *src++;
nkeynes@284
    86
	}
nkeynes@284
    87
	bank_flag = !bank_flag;
nkeynes@284
    88
    }
nkeynes@284
    89
nkeynes@284
    90
    dwsrc = (uint32_t *)src;
nkeynes@284
    91
    while( length >= 4 ) {
nkeynes@284
    92
	*banks[bank_flag]++ = *dwsrc++;
nkeynes@284
    93
	bank_flag = !bank_flag;
nkeynes@284
    94
	length -= 4;
nkeynes@284
    95
    }
nkeynes@284
    96
    
nkeynes@284
    97
    /* Handle non-aligned end of source */
nkeynes@284
    98
    if( length ) {
nkeynes@284
    99
	src = (char *)dwsrc;
nkeynes@284
   100
	char *dest = (char *)banks[bank_flag];
nkeynes@284
   101
	while( length-- > 0 ) {
nkeynes@284
   102
	    *dest++ = *src++;
nkeynes@284
   103
	}
nkeynes@284
   104
    }  
nkeynes@284
   105
}
nkeynes@284
   106
nkeynes@284
   107
/**
nkeynes@284
   108
 * Write an image to 64-bit vram, with a line-stride different from the line-size.
nkeynes@284
   109
 * The destaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
nkeynes@284
   110
 * must be multiples of 4.
nkeynes@284
   111
 */
nkeynes@284
   112
void pvr2_vram64_write_stride( sh4addr_t destaddr, char *src, uint32_t line_bytes, 
nkeynes@284
   113
			       uint32_t line_stride_bytes, uint32_t line_count )
nkeynes@284
   114
{
nkeynes@284
   115
    int bank_flag = (destaddr & 0x04) >> 2;
nkeynes@284
   116
    uint32_t *banks[2];
nkeynes@284
   117
    uint32_t *dwsrc;
nkeynes@284
   118
    uint32_t line_gap;
nkeynes@284
   119
    int line_gap_flag;
nkeynes@284
   120
    int i,j;
nkeynes@284
   121
nkeynes@284
   122
    destaddr = destaddr & 0x7FFFF8;
nkeynes@284
   123
    i = line_stride_bytes - line_bytes;
nkeynes@284
   124
    line_gap_flag = i & 0x04;
nkeynes@284
   125
    line_gap = i >> 3;
nkeynes@284
   126
    line_bytes >>= 2;
nkeynes@284
   127
nkeynes@284
   128
    for( i=destaddr & 0xFFFFF000; i < destaddr + line_stride_bytes*line_count; i+= PAGE_SIZE ) {
nkeynes@284
   129
	texcache_invalidate_page( i );
nkeynes@284
   130
    }
nkeynes@284
   131
nkeynes@284
   132
    banks[0] = (uint32_t *)(video_base + (destaddr >>1));
nkeynes@284
   133
    banks[1] = banks[0] + 0x100000;
nkeynes@284
   134
    if( bank_flag ) 
nkeynes@284
   135
	banks[0]++;
nkeynes@284
   136
    
nkeynes@284
   137
    dwsrc = (uint32_t *)src;
nkeynes@284
   138
    for( i=0; i<line_count; i++ ) {
nkeynes@284
   139
	for( j=0; j<line_bytes; j++ ) {
nkeynes@284
   140
	    *banks[bank_flag]++ = *dwsrc++;
nkeynes@284
   141
	    bank_flag = !bank_flag;
nkeynes@284
   142
	}
nkeynes@285
   143
	banks[0] += line_gap;
nkeynes@285
   144
	banks[1] += line_gap;
nkeynes@284
   145
	if( line_gap_flag ) {
nkeynes@285
   146
	    banks[bank_flag]++;
nkeynes@284
   147
	    bank_flag = !bank_flag;
nkeynes@284
   148
	}
nkeynes@284
   149
    }    
nkeynes@284
   150
}
nkeynes@284
   151
nkeynes@284
   152
/**
nkeynes@284
   153
 * Read an image from 64-bit vram, with a destination line-stride different from the line-size.
nkeynes@284
   154
 * The srcaddr must be 32-bit aligned, and both line_bytes and line_stride_bytes
nkeynes@284
   155
 * must be multiples of 4. line_stride_bytes must be >= line_bytes.
nkeynes@284
   156
 * This method is used to extract a "stride" texture from vram.
nkeynes@284
   157
 */
nkeynes@284
   158
void pvr2_vram64_read_stride( char *dest, uint32_t dest_line_bytes, sh4addr_t srcaddr,
nkeynes@284
   159
				   uint32_t src_line_bytes, uint32_t line_count )
nkeynes@284
   160
{
nkeynes@284
   161
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@284
   162
    uint32_t *banks[2];
nkeynes@284
   163
    uint32_t *dwdest;
nkeynes@284
   164
    uint32_t dest_line_gap;
nkeynes@284
   165
    uint32_t src_line_gap;
nkeynes@284
   166
    uint32_t line_bytes;
nkeynes@284
   167
    int src_line_gap_flag;
nkeynes@284
   168
    int i,j;
nkeynes@284
   169
nkeynes@284
   170
    srcaddr = srcaddr & 0x7FFFF8;
nkeynes@284
   171
    if( src_line_bytes <= dest_line_bytes ) {
nkeynes@284
   172
	dest_line_gap = (dest_line_bytes - src_line_bytes) >> 2;
nkeynes@284
   173
	src_line_gap = 0;
nkeynes@284
   174
	src_line_gap_flag = 0;
nkeynes@284
   175
	line_bytes = src_line_bytes >> 2;
nkeynes@284
   176
    } else {
nkeynes@284
   177
	i = (src_line_bytes - dest_line_bytes);
nkeynes@284
   178
	src_line_gap_flag = i & 0x04;
nkeynes@284
   179
	src_line_gap = i >> 3;
nkeynes@284
   180
	line_bytes = dest_line_bytes >> 2;
nkeynes@284
   181
    }
nkeynes@284
   182
	
nkeynes@284
   183
    banks[0] = (uint32_t *)(video_base + (srcaddr>>1));
nkeynes@284
   184
    banks[1] = banks[0] + 0x100000;
nkeynes@284
   185
    if( bank_flag )
nkeynes@284
   186
	banks[0]++;
nkeynes@284
   187
    
nkeynes@284
   188
    dwdest = (uint32_t *)dest;
nkeynes@284
   189
    for( i=0; i<line_count; i++ ) {
nkeynes@284
   190
	for( j=0; j<line_bytes; j++ ) {
nkeynes@284
   191
	    *dwdest++ = *banks[bank_flag]++;
nkeynes@284
   192
	    bank_flag = !bank_flag;
nkeynes@284
   193
	}
nkeynes@284
   194
	dwdest += dest_line_gap;
nkeynes@284
   195
	banks[0] += src_line_gap;
nkeynes@284
   196
	banks[1] += src_line_gap;
nkeynes@284
   197
	if( src_line_gap_flag ) {
nkeynes@284
   198
	    banks[bank_flag]++;
nkeynes@284
   199
	    bank_flag = !bank_flag;
nkeynes@284
   200
	}
nkeynes@310
   201
    }    
nkeynes@310
   202
}
nkeynes@310
   203
nkeynes@315
   204
nkeynes@310
   205
/**
nkeynes@310
   206
 * @param dest Destination image buffer
nkeynes@310
   207
 * @param banks Source data expressed as two bank pointers
nkeynes@310
   208
 * @param offset Offset into banks[0] specifying where the next byte
nkeynes@310
   209
 *  to read is (0..3)
nkeynes@310
   210
 * @param x1,y1 Destination coordinates
nkeynes@310
   211
 * @param width Width of current destination block
nkeynes@315
   212
 * @param stride Total width of image (ie stride) in bytes
nkeynes@315
   213
 */
nkeynes@315
   214
nkeynes@315
   215
static void pvr2_vram64_detwiddle_4( uint8_t *dest, uint8_t *banks[2], int offset,
nkeynes@315
   216
				     int x1, int y1, int width, int stride )
nkeynes@315
   217
{
nkeynes@315
   218
    if( width == 2 ) {
nkeynes@315
   219
	x1 = x1 >> 1;
nkeynes@315
   220
	uint8_t t1 = *banks[offset<4?0:1]++;
nkeynes@315
   221
	uint8_t t2 = *banks[offset<3?0:1]++;
nkeynes@315
   222
	dest[y1*stride + x1] = (t1 & 0x0F) | (t2<<4);
nkeynes@315
   223
	dest[(y1+1)*stride + x1] = (t1>>4) | (t2&0xF0);
nkeynes@315
   224
    } else if( width == 4 ) {
nkeynes@315
   225
	pvr2_vram64_detwiddle_4( dest, banks, offset, x1, y1, 2, stride );
nkeynes@315
   226
	pvr2_vram64_detwiddle_4( dest, banks, offset+2, x1, y1+2, 2, stride );
nkeynes@315
   227
	pvr2_vram64_detwiddle_4( dest, banks, offset+4, x1+2, y1, 2, stride );
nkeynes@315
   228
	pvr2_vram64_detwiddle_4( dest, banks, offset+6, x1+2, y1+2, 2, stride );
nkeynes@315
   229
	
nkeynes@315
   230
    } else {
nkeynes@315
   231
	int subdivide = width >> 1;
nkeynes@315
   232
	pvr2_vram64_detwiddle_4( dest, banks, offset, x1, y1, subdivide, stride );
nkeynes@315
   233
	pvr2_vram64_detwiddle_4( dest, banks, offset, x1, y1+subdivide, subdivide, stride );
nkeynes@315
   234
	pvr2_vram64_detwiddle_4( dest, banks, offset, x1+subdivide, y1, subdivide, stride );
nkeynes@315
   235
	pvr2_vram64_detwiddle_4( dest, banks, offset, x1+subdivide, y1+subdivide, subdivide, stride );
nkeynes@315
   236
    }
nkeynes@315
   237
}
nkeynes@315
   238
nkeynes@315
   239
/**
nkeynes@315
   240
 * @param dest Destination image buffer
nkeynes@315
   241
 * @param banks Source data expressed as two bank pointers
nkeynes@315
   242
 * @param offset Offset into banks[0] specifying where the next byte
nkeynes@315
   243
 *  to read is (0..3)
nkeynes@315
   244
 * @param x1,y1 Destination coordinates
nkeynes@315
   245
 * @param width Width of current destination block
nkeynes@315
   246
 * @param stride Total width of image (ie stride)
nkeynes@310
   247
 */
nkeynes@310
   248
nkeynes@310
   249
static void pvr2_vram64_detwiddle_8( uint8_t *dest, uint8_t *banks[2], int offset,
nkeynes@315
   250
				     int x1, int y1, int width, int stride )
nkeynes@310
   251
{
nkeynes@310
   252
    if( width == 2 ) {
nkeynes@315
   253
	dest[y1*stride + x1] = *banks[0]++;
nkeynes@315
   254
	dest[(y1+1)*stride + x1] = *banks[offset<3?0:1]++;
nkeynes@315
   255
	dest[y1*stride + x1 + 1] = *banks[offset<2?0:1]++;
nkeynes@315
   256
	dest[(y1+1)*stride + x1 + 1] = *banks[offset==0?0:1]++;
nkeynes@310
   257
	uint8_t *tmp = banks[0]; /* swap banks */
nkeynes@310
   258
	banks[0] = banks[1];
nkeynes@310
   259
	banks[1] = tmp;
nkeynes@310
   260
    } else {
nkeynes@310
   261
	int subdivide = width >> 1;
nkeynes@315
   262
	pvr2_vram64_detwiddle_8( dest, banks, offset, x1, y1, subdivide, stride );
nkeynes@315
   263
	pvr2_vram64_detwiddle_8( dest, banks, offset, x1, y1+subdivide, subdivide, stride );
nkeynes@315
   264
	pvr2_vram64_detwiddle_8( dest, banks, offset, x1+subdivide, y1, subdivide, stride );
nkeynes@315
   265
	pvr2_vram64_detwiddle_8( dest, banks, offset, x1+subdivide, y1+subdivide, subdivide, stride );
nkeynes@284
   266
    }
nkeynes@310
   267
}
nkeynes@310
   268
nkeynes@310
   269
/**
nkeynes@310
   270
 * @param dest Destination image buffer
nkeynes@310
   271
 * @param banks Source data expressed as two bank pointers
nkeynes@310
   272
 * @param offset Offset into banks[0] specifying where the next word
nkeynes@310
   273
 *  to read is (0 or 1)
nkeynes@310
   274
 * @param x1,y1 Destination coordinates
nkeynes@310
   275
 * @param width Width of current destination block
nkeynes@315
   276
 * @param stride Total width of image (ie stride)
nkeynes@310
   277
 */
nkeynes@310
   278
nkeynes@310
   279
static void pvr2_vram64_detwiddle_16( uint16_t *dest, uint16_t *banks[2], int offset,
nkeynes@315
   280
				      int x1, int y1, int width, int stride )
nkeynes@310
   281
{
nkeynes@310
   282
    if( width == 2 ) {
nkeynes@315
   283
	dest[y1*stride + x1] = *banks[0]++;
nkeynes@315
   284
	dest[(y1+1)*stride + x1] = *banks[offset]++;
nkeynes@315
   285
	dest[y1*stride + x1 + 1] = *banks[1]++;
nkeynes@315
   286
	dest[(y1+1)*stride + x1 + 1] = *banks[offset^1]++;
nkeynes@310
   287
    } else {
nkeynes@310
   288
	int subdivide = width >> 1;
nkeynes@315
   289
	pvr2_vram64_detwiddle_16( dest, banks, offset, x1, y1, subdivide, stride );
nkeynes@315
   290
	pvr2_vram64_detwiddle_16( dest, banks, offset, x1, y1+subdivide, subdivide, stride );
nkeynes@315
   291
	pvr2_vram64_detwiddle_16( dest, banks, offset, x1+subdivide, y1, subdivide, stride );
nkeynes@315
   292
	pvr2_vram64_detwiddle_16( dest, banks, offset, x1+subdivide, y1+subdivide, subdivide, stride );
nkeynes@310
   293
    }
nkeynes@310
   294
}
nkeynes@310
   295
nkeynes@310
   296
/**
nkeynes@315
   297
 * Read an image from 64-bit vram stored as twiddled 4-bit pixels. The 
nkeynes@315
   298
 * image is written out to the destination in detwiddled form.
nkeynes@315
   299
 * @param dest destination buffer, which must be at least width*height/2 in length
nkeynes@315
   300
 * @param srcaddr source address in vram
nkeynes@315
   301
 * @param width image width (must be a power of 2)
nkeynes@315
   302
 * @param height image height (must be a power of 2)
nkeynes@315
   303
 */
nkeynes@315
   304
void pvr2_vram64_read_twiddled_4( char *dest, sh4addr_t srcaddr, uint32_t width, uint32_t height )
nkeynes@315
   305
{
nkeynes@315
   306
    int offset_flag = (srcaddr & 0x07);
nkeynes@315
   307
    uint8_t *banks[2];
nkeynes@315
   308
    uint8_t *wdest = (uint8_t*)dest;
nkeynes@315
   309
    uint32_t stride = width >> 1;
nkeynes@315
   310
    int i,j;
nkeynes@315
   311
nkeynes@315
   312
    srcaddr = srcaddr & 0x7FFFF8;
nkeynes@315
   313
nkeynes@315
   314
    banks[0] = (uint8_t *)(video_base + (srcaddr>>1));
nkeynes@315
   315
    banks[1] = banks[0] + 0x400000;
nkeynes@315
   316
    if( offset_flag & 0x04 ) { // If source is not 64-bit aligned, swap the banks
nkeynes@315
   317
	uint8_t *tmp = banks[0];
nkeynes@315
   318
	banks[0] = banks[1];
nkeynes@315
   319
	banks[1] = tmp + 4;
nkeynes@315
   320
	offset_flag &= 0x03;
nkeynes@315
   321
    }
nkeynes@315
   322
    banks[0] += offset_flag;
nkeynes@315
   323
nkeynes@315
   324
    if( width > height ) {
nkeynes@315
   325
	for( i=0; i<width; i+=height ) {
nkeynes@315
   326
	    pvr2_vram64_detwiddle_4( wdest, banks, offset_flag, i, 0, height, stride );
nkeynes@315
   327
	}
nkeynes@315
   328
    } else if( height > width ) {
nkeynes@315
   329
	for( i=0; i<height; i+=width ) {
nkeynes@315
   330
	    pvr2_vram64_detwiddle_4( wdest, banks, offset_flag, 0, i, width, stride );
nkeynes@315
   331
	}
nkeynes@315
   332
    } else if( width == 1 ) {
nkeynes@315
   333
	*wdest = *banks[0];
nkeynes@315
   334
    } else {
nkeynes@315
   335
	pvr2_vram64_detwiddle_4( wdest, banks, offset_flag, 0, 0, width, stride );
nkeynes@315
   336
    }   
nkeynes@315
   337
}
nkeynes@315
   338
nkeynes@315
   339
/**
nkeynes@310
   340
 * Read an image from 64-bit vram stored as twiddled 8-bit pixels. The 
nkeynes@310
   341
 * image is written out to the destination in detwiddled form.
nkeynes@310
   342
 * @param dest destination buffer, which must be at least width*height in length
nkeynes@310
   343
 * @param srcaddr source address in vram
nkeynes@310
   344
 * @param width image width (must be a power of 2)
nkeynes@310
   345
 * @param height image height (must be a power of 2)
nkeynes@310
   346
 */
nkeynes@310
   347
void pvr2_vram64_read_twiddled_8( char *dest, sh4addr_t srcaddr, uint32_t width, uint32_t height )
nkeynes@310
   348
{
nkeynes@310
   349
    int offset_flag = (srcaddr & 0x07);
nkeynes@310
   350
    uint8_t *banks[2];
nkeynes@310
   351
    uint8_t *wdest = (uint8_t*)dest;
nkeynes@310
   352
    int i,j;
nkeynes@310
   353
nkeynes@310
   354
    srcaddr = srcaddr & 0x7FFFF8;
nkeynes@310
   355
nkeynes@310
   356
    banks[0] = (uint8_t *)(video_base + (srcaddr>>1));
nkeynes@310
   357
    banks[1] = banks[0] + 0x400000;
nkeynes@310
   358
    if( offset_flag & 0x04 ) { // If source is not 64-bit aligned, swap the banks
nkeynes@310
   359
	uint8_t *tmp = banks[0];
nkeynes@310
   360
	banks[0] = banks[1];
nkeynes@310
   361
	banks[1] = tmp + 4;
nkeynes@310
   362
	offset_flag &= 0x03;
nkeynes@310
   363
    }
nkeynes@310
   364
    banks[0] += offset_flag;
nkeynes@310
   365
nkeynes@310
   366
    if( width > height ) {
nkeynes@310
   367
	for( i=0; i<width; i+=height ) {
nkeynes@310
   368
	    pvr2_vram64_detwiddle_8( wdest, banks, offset_flag, i, 0, height, width );
nkeynes@310
   369
	}
nkeynes@310
   370
    } else if( height > width ) {
nkeynes@310
   371
	for( i=0; i<height; i+=width ) {
nkeynes@310
   372
	    pvr2_vram64_detwiddle_8( wdest, banks, offset_flag, 0, i, width, width );
nkeynes@310
   373
	}
nkeynes@310
   374
    } else if( width == 1 ) {
nkeynes@310
   375
	*wdest = *banks[0];
nkeynes@310
   376
    } else {
nkeynes@310
   377
	pvr2_vram64_detwiddle_8( wdest, banks, offset_flag, 0, 0, width, width );
nkeynes@310
   378
    }   
nkeynes@310
   379
}
nkeynes@310
   380
nkeynes@310
   381
/**
nkeynes@310
   382
 * Read an image from 64-bit vram stored as twiddled 16-bit pixels. The 
nkeynes@310
   383
 * image is written out to the destination in detwiddled form.
nkeynes@310
   384
 * @param dest destination buffer, which must be at least width*height*2 in length
nkeynes@310
   385
 * @param srcaddr source address in vram (must be 16-bit aligned)
nkeynes@310
   386
 * @param width image width (must be a power of 2)
nkeynes@310
   387
 * @param height image height (must be a power of 2)
nkeynes@310
   388
 */
nkeynes@310
   389
void pvr2_vram64_read_twiddled_16( char *dest, sh4addr_t srcaddr, uint32_t width, uint32_t height ) {
nkeynes@310
   390
    int offset_flag = (srcaddr & 0x06) >> 1;
nkeynes@310
   391
    uint16_t *banks[2];
nkeynes@310
   392
    uint16_t *wdest = (uint16_t*)dest;
nkeynes@310
   393
    int i,j;
nkeynes@310
   394
nkeynes@310
   395
    srcaddr = srcaddr & 0x7FFFF8;
nkeynes@310
   396
nkeynes@310
   397
    banks[0] = (uint16_t *)(video_base + (srcaddr>>1));
nkeynes@310
   398
    banks[1] = banks[0] + 0x200000;
nkeynes@310
   399
    if( offset_flag & 0x02 ) { // If source is not 64-bit aligned, swap the banks
nkeynes@310
   400
	uint16_t *tmp = banks[0];
nkeynes@310
   401
	banks[0] = banks[1];
nkeynes@310
   402
	banks[1] = tmp + 2;
nkeynes@310
   403
	offset_flag &= 0x01;
nkeynes@310
   404
    }
nkeynes@310
   405
    banks[0] += offset_flag;
nkeynes@310
   406
	
nkeynes@310
   407
nkeynes@310
   408
    if( width > height ) {
nkeynes@310
   409
	for( i=0; i<width; i+=height ) {
nkeynes@310
   410
	    pvr2_vram64_detwiddle_16( wdest, banks, offset_flag, i, 0, height, width );
nkeynes@310
   411
	}
nkeynes@310
   412
    } else if( height > width ) {
nkeynes@310
   413
	for( i=0; i<height; i+=width ) {
nkeynes@310
   414
	    pvr2_vram64_detwiddle_16( wdest, banks, offset_flag, 0, i, width, width );
nkeynes@310
   415
	}
nkeynes@310
   416
    } else if( width == 1 ) {
nkeynes@310
   417
	*wdest = *banks[0];
nkeynes@310
   418
    } else {
nkeynes@310
   419
	pvr2_vram64_detwiddle_16( wdest, banks, offset_flag, 0, 0, width, width );
nkeynes@310
   420
    }    
nkeynes@284
   421
}
nkeynes@284
   422
nkeynes@284
   423
void pvr2_vram_write_invert( sh4addr_t destaddr, char *src, uint32_t length, uint32_t line_length )
nkeynes@284
   424
{
nkeynes@284
   425
    char *dest = video_base + (destaddr & 0x007FFFFF);
nkeynes@284
   426
    char *p = src + length - line_length;
nkeynes@284
   427
    while( p >= src ) {
nkeynes@284
   428
	memcpy( dest, p, line_length );
nkeynes@284
   429
	p -= line_length;
nkeynes@284
   430
	dest += line_length;
nkeynes@284
   431
    }
nkeynes@284
   432
}
nkeynes@284
   433
nkeynes@284
   434
void pvr2_vram64_read( char *dest, sh4addr_t srcaddr, uint32_t length )
nkeynes@284
   435
{
nkeynes@284
   436
    int bank_flag = (srcaddr & 0x04) >> 2;
nkeynes@284
   437
    uint32_t *banks[2];
nkeynes@284
   438
    uint32_t *dwdest;
nkeynes@284
   439
    int i;
nkeynes@284
   440
nkeynes@284
   441
    srcaddr = srcaddr & 0x7FFFFF;
nkeynes@284
   442
    if( srcaddr + length > 0x800000 )
nkeynes@284
   443
	length = 0x800000 - srcaddr;
nkeynes@284
   444
nkeynes@284
   445
    banks[0] = ((uint32_t *)(video_base + ((srcaddr&0x007FFFF8)>>1)));
nkeynes@284
   446
    banks[1] = banks[0] + 0x100000;
nkeynes@284
   447
    if( bank_flag )
nkeynes@284
   448
	banks[0]++;
nkeynes@284
   449
    
nkeynes@284
   450
    /* Handle non-aligned start of source */
nkeynes@284
   451
    if( srcaddr & 0x03 ) {
nkeynes@284
   452
	char *src = ((char *)banks[bank_flag]) + (srcaddr & 0x03);
nkeynes@284
   453
	for( i= srcaddr & 0x03; i < 4 && length > 0; i++, length-- ) {
nkeynes@284
   454
	    *dest++ = *src++;
nkeynes@284
   455
	}
nkeynes@284
   456
	bank_flag = !bank_flag;
nkeynes@284
   457
    }
nkeynes@284
   458
nkeynes@284
   459
    dwdest = (uint32_t *)dest;
nkeynes@284
   460
    while( length >= 4 ) {
nkeynes@284
   461
	*dwdest++ = *banks[bank_flag]++;
nkeynes@284
   462
	bank_flag = !bank_flag;
nkeynes@284
   463
	length -= 4;
nkeynes@284
   464
    }
nkeynes@284
   465
    
nkeynes@284
   466
    /* Handle non-aligned end of source */
nkeynes@284
   467
    if( length ) {
nkeynes@284
   468
	dest = (char *)dwdest;
nkeynes@284
   469
	char *src = (char *)banks[bank_flag];
nkeynes@284
   470
	while( length-- > 0 ) {
nkeynes@284
   471
	    *dest++ = *src++;
nkeynes@284
   472
	}
nkeynes@284
   473
    }
nkeynes@284
   474
}
nkeynes@284
   475
nkeynes@309
   476
void pvr2_vram64_dump_file( sh4addr_t addr, uint32_t length, gchar *filename )
nkeynes@309
   477
{
nkeynes@309
   478
    uint32_t tmp[length>>2];
nkeynes@309
   479
    FILE *f = fopen(filename, "wo");
nkeynes@309
   480
    unsigned int i, j;
nkeynes@309
   481
nkeynes@309
   482
    if( f == NULL ) {
nkeynes@309
   483
	ERROR( "Unable to write to dump file '%s' (%s)", filename, strerror(errno) );
nkeynes@309
   484
	return;
nkeynes@309
   485
    }
nkeynes@309
   486
    pvr2_vram64_read( tmp, addr, length );
nkeynes@309
   487
    fprintf( f, "%08X\n", addr );
nkeynes@309
   488
    for( i =0; i<length>>2; i+=8 ) {
nkeynes@309
   489
	for( j=i; j<i+8; j++ ) {
nkeynes@309
   490
	    if( j < length )
nkeynes@309
   491
		fprintf( f, " %08X", tmp[j] );
nkeynes@309
   492
	    else
nkeynes@309
   493
		fprintf( f, "         " );
nkeynes@309
   494
	}
nkeynes@309
   495
	fprintf( f, "\n" );
nkeynes@309
   496
    }
nkeynes@309
   497
    fclose(f);
nkeynes@309
   498
}
nkeynes@309
   499
nkeynes@284
   500
void pvr2_vram64_dump( sh4addr_t addr, uint32_t length, FILE *f ) 
nkeynes@284
   501
{
nkeynes@284
   502
    char tmp[length];
nkeynes@284
   503
    pvr2_vram64_read( tmp, addr, length );
nkeynes@284
   504
    fwrite_dump( tmp, length, f );
nkeynes@284
   505
}
nkeynes@315
   506
nkeynes@315
   507
nkeynes@315
   508
nkeynes@315
   509
/**
nkeynes@315
   510
 * Flush the indicated render buffer back to PVR. Caller is responsible for
nkeynes@315
   511
 * tracking whether there is actually anything in the buffer.
nkeynes@315
   512
 *
nkeynes@315
   513
 * @param buffer A render buffer indicating the address to store to, and the
nkeynes@315
   514
 * format the data needs to be in.
nkeynes@315
   515
 * @param backBuffer TRUE to flush the back buffer, FALSE for 
nkeynes@315
   516
 * the front buffer.
nkeynes@315
   517
 */
nkeynes@315
   518
void pvr2_render_buffer_copy_to_sh4( pvr2_render_buffer_t buffer, 
nkeynes@315
   519
				     gboolean backBuffer )
nkeynes@315
   520
{
nkeynes@315
   521
    if( buffer->render_addr == -1 )
nkeynes@315
   522
	return;
nkeynes@327
   523
    GLenum type = colour_formats[buffer->colour_format].type;
nkeynes@327
   524
    GLenum format = colour_formats[buffer->colour_format].format;
nkeynes@327
   525
    int line_size = buffer->width * colour_formats[buffer->colour_format].bpp;
nkeynes@327
   526
    int size = line_size * buffer->height;
nkeynes@315
   527
    
nkeynes@315
   528
    if( backBuffer ) {
nkeynes@315
   529
	glFinish();
nkeynes@315
   530
	glReadBuffer( GL_BACK );
nkeynes@315
   531
    } else {
nkeynes@315
   532
	glReadBuffer( GL_FRONT );
nkeynes@315
   533
    }
nkeynes@315
   534
nkeynes@315
   535
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@315
   536
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@315
   537
	char target[size];
nkeynes@315
   538
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@315
   539
	pvr2_vram64_write( buffer->render_addr, target, size );
nkeynes@315
   540
    } else {
nkeynes@315
   541
	/* Regular buffer */
nkeynes@315
   542
	char target[size];
nkeynes@315
   543
	glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@315
   544
	pvr2_vram_write_invert( buffer->render_addr, target, size, line_size );
nkeynes@315
   545
    }
nkeynes@315
   546
}
nkeynes@315
   547
nkeynes@315
   548
nkeynes@315
   549
/**
nkeynes@315
   550
 * Copy data from PVR ram into the GL render buffer. 
nkeynes@315
   551
 *
nkeynes@315
   552
 * @param buffer A render buffer indicating the address to read from, and the
nkeynes@315
   553
 * format the data is in.
nkeynes@315
   554
 * @param backBuffer TRUE to write the back buffer, FALSE for 
nkeynes@315
   555
 * the front buffer.
nkeynes@315
   556
 */
nkeynes@315
   557
void pvr2_render_buffer_copy_from_sh4( pvr2_render_buffer_t buffer, 
nkeynes@315
   558
				       gboolean backBuffer )
nkeynes@315
   559
{
nkeynes@315
   560
    if( buffer->render_addr == -1 )
nkeynes@315
   561
	return;
nkeynes@315
   562
nkeynes@327
   563
    GLenum type = colour_formats[buffer->colour_format].type;
nkeynes@327
   564
    GLenum format = colour_formats[buffer->colour_format].format;
nkeynes@327
   565
    int line_size = buffer->width * colour_formats[buffer->colour_format].bpp;
nkeynes@327
   566
    int size = line_size * buffer->height;
nkeynes@327
   567
nkeynes@315
   568
    if( backBuffer ) {
nkeynes@315
   569
	glDrawBuffer( GL_BACK );
nkeynes@315
   570
    } else {
nkeynes@315
   571
	glDrawBuffer( GL_FRONT );
nkeynes@315
   572
    }
nkeynes@315
   573
nkeynes@315
   574
    glRasterPos2i( 0, 0 );
nkeynes@315
   575
    if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
nkeynes@315
   576
	/* Interlaced buffer. Go the double copy... :( */
nkeynes@315
   577
	char target[size];
nkeynes@315
   578
	pvr2_vram64_read( target, buffer->render_addr, size );
nkeynes@315
   579
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@315
   580
		      format, type, target );
nkeynes@315
   581
    } else {
nkeynes@315
   582
	/* Regular buffer - go direct */
nkeynes@315
   583
	char *target = mem_get_region( buffer->render_addr );
nkeynes@315
   584
	glDrawPixels( buffer->width, buffer->height, 
nkeynes@315
   585
		      format, type, target );
nkeynes@315
   586
    }
nkeynes@315
   587
}
.