Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 410:5f8413358e7f
prev408:af496b734734
next417:bd927df302a9
author nkeynes
date Sat Sep 29 11:06:40 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Change extend-block to take a requested size
Terminate blocks on page boundaries for easier invalidation
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@410
     2
 * $Id: sh4trans.c,v 1.6 2007-09-29 11:06:40 nkeynes Exp $
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@359
    20
#include "sh4core.h"
nkeynes@359
    21
#include "sh4trans.h"
nkeynes@359
    22
#include "xltcache.h"
nkeynes@359
    23
nkeynes@359
    24
/**
nkeynes@359
    25
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    26
 * Note this version does not support breakpoints
nkeynes@359
    27
 */
nkeynes@359
    28
uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
nkeynes@359
    29
{
nkeynes@368
    30
    int i;
nkeynes@359
    31
    sh4r.slice_cycle = 0;
nkeynes@359
    32
nkeynes@359
    33
    if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
nkeynes@359
    34
	if( sh4r.event_pending < nanosecs ) {
nkeynes@359
    35
	    sh4r.sh4_state = SH4_STATE_RUNNING;
nkeynes@359
    36
	    sh4r.slice_cycle = sh4r.event_pending;
nkeynes@359
    37
	}
nkeynes@359
    38
    }
nkeynes@359
    39
nkeynes@408
    40
    void * (*code)() = NULL;
nkeynes@368
    41
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@408
    42
	if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@359
    43
	    if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@359
    44
		event_execute();
nkeynes@359
    45
	    }
nkeynes@359
    46
	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@359
    47
	    if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@359
    48
		sh4_accept_interrupt();
nkeynes@408
    49
		code = NULL;
nkeynes@359
    50
	    }
nkeynes@359
    51
	}
nkeynes@408
    52
	
nkeynes@408
    53
	if( code ) { // fast path
nkeynes@408
    54
	    code = code();
nkeynes@408
    55
	} else {
nkeynes@408
    56
	    if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@408
    57
		syscall_invoke( sh4r.pc );
nkeynes@408
    58
		sh4r.in_delay_slot = 0;
nkeynes@408
    59
		sh4r.pc = sh4r.pr;
nkeynes@408
    60
	    }
nkeynes@359
    61
nkeynes@408
    62
	    code = xlat_get_code(sh4r.pc);
nkeynes@408
    63
	    if( code == NULL ) {
nkeynes@408
    64
		code = sh4_translate_basic_block( sh4r.pc );
nkeynes@408
    65
	    }
nkeynes@408
    66
	    code = code();
nkeynes@390
    67
	}
nkeynes@359
    68
    }
nkeynes@359
    69
nkeynes@359
    70
    if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@359
    71
	TMU_run_slice( nanosecs );
nkeynes@359
    72
	SCIF_run_slice( nanosecs );
nkeynes@359
    73
    }
nkeynes@359
    74
    return nanosecs;
nkeynes@359
    75
}
nkeynes@359
    76
nkeynes@359
    77
uint8_t *xlat_output;
nkeynes@359
    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@359
    83
 * @return the address of the translated block
nkeynes@359
    84
 * eg due to lack of buffer space.
nkeynes@359
    85
 */
nkeynes@359
    86
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    87
{
nkeynes@408
    88
    sh4addr_t pc = start;
nkeynes@410
    89
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@359
    90
    int done;
nkeynes@359
    91
    xlat_cache_block_t block = xlat_start_block( start );
nkeynes@359
    92
    xlat_output = (uint8_t *)block->code;
nkeynes@359
    93
    uint8_t *eob = xlat_output + block->size;
nkeynes@408
    94
    sh4_translate_begin_block(pc);
nkeynes@359
    95
nkeynes@408
    96
    do {
nkeynes@359
    97
	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@359
    98
	    uint8_t *oldstart = block->code;
nkeynes@410
    99
	    block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@359
   100
	    xlat_output = block->code + (xlat_output - oldstart);
nkeynes@359
   101
	    eob = block->code + block->size;
nkeynes@359
   102
	}
nkeynes@408
   103
	done = sh4_x86_translate_instruction( pc ); 
nkeynes@410
   104
	assert( xlat_output <= eob );
nkeynes@359
   105
	pc += 2;
nkeynes@410
   106
	if ( pc >= lastpc ) {
nkeynes@410
   107
	    done = 2;
nkeynes@410
   108
	}
nkeynes@408
   109
    } while( !done );
nkeynes@408
   110
    pc += (done - 2);
nkeynes@410
   111
    if( eob - xlat_output < EPILOGUE_SIZE ) {
nkeynes@410
   112
	uint8_t *oldstart = block->code;
nkeynes@410
   113
	block = xlat_extend_block( xlat_output - oldstart + EPILOGUE_SIZE );
nkeynes@410
   114
	xlat_output = block->code + (xlat_output - oldstart);
nkeynes@410
   115
    }	
nkeynes@368
   116
    sh4_translate_end_block(pc);
nkeynes@368
   117
    xlat_commit_block( xlat_output - block->code, pc-start );
nkeynes@359
   118
    return block->code;
nkeynes@359
   119
}
nkeynes@359
   120
nkeynes@398
   121
/**
nkeynes@398
   122
 * Translate a linear basic block to a temporary buffer, execute it, and return
nkeynes@398
   123
 * the result of the execution. The translation is discarded.
nkeynes@398
   124
 */
nkeynes@408
   125
void *sh4_translate_and_run( sh4addr_t start )
nkeynes@398
   126
{
nkeynes@398
   127
    char buf[65536];
nkeynes@359
   128
nkeynes@398
   129
    uint32_t pc = start;
nkeynes@398
   130
    int done;
nkeynes@398
   131
    xlat_output = buf;
nkeynes@398
   132
    uint8_t *eob = xlat_output + sizeof(buf);
nkeynes@398
   133
nkeynes@408
   134
    sh4_translate_begin_block(pc);
nkeynes@398
   135
nkeynes@398
   136
    while( (done = sh4_x86_translate_instruction( pc )) == 0 ) {
nkeynes@398
   137
	assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
nkeynes@398
   138
	pc += 2;
nkeynes@398
   139
    }
nkeynes@398
   140
    pc+=2;
nkeynes@398
   141
    sh4_translate_end_block(pc);
nkeynes@398
   142
nkeynes@408
   143
    void * (*code)() = (void *)buf;
nkeynes@398
   144
    return code();
nkeynes@398
   145
}
.