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.
23 #include "dreamcast.h"
24 #include "sh4/sh4core.h"
25 #include "sh4/sh4trans.h"
26 #include "sh4/xltcache.h"
30 * Execute a timeslice using translated code only (ie translate/execute loop)
32 uint32_t sh4_translate_run_slice( uint32_t nanosecs )
34 void * (*code)() = NULL;
35 while( sh4r.slice_cycle < nanosecs ) {
36 if( sh4r.event_pending <= sh4r.slice_cycle ) {
37 if( sh4r.event_types & PENDING_EVENT ) {
40 /* Eventq execute may (quite likely) deliver an immediate IRQ */
41 if( sh4r.event_types & PENDING_IRQ ) {
42 sh4_accept_interrupt();
48 if( sh4r.pc > 0xFFFFFF00 ) {
49 syscall_invoke( sh4r.pc );
50 sh4r.in_delay_slot = 0;
54 code = xlat_get_code_by_vma( sh4r.pc );
55 if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
56 code = sh4_translate_basic_block( sh4r.pc );
58 } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
59 code = sh4_translate_basic_block( sh4r.pc );
67 xlat_cache_block_t xlat_current_block;
68 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
69 uint32_t xlat_recovery_posn;
71 void sh4_translate_add_recovery( uint32_t icount )
73 xlat_recovery[xlat_recovery_posn].xlat_offset =
74 ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
75 xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
80 * Translate a linear basic block, ie all instructions from the start address
81 * (inclusive) until the next branch/jump instruction or the end of the page
83 * @param start VMA of the block start (which must already be in the icache)
84 * @return the address of the translated block
85 * eg due to lack of buffer space.
87 void * sh4_translate_basic_block( sh4addr_t start )
90 sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
92 xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
93 xlat_output = (uint8_t *)xlat_current_block->code;
94 xlat_recovery_posn = 0;
95 uint8_t *eob = xlat_output + xlat_current_block->size;
97 if( GET_ICACHE_END() < lastpc ) {
98 lastpc = GET_ICACHE_END();
101 sh4_translate_begin_block(pc);
104 /* check for breakpoints at this pc */
105 for( i=0; i<sh4_breakpoint_count; i++ ) {
106 if( sh4_breakpoints[i].address == pc ) {
107 sh4_translate_emit_breakpoint(pc);
111 if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
112 uint8_t *oldstart = xlat_current_block->code;
113 xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
114 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
115 eob = xlat_current_block->code + xlat_current_block->size;
117 done = sh4_translate_instruction( pc );
118 assert( xlat_output <= eob );
120 if ( pc >= lastpc ) {
126 // Add end-of-block recovery for post-instruction checks
127 sh4_translate_add_recovery( (pc - start)>>1 );
129 int epilogue_size = sh4_translate_end_block_size();
130 uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
131 uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
132 if( xlat_current_block->size < finalsize ) {
133 uint8_t *oldstart = xlat_current_block->code;
134 xlat_current_block = xlat_extend_block( finalsize );
135 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
137 sh4_translate_end_block(pc);
138 assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
140 /* Write the recovery records onto the end of the code block */
141 memcpy( xlat_output, xlat_recovery, recovery_size);
142 xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
143 xlat_current_block->recover_table_size = xlat_recovery_posn;
144 xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
145 xlat_commit_block( finalsize, pc-start );
146 return xlat_current_block->code;
150 * "Execute" the supplied recovery record. Currently this only updates
151 * sh4r.pc and sh4r.slice_cycle according to the currently executing
152 * instruction. In future this may be more sophisticated (ie will
153 * call into generated code).
155 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
157 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
158 sh4r.pc += (recovery->sh4_icount<<1);
162 * Same as sh4_translate_run_recovery, but is used to recover from a taken
163 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
165 void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
167 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
168 sh4r.spc += (recovery->sh4_icount<<1);
171 void sh4_translate_exit_recover( )
173 void *code = xlat_get_code_by_vma( sh4r.pc );
175 uint32_t size = xlat_get_code_size( code );
176 void *pc = xlat_get_native_pc( code, size );
178 // could be null if we're not actually running inside the translator
179 xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, TRUE);
180 if( recover != NULL ) {
181 // Can be null if there is no recovery necessary
182 sh4_translate_run_recovery(recover);
188 void sh4_translate_exception_exit_recover( )
190 void *code = xlat_get_code_by_vma( sh4r.spc );
192 uint32_t size = xlat_get_code_size( code );
193 void *pc = xlat_get_native_pc( code, size );
195 // could be null if we're not actually running inside the translator
196 xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
197 if( recover != NULL ) {
198 // Can be null if there is no recovery necessary
199 sh4_translate_run_exception_recovery(recover);
206 void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
208 if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
211 sh4_core_exit( CORE_EXIT_BREAKPOINT );
215 * Exit the current block at the end of the current instruction, flush the
216 * translation cache (completely) and return control to sh4_xlat_run_slice.
218 * As a special case, if the current instruction is actually the last
219 * instruction in the block (ie it's in a delay slot), this function
220 * returns to allow normal completion of the translation block. Otherwise
221 * this function never returns.
223 * Must only be invoked (indirectly) from within translated code.
225 gboolean sh4_translate_flush_cache()
227 void *code = xlat_get_code_by_vma( sh4r.pc );
229 uint32_t size = xlat_get_code_size( code );
230 void *pc = xlat_get_native_pc( code, size );
231 assert( pc != NULL );
233 xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, FALSE);
234 if( recover != NULL ) {
235 // Can be null if there is no recovery necessary
236 sh4_translate_run_recovery(recover);
246 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
250 if( IS_IN_ICACHE(vma) ) {
251 return xlat_get_code( GET_ICACHE_PHYS(vma) );
254 if( vma > 0xFFFFFF00 ) {
259 if( !mmu_update_icache(vma) ) {
260 // fault - off to the fault handler
261 if( !mmu_update_icache(sh4r.pc) ) {
262 // double fault - halt
263 ERROR( "Double fault - halting" );
264 sh4_core_exit(CORE_EXIT_HALT);
269 assert( IS_IN_ICACHE(sh4r.pc) );
270 result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
.