Search
lxdream.org :: lxdream/src/sh4/sh4mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4mem.c
changeset 571:9bc09948d0f2
prev570:d2893980fbf5
next577:a181aeacd6e8
author nkeynes
date Thu Jan 10 08:28:37 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change More MMU work in progess. Much better now...
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@10
    70
nkeynes@527
    71
int32_t sh4_read_p4( sh4addr_t addr )
nkeynes@10
    72
{
nkeynes@10
    73
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
    74
    if( !io ) {
nkeynes@550
    75
	switch( addr & 0x1F000000 ) {
nkeynes@550
    76
	case 0x00000000: case 0x01000000: case 0x02000000: case 0x03000000: 
nkeynes@550
    77
	    /* Store queue - readable? */
nkeynes@550
    78
	    return 0;
nkeynes@550
    79
	    break;
nkeynes@550
    80
	case 0x10000000: return mmu_icache_addr_read( addr );
nkeynes@550
    81
	case 0x11000000: return mmu_icache_data_read( addr );
nkeynes@550
    82
	case 0x12000000: return mmu_itlb_addr_read( addr );
nkeynes@550
    83
	case 0x13000000: return mmu_itlb_data_read( addr );
nkeynes@550
    84
	case 0x14000000: return mmu_ocache_addr_read( addr );
nkeynes@550
    85
	case 0x15000000: return mmu_ocache_data_read( addr );
nkeynes@550
    86
	case 0x16000000: return mmu_utlb_addr_read( addr );
nkeynes@550
    87
	case 0x17000000: return mmu_utlb_data_read( addr );
nkeynes@550
    88
	default:
nkeynes@550
    89
            WARN( "Attempted read from unknown or invalid P4 region: %08X", addr );
nkeynes@550
    90
	    return 0;
nkeynes@282
    91
        }
nkeynes@10
    92
    } else {
nkeynes@156
    93
	int32_t val = io->io_read( addr&0xFFF );
nkeynes@156
    94
	TRACE_P4IO( "Long read %08X <= %08X", io, (addr&0xFFF), val, addr );
nkeynes@156
    95
        return val;
nkeynes@10
    96
    }    
nkeynes@10
    97
}
nkeynes@10
    98
nkeynes@527
    99
void sh4_write_p4( sh4addr_t addr, int32_t val )
nkeynes@10
   100
{
nkeynes@10
   101
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
   102
    if( !io ) {
nkeynes@550
   103
	switch( addr & 0x1F000000 ) {
nkeynes@550
   104
	case 0x00000000: case 0x01000000: case 0x02000000: case 0x03000000: 
nkeynes@10
   105
            /* Store queue */
nkeynes@10
   106
            SH4_WRITE_STORE_QUEUE( addr, val );
nkeynes@550
   107
	    break;
nkeynes@550
   108
	case 0x10000000: mmu_icache_addr_write( addr, val ); break;
nkeynes@550
   109
	case 0x11000000: mmu_icache_data_write( addr, val ); break;
nkeynes@550
   110
	case 0x12000000: mmu_itlb_addr_write( addr, val ); break;
nkeynes@550
   111
	case 0x13000000: mmu_itlb_data_write( addr, val ); break;
nkeynes@550
   112
	case 0x14000000: mmu_ocache_addr_write( addr, val ); break;
nkeynes@550
   113
	case 0x15000000: mmu_ocache_data_write( addr, val ); break;
nkeynes@550
   114
	case 0x16000000: mmu_utlb_addr_write( addr, val ); break;
nkeynes@550
   115
	case 0x17000000: mmu_utlb_data_write( addr, val ); break;
nkeynes@550
   116
	default:
nkeynes@448
   117
            WARN( "Attempted write to unknown P4 region: %08X", addr );
nkeynes@10
   118
        }
nkeynes@10
   119
    } else {
nkeynes@156
   120
	TRACE_P4IO( "Long write %08X => %08X", io, (addr&0xFFF), val, addr );
nkeynes@10
   121
        io->io_write( addr&0xFFF, val );
nkeynes@10
   122
    }
nkeynes@10
   123
}
nkeynes@10
   124
nkeynes@527
   125
