filename | src/sh4/sh4trans.c |
changeset | 991:60c7fab9c880 |
prev | 978:eed5089fcfdb |
next | 1003:7b2688cbbca3 |
author | nkeynes |
date | Wed Mar 04 23:12:21 2009 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Move xltcache to xlat/ src directory Commit new and improved x86 opcode file - cleaned up and added support for amd64 extended registers |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * SH4 translation core module. This part handles the non-target-specific
5 * section of the translation.
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
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.
13 *
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.
18 */
19 #include <assert.h>
20 #include "eventq.h"
21 #include "syscall.h"
22 #include "clock.h"
23 #include "dreamcast.h"
24 #include "sh4/sh4core.h"
25 #include "sh4/sh4trans.h"
26 #include "sh4/sh4mmio.h"
27 #include "sh4/mmu.h"
28 #include "xlat/xltcache.h"
30 /**
31 * Execute a timeslice using translated code only (ie translate/execute loop)
32 */
33 uint32_t sh4_translate_run_slice( uint32_t nanosecs )
34 {
35 void * (*code)() = NULL;
36 while( sh4r.slice_cycle < nanosecs ) {
37 if( sh4r.event_pending <= sh4r.slice_cycle ) {
38 if( sh4r.event_types & PENDING_EVENT ) {
39 event_execute();
40 }
41 /* Eventq execute may (quite likely) deliver an immediate IRQ */
42 if( sh4r.event_types & PENDING_IRQ ) {
43 sh4_accept_interrupt();
44 code = NULL;
45 }
46 }
48 if( code == NULL ) {
49 if( sh4r.pc > 0xFFFFFF00 ) {
50 syscall_invoke( sh4r.pc );
51 sh4r.in_delay_slot = 0;
52 sh4r.pc = sh4r.pr;
53 }
55 code = xlat_get_code_by_vma( sh4r.pc );
56 if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
57 code = sh4_translate_basic_block( sh4r.pc );
58 }
59 } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
60 if( !IS_IN_ICACHE(sh4r.pc) ) {
61 /* If TLB is off, we may have gotten here without updating
62 * the icache, so do it now. This should never fail, so...
63 */
64 mmu_update_icache(sh4r.pc);
65 assert( IS_IN_ICACHE(sh4r.pc) );
66 }
67 code = sh4_translate_basic_block( sh4r.pc );
68 }
69 code = code();
70 }
71 return nanosecs;
72 }
74 uint8_t *xlat_output;
75 xlat_cache_block_t xlat_current_block;
76 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
77 uint32_t xlat_recovery_posn;
79 void sh4_translate_add_recovery( uint32_t icount )
80 {
81 xlat_recovery[xlat_recovery_posn].xlat_offset =
82 ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
83 xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
84 xlat_recovery_posn++;
85 }
87 /**
88 * Translate a linear basic block, ie all instructions from the start address
89 * (inclusive) until the next branch/jump instruction or the end of the page
90 * is reached.
91 * @param start VMA of the block start (which must already be in the icache)
92 * @return the address of the translated block
93 * eg due to lack of buffer space.
94 */
95 void * sh4_translate_basic_block( sh4addr_t start )
96 {
97 sh4addr_t pc = start;
98 sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
99 int done, i;
100 xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
101 xlat_output = (uint8_t *)xlat_current_block->code;
102 xlat_recovery_posn = 0;
103 uint8_t *eob = xlat_output + xlat_current_block->size;
105 if( GET_ICACHE_END() < lastpc ) {
106 lastpc = GET_ICACHE_END();
107 }
109 sh4_translate_begin_block(pc);
111 do {
112 /* check for breakpoints at this pc */
113 for( i=0; i<sh4_breakpoint_count; i++ ) {
114 if( sh4_breakpoints[i].address == pc ) {
115 sh4_translate_emit_breakpoint(pc);
116 break;
117 }
118 }
119 if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
120 uint8_t *oldstart = xlat_current_block->code;
121 xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
122 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
123 eob = xlat_current_block->code + xlat_current_block->size;
124 }
125 done = sh4_translate_instruction( pc );
126 assert( xlat_output <= eob );
127 pc += 2;
128 if ( pc >= lastpc ) {
129 done = 2;
130 }
131 } while( !done );
132 pc += (done - 2);
134 // Add end-of-block recovery for post-instruction checks
135 sh4_translate_add_recovery( (pc - start)>>1 );
137 int epilogue_size = sh4_translate_end_block_size();
138 uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
139 uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
140 if( xlat_current_block->size < finalsize ) {
141 uint8_t *oldstart = xlat_current_block->code;
142 xlat_current_block = xlat_extend_block( finalsize );
143 xlat_output = xlat_current_block->code + (xlat_output - oldstart);
144 }
145 sh4_translate_end_block(pc);
146 assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
148 /* Write the recovery records onto the end of the code block */
149 memcpy( xlat_output, xlat_recovery, recovery_size);
150 xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
151 xlat_current_block->recover_table_size = xlat_recovery_posn;
152 xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
153 xlat_commit_block( finalsize, pc-start );
154 return xlat_current_block->code;
155 }
157 /**
158 * "Execute" the supplied recovery record. Currently this only updates
159 * sh4r.pc and sh4r.slice_cycle according to the currently executing
160 * instruction. In future this may be more sophisticated (ie will
161 * call into generated code).
162 */
163 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
164 {
165 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
166 sh4r.pc += (recovery->sh4_icount<<1);
167 }
169 /**
170 * Same as sh4_translate_run_recovery, but is used to recover from a taken
171 * exception - that is, it fixes sh4r.spc rather than sh4r.pc
172 */
173 void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
174 {
175 sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
176 sh4r.spc += (recovery->sh4_icount<<1);
177 }
179 void sh4_translate_exit_recover( )
180 {
181 void *code = xlat_get_code_by_vma( sh4r.pc );
182 if( code != NULL ) {
183 uint32_t size = xlat_get_code_size( code );
184 void *pc = xlat_get_native_pc( code, size );
185 if( pc != NULL ) {
186 // could be null if we're not actually running inside the translator
187 xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
188 if( recover != NULL ) {
189 // Can be null if there is no recovery necessary
190 sh4_translate_run_recovery(recover);
191 }
192 }
193 }
194 }
196 void sh4_translate_exception_exit_recover( )
197 {
198 void *code = xlat_get_code_by_vma( sh4r.spc );
199 if( code != NULL ) {
200 uint32_t size = xlat_get_code_size( code );
201 void *pc = xlat_get_native_pc( code, size );
202 if( pc != NULL ) {
203 // could be null if we're not actually running inside the translator
204 xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
205 if( recover != NULL ) {
206 // Can be null if there is no recovery necessary
207 sh4_translate_run_exception_recovery(recover);
208 }
209 }
210 }
212 }
214 void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
215 {
216 if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
217 return;
218 }
219 sh4_core_exit( CORE_EXIT_BREAKPOINT );
220 }
222 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
223 {
224 void *result = NULL;
226 if( IS_IN_ICACHE(vma) ) {
227 return xlat_get_code( GET_ICACHE_PHYS(vma) );
228 }
230 if( vma > 0xFFFFFF00 ) {
231 // lxdream hook
232 return NULL;
233 }
235 if( !mmu_update_icache(vma) ) {
236 // fault - off to the fault handler
237 if( !mmu_update_icache(sh4r.pc) ) {
238 // double fault - halt
239 ERROR( "Double fault - halting" );
240 sh4_core_exit(CORE_EXIT_HALT);
241 return NULL;
242 }
243 }
245 assert( IS_IN_ICACHE(sh4r.pc) );
246 result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
247 return result;
248 }
.