Search
lxdream.org :: lxdream/src/sh4/sh4mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4mem.c
changeset 562:e598411b640b
prev561:533f6b478071
next569:a1c49e1e8776
author nkeynes
date Tue Jan 01 05:48:06 2008 +0000 (11 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Add configure and command-line parameters to perform IO tracing
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@10
     3
 * sh4mem.c is responsible for the SH4's access to memory (including memory
nkeynes@10
     4
 * mapped I/O), using the page maps created in mem.c
nkeynes@10
     5
 *
nkeynes@10
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@10
     7
 *
nkeynes@10
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@10
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@10
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@10
    11
 * (at your option) any later version.
nkeynes@10
    12
 *
nkeynes@10
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@10
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@10
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@10
    16
 * GNU General Public License for more details.
nkeynes@10
    17
 */
nkeynes@10
    18
nkeynes@35
    19
#define MODULE sh4_module
nkeynes@35
    20
nkeynes@10
    21
#include <string.h>
nkeynes@10
    22
#include <zlib.h>
nkeynes@10
    23
#include "dream.h"
nkeynes@10
    24
#include "mem.h"
nkeynes@10
    25
#include "mmio.h"
nkeynes@10
    26
#include "dreamcast.h"
nkeynes@422
    27
#include "sh4/sh4core.h"
nkeynes@422
    28
#include "sh4/sh4mmio.h"
nkeynes@422
    29
#include "sh4/xltcache.h"
nkeynes@100
    30
#include "pvr2/pvr2.h"
nkeynes@422
    31
#include "asic.h"
nkeynes@10
    32
nkeynes@10
    33
#define OC_BASE 0x1C000000
nkeynes@10
    34
#define OC_TOP  0x20000000
nkeynes@10
    35
nkeynes@90
    36
#define TRANSLATE_VIDEO_64BIT_ADDRESS(a)  ( (((a)&0x00FFFFF8)>>1)|(((a)&0x00000004)<<20)|((a)&0x03)|0x05000000 )
nkeynes@10
    37
nkeynes@10
    38
#ifdef ENABLE_WATCH
nkeynes@10
    39
#define CHECK_READ_WATCH( addr, size ) \
nkeynes@10
    40
    if( mem_is_watched(addr,size,WATCH_READ) != NULL ) { \
nkeynes@10
    41
        WARN( "Watch triggered at %08X by %d byte read", addr, size ); \
nkeynes@10
    42
        dreamcast_stop(); \
nkeynes@10
    43
    }
nkeynes@10
    44
#define CHECK_WRITE_WATCH( addr, size, val )                  \
nkeynes@10
    45
    if( mem_is_watched(addr,size,WATCH_WRITE) != NULL ) { \
nkeynes@10
    46
        WARN( "Watch triggered at %08X by %d byte write <= %0*X", addr, size, size*2, val ); \
nkeynes@10
    47
        dreamcast_stop(); \
nkeynes@10
    48
    }
nkeynes@10
    49
#else
nkeynes@10
    50
#define CHECK_READ_WATCH( addr, size )
nkeynes@234
    51
#define CHECK_WRITE_WATCH( addr, size, val )
nkeynes@10
    52
#endif
nkeynes@10
    53
nkeynes@406
    54
#ifdef ENABLE_TRACE_IO
nkeynes@172
    55
#define TRACE_IO( str, p, r, ... ) if(io_rgn[(uint32_t)p]->trace_flag && !MMIO_NOTRACE_BYNUM((uint32_t)p,r)) \
nkeynes@172
    56
    TRACE( str " [%s.%s: %s]", __VA_ARGS__,			       \
nkeynes@10
    57
    MMIO_NAME_BYNUM((uint32_t)p), MMIO_REGID_BYNUM((uint32_t)p, r), \
nkeynes@10
    58
    MMIO_REGDESC_BYNUM((uint32_t)p, r) )
nkeynes@172
    59
#define TRACE_P4IO( str, io, r, ... ) if(io->trace_flag && !MMIO_NOTRACE_IOBYNUM(io,r)) \
nkeynes@156
    60
TRACE( str " [%s.%s: %s]", __VA_ARGS__, \
nkeynes@156
    61
    io->id, MMIO_REGID_IOBYNUM(io, r), \
nkeynes@156
    62
    MMIO_REGDESC_IOBYNUM(io, r) )
nkeynes@406
    63
