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