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