Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 711:4f0ba72e58fe
prev669:ab344e42bca9
next736:a02d1475ccfd
author nkeynes
date Wed Jun 25 22:50:41 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix size test before end-of-block extension, and add assert after sh4_translate_end_block() for consistency
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@586
    20
#include <setjmp.h>
nkeynes@430
    21
#include "eventq.h"
nkeynes@430
    22
#include "syscall.h"
nkeynes@586
    23
#include "clock.h"
nkeynes@669
    24
#include "dreamcast.h"
nkeynes@430
    25
#include "sh4/sh4core.h"
nkeynes@430
    26
#include "sh4/sh4trans.h"
nkeynes@430
    27
#include "sh4/xltcache.h"
nkeynes@359
    28
nkeynes@586
    29
nkeynes@586
    30
static jmp_buf xlat_jmp_buf;
nkeynes@586
    31
static gboolean xlat_running = FALSE;
nkeynes@586
    32
nkeynes@586
    33
gboolean sh4_xlat_is_running()
nkeynes@586
    34
{
nkeynes@586
    35
    return xlat_running;
nkeynes@586
    36
}
nkeynes@586
    37
nkeynes@359
    38
/**
nkeynes@359
    39
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    40
 */
nkeynes@359
    41
uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
nkeynes@359
    42
{
nkeynes@359
    43
    sh4r.slice_cycle = 0;
nkeynes@359
    44
nkeynes@359
    45
    if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
nkeynes@617
    46
	sh4_sleep_run_slice(nanosecs);
nkeynes@359
    47
    }
nkeynes@359
    48
nkeynes@586
    49
    switch( setjmp(xlat_jmp_buf) ) {
nkeynes@586
    50
    case XLAT_EXIT_BREAKPOINT:
nkeynes@586
    51
	sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
nkeynes@586
    52
	/* fallthrough */
nkeynes@586
    53
    case XLAT_EXIT_HALT:
nkeynes@586
    54
	if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@586
    55
	    TMU_run_slice( sh4r.slice_cycle );
nkeynes@586
    56
	    SCIF_run_slice( sh4r.slice_cycle );
nkeynes@586
    57
	    dreamcast_stop();
nkeynes@586
    58
	    return sh4r.slice_cycle;
nkeynes@586
    59
	}
nkeynes@586
    60
    case XLAT_EXIT_SYSRESET:
nkeynes@586
    61
	dreamcast_reset();
nkeynes@586
    62
	break;
nkeynes@617
    63
    case XLAT_EXIT_SLEEP:
nkeynes@617
    64
	sh4_sleep_run_slice(nanosecs);
nkeynes@617
    65
        break;	
nkeynes@586
    66
    }
nkeynes@586
    67
    
nkeynes@586
    68
    xlat_running = TRUE;
nkeynes@408
    69
    void * (*code)() = NULL;
nkeynes@368
    70
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@408
    71
	if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@359
    72
	    if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@359
    73
		event_execute();
nkeynes@359
    74
	    }
nkeynes@359
    75
	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@359
    76
	    if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@359
    77
		sh4_accept_interrupt();
nkeynes@408
    78
		code = NULL;
nkeynes@359
    79
	    }
nkeynes@359
    80
	}
nkeynes@408
    81
	
nkeynes@417
    82
	if( code == NULL ) {
nkeynes@408
    83
	    if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@408
    84
		syscall_invoke( sh4r.pc );
nkeynes@408
    85
		sh4r.in_delay_slot = 0;
nkeynes@408
    86
		sh4r.pc = sh4r.pr;
nkeynes@408
    87
	    }
nkeynes@359
    88
nkeynes@586
    89
	    code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@408
    90
	    if( code == NULL ) {
nkeynes@408
    91
		code = sh4_translate_basic_block( sh4r.pc );
nkeynes@408
    92
	    }
nkeynes@390
    93
	}
nkeynes@417
    94
	code = code();
nkeynes@359
    95
    }
nkeynes@359
    96
nkeynes@586
    97
    xlat_running = FALSE;
nkeynes@591
    98
    sh4_starting = FALSE;
nkeynes@619
    99
    sh4r.slice_cycle = nanosecs;
nkeynes@359
   100
    if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@359
   101
	TMU_run_slice( nanosecs );