int32_t sh4_read_phys_word( sh4addr_t addr )
nkeynes@10
   126
{
nkeynes@502
   127
    sh4ptr_t page;
nkeynes@83
   128
    if( addr >= 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
   129
        return SIGNEXT16(sh4_read_p4( addr ));
nkeynes@10
   130
    
nkeynes@10
   131
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   132
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   133
    }
nkeynes@10
   134
nkeynes@10
   135
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   136
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   137
        if( page == NULL ) {
nkeynes@448
   138
            WARN( "Attempted word read to missing page: %08X",
nkeynes@10
   139
                   addr );
nkeynes@10
   140
            return 0;
nkeynes@10
   141
        }
nkeynes@504
   142
        return SIGNEXT16(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   143
    } else {
nkeynes@10
   144
        return SIGNEXT16(*(int16_t *)(page+(addr&0xFFF)));
nkeynes@10
   145
    }
nkeynes@10
   146
}
nkeynes@10
   147
nkeynes@527
   148
/**
nkeynes@527
   149
 * Convenience function to read a quad-word (implemented as two long reads).
nkeynes@527
   150
 */
nkeynes@527
   151
int64_t sh4_read_quad( sh4addr_t addr )
nkeynes@527
   152
{
nkeynes@527
   153
    return ((int64_t)((uint32_t)sh4_read_long(addr))) |
nkeynes@527
   154
	(((int64_t)((uint32_t)sh4_read_long(addr+4))) << 32);
nkeynes@527
   155
}
nkeynes@527
   156
nkeynes@570
   157
int32_t sh4_read_long( sh4addr_t addr )
nkeynes@10
   158
{
nkeynes@502
   159
    sh4ptr_t page;
nkeynes@10
   160
    
nkeynes@10
   161
    CHECK_READ_WATCH(addr,4);
nkeynes@10
   162
nkeynes@418
   163
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   164
        return ZEROEXT32(sh4_read_p4( addr ));
nkeynes@418
   165
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   166
	return ZEROEXT32(*(int32_t *)(sh4_main_ram + (addr&0x00FFFFFF)));
nkeynes@418
   167
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   168
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   169
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   170
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   171
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   172
    }
nkeynes@10
   173
nkeynes@10
   174
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   175
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   176
        int32_t val;
nkeynes@10
   177
        if( page == NULL ) {
nkeynes@448
   178
            WARN( "Attempted long read to missing page: %08X", addr );
nkeynes@10
   179
            return 0;
nkeynes@10
   180
        }
nkeynes@504
   181
        val = io_rgn[(uintptr_t)page]->io_read(addr&0xFFF);
nkeynes@10
   182
        TRACE_IO( "Long read %08X <= %08X", page, (addr&0xFFF), val, addr );
nkeynes@559
   183
        return ZEROEXT32(val);
nkeynes@10
   184
    } else {
nkeynes@559
   185
        return ZEROEXT32(*(int32_t *)(page+(addr&0xFFF)));
nkeynes@10
   186
    }
nkeynes@10
   187
}
nkeynes@10
   188
nkeynes@570
   189
int32_t sh4_read_word( sh4addr_t addr )
nkeynes@10
   190
{
nkeynes@502
   191
    sh4ptr_t page;
nkeynes@10
   192
nkeynes@10
   193
    CHECK_READ_WATCH(addr,2);
nkeynes@10
   194
nkeynes@418
   195
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   196
        return ZEROEXT32(SIGNEXT16(sh4_read_p4( addr )));
nkeynes@418
   197
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   198
	return ZEROEXT32(SIGNEXT16(*(int16_t *)(sh4_main_ram + (addr&0x00FFFFFF))));
nkeynes@418
   199
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   200
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   201
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   202
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   203
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   204
    }
nkeynes@217
   205
    
nkeynes@10
   206
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   207
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   208
        int32_t val;
nkeynes@10
   209
        if( page == NULL ) {
nkeynes@448
   210
	    WARN( "Attempted word read to missing page: %08X", addr );
nkeynes@10
   211
            return 0;
nkeynes@10
   212
        }
