Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 561:533f6b478071
prev559:06714bc64271
next569:a1c49e1e8776
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
view annotate diff log raw
     1 /**
     2  * $Id$
     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 "eventq.h"
    21 #include "syscall.h"
    22 #include "sh4/sh4core.h"
    23 #include "sh4/sh4trans.h"
    24 #include "sh4/xltcache.h"
    27 uint32_t last_pc;
    28 void *last_code;
    29 /**
    30  * Execute a timeslice using translated code only (ie translate/execute loop)
    31  * Note this version does not support breakpoints
    32  */
    33 uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
    34 {
    35     sh4r.slice_cycle = 0;
    37     if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
    38 	if( sh4r.event_pending < nanosecs ) {
    39 	    sh4r.sh4_state = SH4_STATE_RUNNING;
    40 	    sh4r.slice_cycle = sh4r.event_pending;
    41 	}
    42     }
    44     void * (*code)() = NULL;
    45     while( sh4r.slice_cycle < nanosecs ) {
    46 	if( sh4r.event_pending <= sh4r.slice_cycle ) {
    47 	    if( sh4r.event_types & PENDING_EVENT ) {
    48 		event_execute();
    49 	    }
    50 	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
    51 	    if( sh4r.event_types & PENDING_IRQ ) {
    52 		sh4_accept_interrupt();
    53 		code = NULL;
    54 	    }
    55 	}
    57 	if( code == NULL ) {
    58 	    if( sh4r.pc > 0xFFFFFF00 ) {
    59 		syscall_invoke( sh4r.pc );
    60 		sh4r.in_delay_slot = 0;
    61 		sh4r.pc = sh4r.pr;
    62 	    }
    64 	    code = xlat_get_code(sh4r.pc);
    65 	    if( code == NULL ) {
    66 		uint64_t ppa = mmu_vma_to_phys_exec( sh4r.pc );
    67 		if( ppa>>32 ) {
    68 		    // not found, exception
    69 		    ppa = mmu_vma_to_phys_exec( sh4r.pc );
    70 		    if( ppa>>32 ) {
    71 			// double fault - halt
    72 			dreamcast_stop();
    73 			ERROR( "Double fault - halting" );
    74 			return nanosecs;
    75 		    }
    76 		}
    77 		code = sh4_translate_basic_block( sh4r.pc );
    78 	    }
    79 	}
    80         last_pc = sh4r.pc;
    81         last_code = code;	
    82 	code = code();
    83     }
    85     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
    86 	TMU_run_slice( nanosecs );
    87 	SCIF_run_slice( nanosecs );
    88     }
    89     return nanosecs;
    90 }
    92 uint8_t *xlat_output;
    94 /**
    95  * Translate a linear basic block, ie all instructions from the start address
    96  * (inclusive) until the next branch/jump instruction or the end of the page
    97  * is reached.
    98  * @return the address of the translated block
    99  * eg due to lack of buffer space.
   100  */
   101 void * sh4_translate_basic_block( sh4addr_t start )
   102 {
   103     sh4addr_t pc = start;
   104     sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
   105     int done;
   106     xlat_cache_block_t block = xlat_start_block( start );
   107     xlat_output = (uint8_t *)block->code;
   108     uint8_t *eob = xlat_output + block->size;
   109     sh4_translate_begin_block(pc);
   111     do {
   112 	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
   113 	    uint8_t *oldstart = block->code;
   114 	    block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
   115 	    xlat_output = block->code + (xlat_output - oldstart);
   116 	    eob = block->code + block->size;
   117 	}
   118 	done = sh4_translate_instruction( pc ); 
   119 	assert( xlat_output <= eob );
   120 	pc += 2;
   121 	if ( pc >= lastpc ) {
   122 	    done = 2;
   123 	}
   124     } while( !done );
   125     pc += (done - 2);
   126     if( eob - xlat_output < EPILOGUE_SIZE ) {
   127 	uint8_t *oldstart = block->code;
   128 	block = xlat_extend_block( xlat_output - oldstart + EPILOGUE_SIZE );
   129 	xlat_output = block->code + (xlat_output - oldstart);
   130     }	
   131     sh4_translate_end_block(pc);
   132     xlat_commit_block( xlat_output - block->code, pc-start );
   133     return block->code;
   134 }
   136 /**
   137  * Translate a linear basic block to a temporary buffer, execute it, and return
   138  * the result of the execution. The translation is discarded.
   139  */
   140 void *sh4_translate_and_run( sh4addr_t start )
   141 {
   142     unsigned char buf[65536];
   144     sh4addr_t pc = start;
   145     int done;
   146     xlat_output = buf;
   147     uint8_t *eob = xlat_output + sizeof(buf);
   149     sh4_translate_begin_block(pc);
   151     while( (done = sh4_translate_instruction( pc )) == 0 ) {
   152 	assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
   153 	pc += 2;
   154     }
   155     pc+=2;
   156     sh4_translate_end_block(pc);
   158     void * (*code)() = (void *)buf;
   159     return code();
   160 }
.