#else
nkeynes@406
    64
#define TRACE_IO( str, p, r, ... )
nkeynes@406
    65
#define TRACE_P4IO( str, io, r, ... )
nkeynes@406
    66
#endif
nkeynes@10
    67
nkeynes@10
    68
extern struct mem_region mem_rgn[];
nkeynes@10
    69
extern struct mmio_region *P4_io[];
nkeynes@502
    70
sh4ptr_t sh4_main_ram;
nkeynes@10
    71
nkeynes@527
    72
int32_t sh4_read_p4( sh4addr_t addr )
nkeynes@10
    73
{
nkeynes@10
    74
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
    75
    if( !io ) {
nkeynes@550
    76
	switch( addr & 0x1F000000 ) {
nkeynes@550
    77
	case 0x00000000: case 0x01000000: case 0x02000000: case 0x03000000: 
nkeynes@550
    78
	    /* Store queue - readable? */
nkeynes@550
    79
	    return 0;
nkeynes@550
    80
	    break;
nkeynes@550
    81
	case 0x10000000: return mmu_icache_addr_read( addr );
nkeynes@550
    82
	case 0x11000000: return mmu_icache_data_read( addr );
nkeynes@550
    83
	case 0x12000000: return mmu_itlb_addr_read( addr );
nkeynes@550
    84
	case 0x13000000: return mmu_itlb_data_read( addr );
nkeynes@550
    85
	case 0x14000000: return mmu_ocache_addr_read( addr );
nkeynes@550
    86
	case 0x15000000: return mmu_ocache_data_read( addr );
nkeynes@550
    87
	case 0x16000000: return mmu_utlb_addr_read( addr );
nkeynes@550
    88
	case 0x17000000: return mmu_utlb_data_read( addr );
nkeynes@550
    89
	default:
nkeynes@550
    90
            WARN( "Attempted read from unknown or invalid P4 region: %08X", addr );
nkeynes@550
    91
	    return 0;
nkeynes@282
    92
        }
nkeynes@10
    93
    } else {
nkeynes@156
    94
	int32_t val = io->io_read( addr&0xFFF );
nkeynes@156
    95
	TRACE_P4IO( "Long read %08X <= %08X", io, (addr&0xFFF), val, addr );
nkeynes@156
    96
        return val;
nkeynes@10
    97
    }    
nkeynes@10
    98
}
nkeynes@10
    99
nkeynes@527
   100
void sh4_write_p4( sh4addr_t addr, int32_t val )
nkeynes@10
   101
{
nkeynes@10
   102
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
   103
    if( !io ) {
nkeynes@550
   104
	switch( addr & 0x1F000000 ) {
nkeynes@550
   105
	case 0x00000000: case 0x01000000: case 0x02000000: case 0x03000000: 
nkeynes@10
   106
            /* Store queue */
nkeynes@10
   107
            SH4_WRITE_STORE_QUEUE( addr, val );
nkeynes@550
   108
	    break;
nkeynes@550
   109
	case 0x10000000: mmu_icache_addr_write( addr, val ); break;
nkeynes@550
   110
	case 0x11000000: mmu_icache_data_write( addr, val ); break;
nkeynes@550
   111
	case 0x12000000: mmu_itlb_addr_write( addr, val ); break;
nkeynes@550
   112
	case 0x13000000: mmu_itlb_data_write( addr, val ); break;
nkeynes@550
   113
	case 0x14000000: mmu_ocache_addr_write( addr, val ); break;
nkeynes@550
   114
	case 0x15000000: mmu_ocache_data_write( addr, val ); break;
nkeynes@550
   115
	case 0x16000000: mmu_utlb_addr_write( addr, val ); break;
nkeynes@550
   116
	case 0x17000000: mmu_utlb_data_write( addr, val ); break;
nkeynes@550
   117
	default:
nkeynes@448
   118
            WARN( "Attempted write to unknown P4 region: %08X", addr );
nkeynes@10
   119
        }
nkeynes@10
   120
    } else {
nkeynes@156
   121
	TRACE_P4IO( "Long write %08X => %08X", io, (addr&0xFFF), val, addr );
nkeynes@10
   122
        io->io_write( addr&0xFFF, val );
nkeynes@10
   123
    }
nkeynes@10
   124
}
nkeynes@10
   125
nkeynes@527
   126
