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