Search
lxdream.org :: lxdream/src/aica/armcore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/armcore.c
changeset 81:1c1d53584da4
prev80:1d59b19eb505
next86:f151e63f9754
author nkeynes
date Tue Jan 17 12:53:39 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix LDM/STM instructions (yes, again, but for sure this time)
Add debug check - if PC leaves ram halt immediately
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@81
     2
 * $Id: armcore.c,v 1.19 2006-01-17 12:53:39 nkeynes Exp $
nkeynes@30
     3
 * 
nkeynes@30
     4
 * ARM7TDMI CPU emulation core.
nkeynes@30
     5
 *
nkeynes@30
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
     7
 *
nkeynes@30
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    11
 * (at your option) any later version.
nkeynes@30
    12
 *
nkeynes@30
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    16
 * GNU General Public License for more details.
nkeynes@30
    17
 */
nkeynes@2
    18
nkeynes@37
    19
#define MODULE aica_module
nkeynes@37
    20
#include "dream.h"
nkeynes@73
    21
#include "mem.h"
nkeynes@7
    22
#include "aica/armcore.h"
nkeynes@73
    23
#include "aica/aica.h"
nkeynes@2
    24
nkeynes@51
    25
#define STM_R15_OFFSET 12
nkeynes@51
    26
nkeynes@2
    27
struct arm_registers armr;
nkeynes@2
    28
nkeynes@35
    29
void arm_set_mode( int mode );
nkeynes@35
    30
nkeynes@35
    31
uint32_t arm_exceptions[][2] = {{ MODE_SVC, 0x00000000 },
nkeynes@35
    32
				{ MODE_UND, 0x00000004 },
nkeynes@35
    33
				{ MODE_SVC, 0x00000008 },
nkeynes@35
    34
				{ MODE_ABT, 0x0000000C },
nkeynes@35
    35
				{ MODE_ABT, 0x00000010 },
nkeynes@35
    36
				{ MODE_IRQ, 0x00000018 },
nkeynes@35
    37
				{ MODE_FIQ, 0x0000001C } };
nkeynes@35
    38
nkeynes@35
    39
#define EXC_RESET 0
nkeynes@35
    40
#define EXC_UNDEFINED 1
nkeynes@35
    41
#define EXC_SOFTWARE 2
nkeynes@35
    42
#define EXC_PREFETCH_ABORT 3
nkeynes@35
    43
#define EXC_DATA_ABORT 4
nkeynes@35
    44
#define EXC_IRQ 5
nkeynes@35
    45
#define EXC_FAST_IRQ 6
nkeynes@35
    46
nkeynes@35
    47
uint32_t arm_cpu_freq = ARM_BASE_RATE;
nkeynes@35
    48
uint32_t arm_cpu_period = 1000 / ARM_BASE_RATE;
nkeynes@35
    49
nkeynes@73
    50
#define CYCLES_PER_SAMPLE ((ARM_BASE_RATE * 1000000) / AICA_SAMPLE_RATE)
nkeynes@43
    51
nkeynes@43
    52
static struct breakpoint_struct arm_breakpoints[MAX_BREAKPOINTS];
nkeynes@43
    53
static int arm_breakpoint_count = 0;
nkeynes@43
    54
nkeynes@43
    55
void arm_set_breakpoint( uint32_t pc, int type )
nkeynes@43
    56
{
nkeynes@43
    57
    arm_breakpoints[arm_breakpoint_count].address = pc;
nkeynes@43
    58
    arm_breakpoints[arm_breakpoint_count].type = type;
nkeynes@43
    59
    arm_breakpoint_count++;
nkeynes@43
    60
}
nkeynes@43
    61
nkeynes@43
    62
gboolean arm_clear_breakpoint( uint32_t pc, int type )
nkeynes@43
    63
{
nkeynes@43
    64
    int i;
nkeynes@43
    65
nkeynes@43
    66
    for( i=0; i<arm_breakpoint_count; i++ ) {
nkeynes@43
    67
	if( arm_breakpoints[i].address == pc && 
nkeynes@43
    68
	    arm_breakpoints[i].type == type ) {
nkeynes@43
    69
	    while( ++i < arm_breakpoint_count ) {
nkeynes@43
    70
		arm_breakpoints[i-1].address = arm_breakpoints[i].address;
nkeynes@43
    71
		arm_breakpoints[i-1].type = arm_breakpoints[i].type;
nkeynes@43
    72
	    }
nkeynes@43
    73
	    arm_breakpoint_count--;
nkeynes@43
    74
	    return TRUE;
nkeynes@43
    75
	}
nkeynes@43
    76
    }
nkeynes@43
    77
    return FALSE;
nkeynes@43
    78
}
nkeynes@43
    79
nkeynes@43
    80
int arm_get_breakpoint( uint32_t pc )
nkeynes@43
    81
{
nkeynes@43
    82
    int i;
nkeynes@43
    83
    for( i=0; i<arm_breakpoint_count; i++ ) {
nkeynes@43
    84
	if( arm_breakpoints[i].address == pc )
nkeynes@43
    85
	    return arm_breakpoints[i].type;
nkeynes@43
    86
    }
nkeynes@43
    87
    return 0;
nkeynes@43
    88
}
nkeynes@43
    89
nkeynes@73
    90
uint32_t arm_run_slice( uint32_t num_samples )
nkeynes@35
    91
{
nkeynes@73
    92
    int i,j,k;
nkeynes@73
    93
    for( i=0; i<num_samples; i++ ) {
nkeynes@73
    94
	for( j=0; j < CYCLES_PER_SAMPLE; j++ ) {
nkeynes@73
    95
	    armr.icount++;
nkeynes@73
    96
	    if( !arm_execute_instruction() )
nkeynes@73
    97
		return i;
nkeynes@73
    98
#ifdef ENABLE_DEBUG_MODE
nkeynes@73
    99
	    for( k=0; k<arm_breakpoint_count; k++ ) {
nkeynes@73
   100
		if( arm_breakpoints[k].address == armr.r[15] ) {
nkeynes@73
   101
		    dreamcast_stop();
nkeynes@73
   102
		    if( arm_breakpoints[k].type == BREAK_ONESHOT )
nkeynes@73
   103
			arm_clear_breakpoint( armr.r[15], BREAK_ONESHOT );
nkeynes@73
   104
		    return i;
nkeynes@73
   105
		}
nkeynes@73
   106
	    }
nkeynes@73
   107
#endif	
nkeynes@73
   108
	}
nkeynes@80
   109
	
nkeynes@80
   110
	k = MMIO_READ( AICA2, AICA_TCR );
nkeynes@80
   111
	if( k & 0x40 ) {
nkeynes@73
   112
	    uint8_t val = MMIO_READ( AICA2, AICA_TIMER );
nkeynes@73
   113
	    val++;
nkeynes@80
   114
	    if( val == 0 ) {
nkeynes@73
   115
		aica_event( AICA_EVENT_TIMER );
nkeynes@80
   116
		MMIO_WRITE( AICA2, AICA_TCR, k & ~0x40 );
nkeynes@80
   117
	    }
nkeynes@73
   118
	    MMIO_WRITE( AICA2, AICA_TIMER, val );
nkeynes@73
   119
	}
nkeynes@73
   120
	if( !dreamcast_is_running() )
nkeynes@35
   121
	    break;
nkeynes@35
   122
    }
nkeynes@35
   123
nkeynes@73
   124
    return i;
nkeynes@35
   125
}
nkeynes@35
   126
nkeynes@35
   127
void arm_save_state( FILE *f )
nkeynes@35
   128
{
nkeynes@35
   129
    fwrite( &armr, sizeof(armr), 1, f );
nkeynes@35
   130
}
nkeynes@35
   131
nkeynes@35
   132
int arm_load_state( FILE *f )
nkeynes@35
   133
{
nkeynes@35
   134
    fread( &armr, sizeof(armr), 1, f );
nkeynes@35
   135
    return 0;
nkeynes@35
   136
}
nkeynes@35
   137
nkeynes@35
   138
/* Exceptions */
nkeynes@35
   139
void arm_reset( void )
nkeynes@35
   140
{
nkeynes@35
   141
    /* Wipe all processor state */
nkeynes@35
   142
    memset( &armr, 0, sizeof(armr) );
nkeynes@35
   143
nkeynes@35
   144
    armr.cpsr = MODE_SVC | CPSR_I | CPSR_F;
nkeynes@35
   145
    armr.r[15] = 0x00000000;
nkeynes@35
   146
}
nkeynes@35
   147
nkeynes@37
   148
#define SET_CPSR_CONTROL   0x00010000
nkeynes@37
   149
#define SET_CPSR_EXTENSION 0x00020000
nkeynes@37
   150
#define SET_CPSR_STATUS    0x00040000
nkeynes@37
   151
#define SET_CPSR_FLAGS     0x00080000
nkeynes@37
   152
nkeynes@37
   153
uint32_t arm_get_cpsr( void )
nkeynes@37
   154
{
nkeynes@37
   155
    /* write back all flags to the cpsr */
nkeynes@37
   156
    armr.cpsr = armr.cpsr & CPSR_COMPACT_MASK;
nkeynes@37
   157
    if( armr.n ) armr.cpsr |= CPSR_N;
nkeynes@37
   158
    if( armr.z ) armr.cpsr |= CPSR_Z;
nkeynes@37
   159
    if( armr.c ) armr.cpsr |= CPSR_C;
nkeynes@37
   160
    if( armr.v ) armr.cpsr |= CPSR_V;
nkeynes@37
   161
    if( armr.t ) armr.cpsr |= CPSR_T;  
nkeynes@37
   162
    return armr.cpsr;
nkeynes@37
   163
}
nkeynes@37
   164
nkeynes@37
   165
/**
nkeynes@66
   166
 * Return a pointer to the specified register in the user bank,
nkeynes@66
   167
 * regardless of the active bank
nkeynes@66
   168
 */
nkeynes@66
   169
static uint32_t *arm_user_reg( int reg )
nkeynes@66
   170
{
nkeynes@66
   171
    if( IS_EXCEPTION_MODE() ) {
nkeynes@66
   172
	if( reg == 13 || reg == 14 )
nkeynes@66
   173
	    return &armr.user_r[reg-8];
nkeynes@66
   174
	if( IS_FIQ_MODE() ) {
nkeynes@66
   175
	    if( reg >= 8 || reg <= 12 )
nkeynes@66
   176
		return &armr.user_r[reg-8];
nkeynes@66
   177
	}
nkeynes@66
   178
    }
nkeynes@66
   179
    return &armr.r[reg];
nkeynes@66
   180
}
nkeynes@66
   181
