Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 740:dd11269ee48b
prev736:a02d1475ccfd
next809:8bdbf4d95da4
author nkeynes
date Wed Jul 16 10:40:10 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Rationalize the two SH4 run slice impls into sh4.c, and tidy up the vm exits.
Fixes broken soft-reset with emulator core
Fixes broken build without translator
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@736
    55
            if( code == NULL ) {
nkeynes@736
    56
                code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    57
            }
nkeynes@736
    58
        }
nkeynes@736
    59
        code = code();
nkeynes@359
    60
    }
nkeynes@359
    61
    return nanosecs;
nkeynes@359
    62
}
nkeynes@359
    63
nkeynes@359
    64
uint8_t *xlat_output;
nkeynes@596
    65
xlat_cache_block_t xlat_current_block;
nkeynes@586
    66
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
    67
uint32_t xlat_recovery_posn;
nkeynes@359
    68
nkeynes@596
    69
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
    70
{
nkeynes@596
    71
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@736
    72
        ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
    73
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
    74
    xlat_recovery_posn++;
nkeynes@596
    75
}
nkeynes@596
    76
nkeynes@359
    77
/**
nkeynes@359
    78
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    79
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    80
 * is reached.
nkeynes@359
    81
 * @return the address of the translated block
nkeynes@359
    82
 * eg due to lack of buffer space.
nkeynes@359
    83
 */
nkeynes@359
    84
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    85
{
nkeynes@408
    86
    sh4addr_t pc = start;
nkeynes@410
    87
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
    88
    int done, i;
nkeynes@596
    89
    xlat_current_block = xlat_start_block( start );
nkeynes@596
    90
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
    91
    xlat_recovery_posn = 0;
nkeynes@596
    92
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
    93
nkeynes@588
    94
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
    95
        lastpc = GET_ICACHE_END();
nkeynes@588
    96
    }
nkeynes@588
    97
nkeynes@408
    98
    sh4_translate_begin_block(pc);
nkeynes@359
    99
nkeynes@408
   100
    do {
nkeynes@711
   101
        /* check for breakpoints at this pc */
nkeynes@711
   102
        for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@711
   103
            if( sh4_breakpoints[i].address == pc ) {
nkeynes@711
   104
                sh4_translate_emit_breakpoint(pc);
nkeynes@711
   105
                break;
nkeynes@711
   106
            }
nkeynes@711
   107
        }
nkeynes@711
   108
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   109
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   110
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   111
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   112
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   113
        }
nkeynes@711
   114
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   115
        assert( xlat_output <= eob );
nkeynes@711
   116
        pc += 2;
nkeynes@711
   117
        if ( pc >= lastpc ) {
nkeynes@711
   118
            done = 2;
nkeynes@711
   119
        }
nkeynes@408
   120
    } while( !done );
nkeynes@408
   121
    pc += (done - 2);
nkeynes@617
   122
nkeynes@617
   123
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   124
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   125
nkeynes@593
   126
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   127
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   128
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   129
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   130
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   131
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   132
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   133
    }	
nkeynes@368
   134
    sh4_translate_end_block(pc);
nkeynes@711
   135
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@736
   136
nkeynes@586
   137
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   138
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   139
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   140
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@586
   141
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   142
    return xlat_current_block->code;
nkeynes@359
   143
}
nkeynes@359
   144
nkeynes@398
   145
/**
nkeynes@586
   146
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   147
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   148
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   149
 * call into generated code).
nkeynes@398
   150
 */
nkeynes@586
   151
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   152
{
nkeynes@586
   153
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   154
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   155
}
nkeynes@359
   156
nkeynes@740
   157
void sh4_translate_exit_recover( )
nkeynes@586
   158
{
nkeynes@586
   159
    void *pc = xlat_get_native_pc();
nkeynes@586
   160
    if( pc != NULL ) {
nkeynes@736
   161
        // could be null if we're not actually running inside the translator
nkeynes@736
   162
        void *code = xlat_get_code( sh4r.pc );
nkeynes@736
   163
        xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
nkeynes@736
   164
        if( recover != NULL ) {
nkeynes@736
   165
            // Can be null if there is no recovery necessary
nkeynes@736
   166
            sh4_translate_run_recovery(recover);
nkeynes@736
   167
        }
nkeynes@398
   168
    }
nkeynes@586
   169
}
nkeynes@398
   170
nkeynes@591
   171
void sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   172
{
nkeynes@591
   173
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@736
   174
        return;
nkeynes@591
   175
    }
nkeynes@740
   176
    sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@591
   177
}
nkeynes@591
   178
nkeynes@586
   179
/**
nkeynes@586
   180
 * Exit the current block at the end of the current instruction, flush the
nkeynes@586
   181
 * translation cache (completely) and return control to sh4_xlat_run_slice.
nkeynes@586
   182
 *
nkeynes@586
   183
 * As a special case, if the current instruction is actually the last 
nkeynes@586
   184
 * instruction in the block (ie it's in a delay slot), this function 
nkeynes@586
   185
 * returns to allow normal completion of the translation block. Otherwise
nkeynes@586
   186
 * this function never returns.
nkeynes@586
   187
 *
nkeynes@586
   188
 * Must only be invoked (indirectly) from within translated code.
nkeynes@586
   189
 */
nkeynes@740
   190
gboolean sh4_translate_flush_cache()
nkeynes@586
   191
{
nkeynes@586
   192
    void *pc = xlat_get_native_pc();
nkeynes@586
   193
    assert( pc != NULL );
nkeynes@586
   194
nkeynes@586
   195
    void *code = xlat_get_code( sh4r.pc );
nkeynes@586
   196
    xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
nkeynes@586
   197
    if( recover != NULL ) {
nkeynes@736
   198
        // Can be null if there is no recovery necessary
nkeynes@736
   199
        sh4_translate_run_recovery(recover);
nkeynes@736
   200
        xlat_flush_cache();
nkeynes@740
   201
        return TRUE;
nkeynes@586
   202
    } else {
nkeynes@736
   203
        xlat_flush_cache();
nkeynes@740
   204
        return FALSE;
nkeynes@586
   205
    }
nkeynes@398
   206
}
nkeynes@586
   207
nkeynes@586
   208
void *xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   209
{
nkeynes@586
   210
    void *result = NULL;
nkeynes@586
   211
nkeynes@588
   212
    if( IS_IN_ICACHE(vma) ) {
nkeynes@736
   213
        return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   214
    }
nkeynes@586
   215
nkeynes@588
   216
    if( vma > 0xFFFFFF00 ) {
nkeynes@736
   217
        // lxdream hook
nkeynes@736
   218
        return NULL;
nkeynes@588
   219
    }
nkeynes@588
   220
nkeynes@588
   221
    if( !mmu_update_icache(vma) ) {
nkeynes@736
   222
        // fault - off to the fault handler
nkeynes@736
   223
        if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@736
   224
            // double fault - halt
nkeynes@736
   225
            ERROR( "Double fault - halting" );
nkeynes@740
   226
            sh4_core_exit(CORE_EXIT_HALT);
nkeynes@736
   227
            return NULL;
nkeynes@736
   228
        }
nkeynes@588
   229
    }
nkeynes@588
   230
nkeynes@588
   231
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   232
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   233
    return result;
nkeynes@586
   234
}
nkeynes@586
   235
.