Search
lxdream.org :: lxdream/src/sh4/sh4mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4mem.c
changeset 35:21a4be098304
prev10:c898b37506e0
next38:9ccc7ac66a9d
author nkeynes
date Mon Dec 26 03:54:55 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Remove modules.h - move definitions into dream.h
Add source string to output list (taken from module name)
ARM Work in progress
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@35
     2
 * $Id: sh4mem.c,v 1.2 2005-12-26 03:54:55 nkeynes Exp $
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 "sh4mmio.h"
nkeynes@10
    27
#include "dreamcast.h"
nkeynes@10
    28
nkeynes@10
    29
#define OC_BASE 0x1C000000
nkeynes@10
    30
#define OC_TOP  0x20000000
nkeynes@10
    31
nkeynes@10
    32
#define TRANSLATE_VIDEO_64BIT_ADDRESS(a)  ( (((a)&0x00FFFFF8)>>1)|(((a)&0x00000004)<<20)|((a)&0x03)|0x05000000 );
nkeynes@10
    33
nkeynes@10
    34
#ifdef ENABLE_WATCH
nkeynes@10
    35
#define CHECK_READ_WATCH( addr, size ) \
nkeynes@10
    36
    if( mem_is_watched(addr,size,WATCH_READ) != NULL ) { \
nkeynes@10
    37
        WARN( "Watch triggered at %08X by %d byte read", addr, size ); \
nkeynes@10
    38
        dreamcast_stop(); \
nkeynes@10
    39
    }
nkeynes@10
    40
#define CHECK_WRITE_WATCH( addr, size, val )                  \
nkeynes@10
    41
    if( mem_is_watched(addr,size,WATCH_WRITE) != NULL ) { \
nkeynes@10
    42
        WARN( "Watch triggered at %08X by %d byte write <= %0*X", addr, size, size*2, val ); \
nkeynes@10
    43
        dreamcast_stop(); \
nkeynes@10
    44
    }
nkeynes@10
    45
#else
nkeynes@10
    46
#define CHECK_READ_WATCH( addr, size )
nkeynes@10
    47
#define CHECK_WRITE_WATCH( addr, size )
nkeynes@10
    48
#endif
nkeynes@10
    49
nkeynes@10
    50
#define TRACE_IO( str, p, r, ... ) if(io_rgn[(uint32_t)p]->trace_flag) \
nkeynes@10
    51
TRACE( str " [%s.%s: %s]", __VA_ARGS__, \
nkeynes@10
    52
    MMIO_NAME_BYNUM((uint32_t)p), MMIO_REGID_BYNUM((uint32_t)p, r), \
nkeynes@10
    53
    MMIO_REGDESC_BYNUM((uint32_t)p, r) )
nkeynes@10
    54
nkeynes@10
    55
extern struct mem_region mem_rgn[];
nkeynes@10
    56
extern struct mmio_region *P4_io[];
nkeynes@10
    57
nkeynes@10
    58
int32_t sh4_read_p4( uint32_t addr )
nkeynes@10
    59
{
nkeynes@10
    60
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
    61
    if( !io ) {
nkeynes@10
    62
        ERROR( "Attempted read from unknown P4 region: %08X", addr );
nkeynes@10
    63
        return 0;
nkeynes@10
    64
    } else {
nkeynes@10
    65
        return io->io_read( addr&0xFFF );
nkeynes@10
    66
    }    
nkeynes@10
    67
}
nkeynes@10
    68
nkeynes@10
    69
void sh4_write_p4( uint32_t addr, int32_t val )
nkeynes@10
    70
{
nkeynes@10
    71
    struct mmio_region *io = P4_io[(addr&0x1FFFFFFF)>>19];
nkeynes@10
    72
    if( !io ) {
nkeynes@10
    73
        if( (addr & 0xFC000000) == 0xE0000000 ) {
nkeynes@10
    74
            /* Store queue */
nkeynes@10
    75
            SH4_WRITE_STORE_QUEUE( addr, val );
nkeynes@10
    76
        } else {
nkeynes@10
    77
            ERROR( "Attempted write to unknown P4 region: %08X", addr );
nkeynes@10
    78
        }
nkeynes@10
    79
    } else {
nkeynes@10
    80
        io->io_write( addr&0xFFF, val );
nkeynes@10
    81
    }
nkeynes@10
    82
}
nkeynes@10
    83