nkeynes@359
   102
	SCIF_run_slice( nanosecs );
nkeynes@359
   103
    }
nkeynes@359
   104
    return nanosecs;
nkeynes@359
   105
}
nkeynes@359
   106
nkeynes@359
   107
uint8_t *xlat_output;
nkeynes@596
   108
xlat_cache_block_t xlat_current_block;
nkeynes@586
   109
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
   110
uint32_t xlat_recovery_posn;
nkeynes@359
   111
nkeynes@596
   112
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
   113
{
nkeynes@596
   114
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@596
   115
	((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
   116
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
   117
    xlat_recovery_posn++;
nkeynes@596
   118
}
nkeynes@596
   119
nkeynes@359
   120
/**
nkeynes@359
   121
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
   122
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
   123
 * is reached.
nkeynes@359
   124
 * @return the address of the translated block
nkeynes@359
   125
 * eg due to lack of buffer space.
nkeynes@359
   126
 */
nkeynes@359
   127
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
   128
{
nkeynes@408
   129
    sh4addr_t pc = start;
nkeynes@410
   130
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
   131
    int done, i;
nkeynes@596
   132
    xlat_current_block = xlat_start_block( start );
nkeynes@596
   133
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
   134
    xlat_recovery_posn = 0;
nkeynes@596
   135
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
   136
nkeynes@588
   137
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
   138
        lastpc = GET_ICACHE_END();
nkeynes@588
   139
    }
nkeynes@588
   140
nkeynes@408
   141
    sh4_translate_begin_block(pc);
nkeynes@359
   142
nkeynes@408
   143
    do {
nkeynes@711
   144
        /* check for breakpoints at this pc */
nkeynes@711
   145
        for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@711
   146
            if( sh4_breakpoints[i].address == pc ) {
nkeynes@711
   147
                sh4_translate_emit_breakpoint(pc);
nkeynes@711
   148
                break;
nkeynes@711
   149
            }
nkeynes@711
   150
        }
nkeynes@711
   151
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   152
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   153
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   154
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   155
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   156
        }
nkeynes@711
   157
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   158
        assert( xlat_output <= eob );
nkeynes@711
   159
        pc += 2;
nkeynes@711
   160
        if ( pc >= lastpc ) {
nkeynes@711
   161
            done = 2;
nkeynes@711
   162
        }
nkeynes@408
   163
    } while( !done );
nkeynes@408
   164
    pc += (done - 2);
nkeynes@617
   165
nkeynes@617
   166
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   167
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   168
nkeynes@593
   169
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   170
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   171
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   172
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   173
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   174
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   175
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   176
    }	
nkeynes@368
   177
    sh4_translate_end_block(pc);
nkeynes@711
   178
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@711
   179
    
nkeynes@586
   180
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   181
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   182
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   183
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@586
   184
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   185
    return xlat_current_block->code;
nkeynes@359
   186
}
nkeynes@359
   187
nkeynes@398
   188
/**
nkeynes@586
   189
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   190
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   191
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   192
 * call into generated code).
nkeynes@398
   193
 */
nkeynes@586
   194
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   195
{
nkeynes@586
   196
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   197
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   198
}
nkeynes@359
   199
nkeynes@586
   200
void sh4_translate_unwind_stack( gboolean abort_after, unwind_thunk_t thunk )
nkeynes@586
   201
{
nkeynes@586
   202
    void *pc = xlat_get_native_pc();
nkeynes@398
   203
nkeynes@586
   204
    assert( pc != NULL );
nkeynes@586
   205
    void *code = xlat_get_code( sh4r.pc );
nkeynes@586
   206
    xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
nkeynes@586
   207
    if( recover != NULL ) {
nkeynes@586
   208
	// Can be null if there is no recovery necessary
nkeynes@586
   209
	sh4_translate_run_recovery(recover);
nkeynes@586
   210
    }
nkeynes@586
   211
    if( thunk != NULL ) {
nkeynes@586
   212
	thunk();
nkeynes@586
   213
    }
nkeynes@586
   214
    // finally longjmp back into sh4_xlat_run_slice
nkeynes@586
   215
    xlat_running = FALSE;
nkeynes@586
   216
    longjmp(xlat_jmp_buf, XLAT_EXIT_CONTINUE);
nkeynes@586
   217
} 
nkeynes@398
   218