nkeynes@66
   182
#define USER_R(n) *arm_user_reg(n)
nkeynes@66
   183
nkeynes@66
   184
/**
nkeynes@37
   185
 * Set the CPSR to the specified value.
nkeynes@37
   186
 *
nkeynes@37
   187
 * @param value values to set in CPSR
nkeynes@37
   188
 * @param fields set of mask values to define which sections of the 
nkeynes@37
   189
 *   CPSR to set (one of the SET_CPSR_* values above)
nkeynes@37
   190
 */
nkeynes@37
   191
void arm_set_cpsr( uint32_t value, uint32_t fields )
nkeynes@37
   192
{
nkeynes@37
   193
    if( IS_PRIVILEGED_MODE() ) {
nkeynes@37
   194
	if( fields & SET_CPSR_CONTROL ) {
nkeynes@37
   195
	    int mode = value & CPSR_MODE;
nkeynes@37
   196
	    arm_set_mode( mode );
nkeynes@37
   197
	    armr.t = ( value & CPSR_T ); /* Technically illegal to change */
nkeynes@37
   198
	    armr.cpsr = (armr.cpsr & 0xFFFFFF00) | (value & 0x000000FF);
nkeynes@37
   199
	}
nkeynes@37
   200
nkeynes@37
   201
	/* Middle 16 bits not currently defined */
nkeynes@37
   202
    }
nkeynes@37
   203
    if( fields & SET_CPSR_FLAGS ) {
nkeynes@37
   204
	/* Break flags directly out of given value - don't bother writing
nkeynes@37
   205
	 * back to CPSR 
nkeynes@37
   206
	 */
nkeynes@37
   207
	armr.n = ( value & CPSR_N );
nkeynes@37
   208
	armr.z = ( value & CPSR_Z );
nkeynes@37
   209
	armr.c = ( value & CPSR_C );
nkeynes@37
   210
	armr.v = ( value & CPSR_V );
nkeynes@37
   211
    }
nkeynes@37
   212
}
nkeynes@37
   213
nkeynes@37
   214
void arm_set_spsr( uint32_t value, uint32_t fields )
nkeynes@37
   215
{
nkeynes@37
   216
    /* Only defined if we actually have an SPSR register */
nkeynes@37
   217
    if( IS_EXCEPTION_MODE() ) {
nkeynes@37
   218
	if( fields & SET_CPSR_CONTROL ) {
nkeynes@37
   219
	    armr.spsr = (armr.spsr & 0xFFFFFF00) | (value & 0x000000FF);
nkeynes@37
   220
	}
nkeynes@37
   221
nkeynes@37
   222
	/* Middle 16 bits not currently defined */
nkeynes@37
   223
nkeynes@37
   224
	if( fields & SET_CPSR_FLAGS ) {
nkeynes@37
   225
	    armr.spsr = (armr.spsr & 0x00FFFFFF) | (value & 0xFF000000);
nkeynes@37
   226
	}
nkeynes@37
   227
    }
nkeynes@37
   228
}
nkeynes@37
   229
nkeynes@35
   230
/**
nkeynes@35
   231
 * Raise an ARM exception (other than reset, which uses arm_reset().
nkeynes@35
   232
 * @param exception one of the EXC_* exception codes defined above.
nkeynes@35
   233
 */
nkeynes@35
   234
void arm_raise_exception( int exception )
nkeynes@35
   235
{
nkeynes@35
   236
    int mode = arm_exceptions[exception][0];
nkeynes@37
   237
    uint32_t spsr = arm_get_cpsr();
nkeynes@35
   238
    arm_set_mode( mode );
nkeynes@37
   239
    armr.spsr = spsr;
nkeynes@63
   240
    armr.r[14] = armr.r[15] + 4;
nkeynes@59
   241
    armr.cpsr = (spsr & 0xFFFFFF00) | mode | CPSR_I; 
nkeynes@35
   242
    if( mode == MODE_FIQ )
nkeynes@35
   243
	armr.cpsr |= CPSR_F;
nkeynes@35
   244
    armr.r[15] = arm_exceptions[exception][1];
nkeynes@35
   245
}
nkeynes@35
   246
nkeynes@37
   247
void arm_restore_cpsr( void )
nkeynes@35
   248
{
nkeynes@35
   249
    int spsr = armr.spsr;
nkeynes@35
   250
    int mode = spsr & CPSR_MODE;
nkeynes@35
   251
    arm_set_mode( mode );
nkeynes@35
   252
    armr.cpsr = spsr;
nkeynes@37
   253
    armr.n = ( spsr & CPSR_N );
nkeynes@37
   254
    armr.z = ( spsr & CPSR_Z );
nkeynes@37
   255
    armr.c = ( spsr & CPSR_C );
nkeynes@37
   256
    armr.v = ( spsr & CPSR_V );
nkeynes@37
   257
    armr.t = ( spsr & CPSR_T );
nkeynes@35
   258
}
nkeynes@35
   259
nkeynes@35
   260
nkeynes@35
   261
nkeynes@35
   262
/**
nkeynes@35
   263
 * Change the current executing ARM mode to the requested mode.
nkeynes@35
   264
 * Saves any required registers to banks and restores those for the
nkeynes@35
   265
 * correct mode. (Note does not actually update CPSR at the moment).
nkeynes@35
   266
 */
nkeynes@35
   267
void arm_set_mode( int targetMode )
nkeynes@35
   268
{
nkeynes@35
   269
    int currentMode = armr.cpsr & CPSR_MODE;
nkeynes@35
   270
    if( currentMode == targetMode )
nkeynes@35
   271
	return;
nkeynes@35
   272
nkeynes@35
   273
    switch( currentMode ) {
nkeynes@35
   274
    case MODE_USER:
nkeynes@35
   275
    case MODE_SYS:
nkeynes@35
   276
	armr.user_r[5] = armr.r[13];
nkeynes@35
   277
	armr.user_r[6] = armr.r[14];
nkeynes@35
   278
	break;
nkeynes@35
   279
    case MODE_SVC:
nkeynes@35
   280
	armr.svc_r[0] = armr.r[13];
nkeynes@35
   281
	armr.svc_r[1] = armr.r[14];
nkeynes@35
   282
	armr.svc_r[2] = armr.spsr;
nkeynes@35
   283
	break;
nkeynes@35
   284
    case MODE_ABT:
nkeynes@35
   285
	armr.abt_r[0] = armr.r[13];
nkeynes@35
   286
	armr.abt_r[1] = armr.r[14];
nkeynes@35
   287
	armr.abt_r[2] = armr.spsr;
nkeynes@35
   288
	break;
nkeynes@35
   289
    case MODE_UND:
nkeynes@35
   290
	armr.und_r[0] = armr.r[13];
nkeynes@35
   291
	armr.und_r[1] = armr.r[14];
nkeynes@35
   292
	armr.und_r[2] = armr.spsr;
nkeynes@35
   293
	break;
nkeynes@35
   294
    case MODE_IRQ:
nkeynes@35
   295
	armr.irq_r[0] = armr.r[13];
nkeynes@35
   296
	armr.irq_r[1] = armr.r[14];
nkeynes@35
   297
	armr.irq_r[2] = armr.spsr;
nkeynes@35
   298
	break;
nkeynes@35
   299
    case MODE_FIQ:
nkeynes@35
   300
	armr.fiq_r[0] = armr.r[8];
nkeynes@35
   301
	armr.fiq_r[1] = armr.r[9];
nkeynes@35
   302
	armr.fiq_r[2] = armr.r[10];
nkeynes@35
   303
	armr.fiq_r[3] = armr.r[11];
nkeynes@35
   304
	armr.fiq_r[4] = armr.r[12];
nkeynes@35
   305
	armr.fiq_r[5] = armr.r[13];
nkeynes@35
   306
	armr.fiq_r[6] = armr.r[14];
nkeynes@35
   307
	armr.fiq_r[7] = armr.spsr;
nkeynes@35
   308
	armr.r[8] = armr.user_r[0];
nkeynes@35
   309
	armr.r[9] = armr.user_r[1];
nkeynes@35
   310
	armr.r[10] = armr.user_r[2];
nkeynes@35
   311
	armr.r[11] = armr.user_r[3];
nkeynes@35
   312
	armr.r[12] = armr.user_r[4];
nkeynes@35
   313
	break;
nkeynes@35
   314
    }
nkeynes@35
   315
    
nkeynes@35
   316
    switch( targetMode ) {
nkeynes@35
   317
    case MODE_USER:
nkeynes@35
   318
    case MODE_SYS:
nkeynes@35
   319
	armr.r[13] = armr.user_r[5];
nkeynes@35
   320
	armr.r[14] = armr.user_r[6];
nkeynes@35
   321
	break;
nkeynes@35
   322
    case MODE_SVC:
nkeynes@35
   323
	armr.r[13] = armr.svc_r[0];
nkeynes@35
   324
	armr.r[14] = armr.svc_r[1];
nkeynes@35
   325
	armr.spsr = armr.svc_r[2];
nkeynes@35
   326
	break;
nkeynes@35
   327
    case MODE_ABT:
nkeynes@35
   328
	armr.r[13] = armr.abt_r[0];
nkeynes@35
   329
	armr.r[14] = armr.abt_r[1];
nkeynes@35
   330
	armr.spsr = armr.abt_r[2];
nkeynes@35
   331
	break;
nkeynes@35
   332
    case MODE_UND:
nkeynes@35
   333
	armr.r[13] = armr.und_r[0];
nkeynes@35
   334
	armr.r[14] = armr.und_r[1];
nkeynes@35
   335
	armr.spsr = armr.und_r[2];
nkeynes@35
   336
	break;
nkeynes@35
   337
    case MODE_IRQ:
nkeynes@35
   338
	armr.r[13] = armr.irq_r[0];
nkeynes@35
   339
	armr.r[14] = armr.irq_r[1];
nkeynes@35
   340
	armr.spsr = armr.irq_r[2];
nkeynes@35
   341
	break;
nkeynes@35
   342
    case MODE_FIQ:
nkeynes@35
   343
	armr.user_r[0] = armr.r[8];
nkeynes@35
   344
	armr.user_r[1] = armr.r[9];
nkeynes@35
   345
	armr.user_r[2] = armr.r[10];
nkeynes@35
   346
	armr.user_r[3] = armr.r[11];
nkeynes@35
   347
	armr.user_r[4] = armr.r[12];
nkeynes@35
   348
	armr.r[8] = armr.fiq_r[0];
nkeynes@35
   349
	armr.r[9] = armr.fiq_r[1];
nkeynes@35
   350
	armr.r[10] = armr.fiq_r[2];
nkeynes@35
   351
	armr.r[11] = armr.fiq_r[3];
nkeynes@35
   352
	armr.r[12] = armr.fiq_r[4];
nkeynes@35
   353
	armr.r[13] = armr.fiq_r[5];
nkeynes@35
   354
	armr.r[14] = armr.fiq_r[6];
nkeynes@35
   355
	armr.spsr = armr.fiq_r[7];
nkeynes@35
   356
	break;
nkeynes@35
   357
    }
nkeynes@35
   358
}
nkeynes@35
   359