nkeynes@10
    84
int32_t sh4_read_phys_word( uint32_t addr )
nkeynes@10
    85
{
nkeynes@10
    86
    char *page;
nkeynes@10
    87
    if( addr > 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
    88
        return SIGNEXT16(sh4_read_p4( addr ));
nkeynes@10
    89
    
nkeynes@10
    90
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
    91
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
    92
    }
nkeynes@10
    93
nkeynes@10
    94
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
    95
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
    96
        if( page == NULL ) {
nkeynes@10
    97
            ERROR( "Attempted word read to missing page: %08X",
nkeynes@10
    98
                   addr );
nkeynes@10
    99
            return 0;
nkeynes@10
   100
        }
nkeynes@10
   101
        return SIGNEXT16(io_rgn[(uint32_t)page]->io_read(addr&0xFFF));
nkeynes@10
   102
    } else {
nkeynes@10
   103
        return SIGNEXT16(*(int16_t *)(page+(addr&0xFFF)));
nkeynes@10
   104
    }
nkeynes@10
   105
}
nkeynes@10
   106
nkeynes@10
   107
int32_t sh4_read_long( uint32_t addr )
nkeynes@10
   108
{
nkeynes@10
   109
    char *page;
nkeynes@10
   110
    
nkeynes@10
   111
    CHECK_READ_WATCH(addr,4);
nkeynes@10
   112
nkeynes@10
   113
    if( addr > 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
   114
        return sh4_read_p4( addr );
nkeynes@10
   115
    
nkeynes@10
   116
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   117
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   118
    }
nkeynes@10
   119
nkeynes@10
   120
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   121
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   122
        sh4_stop();
nkeynes@10
   123
        return 0;
nkeynes@10
   124
    }
nkeynes@10
   125
nkeynes@10
   126
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   127
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   128
        int32_t val;
nkeynes@10
   129
        if( page == NULL ) {
nkeynes@10
   130
            ERROR( "Attempted long read to missing page: %08X", addr );
nkeynes@10
   131
            return 0;
nkeynes@10
   132
        }
nkeynes@10
   133
        val = io_rgn[(uint32_t)page]->io_read(addr&0xFFF);
nkeynes@10
   134
        TRACE_IO( "Long read %08X <= %08X", page, (addr&0xFFF), val, addr );
nkeynes@10
   135
        return val;
nkeynes@10
   136
    } else {
nkeynes@10
   137
        return *(int32_t *)(page+(addr&0xFFF));
nkeynes@10
   138
    }
nkeynes@10
   139
}
nkeynes@10
   140
nkeynes@10
   141
int32_t sh4_read_word( uint32_t addr )
nkeynes@10
   142
{
nkeynes@10
   143
    char *page;
nkeynes@10
   144
nkeynes@10
   145
    CHECK_READ_WATCH(addr,2);
nkeynes@10
   146
nkeynes@10
   147
    if( addr > 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
   148
        return SIGNEXT16(sh4_read_p4( addr ));
nkeynes@10
   149
    
nkeynes@10
   150
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   151
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   152
    }
nkeynes@10
   153
nkeynes@10
   154
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   155
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   156
        sh4_stop();
nkeynes@10
   157
        return 0;
nkeynes@10
   158
    }
nkeynes@10
   159
nkeynes@10
   160
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   161
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   162
        int32_t val;
nkeynes@10
   163
        if( page == NULL ) {
nkeynes@10
   164
            ERROR( "Attempted word read to missing page: %08X", addr );
nkeynes@10
   165
            return 0;
nkeynes@10
   166
        }
