2 * $Id: armcore.c,v 1.16 2006-01-10 13:56:54 nkeynes Exp $
4 * ARM7TDMI CPU emulation core.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #define MODULE aica_module
21 #include "aica/armcore.h"
24 #define STM_R15_OFFSET 12
26 struct arm_registers armr;
28 void arm_set_mode( int mode );
30 uint32_t arm_exceptions[][2] = {{ MODE_SVC, 0x00000000 },
31 { MODE_UND, 0x00000004 },
32 { MODE_SVC, 0x00000008 },
33 { MODE_ABT, 0x0000000C },
34 { MODE_ABT, 0x00000010 },
35 { MODE_IRQ, 0x00000018 },
36 { MODE_FIQ, 0x0000001C } };
39 #define EXC_UNDEFINED 1
40 #define EXC_SOFTWARE 2
41 #define EXC_PREFETCH_ABORT 3
42 #define EXC_DATA_ABORT 4
44 #define EXC_FAST_IRQ 6
46 uint32_t arm_cpu_freq = ARM_BASE_RATE;
47 uint32_t arm_cpu_period = 1000 / ARM_BASE_RATE;
50 static struct breakpoint_struct arm_breakpoints[MAX_BREAKPOINTS];
51 static int arm_breakpoint_count = 0;
53 void arm_set_breakpoint( uint32_t pc, int type )
55 arm_breakpoints[arm_breakpoint_count].address = pc;
56 arm_breakpoints[arm_breakpoint_count].type = type;
57 arm_breakpoint_count++;
60 gboolean arm_clear_breakpoint( uint32_t pc, int type )
64 for( i=0; i<arm_breakpoint_count; i++ ) {
65 if( arm_breakpoints[i].address == pc &&
66 arm_breakpoints[i].type == type ) {
67 while( ++i < arm_breakpoint_count ) {
68 arm_breakpoints[i-1].address = arm_breakpoints[i].address;
69 arm_breakpoints[i-1].type = arm_breakpoints[i].type;
71 arm_breakpoint_count--;
78 int arm_get_breakpoint( uint32_t pc )
81 for( i=0; i<arm_breakpoint_count; i++ ) {
82 if( arm_breakpoints[i].address == pc )
83 return arm_breakpoints[i].type;
88 uint32_t arm_run_slice( uint32_t nanosecs )
91 uint32_t target = armr.icount + nanosecs / arm_cpu_period;
92 uint32_t start = armr.icount;
93 while( armr.icount < target ) {
95 if( !arm_execute_instruction() )
97 #ifdef ENABLE_DEBUG_MODE
98 for( i=0; i<arm_breakpoint_count; i++ ) {
99 if( arm_breakpoints[i].address == armr.r[15] ) {
103 if( i != arm_breakpoint_count ) {
105 if( arm_breakpoints[i].type == BREAK_ONESHOT )
106 arm_clear_breakpoint( armr.r[15], BREAK_ONESHOT );
112 if( target != armr.icount ) {
113 /* Halted - compute time actually executed */
114 nanosecs = (armr.icount - start) * arm_cpu_period;
119 void arm_save_state( FILE *f )
121 fwrite( &armr, sizeof(armr), 1, f );
124 int arm_load_state( FILE *f )
126 fread( &armr, sizeof(armr), 1, f );
131 void arm_reset( void )
133 /* Wipe all processor state */
134 memset( &armr, 0, sizeof(armr) );
136 armr.cpsr = MODE_SVC | CPSR_I | CPSR_F;
137 armr.r[15] = 0x00000000;
140 #define SET_CPSR_CONTROL 0x00010000
141 #define SET_CPSR_EXTENSION 0x00020000
142 #define SET_CPSR_STATUS 0x00040000
143 #define SET_CPSR_FLAGS 0x00080000
145 uint32_t arm_get_cpsr( void )
147 /* write back all flags to the cpsr */
148 armr.cpsr = armr.cpsr & CPSR_COMPACT_MASK;
149 if( armr.n ) armr.cpsr |= CPSR_N;
150 if( armr.z ) armr.cpsr |= CPSR_Z;
151 if( armr.c ) armr.cpsr |= CPSR_C;
152 if( armr.v ) armr.cpsr |= CPSR_V;
153 if( armr.t ) armr.cpsr |= CPSR_T;
158 * Return a pointer to the specified register in the user bank,
159 * regardless of the active bank
161 static uint32_t *arm_user_reg( int reg )
163 if( IS_EXCEPTION_MODE() ) {
164 if( reg == 13 || reg == 14 )
165 return &armr.user_r[reg-8];
166 if( IS_FIQ_MODE() ) {
167 if( reg >= 8 || reg <= 12 )
168 return &armr.user_r[reg-8];
174 #define USER_R(n) *arm_user_reg(n)
177 * Set the CPSR to the specified value.
179 * @param value values to set in CPSR
180 * @param fields set of mask values to define which sections of the
181 * CPSR to set (one of the SET_CPSR_* values above)
183 void arm_set_cpsr( uint32_t value, uint32_t fields )
185 if( IS_PRIVILEGED_MODE() ) {
186 if( fields & SET_CPSR_CONTROL ) {
187 int mode = value & CPSR_MODE;
188 arm_set_mode( mode );
189 armr.t = ( value & CPSR_T ); /* Technically illegal to change */
190 armr.cpsr = (armr.cpsr & 0xFFFFFF00) | (value & 0x000000FF);
193 /* Middle 16 bits not currently defined */
195 if( fields & SET_CPSR_FLAGS ) {
196 /* Break flags directly out of given value - don't bother writing
199 armr.n = ( value & CPSR_N );
200 armr.z = ( value & CPSR_Z );
201 armr.c = ( value & CPSR_C );
202 armr.v = ( value & CPSR_V );
206 void arm_set_spsr( uint32_t value, uint32_t fields )
208 /* Only defined if we actually have an SPSR register */
209 if( IS_EXCEPTION_MODE() ) {
210 if( fields & SET_CPSR_CONTROL ) {
211 armr.spsr = (armr.spsr & 0xFFFFFF00) | (value & 0x000000FF);
214 /* Middle 16 bits not currently defined */
216 if( fields & SET_CPSR_FLAGS ) {
217 armr.spsr = (armr.spsr & 0x00FFFFFF) | (value & 0xFF000000);
223 * Raise an ARM exception (other than reset, which uses arm_reset().
224 * @param exception one of the EXC_* exception codes defined above.
226 void arm_raise_exception( int exception )
228 int mode = arm_exceptions[exception][0];
229 uint32_t spsr = arm_get_cpsr();
230 arm_set_mode( mode );
232 armr.r[14] = armr.r[15] + 4;
233 armr.cpsr = (spsr & 0xFFFFFF00) | mode | CPSR_I;
234 if( mode == MODE_FIQ )
236 armr.r[15] = arm_exceptions[exception][1];
239 void arm_restore_cpsr( void )
241 int spsr = armr.spsr;
242 int mode = spsr & CPSR_MODE;
243 arm_set_mode( mode );
245 armr.n = ( spsr & CPSR_N );
246 armr.z = ( spsr & CPSR_Z );
247 armr.c = ( spsr & CPSR_C );
248 armr.v = ( spsr & CPSR_V );
249 armr.t = ( spsr & CPSR_T );
255 * Change the current executing ARM mode to the requested mode.
256 * Saves any required registers to banks and restores those for the
257 * correct mode. (Note does not actually update CPSR at the moment).
259 void arm_set_mode( int targetMode )
261 int currentMode = armr.cpsr & CPSR_MODE;
262 if( currentMode == targetMode )
265 switch( currentMode ) {
268 armr.user_r[5] = armr.r[13];
269 armr.user_r[6] = armr.r[14];
272 armr.svc_r[0] = armr.r[13];
273 armr.svc_r[1] = armr.r[14];
274 armr.svc_r[2] = armr.spsr;
277 armr.abt_r[0] = armr.r[13];
278 armr.abt_r[1] = armr.r[14];
279 armr.abt_r[2] = armr.spsr;
282 armr.und_r[0] = armr.r[13];
283 armr.und_r[1] = armr.r[14];
284 armr.und_r[2] = armr.spsr;
287 armr.irq_r[0] = armr.r[13];
288 armr.irq_r[1] = armr.r[14];
289 armr.irq_r[2] = armr.spsr;
292 armr.fiq_r[0] = armr.r[8];
293 armr.fiq_r[1] = armr.r[9];
294 armr.fiq_r[2] = armr.r[10];
295 armr.fiq_r[3] = armr.r[11];
296 armr.fiq_r[4] = armr.r[12];
297 armr.fiq_r[5] = armr.r[13];
298 armr.fiq_r[6] = armr.r[14];
299 armr.fiq_r[7] = armr.spsr;
300 armr.r[8] = armr.user_r[0];
301 armr.r[9] = armr.user_r[1];
302 armr.r[10] = armr.user_r[2];
303 armr.r[11] = armr.user_r[3];
304 armr.r[12] = armr.user_r[4];
308 switch( targetMode ) {
311 armr.r[13] = armr.user_r[5];
312 armr.r[14] = armr.user_r[6];
315 armr.r[13] = armr.svc_r[0];
316 armr.r[14] = armr.svc_r[1];
317 armr.spsr = armr.svc_r[2];
320 armr.r[13] = armr.abt_r[0];
321 armr.r[14] = armr.abt_r[1];
322 armr.spsr = armr.abt_r[2];
325 armr.r[13] = armr.und_r[0];
326 armr.r[14] = armr.und_r[1];
327 armr.spsr = armr.und_r[2];
330 armr.r[13] = armr.irq_r[0];
331 armr.r[14] = armr.irq_r[1];
332 armr.spsr = armr.irq_r[2];
335 armr.user_r[0] = armr.r[8];
336 armr.user_r[1] = armr.r[9];
337 armr.user_r[2] = armr.r[10];
338 armr.user_r[3] = armr.r[11];
339 armr.user_r[4] = armr.r[12];
340 armr.r[8] = armr.fiq_r[0];
341 armr.r[9] = armr.fiq_r[1];
342 armr.r[10] = armr.fiq_r[2];
343 armr.r[11] = armr.fiq_r[3];
344 armr.r[12] = armr.fiq_r[4];
345 armr.r[13] = armr.fiq_r[5];
346 armr.r[14] = armr.fiq_r[6];
347 armr.spsr = armr.fiq_r[7];
352 /* Page references are as per ARM DDI 0100E (June 2000) */
354 #define MEM_READ_BYTE( addr ) arm_read_byte(addr)
355 #define MEM_READ_WORD( addr ) arm_read_word(addr)
356 #define MEM_READ_LONG( addr ) arm_read_long(addr)
357 #define MEM_WRITE_BYTE( addr, val ) arm_write_byte(addr, val)
358 #define MEM_WRITE_WORD( addr, val ) arm_write_word(addr, val)
359 #define MEM_WRITE_LONG( addr, val ) arm_write_long(addr, val)
362 #define IS_NOTBORROW( result, op1, op2 ) (op2 > op1 ? 0 : 1)
363 #define IS_CARRY( result, op1, op2 ) (result < op1 ? 1 : 0)
364 #define IS_SUBOVERFLOW( result, op1, op2 ) (((op1^op2) & (result^op1)) >> 31)
365 #define IS_ADDOVERFLOW( result, op1, op2 ) (((op1&op2) & (result^op1)) >> 31)
367 #define PC armr.r[15]
369 /* Instruction fields */
370 #define COND(ir) (ir>>28)
371 #define GRP(ir) ((ir>>26)&0x03)
372 #define OPCODE(ir) ((ir>>20)&0x1F)
373 #define IFLAG(ir) (ir&0x02000000)
374 #define SFLAG(ir) (ir&0x00100000)
375 #define PFLAG(ir) (ir&0x01000000)
376 #define UFLAG(ir) (ir&0x00800000)
377 #define BFLAG(ir) (ir&0x00400000)
378 #define WFLAG(ir) (ir&0x00200000)
379 #define LFLAG(ir) SFLAG(ir)
380 #define RN(ir) (armr.r[((ir>>16)&0x0F)] + (((ir>>16)&0x0F) == 0x0F ? 4 : 0))
381 #define RD(ir) (armr.r[((ir>>12)&0x0F)] + (((ir>>12)&0x0F) == 0x0F ? 4 : 0))
382 #define RDn(ir) ((ir>>12)&0x0F)
383 #define RS(ir) (armr.r[((ir>>8)&0x0F)] + (((ir>>8)&0x0F) == 0x0F ? 4 : 0))
384 #define RM(ir) (armr.r[(ir&0x0F)] + (((ir&0x0F) == 0x0F ? 4 : 0)) )
385 #define LRN(ir) armr.r[((ir>>16)&0x0F)]
386 #define LRD(ir) armr.r[((ir>>12)&0x0F)]
387 #define LRS(ir) armr.r[((ir>>8)&0x0F)]
388 #define LRM(ir) armr.r[(ir&0x0F)]
390 #define IMM8(ir) (ir&0xFF)
391 #define IMM12(ir) (ir&0xFFF)
392 #define SHIFTIMM(ir) ((ir>>7)&0x1F)
393 #define IMMROT(ir) ((ir>>7)&0x1E)
394 #define ROTIMM12(ir) ROTATE_RIGHT_LONG(IMM8(ir),IMMROT(ir))
395 #define SIGNEXT24(n) ((n&0x00800000) ? (n|0xFF000000) : (n&0x00FFFFFF))
396 #define SHIFT(ir) ((ir>>4)&0x07)
397 #define DISP24(ir) ((ir&0x00FFFFFF))
398 #define UNDEF(ir) do{ arm_raise_exception( EXC_UNDEFINED ); return TRUE; } while(0)
399 #define UNIMP(ir) do{ PC-=4; ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", PC, ir ); dreamcast_stop(); return FALSE; }while(0)
402 * Determine the value of the shift-operand for a data processing instruction,
403 * without determing a value for shift_C (optimized form for instructions that
404 * don't require shift_C ).
405 * @see s5.1 Addressing Mode 1 - Data-processing operands (p A5-2, 218)
407 static uint32_t arm_get_shift_operand( uint32_t ir )
409 uint32_t operand, tmp;
410 if( IFLAG(ir) == 0 ) {
413 case 0: /* (Rm << imm) */
414 operand = operand << SHIFTIMM(ir);
416 case 1: /* (Rm << Rs) */
418 if( tmp > 31 ) operand = 0;
419 else operand = operand << tmp;
421 case 2: /* (Rm >> imm) */
422 operand = operand >> SHIFTIMM(ir);
424 case 3: /* (Rm >> Rs) */
426 if( tmp > 31 ) operand = 0;
427 else operand = operand >> ir;
429 case 4: /* (Rm >>> imm) */
431 if( tmp == 0 ) operand = ((int32_t)operand) >> 31;
432 else operand = ((int32_t)operand) >> tmp;
434 case 5: /* (Rm >>> Rs) */
436 if( tmp > 31 ) operand = ((int32_t)operand) >> 31;
437 else operand = ((int32_t)operand) >> tmp;
441 if( tmp == 0 ) /* RRX aka rotate with carry */
442 operand = (operand >> 1) | (armr.c<<31);
444 operand = ROTATE_RIGHT_LONG(operand,tmp);
448 operand = ROTATE_RIGHT_LONG(operand,tmp);
454 operand = ROTATE_RIGHT_LONG(operand, tmp);
460 * Determine the value of the shift-operand for a data processing instruction,
461 * and set armr.shift_c accordingly.
462 * @see s5.1 Addressing Mode 1 - Data-processing operands (p A5-2, 218)
464 static uint32_t arm_get_shift_operand_s( uint32_t ir )
466 uint32_t operand, tmp;
467 if( IFLAG(ir) == 0 ) {
470 case 0: /* (Rm << imm) */
472 if( tmp == 0 ) { /* Rm */
473 armr.shift_c = armr.c;
474 } else { /* Rm << imm */
475 armr.shift_c = (operand >> (32-tmp)) & 0x01;
476 operand = operand << tmp;
479 case 1: /* (Rm << Rs) */
482 armr.shift_c = armr.c;
485 armr.shift_c = (operand >> (32-tmp)) & 0x01;
486 else armr.shift_c = 0;
488 operand = operand << tmp;
492 case 2: /* (Rm >> imm) */
495 armr.shift_c = operand >> 31;
498 armr.shift_c = (operand >> (tmp-1)) & 0x01;
499 operand = RM(ir) >> tmp;
502 case 3: /* (Rm >> Rs) */
505 armr.shift_c = armr.c;
508 armr.shift_c = (operand >> (tmp-1))&0x01;
509 else armr.shift_c = 0;
511 operand = operand >> tmp;
515 case 4: /* (Rm >>> imm) */
518 armr.shift_c = operand >> 31;
519 operand = -armr.shift_c;
521 armr.shift_c = (operand >> (tmp-1)) & 0x01;
522 operand = ((int32_t)operand) >> tmp;
525 case 5: /* (Rm >>> Rs) */
528 armr.shift_c = armr.c;
531 armr.shift_c = (operand >> (tmp-1))&0x01;
532 operand = ((int32_t)operand) >> tmp;
534 armr.shift_c = operand >> 31;
535 operand = ((int32_t)operand) >> 31;
541 if( tmp == 0 ) { /* RRX aka rotate with carry */
542 armr.shift_c = operand&0x01;
543 operand = (operand >> 1) | (armr.c<<31);
545 armr.shift_c = operand>>(tmp-1);
546 operand = ROTATE_RIGHT_LONG(operand,tmp);
552 armr.shift_c = armr.c;
556 armr.shift_c = operand>>31;
558 armr.shift_c = (operand>>(tmp-1))&0x1;
559 operand = ROTATE_RIGHT_LONG(operand,tmp);
568 armr.shift_c = armr.c;
570 operand = ROTATE_RIGHT_LONG(operand, tmp);
571 armr.shift_c = operand>>31;
578 * Another variant of the shifter code for index-based memory addressing.
579 * Distinguished by the fact that it doesn't support register shifts, and
580 * ignores the I flag (WTF do the load/store instructions use the I flag to
581 * mean the _exact opposite_ of what it means for the data processing
584 static uint32_t arm_get_address_index( uint32_t ir )
586 uint32_t operand = RM(ir);
590 case 0: /* (Rm << imm) */
591 operand = operand << SHIFTIMM(ir);
593 case 2: /* (Rm >> imm) */
594 operand = operand >> SHIFTIMM(ir);
596 case 4: /* (Rm >>> imm) */
598 if( tmp == 0 ) operand = ((int32_t)operand) >> 31;
599 else operand = ((int32_t)operand) >> tmp;
603 if( tmp == 0 ) /* RRX aka rotate with carry */
604 operand = (operand >> 1) | (armr.c<<31);
606 operand = ROTATE_RIGHT_LONG(operand,tmp);
614 * Determine the address operand of a load/store instruction, including
615 * applying any pre/post adjustments to the address registers.
616 * @see s5.2 Addressing Mode 2 - Load and Store Word or Unsigned Byte
617 * @param The instruction word.
618 * @return The calculated address
620 static uint32_t arm_get_address_operand( uint32_t ir )
625 switch( (ir>>21)&0x1D ) {
626 case 0: /* Rn -= imm offset (post-indexed) [5.2.8 A5-28] */
629 LRN(ir) = addr - IMM12(ir);
631 case 4: /* Rn += imm offsett (post-indexed) [5.2.8 A5-28] */
634 LRN(ir) = addr + IMM12(ir);
636 case 8: /* Rn - imm offset [5.2.2 A5-20] */
637 addr = RN(ir) - IMM12(ir);
639 case 9: /* Rn -= imm offset (pre-indexed) [5.2.5 A5-24] */
640 addr = RN(ir) - IMM12(ir);
643 case 12: /* Rn + imm offset [5.2.2 A5-20] */
644 addr = RN(ir) + IMM12(ir);
646 case 13: /* Rn += imm offset [5.2.5 A5-24 ] */
647 addr = RN(ir) + IMM12(ir);
650 case 16: /* Rn -= Rm (post-indexed) [5.2.10 A5-32 ] */
653 LRN(ir) = addr - arm_get_address_index(ir);
655 case 20: /* Rn += Rm (post-indexed) [5.2.10 A5-32 ] */
658 LRN(ir) = addr - arm_get_address_index(ir);
660 case 24: /* Rn - Rm [5.2.4 A5-23] */
661 addr = RN(ir) - arm_get_address_index(ir);
663 case 25: /* RN -= Rm (pre-indexed) [5.2.7 A5-26] */
664 addr = RN(ir) - arm_get_address_index(ir);
667 case 28: /* Rn + Rm [5.2.4 A5-23] */
668 addr = RN(ir) + arm_get_address_index(ir);
670 case 29: /* RN += Rm (pre-indexed) [5.2.7 A5-26] */
671 addr = RN(ir) + arm_get_address_index(ir);
678 gboolean arm_execute_instruction( void )
682 uint32_t operand, operand2, tmp, tmp2, cond;
685 tmp = armr.int_pending & (~armr.cpsr);
688 arm_raise_exception( EXC_FAST_IRQ );
690 arm_raise_exception( EXC_IRQ );
694 ir = MEM_READ_LONG(PC);
699 * Check the condition bits first - if the condition fails return
700 * immediately without actually looking at the rest of the instruction.
728 cond = armr.c && !armr.z;
731 cond = (!armr.c) || armr.z;
734 cond = (armr.n == armr.v);
737 cond = (armr.n != armr.v);
740 cond = (!armr.z) && (armr.n == armr.v);
743 cond = armr.z || (armr.n != armr.v);
756 * Condition passed, now for the actual instructions...
760 if( (ir & 0x0D900000) == 0x01000000 ) {
761 /* Instructions that aren't actual data processing even though
762 * they sit in the DP instruction block.
764 switch( ir & 0x0FF000F0 ) {
765 case 0x01200010: /* BX Rd */
767 armr.r[15] = RM(ir) & 0xFFFFFFFE;
769 case 0x01000000: /* MRS Rd, CPSR */
770 LRD(ir) = arm_get_cpsr();
772 case 0x01400000: /* MRS Rd, SPSR */
775 case 0x01200000: /* MSR CPSR, Rd */
776 arm_set_cpsr( RM(ir), ir );
778 case 0x01600000: /* MSR SPSR, Rd */
779 arm_set_spsr( RM(ir), ir );
781 case 0x03200000: /* MSR CPSR, imm */
782 arm_set_cpsr( ROTIMM12(ir), ir );
784 case 0x03600000: /* MSR SPSR, imm */
785 arm_set_spsr( ROTIMM12(ir), ir );
790 } else if( (ir & 0x0E000090) == 0x00000090 ) {
791 /* Neither are these */
792 switch( (ir>>5)&0x03 ) {
794 /* Arithmetic extension area */
797 LRN(ir) = RM(ir) * RS(ir);
800 tmp = RM(ir) * RS(ir);
806 LRN(ir) = RM(ir) * RS(ir) + RD(ir);
809 tmp = RM(ir) * RS(ir) + RD(ir);
817 case 11: /* UMLALS */
819 case 13: /* SMULLS */
821 case 15: /* SMLALS */
825 tmp = arm_read_long( RN(ir) );
826 switch( RN(ir) & 0x03 ) {
828 tmp = ROTATE_RIGHT_LONG(tmp, 8);
831 tmp = ROTATE_RIGHT_LONG(tmp, 16);
834 tmp = ROTATE_RIGHT_LONG(tmp, 24);
837 arm_write_long( RN(ir), RM(ir) );
841 tmp = arm_read_byte( RN(ir) );
842 arm_write_byte( RN(ir), RM(ir) );
873 /* Data processing */
876 case 0: /* AND Rd, Rn, operand */
877 LRD(ir) = RN(ir) & arm_get_shift_operand(ir);
879 case 1: /* ANDS Rd, Rn, operand */
880 operand = arm_get_shift_operand_s(ir) & RN(ir);
882 if( RDn(ir) == 15 ) {
885 armr.n = operand>>31;
886 armr.z = (operand == 0);
887 armr.c = armr.shift_c;
890 case 2: /* EOR Rd, Rn, operand */
891 LRD(ir) = RN(ir) ^ arm_get_shift_operand(ir);
893 case 3: /* EORS Rd, Rn, operand */
894 operand = arm_get_shift_operand_s(ir) ^ RN(ir);
896 if( RDn(ir) == 15 ) {
899 armr.n = operand>>31;
900 armr.z = (operand == 0);
901 armr.c = armr.shift_c;
904 case 4: /* SUB Rd, Rn, operand */
905 LRD(ir) = RN(ir) - arm_get_shift_operand(ir);
907 case 5: /* SUBS Rd, Rn, operand */
909 operand2 = arm_get_shift_operand(ir);
910 tmp = operand - operand2;
912 if( RDn(ir) == 15 ) {
917 armr.c = IS_NOTBORROW(tmp,operand,operand2);
918 armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
921 case 6: /* RSB Rd, operand, Rn */
922 LRD(ir) = arm_get_shift_operand(ir) - RN(ir);
924 case 7: /* RSBS Rd, operand, Rn */
925 operand = arm_get_shift_operand(ir);
927 tmp = operand - operand2;
929 if( RDn(ir) == 15 ) {
934 armr.c = IS_NOTBORROW(tmp,operand,operand2);
935 armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
938 case 8: /* ADD Rd, Rn, operand */
939 LRD(ir) = RN(ir) + arm_get_shift_operand(ir);
941 case 9: /* ADDS Rd, Rn, operand */
942 operand = arm_get_shift_operand(ir);
944 tmp = operand + operand2;
946 if( RDn(ir) == 15 ) {
951 armr.c = IS_CARRY(tmp,operand,operand2);
952 armr.v = IS_ADDOVERFLOW(tmp,operand,operand2);
956 LRD(ir) = RN(ir) + arm_get_shift_operand(ir) +
960 operand = arm_get_shift_operand(ir);
962 tmp = operand + operand2;
963 tmp2 = tmp + armr.c ? 1 : 0;
965 if( RDn(ir) == 15 ) {
969 armr.z = (tmp == 0 );
970 armr.c = IS_CARRY(tmp,operand,operand2) ||
972 armr.v = IS_ADDOVERFLOW(tmp,operand, operand2) ||
973 ((tmp&0x80000000) != (tmp2&0x80000000));
977 LRD(ir) = RN(ir) - arm_get_shift_operand(ir) -
982 operand2 = arm_get_shift_operand(ir);
983 tmp = operand - operand2;
984 tmp2 = tmp - (armr.c ? 0 : 1);
985 if( RDn(ir) == 15 ) {
989 armr.z = (tmp == 0 );
990 armr.c = IS_NOTBORROW(tmp,operand,operand2) &&
992 armr.v = IS_SUBOVERFLOW(tmp,operand,operand2) ||
993 ((tmp&0x80000000) != (tmp2&0x80000000));
997 LRD(ir) = arm_get_shift_operand(ir) - RN(ir) -
1001 operand = arm_get_shift_operand(ir);
1003 tmp = operand - operand2;
1004 tmp2 = tmp - (armr.c ? 0 : 1);
1005 if( RDn(ir) == 15 ) {
1009 armr.z = (tmp == 0 );
1010 armr.c = IS_NOTBORROW(tmp,operand,operand2) &&
1012 armr.v = IS_SUBOVERFLOW(tmp,operand,operand2) ||
1013 ((tmp&0x80000000) != (tmp2&0x80000000));
1016 case 17: /* TST Rn, operand */
1017 operand = arm_get_shift_operand_s(ir) & RN(ir);
1018 armr.n = operand>>31;
1019 armr.z = (operand == 0);
1020 armr.c = armr.shift_c;
1022 case 19: /* TEQ Rn, operand */
1023 operand = arm_get_shift_operand_s(ir) ^ RN(ir);
1024 armr.n = operand>>31;
1025 armr.z = (operand == 0);
1026 armr.c = armr.shift_c;
1028 case 21: /* CMP Rn, operand */
1030 operand2 = arm_get_shift_operand(ir);
1031 tmp = operand - operand2;
1033 armr.z = (tmp == 0);
1034 armr.c = IS_NOTBORROW(tmp,operand,operand2);
1035 armr.v = IS_SUBOVERFLOW(tmp,operand,operand2);
1037 case 23: /* CMN Rn, operand */
1039 operand2 = arm_get_shift_operand(ir);
1040 tmp = operand + operand2;
1042 armr.z = (tmp == 0);
1043 armr.c = IS_CARRY(tmp,operand,operand2);
1044 armr.v = IS_ADDOVERFLOW(tmp,operand,operand2);
1046 case 24: /* ORR Rd, Rn, operand */
1047 LRD(ir) = RN(ir) | arm_get_shift_operand(ir);
1049 case 25: /* ORRS Rd, Rn, operand */
1050 operand = arm_get_shift_operand_s(ir) | RN(ir);
1052 if( RDn(ir) == 15 ) {
1055 armr.n = operand>>31;
1056 armr.z = (operand == 0);
1057 armr.c = armr.shift_c;
1060 case 26: /* MOV Rd, operand */
1061 LRD(ir) = arm_get_shift_operand(ir);
1063 case 27: /* MOVS Rd, operand */
1064 operand = arm_get_shift_operand_s(ir);
1066 if( RDn(ir) == 15 ) {
1069 armr.n = operand>>31;
1070 armr.z = (operand == 0);
1071 armr.c = armr.shift_c;
1074 case 28: /* BIC Rd, Rn, operand */
1075 LRD(ir) = RN(ir) & (~arm_get_shift_operand(ir));
1077 case 29: /* BICS Rd, Rn, operand */
1078 operand = RN(ir) & (~arm_get_shift_operand_s(ir));
1080 if( RDn(ir) == 15 ) {
1083 armr.n = operand>>31;
1084 armr.z = (operand == 0);
1085 armr.c = armr.shift_c;
1088 case 30: /* MVN Rd, operand */
1089 LRD(ir) = ~arm_get_shift_operand(ir);
1091 case 31: /* MVNS Rd, operand */
1092 operand = ~arm_get_shift_operand_s(ir);
1094 if( RDn(ir) == 15 ) {
1097 armr.n = operand>>31;
1098 armr.z = (operand == 0);
1099 armr.c = armr.shift_c;
1107 case 1: /* Load/store */
1108 operand = arm_get_address_operand(ir);
1109 switch( (ir>>20)&0x17 ) {
1110 case 0: case 16: case 18: /* STR Rd, address */
1111 arm_write_long( operand, RD(ir) );
1113 case 1: case 17: case 19: /* LDR Rd, address */
1114 LRD(ir) = arm_read_long(operand);
1116 case 2: /* STRT Rd, address */
1117 arm_write_long_user( operand, RD(ir) );
1119 case 3: /* LDRT Rd, address */
1120 LRD(ir) = arm_read_long_user( operand );
1122 case 4: case 20: case 22: /* STRB Rd, address */
1123 arm_write_byte( operand, RD(ir) );
1125 case 5: case 21: case 23: /* LDRB Rd, address */
1126 LRD(ir) = arm_read_byte( operand );
1128 case 6: /* STRBT Rd, address */
1129 arm_write_byte_user( operand, RD(ir) );
1131 case 7: /* LDRBT Rd, address */
1132 LRD(ir) = arm_read_byte_user( operand );
1136 case 2: /* Load/store multiple, branch*/
1137 if( (ir & 0x02000000) == 0x02000000 ) { /* B[L] imm24 */
1138 operand = (SIGNEXT24(ir&0x00FFFFFF) << 2);
1139 if( (ir & 0x01000000) == 0x01000000 ) {
1140 armr.r[14] = pc; /* BL */
1142 armr.r[15] = pc + 4 + operand;
1143 } else { /* Load/store multiple */
1146 switch( (ir & 0x01D00000) >> 20 ) {
1148 for( i=15; i>= 0; i-- ) {
1149 if( (ir & (1<<i)) ) {
1150 arm_write_long( operand, armr.r[i] );
1156 for( i=15; i>= 0; i-- ) {
1157 if( (ir & (1<<i)) ) {
1158 armr.r[i] = arm_read_long( operand );
1163 case 4: /* STMDA (S) */
1164 for( i=15; i>= 0; i-- ) {
1165 if( (ir & (1<<i)) ) {
1166 arm_write_long( operand, USER_R(i) );
1171 case 5: /* LDMDA (S) */
1172 if( (ir&0x00008000) ) { /* Load PC */
1173 for( i=15; i>= 0; i-- ) {
1174 if( (ir & (1<<i)) ) {
1175 armr.r[i] = arm_read_long( operand );
1181 for( i=15; i>= 0; i-- ) {
1182 if( (ir & (1<<i)) ) {
1183 USER_R(i) = arm_read_long( operand );
1190 for( i=0; i< 16; i++ ) {
1191 if( (ir & (1<<i)) ) {
1192 arm_write_long( operand, armr.r[i] );
1198 for( i=0; i< 16; i++ ) {
1199 if( (ir & (1<<i)) ) {
1200 armr.r[i] = arm_read_long( operand );
1205 case 12: /* STMIA (S) */
1206 for( i=0; i< 16; i++ ) {
1207 if( (ir & (1<<i)) ) {
1208 arm_write_long( operand, USER_R(i) );
1213 case 13: /* LDMIA (S) */
1214 if( (ir&0x00008000) ) { /* Load PC */
1215 for( i=0; i < 16; i++ ) {
1216 if( (ir & (1<<i)) ) {
1217 armr.r[i] = arm_read_long( operand );
1223 for( i=0; i < 16; i++ ) {
1224 if( (ir & (1<<i)) ) {
1225 USER_R(i) = arm_read_long( operand );
1231 case 16: /* STMDB */
1232 for( i=15; i>= 0; i-- ) {
1233 if( (ir & (1<<i)) ) {
1235 arm_write_long( operand, armr.r[i] );
1239 case 17: /* LDMDB */
1240 for( i=15; i>= 0; i-- ) {
1241 if( (ir & (1<<i)) ) {
1243 armr.r[i] = arm_read_long( operand );
1247 case 20: /* STMDB (S) */
1248 for( i=15; i>= 0; i-- ) {
1249 if( (ir & (1<<i)) ) {
1251 arm_write_long( operand, USER_R(i) );
1255 case 21: /* LDMDB (S) */
1256 if( (ir&0x00008000) ) { /* Load PC */
1257 for( i=15; i>= 0; i-- ) {
1258 if( (ir & (1<<i)) ) {
1260 armr.r[i] = arm_read_long( operand );
1265 for( i=15; i>= 0; i-- ) {
1266 if( (ir & (1<<i)) ) {
1268 USER_R(i) = arm_read_long( operand );
1273 case 24: /* STMIB */
1274 for( i=0; i< 16; i++ ) {
1275 if( (ir & (1<<i)) ) {
1277 arm_write_long( operand, armr.r[i] );
1281 case 25: /* LDMIB */
1282 for( i=0; i< 16; i++ ) {
1283 if( (ir & (1<<i)) ) {
1285 armr.r[i] = arm_read_long( operand );
1289 case 28: /* STMIB (S) */
1290 for( i=0; i< 16; i++ ) {
1291 if( (ir & (1<<i)) ) {
1293 arm_write_long( operand, USER_R(i) );
1297 case 29: /* LDMIB (S) */
1298 if( (ir&0x00008000) ) { /* Load PC */
1299 for( i=0; i < 16; i++ ) {
1300 if( (ir & (1<<i)) ) {
1302 armr.r[i] = arm_read_long( operand );
1307 for( i=0; i < 16; i++ ) {
1308 if( (ir & (1<<i)) ) {
1310 USER_R(i) = arm_read_long( operand );
1322 if( (ir & 0x0F000000) == 0x0F000000 ) { /* SWI */
1323 arm_raise_exception( EXC_SOFTWARE );
.