nkeynes@586
   219
void sh4_translate_exit( int exit_code )
nkeynes@586
   220
{
nkeynes@586
   221
    void *pc = xlat_get_native_pc();
nkeynes@586
   222
    if( pc != NULL ) {
nkeynes@586
   223
	// could be null if we're not actually running inside the translator
nkeynes@586
   224
	void *code = xlat_get_code( sh4r.pc );
nkeynes@586
   225
	xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
nkeynes@586
   226
	if( recover != NULL ) {
nkeynes@586
   227
	    // Can be null if there is no recovery necessary
nkeynes@586
   228
	    sh4_translate_run_recovery(recover);
nkeynes@586
   229
	}
nkeynes@398
   230
    }
nkeynes@586
   231
    // finally longjmp back into sh4_xlat_run_slice
nkeynes@586
   232
    xlat_running = FALSE;
nkeynes@586
   233
    longjmp(xlat_jmp_buf, exit_code);
nkeynes@586
   234
}
nkeynes@398
   235
nkeynes@591
   236
void sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   237
{
nkeynes@591
   238
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@591
   239
	return;
nkeynes@591
   240
    }
nkeynes@591
   241
    sh4_translate_exit( XLAT_EXIT_BREAKPOINT );
nkeynes@591
   242
}
nkeynes@591
   243
nkeynes@586
   244
/**
nkeynes@586
   245
 * Exit the current block at the end of the current instruction, flush the
nkeynes@586
   246
 * translation cache (completely) and return control to sh4_xlat_run_slice.
nkeynes@586
   247
 *
nkeynes@586
   248
 * As a special case, if the current instruction is actually the last 
nkeynes@586
   249
 * instruction in the block (ie it's in a delay slot), this function 
nkeynes@586
   250
 * returns to allow normal completion of the translation block. Otherwise
nkeynes@586
   251
 * this function never returns.
nkeynes@586
   252
 *
nkeynes@586
   253
 * Must only be invoked (indirectly) from within translated code.
nkeynes@586
   254
 */
nkeynes@586
   255
void sh4_translate_flush_cache()
nkeynes@586
   256
{
nkeynes@586
   257
    void *pc = xlat_get_native_pc();
nkeynes@586
   258
    assert( pc != NULL );
nkeynes@586
   259
nkeynes@586
   260
    void *code = xlat_get_code( sh4r.pc );
nkeynes@586
   261
    xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
nkeynes@586
   262
    if( recover != NULL ) {
nkeynes@586
   263
	// Can be null if there is no recovery necessary
nkeynes@586
   264
	sh4_translate_run_recovery(recover);
nkeynes@586
   265
	xlat_flush_cache();
nkeynes@586
   266
	xlat_running = FALSE;
nkeynes@586
   267
	longjmp(xlat_jmp_buf, XLAT_EXIT_CONTINUE);
nkeynes@586
   268
    } else {
nkeynes@586
   269
	xlat_flush_cache();
nkeynes@586
   270
	return;
nkeynes@586
   271
    }
nkeynes@398
   272
}
nkeynes@586
   273
nkeynes@586
   274
void *xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   275
{
nkeynes@586
   276
    void *result = NULL;
nkeynes@586
   277
nkeynes@588
   278
    if( IS_IN_ICACHE(vma) ) {
nkeynes@600
   279
	return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   280
    }
nkeynes@586
   281
nkeynes@588
   282
    if( vma > 0xFFFFFF00 ) {
nkeynes@588
   283
	// lxdream hook
nkeynes@588
   284
	return NULL;
nkeynes@588
   285
    }
nkeynes@588
   286
nkeynes@588
   287
    if( !mmu_update_icache(vma) ) {
nkeynes@588
   288
	// fault - off to the fault handler
nkeynes@588
   289
	if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@588
   290
	    // double fault - halt
nkeynes@588
   291
	    ERROR( "Double fault - halting" );
nkeynes@588
   292
	    dreamcast_stop();
nkeynes@588
   293
	    return NULL;
nkeynes@588
   294
	}
nkeynes@588
   295
    }
nkeynes@588
   296
nkeynes@588
   297
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   298
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   299
    return result;
nkeynes@586
   300
}
nkeynes@586
   301
.