Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 975:007bf7eb944f
prev954:59e17ce91c55
next978:eed5089fcfdb
author nkeynes
date Mon Jan 26 07:26:24 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add read_byte_for_write mem function for correct implementation of AND.B and friends
with TLB enabled.
Add read_byte and read_long MMIO stubs to do correct sign extension of IO reads
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@359
     3
 * 
nkeynes@359
     4
 * SH4 translation core module. This part handles the non-target-specific
nkeynes@359
     5
 * section of the translation.
nkeynes@359
     6
 *
nkeynes@359
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@359
     8
 *
nkeynes@359
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@359
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@359
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@359
    12
 * (at your option) any later version.
nkeynes@359
    13
 *
nkeynes@359
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@359
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@359
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@359
    17
 * GNU General Public License for more details.
nkeynes@359
    18
 */
nkeynes@398
    19
#include <assert.h>
nkeynes@430
    20
#include "eventq.h"
nkeynes@430
    21
#include "syscall.h"
nkeynes@586
    22
#include "clock.h"
nkeynes@669
    23
#include "dreamcast.h"
nkeynes@430
    24
#include "sh4/sh4core.h"
nkeynes@430
    25
#include "sh4/sh4trans.h"
nkeynes@430
    26
#include "sh4/xltcache.h"
nkeynes@975
    27
#include "sh4/sh4mmio.h"
nkeynes@975
    28
#include "sh4/mmu.h"
nkeynes@359
    29
nkeynes@975
    30
unsigned long block_count = 0;
nkeynes@975
    31
nkeynes@975
    32
static void dump_state()
nkeynes@975
    33
{
nkeynes@975
    34
    int i;
nkeynes@975
    35
    printf( "%08x: [%08x] ", sh4r.pc, mmu_urc % mmu_urb );
nkeynes@975
    36
    for( i=0; i<16; i++ ) {
nkeynes@975
    37
        printf( " %08x", sh4r.r[i] );
nkeynes@975
    38
    }
nkeynes@975
    39
    printf( "\n" );
nkeynes@975
    40
}
nkeynes@586
    41
nkeynes@359
    42
/**
nkeynes@359
    43
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    44
 */
nkeynes@740
    45
uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
nkeynes@359
    46
{
nkeynes@408
    47
    void * (*code)() = NULL;
nkeynes@368
    48
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@736
    49
        if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@736
    50
            if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@736
    51
                event_execute();
nkeynes@736
    52
            }
nkeynes@736
    53
            /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@736
    54
            if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@736
    55
                sh4_accept_interrupt();
nkeynes@736
    56
                code = NULL;
nkeynes@736
    57
            }
nkeynes@736
    58
        }
nkeynes@359
    59
nkeynes@736
    60
        if( code == NULL ) {
nkeynes@736
    61
            if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@736
    62
                syscall_invoke( sh4r.pc );
nkeynes@736
    63
                sh4r.in_delay_slot = 0;
nkeynes@736
    64
                sh4r.pc = sh4r.pr;
nkeynes@736
    65
            }
nkeynes@736
    66
nkeynes@736
    67
            code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@953
    68
            if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@736
    69
                code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    70
            }
nkeynes@953
    71
        } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@954
    72
            if( !IS_IN_ICACHE(sh4r.pc) ) {
nkeynes@954
    73
                /* If TLB is off, we may have gotten here without updating
nkeynes@954
    74
                 * the icache, so do it now. This should never fail, so...
nkeynes@954
    75
                 */
nkeynes@954
    76
                mmu_update_icache(sh4r.pc);
nkeynes@954
    77
                assert( IS_IN_ICACHE(sh4r.pc) ); 
nkeynes@954
    78
            }
nkeynes@953
    79
            code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    80
        }
nkeynes@975
    81
//        if( IS_TLB_ENABLED() ) {
nkeynes@975
    82
//            block_count++;
nkeynes@975
    83
//            if( block_count == 52048225 ) {
nkeynes@975
    84
//                dump_state();
nkeynes@975
    85
//            }
nkeynes@975
    86
//        }
nkeynes@736
    87
        code = code();
nkeynes@359
    88
    }
nkeynes@359
    89
    return nanosecs;
nkeynes@359
    90
}
nkeynes@359
    91
nkeynes@359
    92
uint8_t *xlat_output;
nkeynes@596
    93
xlat_cache_block_t xlat_current_block;
nkeynes@586
    94
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
    95
uint32_t xlat_recovery_posn;
nkeynes@359
    96
nkeynes@596
    97
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
    98
{
nkeynes@596
    99
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@736
   100
        ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
   101
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
   102
    xlat_recovery_posn++;
nkeynes@596
   103
}
nkeynes@596
   104
nkeynes@359
   105
/**
nkeynes@359
   106
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
   107
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
   108
 * is reached.
nkeynes@914
   109
 * @param start VMA of the block start (which must already be in the icache)
nkeynes@359
   110
 * @return the address of the translated block
nkeynes@359
   111
 * eg due to lack of buffer space.
nkeynes@359
   112
 */
nkeynes@359
   113
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
   114
{
nkeynes@408
   115
    sh4addr_t pc = start;
nkeynes@410
   116
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
   117
    int done, i;
nkeynes@914
   118
    xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
nkeynes@596
   119
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
   120
    xlat_recovery_posn = 0;
nkeynes@596
   121
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
   122
nkeynes@588
   123
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
   124
        lastpc = GET_ICACHE_END();
nkeynes@588
   125
    }
