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