Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 941:c67574ed4355
prev936:f394309c399a
next949:d7833018931f
author nkeynes
date Sat Jan 03 08:55:15 2009 +0000 (12 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Implement CORE_EXIT_EXCEPTION for use when direct frame messing about doesn't work
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@359
    27
nkeynes@586
    28
nkeynes@359
    29
/**
nkeynes@359
    30
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    31
 */
nkeynes@740
    32
uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
nkeynes@359
    33
{
nkeynes@408
    34
    void * (*code)() = NULL;
nkeynes@368
    35
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@736
    36
        if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@736
    37
            if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@736
    38
                event_execute();
nkeynes@736
    39
            }
nkeynes@736
    40
            /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@736
    41
            if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@736
    42
                sh4_accept_interrupt();
nkeynes@736
    43
                code = NULL;
nkeynes@736
    44
            }
nkeynes@736
    45
        }
nkeynes@359
    46
nkeynes@736
    47
        if( code == NULL ) {
nkeynes@736
    48
            if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@736
    49
                syscall_invoke( sh4r.pc );
nkeynes@736
    50
                sh4r.in_delay_slot = 0;
nkeynes@736
    51
                sh4r.pc = sh4r.pr;
nkeynes@736
    52
            }
nkeynes@736
    53
nkeynes@736
    54
            code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@936
    55
            if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@736
    56
                code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    57
            }
nkeynes@936
    58
        } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
nkeynes@936
    59
            code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    60
        }
nkeynes@736
    61
        code = code();
nkeynes@359
    62
    }
nkeynes@359
    63
    return nanosecs;
nkeynes@359
    64
}
nkeynes@359
    65
nkeynes@359
    66
uint8_t *xlat_output;
nkeynes@596
    67
xlat_cache_block_t xlat_current_block;
nkeynes@586
    68
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
    69
uint32_t xlat_recovery_posn;
nkeynes@359
    70
nkeynes@596
    71
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
    72
{
nkeynes@596
    73
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@736
    74
        ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
    75
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
    76
    xlat_recovery_posn++;
nkeynes@596
    77
}
nkeynes@596
    78
nkeynes@359
    79
/**
nkeynes@359
    80
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    81
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    82
 * is reached.
nkeynes@914
    83
 * @param start VMA of the block start (which must already be in the icache)
nkeynes@359
    84
 * @return the address of the translated block
nkeynes@359
    85
 * eg due to lack of buffer space.
nkeynes@359
    86
 */
nkeynes@359
    87
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    88
{
nkeynes@408
    89
    sh4addr_t pc = start;
nkeynes@410
    90
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
    91
    int done, i;
nkeynes@914
    92
    xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
nkeynes@596
    93
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
    94
    xlat_recovery_posn = 0;
nkeynes@596
    95
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
    96
nkeynes@588
    97
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
    98
        lastpc = GET_ICACHE_END();
nkeynes@588
    99
    }
nkeynes@588
   100
nkeynes@408
   101
    sh4_translate_begin_block(pc);
nkeynes@359
   102
nkeynes@408
   103
    do {
nkeynes@711
   104
        /* check for breakpoints at this pc */
nkeynes@711
   105
        for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@711
   106
            if( sh4_breakpoints[i].address == pc ) {
nkeynes@711
   107
                sh4_translate_emit_breakpoint(pc);
nkeynes@711
   108
                break;
nkeynes@711
   109
            }
nkeynes@711
   110
        }
nkeynes@711
   111
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   112
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   113
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   114
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   115
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   116
        }
nkeynes@711
   117
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   118
        assert( xlat_output <= eob );
nkeynes@711
   119
        pc += 2;
nkeynes@711
   120
        if ( pc >= lastpc ) {
nkeynes@711
   121
            done = 2;
nkeynes@711
   122
        }
nkeynes@408
   123
    } while( !done );
nkeynes@408
   124
    pc += (done - 2);
nkeynes@617
   125
nkeynes@617
   126
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   127
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   128
nkeynes@593
   129
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   130
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   131
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   132
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   133
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   134
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   135
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   136
    }	
nkeynes@368
   137
    sh4_translate_end_block(pc);
nkeynes@711
   138
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@736
   139
nkeynes@586
   140
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   141
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   142
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   143
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@936
   144
    xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
nkeynes@586
   145
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   146
    return xlat_current_block->code;
nkeynes@359
   147
}
nkeynes@359
   148
nkeynes@398
   149
/**
nkeynes@586
   150
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   151
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   152
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   153
 * call into generated code).
nkeynes@398
   154
 */
nkeynes@586
   155
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   156
{
nkeynes@586
   157
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   158
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   159
}
nkeynes@359
   160
nkeynes@941
   161
/**
nkeynes@941
   162
 * Same as sh4_translate_run_recovery, but is used to recover from a taken
nkeynes@941
   163
 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
nkeynes@941
   164
 */
nkeynes@941
   165
