Search
lxdream.org :: lxdream/src/sh4/sh4core.in
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4core.in
changeset 582:c89a69dc427d
prev576:4945fa2ed24f
next1065:bc1cc0c54917
author nkeynes
date Tue Jan 15 10:03:27 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Fix emu instruction translation
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * SH4 emulation core, and parent module for all the SH4 peripheral
     5  * modules.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #define MODULE sh4_module
    21 #include <assert.h>
    22 #include <math.h>
    23 #include "dream.h"
    24 #include "dreamcast.h"
    25 #include "eventq.h"
    26 #include "mem.h"
    27 #include "clock.h"
    28 #include "syscall.h"
    29 #include "sh4/sh4core.h"
    30 #include "sh4/sh4mmio.h"
    31 #include "sh4/intc.h"
    33 #define SH4_CALLTRACE 1
    35 #define MAX_INT 0x7FFFFFFF
    36 #define MIN_INT 0x80000000
    37 #define MAX_INTF 2147483647.0
    38 #define MIN_INTF -2147483648.0
    40 /********************** SH4 Module Definition ****************************/
    42 uint32_t sh4_run_slice( uint32_t nanosecs ) 
    43 {
    44     int i;
    45     sh4r.slice_cycle = 0;
    47     if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
    48 	if( sh4r.event_pending < nanosecs ) {
    49 	    sh4r.sh4_state = SH4_STATE_RUNNING;
    50 	    sh4r.slice_cycle = sh4r.event_pending;
    51 	}
    52     }
    54     if( sh4_breakpoint_count == 0 ) {
    55 	for( ; sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
    56 	    if( SH4_EVENT_PENDING() ) {
    57 		if( sh4r.event_types & PENDING_EVENT ) {
    58 		    event_execute();
    59 		}
    60 		/* Eventq execute may (quite likely) deliver an immediate IRQ */
    61 		if( sh4r.event_types & PENDING_IRQ ) {
    62 		    sh4_accept_interrupt();
    63 		}
    64 	    }
    65 	    if( !sh4_execute_instruction() ) {
    66 		break;
    67 	    }
    68 	}
    69     } else {
    70 	for( ;sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
    71 	    if( SH4_EVENT_PENDING() ) {
    72 		if( sh4r.event_types & PENDING_EVENT ) {
    73 		    event_execute();
    74 		}
    75 		/* Eventq execute may (quite likely) deliver an immediate IRQ */
    76 		if( sh4r.event_types & PENDING_IRQ ) {
    77 		    sh4_accept_interrupt();
    78 		}
    79 	    }
    81 	    if( !sh4_execute_instruction() )
    82 		break;
    83 #ifdef ENABLE_DEBUG_MODE
    84 	    for( i=0; i<sh4_breakpoint_count; i++ ) {
    85 		if( sh4_breakpoints[i].address == sh4r.pc ) {
    86 		    break;
    87 		}
    88 	    }
    89 	    if( i != sh4_breakpoint_count ) {
    90 		dreamcast_stop();
    91 		if( sh4_breakpoints[i].type == BREAK_ONESHOT )
    92 		    sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
    93 		break;
    94 	    }
    95 #endif	
    96 	}
    97     }
    99     /* If we aborted early, but the cpu is still technically running,
   100      * we're doing a hard abort - cut the timeslice back to what we
   101      * actually executed
   102      */
   103     if( sh4r.slice_cycle != nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
   104 	nanosecs = sh4r.slice_cycle;
   105     }
   106     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
   107 	TMU_run_slice( nanosecs );
   108 	SCIF_run_slice( nanosecs );
   109     }
   110     return nanosecs;
   111 }
   113 /********************** SH4 emulation core  ****************************/
   115 #define UNDEF(ir) return sh4_raise_slot_exception(EXC_ILLEGAL, EXC_SLOT_ILLEGAL)
   116 #define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop(); return FALSE; }while(0)
   118 #if(SH4_CALLTRACE == 1)
   119 #define MAX_CALLSTACK 32
   120 static struct call_stack {
   121     sh4addr_t call_addr;
   122     sh4addr_t target_addr;
   123     sh4addr_t stack_pointer;
   124 } call_stack[MAX_CALLSTACK];
   126 static int call_stack_depth = 0;
   127 int sh4_call_trace_on = 0;
   129 static inline void trace_call( sh4addr_t source, sh4addr_t dest ) 
   130 {
   131     if( call_stack_depth < MAX_CALLSTACK ) {
   132 	call_stack[call_stack_depth].call_addr = source;
   133 	call_stack[call_stack_depth].target_addr = dest;
   134 	call_stack[call_stack_depth].stack_pointer = sh4r.r[15];
   135     }
   136     call_stack_depth++;
   137 }
   139 static inline void trace_return( sh4addr_t source, sh4addr_t dest )
   140 {
   141     if( call_stack_depth > 0 ) {
   142 	call_stack_depth--;
   143     }
   144 }
   146 void fprint_stack_trace( FILE *f )
   147 {
   148     int i = call_stack_depth -1;
   149     if( i >= MAX_CALLSTACK )
   150 	i = MAX_CALLSTACK - 1;
   151     for( ; i >= 0; i-- ) {
   152 	fprintf( f, "%d. Call from %08X => %08X, SP=%08X\n", 
   153 		 (call_stack_depth - i), call_stack[i].call_addr,
   154 		 call_stack[i].target_addr, call_stack[i].stack_pointer );
   155     }
   156 }
   158 #define TRACE_CALL( source, dest ) trace_call(source, dest)
   159 #define TRACE_RETURN( source, dest ) trace_return(source, dest)
   160 #else
   161 #define TRACE_CALL( dest, rts ) 
   162 #define TRACE_RETURN( source, dest )
   163 #endif
   165 #define MEM_READ_BYTE( addr, val ) memtmp = mmu_vma_to_phys_read(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { val = sh4_read_byte(memtmp); }
   166 #define MEM_READ_WORD( addr, val ) memtmp = mmu_vma_to_phys_read(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { val = sh4_read_word(memtmp); }
   167 #define MEM_READ_LONG( addr, val ) memtmp = mmu_vma_to_phys_read(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { val = sh4_read_long(memtmp); }
   168 #define MEM_WRITE_BYTE( addr, val ) memtmp = mmu_vma_to_phys_write(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { sh4_write_byte(memtmp, val); }
   169 #define MEM_WRITE_WORD( addr, val ) memtmp = mmu_vma_to_phys_write(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { sh4_write_word(memtmp, val); }
   170 #define MEM_WRITE_LONG( addr, val ) memtmp = mmu_vma_to_phys_write(addr); if( memtmp == MMU_VMA_ERROR ) { return TRUE; } else { sh4_write_long(memtmp, val); }
   172 #define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
   174 #define MEM_FP_READ( addr, reg ) sh4_read_float( addr, reg );
   175 #define MEM_FP_WRITE( addr, reg ) sh4_write_float( addr, reg );
   177 #define CHECKPRIV() if( !IS_SH4_PRIVMODE() ) return sh4_raise_slot_exception( EXC_ILLEGAL, EXC_SLOT_ILLEGAL )
   178 #define CHECKRALIGN16(addr) if( (addr)&0x01 ) return sh4_raise_exception( EXC_DATA_ADDR_READ )
   179 #define CHECKRALIGN32(addr) if( (addr)&0x03 ) return sh4_raise_exception( EXC_DATA_ADDR_READ )
   180 #define CHECKWALIGN16(addr) if( (addr)&0x01 ) return sh4_raise_exception( EXC_DATA_ADDR_WRITE )
   181 #define CHECKWALIGN32(addr) if( (addr)&0x03 ) return sh4_raise_exception( EXC_DATA_ADDR_WRITE )
   183 #define CHECKFPUEN() if( !IS_FPU_ENABLED() ) { if( ir == 0xFFFD ) { UNDEF(ir); } else { return sh4_raise_slot_exception( EXC_FPU_DISABLED, EXC_SLOT_FPU_DISABLED ); } }
   184 #define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); dreamcast_stop(); return FALSE; }
   185 #define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) return sh4_raise_exception(EXC_SLOT_ILLEGAL)
   187 static void sh4_write_float( uint32_t addr, int reg )
   188 {
   189     if( IS_FPU_DOUBLESIZE() ) {
   190 	if( reg & 1 ) {
   191 	    sh4_write_long( addr, *((uint32_t *)&XF((reg)&0x0E)) );
   192 	    sh4_write_long( addr+4, *((uint32_t *)&XF(reg)) );
   193 	} else {
   194 	    sh4_write_long( addr, *((uint32_t *)&FR(reg)) ); 
   195 	    sh4_write_long( addr+4, *((uint32_t *)&FR((reg)|0x01)) );
   196 	}
   197     } else {
   198 	sh4_write_long( addr, *((uint32_t *)&FR((reg))) );
   199     }
   200 }
   202 static void sh4_read_float( uint32_t addr, int reg )
   203 {
   204     if( IS_FPU_DOUBLESIZE() ) {
   205 	if( reg & 1 ) {
   206 	    *((uint32_t *)&XF((reg) & 0x0E)) = sh4_read_long(addr);
   207 	    *((uint32_t *)&XF(reg)) = sh4_read_long(addr+4);
   208 	} else {
   209 	    *((uint32_t *)&FR(reg)) = sh4_read_long(addr);
   210 	    *((uint32_t *)&FR((reg) | 0x01)) = sh4_read_long(addr+4);
   211 	}
   212     } else {
   213 	*((uint32_t *)&FR(reg)) = sh4_read_long(addr);
   214     }
   215 }
   217 gboolean sh4_execute_instruction( void )
   218 {
   219     uint32_t pc;
   220     unsigned short ir;
   221     uint32_t tmp;
   222     float ftmp;
   223     double dtmp;
   224     int64_t memtmp; // temporary holder for memory reads
   226 #define R0 sh4r.r[0]
   227     pc = sh4r.pc;
   228     if( pc > 0xFFFFFF00 ) {
   229 	/* SYSCALL Magic */
   230 	syscall_invoke( pc );
   231 	sh4r.in_delay_slot = 0;
   232 	pc = sh4r.pc = sh4r.pr;
   233 	sh4r.new_pc = sh4r.pc + 2;
   234     }
   235     CHECKRALIGN16(pc);
   237     /* Read instruction */
   238     if( !IS_IN_ICACHE(pc) ) {
   239 	if( !mmu_update_icache(pc) ) {
   240 	    // Fault - look for the fault handler
   241 	    if( !mmu_update_icache(sh4r.pc) ) {
   242 		// double fault - halt
   243 		ERROR( "Double fault - halting" );
   244 		dreamcast_stop();
   245 		return FALSE;
   246 	    }
   247 	}
   248 	pc = sh4r.pc;
   249     }
   250     assert( IS_IN_ICACHE(pc) );
   251     ir = *(uint16_t *)GET_ICACHE_PTR(sh4r.pc);
   252 %%
   253 AND Rm, Rn {: sh4r.r[Rn] &= sh4r.r[Rm]; :}
   254 AND #imm, R0 {: R0 &= imm; :}
   255  AND.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm & tmp ); :}
   256 NOT Rm, Rn {: sh4r.r[Rn] = ~sh4r.r[Rm]; :}
   257 OR Rm, Rn {: sh4r.r[Rn] |= sh4r.r[Rm]; :}
   258 OR #imm, R0  {: R0 |= imm; :}
   259  OR.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm | tmp ); :}
   260 TAS.B @Rn {:
   261     MEM_READ_BYTE( sh4r.r[Rn], tmp );
   262     sh4r.t = ( tmp == 0 ? 1 : 0 );
   263     MEM_WRITE_BYTE( sh4r.r[Rn], tmp | 0x80 );
   264 :}
   265 TST Rm, Rn {: sh4r.t = (sh4r.r[Rn]&sh4r.r[Rm] ? 0 : 1); :}
   266 TST #imm, R0 {: sh4r.t = (R0 & imm ? 0 : 1); :}
   267  TST.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); sh4r.t = ( tmp & imm ? 0 : 1 ); :}
   268 XOR Rm, Rn {: sh4r.r[Rn] ^= sh4r.r[Rm]; :}
   269 XOR #imm, R0 {: R0 ^= imm; :}
   270  XOR.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm ^ tmp ); :}
   271 XTRCT Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rn]>>16) | (sh4r.r[Rm]<<16); :}
   273 ROTL Rn {:
   274     sh4r.t = sh4r.r[Rn] >> 31;
   275     sh4r.r[Rn] <<= 1;
   276     sh4r.r[Rn] |= sh4r.t;
   277 :}
   278 ROTR Rn {:
   279     sh4r.t = sh4r.r[Rn] & 0x00000001;
   280     sh4r.r[Rn] >>= 1;
   281     sh4r.r[Rn] |= (sh4r.t << 31);
   282 :}
   283 ROTCL Rn {:
   284     tmp = sh4r.r[Rn] >> 31;
   285     sh4r.r[Rn] <<= 1;
   286     sh4r.r[Rn] |= sh4r.t;
   287     sh4r.t = tmp;
   288 :}
   289 ROTCR Rn {:
   290     tmp = sh4r.r[Rn] & 0x00000001;
   291     sh4r.r[Rn] >>= 1;
   292     sh4r.r[Rn] |= (sh4r.t << 31 );
   293     sh4r.t = tmp;
   294 :}
   295 SHAD Rm, Rn {:
   296     tmp = sh4r.r[Rm];
   297     if( (tmp & 0x80000000) == 0 ) sh4r.r[Rn] <<= (tmp&0x1f);
   298     else if( (tmp & 0x1F) == 0 )  
   299         sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> 31;
   300     else 
   301 	sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> (((~sh4r.r[Rm]) & 0x1F)+1);
   302 :}
   303 SHLD Rm, Rn {:
   304     tmp = sh4r.r[Rm];
   305     if( (tmp & 0x80000000) == 0 ) sh4r.r[Rn] <<= (tmp&0x1f);
   306     else if( (tmp & 0x1F) == 0 ) sh4r.r[Rn] = 0;
   307     else sh4r.r[Rn] >>= (((~tmp) & 0x1F)+1);
   308 :}
   309 SHAL Rn {:
   310     sh4r.t = sh4r.r[Rn] >> 31;
   311     sh4r.r[Rn] <<= 1;
   312 :}
   313 SHAR Rn {:
   314     sh4r.t = sh4r.r[Rn] & 0x00000001;
   315     sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> 1;
   316 :}
   317 SHLL Rn {: sh4r.t = sh4r.r[Rn] >> 31; sh4r.r[Rn] <<= 1; :}
   318 SHLR Rn {: sh4r.t = sh4r.r[Rn] & 0x00000001; sh4r.r[Rn] >>= 1; :}
   319 SHLL2 Rn {: sh4r.r[Rn] <<= 2; :}
   320 SHLR2 Rn {: sh4r.r[Rn] >>= 2; :}
   321 SHLL8 Rn {: sh4r.r[Rn] <<= 8; :}
   322 SHLR8 Rn {: sh4r.r[Rn] >>= 8; :}
   323 SHLL16 Rn {: sh4r.r[Rn] <<= 16; :}
   324 SHLR16 Rn {: sh4r.r[Rn] >>= 16; :}
   326 EXTU.B Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]&0x000000FF; :}
   327 EXTU.W Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]&0x0000FFFF; :}
   328 EXTS.B Rm, Rn {: sh4r.r[Rn] = SIGNEXT8( sh4r.r[Rm]&0x000000FF ); :}
   329 EXTS.W Rm, Rn {: sh4r.r[Rn] = SIGNEXT16( sh4r.r[Rm]&0x0000FFFF ); :}
   330 SWAP.B Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rm]&0xFFFF0000) | ((sh4r.r[Rm]&0x0000FF00)>>8) | ((sh4r.r[Rm]&0x000000FF)<<8); :}
   331 SWAP.W Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rm]>>16) | (sh4r.r[Rm]<<16); :}
   333 CLRT {: sh4r.t = 0; :}
   334 SETT {: sh4r.t = 1; :}
   335 CLRMAC {: sh4r.mac = 0; :}
   336 LDTLB {: MMU_ldtlb(); :}
   337 CLRS {: sh4r.s = 0; :}
   338 SETS {: sh4r.s = 1; :}
   339 MOVT Rn {: sh4r.r[Rn] = sh4r.t; :}
   340 NOP {: /* NOP */ :}
   342 PREF @Rn {:
   343      tmp = sh4r.r[Rn];
   344      if( (tmp & 0xFC000000) == 0xE0000000 ) {
   345 	 sh4_flush_store_queue(tmp);
   346      }
   347 :}
   348 OCBI @Rn {: :}
   349 OCBP @Rn {: :}
   350 OCBWB @Rn {: :}
   351 MOVCA.L R0, @Rn {:
   352     tmp = sh4r.r[Rn];
   353     CHECKWALIGN32(tmp);
   354     MEM_WRITE_LONG( tmp, R0 );
   355 :}
   356 MOV.B Rm, @(R0, Rn) {: MEM_WRITE_BYTE( R0 + sh4r.r[Rn], sh4r.r[Rm] ); :}
   357 MOV.W Rm, @(R0, Rn) {: 
   358     CHECKWALIGN16( R0 + sh4r.r[Rn] );
   359     MEM_WRITE_WORD( R0 + sh4r.r[Rn], sh4r.r[Rm] );
   360 :}
   361 MOV.L Rm, @(R0, Rn) {:
   362     CHECKWALIGN32( R0 + sh4r.r[Rn] );
   363     MEM_WRITE_LONG( R0 + sh4r.r[Rn], sh4r.r[Rm] );
   364 :}
   365 MOV.B @(R0, Rm), Rn {: MEM_READ_BYTE( R0 + sh4r.r[Rm], sh4r.r[Rn] ); :}
   366 MOV.W @(R0, Rm), Rn {: CHECKRALIGN16( R0 + sh4r.r[Rm] );
   367     MEM_READ_WORD( R0 + sh4r.r[Rm], sh4r.r[Rn] );
   368 :}
   369 MOV.L @(R0, Rm), Rn {: CHECKRALIGN32( R0 + sh4r.r[Rm] );
   370     MEM_READ_LONG( R0 + sh4r.r[Rm], sh4r.r[Rn] );
   371 :}
   372 MOV.L Rm, @(disp, Rn) {:
   373     tmp = sh4r.r[Rn] + disp;
   374     CHECKWALIGN32( tmp );
   375     MEM_WRITE_LONG( tmp, sh4r.r[Rm] );
   376 :}
   377 MOV.B Rm, @Rn {: MEM_WRITE_BYTE( sh4r.r[Rn], sh4r.r[Rm] ); :}
   378 MOV.W Rm, @Rn {: CHECKWALIGN16( sh4r.r[Rn] ); MEM_WRITE_WORD( sh4r.r[Rn], sh4r.r[Rm] ); :}
   379 MOV.L Rm, @Rn {: CHECKWALIGN32( sh4r.r[Rn] ); MEM_WRITE_LONG( sh4r.r[Rn], sh4r.r[Rm] ); :}
   380 MOV.B Rm, @-Rn {: sh4r.r[Rn] --; MEM_WRITE_BYTE( sh4r.r[Rn], sh4r.r[Rm] ); :}
   381 MOV.W Rm, @-Rn {: sh4r.r[Rn] -= 2; CHECKWALIGN16( sh4r.r[Rn] ); MEM_WRITE_WORD( sh4r.r[Rn], sh4r.r[Rm] ); :}
   382 MOV.L Rm, @-Rn {: sh4r.r[Rn] -= 4; CHECKWALIGN32( sh4r.r[Rn] ); MEM_WRITE_LONG( sh4r.r[Rn], sh4r.r[Rm] ); :}
   383 MOV.L @(disp, Rm), Rn {:
   384     tmp = sh4r.r[Rm] + disp;
   385     CHECKRALIGN32( tmp );
   386     MEM_READ_LONG( tmp, sh4r.r[Rn] );
   387 :}
   388 MOV.B @Rm, Rn {: MEM_READ_BYTE( sh4r.r[Rm], sh4r.r[Rn] ); :}
   389  MOV.W @Rm, Rn {: CHECKRALIGN16( sh4r.r[Rm] ); MEM_READ_WORD( sh4r.r[Rm], sh4r.r[Rn] ); :}
   390  MOV.L @Rm, Rn {: CHECKRALIGN32( sh4r.r[Rm] ); MEM_READ_LONG( sh4r.r[Rm], sh4r.r[Rn] ); :}
   391 MOV Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]; :}
   392  MOV.B @Rm+, Rn {: MEM_READ_BYTE( sh4r.r[Rm], sh4r.r[Rn] ); sh4r.r[Rm] ++; :}
   393  MOV.W @Rm+, Rn {: CHECKRALIGN16( sh4r.r[Rm] ); MEM_READ_WORD( sh4r.r[Rm], sh4r.r[Rn] ); sh4r.r[Rm] += 2; :}
   394  MOV.L @Rm+, Rn {: CHECKRALIGN32( sh4r.r[Rm] ); MEM_READ_LONG( sh4r.r[Rm], sh4r.r[Rn] ); sh4r.r[Rm] += 4; :}
   395 MOV.L @(disp, PC), Rn {:
   396     CHECKSLOTILLEGAL();
   397     tmp = (pc&0xFFFFFFFC) + disp + 4;
   398     MEM_READ_LONG( tmp, sh4r.r[Rn] );
   399 :}
   400 MOV.B R0, @(disp, GBR) {: MEM_WRITE_BYTE( sh4r.gbr + disp, R0 ); :}
   401 MOV.W R0, @(disp, GBR) {:
   402     tmp = sh4r.gbr + disp;
   403     CHECKWALIGN16( tmp );
   404     MEM_WRITE_WORD( tmp, R0 );
   405 :}
   406 MOV.L R0, @(disp, GBR) {:
   407     tmp = sh4r.gbr + disp;
   408     CHECKWALIGN32( tmp );
   409     MEM_WRITE_LONG( tmp, R0 );
   410 :}
   411  MOV.B @(disp, GBR), R0 {: MEM_READ_BYTE( sh4r.gbr + disp, R0 ); :}
   412 MOV.W @(disp, GBR), R0 {: 
   413     tmp = sh4r.gbr + disp;
   414     CHECKRALIGN16( tmp );
   415     MEM_READ_WORD( tmp, R0 );
   416 :}
   417 MOV.L @(disp, GBR), R0 {:
   418     tmp = sh4r.gbr + disp;
   419     CHECKRALIGN32( tmp );
   420     MEM_READ_LONG( tmp, R0 );
   421 :}
   422 MOV.B R0, @(disp, Rn) {: MEM_WRITE_BYTE( sh4r.r[Rn] + disp, R0 ); :}
   423 MOV.W R0, @(disp, Rn) {: 
   424     tmp = sh4r.r[Rn] + disp;
   425     CHECKWALIGN16( tmp );
   426     MEM_WRITE_WORD( tmp, R0 );
   427 :}
   428  MOV.B @(disp, Rm), R0 {: MEM_READ_BYTE( sh4r.r[Rm] + disp, R0 ); :}
   429 MOV.W @(disp, Rm), R0 {: 
   430     tmp = sh4r.r[Rm] + disp;
   431     CHECKRALIGN16( tmp );
   432     MEM_READ_WORD( tmp, R0 );
   433 :}
   434 MOV.W @(disp, PC), Rn {:
   435     CHECKSLOTILLEGAL();
   436     tmp = pc + 4 + disp;
   437     MEM_READ_WORD( tmp, sh4r.r[Rn] );
   438 :}
   439 MOVA @(disp, PC), R0 {:
   440     CHECKSLOTILLEGAL();
   441     R0 = (pc&0xFFFFFFFC) + disp + 4;
   442 :}
   443 MOV #imm, Rn {:  sh4r.r[Rn] = imm; :}
   445 CMP/EQ #imm, R0 {: sh4r.t = ( R0 == imm ? 1 : 0 ); :}
   446 CMP/EQ Rm, Rn {: sh4r.t = ( sh4r.r[Rm] == sh4r.r[Rn] ? 1 : 0 ); :}
   447 CMP/GE Rm, Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) >= ((int32_t)sh4r.r[Rm]) ? 1 : 0 ); :}
   448 CMP/GT Rm, Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) > ((int32_t)sh4r.r[Rm]) ? 1 : 0 ); :}
   449 CMP/HI Rm, Rn {: sh4r.t = ( sh4r.r[Rn] > sh4r.r[Rm] ? 1 : 0 ); :}
   450 CMP/HS Rm, Rn {: sh4r.t = ( sh4r.r[Rn] >= sh4r.r[Rm] ? 1 : 0 ); :}
   451 CMP/PL Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) > 0 ? 1 : 0 ); :}
   452 CMP/PZ Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) >= 0 ? 1 : 0 ); :}
   453 CMP/STR Rm, Rn {: 
   454     /* set T = 1 if any byte in RM & RN is the same */
   455     tmp = sh4r.r[Rm] ^ sh4r.r[Rn];
   456     sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
   457              (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
   458 :}
   460 ADD Rm, Rn {: sh4r.r[Rn] += sh4r.r[Rm]; :}
   461 ADD #imm, Rn {: sh4r.r[Rn] += imm; :}
   462 ADDC Rm, Rn {:
   463     tmp = sh4r.r[Rn];
   464     sh4r.r[Rn] += sh4r.r[Rm] + sh4r.t;
   465     sh4r.t = ( sh4r.r[Rn] < tmp || (sh4r.r[Rn] == tmp && sh4r.t != 0) ? 1 : 0 );
   466 :}
   467 ADDV Rm, Rn {:
   468     tmp = sh4r.r[Rn] + sh4r.r[Rm];
   469     sh4r.t = ( (sh4r.r[Rn]>>31) == (sh4r.r[Rm]>>31) && ((sh4r.r[Rn]>>31) != (tmp>>31)) );
   470     sh4r.r[Rn] = tmp;
   471 :}
   472 DIV0U {: sh4r.m = sh4r.q = sh4r.t = 0; :}
   473 DIV0S Rm, Rn {: 
   474     sh4r.q = sh4r.r[Rn]>>31;
   475     sh4r.m = sh4r.r[Rm]>>31;
   476     sh4r.t = sh4r.q ^ sh4r.m;
   477 :}
   478 DIV1 Rm, Rn {:
   479     /* This is derived from the sh4 manual with some simplifications */
   480     uint32_t tmp0, tmp1, tmp2, dir;
   482     dir = sh4r.q ^ sh4r.m;
   483     sh4r.q = (sh4r.r[Rn] >> 31);
   484     tmp2 = sh4r.r[Rm];
   485     sh4r.r[Rn] = (sh4r.r[Rn] << 1) | sh4r.t;
   486     tmp0 = sh4r.r[Rn];
   487     if( dir ) {
   488          sh4r.r[Rn] += tmp2;
   489          tmp1 = (sh4r.r[Rn]<tmp0 ? 1 : 0 );
   490     } else {
   491          sh4r.r[Rn] -= tmp2;
   492          tmp1 = (sh4r.r[Rn]>tmp0 ? 1 : 0 );
   493     }
   494     sh4r.q ^= sh4r.m ^ tmp1;
   495     sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
   496 :}
   497 DMULS.L Rm, Rn {: sh4r.mac = SIGNEXT32(sh4r.r[Rm]) * SIGNEXT32(sh4r.r[Rn]); :}
   498 DMULU.L Rm, Rn {: sh4r.mac = ((uint64_t)sh4r.r[Rm]) * ((uint64_t)sh4r.r[Rn]); :}
   499 DT Rn {:
   500     sh4r.r[Rn] --;
   501     sh4r.t = ( sh4r.r[Rn] == 0 ? 1 : 0 );
   502 :}
   503 MAC.W @Rm+, @Rn+ {:
   504     CHECKRALIGN16( sh4r.r[Rn] );
   505     CHECKRALIGN16( sh4r.r[Rm] );
   506     MEM_READ_WORD(sh4r.r[Rn], tmp);
   507     int32_t stmp = SIGNEXT16(tmp);
   508     sh4r.r[Rn] += 2;
   509     MEM_READ_WORD(sh4r.r[Rm], tmp);
   510     stmp = stmp * SIGNEXT16(tmp);
   511     sh4r.r[Rm] += 2;
   512     if( sh4r.s ) {
   513 	int64_t tmpl = (int64_t)((int32_t)sh4r.mac) + (int64_t)stmp;
   514 	if( tmpl > (int64_t)0x000000007FFFFFFFLL ) {
   515 	    sh4r.mac = 0x000000017FFFFFFFLL;
   516 	} else if( tmpl < (int64_t)0xFFFFFFFF80000000LL ) {
   517 	    sh4r.mac = 0x0000000180000000LL;
   518 	} else {
   519 	    sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
   520 		((uint32_t)(sh4r.mac + stmp));
   521 	}
   522     } else {
   523 	sh4r.mac += SIGNEXT32(stmp);
   524     }
   525 :}
   526 MAC.L @Rm+, @Rn+ {:
   527     CHECKRALIGN32( sh4r.r[Rm] );
   528     CHECKRALIGN32( sh4r.r[Rn] );
   529     MEM_READ_LONG(sh4r.r[Rn], tmp);
   530     int64_t tmpl = SIGNEXT32(tmp);
   531     sh4r.r[Rn] += 4;
   532     MEM_READ_LONG(sh4r.r[Rm], tmp);
   533     tmpl = tmpl * SIGNEXT32(tmp) + sh4r.mac;
   534     sh4r.r[Rm] += 4;
   535     if( sh4r.s ) {
   536         /* 48-bit Saturation. Yuch */
   537         if( tmpl < (int64_t)0xFFFF800000000000LL )
   538             tmpl = 0xFFFF800000000000LL;
   539         else if( tmpl > (int64_t)0x00007FFFFFFFFFFFLL )
   540             tmpl = 0x00007FFFFFFFFFFFLL;
   541     }
   542     sh4r.mac = tmpl;
   543 :}
   544 MUL.L Rm, Rn {: sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   545                         (sh4r.r[Rm] * sh4r.r[Rn]); :}
   546 MULU.W Rm, Rn {:
   547     sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   548                (uint32_t)((sh4r.r[Rm]&0xFFFF) * (sh4r.r[Rn]&0xFFFF));
   549 :}
   550 MULS.W Rm, Rn {:
   551     sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   552                (uint32_t)(SIGNEXT32(sh4r.r[Rm]&0xFFFF) * SIGNEXT32(sh4r.r[Rn]&0xFFFF));
   553 :}
   554 NEGC Rm, Rn {:
   555     tmp = 0 - sh4r.r[Rm];
   556     sh4r.r[Rn] = tmp - sh4r.t;
   557     sh4r.t = ( 0<tmp || tmp<sh4r.r[Rn] ? 1 : 0 );
   558 :}
   559 NEG Rm, Rn {: sh4r.r[Rn] = 0 - sh4r.r[Rm]; :}
   560 SUB Rm, Rn {: sh4r.r[Rn] -= sh4r.r[Rm]; :}
   561 SUBC Rm, Rn {: 
   562     tmp = sh4r.r[Rn];
   563     sh4r.r[Rn] = sh4r.r[Rn] - sh4r.r[Rm] - sh4r.t;
   564     sh4r.t = (sh4r.r[Rn] > tmp || (sh4r.r[Rn] == tmp && sh4r.t == 1));
   565 :}
   567 BRAF Rn {:
   568      CHECKSLOTILLEGAL();
   569      CHECKDEST( pc + 4 + sh4r.r[Rn] );
   570      sh4r.in_delay_slot = 1;
   571      sh4r.pc = sh4r.new_pc;
   572      sh4r.new_pc = pc + 4 + sh4r.r[Rn];
   573      return TRUE;
   574 :}
   575 BSRF Rn {:
   576      CHECKSLOTILLEGAL();
   577      CHECKDEST( pc + 4 + sh4r.r[Rn] );
   578      sh4r.in_delay_slot = 1;
   579      sh4r.pr = sh4r.pc + 4;
   580      sh4r.pc = sh4r.new_pc;
   581      sh4r.new_pc = pc + 4 + sh4r.r[Rn];
   582      TRACE_CALL( pc, sh4r.new_pc );
   583      return TRUE;
   584 :}
   585 BT disp {:
   586     CHECKSLOTILLEGAL();
   587     if( sh4r.t ) {
   588         CHECKDEST( sh4r.pc + disp + 4 )
   589         sh4r.pc += disp + 4;
   590         sh4r.new_pc = sh4r.pc + 2;
   591         return TRUE;
   592     }
   593 :}
   594 BF disp {:
   595     CHECKSLOTILLEGAL();
   596     if( !sh4r.t ) {
   597         CHECKDEST( sh4r.pc + disp + 4 )
   598         sh4r.pc += disp + 4;
   599         sh4r.new_pc = sh4r.pc + 2;
   600         return TRUE;
   601     }
   602 :}
   603 BT/S disp {:
   604     CHECKSLOTILLEGAL();
   605     if( sh4r.t ) {
   606         CHECKDEST( sh4r.pc + disp + 4 )
   607         sh4r.in_delay_slot = 1;
   608         sh4r.pc = sh4r.new_pc;
   609         sh4r.new_pc = pc + disp + 4;
   610         sh4r.in_delay_slot = 1;
   611         return TRUE;
   612     }
   613 :}
   614 BF/S disp {:
   615     CHECKSLOTILLEGAL();
   616     if( !sh4r.t ) {
   617         CHECKDEST( sh4r.pc + disp + 4 )
   618         sh4r.in_delay_slot = 1;
   619         sh4r.pc = sh4r.new_pc;
   620         sh4r.new_pc = pc + disp + 4;
   621         return TRUE;
   622     }
   623 :}
   624 BRA disp {:
   625     CHECKSLOTILLEGAL();
   626     CHECKDEST( sh4r.pc + disp + 4 );
   627     sh4r.in_delay_slot = 1;
   628     sh4r.pc = sh4r.new_pc;
   629     sh4r.new_pc = pc + 4 + disp;
   630     return TRUE;
   631 :}
   632 BSR disp {:
   633     CHECKDEST( sh4r.pc + disp + 4 );
   634     CHECKSLOTILLEGAL();
   635     sh4r.in_delay_slot = 1;
   636     sh4r.pr = pc + 4;
   637     sh4r.pc = sh4r.new_pc;
   638     sh4r.new_pc = pc + 4 + disp;
   639     TRACE_CALL( pc, sh4r.new_pc );
   640     return TRUE;
   641 :}
   642 TRAPA #imm {:
   643     CHECKSLOTILLEGAL();
   644     sh4r.pc += 2;
   645     sh4_raise_trap( imm );
   646     return TRUE;
   647 :}
   648 RTS {: 
   649     CHECKSLOTILLEGAL();
   650     CHECKDEST( sh4r.pr );
   651     sh4r.in_delay_slot = 1;
   652     sh4r.pc = sh4r.new_pc;
   653     sh4r.new_pc = sh4r.pr;
   654     TRACE_RETURN( pc, sh4r.new_pc );
   655     return TRUE;
   656 :}
   657 SLEEP {:
   658     if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
   659 	sh4r.sh4_state = SH4_STATE_STANDBY;
   660     } else {
   661 	sh4r.sh4_state = SH4_STATE_SLEEP;
   662     }
   663     return FALSE; /* Halt CPU */
   664 :}
   665 RTE {:
   666     CHECKPRIV();
   667     CHECKDEST( sh4r.spc );
   668     CHECKSLOTILLEGAL();
   669     sh4r.in_delay_slot = 1;
   670     sh4r.pc = sh4r.new_pc;
   671     sh4r.new_pc = sh4r.spc;
   672     sh4_write_sr( sh4r.ssr );
   673     return TRUE;
   674 :}
   675 JMP @Rn {:
   676     CHECKDEST( sh4r.r[Rn] );
   677     CHECKSLOTILLEGAL();
   678     sh4r.in_delay_slot = 1;
   679     sh4r.pc = sh4r.new_pc;
   680     sh4r.new_pc = sh4r.r[Rn];
   681     return TRUE;
   682 :}
   683 JSR @Rn {:
   684     CHECKDEST( sh4r.r[Rn] );
   685     CHECKSLOTILLEGAL();
   686     sh4r.in_delay_slot = 1;
   687     sh4r.pc = sh4r.new_pc;
   688     sh4r.new_pc = sh4r.r[Rn];
   689     sh4r.pr = pc + 4;
   690     TRACE_CALL( pc, sh4r.new_pc );
   691     return TRUE;
   692 :}
   693 STS MACH, Rn {: sh4r.r[Rn] = (sh4r.mac>>32); :}
   694 STS.L MACH, @-Rn {:
   695     sh4r.r[Rn] -= 4;
   696     CHECKWALIGN32( sh4r.r[Rn] );
   697     MEM_WRITE_LONG( sh4r.r[Rn], (sh4r.mac>>32) );
   698 :}
   699 STC.L SR, @-Rn {:
   700     CHECKPRIV();
   701     sh4r.r[Rn] -= 4;
   702     CHECKWALIGN32( sh4r.r[Rn] );
   703     MEM_WRITE_LONG( sh4r.r[Rn], sh4_read_sr() );
   704 :}
   705 LDS.L @Rm+, MACH {:
   706     CHECKRALIGN32( sh4r.r[Rm] );
   707     MEM_READ_LONG(sh4r.r[Rm], tmp);
   708     sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
   709 	(((uint64_t)tmp)<<32);
   710     sh4r.r[Rm] += 4;
   711 :}
   712 LDC.L @Rm+, SR {:
   713     CHECKSLOTILLEGAL();
   714     CHECKPRIV();
   715     CHECKWALIGN32( sh4r.r[Rm] );
   716     MEM_READ_LONG(sh4r.r[Rm], tmp);
   717     sh4_write_sr( tmp );
   718     sh4r.r[Rm] +=4;
   719 :}
   720 LDS Rm, MACH {:
   721     sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
   722                (((uint64_t)sh4r.r[Rm])<<32);
   723 :}
   724 LDC Rm, SR {:
   725     CHECKSLOTILLEGAL();
   726     CHECKPRIV();
   727     sh4_write_sr( sh4r.r[Rm] );
   728 :}
   729 LDC Rm, SGR {:
   730     CHECKPRIV();
   731     sh4r.sgr = sh4r.r[Rm];
   732 :}
   733 LDC.L @Rm+, SGR {:
   734     CHECKPRIV();
   735     CHECKRALIGN32( sh4r.r[Rm] );
   736     MEM_READ_LONG(sh4r.r[Rm], sh4r.sgr);
   737     sh4r.r[Rm] +=4;
   738 :}
   739 STS MACL, Rn {: sh4r.r[Rn] = (uint32_t)sh4r.mac; :}
   740 STS.L MACL, @-Rn {:
   741     sh4r.r[Rn] -= 4;
   742     CHECKWALIGN32( sh4r.r[Rn] );
   743     MEM_WRITE_LONG( sh4r.r[Rn], (uint32_t)sh4r.mac );
   744 :}
   745 STC.L GBR, @-Rn {:
   746     sh4r.r[Rn] -= 4;
   747     CHECKWALIGN32( sh4r.r[Rn] );
   748     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.gbr );
   749 :}
   750 LDS.L @Rm+, MACL {:
   751     CHECKRALIGN32( sh4r.r[Rm] );
   752     MEM_READ_LONG(sh4r.r[Rm], tmp);
   753     sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
   754                (uint64_t)((uint32_t)tmp);
   755     sh4r.r[Rm] += 4;
   756 :}
   757 LDC.L @Rm+, GBR {:
   758     CHECKRALIGN32( sh4r.r[Rm] );
   759     MEM_READ_LONG(sh4r.r[Rm], sh4r.gbr);
   760     sh4r.r[Rm] +=4;
   761 :}
   762 LDS Rm, MACL {:
   763     sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
   764                (uint64_t)((uint32_t)(sh4r.r[Rm]));
   765 :}
   766 LDC Rm, GBR {: sh4r.gbr = sh4r.r[Rm]; :}
   767 STS PR, Rn {: sh4r.r[Rn] = sh4r.pr; :}
   768 STS.L PR, @-Rn {:
   769     sh4r.r[Rn] -= 4;
   770     CHECKWALIGN32( sh4r.r[Rn] );
   771     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.pr );
   772 :}
   773 STC.L VBR, @-Rn {:
   774     CHECKPRIV();
   775     sh4r.r[Rn] -= 4;
   776     CHECKWALIGN32( sh4r.r[Rn] );
   777     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.vbr );
   778 :}
   779 LDS.L @Rm+, PR {:
   780     CHECKRALIGN32( sh4r.r[Rm] );
   781     MEM_READ_LONG( sh4r.r[Rm], sh4r.pr );
   782     sh4r.r[Rm] += 4;
   783 :}
   784 LDC.L @Rm+, VBR {:
   785     CHECKPRIV();
   786     CHECKRALIGN32( sh4r.r[Rm] );
   787     MEM_READ_LONG(sh4r.r[Rm], sh4r.vbr);
   788     sh4r.r[Rm] +=4;
   789 :}
   790 LDS Rm, PR {: sh4r.pr = sh4r.r[Rm]; :}
   791 LDC Rm, VBR {:
   792     CHECKPRIV();
   793     sh4r.vbr = sh4r.r[Rm];
   794 :}
   795 STC SGR, Rn {:
   796     CHECKPRIV();
   797     sh4r.r[Rn] = sh4r.sgr;
   798 :}
   799 STC.L SGR, @-Rn {:
   800     CHECKPRIV();
   801     sh4r.r[Rn] -= 4;
   802     CHECKWALIGN32( sh4r.r[Rn] );
   803     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.sgr );
   804 :}
   805 STC.L SSR, @-Rn {:
   806     CHECKPRIV();
   807     sh4r.r[Rn] -= 4;
   808     CHECKWALIGN32( sh4r.r[Rn] );
   809     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.ssr );
   810 :}
   811 LDC.L @Rm+, SSR {:
   812     CHECKPRIV();
   813     CHECKRALIGN32( sh4r.r[Rm] );
   814     MEM_READ_LONG(sh4r.r[Rm], sh4r.ssr);
   815     sh4r.r[Rm] +=4;
   816 :}
   817 LDC Rm, SSR {:
   818     CHECKPRIV();
   819     sh4r.ssr = sh4r.r[Rm];
   820 :}
   821 STC.L SPC, @-Rn {:
   822     CHECKPRIV();
   823     sh4r.r[Rn] -= 4;
   824     CHECKWALIGN32( sh4r.r[Rn] );
   825     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.spc );
   826 :}
   827 LDC.L @Rm+, SPC {:
   828     CHECKPRIV();
   829     CHECKRALIGN32( sh4r.r[Rm] );
   830     MEM_READ_LONG(sh4r.r[Rm], sh4r.spc);
   831     sh4r.r[Rm] +=4;
   832 :}
   833 LDC Rm, SPC {:
   834     CHECKPRIV();
   835     sh4r.spc = sh4r.r[Rm];
   836 :}
   837 STS FPUL, Rn {: sh4r.r[Rn] = sh4r.fpul; :}
   838 STS.L FPUL, @-Rn {:
   839     sh4r.r[Rn] -= 4;
   840     CHECKWALIGN32( sh4r.r[Rn] );
   841     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.fpul );
   842 :}
   843 LDS.L @Rm+, FPUL {:
   844     CHECKRALIGN32( sh4r.r[Rm] );
   845     MEM_READ_LONG(sh4r.r[Rm], sh4r.fpul);
   846     sh4r.r[Rm] +=4;
   847 :}
   848 LDS Rm, FPUL {: sh4r.fpul = sh4r.r[Rm]; :}
   849 STS FPSCR, Rn {: sh4r.r[Rn] = sh4r.fpscr; :}
   850 STS.L FPSCR, @-Rn {:
   851     sh4r.r[Rn] -= 4;
   852     CHECKWALIGN32( sh4r.r[Rn] );
   853     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.fpscr );
   854 :}
   855 LDS.L @Rm+, FPSCR {:
   856     CHECKRALIGN32( sh4r.r[Rm] );
   857     MEM_READ_LONG(sh4r.r[Rm], sh4r.fpscr);
   858     sh4r.r[Rm] +=4;
   859     sh4r.fr_bank = &sh4r.fr[(sh4r.fpscr&FPSCR_FR)>>21][0];
   860 :}
   861 LDS Rm, FPSCR {: 
   862     sh4r.fpscr = sh4r.r[Rm]; 
   863     sh4r.fr_bank = &sh4r.fr[(sh4r.fpscr&FPSCR_FR)>>21][0];
   864 :}
   865 STC DBR, Rn {: CHECKPRIV(); sh4r.r[Rn] = sh4r.dbr; :}
   866 STC.L DBR, @-Rn {:
   867     CHECKPRIV();
   868     sh4r.r[Rn] -= 4;
   869     CHECKWALIGN32( sh4r.r[Rn] );
   870     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.dbr );
   871 :}
   872 LDC.L @Rm+, DBR {:
   873     CHECKPRIV();
   874     CHECKRALIGN32( sh4r.r[Rm] );
   875     MEM_READ_LONG(sh4r.r[Rm], sh4r.dbr);
   876     sh4r.r[Rm] +=4;
   877 :}
   878 LDC Rm, DBR {:
   879     CHECKPRIV();
   880     sh4r.dbr = sh4r.r[Rm];
   881 :}
   882 STC.L Rm_BANK, @-Rn {:
   883     CHECKPRIV();
   884     sh4r.r[Rn] -= 4;
   885     CHECKWALIGN32( sh4r.r[Rn] );
   886     MEM_WRITE_LONG( sh4r.r[Rn], sh4r.r_bank[Rm_BANK] );
   887 :}
   888 LDC.L @Rm+, Rn_BANK {:
   889     CHECKPRIV();
   890     CHECKRALIGN32( sh4r.r[Rm] );
   891     MEM_READ_LONG( sh4r.r[Rm], sh4r.r_bank[Rn_BANK] );
   892     sh4r.r[Rm] += 4;
   893 :}
   894 LDC Rm, Rn_BANK {:
   895     CHECKPRIV();
   896     sh4r.r_bank[Rn_BANK] = sh4r.r[Rm];
   897 :}
   898 STC SR, Rn {: 
   899     CHECKPRIV();
   900     sh4r.r[Rn] = sh4_read_sr();
   901 :}
   902 STC GBR, Rn {:
   903     CHECKPRIV();
   904     sh4r.r[Rn] = sh4r.gbr;
   905 :}
   906 STC VBR, Rn {:
   907     CHECKPRIV();
   908     sh4r.r[Rn] = sh4r.vbr;
   909 :}
   910 STC SSR, Rn {:
   911     CHECKPRIV();
   912     sh4r.r[Rn] = sh4r.ssr;
   913 :}
   914 STC SPC, Rn {:
   915     CHECKPRIV();
   916     sh4r.r[Rn] = sh4r.spc;
   917 :}
   918 STC Rm_BANK, Rn {:
   919     CHECKPRIV();
   920     sh4r.r[Rn] = sh4r.r_bank[Rm_BANK];
   921 :}
   923 FADD FRm, FRn {:
   924     CHECKFPUEN();
   925     if( IS_FPU_DOUBLEPREC() ) {
   926 	DR(FRn) += DR(FRm);
   927     } else {
   928 	FR(FRn) += FR(FRm);
   929     }
   930 :}
   931 FSUB FRm, FRn {:
   932     CHECKFPUEN();
   933     if( IS_FPU_DOUBLEPREC() ) {
   934 	DR(FRn) -= DR(FRm);
   935     } else {
   936 	FR(FRn) -= FR(FRm);
   937     }
   938 :}
   940 FMUL FRm, FRn {:
   941     CHECKFPUEN();
   942     if( IS_FPU_DOUBLEPREC() ) {
   943 	DR(FRn) *= DR(FRm);
   944     } else {
   945 	FR(FRn) *= FR(FRm);
   946     }
   947 :}
   949 FDIV FRm, FRn {:
   950     CHECKFPUEN();
   951     if( IS_FPU_DOUBLEPREC() ) {
   952 	DR(FRn) /= DR(FRm);
   953     } else {
   954 	FR(FRn) /= FR(FRm);
   955     }
   956 :}
   958 FCMP/EQ FRm, FRn {:
   959     CHECKFPUEN();
   960     if( IS_FPU_DOUBLEPREC() ) {
   961 	sh4r.t = ( DR(FRn) == DR(FRm) ? 1 : 0 );
   962     } else {
   963 	sh4r.t = ( FR(FRn) == FR(FRm) ? 1 : 0 );
   964     }
   965 :}
   967 FCMP/GT FRm, FRn {:
   968     CHECKFPUEN();
   969     if( IS_FPU_DOUBLEPREC() ) {
   970 	sh4r.t = ( DR(FRn) > DR(FRm) ? 1 : 0 );
   971     } else {
   972 	sh4r.t = ( FR(FRn) > FR(FRm) ? 1 : 0 );
   973     }
   974 :}
   976 FMOV @(R0, Rm), FRn {: MEM_FP_READ( sh4r.r[Rm] + R0, FRn ); :}
   977 FMOV FRm, @(R0, Rn) {: MEM_FP_WRITE( sh4r.r[Rn] + R0, FRm ); :}
   978 FMOV @Rm, FRn {: MEM_FP_READ( sh4r.r[Rm], FRn ); :}
   979 FMOV @Rm+, FRn {: MEM_FP_READ( sh4r.r[Rm], FRn ); sh4r.r[Rm] += FP_WIDTH; :}
   980 FMOV FRm, @Rn {: MEM_FP_WRITE( sh4r.r[Rn], FRm ); :}
   981 FMOV FRm, @-Rn {: sh4r.r[Rn] -= FP_WIDTH; MEM_FP_WRITE( sh4r.r[Rn], FRm ); :}
   982 FMOV FRm, FRn {: 
   983     if( IS_FPU_DOUBLESIZE() )
   984 	DR(FRn) = DR(FRm);
   985     else
   986 	FR(FRn) = FR(FRm);
   987 :}
   988 FSTS FPUL, FRn {: CHECKFPUEN(); FR(FRn) = FPULf; :}
   989 FLDS FRm, FPUL {: CHECKFPUEN(); FPULf = FR(FRm); :}
   990 FLOAT FPUL, FRn {: 
   991     CHECKFPUEN();
   992     if( IS_FPU_DOUBLEPREC() ) {
   993 	if( FRn&1 ) { // No, really...
   994 	    dtmp = (double)FPULi;
   995 	    FR(FRn) = *(((float *)&dtmp)+1);
   996 	} else {
   997 	    DRF(FRn>>1) = (double)FPULi;
   998 	}
   999     } else {
  1000 	FR(FRn) = (float)FPULi;
  1002 :}
  1003 FTRC FRm, FPUL {:
  1004     CHECKFPUEN();
  1005     if( IS_FPU_DOUBLEPREC() ) {
  1006 	if( FRm&1 ) {
  1007 	    dtmp = 0;
  1008 	    *(((float *)&dtmp)+1) = FR(FRm);
  1009 	} else {
  1010 	    dtmp = DRF(FRm>>1);
  1012         if( dtmp >= MAX_INTF )
  1013             FPULi = MAX_INT;
  1014         else if( dtmp <= MIN_INTF )
  1015             FPULi = MIN_INT;
  1016         else 
  1017             FPULi = (int32_t)dtmp;
  1018     } else {
  1019 	ftmp = FR(FRm);
  1020 	if( ftmp >= MAX_INTF )
  1021 	    FPULi = MAX_INT;
  1022 	else if( ftmp <= MIN_INTF )
  1023 	    FPULi = MIN_INT;
  1024 	else
  1025 	    FPULi = (int32_t)ftmp;
  1027 :}
  1028 FNEG FRn {:
  1029     CHECKFPUEN();
  1030     if( IS_FPU_DOUBLEPREC() ) {
  1031 	DR(FRn) = -DR(FRn);
  1032     } else {
  1033         FR(FRn) = -FR(FRn);
  1035 :}
  1036 FABS FRn {:
  1037     CHECKFPUEN();
  1038     if( IS_FPU_DOUBLEPREC() ) {
  1039 	DR(FRn) = fabs(DR(FRn));
  1040     } else {
  1041         FR(FRn) = fabsf(FR(FRn));
  1043 :}
  1044 FSQRT FRn {:
  1045     CHECKFPUEN();
  1046     if( IS_FPU_DOUBLEPREC() ) {
  1047 	DR(FRn) = sqrt(DR(FRn));
  1048     } else {
  1049         FR(FRn) = sqrtf(FR(FRn));
  1051 :}
  1052 FLDI0 FRn {:
  1053     CHECKFPUEN();
  1054     if( IS_FPU_DOUBLEPREC() ) {
  1055 	DR(FRn) = 0.0;
  1056     } else {
  1057         FR(FRn) = 0.0;
  1059 :}
  1060 FLDI1 FRn {:
  1061     CHECKFPUEN();
  1062     if( IS_FPU_DOUBLEPREC() ) {
  1063 	DR(FRn) = 1.0;
  1064     } else {
  1065         FR(FRn) = 1.0;
  1067 :}
  1068 FMAC FR0, FRm, FRn {:
  1069     CHECKFPUEN();
  1070     if( IS_FPU_DOUBLEPREC() ) {
  1071         DR(FRn) += DR(FRm)*DR(0);
  1072     } else {
  1073 	FR(FRn) += FR(FRm)*FR(0);
  1075 :}
  1076 FRCHG {: 
  1077     CHECKFPUEN(); 
  1078     sh4r.fpscr ^= FPSCR_FR; 
  1079     sh4r.fr_bank = &sh4r.fr[(sh4r.fpscr&FPSCR_FR)>>21][0];
  1080 :}
  1081 FSCHG {: CHECKFPUEN(); sh4r.fpscr ^= FPSCR_SZ; :}
  1082 FCNVSD FPUL, FRn {:
  1083     CHECKFPUEN();
  1084     if( IS_FPU_DOUBLEPREC() && !IS_FPU_DOUBLESIZE() ) {
  1085 	DR(FRn) = (double)FPULf;
  1087 :}
  1088 FCNVDS FRm, FPUL {:
  1089     CHECKFPUEN();
  1090     if( IS_FPU_DOUBLEPREC() && !IS_FPU_DOUBLESIZE() ) {
  1091 	FPULf = (float)DR(FRm);
  1093 :}
  1095 FSRRA FRn {:
  1096     CHECKFPUEN();
  1097     if( !IS_FPU_DOUBLEPREC() ) {
  1098 	FR(FRn) = 1.0/sqrtf(FR(FRn));
  1100 :}
  1101 FIPR FVm, FVn {:
  1102     CHECKFPUEN();
  1103     if( !IS_FPU_DOUBLEPREC() ) {
  1104         int tmp2 = FVn<<2;
  1105         tmp = FVm<<2;
  1106         FR(tmp2+3) = FR(tmp)*FR(tmp2) +
  1107             FR(tmp+1)*FR(tmp2+1) +
  1108             FR(tmp+2)*FR(tmp2+2) +
  1109             FR(tmp+3)*FR(tmp2+3);
  1111 :}
  1112 FSCA FPUL, FRn {:
  1113     CHECKFPUEN();
  1114     if( !IS_FPU_DOUBLEPREC() ) {
  1115 	sh4_fsca( FPULi, &(DRF(FRn>>1)) );
  1116 	/*
  1117         float angle = (((float)(FPULi&0xFFFF))/65536.0) * 2 * M_PI;
  1118         FR(FRn) = sinf(angle);
  1119         FR((FRn)+1) = cosf(angle);
  1120 	*/
  1122 :}
  1123 FTRV XMTRX, FVn {:
  1124     CHECKFPUEN();
  1125     if( !IS_FPU_DOUBLEPREC() ) {
  1126 	sh4_ftrv(&(DRF(FVn<<1)), &sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21][0]);
  1127 	/*
  1128         tmp = FVn<<2;
  1129 	float *xf = &sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21][0];
  1130         float fv[4] = { FR(tmp), FR(tmp+1), FR(tmp+2), FR(tmp+3) };
  1131         FR(tmp) = xf[1] * fv[0] + xf[5]*fv[1] +
  1132 	    xf[9]*fv[2] + xf[13]*fv[3];
  1133         FR(tmp+1) = xf[0] * fv[0] + xf[4]*fv[1] +
  1134 	    xf[8]*fv[2] + xf[12]*fv[3];
  1135         FR(tmp+2) = xf[3] * fv[0] + xf[7]*fv[1] +
  1136 	    xf[11]*fv[2] + xf[15]*fv[3];
  1137         FR(tmp+3) = xf[2] * fv[0] + xf[6]*fv[1] +
  1138 	    xf[10]*fv[2] + xf[14]*fv[3];
  1139 	*/
  1141 :}
  1142 UNDEF {:
  1143     UNDEF(ir);
  1144 :}
  1145 %%
  1146     sh4r.pc = sh4r.new_pc;
  1147     sh4r.new_pc += 2;
  1148     sh4r.in_delay_slot = 0;
  1149     return TRUE;
.