Search
lxdream.org :: lxdream/src/sh4/sh4core.in
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4core.in
changeset 970:44d62d0850c8
prev953:f4a156508ad1
next974:16b079ed11bb
author nkeynes
date Thu Jan 22 00:50:09 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Fix Post-increment mov instructions - per the manual, increment doesn't happen if the
source and target registers are the same.
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@586
     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@586
    21
#include <assert.h>
nkeynes@359
    22
#include <math.h>
nkeynes@359
    23
#include "dream.h"
nkeynes@430
    24
#include "dreamcast.h"
nkeynes@430
    25
#include "eventq.h"
nkeynes@430
    26
#include "mem.h"
nkeynes@430
    27
#include "clock.h"
nkeynes@430
    28
#include "syscall.h"
nkeynes@359
    29
#include "sh4/sh4core.h"
nkeynes@359
    30
#include "sh4/sh4mmio.h"
nkeynes@671
    31
#include "sh4/sh4stat.h"
nkeynes@953
    32
#include "sh4/mmu.h"
nkeynes@359
    33
nkeynes@359
    34
#define SH4_CALLTRACE 1
nkeynes@359
    35
nkeynes@359
    36
#define MAX_INT 0x7FFFFFFF
nkeynes@359
    37
#define MIN_INT 0x80000000
nkeynes@359
    38
#define MAX_INTF 2147483647.0
nkeynes@359
    39
#define MIN_INTF -2147483648.0
nkeynes@359
    40
nkeynes@359
    41
/********************** SH4 Module Definition ****************************/
nkeynes@359
    42
nkeynes@740
    43
