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