nkeynes@588
   126
nkeynes@408
   127
    sh4_translate_begin_block(pc);
nkeynes@359
   128
nkeynes@408
   129
    do {
nkeynes@711
   130
        /* check for breakpoints at this pc */
nkeynes@711
   131
        for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@711
   132
            if( sh4_breakpoints[i].address == pc ) {
nkeynes@711
   133
                sh4_translate_emit_breakpoint(pc);
nkeynes@711
   134
                break;
nkeynes@711
   135
            }
nkeynes@711
   136
        }
nkeynes@711
   137
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   138
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   139
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   140
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   141
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   142
        }
nkeynes@711
   143
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   144
        assert( xlat_output <= eob );
nkeynes@711
   145
        pc += 2;
nkeynes@711
   146
        if ( pc >= lastpc ) {
nkeynes@711
   147
            done = 2;
nkeynes@711
   148
        }
nkeynes@408
   149
    } while( !done );
nkeynes@408
   150
    pc += (done - 2);
nkeynes@617
   151
nkeynes@617
   152
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   153
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   154
nkeynes@593
   155
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   156
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   157
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   158
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   159
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   160
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   161
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   162
    }	
nkeynes@368
   163
    sh4_translate_end_block(pc);
nkeynes@711
   164
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@736
   165
nkeynes@586
   166
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   167
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   168
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   169
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@953
   170
    xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
nkeynes@586
   171
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   172
    return xlat_current_block->code;
nkeynes@359
   173
}
nkeynes@359
   174
nkeynes@398
   175
/**
nkeynes@586
   176
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   177
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   178
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   179
 * call into generated code).
nkeynes@398
   180
 */
nkeynes@586
   181
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   182
{
nkeynes@586
   183
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   184
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   185
}
nkeynes@359
   186
nkeynes@953
   187
/**
nkeynes@953
   188
 * Same as sh4_translate_run_recovery, but is used to recover from a taken
nkeynes@953
   189
 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
nkeynes@953
   190
 */
nkeynes@953
   191
void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
nkeynes@953
   192
{
nkeynes@953
   193
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@953
   194
    sh4r.spc += (recovery->sh4_icount<<1);
nkeynes@953
   195
}    
nkeynes@953
   196
nkeynes@740
   197
void sh4_translate_exit_recover( )
nkeynes@586
   198
{
nkeynes@906
   199
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   200
    if( code != NULL ) {
nkeynes@906
   201
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   202
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   203
        if( pc != NULL ) {
nkeynes@906
   204
            // could be null if we're not actually running inside the translator
nkeynes@953
   205
            xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
nkeynes@906
   206
            if( recover != NULL ) {
nkeynes@906
   207
                // Can be null if there is no recovery necessary
nkeynes@906
   208
                sh4_translate_run_recovery(recover);
nkeynes@906
   209
            }
nkeynes@736
   210
        }
nkeynes@398
   211
    }
nkeynes@586
   212
}
nkeynes@398
   213
nkeynes@953
   214
void sh4_translate_exception_exit_recover( )
nkeynes@953
   215
{
nkeynes@953
   216
    void *code = xlat_get_code_by_vma( sh4r.spc );
nkeynes@953
   217
    if( code != NULL ) {
nkeynes@953
   218
        uint32_t size = xlat_get_code_size( code );
nkeynes@953
   219
        void *pc = xlat_get_native_pc( code, size );
nkeynes@953
   220
        if( pc != NULL ) {
nkeynes@953
   221
            // could be null if we're not actually running inside the translator
nkeynes@953
   222
            xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
nkeynes@953
   223
            if( recover != NULL ) {
nkeynes@953
   224
                // Can be null if there is no recovery necessary
nkeynes@953
   225
                sh4_translate_run_exception_recovery(recover);
nkeynes@953
   226
            }
nkeynes@953
   227
        }
nkeynes@953
   228
    }
nkeynes@953
   229
    
nkeynes@953
   230
}
nkeynes@953
   231
nkeynes@905
   232
void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   233
{
nkeynes@591
   234
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@736
   235
        return;
nkeynes@591
   236
    }
nkeynes@740
   237
    sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@591
   238
}
nkeynes@591
   239
nkeynes@905
   240
void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   241
{
nkeynes@586
   242
    void *result = NULL;
nkeynes@586
   243
nkeynes@588
   244
    if( IS_IN_ICACHE(vma) ) {
nkeynes@736
   245
        return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   246
    }
nkeynes@586
   247
nkeynes@588
   248
    if( vma > 0xFFFFFF00 ) {
nkeynes@736
   249
        // lxdream hook
nkeynes@736
   250
        return NULL;
nkeynes@588
   251
    }
nkeynes@588
   252
nkeynes@588
   253
    if( !mmu_update_icache(vma) ) {
nkeynes@736
   254
        // fault - off to the fault handler
nkeynes@736
   255
        if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@736
   256
            // double fault - halt
nkeynes@736
   257
            ERROR( "Double fault - halting" );
nkeynes@740
   258
            sh4_core_exit(CORE_EXIT_HALT);
nkeynes@736
   259
            return NULL;
nkeynes@736
   260
        }
nkeynes@588
   261
    }
nkeynes@588
   262
nkeynes@588
   263
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   264
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   265
    return result;
nkeynes@586
   266
}
nkeynes@586
   267
.