uint32_t sh4_emulate_run_slice( uint32_t nanosecs ) 
nkeynes@359
    44
{
nkeynes@359
    45
    int i;
nkeynes@359
    46
nkeynes@359
    47
    if( sh4_breakpoint_count == 0 ) {
nkeynes@359
    48
	for( ; sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
nkeynes@359
    49
	    if( SH4_EVENT_PENDING() ) {
nkeynes@359
    50
		if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@359
    51
		    event_execute();
nkeynes@359
    52
		}
nkeynes@359
    53
		/* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@359
    54
		if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@359
    55
		    sh4_accept_interrupt();
nkeynes@359
    56
		}
nkeynes@359
    57
	    }
nkeynes@359
    58
	    if( !sh4_execute_instruction() ) {
nkeynes@359
    59
		break;
nkeynes@359
    60
	    }
nkeynes@359
    61
	}
nkeynes@359
    62
    } else {
nkeynes@359
    63
	for( ;sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
nkeynes@359
    64
	    if( SH4_EVENT_PENDING() ) {
nkeynes@359
    65
		if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@359
    66
		    event_execute();
nkeynes@359
    67
		}
nkeynes@359
    68
		/* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@359
    69
		if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@359
    70
		    sh4_accept_interrupt();
nkeynes@359
    71
		}
nkeynes@359
    72
	    }
nkeynes@359
    73
                 
nkeynes@359
    74
	    if( !sh4_execute_instruction() )
nkeynes@359
    75
		break;
nkeynes@359
    76
#ifdef ENABLE_DEBUG_MODE
nkeynes@359
    77
	    for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@359
    78
		if( sh4_breakpoints[i].address == sh4r.pc ) {
nkeynes@359
    79
		    break;
nkeynes@359
    80
		}
nkeynes@359
    81
	    }
nkeynes@359
    82
	    if( i != sh4_breakpoint_count ) {
nkeynes@740
    83
	    	sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@359
    84
	    }
nkeynes@359
    85
#endif	
nkeynes@359
    86
	}
nkeynes@359
    87
    }
nkeynes@359
    88
nkeynes@359
    89
    /* If we aborted early, but the cpu is still technically running,
nkeynes@359
    90
     * we're doing a hard abort - cut the timeslice back to what we
nkeynes@359
    91
     * actually executed
nkeynes@359
    92
     */
nkeynes@359
    93
    if( sh4r.slice_cycle != nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
nkeynes@359
    94
	nanosecs = sh4r.slice_cycle;
nkeynes@359
    95
    }
nkeynes@359
    96
    if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
nkeynes@359
    97
	TMU_run_slice( nanosecs );
nkeynes@359
    98
	SCIF_run_slice( nanosecs );
nkeynes@359
    99
    }
nkeynes@359
   100
    return nanosecs;
nkeynes@359
   101
}
nkeynes@359
   102
nkeynes@359
   103
/********************** SH4 emulation core  ****************************/
nkeynes@359
   104
nkeynes@359
   105
#if(SH4_CALLTRACE == 1)
nkeynes@359
   106
#define MAX_CALLSTACK 32
nkeynes@359
   107
static struct call_stack {
nkeynes@359
   108
    sh4addr_t call_addr;
nkeynes@359
   109
    sh4addr_t target_addr;
nkeynes@359
   110
    sh4addr_t stack_pointer;
nkeynes@359
   111
} call_stack[MAX_CALLSTACK];
nkeynes@359
   112
nkeynes@359
   113
static int call_stack_depth = 0;
nkeynes@359
   114
int sh4_call_trace_on = 0;
nkeynes@359
   115
nkeynes@430
   116
static inline void trace_call( sh4addr_t source, sh4addr_t dest ) 
nkeynes@359
   117
{
nkeynes@359
   118
    if( call_stack_depth < MAX_CALLSTACK ) {
nkeynes@359
   119
	call_stack[call_stack_depth].call_addr = source;
nkeynes@359
   120
	call_stack[call_stack_depth].target_addr = dest;
nkeynes@359
   121
	call_stack[call_stack_depth].stack_pointer = sh4r.r[15];
nkeynes@359
   122
    }
nkeynes@359
   123
    call_stack_depth++;
nkeynes@359
   124
}
nkeynes@359
   125
nkeynes@430
   126
static inline void trace_return( sh4addr_t source, sh4addr_t dest )
nkeynes@359
   127
{
nkeynes@359
   128
    if( call_stack_depth > 0 ) {
nkeynes@359
   129
	call_stack_depth--;
nkeynes@359
   130
    }
nkeynes@359
   131
}
nkeynes@359
   132
nkeynes@359
   133
void fprint_stack_trace( FILE *f )
nkeynes@359
   134
{
nkeynes@359
   135
    int i = call_stack_depth -1;
nkeynes@359
   136
    if( i >= MAX_CALLSTACK )
nkeynes@359
   137
	i = MAX_CALLSTACK - 1;
nkeynes@359
   138
    for( ; i >= 0; i-- ) {
nkeynes@359
   139
	fprintf( f, "%d. Call from %08X => %08X, SP=%08X\n", 
nkeynes@359
   140
		 (call_stack_depth - i), call_stack[i].call_addr,
nkeynes@359
   141
		 call_stack[i].target_addr, call_stack[i].stack_pointer );
nkeynes@359
   142
    }
nkeynes@359
   143
}
nkeynes@359
   144
nkeynes@359
   145
#define TRACE_CALL( source, dest ) trace_call(source, dest)
nkeynes@359
   146
#define TRACE_RETURN( source, dest ) trace_return(source, dest)
nkeynes@359
   147
#else
nkeynes@359
   148
#define TRACE_CALL( dest, rts ) 
nkeynes@359
   149
#define TRACE_RETURN( source, dest )
nkeynes@359
   150
#endif
nkeynes@359
   151
nkeynes@953
   152
static gboolean FASTCALL sh4_raise_slot_exception( int normal_code, int slot_code ) {
nkeynes@953
   153
    if( sh4r.in_delay_slot ) {
nkeynes@953
   154
        sh4_raise_exception(slot_code);
nkeynes@953
   155
    } else {
nkeynes@953
   156
        sh4_raise_exception(normal_code);
nkeynes@953
   157
    }
nkeynes@953
   158
    return TRUE;
nkeynes@953
   159
}
nkeynes@953
   160
nkeynes@953
   161
nkeynes@953
   162
#define CHECKPRIV() if( !IS_SH4_PRIVMODE() ) { return sh4_raise_slot_exception( EXC_ILLEGAL, EXC_SLOT_ILLEGAL ); }
nkeynes@953
   163
#define CHECKRALIGN16(addr) if( (addr)&0x01 ) { sh4_raise_exception( EXC_DATA_ADDR_READ ); return TRUE; }
nkeynes@953
   164
#define CHECKRALIGN32(addr) if( (addr)&0x03 ) { sh4_raise_exception( EXC_DATA_ADDR_READ ); return TRUE; }
nkeynes@953
   165
#define CHECKRALIGN64(addr) if( (addr)&0x07 ) { sh4_raise_exception( EXC_DATA_ADDR_READ ); return TRUE; }
nkeynes@953
   166
#define CHECKWALIGN16(addr) if( (addr)&0x01 ) { sh4_raise_exception( EXC_DATA_ADDR_WRITE ); return TRUE; }
nkeynes@953
   167
#define CHECKWALIGN32(addr) if( (addr)&0x03 ) { sh4_raise_exception( EXC_DATA_ADDR_WRITE ); return TRUE; }
nkeynes@953
   168
#define CHECKWALIGN64(addr) if( (addr)&0x07 ) { sh4_raise_exception( EXC_DATA_ADDR_WRITE ); return TRUE; }
nkeynes@732
   169
nkeynes@732
   170
#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@740
   171
#define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); sh4_core_exit(CORE_EXIT_HALT); return FALSE; }
nkeynes@953
   172
#define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) { sh4_raise_exception(EXC_SLOT_ILLEGAL); return TRUE; }
nkeynes@953
   173
nkeynes@953
   174
#define ADDRSPACE (IS_SH4_PRIVMODE() ? sh4_address_space : sh4_user_address_space)
nkeynes@953
   175
#define SQADDRSPACE (IS_SH4_PRIVMODE() ? storequeue_address_space : storequeue_user_address_space)
nkeynes@732
   176
nkeynes@927
   177
#ifdef HAVE_FRAME_ADDRESS
nkeynes@927
   178
static FASTCALL __attribute__((noinline)) void *__first_arg(void *a, void *b) { return a; }
nkeynes@927
   179
#define INIT_EXCEPTIONS(label) goto *__first_arg(&&fnstart,&&label); fnstart:
nkeynes@953
   180
#define MEM_READ_BYTE( addr, val ) val = ((mem_read_exc_fn_t)ADDRSPACE[(addr)>>12]->read_byte)((addr), &&except)
nkeynes@953
   181
#define MEM_READ_WORD( addr, val ) val = ((mem_read_exc_fn_t)ADDRSPACE[(addr)>>12]->read_word)((addr), &&except)
nkeynes@953
   182
#define MEM_READ_LONG( addr, val ) val = ((mem_read_exc_fn_t)ADDRSPACE[(addr)>>12]->read_long)((addr), &&except)
nkeynes@953
   183
#define MEM_WRITE_BYTE( addr, val ) ((mem_write_exc_fn_t)ADDRSPACE[(addr)>>12]->write_byte)((addr), (val), &&except)
nkeynes@953
   184
#define MEM_WRITE_WORD( addr, val ) ((mem_write_exc_fn_t)ADDRSPACE[(addr)>>12]->write_word)((addr), (val), &&except)
nkeynes@953
   185
#define MEM_WRITE_LONG( addr, val ) ((mem_write_exc_fn_t)ADDRSPACE[(addr)>>12]->write_long)((addr), (val), &&except)
nkeynes@953
   186
#define MEM_PREFETCH( addr ) ((mem_prefetch_exc_fn_t)ADDRSPACE[(addr)>>12]->prefetch)((addr), &&except)
nkeynes@927
   187
#else
nkeynes@927
   188
#define INIT_EXCEPTIONS(label)
nkeynes@953
   189
#define MEM_READ_BYTE( addr, val ) val = ADDRSPACE[(addr)>>12]->read_byte(addr)
nkeynes@953
   190
#define MEM_READ_WORD( addr, val ) val = ADDRSPACE[(addr)>>12]->read_word(addr)
nkeynes@953
   191
#define MEM_READ_LONG( addr, val ) val = ADDRSPACE[(addr)>>12]->read_long(addr)
nkeynes@953
   192
#define MEM_WRITE_BYTE( addr, val ) ADDRSPACE[(addr)>>12]->write_byte(addr, val)
nkeynes@953
   193
#define MEM_WRITE_WORD( addr, val ) ADDRSPACE[(addr)>>12]->write_word(addr, val)
nkeynes@953
   194
#define MEM_WRITE_LONG( addr, val ) ADDRSPACE[(addr)>>12]->write_long(addr, val)
nkeynes@953
   195
#define MEM_PREFETCH( addr ) ADDRSPACE[(addr)>>12]->prefetch(addr)
nkeynes@927
   196
#endif
nkeynes@359
   197
nkeynes@359
   198
#define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
nkeynes@359
   199
nkeynes@732
   200
#define MEM_FP_READ( addr, reg ) \
nkeynes@732
   201
    if( IS_FPU_DOUBLESIZE() ) { \
nkeynes@732
   202
	CHECKRALIGN64(addr); \
nkeynes@927
   203
        if( reg & 1 ) { \
nkeynes@953
   204
            MEM_READ_LONG( addr, *((uint32_t *)&XF((reg) & 0x0E)) ); \
nkeynes@953
   205
            MEM_READ_LONG( addr+4, *((uint32_t *)&XF(reg)) ); \
nkeynes@927
   206
        } else { \
nkeynes@953
   207
            MEM_READ_LONG( addr, *((uint32_t *)&FR(reg)) ); \
nkeynes@953
   208
            MEM_READ_LONG( addr+4, *((uint32_t *)&FR((reg)|0x01)) ); \
nkeynes@732
   209
	} \
nkeynes@732
   210
    } else { \
nkeynes@732
   211
        CHECKRALIGN32(addr); \
nkeynes@953
   212
        MEM_READ_LONG( addr, *((uint32_t *)&FR(reg)) ); \
nkeynes@359
   213
    }
nkeynes@732
   214
#define MEM_FP_WRITE( addr, reg ) \
nkeynes@732
   215
    if( IS_FPU_DOUBLESIZE() ) { \
nkeynes@732
   216
        CHECKWALIGN64(addr); \
nkeynes@927
   217
        if( reg & 1 ) { \
nkeynes@953
   218
	    MEM_WRITE_LONG( addr, *((uint32_t *)&XF((reg)&0x0E)) ); \
nkeynes@953
   219
	    MEM_WRITE_LONG( addr+4, *((uint32_t *)&XF(reg)) ); \
nkeynes@927
   220
        } else { \
nkeynes@953
   221
	    MEM_WRITE_LONG( addr, *((uint32_t *)&FR(reg)) ); \
nkeynes@953
   222
	    MEM_WRITE_LONG( addr+4, *((uint32_t *)&FR((reg)|0x01)) ); \
nkeynes@732
   223
	} \
nkeynes@732
   224
    } else { \
nkeynes@732
   225
    	CHECKWALIGN32(addr); \
nkeynes@953
   226
        MEM_WRITE_LONG(addr, *((uint32_t *)&FR((reg))) ); \
nkeynes@359
   227
    }
nkeynes@359
   228
nkeynes@953
   229
#define UNDEF(ir)
nkeynes@953
   230
#define UNIMP(ir)
nkeynes@953
   231
nkeynes@953
   232
/**
nkeynes@953
   233
 * Perform instruction-completion following core exit of a partially completed
nkeynes@953
   234
 * instruction. NOTE: This is only allowed on memory writes, operation is not
nkeynes@953
   235
 * guaranteed in any other case.
nkeynes@953
   236
 */
nkeynes@953
   237
void sh4_finalize_instruction( void )
nkeynes@953
   238
{
nkeynes@953
   239
    unsigned short ir;
nkeynes@953
   240
    uint32_t tmp;
nkeynes@953
   241
nkeynes@953
   242
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@953
   243
    ir = *(uint16_t *)GET_ICACHE_PTR(sh4r.pc);
nkeynes@953
   244
    
nkeynes@953
   245
    /**
nkeynes@953
   246
     * Note - we can't take an exit on a control transfer instruction itself,
nkeynes@953
   247
     * which means the exit must have happened in the delay slot. So for these
nkeynes@953
   248
     * cases, finalize the delay slot instruction, and re-execute the control transfer.
nkeynes@953
   249
     *
nkeynes@953
   250
     * For delay slots which modify the argument used in the branch instruction,
nkeynes@953
   251
     * we pretty much just assume that that can't have already happened in an exit case.
nkeynes@953
   252
     */
nkeynes@953
   253
    
nkeynes@953
   254
%%
nkeynes@953
   255
BRA disp {: 
nkeynes@953
   256
    sh4r.pc += 2; 
nkeynes@953
   257
    sh4_finalize_instruction(); 
nkeynes@953
   258
    sh4r.pc += disp;
nkeynes@953
   259
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   260
:}
nkeynes@953
   261
BRAF Rn {: 
nkeynes@953
   262
    sh4r.pc += 2; 
nkeynes@953
   263
    tmp = sh4r.r[Rn];
nkeynes@953
   264
    sh4_finalize_instruction(); 
nkeynes@953
   265
    sh4r.pc += tmp;
nkeynes@953
   266
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   267
:}
nkeynes@953
   268
BSR disp {: 
nkeynes@953
   269
    /* Note: PR is already set */ 
nkeynes@953
   270
    sh4r.pc += 2;
nkeynes@953
   271
    sh4_finalize_instruction();
nkeynes@953
   272
    sh4r.pc += disp;
nkeynes@953
   273
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   274
:}
nkeynes@953
   275
BSRF Rn {:
nkeynes@953
   276
    /* Note: PR is already set */ 
nkeynes@953
   277
    sh4r.pc += 2;
nkeynes@953
   278
    tmp = sh4r.r[Rn];
nkeynes@953
   279
    sh4_finalize_instruction();
nkeynes@953
   280
    sh4r.pc += tmp;
nkeynes@953
   281
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   282
:}
nkeynes@953
   283
BF/S disp {: 
nkeynes@953
   284
    sh4r.pc += 2;
nkeynes@953
   285
    sh4_finalize_instruction();
nkeynes@953
   286
    if( !sh4r.t ) {
nkeynes@953
   287
        sh4r.pc += disp;
nkeynes@953
   288
    }
nkeynes@953
   289
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   290
:}
nkeynes@953
   291
BT/S disp {: 
nkeynes@953
   292
    sh4r.pc += 2;
nkeynes@953
   293
    sh4_finalize_instruction();
nkeynes@953
   294
    if( sh4r.t ) {
nkeynes@953
   295
        sh4r.pc += disp;
nkeynes@953
   296
    }
nkeynes@953
   297
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   298
:}
nkeynes@953
   299
JMP @Rn {:
nkeynes@953
   300
    sh4r.pc += 2;
nkeynes@953
   301
    tmp = sh4r.r[Rn];
nkeynes@953
   302
    sh4_finalize_instruction();
nkeynes@953
   303
    sh4r.pc = tmp;
nkeynes@953
   304
    sh4r.new_pc = tmp + 2;
nkeynes@953
   305
    sh4r.slice_cycle += 2*sh4_cpu_period;
nkeynes@953
   306
    return;
nkeynes@953
   307
:}
nkeynes@953
   308
JSR @Rn {: 
nkeynes@953
   309
    /* Note: PR is already set */ 
nkeynes@953
   310
    sh4r.pc += 2;
nkeynes@953
   311
    tmp = sh4r.r[Rn];
nkeynes@953
   312
    sh4_finalize_instruction();
nkeynes@953
   313
    sh4r.pc = tmp;
nkeynes@953
   314
    sh4r.new_pc = tmp + 2;
nkeynes@953
   315
    sh4r.slice_cycle += 2*sh4_cpu_period;
nkeynes@953
   316
    return;
nkeynes@953
   317
:}
nkeynes@953
   318
RTS {: 
nkeynes@953
   319
    sh4r.pc += 2;
nkeynes@953
   320
    sh4_finalize_instruction();
nkeynes@953
   321
    sh4r.pc = sh4r.pr;
nkeynes@953
   322
    sh4r.new_pc = sh4r.pr + 2;
nkeynes@953
   323
    sh4r.slice_cycle += 2*sh4_cpu_period;
nkeynes@953
   324
    return;
nkeynes@953
   325
:}
nkeynes@953
   326
RTE {: 
nkeynes@953
   327
    /* SR is already set */
nkeynes@953
   328
    sh4r.pc += 2;
nkeynes@953
   329
    sh4_finalize_instruction();
nkeynes@953
   330
    sh4r.pc = sh4r.spc;
nkeynes@953
   331
    sh4r.new_pc = sh4r.pr + 2;
nkeynes@953
   332
    sh4r.slice_cycle += 2*sh4_cpu_period;
nkeynes@953
   333
    return;
nkeynes@953
   334
:}
nkeynes@953
   335
MOV.B Rm, @-Rn {: sh4r.r[Rn]--; :}
nkeynes@953
   336
MOV.W Rm, @-Rn {: sh4r.r[Rn] -= 2; :}
nkeynes@953
   337
MOV.L Rm, @-Rn {: sh4r.r[Rn] -= 4; :}
nkeynes@970
   338
MOV.B @Rm+, Rn {: if( Rm != Rn ) { sh4r.r[Rm] ++;  } :}
nkeynes@970
   339
MOV.W @Rm+, Rn {: if( Rm != Rn ) { sh4r.r[Rm] += 2; } :}
nkeynes@970
   340
MOV.L @Rm+, Rn {: if( Rm != Rn ) { sh4r.r[Rm] += 4; } :}
nkeynes@953
   341
%%
nkeynes@953
   342
    sh4r.pc += 2;
nkeynes@953
   343
    sh4r.new_pc = sh4r.pc+2;
nkeynes@953
   344
    sh4r.slice_cycle += sh4_cpu_period;
nkeynes@953
   345
}
nkeynes@953
   346
nkeynes@953
   347
#undef UNDEF(ir)
nkeynes@953
   348
#undef UNIMP(ir)
nkeynes@953
   349
nkeynes@953
   350
#define UNDEF(ir) return sh4_raise_slot_exception(EXC_ILLEGAL, EXC_SLOT_ILLEGAL)
nkeynes@953
   351
#define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); sh4_core_exit(CORE_EXIT_HALT); return FALSE; }while(0)
nkeynes@953
   352
nkeynes@953
   353
nkeynes@359
   354
gboolean sh4_execute_instruction( void )
nkeynes@359
   355
{
nkeynes@359
   356
    uint32_t pc;
nkeynes@359
   357
    unsigned short ir;
nkeynes@359
   358
    uint32_t tmp;
nkeynes@359
   359
    float ftmp;
nkeynes@359
   360
    double dtmp;
nkeynes@586
   361
    int64_t memtmp; // temporary holder for memory reads
nkeynes@927
   362
nkeynes@927
   363
    INIT_EXCEPTIONS(except)
nkeynes@359
   364
    
nkeynes@359
   365
#define R0 sh4r.r[0]
nkeynes@359
   366
    pc = sh4r.pc;
nkeynes@359
   367
    if( pc > 0xFFFFFF00 ) {
nkeynes@359
   368
	/* SYSCALL Magic */
nkeynes@359
   369
	syscall_invoke( pc );
nkeynes@359
   370
	sh4r.in_delay_slot = 0;
nkeynes@359
   371
	pc = sh4r.pc = sh4r.pr;
nkeynes@359
   372
	sh4r.new_pc = sh4r.pc + 2;
nkeynes@671
   373
        return TRUE;
nkeynes@359
   374
    }
nkeynes@359
   375
    CHECKRALIGN16(pc);
nkeynes@359
   376
nkeynes@671
   377
#ifdef ENABLE_SH4STATS
nkeynes@671
   378
    sh4_stats_add_by_pc(sh4r.pc);
nkeynes@671
   379
#endif
nkeynes@671
   380
nkeynes@359
   381
    /* Read instruction */
nkeynes@586
   382
    if( !IS_IN_ICACHE(pc) ) {
nkeynes@586
   383
	if( !mmu_update_icache(pc) ) {
nkeynes@586
   384
	    // Fault - look for the fault handler
nkeynes@586
   385
	    if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@586
   386
		// double fault - halt
nkeynes@586
   387
		ERROR( "Double fault - halting" );
nkeynes@740
   388
		sh4_core_exit(CORE_EXIT_HALT);
nkeynes@586
   389
		return FALSE;
nkeynes@586
   390
	    }
nkeynes@359
   391
	}
nkeynes@586
   392
	pc = sh4r.pc;
nkeynes@359
   393
    }
nkeynes@586
   394
    assert( IS_IN_ICACHE(pc) );
nkeynes@586
   395
    ir = *(uint16_t *)GET_ICACHE_PTR(sh4r.pc);
nkeynes@953
   396
    
nkeynes@953
   397
    /* FIXME: This is a bit of a hack, but the PC of the delay slot should not
nkeynes@953
   398
     * be visible until after the instruction has executed (for exception 
nkeynes@953
   399
     * correctness)
nkeynes@953
   400
     */
nkeynes@953
   401
    if( sh4r.in_delay_slot ) {
nkeynes@953
   402
    	sh4r.pc -= 2;
nkeynes@953
   403
    }
nkeynes@359
   404
%%
nkeynes@359
   405
AND Rm, Rn {: sh4r.r[Rn] &= sh4r.r[Rm]; :}
nkeynes@359
   406
AND #imm, R0 {: R0 &= imm; :}
nkeynes@586
   407
 AND.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm & tmp ); :}