nkeynes@5
   360
/* Page references are as per ARM DDI 0100E (June 2000) */
nkeynes@2
   361
nkeynes@11
   362
#define MEM_READ_BYTE( addr ) arm_read_byte(addr)
nkeynes@11
   363
#define MEM_READ_WORD( addr ) arm_read_word(addr)
nkeynes@11
   364
#define MEM_READ_LONG( addr ) arm_read_long(addr)
nkeynes@11
   365
#define MEM_WRITE_BYTE( addr, val ) arm_write_byte(addr, val)
nkeynes@11
   366
#define MEM_WRITE_WORD( addr, val ) arm_write_word(addr, val)
nkeynes@11
   367
#define MEM_WRITE_LONG( addr, val ) arm_write_long(addr, val)
nkeynes@2
   368
nkeynes@5
   369
nkeynes@5
   370
#define IS_NOTBORROW( result, op1, op2 ) (op2 > op1 ? 0 : 1)
nkeynes@5
   371
#define IS_CARRY( result, op1, op2 ) (result < op1 ? 1 : 0)
nkeynes@5
   372
#define IS_SUBOVERFLOW( result, op1, op2 ) (((op1^op2) & (result^op1)) >> 31)
nkeynes@5
   373
#define IS_ADDOVERFLOW( result, op1, op2 ) (((op1&op2) & (result^op1)) >> 31)
nkeynes@5
   374
nkeynes@7
   375
#define PC armr.r[15]
nkeynes@2
   376
nkeynes@5
   377
/* Instruction fields */
nkeynes@5
   378
#define COND(ir) (ir>>28)
nkeynes@5
   379
#define GRP(ir) ((ir>>26)&0x03)
nkeynes@5
   380
#define OPCODE(ir) ((ir>>20)&0x1F)
nkeynes@5
   381
#define IFLAG(ir) (ir&0x02000000)
nkeynes@5
   382
#define SFLAG(ir) (ir&0x00100000)
nkeynes@5
   383
#define PFLAG(ir) (ir&0x01000000)
nkeynes@5
   384
#define UFLAG(ir) (ir&0x00800000)
nkeynes@5
   385
#define BFLAG(ir) (ir&0x00400000)
nkeynes@46
   386
#define WFLAG(ir) (ir&0x00200000)
nkeynes@5
   387
#define LFLAG(ir) SFLAG(ir)
nkeynes@5
   388
#define RN(ir) (armr.r[((ir>>16)&0x0F)] + (((ir>>16)&0x0F) == 0x0F ? 4 : 0))
nkeynes@37
   389
#define RD(ir) (armr.r[((ir>>12)&0x0F)] + (((ir>>12)&0x0F) == 0x0F ? 4 : 0))
nkeynes@5
   390
#define RDn(ir) ((ir>>12)&0x0F)
nkeynes@37
   391
#define RS(ir) (armr.r[((ir>>8)&0x0F)] + (((ir>>8)&0x0F) == 0x0F ? 4 : 0))
nkeynes@37
   392
#define RM(ir) (armr.r[(ir&0x0F)] + (((ir&0x0F) == 0x0F ? 4 : 0)) )
nkeynes@5
   393
#define LRN(ir) armr.r[((ir>>16)&0x0F)]
nkeynes@5
   394
#define LRD(ir) armr.r[((ir>>12)&0x0F)]
nkeynes@5
   395
#define LRS(ir) armr.r[((ir>>8)&0x0F)]
nkeynes@5
   396
#define LRM(ir) armr.r[(ir&0x0F)]
nkeynes@5
   397
nkeynes@5
   398
#define IMM8(ir) (ir&0xFF)
nkeynes@5
   399
#define IMM12(ir) (ir&0xFFF)
nkeynes@7
   400
#define SHIFTIMM(ir) ((ir>>7)&0x1F)
nkeynes@7
   401
#define IMMROT(ir) ((ir>>7)&0x1E)
nkeynes@37
   402
#define ROTIMM12(ir) ROTATE_RIGHT_LONG(IMM8(ir),IMMROT(ir))
nkeynes@37
   403
#define SIGNEXT24(n) ((n&0x00800000) ? (n|0xFF000000) : (n&0x00FFFFFF))
nkeynes@5
   404
#define SHIFT(ir) ((ir>>4)&0x07)
nkeynes@5
   405
#define DISP24(ir) ((ir&0x00FFFFFF))
nkeynes@37
   406
#define UNDEF(ir) do{ arm_raise_exception( EXC_UNDEFINED ); return TRUE; } while(0)
nkeynes@46
   407
#define UNIMP(ir) do{ PC-=4; ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", PC, ir ); dreamcast_stop(); return FALSE; }while(0)
nkeynes@7
   408
nkeynes@37
   409
/**
nkeynes@37
   410
 * Determine the value of the shift-operand for a data processing instruction,
nkeynes@37
   411
 * without determing a value for shift_C (optimized form for instructions that
nkeynes@37
   412
 * don't require shift_C ).
nkeynes@37
   413
 * @see s5.1 Addressing Mode 1 - Data-processing operands (p A5-2, 218)
nkeynes@37
   414
 */
nkeynes@5
   415
static uint32_t arm_get_shift_operand( uint32_t ir )
nkeynes@5
   416
{
nkeynes@5
   417
	uint32_t operand, tmp;
nkeynes@5
   418
	if( IFLAG(ir) == 0 ) {
nkeynes@5
   419
		operand = RM(ir);
nkeynes@5
   420
		switch(SHIFT(ir)) {
nkeynes@5
   421
		case 0: /* (Rm << imm) */
nkeynes@5
   422
			operand = operand << SHIFTIMM(ir);
nkeynes@5
   423
			break;
nkeynes@5
   424
		case 1: /* (Rm << Rs) */
nkeynes@5
   425
			tmp = RS(ir)&0xFF;
nkeynes@5
   426
			if( tmp > 31 ) operand = 0;
nkeynes@5
   427
			else operand = operand << tmp;
nkeynes@5
   428
			break;
nkeynes@5
   429
		case 2: /* (Rm >> imm) */
nkeynes@5
   430
			operand = operand >> SHIFTIMM(ir);
nkeynes@5
   431
			break;
nkeynes@5
   432
		case 3: /* (Rm >> Rs) */
nkeynes@5
   433
			tmp = RS(ir) & 0xFF;
nkeynes@5
   434
			if( tmp > 31 ) operand = 0;
nkeynes@5
   435
			else operand = operand >> ir;
nkeynes@5
   436
			break;
nkeynes@5
   437
		case 4: /* (Rm >>> imm) */
nkeynes@5
   438
			tmp = SHIFTIMM(ir);
nkeynes@5
   439
			if( tmp == 0 ) operand = ((int32_t)operand) >> 31;
nkeynes@5
   440
			else operand = ((int32_t)operand) >> tmp;
nkeynes@5
   441
			break;
nkeynes@5
   442
		case 5: /* (Rm >>> Rs) */
nkeynes@5
   443
			tmp = RS(ir) & 0xFF;
nkeynes@5
   444
			if( tmp > 31 ) operand = ((int32_t)operand) >> 31;
nkeynes@5
   445
			else operand = ((int32_t)operand) >> tmp;
nkeynes@5
   446
			break;
nkeynes@5
   447
		case 6:
nkeynes@5
   448
			tmp = SHIFTIMM(ir);
nkeynes@5
   449
			if( tmp == 0 ) /* RRX aka rotate with carry */
nkeynes@7
   450
				operand = (operand >> 1) | (armr.c<<31);
nkeynes@5
   451
			else
nkeynes@5
   452
				operand = ROTATE_RIGHT_LONG(operand,tmp);
nkeynes@5
   453
			break;
nkeynes@5
   454
		case 7:
nkeynes@5
   455
			tmp = RS(ir)&0x1F;
nkeynes@5
   456
			operand = ROTATE_RIGHT_LONG(operand,tmp);
nkeynes@5
   457
			break;
nkeynes@5
   458
		}
nkeynes@5
   459
	} else {
nkeynes@5
   460
		operand = IMM8(ir);
nkeynes@5
   461
		tmp = IMMROT(ir);
nkeynes@5
   462
		operand = ROTATE_RIGHT_LONG(operand, tmp);
nkeynes@5
   463
	}
nkeynes@5
   464
	return operand;
nkeynes@5
   465
}
nkeynes@5
   466
nkeynes@5
   467
/**
nkeynes@37
   468
 * Determine the value of the shift-operand for a data processing instruction,
nkeynes@37
   469
 * and set armr.shift_c accordingly.
nkeynes@37
   470
 * @see s5.1 Addressing Mode 1 - Data-processing operands (p A5-2, 218)
nkeynes@5
   471
 */
nkeynes@5
   472