nkeynes@504
   213
        val = SIGNEXT16(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   214
        TRACE_IO( "Word read %04X <= %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@559
   215
        return ZEROEXT32(val);
nkeynes@10
   216
    } else {
nkeynes@559
   217
        return ZEROEXT32(SIGNEXT16(*(int16_t *)(page+(addr&0xFFF))));
nkeynes@10
   218
    }
nkeynes@10
   219
}
nkeynes@10
   220
nkeynes@570
   221
int32_t sh4_read_byte( sh4addr_t addr )
nkeynes@10
   222
{
nkeynes@502
   223
    sh4ptr_t page;
nkeynes@10
   224
nkeynes@10
   225
    CHECK_READ_WATCH(addr,1);
nkeynes@10
   226
nkeynes@418
   227
    if( addr >= 0xE0000000 ) { /* P4 Area, handled specially */
nkeynes@559
   228
        return ZEROEXT32(SIGNEXT8(sh4_read_p4( addr )));
nkeynes@418
   229
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@559
   230
	return ZEROEXT32(SIGNEXT8(*(int8_t *)(sh4_main_ram + (addr&0x00FFFFFF))));
nkeynes@418
   231
    } else if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   232
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   233
    	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@217
   234
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   235
	pvr2_render_buffer_invalidate(addr, FALSE);
nkeynes@10
   236
    }
nkeynes@217
   237
nkeynes@10
   238
    
nkeynes@10
   239
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   240
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   241
        int32_t val;
nkeynes@10
   242
        if( page == NULL ) {
nkeynes@448
   243
            WARN( "Attempted byte read to missing page: %08X", addr );
nkeynes@10
   244
            return 0;
nkeynes@10
   245
        }
nkeynes@504
   246
        val = SIGNEXT8(io_rgn[(uintptr_t)page]->io_read(addr&0xFFF));
nkeynes@10
   247
        TRACE_IO( "Byte read %02X <= %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@559
   248
        return ZEROEXT32(val);
nkeynes@10
   249
    } else {
nkeynes@559
   250
        return ZEROEXT32(SIGNEXT8(*(int8_t *)(page+(addr&0xFFF))));
nkeynes@10
   251
    }
nkeynes@10
   252
}
nkeynes@10
   253
nkeynes@527
   254
/**
nkeynes@527
   255
 * Convenience function to write a quad-word (implemented as two long writes).
nkeynes@527
   256
 */
nkeynes@527
   257
void sh4_write_quad( sh4addr_t addr, uint64_t val )
nkeynes@527
   258
{
nkeynes@527
   259
    sh4_write_long( addr, (uint32_t)val );
nkeynes@527
   260
    sh4_write_long( addr+4, (uint32_t)(val>>32) );
nkeynes@527
   261
}
nkeynes@527
   262
nkeynes@570
   263