int32_t sh4_read_phys_word( sh4addr_t addr )
nkeynes@10
   127
{
nkeynes@502
   128
    sh4ptr_t page;
nkeynes@83
   129
    if( addr >= 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
   130
        return SIGNEXT16(sh4_read_p4( addr ));
nkeynes@10
   131
    
nkeynes@10
   132
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   133
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   134
    }
nkeynes@10
   135
nkeynes@10
   136
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   137
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   138
        if( page == NULL ) {
nkeynes@448
   139
            WARN( "Attempted word read to missing page: %08X",
nkeynes@10
   140
                   addr );
nkeynes@10
   141
            return 0;
nkeynes@10
   142
        }
nkeynes@504
   143
        return SIGNEXT16(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   144
    } else {
nkeynes@10
   145
        return SIGNEXT16(*(int16_t *)(page+(addr&0xFFF)));
nkeynes@10
   146
    }
nkeynes@10
   147
}
nkeynes@10
   148
nkeynes@527
   149
/**
nkeynes@527
   150
 * Convenience function to read a quad-word (implemented as two long reads).
nkeynes@527
   151
 */
nkeynes@527
   152
int64_t sh4_read_quad( sh4addr_t addr )
nkeynes@527
   153
{
nkeynes@527
   154
    return ((int64_t)((uint32_t)sh4_read_long(addr))) |
nkeynes@527
   155
	(((int64_t)((uint32_t)sh4_read_long(addr+4))) << 32);
nkeynes@527
   156
}
nkeynes@527
   157
nkeynes@559
   158
int64_t sh4_read_long( sh4addr_t vma )
nkeynes@10
   159
{
nkeynes@502
   160
    sh4ptr_t page;
nkeynes@10
   161
    
nkeynes@10
   162
    CHECK_READ_WATCH(addr,4);
nkeynes@10
   163
nkeynes@559
   164
    uint64_t ppa = mmu_vma_to_phys_read(vma);
nkeynes@559
   165
    if( ppa>>32 ) {
nkeynes@559
   166
	return ppa;
nkeynes@559
   167
    }
nkeynes@559
   168
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   169
nkeynes@418
   170
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   171
        return ZEROEXT32(sh4_read_p4( addr ));
nkeynes@418
   172
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   173
	return ZEROEXT32(*(int32_t *)(sh4_main_ram + (addr&0x00FFFFFF)));
nkeynes@418
   174
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   175
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   176
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   177
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   178
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   179
    }
nkeynes@10
   180
nkeynes@10
   181
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   182
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   183
        int32_t val;
nkeynes@10
   184
        if( page == NULL ) {
nkeynes@448
   185
            WARN( "Attempted long read to missing page: %08X", addr );
nkeynes@10
   186
            return 0;
nkeynes@10
   187
        }
nkeynes@504
   188
        val = io_rgn[(uintptr_t)page]->io_read(addr&0xFFF);
nkeynes@10
   189
        TRACE_IO( "Long read %08X <= %08X", page, (addr&0xFFF), val, addr );
nkeynes@559
   190
        return ZEROEXT32(val);
nkeynes@10
   191
    } else {
nkeynes@559
   192
        return ZEROEXT32(*(int32_t *)(page+(addr&0xFFF)));
nkeynes@10
   193
    }
nkeynes@10
   194
}
nkeynes@10
   195
nkeynes@559
   196
int64_t sh4_read_word( sh4addr_t vma )
nkeynes@10
   197
{
nkeynes@502
   198
    sh4ptr_t page;
nkeynes@10
   199
nkeynes@10
   200
    CHECK_READ_WATCH(addr,2);
nkeynes@10
   201
nkeynes@559
   202
    uint64_t ppa = mmu_vma_to_phys_read(vma);
nkeynes@559
   203
    if( ppa>>32 ) {
nkeynes@559
   204
	return ppa;
nkeynes@559
   205
    }
nkeynes@559
   206
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   207
nkeynes@418
   208
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   209
        return ZEROEXT32(SIGNEXT16(sh4_read_p4( addr )));
nkeynes@418
   210
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   211
	return ZEROEXT32(SIGNEXT16(*(int16_t *)(sh4_main_ram + (addr&0x00FFFFFF))));
nkeynes@418
   212
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   213
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   214
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   215
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   216
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   217
    }
nkeynes@217
   218
    
nkeynes@10
   219
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   220
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   221
        int32_t val;