static uint32_t arm_get_shift_operand_s( uint32_t ir )
nkeynes@5
   473
{
nkeynes@5
   474
	uint32_t operand, tmp;
nkeynes@5
   475
	if( IFLAG(ir) == 0 ) {
nkeynes@5
   476
		operand = RM(ir);
nkeynes@5
   477
		switch(SHIFT(ir)) {
nkeynes@5
   478
		case 0: /* (Rm << imm) */
nkeynes@5
   479
			tmp = SHIFTIMM(ir);
nkeynes@5
   480
			if( tmp == 0 ) { /* Rm */
nkeynes@5
   481
				armr.shift_c = armr.c;
nkeynes@5
   482
			} else { /* Rm << imm */
nkeynes@5
   483
				armr.shift_c = (operand >> (32-tmp)) & 0x01;
nkeynes@5
   484
				operand = operand << tmp;
nkeynes@5
   485
			}
nkeynes@5
   486
			break;
nkeynes@5
   487
		case 1: /* (Rm << Rs) */
nkeynes@5
   488
			tmp = RS(ir)&0xFF;
nkeynes@5
   489
			if( tmp == 0 ) {
nkeynes@5
   490
				armr.shift_c = armr.c;
nkeynes@5
   491
			} else {
nkeynes@5
   492
				if( tmp <= 32 )
nkeynes@5
   493
					armr.shift_c = (operand >> (32-tmp)) & 0x01;
nkeynes@5
   494
				else armr.shift_c = 0;
nkeynes@5
   495
				if( tmp < 32 )
nkeynes@5
   496
					operand = operand << tmp;
nkeynes@5
   497
				else operand = 0;
nkeynes@5
   498
			}
nkeynes@5
   499
			break;
nkeynes@5
   500
		case 2: /* (Rm >> imm) */
nkeynes@5
   501
			tmp = SHIFTIMM(ir);
nkeynes@5
   502
			if( tmp == 0 ) {
nkeynes@5
   503
				armr.shift_c = operand >> 31;
nkeynes@5
   504
				operand = 0;
nkeynes@5
   505
			} else {
nkeynes@5
   506
				armr.shift_c = (operand >> (tmp-1)) & 0x01;
nkeynes@5
   507
				operand = RM(ir) >> tmp;
nkeynes@5
   508
			}
nkeynes@5
   509
			break;
nkeynes@5
   510
		case 3: /* (Rm >> Rs) */
nkeynes@5
   511
			tmp = RS(ir) & 0xFF;
nkeynes@5
   512
			if( tmp == 0 ) {
nkeynes@5
   513
				armr.shift_c = armr.c;
nkeynes@5
   514
			} else {
nkeynes@5
   515
				if( tmp <= 32 )
nkeynes@5
   516
					armr.shift_c = (operand >> (tmp-1))&0x01;
nkeynes@5
   517
				else armr.shift_c = 0;
nkeynes@5
   518
				if( tmp < 32 )
nkeynes@5
   519
					operand = operand >> tmp;
nkeynes@5
   520
				else operand = 0;
nkeynes@5
   521
			}
nkeynes@5
   522
			break;
nkeynes@5
   523
		case 4: /* (Rm >>> imm) */
nkeynes@5
   524
			tmp = SHIFTIMM(ir);
nkeynes@5
   525
			if( tmp == 0 ) {
nkeynes@5
   526
				armr.shift_c = operand >> 31;
nkeynes@5
   527
				operand = -armr.shift_c;
nkeynes@5
   528
			} else {
nkeynes@5
   529
				armr.shift_c = (operand >> (tmp-1)) & 0x01;
nkeynes@5
   530
				operand = ((int32_t)operand) >> tmp;
nkeynes@5
   531
			}
nkeynes@5
   532
			break;
nkeynes@5
   533
		case 5: /* (Rm >>> Rs) */
nkeynes@5
   534
			tmp = RS(ir) & 0xFF;
nkeynes@5
   535
			if( tmp == 0 ) {
nkeynes@5
   536
				armr.shift_c = armr.c;
nkeynes@5
   537
			} else {
nkeynes@5
   538
				if( tmp < 32 ) {
nkeynes@5
   539
					armr.shift_c = (operand >> (tmp-1))&0x01;
nkeynes@5
   540
					operand = ((int32_t)operand) >> tmp;
nkeynes@5
   541
				} else {
nkeynes@5
   542
					armr.shift_c = operand >> 31;
nkeynes@5
   543
					operand = ((int32_t)operand) >> 31;
nkeynes@5
   544
				}
nkeynes@5
   545
			}
nkeynes@5
   546
			break;
nkeynes@5
   547
		case 6:
nkeynes@5
   548
			tmp = SHIFTIMM(ir);
nkeynes@5
   549
			if( tmp == 0 ) { /* RRX aka rotate with carry */
nkeynes@5
   550
				armr.shift_c = operand&0x01;
nkeynes@7
   551
				operand = (operand >> 1) | (armr.c<<31);
nkeynes@5
   552
			} else {
nkeynes@5
   553
				armr.shift_c = operand>>(tmp-1);
nkeynes@5
   554
				operand = ROTATE_RIGHT_LONG(operand,tmp);
nkeynes@5
   555
			}
nkeynes@5
   556
			break;
nkeynes@5
   557
		case 7:
nkeynes@5
   558
			tmp = RS(ir)&0xFF;
nkeynes@5
   559
			if( tmp == 0 ) {
nkeynes@5
   560
				armr.shift_c = armr.c;
nkeynes@5
   561
			} else {
nkeynes@5
   562
				tmp &= 0x1F;
nkeynes@5
   563
				if( tmp == 0 ) {
nkeynes@5
   564
					armr.shift_c = operand>>31;
nkeynes@5
   565
				} else {
nkeynes@5
   566
					armr.shift_c = (operand>>(tmp-1))&0x1;
nkeynes@5
   567
					operand = ROTATE_RIGHT_LONG(operand,tmp);
nkeynes@5
   568
				}
nkeynes@5
   569
			}
nkeynes@5
   570
			break;
nkeynes@5
   571
		}
nkeynes@5
   572
	} else {
nkeynes@5
   573
		operand = IMM8(ir);
nkeynes@5
   574
		tmp = IMMROT(ir);
nkeynes@5
   575
		if( tmp == 0 ) {
nkeynes@5
   576
			armr.shift_c = armr.c;
nkeynes@5
   577
		} else {
nkeynes@5
   578
			operand = ROTATE_RIGHT_LONG(operand, tmp);
nkeynes@5
   579
			armr.shift_c = operand>>31;
nkeynes@5
   580
		}
nkeynes@5
   581
	}
nkeynes@5
   582
	return operand;
nkeynes@5
   583
}
nkeynes@5
   584
nkeynes@5
   585
/**
nkeynes@5
   586
 * Another variant of the shifter code for index-based memory addressing.
nkeynes@5
   587
 * Distinguished by the fact that it doesn't support register shifts, and
nkeynes@5
   588
 * ignores the I flag (WTF do the load/store instructions use the I flag to
nkeynes@5
   589
 * mean the _exact opposite_ of what it means for the data processing 
nkeynes@5
   590
 * instructions ???)
nkeynes@5
   591
 */
nkeynes@5
   592
static uint32_t arm_get_address_index( uint32_t ir )
nkeynes@5
   593
{
nkeynes@5
   594
	uint32_t operand = RM(ir);
nkeynes@7
   595
	uint32_t tmp;
nkeynes@7
   596
	
nkeynes@5
   597
	switch(SHIFT(ir)) {
nkeynes@5
   598
	case 0: /* (Rm << imm) */
nkeynes@5
   599
		operand = operand << SHIFTIMM(ir);
nkeynes@5
   600
		break;
nkeynes@5
   601
	case 2: /* (Rm >> imm) */
nkeynes@5
   602
		operand = operand >> SHIFTIMM(ir);
nkeynes@5
   603
		break;
nkeynes@5
   604
	case 4: /* (Rm >>> imm) */
nkeynes@5
   605
		tmp = SHIFTIMM(ir);
nkeynes@5
   606
		if( tmp == 0 ) operand = ((int32_t)operand) >> 31;
nkeynes@5
   607
		else operand = ((int32_t)operand) >> tmp;
nkeynes@5
   608
		break;
nkeynes@5
   609
	case 6:
nkeynes@5
   610
		tmp = SHIFTIMM(ir);
nkeynes@5
   611
		if( tmp == 0 ) /* RRX aka rotate with carry */
nkeynes@7
   612
			operand = (operand >> 1) | (armr.c<<31);
nkeynes@5
   613
		else
nkeynes@5
   614
			operand = ROTATE_RIGHT_LONG(operand,tmp);
nkeynes@5
   615
		break;
nkeynes@5
   616
	default: UNIMP(ir);
nkeynes@5
   617
	}
nkeynes@5
   618
	return operand;	
nkeynes@5
   619
}
nkeynes@5
   620
nkeynes@37
   621
/**
nkeynes@37
   622
 * Determine the address operand of a load/store instruction, including
nkeynes@37
   623
 * applying any pre/post adjustments to the address registers.
nkeynes@37
   624
 * @see s5.2 Addressing Mode 2 - Load and Store Word or Unsigned Byte
nkeynes@37
   625
 * @param The instruction word.
nkeynes@37
   626
 * @return The calculated address
nkeynes@37
   627
 */
nkeynes@5
   628
static uint32_t arm_get_address_operand( uint32_t ir )
nkeynes@5
   629
{
nkeynes@5
   630
	uint32_t addr;
nkeynes@5
   631
	
nkeynes@5
   632
	/* I P U . W */
nkeynes@5
   633
	switch( (ir>>21)&0x1D ) {
nkeynes@5
   634
	case 0: /* Rn -= imm offset (post-indexed) [5.2.8 A5-28] */
nkeynes@5
   635
	case 1:
nkeynes@5
   636
		addr = RN(ir);
nkeynes@7
   637
		LRN(ir) = addr - IMM12(ir);
nkeynes@5
   638
		break;
nkeynes@5
   639
	case 4: /* Rn += imm offsett (post-indexed) [5.2.8 A5-28] */
nkeynes@5
   640
	case 5:
nkeynes@5
   641
		addr = RN(ir);
nkeynes@7
   642
		LRN(ir) = addr + IMM12(ir);
nkeynes@5
   643
		break;
nkeynes@5
   644
	case 8: /* Rn - imm offset  [5.2.2 A5-20] */
nkeynes@5
   645
		addr = RN(ir) - IMM12(ir);
nkeynes@5
   646
		break;
nkeynes@5
   647
	case 9: /* Rn -= imm offset (pre-indexed)  [5.2.5 A5-24] */
nkeynes@5
   648
		addr = RN(ir) - IMM12(ir);
nkeynes@7
   649
		LRN(ir) = addr;
nkeynes@5
   650
		break;
nkeynes@5
   651
	case 12: /* Rn + imm offset  [5.2.2 A5-20] */
nkeynes@5
   652
		addr = RN(ir) + IMM12(ir);
nkeynes@5
   653
		break;
nkeynes@5
   654
	case 13: /* Rn += imm offset  [5.2.5 A5-24 ] */
nkeynes@5
   655
		addr = RN(ir) + IMM12(ir);
nkeynes@7
   656
		LRN(ir) = addr;
nkeynes@5
   657
		break;
nkeynes@5
   658
	case 16: /* Rn -= Rm (post-indexed)  [5.2.10 A5-32 ] */
nkeynes@5
   659
	case 17:
nkeynes@5
   660
		addr = RN(ir);
nkeynes@7
   661
		LRN(ir) = addr - arm_get_address_index(ir);
nkeynes@5
   662
		break;
nkeynes@5
   663
	case 20: /* Rn += Rm (post-indexed)  [5.2.10 A5-32 ] */
nkeynes@5
   664
	case 21:
nkeynes@5
   665
		addr = RN(ir);
nkeynes@7
   666
		LRN(ir) = addr - arm_get_address_index(ir);
nkeynes@5
   667
		break;
nkeynes@5
   668
	case 24: /* Rn - Rm  [5.2.4 A5-23] */
nkeynes@5
   669
		addr = RN(ir) - arm_get_address_index(ir);
nkeynes@5
   670
		break;
nkeynes@5
   671
	case 25: /* RN -= Rm (pre-indexed)  [5.2.7 A5-26] */
nkeynes@5
   672
		addr = RN(ir) - arm_get_address_index(ir);
nkeynes@7
   673
		LRN(ir) = addr;
nkeynes@5
   674
		break;
nkeynes@5
   675
	case 28: /* Rn + Rm  [5.2.4 A5-23] */
nkeynes@5
   676
		addr = RN(ir) + arm_get_address_index(ir);
nkeynes@5
   677
		break;
nkeynes@5
   678
	case 29: /* RN += Rm (pre-indexed) [5.2.7 A5-26] */
nkeynes@5
   679
		addr = RN(ir) + arm_get_address_index(ir);
nkeynes@7
   680
		LRN(ir) = addr;
nkeynes@5
   681
		break;
nkeynes@5
   682
	}
nkeynes@5
   683
	return addr;
nkeynes@5
   684
}
nkeynes@5
   685