nkeynes@359
   408
NOT Rm, Rn {: sh4r.r[Rn] = ~sh4r.r[Rm]; :}
nkeynes@359
   409
OR Rm, Rn {: sh4r.r[Rn] |= sh4r.r[Rm]; :}
nkeynes@359
   410
OR #imm, R0  {: R0 |= imm; :}
nkeynes@586
   411
 OR.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm | tmp ); :}
nkeynes@359
   412
TAS.B @Rn {:
nkeynes@586
   413
    MEM_READ_BYTE( sh4r.r[Rn], tmp );
nkeynes@359
   414
    sh4r.t = ( tmp == 0 ? 1 : 0 );
nkeynes@359
   415
    MEM_WRITE_BYTE( sh4r.r[Rn], tmp | 0x80 );
nkeynes@359
   416
:}
nkeynes@359
   417
TST Rm, Rn {: sh4r.t = (sh4r.r[Rn]&sh4r.r[Rm] ? 0 : 1); :}
nkeynes@359
   418
TST #imm, R0 {: sh4r.t = (R0 & imm ? 0 : 1); :}
nkeynes@586
   419
 TST.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); sh4r.t = ( tmp & imm ? 0 : 1 ); :}
nkeynes@359
   420
XOR Rm, Rn {: sh4r.r[Rn] ^= sh4r.r[Rm]; :}
nkeynes@359
   421
XOR #imm, R0 {: R0 ^= imm; :}
nkeynes@586
   422
 XOR.B #imm, @(R0, GBR) {: MEM_READ_BYTE(R0+sh4r.gbr, tmp); MEM_WRITE_BYTE( R0 + sh4r.gbr, imm ^ tmp ); :}
nkeynes@359
   423
XTRCT Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rn]>>16) | (sh4r.r[Rm]<<16); :}
nkeynes@359
   424
nkeynes@359
   425
ROTL Rn {:
nkeynes@359
   426
    sh4r.t = sh4r.r[Rn] >> 31;
nkeynes@359
   427
    sh4r.r[Rn] <<= 1;
nkeynes@359
   428
    sh4r.r[Rn] |= sh4r.t;
nkeynes@359
   429
:}
nkeynes@359
   430
ROTR Rn {:
nkeynes@359
   431
    sh4r.t = sh4r.r[Rn] & 0x00000001;
nkeynes@359
   432
    sh4r.r[Rn] >>= 1;
nkeynes@359
   433
    sh4r.r[Rn] |= (sh4r.t << 31);
nkeynes@359
   434
:}
nkeynes@359
   435
ROTCL Rn {:
nkeynes@359
   436
    tmp = sh4r.r[Rn] >> 31;
nkeynes@359
   437
    sh4r.r[Rn] <<= 1;
nkeynes@359
   438
    sh4r.r[Rn] |= sh4r.t;
nkeynes@359
   439
    sh4r.t = tmp;
nkeynes@359
   440
:}
nkeynes@359
   441
ROTCR Rn {:
nkeynes@359
   442
    tmp = sh4r.r[Rn] & 0x00000001;
nkeynes@359
   443
    sh4r.r[Rn] >>= 1;
nkeynes@359
   444
    sh4r.r[Rn] |= (sh4r.t << 31 );
nkeynes@359
   445
    sh4r.t = tmp;
nkeynes@359
   446
:}
nkeynes@359
   447
SHAD Rm, Rn {:
nkeynes@359
   448
    tmp = sh4r.r[Rm];
nkeynes@359
   449
    if( (tmp & 0x80000000) == 0 ) sh4r.r[Rn] <<= (tmp&0x1f);
nkeynes@359
   450
    else if( (tmp & 0x1F) == 0 )  
nkeynes@359
   451
        sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> 31;
nkeynes@359
   452
    else 
nkeynes@359
   453
	sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> (((~sh4r.r[Rm]) & 0x1F)+1);
nkeynes@359
   454
:}
nkeynes@359
   455
SHLD Rm, Rn {:
nkeynes@359
   456
    tmp = sh4r.r[Rm];
nkeynes@359
   457
    if( (tmp & 0x80000000) == 0 ) sh4r.r[Rn] <<= (tmp&0x1f);
nkeynes@359
   458
    else if( (tmp & 0x1F) == 0 ) sh4r.r[Rn] = 0;
nkeynes@359
   459
    else sh4r.r[Rn] >>= (((~tmp) & 0x1F)+1);
nkeynes@359
   460
:}
nkeynes@359
   461
SHAL Rn {:
nkeynes@359
   462
    sh4r.t = sh4r.r[Rn] >> 31;
nkeynes@359
   463
    sh4r.r[Rn] <<= 1;
nkeynes@359
   464
:}
nkeynes@359
   465
SHAR Rn {:
nkeynes@359
   466
    sh4r.t = sh4r.r[Rn] & 0x00000001;
nkeynes@359
   467
    sh4r.r[Rn] = ((int32_t)sh4r.r[Rn]) >> 1;
nkeynes@359
   468
:}
nkeynes@359
   469
SHLL Rn {: sh4r.t = sh4r.r[Rn] >> 31; sh4r.r[Rn] <<= 1; :}
nkeynes@359
   470
SHLR Rn {: sh4r.t = sh4r.r[Rn] & 0x00000001; sh4r.r[Rn] >>= 1; :}
nkeynes@359
   471
SHLL2 Rn {: sh4r.r[Rn] <<= 2; :}
nkeynes@359
   472
SHLR2 Rn {: sh4r.r[Rn] >>= 2; :}
nkeynes@359
   473
SHLL8 Rn {: sh4r.r[Rn] <<= 8; :}
nkeynes@359
   474
SHLR8 Rn {: sh4r.r[Rn] >>= 8; :}
nkeynes@359
   475
SHLL16 Rn {: sh4r.r[Rn] <<= 16; :}
nkeynes@359
   476
SHLR16 Rn {: sh4r.r[Rn] >>= 16; :}
nkeynes@359
   477
nkeynes@359
   478
EXTU.B Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]&0x000000FF; :}
nkeynes@359
   479
EXTU.W Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]&0x0000FFFF; :}
nkeynes@359
   480
EXTS.B Rm, Rn {: sh4r.r[Rn] = SIGNEXT8( sh4r.r[Rm]&0x000000FF ); :}
nkeynes@359
   481
EXTS.W Rm, Rn {: sh4r.r[Rn] = SIGNEXT16( sh4r.r[Rm]&0x0000FFFF ); :}
nkeynes@359
   482
SWAP.B Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rm]&0xFFFF0000) | ((sh4r.r[Rm]&0x0000FF00)>>8) | ((sh4r.r[Rm]&0x000000FF)<<8); :}
nkeynes@359
   483
SWAP.W Rm, Rn {: sh4r.r[Rn] = (sh4r.r[Rm]>>16) | (sh4r.r[Rm]<<16); :}
nkeynes@359
   484
nkeynes@359
   485
CLRT {: sh4r.t = 0; :}
nkeynes@359
   486
SETT {: sh4r.t = 1; :}
nkeynes@359
   487
CLRMAC {: sh4r.mac = 0; :}
nkeynes@550
   488
LDTLB {: MMU_ldtlb(); :}
nkeynes@359
   489
CLRS {: sh4r.s = 0; :}
nkeynes@359
   490
SETS {: sh4r.s = 1; :}
nkeynes@359
   491
MOVT Rn {: sh4r.r[Rn] = sh4r.t; :}
nkeynes@359
   492
NOP {: /* NOP */ :}
nkeynes@359
   493
nkeynes@359
   494
PREF @Rn {:
nkeynes@953
   495
    MEM_PREFETCH(sh4r.r[Rn]);
nkeynes@359
   496
:}
nkeynes@359
   497
OCBI @Rn {: :}
nkeynes@359
   498
OCBP @Rn {: :}
nkeynes@359
   499
OCBWB @Rn {: :}
nkeynes@359
   500
