2 * $Id: sh4trans.c,v 1.8 2007-10-08 12:06:01 nkeynes Exp $
4 * SH4 translation core module. This part handles the non-target-specific
5 * section of the translation.
7 * Copyright (c) 2005 Nathan Keynes.
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.
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.
22 #include "sh4/sh4core.h"
23 #include "sh4/sh4trans.h"
24 #include "sh4/xltcache.h"
30 * Execute a timeslice using translated code only (ie translate/execute loop)
31 * Note this version does not support breakpoints
33 uint32_t sh4_xlat_run_slice( uint32_t nanosecs )
37 if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
38 if( sh4r.event_pending < nanosecs ) {
39 sh4r.sh4_state = SH4_STATE_RUNNING;
40 sh4r.slice_cycle = sh4r.event_pending;
44 void * (*code)() = NULL;
45 while( sh4r.slice_cycle < nanosecs ) {
46 if( sh4r.event_pending <= sh4r.slice_cycle ) {
47 if( sh4r.event_types & PENDING_EVENT ) {
50 /* Eventq execute may (quite likely) deliver an immediate IRQ */
51 if( sh4r.event_types & PENDING_IRQ ) {
52 sh4_accept_interrupt();
58 if( sh4r.pc > 0xFFFFFF00 ) {
59 syscall_invoke( sh4r.pc );
60 sh4r.in_delay_slot = 0;
64 code = xlat_get_code(sh4r.pc);
66 uint64_t ppa = mmu_vma_to_phys_exec( sh4r.pc );
68 // not found, exception
69 ppa = mmu_vma_to_phys_exec( sh4r.pc );
71 // double fault - halt
73 ERROR( "Double fault - halting" );
77 code = sh4_translate_basic_block( sh4r.pc );
85 if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
86 TMU_run_slice( nanosecs );
87 SCIF_run_slice( nanosecs );
95 * Translate a linear basic block, ie all instructions from the start address
96 * (inclusive) until the next branch/jump instruction or the end of the page
98 * @return the address of the translated block
99 * eg due to lack of buffer space.
101 void * sh4_translate_basic_block( sh4addr_t start )
103 sh4addr_t pc = start;
104 sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
106 xlat_cache_block_t block = xlat_start_block( start );
107 xlat_output = (uint8_t *)block->code;
108 uint8_t *eob = xlat_output + block->size;
109 sh4_translate_begin_block(pc);
112 if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
113 uint8_t *oldstart = block->code;
114 block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
115 xlat_output = block->code + (xlat_output - oldstart);
116 eob = block->code + block->size;
118 done = sh4_translate_instruction( pc );
119 assert( xlat_output <= eob );
121 if ( pc >= lastpc ) {
126 if( eob - xlat_output < EPILOGUE_SIZE ) {
127 uint8_t *oldstart = block->code;
128 block = xlat_extend_block( xlat_output - oldstart + EPILOGUE_SIZE );
129 xlat_output = block->code + (xlat_output - oldstart);
131 sh4_translate_end_block(pc);
132 xlat_commit_block( xlat_output - block->code, pc-start );
137 * Translate a linear basic block to a temporary buffer, execute it, and return
138 * the result of the execution. The translation is discarded.
140 void *sh4_translate_and_run( sh4addr_t start )
142 unsigned char buf[65536];
144 sh4addr_t pc = start;
147 uint8_t *eob = xlat_output + sizeof(buf);
149 sh4_translate_begin_block(pc);
151 while( (done = sh4_translate_instruction( pc )) == 0 ) {
152 assert( (eob - xlat_output) >= MAX_INSTRUCTION_SIZE );
156 sh4_translate_end_block(pc);
158 void * (*code)() = (void *)buf;
.