nkeynes@30
   686
gboolean arm_execute_instruction( void ) 
nkeynes@2
   687
{
nkeynes@59
   688
    uint32_t pc;
nkeynes@59
   689
    uint32_t ir;
nkeynes@49
   690
    uint32_t operand, operand2, tmp, tmp2, cond;
nkeynes@66
   691
    int i;
nkeynes@2
   692
nkeynes@52
   693
    tmp = armr.int_pending & (~armr.cpsr);
nkeynes@51
   694
    if( tmp ) {
nkeynes@51
   695
	if( tmp & CPSR_F ) {
nkeynes@51
   696
	    arm_raise_exception( EXC_FAST_IRQ );
nkeynes@51
   697
	} else {
nkeynes@51
   698
	    arm_raise_exception( EXC_IRQ );
nkeynes@51
   699
	}
nkeynes@51
   700
    }
nkeynes@51
   701
nkeynes@59
   702
    ir = MEM_READ_LONG(PC);
nkeynes@59
   703
    pc = PC + 4;
nkeynes@37
   704
    PC = pc;
nkeynes@2
   705
nkeynes@37
   706
    /** 
nkeynes@37
   707
     * Check the condition bits first - if the condition fails return 
nkeynes@37
   708
     * immediately without actually looking at the rest of the instruction.
nkeynes@37
   709
     */
nkeynes@37
   710
    switch( COND(ir) ) {
nkeynes@37
   711
    case 0: /* EQ */ 
nkeynes@37
   712
	cond = armr.z;
nkeynes@37
   713
	break;
nkeynes@37
   714
    case 1: /* NE */
nkeynes@37
   715
	cond = !armr.z;
nkeynes@37
   716
	break;
nkeynes@37
   717
    case 2: /* CS/HS */
nkeynes@37
   718
	cond = armr.c;
nkeynes@37
   719
	break;
nkeynes@37
   720
    case 3: /* CC/LO */
nkeynes@37
   721
	cond = !armr.c;
nkeynes@37
   722
	break;
nkeynes@37
   723
    case 4: /* MI */
nkeynes@37
   724
	cond = armr.n;
nkeynes@37
   725
	break;
nkeynes@37
   726
    case 5: /* PL */
nkeynes@37
   727
	cond = !armr.n;
nkeynes@37
   728
	break;
nkeynes@37
   729
    case 6: /* VS */
nkeynes@37
   730
	cond = armr.v;
nkeynes@37
   731
	break;
nkeynes@37
   732
    case 7: /* VC */
nkeynes@37
   733
	cond = !armr.v;
nkeynes@37
   734
	break;
nkeynes@37
   735
    case 8: /* HI */
nkeynes@37
   736
	cond = armr.c && !armr.z;
nkeynes@37
   737
	break;
nkeynes@37
   738
    case 9: /* LS */
nkeynes@37
   739
	cond = (!armr.c) || armr.z;
nkeynes@37
   740
	break;
nkeynes@37
   741
    case 10: /* GE */
nkeynes@37
   742
	cond = (armr.n == armr.v);
nkeynes@37
   743
	break;
nkeynes@37
   744
    case 11: /* LT */
nkeynes@37
   745
	cond = (armr.n != armr.v);
nkeynes@37
   746
	break;
nkeynes@37
   747
    case 12: /* GT */
nkeynes@37
   748
	cond = (!armr.z) && (armr.n == armr.v);
nkeynes@37
   749
	break;
nkeynes@37
   750
    case 13: /* LE */
nkeynes@37
   751
	cond = armr.z || (armr.n != armr.v);
nkeynes@37
   752
	break;
nkeynes@37
   753
    case 14: /* AL */
nkeynes@37
   754
	cond = 1;
nkeynes@37
   755
	break;
nkeynes@37
   756
    case 15: /* (NV) */
nkeynes@37
   757
	cond = 0;
nkeynes@37
   758
	UNDEF(ir);
nkeynes@37
   759
    }
nkeynes@37
   760
    if( !cond )
nkeynes@37
   761
	return TRUE;
nkeynes@5
   762
nkeynes@37
   763
    /**
nkeynes@37
   764
     * Condition passed, now for the actual instructions...
nkeynes@37
   765
     */
nkeynes@37
   766
    switch( GRP(ir) ) {
nkeynes@37
   767
    case 0:
nkeynes@37
   768
	if( (ir & 0x0D900000) == 0x01000000 ) {
nkeynes@37
   769
	    /* Instructions that aren't actual data processing even though
nkeynes@37
   770
	     * they sit in the DP instruction block.
nkeynes@37
   771
	     */
nkeynes@37
   772
	    switch( ir & 0x0FF000F0 ) {
nkeynes@37
   773
	    case 0x01200010: /* BX Rd */
nkeynes@37
   774
		armr.t = ir & 0x01;
nkeynes@37
   775
		armr.r[15] = RM(ir) & 0xFFFFFFFE;
nkeynes@37
   776
		break;
nkeynes@37
   777
	    case 0x01000000: /* MRS Rd, CPSR */
nkeynes@37
   778
		LRD(ir) = arm_get_cpsr();
nkeynes@37
   779
		break;
nkeynes@37
   780
	    case 0x01400000: /* MRS Rd, SPSR */
nkeynes@37
   781
		LRD(ir) = armr.spsr;
nkeynes@37
   782
		break;
nkeynes@37
   783
	    case 0x01200000: /* MSR CPSR, Rd */
nkeynes@37
   784
		arm_set_cpsr( RM(ir), ir );
nkeynes@37
   785
		break;
nkeynes@37
   786
	    case 0x01600000: /* MSR SPSR, Rd */
nkeynes@37
   787
		arm_set_spsr( RM(ir), ir );
nkeynes@37
   788
		break;
nkeynes@37
   789
	    case 0x03200000: /* MSR CPSR, imm */
nkeynes@37
   790
		arm_set_cpsr( ROTIMM12(ir), ir );
nkeynes@37
   791
		break;
nkeynes@37
   792
	    case 0x03600000: /* MSR SPSR, imm */
nkeynes@37
   793
		arm_set_spsr( ROTIMM12(ir), ir );
nkeynes@37
   794
		break;
nkeynes@37
   795
	    default:
nkeynes@37
   796
		UNIMP(ir);
nkeynes@37
   797
	    }
nkeynes@37
   798
	} else if( (ir & 0x0E000090) == 0x00000090 ) {
nkeynes@37
   799
	    /* Neither are these */
nkeynes@37
   800
	    switch( (ir>>5)&0x03 ) {
nkeynes@37
   801
	    case 0:
nkeynes@37
   802
		/* Arithmetic extension area */
nkeynes@37
   803
		switch(OPCODE(ir)) {
nkeynes@37
   804
		case 0: /* MUL */
nkeynes@51
   805
		    LRN(ir) = RM(ir) * RS(ir);
nkeynes@37
   806
		    break;
nkeynes@37
   807
		case 1: /* MULS */
nkeynes@51
   808
		    tmp = RM(ir) * RS(ir);
nkeynes@51
   809
		    LRN(ir) = tmp;
nkeynes@51
   810
		    armr.n = tmp>>31;
nkeynes@51
   811
		    armr.z = (tmp == 0);
nkeynes@37
   812
		    break;
nkeynes@37
   813
		case 2: /* MLA */
nkeynes@51
   814
		    LRN(ir) = RM(ir) * RS(ir) + RD(ir);
nkeynes@37
   815
		    break;
nkeynes@37
   816
		case 3: /* MLAS */
nkeynes@51
   817
		    tmp = RM(ir) * RS(ir) + RD(ir);
nkeynes@51
   818
		    LRN(ir) = tmp;
nkeynes@51
   819
		    armr.n = tmp>>31;
nkeynes@51
   820
		    armr.z = (tmp == 0);
nkeynes@37
   821
		    break;
nkeynes@37
   822
		case 8: /* UMULL */
nkeynes@37
   823
		case 9: /* UMULLS */
nkeynes@37
   824
		case 10: /* UMLAL */
nkeynes@37
   825
		case 11: /* UMLALS */
nkeynes@37
   826
		case 12: /* SMULL */
nkeynes@37
   827
		case 13: /* SMULLS */
nkeynes@37
   828
		case 14: /* SMLAL */
nkeynes@37
   829
		case 15: /* SMLALS */
nkeynes@63
   830
		    UNIMP(ir);
nkeynes@37
   831
		    break;
nkeynes@37
   832
		case 16: /* SWP */
nkeynes@51
   833
		    tmp = arm_read_long( RN(ir) );
nkeynes@51
   834
		    switch( RN(ir) & 0x03 ) {
nkeynes@51
   835
		    case 1:
nkeynes@51
   836
			tmp = ROTATE_RIGHT_LONG(tmp, 8);
nkeynes@51
   837
			break;
nkeynes@51
   838
		    case 2:
nkeynes@51
   839
			tmp = ROTATE_RIGHT_LONG(tmp, 16);
nkeynes@51
   840
			break;
nkeynes@51
   841
		    case 3:
nkeynes@51
   842
			tmp = ROTATE_RIGHT_LONG(tmp, 24);
nkeynes@51
   843
			break;
nkeynes@51
   844
		    }
nkeynes@51
   845
		    arm_write_long( RN(ir), RM(ir) );
nkeynes@51
   846
		    LRD(ir) = tmp;
nkeynes@37
   847
		    break;
nkeynes@37
   848
		case 20: /* SWPB */
nkeynes@51
   849
		    tmp = arm_read_byte( RN(ir) );
nkeynes@51
   850
		    arm_write_byte( RN(ir), RM(ir) );
nkeynes@51
   851
		    LRD(ir) = tmp;
nkeynes@37
   852
		    break;
nkeynes@37
   853
		default:
nkeynes@37
   854
		    UNIMP(ir);
nkeynes@5
   855
		}
nkeynes@5
   856
		break;
nkeynes@37
   857
	    case 1:
nkeynes@37
   858
		if( LFLAG(ir) ) {
nkeynes@37
   859
		    /* LDRH */
nkeynes@37
   860
		} else {
nkeynes@37
   861
		    /* STRH */
nkeynes@37
   862
		}
nkeynes@49
   863
		UNIMP(ir);
nkeynes@5
   864
		break;
nkeynes@37
   865
	    case 2:
nkeynes@37
   866
		if( LFLAG(ir) ) {
nkeynes@37
   867
		    /* LDRSB */
nkeynes@37
   868
		} else {
nkeynes@37
   869
		}
nkeynes@49
   870
		UNIMP(ir);
nkeynes@5
   871
		break;
nkeynes@37
   872
	    case 3:
nkeynes@37
   873
		if( LFLAG(ir) ) {
nkeynes@37
   874
		    /* LDRSH */
nkeynes@37
   875
		} else {
nkeynes@37
   876
		}
nkeynes@49
   877
		UNIMP(ir);
nkeynes@5
   878
		break;
nkeynes@37
   879
	    }
nkeynes@37
   880
	} else {
nkeynes@37
   881
	    /* Data processing */
nkeynes@37
   882
nkeynes@37
   883
	    switch(OPCODE(ir)) {
nkeynes@37
   884
	    case 0: /* AND Rd, Rn, operand */
nkeynes@37
   885
		LRD(ir) = RN(ir) & arm_get_shift_operand(ir);
nkeynes@37
   886
		break;
nkeynes@37
   887
	    case 1: /* ANDS Rd, Rn, operand */
nkeynes@37
   888
		operand = arm_get_shift_operand_s(ir) & RN(ir);
nkeynes@37
   889
		LRD(ir) = operand;
nkeynes@37
   890
		if( RDn(ir) == 15 ) {
nkeynes@37
   891
		    arm_restore_cpsr();
nkeynes@37
   892
		} else {
nkeynes@37
   893
		    armr.n = operand>>31;
nkeynes@37
   894
		    armr.z = (operand == 0);
nkeynes@37
   895
		    armr.c = armr.shift_c;
nkeynes@37
   896
		}
nkeynes@37
   897
		break;
nkeynes@37
   898
	    case 2: /* EOR Rd, Rn, operand */
nkeynes@37
   899
		LRD(ir) = RN(ir) ^ arm_get_shift_operand(ir);
nkeynes@37
   900
		break;
nkeynes@37
   901
	    case 3: /* EORS Rd, Rn, operand */
nkeynes@37
   902
		operand = arm_get_shift_operand_s(ir) ^ RN(ir);
nkeynes@37
   903
		LRD(ir) = operand;
nkeynes@37
   904
		if( RDn(ir) == 15 ) {
nkeynes@37
   905
		    arm_restore_cpsr();
nkeynes@37
   906
		} else {
nkeynes@37
   907
		    armr.n = operand>>31;
nkeynes@37
   908
		    armr.z = (operand == 0);
nkeynes@37
   909
		    armr.c = armr.shift_c;
nkeynes@37
   910
		}
nkeynes@37
   911
		break;
nkeynes@37
   912
	    case 4: /* SUB Rd, Rn, operand */
nkeynes@37
   913
		LRD(ir) = RN(ir) - arm_get_shift_operand(ir);
nkeynes@37
   914
		break;
nkeynes@37
   915
	    case 5: /* SUBS Rd, Rn, operand */
nkeynes@37
   916
		operand = RN(ir);
nkeynes@37
   917
		operand2 = arm_get_shift_operand(ir);
nkeynes@37
   918
		tmp = operand - operand2;
nkeynes@37
   919
		LRD(ir) = tmp;
nkeynes@37
   920
		if( RDn(ir) == 15 ) {
nkeynes@37
   921
		    arm_restore_cpsr();
nkeynes@37
   922
		} else {
nkeynes@37
   923
		    armr.n = tmp>>31;
nkeynes@37
   924
		    armr.z = (tmp == 0);
nkeynes@37
   925
		    armr.c = IS_NOTBORROW(tmp,operand,operand2);
nkeynes@37
   926
		    armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
nkeynes@37
   927
		}
nkeynes@37
   928
		break;
nkeynes@37
   929
	    case 6: /* RSB Rd, operand, Rn */
nkeynes@37
   930
		LRD(ir) = arm_get_shift_operand(ir) - RN(ir);
nkeynes@37
   931
		break;
nkeynes@37
   932
	    case 7: /* RSBS Rd, operand, Rn */
nkeynes@37
   933
		operand = arm_get_shift_operand(ir);
nkeynes@37
   934
		operand2 = RN(ir);
nkeynes@37
   935
		tmp = operand - operand2;
nkeynes@37
   936
		LRD(ir) = tmp;
nkeynes@37
   937
		if( RDn(ir) == 15 ) {
nkeynes@37
   938
		    arm_restore_cpsr();
nkeynes@37
   939
		} else {
nkeynes@37
   940
		    armr.n = tmp>>31;
nkeynes@37
   941
		    armr.z = (tmp == 0);
nkeynes@37
   942
		    armr.c = IS_NOTBORROW(tmp,operand,operand2);
nkeynes@37
   943
		    armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
nkeynes@37
   944
		}
nkeynes@37
   945
		break;
nkeynes@37
   946
	    case 8: /* ADD Rd, Rn, operand */
nkeynes@37
   947
		LRD(ir) = RN(ir) + arm_get_shift_operand(ir);
nkeynes@37
   948
		break;
nkeynes@37
   949
	    case 9: /* ADDS Rd, Rn, operand */
nkeynes@37
   950
		operand = arm_get_shift_operand(ir);
nkeynes@37
   951
		operand2 = RN(ir);
nkeynes@37
   952
		tmp = operand + operand2;
nkeynes@37
   953
		LRD(ir) = tmp;
nkeynes@37
   954
		if( RDn(ir) == 15 ) {
nkeynes@37
   955
		    arm_restore_cpsr();
nkeynes@37
   956
		} else {
nkeynes@37
   957
		    armr.n = tmp>>31;
nkeynes@37
   958
		    armr.z = (tmp == 0);
nkeynes@37
   959
		    armr.c = IS_CARRY(tmp,operand,operand2);
nkeynes@37
   960
		    armr.v = IS_ADDOVERFLOW(tmp,operand,operand2);
nkeynes@37
   961
		}
nkeynes@37
   962
		break;			
nkeynes@37
   963
	    case 10: /* ADC */
nkeynes@49
   964
		LRD(ir) = RN(ir) + arm_get_shift_operand(ir) + 
nkeynes@49
   965
		    (armr.c ? 1 : 0);
nkeynes@49
   966
		break;
nkeynes@37
   967
	    case 11: /* ADCS */
nkeynes@49
   968
		operand = arm_get_shift_operand(ir);
nkeynes@49
   969
		operand2 = RN(ir);
nkeynes@49
   970
		tmp = operand + operand2;
nkeynes@49
   971
		tmp2 = tmp + armr.c ? 1 : 0;
nkeynes@49
   972
		LRD(ir) = tmp2;
nkeynes@49
   973
		if( RDn(ir) == 15 ) {
nkeynes@49
   974
		    arm_restore_cpsr();
nkeynes@49
   975
		} else {
nkeynes@49
   976
		    armr.n = tmp >> 31;
nkeynes@49
   977
		    armr.z = (tmp == 0 );
nkeynes@49
   978
		    armr.c = IS_CARRY(tmp,operand,operand2) ||
nkeynes@49
   979
			(tmp2 < tmp);
nkeynes@49
   980
		    armr.v = IS_ADDOVERFLOW(tmp,operand, operand2) ||
nkeynes@49
   981
			((tmp&0x80000000) != (tmp2&0x80000000));
nkeynes@49
   982
		}
nkeynes@49
   983
		break;
nkeynes@37
   984
	    case 12: /* SBC */
nkeynes@49
   985
		LRD(ir) = RN(ir) - arm_get_shift_operand(ir) - 
nkeynes@49
   986
		    (armr.c ? 0 : 1);
nkeynes@49
   987
		break;
nkeynes@37
   988
	    case 13: /* SBCS */
nkeynes@49
   989
		operand = RN(ir);
nkeynes@49
   990
		operand2 = arm_get_shift_operand(ir);
nkeynes@49
   991
		tmp = operand - operand2;
nkeynes@49
   992
		tmp2 = tmp - (armr.c ? 0 : 1);
nkeynes@49
   993
		if( RDn(ir) == 15 ) {
nkeynes@49
   994
		    arm_restore_cpsr();
nkeynes@49
   995
		} else {
nkeynes@49
   996
		    armr.n = tmp >> 31;
nkeynes@49
   997
		    armr.z = (tmp == 0 );
nkeynes@49
   998
		    armr.c = IS_NOTBORROW(tmp,operand,operand2) &&
nkeynes@49
   999
			(tmp2<tmp);
nkeynes@49
  1000
		    armr.v = IS_SUBOVERFLOW(tmp,operand,operand2) ||
nkeynes@49
  1001
			((tmp&0x80000000) != (tmp2&0x80000000));
nkeynes@49
  1002
		}
nkeynes@49
  1003
		break;
nkeynes@37
  1004
	    case 14: /* RSC */
nkeynes@49
  1005
		LRD(ir) = arm_get_shift_operand(ir) - RN(ir) -
nkeynes@49
  1006
		    (armr.c ? 0 : 1);
nkeynes@49
  1007
		break;
nkeynes@37
  1008
	    case 15: /* RSCS */
nkeynes@49
  1009
		operand = arm_get_shift_operand(ir);
nkeynes@49
  1010
		operand2 = RN(ir);
nkeynes@49
  1011
		tmp = operand - operand2;
nkeynes@49
  1012
		tmp2 = tmp - (armr.c ? 0 : 1);
nkeynes@49
  1013
		if( RDn(ir) == 15 ) {
nkeynes@49
  1014
		    arm_restore_cpsr();
nkeynes@49
  1015
		} else {
nkeynes@49
  1016
		    armr.n = tmp >> 31;
nkeynes@49
  1017
		    armr.z = (tmp == 0 );
nkeynes@49
  1018
		    armr.c = IS_NOTBORROW(tmp,operand,operand2) &&
nkeynes@49
  1019
			(tmp2<tmp);
nkeynes@49
  1020
		    armr.v = IS_SUBOVERFLOW(tmp,operand,operand2) ||
nkeynes@49
  1021
			((tmp&0x80000000) != (tmp2&0x80000000));
nkeynes@49
  1022
		}
nkeynes@37
  1023
		break;
nkeynes@37
  1024
	    case 17: /* TST Rn, operand */
nkeynes@37
  1025
		operand = arm_get_shift_operand_s(ir) & RN(ir);
nkeynes@37
  1026
		armr.n = operand>>31;
nkeynes@37
  1027
		armr.z = (operand == 0);
nkeynes@37
  1028
		armr.c = armr.shift_c;
nkeynes@37
  1029
		break;
nkeynes@37
  1030
	    case 19: /* TEQ Rn, operand */
nkeynes@37
  1031
		operand = arm_get_shift_operand_s(ir) ^ RN(ir);
nkeynes@37
  1032
		armr.n = operand>>31;
nkeynes@37
  1033
		armr.z = (operand == 0);
nkeynes@37
  1034
		armr.c = armr.shift_c;
nkeynes@37
  1035
		break;				
nkeynes@37
  1036
	    case 21: /* CMP Rn, operand */
nkeynes@37
  1037
		operand = RN(ir);
nkeynes@37
  1038
		operand2 = arm_get_shift_operand(ir);
nkeynes@37
  1039
		tmp = operand - operand2;
nkeynes@37
  1040
		armr.n = tmp>>31;
nkeynes@37
  1041
		armr.z = (tmp == 0);
nkeynes@37
  1042
		armr.c = IS_NOTBORROW(tmp,operand,operand2);
nkeynes@37
  1043
		armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
nkeynes@37
  1044
		break;
nkeynes@37
  1045
	    case 23: /* CMN Rn, operand */
nkeynes@37
  1046
		operand = RN(ir);
nkeynes@37
  1047
		operand2 = arm_get_shift_operand(ir);
nkeynes@37
  1048
		tmp = operand + operand2;
nkeynes@37
  1049
		armr.n = tmp>>31;
nkeynes@37
  1050
		armr.z = (tmp == 0);
nkeynes@37
  1051
		armr.c = IS_CARRY(tmp,operand,operand2);
nkeynes@37
  1052
		armr.v = IS_ADDOVERFLOW(tmp,operand,operand2);
nkeynes@37
  1053
		break;
nkeynes@37
  1054
	    case 24: /* ORR Rd, Rn, operand */
nkeynes@37
  1055
		LRD(ir) = RN(ir) | arm_get_shift_operand(ir);
nkeynes@37
  1056
		break;
nkeynes@37
  1057
	    case 25: /* ORRS Rd, Rn, operand */
nkeynes@37
  1058
		operand = arm_get_shift_operand_s(ir) | RN(ir);
nkeynes@37
  1059
		LRD(ir) = operand;
nkeynes@37
  1060
		if( RDn(ir) == 15 ) {
nkeynes@37
  1061
		    arm_restore_cpsr();
nkeynes@37
  1062
		} else {
nkeynes@37
  1063
		    armr.n = operand>>31;
nkeynes@37
  1064
		    armr.z = (operand == 0);
nkeynes@37
  1065
		    armr.c = armr.shift_c;
nkeynes@37
  1066
		}
nkeynes@37
  1067
		break;
nkeynes@37
  1068
	    case 26: /* MOV Rd, operand */
nkeynes@37
  1069
		LRD(ir) = arm_get_shift_operand(ir);
nkeynes@37
  1070
		break;
nkeynes@37
  1071
	    case 27: /* MOVS Rd, operand */
nkeynes@37
  1072
		operand = arm_get_shift_operand_s(ir);
nkeynes@37
  1073
		LRD(ir) = operand;
nkeynes@37
  1074
		if( RDn(ir) == 15 ) {
nkeynes@37
  1075
		    arm_restore_cpsr();
nkeynes@37
  1076
		} else {
nkeynes@37
  1077
		    armr.n = operand>>31;
nkeynes@37
  1078
		    armr.z = (operand == 0);
nkeynes@37
  1079
		    armr.c = armr.shift_c;
nkeynes@37
  1080
		}
nkeynes@37
  1081
		break;
nkeynes@37
  1082
	    case 28: /* BIC Rd, Rn, operand */
nkeynes@37
  1083
		LRD(ir) = RN(ir) & (~arm_get_shift_operand(ir));
nkeynes@37
  1084
		break;
nkeynes@37
  1085
	    case 29: /* BICS Rd, Rn, operand */
nkeynes@37
  1086
		operand = RN(ir) & (~arm_get_shift_operand_s(ir));
nkeynes@37
  1087
		LRD(ir) = operand;
nkeynes@37
  1088
		if( RDn(ir) == 15 ) {
nkeynes@37
  1089
		    arm_restore_cpsr();
nkeynes@37
  1090
		} else {
nkeynes@37
  1091
		    armr.n = operand>>31;
nkeynes@37
  1092
		    armr.z = (operand == 0);
nkeynes@37
  1093
		    armr.c = armr.shift_c;
nkeynes@37
  1094
		}
nkeynes@37
  1095
		break;
nkeynes@37
  1096
	    case 30: /* MVN Rd, operand */
nkeynes@37
  1097
		LRD(ir) = ~arm_get_shift_operand(ir);
nkeynes@37
  1098
		break;
nkeynes@37
  1099
	    case 31: /* MVNS Rd, operand */
nkeynes@37
  1100
		operand = ~arm_get_shift_operand_s(ir);
nkeynes@37
  1101
		LRD(ir) = operand;
nkeynes@37
  1102
		if( RDn(ir) == 15 ) {
nkeynes@37
  1103
		    arm_restore_cpsr();
nkeynes@37
  1104
		} else {
nkeynes@37
  1105
		    armr.n = operand>>31;
nkeynes@37
  1106
		    armr.z = (operand == 0);
nkeynes@37
  1107
		    armr.c = armr.shift_c;
nkeynes@37
  1108
		}
nkeynes@37
  1109
		break;
nkeynes@37
  1110
	    default:
nkeynes@37
  1111
		UNIMP(ir);
nkeynes@37
  1112
	    }
nkeynes@5
  1113
	}
nkeynes@37
  1114
	break;
nkeynes@37
  1115
    case 1: /* Load/store */
nkeynes@37
  1116
	operand = arm_get_address_operand(ir);
nkeynes@37
  1117
	switch( (ir>>20)&0x17 ) {
nkeynes@37
  1118
	case 0: case 16: case 18: /* STR Rd, address */
nkeynes@37
  1119
	    arm_write_long( operand, RD(ir) );
nkeynes@37
  1120
	    break;
nkeynes@37
  1121
	case 1: case 17: case 19: /* LDR Rd, address */
nkeynes@37
  1122
	    LRD(ir) = arm_read_long(operand);
nkeynes@37
  1123
	    break;
nkeynes@37
  1124
	case 2: /* STRT Rd, address */
nkeynes@37
  1125
	    arm_write_long_user( operand, RD(ir) );
nkeynes@37
  1126
	    break;
nkeynes@37
  1127
	case 3: /* LDRT Rd, address */
nkeynes@37
  1128
	    LRD(ir) = arm_read_long_user( operand );
nkeynes@37
  1129
	    break;
nkeynes@37
  1130
	case 4: case 20: case 22: /* STRB Rd, address */
nkeynes@37
  1131
	    arm_write_byte( operand, RD(ir) );
nkeynes@37
  1132
	    break;
nkeynes@37
  1133
	case 5: case 21: case 23: /* LDRB Rd, address */
nkeynes@37
  1134
	    LRD(ir) = arm_read_byte( operand );
nkeynes@37
  1135
	    break;
nkeynes@37
  1136
	case 6: /* STRBT Rd, address */
nkeynes@37
  1137
	    arm_write_byte_user( operand, RD(ir) );
nkeynes@37
  1138
	    break;
nkeynes@37
  1139
	case 7: /* LDRBT Rd, address */
nkeynes@37
  1140
	    LRD(ir) = arm_read_byte_user( operand );
nkeynes@37
  1141
	    break;
nkeynes@37
  1142
	}
nkeynes@37
  1143
	break;
nkeynes@37
  1144
    case 2: /* Load/store multiple, branch*/
nkeynes@37
  1145
	if( (ir & 0x02000000) == 0x02000000 ) { /* B[L] imm24 */
nkeynes@37
  1146
	    operand = (SIGNEXT24(ir&0x00FFFFFF) << 2);
nkeynes@37
  1147
	    if( (ir & 0x01000000) == 0x01000000 ) { 
nkeynes@37
  1148
		armr.r[14] = pc; /* BL */
nkeynes@37
  1149
	    }
nkeynes@37
  1150
	    armr.r[15] = pc + 4 + operand;
nkeynes@37
  1151
	} else { /* Load/store multiple */
nkeynes@81
  1152
	    gboolean needRestore = FALSE;
nkeynes@46
  1153
	    operand = RN(ir);
nkeynes@66
  1154
	    
nkeynes@66
  1155
	    switch( (ir & 0x01D00000) >> 20 ) {
nkeynes@66
  1156
	    case 0: /* STMDA */
nkeynes@81
  1157
		if( ir & 0x8000 ) {
nkeynes@81
  1158
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1159
		    operand -= 4;
nkeynes@81
  1160
		}
nkeynes@81
  1161
		for( i=14; i>= 0; i-- ) {
nkeynes@66
  1162
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1163
			arm_write_long( operand, armr.r[i] );
nkeynes@66
  1164
			operand -= 4;
nkeynes@66
  1165
		    }
nkeynes@66
  1166
		}
nkeynes@66
  1167
		break;
nkeynes@66
  1168
	    case 1: /* LDMDA */
nkeynes@66
  1169
		for( i=15; i>= 0; i-- ) {
nkeynes@66
  1170
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1171
			armr.r[i] = arm_read_long( operand );
nkeynes@66
  1172
			operand -= 4;
nkeynes@66
  1173
		    }
nkeynes@66
  1174
		}
nkeynes@66
  1175
		break;
nkeynes@66
  1176
	    case 4: /* STMDA (S) */
nkeynes@81
  1177
		if( ir & 0x8000 ) {
nkeynes@81
  1178
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1179
		    operand -= 4;
nkeynes@81
  1180
		}
nkeynes@81
  1181
		for( i=14; i>= 0; i-- ) {
nkeynes@66
  1182
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1183
			arm_write_long( operand, USER_R(i) );
nkeynes@66
  1184
			operand -= 4;
nkeynes@66
  1185
		    }
nkeynes@66
  1186
		}
nkeynes@66
  1187
		break;
nkeynes@66
  1188
	    case 5: /* LDMDA (S) */
nkeynes@66
  1189
		if( (ir&0x00008000) ) { /* Load PC */
nkeynes@66
  1190
		    for( i=15; i>= 0; i-- ) {
nkeynes@66
  1191
			if( (ir & (1<<i)) ) {
nkeynes@66
  1192
			    armr.r[i] = arm_read_long( operand );
nkeynes@66
  1193
			    operand -= 4;
nkeynes@46
  1194
			}
nkeynes@46
  1195
		    }
nkeynes@81
  1196
		    needRestore = TRUE;
nkeynes@46
  1197
		} else {
nkeynes@66
  1198
		    for( i=15; i>= 0; i-- ) {
nkeynes@66
  1199
			if( (ir & (1<<i)) ) {
nkeynes@66
  1200
			    USER_R(i) = arm_read_long( operand );
nkeynes@66
  1201
			    operand -= 4;
nkeynes@46
  1202
			}
nkeynes@46
  1203
		    }
nkeynes@66
  1204
		}
nkeynes@66
  1205
		break;
nkeynes@66
  1206
	    case 8: /* STMIA */
nkeynes@81
  1207
		for( i=0; i< 15; i++ ) {
nkeynes@66
  1208
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1209
			arm_write_long( operand, armr.r[i] );
nkeynes@66
  1210
			operand += 4;
nkeynes@66
  1211
		    }
nkeynes@66
  1212
		}
