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