Search
lxdream.org :: lxdream/src/sh4/sh4core.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4core.c
changeset 157:fbe03268ad8a
prev124:ceb38f08619a
next164:84f6b203cfe1
author nkeynes
date Thu Jun 15 10:27:10 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Add preliminary call-stack tracing ability
Fix INTC state save/load/reset
view annotate diff log raw
     1 /**
     2  * $Id: sh4core.c,v 1.27 2006-06-15 10:27:10 nkeynes Exp $
     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 <math.h>
    22 #include "dream.h"
    23 #include "sh4/sh4core.h"
    24 #include "sh4/sh4mmio.h"
    25 #include "sh4/intc.h"
    26 #include "mem.h"
    27 #include "clock.h"
    28 #include "syscall.h"
    30 #define SH4_CALLTRACE 1
    32 #define MAX_INT 0x7FFFFFFF
    33 #define MIN_INT 0x80000000
    34 #define MAX_INTF 2147483647.0
    35 #define MIN_INTF -2147483648.0
    37 /* CPU-generated exception code/vector pairs */
    38 #define EXC_POWER_RESET  0x000 /* vector special */
    39 #define EXC_MANUAL_RESET 0x020
    40 #define EXC_SLOT_ILLEGAL 0x1A0
    41 #define EXC_ILLEGAL      0x180
    42 #define EXV_ILLEGAL      0x100
    43 #define EXC_TRAP         0x160
    44 #define EXV_TRAP         0x100
    45 #define EXC_FPDISABLE    0x800
    46 #define EXV_FPDISABLE    0x100
    48 /********************** SH4 Module Definition ****************************/
    50 void sh4_init( void );
    51 void sh4_reset( void );
    52 uint32_t sh4_run_slice( uint32_t );
    53 void sh4_start( void );
    54 void sh4_stop( void );
    55 void sh4_save_state( FILE *f );
    56 int sh4_load_state( FILE *f );
    58 struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset, 
    59 				       NULL, sh4_run_slice, sh4_stop,
    60 				       sh4_save_state, sh4_load_state };
    62 struct sh4_registers sh4r;
    64 void sh4_init(void)
    65 {
    66     register_io_regions( mmio_list_sh4mmio );
    67     mmu_init();
    68     sh4_reset();
    69 }
    71 void sh4_reset(void)
    72 {
    73     /* zero everything out, for the sake of having a consistent state. */
    74     memset( &sh4r, 0, sizeof(sh4r) );
    76     /* Resume running if we were halted */
    77     sh4r.sh4_state = SH4_STATE_RUNNING;
    79     sh4r.pc    = 0xA0000000;
    80     sh4r.new_pc= 0xA0000002;
    81     sh4r.vbr   = 0x00000000;
    82     sh4r.fpscr = 0x00040001;
    83     sh4r.sr    = 0x700000F0;
    85     /* Mem reset will do this, but if we want to reset _just_ the SH4... */
    86     MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
    88     /* Peripheral modules */
    89     INTC_reset();
    90     TMU_reset();
    91     SCIF_reset();
    92 }
    94 static struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
    95 static int sh4_breakpoint_count = 0;
    97 void sh4_set_breakpoint( uint32_t pc, int type )
    98 {
    99     sh4_breakpoints[sh4_breakpoint_count].address = pc;
   100     sh4_breakpoints[sh4_breakpoint_count].type = type;
   101     sh4_breakpoint_count++;
   102 }
   104 gboolean sh4_clear_breakpoint( uint32_t pc, int type )
   105 {
   106     int i;
   108     for( i=0; i<sh4_breakpoint_count; i++ ) {
   109 	if( sh4_breakpoints[i].address == pc && 
   110 	    sh4_breakpoints[i].type == type ) {
   111 	    while( ++i < sh4_breakpoint_count ) {
   112 		sh4_breakpoints[i-1].address = sh4_breakpoints[i].address;
   113 		sh4_breakpoints[i-1].type = sh4_breakpoints[i].type;
   114 	    }
   115 	    sh4_breakpoint_count--;
   116 	    return TRUE;
   117 	}
   118     }
   119     return FALSE;
   120 }
   122 int sh4_get_breakpoint( uint32_t pc )
   123 {
   124     int i;
   125     for( i=0; i<sh4_breakpoint_count; i++ ) {
   126 	if( sh4_breakpoints[i].address == pc )
   127 	    return sh4_breakpoints[i].type;
   128     }
   129     return 0;
   130 }
   132 uint32_t sh4_run_slice( uint32_t nanosecs ) 
   133 {
   134     int target = sh4r.icount + nanosecs / sh4_cpu_period;
   135     int start = sh4r.icount;
   136     int i;
   138     if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
   139 	if( sh4r.int_pending != 0 )
   140 	    sh4r.sh4_state = SH4_STATE_RUNNING;;
   141     }
   143     for( sh4r.slice_cycle = 0; sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
   144 	if( !sh4_execute_instruction() )
   145 	    break;
   146 #ifdef ENABLE_DEBUG_MODE
   147 	for( i=0; i<sh4_breakpoint_count; i++ ) {
   148 	    if( sh4_breakpoints[i].address == sh4r.pc ) {
   149 		break;
   150 	    }
   151 	}
   152 	if( i != sh4_breakpoint_count ) {
   153 	    dreamcast_stop();
   154 	    if( sh4_breakpoints[i].type == BREAK_ONESHOT )
   155 		sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
   156 	    break;
   157 	}
   158 #endif	
   159     }
   161     /* If we aborted early, but the cpu is still technically running,
   162      * we're doing a hard abort - cut the timeslice back to what we
   163      * actually executed
   164      */
   165     if( sh4r.slice_cycle != nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
   166 	nanosecs = sh4r.slice_cycle;
   167     }
   168     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
   169 	TMU_run_slice( nanosecs );
   170 	SCIF_run_slice( nanosecs );
   171     }
   172     sh4r.icount += sh4r.slice_cycle / sh4_cpu_period;
   173     return nanosecs;
   174 }
   176 void sh4_stop(void)
   177 {
   179 }
   181 void sh4_save_state( FILE *f )
   182 {
   183     fwrite( &sh4r, sizeof(sh4r), 1, f );
   184     INTC_save_state( f );
   185     TMU_save_state( f );
   186     SCIF_save_state( f );
   187 }
   189 int sh4_load_state( FILE * f )
   190 {
   191     fread( &sh4r, sizeof(sh4r), 1, f );
   192     INTC_load_state( f );
   193     TMU_load_state( f );
   194     return SCIF_load_state( f );
   195 }
   197 /********************** SH4 emulation core  ****************************/
   199 void sh4_set_pc( int pc )
   200 {
   201     sh4r.pc = pc;
   202     sh4r.new_pc = pc+2;
   203 }
   205 #define UNDEF(ir) do{ ERROR( "Raising exception on undefined instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop();  return FALSE; }while(0)
   206 #define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop(); return FALSE; }while(0)
   208 #if(SH4_CALLTRACE == 1)
   209 #define MAX_CALLSTACK 32
   210 static struct call_stack {
   211     sh4addr_t call_addr;
   212     sh4addr_t target_addr;
   213     sh4addr_t stack_pointer;
   214 } call_stack[MAX_CALLSTACK];
   216 static int call_stack_depth = 0;
   217 int sh4_call_trace_on = 0;
   219 static inline trace_call( sh4addr_t source, sh4addr_t dest ) 
   220 {
   221     if( call_stack_depth < MAX_CALLSTACK ) {
   222 	call_stack[call_stack_depth].call_addr = source;
   223 	call_stack[call_stack_depth].target_addr = dest;
   224 	call_stack[call_stack_depth].stack_pointer = sh4r.r[15];
   225     }
   226     call_stack_depth++;
   227 }
   229 static inline trace_return( sh4addr_t source, sh4addr_t dest )
   230 {
   231     if( call_stack_depth > 0 ) {
   232 	call_stack_depth--;
   233     }
   234 }
   236 void fprint_stack_trace( FILE *f )
   237 {
   238     int i = call_stack_depth -1;
   239     if( i >= MAX_CALLSTACK )
   240 	i = MAX_CALLSTACK - 1;
   241     for( ; i >= 0; i-- ) {
   242 	fprintf( f, "%d. Call from %08X => %08X, SP=%08X\n", 
   243 		 (call_stack_depth - i), call_stack[i].call_addr,
   244 		 call_stack[i].target_addr, call_stack[i].stack_pointer );
   245     }
   246 }
   248 #define TRACE_CALL( source, dest ) trace_call(source, dest)
   249 #define TRACE_RETURN( source, dest ) trace_return(source, dest)
   250 #else
   251 #define TRACE_CALL( dest, rts ) 
   252 #define TRACE_RETURN( source, dest )
   253 #endif
   255 #define RAISE( x, v ) do{ \
   256     if( sh4r.vbr == 0 ) { \
   257         ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
   258         dreamcast_stop(); return FALSE;	\
   259     } else { \
   260         sh4r.spc = sh4r.pc + 2; \
   261         sh4r.ssr = sh4_read_sr(); \
   262         sh4r.sgr = sh4r.r[15]; \
   263         MMIO_WRITE(MMU,EXPEVT,x); \
   264         sh4r.pc = sh4r.vbr + v; \
   265         sh4r.new_pc = sh4r.pc + 2; \
   266         sh4_load_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
   267     } \
   268     return TRUE; } while(0)
   270 #define MEM_READ_BYTE( addr ) sh4_read_byte(addr)
   271 #define MEM_READ_WORD( addr ) sh4_read_word(addr)
   272 #define MEM_READ_LONG( addr ) sh4_read_long(addr)
   273 #define MEM_WRITE_BYTE( addr, val ) sh4_write_byte(addr, val)
   274 #define MEM_WRITE_WORD( addr, val ) sh4_write_word(addr, val)
   275 #define MEM_WRITE_LONG( addr, val ) sh4_write_long(addr, val)
   277 #define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
   279 #define MEM_FP_READ( addr, reg ) sh4_read_float( addr, reg );
   281 #define MEM_FP_WRITE( addr, reg ) sh4_write_float( addr, reg );
   283 #define CHECK( x, c, v ) if( !x ) RAISE( c, v )
   284 #define CHECKPRIV() CHECK( IS_SH4_PRIVMODE(), EXC_ILLEGAL, EXV_ILLEGAL )
   285 #define CHECKFPUEN() CHECK( IS_FPU_ENABLED(), EXC_FPDISABLE, EXV_FPDISABLE )
   286 #define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); dreamcast_stop(); return FALSE; }
   287 #define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) { RAISE(EXC_SLOT_ILLEGAL,EXV_ILLEGAL); }
   289 static void sh4_switch_banks( )
   290 {
   291     uint32_t tmp[8];
   293     memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
   294     memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
   295     memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
   296 }
   298 static void sh4_load_sr( uint32_t newval )
   299 {
   300     if( (newval ^ sh4r.sr) & SR_RB )
   301         sh4_switch_banks();
   302     sh4r.sr = newval;
   303     sh4r.t = (newval&SR_T) ? 1 : 0;
   304     sh4r.s = (newval&SR_S) ? 1 : 0;
   305     sh4r.m = (newval&SR_M) ? 1 : 0;
   306     sh4r.q = (newval&SR_Q) ? 1 : 0;
   307     intc_mask_changed();
   308 }
   310 static void sh4_write_float( uint32_t addr, int reg )
   311 {
   312     if( IS_FPU_DOUBLESIZE() ) {
   313 	if( reg & 1 ) {
   314 	    sh4_write_long( addr, *((uint32_t *)&XF((reg)&0x0E)) );
   315 	    sh4_write_long( addr+4, *((uint32_t *)&XF(reg)) );
   316 	} else {
   317 	    sh4_write_long( addr, *((uint32_t *)&FR(reg)) ); 
   318 	    sh4_write_long( addr+4, *((uint32_t *)&FR((reg)|0x01)) );
   319 	}
   320     } else {
   321 	sh4_write_long( addr, *((uint32_t *)&FR((reg))) );
   322     }
   323 }
   325 static void sh4_read_float( uint32_t addr, int reg )
   326 {
   327     if( IS_FPU_DOUBLESIZE() ) {
   328 	if( reg & 1 ) {
   329 	    *((uint32_t *)&XF((reg) & 0x0E)) = sh4_read_long(addr);
   330 	    *((uint32_t *)&XF(reg)) = sh4_read_long(addr+4);
   331 	} else {
   332 	    *((uint32_t *)&FR(reg)) = sh4_read_long(addr);
   333 	    *((uint32_t *)&FR((reg) | 0x01)) = sh4_read_long(addr+4);
   334 	}
   335     } else {
   336 	*((uint32_t *)&FR(reg)) = sh4_read_long(addr);
   337     }
   338 }
   340 static uint32_t sh4_read_sr( void )
   341 {
   342     /* synchronize sh4r.sr with the various bitflags */
   343     sh4r.sr &= SR_MQSTMASK;
   344     if( sh4r.t ) sh4r.sr |= SR_T;
   345     if( sh4r.s ) sh4r.sr |= SR_S;
   346     if( sh4r.m ) sh4r.sr |= SR_M;
   347     if( sh4r.q ) sh4r.sr |= SR_Q;
   348     return sh4r.sr;
   349 }
   350 /* function for external use */
   351 void sh4_raise_exception( int code, int vector )
   352 {
   353     RAISE(code, vector);
   354 }
   356 static void sh4_accept_interrupt( void )
   357 {
   358     uint32_t code = intc_accept_interrupt();
   359     sh4r.ssr = sh4_read_sr();
   360     sh4r.spc = sh4r.pc;
   361     sh4r.sgr = sh4r.r[15];
   362     sh4_load_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
   363     MMIO_WRITE( MMU, INTEVT, code );
   364     sh4r.pc = sh4r.vbr + 0x600;
   365     sh4r.new_pc = sh4r.pc + 2;
   366     //    WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
   367 }
   369 gboolean sh4_execute_instruction( void )
   370 {
   371     uint32_t pc;
   372     unsigned short ir;
   373     uint32_t tmp;
   374     uint64_t tmpl;
   375     float ftmp;
   376     double dtmp;
   378 #define R0 sh4r.r[0]
   379 #define FR0 FR(0)
   380 #define DR0 DR(0)
   381 #define RN(ir) sh4r.r[(ir&0x0F00)>>8]
   382 #define RN_BANK(ir) sh4r.r_bank[(ir&0x0070)>>4]
   383 #define RM(ir) sh4r.r[(ir&0x00F0)>>4]
   384 #define DISP4(ir) (ir&0x000F) /* 4-bit displacements are *NOT* sign-extended */
   385 #define DISP8(ir) (ir&0x00FF)
   386 #define PCDISP8(ir) SIGNEXT8(ir&0x00FF)
   387 #define IMM8(ir) SIGNEXT8(ir&0x00FF)
   388 #define UIMM8(ir) (ir&0x00FF) /* Unsigned immmediate */
   389 #define DISP12(ir) SIGNEXT12(ir&0x0FFF)
   390 #define FRNn(ir) ((ir&0x0F00)>>8)
   391 #define FRMn(ir) ((ir&0x00F0)>>4)
   392 #define DRNn(ir) ((ir&0x0E00)>>9)
   393 #define DRMn(ir) ((ir&0x00E0)>>5)
   394 #define FVN(ir) ((ir&0x0C00)>>8)
   395 #define FVM(ir) ((ir&0x0300)>>6)
   396 #define FRN(ir) FR(FRNn(ir))
   397 #define FRM(ir) FR(FRMn(ir))
   398 #define FRNi(ir) (*((uint32_t *)&FR(FRNn(ir))))
   399 #define FRMi(ir) (*((uint32_t *)&FR(FRMn(ir))))
   400 #define DRN(ir) DRb(DRNn(ir), ir&0x0100)
   401 #define DRM(ir) DRb(DRMn(ir),ir&0x0010)
   402 #define DRNi(ir) (*((uint64_t *)&DR(FRNn(ir))))
   403 #define DRMi(ir) (*((uint64_t *)&DR(FRMn(ir))))
   404 #define FPULf   *((float *)&sh4r.fpul)
   405 #define FPULi    (sh4r.fpul)
   407     if( SH4_INT_PENDING() ) 
   408         sh4_accept_interrupt();
   410     pc = sh4r.pc;
   411     if( pc > 0xFFFFFF00 ) {
   412 	/* SYSCALL Magic */
   413 	syscall_invoke( pc );
   414 	sh4r.in_delay_slot = 0;
   415 	pc = sh4r.pc = sh4r.pr;
   416 	sh4r.new_pc = sh4r.pc + 2;
   417     }
   418     ir = MEM_READ_WORD(pc);
   419     sh4r.icount++;
   421     switch( (ir&0xF000)>>12 ) {
   422         case 0: /* 0000nnnnmmmmxxxx */
   423             switch( ir&0x000F ) {
   424                 case 2:
   425                     switch( (ir&0x00F0)>>4 ) {
   426                         case 0: /* STC     SR, Rn */
   427                             CHECKPRIV();
   428                             RN(ir) = sh4_read_sr();
   429                             break;
   430                         case 1: /* STC     GBR, Rn */
   431                             RN(ir) = sh4r.gbr;
   432                             break;
   433                         case 2: /* STC     VBR, Rn */
   434                             CHECKPRIV();
   435                             RN(ir) = sh4r.vbr;
   436                             break;
   437                         case 3: /* STC     SSR, Rn */
   438                             CHECKPRIV();
   439                             RN(ir) = sh4r.ssr;
   440                             break;
   441                         case 4: /* STC     SPC, Rn */
   442                             CHECKPRIV();
   443                             RN(ir) = sh4r.spc;
   444                             break;
   445                         case 8: case 9: case 10: case 11: case 12: case 13:
   446                         case 14: case 15:/* STC     Rm_bank, Rn */
   447                             CHECKPRIV();
   448                             RN(ir) = RN_BANK(ir);
   449                             break;
   450                         default: UNDEF(ir);
   451                     }
   452                     break;
   453                 case 3:
   454                     switch( (ir&0x00F0)>>4 ) {
   455                         case 0: /* BSRF    Rn */
   456                             CHECKDEST( pc + 4 + RN(ir) );
   457                             CHECKSLOTILLEGAL();
   458                             sh4r.in_delay_slot = 1;
   459                             sh4r.pr = sh4r.pc + 4;
   460                             sh4r.pc = sh4r.new_pc;
   461                             sh4r.new_pc = pc + 4 + RN(ir);
   462 			    TRACE_CALL( pc, sh4r.new_pc );
   463                             return TRUE;
   464                         case 2: /* BRAF    Rn */
   465                             CHECKDEST( pc + 4 + RN(ir) );
   466                             CHECKSLOTILLEGAL();
   467                             sh4r.in_delay_slot = 1;
   468                             sh4r.pc = sh4r.new_pc;
   469                             sh4r.new_pc = pc + 4 + RN(ir);
   470                             return TRUE;
   471                         case 8: /* PREF    [Rn] */
   472                             tmp = RN(ir);
   473                             if( (tmp & 0xFC000000) == 0xE0000000 ) {
   474                                 /* Store queue operation */
   475                                 int queue = (tmp&0x20)>>2;
   476                                 int32_t *src = &sh4r.store_queue[queue];
   477                                 uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
   478                                 uint32_t target = tmp&0x03FFFFE0 | hi;
   479                                 mem_copy_to_sh4( target, src, 32 );
   480 				//if( (target &0xFF000000) != 0x04000000 ) 
   481 				//    WARN( "Executed SQ%c => %08X",
   482 				//	  (queue == 0 ? '0' : '1'), target );
   483                             }
   484                             break;
   485                         case 9: /* OCBI    [Rn] */
   486                         case 10:/* OCBP    [Rn] */
   487                         case 11:/* OCBWB   [Rn] */
   488                             /* anything? */
   489                             break;
   490                         case 12:/* MOVCA.L R0, [Rn] */
   491                             UNIMP(ir);
   492                         default: UNDEF(ir);
   493                     }
   494                     break;
   495                 case 4: /* MOV.B   Rm, [R0 + Rn] */
   496                     MEM_WRITE_BYTE( R0 + RN(ir), RM(ir) );
   497                     break;
   498                 case 5: /* MOV.W   Rm, [R0 + Rn] */
   499                     MEM_WRITE_WORD( R0 + RN(ir), RM(ir) );
   500                     break;
   501                 case 6: /* MOV.L   Rm, [R0 + Rn] */
   502                     MEM_WRITE_LONG( R0 + RN(ir), RM(ir) );
   503                     break;
   504                 case 7: /* MUL.L   Rm, Rn */
   505                     sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   506                         (RM(ir) * RN(ir));
   507                     break;
   508                 case 8: 
   509                     switch( (ir&0x0FF0)>>4 ) {
   510                         case 0: /* CLRT    */
   511                             sh4r.t = 0;
   512                             break;
   513                         case 1: /* SETT    */
   514                             sh4r.t = 1;
   515                             break;
   516                         case 2: /* CLRMAC  */
   517                             sh4r.mac = 0;
   518                             break;
   519                         case 3: /* LDTLB   */
   520                             break;
   521                         case 4: /* CLRS    */
   522                             sh4r.s = 0;
   523                             break;
   524                         case 5: /* SETS    */
   525                             sh4r.s = 1;
   526                             break;
   527                         default: UNDEF(ir);
   528                     }
   529                     break;
   530                 case 9: 
   531                     if( (ir&0x00F0) == 0x20 ) /* MOVT    Rn */
   532                         RN(ir) = sh4r.t;
   533                     else if( ir == 0x0019 ) /* DIV0U   */
   534                         sh4r.m = sh4r.q = sh4r.t = 0;
   535                     else if( ir == 0x0009 )
   536                         /* NOP     */;
   537                     else UNDEF(ir);
   538                     break;
   539                 case 10:
   540                     switch( (ir&0x00F0) >> 4 ) {
   541                         case 0: /* STS     MACH, Rn */
   542                             RN(ir) = sh4r.mac >> 32;
   543                             break;
   544                         case 1: /* STS     MACL, Rn */
   545                             RN(ir) = (uint32_t)sh4r.mac;
   546                             break;
   547                         case 2: /* STS     PR, Rn */
   548                             RN(ir) = sh4r.pr;
   549                             break;
   550                         case 3: /* STC     SGR, Rn */
   551                             CHECKPRIV();
   552                             RN(ir) = sh4r.sgr;
   553                             break;
   554                         case 5:/* STS      FPUL, Rn */
   555                             RN(ir) = sh4r.fpul;
   556                             break;
   557                         case 6: /* STS     FPSCR, Rn */
   558                             RN(ir) = sh4r.fpscr;
   559                             break;
   560                         case 15:/* STC     DBR, Rn */
   561                             CHECKPRIV();
   562                             RN(ir) = sh4r.dbr;
   563                             break;
   564                         default: UNDEF(ir);
   565                     }
   566                     break;
   567                 case 11:
   568                     switch( (ir&0x0FF0)>>4 ) {
   569                         case 0: /* RTS     */
   570                             CHECKDEST( sh4r.pr );
   571                             CHECKSLOTILLEGAL();
   572                             sh4r.in_delay_slot = 1;
   573                             sh4r.pc = sh4r.new_pc;
   574                             sh4r.new_pc = sh4r.pr;
   575                             TRACE_RETURN( pc, sh4r.new_pc );
   576                             return TRUE;
   577                         case 1: /* SLEEP   */
   578 			    if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
   579 				sh4r.sh4_state = SH4_STATE_STANDBY;
   580 			    } else {
   581 				sh4r.sh4_state = SH4_STATE_SLEEP;
   582 			    }
   583 			    return FALSE; /* Halt CPU */
   584                         case 2: /* RTE     */
   585                             CHECKPRIV();
   586                             CHECKDEST( sh4r.spc );
   587                             CHECKSLOTILLEGAL();
   588                             sh4r.in_delay_slot = 1;
   589                             sh4r.pc = sh4r.new_pc;
   590                             sh4r.new_pc = sh4r.spc;
   591                             sh4_load_sr( sh4r.ssr );
   592                             return TRUE;
   593                         default:UNDEF(ir);
   594                     }
   595                     break;
   596                 case 12:/* MOV.B   [R0+R%d], R%d */
   597                     RN(ir) = MEM_READ_BYTE( R0 + RM(ir) );
   598                     break;
   599                 case 13:/* MOV.W   [R0+R%d], R%d */
   600                     RN(ir) = MEM_READ_WORD( R0 + RM(ir) );
   601                     break;
   602                 case 14:/* MOV.L   [R0+R%d], R%d */
   603                     RN(ir) = MEM_READ_LONG( R0 + RM(ir) );
   604                     break;
   605                 case 15:/* MAC.L   [Rm++], [Rn++] */
   606                     tmpl = ( SIGNEXT32(MEM_READ_LONG(RM(ir))) *
   607                                   SIGNEXT32(MEM_READ_LONG(RN(ir))) );
   608                     if( sh4r.s ) {
   609                         /* 48-bit Saturation. Yuch */
   610                         tmpl += SIGNEXT48(sh4r.mac);
   611                         if( tmpl < 0xFFFF800000000000LL )
   612                             tmpl = 0xFFFF800000000000LL;
   613                         else if( tmpl > 0x00007FFFFFFFFFFFLL )
   614                             tmpl = 0x00007FFFFFFFFFFFLL;
   615                         sh4r.mac = (sh4r.mac&0xFFFF000000000000LL) |
   616                             (tmpl&0x0000FFFFFFFFFFFFLL);
   617                     } else sh4r.mac = tmpl;
   619                     RM(ir) += 4;
   620                     RN(ir) += 4;
   622                     break;
   623                 default: UNDEF(ir);
   624             }
   625             break;
   626         case 1: /* 0001nnnnmmmmdddd */
   627             /* MOV.L   Rm, [Rn + disp4*4] */
   628             MEM_WRITE_LONG( RN(ir) + (DISP4(ir)<<2), RM(ir) );
   629             break;
   630         case 2: /* 0010nnnnmmmmxxxx */
   631             switch( ir&0x000F ) {
   632                 case 0: /* MOV.B   Rm, [Rn] */
   633                     MEM_WRITE_BYTE( RN(ir), RM(ir) );
   634                     break;
   635                 case 1: /* MOV.W   Rm, [Rn] */
   636                     MEM_WRITE_WORD( RN(ir), RM(ir) );
   637                     break;
   638                 case 2: /* MOV.L   Rm, [Rn] */
   639                     MEM_WRITE_LONG( RN(ir), RM(ir) );
   640                     break;
   641                 case 3: UNDEF(ir);
   642                     break;
   643                 case 4: /* MOV.B   Rm, [--Rn] */
   644                     RN(ir) --;
   645                     MEM_WRITE_BYTE( RN(ir), RM(ir) );
   646                     break;
   647                 case 5: /* MOV.W   Rm, [--Rn] */
   648                     RN(ir) -= 2;
   649                     MEM_WRITE_WORD( RN(ir), RM(ir) );
   650                     break;
   651                 case 6: /* MOV.L   Rm, [--Rn] */
   652                     RN(ir) -= 4;
   653                     MEM_WRITE_LONG( RN(ir), RM(ir) );
   654                     break;
   655                 case 7: /* DIV0S   Rm, Rn */
   656                     sh4r.q = RN(ir)>>31;
   657                     sh4r.m = RM(ir)>>31;
   658                     sh4r.t = sh4r.q ^ sh4r.m;
   659                     break;
   660                 case 8: /* TST     Rm, Rn */
   661                     sh4r.t = (RN(ir)&RM(ir) ? 0 : 1);
   662                     break;
   663                 case 9: /* AND     Rm, Rn */
   664                     RN(ir) &= RM(ir);
   665                     break;
   666                 case 10:/* XOR     Rm, Rn */
   667                     RN(ir) ^= RM(ir);
   668                     break;
   669                 case 11:/* OR      Rm, Rn */
   670                     RN(ir) |= RM(ir);
   671                     break;
   672                 case 12:/* CMP/STR Rm, Rn */
   673                     /* set T = 1 if any byte in RM & RN is the same */
   674                     tmp = RM(ir) ^ RN(ir);
   675                     sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
   676                               (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
   677                     break;
   678                 case 13:/* XTRCT   Rm, Rn */
   679                     RN(ir) = (RN(ir)>>16) | (RM(ir)<<16);
   680                     break;
   681                 case 14:/* MULU.W  Rm, Rn */
   682                     sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   683                         (uint32_t)((RM(ir)&0xFFFF) * (RN(ir)&0xFFFF));
   684                     break;
   685                 case 15:/* MULS.W  Rm, Rn */
   686                     sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
   687                         (uint32_t)(SIGNEXT32(RM(ir)&0xFFFF) * SIGNEXT32(RN(ir)&0xFFFF));
   688                     break;
   689             }
   690             break;
   691         case 3: /* 0011nnnnmmmmxxxx */
   692             switch( ir&0x000F ) {
   693                 case 0: /* CMP/EQ  Rm, Rn */
   694                     sh4r.t = ( RM(ir) == RN(ir) ? 1 : 0 );
   695                     break;
   696                 case 2: /* CMP/HS  Rm, Rn */
   697                     sh4r.t = ( RN(ir) >= RM(ir) ? 1 : 0 );
   698                     break;
   699                 case 3: /* CMP/GE  Rm, Rn */
   700                     sh4r.t = ( ((int32_t)RN(ir)) >= ((int32_t)RM(ir)) ? 1 : 0 );
   701                     break;
   702                 case 4: { /* DIV1    Rm, Rn */
   703                     /* This is just from the sh4p manual with some
   704                      * simplifications (someone want to check it's correct? :)
   705                      * Why they couldn't just provide a real DIV instruction...
   706                      * Please oh please let the translator batch these things
   707                      * up into a single DIV... */
   708                     uint32_t tmp0, tmp1, tmp2, dir;
   710                     dir = sh4r.q ^ sh4r.m;
   711                     sh4r.q = (RN(ir) >> 31);
   712                     tmp2 = RM(ir);
   713                     RN(ir) = (RN(ir) << 1) | sh4r.t;
   714                     tmp0 = RN(ir);
   715                     if( dir ) {
   716                         RN(ir) += tmp2;
   717                         tmp1 = (RN(ir)<tmp0 ? 1 : 0 );
   718                     } else {
   719                         RN(ir) -= tmp2;
   720                         tmp1 = (RN(ir)>tmp0 ? 1 : 0 );
   721                     }
   722                     sh4r.q ^= sh4r.m ^ tmp1;
   723                     sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
   724                     break; }
   725                 case 5: /* DMULU.L Rm, Rn */
   726                     sh4r.mac = ((uint64_t)RM(ir)) * ((uint64_t)RN(ir));
   727                     break;
   728                 case 6: /* CMP/HI  Rm, Rn */
   729                     sh4r.t = ( RN(ir) > RM(ir) ? 1 : 0 );
   730                     break;
   731                 case 7: /* CMP/GT  Rm, Rn */
   732                     sh4r.t = ( ((int32_t)RN(ir)) > ((int32_t)RM(ir)) ? 1 : 0 );
   733                     break;
   734                 case 8: /* SUB     Rm, Rn */
   735                     RN(ir) -= RM(ir);
   736                     break;
   737                 case 10:/* SUBC    Rm, Rn */
   738                     tmp = RN(ir);
   739                     RN(ir) = RN(ir) - RM(ir) - sh4r.t;
   740                     sh4r.t = (RN(ir) > tmp || (RN(ir) == tmp && sh4r.t == 1));
   741                     break;
   742                 case 11:/* SUBV    Rm, Rn */
   743                     UNIMP(ir);
   744                     break;
   745                 case 12:/* ADD     Rm, Rn */
   746                     RN(ir) += RM(ir);
   747                     break;
   748                 case 13:/* DMULS.L Rm, Rn */
   749                     sh4r.mac = SIGNEXT32(RM(ir)) * SIGNEXT32(RN(ir));
   750                     break;
   751                 case 14:/* ADDC    Rm, Rn */
   752                     tmp = RN(ir);
   753                     RN(ir) += RM(ir) + sh4r.t;
   754                     sh4r.t = ( RN(ir) < tmp || (RN(ir) == tmp && sh4r.t != 0) ? 1 : 0 );
   755                     break;
   756                 case 15:/* ADDV    Rm, Rn */
   757                     UNIMP(ir);
   758                     break;
   759                 default: UNDEF(ir);
   760             }
   761             break;
   762         case 4: /* 0100nnnnxxxxxxxx */
   763             switch( ir&0x00FF ) {
   764                 case 0x00: /* SHLL    Rn */
   765                     sh4r.t = RN(ir) >> 31;
   766                     RN(ir) <<= 1;
   767                     break;
   768                 case 0x01: /* SHLR    Rn */
   769                     sh4r.t = RN(ir) & 0x00000001;
   770                     RN(ir) >>= 1;
   771                     break;
   772                 case 0x02: /* STS.L   MACH, [--Rn] */
   773                     RN(ir) -= 4;
   774                     MEM_WRITE_LONG( RN(ir), (sh4r.mac>>32) );
   775                     break;
   776                 case 0x03: /* STC.L   SR, [--Rn] */
   777                     CHECKPRIV();
   778                     RN(ir) -= 4;
   779                     MEM_WRITE_LONG( RN(ir), sh4_read_sr() );
   780                     break;
   781                 case 0x04: /* ROTL    Rn */
   782                     sh4r.t = RN(ir) >> 31;
   783                     RN(ir) <<= 1;
   784                     RN(ir) |= sh4r.t;
   785                     break;
   786                 case 0x05: /* ROTR    Rn */
   787                     sh4r.t = RN(ir) & 0x00000001;
   788                     RN(ir) >>= 1;
   789                     RN(ir) |= (sh4r.t << 31);
   790                     break;
   791                 case 0x06: /* LDS.L   [Rn++], MACH */
   792                     sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
   793                         (((uint64_t)MEM_READ_LONG(RN(ir)))<<32);
   794                     RN(ir) += 4;
   795                     break;
   796                 case 0x07: /* LDC.L   [Rn++], SR */
   797                     CHECKPRIV();
   798                     sh4_load_sr( MEM_READ_LONG(RN(ir)) );
   799                     RN(ir) +=4;
   800                     break;
   801                 case 0x08: /* SHLL2   Rn */
   802                     RN(ir) <<= 2;
   803                     break;
   804                 case 0x09: /* SHLR2   Rn */
   805                     RN(ir) >>= 2;
   806                     break;
   807                 case 0x0A: /* LDS     Rn, MACH */
   808                     sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
   809                         (((uint64_t)RN(ir))<<32);
   810                     break;
   811                 case 0x0B: /* JSR     [Rn] */
   812                     CHECKDEST( RN(ir) );
   813                     CHECKSLOTILLEGAL();
   814                     sh4r.in_delay_slot = 1;
   815                     sh4r.pc = sh4r.new_pc;
   816                     sh4r.new_pc = RN(ir);
   817                     sh4r.pr = pc + 4;
   818 		    TRACE_CALL( pc, sh4r.new_pc );
   819                     return TRUE;
   820                 case 0x0E: /* LDC     Rn, SR */
   821                     CHECKPRIV();
   822                     sh4_load_sr( RN(ir) );
   823                     break;
   824                 case 0x10: /* DT      Rn */
   825                     RN(ir) --;
   826                     sh4r.t = ( RN(ir) == 0 ? 1 : 0 );
   827                     break;
   828                 case 0x11: /* CMP/PZ  Rn */
   829                     sh4r.t = ( ((int32_t)RN(ir)) >= 0 ? 1 : 0 );
   830                     break;
   831                 case 0x12: /* STS.L   MACL, [--Rn] */
   832                     RN(ir) -= 4;
   833                     MEM_WRITE_LONG( RN(ir), (uint32_t)sh4r.mac );
   834                     break;
   835                 case 0x13: /* STC.L   GBR, [--Rn] */
   836                     RN(ir) -= 4;
   837                     MEM_WRITE_LONG( RN(ir), sh4r.gbr );
   838                     break;
   839                 case 0x15: /* CMP/PL  Rn */
   840                     sh4r.t = ( ((int32_t)RN(ir)) > 0 ? 1 : 0 );
   841                     break;
   842                 case 0x16: /* LDS.L   [Rn++], MACL */
   843                     sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
   844                         (uint64_t)((uint32_t)MEM_READ_LONG(RN(ir)));
   845                     RN(ir) += 4;
   846                     break;
   847                 case 0x17: /* LDC.L   [Rn++], GBR */
   848                     sh4r.gbr = MEM_READ_LONG(RN(ir));
   849                     RN(ir) +=4;
   850                     break;
   851                 case 0x18: /* SHLL8   Rn */
   852                     RN(ir) <<= 8;
   853                     break;
   854                 case 0x19: /* SHLR8   Rn */
   855                     RN(ir) >>= 8;
   856                     break;
   857                 case 0x1A: /* LDS     Rn, MACL */
   858                     sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
   859                         (uint64_t)((uint32_t)(RN(ir)));
   860                     break;
   861                 case 0x1B: /* TAS.B   [Rn] */
   862                     tmp = MEM_READ_BYTE( RN(ir) );
   863                     sh4r.t = ( tmp == 0 ? 1 : 0 );
   864                     MEM_WRITE_BYTE( RN(ir), tmp | 0x80 );
   865                     break;
   866                 case 0x1E: /* LDC     Rn, GBR */
   867                     sh4r.gbr = RN(ir);
   868                     break;
   869                 case 0x20: /* SHAL    Rn */
   870                     sh4r.t = RN(ir) >> 31;
   871                     RN(ir) <<= 1;
   872                     break;
   873                 case 0x21: /* SHAR    Rn */
   874                     sh4r.t = RN(ir) & 0x00000001;
   875                     RN(ir) = ((int32_t)RN(ir)) >> 1;
   876                     break;
   877                 case 0x22: /* STS.L   PR, [--Rn] */
   878                     RN(ir) -= 4;
   879                     MEM_WRITE_LONG( RN(ir), sh4r.pr );
   880                     break;
   881                 case 0x23: /* STC.L   VBR, [--Rn] */
   882                     CHECKPRIV();
   883                     RN(ir) -= 4;
   884                     MEM_WRITE_LONG( RN(ir), sh4r.vbr );
   885                     break;
   886                 case 0x24: /* ROTCL   Rn */
   887                     tmp = RN(ir) >> 31;
   888                     RN(ir) <<= 1;
   889                     RN(ir) |= sh4r.t;
   890                     sh4r.t = tmp;
   891                     break;
   892                 case 0x25: /* ROTCR   Rn */
   893                     tmp = RN(ir) & 0x00000001;
   894                     RN(ir) >>= 1;
   895                     RN(ir) |= (sh4r.t << 31 );
   896                     sh4r.t = tmp;
   897                     break;
   898                 case 0x26: /* LDS.L   [Rn++], PR */
   899                     sh4r.pr = MEM_READ_LONG( RN(ir) );
   900                     RN(ir) += 4;
   901                     break;
   902                 case 0x27: /* LDC.L   [Rn++], VBR */
   903                     CHECKPRIV();
   904                     sh4r.vbr = MEM_READ_LONG(RN(ir));
   905                     RN(ir) +=4;
   906                     break;
   907                 case 0x28: /* SHLL16  Rn */
   908                     RN(ir) <<= 16;
   909                     break;
   910                 case 0x29: /* SHLR16  Rn */
   911                     RN(ir) >>= 16;
   912                     break;
   913                 case 0x2A: /* LDS     Rn, PR */
   914                     sh4r.pr = RN(ir);
   915                     break;
   916                 case 0x2B: /* JMP     [Rn] */
   917                     CHECKDEST( RN(ir) );
   918                     CHECKSLOTILLEGAL();
   919                     sh4r.in_delay_slot = 1;
   920                     sh4r.pc = sh4r.new_pc;
   921                     sh4r.new_pc = RN(ir);
   922                     return TRUE;
   923                 case 0x2E: /* LDC     Rn, VBR */
   924                     CHECKPRIV();
   925                     sh4r.vbr = RN(ir);
   926                     break;
   927                 case 0x32: /* STC.L   SGR, [--Rn] */
   928                     CHECKPRIV();
   929                     RN(ir) -= 4;
   930                     MEM_WRITE_LONG( RN(ir), sh4r.sgr );
   931                     break;
   932                 case 0x33: /* STC.L   SSR, [--Rn] */
   933                     CHECKPRIV();
   934                     RN(ir) -= 4;
   935                     MEM_WRITE_LONG( RN(ir), sh4r.ssr );
   936                     break;
   937                 case 0x37: /* LDC.L   [Rn++], SSR */
   938                     CHECKPRIV();
   939                     sh4r.ssr = MEM_READ_LONG(RN(ir));
   940                     RN(ir) +=4;
   941                     break;
   942                 case 0x3E: /* LDC     Rn, SSR */
   943                     CHECKPRIV();
   944                     sh4r.ssr = RN(ir);
   945                     break;
   946                 case 0x43: /* STC.L   SPC, [--Rn] */
   947                     CHECKPRIV();
   948                     RN(ir) -= 4;
   949                     MEM_WRITE_LONG( RN(ir), sh4r.spc );
   950                     break;
   951                 case 0x47: /* LDC.L   [Rn++], SPC */
   952                     CHECKPRIV();
   953                     sh4r.spc = MEM_READ_LONG(RN(ir));
   954                     RN(ir) +=4;
   955                     break;
   956                 case 0x4E: /* LDC     Rn, SPC */
   957                     CHECKPRIV();
   958                     sh4r.spc = RN(ir);
   959                     break;
   960                 case 0x52: /* STS.L   FPUL, [--Rn] */
   961                     RN(ir) -= 4;
   962                     MEM_WRITE_LONG( RN(ir), sh4r.fpul );
   963                     break;
   964                 case 0x56: /* LDS.L   [Rn++], FPUL */
   965                     sh4r.fpul = MEM_READ_LONG(RN(ir));
   966                     RN(ir) +=4;
   967                     break;
   968                 case 0x5A: /* LDS     Rn, FPUL */
   969                     sh4r.fpul = RN(ir);
   970                     break;
   971                 case 0x62: /* STS.L   FPSCR, [--Rn] */
   972                     RN(ir) -= 4;
   973                     MEM_WRITE_LONG( RN(ir), sh4r.fpscr );
   974                     break;
   975                 case 0x66: /* LDS.L   [Rn++], FPSCR */
   976                     sh4r.fpscr = MEM_READ_LONG(RN(ir));
   977                     RN(ir) +=4;
   978                     break;
   979                 case 0x6A: /* LDS     Rn, FPSCR */
   980                     sh4r.fpscr = RN(ir);
   981                     break;
   982                 case 0xF2: /* STC.L   DBR, [--Rn] */
   983                     CHECKPRIV();
   984                     RN(ir) -= 4;
   985                     MEM_WRITE_LONG( RN(ir), sh4r.dbr );
   986                     break;
   987                 case 0xF6: /* LDC.L   [Rn++], DBR */
   988                     CHECKPRIV();
   989                     sh4r.dbr = MEM_READ_LONG(RN(ir));
   990                     RN(ir) +=4;
   991                     break;
   992                 case 0xFA: /* LDC     Rn, DBR */
   993                     CHECKPRIV();
   994                     sh4r.dbr = RN(ir);
   995                     break;
   996                 case 0x83: case 0x93: case 0xA3: case 0xB3: case 0xC3:
   997                 case 0xD3: case 0xE3: case 0xF3: /* STC.L   Rn_BANK, [--Rn] */
   998                     CHECKPRIV();
   999                     RN(ir) -= 4;
  1000                     MEM_WRITE_LONG( RN(ir), RN_BANK(ir) );
  1001                     break;
  1002                 case 0x87: case 0x97: case 0xA7: case 0xB7: case 0xC7:
  1003                 case 0xD7: case 0xE7: case 0xF7: /* LDC.L   [Rn++], Rn_BANK */
  1004                     CHECKPRIV();
  1005                     RN_BANK(ir) = MEM_READ_LONG( RN(ir) );
  1006                     RN(ir) += 4;
  1007                     break;
  1008                 case 0x8E: case 0x9E: case 0xAE: case 0xBE: case 0xCE:
  1009                 case 0xDE: case 0xEE: case 0xFE: /* LDC     Rm, Rn_BANK */
  1010                     CHECKPRIV();
  1011                     RN_BANK(ir) = RM(ir);
  1012                     break;
  1013                 default:
  1014                     if( (ir&0x000F) == 0x0F ) {
  1015                         /* MAC.W   [Rm++], [Rn++] */
  1016                         tmp = SIGNEXT16(MEM_READ_WORD(RM(ir))) *
  1017                             SIGNEXT16(MEM_READ_WORD(RN(ir)));
  1018                         if( sh4r.s ) {
  1019                             /* FIXME */
  1020                             UNIMP(ir);
  1021                         } else sh4r.mac += SIGNEXT32(tmp);
  1022                         RM(ir) += 2;
  1023                         RN(ir) += 2;
  1024                     } else if( (ir&0x000F) == 0x0C ) {
  1025                         /* SHAD    Rm, Rn */
  1026                         tmp = RM(ir);
  1027                         if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
  1028                         else if( (tmp & 0x1F) == 0 )  
  1029 			  RN(ir) = ((int32_t)RN(ir)) >> 31;
  1030                         else 
  1031 			  RN(ir) = ((int32_t)RN(ir)) >> (((~RM(ir)) & 0x1F)+1);
  1032                     } else if( (ir&0x000F) == 0x0D ) {
  1033                         /* SHLD    Rm, Rn */
  1034                         tmp = RM(ir);
  1035                         if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
  1036                         else if( (tmp & 0x1F) == 0 ) RN(ir) = 0;
  1037                         else RN(ir) >>= (((~tmp) & 0x1F)+1);
  1038                     } else UNDEF(ir);
  1040             break;
  1041         case 5: /* 0101nnnnmmmmdddd */
  1042             /* MOV.L   [Rm + disp4*4], Rn */
  1043             RN(ir) = MEM_READ_LONG( RM(ir) + (DISP4(ir)<<2) );
  1044             break;
  1045         case 6: /* 0110xxxxxxxxxxxx */
  1046             switch( ir&0x000f ) {
  1047                 case 0: /* MOV.B   [Rm], Rn */
  1048                     RN(ir) = MEM_READ_BYTE( RM(ir) );
  1049                     break;
  1050                 case 1: /* MOV.W   [Rm], Rn */
  1051                     RN(ir) = MEM_READ_WORD( RM(ir) );
  1052                     break;
  1053                 case 2: /* MOV.L   [Rm], Rn */
  1054                     RN(ir) = MEM_READ_LONG( RM(ir) );
  1055                     break;
  1056                 case 3: /* MOV     Rm, Rn */
  1057                     RN(ir) = RM(ir);
  1058                     break;
  1059                 case 4: /* MOV.B   [Rm++], Rn */
  1060                     RN(ir) = MEM_READ_BYTE( RM(ir) );
  1061                     RM(ir) ++;
  1062                     break;
  1063                 case 5: /* MOV.W   [Rm++], Rn */
  1064                     RN(ir) = MEM_READ_WORD( RM(ir) );
  1065                     RM(ir) += 2;
  1066                     break;
  1067                 case 6: /* MOV.L   [Rm++], Rn */
  1068                     RN(ir) = MEM_READ_LONG( RM(ir) );
  1069                     RM(ir) += 4;
  1070                     break;
  1071                 case 7: /* NOT     Rm, Rn */
  1072                     RN(ir) = ~RM(ir);
  1073                     break;
  1074                 case 8: /* SWAP.B  Rm, Rn */
  1075                     RN(ir) = (RM(ir)&0xFFFF0000) | ((RM(ir)&0x0000FF00)>>8) |
  1076                         ((RM(ir)&0x000000FF)<<8);
  1077                     break;
  1078                 case 9: /* SWAP.W  Rm, Rn */
  1079                     RN(ir) = (RM(ir)>>16) | (RM(ir)<<16);
  1080                     break;
  1081                 case 10:/* NEGC    Rm, Rn */
  1082                     tmp = 0 - RM(ir);
  1083                     RN(ir) = tmp - sh4r.t;
  1084                     sh4r.t = ( 0<tmp || tmp<RN(ir) ? 1 : 0 );
  1085                     break;
  1086                 case 11:/* NEG     Rm, Rn */
  1087                     RN(ir) = 0 - RM(ir);
  1088                     break;
  1089                 case 12:/* EXTU.B  Rm, Rn */
  1090                     RN(ir) = RM(ir)&0x000000FF;
  1091                     break;
  1092                 case 13:/* EXTU.W  Rm, Rn */
  1093                     RN(ir) = RM(ir)&0x0000FFFF;
  1094                     break;
  1095                 case 14:/* EXTS.B  Rm, Rn */
  1096                     RN(ir) = SIGNEXT8( RM(ir)&0x000000FF );
  1097                     break;
  1098                 case 15:/* EXTS.W  Rm, Rn */
  1099                     RN(ir) = SIGNEXT16( RM(ir)&0x0000FFFF );
  1100                     break;
  1102             break;
  1103         case 7: /* 0111nnnniiiiiiii */
  1104             /* ADD    imm8, Rn */
  1105             RN(ir) += IMM8(ir);
  1106             break;
  1107         case 8: /* 1000xxxxxxxxxxxx */
  1108             switch( (ir&0x0F00) >> 8 ) {
  1109                 case 0: /* MOV.B   R0, [Rm + disp4] */
  1110                     MEM_WRITE_BYTE( RM(ir) + DISP4(ir), R0 );
  1111                     break;
  1112                 case 1: /* MOV.W   R0, [Rm + disp4*2] */
  1113                     MEM_WRITE_WORD( RM(ir) + (DISP4(ir)<<1), R0 );
  1114                     break;
  1115                 case 4: /* MOV.B   [Rm + disp4], R0 */
  1116                     R0 = MEM_READ_BYTE( RM(ir) + DISP4(ir) );
  1117                     break;
  1118                 case 5: /* MOV.W   [Rm + disp4*2], R0 */
  1119                     R0 = MEM_READ_WORD( RM(ir) + (DISP4(ir)<<1) );
  1120                     break;
  1121                 case 8: /* CMP/EQ  imm, R0 */
  1122                     sh4r.t = ( R0 == IMM8(ir) ? 1 : 0 );
  1123                     break;
  1124                 case 9: /* BT      disp8 */
  1125                     CHECKSLOTILLEGAL()
  1126                     if( sh4r.t ) {
  1127                         CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
  1128                         sh4r.pc += (PCDISP8(ir)<<1) + 4;
  1129                         sh4r.new_pc = sh4r.pc + 2;
  1130                         return TRUE;
  1132                     break;
  1133                 case 11:/* BF      disp8 */
  1134                     CHECKSLOTILLEGAL()
  1135                     if( !sh4r.t ) {
  1136                         CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
  1137                         sh4r.pc += (PCDISP8(ir)<<1) + 4;
  1138                         sh4r.new_pc = sh4r.pc + 2;
  1139                         return TRUE;
  1141                     break;
  1142                 case 13:/* BT/S    disp8 */
  1143                     CHECKSLOTILLEGAL()
  1144                     if( sh4r.t ) {
  1145                         CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
  1146                         sh4r.in_delay_slot = 1;
  1147                         sh4r.pc = sh4r.new_pc;
  1148                         sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
  1149                         sh4r.in_delay_slot = 1;
  1150                         return TRUE;
  1152                     break;
  1153                 case 15:/* BF/S    disp8 */
  1154                     CHECKSLOTILLEGAL()
  1155                     if( !sh4r.t ) {
  1156                         CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
  1157                         sh4r.in_delay_slot = 1;
  1158                         sh4r.pc = sh4r.new_pc;
  1159                         sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
  1160                         return TRUE;
  1162                     break;
  1163                 default: UNDEF(ir);
  1165             break;
  1166         case 9: /* 1001xxxxxxxxxxxx */
  1167             /* MOV.W   [disp8*2 + pc + 4], Rn */
  1168             RN(ir) = MEM_READ_WORD( pc + 4 + (DISP8(ir)<<1) );
  1169             break;
  1170         case 10:/* 1010dddddddddddd */
  1171             /* BRA     disp12 */
  1172             CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
  1173             CHECKSLOTILLEGAL()
  1174             sh4r.in_delay_slot = 1;
  1175             sh4r.pc = sh4r.new_pc;
  1176             sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
  1177             return TRUE;
  1178         case 11:/* 1011dddddddddddd */
  1179             /* BSR     disp12 */
  1180             CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
  1181             CHECKSLOTILLEGAL()
  1182             sh4r.in_delay_slot = 1;
  1183             sh4r.pr = pc + 4;
  1184             sh4r.pc = sh4r.new_pc;
  1185             sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
  1186 	    TRACE_CALL( pc, sh4r.new_pc );
  1187             return TRUE;
  1188         case 12:/* 1100xxxxdddddddd */
  1189         switch( (ir&0x0F00)>>8 ) {
  1190                 case 0: /* MOV.B  R0, [GBR + disp8] */
  1191                     MEM_WRITE_BYTE( sh4r.gbr + DISP8(ir), R0 );
  1192                     break;
  1193                 case 1: /* MOV.W  R0, [GBR + disp8*2] */
  1194                     MEM_WRITE_WORD( sh4r.gbr + (DISP8(ir)<<1), R0 );
  1195                     break;
  1196                 case  2: /*MOV.L   R0, [GBR + disp8*4] */
  1197                     MEM_WRITE_LONG( sh4r.gbr + (DISP8(ir)<<2), R0 );
  1198                     break;
  1199                 case 3: /* TRAPA   imm8 */
  1200                     CHECKSLOTILLEGAL()
  1201                     sh4r.in_delay_slot = 1;
  1202                     MMIO_WRITE( MMU, TRA, UIMM8(ir)<<2 );
  1203                     RAISE( EXC_TRAP, EXV_TRAP );
  1204                     break;
  1205                 case 4: /* MOV.B   [GBR + disp8], R0 */
  1206                     R0 = MEM_READ_BYTE( sh4r.gbr + DISP8(ir) );
  1207                     break;
  1208                 case 5: /* MOV.W   [GBR + disp8*2], R0 */
  1209                     R0 = MEM_READ_WORD( sh4r.gbr + (DISP8(ir)<<1) );
  1210                     break;
  1211                 case 6: /* MOV.L   [GBR + disp8*4], R0 */
  1212                     R0 = MEM_READ_LONG( sh4r.gbr + (DISP8(ir)<<2) );
  1213                     break;
  1214                 case 7: /* MOVA    disp8 + pc&~3 + 4, R0 */
  1215                     R0 = (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4;
  1216                     break;
  1217                 case 8: /* TST     imm8, R0 */
  1218                     sh4r.t = (R0 & UIMM8(ir) ? 0 : 1);
  1219                     break;
  1220                 case 9: /* AND     imm8, R0 */
  1221                     R0 &= UIMM8(ir);
  1222                     break;
  1223                 case 10:/* XOR     imm8, R0 */
  1224                     R0 ^= UIMM8(ir);
  1225                     break;
  1226                 case 11:/* OR      imm8, R0 */
  1227                     R0 |= UIMM8(ir);
  1228                     break;
  1229                 case 12:/* TST.B   imm8, [R0+GBR] */
  1230                     sh4r.t = ( MEM_READ_BYTE(R0 + sh4r.gbr) & UIMM8(ir) ? 0 : 1 );
  1231                     break;
  1232                 case 13:/* AND.B   imm8, [R0+GBR] */
  1233                     MEM_WRITE_BYTE( R0 + sh4r.gbr,
  1234                                     UIMM8(ir) & MEM_READ_BYTE(R0 + sh4r.gbr) );
  1235                     break;
  1236                 case 14:/* XOR.B   imm8, [R0+GBR] */
  1237                     MEM_WRITE_BYTE( R0 + sh4r.gbr,
  1238                                     UIMM8(ir) ^ MEM_READ_BYTE(R0 + sh4r.gbr) );
  1239                     break;
  1240                 case 15:/* OR.B    imm8, [R0+GBR] */
  1241                     MEM_WRITE_BYTE( R0 + sh4r.gbr,
  1242                                     UIMM8(ir) | MEM_READ_BYTE(R0 + sh4r.gbr) );
  1243                     break;
  1245             break;
  1246         case 13:/* 1101nnnndddddddd */
  1247             /* MOV.L   [disp8*4 + pc&~3 + 4], Rn */
  1248             RN(ir) = MEM_READ_LONG( (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4 );
  1249             break;
  1250         case 14:/* 1110nnnniiiiiiii */
  1251             /* MOV     imm8, Rn */
  1252             RN(ir) = IMM8(ir);
  1253             break;
  1254         case 15:/* 1111xxxxxxxxxxxx */
  1255             CHECKFPUEN();
  1256 	    if( IS_FPU_DOUBLEPREC() ) {
  1257 		switch( ir&0x000F ) {
  1258                 case 0: /* FADD    FRm, FRn */
  1259                     DRN(ir) += DRM(ir);
  1260                     break;
  1261                 case 1: /* FSUB    FRm, FRn */
  1262                     DRN(ir) -= DRM(ir);
  1263                     break;
  1264                 case 2: /* FMUL    FRm, FRn */
  1265                     DRN(ir) = DRN(ir) * DRM(ir);
  1266                     break;
  1267                 case 3: /* FDIV    FRm, FRn */
  1268                     DRN(ir) = DRN(ir) / DRM(ir);
  1269                     break;
  1270                 case 4: /* FCMP/EQ FRm, FRn */
  1271                     sh4r.t = ( DRN(ir) == DRM(ir) ? 1 : 0 );
  1272                     break;
  1273                 case 5: /* FCMP/GT FRm, FRn */
  1274                     sh4r.t = ( DRN(ir) > DRM(ir) ? 1 : 0 );
  1275                     break;
  1276                 case 6: /* FMOV.S  [Rm+R0], FRn */
  1277                     MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
  1278                     break;
  1279                 case 7: /* FMOV.S  FRm, [Rn+R0] */
  1280                     MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
  1281                     break;
  1282                 case 8: /* FMOV.S  [Rm], FRn */
  1283                     MEM_FP_READ( RM(ir), FRNn(ir) );
  1284                     break;
  1285                 case 9: /* FMOV.S  [Rm++], FRn */
  1286                     MEM_FP_READ( RM(ir), FRNn(ir) );
  1287                     RM(ir) += FP_WIDTH;
  1288                     break;
  1289                 case 10:/* FMOV.S  FRm, [Rn] */
  1290                     MEM_FP_WRITE( RN(ir), FRMn(ir) );
  1291                     break;
  1292                 case 11:/* FMOV.S  FRm, [--Rn] */
  1293                     RN(ir) -= FP_WIDTH;
  1294                     MEM_FP_WRITE( RN(ir), FRMn(ir) );
  1295                     break;
  1296                 case 12:/* FMOV    FRm, FRn */
  1297 		    if( IS_FPU_DOUBLESIZE() )
  1298 			DRN(ir) = DRM(ir);
  1299 		    else
  1300 			FRN(ir) = FRM(ir);
  1301                     break;
  1302                 case 13:
  1303                     switch( (ir&0x00F0) >> 4 ) {
  1304 		    case 0: /* FSTS    FPUL, FRn */
  1305 			FRN(ir) = FPULf;
  1306 			break;
  1307 		    case 1: /* FLDS    FRn,FPUL */
  1308 			FPULf = FRN(ir);
  1309 			break;
  1310 		    case 2: /* FLOAT   FPUL, FRn */
  1311 			DRN(ir) = (float)FPULi;
  1312 			break;
  1313 		    case 3: /* FTRC    FRn, FPUL */
  1314 			dtmp = DRN(ir);
  1315 			if( dtmp >= MAX_INTF )
  1316 			    FPULi = MAX_INT;
  1317 			else if( dtmp <= MIN_INTF )
  1318 			    FPULi = MIN_INT;
  1319 			else 
  1320 			    FPULi = (int32_t)dtmp;
  1321 			break;
  1322 		    case 4: /* FNEG    FRn */
  1323 			DRN(ir) = -DRN(ir);
  1324 			break;
  1325 		    case 5: /* FABS    FRn */
  1326 			DRN(ir) = fabs(DRN(ir));
  1327 			break;
  1328 		    case 6: /* FSQRT   FRn */
  1329 			DRN(ir) = sqrt(DRN(ir));
  1330 			break;
  1331 		    case 7: /* FSRRA FRn */
  1332 			DRN(ir) = 1.0/sqrt(DRN(ir));
  1333 			break;
  1334 		    case 8: /* FLDI0   FRn */
  1335 			DRN(ir) = 0.0;
  1336 			break;
  1337 		    case 9: /* FLDI1   FRn */
  1338 			DRN(ir) = 1.0;
  1339 			break;
  1340 		    case 10: /* FCNVSD FPUL, DRn */
  1341 			DRN(ir) = (double)FPULf;
  1342 			break;
  1343 		    case 11: /* FCNVDS DRn, FPUL */
  1344 			FPULf = (float)DRN(ir);
  1345 			break;
  1346 		    case 14:/* FIPR    FVm, FVn */
  1347 			UNDEF(ir);
  1348 			break;
  1349 		    case 15:
  1350 			if( (ir&0x0300) == 0x0100 ) { /* FTRV    XMTRX,FVn */
  1351 			    break;
  1353 			else if( (ir&0x0100) == 0 ) { /* FSCA    FPUL, DRn */
  1354 			    float angle = (((float)(short)(FPULi>>16)) +
  1355 					   ((float)(FPULi&16)/65536.0)) *
  1356 				2 * M_PI;
  1357 			    int reg = DRNn(ir);
  1358 			    DR(reg) = sinf(angle);
  1359 			    DR(reg+1) = cosf(angle);
  1360 			    break;
  1362 			else if( ir == 0xFBFD ) {
  1363 			    /* FRCHG   */
  1364 			    sh4r.fpscr ^= FPSCR_FR;
  1365 			    break;
  1367 			else if( ir == 0xF3FD ) {
  1368 			    /* FSCHG   */
  1369 			    sh4r.fpscr ^= FPSCR_SZ;
  1370 			    break;
  1372 		    default: UNDEF(ir);
  1374                     break;
  1375                 case 14:/* FMAC    FR0, FRm, FRn */
  1376                     DRN(ir) += DRM(ir)*DR0;
  1377                     break;
  1378                 default: UNDEF(ir);
  1380 	    } else { /* Single precision */
  1381 		switch( ir&0x000F ) {
  1382                 case 0: /* FADD    FRm, FRn */
  1383                     FRN(ir) += FRM(ir);
  1384                     break;
  1385                 case 1: /* FSUB    FRm, FRn */
  1386                     FRN(ir) -= FRM(ir);
  1387                     break;
  1388                 case 2: /* FMUL    FRm, FRn */
  1389                     FRN(ir) = FRN(ir) * FRM(ir);
  1390                     break;
  1391                 case 3: /* FDIV    FRm, FRn */
  1392                     FRN(ir) = FRN(ir) / FRM(ir);
  1393                     break;
  1394                 case 4: /* FCMP/EQ FRm, FRn */
  1395                     sh4r.t = ( FRN(ir) == FRM(ir) ? 1 : 0 );
  1396                     break;
  1397                 case 5: /* FCMP/GT FRm, FRn */
  1398                     sh4r.t = ( FRN(ir) > FRM(ir) ? 1 : 0 );
  1399                     break;
  1400                 case 6: /* FMOV.S  [Rm+R0], FRn */
  1401                     MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
  1402                     break;
  1403                 case 7: /* FMOV.S  FRm, [Rn+R0] */
  1404                     MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
  1405                     break;
  1406                 case 8: /* FMOV.S  [Rm], FRn */
  1407                     MEM_FP_READ( RM(ir), FRNn(ir) );
  1408                     break;
  1409                 case 9: /* FMOV.S  [Rm++], FRn */
  1410                     MEM_FP_READ( RM(ir), FRNn(ir) );
  1411                     RM(ir) += FP_WIDTH;
  1412                     break;
  1413                 case 10:/* FMOV.S  FRm, [Rn] */
  1414                     MEM_FP_WRITE( RN(ir), FRMn(ir) );
  1415                     break;
  1416                 case 11:/* FMOV.S  FRm, [--Rn] */
  1417                     RN(ir) -= FP_WIDTH;
  1418                     MEM_FP_WRITE( RN(ir), FRMn(ir) );
  1419                     break;
  1420                 case 12:/* FMOV    FRm, FRn */
  1421 		    if( IS_FPU_DOUBLESIZE() )
  1422 			DRN(ir) = DRM(ir);
  1423 		    else
  1424 			FRN(ir) = FRM(ir);
  1425                     break;
  1426                 case 13:
  1427                     switch( (ir&0x00F0) >> 4 ) {
  1428 		    case 0: /* FSTS    FPUL, FRn */
  1429 			FRN(ir) = FPULf;
  1430 			break;
  1431 		    case 1: /* FLDS    FRn,FPUL */
  1432 			FPULf = FRN(ir);
  1433 			break;
  1434 		    case 2: /* FLOAT   FPUL, FRn */
  1435 			FRN(ir) = (float)FPULi;
  1436 			break;
  1437 		    case 3: /* FTRC    FRn, FPUL */
  1438 			ftmp = FRN(ir);
  1439 			if( ftmp >= MAX_INTF )
  1440 			    FPULi = MAX_INT;
  1441 			else if( ftmp <= MIN_INTF )
  1442 			    FPULi = MIN_INT;
  1443 			else
  1444 			    FPULi = (int32_t)ftmp;
  1445 			break;
  1446 		    case 4: /* FNEG    FRn */
  1447 			FRN(ir) = -FRN(ir);
  1448 			break;
  1449 		    case 5: /* FABS    FRn */
  1450 			FRN(ir) = fabsf(FRN(ir));
  1451 			break;
  1452 		    case 6: /* FSQRT   FRn */
  1453 			FRN(ir) = sqrtf(FRN(ir));
  1454 			break;
  1455 		    case 7: /* FSRRA FRn */
  1456 			FRN(ir) = 1.0/sqrtf(FRN(ir));
  1457 			break;
  1458 		    case 8: /* FLDI0   FRn */
  1459 			FRN(ir) = 0.0;
  1460 			break;
  1461 		    case 9: /* FLDI1   FRn */
  1462 			FRN(ir) = 1.0;
  1463 			break;
  1464 		    case 10: /* FCNVSD FPUL, DRn */
  1465 			UNDEF(ir);
  1466 			break;
  1467 		    case 11: /* FCNVDS DRn, FPUL */
  1468 			UNDEF(ir);
  1469 			break;
  1470 		    case 14:/* FIPR    FVm, FVn */
  1471                             /* FIXME: This is not going to be entirely accurate
  1472                              * as the SH4 instruction is less precise. Also
  1473                              * need to check for 0s and infinities.
  1474                              */
  1476                             int tmp2 = FVN(ir);
  1477                             tmp = FVM(ir);
  1478                             FR(tmp2+3) = FR(tmp)*FR(tmp2) +
  1479                                 FR(tmp+1)*FR(tmp2+1) +
  1480                                 FR(tmp+2)*FR(tmp2+2) +
  1481                                 FR(tmp+3)*FR(tmp2+3);
  1482                             break;
  1484 		    case 15:
  1485 			if( (ir&0x0300) == 0x0100 ) { /* FTRV    XMTRX,FVn */
  1486 			    tmp = FVN(ir);
  1487 			    float fv[4] = { FR(tmp), FR(tmp+1), FR(tmp+2), FR(tmp+3) };
  1488 			    FR(tmp) = XF(0) * fv[0] + XF(4)*fv[1] +
  1489 				XF(8)*fv[2] + XF(12)*fv[3];
  1490 			    FR(tmp+1) = XF(1) * fv[0] + XF(5)*fv[1] +
  1491 				XF(9)*fv[2] + XF(13)*fv[3];
  1492 			    FR(tmp+2) = XF(2) * fv[0] + XF(6)*fv[1] +
  1493 				XF(10)*fv[2] + XF(14)*fv[3];
  1494 			    FR(tmp+3) = XF(3) * fv[0] + XF(7)*fv[1] +
  1495 				XF(11)*fv[2] + XF(15)*fv[3];
  1496 			    break;
  1498 			else if( (ir&0x0100) == 0 ) { /* FSCA    FPUL, DRn */
  1499 			    float angle = (((float)(short)(FPULi>>16)) +
  1500 					   (((float)(FPULi&0xFFFF))/65536.0)) *
  1501 				2 * M_PI;
  1502 			    int reg = FRNn(ir);
  1503 			    FR(reg) = sinf(angle);
  1504 			    FR(reg+1) = cosf(angle);
  1505 			    break;
  1507 			else if( ir == 0xFBFD ) {
  1508 			    /* FRCHG   */
  1509 			    sh4r.fpscr ^= FPSCR_FR;
  1510 			    break;
  1512 			else if( ir == 0xF3FD ) {
  1513 			    /* FSCHG   */
  1514 			    sh4r.fpscr ^= FPSCR_SZ;
  1515 			    break;
  1517 		    default: UNDEF(ir);
  1519                     break;
  1520                 case 14:/* FMAC    FR0, FRm, FRn */
  1521                     FRN(ir) += FRM(ir)*FR0;
  1522                     break;
  1523                 default: UNDEF(ir);
  1526 	    break;
  1528     sh4r.pc = sh4r.new_pc;
  1529     sh4r.new_pc += 2;
  1530     sh4r.in_delay_slot = 0;
.