MOVCA.L R0, @Rn {:
nkeynes@359
   501
    tmp = sh4r.r[Rn];
nkeynes@359
   502
    CHECKWALIGN32(tmp);
nkeynes@359
   503
    MEM_WRITE_LONG( tmp, R0 );
nkeynes@359
   504
:}
nkeynes@359
   505
MOV.B Rm, @(R0, Rn) {: MEM_WRITE_BYTE( R0 + sh4r.r[Rn], sh4r.r[Rm] ); :}
nkeynes@359
   506
MOV.W Rm, @(R0, Rn) {: 
nkeynes@359
   507
    CHECKWALIGN16( R0 + sh4r.r[Rn] );
nkeynes@359
   508
    MEM_WRITE_WORD( R0 + sh4r.r[Rn], sh4r.r[Rm] );
nkeynes@359
   509
:}
nkeynes@359
   510
MOV.L Rm, @(R0, Rn) {:
nkeynes@359
   511
    CHECKWALIGN32( R0 + sh4r.r[Rn] );
nkeynes@359
   512
    MEM_WRITE_LONG( R0 + sh4r.r[Rn], sh4r.r[Rm] );
nkeynes@359
   513
:}
nkeynes@586
   514
MOV.B @(R0, Rm), Rn {: MEM_READ_BYTE( R0 + sh4r.r[Rm], sh4r.r[Rn] ); :}
nkeynes@359
   515
MOV.W @(R0, Rm), Rn {: CHECKRALIGN16( R0 + sh4r.r[Rm] );
nkeynes@586
   516
    MEM_READ_WORD( R0 + sh4r.r[Rm], sh4r.r[Rn] );
nkeynes@359
   517
:}
nkeynes@359
   518
MOV.L @(R0, Rm), Rn {: CHECKRALIGN32( R0 + sh4r.r[Rm] );
nkeynes@586
   519
    MEM_READ_LONG( R0 + sh4r.r[Rm], sh4r.r[Rn] );
nkeynes@359
   520
:}
nkeynes@359
   521
MOV.L Rm, @(disp, Rn) {:
nkeynes@359
   522
    tmp = sh4r.r[Rn] + disp;
nkeynes@359
   523
    CHECKWALIGN32( tmp );
nkeynes@359
   524
    MEM_WRITE_LONG( tmp, sh4r.r[Rm] );
nkeynes@359
   525
:}
nkeynes@359
   526
MOV.B Rm, @Rn {: MEM_WRITE_BYTE( sh4r.r[Rn], sh4r.r[Rm] ); :}
nkeynes@359
   527
MOV.W Rm, @Rn {: CHECKWALIGN16( sh4r.r[Rn] ); MEM_WRITE_WORD( sh4r.r[Rn], sh4r.r[Rm] ); :}
nkeynes@359
   528
MOV.L Rm, @Rn {: CHECKWALIGN32( sh4r.r[Rn] ); MEM_WRITE_LONG( sh4r.r[Rn], sh4r.r[Rm] ); :}
nkeynes@587
   529
 MOV.B Rm, @-Rn {: MEM_WRITE_BYTE( sh4r.r[Rn]-1, sh4r.r[Rm] ); sh4r.r[Rn]--; :}
nkeynes@587
   530
 MOV.W Rm, @-Rn {: CHECKWALIGN16( sh4r.r[Rn] ); MEM_WRITE_WORD( sh4r.r[Rn]-2, sh4r.r[Rm] ); sh4r.r[Rn] -= 2; :}
nkeynes@587
   531
 MOV.L Rm, @-Rn {: CHECKWALIGN32( sh4r.r[Rn] ); MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.r[Rm] ); sh4r.r[Rn] -= 4; :}
nkeynes@359
   532
MOV.L @(disp, Rm), Rn {:
nkeynes@359
   533
    tmp = sh4r.r[Rm] + disp;
nkeynes@359
   534
    CHECKRALIGN32( tmp );
nkeynes@586
   535
    MEM_READ_LONG( tmp, sh4r.r[Rn] );
nkeynes@359
   536
:}
nkeynes@586
   537
MOV.B @Rm, Rn {: MEM_READ_BYTE( sh4r.r[Rm], sh4r.r[Rn] ); :}
nkeynes@586
   538
 MOV.W @Rm, Rn {: CHECKRALIGN16( sh4r.r[Rm] ); MEM_READ_WORD( sh4r.r[Rm], sh4r.r[Rn] ); :}
nkeynes@586
   539
 MOV.L @Rm, Rn {: CHECKRALIGN32( sh4r.r[Rm] ); MEM_READ_LONG( sh4r.r[Rm], sh4r.r[Rn] ); :}
nkeynes@359
   540
MOV Rm, Rn {: sh4r.r[Rn] = sh4r.r[Rm]; :}
nkeynes@970
   541
 MOV.B @Rm+, Rn {: MEM_READ_BYTE( sh4r.r[Rm], sh4r.r[Rn] ); if( Rm != Rn ) { sh4r.r[Rm] ++; } :}
nkeynes@970
   542
 MOV.W @Rm+, Rn {: CHECKRALIGN16( sh4r.r[Rm] ); MEM_READ_WORD( sh4r.r[Rm], sh4r.r[Rn] ); if( Rm != Rn ) { sh4r.r[Rm] += 2; } :}
nkeynes@970
   543
 MOV.L @Rm+, Rn {: CHECKRALIGN32( sh4r.r[Rm] ); MEM_READ_LONG( sh4r.r[Rm], sh4r.r[Rn] ); if( Rm != Rn ) { sh4r.r[Rm] += 4; } :}
nkeynes@359
   544
MOV.L @(disp, PC), Rn {:
nkeynes@359
   545
    CHECKSLOTILLEGAL();
nkeynes@359
   546
    tmp = (pc&0xFFFFFFFC) + disp + 4;
nkeynes@586
   547
    MEM_READ_LONG( tmp, sh4r.r[Rn] );
nkeynes@359
   548
:}
nkeynes@359
   549
MOV.B R0, @(disp, GBR) {: MEM_WRITE_BYTE( sh4r.gbr + disp, R0 ); :}
nkeynes@359
   550
MOV.W R0, @(disp, GBR) {:
nkeynes@359
   551
    tmp = sh4r.gbr + disp;
nkeynes@359
   552
    CHECKWALIGN16( tmp );
nkeynes@359
   553
    MEM_WRITE_WORD( tmp, R0 );
nkeynes@359
   554
:}
nkeynes@359
   555
MOV.L R0, @(disp, GBR) {:
nkeynes@359
   556
    tmp = sh4r.gbr + disp;
nkeynes@359
   557
    CHECKWALIGN32( tmp );
nkeynes@359
   558
    MEM_WRITE_LONG( tmp, R0 );
nkeynes@359
   559
:}
nkeynes@586
   560
 MOV.B @(disp, GBR), R0 {: MEM_READ_BYTE( sh4r.gbr + disp, R0 ); :}
nkeynes@359
   561
MOV.W @(disp, GBR), R0 {: 
nkeynes@359
   562
    tmp = sh4r.gbr + disp;
nkeynes@359
   563
    CHECKRALIGN16( tmp );
nkeynes@586
   564
    MEM_READ_WORD( tmp, R0 );
nkeynes@359
   565
:}
nkeynes@359
   566
MOV.L @(disp, GBR), R0 {:
nkeynes@359
   567
    tmp = sh4r.gbr + disp;
nkeynes@359
   568
    CHECKRALIGN32( tmp );
nkeynes@586
   569
    MEM_READ_LONG( tmp, R0 );
nkeynes@359
   570
:}
nkeynes@359
   571
MOV.B R0, @(disp, Rn) {: MEM_WRITE_BYTE( sh4r.r[Rn] + disp, R0 ); :}
nkeynes@359
   572
MOV.W R0, @(disp, Rn) {: 
nkeynes@359
   573
    tmp = sh4r.r[Rn] + disp;
nkeynes@359
   574
    CHECKWALIGN16( tmp );
nkeynes@359
   575
    MEM_WRITE_WORD( tmp, R0 );
nkeynes@359
   576
:}
nkeynes@586
   577
 MOV.B @(disp, Rm), R0 {: MEM_READ_BYTE( sh4r.r[Rm] + disp, R0 ); :}
nkeynes@359
   578
MOV.W @(disp, Rm), R0 {: 
nkeynes@359
   579
    tmp = sh4r.r[Rm] + disp;
nkeynes@359
   580
    CHECKRALIGN16( tmp );
nkeynes@586
   581
    MEM_READ_WORD( tmp, R0 );
nkeynes@359
   582
:}
nkeynes@359
   583
MOV.W @(disp, PC), Rn {:
nkeynes@359
   584
    CHECKSLOTILLEGAL();
nkeynes@359
   585
    tmp = pc + 4 + disp;
nkeynes@586
   586
    MEM_READ_WORD( tmp, sh4r.r[Rn] );
nkeynes@359
   587
:}
nkeynes@359
   588
MOVA @(disp, PC), R0 {:
nkeynes@359
   589
    CHECKSLOTILLEGAL();
nkeynes@359
   590
    R0 = (pc&0xFFFFFFFC) + disp + 4;
nkeynes@359
   591
:}
nkeynes@359
   592
MOV #imm, Rn {:  sh4r.r[Rn] = imm; :}
nkeynes@359
   593
nkeynes@732
   594
FMOV @(R0, Rm), FRn {: MEM_FP_READ( sh4r.r[Rm] + R0, FRn ); :}
nkeynes@732
   595
FMOV FRm, @(R0, Rn) {: MEM_FP_WRITE( sh4r.r[Rn] + R0, FRm ); :}
nkeynes@732
   596
FMOV @Rm, FRn {: MEM_FP_READ( sh4r.r[Rm], FRn ); :}
nkeynes@732
   597
FMOV @Rm+, FRn {: MEM_FP_READ( sh4r.r[Rm], FRn ); sh4r.r[Rm] += FP_WIDTH; :}
nkeynes@732
   598
FMOV FRm, @Rn {: MEM_FP_WRITE( sh4r.r[Rn], FRm ); :}
nkeynes@732
   599
 FMOV FRm, @-Rn {: MEM_FP_WRITE( sh4r.r[Rn] - FP_WIDTH, FRm ); sh4r.r[Rn] -= FP_WIDTH; :}
nkeynes@732
   600
FMOV FRm, FRn {: 
nkeynes@732
   601
    if( IS_FPU_DOUBLESIZE() )
nkeynes@732
   602
	DR(FRn) = DR(FRm);
nkeynes@732
   603
    else
nkeynes@732
   604
	FR(FRn) = FR(FRm);
nkeynes@732
   605
:}
nkeynes@732
   606
nkeynes@359
   607
CMP/EQ #imm, R0 {: sh4r.t = ( R0 == imm ? 1 : 0 ); :}
nkeynes@359
   608
CMP/EQ Rm, Rn {: sh4r.t = ( sh4r.r[Rm] == sh4r.r[Rn] ? 1 : 0 ); :}
nkeynes@359
   609
CMP/GE Rm, Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) >= ((int32_t)sh4r.r[Rm]) ? 1 : 0 ); :}
nkeynes@359
   610
CMP/GT Rm, Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) > ((int32_t)sh4r.r[Rm]) ? 1 : 0 ); :}
nkeynes@359
   611