void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
nkeynes@941
   166
{
nkeynes@941
   167
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@941
   168
    sh4r.spc += (recovery->sh4_icount<<1);
nkeynes@941
   169
}    
nkeynes@941
   170
nkeynes@740
   171
void sh4_translate_exit_recover( )
nkeynes@586
   172
{
nkeynes@906
   173
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   174
    if( code != NULL ) {
nkeynes@906
   175
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   176
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   177
        if( pc != NULL ) {
nkeynes@906
   178
            // could be null if we're not actually running inside the translator
nkeynes@906
   179
            xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, TRUE);
nkeynes@906
   180
            if( recover != NULL ) {
nkeynes@906
   181
                // Can be null if there is no recovery necessary
nkeynes@906
   182
                sh4_translate_run_recovery(recover);
nkeynes@906
   183
            }
nkeynes@736
   184
        }
nkeynes@398
   185
    }
nkeynes@586
   186
}
nkeynes@398
   187
nkeynes@941
   188
void sh4_translate_exception_exit_recover( )
nkeynes@941
   189
{
nkeynes@941
   190
    void *code = xlat_get_code_by_vma( sh4r.spc );
nkeynes@941
   191
    if( code != NULL ) {
nkeynes@941
   192
        uint32_t size = xlat_get_code_size( code );
nkeynes@941
   193
        void *pc = xlat_get_native_pc( code, size );
nkeynes@941
   194
        if( pc != NULL ) {
nkeynes@941
   195
            // could be null if we're not actually running inside the translator
nkeynes@941
   196
            xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
nkeynes@941
   197
            if( recover != NULL ) {
nkeynes@941
   198
                // Can be null if there is no recovery necessary
nkeynes@941
   199
                sh4_translate_run_exception_recovery(recover);
nkeynes@941
   200
            }
nkeynes@941
   201
        }
nkeynes@941
   202
    }
nkeynes@941
   203
    
nkeynes@941
   204
}
nkeynes@941
   205
nkeynes@905
   206
void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   207
{
nkeynes@591
   208
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@736
   209
        return;
nkeynes@591
   210
    }
nkeynes@740
   211
    sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@591
   212
}
nkeynes@591
   213
nkeynes@586
   214
/**
nkeynes@586
   215
 * Exit the current block at the end of the current instruction, flush the
nkeynes@586
   216
 * translation cache (completely) and return control to sh4_xlat_run_slice.
nkeynes@586
   217
 *
nkeynes@586
   218
 * As a special case, if the current instruction is actually the last 
nkeynes@586
   219
 * instruction in the block (ie it's in a delay slot), this function 
nkeynes@586
   220
 * returns to allow normal completion of the translation block. Otherwise
nkeynes@586
   221
 * this function never returns.
nkeynes@586
   222
 *
nkeynes@586
   223
 * Must only be invoked (indirectly) from within translated code.
nkeynes@586
   224
 */
nkeynes@740
   225
gboolean sh4_translate_flush_cache()
nkeynes@586
   226
{
nkeynes@906
   227
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   228
    if( code != NULL ) {
nkeynes@906
   229
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   230
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   231
        assert( pc != NULL );
nkeynes@586
   232
nkeynes@906
   233
        xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, FALSE);
nkeynes@906
   234
        if( recover != NULL ) {
nkeynes@906
   235
            // Can be null if there is no recovery necessary
nkeynes@906
   236
            sh4_translate_run_recovery(recover);
nkeynes@906
   237
            xlat_flush_cache();
nkeynes@906
   238
            return TRUE;
nkeynes@906
   239
        } else {
nkeynes@906
   240
            xlat_flush_cache();
nkeynes@906
   241
            return FALSE;
nkeynes@906
   242
        }
nkeynes@586
   243
    }
nkeynes@398
   244
}
nkeynes@586
   245
nkeynes@905
   246
void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   247
{
nkeynes@586
   248
    void *result = NULL;
nkeynes@586
   249
nkeynes@588
   250
    if( IS_IN_ICACHE(vma) ) {
nkeynes@736
   251
        return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   252
    }
nkeynes@586
   253
nkeynes@588
   254
    if( vma > 0xFFFFFF00 ) {
nkeynes@736
   255
        // lxdream hook
nkeynes@736
   256
        return NULL;
nkeynes@588
   257
    }
nkeynes@588
   258
nkeynes@588
   259
    if( !mmu_update_icache(vma) ) {
nkeynes@736
   260
        // fault - off to the fault handler
nkeynes@736
   261
        if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@736
   262
            // double fault - halt
nkeynes@736
   263
            ERROR( "Double fault - halting" );
nkeynes@740
   264
            sh4_core_exit(CORE_EXIT_HALT);
nkeynes@736
   265
            return NULL;
nkeynes@736
   266
        }
nkeynes@588
   267
    }
nkeynes@588
   268
nkeynes@588
   269
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   270
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   271
    return result;
nkeynes@586
   272
}
nkeynes@586
   273
.