Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 1067:d3c00ffccfcd
prev1065:bc1cc0c54917
prev949:d7833018931f
next1091:186558374345
author nkeynes
date Sun Jul 05 13:54:48 2009 +1000 (10 years ago)
permissions -rw-r--r--
last change No-op merge lxdream-mem to tip to remove head (Long since merged in
actuality)
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@975
    26
#include "sh4/sh4mmio.h"
nkeynes@975
    27
#include "sh4/mmu.h"
nkeynes@991
    28
#include "xlat/xltcache.h"
nkeynes@586
    29
nkeynes@359
    30
/**
nkeynes@359
    31
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    32
 */
nkeynes@740
    33
uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
nkeynes@359
    34
{
nkeynes@408
    35
    void * (*code)() = NULL;
nkeynes@368
    36
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@736
    37
        if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@736
    38
            if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@736
    39
                event_execute();
nkeynes@736
    40
            }
nkeynes@736
    41
            /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@736
    42
            if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@736
    43
                sh4_accept_interrupt();
nkeynes@736
    44
                code = NULL;
nkeynes@736
    45
            }
nkeynes@736
    46
        }
nkeynes@359
    47
nkeynes@736
    48
        if( code == NULL ) {
nkeynes@1014
    49
            if( IS_SYSCALL(sh4r.pc) ) {
nkeynes@736
    50
                syscall_invoke( sh4r.pc );
nkeynes@736
    51
                sh4r.in_delay_slot = 0;
nkeynes@736
    52
                sh4r.pc = sh4r.pr;
nkeynes@736
    53
            }
nkeynes@736
    54
nkeynes@736
    55
            code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@936
    56
            if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@736
    57
                code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    58
            }
nkeynes@936
    59
        } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@954
    60
            if( !IS_IN_ICACHE(sh4r.pc) ) {
nkeynes@954
    61
                /* If TLB is off, we may have gotten here without updating
nkeynes@954
    62
                 * the icache, so do it now. This should never fail, so...
nkeynes@954
    63
                 */
nkeynes@954
    64
                mmu_update_icache(sh4r.pc);
nkeynes@954
    65
                assert( IS_IN_ICACHE(sh4r.pc) ); 
nkeynes@954
    66
            }
nkeynes@936
    67
            code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    68
        }
nkeynes@736
    69
        code = code();
nkeynes@359
    70
    }
nkeynes@359
    71
    return nanosecs;
nkeynes@359
    72
}
nkeynes@359
    73
nkeynes@359
    74
uint8_t *xlat_output;
nkeynes@596
    75
xlat_cache_block_t xlat_current_block;
nkeynes@586
    76
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
    77
uint32_t xlat_recovery_posn;
nkeynes@359
    78
nkeynes@596
    79
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
    80
{
nkeynes@596
    81
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@736
    82
        ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
    83
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
    84
    xlat_recovery_posn++;
nkeynes@596
    85
}
nkeynes@596
    86
nkeynes@359
    87
/**
nkeynes@359
    88
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    89
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    90
 * is reached.
nkeynes@914
    91
 * @param start VMA of the block start (which must already be in the icache)
nkeynes@359
    92
 * @return the address of the translated block
nkeynes@359
    93
 * eg due to lack of buffer space.
nkeynes@359
    94
 */
nkeynes@359
    95
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    96
{
nkeynes@408
    97
    sh4addr_t pc = start;
nkeynes@410
    98
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
    99
    int done, i;
nkeynes@914
   100
    xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
nkeynes@596
   101
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
   102
    xlat_recovery_posn = 0;
nkeynes@596
   103
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
   104
nkeynes@588
   105
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
   106
        lastpc = GET_ICACHE_END();
nkeynes@588
   107
    }
nkeynes@588
   108
nkeynes@408
   109
    sh4_translate_begin_block(pc);
nkeynes@359
   110
nkeynes@408
   111
    do {
nkeynes@711
   112
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   113
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   114
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   115
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   116
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   117
        }
nkeynes@711
   118
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   119
        assert( xlat_output <= eob );
nkeynes@711
   120
        pc += 2;
nkeynes@711
   121
        if ( pc >= lastpc ) {
nkeynes@711
   122
            done = 2;
nkeynes@711
   123
        }
nkeynes@408
   124
    } while( !done );
nkeynes@408
   125
    pc += (done - 2);