CMP/HI Rm, Rn {: sh4r.t = ( sh4r.r[Rn] > sh4r.r[Rm] ? 1 : 0 ); :}
nkeynes@359
   612
CMP/HS Rm, Rn {: sh4r.t = ( sh4r.r[Rn] >= sh4r.r[Rm] ? 1 : 0 ); :}
nkeynes@359
   613
CMP/PL Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) > 0 ? 1 : 0 ); :}
nkeynes@359
   614
CMP/PZ Rn {: sh4r.t = ( ((int32_t)sh4r.r[Rn]) >= 0 ? 1 : 0 ); :}
nkeynes@359
   615
CMP/STR Rm, Rn {: 
nkeynes@359
   616
    /* set T = 1 if any byte in RM & RN is the same */
nkeynes@359
   617
    tmp = sh4r.r[Rm] ^ sh4r.r[Rn];
nkeynes@359
   618
    sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
nkeynes@359
   619
             (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
nkeynes@359
   620
:}
nkeynes@359
   621
nkeynes@359
   622
ADD Rm, Rn {: sh4r.r[Rn] += sh4r.r[Rm]; :}
nkeynes@359
   623
ADD #imm, Rn {: sh4r.r[Rn] += imm; :}
nkeynes@359
   624
ADDC Rm, Rn {:
nkeynes@359
   625
    tmp = sh4r.r[Rn];
nkeynes@359
   626
    sh4r.r[Rn] += sh4r.r[Rm] + sh4r.t;
nkeynes@359
   627
    sh4r.t = ( sh4r.r[Rn] < tmp || (sh4r.r[Rn] == tmp && sh4r.t != 0) ? 1 : 0 );
nkeynes@359
   628
:}
nkeynes@359
   629
ADDV Rm, Rn {:
nkeynes@359
   630
    tmp = sh4r.r[Rn] + sh4r.r[Rm];
nkeynes@359
   631
    sh4r.t = ( (sh4r.r[Rn]>>31) == (sh4r.r[Rm]>>31) && ((sh4r.r[Rn]>>31) != (tmp>>31)) );
nkeynes@359
   632
    sh4r.r[Rn] = tmp;
nkeynes@359
   633
:}
nkeynes@359
   634
DIV0U {: sh4r.m = sh4r.q = sh4r.t = 0; :}
nkeynes@359
   635
DIV0S Rm, Rn {: 
nkeynes@359
   636
    sh4r.q = sh4r.r[Rn]>>31;
nkeynes@359
   637
    sh4r.m = sh4r.r[Rm]>>31;
nkeynes@359
   638
    sh4r.t = sh4r.q ^ sh4r.m;
nkeynes@359
   639
:}
nkeynes@359
   640
DIV1 Rm, Rn {:
nkeynes@384
   641
    /* This is derived from the sh4 manual with some simplifications */
nkeynes@359
   642
    uint32_t tmp0, tmp1, tmp2, dir;
nkeynes@359
   643
nkeynes@359
   644
    dir = sh4r.q ^ sh4r.m;
nkeynes@359
   645
    sh4r.q = (sh4r.r[Rn] >> 31);
nkeynes@359
   646
    tmp2 = sh4r.r[Rm];
nkeynes@359
   647
    sh4r.r[Rn] = (sh4r.r[Rn] << 1) | sh4r.t;
nkeynes@359
   648
    tmp0 = sh4r.r[Rn];
nkeynes@359
   649
    if( dir ) {
nkeynes@359
   650
         sh4r.r[Rn] += tmp2;
nkeynes@359
   651
         tmp1 = (sh4r.r[Rn]<tmp0 ? 1 : 0 );
nkeynes@359
   652
    } else {
nkeynes@359
   653
         sh4r.r[Rn] -= tmp2;
nkeynes@359
   654
         tmp1 = (sh4r.r[Rn]>tmp0 ? 1 : 0 );
nkeynes@359
   655
    }
nkeynes@359
   656
    sh4r.q ^= sh4r.m ^ tmp1;
nkeynes@359
   657
    sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
nkeynes@359
   658
:}
nkeynes@359
   659
DMULS.L Rm, Rn {: sh4r.mac = SIGNEXT32(sh4r.r[Rm]) * SIGNEXT32(sh4r.r[Rn]); :}
nkeynes@359
   660
DMULU.L Rm, Rn {: sh4r.mac = ((uint64_t)sh4r.r[Rm]) * ((uint64_t)sh4r.r[Rn]); :}
nkeynes@359
   661
DT Rn {:
nkeynes@359
   662
    sh4r.r[Rn] --;
nkeynes@359
   663
    sh4r.t = ( sh4r.r[Rn] == 0 ? 1 : 0 );
nkeynes@359
   664
:}
nkeynes@359
   665
MAC.W @Rm+, @Rn+ {:
nkeynes@587
   666
    int32_t stmp;
nkeynes@587
   667
    if( Rm == Rn ) {
nkeynes@587
   668
	CHECKRALIGN16(sh4r.r[Rn]);
nkeynes@587
   669
	MEM_READ_WORD( sh4r.r[Rn], tmp );
nkeynes@587
   670
	stmp = SIGNEXT16(tmp);
nkeynes@587
   671
	MEM_READ_WORD( sh4r.r[Rn]+2, tmp );
nkeynes@587
   672
	stmp *= SIGNEXT16(tmp);
nkeynes@587
   673
	sh4r.r[Rn] += 4;
nkeynes@587
   674
    } else {
nkeynes@587
   675
	CHECKRALIGN16( sh4r.r[Rn] );
nkeynes@587
   676
	CHECKRALIGN16( sh4r.r[Rm] );
nkeynes@587
   677
	MEM_READ_WORD(sh4r.r[Rn], tmp);
nkeynes@587
   678
	stmp = SIGNEXT16(tmp);
nkeynes@587
   679
	MEM_READ_WORD(sh4r.r[Rm], tmp);
nkeynes@587
   680
	stmp = stmp * SIGNEXT16(tmp);
nkeynes@587
   681
	sh4r.r[Rn] += 2;
nkeynes@587
   682
	sh4r.r[Rm] += 2;
nkeynes@587
   683
    }
nkeynes@359
   684
    if( sh4r.s ) {
nkeynes@359
   685
	int64_t tmpl = (int64_t)((int32_t)sh4r.mac) + (int64_t)stmp;
nkeynes@359
   686
	if( tmpl > (int64_t)0x000000007FFFFFFFLL ) {
nkeynes@359
   687
	    sh4r.mac = 0x000000017FFFFFFFLL;
nkeynes@359
   688
	} else if( tmpl < (int64_t)0xFFFFFFFF80000000LL ) {
nkeynes@359
   689
	    sh4r.mac = 0x0000000180000000LL;
nkeynes@359
   690
	} else {
nkeynes@359
   691
	    sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
nkeynes@359
   692
		((uint32_t)(sh4r.mac + stmp));
nkeynes@359
   693
	}
nkeynes@359
   694
    } else {
nkeynes@359
   695
	sh4r.mac += SIGNEXT32(stmp);
nkeynes@359
   696
    }
nkeynes@359
   697
:}
nkeynes@359
   698
MAC.L @Rm+, @Rn+ {:
nkeynes@587
   699
    int64_t tmpl;
nkeynes@587
   700
    if( Rm == Rn ) {
nkeynes@587
   701
	CHECKRALIGN32( sh4r.r[Rn] );
nkeynes@587
   702
	MEM_READ_LONG(sh4r.r[Rn], tmp);
nkeynes@587
   703
	tmpl = SIGNEXT32(tmp);
nkeynes@587
   704
	MEM_READ_LONG(sh4r.r[Rn]+4, tmp);
nkeynes@587
   705
	tmpl = tmpl * SIGNEXT32(tmp) + sh4r.mac;
nkeynes@587
   706
	sh4r.r[Rn] += 8;
nkeynes@587
   707
    } else {
nkeynes@587
   708
	CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@587
   709
	CHECKRALIGN32( sh4r.r[Rn] );
nkeynes@587
   710
	MEM_READ_LONG(sh4r.r[Rn], tmp);
nkeynes@587
   711
	tmpl = SIGNEXT32(tmp);
nkeynes@587
   712
	MEM_READ_LONG(sh4r.r[Rm], tmp);
nkeynes@587
   713
	tmpl = tmpl * SIGNEXT32(tmp) + sh4r.mac;
nkeynes@587
   714
	sh4r.r[Rn] += 4;
nkeynes@587
   715
	sh4r.r[Rm] += 4;
nkeynes@587
   716
    }
nkeynes@359
   717
    if( sh4r.s ) {
nkeynes@359
   718
        /* 48-bit Saturation. Yuch */
nkeynes@359
   719
        if( tmpl < (int64_t)0xFFFF800000000000LL )
nkeynes@359
   720
            tmpl = 0xFFFF800000000000LL;
nkeynes@359
   721
        else if( tmpl > (int64_t)0x00007FFFFFFFFFFFLL )
nkeynes@359
   722
            tmpl = 0x00007FFFFFFFFFFFLL;
nkeynes@359
   723
    }
nkeynes@359
   724
    sh4r.mac = tmpl;
nkeynes@359
   725
:}
nkeynes@359
   726
MUL.L Rm, Rn {: sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
nkeynes@359
   727
                        (sh4r.r[Rm] * sh4r.r[Rn]); :}
nkeynes@359
   728
MULU.W Rm, Rn {:
nkeynes@359
   729
    sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
nkeynes@359
   730
               (uint32_t)((sh4r.r[Rm]&0xFFFF) * (sh4r.r[Rn]&0xFFFF));
nkeynes@359
   731
:}
nkeynes@359
   732
MULS.W Rm, Rn {:
nkeynes@359
   733
    sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
nkeynes@359
   734
               (uint32_t)(SIGNEXT32(sh4r.r[Rm]&0xFFFF) * SIGNEXT32(sh4r.r[Rn]&0xFFFF));
nkeynes@359
   735
:}
nkeynes@359
   736
NEGC Rm, Rn {:
nkeynes@359
   737
    tmp = 0 - sh4r.r[Rm];
nkeynes@359
   738
    sh4r.r[Rn] = tmp - sh4r.t;
nkeynes@359
   739
    sh4r.t = ( 0<tmp || tmp<sh4r.r[Rn] ? 1 : 0 );
nkeynes@359
   740
:}
nkeynes@359
   741
NEG Rm, Rn {: sh4r.r[Rn] = 0 - sh4r.r[Rm]; :}
nkeynes@359
   742
SUB Rm, Rn {: sh4r.r[Rn] -= sh4r.r[Rm]; :}
nkeynes@359
   743
SUBC Rm, Rn {: 
nkeynes@359
   744
    tmp = sh4r.r[Rn];
nkeynes@359
   745
    sh4r.r[Rn] = sh4r.r[Rn] - sh4r.r[Rm] - sh4r.t;
nkeynes@359
   746
    sh4r.t = (sh4r.r[Rn] > tmp || (sh4r.r[Rn] == tmp && sh4r.t == 1));
nkeynes@359
   747
:}
nkeynes@359
   748
nkeynes@359
   749
BRAF Rn {:
nkeynes@359
   750
     CHECKSLOTILLEGAL();
nkeynes@359
   751
     CHECKDEST( pc + 4 + sh4r.r[Rn] );
nkeynes@359
   752
     sh4r.in_delay_slot = 1;
nkeynes@359
   753
     sh4r.pc = sh4r.new_pc;
nkeynes@359
   754
     sh4r.new_pc = pc + 4 + sh4r.r[Rn];
nkeynes@359
   755
     return TRUE;
nkeynes@359
   756
:}
nkeynes@359
   757
BSRF Rn {:
nkeynes@359
   758
     CHECKSLOTILLEGAL();
nkeynes@359
   759
     CHECKDEST( pc + 4 + sh4r.r[Rn] );
nkeynes@359
   760
     sh4r.in_delay_slot = 1;
nkeynes@359
   761
     sh4r.pr = sh4r.pc + 4;
nkeynes@359
   762
     sh4r.pc = sh4r.new_pc;
nkeynes@359
   763
     sh4r.new_pc = pc + 4 + sh4r.r[Rn];
nkeynes@359
   764
     TRACE_CALL( pc, sh4r.new_pc );
nkeynes@359
   765
     return TRUE;
nkeynes@359
   766
:}
nkeynes@359
   767
BT disp {:
nkeynes@359
   768
    CHECKSLOTILLEGAL();
nkeynes@359
   769
    if( sh4r.t ) {
nkeynes@359
   770
        CHECKDEST( sh4r.pc + disp + 4 )
nkeynes@359
   771
        sh4r.pc += disp + 4;
nkeynes@359
   772
        sh4r.new_pc = sh4r.pc + 2;
nkeynes@359
   773
        return TRUE;
nkeynes@359
   774
    }
nkeynes@359
   775
:}
nkeynes@359
   776
BF disp {:
nkeynes@359
   777
    CHECKSLOTILLEGAL();
nkeynes@359
   778
    if( !sh4r.t ) {
nkeynes@359
   779
        CHECKDEST( sh4r.pc + disp + 4 )
nkeynes@359
   780
        sh4r.pc += disp + 4;
nkeynes@359
   781
        sh4r.new_pc = sh4r.pc + 2;
nkeynes@359
   782
        return TRUE;
nkeynes@359
   783
    }
nkeynes@359
   784
:}
nkeynes@359
   785
BT/S disp {:
nkeynes@359
   786
    CHECKSLOTILLEGAL();
nkeynes@359
   787
    if( sh4r.t ) {
nkeynes@359
   788
        CHECKDEST( sh4r.pc + disp + 4 )
nkeynes@359
   789
        sh4r.in_delay_slot = 1;
nkeynes@359
   790
        sh4r.pc = sh4r.new_pc;
nkeynes@359
   791
        sh4r.new_pc = pc + disp + 4;
nkeynes@359
   792
        sh4r.in_delay_slot = 1;
nkeynes@359
   793
        return TRUE;
nkeynes@359
   794
    }
nkeynes@359
   795
:}
nkeynes@359
   796
BF/S disp {:
nkeynes@359
   797
    CHECKSLOTILLEGAL();
nkeynes@359
   798
    if( !sh4r.t ) {
nkeynes@359
   799
        CHECKDEST( sh4r.pc + disp + 4 )
nkeynes@359
   800
        sh4r.in_delay_slot = 1;
nkeynes@359
   801
        sh4r.pc = sh4r.new_pc;
nkeynes@359
   802
        sh4r.new_pc = pc + disp + 4;
nkeynes@359
   803
        return TRUE;
nkeynes@359
   804
    }
nkeynes@359
   805
:}
nkeynes@359
   806
BRA disp {:
nkeynes@359
   807
    CHECKSLOTILLEGAL();
nkeynes@359
   808
    CHECKDEST( sh4r.pc + disp + 4 );
nkeynes@359
   809
    sh4r.in_delay_slot = 1;
nkeynes@359
   810
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   811
    sh4r.new_pc = pc + 4 + disp;
nkeynes@359
   812
    return TRUE;
nkeynes@359
   813
:}
nkeynes@359
   814
BSR disp {:
nkeynes@359
   815
    CHECKDEST( sh4r.pc + disp + 4 );
nkeynes@359
   816
    CHECKSLOTILLEGAL();
nkeynes@359
   817
    sh4r.in_delay_slot = 1;
nkeynes@359
   818
    sh4r.pr = pc + 4;
nkeynes@359
   819
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   820
    sh4r.new_pc = pc + 4 + disp;
nkeynes@359
   821
    TRACE_CALL( pc, sh4r.new_pc );
nkeynes@359
   822
    return TRUE;
nkeynes@359
   823
:}
nkeynes@359
   824
TRAPA #imm {:
nkeynes@359
   825
    CHECKSLOTILLEGAL();
nkeynes@359
   826
    sh4r.pc += 2;
nkeynes@586
   827
    sh4_raise_trap( imm );
nkeynes@586
   828
    return TRUE;
nkeynes@359
   829
:}
nkeynes@359
   830
