Search
lxdream.org :: lxdream/src/sh4/sh4x86.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4x86.c
changeset 386:6fb10951326a
prev381:aade6c9aca4d
next388:13bae2fb0373
author nkeynes
date Sun Sep 16 07:03:23 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Implement MAC.W, MAC.L and DIV1
Correct SHAD/SHLD
Fix privilege and slot illegal checks on LDC/STC opcodes
Fix various other small bugs
file annotate diff log raw
1.1 --- a/src/sh4/sh4x86.c Wed Sep 12 11:41:43 2007 +0000
1.2 +++ b/src/sh4/sh4x86.c Sun Sep 16 07:03:23 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: sh4x86.c,v 1.8 2007-09-12 11:41:43 nkeynes Exp $
1.6 + * $Id: sh4x86.c,v 1.9 2007-09-16 07:03:23 nkeynes Exp $
1.7 *
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.10 @@ -56,6 +56,15 @@
1.11
1.12 static struct sh4_x86_state sh4_x86;
1.13
1.14 +void signsat48( void )
1.15 +{
1.16 + if( ((int64_t)sh4r.mac) < (int64_t)0xFFFF800000000000LL )
1.17 + sh4r.mac = 0xFFFF800000000000LL;
1.18 + else if( ((int64_t)sh4r.mac) > (int64_t)0x00007FFFFFFFFFFFLL )
1.19 + sh4r.mac = 0x00007FFFFFFFFFFFLL;
1.20 +}
1.21 +
1.22 +
1.23 void sh4_x86_init()
1.24 {
1.25 sh4_x86.backpatch_list = malloc(DEFAULT_BACKPATCH_SIZE);
1.26 @@ -156,12 +165,23 @@
1.27 */
1.28 static inline void load_xf_bank( int bankreg )
1.29 {
1.30 + NOT_r32( bankreg );
1.31 SHR_imm8_r32( (21 - 6), bankreg ); // Extract bit 21 then *64 for bank size
1.32 AND_imm8s_r32( 0x40, bankreg ); // Complete extraction
1.33 OP(0x8D); OP(0x44+(bankreg<<3)); OP(0x28+bankreg); OP(REG_OFFSET(fr)); // LEA [ebp+bankreg+disp], bankreg
1.34 }
1.35
1.36 /**
1.37 + * Update the fr_bank pointer based on the current fpscr value.
1.38 + */
1.39 +static inline void update_fr_bank( int fpscrreg )
1.40 +{
1.41 + SHR_imm8_r32( (21 - 6), fpscrreg ); // Extract bit 21 then *64 for bank size
1.42 + AND_imm8s_r32( 0x40, fpscrreg ); // Complete extraction
1.43 + OP(0x8D); OP(0x44+(fpscrreg<<3)); OP(0x28+fpscrreg); OP(REG_OFFSET(fr)); // LEA [ebp+fpscrreg+disp], fpscrreg
1.44 + store_spreg( fpscrreg, REG_OFFSET(fr_bank) );
1.45 +}
1.46 +/**
1.47 * Push FPUL (as a 32-bit float) onto the FPU stack
1.48 */
1.49 static inline void push_fpul( )
1.50 @@ -242,11 +262,11 @@
1.51 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
1.52 {
1.53 ADD_imm8s_r32( 4, addr );
1.54 + PUSH_r32(arg2b);
1.55 PUSH_r32(addr);
1.56 - PUSH_r32(arg2b);
1.57 ADD_imm8s_r32( -4, addr );
1.58 + PUSH_r32(arg2a);
1.59 PUSH_r32(addr);
1.60 - PUSH_r32(arg2a);
1.61 call_func0(sh4_write_long);
1.62 ADD_imm8s_r32( 8, R_ESP );
1.63 call_func0(sh4_write_long);
1.64 @@ -324,6 +344,13 @@
1.65 JNE_exit(EXIT_DATA_ADDR_WRITE);
1.66 }
1.67
1.68 +static inline void raise_exception( int exc )
1.69 +{
1.70 + PUSH_imm32(exc);
1.71 + call_func0(sh4_raise_exception);
1.72 + ADD_imm8s_r32( 4, R_ESP );
1.73 + sh4_x86.in_delay_slot = FALSE;
1.74 +}
1.75
1.76 #define UNDEF()
1.77 #define MEM_RESULT(value_reg) if(value_reg != R_EAX) { MOV_r32_r32(R_EAX,value_reg); }
1.78 @@ -334,8 +361,8 @@
1.79 #define MEM_WRITE_WORD( addr_reg, value_reg ) call_func2(sh4_write_word, addr_reg, value_reg)
1.80 #define MEM_WRITE_LONG( addr_reg, value_reg ) call_func2(sh4_write_long, addr_reg, value_reg)
1.81
1.82 -#define RAISE_EXCEPTION( exc ) call_func1(sh4_raise_exception, exc);
1.83 -#define SLOTILLEGAL() RAISE_EXCEPTION(EXC_SLOT_ILLEGAL); return 1
1.84 +#define RAISE_EXCEPTION( exc ) raise_exception(exc); return 1;
1.85 +#define SLOTILLEGAL() JMP_exit(EXIT_SLOT_ILLEGAL); sh4_x86.in_delay_slot = FALSE; return 1;
1.86
1.87
1.88
1.89 @@ -415,6 +442,9 @@
1.90
1.91 load_imm32( R_EAX, (uint32_t)sh4_raise_exception ); // 6
1.92 CALL_r32( R_EAX ); // 2
1.93 + ADD_imm8s_r32( 4, R_ESP );
1.94 + POP_r32(R_ESI);
1.95 + POP_r32(R_EDI);
1.96 POP_r32(R_EBP);
1.97 RET();
1.98
1.99 @@ -443,6 +473,7 @@
1.100 case 0x0:
1.101 { /* STC SR, Rn */
1.102 uint32_t Rn = ((ir>>8)&0xF);
1.103 + check_priv();
1.104 call_func0(sh4_read_sr);
1.105 store_reg( R_EAX, Rn );
1.106 }
1.107 @@ -457,6 +488,7 @@
1.108 case 0x2:
1.109 { /* STC VBR, Rn */
1.110 uint32_t Rn = ((ir>>8)&0xF);
1.111 + check_priv();
1.112 load_spreg( R_EAX, R_VBR );
1.113 store_reg( R_EAX, Rn );
1.114 }
1.115 @@ -464,6 +496,7 @@
1.116 case 0x3:
1.117 { /* STC SSR, Rn */
1.118 uint32_t Rn = ((ir>>8)&0xF);
1.119 + check_priv();
1.120 load_spreg( R_EAX, R_SSR );
1.121 store_reg( R_EAX, Rn );
1.122 }
1.123 @@ -471,6 +504,7 @@
1.124 case 0x4:
1.125 { /* STC SPC, Rn */
1.126 uint32_t Rn = ((ir>>8)&0xF);
1.127 + check_priv();
1.128 load_spreg( R_EAX, R_SPC );
1.129 store_reg( R_EAX, Rn );
1.130 }
1.131 @@ -483,6 +517,7 @@
1.132 case 0x1:
1.133 { /* STC Rm_BANK, Rn */
1.134 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm_BANK = ((ir>>4)&0x7);
1.135 + check_priv();
1.136 load_spreg( R_EAX, REG_OFFSET(r_bank[Rm_BANK]) );
1.137 store_reg( R_EAX, Rn );
1.138 }
1.139 @@ -502,7 +537,6 @@
1.140 load_reg( R_EDI, Rn );
1.141 ADD_r32_r32( R_EAX, R_EDI );
1.142 sh4_x86.in_delay_slot = TRUE;
1.143 - INC_r32(R_ESI);
1.144 return 0;
1.145 }
1.146 }
1.147 @@ -514,8 +548,8 @@
1.148 SLOTILLEGAL();
1.149 } else {
1.150 load_reg( R_EDI, Rn );
1.151 + ADD_imm32_r32( pc + 4, R_EDI );
1.152 sh4_x86.in_delay_slot = TRUE;
1.153 - INC_r32(R_ESI);
1.154 return 0;
1.155 }
1.156 }
1.157 @@ -698,6 +732,7 @@
1.158 case 0x3:
1.159 { /* STC SGR, Rn */
1.160 uint32_t Rn = ((ir>>8)&0xF);
1.161 + check_priv();
1.162 load_spreg( R_EAX, R_SGR );
1.163 store_reg( R_EAX, Rn );
1.164 }
1.165 @@ -719,6 +754,7 @@
1.166 case 0xF:
1.167 { /* STC DBR, Rn */
1.168 uint32_t Rn = ((ir>>8)&0xF);
1.169 + check_priv();
1.170 load_spreg( R_EAX, R_DBR );
1.171 store_reg( R_EAX, Rn );
1.172 }
1.173 @@ -737,7 +773,6 @@
1.174 } else {
1.175 load_spreg( R_EDI, R_PR );
1.176 sh4_x86.in_delay_slot = TRUE;
1.177 - INC_r32(R_ESI);
1.178 return 0;
1.179 }
1.180 }
1.181 @@ -753,13 +788,12 @@
1.182 if( sh4_x86.in_delay_slot ) {
1.183 SLOTILLEGAL();
1.184 } else {
1.185 - load_spreg( R_EDI, R_PR );
1.186 + load_spreg( R_EDI, R_SPC );
1.187 load_spreg( R_EAX, R_SSR );
1.188 call_func1( sh4_write_sr, R_EAX );
1.189 sh4_x86.in_delay_slot = TRUE;
1.190 sh4_x86.priv_checked = FALSE;
1.191 sh4_x86.fpuen_checked = FALSE;
1.192 - INC_r32(R_ESI);
1.193 return 0;
1.194 }
1.195 }
1.196 @@ -804,6 +838,26 @@
1.197 case 0xF:
1.198 { /* MAC.L @Rm+, @Rn+ */
1.199 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.200 + load_reg( R_ECX, Rm );
1.201 + check_ralign32( R_ECX );
1.202 + load_reg( R_ECX, Rn );
1.203 + check_ralign32( R_ECX );
1.204 + ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rn]) );
1.205 + MEM_READ_LONG( R_ECX, R_EAX );
1.206 + PUSH_r32( R_EAX );
1.207 + load_reg( R_ECX, Rm );
1.208 + ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
1.209 + MEM_READ_LONG( R_ECX, R_EAX );
1.210 + POP_r32( R_ECX );
1.211 + IMUL_r32( R_ECX );
1.212 + ADD_r32_sh4r( R_EAX, R_MACL );
1.213 + ADC_r32_sh4r( R_EDX, R_MACH );
1.214 +
1.215 + load_spreg( R_ECX, R_S );
1.216 + TEST_r32_r32(R_ECX, R_ECX);
1.217 + JE_rel8( 7, nosat );
1.218 + call_func0( signsat48 );
1.219 + JMP_TARGET( nosat );
1.220 }
1.221 break;
1.222 default:
1.223 @@ -836,8 +890,8 @@
1.224 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.225 load_reg( R_ECX, Rn );
1.226 check_walign16( R_ECX );
1.227 - MEM_READ_WORD( R_ECX, R_EAX );
1.228 - store_reg( R_EAX, Rn );
1.229 + load_reg( R_EAX, Rm );
1.230 + MEM_WRITE_WORD( R_ECX, R_EAX );
1.231 }
1.232 break;
1.233 case 0x2:
1.234 @@ -854,7 +908,7 @@
1.235 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.236 load_reg( R_EAX, Rm );
1.237 load_reg( R_ECX, Rn );
1.238 - ADD_imm8s_r32( -1, Rn );
1.239 + ADD_imm8s_r32( -1, R_ECX );
1.240 store_reg( R_ECX, Rn );
1.241 MEM_WRITE_BYTE( R_ECX, R_EAX );
1.242 }
1.243 @@ -866,6 +920,7 @@
1.244 check_walign16( R_ECX );
1.245 load_reg( R_EAX, Rm );
1.246 ADD_imm8s_r32( -2, R_ECX );
1.247 + store_reg( R_ECX, Rn );
1.248 MEM_WRITE_WORD( R_ECX, R_EAX );
1.249 }
1.250 break;
1.251 @@ -884,13 +939,13 @@
1.252 { /* DIV0S Rm, Rn */
1.253 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.254 load_reg( R_EAX, Rm );
1.255 - load_reg( R_ECX, Rm );
1.256 + load_reg( R_ECX, Rn );
1.257 SHR_imm8_r32( 31, R_EAX );
1.258 SHR_imm8_r32( 31, R_ECX );
1.259 store_spreg( R_EAX, R_M );
1.260 store_spreg( R_ECX, R_Q );
1.261 CMP_r32_r32( R_EAX, R_ECX );
1.262 - SETE_t();
1.263 + SETNE_t();
1.264 }
1.265 break;
1.266 case 0x8:
1.267 @@ -1015,19 +1070,26 @@
1.268 case 0x4:
1.269 { /* DIV1 Rm, Rn */
1.270 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.271 - load_reg( R_ECX, Rn );
1.272 + load_spreg( R_ECX, R_M );
1.273 + load_reg( R_EAX, Rn );
1.274 LDC_t();
1.275 - RCL1_r32( R_ECX ); // OP2
1.276 - SETC_r32( R_EDX ); // Q
1.277 - load_spreg( R_EAX, R_Q );
1.278 - CMP_sh4r_r32( R_M, R_EAX );
1.279 - JE_rel8(8,mqequal);
1.280 - ADD_sh4r_r32( REG_OFFSET(r[Rm]), R_ECX );
1.281 - JMP_rel8(3, mqnotequal);
1.282 + RCL1_r32( R_EAX );
1.283 + SETC_r8( R_DL ); // Q'
1.284 + CMP_sh4r_r32( R_Q, R_ECX );
1.285 + JE_rel8(5, mqequal);
1.286 + ADD_sh4r_r32( REG_OFFSET(r[Rm]), R_EAX );
1.287 + JMP_rel8(3, end);
1.288 JMP_TARGET(mqequal);
1.289 - SUB_sh4r_r32( REG_OFFSET(r[Rm]), R_ECX );
1.290 - JMP_TARGET(mqnotequal);
1.291 - // TODO
1.292 + SUB_sh4r_r32( REG_OFFSET(r[Rm]), R_EAX );
1.293 + JMP_TARGET(end);
1.294 + store_reg( R_EAX, Rn ); // Done with Rn now
1.295 + SETC_r8(R_AL); // tmp1
1.296 + XOR_r8_r8( R_DL, R_AL ); // Q' = Q ^ tmp1
1.297 + XOR_r8_r8( R_AL, R_CL ); // Q'' = Q' ^ M
1.298 + store_spreg( R_ECX, R_Q );
1.299 + XOR_imm8s_r32( 1, R_AL ); // T = !Q'
1.300 + MOVZX_r8_r32( R_AL, R_EAX );
1.301 + store_spreg( R_EAX, R_T );
1.302 }
1.303 break;
1.304 case 0x5:
1.305 @@ -1148,7 +1210,7 @@
1.306 { /* DT Rn */
1.307 uint32_t Rn = ((ir>>8)&0xF);
1.308 load_reg( R_EAX, Rn );
1.309 - ADD_imm8s_r32( -1, Rn );
1.310 + ADD_imm8s_r32( -1, R_EAX );
1.311 store_reg( R_EAX, Rn );
1.312 SETE_t();
1.313 }
1.314 @@ -1203,7 +1265,7 @@
1.315 { /* STS.L MACH, @-Rn */
1.316 uint32_t Rn = ((ir>>8)&0xF);
1.317 load_reg( R_ECX, Rn );
1.318 - ADD_imm8s_r32( -4, Rn );
1.319 + ADD_imm8s_r32( -4, R_ECX );
1.320 store_reg( R_ECX, Rn );
1.321 load_spreg( R_EAX, R_MACH );
1.322 MEM_WRITE_LONG( R_ECX, R_EAX );
1.323 @@ -1213,7 +1275,7 @@
1.324 { /* STS.L MACL, @-Rn */
1.325 uint32_t Rn = ((ir>>8)&0xF);
1.326 load_reg( R_ECX, Rn );
1.327 - ADD_imm8s_r32( -4, Rn );
1.328 + ADD_imm8s_r32( -4, R_ECX );
1.329 store_reg( R_ECX, Rn );
1.330 load_spreg( R_EAX, R_MACL );
1.331 MEM_WRITE_LONG( R_ECX, R_EAX );
1.332 @@ -1223,7 +1285,7 @@
1.333 { /* STS.L PR, @-Rn */
1.334 uint32_t Rn = ((ir>>8)&0xF);
1.335 load_reg( R_ECX, Rn );
1.336 - ADD_imm8s_r32( -4, Rn );
1.337 + ADD_imm8s_r32( -4, R_ECX );
1.338 store_reg( R_ECX, Rn );
1.339 load_spreg( R_EAX, R_PR );
1.340 MEM_WRITE_LONG( R_ECX, R_EAX );
1.341 @@ -1232,8 +1294,9 @@
1.342 case 0x3:
1.343 { /* STC.L SGR, @-Rn */
1.344 uint32_t Rn = ((ir>>8)&0xF);
1.345 + check_priv();
1.346 load_reg( R_ECX, Rn );
1.347 - ADD_imm8s_r32( -4, Rn );
1.348 + ADD_imm8s_r32( -4, R_ECX );
1.349 store_reg( R_ECX, Rn );
1.350 load_spreg( R_EAX, R_SGR );
1.351 MEM_WRITE_LONG( R_ECX, R_EAX );
1.352 @@ -1243,7 +1306,7 @@
1.353 { /* STS.L FPUL, @-Rn */
1.354 uint32_t Rn = ((ir>>8)&0xF);
1.355 load_reg( R_ECX, Rn );
1.356 - ADD_imm8s_r32( -4, Rn );
1.357 + ADD_imm8s_r32( -4, R_ECX );
1.358 store_reg( R_ECX, Rn );
1.359 load_spreg( R_EAX, R_FPUL );
1.360 MEM_WRITE_LONG( R_ECX, R_EAX );
1.361 @@ -1253,7 +1316,7 @@
1.362 { /* STS.L FPSCR, @-Rn */
1.363 uint32_t Rn = ((ir>>8)&0xF);
1.364 load_reg( R_ECX, Rn );
1.365 - ADD_imm8s_r32( -4, Rn );
1.366 + ADD_imm8s_r32( -4, R_ECX );
1.367 store_reg( R_ECX, Rn );
1.368 load_spreg( R_EAX, R_FPSCR );
1.369 MEM_WRITE_LONG( R_ECX, R_EAX );
1.370 @@ -1262,8 +1325,9 @@
1.371 case 0xF:
1.372 { /* STC.L DBR, @-Rn */
1.373 uint32_t Rn = ((ir>>8)&0xF);
1.374 + check_priv();
1.375 load_reg( R_ECX, Rn );
1.376 - ADD_imm8s_r32( -4, Rn );
1.377 + ADD_imm8s_r32( -4, R_ECX );
1.378 store_reg( R_ECX, Rn );
1.379 load_spreg( R_EAX, R_DBR );
1.380 MEM_WRITE_LONG( R_ECX, R_EAX );
1.381 @@ -1281,8 +1345,9 @@
1.382 case 0x0:
1.383 { /* STC.L SR, @-Rn */
1.384 uint32_t Rn = ((ir>>8)&0xF);
1.385 + check_priv();
1.386 load_reg( R_ECX, Rn );
1.387 - ADD_imm8s_r32( -4, Rn );
1.388 + ADD_imm8s_r32( -4, R_ECX );
1.389 store_reg( R_ECX, Rn );
1.390 call_func0( sh4_read_sr );
1.391 MEM_WRITE_LONG( R_ECX, R_EAX );
1.392 @@ -1292,7 +1357,7 @@
1.393 { /* STC.L GBR, @-Rn */
1.394 uint32_t Rn = ((ir>>8)&0xF);
1.395 load_reg( R_ECX, Rn );
1.396 - ADD_imm8s_r32( -4, Rn );
1.397 + ADD_imm8s_r32( -4, R_ECX );
1.398 store_reg( R_ECX, Rn );
1.399 load_spreg( R_EAX, R_GBR );
1.400 MEM_WRITE_LONG( R_ECX, R_EAX );
1.401 @@ -1301,8 +1366,9 @@
1.402 case 0x2:
1.403 { /* STC.L VBR, @-Rn */
1.404 uint32_t Rn = ((ir>>8)&0xF);
1.405 + check_priv();
1.406 load_reg( R_ECX, Rn );
1.407 - ADD_imm8s_r32( -4, Rn );
1.408 + ADD_imm8s_r32( -4, R_ECX );
1.409 store_reg( R_ECX, Rn );
1.410 load_spreg( R_EAX, R_VBR );
1.411 MEM_WRITE_LONG( R_ECX, R_EAX );
1.412 @@ -1311,8 +1377,9 @@
1.413 case 0x3:
1.414 { /* STC.L SSR, @-Rn */
1.415 uint32_t Rn = ((ir>>8)&0xF);
1.416 + check_priv();
1.417 load_reg( R_ECX, Rn );
1.418 - ADD_imm8s_r32( -4, Rn );
1.419 + ADD_imm8s_r32( -4, R_ECX );
1.420 store_reg( R_ECX, Rn );
1.421 load_spreg( R_EAX, R_SSR );
1.422 MEM_WRITE_LONG( R_ECX, R_EAX );
1.423 @@ -1321,8 +1388,9 @@
1.424 case 0x4:
1.425 { /* STC.L SPC, @-Rn */
1.426 uint32_t Rn = ((ir>>8)&0xF);
1.427 + check_priv();
1.428 load_reg( R_ECX, Rn );
1.429 - ADD_imm8s_r32( -4, Rn );
1.430 + ADD_imm8s_r32( -4, R_ECX );
1.431 store_reg( R_ECX, Rn );
1.432 load_spreg( R_EAX, R_SPC );
1.433 MEM_WRITE_LONG( R_ECX, R_EAX );
1.434 @@ -1336,8 +1404,9 @@
1.435 case 0x1:
1.436 { /* STC.L Rm_BANK, @-Rn */
1.437 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm_BANK = ((ir>>4)&0x7);
1.438 + check_priv();
1.439 load_reg( R_ECX, Rn );
1.440 - ADD_imm8s_r32( -4, Rn );
1.441 + ADD_imm8s_r32( -4, R_ECX );
1.442 store_reg( R_ECX, Rn );
1.443 load_spreg( R_EAX, REG_OFFSET(r_bank[Rm_BANK]) );
1.444 MEM_WRITE_LONG( R_ECX, R_EAX );
1.445 @@ -1443,6 +1512,7 @@
1.446 case 0x3:
1.447 { /* LDC.L @Rm+, SGR */
1.448 uint32_t Rm = ((ir>>8)&0xF);
1.449 + check_priv();
1.450 load_reg( R_EAX, Rm );
1.451 MOV_r32_r32( R_EAX, R_ECX );
1.452 ADD_imm8s_r32( 4, R_EAX );
1.453 @@ -1471,11 +1541,13 @@
1.454 store_reg( R_EAX, Rm );
1.455 MEM_READ_LONG( R_ECX, R_EAX );
1.456 store_spreg( R_EAX, R_FPSCR );
1.457 + update_fr_bank( R_EAX );
1.458 }
1.459 break;
1.460 case 0xF:
1.461 { /* LDC.L @Rm+, DBR */
1.462 uint32_t Rm = ((ir>>8)&0xF);
1.463 + check_priv();
1.464 load_reg( R_EAX, Rm );
1.465 MOV_r32_r32( R_EAX, R_ECX );
1.466 ADD_imm8s_r32( 4, R_EAX );
1.467 @@ -1496,14 +1568,19 @@
1.468 case 0x0:
1.469 { /* LDC.L @Rm+, SR */
1.470 uint32_t Rm = ((ir>>8)&0xF);
1.471 - load_reg( R_EAX, Rm );
1.472 - MOV_r32_r32( R_EAX, R_ECX );
1.473 - ADD_imm8s_r32( 4, R_EAX );
1.474 - store_reg( R_EAX, Rm );
1.475 - MEM_READ_LONG( R_ECX, R_EAX );
1.476 - call_func1( sh4_write_sr, R_EAX );
1.477 - sh4_x86.priv_checked = FALSE;
1.478 - sh4_x86.fpuen_checked = FALSE;
1.479 + if( sh4_x86.in_delay_slot ) {
1.480 + SLOTILLEGAL();
1.481 + } else {
1.482 + check_priv();
1.483 + load_reg( R_EAX, Rm );
1.484 + MOV_r32_r32( R_EAX, R_ECX );
1.485 + ADD_imm8s_r32( 4, R_EAX );
1.486 + store_reg( R_EAX, Rm );
1.487 + MEM_READ_LONG( R_ECX, R_EAX );
1.488 + call_func1( sh4_write_sr, R_EAX );
1.489 + sh4_x86.priv_checked = FALSE;
1.490 + sh4_x86.fpuen_checked = FALSE;
1.491 + }
1.492 }
1.493 break;
1.494 case 0x1:
1.495 @@ -1520,6 +1597,7 @@
1.496 case 0x2:
1.497 { /* LDC.L @Rm+, VBR */
1.498 uint32_t Rm = ((ir>>8)&0xF);
1.499 + check_priv();
1.500 load_reg( R_EAX, Rm );
1.501 MOV_r32_r32( R_EAX, R_ECX );
1.502 ADD_imm8s_r32( 4, R_EAX );
1.503 @@ -1531,6 +1609,7 @@
1.504 case 0x3:
1.505 { /* LDC.L @Rm+, SSR */
1.506 uint32_t Rm = ((ir>>8)&0xF);
1.507 + check_priv();
1.508 load_reg( R_EAX, Rm );
1.509 MOV_r32_r32( R_EAX, R_ECX );
1.510 ADD_imm8s_r32( 4, R_EAX );
1.511 @@ -1542,6 +1621,7 @@
1.512 case 0x4:
1.513 { /* LDC.L @Rm+, SPC */
1.514 uint32_t Rm = ((ir>>8)&0xF);
1.515 + check_priv();
1.516 load_reg( R_EAX, Rm );
1.517 MOV_r32_r32( R_EAX, R_ECX );
1.518 ADD_imm8s_r32( 4, R_EAX );
1.519 @@ -1558,6 +1638,7 @@
1.520 case 0x1:
1.521 { /* LDC.L @Rm+, Rn_BANK */
1.522 uint32_t Rm = ((ir>>8)&0xF); uint32_t Rn_BANK = ((ir>>4)&0x7);
1.523 + check_priv();
1.524 load_reg( R_EAX, Rm );
1.525 MOV_r32_r32( R_EAX, R_ECX );
1.526 ADD_imm8s_r32( 4, R_EAX );
1.527 @@ -1656,6 +1737,7 @@
1.528 case 0x3:
1.529 { /* LDC Rm, SGR */
1.530 uint32_t Rm = ((ir>>8)&0xF);
1.531 + check_priv();
1.532 load_reg( R_EAX, Rm );
1.533 store_spreg( R_EAX, R_SGR );
1.534 }
1.535 @@ -1672,11 +1754,13 @@
1.536 uint32_t Rm = ((ir>>8)&0xF);
1.537 load_reg( R_EAX, Rm );
1.538 store_spreg( R_EAX, R_FPSCR );
1.539 + update_fr_bank( R_EAX );
1.540 }
1.541 break;
1.542 case 0xF:
1.543 { /* LDC Rm, DBR */
1.544 uint32_t Rm = ((ir>>8)&0xF);
1.545 + check_priv();
1.546 load_reg( R_EAX, Rm );
1.547 store_spreg( R_EAX, R_DBR );
1.548 }
1.549 @@ -1698,7 +1782,6 @@
1.550 store_spreg( R_EAX, R_PR );
1.551 load_reg( R_EDI, Rn );
1.552 sh4_x86.in_delay_slot = TRUE;
1.553 - INC_r32(R_ESI);
1.554 return 0;
1.555 }
1.556 }
1.557 @@ -1711,6 +1794,7 @@
1.558 TEST_r8_r8( R_AL, R_AL );
1.559 SETE_t();
1.560 OR_imm8_r8( 0x80, R_AL );
1.561 + load_reg( R_ECX, Rn );
1.562 MEM_WRITE_BYTE( R_ECX, R_EAX );
1.563 }
1.564 break;
1.565 @@ -1722,7 +1806,6 @@
1.566 } else {
1.567 load_reg( R_EDI, Rn );
1.568 sh4_x86.in_delay_slot = TRUE;
1.569 - INC_r32(R_ESI);
1.570 return 0;
1.571 }
1.572 }
1.573 @@ -1739,16 +1822,23 @@
1.574 load_reg( R_EAX, Rn );
1.575 load_reg( R_ECX, Rm );
1.576 CMP_imm32_r32( 0, R_ECX );
1.577 - JAE_rel8(9, doshl);
1.578 + JGE_rel8(16, doshl);
1.579
1.580 NEG_r32( R_ECX ); // 2
1.581 AND_imm8_r8( 0x1F, R_CL ); // 3
1.582 + JE_rel8( 4, emptysar); // 2
1.583 SAR_r32_CL( R_EAX ); // 2
1.584 - JMP_rel8(5, end); // 2
1.585 + JMP_rel8(10, end); // 2
1.586 +
1.587 + JMP_TARGET(emptysar);
1.588 + SAR_imm8_r32(31, R_EAX ); // 3
1.589 + JMP_rel8(5, end2);
1.590 +
1.591 JMP_TARGET(doshl);
1.592 AND_imm8_r8( 0x1F, R_CL ); // 3
1.593 SHL_r32_CL( R_EAX ); // 2
1.594 JMP_TARGET(end);
1.595 + JMP_TARGET(end2);
1.596 store_reg( R_EAX, Rn );
1.597 }
1.598 break;
1.599 @@ -1757,13 +1847,24 @@
1.600 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.601 load_reg( R_EAX, Rn );
1.602 load_reg( R_ECX, Rm );
1.603 + CMP_imm32_r32( 0, R_ECX );
1.604 + JGE_rel8(15, doshl);
1.605
1.606 - MOV_r32_r32( R_EAX, R_EDX );
1.607 - SHL_r32_CL( R_EAX );
1.608 - NEG_r32( R_ECX );
1.609 - SHR_r32_CL( R_EDX );
1.610 - CMP_imm8s_r32( 0, R_ECX );
1.611 - CMOVAE_r32_r32( R_EDX, R_EAX );
1.612 + NEG_r32( R_ECX ); // 2
1.613 + AND_imm8_r8( 0x1F, R_CL ); // 3
1.614 + JE_rel8( 4, emptyshr );
1.615 + SHR_r32_CL( R_EAX ); // 2
1.616 + JMP_rel8(9, end); // 2
1.617 +
1.618 + JMP_TARGET(emptyshr);
1.619 + XOR_r32_r32( R_EAX, R_EAX );
1.620 + JMP_rel8(5, end2);
1.621 +
1.622 + JMP_TARGET(doshl);
1.623 + AND_imm8_r8( 0x1F, R_CL ); // 3
1.624 + SHL_r32_CL( R_EAX ); // 2
1.625 + JMP_TARGET(end);
1.626 + JMP_TARGET(end2);
1.627 store_reg( R_EAX, Rn );
1.628 }
1.629 break;
1.630 @@ -1774,10 +1875,15 @@
1.631 case 0x0:
1.632 { /* LDC Rm, SR */
1.633 uint32_t Rm = ((ir>>8)&0xF);
1.634 - load_reg( R_EAX, Rm );
1.635 - call_func1( sh4_write_sr, R_EAX );
1.636 - sh4_x86.priv_checked = FALSE;
1.637 - sh4_x86.fpuen_checked = FALSE;
1.638 + if( sh4_x86.in_delay_slot ) {
1.639 + SLOTILLEGAL();
1.640 + } else {
1.641 + check_priv();
1.642 + load_reg( R_EAX, Rm );
1.643 + call_func1( sh4_write_sr, R_EAX );
1.644 + sh4_x86.priv_checked = FALSE;
1.645 + sh4_x86.fpuen_checked = FALSE;
1.646 + }
1.647 }
1.648 break;
1.649 case 0x1:
1.650 @@ -1790,6 +1896,7 @@
1.651 case 0x2:
1.652 { /* LDC Rm, VBR */
1.653 uint32_t Rm = ((ir>>8)&0xF);
1.654 + check_priv();
1.655 load_reg( R_EAX, Rm );
1.656 store_spreg( R_EAX, R_VBR );
1.657 }
1.658 @@ -1797,6 +1904,7 @@
1.659 case 0x3:
1.660 { /* LDC Rm, SSR */
1.661 uint32_t Rm = ((ir>>8)&0xF);
1.662 + check_priv();
1.663 load_reg( R_EAX, Rm );
1.664 store_spreg( R_EAX, R_SSR );
1.665 }
1.666 @@ -1804,6 +1912,7 @@
1.667 case 0x4:
1.668 { /* LDC Rm, SPC */
1.669 uint32_t Rm = ((ir>>8)&0xF);
1.670 + check_priv();
1.671 load_reg( R_EAX, Rm );
1.672 store_spreg( R_EAX, R_SPC );
1.673 }
1.674 @@ -1816,6 +1925,7 @@
1.675 case 0x1:
1.676 { /* LDC Rm, Rn_BANK */
1.677 uint32_t Rm = ((ir>>8)&0xF); uint32_t Rn_BANK = ((ir>>4)&0x7);
1.678 + check_priv();
1.679 load_reg( R_EAX, Rm );
1.680 store_spreg( R_EAX, REG_OFFSET(r_bank[Rn_BANK]) );
1.681 }
1.682 @@ -1825,6 +1935,43 @@
1.683 case 0xF:
1.684 { /* MAC.W @Rm+, @Rn+ */
1.685 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.686 + load_reg( R_ECX, Rm );
1.687 + check_ralign16( R_ECX );
1.688 + load_reg( R_ECX, Rn );
1.689 + check_ralign16( R_ECX );
1.690 + ADD_imm8s_sh4r( 2, REG_OFFSET(r[Rn]) );
1.691 + MEM_READ_WORD( R_ECX, R_EAX );
1.692 + PUSH_r32( R_EAX );
1.693 + load_reg( R_ECX, Rm );
1.694 + ADD_imm8s_sh4r( 2, REG_OFFSET(r[Rm]) );
1.695 + MEM_READ_WORD( R_ECX, R_EAX );
1.696 + POP_r32( R_ECX );
1.697 + IMUL_r32( R_ECX );
1.698 +
1.699 + load_spreg( R_ECX, R_S );
1.700 + TEST_r32_r32( R_ECX, R_ECX );
1.701 + JE_rel8( 47, nosat );
1.702 +
1.703 + ADD_r32_sh4r( R_EAX, R_MACL ); // 6
1.704 + JNO_rel8( 51, end ); // 2
1.705 + load_imm32( R_EDX, 1 ); // 5
1.706 + store_spreg( R_EDX, R_MACH ); // 6
1.707 + JS_rel8( 13, positive ); // 2
1.708 + load_imm32( R_EAX, 0x80000000 );// 5
1.709 + store_spreg( R_EAX, R_MACL ); // 6
1.710 + JMP_rel8( 25, end2 ); // 2
1.711 +
1.712 + JMP_TARGET(positive);
1.713 + load_imm32( R_EAX, 0x7FFFFFFF );// 5
1.714 + store_spreg( R_EAX, R_MACL ); // 6
1.715 + JMP_rel8( 12, end3); // 2
1.716 +
1.717 + JMP_TARGET(nosat);
1.718 + ADD_r32_sh4r( R_EAX, R_MACL ); // 6
1.719 + ADC_r32_sh4r( R_EDX, R_MACH ); // 6
1.720 + JMP_TARGET(end);
1.721 + JMP_TARGET(end2);
1.722 + JMP_TARGET(end3);
1.723 }
1.724 break;
1.725 }
1.726 @@ -1846,7 +1993,7 @@
1.727 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.728 load_reg( R_ECX, Rm );
1.729 MEM_READ_BYTE( R_ECX, R_EAX );
1.730 - store_reg( R_ECX, Rn );
1.731 + store_reg( R_EAX, Rn );
1.732 }
1.733 break;
1.734 case 0x1:
1.735 @@ -1901,7 +2048,7 @@
1.736 { /* MOV.L @Rm+, Rn */
1.737 uint32_t Rn = ((ir>>8)&0xF); uint32_t Rm = ((ir>>4)&0xF);
1.738 load_reg( R_EAX, Rm );
1.739 - check_ralign32( R_ECX );
1.740 + check_ralign32( R_EAX );
1.741 MOV_r32_r32( R_EAX, R_ECX );
1.742 ADD_imm8s_r32( 4, R_EAX );
1.743 store_reg( R_EAX, Rm );
1.744 @@ -2083,13 +2230,12 @@
1.745 if( sh4_x86.in_delay_slot ) {
1.746 SLOTILLEGAL();
1.747 } else {
1.748 - load_imm32( R_EDI, pc + 2 );
1.749 + load_imm32( R_EDI, pc + 4 );
1.750 CMP_imm8s_sh4r( 0, R_T );
1.751 JE_rel8( 5, nottaken );
1.752 load_imm32( R_EDI, disp + pc + 4 );
1.753 JMP_TARGET(nottaken);
1.754 sh4_x86.in_delay_slot = TRUE;
1.755 - INC_r32(R_ESI);
1.756 return 0;
1.757 }
1.758 }
1.759 @@ -2100,13 +2246,12 @@
1.760 if( sh4_x86.in_delay_slot ) {
1.761 SLOTILLEGAL();
1.762 } else {
1.763 - load_imm32( R_EDI, pc + 2 );
1.764 + load_imm32( R_EDI, pc + 4 );
1.765 CMP_imm8s_sh4r( 0, R_T );
1.766 JNE_rel8( 5, nottaken );
1.767 load_imm32( R_EDI, disp + pc + 4 );
1.768 JMP_TARGET(nottaken);
1.769 sh4_x86.in_delay_slot = TRUE;
1.770 - INC_r32(R_ESI);
1.771 return 0;
1.772 }
1.773 }
1.774 @@ -2136,7 +2281,6 @@
1.775 } else {
1.776 load_imm32( R_EDI, disp + pc + 4 );
1.777 sh4_x86.in_delay_slot = TRUE;
1.778 - INC_r32(R_ESI);
1.779 return 0;
1.780 }
1.781 }
1.782 @@ -2151,7 +2295,6 @@
1.783 store_spreg( R_EAX, R_PR );
1.784 load_imm32( R_EDI, disp + pc + 4 );
1.785 sh4_x86.in_delay_slot = TRUE;
1.786 - INC_r32(R_ESI);
1.787 return 0;
1.788 }
1.789 }
1.790 @@ -2287,8 +2430,10 @@
1.791 load_reg( R_EAX, 0 );
1.792 load_spreg( R_ECX, R_GBR );
1.793 ADD_r32_r32( R_EAX, R_ECX );
1.794 - MEM_READ_BYTE( R_ECX, R_EAX );
1.795 - AND_imm32_r32(imm, R_ECX );
1.796 + PUSH_r32(R_ECX);
1.797 + call_func0(sh4_read_byte);
1.798 + POP_r32(R_ECX);
1.799 + AND_imm32_r32(imm, R_EAX );
1.800 MEM_WRITE_BYTE( R_ECX, R_EAX );
1.801 }
1.802 break;
1.803 @@ -2298,7 +2443,9 @@
1.804 load_reg( R_EAX, 0 );
1.805 load_spreg( R_ECX, R_GBR );
1.806 ADD_r32_r32( R_EAX, R_ECX );
1.807 - MEM_READ_BYTE( R_ECX, R_EAX );
1.808 + PUSH_r32(R_ECX);
1.809 + call_func0(sh4_read_byte);
1.810 + POP_r32(R_ECX);
1.811 XOR_imm32_r32( imm, R_EAX );
1.812 MEM_WRITE_BYTE( R_ECX, R_EAX );
1.813 }
1.814 @@ -2309,8 +2456,10 @@
1.815 load_reg( R_EAX, 0 );
1.816 load_spreg( R_ECX, R_GBR );
1.817 ADD_r32_r32( R_EAX, R_ECX );
1.818 - MEM_READ_BYTE( R_ECX, R_EAX );
1.819 - OR_imm32_r32(imm, R_ECX );
1.820 + PUSH_r32(R_ECX);
1.821 + call_func0(sh4_read_byte);
1.822 + POP_r32(R_ECX);
1.823 + OR_imm32_r32(imm, R_EAX );
1.824 MEM_WRITE_BYTE( R_ECX, R_EAX );
1.825 }
1.826 break;
1.827 @@ -2324,7 +2473,7 @@
1.828 } else {
1.829 load_imm32( R_ECX, (pc & 0xFFFFFFFC) + disp + 4 );
1.830 MEM_READ_LONG( R_ECX, R_EAX );
1.831 - store_reg( R_EAX, 0 );
1.832 + store_reg( R_EAX, Rn );
1.833 }
1.834 }
1.835 break;
1.836 @@ -2435,10 +2584,10 @@
1.837 JMP_TARGET(doubleprec);
1.838 push_dr(R_EDX, FRm);
1.839 push_dr(R_EDX, FRn);
1.840 + JMP_TARGET(end);
1.841 FCOMIP_st(1);
1.842 SETE_t();
1.843 FPOP_st();
1.844 - JMP_TARGET(end);
1.845 }
1.846 break;
1.847 case 0x5:
1.848 @@ -2475,7 +2624,7 @@
1.849 load_fr_bank( R_ECX );
1.850 store_fr( R_ECX, R_EAX, FRn );
1.851 if( FRn&1 ) {
1.852 - JMP_rel8(46, end);
1.853 + JMP_rel8(48, end);
1.854 JMP_TARGET(doublesize);
1.855 MEM_READ_DOUBLE( R_EDX, R_EAX, R_EDX );
1.856 load_spreg( R_ECX, R_FPSCR ); // assume read_long clobbered it
1.857 @@ -2508,7 +2657,7 @@
1.858 load_fr( R_ECX, R_EAX, FRm );
1.859 MEM_WRITE_LONG( R_EDX, R_EAX ); // 12
1.860 if( FRm&1 ) {
1.861 - JMP_rel8( 46, end );
1.862 + JMP_rel8( 48, end );
1.863 JMP_TARGET(doublesize);
1.864 load_xf_bank( R_ECX );
1.865 load_fr( R_ECX, R_EAX, FRm&0x0E );
1.866 @@ -2539,7 +2688,7 @@
1.867 load_fr_bank( R_ECX );
1.868 store_fr( R_ECX, R_EAX, FRn );
1.869 if( FRn&1 ) {
1.870 - JMP_rel8(46, end);
1.871 + JMP_rel8(48, end);
1.872 JMP_TARGET(doublesize);
1.873 MEM_READ_DOUBLE( R_EDX, R_EAX, R_EDX );
1.874 load_spreg( R_ECX, R_FPSCR ); // assume read_long clobbered it
1.875 @@ -2574,7 +2723,7 @@
1.876 load_fr_bank( R_ECX );
1.877 store_fr( R_ECX, R_EAX, FRn );
1.878 if( FRn&1 ) {
1.879 - JMP_rel8(52, end);
1.880 + JMP_rel8(54, end);
1.881 JMP_TARGET(doublesize);
1.882 ADD_imm8s_r32( 8, R_EAX );
1.883 store_reg(R_EAX, Rm);
1.884 @@ -2609,7 +2758,7 @@
1.885 load_fr( R_ECX, R_EAX, FRm );
1.886 MEM_WRITE_LONG( R_EDX, R_EAX ); // 12
1.887 if( FRm&1 ) {
1.888 - JMP_rel8( 46, end );
1.889 + JMP_rel8( 48, end );
1.890 JMP_TARGET(doublesize);
1.891 load_xf_bank( R_ECX );
1.892 load_fr( R_ECX, R_EAX, FRm&0x0E );
1.893 @@ -2635,14 +2784,14 @@
1.894 check_walign32( R_EDX );
1.895 load_spreg( R_ECX, R_FPSCR );
1.896 TEST_imm32_r32( FPSCR_SZ, R_ECX );
1.897 - JNE_rel8(20, doublesize);
1.898 + JNE_rel8(26, doublesize);
1.899 load_fr_bank( R_ECX );
1.900 load_fr( R_ECX, R_EAX, FRm );
1.901 ADD_imm8s_r32(-4,R_EDX);
1.902 store_reg( R_EDX, Rn );
1.903 MEM_WRITE_LONG( R_EDX, R_EAX ); // 12
1.904 if( FRm&1 ) {
1.905 - JMP_rel8( 46, end );
1.906 + JMP_rel8( 54, end );
1.907 JMP_TARGET(doublesize);
1.908 load_xf_bank( R_ECX );
1.909 load_fr( R_ECX, R_EAX, FRm&0x0E );
1.910 @@ -2652,7 +2801,7 @@
1.911 MEM_WRITE_DOUBLE( R_EDX, R_EAX, R_ECX );
1.912 JMP_TARGET(end);
1.913 } else {
1.914 - JMP_rel8( 39, end );
1.915 + JMP_rel8( 45, end );
1.916 JMP_TARGET(doublesize);
1.917 load_fr_bank( R_ECX );
1.918 load_fr( R_ECX, R_EAX, FRm&0x0E );
1.919 @@ -2682,7 +2831,7 @@
1.920 load_fr( R_EDX, R_EAX, FRm ); // PR=0 branch
1.921 store_fr( R_EDX, R_EAX, FRn );
1.922 if( FRm&1 ) {
1.923 - JMP_rel8(22, end);
1.924 + JMP_rel8(24, end);
1.925 JMP_TARGET(doublesize);
1.926 load_xf_bank( R_ECX );
1.927 load_fr( R_ECX, R_EAX, FRm-1 );
1.928 @@ -2698,7 +2847,7 @@
1.929 JMP_TARGET(end);
1.930 } else /* FRm&1 == 0 */ {
1.931 if( FRn&1 ) {
1.932 - JMP_rel8(22, end);
1.933 + JMP_rel8(24, end);
1.934 load_xf_bank( R_ECX );
1.935 load_fr( R_EDX, R_EAX, FRm );
1.936 load_fr( R_EDX, R_EDX, FRm+1 );
1.937 @@ -2925,16 +3074,17 @@
1.938 load_spreg( R_ECX, R_FPSCR );
1.939 XOR_imm32_r32( FPSCR_FR, R_ECX );
1.940 store_spreg( R_ECX, R_FPSCR );
1.941 + update_fr_bank( R_ECX );
1.942 }
1.943 break;
1.944 case 0x3:
1.945 { /* UNDEF */
1.946 if( sh4_x86.in_delay_slot ) {
1.947 - RAISE_EXCEPTION(EXC_SLOT_ILLEGAL);
1.948 + SLOTILLEGAL();
1.949 } else {
1.950 - RAISE_EXCEPTION(EXC_ILLEGAL);
1.951 + JMP_exit(EXIT_ILLEGAL);
1.952 + return 1;
1.953 }
1.954 - return 1;
1.955 }
1.956 break;
1.957 default:
1.958 @@ -2983,10 +3133,12 @@
1.959 break;
1.960 }
1.961
1.962 - INC_r32(R_ESI);
1.963 if( sh4_x86.in_delay_slot ) {
1.964 + ADD_imm8s_r32(2,R_ESI);
1.965 sh4_x86.in_delay_slot = FALSE;
1.966 return 1;
1.967 + } else {
1.968 + INC_r32(R_ESI);
1.969 }
1.970 return 0;
1.971 }
.