Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 359:c588dce7ebde
next368:36fac4c42322
author nkeynes
date Thu Aug 23 12:33:27 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Commit decoder generator
Translator work in progress
Fix mac.l, mac.w in emu core
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@359
     2
 * $Id: sh4trans.c,v 1.1 2007-08-23 12:33:27 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@359
    19
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@359
    30
    int i, result = 1;
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@359
    40
    for( ; sh4r.slice_cycle < nanosecs && result != 0; sh4r.slice_cycle ) {
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@359
    51
	int (*code)() = xlat_get_code(sh4r.pc);
nkeynes@359
    52
	if( code == NULL ) {
nkeynes@359
    53
	    code = sh4_translate_basic_block( sh4r.pc );
nkeynes@359
    54
	}
nkeynes@359
    55
	result = code();
nkeynes@359
    56
	sh4r.slice_cycle += result;
nkeynes@359
    57
    }
nkeynes@359
    58
nkeynes@359
    59
    /* If we aborted early, but the cpu is still technically running,
nkeynes@359
    60
     * we're doing a hard abort - cut the timeslice back to what we
nkeynes@359
    61
     * actually executed
nkeynes@359
    62
     */
nkeynes@359
    63
    if( sh4r.slice_cycle < nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
nkeynes@359
    64
	nanosecs = sh4r.slice_cycle;
nkeynes@359
    65
    }
nkeynes@359
    66
    if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@359
    67
	TMU_run_slice( nanosecs );
nkeynes@359
    68
	SCIF_run_slice( nanosecs );
nkeynes@359
    69
    }
nkeynes@359
    70
    return nanosecs;
nkeynes@359
    71
}
nkeynes@359
    72
nkeynes@359
    73
uint8_t *xlat_output;
nkeynes@359
    74
nkeynes@359
    75
/**
nkeynes@359
    76
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    77
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    78
 * is reached.
nkeynes@359
    79
 * @return the address of the translated block
nkeynes@359
    80
 * eg due to lack of buffer space.
nkeynes@359
    81
 */
nkeynes@359
    82
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    83
{
nkeynes@359
    84
    uint32_t pc = start;
nkeynes@359
    85
    int done;
nkeynes@359
    86
    xlat_cache_block_t block = xlat_start_block( start );
nkeynes@359
    87
    xlat_output = (uint8_t *)block->code;
nkeynes@359
    88
    uint8_t *eob = xlat_output + block->size;
nkeynes@359
    89
    sh4_translate_begin_block();
nkeynes@359
    90
nkeynes@359
    91
    while( (done = sh4_x86_translate_instruction( pc )) == 0 ) {
nkeynes@359
    92
	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@359
    93
	    uint8_t *oldstart = block->code;
nkeynes@359
    94
	    block = xlat_extend_block();
nkeynes@359
    95
	    xlat_output = block->code + (xlat_output - oldstart);
nkeynes@359
    96
	    eob = block->code + block->size;
nkeynes@359
    97
	}
nkeynes@359
    98
	pc += 2;
nkeynes@359
    99
    }
nkeynes@359
   100
    sh4_translate_end_block(done);
nkeynes@359
   101
    xlat_commit_block( xlat_output - block->size, pc-start );
nkeynes@359
   102
    return block->code;
nkeynes@359
   103
}
nkeynes@359
   104
nkeynes@359
   105
.