nkeynes@10
   167
        val = SIGNEXT16(io_rgn[(uint32_t)page]->io_read(addr&0xFFF));
nkeynes@10
   168
        TRACE_IO( "Word read %04X <= %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@10
   169
        return val;
nkeynes@10
   170
    } else {
nkeynes@10
   171
        return SIGNEXT16(*(int16_t *)(page+(addr&0xFFF)));
nkeynes@10
   172
    }
nkeynes@10
   173
}
nkeynes@10
   174
nkeynes@10
   175
int32_t sh4_read_byte( uint32_t addr )
nkeynes@10
   176
{
nkeynes@10
   177
    char *page;
nkeynes@10
   178
nkeynes@10
   179
    CHECK_READ_WATCH(addr,1);
nkeynes@10
   180
nkeynes@10
   181
    if( addr > 0xE0000000 ) /* P4 Area, handled specially */
nkeynes@10
   182
        return SIGNEXT8(sh4_read_p4( addr ));
nkeynes@10
   183
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   184
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   185
    }
nkeynes@10
   186
    
nkeynes@10
   187
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   188
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   189
        sh4_stop();
nkeynes@10
   190
        return 0;
nkeynes@10
   191
    }
nkeynes@10
   192
nkeynes@10
   193
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   194
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   195
        int32_t val;
nkeynes@10
   196
        if( page == NULL ) {
nkeynes@10
   197
            ERROR( "Attempted byte read to missing page: %08X", addr );
nkeynes@10
   198
            return 0;
nkeynes@10
   199
        }
nkeynes@10
   200
        val = SIGNEXT8(io_rgn[(uint32_t)page]->io_read(addr&0xFFF));
nkeynes@10
   201
        TRACE_IO( "Byte read %02X <= %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@10
   202
        return val;
nkeynes@10
   203
    } else {
nkeynes@10
   204
        return SIGNEXT8(*(int8_t *)(page+(addr&0xFFF)));
nkeynes@10
   205
    }
nkeynes@10
   206
}
nkeynes@10
   207
nkeynes@10
   208
void sh4_write_long( uint32_t addr, uint32_t val )
nkeynes@10
   209
{
nkeynes@10
   210
    char *page;
nkeynes@10
   211
    
nkeynes@10
   212
    CHECK_WRITE_WATCH(addr,4,val);
nkeynes@10
   213
nkeynes@10
   214
    if( addr > 0xE0000000 ) {
nkeynes@10
   215
        sh4_write_p4( addr, val );
nkeynes@10
   216
        return;
nkeynes@10
   217
    }
nkeynes@10
   218
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   219
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   220
    }
nkeynes@10
   221
nkeynes@10
   222
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   223
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   224
        sh4_stop();
nkeynes@10
   225
        return;
nkeynes@10
   226
    }
nkeynes@10
   227
    if( (addr&0x1FFFFFFF) < 0x200000 ) {
nkeynes@10
   228
        ERROR( "Attempted write to read-only memory: %08X => %08X", val, addr);
nkeynes@10
   229
        sh4_stop();
nkeynes@10
   230
        return;
nkeynes@10
   231
    }
nkeynes@10
   232
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   233
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   234
        if( page == NULL ) {
nkeynes@10
   235
            ERROR( "Long write to missing page: %08X => %08X", val, addr );
nkeynes@10
   236
            return;
nkeynes@10
   237
        }
nkeynes@10
   238
        TRACE_IO( "Long write %08X => %08X", page, (addr&0xFFF), val, addr );
