Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 417:bd927df302a9
prev410:5f8413358e7f
next430:467519b050f4
author nkeynes
date Thu Oct 04 08:47:52 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Add explicit branch cases for main ram - yes it's faster...
view annotate diff log raw
     1 /**
     2  * $Id: sh4trans.c,v 1.7 2007-10-04 08:47:27 nkeynes Exp $
     3  * 
     4  * SH4 translation core module. This part handles the non-target-specific
     5  * section of the translation.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    19 #include <assert.h>
    20 #include "sh4core.h"
    21 #include "sh4trans.h"
    22 #include "xltcache.h"
    24 /**
    25  * Execute a timeslice using translated code only (ie translate/execute loop)
    26  * Note this version does not support breakpoints
    27  */
    28 uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
    29 {
    30     int i;
    31     sh4r.slice_cycle = 0;
    33     if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
    34 	if( sh4r.event_pending < nanosecs ) {
    35 	    sh4r.sh4_state = SH4_STATE_RUNNING;
    36 	    sh4r.slice_cycle = sh4r.event_pending;
    37 	}
    38     }
    40     void * (*code)() = NULL;
    41     while( sh4r.slice_cycle < nanosecs ) {
    42 	if( sh4r.event_pending <= sh4r.slice_cycle ) {
    43 	    if( sh4r.event_types & PENDING_EVENT ) {
    44 		event_execute();
    45 	    }
    46 	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
    47 	    if( sh4r.event_types & PENDING_IRQ ) {
    48 		sh4_accept_interrupt();
    49 		code = NULL;
    50 	    }
    51 	}
    53 	if( code == NULL ) {
    54 	    if( sh4r.pc > 0xFFFFFF00 ) {
    55 		syscall_invoke( sh4r.pc );
    56 		sh4r.in_delay_slot = 0;
    57 		sh4r.pc = sh4r.pr;
    58 	    }
    60 	    code = xlat_get_code(sh4r.pc);
    61 	    if( code == NULL ) {
    62 		code = sh4_translate_basic_block( sh4r.pc );
    63 	    }
    64 	}
    65 	code = code();
    66     }
    68     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
    69 	TMU_run_slice( nanosecs );
    70 	SCIF_run_slice( nanosecs );
    71     }
    72     return nanosecs;
    73 }
    75 uint8_t *xlat_output;
    77 /**
    78  * Translate a linear basic block, ie all instructions from the start address
    79  * (inclusive) until the next branch/jump instruction or the end of the page
    80  * is reached.
    81  * @return the address of the translated block
    82  * eg due to lack of buffer space.
    83  */
    84 void * sh4_translate_basic_block( sh4addr_t start )
    85 {
    86     sh4addr_t pc = start;
    87     sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
    88     int done;
    89     xlat_cache_block_t block = xlat_start_block( start );
    90     xlat_output = (uint8_t *)block->code;
    91     uint8_t *eob = xlat_output + block->size;
    92     sh4_translate_begin_block(pc);
    94     do {
    95 	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
    96 	    uint8_t *oldstart = block->code;
    97 	    block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
    98 	    xlat_output = block->code + (xlat_output - oldstart);
    99 	    eob = block->code + block->size;
   100 	}
   101 	done = sh4_x86_translate_instruction( pc ); 
   102 	assert( xlat_output <= eob );
   103 	pc += 2;
   104 	if ( pc >= lastpc ) {
   105 	    done = 2;
   106 	}
   107     } while( !done );
   108     pc += (done - 2);
   109     if( eob - xlat_output < EPILOGUE_SIZE ) {
   110 	uint8_t *oldstart = block->code;
   111 	block = xlat_extend_block( xlat_output - oldstart + EPILOGUE_SIZE );
   112 	xlat_output = block->code + (xlat_output - oldstart);
   113     }	
   114     sh4_translate_end_block(pc);
   115     xlat_commit_block( xlat_output - block->code, pc-start );
   116     return block->code;
   117 }
   119 /**
   120  * Translate a linear basic block to a temporary buffer, execute it, and return
   121  * the result of the execution. The translation is discarded.
   122  */
   123 void *sh4_translate_and_run( sh4addr_t start )
   124 {
   125     char buf[65536];
   127     uint32_t pc = start;
   128     int done;
   129     xlat_output = buf;
   130     uint8_t *eob = xlat_output + sizeof(buf);
   132     sh4_translate_begin_block(pc);
   134     while( (done = sh4_x86_translate_instruction( pc )) == 0 ) {
   135 	assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
   136 	pc += 2;
   137     }
   138     pc+=2;
   139     sh4_translate_end_block(pc);
   141     void * (*code)() = (void *)buf;
   142     return code();
   143 }
.