nkeynes@81
  1213
		if( ir & 0x8000 ) {
nkeynes@81
  1214
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1215
		    operand += 4;
nkeynes@81
  1216
		}
nkeynes@66
  1217
		break;
nkeynes@66
  1218
	    case 9: /* LDMIA */
nkeynes@66
  1219
		for( i=0; i< 16; i++ ) {
nkeynes@66
  1220
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1221
			armr.r[i] = arm_read_long( operand );
nkeynes@66
  1222
			operand += 4;
nkeynes@66
  1223
		    }
nkeynes@66
  1224
		}
nkeynes@66
  1225
		break;
nkeynes@66
  1226
	    case 12: /* STMIA (S) */
nkeynes@81
  1227
		for( i=0; i< 15; i++ ) {
nkeynes@66
  1228
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1229
			arm_write_long( operand, USER_R(i) );
nkeynes@66
  1230
			operand += 4;
nkeynes@66
  1231
		    }
nkeynes@66
  1232
		}
nkeynes@81
  1233
		if( ir & 0x8000 ) {
nkeynes@81
  1234
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1235
		    operand += 4;
nkeynes@81
  1236
		}
nkeynes@66
  1237
		break;
nkeynes@66
  1238
	    case 13: /* LDMIA (S) */
nkeynes@66
  1239
		if( (ir&0x00008000) ) { /* Load PC */
nkeynes@66
  1240
		    for( i=0; i < 16; i++ ) {
nkeynes@66
  1241
			if( (ir & (1<<i)) ) {
nkeynes@66
  1242
			    armr.r[i] = arm_read_long( operand );
nkeynes@66
  1243
			    operand += 4;
nkeynes@46
  1244
			}
nkeynes@46
  1245
		    }
nkeynes@81
  1246
		    needRestore = TRUE;
nkeynes@66
  1247
		} else {
nkeynes@66
  1248
		    for( i=0; i < 16; i++ ) {
nkeynes@66
  1249
			if( (ir & (1<<i)) ) {
nkeynes@66
  1250
			    USER_R(i) = arm_read_long( operand );
nkeynes@66
  1251
			    operand += 4;
nkeynes@46
  1252
			}
nkeynes@46
  1253
		    }
nkeynes@46
  1254
		}
