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