RTS {: 
nkeynes@359
   831
    CHECKSLOTILLEGAL();
nkeynes@359
   832
    CHECKDEST( sh4r.pr );
nkeynes@359
   833
    sh4r.in_delay_slot = 1;
nkeynes@359
   834
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   835
    sh4r.new_pc = sh4r.pr;
nkeynes@359
   836
    TRACE_RETURN( pc, sh4r.new_pc );
nkeynes@359
   837
    return TRUE;
nkeynes@359
   838
:}
nkeynes@359
   839
SLEEP {:
nkeynes@359
   840
    if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
nkeynes@359
   841
	sh4r.sh4_state = SH4_STATE_STANDBY;
nkeynes@359
   842
    } else {
nkeynes@359
   843
	sh4r.sh4_state = SH4_STATE_SLEEP;
nkeynes@359
   844
    }
nkeynes@359
   845
    return FALSE; /* Halt CPU */
nkeynes@359
   846
:}
nkeynes@359
   847
RTE {:
nkeynes@359
   848
    CHECKPRIV();
nkeynes@359
   849
    CHECKDEST( sh4r.spc );
nkeynes@359
   850
    CHECKSLOTILLEGAL();
nkeynes@359
   851
    sh4r.in_delay_slot = 1;
nkeynes@359
   852
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   853
    sh4r.new_pc = sh4r.spc;
nkeynes@374
   854
    sh4_write_sr( sh4r.ssr );
nkeynes@359
   855
    return TRUE;
nkeynes@359
   856
:}
nkeynes@359
   857
JMP @Rn {:
nkeynes@359
   858
    CHECKDEST( sh4r.r[Rn] );
nkeynes@359
   859
    CHECKSLOTILLEGAL();
nkeynes@359
   860
    sh4r.in_delay_slot = 1;
nkeynes@359
   861
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   862
    sh4r.new_pc = sh4r.r[Rn];
nkeynes@359
   863
    return TRUE;
nkeynes@359
   864
:}
nkeynes@359
   865
JSR @Rn {:
nkeynes@359
   866
    CHECKDEST( sh4r.r[Rn] );
nkeynes@359
   867
    CHECKSLOTILLEGAL();
nkeynes@359
   868
    sh4r.in_delay_slot = 1;
nkeynes@359
   869
    sh4r.pc = sh4r.new_pc;
nkeynes@359
   870
    sh4r.new_pc = sh4r.r[Rn];
nkeynes@359
   871
    sh4r.pr = pc + 4;
nkeynes@359
   872
    TRACE_CALL( pc, sh4r.new_pc );
nkeynes@359
   873
    return TRUE;
nkeynes@359
   874
:}
nkeynes@359
   875
STS MACH, Rn {: sh4r.r[Rn] = (sh4r.mac>>32); :}
nkeynes@359
   876
STS.L MACH, @-Rn {:
nkeynes@587
   877
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   878
    MEM_WRITE_LONG( sh4r.r[Rn]-4, (sh4r.mac>>32) );
nkeynes@359
   879
    sh4r.r[Rn] -= 4;
nkeynes@359
   880
:}
nkeynes@359
   881
STC.L SR, @-Rn {:
nkeynes@359
   882
    CHECKPRIV();
nkeynes@587
   883
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   884
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4_read_sr() );
nkeynes@359
   885
    sh4r.r[Rn] -= 4;
nkeynes@359
   886
:}
nkeynes@359
   887
LDS.L @Rm+, MACH {:
nkeynes@359
   888
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   889
    MEM_READ_LONG(sh4r.r[Rm], tmp);
nkeynes@359
   890
    sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
nkeynes@586
   891
	(((uint64_t)tmp)<<32);
nkeynes@359
   892
    sh4r.r[Rm] += 4;
nkeynes@359
   893
:}
nkeynes@359
   894
LDC.L @Rm+, SR {:
nkeynes@359
   895
    CHECKSLOTILLEGAL();
nkeynes@359
   896
    CHECKPRIV();
nkeynes@359
   897
    CHECKWALIGN32( sh4r.r[Rm] );
nkeynes@586
   898
    MEM_READ_LONG(sh4r.r[Rm], tmp);
nkeynes@586
   899
    sh4_write_sr( tmp );
nkeynes@359
   900
    sh4r.r[Rm] +=4;
nkeynes@359
   901
:}
nkeynes@359
   902
LDS Rm, MACH {:
nkeynes@359
   903
    sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
nkeynes@359
   904
               (((uint64_t)sh4r.r[Rm])<<32);
nkeynes@359
   905
:}
nkeynes@359
   906
LDC Rm, SR {:
nkeynes@359
   907
    CHECKSLOTILLEGAL();
nkeynes@359
   908
    CHECKPRIV();
nkeynes@374
   909
    sh4_write_sr( sh4r.r[Rm] );
nkeynes@359
   910
:}
nkeynes@359
   911
LDC Rm, SGR {:
nkeynes@359
   912
    CHECKPRIV();
nkeynes@359
   913
    sh4r.sgr = sh4r.r[Rm];
nkeynes@359
   914
:}
nkeynes@359
   915
LDC.L @Rm+, SGR {:
nkeynes@359
   916
    CHECKPRIV();
nkeynes@359
   917
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   918
    MEM_READ_LONG(sh4r.r[Rm], sh4r.sgr);
nkeynes@359
   919
    sh4r.r[Rm] +=4;
nkeynes@359
   920
:}
nkeynes@359
   921
STS MACL, Rn {: sh4r.r[Rn] = (uint32_t)sh4r.mac; :}
nkeynes@359
   922
STS.L MACL, @-Rn {:
nkeynes@587
   923
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   924
    MEM_WRITE_LONG( sh4r.r[Rn]-4, (uint32_t)sh4r.mac );
nkeynes@359
   925
    sh4r.r[Rn] -= 4;
nkeynes@359
   926
:}
nkeynes@359
   927
STC.L GBR, @-Rn {:
nkeynes@587
   928
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   929
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.gbr );
nkeynes@359
   930
    sh4r.r[Rn] -= 4;
nkeynes@359
   931
:}
nkeynes@359
   932
LDS.L @Rm+, MACL {:
nkeynes@359
   933
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   934
    MEM_READ_LONG(sh4r.r[Rm], tmp);
nkeynes@359
   935
    sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
nkeynes@586
   936
               (uint64_t)((uint32_t)tmp);
nkeynes@359
   937
    sh4r.r[Rm] += 4;
nkeynes@359
   938
:}
nkeynes@359
   939