nkeynes@66
  1255
		break;
nkeynes@66
  1256
	    case 16: /* STMDB */
nkeynes@81
  1257
		if( ir & 0x8000 ) {
nkeynes@81
  1258
		    operand -= 4;
nkeynes@81
  1259
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1260
		}
nkeynes@81
  1261
		for( i=14; i>= 0; i-- ) {
nkeynes@66
  1262
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1263
			operand -= 4;
nkeynes@66
  1264
			arm_write_long( operand, armr.r[i] );
nkeynes@46
  1265
		    }
nkeynes@46
  1266
		}
nkeynes@66
  1267
		break;
nkeynes@66
  1268
	    case 17: /* LDMDB */
nkeynes@66
  1269
		for( i=15; i>= 0; i-- ) {
nkeynes@66
  1270
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1271
			operand -= 4;
nkeynes@66
  1272
			armr.r[i] = arm_read_long( operand );
nkeynes@66
  1273
		    }
nkeynes@66
  1274
		}
nkeynes@66
  1275
		break;
nkeynes@66
  1276
	    case 20: /* STMDB (S) */
nkeynes@81
  1277
		if( ir & 0x8000 ) {
nkeynes@81
  1278
		    operand -= 4;
nkeynes@81
  1279
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1280
		}
nkeynes@81
  1281
		for( i=14; i>= 0; i-- ) {
nkeynes@66
  1282
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1283
			operand -= 4;
nkeynes@66
  1284
			arm_write_long( operand, USER_R(i) );
nkeynes@66
  1285
		    }