nkeynes@617
   126
nkeynes@617
   127
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   128
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   129
nkeynes@593
   130
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   131
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   132
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   133
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   134
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   135
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   136
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   137
    }	
nkeynes@368
   138
    sh4_translate_end_block(pc);
nkeynes@711
   139
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@736
   140
nkeynes@586
   141
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   142
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   143
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   144
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@936
   145
    xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
nkeynes@586
   146
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   147
    return xlat_current_block->code;
nkeynes@359
   148
}
nkeynes@359
   149
nkeynes@398
   150
/**
nkeynes@586
   151
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   152
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   153
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   154
 * call into generated code).
nkeynes@398
   155
 */
nkeynes@586
   156
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   157
{
nkeynes@586
   158
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   159
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   160
}
nkeynes@359
   161
nkeynes@941
   162
/**
nkeynes@941
   163
 * Same as sh4_translate_run_recovery, but is used to recover from a taken
nkeynes@941
   164
 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
nkeynes@941
   165
 */
nkeynes@941
   166
void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
nkeynes@941
   167
{
nkeynes@941
   168
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@941
   169
    sh4r.spc += (recovery->sh4_icount<<1);
nkeynes@941
   170
}    
nkeynes@941
   171
nkeynes@740
   172
void sh4_translate_exit_recover( )
nkeynes@586
   173
{
nkeynes@906
   174
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   175
    if( code != NULL ) {
nkeynes@906
   176
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   177
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   178
        if( pc != NULL ) {
nkeynes@906
   179
            // could be null if we're not actually running inside the translator
nkeynes@949
   180
            xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
nkeynes@906
   181
            if( recover != NULL ) {
nkeynes@906
   182
                // Can be null if there is no recovery necessary
nkeynes@906
   183
                sh4_translate_run_recovery(recover);
nkeynes@906
   184
            }
nkeynes@736
   185
        }
nkeynes@398
   186
    }
nkeynes@586
   187
}
nkeynes@398
   188
nkeynes@941
   189
void sh4_translate_exception_exit_recover( )
nkeynes@941
   190
{
nkeynes@941
   191
    void *code = xlat_get_code_by_vma( sh4r.spc );
nkeynes@941
   192
    if( code != NULL ) {
nkeynes@941
   193
        uint32_t size = xlat_get_code_size( code );
nkeynes@941
   194
        void *pc = xlat_get_native_pc( code, size );
nkeynes@941
   195
        if( pc != NULL ) {
nkeynes@941
   196
            // could be null if we're not actually running inside the translator
nkeynes@941
   197
            xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
nkeynes@941
   198
            if( recover != NULL ) {
nkeynes@941
   199
                // Can be null if there is no recovery necessary
nkeynes@941
   200
                sh4_translate_run_exception_recovery(recover);
nkeynes@941
   201
            }
nkeynes@941
   202
        }
nkeynes@941
   203
    }
nkeynes@941
   204
    
nkeynes@941
   205
}
nkeynes@941
   206
nkeynes@905
   207
void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   208
{
nkeynes@591
   209
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@736
   210
        return;
nkeynes@591
   211
    }
nkeynes@740
   212
    sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@591
   213
}
nkeynes@591
   214
nkeynes@905
   215
void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   216
{
nkeynes@586
   217
    void *result = NULL;
nkeynes@586
   218
nkeynes@588
   219
    if( IS_IN_ICACHE(vma) ) {
nkeynes@736
   220
        return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   221
    }
nkeynes@586
   222
nkeynes@1014
   223
    if( IS_SYSCALL(vma) ) {
nkeynes@736
   224
        // lxdream hook
nkeynes@736
   225
        return NULL;
nkeynes@588
   226
    }
nkeynes@588
   227
nkeynes@588
   228
    if( !mmu_update_icache(vma) ) {
nkeynes@736
   229
        // fault - off to the fault handler
nkeynes@736
   230
        if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@736
   231
            // double fault - halt
nkeynes@736
   232
            ERROR( "Double fault - halting" );
nkeynes@740
   233
            sh4_core_exit(CORE_EXIT_HALT);
nkeynes@736
   234
            return NULL;
nkeynes@736
   235
        }
nkeynes@588
   236
    }
nkeynes@588
   237
nkeynes@588
   238
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   239
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   240
    return result;
nkeynes@586
   241
}
nkeynes@586
   242
.