nkeynes@10
   222
        if( page == NULL ) {
nkeynes@448
   223
	    WARN( "Attempted word read to missing page: %08X", addr );
nkeynes@10
   224
            return 0;
nkeynes@10
   225
        }
nkeynes@504
   226
        val = SIGNEXT16(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   227
        TRACE_IO( "Word read %04X <= %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@559
   228
        return ZEROEXT32(val);
nkeynes@10
   229
    } else {
nkeynes@559
   230
        return ZEROEXT32(SIGNEXT16(*(int16_t *)(page+(addr&0xFFF))));
nkeynes@10
   231
    }
nkeynes@10
   232
}
nkeynes@10
   233
nkeynes@559
   234
int64_t sh4_read_byte( sh4addr_t vma )
nkeynes@10
   235
{
nkeynes@502
   236
    sh4ptr_t page;
nkeynes@10
   237
nkeynes@10
   238
    CHECK_READ_WATCH(addr,1);
nkeynes@10
   239
nkeynes@559
   240
    uint64_t ppa = mmu_vma_to_phys_read(vma);
nkeynes@559
   241
    if( ppa>>32 ) {
nkeynes@559
   242
	return ppa;
nkeynes@559
   243
    }
nkeynes@559
   244
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   245
nkeynes@418
   246
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   247
        return ZEROEXT32(SIGNEXT8(sh4_read_p4( addr )));
nkeynes@418
   248
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   249
	return ZEROEXT32(SIGNEXT8(*(int8_t *)(sh4_main_ram + (addr&0x00FFFFFF))));
nkeynes@418
   250
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   251
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   252
    	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   253
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   254
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   255
    }
nkeynes@217
   256
nkeynes@10
   257
    
nkeynes@10
   258
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   259
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   260
        int32_t val;
nkeynes@10
   261
        if( page == NULL ) {
nkeynes@448
   262
            WARN( "Attempted byte read to missing page: %08X", addr );
nkeynes@10
   263
            return 0;
nkeynes@10
   264
        }
nkeynes@504
   265
        val = SIGNEXT8(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   266
        TRACE_IO( "Byte read %02X <= %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@559
   267
        return ZEROEXT32(val);
nkeynes@10
   268
    } else {
nkeynes@559
   269
        return ZEROEXT32(SIGNEXT8(*(int8_t *)(page+(addr&0xFFF))));
nkeynes@10
   270
    }
nkeynes@10
   271
}
nkeynes@10
   272
nkeynes@527
   273
/**
nkeynes@527
   274
 * Convenience function to write a quad-word (implemented as two long writes).
nkeynes@527
   275
 */
nkeynes@527
   276
void sh4_write_quad( sh4addr_t addr, uint64_t val )
nkeynes@527
   277
{
nkeynes@527
   278
    sh4_write_long( addr, (uint32_t)val );
nkeynes@527
   279
    sh4_write_long( addr+4, (uint32_t)(val>>32) );
nkeynes@527
   280
}
nkeynes@527
   281
nkeynes@559
   282