LDC.L @Rm+, GBR {:
nkeynes@359
   940
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   941
    MEM_READ_LONG(sh4r.r[Rm], sh4r.gbr);
nkeynes@359
   942
    sh4r.r[Rm] +=4;
nkeynes@359
   943
:}
nkeynes@359
   944
LDS Rm, MACL {:
nkeynes@359
   945
    sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
nkeynes@359
   946
               (uint64_t)((uint32_t)(sh4r.r[Rm]));
nkeynes@359
   947
:}
nkeynes@359
   948
LDC Rm, GBR {: sh4r.gbr = sh4r.r[Rm]; :}
nkeynes@359
   949
STS PR, Rn {: sh4r.r[Rn] = sh4r.pr; :}
nkeynes@359
   950
STS.L PR, @-Rn {:
nkeynes@587
   951
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   952
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.pr );
nkeynes@359
   953
    sh4r.r[Rn] -= 4;
nkeynes@359
   954
:}
nkeynes@359
   955
STC.L VBR, @-Rn {:
nkeynes@359
   956
    CHECKPRIV();
nkeynes@587
   957
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   958
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.vbr );
nkeynes@359
   959
    sh4r.r[Rn] -= 4;
nkeynes@359
   960
:}
nkeynes@359
   961
LDS.L @Rm+, PR {:
nkeynes@359
   962
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   963
    MEM_READ_LONG( sh4r.r[Rm], sh4r.pr );
nkeynes@359
   964
    sh4r.r[Rm] += 4;
nkeynes@359
   965
:}
nkeynes@359
   966
LDC.L @Rm+, VBR {:
nkeynes@359
   967
    CHECKPRIV();
nkeynes@359
   968
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   969
    MEM_READ_LONG(sh4r.r[Rm], sh4r.vbr);
nkeynes@359
   970
    sh4r.r[Rm] +=4;
nkeynes@359
   971
:}
nkeynes@359
   972
LDS Rm, PR {: sh4r.pr = sh4r.r[Rm]; :}
nkeynes@359
   973
LDC Rm, VBR {:
nkeynes@359
   974
    CHECKPRIV();
nkeynes@359
   975
    sh4r.vbr = sh4r.r[Rm];
nkeynes@359
   976
:}
nkeynes@359
   977
STC SGR, Rn {:
nkeynes@359
   978
    CHECKPRIV();
nkeynes@359
   979
    sh4r.r[Rn] = sh4r.sgr;
nkeynes@359
   980
:}
nkeynes@359
   981
STC.L SGR, @-Rn {:
nkeynes@359
   982
    CHECKPRIV();
nkeynes@587
   983
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   984
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.sgr );
nkeynes@359
   985
    sh4r.r[Rn] -= 4;
nkeynes@359
   986
:}
nkeynes@359
   987
STC.L SSR, @-Rn {:
nkeynes@359
   988
    CHECKPRIV();
nkeynes@587
   989
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
   990
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.ssr );
nkeynes@359
   991
    sh4r.r[Rn] -= 4;
nkeynes@359
   992
:}
nkeynes@359
   993
LDC.L @Rm+, SSR {:
nkeynes@359
   994
    CHECKPRIV();
nkeynes@359
   995
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
   996
    MEM_READ_LONG(sh4r.r[Rm], sh4r.ssr);
nkeynes@359
   997
    sh4r.r[Rm] +=4;
nkeynes@359
   998
:}
nkeynes@359
   999
LDC Rm, SSR {:
nkeynes@359
  1000
    CHECKPRIV();
nkeynes@359
  1001
    sh4r.ssr = sh4r.r[Rm];
nkeynes@359
  1002
:}
nkeynes@359
  1003
STC.L SPC, @-Rn {:
nkeynes@359
  1004
    CHECKPRIV();
nkeynes@587
  1005
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
  1006
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.spc );
nkeynes@359
  1007
    sh4r.r[Rn] -= 4;
nkeynes@359
  1008
:}
nkeynes@359
  1009
LDC.L @Rm+, SPC {:
nkeynes@359
  1010
    CHECKPRIV();
nkeynes@359
  1011
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
  1012
    MEM_READ_LONG(sh4r.r[Rm], sh4r.spc);
nkeynes@359
  1013
    sh4r.r[Rm] +=4;
nkeynes@359
  1014
:}
nkeynes@359
  1015
LDC Rm, SPC {:
nkeynes@359
  1016
    CHECKPRIV();
nkeynes@359
  1017
    sh4r.spc = sh4r.r[Rm];
nkeynes@359
  1018
:}
nkeynes@626
  1019
STS FPUL, Rn {: 
nkeynes@626
  1020
    CHECKFPUEN();
nkeynes@669
  1021
    sh4r.r[Rn] = FPULi; 
nkeynes@626
  1022
:}
nkeynes@359
  1023
STS.L FPUL, @-Rn {:
nkeynes@626
  1024
    CHECKFPUEN();
nkeynes@587
  1025
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@669
  1026
    MEM_WRITE_LONG( sh4r.r[Rn]-4, FPULi );
nkeynes@359
  1027
    sh4r.r[Rn] -= 4;
nkeynes@359
  1028
:}
nkeynes@359
  1029
LDS.L @Rm+, FPUL {:
nkeynes@626
  1030
    CHECKFPUEN();
nkeynes@359
  1031
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@669
  1032
    MEM_READ_LONG(sh4r.r[Rm], FPULi);
nkeynes@359
  1033
    sh4r.r[Rm] +=4;
nkeynes@359
  1034
:}
nkeynes@626
  1035
LDS Rm, FPUL {:
nkeynes@626
  1036
    CHECKFPUEN();
nkeynes@669
  1037
    FPULi = sh4r.r[Rm]; 
nkeynes@626
  1038
:}
nkeynes@626
  1039
STS FPSCR, Rn {: 
nkeynes@626
  1040
    CHECKFPUEN();
nkeynes@626
  1041
    sh4r.r[Rn] = sh4r.fpscr; 
nkeynes@626
  1042
:}
nkeynes@359
  1043
STS.L FPSCR, @-Rn {:
nkeynes@626
  1044
    CHECKFPUEN();
nkeynes@587
  1045
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
  1046
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.fpscr );
nkeynes@359
  1047
    sh4r.r[Rn] -= 4;
nkeynes@359
  1048
:}
nkeynes@359
  1049
LDS.L @Rm+, FPSCR {:
nkeynes@626
  1050
    CHECKFPUEN();
nkeynes@359
  1051
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@669
  1052
    MEM_READ_LONG(sh4r.r[Rm], tmp);
nkeynes@359
  1053
    sh4r.r[Rm] +=4;
nkeynes@669
  1054
    sh4_write_fpscr( tmp );
nkeynes@359
  1055
:}
nkeynes@374
  1056
LDS Rm, FPSCR {: 
nkeynes@626
  1057
    CHECKFPUEN();
nkeynes@669
  1058
    sh4_write_fpscr( sh4r.r[Rm] );
nkeynes@374
  1059
:}
nkeynes@359
  1060
STC DBR, Rn {: CHECKPRIV(); sh4r.r[Rn] = sh4r.dbr; :}
nkeynes@359
  1061
STC.L DBR, @-Rn {:
nkeynes@359
  1062
    CHECKPRIV();
nkeynes@587
  1063
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
  1064
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.dbr );
nkeynes@359
  1065
    sh4r.r[Rn] -= 4;
nkeynes@359
  1066
:}
nkeynes@359
  1067
LDC.L @Rm+, DBR {:
nkeynes@359
  1068
    CHECKPRIV();
nkeynes@359
  1069
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
  1070
    MEM_READ_LONG(sh4r.r[Rm], sh4r.dbr);
nkeynes@359
  1071
    sh4r.r[Rm] +=4;
nkeynes@359
  1072
:}
nkeynes@359
  1073
LDC Rm, DBR {:
nkeynes@359
  1074
    CHECKPRIV();
nkeynes@359
  1075
    sh4r.dbr = sh4r.r[Rm];
nkeynes@359
  1076
:}
nkeynes@359
  1077
STC.L Rm_BANK, @-Rn {:
nkeynes@359
  1078
    CHECKPRIV();
nkeynes@587
  1079
    CHECKWALIGN32( sh4r.r[Rn] );
nkeynes@587
  1080
    MEM_WRITE_LONG( sh4r.r[Rn]-4, sh4r.r_bank[Rm_BANK] );
nkeynes@359
  1081
    sh4r.r[Rn] -= 4;
nkeynes@359
  1082
:}
nkeynes@359
  1083
LDC.L @Rm+, Rn_BANK {:
nkeynes@359
  1084
    CHECKPRIV();
nkeynes@359
  1085
    CHECKRALIGN32( sh4r.r[Rm] );
nkeynes@586
  1086
    MEM_READ_LONG( sh4r.r[Rm], sh4r.r_bank[Rn_BANK] );
nkeynes@359
  1087
    sh4r.r[Rm] += 4;
nkeynes@359
  1088
:}
nkeynes@359
  1089
LDC Rm, Rn_BANK {:
nkeynes@359
  1090
    CHECKPRIV();
nkeynes@359
  1091
    sh4r.r_bank[Rn_BANK] = sh4r.r[Rm];
nkeynes@359
  1092
:}
nkeynes@359
  1093
STC SR, Rn {: 
nkeynes@359
  1094
    CHECKPRIV();
nkeynes@359
  1095
    sh4r.r[Rn] = sh4_read_sr();
nkeynes@359
  1096
:}
nkeynes@359
  1097
STC GBR, Rn {:
nkeynes@359
  1098
    sh4r.r[Rn] = sh4r.gbr;
nkeynes@359
  1099
:}
nkeynes@359
  1100
STC VBR, Rn {:
nkeynes@359
  1101
    CHECKPRIV();
nkeynes@359
  1102
    sh4r.r[Rn] = sh4r.vbr;
nkeynes@359
  1103
:}
nkeynes@359
  1104
STC SSR, Rn {:
nkeynes@359
  1105
    CHECKPRIV();
nkeynes@359
  1106
    sh4r.r[Rn] = sh4r.ssr;
nkeynes@359
  1107
:}
nkeynes@359
  1108
STC SPC, Rn {:
nkeynes@359
  1109
    CHECKPRIV();
nkeynes@359
  1110
    sh4r.r[Rn] = sh4r.spc;
nkeynes@359
  1111
:}
nkeynes@359
  1112
STC Rm_BANK, Rn {:
nkeynes@359
  1113
    CHECKPRIV();
nkeynes@359
  1114
    sh4r.r[Rn] = sh4r.r_bank[Rm_BANK];
nkeynes@359
  1115
:}
nkeynes@359
  1116
nkeynes@359
  1117
FADD FRm, FRn {:
nkeynes@359
  1118
    CHECKFPUEN();
nkeynes@359
  1119
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1120
	DR(FRn) += DR(FRm);
nkeynes@359
  1121
    } else {
nkeynes@359
  1122
	FR(FRn) += FR(FRm);
nkeynes@359
  1123
    }
nkeynes@359
  1124
:}
nkeynes@359
  1125