void sh4_write_long( sh4addr_t addr, uint32_t val )
nkeynes@10
   264
{
nkeynes@502
   265
    sh4ptr_t page;
nkeynes@400
   266
nkeynes@10
   267
    CHECK_WRITE_WATCH(addr,4,val);
nkeynes@10
   268
nkeynes@83
   269
    if( addr >= 0xE0000000 ) {
nkeynes@10
   270
        sh4_write_p4( addr, val );
nkeynes@570
   271
        return;
nkeynes@418
   272
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   273
	*(uint32_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   274
	xlat_invalidate_long(addr);
nkeynes@570
   275
	return;
nkeynes@418
   276
    } else if( (addr&0x1F800000) == 0x04000000 || 
nkeynes@418
   277
	       (addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   278
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   279
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   280
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   281
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   282
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   283
    }
nkeynes@10
   284
nkeynes@10
   285
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   286
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@10
   287
        sh4_stop();
nkeynes@570
   288
        return;
nkeynes@10
   289
    }
nkeynes@137
   290
    if( (addr&0x1F800000) == 0x00800000 )
nkeynes@137
   291
	asic_g2_write_word();
nkeynes@137
   292
nkeynes@10
   293
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   294
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   295
        if( page == NULL ) {
nkeynes@137
   296
	    if( (addr & 0x1F000000) >= 0x04000000 &&
nkeynes@137
   297
		(addr & 0x1F000000) < 0x07000000 )
nkeynes@570
   298
		return;
nkeynes@448
   299
            WARN( "Long write to missing page: %08X => %08X", val, addr );
nkeynes@570
   300
            return;
nkeynes@10
   301
        }
nkeynes@10
   302
        TRACE_IO( "Long write %08X => %08X", page, (addr&0xFFF), val, addr );
nkeynes@504
   303
        io_rgn[(uintptr_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   304
    } else {
nkeynes@10
   305
        *(uint32_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   306
    }
nkeynes@10
   307
}
nkeynes@10
   308
nkeynes@570
   309
void sh4_write_word( sh4addr_t addr, uint32_t val )
nkeynes@10
   310
{
nkeynes@502
   311
    sh4ptr_t page;
nkeynes@10
   312
nkeynes@10
   313
    CHECK_WRITE_WATCH(addr,2,val);
nkeynes@10
   314
nkeynes@83
   315
    if( addr >= 0xE0000000 ) {
nkeynes@10
   316
        sh4_write_p4( addr, (int16_t)val );
nkeynes@570
   317
        return;
nkeynes@418
   318
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   319
	*(uint16_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   320
	xlat_invalidate_word(addr);
nkeynes@570
   321
	return;
nkeynes@418
   322
    } else if( (addr&0x1F800000) == 0x04000000 ||
nkeynes@130
   323
	(addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   324
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   325
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   326
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   327
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   328
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   329
    }
nkeynes@406
   330
nkeynes@400
   331
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   332
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@400
   333
        sh4_stop();
nkeynes@570
   334
        return;
nkeynes@400
   335
    }
nkeynes@10
   336
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   337
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   338
        if( page == NULL ) {
nkeynes@448
   339
            WARN( "Attempted word write to missing page: %08X", addr );
nkeynes@570
   340
            return;
nkeynes@10
   341
        }
nkeynes@10
   342
        TRACE_IO( "Word write %04X => %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@504
   343
        io_rgn[(uintptr_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   344
    } else {
nkeynes@10
   345
        *(uint16_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   346
    }
nkeynes@10
   347
}
nkeynes@10
   348
nkeynes@570
   349
void sh4_write_byte( sh4addr_t addr, uint32_t val )
nkeynes@10
   350
{
nkeynes@502
   351
    sh4ptr_t page;
nkeynes@559
   352
nkeynes@10
   353
    CHECK_WRITE_WATCH(addr,1,val);
nkeynes@10
   354
nkeynes@83
   355
    if( addr >= 0xE0000000 ) {
nkeynes@10
   356
        sh4_write_p4( addr, (int8_t)val );
nkeynes@570
   357
        return;
nkeynes@418
   358
    } else if( (addr&0x1C000000) == 0x0C000000 ) {
nkeynes@418
   359
	*(uint8_t *)(sh4_main_ram + (addr&0x00FFFFFF)) = val;
nkeynes@419
   360
	xlat_invalidate_word(addr);
nkeynes@570
   361
	return;
nkeynes@418
   362
    } else if( (addr&0x1F800000) == 0x04000000 ||
nkeynes@418
   363
	       (addr&0x1F800000) == 0x11000000 ) {
nkeynes@150
   364
	texcache_invalidate_page(addr& 0x7FFFFF);
nkeynes@10
   365
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@355
   366
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@165
   367
    } else if( (addr&0x1F800000) == 0x05000000 ) {
nkeynes@355
   368
	pvr2_render_buffer_invalidate(addr, TRUE);
nkeynes@10
   369
    }
nkeynes@10
   370
    
nkeynes@400
   371
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@448
   372
        WARN( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@400
   373
        sh4_stop();
nkeynes@570
   374
        return;
nkeynes@400
   375
    }
nkeynes@10
   376
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@504
   377
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   378
        if( page == NULL ) {
nkeynes@448
   379
            WARN( "Attempted byte write to missing page: %08X", addr );
nkeynes@570
   380
            return;
nkeynes@10
   381
        }
nkeynes@10
   382
        TRACE_IO( "Byte write %02X => %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@504
   383
        io_rgn[(uintptr_t)page]->io_write( (addr&0xFFF), val);
nkeynes@10
   384
    } else {
nkeynes@10
   385
        *(uint8_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   386
    }
nkeynes@10
   387
}
nkeynes@10
   388
nkeynes@10
   389
nkeynes@10
   390
nkeynes@10
   391
/* FIXME: Handle all the many special cases when the range doesn't fall cleanly
nkeynes@400
   392
 * into the same memory block
nkeynes@10
   393
 */
nkeynes@527
   394
void mem_copy_from_sh4( sh4ptr_t dest, sh4addr_t srcaddr, size_t count ) {
nkeynes@103
   395
    if( srcaddr >= 0x04000000 && srcaddr < 0x05000000 ) {
nkeynes@103
   396
	pvr2_vram64_read( dest, srcaddr, count );
nkeynes@103
   397
    } else {
nkeynes@502
   398
	sh4ptr_t src = mem_get_region(srcaddr);
nkeynes@103
   399
	if( src == NULL ) {
nkeynes@448
   400
	    WARN( "Attempted block read from unknown address %08X", srcaddr );
nkeynes@103
   401
	} else {
nkeynes@103
   402
	    memcpy( dest, src, count );
nkeynes@103
   403
	}
nkeynes@103
   404
    }
nkeynes@10
   405
}
nkeynes@10
   406
nkeynes@527
   407
void mem_copy_to_sh4( sh4addr_t destaddr, sh4ptr_t src, size_t count ) {
nkeynes@325
   408
    if( destaddr >= 0x10000000 && destaddr < 0x14000000 ) {
nkeynes@325
   409
	pvr2_dma_write( destaddr, src, count );
nkeynes@325
   410
	return;
nkeynes@325
   411
    } else if( (destaddr & 0x1F800000) == 0x05000000 ) {
nkeynes@355
   412
	pvr2_render_buffer_invalidate( destaddr, TRUE );
nkeynes@325
   413
    } else if( (destaddr & 0x1F800000) == 0x04000000 ) {
nkeynes@103
   414
	pvr2_vram64_write( destaddr, src, count );
nkeynes@325
   415
	return;
nkeynes@325
   416
    }
nkeynes@502
   417
    sh4ptr_t dest = mem_get_region(destaddr);
nkeynes@325
   418
    if( dest == NULL )
nkeynes@448
   419
	WARN( "Attempted block write to unknown address %08X", destaddr );
nkeynes@325
   420
    else {
nkeynes@400
   421
	xlat_invalidate_block( destaddr, count );
nkeynes@325
   422
	memcpy( dest, src, count );
nkeynes@90
   423
    }
nkeynes@10
   424
}
nkeynes@369
   425
nkeynes@527
   426
void sh4_flush_store_queue( sh4addr_t addr )
nkeynes@369
   427
{
nkeynes@369
   428
    /* Store queue operation */
nkeynes@571
   429
    if( IS_MMU_ENABLED() ) {
nkeynes@571
   430
nkeynes@571
   431
    }
nkeynes@369
   432
    int queue = (addr&0x20)>>2;
nkeynes@502
   433
    sh4ptr_t src = (sh4ptr_t)&sh4r.store_queue[queue];
nkeynes@369
   434
    uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
nkeynes@422
   435
    uint32_t target = (addr&0x03FFFFE0) | hi;
nkeynes@369
   436
    mem_copy_to_sh4( target, src, 32 );
nkeynes@369
   437
}
nkeynes@559
   438
nkeynes@559
   439
sh4ptr_t sh4_get_region_by_vma( sh4addr_t vma )
nkeynes@559
   440
{
nkeynes@559
   441
    uint64_t ppa = mmu_vma_to_phys_read(vma);
nkeynes@559
   442
    if( ppa>>32 ) {
nkeynes@559
   443
	return 0;
nkeynes@559
   444
    }
nkeynes@559
   445
nkeynes@559
   446
    sh4addr_t addr = (sh4addr_t)ppa;
nkeynes@559
   447
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@559
   448
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@559
   449
        return NULL;
nkeynes@559
   450
    } else {
nkeynes@559
   451
        return page+(addr&0xFFF);
nkeynes@559
   452
    }
nkeynes@559
   453
}
nkeynes@559
   454
.