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