FSUB FRm, FRn {:
nkeynes@359
  1126
    CHECKFPUEN();
nkeynes@359
  1127
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1128
	DR(FRn) -= DR(FRm);
nkeynes@359
  1129
    } else {
nkeynes@359
  1130
	FR(FRn) -= FR(FRm);
nkeynes@359
  1131
    }
nkeynes@359
  1132
:}
nkeynes@359
  1133
nkeynes@359
  1134
FMUL FRm, FRn {:
nkeynes@359
  1135
    CHECKFPUEN();
nkeynes@359
  1136
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1137
	DR(FRn) *= DR(FRm);
nkeynes@359
  1138
    } else {
nkeynes@359
  1139
	FR(FRn) *= FR(FRm);
nkeynes@359
  1140
    }
nkeynes@359
  1141
:}
nkeynes@359
  1142
nkeynes@359
  1143
FDIV FRm, FRn {:
nkeynes@359
  1144
    CHECKFPUEN();
nkeynes@359
  1145
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1146
	DR(FRn) /= DR(FRm);
nkeynes@359
  1147
    } else {
nkeynes@359
  1148
	FR(FRn) /= FR(FRm);
nkeynes@359
  1149
    }
nkeynes@359
  1150
:}
nkeynes@359
  1151
nkeynes@359
  1152
FCMP/EQ FRm, FRn {:
nkeynes@359
  1153
    CHECKFPUEN();
nkeynes@359
  1154
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1155
	sh4r.t = ( DR(FRn) == DR(FRm) ? 1 : 0 );
nkeynes@359
  1156
    } else {
nkeynes@359
  1157
	sh4r.t = ( FR(FRn) == FR(FRm) ? 1 : 0 );
nkeynes@359
  1158
    }
nkeynes@359
  1159
:}
nkeynes@359
  1160
nkeynes@359
  1161
FCMP/GT FRm, FRn {:
nkeynes@359
  1162
    CHECKFPUEN();
nkeynes@359
  1163
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1164
	sh4r.t = ( DR(FRn) > DR(FRm) ? 1 : 0 );
nkeynes@359
  1165
    } else {
nkeynes@359
  1166
	sh4r.t = ( FR(FRn) > FR(FRm) ? 1 : 0 );
nkeynes@359
  1167
    }
nkeynes@359
  1168
:}
nkeynes@359
  1169
nkeynes@359
  1170
FSTS FPUL, FRn {: CHECKFPUEN(); FR(FRn) = FPULf; :}
nkeynes@359
  1171
FLDS FRm, FPUL {: CHECKFPUEN(); FPULf = FR(FRm); :}
nkeynes@359
  1172
FLOAT FPUL, FRn {: 
nkeynes@359
  1173
    CHECKFPUEN();
nkeynes@374
  1174
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@374
  1175
	if( FRn&1 ) { // No, really...
nkeynes@374
  1176
	    dtmp = (double)FPULi;
nkeynes@374
  1177
	    FR(FRn) = *(((float *)&dtmp)+1);
nkeynes@374
  1178
	} else {
nkeynes@374
  1179
	    DRF(FRn>>1) = (double)FPULi;
nkeynes@374
  1180
	}
nkeynes@374
  1181
    } else {
nkeynes@359
  1182
	FR(FRn) = (float)FPULi;
nkeynes@374
  1183
    }
nkeynes@359
  1184
:}
nkeynes@359
  1185
FTRC FRm, FPUL {:
nkeynes@359
  1186
    CHECKFPUEN();
nkeynes@359
  1187
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@374
  1188
	if( FRm&1 ) {
nkeynes@374
  1189
	    dtmp = 0;
nkeynes@374
  1190
	    *(((float *)&dtmp)+1) = FR(FRm);
nkeynes@374
  1191
	} else {
nkeynes@374
  1192
	    dtmp = DRF(FRm>>1);
nkeynes@374
  1193
	}
nkeynes@359
  1194
        if( dtmp >= MAX_INTF )
nkeynes@359
  1195
            FPULi = MAX_INT;
nkeynes@359
  1196
        else if( dtmp <= MIN_INTF )
nkeynes@359
  1197
            FPULi = MIN_INT;
nkeynes@359
  1198
        else 
nkeynes@359
  1199
            FPULi = (int32_t)dtmp;
nkeynes@359
  1200
    } else {
nkeynes@359
  1201
	ftmp = FR(FRm);
nkeynes@359
  1202
	if( ftmp >= MAX_INTF )
nkeynes@359
  1203
	    FPULi = MAX_INT;
nkeynes@359
  1204
	else if( ftmp <= MIN_INTF )
nkeynes@359
  1205
	    FPULi = MIN_INT;
nkeynes@359
  1206
	else
nkeynes@359
  1207
	    FPULi = (int32_t)ftmp;
nkeynes@359
  1208
    }
nkeynes@359
  1209
:}
nkeynes@359
  1210
FNEG FRn {:
nkeynes@359
  1211
    CHECKFPUEN();
nkeynes@359
  1212
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1213
	DR(FRn) = -DR(FRn);
nkeynes@359
  1214
    } else {
nkeynes@359
  1215
        FR(FRn) = -FR(FRn);
nkeynes@359
  1216
    }
nkeynes@359
  1217
:}
nkeynes@359
  1218
FABS FRn {:
nkeynes@359
  1219
    CHECKFPUEN();
nkeynes@359
  1220
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1221
	DR(FRn) = fabs(DR(FRn));
nkeynes@359
  1222
    } else {
nkeynes@359
  1223
        FR(FRn) = fabsf(FR(FRn));
nkeynes@359
  1224
    }
nkeynes@359
  1225
:}
nkeynes@359
  1226
FSQRT FRn {:
nkeynes@359
  1227
    CHECKFPUEN();
nkeynes@359
  1228
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1229
	DR(FRn) = sqrt(DR(FRn));
nkeynes@359
  1230
    } else {
nkeynes@359
  1231
        FR(FRn) = sqrtf(FR(FRn));
nkeynes@359
  1232
    }
nkeynes@359
  1233
:}
nkeynes@359
  1234
FLDI0 FRn {:
nkeynes@359
  1235
    CHECKFPUEN();
nkeynes@359
  1236
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1237
	DR(FRn) = 0.0;
nkeynes@359
  1238
    } else {
nkeynes@359
  1239
        FR(FRn) = 0.0;
nkeynes@359
  1240
    }
nkeynes@359
  1241
:}
nkeynes@359
  1242
FLDI1 FRn {:
nkeynes@359
  1243
    CHECKFPUEN();
nkeynes@359
  1244
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1245
	DR(FRn) = 1.0;
nkeynes@359
  1246
    } else {
nkeynes@359
  1247
        FR(FRn) = 1.0;
nkeynes@359
  1248
    }
nkeynes@359
  1249
:}
nkeynes@359
  1250
FMAC FR0, FRm, FRn {:
nkeynes@359
  1251
    CHECKFPUEN();
nkeynes@359
  1252
    if( IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1253
        DR(FRn) += DR(FRm)*DR(0);
nkeynes@359
  1254
    } else {
nkeynes@359
  1255
	FR(FRn) += FR(FRm)*FR(0);
nkeynes@359
  1256
    }
nkeynes@359
  1257
:}
nkeynes@374
  1258
FRCHG {: 
nkeynes@374
  1259
    CHECKFPUEN(); 
nkeynes@374
  1260
    sh4r.fpscr ^= FPSCR_FR; 
nkeynes@669
  1261
    sh4_switch_fr_banks();
nkeynes@374
  1262
:}
nkeynes@359
  1263
FSCHG {: CHECKFPUEN(); sh4r.fpscr ^= FPSCR_SZ; :}
nkeynes@359
  1264
FCNVSD FPUL, FRn {:
nkeynes@359
  1265
    CHECKFPUEN();
nkeynes@359
  1266
    if( IS_FPU_DOUBLEPREC() && !IS_FPU_DOUBLESIZE() ) {
nkeynes@359
  1267
	DR(FRn) = (double)FPULf;
nkeynes@359
  1268
    }
nkeynes@359
  1269
:}
nkeynes@359
  1270
FCNVDS FRm, FPUL {:
nkeynes@359
  1271
    CHECKFPUEN();
nkeynes@359
  1272
    if( IS_FPU_DOUBLEPREC() && !IS_FPU_DOUBLESIZE() ) {
nkeynes@359
  1273
	FPULf = (float)DR(FRm);
nkeynes@359
  1274
    }
nkeynes@359
  1275
:}
nkeynes@359
  1276
nkeynes@359
  1277
FSRRA FRn {:
nkeynes@359
  1278
    CHECKFPUEN();
nkeynes@359
  1279
    if( !IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1280
	FR(FRn) = 1.0/sqrtf(FR(FRn));
nkeynes@359
  1281
    }
nkeynes@359
  1282
:}
nkeynes@359
  1283
FIPR FVm, FVn {:
nkeynes@359
  1284
    CHECKFPUEN();
nkeynes@359
  1285
    if( !IS_FPU_DOUBLEPREC() ) {
nkeynes@359
  1286
        int tmp2 = FVn<<2;
nkeynes@359
  1287
        tmp = FVm<<2;
nkeynes@359
  1288
        FR(tmp2+3) = FR(tmp)*FR(tmp2) +
nkeynes@359
  1289
            FR(tmp+1)*FR(tmp2+1) +
nkeynes@359
  1290
            FR(tmp+2)*FR(tmp2+2) +
nkeynes@359
  1291
            FR(tmp+3)*FR(tmp2+3);
nkeynes@359
  1292
    }
nkeynes@359
  1293
:}
nkeynes@359
  1294
FSCA FPUL, FRn {:
nkeynes@359
  1295
    CHECKFPUEN();
nkeynes@359
  1296
    if( !IS_FPU_DOUBLEPREC() ) {
nkeynes@758
  1297
	sh4_fsca( FPULi, (float *)&(DRF(FRn>>1)) );
nkeynes@391
  1298
	/*
nkeynes@359
  1299
        float angle = (((float)(FPULi&0xFFFF))/65536.0) * 2 * M_PI;
nkeynes@359
  1300
        FR(FRn) = sinf(angle);
nkeynes@359
  1301
        FR((FRn)+1) = cosf(angle);
nkeynes@391
  1302
	*/
nkeynes@359
  1303
    }
nkeynes@359
  1304
:}
nkeynes@359
  1305
FTRV XMTRX, FVn {:
nkeynes@359
  1306
    CHECKFPUEN();
nkeynes@359
  1307
    if( !IS_FPU_DOUBLEPREC() ) {
nkeynes@758
  1308
	sh4_ftrv((float *)&(DRF(FVn<<1)) );
nkeynes@359
  1309
    }
nkeynes@359
  1310
:}
nkeynes@359
  1311
UNDEF {:
nkeynes@359
  1312
    UNDEF(ir);
nkeynes@359
  1313
:}
nkeynes@359
  1314
%%
nkeynes@359
  1315
    sh4r.pc = sh4r.new_pc;
nkeynes@359
  1316
    sh4r.new_pc += 2;
nkeynes@927
  1317
nkeynes@927
  1318
except:
nkeynes@359
  1319
    sh4r.in_delay_slot = 0;
nkeynes@359
  1320
    return TRUE;
nkeynes@359
  1321
}
.