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/sh4mmio.h"
27 #include "sh4/sh4dasm.h"
29 #include "xlat/xltcache.h"
30 #include "xlat/xlatdasm.h"
32 //#define SINGLESTEP 1
35 * Execute a timeslice using translated code only (ie translate/execute loop)
37 uint32_t sh4_translate_run_slice( uint32_t nanosecs )
39 event_schedule( EVENT_ENDTIMESLICE, nanosecs );
41 if( sh4r.event_pending <= sh4r.slice_cycle ) {
42 sh4_handle_pending_events();
43 if( sh4r.slice_cycle >= nanosecs )
47 if( IS_SYSCALL(sh4r.pc) ) {
48 uint32_t pc = sh4r.pc;
50 sh4r.in_delay_slot = 0;
54 void * (*code)() = xlat_get_code_by_vma( sh4r.pc );
56 while( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
57 code = XLAT_BLOCK_CHAIN(code);
59 code = sh4_translate_basic_block( sh4r.pc );
64 code = sh4_translate_basic_block( sh4r.pc );
71 xlat_cache_block_t xlat_current_block;
72 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
73 uint32_t xlat_recovery_posn;
75 void sh4_translate_add_recovery( uint32_t icount )
77 xlat_recovery[xlat_recovery_posn].xlat_offset =
78 ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
79 xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
84 * Translate a linear basic block, ie all instructions from the start address
85 * (inclusive) until the next branch/jump instruction or the end of the page
87 * @param start VMA of the block start (which must already be in the icache)
88 * @return the address of the translated block
89 * eg due to lack of buffer space.
91 void * sh4_translate_basic_block( sh4addr_t start )
94 sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
96 xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
97 xlat_output = (uint8_t *)xlat_current_block->code;
98 xlat_recovery_posn = 0;
99 uint8_t *eob = xlat_output + xlat_current_block->size;
101 if( GET_ICACHE_END() < lastpc ) {
102 lastpc = GET_ICACHE_END();
105 sh4_translate_begin_block(pc);
108 if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
109 uint8_t *oldstart = xlat_current_block->code;
110 xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
111 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
112 eob = xlat_current_block->code + xlat_current_block->size;
114 done = sh4_translate_instruction( pc );
115 assert( xlat_output <= eob );
117 if ( pc >= lastpc && done == 0 ) {
121 if( !done ) done = 2;
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, start, pc );
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_pre_recovery(code, pc);
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 );
214 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
218 if( IS_IN_ICACHE(vma) ) {
219 return xlat_get_code( GET_ICACHE_PHYS(vma) );
222 if( IS_SYSCALL(vma) ) {
227 if( !mmu_update_icache(vma) ) {
228 // fault - off to the fault handler
229 if( !mmu_update_icache(sh4r.pc) ) {
230 // double fault - halt
231 ERROR( "Double fault - halting" );
232 sh4_core_exit(CORE_EXIT_HALT);
237 assert( IS_IN_ICACHE(sh4r.pc) );
238 result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
243 * Crashdump translation information.
245 * Print out the currently executing block (if any), in source and target
248 * Note: we want to be _really_ careful not to cause a second-level crash
249 * at this point (e.g. if the lookup tables are corrupted...)
251 void sh4_translate_crashdump()
253 if( !IS_IN_ICACHE(sh4r.pc) ) {
254 /** If we're crashing due to an icache lookup failure, we'll probably
255 * hit this case - just complain and return.
257 fprintf( stderr, "** SH4 PC not in current instruction region **\n" );
260 uint32_t pma = GET_ICACHE_PHYS(sh4r.pc);
261 void *code = xlat_get_code( pma );
263 fprintf( stderr, "** No translated block for current SH4 PC **\n" );
267 /* Sanity check on the code pointer */
268 if( !xlat_is_code_pointer(code) ) {
269 fprintf( stderr, "** Possibly corrupt translation cache **\n" );
273 void *native_pc = xlat_get_native_pc( code, xlat_get_code_size(code) );
274 sh4_translate_disasm_block( stderr, code, sh4r.pc, native_pc );
278 * Dual-dump the translated block and original SH4 code for the basic block
279 * starting at sh4_pc. If there is no translated block, this prints an error
282 void sh4_translate_dump_block( uint32_t sh4_pc )
284 if( !IS_IN_ICACHE(sh4_pc) ) {
285 fprintf( stderr, "** Address %08x not in current instruction region **\n", sh4_pc );
288 uint32_t pma = GET_ICACHE_PHYS(sh4_pc);
289 void *code = xlat_get_code( pma );
291 fprintf( stderr, "** No translated block for address %08x **\n", sh4_pc );
294 sh4_translate_disasm_block( stderr, code, sh4_pc, NULL );
298 static struct xlat_symbol xlat_symbol_table[] = {
299 { "sh4r+128", ((char *)&sh4r)+128 },
300 { "sh4_cpu_period", &sh4_cpu_period },
301 { "sh4_address_space", NULL },
302 { "sh4_user_address_space", NULL },
303 { "sh4_translate_breakpoint_hit", sh4_translate_breakpoint_hit },
304 { "sh4_translate_link_block", sh4_translate_link_block },
305 { "sh4_write_fpscr", sh4_write_fpscr },
306 { "sh4_write_sr", sh4_write_sr },
307 { "sh4_read_sr", sh4_read_sr },
308 { "sh4_raise_exception", sh4_raise_exception },
309 { "sh4_sleep", sh4_sleep },
310 { "sh4_fsca", sh4_fsca },
311 { "sh4_ftrv", sh4_ftrv },
312 { "sh4_switch_fr_banks", sh4_switch_fr_banks },
313 { "sh4_execute_instruction", sh4_execute_instruction },
314 { "signsat48", signsat48 },
315 { "xlat_get_code_by_vma", xlat_get_code_by_vma },
316 { "xlat_get_code", xlat_get_code }
320 * Disassemble the given translated code block, and it's source code block
321 * side-by-side. The current native pc will be marked if non-null.
323 void sh4_translate_disasm_block( FILE *out, void *code, sh4addr_t source_start, void *native_pc )
328 xlat_symbol_table[2].ptr = sh4_address_space;
329 xlat_symbol_table[3].ptr = sh4_user_address_space;
330 xlat_disasm_init( xlat_symbol_table, sizeof(xlat_symbol_table)/sizeof(struct xlat_symbol) );
332 uintptr_t target_start = (uintptr_t)code, target_pc;
333 uintptr_t target_end = target_start + xlat_get_code_size(code);
334 uint32_t source_pc = source_start;
335 uint32_t source_end = source_pc;
336 xlat_recovery_record_t source_recov_table = XLAT_RECOVERY_TABLE(code);
337 xlat_recovery_record_t source_recov_end = source_recov_table + XLAT_BLOCK_FOR_CODE(code)->recover_table_size - 1;
339 for( target_pc = target_start; target_pc < target_end; ) {
340 uintptr_t pc2 = xlat_disasm_instruction( target_pc, buf, sizeof(buf), op );
341 #if SIZEOF_VOID_P == 8
342 fprintf( out, "%c%016lx: %-30s %-40s", (target_pc == (uintptr_t)native_pc ? '*' : ' '),
343 target_pc, op, buf );
345 fprintf( out, "%c%08lx: %-30s %-40s", (target_pc == (uintptr_t)native_pc ? '*' : ' '),
346 target_pc, op, buf );
348 if( source_recov_table < source_recov_end &&
349 target_pc >= (target_start + source_recov_table->xlat_offset) ) {
350 source_recov_table++;
351 if( source_end < (source_start + (source_recov_table->sh4_icount)*2) )
352 source_end = source_start + (source_recov_table->sh4_icount)*2;
355 if( source_pc < source_end ) {
356 uint32_t source_pc2 = sh4_disasm_instruction( source_pc, buf, sizeof(buf), op );
357 fprintf( out, " %08X: %s %s\n", source_pc, op, buf );
358 source_pc = source_pc2;
360 fprintf( out, "\n" );
366 while( source_pc < source_end ) {
367 uint32_t source_pc2 = sh4_disasm_instruction( source_pc, buf, sizeof(buf), op );
368 fprintf( out, "%*c %08X: %s %s\n", 72,' ', source_pc, op, buf );
369 source_pc = source_pc2;
374 void sh4_translate_dump_cache_by_activity( unsigned int topN )
376 struct xlat_block_ref blocks[topN];
377 topN = xlat_get_cache_blocks_by_activity(blocks, topN);
379 for( i=0; i<topN; i++ ) {
380 fprintf( stderr, "0x%08X (%p): %d \n", blocks[i].pc, blocks[i].block->code, blocks[i].block->active);
381 sh4_translate_disasm_block( stderr, blocks[i].block->code, blocks[i].pc, NULL );
382 fprintf( stderr, "\n" );
.