Search
lxdream.org :: lxdream/src/sh4/sh4x86.in
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4x86.in
changeset 875:2147174fb320
prev872:a3041742bb7d
next879:a07af43e03c4
author nkeynes
date Tue Oct 14 08:44:37 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix a few more subtle flag problems
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * SH4 => x86 translation. This version does no real optimization, it just
     5  * outputs straight-line x86 code - it mainly exists to provide a baseline
     6  * to test the optimizing versions against.
     7  *
     8  * Copyright (c) 2007 Nathan Keynes.
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License as published by
    12  * the Free Software Foundation; either version 2 of the License, or
    13  * (at your option) any later version.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  */
    21 #include <assert.h>
    22 #include <math.h>
    24 #ifndef NDEBUG
    25 #define DEBUG_JUMPS 1
    26 #endif
    28 #include "sh4/xltcache.h"
    29 #include "sh4/sh4core.h"
    30 #include "sh4/sh4trans.h"
    31 #include "sh4/sh4stat.h"
    32 #include "sh4/sh4mmio.h"
    33 #include "sh4/x86op.h"
    34 #include "clock.h"
    36 #define DEFAULT_BACKPATCH_SIZE 4096
    38 struct backpatch_record {
    39     uint32_t fixup_offset;
    40     uint32_t fixup_icount;
    41     int32_t exc_code;
    42 };
    44 #define DELAY_NONE 0
    45 #define DELAY_PC 1
    46 #define DELAY_PC_PR 2
    48 /** 
    49  * Struct to manage internal translation state. This state is not saved -
    50  * it is only valid between calls to sh4_translate_begin_block() and
    51  * sh4_translate_end_block()
    52  */
    53 struct sh4_x86_state {
    54     int in_delay_slot;
    55     gboolean priv_checked; /* true if we've already checked the cpu mode. */
    56     gboolean fpuen_checked; /* true if we've already checked fpu enabled. */
    57     gboolean branch_taken; /* true if we branched unconditionally */
    58     uint32_t block_start_pc;
    59     uint32_t stack_posn;   /* Trace stack height for alignment purposes */
    60     int tstate;
    62     /* mode flags */
    63     gboolean tlb_on; /* True if tlb translation is active */
    65     /* Allocated memory for the (block-wide) back-patch list */
    66     struct backpatch_record *backpatch_list;
    67     uint32_t backpatch_posn;
    68     uint32_t backpatch_size;
    69 };
    71 #define TSTATE_NONE -1
    72 #define TSTATE_O    0
    73 #define TSTATE_C    2
    74 #define TSTATE_E    4
    75 #define TSTATE_NE   5
    76 #define TSTATE_G    0xF
    77 #define TSTATE_GE   0xD
    78 #define TSTATE_A    7
    79 #define TSTATE_AE   3
    81 #ifdef ENABLE_SH4STATS
    82 #define COUNT_INST(id) load_imm32(R_EAX,id); call_func1(sh4_stats_add, R_EAX); sh4_x86.tstate = TSTATE_NONE
    83 #else
    84 #define COUNT_INST(id)
    85 #endif
    87 /** Branch if T is set (either in the current cflags, or in sh4r.t) */
    88 #define JT_rel8(label) if( sh4_x86.tstate == TSTATE_NONE ) { \
    89 	CMP_imm8s_sh4r( 1, R_T ); sh4_x86.tstate = TSTATE_E; } \
    90     OP(0x70+sh4_x86.tstate); MARK_JMP8(label); OP(-1)
    92 /** Branch if T is clear (either in the current cflags or in sh4r.t) */
    93 #define JF_rel8(label) if( sh4_x86.tstate == TSTATE_NONE ) { \
    94 	CMP_imm8s_sh4r( 1, R_T ); sh4_x86.tstate = TSTATE_E; } \
    95     OP(0x70+ (sh4_x86.tstate^1)); MARK_JMP8(label); OP(-1)
    97 static struct sh4_x86_state sh4_x86;
    99 static uint32_t max_int = 0x7FFFFFFF;
   100 static uint32_t min_int = 0x80000000;
   101 static uint32_t save_fcw; /* save value for fpu control word */
   102 static uint32_t trunc_fcw = 0x0F7F; /* fcw value for truncation mode */
   104 void sh4_translate_init(void)
   105 {
   106     sh4_x86.backpatch_list = malloc(DEFAULT_BACKPATCH_SIZE);
   107     sh4_x86.backpatch_size = DEFAULT_BACKPATCH_SIZE / sizeof(struct backpatch_record);
   108 }
   111 static void sh4_x86_add_backpatch( uint8_t *fixup_addr, uint32_t fixup_pc, uint32_t exc_code )
   112 {
   113     if( sh4_x86.backpatch_posn == sh4_x86.backpatch_size ) {
   114 	sh4_x86.backpatch_size <<= 1;
   115 	sh4_x86.backpatch_list = realloc( sh4_x86.backpatch_list, 
   116 					  sh4_x86.backpatch_size * sizeof(struct backpatch_record));
   117 	assert( sh4_x86.backpatch_list != NULL );
   118     }
   119     if( sh4_x86.in_delay_slot ) {
   120 	fixup_pc -= 2;
   121     }
   122     sh4_x86.backpatch_list[sh4_x86.backpatch_posn].fixup_offset = 
   123 	((uint8_t *)fixup_addr) - ((uint8_t *)xlat_current_block->code);
   124     sh4_x86.backpatch_list[sh4_x86.backpatch_posn].fixup_icount = (fixup_pc - sh4_x86.block_start_pc)>>1;
   125     sh4_x86.backpatch_list[sh4_x86.backpatch_posn].exc_code = exc_code;
   126     sh4_x86.backpatch_posn++;
   127 }
   129 /**
   130  * Emit an instruction to load an SH4 reg into a real register
   131  */
   132 static inline void load_reg( int x86reg, int sh4reg ) 
   133 {
   134     /* mov [bp+n], reg */
   135     OP(0x8B);
   136     OP(0x45 + (x86reg<<3));
   137     OP(REG_OFFSET(r[sh4reg]));
   138 }
   140 static inline void load_reg16s( int x86reg, int sh4reg )
   141 {
   142     OP(0x0F);
   143     OP(0xBF);
   144     MODRM_r32_sh4r(x86reg, REG_OFFSET(r[sh4reg]));
   145 }
   147 static inline void load_reg16u( int x86reg, int sh4reg )
   148 {
   149     OP(0x0F);
   150     OP(0xB7);
   151     MODRM_r32_sh4r(x86reg, REG_OFFSET(r[sh4reg]));
   153 }
   155 #define load_spreg( x86reg, regoff ) MOV_sh4r_r32( regoff, x86reg )
   156 #define store_spreg( x86reg, regoff ) MOV_r32_sh4r( x86reg, regoff )
   157 /**
   158  * Emit an instruction to load an immediate value into a register
   159  */
   160 static inline void load_imm32( int x86reg, uint32_t value ) {
   161     /* mov #value, reg */
   162     OP(0xB8 + x86reg);
   163     OP32(value);
   164 }
   166 /**
   167  * Load an immediate 64-bit quantity (note: x86-64 only)
   168  */
   169 static inline void load_imm64( int x86reg, uint64_t value ) {
   170     /* mov #value, reg */
   171     REXW();
   172     OP(0xB8 + x86reg);
   173     OP64(value);
   174 }
   176 /**
   177  * Emit an instruction to store an SH4 reg (RN)
   178  */
   179 void static inline store_reg( int x86reg, int sh4reg ) {
   180     /* mov reg, [bp+n] */
   181     OP(0x89);
   182     OP(0x45 + (x86reg<<3));
   183     OP(REG_OFFSET(r[sh4reg]));
   184 }
   186 /**
   187  * Load an FR register (single-precision floating point) into an integer x86
   188  * register (eg for register-to-register moves)
   189  */
   190 #define load_fr(reg,frm)  OP(0x8B); MODRM_r32_ebp32(reg, REG_OFFSET(fr[0][(frm)^1]) )
   191 #define load_xf(reg,frm)  OP(0x8B); MODRM_r32_ebp32(reg, REG_OFFSET(fr[1][(frm)^1]) )
   193 /**
   194  * Load the low half of a DR register (DR or XD) into an integer x86 register 
   195  */
   196 #define load_dr0(reg,frm) OP(0x8B); MODRM_r32_ebp32(reg, REG_OFFSET(fr[frm&1][frm|0x01]) )
   197 #define load_dr1(reg,frm) OP(0x8B); MODRM_r32_ebp32(reg, REG_OFFSET(fr[frm&1][frm&0x0E]) )
   199 /**
   200  * Store an FR register (single-precision floating point) from an integer x86+
   201  * register (eg for register-to-register moves)
   202  */
   203 #define store_fr(reg,frm) OP(0x89); MODRM_r32_ebp32( reg, REG_OFFSET(fr[0][(frm)^1]) )
   204 #define store_xf(reg,frm) OP(0x89); MODRM_r32_ebp32( reg, REG_OFFSET(fr[1][(frm)^1]) )
   206 #define store_dr0(reg,frm) OP(0x89); MODRM_r32_ebp32( reg, REG_OFFSET(fr[frm&1][frm|0x01]) )
   207 #define store_dr1(reg,frm) OP(0x89); MODRM_r32_ebp32( reg, REG_OFFSET(fr[frm&1][frm&0x0E]) )
   210 #define push_fpul()  FLDF_sh4r(R_FPUL)
   211 #define pop_fpul()   FSTPF_sh4r(R_FPUL)
   212 #define push_fr(frm) FLDF_sh4r( REG_OFFSET(fr[0][(frm)^1]) )
   213 #define pop_fr(frm)  FSTPF_sh4r( REG_OFFSET(fr[0][(frm)^1]) )
   214 #define push_xf(frm) FLDF_sh4r( REG_OFFSET(fr[1][(frm)^1]) )
   215 #define pop_xf(frm)  FSTPF_sh4r( REG_OFFSET(fr[1][(frm)^1]) )
   216 #define push_dr(frm) FLDD_sh4r( REG_OFFSET(fr[0][(frm)&0x0E]) )
   217 #define pop_dr(frm)  FSTPD_sh4r( REG_OFFSET(fr[0][(frm)&0x0E]) )
   218 #define push_xdr(frm) FLDD_sh4r( REG_OFFSET(fr[1][(frm)&0x0E]) )
   219 #define pop_xdr(frm)  FSTPD_sh4r( REG_OFFSET(fr[1][(frm)&0x0E]) )
   223 /* Exception checks - Note that all exception checks will clobber EAX */
   225 #define check_priv( ) \
   226     if( !sh4_x86.priv_checked ) { \
   227 	sh4_x86.priv_checked = TRUE;\
   228 	load_spreg( R_EAX, R_SR );\
   229 	AND_imm32_r32( SR_MD, R_EAX );\
   230 	if( sh4_x86.in_delay_slot ) {\
   231 	    JE_exc( EXC_SLOT_ILLEGAL );\
   232 	} else {\
   233 	    JE_exc( EXC_ILLEGAL );\
   234 	}\
   235 	sh4_x86.tstate = TSTATE_NONE; \
   236     }\
   238 #define check_fpuen( ) \
   239     if( !sh4_x86.fpuen_checked ) {\
   240 	sh4_x86.fpuen_checked = TRUE;\
   241 	load_spreg( R_EAX, R_SR );\
   242 	AND_imm32_r32( SR_FD, R_EAX );\
   243 	if( sh4_x86.in_delay_slot ) {\
   244 	    JNE_exc(EXC_SLOT_FPU_DISABLED);\
   245 	} else {\
   246 	    JNE_exc(EXC_FPU_DISABLED);\
   247 	}\
   248 	sh4_x86.tstate = TSTATE_NONE; \
   249     }
   251 #define check_ralign16( x86reg ) \
   252     TEST_imm32_r32( 0x00000001, x86reg ); \
   253     JNE_exc(EXC_DATA_ADDR_READ)
   255 #define check_walign16( x86reg ) \
   256     TEST_imm32_r32( 0x00000001, x86reg ); \
   257     JNE_exc(EXC_DATA_ADDR_WRITE);
   259 #define check_ralign32( x86reg ) \
   260     TEST_imm32_r32( 0x00000003, x86reg ); \
   261     JNE_exc(EXC_DATA_ADDR_READ)
   263 #define check_walign32( x86reg ) \
   264     TEST_imm32_r32( 0x00000003, x86reg ); \
   265     JNE_exc(EXC_DATA_ADDR_WRITE);
   267 #define check_ralign64( x86reg ) \
   268     TEST_imm32_r32( 0x00000007, x86reg ); \
   269     JNE_exc(EXC_DATA_ADDR_READ)
   271 #define check_walign64( x86reg ) \
   272     TEST_imm32_r32( 0x00000007, x86reg ); \
   273     JNE_exc(EXC_DATA_ADDR_WRITE);
   275 #define UNDEF(ir)
   276 #define MEM_RESULT(value_reg) if(value_reg != R_EAX) { MOV_r32_r32(R_EAX,value_reg); }
   277 #define MEM_READ_BYTE( addr_reg, value_reg ) call_func1(sh4_read_byte, addr_reg ); MEM_RESULT(value_reg)
   278 #define MEM_READ_WORD( addr_reg, value_reg ) call_func1(sh4_read_word, addr_reg ); MEM_RESULT(value_reg)
   279 #define MEM_READ_LONG( addr_reg, value_reg ) call_func1(sh4_read_long, addr_reg ); MEM_RESULT(value_reg)
   280 #define MEM_WRITE_BYTE( addr_reg, value_reg ) call_func2(sh4_write_byte, addr_reg, value_reg)
   281 #define MEM_WRITE_WORD( addr_reg, value_reg ) call_func2(sh4_write_word, addr_reg, value_reg)
   282 #define MEM_WRITE_LONG( addr_reg, value_reg ) call_func2(sh4_write_long, addr_reg, value_reg)
   284 /**
   285  * Perform MMU translation on the address in addr_reg for a read operation, iff the TLB is turned 
   286  * on, otherwise do nothing. Clobbers EAX, ECX and EDX. May raise a TLB exception or address error.
   287  */
   288 #define MMU_TRANSLATE_READ( addr_reg ) if( sh4_x86.tlb_on ) { call_func1(mmu_vma_to_phys_read, addr_reg); CMP_imm32_r32(MMU_VMA_ERROR, R_EAX); JE_exc(-1); MEM_RESULT(addr_reg); }
   290 #define MMU_TRANSLATE_READ_EXC( addr_reg, exc_code ) if( sh4_x86.tlb_on ) { call_func1(mmu_vma_to_phys_read, addr_reg); CMP_imm32_r32(MMU_VMA_ERROR, R_EAX); JE_exc(exc_code); MEM_RESULT(addr_reg) }
   291 /**
   292  * Perform MMU translation on the address in addr_reg for a write operation, iff the TLB is turned 
   293  * on, otherwise do nothing. Clobbers EAX, ECX and EDX. May raise a TLB exception or address error.
   294  */
   295 #define MMU_TRANSLATE_WRITE( addr_reg ) if( sh4_x86.tlb_on ) { call_func1(mmu_vma_to_phys_write, addr_reg); CMP_imm32_r32(MMU_VMA_ERROR, R_EAX); JE_exc(-1); MEM_RESULT(addr_reg); }
   297 #define MEM_READ_SIZE (CALL_FUNC1_SIZE)
   298 #define MEM_WRITE_SIZE (CALL_FUNC2_SIZE)
   299 #define MMU_TRANSLATE_SIZE (sh4_x86.tlb_on ? (CALL_FUNC1_SIZE + 12) : 0 )
   301 #define SLOTILLEGAL() JMP_exc(EXC_SLOT_ILLEGAL); sh4_x86.in_delay_slot = DELAY_NONE; return 1;
   303 /****** Import appropriate calling conventions ******/
   304 #if SIZEOF_VOID_P == 8
   305 #include "sh4/ia64abi.h"
   306 #else /* 32-bit system */
   307 #ifdef APPLE_BUILD
   308 #include "sh4/ia32mac.h"
   309 #else
   310 #include "sh4/ia32abi.h"
   311 #endif
   312 #endif
   314 uint32_t sh4_translate_end_block_size()
   315 {
   316     if( sh4_x86.backpatch_posn <= 3 ) {
   317 	return EPILOGUE_SIZE + (sh4_x86.backpatch_posn*12);
   318     } else {
   319 	return EPILOGUE_SIZE + 48 + (sh4_x86.backpatch_posn-3)*15;
   320     }
   321 }
   324 /**
   325  * Embed a breakpoint into the generated code
   326  */
   327 void sh4_translate_emit_breakpoint( sh4vma_t pc )
   328 {
   329     load_imm32( R_EAX, pc );
   330     call_func1( sh4_translate_breakpoint_hit, R_EAX );
   331     sh4_x86.tstate = TSTATE_NONE;
   332 }
   335 #define UNTRANSLATABLE(pc) !IS_IN_ICACHE(pc)
   337 /**
   338  * Embed a call to sh4_execute_instruction for situations that we
   339  * can't translate (just page-crossing delay slots at the moment).
   340  * Caller is responsible for setting new_pc before calling this function.
   341  *
   342  * Performs:
   343  *   Set PC = endpc
   344  *   Set sh4r.in_delay_slot = sh4_x86.in_delay_slot
   345  *   Update slice_cycle for endpc+2 (single step doesn't update slice_cycle)
   346  *   Call sh4_execute_instruction
   347  *   Call xlat_get_code_by_vma / xlat_get_code as for normal exit
   348  */
   349 void exit_block_emu( sh4vma_t endpc )
   350 {
   351     load_imm32( R_ECX, endpc - sh4_x86.block_start_pc );   // 5
   352     ADD_r32_sh4r( R_ECX, R_PC );
   354     load_imm32( R_ECX, (((endpc - sh4_x86.block_start_pc)>>1)+1)*sh4_cpu_period ); // 5
   355     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   356     load_imm32( R_ECX, sh4_x86.in_delay_slot ? 1 : 0 );
   357     store_spreg( R_ECX, REG_OFFSET(in_delay_slot) );
   359     call_func0( sh4_execute_instruction );    
   360     load_spreg( R_EAX, R_PC );
   361     if( sh4_x86.tlb_on ) {
   362 	call_func1(xlat_get_code_by_vma,R_EAX);
   363     } else {
   364 	call_func1(xlat_get_code,R_EAX);
   365     }
   366     AND_imm8s_rptr( 0xFC, R_EAX );
   367     POP_r32(R_EBP);
   368     RET();
   369 } 
   371 /**
   372  * Translate a single instruction. Delayed branches are handled specially
   373  * by translating both branch and delayed instruction as a single unit (as
   374  * 
   375  * The instruction MUST be in the icache (assert check)
   376  *
   377  * @return true if the instruction marks the end of a basic block
   378  * (eg a branch or 
   379  */
   380 uint32_t sh4_translate_instruction( sh4vma_t pc )
   381 {
   382     uint32_t ir;
   383     /* Read instruction from icache */
   384     assert( IS_IN_ICACHE(pc) );
   385     ir = *(uint16_t *)GET_ICACHE_PTR(pc);
   387 	/* PC is not in the current icache - this usually means we're running
   388 	 * with MMU on, and we've gone past the end of the page. And since 
   389 	 * sh4_translate_block is pretty careful about this, it means we're
   390 	 * almost certainly in a delay slot.
   391 	 *
   392 	 * Since we can't assume the page is present (and we can't fault it in
   393 	 * at this point, inline a call to sh4_execute_instruction (with a few
   394 	 * small repairs to cope with the different environment).
   395 	 */
   397     if( !sh4_x86.in_delay_slot ) {
   398 	sh4_translate_add_recovery( (pc - sh4_x86.block_start_pc)>>1 );
   399     }
   400 %%
   401 /* ALU operations */
   402 ADD Rm, Rn {:
   403     COUNT_INST(I_ADD);
   404     load_reg( R_EAX, Rm );
   405     load_reg( R_ECX, Rn );
   406     ADD_r32_r32( R_EAX, R_ECX );
   407     store_reg( R_ECX, Rn );
   408     sh4_x86.tstate = TSTATE_NONE;
   409 :}
   410 ADD #imm, Rn {:  
   411     COUNT_INST(I_ADDI);
   412     load_reg( R_EAX, Rn );
   413     ADD_imm8s_r32( imm, R_EAX );
   414     store_reg( R_EAX, Rn );
   415     sh4_x86.tstate = TSTATE_NONE;
   416 :}
   417 ADDC Rm, Rn {:
   418     COUNT_INST(I_ADDC);
   419     if( sh4_x86.tstate != TSTATE_C ) {
   420 	LDC_t();
   421     }
   422     load_reg( R_EAX, Rm );
   423     load_reg( R_ECX, Rn );
   424     ADC_r32_r32( R_EAX, R_ECX );
   425     store_reg( R_ECX, Rn );
   426     SETC_t();
   427     sh4_x86.tstate = TSTATE_C;
   428 :}
   429 ADDV Rm, Rn {:
   430     COUNT_INST(I_ADDV);
   431     load_reg( R_EAX, Rm );
   432     load_reg( R_ECX, Rn );
   433     ADD_r32_r32( R_EAX, R_ECX );
   434     store_reg( R_ECX, Rn );
   435     SETO_t();
   436     sh4_x86.tstate = TSTATE_O;
   437 :}
   438 AND Rm, Rn {:
   439     COUNT_INST(I_AND);
   440     load_reg( R_EAX, Rm );
   441     load_reg( R_ECX, Rn );
   442     AND_r32_r32( R_EAX, R_ECX );
   443     store_reg( R_ECX, Rn );
   444     sh4_x86.tstate = TSTATE_NONE;
   445 :}
   446 AND #imm, R0 {:  
   447     COUNT_INST(I_ANDI);
   448     load_reg( R_EAX, 0 );
   449     AND_imm32_r32(imm, R_EAX); 
   450     store_reg( R_EAX, 0 );
   451     sh4_x86.tstate = TSTATE_NONE;
   452 :}
   453 AND.B #imm, @(R0, GBR) {: 
   454     COUNT_INST(I_ANDB);
   455     load_reg( R_EAX, 0 );
   456     load_spreg( R_ECX, R_GBR );
   457     ADD_r32_r32( R_ECX, R_EAX );
   458     MMU_TRANSLATE_WRITE( R_EAX );
   459     PUSH_realigned_r32(R_EAX);
   460     MEM_READ_BYTE( R_EAX, R_EAX );
   461     POP_realigned_r32(R_ECX);
   462     AND_imm32_r32(imm, R_EAX );
   463     MEM_WRITE_BYTE( R_ECX, R_EAX );
   464     sh4_x86.tstate = TSTATE_NONE;
   465 :}
   466 CMP/EQ Rm, Rn {:  
   467     COUNT_INST(I_CMPEQ);
   468     load_reg( R_EAX, Rm );
   469     load_reg( R_ECX, Rn );
   470     CMP_r32_r32( R_EAX, R_ECX );
   471     SETE_t();
   472     sh4_x86.tstate = TSTATE_E;
   473 :}
   474 CMP/EQ #imm, R0 {:  
   475     COUNT_INST(I_CMPEQI);
   476     load_reg( R_EAX, 0 );
   477     CMP_imm8s_r32(imm, R_EAX);
   478     SETE_t();
   479     sh4_x86.tstate = TSTATE_E;
   480 :}
   481 CMP/GE Rm, Rn {:  
   482     COUNT_INST(I_CMPGE);
   483     load_reg( R_EAX, Rm );
   484     load_reg( R_ECX, Rn );
   485     CMP_r32_r32( R_EAX, R_ECX );
   486     SETGE_t();
   487     sh4_x86.tstate = TSTATE_GE;
   488 :}
   489 CMP/GT Rm, Rn {: 
   490     COUNT_INST(I_CMPGT);
   491     load_reg( R_EAX, Rm );
   492     load_reg( R_ECX, Rn );
   493     CMP_r32_r32( R_EAX, R_ECX );
   494     SETG_t();
   495     sh4_x86.tstate = TSTATE_G;
   496 :}
   497 CMP/HI Rm, Rn {:  
   498     COUNT_INST(I_CMPHI);
   499     load_reg( R_EAX, Rm );
   500     load_reg( R_ECX, Rn );
   501     CMP_r32_r32( R_EAX, R_ECX );
   502     SETA_t();
   503     sh4_x86.tstate = TSTATE_A;
   504 :}
   505 CMP/HS Rm, Rn {: 
   506     COUNT_INST(I_CMPHS);
   507     load_reg( R_EAX, Rm );
   508     load_reg( R_ECX, Rn );
   509     CMP_r32_r32( R_EAX, R_ECX );
   510     SETAE_t();
   511     sh4_x86.tstate = TSTATE_AE;
   512  :}
   513 CMP/PL Rn {: 
   514     COUNT_INST(I_CMPPL);
   515     load_reg( R_EAX, Rn );
   516     CMP_imm8s_r32( 0, R_EAX );
   517     SETG_t();
   518     sh4_x86.tstate = TSTATE_G;
   519 :}
   520 CMP/PZ Rn {:  
   521     COUNT_INST(I_CMPPZ);
   522     load_reg( R_EAX, Rn );
   523     CMP_imm8s_r32( 0, R_EAX );
   524     SETGE_t();
   525     sh4_x86.tstate = TSTATE_GE;
   526 :}
   527 CMP/STR Rm, Rn {:  
   528     COUNT_INST(I_CMPSTR);
   529     load_reg( R_EAX, Rm );
   530     load_reg( R_ECX, Rn );
   531     XOR_r32_r32( R_ECX, R_EAX );
   532     TEST_r8_r8( R_AL, R_AL );
   533     JE_rel8(target1);
   534     TEST_r8_r8( R_AH, R_AH );
   535     JE_rel8(target2);
   536     SHR_imm8_r32( 16, R_EAX );
   537     TEST_r8_r8( R_AL, R_AL );
   538     JE_rel8(target3);
   539     TEST_r8_r8( R_AH, R_AH );
   540     JMP_TARGET(target1);
   541     JMP_TARGET(target2);
   542     JMP_TARGET(target3);
   543     SETE_t();
   544     sh4_x86.tstate = TSTATE_E;
   545 :}
   546 DIV0S Rm, Rn {:
   547     COUNT_INST(I_DIV0S);
   548     load_reg( R_EAX, Rm );
   549     load_reg( R_ECX, Rn );
   550     SHR_imm8_r32( 31, R_EAX );
   551     SHR_imm8_r32( 31, R_ECX );
   552     store_spreg( R_EAX, R_M );
   553     store_spreg( R_ECX, R_Q );
   554     CMP_r32_r32( R_EAX, R_ECX );
   555     SETNE_t();
   556     sh4_x86.tstate = TSTATE_NE;
   557 :}
   558 DIV0U {:  
   559     COUNT_INST(I_DIV0U);
   560     XOR_r32_r32( R_EAX, R_EAX );
   561     store_spreg( R_EAX, R_Q );
   562     store_spreg( R_EAX, R_M );
   563     store_spreg( R_EAX, R_T );
   564     sh4_x86.tstate = TSTATE_C; // works for DIV1
   565 :}
   566 DIV1 Rm, Rn {:
   567     COUNT_INST(I_DIV1);
   568     load_spreg( R_ECX, R_M );
   569     load_reg( R_EAX, Rn );
   570     if( sh4_x86.tstate != TSTATE_C ) {
   571 	LDC_t();
   572     }
   573     RCL1_r32( R_EAX );
   574     SETC_r8( R_DL ); // Q'
   575     CMP_sh4r_r32( R_Q, R_ECX );
   576     JE_rel8(mqequal);
   577     ADD_sh4r_r32( REG_OFFSET(r[Rm]), R_EAX );
   578     JMP_rel8(end);
   579     JMP_TARGET(mqequal);
   580     SUB_sh4r_r32( REG_OFFSET(r[Rm]), R_EAX );
   581     JMP_TARGET(end);
   582     store_reg( R_EAX, Rn ); // Done with Rn now
   583     SETC_r8(R_AL); // tmp1
   584     XOR_r8_r8( R_DL, R_AL ); // Q' = Q ^ tmp1
   585     XOR_r8_r8( R_AL, R_CL ); // Q'' = Q' ^ M
   586     store_spreg( R_ECX, R_Q );
   587     XOR_imm8s_r32( 1, R_AL );   // T = !Q'
   588     MOVZX_r8_r32( R_AL, R_EAX );
   589     store_spreg( R_EAX, R_T );
   590     sh4_x86.tstate = TSTATE_NONE;
   591 :}
   592 DMULS.L Rm, Rn {:  
   593     COUNT_INST(I_DMULS);
   594     load_reg( R_EAX, Rm );
   595     load_reg( R_ECX, Rn );
   596     IMUL_r32(R_ECX);
   597     store_spreg( R_EDX, R_MACH );
   598     store_spreg( R_EAX, R_MACL );
   599     sh4_x86.tstate = TSTATE_NONE;
   600 :}
   601 DMULU.L Rm, Rn {:  
   602     COUNT_INST(I_DMULU);
   603     load_reg( R_EAX, Rm );
   604     load_reg( R_ECX, Rn );
   605     MUL_r32(R_ECX);
   606     store_spreg( R_EDX, R_MACH );
   607     store_spreg( R_EAX, R_MACL );    
   608     sh4_x86.tstate = TSTATE_NONE;
   609 :}
   610 DT Rn {:  
   611     COUNT_INST(I_DT);
   612     load_reg( R_EAX, Rn );
   613     ADD_imm8s_r32( -1, R_EAX );
   614     store_reg( R_EAX, Rn );
   615     SETE_t();
   616     sh4_x86.tstate = TSTATE_E;
   617 :}
   618 EXTS.B Rm, Rn {:  
   619     COUNT_INST(I_EXTSB);
   620     load_reg( R_EAX, Rm );
   621     MOVSX_r8_r32( R_EAX, R_EAX );
   622     store_reg( R_EAX, Rn );
   623 :}
   624 EXTS.W Rm, Rn {:  
   625     COUNT_INST(I_EXTSW);
   626     load_reg( R_EAX, Rm );
   627     MOVSX_r16_r32( R_EAX, R_EAX );
   628     store_reg( R_EAX, Rn );
   629 :}
   630 EXTU.B Rm, Rn {:  
   631     COUNT_INST(I_EXTUB);
   632     load_reg( R_EAX, Rm );
   633     MOVZX_r8_r32( R_EAX, R_EAX );
   634     store_reg( R_EAX, Rn );
   635 :}
   636 EXTU.W Rm, Rn {:  
   637     COUNT_INST(I_EXTUW);
   638     load_reg( R_EAX, Rm );
   639     MOVZX_r16_r32( R_EAX, R_EAX );
   640     store_reg( R_EAX, Rn );
   641 :}
   642 MAC.L @Rm+, @Rn+ {:
   643     COUNT_INST(I_MACL);
   644     if( Rm == Rn ) {
   645 	load_reg( R_EAX, Rm );
   646 	check_ralign32( R_EAX );
   647 	MMU_TRANSLATE_READ( R_EAX );
   648 	PUSH_realigned_r32( R_EAX );
   649 	load_reg( R_EAX, Rn );
   650 	ADD_imm8s_r32( 4, R_EAX );
   651 	MMU_TRANSLATE_READ_EXC( R_EAX, -5 );
   652 	ADD_imm8s_sh4r( 8, REG_OFFSET(r[Rn]) );
   653 	// Note translate twice in case of page boundaries. Maybe worth
   654 	// adding a page-boundary check to skip the second translation
   655     } else {
   656 	load_reg( R_EAX, Rm );
   657 	check_ralign32( R_EAX );
   658 	MMU_TRANSLATE_READ( R_EAX );
   659 	load_reg( R_ECX, Rn );
   660 	check_ralign32( R_ECX );
   661 	PUSH_realigned_r32( R_EAX );
   662 	MMU_TRANSLATE_READ_EXC( R_ECX, -5 );
   663 	MOV_r32_r32( R_ECX, R_EAX );
   664 	ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rn]) );
   665 	ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
   666     }
   667     MEM_READ_LONG( R_EAX, R_EAX );
   668     POP_r32( R_ECX );
   669     PUSH_r32( R_EAX );
   670     MEM_READ_LONG( R_ECX, R_EAX );
   671     POP_realigned_r32( R_ECX );
   673     IMUL_r32( R_ECX );
   674     ADD_r32_sh4r( R_EAX, R_MACL );
   675     ADC_r32_sh4r( R_EDX, R_MACH );
   677     load_spreg( R_ECX, R_S );
   678     TEST_r32_r32(R_ECX, R_ECX);
   679     JE_rel8( nosat );
   680     call_func0( signsat48 );
   681     JMP_TARGET( nosat );
   682     sh4_x86.tstate = TSTATE_NONE;
   683 :}
   684 MAC.W @Rm+, @Rn+ {:  
   685     COUNT_INST(I_MACW);
   686     if( Rm == Rn ) {
   687 	load_reg( R_EAX, Rm );
   688 	check_ralign16( R_EAX );
   689 	MMU_TRANSLATE_READ( R_EAX );
   690 	PUSH_realigned_r32( R_EAX );
   691 	load_reg( R_EAX, Rn );
   692 	ADD_imm8s_r32( 2, R_EAX );
   693 	MMU_TRANSLATE_READ_EXC( R_EAX, -5 );
   694 	ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rn]) );
   695 	// Note translate twice in case of page boundaries. Maybe worth
   696 	// adding a page-boundary check to skip the second translation
   697     } else {
   698 	load_reg( R_EAX, Rm );
   699 	check_ralign16( R_EAX );
   700 	MMU_TRANSLATE_READ( R_EAX );
   701 	load_reg( R_ECX, Rn );
   702 	check_ralign16( R_ECX );
   703 	PUSH_realigned_r32( R_EAX );
   704 	MMU_TRANSLATE_READ_EXC( R_ECX, -5 );
   705 	MOV_r32_r32( R_ECX, R_EAX );
   706 	ADD_imm8s_sh4r( 2, REG_OFFSET(r[Rn]) );
   707 	ADD_imm8s_sh4r( 2, REG_OFFSET(r[Rm]) );
   708     }
   709     MEM_READ_WORD( R_EAX, R_EAX );
   710     POP_r32( R_ECX );
   711     PUSH_r32( R_EAX );
   712     MEM_READ_WORD( R_ECX, R_EAX );
   713     POP_realigned_r32( R_ECX );
   714     IMUL_r32( R_ECX );
   716     load_spreg( R_ECX, R_S );
   717     TEST_r32_r32( R_ECX, R_ECX );
   718     JE_rel8( nosat );
   720     ADD_r32_sh4r( R_EAX, R_MACL );  // 6
   721     JNO_rel8( end );            // 2
   722     load_imm32( R_EDX, 1 );         // 5
   723     store_spreg( R_EDX, R_MACH );   // 6
   724     JS_rel8( positive );        // 2
   725     load_imm32( R_EAX, 0x80000000 );// 5
   726     store_spreg( R_EAX, R_MACL );   // 6
   727     JMP_rel8(end2);           // 2
   729     JMP_TARGET(positive);
   730     load_imm32( R_EAX, 0x7FFFFFFF );// 5
   731     store_spreg( R_EAX, R_MACL );   // 6
   732     JMP_rel8(end3);            // 2
   734     JMP_TARGET(nosat);
   735     ADD_r32_sh4r( R_EAX, R_MACL );  // 6
   736     ADC_r32_sh4r( R_EDX, R_MACH );  // 6
   737     JMP_TARGET(end);
   738     JMP_TARGET(end2);
   739     JMP_TARGET(end3);
   740     sh4_x86.tstate = TSTATE_NONE;
   741 :}
   742 MOVT Rn {:  
   743     COUNT_INST(I_MOVT);
   744     load_spreg( R_EAX, R_T );
   745     store_reg( R_EAX, Rn );
   746 :}
   747 MUL.L Rm, Rn {:  
   748     COUNT_INST(I_MULL);
   749     load_reg( R_EAX, Rm );
   750     load_reg( R_ECX, Rn );
   751     MUL_r32( R_ECX );
   752     store_spreg( R_EAX, R_MACL );
   753     sh4_x86.tstate = TSTATE_NONE;
   754 :}
   755 MULS.W Rm, Rn {:
   756     COUNT_INST(I_MULSW);
   757     load_reg16s( R_EAX, Rm );
   758     load_reg16s( R_ECX, Rn );
   759     MUL_r32( R_ECX );
   760     store_spreg( R_EAX, R_MACL );
   761     sh4_x86.tstate = TSTATE_NONE;
   762 :}
   763 MULU.W Rm, Rn {:  
   764     COUNT_INST(I_MULUW);
   765     load_reg16u( R_EAX, Rm );
   766     load_reg16u( R_ECX, Rn );
   767     MUL_r32( R_ECX );
   768     store_spreg( R_EAX, R_MACL );
   769     sh4_x86.tstate = TSTATE_NONE;
   770 :}
   771 NEG Rm, Rn {:
   772     COUNT_INST(I_NEG);
   773     load_reg( R_EAX, Rm );
   774     NEG_r32( R_EAX );
   775     store_reg( R_EAX, Rn );
   776     sh4_x86.tstate = TSTATE_NONE;
   777 :}
   778 NEGC Rm, Rn {:  
   779     COUNT_INST(I_NEGC);
   780     load_reg( R_EAX, Rm );
   781     XOR_r32_r32( R_ECX, R_ECX );
   782     LDC_t();
   783     SBB_r32_r32( R_EAX, R_ECX );
   784     store_reg( R_ECX, Rn );
   785     SETC_t();
   786     sh4_x86.tstate = TSTATE_C;
   787 :}
   788 NOT Rm, Rn {:  
   789     COUNT_INST(I_NOT);
   790     load_reg( R_EAX, Rm );
   791     NOT_r32( R_EAX );
   792     store_reg( R_EAX, Rn );
   793     sh4_x86.tstate = TSTATE_NONE;
   794 :}
   795 OR Rm, Rn {:  
   796     COUNT_INST(I_OR);
   797     load_reg( R_EAX, Rm );
   798     load_reg( R_ECX, Rn );
   799     OR_r32_r32( R_EAX, R_ECX );
   800     store_reg( R_ECX, Rn );
   801     sh4_x86.tstate = TSTATE_NONE;
   802 :}
   803 OR #imm, R0 {:
   804     COUNT_INST(I_ORI);
   805     load_reg( R_EAX, 0 );
   806     OR_imm32_r32(imm, R_EAX);
   807     store_reg( R_EAX, 0 );
   808     sh4_x86.tstate = TSTATE_NONE;
   809 :}
   810 OR.B #imm, @(R0, GBR) {:  
   811     COUNT_INST(I_ORB);
   812     load_reg( R_EAX, 0 );
   813     load_spreg( R_ECX, R_GBR );
   814     ADD_r32_r32( R_ECX, R_EAX );
   815     MMU_TRANSLATE_WRITE( R_EAX );
   816     PUSH_realigned_r32(R_EAX);
   817     MEM_READ_BYTE( R_EAX, R_EAX );
   818     POP_realigned_r32(R_ECX);
   819     OR_imm32_r32(imm, R_EAX );
   820     MEM_WRITE_BYTE( R_ECX, R_EAX );
   821     sh4_x86.tstate = TSTATE_NONE;
   822 :}
   823 ROTCL Rn {:
   824     COUNT_INST(I_ROTCL);
   825     load_reg( R_EAX, Rn );
   826     if( sh4_x86.tstate != TSTATE_C ) {
   827 	LDC_t();
   828     }
   829     RCL1_r32( R_EAX );
   830     store_reg( R_EAX, Rn );
   831     SETC_t();
   832     sh4_x86.tstate = TSTATE_C;
   833 :}
   834 ROTCR Rn {:  
   835     COUNT_INST(I_ROTCR);
   836     load_reg( R_EAX, Rn );
   837     if( sh4_x86.tstate != TSTATE_C ) {
   838 	LDC_t();
   839     }
   840     RCR1_r32( R_EAX );
   841     store_reg( R_EAX, Rn );
   842     SETC_t();
   843     sh4_x86.tstate = TSTATE_C;
   844 :}
   845 ROTL Rn {:  
   846     COUNT_INST(I_ROTL);
   847     load_reg( R_EAX, Rn );
   848     ROL1_r32( R_EAX );
   849     store_reg( R_EAX, Rn );
   850     SETC_t();
   851     sh4_x86.tstate = TSTATE_C;
   852 :}
   853 ROTR Rn {:  
   854     COUNT_INST(I_ROTR);
   855     load_reg( R_EAX, Rn );
   856     ROR1_r32( R_EAX );
   857     store_reg( R_EAX, Rn );
   858     SETC_t();
   859     sh4_x86.tstate = TSTATE_C;
   860 :}
   861 SHAD Rm, Rn {:
   862     COUNT_INST(I_SHAD);
   863     /* Annoyingly enough, not directly convertible */
   864     load_reg( R_EAX, Rn );
   865     load_reg( R_ECX, Rm );
   866     CMP_imm32_r32( 0, R_ECX );
   867     JGE_rel8(doshl);
   869     NEG_r32( R_ECX );      // 2
   870     AND_imm8_r8( 0x1F, R_CL ); // 3
   871     JE_rel8(emptysar);     // 2
   872     SAR_r32_CL( R_EAX );       // 2
   873     JMP_rel8(end);          // 2
   875     JMP_TARGET(emptysar);
   876     SAR_imm8_r32(31, R_EAX );  // 3
   877     JMP_rel8(end2);
   879     JMP_TARGET(doshl);
   880     AND_imm8_r8( 0x1F, R_CL ); // 3
   881     SHL_r32_CL( R_EAX );       // 2
   882     JMP_TARGET(end);
   883     JMP_TARGET(end2);
   884     store_reg( R_EAX, Rn );
   885     sh4_x86.tstate = TSTATE_NONE;
   886 :}
   887 SHLD Rm, Rn {:  
   888     COUNT_INST(I_SHLD);
   889     load_reg( R_EAX, Rn );
   890     load_reg( R_ECX, Rm );
   891     CMP_imm32_r32( 0, R_ECX );
   892     JGE_rel8(doshl);
   894     NEG_r32( R_ECX );      // 2
   895     AND_imm8_r8( 0x1F, R_CL ); // 3
   896     JE_rel8(emptyshr );
   897     SHR_r32_CL( R_EAX );       // 2
   898     JMP_rel8(end);          // 2
   900     JMP_TARGET(emptyshr);
   901     XOR_r32_r32( R_EAX, R_EAX );
   902     JMP_rel8(end2);
   904     JMP_TARGET(doshl);
   905     AND_imm8_r8( 0x1F, R_CL ); // 3
   906     SHL_r32_CL( R_EAX );       // 2
   907     JMP_TARGET(end);
   908     JMP_TARGET(end2);
   909     store_reg( R_EAX, Rn );
   910     sh4_x86.tstate = TSTATE_NONE;
   911 :}
   912 SHAL Rn {: 
   913     COUNT_INST(I_SHAL);
   914     load_reg( R_EAX, Rn );
   915     SHL1_r32( R_EAX );
   916     SETC_t();
   917     store_reg( R_EAX, Rn );
   918     sh4_x86.tstate = TSTATE_C;
   919 :}
   920 SHAR Rn {:  
   921     COUNT_INST(I_SHAR);
   922     load_reg( R_EAX, Rn );
   923     SAR1_r32( R_EAX );
   924     SETC_t();
   925     store_reg( R_EAX, Rn );
   926     sh4_x86.tstate = TSTATE_C;
   927 :}
   928 SHLL Rn {:  
   929     COUNT_INST(I_SHLL);
   930     load_reg( R_EAX, Rn );
   931     SHL1_r32( R_EAX );
   932     SETC_t();
   933     store_reg( R_EAX, Rn );
   934     sh4_x86.tstate = TSTATE_C;
   935 :}
   936 SHLL2 Rn {:
   937     COUNT_INST(I_SHLL);
   938     load_reg( R_EAX, Rn );
   939     SHL_imm8_r32( 2, R_EAX );
   940     store_reg( R_EAX, Rn );
   941     sh4_x86.tstate = TSTATE_NONE;
   942 :}
   943 SHLL8 Rn {:  
   944     COUNT_INST(I_SHLL);
   945     load_reg( R_EAX, Rn );
   946     SHL_imm8_r32( 8, R_EAX );
   947     store_reg( R_EAX, Rn );
   948     sh4_x86.tstate = TSTATE_NONE;
   949 :}
   950 SHLL16 Rn {:  
   951     COUNT_INST(I_SHLL);
   952     load_reg( R_EAX, Rn );
   953     SHL_imm8_r32( 16, R_EAX );
   954     store_reg( R_EAX, Rn );
   955     sh4_x86.tstate = TSTATE_NONE;
   956 :}
   957 SHLR Rn {:  
   958     COUNT_INST(I_SHLR);
   959     load_reg( R_EAX, Rn );
   960     SHR1_r32( R_EAX );
   961     SETC_t();
   962     store_reg( R_EAX, Rn );
   963     sh4_x86.tstate = TSTATE_C;
   964 :}
   965 SHLR2 Rn {:  
   966     COUNT_INST(I_SHLR);
   967     load_reg( R_EAX, Rn );
   968     SHR_imm8_r32( 2, R_EAX );
   969     store_reg( R_EAX, Rn );
   970     sh4_x86.tstate = TSTATE_NONE;
   971 :}
   972 SHLR8 Rn {:  
   973     COUNT_INST(I_SHLR);
   974     load_reg( R_EAX, Rn );
   975     SHR_imm8_r32( 8, R_EAX );
   976     store_reg( R_EAX, Rn );
   977     sh4_x86.tstate = TSTATE_NONE;
   978 :}
   979 SHLR16 Rn {:  
   980     COUNT_INST(I_SHLR);
   981     load_reg( R_EAX, Rn );
   982     SHR_imm8_r32( 16, R_EAX );
   983     store_reg( R_EAX, Rn );
   984     sh4_x86.tstate = TSTATE_NONE;
   985 :}
   986 SUB Rm, Rn {:  
   987     COUNT_INST(I_SUB);
   988     load_reg( R_EAX, Rm );
   989     load_reg( R_ECX, Rn );
   990     SUB_r32_r32( R_EAX, R_ECX );
   991     store_reg( R_ECX, Rn );
   992     sh4_x86.tstate = TSTATE_NONE;
   993 :}
   994 SUBC Rm, Rn {:  
   995     COUNT_INST(I_SUBC);
   996     load_reg( R_EAX, Rm );
   997     load_reg( R_ECX, Rn );
   998     if( sh4_x86.tstate != TSTATE_C ) {
   999 	LDC_t();
  1001     SBB_r32_r32( R_EAX, R_ECX );
  1002     store_reg( R_ECX, Rn );
  1003     SETC_t();
  1004     sh4_x86.tstate = TSTATE_C;
  1005 :}
  1006 SUBV Rm, Rn {:  
  1007     COUNT_INST(I_SUBV);
  1008     load_reg( R_EAX, Rm );
  1009     load_reg( R_ECX, Rn );
  1010     SUB_r32_r32( R_EAX, R_ECX );
  1011     store_reg( R_ECX, Rn );
  1012     SETO_t();
  1013     sh4_x86.tstate = TSTATE_O;
  1014 :}
  1015 SWAP.B Rm, Rn {:  
  1016     COUNT_INST(I_SWAPB);
  1017     load_reg( R_EAX, Rm );
  1018     XCHG_r8_r8( R_AL, R_AH ); // NB: does not touch EFLAGS
  1019     store_reg( R_EAX, Rn );
  1020 :}
  1021 SWAP.W Rm, Rn {:  
  1022     COUNT_INST(I_SWAPB);
  1023     load_reg( R_EAX, Rm );
  1024     MOV_r32_r32( R_EAX, R_ECX );
  1025     SHL_imm8_r32( 16, R_ECX );
  1026     SHR_imm8_r32( 16, R_EAX );
  1027     OR_r32_r32( R_EAX, R_ECX );
  1028     store_reg( R_ECX, Rn );
  1029     sh4_x86.tstate = TSTATE_NONE;
  1030 :}
  1031 TAS.B @Rn {:  
  1032     COUNT_INST(I_TASB);
  1033     load_reg( R_EAX, Rn );
  1034     MMU_TRANSLATE_WRITE( R_EAX );
  1035     PUSH_realigned_r32( R_EAX );
  1036     MEM_READ_BYTE( R_EAX, R_EAX );
  1037     TEST_r8_r8( R_AL, R_AL );
  1038     SETE_t();
  1039     OR_imm8_r8( 0x80, R_AL );
  1040     POP_realigned_r32( R_ECX );
  1041     MEM_WRITE_BYTE( R_ECX, R_EAX );
  1042     sh4_x86.tstate = TSTATE_NONE;
  1043 :}
  1044 TST Rm, Rn {:  
  1045     COUNT_INST(I_TST);
  1046     load_reg( R_EAX, Rm );
  1047     load_reg( R_ECX, Rn );
  1048     TEST_r32_r32( R_EAX, R_ECX );
  1049     SETE_t();
  1050     sh4_x86.tstate = TSTATE_E;
  1051 :}
  1052 TST #imm, R0 {:  
  1053     COUNT_INST(I_TSTI);
  1054     load_reg( R_EAX, 0 );
  1055     TEST_imm32_r32( imm, R_EAX );
  1056     SETE_t();
  1057     sh4_x86.tstate = TSTATE_E;
  1058 :}
  1059 TST.B #imm, @(R0, GBR) {:  
  1060     COUNT_INST(I_TSTB);
  1061     load_reg( R_EAX, 0);
  1062     load_reg( R_ECX, R_GBR);
  1063     ADD_r32_r32( R_ECX, R_EAX );
  1064     MMU_TRANSLATE_READ( R_EAX );
  1065     MEM_READ_BYTE( R_EAX, R_EAX );
  1066     TEST_imm8_r8( imm, R_AL );
  1067     SETE_t();
  1068     sh4_x86.tstate = TSTATE_E;
  1069 :}
  1070 XOR Rm, Rn {:  
  1071     COUNT_INST(I_XOR);
  1072     load_reg( R_EAX, Rm );
  1073     load_reg( R_ECX, Rn );
  1074     XOR_r32_r32( R_EAX, R_ECX );
  1075     store_reg( R_ECX, Rn );
  1076     sh4_x86.tstate = TSTATE_NONE;
  1077 :}
  1078 XOR #imm, R0 {:  
  1079     COUNT_INST(I_XORI);
  1080     load_reg( R_EAX, 0 );
  1081     XOR_imm32_r32( imm, R_EAX );
  1082     store_reg( R_EAX, 0 );
  1083     sh4_x86.tstate = TSTATE_NONE;
  1084 :}
  1085 XOR.B #imm, @(R0, GBR) {:  
  1086     COUNT_INST(I_XORB);
  1087     load_reg( R_EAX, 0 );
  1088     load_spreg( R_ECX, R_GBR );
  1089     ADD_r32_r32( R_ECX, R_EAX );
  1090     MMU_TRANSLATE_WRITE( R_EAX );
  1091     PUSH_realigned_r32(R_EAX);
  1092     MEM_READ_BYTE(R_EAX, R_EAX);
  1093     POP_realigned_r32(R_ECX);
  1094     XOR_imm32_r32( imm, R_EAX );
  1095     MEM_WRITE_BYTE( R_ECX, R_EAX );
  1096     sh4_x86.tstate = TSTATE_NONE;
  1097 :}
  1098 XTRCT Rm, Rn {:
  1099     COUNT_INST(I_XTRCT);
  1100     load_reg( R_EAX, Rm );
  1101     load_reg( R_ECX, Rn );
  1102     SHL_imm8_r32( 16, R_EAX );
  1103     SHR_imm8_r32( 16, R_ECX );
  1104     OR_r32_r32( R_EAX, R_ECX );
  1105     store_reg( R_ECX, Rn );
  1106     sh4_x86.tstate = TSTATE_NONE;
  1107 :}
  1109 /* Data move instructions */
  1110 MOV Rm, Rn {:  
  1111     COUNT_INST(I_MOV);
  1112     load_reg( R_EAX, Rm );
  1113     store_reg( R_EAX, Rn );
  1114 :}
  1115 MOV #imm, Rn {:  
  1116     COUNT_INST(I_MOVI);
  1117     load_imm32( R_EAX, imm );
  1118     store_reg( R_EAX, Rn );
  1119 :}
  1120 MOV.B Rm, @Rn {:  
  1121     COUNT_INST(I_MOVB);
  1122     load_reg( R_EAX, Rn );
  1123     MMU_TRANSLATE_WRITE( R_EAX );
  1124     load_reg( R_EDX, Rm );
  1125     MEM_WRITE_BYTE( R_EAX, R_EDX );
  1126     sh4_x86.tstate = TSTATE_NONE;
  1127 :}
  1128 MOV.B Rm, @-Rn {:  
  1129     COUNT_INST(I_MOVB);
  1130     load_reg( R_EAX, Rn );
  1131     ADD_imm8s_r32( -1, R_EAX );
  1132     MMU_TRANSLATE_WRITE( R_EAX );
  1133     load_reg( R_EDX, Rm );
  1134     ADD_imm8s_sh4r( -1, REG_OFFSET(r[Rn]) );
  1135     MEM_WRITE_BYTE( R_EAX, R_EDX );
  1136     sh4_x86.tstate = TSTATE_NONE;
  1137 :}
  1138 MOV.B Rm, @(R0, Rn) {:  
  1139     COUNT_INST(I_MOVB);
  1140     load_reg( R_EAX, 0 );
  1141     load_reg( R_ECX, Rn );
  1142     ADD_r32_r32( R_ECX, R_EAX );
  1143     MMU_TRANSLATE_WRITE( R_EAX );
  1144     load_reg( R_EDX, Rm );
  1145     MEM_WRITE_BYTE( R_EAX, R_EDX );
  1146     sh4_x86.tstate = TSTATE_NONE;
  1147 :}
  1148 MOV.B R0, @(disp, GBR) {:  
  1149     COUNT_INST(I_MOVB);
  1150     load_spreg( R_EAX, R_GBR );
  1151     ADD_imm32_r32( disp, R_EAX );
  1152     MMU_TRANSLATE_WRITE( R_EAX );
  1153     load_reg( R_EDX, 0 );
  1154     MEM_WRITE_BYTE( R_EAX, R_EDX );
  1155     sh4_x86.tstate = TSTATE_NONE;
  1156 :}
  1157 MOV.B R0, @(disp, Rn) {:  
  1158     COUNT_INST(I_MOVB);
  1159     load_reg( R_EAX, Rn );
  1160     ADD_imm32_r32( disp, R_EAX );
  1161     MMU_TRANSLATE_WRITE( R_EAX );
  1162     load_reg( R_EDX, 0 );
  1163     MEM_WRITE_BYTE( R_EAX, R_EDX );
  1164     sh4_x86.tstate = TSTATE_NONE;
  1165 :}
  1166 MOV.B @Rm, Rn {:  
  1167     COUNT_INST(I_MOVB);
  1168     load_reg( R_EAX, Rm );
  1169     MMU_TRANSLATE_READ( R_EAX );
  1170     MEM_READ_BYTE( R_EAX, R_EAX );
  1171     store_reg( R_EAX, Rn );
  1172     sh4_x86.tstate = TSTATE_NONE;
  1173 :}
  1174 MOV.B @Rm+, Rn {:  
  1175     COUNT_INST(I_MOVB);
  1176     load_reg( R_EAX, Rm );
  1177     MMU_TRANSLATE_READ( R_EAX );
  1178     ADD_imm8s_sh4r( 1, REG_OFFSET(r[Rm]) );
  1179     MEM_READ_BYTE( R_EAX, R_EAX );
  1180     store_reg( R_EAX, Rn );
  1181     sh4_x86.tstate = TSTATE_NONE;
  1182 :}
  1183 MOV.B @(R0, Rm), Rn {:  
  1184     COUNT_INST(I_MOVB);
  1185     load_reg( R_EAX, 0 );
  1186     load_reg( R_ECX, Rm );
  1187     ADD_r32_r32( R_ECX, R_EAX );
  1188     MMU_TRANSLATE_READ( R_EAX )
  1189     MEM_READ_BYTE( R_EAX, R_EAX );
  1190     store_reg( R_EAX, Rn );
  1191     sh4_x86.tstate = TSTATE_NONE;
  1192 :}
  1193 MOV.B @(disp, GBR), R0 {:  
  1194     COUNT_INST(I_MOVB);
  1195     load_spreg( R_EAX, R_GBR );
  1196     ADD_imm32_r32( disp, R_EAX );
  1197     MMU_TRANSLATE_READ( R_EAX );
  1198     MEM_READ_BYTE( R_EAX, R_EAX );
  1199     store_reg( R_EAX, 0 );
  1200     sh4_x86.tstate = TSTATE_NONE;
  1201 :}
  1202 MOV.B @(disp, Rm), R0 {:  
  1203     COUNT_INST(I_MOVB);
  1204     load_reg( R_EAX, Rm );
  1205     ADD_imm32_r32( disp, R_EAX );
  1206     MMU_TRANSLATE_READ( R_EAX );
  1207     MEM_READ_BYTE( R_EAX, R_EAX );
  1208     store_reg( R_EAX, 0 );
  1209     sh4_x86.tstate = TSTATE_NONE;
  1210 :}
  1211 MOV.L Rm, @Rn {:
  1212     COUNT_INST(I_MOVL);
  1213     load_reg( R_EAX, Rn );
  1214     check_walign32(R_EAX);
  1215     MMU_TRANSLATE_WRITE( R_EAX );
  1216     load_reg( R_EDX, Rm );
  1217     MEM_WRITE_LONG( R_EAX, R_EDX );
  1218     sh4_x86.tstate = TSTATE_NONE;
  1219 :}
  1220 MOV.L Rm, @-Rn {:  
  1221     COUNT_INST(I_MOVL);
  1222     load_reg( R_EAX, Rn );
  1223     ADD_imm8s_r32( -4, R_EAX );
  1224     check_walign32( R_EAX );
  1225     MMU_TRANSLATE_WRITE( R_EAX );
  1226     load_reg( R_EDX, Rm );
  1227     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  1228     MEM_WRITE_LONG( R_EAX, R_EDX );
  1229     sh4_x86.tstate = TSTATE_NONE;
  1230 :}
  1231 MOV.L Rm, @(R0, Rn) {:  
  1232     COUNT_INST(I_MOVL);
  1233     load_reg( R_EAX, 0 );
  1234     load_reg( R_ECX, Rn );
  1235     ADD_r32_r32( R_ECX, R_EAX );
  1236     check_walign32( R_EAX );
  1237     MMU_TRANSLATE_WRITE( R_EAX );
  1238     load_reg( R_EDX, Rm );
  1239     MEM_WRITE_LONG( R_EAX, R_EDX );
  1240     sh4_x86.tstate = TSTATE_NONE;
  1241 :}
  1242 MOV.L R0, @(disp, GBR) {:  
  1243     COUNT_INST(I_MOVL);
  1244     load_spreg( R_EAX, R_GBR );
  1245     ADD_imm32_r32( disp, R_EAX );
  1246     check_walign32( R_EAX );
  1247     MMU_TRANSLATE_WRITE( R_EAX );
  1248     load_reg( R_EDX, 0 );
  1249     MEM_WRITE_LONG( R_EAX, R_EDX );
  1250     sh4_x86.tstate = TSTATE_NONE;
  1251 :}
  1252 MOV.L Rm, @(disp, Rn) {:  
  1253     COUNT_INST(I_MOVL);
  1254     load_reg( R_EAX, Rn );
  1255     ADD_imm32_r32( disp, R_EAX );
  1256     check_walign32( R_EAX );
  1257     MMU_TRANSLATE_WRITE( R_EAX );
  1258     load_reg( R_EDX, Rm );
  1259     MEM_WRITE_LONG( R_EAX, R_EDX );
  1260     sh4_x86.tstate = TSTATE_NONE;
  1261 :}
  1262 MOV.L @Rm, Rn {:  
  1263     COUNT_INST(I_MOVL);
  1264     load_reg( R_EAX, Rm );
  1265     check_ralign32( R_EAX );
  1266     MMU_TRANSLATE_READ( R_EAX );
  1267     MEM_READ_LONG( R_EAX, R_EAX );
  1268     store_reg( R_EAX, Rn );
  1269     sh4_x86.tstate = TSTATE_NONE;
  1270 :}
  1271 MOV.L @Rm+, Rn {:  
  1272     COUNT_INST(I_MOVL);
  1273     load_reg( R_EAX, Rm );
  1274     check_ralign32( R_EAX );
  1275     MMU_TRANSLATE_READ( R_EAX );
  1276     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  1277     MEM_READ_LONG( R_EAX, R_EAX );
  1278     store_reg( R_EAX, Rn );
  1279     sh4_x86.tstate = TSTATE_NONE;
  1280 :}
  1281 MOV.L @(R0, Rm), Rn {:  
  1282     COUNT_INST(I_MOVL);
  1283     load_reg( R_EAX, 0 );
  1284     load_reg( R_ECX, Rm );
  1285     ADD_r32_r32( R_ECX, R_EAX );
  1286     check_ralign32( R_EAX );
  1287     MMU_TRANSLATE_READ( R_EAX );
  1288     MEM_READ_LONG( R_EAX, R_EAX );
  1289     store_reg( R_EAX, Rn );
  1290     sh4_x86.tstate = TSTATE_NONE;
  1291 :}
  1292 MOV.L @(disp, GBR), R0 {:
  1293     COUNT_INST(I_MOVL);
  1294     load_spreg( R_EAX, R_GBR );
  1295     ADD_imm32_r32( disp, R_EAX );
  1296     check_ralign32( R_EAX );
  1297     MMU_TRANSLATE_READ( R_EAX );
  1298     MEM_READ_LONG( R_EAX, R_EAX );
  1299     store_reg( R_EAX, 0 );
  1300     sh4_x86.tstate = TSTATE_NONE;
  1301 :}
  1302 MOV.L @(disp, PC), Rn {:  
  1303     COUNT_INST(I_MOVLPC);
  1304     if( sh4_x86.in_delay_slot ) {
  1305 	SLOTILLEGAL();
  1306     } else {
  1307 	uint32_t target = (pc & 0xFFFFFFFC) + disp + 4;
  1308 	if( IS_IN_ICACHE(target) ) {
  1309 	    // If the target address is in the same page as the code, it's
  1310 	    // pretty safe to just ref it directly and circumvent the whole
  1311 	    // memory subsystem. (this is a big performance win)
  1313 	    // FIXME: There's a corner-case that's not handled here when
  1314 	    // the current code-page is in the ITLB but not in the UTLB.
  1315 	    // (should generate a TLB miss although need to test SH4 
  1316 	    // behaviour to confirm) Unlikely to be anyone depending on this
  1317 	    // behaviour though.
  1318 	    sh4ptr_t ptr = GET_ICACHE_PTR(target);
  1319 	    MOV_moff32_EAX( ptr );
  1320 	} else {
  1321 	    // Note: we use sh4r.pc for the calc as we could be running at a
  1322 	    // different virtual address than the translation was done with,
  1323 	    // but we can safely assume that the low bits are the same.
  1324 	    load_imm32( R_EAX, (pc-sh4_x86.block_start_pc) + disp + 4 - (pc&0x03) );
  1325 	    ADD_sh4r_r32( R_PC, R_EAX );
  1326 	    MMU_TRANSLATE_READ( R_EAX );
  1327 	    MEM_READ_LONG( R_EAX, R_EAX );
  1328 	    sh4_x86.tstate = TSTATE_NONE;
  1330 	store_reg( R_EAX, Rn );
  1332 :}
  1333 MOV.L @(disp, Rm), Rn {:  
  1334     COUNT_INST(I_MOVL);
  1335     load_reg( R_EAX, Rm );
  1336     ADD_imm8s_r32( disp, R_EAX );
  1337     check_ralign32( R_EAX );
  1338     MMU_TRANSLATE_READ( R_EAX );
  1339     MEM_READ_LONG( R_EAX, R_EAX );
  1340     store_reg( R_EAX, Rn );
  1341     sh4_x86.tstate = TSTATE_NONE;
  1342 :}
  1343 MOV.W Rm, @Rn {:  
  1344     COUNT_INST(I_MOVW);
  1345     load_reg( R_EAX, Rn );
  1346     check_walign16( R_EAX );
  1347     MMU_TRANSLATE_WRITE( R_EAX )
  1348     load_reg( R_EDX, Rm );
  1349     MEM_WRITE_WORD( R_EAX, R_EDX );
  1350     sh4_x86.tstate = TSTATE_NONE;
  1351 :}
  1352 MOV.W Rm, @-Rn {:  
  1353     COUNT_INST(I_MOVW);
  1354     load_reg( R_EAX, Rn );
  1355     ADD_imm8s_r32( -2, R_EAX );
  1356     check_walign16( R_EAX );
  1357     MMU_TRANSLATE_WRITE( R_EAX );
  1358     load_reg( R_EDX, Rm );
  1359     ADD_imm8s_sh4r( -2, REG_OFFSET(r[Rn]) );
  1360     MEM_WRITE_WORD( R_EAX, R_EDX );
  1361     sh4_x86.tstate = TSTATE_NONE;
  1362 :}
  1363 MOV.W Rm, @(R0, Rn) {:  
  1364     COUNT_INST(I_MOVW);
  1365     load_reg( R_EAX, 0 );
  1366     load_reg( R_ECX, Rn );
  1367     ADD_r32_r32( R_ECX, R_EAX );
  1368     check_walign16( R_EAX );
  1369     MMU_TRANSLATE_WRITE( R_EAX );
  1370     load_reg( R_EDX, Rm );
  1371     MEM_WRITE_WORD( R_EAX, R_EDX );
  1372     sh4_x86.tstate = TSTATE_NONE;
  1373 :}
  1374 MOV.W R0, @(disp, GBR) {:  
  1375     COUNT_INST(I_MOVW);
  1376     load_spreg( R_EAX, R_GBR );
  1377     ADD_imm32_r32( disp, R_EAX );
  1378     check_walign16( R_EAX );
  1379     MMU_TRANSLATE_WRITE( R_EAX );
  1380     load_reg( R_EDX, 0 );
  1381     MEM_WRITE_WORD( R_EAX, R_EDX );
  1382     sh4_x86.tstate = TSTATE_NONE;
  1383 :}
  1384 MOV.W R0, @(disp, Rn) {:  
  1385     COUNT_INST(I_MOVW);
  1386     load_reg( R_EAX, Rn );
  1387     ADD_imm32_r32( disp, R_EAX );
  1388     check_walign16( R_EAX );
  1389     MMU_TRANSLATE_WRITE( R_EAX );
  1390     load_reg( R_EDX, 0 );
  1391     MEM_WRITE_WORD( R_EAX, R_EDX );
  1392     sh4_x86.tstate = TSTATE_NONE;
  1393 :}
  1394 MOV.W @Rm, Rn {:  
  1395     COUNT_INST(I_MOVW);
  1396     load_reg( R_EAX, Rm );
  1397     check_ralign16( R_EAX );
  1398     MMU_TRANSLATE_READ( R_EAX );
  1399     MEM_READ_WORD( R_EAX, R_EAX );
  1400     store_reg( R_EAX, Rn );
  1401     sh4_x86.tstate = TSTATE_NONE;
  1402 :}
  1403 MOV.W @Rm+, Rn {:  
  1404     COUNT_INST(I_MOVW);
  1405     load_reg( R_EAX, Rm );
  1406     check_ralign16( R_EAX );
  1407     MMU_TRANSLATE_READ( R_EAX );
  1408     ADD_imm8s_sh4r( 2, REG_OFFSET(r[Rm]) );
  1409     MEM_READ_WORD( R_EAX, R_EAX );
  1410     store_reg( R_EAX, Rn );
  1411     sh4_x86.tstate = TSTATE_NONE;
  1412 :}
  1413 MOV.W @(R0, Rm), Rn {:  
  1414     COUNT_INST(I_MOVW);
  1415     load_reg( R_EAX, 0 );
  1416     load_reg( R_ECX, Rm );
  1417     ADD_r32_r32( R_ECX, R_EAX );
  1418     check_ralign16( R_EAX );
  1419     MMU_TRANSLATE_READ( R_EAX );
  1420     MEM_READ_WORD( R_EAX, R_EAX );
  1421     store_reg( R_EAX, Rn );
  1422     sh4_x86.tstate = TSTATE_NONE;
  1423 :}
  1424 MOV.W @(disp, GBR), R0 {:  
  1425     COUNT_INST(I_MOVW);
  1426     load_spreg( R_EAX, R_GBR );
  1427     ADD_imm32_r32( disp, R_EAX );
  1428     check_ralign16( R_EAX );
  1429     MMU_TRANSLATE_READ( R_EAX );
  1430     MEM_READ_WORD( R_EAX, R_EAX );
  1431     store_reg( R_EAX, 0 );
  1432     sh4_x86.tstate = TSTATE_NONE;
  1433 :}
  1434 MOV.W @(disp, PC), Rn {:  
  1435     COUNT_INST(I_MOVW);
  1436     if( sh4_x86.in_delay_slot ) {
  1437 	SLOTILLEGAL();
  1438     } else {
  1439 	// See comments for MOV.L @(disp, PC), Rn
  1440 	uint32_t target = pc + disp + 4;
  1441 	if( IS_IN_ICACHE(target) ) {
  1442 	    sh4ptr_t ptr = GET_ICACHE_PTR(target);
  1443 	    MOV_moff32_EAX( ptr );
  1444 	    MOVSX_r16_r32( R_EAX, R_EAX );
  1445 	} else {
  1446 	    load_imm32( R_EAX, (pc - sh4_x86.block_start_pc) + disp + 4 );
  1447 	    ADD_sh4r_r32( R_PC, R_EAX );
  1448 	    MMU_TRANSLATE_READ( R_EAX );
  1449 	    MEM_READ_WORD( R_EAX, R_EAX );
  1450 	    sh4_x86.tstate = TSTATE_NONE;
  1452 	store_reg( R_EAX, Rn );
  1454 :}
  1455 MOV.W @(disp, Rm), R0 {:  
  1456     COUNT_INST(I_MOVW);
  1457     load_reg( R_EAX, Rm );
  1458     ADD_imm32_r32( disp, R_EAX );
  1459     check_ralign16( R_EAX );
  1460     MMU_TRANSLATE_READ( R_EAX );
  1461     MEM_READ_WORD( R_EAX, R_EAX );
  1462     store_reg( R_EAX, 0 );
  1463     sh4_x86.tstate = TSTATE_NONE;
  1464 :}
  1465 MOVA @(disp, PC), R0 {:  
  1466     COUNT_INST(I_MOVA);
  1467     if( sh4_x86.in_delay_slot ) {
  1468 	SLOTILLEGAL();
  1469     } else {
  1470 	load_imm32( R_ECX, (pc - sh4_x86.block_start_pc) + disp + 4 - (pc&0x03) );
  1471 	ADD_sh4r_r32( R_PC, R_ECX );
  1472 	store_reg( R_ECX, 0 );
  1473 	sh4_x86.tstate = TSTATE_NONE;
  1475 :}
  1476 MOVCA.L R0, @Rn {:  
  1477     COUNT_INST(I_MOVCA);
  1478     load_reg( R_EAX, Rn );
  1479     check_walign32( R_EAX );
  1480     MMU_TRANSLATE_WRITE( R_EAX );
  1481     load_reg( R_EDX, 0 );
  1482     MEM_WRITE_LONG( R_EAX, R_EDX );
  1483     sh4_x86.tstate = TSTATE_NONE;
  1484 :}
  1486 /* Control transfer instructions */
  1487 BF disp {:
  1488     COUNT_INST(I_BF);
  1489     if( sh4_x86.in_delay_slot ) {
  1490 	SLOTILLEGAL();
  1491     } else {
  1492 	sh4vma_t target = disp + pc + 4;
  1493 	JT_rel8( nottaken );
  1494 	exit_block_rel(target, pc+2 );
  1495 	JMP_TARGET(nottaken);
  1496 	return 2;
  1498 :}
  1499 BF/S disp {:
  1500     COUNT_INST(I_BFS);
  1501     if( sh4_x86.in_delay_slot ) {
  1502 	SLOTILLEGAL();
  1503     } else {
  1504 	sh4_x86.in_delay_slot = DELAY_PC;
  1505 	if( UNTRANSLATABLE(pc+2) ) {
  1506 	    load_imm32( R_EAX, pc + 4 - sh4_x86.block_start_pc );
  1507 	    JT_rel8(nottaken);
  1508 	    ADD_imm32_r32( disp, R_EAX );
  1509 	    JMP_TARGET(nottaken);
  1510 	    ADD_sh4r_r32( R_PC, R_EAX );
  1511 	    store_spreg( R_EAX, R_NEW_PC );
  1512 	    exit_block_emu(pc+2);
  1513 	    sh4_x86.branch_taken = TRUE;
  1514 	    return 2;
  1515 	} else {
  1516 	    if( sh4_x86.tstate == TSTATE_NONE ) {
  1517 		CMP_imm8s_sh4r( 1, R_T );
  1518 		sh4_x86.tstate = TSTATE_E;
  1520 	    sh4vma_t target = disp + pc + 4;
  1521 	    OP(0x0F); OP(0x80+sh4_x86.tstate); uint32_t *patch = (uint32_t *)xlat_output; OP32(0); // JT rel32
  1522 	    sh4_translate_instruction(pc+2);
  1523 	    exit_block_rel( target, pc+4 );
  1525 	    // not taken
  1526 	    *patch = (xlat_output - ((uint8_t *)patch)) - 4;
  1527 	    sh4_translate_instruction(pc+2);
  1528 	    return 4;
  1531 :}
  1532 BRA disp {:  
  1533     COUNT_INST(I_BRA);
  1534     if( sh4_x86.in_delay_slot ) {
  1535 	SLOTILLEGAL();
  1536     } else {
  1537 	sh4_x86.in_delay_slot = DELAY_PC;
  1538 	sh4_x86.branch_taken = TRUE;
  1539 	if( UNTRANSLATABLE(pc+2) ) {
  1540 	    load_spreg( R_EAX, R_PC );
  1541 	    ADD_imm32_r32( pc + disp + 4 - sh4_x86.block_start_pc, R_EAX );
  1542 	    store_spreg( R_EAX, R_NEW_PC );
  1543 	    exit_block_emu(pc+2);
  1544 	    return 2;
  1545 	} else {
  1546 	    sh4_translate_instruction( pc + 2 );
  1547 	    exit_block_rel( disp + pc + 4, pc+4 );
  1548 	    return 4;
  1551 :}
  1552 BRAF Rn {:  
  1553     COUNT_INST(I_BRAF);
  1554     if( sh4_x86.in_delay_slot ) {
  1555 	SLOTILLEGAL();
  1556     } else {
  1557 	load_spreg( R_EAX, R_PC );
  1558 	ADD_imm32_r32( pc + 4 - sh4_x86.block_start_pc, R_EAX );
  1559 	ADD_sh4r_r32( REG_OFFSET(r[Rn]), R_EAX );
  1560 	store_spreg( R_EAX, R_NEW_PC );
  1561 	sh4_x86.in_delay_slot = DELAY_PC;
  1562 	sh4_x86.tstate = TSTATE_NONE;
  1563 	sh4_x86.branch_taken = TRUE;
  1564 	if( UNTRANSLATABLE(pc+2) ) {
  1565 	    exit_block_emu(pc+2);
  1566 	    return 2;
  1567 	} else {
  1568 	    sh4_translate_instruction( pc + 2 );
  1569 	    exit_block_newpcset(pc+2);
  1570 	    return 4;
  1573 :}
  1574 BSR disp {:  
  1575     COUNT_INST(I_BSR);
  1576     if( sh4_x86.in_delay_slot ) {
  1577 	SLOTILLEGAL();
  1578     } else {
  1579 	load_spreg( R_EAX, R_PC );
  1580 	ADD_imm32_r32( pc + 4 - sh4_x86.block_start_pc, R_EAX );
  1581 	store_spreg( R_EAX, R_PR );
  1582 	sh4_x86.in_delay_slot = DELAY_PC;
  1583 	sh4_x86.branch_taken = TRUE;
  1584 	sh4_x86.tstate = TSTATE_NONE;
  1585 	if( UNTRANSLATABLE(pc+2) ) {
  1586 	    ADD_imm32_r32( disp, R_EAX );
  1587 	    store_spreg( R_EAX, R_NEW_PC );
  1588 	    exit_block_emu(pc+2);
  1589 	    return 2;
  1590 	} else {
  1591 	    sh4_translate_instruction( pc + 2 );
  1592 	    exit_block_rel( disp + pc + 4, pc+4 );
  1593 	    return 4;
  1596 :}
  1597 BSRF Rn {:  
  1598     COUNT_INST(I_BSRF);
  1599     if( sh4_x86.in_delay_slot ) {
  1600 	SLOTILLEGAL();
  1601     } else {
  1602 	load_spreg( R_EAX, R_PC );
  1603 	ADD_imm32_r32( pc + 4 - sh4_x86.block_start_pc, R_EAX );
  1604 	store_spreg( R_EAX, R_PR );
  1605 	ADD_sh4r_r32( REG_OFFSET(r[Rn]), R_EAX );
  1606 	store_spreg( R_EAX, R_NEW_PC );
  1608 	sh4_x86.in_delay_slot = DELAY_PC;
  1609 	sh4_x86.tstate = TSTATE_NONE;
  1610 	sh4_x86.branch_taken = TRUE;
  1611 	if( UNTRANSLATABLE(pc+2) ) {
  1612 	    exit_block_emu(pc+2);
  1613 	    return 2;
  1614 	} else {
  1615 	    sh4_translate_instruction( pc + 2 );
  1616 	    exit_block_newpcset(pc+2);
  1617 	    return 4;
  1620 :}
  1621 BT disp {:
  1622     COUNT_INST(I_BT);
  1623     if( sh4_x86.in_delay_slot ) {
  1624 	SLOTILLEGAL();
  1625     } else {
  1626 	sh4vma_t target = disp + pc + 4;
  1627 	JF_rel8( nottaken );
  1628 	exit_block_rel(target, pc+2 );
  1629 	JMP_TARGET(nottaken);
  1630 	return 2;
  1632 :}
  1633 BT/S disp {:
  1634     COUNT_INST(I_BTS);
  1635     if( sh4_x86.in_delay_slot ) {
  1636 	SLOTILLEGAL();
  1637     } else {
  1638 	sh4_x86.in_delay_slot = DELAY_PC;
  1639 	if( UNTRANSLATABLE(pc+2) ) {
  1640 	    load_imm32( R_EAX, pc + 4 - sh4_x86.block_start_pc );
  1641 	    JF_rel8(nottaken);
  1642 	    ADD_imm32_r32( disp, R_EAX );
  1643 	    JMP_TARGET(nottaken);
  1644 	    ADD_sh4r_r32( R_PC, R_EAX );
  1645 	    store_spreg( R_EAX, R_NEW_PC );
  1646 	    exit_block_emu(pc+2);
  1647 	    sh4_x86.branch_taken = TRUE;
  1648 	    return 2;
  1649 	} else {
  1650 	    if( sh4_x86.tstate == TSTATE_NONE ) {
  1651 		CMP_imm8s_sh4r( 1, R_T );
  1652 		sh4_x86.tstate = TSTATE_E;
  1654 	    OP(0x0F); OP(0x80+(sh4_x86.tstate^1)); uint32_t *patch = (uint32_t *)xlat_output; OP32(0); // JF rel32
  1655 	    sh4_translate_instruction(pc+2);
  1656 	    exit_block_rel( disp + pc + 4, pc+4 );
  1657 	    // not taken
  1658 	    *patch = (xlat_output - ((uint8_t *)patch)) - 4;
  1659 	    sh4_translate_instruction(pc+2);
  1660 	    return 4;
  1663 :}
  1664 JMP @Rn {:  
  1665     COUNT_INST(I_JMP);
  1666     if( sh4_x86.in_delay_slot ) {
  1667 	SLOTILLEGAL();
  1668     } else {
  1669 	load_reg( R_ECX, Rn );
  1670 	store_spreg( R_ECX, R_NEW_PC );
  1671 	sh4_x86.in_delay_slot = DELAY_PC;
  1672 	sh4_x86.branch_taken = TRUE;
  1673 	if( UNTRANSLATABLE(pc+2) ) {
  1674 	    exit_block_emu(pc+2);
  1675 	    return 2;
  1676 	} else {
  1677 	    sh4_translate_instruction(pc+2);
  1678 	    exit_block_newpcset(pc+2);
  1679 	    return 4;
  1682 :}
  1683 JSR @Rn {:  
  1684     COUNT_INST(I_JSR);
  1685     if( sh4_x86.in_delay_slot ) {
  1686 	SLOTILLEGAL();
  1687     } else {
  1688 	load_spreg( R_EAX, R_PC );
  1689 	ADD_imm32_r32( pc + 4 - sh4_x86.block_start_pc, R_EAX );
  1690 	store_spreg( R_EAX, R_PR );
  1691 	load_reg( R_ECX, Rn );
  1692 	store_spreg( R_ECX, R_NEW_PC );
  1693 	sh4_x86.in_delay_slot = DELAY_PC;
  1694 	sh4_x86.branch_taken = TRUE;
  1695 	sh4_x86.tstate = TSTATE_NONE;
  1696 	if( UNTRANSLATABLE(pc+2) ) {
  1697 	    exit_block_emu(pc+2);
  1698 	    return 2;
  1699 	} else {
  1700 	    sh4_translate_instruction(pc+2);
  1701 	    exit_block_newpcset(pc+2);
  1702 	    return 4;
  1705 :}
  1706 RTE {:  
  1707     COUNT_INST(I_RTE);
  1708     if( sh4_x86.in_delay_slot ) {
  1709 	SLOTILLEGAL();
  1710     } else {
  1711 	check_priv();
  1712 	load_spreg( R_ECX, R_SPC );
  1713 	store_spreg( R_ECX, R_NEW_PC );
  1714 	load_spreg( R_EAX, R_SSR );
  1715 	call_func1( sh4_write_sr, R_EAX );
  1716 	sh4_x86.in_delay_slot = DELAY_PC;
  1717 	sh4_x86.priv_checked = FALSE;
  1718 	sh4_x86.fpuen_checked = FALSE;
  1719 	sh4_x86.tstate = TSTATE_NONE;
  1720 	sh4_x86.branch_taken = TRUE;
  1721 	if( UNTRANSLATABLE(pc+2) ) {
  1722 	    exit_block_emu(pc+2);
  1723 	    return 2;
  1724 	} else {
  1725 	    sh4_translate_instruction(pc+2);
  1726 	    exit_block_newpcset(pc+2);
  1727 	    return 4;
  1730 :}
  1731 RTS {:  
  1732     COUNT_INST(I_RTS);
  1733     if( sh4_x86.in_delay_slot ) {
  1734 	SLOTILLEGAL();
  1735     } else {
  1736 	load_spreg( R_ECX, R_PR );
  1737 	store_spreg( R_ECX, R_NEW_PC );
  1738 	sh4_x86.in_delay_slot = DELAY_PC;
  1739 	sh4_x86.branch_taken = TRUE;
  1740 	if( UNTRANSLATABLE(pc+2) ) {
  1741 	    exit_block_emu(pc+2);
  1742 	    return 2;
  1743 	} else {
  1744 	    sh4_translate_instruction(pc+2);
  1745 	    exit_block_newpcset(pc+2);
  1746 	    return 4;
  1749 :}
  1750 TRAPA #imm {:  
  1751     COUNT_INST(I_TRAPA);
  1752     if( sh4_x86.in_delay_slot ) {
  1753 	SLOTILLEGAL();
  1754     } else {
  1755 	load_imm32( R_ECX, pc+2 - sh4_x86.block_start_pc );   // 5
  1756 	ADD_r32_sh4r( R_ECX, R_PC );
  1757 	load_imm32( R_EAX, imm );
  1758 	call_func1( sh4_raise_trap, R_EAX );
  1759 	sh4_x86.tstate = TSTATE_NONE;
  1760 	exit_block_pcset(pc);
  1761 	sh4_x86.branch_taken = TRUE;
  1762 	return 2;
  1764 :}
  1765 UNDEF {:  
  1766     COUNT_INST(I_UNDEF);
  1767     if( sh4_x86.in_delay_slot ) {
  1768 	SLOTILLEGAL();
  1769     } else {
  1770 	JMP_exc(EXC_ILLEGAL);
  1771 	return 2;
  1773 :}
  1775 CLRMAC {:  
  1776     COUNT_INST(I_CLRMAC);
  1777     XOR_r32_r32(R_EAX, R_EAX);
  1778     store_spreg( R_EAX, R_MACL );
  1779     store_spreg( R_EAX, R_MACH );
  1780     sh4_x86.tstate = TSTATE_NONE;
  1781 :}
  1782 CLRS {:
  1783     COUNT_INST(I_CLRS);
  1784     CLC();
  1785     SETC_sh4r(R_S);
  1786     sh4_x86.tstate = TSTATE_NONE;
  1787 :}
  1788 CLRT {:  
  1789     COUNT_INST(I_CLRT);
  1790     CLC();
  1791     SETC_t();
  1792     sh4_x86.tstate = TSTATE_C;
  1793 :}
  1794 SETS {:  
  1795     COUNT_INST(I_SETS);
  1796     STC();
  1797     SETC_sh4r(R_S);
  1798     sh4_x86.tstate = TSTATE_NONE;
  1799 :}
  1800 SETT {:  
  1801     COUNT_INST(I_SETT);
  1802     STC();
  1803     SETC_t();
  1804     sh4_x86.tstate = TSTATE_C;
  1805 :}
  1807 /* Floating point moves */
  1808 FMOV FRm, FRn {:  
  1809     COUNT_INST(I_FMOV1);
  1810     check_fpuen();
  1811     load_spreg( R_ECX, R_FPSCR );
  1812     TEST_imm32_r32( FPSCR_SZ, R_ECX );
  1813     JNE_rel8(doublesize);
  1814     load_fr( R_EAX, FRm ); // SZ=0 branch
  1815     store_fr( R_EAX, FRn );
  1816     JMP_rel8(end);
  1817     JMP_TARGET(doublesize);
  1818     load_dr0( R_EAX, FRm );
  1819     load_dr1( R_ECX, FRm );
  1820     store_dr0( R_EAX, FRn );
  1821     store_dr1( R_ECX, FRn );
  1822     JMP_TARGET(end);
  1823     sh4_x86.tstate = TSTATE_NONE;
  1824 :}
  1825 FMOV FRm, @Rn {: 
  1826     COUNT_INST(I_FMOV2);
  1827     check_fpuen();
  1828     load_reg( R_EAX, Rn );
  1829     load_spreg( R_EDX, R_FPSCR );
  1830     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1831     JNE_rel8(doublesize);
  1833     check_walign32( R_EAX );
  1834     MMU_TRANSLATE_WRITE( R_EAX );
  1835     load_fr( R_ECX, FRm );
  1836     MEM_WRITE_LONG( R_EAX, R_ECX ); // 12
  1837     JMP_rel8(end);
  1839     JMP_TARGET(doublesize);
  1840     check_walign64( R_EAX );
  1841     MMU_TRANSLATE_WRITE( R_EAX );
  1842     load_dr0( R_ECX, FRm );
  1843     load_dr1( R_EDX, FRm );
  1844     MEM_WRITE_DOUBLE( R_EAX, R_ECX, R_EDX );
  1845     JMP_TARGET(end);
  1846     sh4_x86.tstate = TSTATE_NONE;
  1847 :}
  1848 FMOV @Rm, FRn {:  
  1849     COUNT_INST(I_FMOV5);
  1850     check_fpuen();
  1851     load_reg( R_EAX, Rm );
  1852     load_spreg( R_EDX, R_FPSCR );
  1853     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1854     JNE_rel8(doublesize);
  1856     check_ralign32( R_EAX );
  1857     MMU_TRANSLATE_READ( R_EAX );
  1858     MEM_READ_LONG( R_EAX, R_EAX );
  1859     store_fr( R_EAX, FRn );
  1860     JMP_rel8(end);
  1862     JMP_TARGET(doublesize);
  1863     check_ralign64( R_EAX );
  1864     MMU_TRANSLATE_READ( R_EAX );
  1865     MEM_READ_DOUBLE( R_EAX, R_ECX, R_EAX );
  1866     store_dr0( R_ECX, FRn );
  1867     store_dr1( R_EAX, FRn );
  1868     JMP_TARGET(end);
  1869     sh4_x86.tstate = TSTATE_NONE;
  1870 :}
  1871 FMOV FRm, @-Rn {:  
  1872     COUNT_INST(I_FMOV3);
  1873     check_fpuen();
  1874     load_reg( R_EAX, Rn );
  1875     load_spreg( R_EDX, R_FPSCR );
  1876     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1877     JNE_rel8(doublesize);
  1879     check_walign32( R_EAX );
  1880     ADD_imm8s_r32( -4, R_EAX );
  1881     MMU_TRANSLATE_WRITE( R_EAX );
  1882     load_fr( R_ECX, FRm );
  1883     ADD_imm8s_sh4r(-4,REG_OFFSET(r[Rn]));
  1884     MEM_WRITE_LONG( R_EAX, R_ECX );
  1885     JMP_rel8(end);
  1887     JMP_TARGET(doublesize);
  1888     check_walign64( R_EAX );
  1889     ADD_imm8s_r32(-8,R_EAX);
  1890     MMU_TRANSLATE_WRITE( R_EAX );
  1891     load_dr0( R_ECX, FRm );
  1892     load_dr1( R_EDX, FRm );
  1893     ADD_imm8s_sh4r(-8,REG_OFFSET(r[Rn]));
  1894     MEM_WRITE_DOUBLE( R_EAX, R_ECX, R_EDX );
  1895     JMP_TARGET(end);
  1897     sh4_x86.tstate = TSTATE_NONE;
  1898 :}
  1899 FMOV @Rm+, FRn {:
  1900     COUNT_INST(I_FMOV6);
  1901     check_fpuen();
  1902     load_reg( R_EAX, Rm );
  1903     load_spreg( R_EDX, R_FPSCR );
  1904     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1905     JNE_rel8(doublesize);
  1907     check_ralign32( R_EAX );
  1908     MMU_TRANSLATE_READ( R_EAX );
  1909     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  1910     MEM_READ_LONG( R_EAX, R_EAX );
  1911     store_fr( R_EAX, FRn );
  1912     JMP_rel8(end);
  1914     JMP_TARGET(doublesize);
  1915     check_ralign64( R_EAX );
  1916     MMU_TRANSLATE_READ( R_EAX );
  1917     ADD_imm8s_sh4r( 8, REG_OFFSET(r[Rm]) );
  1918     MEM_READ_DOUBLE( R_EAX, R_ECX, R_EAX );
  1919     store_dr0( R_ECX, FRn );
  1920     store_dr1( R_EAX, FRn );
  1921     JMP_TARGET(end);
  1923     sh4_x86.tstate = TSTATE_NONE;
  1924 :}
  1925 FMOV FRm, @(R0, Rn) {:  
  1926     COUNT_INST(I_FMOV4);
  1927     check_fpuen();
  1928     load_reg( R_EAX, Rn );
  1929     ADD_sh4r_r32( REG_OFFSET(r[0]), R_EAX );
  1930     load_spreg( R_EDX, R_FPSCR );
  1931     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1932     JNE_rel8(doublesize);
  1934     check_walign32( R_EAX );
  1935     MMU_TRANSLATE_WRITE( R_EAX );
  1936     load_fr( R_ECX, FRm );
  1937     MEM_WRITE_LONG( R_EAX, R_ECX ); // 12
  1938     JMP_rel8(end);
  1940     JMP_TARGET(doublesize);
  1941     check_walign64( R_EAX );
  1942     MMU_TRANSLATE_WRITE( R_EAX );
  1943     load_dr0( R_ECX, FRm );
  1944     load_dr1( R_EDX, FRm );
  1945     MEM_WRITE_DOUBLE( R_EAX, R_ECX, R_EDX );
  1946     JMP_TARGET(end);
  1948     sh4_x86.tstate = TSTATE_NONE;
  1949 :}
  1950 FMOV @(R0, Rm), FRn {:  
  1951     COUNT_INST(I_FMOV7);
  1952     check_fpuen();
  1953     load_reg( R_EAX, Rm );
  1954     ADD_sh4r_r32( REG_OFFSET(r[0]), R_EAX );
  1955     load_spreg( R_EDX, R_FPSCR );
  1956     TEST_imm32_r32( FPSCR_SZ, R_EDX );
  1957     JNE_rel8(doublesize);
  1959     check_ralign32( R_EAX );
  1960     MMU_TRANSLATE_READ( R_EAX );
  1961     MEM_READ_LONG( R_EAX, R_EAX );
  1962     store_fr( R_EAX, FRn );
  1963     JMP_rel8(end);
  1965     JMP_TARGET(doublesize);
  1966     check_ralign64( R_EAX );
  1967     MMU_TRANSLATE_READ( R_EAX );
  1968     MEM_READ_DOUBLE( R_EAX, R_ECX, R_EAX );
  1969     store_dr0( R_ECX, FRn );
  1970     store_dr1( R_EAX, FRn );
  1971     JMP_TARGET(end);
  1973     sh4_x86.tstate = TSTATE_NONE;
  1974 :}
  1975 FLDI0 FRn {:  /* IFF PR=0 */
  1976     COUNT_INST(I_FLDI0);
  1977     check_fpuen();
  1978     load_spreg( R_ECX, R_FPSCR );
  1979     TEST_imm32_r32( FPSCR_PR, R_ECX );
  1980     JNE_rel8(end);
  1981     XOR_r32_r32( R_EAX, R_EAX );
  1982     store_fr( R_EAX, FRn );
  1983     JMP_TARGET(end);
  1984     sh4_x86.tstate = TSTATE_NONE;
  1985 :}
  1986 FLDI1 FRn {:  /* IFF PR=0 */
  1987     COUNT_INST(I_FLDI1);
  1988     check_fpuen();
  1989     load_spreg( R_ECX, R_FPSCR );
  1990     TEST_imm32_r32( FPSCR_PR, R_ECX );
  1991     JNE_rel8(end);
  1992     load_imm32(R_EAX, 0x3F800000);
  1993     store_fr( R_EAX, FRn );
  1994     JMP_TARGET(end);
  1995     sh4_x86.tstate = TSTATE_NONE;
  1996 :}
  1998 FLOAT FPUL, FRn {:  
  1999     COUNT_INST(I_FLOAT);
  2000     check_fpuen();
  2001     load_spreg( R_ECX, R_FPSCR );
  2002     FILD_sh4r(R_FPUL);
  2003     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2004     JNE_rel8(doubleprec);
  2005     pop_fr( FRn );
  2006     JMP_rel8(end);
  2007     JMP_TARGET(doubleprec);
  2008     pop_dr( FRn );
  2009     JMP_TARGET(end);
  2010     sh4_x86.tstate = TSTATE_NONE;
  2011 :}
  2012 FTRC FRm, FPUL {:  
  2013     COUNT_INST(I_FTRC);
  2014     check_fpuen();
  2015     load_spreg( R_ECX, R_FPSCR );
  2016     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2017     JNE_rel8(doubleprec);
  2018     push_fr( FRm );
  2019     JMP_rel8(doop);
  2020     JMP_TARGET(doubleprec);
  2021     push_dr( FRm );
  2022     JMP_TARGET( doop );
  2023     load_ptr( R_ECX, &max_int );
  2024     FILD_r32ind( R_ECX );
  2025     FCOMIP_st(1);
  2026     JNA_rel8( sat );
  2027     load_ptr( R_ECX, &min_int );  // 5
  2028     FILD_r32ind( R_ECX );           // 2
  2029     FCOMIP_st(1);                   // 2
  2030     JAE_rel8( sat2 );            // 2
  2031     load_ptr( R_EAX, &save_fcw );
  2032     FNSTCW_r32ind( R_EAX );
  2033     load_ptr( R_EDX, &trunc_fcw );
  2034     FLDCW_r32ind( R_EDX );
  2035     FISTP_sh4r(R_FPUL);             // 3
  2036     FLDCW_r32ind( R_EAX );
  2037     JMP_rel8(end);             // 2
  2039     JMP_TARGET(sat);
  2040     JMP_TARGET(sat2);
  2041     MOV_r32ind_r32( R_ECX, R_ECX ); // 2
  2042     store_spreg( R_ECX, R_FPUL );
  2043     FPOP_st();
  2044     JMP_TARGET(end);
  2045     sh4_x86.tstate = TSTATE_NONE;
  2046 :}
  2047 FLDS FRm, FPUL {:  
  2048     COUNT_INST(I_FLDS);
  2049     check_fpuen();
  2050     load_fr( R_EAX, FRm );
  2051     store_spreg( R_EAX, R_FPUL );
  2052     sh4_x86.tstate = TSTATE_NONE;
  2053 :}
  2054 FSTS FPUL, FRn {:  
  2055     COUNT_INST(I_FSTS);
  2056     check_fpuen();
  2057     load_spreg( R_EAX, R_FPUL );
  2058     store_fr( R_EAX, FRn );
  2059     sh4_x86.tstate = TSTATE_NONE;
  2060 :}
  2061 FCNVDS FRm, FPUL {:  
  2062     COUNT_INST(I_FCNVDS);
  2063     check_fpuen();
  2064     load_spreg( R_ECX, R_FPSCR );
  2065     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2066     JE_rel8(end); // only when PR=1
  2067     push_dr( FRm );
  2068     pop_fpul();
  2069     JMP_TARGET(end);
  2070     sh4_x86.tstate = TSTATE_NONE;
  2071 :}
  2072 FCNVSD FPUL, FRn {:  
  2073     COUNT_INST(I_FCNVSD);
  2074     check_fpuen();
  2075     load_spreg( R_ECX, R_FPSCR );
  2076     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2077     JE_rel8(end); // only when PR=1
  2078     push_fpul();
  2079     pop_dr( FRn );
  2080     JMP_TARGET(end);
  2081     sh4_x86.tstate = TSTATE_NONE;
  2082 :}
  2084 /* Floating point instructions */
  2085 FABS FRn {:  
  2086     COUNT_INST(I_FABS);
  2087     check_fpuen();
  2088     load_spreg( R_ECX, R_FPSCR );
  2089     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2090     JNE_rel8(doubleprec);
  2091     push_fr(FRn); // 6
  2092     FABS_st0(); // 2
  2093     pop_fr(FRn); //6
  2094     JMP_rel8(end); // 2
  2095     JMP_TARGET(doubleprec);
  2096     push_dr(FRn);
  2097     FABS_st0();
  2098     pop_dr(FRn);
  2099     JMP_TARGET(end);
  2100     sh4_x86.tstate = TSTATE_NONE;
  2101 :}
  2102 FADD FRm, FRn {:  
  2103     COUNT_INST(I_FADD);
  2104     check_fpuen();
  2105     load_spreg( R_ECX, R_FPSCR );
  2106     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2107     JNE_rel8(doubleprec);
  2108     push_fr(FRm);
  2109     push_fr(FRn);
  2110     FADDP_st(1);
  2111     pop_fr(FRn);
  2112     JMP_rel8(end);
  2113     JMP_TARGET(doubleprec);
  2114     push_dr(FRm);
  2115     push_dr(FRn);
  2116     FADDP_st(1);
  2117     pop_dr(FRn);
  2118     JMP_TARGET(end);
  2119     sh4_x86.tstate = TSTATE_NONE;
  2120 :}
  2121 FDIV FRm, FRn {:  
  2122     COUNT_INST(I_FDIV);
  2123     check_fpuen();
  2124     load_spreg( R_ECX, R_FPSCR );
  2125     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2126     JNE_rel8(doubleprec);
  2127     push_fr(FRn);
  2128     push_fr(FRm);
  2129     FDIVP_st(1);
  2130     pop_fr(FRn);
  2131     JMP_rel8(end);
  2132     JMP_TARGET(doubleprec);
  2133     push_dr(FRn);
  2134     push_dr(FRm);
  2135     FDIVP_st(1);
  2136     pop_dr(FRn);
  2137     JMP_TARGET(end);
  2138     sh4_x86.tstate = TSTATE_NONE;
  2139 :}
  2140 FMAC FR0, FRm, FRn {:  
  2141     COUNT_INST(I_FMAC);
  2142     check_fpuen();
  2143     load_spreg( R_ECX, R_FPSCR );
  2144     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2145     JNE_rel8(doubleprec);
  2146     push_fr( 0 );
  2147     push_fr( FRm );
  2148     FMULP_st(1);
  2149     push_fr( FRn );
  2150     FADDP_st(1);
  2151     pop_fr( FRn );
  2152     JMP_rel8(end);
  2153     JMP_TARGET(doubleprec);
  2154     push_dr( 0 );
  2155     push_dr( FRm );
  2156     FMULP_st(1);
  2157     push_dr( FRn );
  2158     FADDP_st(1);
  2159     pop_dr( FRn );
  2160     JMP_TARGET(end);
  2161     sh4_x86.tstate = TSTATE_NONE;
  2162 :}
  2164 FMUL FRm, FRn {:  
  2165     COUNT_INST(I_FMUL);
  2166     check_fpuen();
  2167     load_spreg( R_ECX, R_FPSCR );
  2168     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2169     JNE_rel8(doubleprec);
  2170     push_fr(FRm);
  2171     push_fr(FRn);
  2172     FMULP_st(1);
  2173     pop_fr(FRn);
  2174     JMP_rel8(end);
  2175     JMP_TARGET(doubleprec);
  2176     push_dr(FRm);
  2177     push_dr(FRn);
  2178     FMULP_st(1);
  2179     pop_dr(FRn);
  2180     JMP_TARGET(end);
  2181     sh4_x86.tstate = TSTATE_NONE;
  2182 :}
  2183 FNEG FRn {:  
  2184     COUNT_INST(I_FNEG);
  2185     check_fpuen();
  2186     load_spreg( R_ECX, R_FPSCR );
  2187     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2188     JNE_rel8(doubleprec);
  2189     push_fr(FRn);
  2190     FCHS_st0();
  2191     pop_fr(FRn);
  2192     JMP_rel8(end);
  2193     JMP_TARGET(doubleprec);
  2194     push_dr(FRn);
  2195     FCHS_st0();
  2196     pop_dr(FRn);
  2197     JMP_TARGET(end);
  2198     sh4_x86.tstate = TSTATE_NONE;
  2199 :}
  2200 FSRRA FRn {:  
  2201     COUNT_INST(I_FSRRA);
  2202     check_fpuen();
  2203     load_spreg( R_ECX, R_FPSCR );
  2204     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2205     JNE_rel8(end); // PR=0 only
  2206     FLD1_st0();
  2207     push_fr(FRn);
  2208     FSQRT_st0();
  2209     FDIVP_st(1);
  2210     pop_fr(FRn);
  2211     JMP_TARGET(end);
  2212     sh4_x86.tstate = TSTATE_NONE;
  2213 :}
  2214 FSQRT FRn {:  
  2215     COUNT_INST(I_FSQRT);
  2216     check_fpuen();
  2217     load_spreg( R_ECX, R_FPSCR );
  2218     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2219     JNE_rel8(doubleprec);
  2220     push_fr(FRn);
  2221     FSQRT_st0();
  2222     pop_fr(FRn);
  2223     JMP_rel8(end);
  2224     JMP_TARGET(doubleprec);
  2225     push_dr(FRn);
  2226     FSQRT_st0();
  2227     pop_dr(FRn);
  2228     JMP_TARGET(end);
  2229     sh4_x86.tstate = TSTATE_NONE;
  2230 :}
  2231 FSUB FRm, FRn {:  
  2232     COUNT_INST(I_FSUB);
  2233     check_fpuen();
  2234     load_spreg( R_ECX, R_FPSCR );
  2235     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2236     JNE_rel8(doubleprec);
  2237     push_fr(FRn);
  2238     push_fr(FRm);
  2239     FSUBP_st(1);
  2240     pop_fr(FRn);
  2241     JMP_rel8(end);
  2242     JMP_TARGET(doubleprec);
  2243     push_dr(FRn);
  2244     push_dr(FRm);
  2245     FSUBP_st(1);
  2246     pop_dr(FRn);
  2247     JMP_TARGET(end);
  2248     sh4_x86.tstate = TSTATE_NONE;
  2249 :}
  2251 FCMP/EQ FRm, FRn {:  
  2252     COUNT_INST(I_FCMPEQ);
  2253     check_fpuen();
  2254     load_spreg( R_ECX, R_FPSCR );
  2255     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2256     JNE_rel8(doubleprec);
  2257     push_fr(FRm);
  2258     push_fr(FRn);
  2259     JMP_rel8(end);
  2260     JMP_TARGET(doubleprec);
  2261     push_dr(FRm);
  2262     push_dr(FRn);
  2263     JMP_TARGET(end);
  2264     FCOMIP_st(1);
  2265     SETE_t();
  2266     FPOP_st();
  2267     sh4_x86.tstate = TSTATE_NONE;
  2268 :}
  2269 FCMP/GT FRm, FRn {:  
  2270     COUNT_INST(I_FCMPGT);
  2271     check_fpuen();
  2272     load_spreg( R_ECX, R_FPSCR );
  2273     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2274     JNE_rel8(doubleprec);
  2275     push_fr(FRm);
  2276     push_fr(FRn);
  2277     JMP_rel8(end);
  2278     JMP_TARGET(doubleprec);
  2279     push_dr(FRm);
  2280     push_dr(FRn);
  2281     JMP_TARGET(end);
  2282     FCOMIP_st(1);
  2283     SETA_t();
  2284     FPOP_st();
  2285     sh4_x86.tstate = TSTATE_NONE;
  2286 :}
  2288 FSCA FPUL, FRn {:  
  2289     COUNT_INST(I_FSCA);
  2290     check_fpuen();
  2291     load_spreg( R_ECX, R_FPSCR );
  2292     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2293     JNE_rel8(doubleprec );
  2294     LEA_sh4r_rptr( REG_OFFSET(fr[0][FRn&0x0E]), R_ECX );
  2295     load_spreg( R_EDX, R_FPUL );
  2296     call_func2( sh4_fsca, R_EDX, R_ECX );
  2297     JMP_TARGET(doubleprec);
  2298     sh4_x86.tstate = TSTATE_NONE;
  2299 :}
  2300 FIPR FVm, FVn {:  
  2301     COUNT_INST(I_FIPR);
  2302     check_fpuen();
  2303     load_spreg( R_ECX, R_FPSCR );
  2304     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2305     JNE_rel8( doubleprec);
  2307     push_fr( FVm<<2 );
  2308     push_fr( FVn<<2 );
  2309     FMULP_st(1);
  2310     push_fr( (FVm<<2)+1);
  2311     push_fr( (FVn<<2)+1);
  2312     FMULP_st(1);
  2313     FADDP_st(1);
  2314     push_fr( (FVm<<2)+2);
  2315     push_fr( (FVn<<2)+2);
  2316     FMULP_st(1);
  2317     FADDP_st(1);
  2318     push_fr( (FVm<<2)+3);
  2319     push_fr( (FVn<<2)+3);
  2320     FMULP_st(1);
  2321     FADDP_st(1);
  2322     pop_fr( (FVn<<2)+3);
  2323     JMP_TARGET(doubleprec);
  2324     sh4_x86.tstate = TSTATE_NONE;
  2325 :}
  2326 FTRV XMTRX, FVn {:  
  2327     COUNT_INST(I_FTRV);
  2328     check_fpuen();
  2329     load_spreg( R_ECX, R_FPSCR );
  2330     TEST_imm32_r32( FPSCR_PR, R_ECX );
  2331     JNE_rel8( doubleprec );
  2332     LEA_sh4r_rptr( REG_OFFSET(fr[0][FVn<<2]), R_EDX );
  2333     call_func1( sh4_ftrv, R_EDX );  // 12
  2334     JMP_TARGET(doubleprec);
  2335     sh4_x86.tstate = TSTATE_NONE;
  2336 :}
  2338 FRCHG {:  
  2339     COUNT_INST(I_FRCHG);
  2340     check_fpuen();
  2341     load_spreg( R_ECX, R_FPSCR );
  2342     XOR_imm32_r32( FPSCR_FR, R_ECX );
  2343     store_spreg( R_ECX, R_FPSCR );
  2344     call_func0( sh4_switch_fr_banks );
  2345     sh4_x86.tstate = TSTATE_NONE;
  2346 :}
  2347 FSCHG {:  
  2348     COUNT_INST(I_FSCHG);
  2349     check_fpuen();
  2350     load_spreg( R_ECX, R_FPSCR );
  2351     XOR_imm32_r32( FPSCR_SZ, R_ECX );
  2352     store_spreg( R_ECX, R_FPSCR );
  2353     sh4_x86.tstate = TSTATE_NONE;
  2354 :}
  2356 /* Processor control instructions */
  2357 LDC Rm, SR {:
  2358     COUNT_INST(I_LDCSR);
  2359     if( sh4_x86.in_delay_slot ) {
  2360 	SLOTILLEGAL();
  2361     } else {
  2362 	check_priv();
  2363 	load_reg( R_EAX, Rm );
  2364 	call_func1( sh4_write_sr, R_EAX );
  2365 	sh4_x86.priv_checked = FALSE;
  2366 	sh4_x86.fpuen_checked = FALSE;
  2367 	sh4_x86.tstate = TSTATE_NONE;
  2369 :}
  2370 LDC Rm, GBR {: 
  2371     COUNT_INST(I_LDC);
  2372     load_reg( R_EAX, Rm );
  2373     store_spreg( R_EAX, R_GBR );
  2374 :}
  2375 LDC Rm, VBR {:  
  2376     COUNT_INST(I_LDC);
  2377     check_priv();
  2378     load_reg( R_EAX, Rm );
  2379     store_spreg( R_EAX, R_VBR );
  2380     sh4_x86.tstate = TSTATE_NONE;
  2381 :}
  2382 LDC Rm, SSR {:  
  2383     COUNT_INST(I_LDC);
  2384     check_priv();
  2385     load_reg( R_EAX, Rm );
  2386     store_spreg( R_EAX, R_SSR );
  2387     sh4_x86.tstate = TSTATE_NONE;
  2388 :}
  2389 LDC Rm, SGR {:  
  2390     COUNT_INST(I_LDC);
  2391     check_priv();
  2392     load_reg( R_EAX, Rm );
  2393     store_spreg( R_EAX, R_SGR );
  2394     sh4_x86.tstate = TSTATE_NONE;
  2395 :}
  2396 LDC Rm, SPC {:  
  2397     COUNT_INST(I_LDC);
  2398     check_priv();
  2399     load_reg( R_EAX, Rm );
  2400     store_spreg( R_EAX, R_SPC );
  2401     sh4_x86.tstate = TSTATE_NONE;
  2402 :}
  2403 LDC Rm, DBR {:  
  2404     COUNT_INST(I_LDC);
  2405     check_priv();
  2406     load_reg( R_EAX, Rm );
  2407     store_spreg( R_EAX, R_DBR );
  2408     sh4_x86.tstate = TSTATE_NONE;
  2409 :}
  2410 LDC Rm, Rn_BANK {:  
  2411     COUNT_INST(I_LDC);
  2412     check_priv();
  2413     load_reg( R_EAX, Rm );
  2414     store_spreg( R_EAX, REG_OFFSET(r_bank[Rn_BANK]) );
  2415     sh4_x86.tstate = TSTATE_NONE;
  2416 :}
  2417 LDC.L @Rm+, GBR {:  
  2418     COUNT_INST(I_LDCM);
  2419     load_reg( R_EAX, Rm );
  2420     check_ralign32( R_EAX );
  2421     MMU_TRANSLATE_READ( R_EAX );
  2422     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2423     MEM_READ_LONG( R_EAX, R_EAX );
  2424     store_spreg( R_EAX, R_GBR );
  2425     sh4_x86.tstate = TSTATE_NONE;
  2426 :}
  2427 LDC.L @Rm+, SR {:
  2428     COUNT_INST(I_LDCSRM);
  2429     if( sh4_x86.in_delay_slot ) {
  2430 	SLOTILLEGAL();
  2431     } else {
  2432 	check_priv();
  2433 	load_reg( R_EAX, Rm );
  2434 	check_ralign32( R_EAX );
  2435 	MMU_TRANSLATE_READ( R_EAX );
  2436 	ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2437 	MEM_READ_LONG( R_EAX, R_EAX );
  2438 	call_func1( sh4_write_sr, R_EAX );
  2439 	sh4_x86.priv_checked = FALSE;
  2440 	sh4_x86.fpuen_checked = FALSE;
  2441 	sh4_x86.tstate = TSTATE_NONE;
  2443 :}
  2444 LDC.L @Rm+, VBR {:  
  2445     COUNT_INST(I_LDCM);
  2446     check_priv();
  2447     load_reg( R_EAX, Rm );
  2448     check_ralign32( R_EAX );
  2449     MMU_TRANSLATE_READ( R_EAX );
  2450     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2451     MEM_READ_LONG( R_EAX, R_EAX );
  2452     store_spreg( R_EAX, R_VBR );
  2453     sh4_x86.tstate = TSTATE_NONE;
  2454 :}
  2455 LDC.L @Rm+, SSR {:
  2456     COUNT_INST(I_LDCM);
  2457     check_priv();
  2458     load_reg( R_EAX, Rm );
  2459     check_ralign32( R_EAX );
  2460     MMU_TRANSLATE_READ( R_EAX );
  2461     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2462     MEM_READ_LONG( R_EAX, R_EAX );
  2463     store_spreg( R_EAX, R_SSR );
  2464     sh4_x86.tstate = TSTATE_NONE;
  2465 :}
  2466 LDC.L @Rm+, SGR {:  
  2467     COUNT_INST(I_LDCM);
  2468     check_priv();
  2469     load_reg( R_EAX, Rm );
  2470     check_ralign32( R_EAX );
  2471     MMU_TRANSLATE_READ( R_EAX );
  2472     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2473     MEM_READ_LONG( R_EAX, R_EAX );
  2474     store_spreg( R_EAX, R_SGR );
  2475     sh4_x86.tstate = TSTATE_NONE;
  2476 :}
  2477 LDC.L @Rm+, SPC {:  
  2478     COUNT_INST(I_LDCM);
  2479     check_priv();
  2480     load_reg( R_EAX, Rm );
  2481     check_ralign32( R_EAX );
  2482     MMU_TRANSLATE_READ( R_EAX );
  2483     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2484     MEM_READ_LONG( R_EAX, R_EAX );
  2485     store_spreg( R_EAX, R_SPC );
  2486     sh4_x86.tstate = TSTATE_NONE;
  2487 :}
  2488 LDC.L @Rm+, DBR {:  
  2489     COUNT_INST(I_LDCM);
  2490     check_priv();
  2491     load_reg( R_EAX, Rm );
  2492     check_ralign32( R_EAX );
  2493     MMU_TRANSLATE_READ( R_EAX );
  2494     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2495     MEM_READ_LONG( R_EAX, R_EAX );
  2496     store_spreg( R_EAX, R_DBR );
  2497     sh4_x86.tstate = TSTATE_NONE;
  2498 :}
  2499 LDC.L @Rm+, Rn_BANK {:  
  2500     COUNT_INST(I_LDCM);
  2501     check_priv();
  2502     load_reg( R_EAX, Rm );
  2503     check_ralign32( R_EAX );
  2504     MMU_TRANSLATE_READ( R_EAX );
  2505     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2506     MEM_READ_LONG( R_EAX, R_EAX );
  2507     store_spreg( R_EAX, REG_OFFSET(r_bank[Rn_BANK]) );
  2508     sh4_x86.tstate = TSTATE_NONE;
  2509 :}
  2510 LDS Rm, FPSCR {:
  2511     COUNT_INST(I_LDSFPSCR);
  2512     check_fpuen();
  2513     load_reg( R_EAX, Rm );
  2514     call_func1( sh4_write_fpscr, R_EAX );
  2515     sh4_x86.tstate = TSTATE_NONE;
  2516 :}
  2517 LDS.L @Rm+, FPSCR {:  
  2518     COUNT_INST(I_LDSFPSCRM);
  2519     check_fpuen();
  2520     load_reg( R_EAX, Rm );
  2521     check_ralign32( R_EAX );
  2522     MMU_TRANSLATE_READ( R_EAX );
  2523     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2524     MEM_READ_LONG( R_EAX, R_EAX );
  2525     call_func1( sh4_write_fpscr, R_EAX );
  2526     sh4_x86.tstate = TSTATE_NONE;
  2527 :}
  2528 LDS Rm, FPUL {:  
  2529     COUNT_INST(I_LDS);
  2530     check_fpuen();
  2531     load_reg( R_EAX, Rm );
  2532     store_spreg( R_EAX, R_FPUL );
  2533 :}
  2534 LDS.L @Rm+, FPUL {:  
  2535     COUNT_INST(I_LDSM);
  2536     check_fpuen();
  2537     load_reg( R_EAX, Rm );
  2538     check_ralign32( R_EAX );
  2539     MMU_TRANSLATE_READ( R_EAX );
  2540     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2541     MEM_READ_LONG( R_EAX, R_EAX );
  2542     store_spreg( R_EAX, R_FPUL );
  2543     sh4_x86.tstate = TSTATE_NONE;
  2544 :}
  2545 LDS Rm, MACH {: 
  2546     COUNT_INST(I_LDS);
  2547     load_reg( R_EAX, Rm );
  2548     store_spreg( R_EAX, R_MACH );
  2549 :}
  2550 LDS.L @Rm+, MACH {:  
  2551     COUNT_INST(I_LDSM);
  2552     load_reg( R_EAX, Rm );
  2553     check_ralign32( R_EAX );
  2554     MMU_TRANSLATE_READ( R_EAX );
  2555     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2556     MEM_READ_LONG( R_EAX, R_EAX );
  2557     store_spreg( R_EAX, R_MACH );
  2558     sh4_x86.tstate = TSTATE_NONE;
  2559 :}
  2560 LDS Rm, MACL {:  
  2561     COUNT_INST(I_LDS);
  2562     load_reg( R_EAX, Rm );
  2563     store_spreg( R_EAX, R_MACL );
  2564 :}
  2565 LDS.L @Rm+, MACL {:  
  2566     COUNT_INST(I_LDSM);
  2567     load_reg( R_EAX, Rm );
  2568     check_ralign32( R_EAX );
  2569     MMU_TRANSLATE_READ( R_EAX );
  2570     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2571     MEM_READ_LONG( R_EAX, R_EAX );
  2572     store_spreg( R_EAX, R_MACL );
  2573     sh4_x86.tstate = TSTATE_NONE;
  2574 :}
  2575 LDS Rm, PR {:  
  2576     COUNT_INST(I_LDS);
  2577     load_reg( R_EAX, Rm );
  2578     store_spreg( R_EAX, R_PR );
  2579 :}
  2580 LDS.L @Rm+, PR {:  
  2581     COUNT_INST(I_LDSM);
  2582     load_reg( R_EAX, Rm );
  2583     check_ralign32( R_EAX );
  2584     MMU_TRANSLATE_READ( R_EAX );
  2585     ADD_imm8s_sh4r( 4, REG_OFFSET(r[Rm]) );
  2586     MEM_READ_LONG( R_EAX, R_EAX );
  2587     store_spreg( R_EAX, R_PR );
  2588     sh4_x86.tstate = TSTATE_NONE;
  2589 :}
  2590 LDTLB {:  
  2591     COUNT_INST(I_LDTLB);
  2592     call_func0( MMU_ldtlb );
  2593     sh4_x86.tstate = TSTATE_NONE;
  2594 :}
  2595 OCBI @Rn {:
  2596     COUNT_INST(I_OCBI);
  2597 :}
  2598 OCBP @Rn {:
  2599     COUNT_INST(I_OCBP);
  2600 :}
  2601 OCBWB @Rn {:
  2602     COUNT_INST(I_OCBWB);
  2603 :}
  2604 PREF @Rn {:
  2605     COUNT_INST(I_PREF);
  2606     load_reg( R_EAX, Rn );
  2607     MOV_r32_r32( R_EAX, R_ECX );
  2608     AND_imm32_r32( 0xFC000000, R_EAX );
  2609     CMP_imm32_r32( 0xE0000000, R_EAX );
  2610     JNE_rel8(end);
  2611     call_func1( sh4_flush_store_queue, R_ECX );
  2612     TEST_r32_r32( R_EAX, R_EAX );
  2613     JE_exc(-1);
  2614     JMP_TARGET(end);
  2615     sh4_x86.tstate = TSTATE_NONE;
  2616 :}
  2617 SLEEP {: 
  2618     COUNT_INST(I_SLEEP);
  2619     check_priv();
  2620     call_func0( sh4_sleep );
  2621     sh4_x86.tstate = TSTATE_NONE;
  2622     sh4_x86.in_delay_slot = DELAY_NONE;
  2623     return 2;
  2624 :}
  2625 STC SR, Rn {:
  2626     COUNT_INST(I_STCSR);
  2627     check_priv();
  2628     call_func0(sh4_read_sr);
  2629     store_reg( R_EAX, Rn );
  2630     sh4_x86.tstate = TSTATE_NONE;
  2631 :}
  2632 STC GBR, Rn {:  
  2633     COUNT_INST(I_STC);
  2634     load_spreg( R_EAX, R_GBR );
  2635     store_reg( R_EAX, Rn );
  2636 :}
  2637 STC VBR, Rn {:  
  2638     COUNT_INST(I_STC);
  2639     check_priv();
  2640     load_spreg( R_EAX, R_VBR );
  2641     store_reg( R_EAX, Rn );
  2642     sh4_x86.tstate = TSTATE_NONE;
  2643 :}
  2644 STC SSR, Rn {:  
  2645     COUNT_INST(I_STC);
  2646     check_priv();
  2647     load_spreg( R_EAX, R_SSR );
  2648     store_reg( R_EAX, Rn );
  2649     sh4_x86.tstate = TSTATE_NONE;
  2650 :}
  2651 STC SPC, Rn {:  
  2652     COUNT_INST(I_STC);
  2653     check_priv();
  2654     load_spreg( R_EAX, R_SPC );
  2655     store_reg( R_EAX, Rn );
  2656     sh4_x86.tstate = TSTATE_NONE;
  2657 :}
  2658 STC SGR, Rn {:  
  2659     COUNT_INST(I_STC);
  2660     check_priv();
  2661     load_spreg( R_EAX, R_SGR );
  2662     store_reg( R_EAX, Rn );
  2663     sh4_x86.tstate = TSTATE_NONE;
  2664 :}
  2665 STC DBR, Rn {:  
  2666     COUNT_INST(I_STC);
  2667     check_priv();
  2668     load_spreg( R_EAX, R_DBR );
  2669     store_reg( R_EAX, Rn );
  2670     sh4_x86.tstate = TSTATE_NONE;
  2671 :}
  2672 STC Rm_BANK, Rn {:
  2673     COUNT_INST(I_STC);
  2674     check_priv();
  2675     load_spreg( R_EAX, REG_OFFSET(r_bank[Rm_BANK]) );
  2676     store_reg( R_EAX, Rn );
  2677     sh4_x86.tstate = TSTATE_NONE;
  2678 :}
  2679 STC.L SR, @-Rn {:
  2680     COUNT_INST(I_STCSRM);
  2681     check_priv();
  2682     load_reg( R_EAX, Rn );
  2683     check_walign32( R_EAX );
  2684     ADD_imm8s_r32( -4, R_EAX );
  2685     MMU_TRANSLATE_WRITE( R_EAX );
  2686     PUSH_realigned_r32( R_EAX );
  2687     call_func0( sh4_read_sr );
  2688     POP_realigned_r32( R_ECX );
  2689     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2690     MEM_WRITE_LONG( R_ECX, R_EAX );
  2691     sh4_x86.tstate = TSTATE_NONE;
  2692 :}
  2693 STC.L VBR, @-Rn {:  
  2694     COUNT_INST(I_STCM);
  2695     check_priv();
  2696     load_reg( R_EAX, Rn );
  2697     check_walign32( R_EAX );
  2698     ADD_imm8s_r32( -4, R_EAX );
  2699     MMU_TRANSLATE_WRITE( R_EAX );
  2700     load_spreg( R_EDX, R_VBR );
  2701     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2702     MEM_WRITE_LONG( R_EAX, R_EDX );
  2703     sh4_x86.tstate = TSTATE_NONE;
  2704 :}
  2705 STC.L SSR, @-Rn {:  
  2706     COUNT_INST(I_STCM);
  2707     check_priv();
  2708     load_reg( R_EAX, Rn );
  2709     check_walign32( R_EAX );
  2710     ADD_imm8s_r32( -4, R_EAX );
  2711     MMU_TRANSLATE_WRITE( R_EAX );
  2712     load_spreg( R_EDX, R_SSR );
  2713     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2714     MEM_WRITE_LONG( R_EAX, R_EDX );
  2715     sh4_x86.tstate = TSTATE_NONE;
  2716 :}
  2717 STC.L SPC, @-Rn {:
  2718     COUNT_INST(I_STCM);
  2719     check_priv();
  2720     load_reg( R_EAX, Rn );
  2721     check_walign32( R_EAX );
  2722     ADD_imm8s_r32( -4, R_EAX );
  2723     MMU_TRANSLATE_WRITE( R_EAX );
  2724     load_spreg( R_EDX, R_SPC );
  2725     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2726     MEM_WRITE_LONG( R_EAX, R_EDX );
  2727     sh4_x86.tstate = TSTATE_NONE;
  2728 :}
  2729 STC.L SGR, @-Rn {:  
  2730     COUNT_INST(I_STCM);
  2731     check_priv();
  2732     load_reg( R_EAX, Rn );
  2733     check_walign32( R_EAX );
  2734     ADD_imm8s_r32( -4, R_EAX );
  2735     MMU_TRANSLATE_WRITE( R_EAX );
  2736     load_spreg( R_EDX, R_SGR );
  2737     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2738     MEM_WRITE_LONG( R_EAX, R_EDX );
  2739     sh4_x86.tstate = TSTATE_NONE;
  2740 :}
  2741 STC.L DBR, @-Rn {:  
  2742     COUNT_INST(I_STCM);
  2743     check_priv();
  2744     load_reg( R_EAX, Rn );
  2745     check_walign32( R_EAX );
  2746     ADD_imm8s_r32( -4, R_EAX );
  2747     MMU_TRANSLATE_WRITE( R_EAX );
  2748     load_spreg( R_EDX, R_DBR );
  2749     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2750     MEM_WRITE_LONG( R_EAX, R_EDX );
  2751     sh4_x86.tstate = TSTATE_NONE;
  2752 :}
  2753 STC.L Rm_BANK, @-Rn {:  
  2754     COUNT_INST(I_STCM);
  2755     check_priv();
  2756     load_reg( R_EAX, Rn );
  2757     check_walign32( R_EAX );
  2758     ADD_imm8s_r32( -4, R_EAX );
  2759     MMU_TRANSLATE_WRITE( R_EAX );
  2760     load_spreg( R_EDX, REG_OFFSET(r_bank[Rm_BANK]) );
  2761     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2762     MEM_WRITE_LONG( R_EAX, R_EDX );
  2763     sh4_x86.tstate = TSTATE_NONE;
  2764 :}
  2765 STC.L GBR, @-Rn {:  
  2766     COUNT_INST(I_STCM);
  2767     load_reg( R_EAX, Rn );
  2768     check_walign32( R_EAX );
  2769     ADD_imm8s_r32( -4, R_EAX );
  2770     MMU_TRANSLATE_WRITE( R_EAX );
  2771     load_spreg( R_EDX, R_GBR );
  2772     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2773     MEM_WRITE_LONG( R_EAX, R_EDX );
  2774     sh4_x86.tstate = TSTATE_NONE;
  2775 :}
  2776 STS FPSCR, Rn {:  
  2777     COUNT_INST(I_STSFPSCR);
  2778     check_fpuen();
  2779     load_spreg( R_EAX, R_FPSCR );
  2780     store_reg( R_EAX, Rn );
  2781 :}
  2782 STS.L FPSCR, @-Rn {:  
  2783     COUNT_INST(I_STSFPSCRM);
  2784     check_fpuen();
  2785     load_reg( R_EAX, Rn );
  2786     check_walign32( R_EAX );
  2787     ADD_imm8s_r32( -4, R_EAX );
  2788     MMU_TRANSLATE_WRITE( R_EAX );
  2789     load_spreg( R_EDX, R_FPSCR );
  2790     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2791     MEM_WRITE_LONG( R_EAX, R_EDX );
  2792     sh4_x86.tstate = TSTATE_NONE;
  2793 :}
  2794 STS FPUL, Rn {:  
  2795     COUNT_INST(I_STS);
  2796     check_fpuen();
  2797     load_spreg( R_EAX, R_FPUL );
  2798     store_reg( R_EAX, Rn );
  2799 :}
  2800 STS.L FPUL, @-Rn {:  
  2801     COUNT_INST(I_STSM);
  2802     check_fpuen();
  2803     load_reg( R_EAX, Rn );
  2804     check_walign32( R_EAX );
  2805     ADD_imm8s_r32( -4, R_EAX );
  2806     MMU_TRANSLATE_WRITE( R_EAX );
  2807     load_spreg( R_EDX, R_FPUL );
  2808     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2809     MEM_WRITE_LONG( R_EAX, R_EDX );
  2810     sh4_x86.tstate = TSTATE_NONE;
  2811 :}
  2812 STS MACH, Rn {:  
  2813     COUNT_INST(I_STS);
  2814     load_spreg( R_EAX, R_MACH );
  2815     store_reg( R_EAX, Rn );
  2816 :}
  2817 STS.L MACH, @-Rn {:  
  2818     COUNT_INST(I_STSM);
  2819     load_reg( R_EAX, Rn );
  2820     check_walign32( R_EAX );
  2821     ADD_imm8s_r32( -4, R_EAX );
  2822     MMU_TRANSLATE_WRITE( R_EAX );
  2823     load_spreg( R_EDX, R_MACH );
  2824     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2825     MEM_WRITE_LONG( R_EAX, R_EDX );
  2826     sh4_x86.tstate = TSTATE_NONE;
  2827 :}
  2828 STS MACL, Rn {:  
  2829     COUNT_INST(I_STS);
  2830     load_spreg( R_EAX, R_MACL );
  2831     store_reg( R_EAX, Rn );
  2832 :}
  2833 STS.L MACL, @-Rn {:  
  2834     COUNT_INST(I_STSM);
  2835     load_reg( R_EAX, Rn );
  2836     check_walign32( R_EAX );
  2837     ADD_imm8s_r32( -4, R_EAX );
  2838     MMU_TRANSLATE_WRITE( R_EAX );
  2839     load_spreg( R_EDX, R_MACL );
  2840     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2841     MEM_WRITE_LONG( R_EAX, R_EDX );
  2842     sh4_x86.tstate = TSTATE_NONE;
  2843 :}
  2844 STS PR, Rn {:  
  2845     COUNT_INST(I_STS);
  2846     load_spreg( R_EAX, R_PR );
  2847     store_reg( R_EAX, Rn );
  2848 :}
  2849 STS.L PR, @-Rn {:  
  2850     COUNT_INST(I_STSM);
  2851     load_reg( R_EAX, Rn );
  2852     check_walign32( R_EAX );
  2853     ADD_imm8s_r32( -4, R_EAX );
  2854     MMU_TRANSLATE_WRITE( R_EAX );
  2855     load_spreg( R_EDX, R_PR );
  2856     ADD_imm8s_sh4r( -4, REG_OFFSET(r[Rn]) );
  2857     MEM_WRITE_LONG( R_EAX, R_EDX );
  2858     sh4_x86.tstate = TSTATE_NONE;
  2859 :}
  2861 NOP {: 
  2862     COUNT_INST(I_NOP);
  2863     /* Do nothing. Well, we could emit an 0x90, but what would really be the point? */ 
  2864 :}
  2865 %%
  2866     sh4_x86.in_delay_slot = DELAY_NONE;
  2867     return 0;
.