nkeynes@10
   239
        io_rgn[(uint32_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   240
    } else {
nkeynes@10
   241
        *(uint32_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   242
    }
nkeynes@10
   243
}
nkeynes@10
   244
nkeynes@10
   245
void sh4_write_word( uint32_t addr, uint32_t val )
nkeynes@10
   246
{
nkeynes@10
   247
    char *page;
nkeynes@10
   248
nkeynes@10
   249
    CHECK_WRITE_WATCH(addr,2,val);
nkeynes@10
   250
nkeynes@10
   251
    if( addr > 0xE0000000 ) {
nkeynes@10
   252
        sh4_write_p4( addr, (int16_t)val );
nkeynes@10
   253
        return;
nkeynes@10
   254
    }
nkeynes@10
   255
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   256
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   257
    }
nkeynes@10
   258
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   259
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   260
        sh4_stop();
nkeynes@10
   261
        return;
nkeynes@10
   262
    }
nkeynes@10
   263
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   264
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   265
        if( page == NULL ) {
nkeynes@10
   266
            ERROR( "Attempted word write to missing page: %08X", addr );
nkeynes@10
   267
            return;
nkeynes@10
   268
        }
nkeynes@10
   269
        TRACE_IO( "Word write %04X => %08X", page, (addr&0xFFF), val&0xFFFF, addr );
nkeynes@10
   270
        io_rgn[(uint32_t)page]->io_write(addr&0xFFF, val);
nkeynes@10
   271
    } else {
nkeynes@10
   272
        *(uint16_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   273
    }
nkeynes@10
   274
}
nkeynes@10
   275
nkeynes@10
   276
void sh4_write_byte( uint32_t addr, uint32_t val )
nkeynes@10
   277
{
nkeynes@10
   278
    char *page;
nkeynes@10
   279
    
nkeynes@10
   280
    CHECK_WRITE_WATCH(addr,1,val);
nkeynes@10
   281
nkeynes@10
   282
    if( addr > 0xE0000000 ) {
nkeynes@10
   283
        sh4_write_p4( addr, (int8_t)val );
nkeynes@10
   284
        return;
nkeynes@10
   285
    }
nkeynes@10
   286
    if( (addr&0x1F800000) == 0x04000000 ) {
nkeynes@10
   287
        addr = TRANSLATE_VIDEO_64BIT_ADDRESS(addr);
nkeynes@10
   288
    }
nkeynes@10
   289
    
nkeynes@10
   290
    if( IS_MMU_ENABLED() ) {
nkeynes@10
   291
        ERROR( "user-mode & mmu translation not implemented, aborting", NULL );
nkeynes@10
   292
        sh4_stop();
nkeynes@10
   293
        return;
nkeynes@10
   294
    }
nkeynes@10
   295
    page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   296
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   297
        if( page == NULL ) {
nkeynes@10
   298
            ERROR( "Attempted byte write to missing page: %08X", addr );
nkeynes@10
   299
            return;
nkeynes@10
   300
        }
nkeynes@10
   301
        TRACE_IO( "Byte write %02X => %08X", page, (addr&0xFFF), val&0xFF, addr );
nkeynes@10
   302
        io_rgn[(uint32_t)page]->io_write( (addr&0xFFF), val);
nkeynes@10
   303
    } else {
nkeynes@10
   304
        *(uint8_t *)(page+(addr&0xFFF)) = val;
nkeynes@10
   305
    }
nkeynes@10
   306
}
nkeynes@10
   307
nkeynes@10
   308
nkeynes@10
   309
nkeynes@10
   310
/* FIXME: Handle all the many special cases when the range doesn't fall cleanly
nkeynes@10
   311
 * into the same memory black
nkeynes@10
   312
 */
nkeynes@10
   313
void mem_copy_from_sh4( char *dest, uint32_t srcaddr, size_t count ) {
nkeynes@10
   314
    char *src = mem_get_region(srcaddr);
nkeynes@10
   315
    memcpy( dest, src, count );
nkeynes@10
   316
}
nkeynes@10
   317
nkeynes@10
   318
void mem_copy_to_sh4( uint32_t destaddr, char *src, size_t count ) {
nkeynes@10
   319
    char *dest = mem_get_region(destaddr);
nkeynes@10
   320
    memcpy( dest, src, count );
nkeynes@10
   321
}
.