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"
28 #include "xlat/xltcache.h"
30 //#define SINGLESTEP 1
33 * Execute a timeslice using translated code only (ie translate/execute loop)
35 uint32_t sh4_translate_run_slice( uint32_t nanosecs )
37 event_schedule( EVENT_ENDTIMESLICE, nanosecs );
39 if( sh4r.event_pending <= sh4r.slice_cycle ) {
40 sh4_handle_pending_events();
41 if( sh4r.slice_cycle >= nanosecs )
45 if( IS_SYSCALL(sh4r.pc) ) {
46 uint32_t pc = sh4r.pc;
48 sh4r.in_delay_slot = 0;
52 void * (*code)() = xlat_get_code_by_vma( sh4r.pc );
54 while( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
55 code = XLAT_BLOCK_CHAIN(code);
57 code = sh4_translate_basic_block( sh4r.pc );
62 code = sh4_translate_basic_block( sh4r.pc );
69 xlat_cache_block_t xlat_current_block;
70 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
71 uint32_t xlat_recovery_posn;
73 void sh4_translate_add_recovery( uint32_t icount )
75 xlat_recovery[xlat_recovery_posn].xlat_offset =
76 ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
77 xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
82 * Translate a linear basic block, ie all instructions from the start address
83 * (inclusive) until the next branch/jump instruction or the end of the page
85 * @param start VMA of the block start (which must already be in the icache)
86 * @return the address of the translated block
87 * eg due to lack of buffer space.
89 void * sh4_translate_basic_block( sh4addr_t start )
92 sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
94 xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
95 xlat_output = (uint8_t *)xlat_current_block->code;
96 xlat_recovery_posn = 0;
97 uint8_t *eob = xlat_output + xlat_current_block->size;
99 if( GET_ICACHE_END() < lastpc ) {
100 lastpc = GET_ICACHE_END();
103 sh4_translate_begin_block(pc);
106 if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
107 uint8_t *oldstart = xlat_current_block->code;
108 xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
109 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
110 eob = xlat_current_block->code + xlat_current_block->size;
112 done = sh4_translate_instruction( pc );
113 assert( xlat_output <= eob );
115 if ( pc >= lastpc && done == 0 ) {
119 if( !done ) done = 2;
124 // Add end-of-block recovery for post-instruction checks
125 sh4_translate_add_recovery( (pc - start)>>1 );
127 int epilogue_size = sh4_translate_end_block_size();
128 uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
129 uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
130 if( xlat_current_block->size < finalsize ) {
131 uint8_t *oldstart = xlat_current_block->code;
132 xlat_current_block = xlat_extend_block( finalsize );
133 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
135 sh4_translate_end_block(pc);
136 assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
138 /* Write the recovery records onto the end of the code block */
139 memcpy( xlat_output, xlat_recovery, recovery_size);
140 xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
141 xlat_current_block->recover_table_size = xlat_recovery_posn;
142 xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
143 xlat_commit_block( finalsize, start, pc );
144 return xlat_current_block->code;
148 * "Execute" the supplied recovery record. Currently this only updates
149 * sh4r.pc and sh4r.slice_cycle according to the currently executing
150 * instruction. In future this may be more sophisticated (ie will
151 * call into generated code).
153 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
155 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
156 sh4r.pc += (recovery->sh4_icount<<1);
160 * Same as sh4_translate_run_recovery, but is used to recover from a taken
161 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
163 void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
165 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
166 sh4r.spc += (recovery->sh4_icount<<1);
169 void sh4_translate_exit_recover( )
171 void *code = xlat_get_code_by_vma( sh4r.pc );
173 uint32_t size = xlat_get_code_size( code );
174 void *pc = xlat_get_native_pc( code, size );
176 // could be null if we're not actually running inside the translator
177 xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
178 if( recover != NULL ) {
179 // Can be null if there is no recovery necessary
180 sh4_translate_run_recovery(recover);
186 void sh4_translate_exception_exit_recover( )
188 void *code = xlat_get_code_by_vma( sh4r.spc );
190 uint32_t size = xlat_get_code_size( code );
191 void *pc = xlat_get_native_pc( code, size );
193 // could be null if we're not actually running inside the translator
194 xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
195 if( recover != NULL ) {
196 // Can be null if there is no recovery necessary
197 sh4_translate_run_exception_recovery(recover);
204 void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
206 if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
209 sh4_core_exit( CORE_EXIT_BREAKPOINT );
212 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
216 if( IS_IN_ICACHE(vma) ) {
217 return xlat_get_code( GET_ICACHE_PHYS(vma) );
220 if( IS_SYSCALL(vma) ) {
225 if( !mmu_update_icache(vma) ) {
226 // fault - off to the fault handler
227 if( !mmu_update_icache(sh4r.pc) ) {
228 // double fault - halt
229 ERROR( "Double fault - halting" );
230 sh4_core_exit(CORE_EXIT_HALT);
235 assert( IS_IN_ICACHE(sh4r.pc) );
236 result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
241 * Crashdump translation information.
243 * Print out the currently executing block (if any), in source and target
246 * Note: we want to be _really_ careful not to cause a second-level crash
247 * at this point (e.g. if the lookup tables are corrupted...)
249 void sh4_translate_crashdump()
251 if( !IS_IN_ICACHE(sh4r.pc) ) {
252 /** If we're crashing due to an icache lookup failure, we'll probably
253 * hit this case - just complain and return.
255 fprintf( stderr, "** SH4 PC not in current instruction region **\n" );
258 uint32_t pma = GET_ICACHE_PHYS(sh4r.pc);
259 void *code = xlat_get_code( pma );
261 fprintf( stderr, "** No translated block for current SH4 PC **\n" );
265 /* Sanity check on the code pointer */
266 if( !xlat_is_code_pointer(code) ) {
267 fprintf( stderr, "** Possibly corrupt translation cache **\n" );
271 void *native_pc = xlat_get_native_pc( code, xlat_get_code_size(code) );
272 sh4_translate_disasm_block( stderr, code, sh4r.pc, native_pc );
276 * Dual-dump the translated block and original SH4 code for the basic block
277 * starting at sh4_pc. If there is no translated block, this prints an error
280 void sh4_translate_dump_block( uint32_t sh4_pc )
282 if( !IS_IN_ICACHE(sh4_pc) ) {
283 fprintf( stderr, "** Address %08x not in current instruction region **\n", sh4_pc );
286 uint32_t pma = GET_ICACHE_PHYS(sh4_pc);
287 void *code = xlat_get_code( pma );
289 fprintf( stderr, "** No translated block for address %08x **\n", sh4_pc );
292 sh4_translate_disasm_block( stderr, code, sh4_pc, NULL );
295 void sh4_translate_dump_cache_by_activity( unsigned int topN )
297 struct xlat_block_ref blocks[topN];
298 topN = xlat_get_cache_blocks_by_activity(blocks, topN);
300 for( i=0; i<topN; i++ ) {
301 fprintf( stderr, "0x%08X (%p): %d \n", blocks[i].pc, blocks[i].block->code, blocks[i].block->active);
302 sh4_translate_disasm_block( stderr, blocks[i].block->code, blocks[i].pc, NULL );
303 fprintf( stderr, "\n" );
.