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
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@561
     2
 * $Id$
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@559
    26
nkeynes@559
    27
uint32_t last_pc;
nkeynes@559
    28
void *last_code;
nkeynes@359
    29
/**
nkeynes@359
    30
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    31
 * Note this version does not support breakpoints
nkeynes@359
    32
 */
nkeynes@359
    33
uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
nkeynes@359
    34
{
nkeynes@359
    35
    sh4r.slice_cycle = 0;
nkeynes@359
    36
nkeynes@359
    37
    if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
nkeynes@359
    38
	if( sh4r.event_pending < nanosecs ) {
nkeynes@359
    39
	    sh4r.sh4_state = SH4_STATE_RUNNING;
nkeynes@359
    40
	    sh4r.slice_cycle = sh4r.event_pending;
nkeynes@359
    41
	}
nkeynes@359
    42
    }
nkeynes@359
    43
nkeynes@408
    44
    void * (*code)() = NULL;
nkeynes@368
    45
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@408
    46
	if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@359
    47
	    if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@359
    48
		event_execute();
nkeynes@359
    49
	    }
nkeynes@359
    50
	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@359
    51
	    if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@359
    52
		sh4_accept_interrupt();
nkeynes@408
    53
		code = NULL;
nkeynes@359
    54
	    }
nkeynes@359
    55
	}
nkeynes@408
    56
	
nkeynes@417
    57
	if( code == NULL ) {
nkeynes@408
    58
	    if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@408
    59
		syscall_invoke( sh4r.pc );
nkeynes@408
    60
		sh4r.in_delay_slot = 0;
nkeynes@408
    61
		sh4r.pc = sh4r.pr;
nkeynes@408
    62
	    }
nkeynes@359
    63
nkeynes@408
    64
	    code = xlat_get_code(sh4r.pc);
nkeynes@408
    65
	    if( code == NULL ) {
nkeynes@559
    66
		uint64_t ppa = mmu_vma_to_phys_exec( sh4r.pc );
nkeynes@559
    67
		if( ppa>>32 ) {
nkeynes@559
    68
		    // not found, exception
nkeynes@559
    69
		    ppa = mmu_vma_to_phys_exec( sh4r.pc );
nkeynes@559
    70
		    if( ppa>>32 ) {
nkeynes@559
    71
			// double fault - halt
nkeynes@559
    72
			dreamcast_stop();
nkeynes@559
    73
			ERROR( "Double fault - halting" );
nkeynes@559
    74
			return nanosecs;
nkeynes@559
    75
		    }
nkeynes@559
    76
		}
nkeynes@408
    77
		code = sh4_translate_basic_block( sh4r.pc );
nkeynes@408
    78
	    }
nkeynes@390
    79
	}
nkeynes@559
    80
        last_pc = sh4r.pc;
nkeynes@559
    81
        last_code = code;	
nkeynes@417
    82
	code = code();
nkeynes@359
    83
    }
nkeynes@359
    84
nkeynes@359
    85
    if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@359
    86
	TMU_run_slice( nanosecs );
nkeynes@359
    87
	SCIF_run_slice( nanosecs );
nkeynes@359
    88
    }
nkeynes@359
    89
    return nanosecs;
nkeynes@359
    90
}
nkeynes@359
    91
nkeynes@359
    92
uint8_t *xlat_output;
nkeynes@359
    93
nkeynes@359
    94
/**
nkeynes@359
    95
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    96
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    97
 * is reached.
nkeynes@359
    98
 * @return the address of the translated block
nkeynes@359
    99
 * eg due to lack of buffer space.
nkeynes@359
   100
 */
nkeynes@359
   101
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
   102
{
nkeynes@408
   103
    sh4addr_t pc = start;
nkeynes@410
   104
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@359
   105
    int done;
nkeynes@359
   106
    xlat_cache_block_t block = xlat_start_block( start );
nkeynes@359
   107
    xlat_output = (uint8_t *)block->code;
nkeynes@359
   108
    uint8_t *eob = xlat_output + block->size;
nkeynes@408
   109
    sh4_translate_begin_block(pc);
nkeynes@359
   110
nkeynes@408
   111
    do {
nkeynes@359
   112
	if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@359
   113
	    uint8_t *oldstart = block->code;
nkeynes@410
   114
	    block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@359
   115
	    xlat_output = block->code + (xlat_output - oldstart);
nkeynes@359
   116
	    eob = block->code + block->size;
nkeynes@359
   117
	}
nkeynes@527
   118
	done = sh4_translate_instruction( pc ); 
nkeynes@410
   119
	assert( xlat_output <= eob );
nkeynes@359
   120
	pc += 2;
nkeynes@410
   121
	if ( pc >= lastpc ) {
nkeynes@410
   122
	    done = 2;
nkeynes@410
   123
	}
nkeynes@408
   124
    } while( !done );
nkeynes@408
   125
    pc += (done - 2);
nkeynes@410
   126
    if( eob - xlat_output < EPILOGUE_SIZE ) {
nkeynes@410
   127
	uint8_t *oldstart = block->code;
nkeynes@410
   128
	block = xlat_extend_block( xlat_output - oldstart + EPILOGUE_SIZE );
nkeynes@410
   129
	xlat_output = block->code + (xlat_output - oldstart);
nkeynes@410
   130
    }	
nkeynes@368
   131
    sh4_translate_end_block(pc);
nkeynes@368
   132
    xlat_commit_block( xlat_output - block->code, pc-start );
nkeynes@359
   133
    return block->code;
nkeynes@359
   134
}
nkeynes@359
   135
nkeynes@398
   136
/**
nkeynes@398
   137
 * Translate a linear basic block to a temporary buffer, execute it, and return
nkeynes@398
   138
 * the result of the execution. The translation is discarded.
nkeynes@398
   139
 */
nkeynes@408
   140
void *sh4_translate_and_run( sh4addr_t start )
nkeynes@398
   141
{
nkeynes@430
   142
    unsigned char buf[65536];
nkeynes@359
   143
nkeynes@430
   144
    sh4addr_t pc = start;
nkeynes@398
   145
    int done;
nkeynes@398
   146
    xlat_output = buf;
nkeynes@398
   147
    uint8_t *eob = xlat_output + sizeof(buf);
nkeynes@398
   148
nkeynes@408
   149
    sh4_translate_begin_block(pc);
nkeynes@398
   150
nkeynes@527
   151
    while( (done = sh4_translate_instruction( pc )) == 0 ) {
nkeynes@398
   152
	assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
nkeynes@398
   153
	pc += 2;
nkeynes@398
   154
    }
nkeynes@398
   155
    pc+=2;
nkeynes@398
   156
    sh4_translate_end_block(pc);
nkeynes@398
   157
nkeynes@408
   158
    void * (*code)() = (void *)buf;
nkeynes@398
   159
    return code();
nkeynes@398
   160
}
.