1.1 --- a/src/sh4/sh4x86.c Tue Aug 28 08:46:14 2007 +0000
1.2 +++ b/src/sh4/sh4x86.c Tue Sep 04 08:40:23 2007 +0000
1.5 - * $Id: sh4x86.c,v 1.2 2007-08-28 08:46:14 nkeynes Exp $
1.6 + * $Id: sh4x86.c,v 1.3 2007-09-04 08:40:23 nkeynes Exp $
1.8 * SH4 => x86 translation. This version does no real optimization, it just
1.9 * outputs straight-line x86 code - it mainly exists to provide a baseline
1.11 * GNU General Public License for more details.
1.14 -#include "sh4core.h"
1.15 -#include "sh4trans.h"
1.17 +#include <assert.h>
1.19 +#include "sh4/sh4core.h"
1.20 +#include "sh4/sh4trans.h"
1.21 +#include "sh4/x86op.h"
1.24 +#define DEFAULT_BACKPATCH_SIZE 4096
1.27 + * Struct to manage internal translation state. This state is not saved -
1.28 + * it is only valid between calls to sh4_translate_begin_block() and
1.29 + * sh4_translate_end_block()
1.31 +struct sh4_x86_state {
1.32 + gboolean in_delay_slot;
1.33 + gboolean priv_checked; /* true if we've already checked the cpu mode. */
1.34 + gboolean fpuen_checked; /* true if we've already checked fpu enabled. */
1.36 + /* Allocated memory for the (block-wide) back-patch list */
1.37 + uint32_t **backpatch_list;
1.38 + uint32_t backpatch_posn;
1.39 + uint32_t backpatch_size;
1.42 +#define EXIT_DATA_ADDR_READ 0
1.43 +#define EXIT_DATA_ADDR_WRITE 7
1.44 +#define EXIT_ILLEGAL 14
1.45 +#define EXIT_SLOT_ILLEGAL 21
1.46 +#define EXIT_FPU_DISABLED 28
1.47 +#define EXIT_SLOT_FPU_DISABLED 35
1.49 +static struct sh4_x86_state sh4_x86;
1.51 +void sh4_x86_init()
1.53 + sh4_x86.backpatch_list = malloc(DEFAULT_BACKPATCH_SIZE);
1.54 + sh4_x86.backpatch_size = DEFAULT_BACKPATCH_SIZE / sizeof(uint32_t *);
1.58 +static void sh4_x86_add_backpatch( uint8_t *ptr )
1.60 + if( sh4_x86.backpatch_posn == sh4_x86.backpatch_size ) {
1.61 + sh4_x86.backpatch_size <<= 1;
1.62 + sh4_x86.backpatch_list = realloc( sh4_x86.backpatch_list, sh4_x86.backpatch_size * sizeof(uint32_t *) );
1.63 + assert( sh4_x86.backpatch_list != NULL );
1.65 + sh4_x86.backpatch_list[sh4_x86.backpatch_posn++] = (uint32_t *)ptr;
1.68 +static void sh4_x86_do_backpatch( uint8_t *reloc_base )
1.71 + for( i=0; i<sh4_x86.backpatch_posn; i++ ) {
1.72 + *sh4_x86.backpatch_list[i] += (reloc_base - ((uint8_t *)sh4_x86.backpatch_list[i]));
1.77 +#define MARK_JMP(x,n) uint8_t *_mark_jmp_##x = xlat_output + n
1.78 +#define CHECK_JMP(x) assert( _mark_jmp_##x == xlat_output )
1.80 +#define MARK_JMP(x,n)
1.81 +#define CHECK_JMP(x)
1.86 * Emit an instruction to load an SH4 reg into a real register
1.88 OP(REG_OFFSET(r[sh4reg]));
1.92 + * Load the SR register into an x86 register
1.94 +static inline void read_sr( int x86reg )
1.96 + MOV_ebp_r32( R_M, x86reg );
1.97 + SHL1_r32( x86reg );
1.98 + OR_ebp_r32( R_Q, x86reg );
1.99 + SHL_imm8_r32( 7, x86reg );
1.100 + OR_ebp_r32( R_S, x86reg );
1.101 + SHL1_r32( x86reg );
1.102 + OR_ebp_r32( R_T, x86reg );
1.103 + OR_ebp_r32( R_SR, x86reg );
1.106 +static inline void write_sr( int x86reg )
1.108 + TEST_imm32_r32( SR_M, x86reg );
1.110 + TEST_imm32_r32( SR_Q, x86reg );
1.112 + TEST_imm32_r32( SR_S, x86reg );
1.114 + TEST_imm32_r32( SR_T, x86reg );
1.116 + AND_imm32_r32( SR_MQSTMASK, x86reg );
1.117 + MOV_r32_ebp( x86reg, R_SR );
1.121 static inline void load_spreg( int x86reg, int regoffset )
1.123 /* mov [bp+n], reg */
1.125 static inline void call_func0( void *ptr )
1.127 load_imm32(R_EAX, (uint32_t)ptr);
1.129 - MODRM_rm32_r32(R_EAX, 2);
1.133 static inline void call_func1( void *ptr, int arg1 )
1.134 @@ -92,6 +185,59 @@
1.135 ADD_imm8s_r32( -4, R_ESP );
1.138 +/* Exception checks - Note that all exception checks will clobber EAX */
1.139 +static void check_priv( )
1.141 + if( !sh4_x86.priv_checked ) {
1.142 + sh4_x86.priv_checked = TRUE;
1.143 + load_spreg( R_EAX, R_SR );
1.144 + AND_imm32_r32( SR_MD, R_EAX );
1.145 + if( sh4_x86.in_delay_slot ) {
1.146 + JE_exit( EXIT_SLOT_ILLEGAL );
1.148 + JE_exit( EXIT_ILLEGAL );
1.153 +static void check_fpuen( )
1.155 + if( !sh4_x86.fpuen_checked ) {
1.156 + sh4_x86.fpuen_checked = TRUE;
1.157 + load_spreg( R_EAX, R_SR );
1.158 + AND_imm32_r32( SR_FD, R_EAX );
1.159 + if( sh4_x86.in_delay_slot ) {
1.160 + JNE_exit(EXIT_SLOT_FPU_DISABLED);
1.162 + JNE_exit(EXIT_FPU_DISABLED);
1.167 +static void check_ralign16( int x86reg )
1.169 + TEST_imm32_r32( 0x00000001, x86reg );
1.170 + JNE_exit(EXIT_DATA_ADDR_READ);
1.173 +static void check_walign16( int x86reg )
1.175 + TEST_imm32_r32( 0x00000001, x86reg );
1.176 + JNE_exit(EXIT_DATA_ADDR_WRITE);
1.179 +static void check_ralign32( int x86reg )
1.181 + TEST_imm32_r32( 0x00000003, x86reg );
1.182 + JNE_exit(EXIT_DATA_ADDR_READ);
1.184 +static void check_walign32( int x86reg )
1.186 + TEST_imm32_r32( 0x00000003, x86reg );
1.187 + JNE_exit(EXIT_DATA_ADDR_WRITE);
1.192 #define MEM_RESULT(value_reg) if(value_reg != R_EAX) { MOV_r32_r32(R_EAX,value_reg); }
1.193 #define MEM_READ_BYTE( addr_reg, value_reg ) call_func1(sh4_read_byte, addr_reg ); MEM_RESULT(value_reg)
1.194 @@ -101,30 +247,83 @@
1.195 #define MEM_WRITE_WORD( addr_reg, value_reg ) call_func2(sh4_write_word, addr_reg, value_reg)
1.196 #define MEM_WRITE_LONG( addr_reg, value_reg ) call_func2(sh4_write_long, addr_reg, value_reg)
1.198 +#define RAISE_EXCEPTION( exc ) call_func1(sh4_raise_exception, exc);
1.199 +#define CHECKSLOTILLEGAL() if(sh4_x86.in_delay_slot) RAISE_EXCEPTION(EXC_SLOT_ILLEGAL)
1.204 * Emit the 'start of block' assembly. Sets up the stack frame and save
1.205 * SI/DI as required
1.207 -void sh4_translate_begin_block() {
1.209 - *xlat_output++ = 0x50 + R_EBP;
1.211 +void sh4_translate_begin_block()
1.215 /* mov &sh4r, ebp */
1.216 load_imm32( R_EBP, (uint32_t)&sh4r );
1.219 + sh4_x86.in_delay_slot = FALSE;
1.220 + sh4_x86.priv_checked = FALSE;
1.221 + sh4_x86.fpuen_checked = FALSE;
1.222 + sh4_x86.backpatch_posn = 0;
1.225 - /* load carry from SR */
1.227 + * Exit the block early (ie branch out), conditionally or otherwise
1.229 +void exit_block( uint32_t pc )
1.231 + load_imm32( R_ECX, pc );
1.232 + store_spreg( R_ECX, REG_OFFSET(pc) );
1.233 + MOV_moff32_EAX( (uint32_t)&sh4_cpu_period );
1.234 + load_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.235 + MUL_r32( R_ESI );
1.236 + ADD_r32_r32( R_EAX, R_ECX );
1.237 + store_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.238 + XOR_r32_r32( R_EAX, R_EAX );
1.243 * Flush any open regs back to memory, restore SI/DI/, update PC, etc
1.245 void sh4_translate_end_block( sh4addr_t pc ) {
1.247 - *xlat_output++ = 0x58 + R_EBP;
1.248 + assert( !sh4_x86.in_delay_slot ); // should never stop here
1.249 + // Normal termination - save PC, cycle count
1.250 + exit_block( pc );
1.253 - *xlat_output++ = 0xC3;
1.254 + uint8_t *end_ptr = xlat_output;
1.255 + // Exception termination. Jump block for various exception codes:
1.256 + PUSH_imm32( EXC_DATA_ADDR_READ );
1.258 + PUSH_imm32( EXC_DATA_ADDR_WRITE );
1.260 + PUSH_imm32( EXC_ILLEGAL );
1.262 + PUSH_imm32( EXC_SLOT_ILLEGAL );
1.264 + PUSH_imm32( EXC_FPU_DISABLED );
1.266 + PUSH_imm32( EXC_SLOT_FPU_DISABLED );
1.268 + load_spreg( R_ECX, REG_OFFSET(pc) );
1.269 + ADD_r32_r32( R_ESI, R_ECX );
1.270 + ADD_r32_r32( R_ESI, R_ECX );
1.271 + store_spreg( R_ECX, REG_OFFSET(pc) );
1.272 + MOV_moff32_EAX( (uint32_t)&sh4_cpu_period );
1.273 + load_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.274 + MUL_r32( R_ESI );
1.275 + ADD_r32_r32( R_EAX, R_ECX );
1.276 + store_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.278 + load_imm32( R_EAX, (uint32_t)sh4_raise_exception ); // 6
1.279 + CALL_r32( R_EAX ); // 2
1.283 + sh4_x86_do_backpatch( end_ptr );
1.287 @@ -138,7 +337,7 @@
1.288 uint32_t sh4_x86_translate_instruction( uint32_t pc )
1.290 uint16_t ir = sh4_read_word( pc );
1.293 switch( (ir&0xF000) >> 12 ) {
1.296 @@ -149,7 +348,8 @@
1.299 uint32_t Rn = ((ir>>8)&0xF);
1.301 + read_sr( R_EAX );
1.302 + store_reg( R_EAX, Rn );
1.306 @@ -566,6 +766,18 @@
1.308 { /* CMP/STR Rm, Rn */
1.309 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.310 + load_reg( R_EAX, Rm );
1.311 + load_reg( R_ECX, Rn );
1.312 + XOR_r32_r32( R_ECX, R_EAX );
1.313 + TEST_r8_r8( R_AL, R_AL );
1.315 + TEST_r8_r8( R_AH, R_AH ); // 2
1.317 + SHR_imm8_r32( 16, R_EAX ); // 3
1.318 + TEST_r8_r8( R_AL, R_AL ); // 2
1.320 + TEST_r8_r8( R_AH, R_AH ); // 2
1.325 @@ -880,6 +1092,11 @@
1.326 { /* STC.L SR, @-Rn */
1.327 uint32_t Rn = ((ir>>8)&0xF);
1.329 + load_reg( R_ECX, Rn );
1.330 + ADD_imm8s_r32( -4, Rn );
1.331 + store_reg( R_ECX, Rn );
1.332 + read_sr( R_EAX );
1.333 + MEM_WRITE_LONG( R_ECX, R_EAX );
1.337 @@ -1085,6 +1302,12 @@
1.339 { /* LDC.L @Rm+, SR */
1.340 uint32_t Rm = ((ir>>8)&0xF);
1.341 + load_reg( R_EAX, Rm );
1.342 + MOV_r32_r32( R_EAX, R_ECX );
1.343 + ADD_imm8s_r32( 4, R_EAX );
1.344 + store_reg( R_EAX, Rm );
1.345 + MEM_READ_LONG( R_ECX, R_EAX );
1.346 + write_sr( R_EAX );
1.350 @@ -1312,6 +1535,16 @@
1.352 { /* SHLD Rm, Rn */
1.353 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.354 + load_reg( R_EAX, Rn );
1.355 + load_reg( R_ECX, Rm );
1.357 + MOV_r32_r32( R_EAX, R_EDX );
1.358 + SHL_r32_CL( R_EAX );
1.359 + NEG_r32( R_ECX );
1.360 + SHR_r32_CL( R_EDX );
1.361 + CMP_imm8s_r32( 0, R_ECX );
1.362 + CMOVAE_r32_r32( R_EDX, R_EAX );
1.363 + store_reg( R_EAX, Rn );
1.367 @@ -1321,7 +1554,8 @@
1.370 uint32_t Rm = ((ir>>8)&0xF);
1.371 - /* We need to be a little careful about SR */
1.372 + load_reg( R_EAX, Rm );
1.373 + write_sr( R_EAX );
1.377 @@ -1590,6 +1824,10 @@
1.380 int32_t disp = SIGNEXT8(ir&0xFF)<<1;
1.381 + CMP_imm8s_ebp( 0, R_T );
1.383 + exit_block( disp + pc + 4 );
1.388 @@ -1601,6 +1839,10 @@
1.391 int32_t disp = SIGNEXT8(ir&0xFF)<<1;
1.392 + CMP_imm8s_ebp( 0, R_T );
1.394 + exit_block( disp + pc + 4 );
1.395 + sh4_x86.in_delay_slot = TRUE;
1.399 @@ -1619,6 +1861,7 @@
1.402 int32_t disp = SIGNEXT12(ir&0xFFF)<<1;
1.403 + exit_block( disp + pc + 4 );
1.407 @@ -1697,6 +1940,9 @@
1.409 { /* TST #imm, R0 */
1.410 uint32_t imm = (ir&0xFF);
1.411 + load_reg( R_EAX, 0 );
1.412 + TEST_imm32_r32( imm, R_EAX );
1.417 @@ -1726,6 +1972,12 @@
1.419 { /* TST.B #imm, @(R0, GBR) */
1.420 uint32_t imm = (ir&0xFF);
1.421 + load_reg( R_EAX, 0);
1.422 + load_reg( R_ECX, R_GBR);
1.423 + ADD_r32_r32( R_EAX, R_ECX );
1.424 + MEM_READ_BYTE( R_ECX, R_EAX );
1.425 + TEST_imm8_r8( imm, R_EAX );
1.430 @@ -1960,6 +2212,7 @@