nkeynes@66
  1286
		}
nkeynes@66
  1287
		break;
nkeynes@66
  1288
	    case 21: /* LDMDB (S) */
nkeynes@66
  1289
		if( (ir&0x00008000) ) { /* Load PC */
nkeynes@66
  1290
		    for( i=15; i>= 0; i-- ) {
nkeynes@66
  1291
			if( (ir & (1<<i)) ) {
nkeynes@66
  1292
			    operand -= 4;
nkeynes@66
  1293
			    armr.r[i] = arm_read_long( operand );
nkeynes@66
  1294
			}
nkeynes@66
  1295
		    }
nkeynes@81
  1296
		    needRestore = TRUE;
nkeynes@66
  1297
		} else {
nkeynes@66
  1298
		    for( i=15; i>= 0; i-- ) {
nkeynes@66
  1299
			if( (ir & (1<<i)) ) {
nkeynes@66
  1300
			    operand -= 4;
nkeynes@66
  1301
			    USER_R(i) = arm_read_long( operand );
nkeynes@66
  1302
			}
nkeynes@66
  1303
		    }
nkeynes@66
  1304
		}
nkeynes@66
  1305
		break;
nkeynes@66
  1306
	    case 24: /* STMIB */
nkeynes@81
  1307
		for( i=0; i< 15; i++ ) {
nkeynes@66
  1308
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1309
			operand += 4;
nkeynes@66
  1310
			arm_write_long( operand, armr.r[i] );
nkeynes@66
  1311
		    }
nkeynes@66
  1312
		}
nkeynes@81
  1313
		if( ir & 0x8000 ) {
nkeynes@81
  1314
		    operand += 4;
nkeynes@81
  1315
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1316
		}
nkeynes@66
  1317
		break;
nkeynes@66
  1318
	    case 25: /* LDMIB */
nkeynes@66
  1319
		for( i=0; i< 16; i++ ) {
nkeynes@66
  1320
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1321
			operand += 4;
nkeynes@66
  1322
			armr.r[i] = arm_read_long( operand );
nkeynes@66
  1323
		    }
nkeynes@66
  1324
		}
nkeynes@66
  1325
		break;
nkeynes@66
  1326
	    case 28: /* STMIB (S) */
nkeynes@81
  1327
		for( i=0; i< 15; i++ ) {
nkeynes@66
  1328
		    if( (ir & (1<<i)) ) {
nkeynes@66
  1329
			operand += 4;
nkeynes@66
  1330
			arm_write_long( operand, USER_R(i) );
nkeynes@66
  1331
		    }
nkeynes@66
  1332
		}
nkeynes@81
  1333
		if( ir & 0x8000 ) {
nkeynes@81
  1334
		    operand += 4;
nkeynes@81
  1335
		    arm_write_long( operand, armr.r[15]+4 );
nkeynes@81
  1336
		}
nkeynes@66
  1337
		break;
nkeynes@66
  1338
	    case 29: /* LDMIB (S) */
nkeynes@66
  1339
		if( (ir&0x00008000) ) { /* Load PC */
nkeynes@66
  1340
		    for( i=0; i < 16; i++ ) {
nkeynes@66
  1341
			if( (ir & (1<<i)) ) {
nkeynes@66
  1342
			    operand += 4;
nkeynes@66
  1343
			    armr.r[i] = arm_read_long( operand );
nkeynes@66
  1344
			}
nkeynes@66
  1345
		    }
nkeynes@81
  1346
		    needRestore = TRUE;
nkeynes@66
  1347
		} else {
nkeynes@66
  1348
		    for( i=0; i < 16; i++ ) {
nkeynes@66
  1349
			if( (ir & (1<<i)) ) {
nkeynes@66
  1350
			    operand += 4;
nkeynes@66
  1351
			    USER_R(i) = arm_read_long( operand );
nkeynes@66
  1352
			}
nkeynes@66
  1353
		    }
nkeynes@66
  1354
		}
nkeynes@66
  1355
		break;
nkeynes@46
  1356
	    }
nkeynes@66
  1357
	    
nkeynes@66
  1358
	    if( WFLAG(ir) ) 
nkeynes@66
  1359
		LRN(ir) = operand;
nkeynes@81
  1360
	    if( needRestore ) 
nkeynes@81
  1361
		arm_restore_cpsr();
nkeynes@37
  1362
	}
nkeynes@37
  1363
	break;
nkeynes@37
  1364
    case 3: /* Copro */
nkeynes@51
  1365
	if( (ir & 0x0F000000) == 0x0F000000 ) { /* SWI */
nkeynes@51
  1366
	    arm_raise_exception( EXC_SOFTWARE );
nkeynes@51
  1367
	} else {
nkeynes@51
  1368
	    UNIMP(ir);
nkeynes@51
  1369
	}
nkeynes@37
  1370
	break;
nkeynes@37
  1371
    }
nkeynes@81
  1372
    if( armr.r[15] > 0x00200000 ) {
nkeynes@81
  1373
	dreamcast_stop();
nkeynes@81
  1374
	ERROR( "BRANCH to fishkill at %08X", pc );
nkeynes@81
  1375
	return FALSE;
nkeynes@81
  1376
    }
nkeynes@37
  1377
    return TRUE;
nkeynes@2
  1378
}
.