Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 398:16b0856ea511
prev390:d066209999f1
next408:af496b734734
author nkeynes
date Thu Sep 20 08:37:19 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Move support routines to sh4.c
view annotate diff log raw
     1 /**
     2  * $Id: sh4trans.c,v 1.4 2007-09-19 12:09:33 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     while( sh4r.slice_cycle < nanosecs ) {
    41 	if( SH4_EVENT_PENDING() ) {
    42 	    if( sh4r.event_types & PENDING_EVENT ) {
    43 		event_execute();
    44 	    }
    45 	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
    46 	    if( sh4r.event_types & PENDING_IRQ ) {
    47 		sh4_accept_interrupt();
    48 	    }
    49 	}
    51 	if( sh4r.pc > 0xFFFFFF00 ) {
    52 	    syscall_invoke( sh4r.pc );
    53 	    sh4r.in_delay_slot = 0;
    54 	    sh4r.pc = sh4r.pr;
    55 	}
    57 	gboolean (*code)() = xlat_get_code(sh4r.pc);
    58 	if( code == NULL ) {
    59 	    code = sh4_translate_basic_block( sh4r.pc );
    60 	}
    61 	if( !code() )
    62 	    break;
    63     }
    65     /* If we aborted early, but the cpu is still technically running,
    66      * we're doing a hard abort - cut the timeslice back to what we
    67      * actually executed
    68      */
    69     if( sh4r.slice_cycle < nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
    70 	nanosecs = sh4r.slice_cycle;
    71     }
    72     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
    73 	TMU_run_slice( nanosecs );
    74 	SCIF_run_slice( nanosecs );
    75     }
    76     return nanosecs;
    77 }
    79 uint8_t *xlat_output;
    81 /**
    82  * Translate a linear basic block, ie all instructions from the start address
    83  * (inclusive) until the next branch/jump instruction or the end of the page
    84  * is reached.
    85  * @return the address of the translated block
    86  * eg due to lack of buffer space.
    87  */
    88 void * sh4_translate_basic_block( sh4addr_t start )
    89 {
    90     uint32_t pc = start;
    91     int done;
    92     xlat_cache_block_t block = xlat_start_block( start );
    93     xlat_output = (uint8_t *)block->code;
    94     uint8_t *eob = xlat_output + block->size;
    95     sh4_translate_begin_block();
    97     while( (done = sh4_x86_translate_instruction( pc )) == 0 ) {
    98 	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
    99 	    uint8_t *oldstart = block->code;
   100 	    block = xlat_extend_block();
   101 	    xlat_output = block->code + (xlat_output - oldstart);
   102 	    eob = block->code + block->size;
   103 	}
   104 	pc += 2;
   105     }
   106     pc+=2;
   107     sh4_translate_end_block(pc);
   108     xlat_commit_block( xlat_output - block->code, pc-start );
   109     return block->code;
   110 }
   112 /**
   113  * Translate a linear basic block to a temporary buffer, execute it, and return
   114  * the result of the execution. The translation is discarded.
   115  */
   116 gboolean sh4_translate_and_run( sh4addr_t start )
   117 {
   118     char buf[65536];
   120     uint32_t pc = start;
   121     int done;
   122     xlat_output = buf;
   123     uint8_t *eob = xlat_output + sizeof(buf);
   125     sh4_translate_begin_block();
   127     while( (done = sh4_x86_translate_instruction( pc )) == 0 ) {
   128 	assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
   129 	pc += 2;
   130     }
   131     pc+=2;
   132     sh4_translate_end_block(pc);
   134     gboolean (*code)() = (void *)buf;
   135     return code();
   136 }
.