int32_t sh4_write_long( sh4addr_t vma, uint32_t val )
nkeynes@10
   283
{
nkeynes@502
   284
    sh4ptr_t page;
nkeynes@400
   285
nkeynes@559
   286
    uint64_t ppa = mmu_vma_to_phys_write(vma);
nkeynes@559
   287
    if( ppa>>32 ) {
nkeynes@559
   288
	return ppa>>32;
nkeynes@559
   289
    }
nkeynes@559
   290
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   291
nkeynes@10
   292
    CHECK_WRITE_WATCH(addr,4,val);
nkeynes@10
   293
nkeynes@83
   294
    if( addr >= 0xE0000000 ) {
nkeynes@10
   295
        sh4_write_p4( addr, val );
nkeynes@559
   296
        return 0;
nkeynes@418
   297
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   298
	*(uint32_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   299
	xlat_invalidate_long(addr);
nkeynes@559
   300
	return 0;
nkeynes@418
   301
    } else if( (addr&0x1F800000) == 0x04000000 || 
nkeynes@418
   302
	       (addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   303
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   304
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   305
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   306
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   307
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   308
    }
nkeynes@10
   309
nkeynes@10
   310
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   311
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@10
   312
        sh4_stop();
nkeynes@559
   313
        return 0;
nkeynes@10
   314
    }
nkeynes@137
   315
    if( (addr&0x1F800000) == 0x00800000 )
nkeynes@137
   316
	asic_g2_write_word();
nkeynes@137
   317
nkeynes@10
   318
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   319
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   320
        if( page == NULL ) {
nkeynes@137
   321
	    if( (addr & 0x1F000000) >= 0x04000000 &&
nkeynes@137
   322
		(addr & 0x1F000000) < 0x07000000 )
nkeynes@559
   323
		return 0;
nkeynes@448
   324
            WARN( "Long write to missing page: %08X => %08X", val, addr );
nkeynes@559
   325
            return 0;
nkeynes@10
   326
        }
nkeynes@10
   327
        TRACE_IO( "Long write %08X => %08X", page, (addr&0xFFF), val, addr );
nkeynes@504
   328
        io_rgn[(uintptr_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   329
    } else {
nkeynes@10
   330
        *(uint32_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   331
    }
nkeynes@559
   332
    return 0;
nkeynes@10
   333
}
nkeynes@10
   334
nkeynes@559
   335
int32_t sh4_write_word( sh4addr_t vma, uint32_t val )
nkeynes@10
   336
{
nkeynes@502
   337
    sh4ptr_t page;
nkeynes@10
   338
nkeynes@559
   339
    uint64_t ppa = mmu_vma_to_phys_write(vma);
nkeynes@559
   340
    if( ppa>>32 ) {
nkeynes@559
   341
	return ppa>>32;
nkeynes@559
   342
    }
nkeynes@559
   343
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   344
nkeynes@10
   345
    CHECK_WRITE_WATCH(addr,2,val);
nkeynes@10
   346
nkeynes@83
   347
    if( addr >= 0xE0000000 ) {
nkeynes@10
   348
        sh4_write_p4( addr, (int16_t)val );
nkeynes@559
   349
        return 0;
nkeynes@418
   350
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   351
	*(uint16_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   352
	xlat_invalidate_word(addr);
nkeynes@559
   353
	return 0;
nkeynes@418
   354
    } else if( (addr&0x1F800000) == 0x04000000 ||
nkeynes@130
   355
	(addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   356
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   357
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   358
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   359
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   360
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   361
    }
nkeynes@406
   362
nkeynes@400
   363
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   364
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@400
   365
        sh4_stop();
nkeynes@559
   366
        return 0;
nkeynes@400
   367
    }
nkeynes@10
   368
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   369
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   370
        if( page == NULL ) {
nkeynes@448
   371
            WARN( "Attempted word write to missing page: %08X", addr );
nkeynes@559
   372
            return 0;
nkeynes@10
   373
        }
nkeynes@10
   374
        TRACE_IO( "Word write %04X => %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@504
   375
        io_rgn[(uintptr_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   376
    } else {
nkeynes@10
   377
        *(uint16_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   378
    }
nkeynes@559
   379
    return 0;
nkeynes@10
   380
}
nkeynes@10
   381
nkeynes@559
   382
int32_t sh4_write_byte( sh4addr_t vma, uint32_t val )
nkeynes@10
   383
{
nkeynes@502
   384
    sh4ptr_t page;
nkeynes@559
   385
nkeynes@559
   386
    uint64_t ppa = mmu_vma_to_phys_write(vma);
nkeynes@559
   387
    if( ppa>>32 ) {
nkeynes@559
   388
	return ppa>>32;
nkeynes@559
   389
    }
nkeynes@559
   390
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@10
   391
    
nkeynes@10
   392
    CHECK_WRITE_WATCH(addr,1,val);
nkeynes@10
   393
nkeynes@83
   394
    if( addr >= 0xE0000000 ) {
nkeynes@10
   395
        sh4_write_p4( addr, (int8_t)val );
nkeynes@559
   396
        return 0;
nkeynes@418
   397
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   398
	*(uint8_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   399
	xlat_invalidate_word(addr);
nkeynes@559
   400
	return 0;
nkeynes@418
   401
    } else if( (addr&0x1F800000) == 0x04000000 ||
nkeynes@418
   402
	       (addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   403
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   404
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   405
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   406
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   407
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   408
    }
nkeynes@10
   409
    
nkeynes@400
   410
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   411
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@400
   412
        sh4_stop();
nkeynes@559
   413
        return 0;
nkeynes@400
   414
    }
nkeynes@10
   415
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   416
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   417
        if( page == NULL ) {
nkeynes@448
   418
            WARN( "Attempted byte write to missing page: %08X", addr );
nkeynes@559
   419
            return 0;
nkeynes@10
   420
        }
nkeynes@10
   421
        TRACE_IO( "Byte write %02X => %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@504
   422
        io_rgn[(uintptr_t)page]->io_write( (addr&0xFFF), val);
nkeynes@10
   423
    } else {
nkeynes@10
   424
        *(uint8_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   425
    }
nkeynes@559
   426
    return 0;
nkeynes@10
   427
}
nkeynes@10
   428
nkeynes@10
   429
nkeynes@10
   430
nkeynes@10
   431
/* FIXME: Handle all the many special cases when the range doesn't fall cleanly
nkeynes@400
   432
 * into the same memory block
nkeynes@10
   433
 */
nkeynes@527
   434
void mem_copy_from_sh4( sh4ptr_t dest, sh4addr_t srcaddr, size_t count ) {
nkeynes@103
   435
    if( srcaddr >= 0x04000000 && srcaddr < 0x05000000 ) {
nkeynes@103
   436
	pvr2_vram64_read( dest, srcaddr, count );
nkeynes@103
   437
    } else {
nkeynes@502
   438
	sh4ptr_t src = mem_get_region(srcaddr);
nkeynes@103
   439
	if( src == NULL ) {
nkeynes@448
   440
	    WARN( "Attempted block read from unknown address %08X", srcaddr );
nkeynes@103
   441
	} else {
nkeynes@103
   442
	    memcpy( dest, src, count );
nkeynes@103
   443
	}
nkeynes@103
   444
    }
nkeynes@10
   445
}
nkeynes@10
   446
nkeynes@527
   447
void mem_copy_to_sh4( sh4addr_t destaddr, sh4ptr_t src, size_t count ) {
nkeynes@325
   448
    if( destaddr >= 0x10000000 && destaddr < 0x14000000 ) {
nkeynes@325
   449
	pvr2_dma_write( destaddr, src, count );
nkeynes@325
   450
	return;
nkeynes@325
   451
    } else if( (destaddr & 0x1F800000) == 0x05000000 ) {
nkeynes@355
   452
	pvr2_render_buffer_invalidate( destaddr, TRUE );
nkeynes@325
   453
    } else if( (destaddr & 0x1F800000) == 0x04000000 ) {
nkeynes@103
   454
	pvr2_vram64_write( destaddr, src, count );
nkeynes@325
   455
	return;
nkeynes@325
   456
    }
nkeynes@502
   457
    sh4ptr_t dest = mem_get_region(destaddr);
nkeynes@325
   458
    if( dest == NULL )
nkeynes@448
   459
	WARN( "Attempted block write to unknown address %08X", destaddr );
nkeynes@325
   460
    else {
nkeynes@400
   461
	xlat_invalidate_block( destaddr, count );
nkeynes@325
   462
	memcpy( dest, src, count );
nkeynes@90
   463
    }
nkeynes@10
   464
}
nkeynes@369
   465
nkeynes@527
   466
void sh4_flush_store_queue( sh4addr_t addr )
nkeynes@369
   467
{
nkeynes@369
   468
    /* Store queue operation */
nkeynes@369
   469
    int queue = (addr&0x20)>>2;
nkeynes@502
   470
    sh4ptr_t src = (sh4ptr_t)&sh4r.store_queue[queue];
nkeynes@369
   471
    uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
nkeynes@422
   472
    uint32_t target = (addr&0x03FFFFE0) | hi;
nkeynes@369
   473
    mem_copy_to_sh4( target, src, 32 );
nkeynes@369
   474
}
nkeynes@559
   475
nkeynes@559
   476
sh4ptr_t sh4_get_region_by_vma( sh4addr_t vma )
nkeynes@559
   477
{
nkeynes@559
   478
    uint64_t ppa = mmu_vma_to_phys_read(vma);
nkeynes@559
   479
    if( ppa>>32 ) {
nkeynes@559
   480
	return 0;
nkeynes@559
   481
    }
nkeynes@559
   482
nkeynes@559
   483
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   484
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@559
   485
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@559
   486
        return NULL;
nkeynes@559
   487
    } else {
nkeynes@559
   488
        return page+(addr&0xFFF);
nkeynes@559
   489
    }
nkeynes@559
   490
}
nkeynes@559
   491
.