Previous 15994 Revisions Next

r1687 Sunday 17th February, 2008 at 22:11:17 UTC by R. Belmont
[ARM7] Early sync to latest MAME for easier GBA testing.
[src/emu/cpu/arm7]arm7.c arm7core.c arm7core.h arm7exec.c

src/emu/cpu/arm7/arm7core.c
r1686r1687
8989
9090/* Prototypes */
9191
92//SJE: should these be inline? or are they too big to see any benefit?
92// SJE: should these be inline? or are they too big to see any benefit?
9393
9494static void HandleCoProcDO(UINT32 insn);
9595static void HandleCoProcRT(UINT32 insn);
9696static void HandleCoProcDT(UINT32 insn);
9797static void HandleHalfWordDT(UINT32 insn);
9898static void HandleSwap(UINT32 insn);
99static void HandlePSRTransfer( UINT32 insn );
100static void HandleALU( UINT32 insn);
101static void HandleMul( UINT32 insn);
102static void HandleUMulLong( UINT32 insn);
103static void HandleSMulLong( UINT32 insn);
104//static void HandleBranch( UINT32 insn);
105INLINE void HandleBranch( UINT32 insn);       //pretty short, so inline should be ok
106static void HandleMemSingle( UINT32 insn);
107static void HandleMemBlock( UINT32 insn);
108static UINT32 decodeShift( UINT32 insn, UINT32 *pCarry);
109INLINE void SwitchMode( int );
99static void HandlePSRTransfer(UINT32 insn);
100static void HandleALU(UINT32 insn);
101static void HandleMul(UINT32 insn);
102static void HandleUMulLong(UINT32 insn);
103static void HandleSMulLong(UINT32 insn);
104INLINE void HandleBranch(UINT32 insn);       // pretty short, so inline should be ok
105static void HandleMemSingle(UINT32 insn);
106static void HandleMemBlock(UINT32 insn);
107static UINT32 decodeShift(UINT32 insn, UINT32 *pCarry);
108INLINE void SwitchMode(int);
110109static void arm7_check_irq_state(void);
111110
112INLINE void arm7_cpu_write32( int addr, UINT32 data );
113INLINE void arm7_cpu_write16( int addr, UINT16 data );
114INLINE void arm7_cpu_write8( int addr, UINT8 data );
115INLINE UINT32 arm7_cpu_read32( int addr );
116INLINE UINT16 arm7_cpu_read16( int addr );
117INLINE UINT8 arm7_cpu_read8( offs_t addr );
111INLINE void arm7_cpu_write32(UINT32 addr, UINT32 data);
112INLINE void arm7_cpu_write16(UINT32 addr, UINT16 data);
113INLINE void arm7_cpu_write8(UINT32 addr, UINT8 data);
114INLINE UINT32 arm7_cpu_read32(UINT32 addr);
115INLINE UINT16 arm7_cpu_read16(UINT32 addr);
116INLINE UINT8 arm7_cpu_read8(offs_t addr);
118117
119118/* Static Vars */
120//Note: for multi-cpu implementation, this approach won't work w/o modification
121WRITE32_HANDLER((*arm7_coproc_do_callback));        //holder for the co processor Data Operations Callback func.
122READ32_HANDLER((*arm7_coproc_rt_r_callback));   //holder for the co processor Register Transfer Read Callback func.
123WRITE32_HANDLER((*arm7_coproc_rt_w_callback));  //holder for the co processor Register Transfer Write Callback Callback func.
124//holder for the co processor Data Transfer Read & Write Callback funcs
125void (*arm7_coproc_dt_r_callback)(UINT32 insn, UINT32* prn, UINT32 (*read32)(int addr));
126void (*arm7_coproc_dt_w_callback)(UINT32 insn, UINT32* prn, void (*write32)(int addr, UINT32 data));
119// Note: for multi-cpu implementation, this approach won't work w/o modification
120WRITE32_HANDLER((*arm7_coproc_do_callback));    // holder for the co processor Data Operations Callback func.
121READ32_HANDLER((*arm7_coproc_rt_r_callback));   // holder for the co processor Register Transfer Read Callback func.
122WRITE32_HANDLER((*arm7_coproc_rt_w_callback));  // holder for the co processor Register Transfer Write Callback Callback func.
123// holder for the co processor Data Transfer Read & Write Callback funcs
124void (*arm7_coproc_dt_r_callback)(UINT32 insn, UINT32 *prn, UINT32 (*read32)(UINT32 addr));
125void (*arm7_coproc_dt_w_callback)(UINT32 insn, UINT32 *prn, void (*write32)(UINT32 addr, UINT32 data));
127126
128127#ifdef ENABLE_DEBUGGER
129//custom dasm callback handlers for co-processor instructions
130char *(*arm7_dasm_cop_dt_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
131char *(*arm7_dasm_cop_rt_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
132char *(*arm7_dasm_cop_do_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
128// custom dasm callback handlers for co-processor instructions
129char *(*arm7_dasm_cop_dt_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
130char *(*arm7_dasm_cop_rt_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
131char *(*arm7_dasm_cop_do_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
133132#endif
134133
135134/***************************************************************************
136135 * Default Memory Handlers
137136 ***************************************************************************/
138INLINE void arm7_cpu_write32( int addr, UINT32 data )
137INLINE void arm7_cpu_write32(UINT32 addr, UINT32 data)
139138{
140   addr &= ~3;
141   program_write_dword_32le(addr,data);
139    addr &= ~3;
140    program_write_dword_32le(addr, data);
142141}
143142
144143
145INLINE void arm7_cpu_write16( int addr, UINT16 data )
144INLINE void arm7_cpu_write16(UINT32 addr, UINT16 data)
146145{
147   addr &= ~1;
148   program_write_word_32le(addr,data);
146    addr &= ~1;
147    program_write_word_32le(addr, data);
149148}
150149
151INLINE void arm7_cpu_write8( int addr, UINT8 data )
150INLINE void arm7_cpu_write8(UINT32 addr, UINT8 data)
152151{
153      program_write_byte_32le(addr,data);
152    program_write_byte_32le(addr, data);
154153}
155154
156INLINE UINT32 arm7_cpu_read32( int addr )
155INLINE UINT32 arm7_cpu_read32(offs_t addr)
157156{
158157    UINT32 result;
159158
160    if (addr&3)
159    if (addr & 3)
161160    {
162        result = program_read_dword_32le(addr);
163        result = ( result >> ( 8 * ( addr & 3 ) ) ) | ( result << ( 32 - ( 8 * ( addr & 3 ) ) ) );
161        result = program_read_dword_32le(addr & ~3);
162        result = (result >> (8 * (addr & 3))) | (result << (32 - (8 * (addr & 3))));
164163    }
165164    else
166165    {
167       result = program_read_dword_32le(addr);
166        result = program_read_dword_32le(addr);
168167    }
168
169169    return result;
170170}
171171
172INLINE UINT16 arm7_cpu_read16( int addr )
172INLINE UINT16 arm7_cpu_read16(offs_t addr)
173173{
174   UINT16 result;
174    UINT16 result;
175175
176   result = program_read_word_32le(addr & ~1);
176    result = program_read_word_32le(addr & ~1);
177177
178   if (addr & 1)
179   {
180      result = ((result>>8) & 0xff) | ((result&0xff)<<8);
181   }
178    if (addr & 1)
179    {
180        result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
181    }
182182
183   return result;
183    return result;
184184}
185185
186INLINE UINT8 arm7_cpu_read8( offs_t addr )
186INLINE UINT8 arm7_cpu_read8(offs_t addr)
187187{
188    //Handle through normal 8 bit handler ( for 32 bit cpu )
188    // Handle through normal 8 bit handler (for 32 bit cpu)
189189    return program_read_byte_32le(addr);
190190}
191191
r1686r1687
193193 * helper funcs
194194 ***************/
195195
196// TODO LD:
197//  - SIGN_BITS_DIFFER = THUMB_SIGN_BITS_DIFFER
198//  - do while (0)
199//  - HandleALUAddFlags = HandleThumbALUAddFlags except for PC incr
200//  - HandleALUSubFlags = HandleThumbALUSubFlags except for PC incr
201
196202/* Set NZCV flags for ADDS / SUBS */
197#define HandleALUAddFlags(rd, rn, op2) \
198  if (insn & INSN_S) \
199    SET_CPSR( \
200      ((GET_CPSR &~ (N_MASK | Z_MASK | V_MASK | C_MASK)) \
201      | (((!SIGN_BITS_DIFFER(rn, op2)) && SIGN_BITS_DIFFER(rn, rd)) \
202          << V_BIT) \
203      | (((~(rn)) < (op2)) << C_BIT) \
204      | HandleALUNZFlags(rd))); \
203#define HandleALUAddFlags(rd, rn, op2)                                                \
204  if (insn & INSN_S)                                                                  \
205    SET_CPSR(((GET_CPSR & ~(N_MASK | Z_MASK | V_MASK | C_MASK))                       \
206              | (((!SIGN_BITS_DIFFER(rn, op2)) && SIGN_BITS_DIFFER(rn, rd)) << V_BIT) \
207              | (((~(rn)) < (op2)) << C_BIT)                                          \
208              | HandleALUNZFlags(rd)));                                               \
205209  R15 += 4;
206210
207#define HandleThumbALUAddFlags(rd, rn, op2) \
208    SET_CPSR( \
209      ((GET_CPSR &~ (N_MASK | Z_MASK | V_MASK | C_MASK)) \
210      | (((!THUMB_SIGN_BITS_DIFFER(rn, op2)) && THUMB_SIGN_BITS_DIFFER(rn, rd)) \
211          << V_BIT) \
212      | (((~(rn)) < (op2)) << C_BIT) \
213      | HandleALUNZFlags(rd))); \
214     R15 += 2;
211#define HandleThumbALUAddFlags(rd, rn, op2)                                                       \
212    SET_CPSR(((GET_CPSR & ~(N_MASK | Z_MASK | V_MASK | C_MASK))                                   \
213              | (((!THUMB_SIGN_BITS_DIFFER(rn, op2)) && THUMB_SIGN_BITS_DIFFER(rn, rd)) << V_BIT) \
214              | (((~(rn)) < (op2)) << C_BIT)                                                      \
215              | HandleALUNZFlags(rd)));                                                           \
216  R15 += 2;
215217
216218#define IsNeg(i) ((i) >> 31)
217219#define IsPos(i) ((~(i)) >> 31)
218220
219#define HandleALUSubFlags(rd, rn, op2) \
220  if (insn & INSN_S) \
221    SET_CPSR( \
222      ((GET_CPSR &~ (N_MASK | Z_MASK | V_MASK | C_MASK)) \
223      | ((SIGN_BITS_DIFFER(rn, op2) && SIGN_BITS_DIFFER(rn, rd)) \
224          << V_BIT) \
225      | (((IsNeg(rn) & IsPos(op2)) | (IsNeg(rn) & IsPos(rd)) | (IsPos(op2) & IsPos(rd))) ? C_MASK : 0) \
226      | HandleALUNZFlags(rd))); \
221#define HandleALUSubFlags(rd, rn, op2)                                                                         \
222  if (insn & INSN_S)                                                                                           \
223    SET_CPSR(((GET_CPSR & ~(N_MASK | Z_MASK | V_MASK | C_MASK))                                                \
224              | ((SIGN_BITS_DIFFER(rn, op2) && SIGN_BITS_DIFFER(rn, rd)) << V_BIT)                             \
225              | (((IsNeg(rn) & IsPos(op2)) | (IsNeg(rn) & IsPos(rd)) | (IsPos(op2) & IsPos(rd))) ? C_MASK : 0) \
226              | HandleALUNZFlags(rd)));                                                                        \
227227  R15 += 4;
228228
229#define HandleThumbALUSubFlags(rd, rn, op2) \
230    SET_CPSR( \
231      ((GET_CPSR &~ (N_MASK | Z_MASK | V_MASK | C_MASK)) \
232      | ((THUMB_SIGN_BITS_DIFFER(rn, op2) && THUMB_SIGN_BITS_DIFFER(rn, rd)) \
233          << V_BIT) \
234      | (((IsNeg(rn) & IsPos(op2)) | (IsNeg(rn) & IsPos(rd)) | (IsPos(op2) & IsPos(rd))) ? C_MASK : 0) \
235      | HandleALUNZFlags(rd))); \
236   R15 += 2;
229#define HandleThumbALUSubFlags(rd, rn, op2)                                                                    \
230    SET_CPSR(((GET_CPSR & ~(N_MASK | Z_MASK | V_MASK | C_MASK))                                                \
231              | ((THUMB_SIGN_BITS_DIFFER(rn, op2) && THUMB_SIGN_BITS_DIFFER(rn, rd)) << V_BIT)                 \
232              | (((IsNeg(rn) & IsPos(op2)) | (IsNeg(rn) & IsPos(rd)) | (IsPos(op2) & IsPos(rd))) ? C_MASK : 0) \
233              | HandleALUNZFlags(rd)));                                                                        \
234  R15 += 2;
237235
238236/* Set NZC flags for logical operations. */
239237
240//This macro (which I didn't write) - doesn't make it obvious that the SIGN BIT = 31, just as the N Bit does,
241//therfore, N is set by default
242#define HandleALUNZFlags(rd) \
238// This macro (which I didn't write) - doesn't make it obvious that the SIGN BIT = 31, just as the N Bit does,
239// therefore, N is set by default
240#define HandleALUNZFlags(rd)               \
243241  (((rd) & SIGN_BIT) | ((!(rd)) << Z_BIT))
244242
245243
246//Long ALU Functions use bit 63
247#define HandleLongALUNZFlags(rd) \
248  ((((rd) & ((UINT64)1<<63))>>32) | ((!(rd)) << Z_BIT))
244// Long ALU Functions use bit 63
245#define HandleLongALUNZFlags(rd)                            \
246  ((((rd) & ((UINT64)1 << 63)) >> 32) | ((!(rd)) << Z_BIT))
249247
250#define HandleALULogicalFlags(rd, sc) \
251  if (insn & INSN_S) \
252    SET_CPSR( ((GET_CPSR &~ (N_MASK | Z_MASK | C_MASK)) \
253                     | HandleALUNZFlags(rd) \
254                     | (((sc) != 0) << C_BIT)));\
248#define HandleALULogicalFlags(rd, sc)                  \
249  if (insn & INSN_S)                                   \
250    SET_CPSR(((GET_CPSR & ~(N_MASK | Z_MASK | C_MASK)) \
251              | HandleALUNZFlags(rd)                   \
252              | (((sc) != 0) << C_BIT)));              \
255253  R15 += 4;
256254
257//convert cpsr mode num into to text
255// convert cpsr mode num into to text
258256static const char modetext[ARM7_NUM_MODES][5] = {
259         "USER","FIRQ","IRQ","SVC","ILL1","ILL2","ILL3","ABT",
260         "ILL4","ILL5","ILL6","UND","ILL7","ILL8","ILL9","SYS"
257    "USER", "FIRQ", "IRQ",  "SVC", "ILL1", "ILL2", "ILL3", "ABT",
258    "ILL4", "ILL5", "ILL6", "UND", "ILL7", "ILL8", "ILL9", "SYS"
261259};
262static const char* GetModeText( int cpsr )
260static const char *GetModeText(int cpsr)
263261{
264262    return modetext[cpsr & MODE_FLAG];
265263}
266264
267//used to be functions, but no longer a need, so we'll use define for better speed.
268#define GetRegister(rIndex) ARM7REG(sRegisterTable[GET_MODE][rIndex])
269#define SetRegister(rIndex,value) ARM7REG(sRegisterTable[GET_MODE][rIndex]) = value
265// used to be functions, but no longer a need, so we'll use define for better speed.
266#define GetRegister(rIndex)        ARM7REG(sRegisterTable[GET_MODE][rIndex])
267#define SetRegister(rIndex, value) ARM7REG(sRegisterTable[GET_MODE][rIndex]) = value
270268
271//I could prob. convert to macro, but Switchmode shouldn't occur that often in emulated code..
272INLINE void SwitchMode (int cpsr_mode_val)
269// I could prob. convert to macro, but Switchmode shouldn't occur that often in emulated code..
270INLINE void SwitchMode(int cpsr_mode_val)
273271{
274272    UINT32 cspr = GET_CPSR & ~MODE_FLAG;
275273    SET_CPSR(cspr | cpsr_mode_val);
r1686r1687
294292   ROR >32   = Same result as ROR n-32 until amount in range of 1-32 then follow rules
295293*/
296294
297static UINT32 decodeShift( UINT32 insn, UINT32 *pCarry)
295static UINT32 decodeShift(UINT32 insn, UINT32 *pCarry)
298296{
299    UINT32 k  = (insn & INSN_OP2_SHIFT) >> INSN_OP2_SHIFT_SHIFT;  //Bits 11-7
300    UINT32 rm = GET_REGISTER( insn & INSN_OP2_RM );
297    UINT32 k  = (insn & INSN_OP2_SHIFT) >> INSN_OP2_SHIFT_SHIFT;  // Bits 11-7
298    UINT32 rm = GET_REGISTER(insn & INSN_OP2_RM);
301299    UINT32 t  = (insn & INSN_OP2_SHIFT_TYPE) >> INSN_OP2_SHIFT_TYPE_SHIFT;
302300
303    if ((insn & INSN_OP2_RM)==0xf) {
304        rm+=8;
301    if ((insn & INSN_OP2_RM) == 0xf) {
302        rm += 8;
305303    }
306304
307305    /* All shift types ending in 1 are Rk, not #k */
308    if( t & 1 )
306    if (t & 1)
309307    {
310//      LOG(("%08x:  RegShift %02x %02x\n",R15, k>>1,GET_REGISTER(k >> 1)));
311        #if ARM7_DEBUG_CORE
312            if((insn&0x80)==0x80)
313                LOG(("%08x:  RegShift ERROR (p36)\n",R15));
314        #endif
308//      LOG(("%08x:  RegShift %02x %02x\n", R15, k >> 1, GET_REGISTER(k >> 1)));
309#if ARM7_DEBUG_CORE
310            if ((insn & 0x80) == 0x80)
311                LOG(("%08x:  RegShift ERROR (p36)\n", R15));
312#endif
315313
316        //see p35 for check on this
317        //k = GET_REGISTER(k >> 1)&0x1f;
314        // see p35 for check on this
315        //k = GET_REGISTER(k >> 1) & 0x1f;
318316
319        //Keep only the bottom 8 bits for a Register Shift
320        k = GET_REGISTER(k >> 1)&0xff;
317        // Keep only the bottom 8 bits for a Register Shift
318        k = GET_REGISTER(k >> 1) & 0xff;
321319
322        if( k == 0 ) /* Register shift by 0 is a no-op */
320        if (k == 0) /* Register shift by 0 is a no-op */
323321        {
324//          LOG(("%08x:  NO-OP Regshift\n",R15));
325            /* TODO this is wrong for at least ROR by reg with with lower
322//          LOG(("%08x:  NO-OP Regshift\n", R15));
323            /* TODO this is wrong for at least ROR by reg with lower
326324             *      5 bits 0 but lower 8 bits non zero */
327            if (pCarry) *pCarry = GET_CPSR & C_MASK;
325            if (pCarry)
326                *pCarry = GET_CPSR & C_MASK;
328327            return rm;
329328        }
330329    }
r1686r1687
332331    switch (t >> 1)
333332    {
334333    case 0:                     /* LSL */
335        //LSL  32   = Result of 0, Carry = Bit 0 of RM
336        //LSL >32   = Result of 0, Carry out 0
337        if(k>=32)
334        // LSL  32   = Result of 0, Carry = Bit 0 of RM
335        // LSL >32   = Result of 0, Carry out 0
336        if (k >= 32)
338337        {
339            if(pCarry)  *pCarry = (k==32)?rm&1:0;
338            if (pCarry)
339                *pCarry = (k == 32) ? rm & 1 : 0;
340340            return 0;
341341        }
342342        else
343343        {
344344            if (pCarry)
345345            {
346            //LSL      0   = Result = RM, Carry = Old Contents of CPSR C Bit
347            //LSL (0,31)   = Result shifted, least significant bit is in carry out
346            // LSL      0   = Result = RM, Carry = Old Contents of CPSR C Bit
347            // LSL (0,31)   = Result shifted, least significant bit is in carry out
348348            *pCarry = k ? (rm & (1 << (32 - k))) : (GET_CPSR & C_MASK);
349349            }
350350            return k ? LSL(rm, k) : rm;
r1686r1687
354354    case 1:                         /* LSR */
355355        if (k == 0 || k == 32)
356356        {
357            if (pCarry) *pCarry = rm & SIGN_BIT;
357            if (pCarry)
358                *pCarry = rm & SIGN_BIT;
358359            return 0;
359360        }
360361        else if (k > 32)
361362        {
362            if (pCarry) *pCarry = 0;
363            if (pCarry)
364                *pCarry = 0;
363365            return 0;
364366        }
365367        else
366368        {
367            if (pCarry) *pCarry = (rm & (1 << (k - 1)));
369            if (pCarry)
370                *pCarry = (rm & (1 << (k - 1)));
368371            return LSR(rm, k);
369372        }
370373        break;
r1686r1687
373376        if (k == 0 || k > 32)
374377            k = 32;
375378
376        if (pCarry) *pCarry = (rm & (1 << (k - 1)));
379        if (pCarry)
380            *pCarry = (rm & (1 << (k - 1)));
377381        if (k >= 32)
378382            return rm & SIGN_BIT ? 0xffffffffu : 0;
379383        else
r1686r1687
388392    case 3:                     /* ROR and RRX */
389393        if (k)
390394        {
391            while (k > 32) k -= 32;
392            if (pCarry) *pCarry = rm & (1 << (k - 1));
395            while (k > 32)
396                k -= 32;
397            if (pCarry)
398                *pCarry = rm & (1 << (k - 1));
393399            return ROR(rm, k);
394400        }
395401        else
396402        {
397403            /* RRX */
398            if (pCarry) *pCarry = (rm & 1);
404            if (pCarry)
405                *pCarry = (rm & 1);
399406            return LSR(rm, 1) | ((GET_CPSR & C_MASK) << 2);
400407        }
401408        break;
402409    }
403410
404    LOG(("%08x: Decodeshift error\n",R15));
411    LOG(("%08x: Decodeshift error\n", R15));
405412    return 0;
406413} /* decodeShift */
407414
408415
409static int loadInc ( UINT32 pat, UINT32 rbv, UINT32 s)
416static int loadInc(UINT32 pat, UINT32 rbv, UINT32 s)
410417{
411    int i,result;
418    int i, result;
412419
413420    result = 0;
414421    rbv &= ~3;
415    for( i=0; i<16; i++ )
422    for (i = 0; i < 16; i++)
416423    {
417        if( (pat>>i)&1 )
424        if ((pat >> i) & 1)
418425        {
419            if (i==15) {
426            if (i == 15) {
420427                if (s) /* Pull full contents from stack */
421                    SET_REGISTER( 15, READ32(rbv+=4) );
428                    SET_REGISTER(15, READ32(rbv += 4));
422429                else /* Pull only address, preserve mode & status flags */
423                    SET_REGISTER( 15, READ32(rbv+=4) );
430                    SET_REGISTER(15, READ32(rbv += 4));
424431            } else
425                SET_REGISTER( i, READ32(rbv+=4) );
432                SET_REGISTER(i, READ32(rbv += 4));
426433
427434            result++;
428435        }
r1686r1687
430437    return result;
431438}
432439
433static int loadDec( UINT32 pat, UINT32 rbv, UINT32 s)
440static int loadDec(UINT32 pat, UINT32 rbv, UINT32 s)
434441{
435    int i,result;
442    int i, result;
436443
437444    result = 0;
438445    rbv &= ~3;
439    for( i=15; i>=0; i-- )
446    for (i = 15; i >= 0; i--)
440447    {
441        if( (pat>>i)&1 )
448        if ((pat >> i) & 1)
442449        {
443            if (i==15) {
450            if (i == 15) {
444451                if (s) /* Pull full contents from stack */
445                    SET_REGISTER( 15, READ32(rbv-=4) );
452                    SET_REGISTER(15, READ32(rbv -= 4));
446453                else /* Pull only address, preserve mode & status flags */
447                    SET_REGISTER( 15, READ32(rbv-=4) );
454                    SET_REGISTER(15, READ32(rbv -= 4));
448455            }
449456            else
450                SET_REGISTER( i, READ32(rbv -=4) );
457                SET_REGISTER(i, READ32(rbv -= 4));
451458            result++;
452459        }
453460    }
454461    return result;
455462}
456463
457static int storeInc( UINT32 pat, UINT32 rbv)
464static int storeInc(UINT32 pat, UINT32 rbv)
458465{
459    int i,result;
466    int i, result;
460467
461468    result = 0;
462    for( i=0; i<16; i++ )
469    for (i = 0; i < 16; i++)
463470    {
464        if( (pat>>i)&1 )
471        if ((pat >> i) & 1)
465472        {
466            #if ARM7_DEBUG_CORE
467                if(i==15) /* R15 is plus 12 from address of STM */
468                    LOG(("%08x: StoreInc on R15\n",R15));
469            #endif
470            WRITE32( rbv += 4, GET_REGISTER(i) );
473#if ARM7_DEBUG_CORE
474            if (i == 15) /* R15 is plus 12 from address of STM */
475                LOG(("%08x: StoreInc on R15\n", R15));
476#endif
477            WRITE32(rbv += 4, GET_REGISTER(i));
471478            result++;
472479        }
473480    }
474481    return result;
475482} /* storeInc */
476483
477static int storeDec( UINT32 pat, UINT32 rbv)
484static int storeDec(UINT32 pat, UINT32 rbv)
478485{
479    int i,result;
486    int i, result;
480487
481488    result = 0;
482    for( i=15; i>=0; i-- )
489    for (i = 15; i >= 0; i--)
483490    {
484        if( (pat>>i)&1 )
491        if ((pat >> i) & 1)
485492        {
486            #if ARM7_DEBUG_CORE
487                if(i==15) /* R15 is plus 12 from address of STM */
488                    LOG(("%08x: StoreDec on R15\n",R15));
489            #endif
490            WRITE32( rbv -= 4, GET_REGISTER(i) );
493#if ARM7_DEBUG_CORE
494            if (i == 15) /* R15 is plus 12 from address of STM */
495                LOG(("%08x: StoreDec on R15\n", R15));
496#endif
497            WRITE32(rbv -= 4, GET_REGISTER(i));
491498            result++;
492499        }
493500    }
r1686r1687
498505 *                            Main CPU Funcs
499506 ***************************************************************************/
500507
501//CPU INIT
508// CPU INIT
502509static void arm7_core_init(const char *cpuname, int index)
503510{
504511    state_save_register_item_array(cpuname, index, ARM7.sArmRegister);
r1686r1687
510517    state_save_register_item(cpuname, index, ARM7.pendingSwi);
511518}
512519
513//CPU RESET
520// CPU RESET
514521static void arm7_core_reset(void)
515522{
516   int (*save_irqcallback)(int) = ARM7.irq_callback;
523    int (*save_irqcallback)(int) = ARM7.irq_callback;
524
517525    memset(&ARM7, 0, sizeof(ARM7));
518526    ARM7.irq_callback = save_irqcallback;
519527
r1686r1687
521529    SwitchMode(eARM7_MODE_SVC);
522530    SET_CPSR(GET_CPSR | I_MASK | F_MASK | 0x10);
523531    R15 = 0;
524   change_pc(R15);
532    change_pc(R15);
525533}
526534
527//Execute used to be here.. moved to separate file (arm7exec.c) to be included by cpu cores separately
535// Execute used to be here.. moved to separate file (arm7exec.c) to be included by cpu cores separately
528536
529//CPU CHECK IRQ STATE
530//Note: couldn't find any exact cycle counts for most of these exceptions
537// CPU CHECK IRQ STATE
538// Note: couldn't find any exact cycle counts for most of these exceptions
531539static void arm7_check_irq_state(void)
532540{
533541    UINT32 cpsr = GET_CPSR;   /* save current CPSR */
534    UINT32 pc = R15+4;        /* save old pc (already incremented in pipeline) */;
542    UINT32 pc = R15 + 4;      /* save old pc (already incremented in pipeline) */;
535543
536544    /* Exception priorities:
537545
r1686r1687
544552        Software Interrupt
545553    */
546554
547    //Data Abort
555    // Data Abort
548556    if (ARM7.pendingAbtD) {
549557        SwitchMode(eARM7_MODE_ABT);             /* Set ABT mode so PC is saved to correct R14 bank */
550        SET_REGISTER( 14, pc );                 /* save PC to R14 */
551        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
558        SET_REGISTER(14, pc);                   /* save PC to R14 */
559        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
552560        SET_CPSR(GET_CPSR | I_MASK);            /* Mask IRQ */
553561        SET_CPSR(GET_CPSR & ~T_MASK);
554562        R15 = 0x10;                             /* IRQ Vector address */
555   change_pc(R15);
563        change_pc(R15);
556564        ARM7.pendingAbtD = 0;
557565        return;
558566    }
559567
560    //FIQ
561    if (ARM7.pendingFiq && (cpsr & F_MASK)==0) {
568    // FIQ
569    if (ARM7.pendingFiq && (cpsr & F_MASK) == 0) {
562570        SwitchMode(eARM7_MODE_FIQ);             /* Set FIQ mode so PC is saved to correct R14 bank */
563        SET_REGISTER( 14, pc );                 /* save PC to R14 */
564        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
571        SET_REGISTER(14, pc);                   /* save PC to R14 */
572        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
565573        SET_CPSR(GET_CPSR | I_MASK | F_MASK);   /* Mask both IRQ & FIQ */
566574        SET_CPSR(GET_CPSR & ~T_MASK);
567575        R15 = 0x1c;                             /* IRQ Vector address */
568   change_pc(R15);
576        change_pc(R15);
569577        return;
570578    }
571579
572    //IRQ
573    if (ARM7.pendingIrq && (cpsr & I_MASK)==0) {
580    // IRQ
581    if (ARM7.pendingIrq && (cpsr & I_MASK) == 0) {
574582        SwitchMode(eARM7_MODE_IRQ);             /* Set IRQ mode so PC is saved to correct R14 bank */
575        SET_REGISTER( 14, pc );                 /* save PC to R14 */
576        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
583        SET_REGISTER(14, pc);                   /* save PC to R14 */
584        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
577585        SET_CPSR(GET_CPSR | I_MASK);            /* Mask IRQ */
578586        SET_CPSR(GET_CPSR & ~T_MASK);
579587        R15 = 0x18;                             /* IRQ Vector address */
580   change_pc(R15);
588        change_pc(R15);
581589        return;
582590    }
583591
584    //Prefetch Abort
592    // Prefetch Abort
585593    if (ARM7.pendingAbtP) {
586594        SwitchMode(eARM7_MODE_ABT);             /* Set ABT mode so PC is saved to correct R14 bank */
587        SET_REGISTER( 14, pc );                 /* save PC to R14 */
588        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
595        SET_REGISTER(14, pc);                   /* save PC to R14 */
596        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
589597        SET_CPSR(GET_CPSR | I_MASK);            /* Mask IRQ */
590598        SET_CPSR(GET_CPSR & ~T_MASK);
591599        R15 = 0x0c;                             /* IRQ Vector address */
592   change_pc(R15);
600        change_pc(R15);
593601        ARM7.pendingAbtP = 0;
594602        return;
595603    }
596604
597    //Undefined instruction
605    // Undefined instruction
598606    if (ARM7.pendingUnd) {
599607        SwitchMode(eARM7_MODE_UND);             /* Set UND mode so PC is saved to correct R14 bank */
600        SET_REGISTER( 14, pc );                 /* save PC to R14 */
601        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
608        SET_REGISTER(14, pc);                   /* save PC to R14 */
609        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
602610        SET_CPSR(GET_CPSR | I_MASK);            /* Mask IRQ */
603611        SET_CPSR(GET_CPSR & ~T_MASK);
604612        R15 = 0x04;                             /* IRQ Vector address */
605   change_pc(R15);
613        change_pc(R15);
606614        ARM7.pendingUnd = 0;
607615        return;
608616    }
609617
610    //Software Interrupt
618    // Software Interrupt
611619    if (ARM7.pendingSwi) {
612620        SwitchMode(eARM7_MODE_SVC);             /* Set SVC mode so PC is saved to correct R14 bank */
613   // compensate for prefetch (should this also be done for normal IRQ?)
614   if (T_IS_SET(GET_CPSR))
615   {
616           SET_REGISTER( 14, pc-2 );                 /* save PC to R14 */
617   }
618   else
619   {
620           SET_REGISTER( 14, pc );                 /* save PC to R14 */
621   }
622        SET_REGISTER( SPSR, cpsr );             /* Save current CPSR */
621        // compensate for prefetch (should this also be done for normal IRQ?)
622        if (T_IS_SET(GET_CPSR))
623        {
624                SET_REGISTER(14, pc-2);         /* save PC to R14 */
625        }
626        else
627        {
628                SET_REGISTER(14, pc);           /* save PC to R14 */
629        }
630        SET_REGISTER(SPSR, cpsr);               /* Save current CPSR */
623631        SET_CPSR(GET_CPSR | I_MASK);            /* Mask IRQ */
624        SET_CPSR(GET_CPSR & ~T_MASK);      /* Go to ARM mode */
625        R15 = 0x08;            /* Jump to the SWI vector */
626   change_pc(R15);
632        SET_CPSR(GET_CPSR & ~T_MASK);           /* Go to ARM mode */
633        R15 = 0x08;                             /* Jump to the SWI vector */
634        change_pc(R15);
627635        ARM7.pendingSwi = 0;
628636        return;
629637    }
630638}
631639
632//CPU - SET IRQ LINE
640// CPU - SET IRQ LINE
633641static void arm7_core_set_irq_line(int irqline, int state)
634642{
635643    switch (irqline) {
636644
637645    case ARM7_IRQ_LINE: /* IRQ */
638        ARM7.pendingIrq= (state & 1);
646        ARM7.pendingIrq = state & 1;
639647        break;
640648
641649    case ARM7_FIRQ_LINE: /* FIRQ */
642        ARM7.pendingFiq= (state & 1);
650        ARM7.pendingFiq = state & 1;
643651        break;
644652
645653    case ARM7_ABORT_EXCEPTION:
646        ARM7.pendingAbtD= (state & 1);
654        ARM7.pendingAbtD = state & 1;
647655        break;
648656    case ARM7_ABORT_PREFETCH_EXCEPTION:
649        ARM7.pendingAbtP= (state & 1);
657        ARM7.pendingAbtP = state & 1;
650658        break;
651659
652660    case ARM7_UNDEFINE_EXCEPTION:
653        ARM7.pendingUnd= (state & 1);
661        ARM7.pendingUnd = state & 1;
654662        break;
655663    }
656664
r1686r1687
665673static void HandleCoProcDO(UINT32 insn)
666674{
667675    // This instruction simply instructs the co-processor to do something, no data is returned to ARM7 core
668    if(arm7_coproc_do_callback)
669        arm7_coproc_do_callback(insn,0,0);      //simply pass entire opcode to callback - since data format is actually dependent on co-proc implementation
676    if (arm7_coproc_do_callback)
677        arm7_coproc_do_callback(insn, 0, 0);    // simply pass entire opcode to callback - since data format is actually dependent on co-proc implementation
670678    else
671        LOG(("%08x: Co-Processor Data Operation executed, but no callback defined!\n",R15));
679        LOG(("%08x: Co-Processor Data Operation executed, but no callback defined!\n", R15));
672680}
673681
674//Co-Processor Register Transfer - To/From Arm to Co-Proc
682// Co-Processor Register Transfer - To/From Arm to Co-Proc
675683static void HandleCoProcRT(UINT32 insn)
676684{
677685
678686    /* xxxx 1110 oooL nnnn dddd cccc ppp1 mmmm */
679687
680688    // Load (MRC) data from Co-Proc to ARM7 register
681    if( insn&0x00100000 )       //Bit 20 = Load or Store
689    if (insn & 0x00100000)       // Bit 20 = Load or Store
690    {
691        if (arm7_coproc_rt_r_callback)
682692        {
683            if(arm7_coproc_rt_r_callback)
684            {
685                UINT32 res = arm7_coproc_rt_r_callback(insn,0);   //RT Read handler must parse opcode & return appropriate result
686                SET_REGISTER((insn>>12)&0xf,res);
687            }
688            else
689                LOG(("%08x: Co-Processor Register Transfer executed, but no RT Read callback defined!\n",R15));
693            UINT32 res = arm7_coproc_rt_r_callback(insn, 0);   // RT Read handler must parse opcode & return appropriate result
694            SET_REGISTER((insn >> 12) & 0xf, res);
690695        }
696        else
697            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Read callback defined!\n", R15));
698    }
691699    // Store (MCR) data from ARM7 to Co-Proc register
692700    else
693        {
694        if(arm7_coproc_rt_r_callback)
695            arm7_coproc_rt_w_callback(insn,GET_REGISTER((insn>>12)&0xf),0);
701    {
702        if (arm7_coproc_rt_r_callback)
703            arm7_coproc_rt_w_callback(insn, GET_REGISTER((insn >> 12) & 0xf), 0);
696704        else
697            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Write callback defined!\n",R15));
698        }
705            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Write callback defined!\n", R15));
706    }
699707}
700708
701/*Data Transfer - To/From Arm to Co-Proc
709/* Data Transfer - To/From Arm to Co-Proc
702710   Loading or Storing, the co-proc function is responsible to read/write from the base register supplied + offset
703711   8 bit immediate value Base Offset address is << 2 to get the actual #
704712
r1686r1687
709717                address supplied in that case, is simply the base. I suppose this is irrelevant if write back not set
710718                but if co-proc reads multiple address, it must handle the offset adjustment itself.
711719*/
712//todo: test with valid instructions
720// todo: test with valid instructions
713721static void HandleCoProcDT(UINT32 insn)
714722{
715    UINT32 rn = (insn>>16)&0xf;
723    UINT32 rn = (insn >> 16) & 0xf;
716724    UINT32 rnv = GET_REGISTER(rn);    // Get Address Value stored from Rn
717725    UINT32 ornv = rnv;                // Keep value of Rn
718    UINT32 off = (insn&0xff)<<2;      // Offset is << 2 according to manual
719    UINT32* prn = &ARM7REG(rn);       // Pointer to our register, so it can be changed in the callback
726    UINT32 off = (insn & 0xff) << 2;  // Offset is << 2 according to manual
727    UINT32 *prn = &ARM7REG(rn);       // Pointer to our register, so it can be changed in the callback
720728
721    //Pointers to read32/write32 functions
722    void (*write32)(int addr, UINT32 data);
723    UINT32 (*read32)(int addr);
729    // Pointers to read32/write32 functions
730    void (*write32)(UINT32 addr, UINT32 data);
731    UINT32 (*read32)(UINT32 addr);
724732    write32 = PTR_WRITE32;
725733    read32 = PTR_READ32;
726734
727    #if ARM7_DEBUG_CORE
728        if(((insn>>16)&0xf)==15 && (insn & 0x200000))
729            LOG(("%08x: Illegal use of R15 as base for write back value!\n",R15));
730    #endif
735#if ARM7_DEBUG_CORE
736    if (((insn >> 16) & 0xf) == 15 && (insn & 0x200000))
737        LOG(("%08x: Illegal use of R15 as base for write back value!\n", R15));
738#endif
731739
732    //Pre-Increment base address (IF POST INCREMENT - CALL BACK FUNCTION MUST DO IT)
733    if(insn&0x1000000 && off)
740    // Pre-Increment base address (IF POST INCREMENT - CALL BACK FUNCTION MUST DO IT)
741    if ((insn & 0x1000000) && off)
734742    {
735        //Up - Down bit
736        if(insn&0x800000)
737            rnv+=off;
743        // Up - Down bit
744        if (insn & 0x800000)
745            rnv += off;
738746        else
739            rnv-=off;
747            rnv -= off;
740748    }
741749
742750    // Load (LDC) data from ARM7 memory to Co-Proc memory
743    if( insn&0x00100000 )
744        {
745            if(arm7_coproc_dt_r_callback)
746                arm7_coproc_dt_r_callback(insn,prn,read32);
747            else
748                LOG(("%08x: Co-Processer Data Transfer executed, but no READ callback defined!\n",R15));
749        }
751    if (insn & 0x00100000)
752    {
753        if (arm7_coproc_dt_r_callback)
754            arm7_coproc_dt_r_callback(insn, prn, read32);
755        else
756            LOG(("%08x: Co-Processer Data Transfer executed, but no READ callback defined!\n", R15));
757    }
750758    // Store (STC) data from Co-Proc to ARM7 memory
751759    else
752        {
753            if(arm7_coproc_dt_w_callback)
754                arm7_coproc_dt_w_callback(insn,prn,write32);
755            else
756                LOG(("%08x: Co-Processer Data Transfer executed, but no WRITE callback defined!\n",R15));
757        }
760    {
761        if (arm7_coproc_dt_w_callback)
762            arm7_coproc_dt_w_callback(insn, prn, write32);
763        else
764            LOG(("%08x: Co-Processer Data Transfer executed, but no WRITE callback defined!\n", R15));
765    }
758766
759    //If writeback not used - ensure the original value of RN is restored in case co-proc callback changed value
760    if((insn & 0x200000)==0)
761        SET_REGISTER(rn,ornv);
767    // If writeback not used - ensure the original value of RN is restored in case co-proc callback changed value
768    if ((insn & 0x200000) == 0)
769        SET_REGISTER(rn, ornv);
762770}
763771
764static void HandleBranch(  UINT32 insn )
772static void HandleBranch(UINT32 insn)
765773{
766774    UINT32 off = (insn & INSN_BRANCH) << 2;
767775
768776    /* Save PC into LR if this is a branch with link */
769777    if (insn & INSN_BL)
770778    {
771        SET_REGISTER(14,R15 + 4);
779        SET_REGISTER(14, R15 + 4);
772780    }
773781
774782    /* Sign-extend the 24-bit offset in our calculations */
r1686r1687
781789        R15 += off + 8;
782790    }
783791
784   change_pc(R15);
792    change_pc(R15);
785793}
786794
787static void HandleMemSingle( UINT32 insn )
795static void HandleMemSingle(UINT32 insn)
788796{
789797    UINT32 rn, rnv, off, rd;
790798
r1686r1687
817825
818826        if (insn & INSN_SDT_W)
819827        {
820            SET_REGISTER(rn,rnv);
828            SET_REGISTER(rn, rnv);
821829
822    //check writeback???
830    // check writeback???
823831        }
824832        else if (rn == eR15)
825833        {
r1686r1687
846854        /* Load */
847855        if (insn & INSN_SDT_B)
848856        {
849            SET_REGISTER(rd,(UINT32) READ8(rnv));
857            SET_REGISTER(rd, (UINT32)READ8(rnv));
850858        }
851859        else
852860        {
r1686r1687
854862            {
855863                R15 = READ32(rnv);
856864                R15 -= 4;
857      change_pc(R15);
858                //LDR, PC takes 2S + 2N + 1I (5 total cycles)
865                change_pc(R15);
866                // LDR, PC takes 2S + 2N + 1I (5 total cycles)
859867                ARM7_ICOUNT -= 2;
860868            }
861869            else
862870            {
863                SET_REGISTER(rd,READ32(rnv));
871                SET_REGISTER(rd, READ32(rnv));
864872            }
865873        }
866874    }
r1686r1687
869877        /* Store */
870878        if (insn & INSN_SDT_B)
871879        {
872            #if ARM7_DEBUG_CORE
873                if(rd==eR15)
880#if ARM7_DEBUG_CORE
881                if (rd == eR15)
874882                    LOG(("Wrote R15 in byte mode\n"));
875            #endif
883#endif
876884
877885            WRITE8(rnv, (UINT8) GET_REGISTER(rd) & 0xffu);
878886        }
879887        else
880888        {
881            #if ARM7_DEBUG_CORE
882                if(rd==eR15)
889#if ARM7_DEBUG_CORE
890                if (rd == eR15)
883891                    LOG(("Wrote R15 in 32bit mode\n"));
884            #endif
892#endif
885893
886894            //WRITE32(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(rd));
887            WRITE32(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(rd)); //manual says STR rd = PC, +12
895            WRITE32(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(rd)); // manual says STR rd = PC, +12
888896        }
889        //Store takes only 2 N Cycles, so add + 1
897        // Store takes only 2 N Cycles, so add + 1
890898        ARM7_ICOUNT += 1;
891899    }
892900
893901    /* Do post-indexing writeback */
894    if (!(insn & INSN_SDT_P)/* && (insn&INSN_SDT_W)*/)
902    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
895903    {
896904        if (insn & INSN_SDT_U)
897905        {
898906            /* Writeback is applied in pipeline, before value is read from mem,
899907                so writeback is effectively ignored */
900            if (rd==rn) {
901                SET_REGISTER(rn,GET_REGISTER(rd));
902                //todo: check for offs... ?
908            if (rd == rn) {
909                SET_REGISTER(rn, GET_REGISTER(rd));
910                // todo: check for offs... ?
903911            }
904912            else {
905913
906                if ((insn&INSN_SDT_W)!=0)
907                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n",R15,(insn & INSN_SDT_P)!=0,(insn&INSN_SDT_W)!=0,(insn & INSN_SDT_U)!=0));
914                if ((insn & INSN_SDT_W) != 0)
915                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
908916
909                SET_REGISTER(rn,(rnv + off));
917                SET_REGISTER(rn, (rnv + off));
910918            }
911919        }
912920        else
913921        {
914922            /* Writeback is applied in pipeline, before value is read from mem,
915923                so writeback is effectively ignored */
916            if (rd==rn) {
917                SET_REGISTER(rn,GET_REGISTER(rd));
924            if (rd == rn) {
925                SET_REGISTER(rn, GET_REGISTER(rd));
918926            }
919927            else {
920                SET_REGISTER(rn,(rnv - off));
928                SET_REGISTER(rn, (rnv - off));
921929
922                if ((insn&INSN_SDT_W)!=0)
923                LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n",R15,(insn & INSN_SDT_P)!=0,(insn&INSN_SDT_W)!=0,(insn & INSN_SDT_U)!=0));
930                if ((insn & INSN_SDT_W) != 0)
931                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
924932            }
925933        }
926934    }
r1686r1687
933941{
934942    UINT32 rn, rnv, off, rd;
935943
936    //Immediate or Register Offset?
937    if(insn & 0x400000) {               //Bit 22 - 1 = immediate, 0 = register
938        //imm. value in high nibble (bits 8-11) and lo nibble (bit 0-3)
939        off = (((insn>>8)&0x0f)<<4) | (insn&0x0f);
944    // Immediate or Register Offset?
945    if (insn & 0x400000) {               // Bit 22 - 1 = immediate, 0 = register
946        // imm. value in high nibble (bits 8-11) and lo nibble (bit 0-3)
947        off = (((insn >> 8) & 0x0f) << 4) | (insn & 0x0f);
940948    }
941949    else {
942        //register
950        // register
943951        off = GET_REGISTER(insn & 0x0f);
944952    }
945953
r1686r1687
960968
961969        if (insn & INSN_SDT_W)
962970        {
963            SET_REGISTER(rn,rnv);
971            SET_REGISTER(rn, rnv);
964972
965        //check writeback???
973        // check writeback???
966974        }
967975        else if (rn == eR15)
968976        {
r1686r1687
988996    /* Load */
989997    if (insn & INSN_SDT_L)
990998    {
991        //Signed?
992        if(insn & 0x40)
999        // Signed?
1000        if (insn & 0x40)
9931001        {
9941002            UINT32 newval = 0;
9951003
996            //Signed Half Word?
997            if(insn & 0x20) {
998                UINT16 signbyte,databyte;
1004            // Signed Half Word?
1005            if (insn & 0x20) {
1006                UINT16 signbyte, databyte;
9991007                databyte = READ16(rnv) & 0xFFFF;
10001008                signbyte = (databyte & 0x8000) ? 0xffff : 0;
1001                newval = (UINT32)(signbyte<<16)|databyte;
1009                newval = (UINT32)(signbyte << 16)|databyte;
10021010            }
1003            //Signed Byte
1011            // Signed Byte
10041012            else {
10051013                UINT8 databyte;
10061014                UINT32 signbyte;
10071015                databyte = READ8(rnv) & 0xff;
10081016                signbyte = (databyte & 0x80) ? 0xffffff : 0;
1009                newval = (UINT32)(signbyte<<8)|databyte;
1017                newval = (UINT32)(signbyte << 8)|databyte;
10101018            }
10111019
1012            //PC?
1013            if(rd == eR15)
1020            // PC?
1021            if (rd == eR15)
10141022            {
10151023                R15 = newval + 8;
1016            //LDR(H,SH,SB) PC takes 2S + 2N + 1I (5 total cycles)
1024                // LDR(H,SH,SB) PC takes 2S + 2N + 1I (5 total cycles)
10171025                ARM7_ICOUNT -= 2;
10181026
10191027            }
10201028            else
10211029            {
1022                SET_REGISTER(rd,newval);
1030                SET_REGISTER(rd, newval);
10231031                R15 += 4;
10241032            }
10251033        }
1026        //Unsigned Half Word
1034        // Unsigned Half Word
10271035        else
10281036        {
10291037            if (rd == eR15)
r1686r1687
10321040            }
10331041            else
10341042            {
1035                SET_REGISTER(rd,READ16(rnv));
1043                SET_REGISTER(rd, READ16(rnv));
10361044                R15 += 4;
10371045            }
10381046        }
r1686r1687
10421050    /* Store */
10431051    else
10441052    {
1045        //WRITE16(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(rd));
1046        WRITE16(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(rd)); //manual says STR RD=PC, +12 of address
1047        if(rn != eR15)
1053        // WRITE16(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(rd));
1054        WRITE16(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(rd)); // manual says STR RD=PC, +12 of address
1055        if (rn != eR15)
10481056            R15 += 4;
1049        //STRH takes 2 cycles, so we add + 1
1057        // STRH takes 2 cycles, so we add + 1
10501058        ARM7_ICOUNT += 1;
10511059    }
10521060
10531061
10541062
1055    //SJE: No idea if this writeback code works or makes sense here..
1063    // SJE: No idea if this writeback code works or makes sense here..
10561064
10571065    /* Do post-indexing writeback */
1058    if (!(insn & INSN_SDT_P)/* && (insn&INSN_SDT_W)*/)
1066    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
10591067    {
10601068        if (insn & INSN_SDT_U)
10611069        {
10621070            /* Writeback is applied in pipeline, before value is read from mem,
10631071                so writeback is effectively ignored */
1064            if (rd==rn) {
1065                SET_REGISTER(rn,GET_REGISTER(rd));
1066                //todo: check for offs... ?
1072            if (rd == rn) {
1073                SET_REGISTER(rn, GET_REGISTER(rd));
1074                // todo: check for offs... ?
10671075            }
10681076            else {
10691077
1070                if ((insn&INSN_SDT_W)!=0)
1071                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n",R15,(insn & INSN_SDT_P)!=0,(insn&INSN_SDT_W)!=0,(insn & INSN_SDT_U)!=0));
1078                if ((insn & INSN_SDT_W) != 0)
1079                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
10721080
1073                SET_REGISTER(rn,(rnv + off));
1081                SET_REGISTER(rn, (rnv + off));
10741082            }
10751083        }
10761084        else
10771085        {
10781086            /* Writeback is applied in pipeline, before value is read from mem,
10791087                so writeback is effectively ignored */
1080            if (rd==rn) {
1081                SET_REGISTER(rn,GET_REGISTER(rd));
1088            if (rd == rn) {
1089                SET_REGISTER(rn, GET_REGISTER(rd));
10821090            }
10831091            else {
1084                SET_REGISTER(rn,(rnv - off));
1092                SET_REGISTER(rn, (rnv - off));
10851093
1086                if ((insn&INSN_SDT_W)!=0)
1087                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n",R15,(insn & INSN_SDT_P)!=0,(insn&INSN_SDT_W)!=0,(insn & INSN_SDT_U)!=0));
1094                if ((insn & INSN_SDT_W) != 0)
1095                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
10881096            }
10891097        }
10901098    }
1091   change_pc(R15);
1099    change_pc(R15);
10921100}
10931101
10941102static void HandleSwap(UINT32 insn)
10951103{
10961104    UINT32 rn, rm, rd, tmp;
10971105
1098    rn = GET_REGISTER((insn>>16)&0xf);   // reg. w/read address
1099    rm = GET_REGISTER(insn&0xf);   // reg. w/write address
1100    rd = (insn>>12)&0xf;      // dest reg
1106    rn = GET_REGISTER((insn >> 16) & 0xf);  // reg. w/read address
1107    rm = GET_REGISTER(insn & 0xf);          // reg. w/write address
1108    rd = (insn >> 12) & 0xf;                // dest reg
11011109
1102    #if ARM7_DEBUG_CORE
1103    if(rn == 15 || rm == 15 || rd == 15)
1104        LOG(("%08x: Illegal use of R15 in Swap Instruction\n",R15));
1105    #endif
1110#if ARM7_DEBUG_CORE
1111    if (rn == 15 || rm == 15 || rd == 15)
1112        LOG(("%08x: Illegal use of R15 in Swap Instruction\n", R15));
1113#endif
11061114
11071115    // can be byte or word
1108    if(insn & 0x400000)
1116    if (insn & 0x400000)
11091117    {
1110       tmp = READ8(rn);
1111   WRITE8(rn, rm);
1112   SET_REGISTER(rd, tmp);
1118        tmp = READ8(rn);
1119        WRITE8(rn, rm);
1120        SET_REGISTER(rd, tmp);
11131121    }
11141122    else
11151123    {
11161124        tmp = READ32(rn);
1117   WRITE32(rn, rm);
1118   SET_REGISTER(rd, tmp);
1125        WRITE32(rn, rm);
1126        SET_REGISTER(rd, tmp);
11191127    }
11201128
11211129    R15 += 4;
1122    //Instruction takes 1S+2N+1I cycles - so we subtract one more..
1123    ARM7_ICOUNT -=1;
1130    // Instruction takes 1S+2N+1I cycles - so we subtract one more..
1131    ARM7_ICOUNT -= 1;
11241132}
11251133
1126static void HandlePSRTransfer( UINT32 insn )
1134static void HandlePSRTransfer(UINT32 insn)
11271135{
1128    int reg = (insn & 0x400000) ? SPSR : eCPSR; //Either CPSR or SPSR
1136    int reg = (insn & 0x400000) ? SPSR : eCPSR; // Either CPSR or SPSR
11291137    UINT32 newval, val = 0;
11301138    int oldmode = GET_CPSR & MODE_FLAG;
11311139
11321140    // get old value of CPSR/SPSR
11331141    newval = GET_REGISTER(reg);
11341142
1135    //MSR ( bit 21 set ) - Copy value to CPSR/SPSR
1136    if( (insn & 0x00200000) )
1143    // MSR (bit 21 set) - Copy value to CPSR/SPSR
1144    if ((insn & 0x00200000))
11371145    {
1138        //Immediate Value?
1139        if(insn & INSN_I) {
1140            //Value can be specified for a Right Rotate, 2x the value specified.
1146        // Immediate Value?
1147        if (insn & INSN_I) {
1148            // Value can be specified for a Right Rotate, 2x the value specified.
11411149            int by = (insn & INSN_OP2_ROTATE) >> INSN_OP2_ROTATE_SHIFT;
11421150            if (by)
11431151                val = ROR(insn & INSN_OP2_IMM, by << 1);
11441152            else
11451153                val = insn & INSN_OP2_IMM;
11461154        }
1147        //Value from Register
1155        // Value from Register
11481156        else
1149   {
1157        {
11501158            val = GET_REGISTER(insn & 0x0f);
11511159        }
11521160
1153   // apply field code bits
1154   if (reg == eCPSR)
1155   {
1156      if (oldmode != eARM7_MODE_USER)
1157      {
1158         if (insn & 0x00010000)
1159         {
1160            newval = (newval & 0xffffff00) | (val & 0xff);
1161         }
1162         if (insn & 0x00020000)
1163         {
1164            newval = (newval & 0xffff00ff) | (val & 0xff00);
1165         }
1166         if (insn & 0x00040000)
1167         {
1168            newval = (newval & 0xff00ffff) | (val & 0xff0000);
1169         }
1170      }
1161        // apply field code bits
1162        if (reg == eCPSR)
1163        {
1164            if (oldmode != eARM7_MODE_USER)
1165            {
1166                if (insn & 0x00010000)
1167                {
1168                    newval = (newval & 0xffffff00) | (val & 0x000000ff);
1169                }
1170                if (insn & 0x00020000)
1171                {
1172                    newval = (newval & 0xffff00ff) | (val & 0x0000ff00);
1173                }
1174                if (insn & 0x00040000)
1175                {
1176                    newval = (newval & 0xff00ffff) | (val & 0x00ff0000);
1177                }
1178            }
11711179
1172      // status flags can be modified regardless of mode
1173      if (insn & 0x00080000)
1174      {
1175         // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1176         newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1177      }
1178   }
1179   else   // SPSR has stricter requirements
1180   {
1181      if (((GET_CPSR & 0x1f) > 0x10) && ((GET_CPSR & 0x1f) < 0x1f))
1182      {
1183         if (insn & 0x00010000)
1184         {
1185            newval = (newval & 0xffffff00) | (val & 0xff);
1186         }
1187         if (insn & 0x00020000)
1188         {
1189            newval = (newval & 0xffff00ff) | (val & 0xff00);
1190         }
1191         if (insn & 0x00040000)
1192         {
1193            newval = (newval & 0xff00ffff) | (val & 0xff0000);
1194         }
1195         if (insn & 0x00080000)
1196         {
1197            // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1198            newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1199         }
1200      }
1201   }
1180            // status flags can be modified regardless of mode
1181            if (insn & 0x00080000)
1182            {
1183                // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1184                newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1185            }
1186        }
1187        else    // SPSR has stricter requirements
1188        {
1189            if (((GET_CPSR & 0x1f) > 0x10) && ((GET_CPSR & 0x1f) < 0x1f))
1190            {
1191                if (insn & 0x00010000)
1192                {
1193                    newval = (newval & 0xffffff00) | (val & 0xff);
1194                }
1195                if (insn & 0x00020000)
1196                {
1197                    newval = (newval & 0xffff00ff) | (val & 0xff00);
1198                }
1199                if (insn & 0x00040000)
1200                {
1201                    newval = (newval & 0xff00ffff) | (val & 0xff0000);
1202                }
1203                if (insn & 0x00080000)
1204                {
1205                    // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1206                    newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1207                }
1208            }
1209        }
12021210
1203   // force valid mode
1204   newval |= 0x10;
1211        // force valid mode
1212        newval |= 0x10;
12051213
1206        //Update the Register
1214        // Update the Register
12071215        SET_REGISTER(reg, newval);
12081216
1209        //Switch to new mode if changed
1210        if( (newval & MODE_FLAG) != oldmode)
1217        // Switch to new mode if changed
1218        if ((newval & MODE_FLAG) != oldmode)
12111219            SwitchMode(GET_MODE);
12121220
12131221    }
1214    //MRS ( bit 21 clear ) - Copy CPSR or SPSR to specified Register
1222    // MRS (bit 21 clear) - Copy CPSR or SPSR to specified Register
12151223    else
12161224    {
1217        SET_REGISTER( (insn>>12)& 0x0f ,GET_REGISTER(reg));
1225        SET_REGISTER((insn >> 12)& 0x0f, GET_REGISTER(reg));
12181226    }
12191227}
12201228
1221static void HandleALU( UINT32 insn )
1229static void HandleALU(UINT32 insn)
12221230{
1223    UINT32 op2, sc=0, rd, rn, opcode;
1231    UINT32 op2, sc = 0, rd, rn, opcode;
12241232    UINT32 by, rdn;
12251233    UINT32 oldR15 = R15;
12261234
r1686r1687
12441252        }
12451253        else
12461254        {
1247            op2 = insn & INSN_OP2;      //SJE: Shouldn't this be INSN_OP2_IMM?
1255            op2 = insn & INSN_OP2;      // SJE: Shouldn't this be INSN_OP2_IMM?
12481256            sc = GET_CPSR & C_MASK;
12491257        }
12501258    }
r1686r1687
12531261    {
12541262        op2 = decodeShift(insn, (insn & INSN_S) ? &sc : NULL);
12551263
1264        // LD TODO sc will always be 0 if this applies
12561265        if (!(insn & INSN_S))
1257            sc=0;
1266            sc = 0;
12581267    }
12591268
1269    // LD TODO this comment is wrong
12601270    /* Calculate Rn to account for pipelining */
12611271    if ((opcode & 0xd) != 0xd) /* No Rn in MOV */
12621272    {
12631273        if ((rn = (insn & INSN_RN) >> INSN_RN_SHIFT) == eR15)
12641274        {
1265            #if ARM7_DEBUG_CORE
1266                LOG(("%08x:  Pipelined R15 (Shift %d)\n",R15,(insn&INSN_I?8:insn&0x10u?12:12)));
1267            #endif
1268            rn=R15+8;
1275#if ARM7_DEBUG_CORE
1276            LOG(("%08x:  Pipelined R15 (Shift %d)\n", R15, (insn & INSN_I ? 8 : insn & 0x10u ? 12 : 12)));
1277#endif
1278            rn = R15 + 8;
12691279        }
12701280        else
12711281        {
r1686r1687
13121322        HandleALULogicalFlags(rd, sc);
13131323        break;
13141324    case OPCODE_BIC:
1315        rd = rn &~ op2;
1325        rd = rn & ~op2;
13161326        HandleALULogicalFlags(rd, sc);
13171327        break;
13181328    case OPCODE_TEQ:
r1686r1687
13381348    rdn = (insn & INSN_RD) >> INSN_RD_SHIFT;
13391349    if ((opcode & 0xc) != 0x8)
13401350    {
1341        //If Rd = R15, but S Flag not set, Result is placed in R15, but CPSR is not affected (page 44)
1351        // If Rd = R15, but S Flag not set, Result is placed in R15, but CPSR is not affected (page 44)
13421352        if (rdn == eR15 && !(insn & INSN_S))
13431353        {
13441354            R15 = rd;
13451355        }
13461356        else
13471357        {
1348            //Rd = 15 and S Flag IS set, Result is placed in R15, and current mode SPSR moved to CPSR
1349            if (rdn==eR15) {
1358            // Rd = 15 and S Flag IS set, Result is placed in R15, and current mode SPSR moved to CPSR
1359            if (rdn == eR15) {
13501360
1351                //Update CPSR from SPSR
1361                // Update CPSR from SPSR
13521362                SET_CPSR(GET_REGISTER(SPSR));
13531363                SwitchMode(GET_MODE);
13541364
r1686r1687
13591369            }
13601370            else
13611371                /* S Flag is set - Write results to register & update CPSR (which was already handled using HandleALU flag macros) */
1362                SET_REGISTER(rdn,rd);
1372                SET_REGISTER(rdn, rd);
13631373        }
13641374    }
1365    //SJE: Don't think this applies any more.. (see page 44 at bottom)
1375    // SJE: Don't think this applies any more.. (see page 44 at bottom)
13661376    /* TST & TEQ can affect R15 (the condition code register) with the S bit set */
1367    else if (rdn==eR15)
1377    else if (rdn == eR15)
13681378    {
13691379        if (insn & INSN_S) {
1370            #if ARM7_DEBUG_CORE
1371                LOG(("%08x: TST class on R15 s bit set\n",R15));
1372            #endif
1380#if ARM7_DEBUG_CORE
1381                LOG(("%08x: TST class on R15 s bit set\n", R15));
1382#endif
13731383            R15 = rd;
13741384
13751385            /* IRQ masks may have changed in this instruction */
r1686r1687
13771387        }
13781388        else
13791389        {
1380            #if ARM7_DEBUG_CORE
1381                LOG(("%08x: TST class on R15 no s bit set\n",R15));
1382            #endif
1390#if ARM7_DEBUG_CORE
1391                LOG(("%08x: TST class on R15 no s bit set\n", R15));
1392#endif
13831393        }
13841394    }
13851395
13861396    if (oldR15 != R15)
1387   change_pc(R15);
1397        change_pc(R15);
13881398}
13891399
1390static void HandleMul( UINT32 insn)
1400static void HandleMul(UINT32 insn)
13911401{
13921402    UINT32 r;
13931403
13941404    /* Do the basic multiply of Rm and Rs */
1395    r = GET_REGISTER( insn&INSN_MUL_RM ) *
1396        GET_REGISTER( (insn&INSN_MUL_RS)>>INSN_MUL_RS_SHIFT );
1405    r = GET_REGISTER(insn & INSN_MUL_RM) *
1406        GET_REGISTER((insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT);
13971407
1398    #if ARM7_DEBUG_CORE
1399    if(
1400        ((insn&INSN_MUL_RM)==0xf) ||
1401        (((insn&INSN_MUL_RS)>>INSN_MUL_RS_SHIFT )==0xf) ||
1402        (((insn&INSN_MUL_RN)>>INSN_MUL_RN_SHIFT)==0xf)
1403       )
1404        LOG(("%08x:  R15 used in mult\n",R15));
1405    #endif
1408#if ARM7_DEBUG_CORE
1409    if ((insn & INSN_MUL_RM) == 0xf ||
1410        ((insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT) == 0xf ||
1411        ((insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT) == 0xf)
1412        LOG(("%08x:  R15 used in mult\n", R15));
1413#endif
14061414
14071415    /* Add on Rn if this is a MLA */
14081416    if (insn & INSN_MUL_A)
14091417    {
1410        r += GET_REGISTER((insn&INSN_MUL_RN)>>INSN_MUL_RN_SHIFT);
1418        r += GET_REGISTER((insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT);
14111419    }
14121420
14131421    /* Write the result */
1414    SET_REGISTER((insn&INSN_MUL_RD)>>INSN_MUL_RD_SHIFT,r);
1422    SET_REGISTER((insn & INSN_MUL_RD) >> INSN_MUL_RD_SHIFT, r);
14151423
14161424    /* Set N and Z if asked */
1417    if( insn & INSN_S )
1425    if (insn & INSN_S)
14181426    {
1419        SET_CPSR ( (GET_CPSR &~ (N_MASK | Z_MASK)) | HandleALUNZFlags(r));
1427        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleALUNZFlags(r));
14201428    }
14211429}
14221430
1423//todo: add proper cycle counts
1424static void HandleSMulLong( UINT32 insn)
1431// todo: add proper cycle counts
1432static void HandleSMulLong(UINT32 insn)
14251433{
14261434    INT32 rm, rs;
1427    UINT32 rhi,rlo;
1428    INT64 res=0;
1435    UINT32 rhi, rlo;
1436    INT64 res = 0;
14291437
1430    rm  = (INT32)GET_REGISTER(insn&0xf);
1431    rs  = (INT32)GET_REGISTER(((insn>>8)&0xf));
1432    rhi = (insn>>16)&0xf;
1433    rlo = (insn>>12)&0xf;
1438    rm  = (INT32)GET_REGISTER(insn & 0xf);
1439    rs  = (INT32)GET_REGISTER(((insn >> 8) & 0xf));
1440    rhi = (insn >> 16) & 0xf;
1441    rlo = (insn >> 12) & 0xf;
14341442
1435    #if ARM7_DEBUG_CORE
1436        if( ((insn&0xf) == 15) || (((insn>>8)&0xf) == 15) || (((insn>>16)&0xf) == 15) || (((insn>>12)&0xf) == 15)
1437            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n",R15));
1438    #endif
1443#if ARM7_DEBUG_CORE
1444        if ((insn & 0xf) == 15 || ((insn >> 8) & 0xf) == 15 || ((insn >> 16) & 0xf) == 15 || ((insn >> 12) & 0xf) == 15)
1445            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1446#endif
14391447
14401448    /* Perform the multiplication */
14411449    res = (INT64)rm * rs;
r1686r1687
14431451    /* Add on Rn if this is a MLA */
14441452    if (insn & INSN_MUL_A)
14451453    {
1446        INT64 acum = (INT64)((((INT64)(GET_REGISTER(rhi)))<<32) | GET_REGISTER(rlo));
1454        INT64 acum = (INT64)((((INT64)(GET_REGISTER(rhi))) << 32) | GET_REGISTER(rlo));
14471455        res += acum;
14481456    }
14491457
14501458    /* Write the result (upper dword goes to RHi, lower to RLo) */
1451    SET_REGISTER(rhi, res>>32);
1459    SET_REGISTER(rhi, res >> 32);
14521460    SET_REGISTER(rlo, res & 0xFFFFFFFF);
14531461
14541462    /* Set N and Z if asked */
1455    if( insn & INSN_S )
1463    if (insn & INSN_S)
14561464    {
1457        SET_CPSR ( (GET_CPSR &~ (N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1465        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
14581466    }
14591467}
14601468
1461//todo: add proper cycle counts
1462static void HandleUMulLong( UINT32 insn)
1469// todo: add proper cycle counts
1470static void HandleUMulLong(UINT32 insn)
14631471{
14641472    UINT32 rm, rs;
1465    UINT32 rhi,rlo;
1466    UINT64 res=0;
1473    UINT32 rhi, rlo;
1474    UINT64 res = 0;
14671475
1468    rm  = (INT32)GET_REGISTER(insn&0xf);
1469    rs  = (INT32)GET_REGISTER(((insn>>8)&0xf));
1470    rhi = (insn>>16)&0xf;
1471    rlo = (insn>>12)&0xf;
1476    rm  = (INT32)GET_REGISTER(insn & 0xf);
1477    rs  = (INT32)GET_REGISTER(((insn >> 8) & 0xf));
1478    rhi = (insn >> 16) & 0xf;
1479    rlo = (insn >> 12) & 0xf;
14721480
1473    #if ARM7_DEBUG_CORE
1474        if( ((insn&0xf) == 15) || (((insn>>8)&0xf) == 15) || (((insn>>16)&0xf) == 15) || (((insn>>12)&0xf) == 15)
1475            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n",R15));
1476    #endif
1481#if ARM7_DEBUG_CORE
1482        if (((insn & 0xf) == 15) || (((insn >> 8) & 0xf) == 15) || (((insn >> 16) & 0xf) == 15) || (((insn >> 12) & 0xf) == 15)
1483            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1484#endif
14771485
14781486    /* Perform the multiplication */
14791487    res = (UINT64)rm * rs;
r1686r1687
14811489    /* Add on Rn if this is a MLA */
14821490    if (insn & INSN_MUL_A)
14831491    {
1484        UINT64 acum = (UINT64)((((UINT64)(GET_REGISTER(rhi)))<<32) | GET_REGISTER(rlo));
1492        UINT64 acum = (UINT64)((((UINT64)(GET_REGISTER(rhi))) << 32) | GET_REGISTER(rlo));
14851493        res += acum;
14861494    }
14871495
14881496    /* Write the result (upper dword goes to RHi, lower to RLo) */
1489    SET_REGISTER(rhi, res>>32);
1497    SET_REGISTER(rhi, res >> 32);
14901498    SET_REGISTER(rlo, res & 0xFFFFFFFF);
14911499
14921500    /* Set N and Z if asked */
1493    if( insn & INSN_S )
1501    if (insn & INSN_S)
14941502    {
1495        SET_CPSR ( (GET_CPSR &~ (N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1503        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
14961504    }
14971505}
14981506
1499static void HandleMemBlock( UINT32 insn)
1507static void HandleMemBlock(UINT32 insn)
15001508{
15011509    UINT32 rb = (insn & INSN_RN) >> INSN_RN_SHIFT;
15021510    UINT32 rbp = GET_REGISTER(rb);
r1686r1687
15041512    int result;
15051513
15061514#if ARM7_DEBUG_CORE
1507    if(rbp & 3)
1508        LOG(("%08x: Unaligned Mem Transfer @ %08x\n",R15,rbp));
1515    if (rbp & 3)
1516        LOG(("%08x: Unaligned Mem Transfer @ %08x\n", R15, rbp));
15091517#endif
15101518
1511    //We will specify the cycle count for each case, so remove the -3 that occurs at the end
1512    ARM7_ICOUNT +=3;
1519    // We will specify the cycle count for each case, so remove the -3 that occurs at the end
1520    ARM7_ICOUNT += 3;
15131521
15141522    if (insn & INSN_BDT_L)
15151523    {
r1686r1687
15221530                rbp = rbp + (- 4);
15231531            }
15241532
1525            //S Flag Set, but R15 not in list = User Bank Transfer
1526            if(insn & INSN_BDT_S && ((insn & 0x8000)==0))
1533            // S Flag Set, but R15 not in list = User Bank Transfer
1534            if (insn & INSN_BDT_S && (insn & 0x8000) == 0)
15271535            {
1528                //set to user mode - then do the transfer, and set back
1536                // set to user mode - then do the transfer, and set back
15291537                int curmode = GET_MODE;
15301538                SwitchMode(eARM7_MODE_USER);
1531                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n",R15));
1532                result = loadInc( insn & 0xffff, rbp, insn&INSN_BDT_S );
1533                //todo - not sure if Writeback occurs on User registers also..
1539                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1540                result = loadInc(insn & 0xffff, rbp, insn & INSN_BDT_S);
1541                // todo - not sure if Writeback occurs on User registers also..
15341542                SwitchMode(curmode);
15351543            }
15361544            else
1537                result = loadInc( insn & 0xffff, rbp, insn&INSN_BDT_S );
1545                result = loadInc(insn & 0xffff, rbp, insn & INSN_BDT_S);
15381546
15391547            if (insn & INSN_BDT_W)
15401548            {
1541                #if ARM7_DEBUG_CORE
1542                    if(rb==15)
1543                        LOG(("%08x:  Illegal LDRM writeback to r15\n",R15));
1544                #endif
1545                SET_REGISTER(rb,GET_REGISTER(rb)+result*4);
1549#if ARM7_DEBUG_CORE
1550                    if (rb == 15)
1551                        LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1552#endif
1553                SET_REGISTER(rb, GET_REGISTER(rb) + result * 4);
15461554            }
15471555
1548            //R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1556            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
15491557            if (insn & 0x8000) {
1550                R15-=4;     //SJE: I forget why i did this?
1551                //S - Flag Set? Signals transfer of current mode SPSR->CPSR
1552                if(insn & INSN_BDT_S) {
1558                R15 -= 4;     // SJE: I forget why i did this?
1559                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1560                if (insn & INSN_BDT_S) {
15531561                    SET_CPSR(GET_REGISTER(SPSR));
15541562                    SwitchMode(GET_MODE);
15551563                }
15561564            }
1557            //LDM PC - takes 1 extra cycle
1558            ARM7_ICOUNT -=1;
1565            // LDM PC - takes 1 extra cycle
1566            ARM7_ICOUNT -= 1;
15591567        }
15601568        else
15611569        {
r1686r1687
15651573                rbp = rbp - (- 4);
15661574            }
15671575
1568            //S Flag Set, but R15 not in list = User Bank Transfer
1569            if(insn & INSN_BDT_S && ((insn & 0x8000)==0))
1576            // S Flag Set, but R15 not in list = User Bank Transfer
1577            if (insn & INSN_BDT_S && ((insn & 0x8000) == 0))
15701578            {
1571                //set to user mode - then do the transfer, and set back
1579                // set to user mode - then do the transfer, and set back
15721580                int curmode = GET_MODE;
15731581                SwitchMode(eARM7_MODE_USER);
1574                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n",R15));
1575                result = loadDec( insn&0xffff, rbp, insn&INSN_BDT_S );
1576                //todo - not sure if Writeback occurs on User registers also..
1582                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1583                result = loadDec(insn & 0xffff, rbp, insn & INSN_BDT_S);
1584                // todo - not sure if Writeback occurs on User registers also..
15771585                SwitchMode(curmode);
15781586            }
15791587            else
1580                result = loadDec( insn&0xffff, rbp, insn&INSN_BDT_S );
1588                result = loadDec(insn & 0xffff, rbp, insn & INSN_BDT_S);
15811589
15821590            if (insn & INSN_BDT_W)
15831591            {
1584                if (rb==0xf)
1585                    LOG(("%08x:  Illegal LDRM writeback to r15\n",R15));
1586                SET_REGISTER(rb,GET_REGISTER(rb)-result*4);
1592                if (rb == 0xf)
1593                    LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1594                SET_REGISTER(rb, GET_REGISTER(rb)-result*4);
15871595            }
15881596
1589            //R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1597            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
15901598            if (insn & 0x8000) {
1591                R15-=4;     //SJE: I forget why i did this?
1592                //S - Flag Set? Signals transfer of current mode SPSR->CPSR
1593                if(insn & INSN_BDT_S) {
1599                R15 -= 4;     // SJE: I forget why i did this?
1600                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1601                if (insn & INSN_BDT_S) {
15941602                    SET_CPSR(GET_REGISTER(SPSR));
15951603                    SwitchMode(GET_MODE);
15961604                }
1597                //LDM PC - takes 1 extra cycle
1598                ARM7_ICOUNT -=1;
1605                // LDM PC - takes 1 extra cycle
1606                ARM7_ICOUNT -= 1;
15991607            }
16001608
1601            //LDM (NO PC) takes nS + 1n + 1I cycles (n = # of register transfers)
1602            ARM7_ICOUNT -= (result+1+1);
1609            // LDM (NO PC) takes nS + 1n + 1I cycles (n = # of register transfers)
1610            ARM7_ICOUNT -= result + 1 + 1;
16031611        }
16041612    } /* Loading */
16051613    else
16061614    {
16071615        /* Storing */
1608        if (insn & (1<<eR15))
1616        if (insn & (1 << eR15))
16091617        {
1610            #if ARM7_DEBUG_CORE
1611                LOG(("%08x: Writing R15 in strm\n",R15));
1612            #endif
1618#if ARM7_DEBUG_CORE
1619                LOG(("%08x: Writing R15 in strm\n", R15));
1620#endif
16131621            /* special case handling if writing to PC */
16141622            R15 += 12;
16151623        }
r1686r1687
16211629                rbp = rbp + (- 4);
16221630            }
16231631
1624            //S Flag Set, but R15 not in list = User Bank Transfer
1625            if(insn & INSN_BDT_S && ((insn & 0x8000)==0))
1632            // S Flag Set, but R15 not in list = User Bank Transfer
1633            if (insn & INSN_BDT_S && (insn & 0x8000) == 0)
16261634            {
1627                //todo: needs to be tested..
1635                // todo: needs to be tested..
16281636
1629                //set to user mode - then do the transfer, and set back
1637                // set to user mode - then do the transfer, and set back
16301638                int curmode = GET_MODE;
16311639                SwitchMode(eARM7_MODE_USER);
1632                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n",R15));
1633                result = storeInc( insn&0xffff, rbp );
1634                //todo - not sure if Writeback occurs on User registers also..
1640                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1641                result = storeInc(insn & 0xffff, rbp);
1642                // todo - not sure if Writeback occurs on User registers also..
16351643                SwitchMode(curmode);
16361644            }
16371645            else
1638                result = storeInc( insn&0xffff, rbp );
1646                result = storeInc(insn & 0xffff, rbp);
16391647
1640            if( insn & INSN_BDT_W )
1648            if (insn & INSN_BDT_W)
16411649            {
1642                SET_REGISTER(rb,GET_REGISTER(rb)+result*4);
1650                SET_REGISTER(rb, GET_REGISTER(rb) + result * 4);
16431651            }
16441652        }
16451653        else
r1686r1687
16471655            /* Decrementing */
16481656            if (!(insn & INSN_BDT_P))
16491657            {
1650                rbp = rbp - (- 4);
1658                rbp = rbp - (-4);
16511659            }
16521660
1653            //S Flag Set, but R15 not in list = User Bank Transfer
1654            if(insn & INSN_BDT_S && ((insn & 0x8000)==0))
1661            // S Flag Set, but R15 not in list = User Bank Transfer
1662            if (insn & INSN_BDT_S && (insn & 0x8000) == 0)
16551663            {
1656                //set to user mode - then do the transfer, and set back
1664                // set to user mode - then do the transfer, and set back
16571665                int curmode = GET_MODE;
16581666                SwitchMode(eARM7_MODE_USER);
1659                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n",R15));
1660                result = storeDec( insn&0xffff, rbp );
1661                //todo - not sure if Writeback occurs on User registers also..
1667                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1668                result = storeDec(insn & 0xffff, rbp);
1669                // todo - not sure if Writeback occurs on User registers also..
16621670                SwitchMode(curmode);
16631671            }
16641672            else
1665                result = storeDec( insn&0xffff, rbp );
1673                result = storeDec(insn & 0xffff, rbp);
16661674
1667            if( insn & INSN_BDT_W )
1675            if (insn & INSN_BDT_W)
16681676            {
1669                SET_REGISTER(rb,GET_REGISTER(rb)-result*4);
1677                SET_REGISTER(rb, GET_REGISTER(rb) - result * 4);
16701678            }
16711679        }
1672        if( insn & (1<<eR15) )
1680        if (insn & (1 << eR15))
16731681            R15 -= 12;
16741682
1675        //STM takes (n+1)S+2N+1I cycles (n = # of register transfers)
1676        ARM7_ICOUNT -= ((result+1)+2+1);
1683        // STM takes (n+1)S+2N+1I cycles (n = # of register transfers)
1684        ARM7_ICOUNT -= (result + 1) + 2 + 1;
16771685    }
16781686
16791687    if (oldR15 != R15)
1680   change_pc(R15);
1688        change_pc(R15);
16811689} /* HandleMemBlock */
1682
1683
src/emu/cpu/arm7/arm7core.h
r1686r1687
2828
2929 ******************************************************************************/
3030
31
32
3331#ifndef ARM7CORE_H
3432#define ARM7CORE_H
3533
r1686r1687
4442    ARM7_ABORT_EXCEPTION, ARM7_ABORT_PREFETCH_EXCEPTION, ARM7_UNDEFINE_EXCEPTION,
4543    ARM7_NUM_LINES
4644};
47//Really there's only 1 ABORT Line.. and cpu decides whether it's during data fetch or prefetch, but we let the user specify
45// Really there's only 1 ABORT Line.. and cpu decides whether it's during data fetch or prefetch, but we let the user specify
4846
4947/****************************************************************************************************
5048 *  ARM7 CORE REGISTERS
5149 ***************************************************************************************************/
5250enum
5351{
54    ARM7_PC=0,
52    ARM7_PC = 0,
5553    ARM7_R0, ARM7_R1, ARM7_R2, ARM7_R3, ARM7_R4, ARM7_R5, ARM7_R6, ARM7_R7,
5654    ARM7_R8, ARM7_R9, ARM7_R10, ARM7_R11, ARM7_R12, ARM7_R13, ARM7_R14, ARM7_R15,
5755    ARM7_FR8, ARM7_FR9, ARM7_FR10, ARM7_FR11, ARM7_FR12, ARM7_FR13, ARM7_FR14,
r1686r1687
5957    ARM7_CPSR, ARM7_AR13, ARM7_AR14, ARM7_ASPSR, ARM7_UR13, ARM7_UR14, ARM7_USPSR
6058};
6159
62#define ARM7CORE_REGS \
60#define ARM7CORE_REGS                   \
6361    UINT32 sArmRegister[kNumRegisters]; \
64    UINT8 pendingIrq; \
65    UINT8 pendingFiq; \
66    UINT8 pendingAbtD; \
67    UINT8 pendingAbtP; \
68    UINT8 pendingUnd; \
69    UINT8 pendingSwi; \
62    UINT8 pendingIrq;                   \
63    UINT8 pendingFiq;                   \
64    UINT8 pendingAbtD;                  \
65    UINT8 pendingAbtP;                  \
66    UINT8 pendingUnd;                   \
67    UINT8 pendingSwi;                   \
7068    int (*irq_callback)(int);
7169
7270
r1686r1687
9391enum
9492{
9593    /* All modes have the following */
96    eR0=0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
97    eR8,eR9,eR10,eR11,eR12,
94    eR0 = 0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
95    eR8, eR9, eR10, eR11, eR12,
9896    eR13, /* Stack Pointer */
9997    eR14, /* Link Register (holds return address) */
10098    eR15, /* Program Counter */
10199    eCPSR, /* Current Status Program Register */
102100
103101    /* Fast Interrupt - Bank switched registers */
104    eR8_FIQ,eR9_FIQ,eR10_FIQ,eR11_FIQ,eR12_FIQ,eR13_FIQ,eR14_FIQ,eSPSR_FIQ,
102    eR8_FIQ, eR9_FIQ, eR10_FIQ, eR11_FIQ, eR12_FIQ, eR13_FIQ, eR14_FIQ, eSPSR_FIQ,
105103
106    /* IRQ - Bank switched registers*/
107    eR13_IRQ,eR14_IRQ,eSPSR_IRQ,
104    /* IRQ - Bank switched registers */
105    eR13_IRQ, eR14_IRQ, eSPSR_IRQ,
108106
109    /* Supervisor/Service Mode - Bank switched registers*/
110    eR13_SVC,eR14_SVC,eSPSR_SVC,
107    /* Supervisor/Service Mode - Bank switched registers */
108    eR13_SVC, eR14_SVC, eSPSR_SVC,
111109
112    /* Abort Mode - Bank switched registers*/
113    eR13_ABT,eR14_ABT,eSPSR_ABT,
110    /* Abort Mode - Bank switched registers */
111    eR13_ABT, eR14_ABT, eSPSR_ABT,
114112
115    /* Undefined Mode - Bank switched registers*/
116    eR13_UND,eR14_UND,eSPSR_UND,
113    /* Undefined Mode - Bank switched registers */
114    eR13_UND, eR14_UND, eSPSR_UND,
117115
118116    kNumRegisters
119117};
r1686r1687
145143static const int sRegisterTable[ARM7_NUM_MODES][18] =
146144{
147145    { /* USR */
148        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
149        eR8,eR9,eR10,eR11,eR12,
150        eR13,eR14,
151        eR15,eCPSR  //No SPSR in this mode
146        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
147        eR8, eR9, eR10, eR11, eR12,
148        eR13, eR14,
149        eR15, eCPSR  // No SPSR in this mode
152150    },
153151    { /* FIQ */
154        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
155        eR8_FIQ,eR9_FIQ,eR10_FIQ,eR11_FIQ,eR12_FIQ,
156        eR13_FIQ,eR14_FIQ,
157        eR15,eCPSR,eSPSR_FIQ
152        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
153        eR8_FIQ, eR9_FIQ, eR10_FIQ, eR11_FIQ, eR12_FIQ,
154        eR13_FIQ, eR14_FIQ,
155        eR15, eCPSR, eSPSR_FIQ
158156    },
159157    { /* IRQ */
160        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
161        eR8,eR9,eR10,eR11,eR12,
162        eR13_IRQ,eR14_IRQ,
163        eR15,eCPSR,eSPSR_IRQ
158        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
159        eR8, eR9, eR10, eR11, eR12,
160        eR13_IRQ, eR14_IRQ,
161        eR15, eCPSR, eSPSR_IRQ
164162    },
165163    { /* SVC */
166        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
167        eR8,eR9,eR10,eR11,eR12,
168        eR13_SVC,eR14_SVC,
169        eR15,eCPSR,eSPSR_SVC
164        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
165        eR8, eR9, eR10, eR11, eR12,
166        eR13_SVC, eR14_SVC,
167        eR15, eCPSR, eSPSR_SVC
170168    },
171    {0},{0},{0},        //values for modes 4,5,6 are not valid
169    {0}, {0}, {0},        // values for modes 4,5,6 are not valid
172170    { /* ABT */
173        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
174        eR8,eR9,eR10,eR11,eR12,
175        eR13_ABT,eR14_ABT,
176        eR15,eCPSR,eSPSR_ABT
171        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
172        eR8, eR9, eR10, eR11, eR12,
173        eR13_ABT, eR14_ABT,
174        eR15, eCPSR, eSPSR_ABT
177175    },
178    {0},{0},{0},        //values for modes 8,9,a are not valid!
176    {0}, {0}, {0},        // values for modes 8,9,a are not valid!
179177    { /* UND */
180        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
181        eR8,eR9,eR10,eR11,eR12,
182        eR13_UND,eR14_UND,
183        eR15,eCPSR,eSPSR_UND
178        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
179        eR8, eR9, eR10, eR11, eR12,
180        eR13_UND, eR14_UND,
181        eR15, eCPSR, eSPSR_UND
184182    },
185    {0},{0},{0},        //values for modes c,d,e are not valid!
183    {0}, {0}, {0},        // values for modes c,d, e are not valid!
186184    { /* SYS */
187        eR0,eR1,eR2,eR3,eR4,eR5,eR6,eR7,
188        eR8,eR9,eR10,eR11,eR12,
189        eR13,eR14,
190        eR15,eCPSR  //No SPSR in this mode
185        eR0, eR1, eR2, eR3, eR4, eR5, eR6, eR7,
186        eR8, eR9, eR10, eR11, eR12,
187        eR13, eR14,
188        eR15, eCPSR  // No SPSR in this mode
191189    }
192190};
193191
r1686r1687
197195#define V_BIT   28
198196#define I_BIT   7
199197#define F_BIT   6
200#define T_BIT   5   //Thumb mode
198#define T_BIT   5   // Thumb mode
201199
202#define N_MASK  ((UINT32)(1<<N_BIT)) /* Negative flag */
203#define Z_MASK  ((UINT32)(1<<Z_BIT)) /* Zero flag */
204#define C_MASK  ((UINT32)(1<<C_BIT)) /* Carry flag */
205#define V_MASK  ((UINT32)(1<<V_BIT)) /* oVerflow flag */
206#define I_MASK  ((UINT32)(1<<I_BIT)) /* Interrupt request disable */
207#define F_MASK  ((UINT32)(1<<F_BIT)) /* Fast interrupt request disable */
208#define T_MASK  ((UINT32)(1<<T_BIT)) /* Thumb Mode flag */
200#define N_MASK  ((UINT32)(1 << N_BIT)) /* Negative flag */
201#define Z_MASK  ((UINT32)(1 << Z_BIT)) /* Zero flag */
202#define C_MASK  ((UINT32)(1 << C_BIT)) /* Carry flag */
203#define V_MASK  ((UINT32)(1 << V_BIT)) /* oVerflow flag */
204#define I_MASK  ((UINT32)(1 << I_BIT)) /* Interrupt request disable */
205#define F_MASK  ((UINT32)(1 << F_BIT)) /* Fast interrupt request disable */
206#define T_MASK  ((UINT32)(1 << T_BIT)) /* Thumb Mode flag */
209207
210208#define N_IS_SET(pc)    ((pc) & N_MASK)
211209#define Z_IS_SET(pc)    ((pc) & Z_MASK)
r1686r1687
224222#define T_IS_CLEAR(pc)  (!T_IS_SET(pc))
225223
226224/* Deconstructing an instruction */
227//todo: use these in all places (including dasm file)
228#define INSN_COND           ((UINT32) 0xf0000000u)
229#define INSN_SDT_L          ((UINT32) 0x00100000u)
230#define INSN_SDT_W          ((UINT32) 0x00200000u)
231#define INSN_SDT_B          ((UINT32) 0x00400000u)
232#define INSN_SDT_U          ((UINT32) 0x00800000u)
233#define INSN_SDT_P          ((UINT32) 0x01000000u)
234#define INSN_BDT_L          ((UINT32) 0x00100000u)
235#define INSN_BDT_W          ((UINT32) 0x00200000u)
236#define INSN_BDT_S          ((UINT32) 0x00400000u)
237#define INSN_BDT_U          ((UINT32) 0x00800000u)
238#define INSN_BDT_P          ((UINT32) 0x01000000u)
239#define INSN_BDT_REGS       ((UINT32) 0x0000ffffu)
240#define INSN_SDT_IMM        ((UINT32) 0x00000fffu)
241#define INSN_MUL_A          ((UINT32) 0x00200000u)
242#define INSN_MUL_RM         ((UINT32) 0x0000000fu)
243#define INSN_MUL_RS         ((UINT32) 0x00000f00u)
244#define INSN_MUL_RN         ((UINT32) 0x0000f000u)
245#define INSN_MUL_RD         ((UINT32) 0x000f0000u)
246#define INSN_I              ((UINT32) 0x02000000u)
247#define INSN_OPCODE         ((UINT32) 0x01e00000u)
248#define INSN_S              ((UINT32) 0x00100000u)
249#define INSN_BL             ((UINT32) 0x01000000u)
250#define INSN_BRANCH         ((UINT32) 0x00ffffffu)
251#define INSN_SWI            ((UINT32) 0x00ffffffu)
252#define INSN_RN             ((UINT32) 0x000f0000u)
253#define INSN_RD             ((UINT32) 0x0000f000u)
254#define INSN_OP2            ((UINT32) 0x00000fffu)
255#define INSN_OP2_SHIFT      ((UINT32) 0x00000f80u)
256#define INSN_OP2_SHIFT_TYPE ((UINT32) 0x00000070u)
257#define INSN_OP2_RM         ((UINT32) 0x0000000fu)
258#define INSN_OP2_ROTATE     ((UINT32) 0x00000f00u)
259#define INSN_OP2_IMM        ((UINT32) 0x000000ffu)
225// todo: use these in all places (including dasm file)
226#define INSN_COND           ((UINT32)0xf0000000u)
227#define INSN_SDT_L          ((UINT32)0x00100000u)
228#define INSN_SDT_W          ((UINT32)0x00200000u)
229#define INSN_SDT_B          ((UINT32)0x00400000u)
230#define INSN_SDT_U          ((UINT32)0x00800000u)
231#define INSN_SDT_P          ((UINT32)0x01000000u)
232#define INSN_BDT_L          ((UINT32)0x00100000u)
233#define INSN_BDT_W          ((UINT32)0x00200000u)
234#define INSN_BDT_S          ((UINT32)0x00400000u)
235#define INSN_BDT_U          ((UINT32)0x00800000u)
236#define INSN_BDT_P          ((UINT32)0x01000000u)
237#define INSN_BDT_REGS       ((UINT32)0x0000ffffu)
238#define INSN_SDT_IMM        ((UINT32)0x00000fffu)
239#define INSN_MUL_A          ((UINT32)0x00200000u)
240#define INSN_MUL_RM         ((UINT32)0x0000000fu)
241#define INSN_MUL_RS         ((UINT32)0x00000f00u)
242#define INSN_MUL_RN         ((UINT32)0x0000f000u)
243#define INSN_MUL_RD         ((UINT32)0x000f0000u)
244#define INSN_I              ((UINT32)0x02000000u)
245#define INSN_OPCODE         ((UINT32)0x01e00000u)
246#define INSN_S              ((UINT32)0x00100000u)
247#define INSN_BL             ((UINT32)0x01000000u)
248#define INSN_BRANCH         ((UINT32)0x00ffffffu)
249#define INSN_SWI            ((UINT32)0x00ffffffu)
250#define INSN_RN             ((UINT32)0x000f0000u)
251#define INSN_RD             ((UINT32)0x0000f000u)
252#define INSN_OP2            ((UINT32)0x00000fffu)
253#define INSN_OP2_SHIFT      ((UINT32)0x00000f80u)
254#define INSN_OP2_SHIFT_TYPE ((UINT32)0x00000070u)
255#define INSN_OP2_RM         ((UINT32)0x0000000fu)
256#define INSN_OP2_ROTATE     ((UINT32)0x00000f00u)
257#define INSN_OP2_IMM        ((UINT32)0x000000ffu)
260258#define INSN_OP2_SHIFT_TYPE_SHIFT   4
261259#define INSN_OP2_SHIFT_SHIFT        7
262260#define INSN_OP2_ROTATE_SHIFT       8
r1686r1687
268266#define INSN_RD_SHIFT               12
269267#define INSN_COND_SHIFT             28
270268
271#define THUMB_INSN_TYPE      ((UINT16) 0xf000)
272#define THUMB_COND_TYPE      ((UINT16) 0x0f00)
273#define THUMB_GROUP4_TYPE   ((UINT16) 0x0c00)
274#define THUMB_GROUP5_TYPE   ((UINT16) 0x0e00)
275#define THUMB_GROUP5_RM      ((UINT16) 0x01c0)
276#define THUMB_GROUP5_RN      ((UINT16) 0x0038)
277#define THUMB_GROUP5_RD      ((UINT16) 0x0007)
278#define THUMB_ADDSUB_RNIMM   ((UINT16) 0x01c0)
279#define THUMB_ADDSUB_RS      ((UINT16) 0x0038)
280#define THUMB_ADDSUB_RD      ((UINT16) 0x0007)
281#define THUMB_INSN_ADDSUB   ((UINT16) 0x0800)
282#define THUMB_INSN_CMP      ((UINT16) 0x0800)
283#define THUMB_INSN_SUB      ((UINT16) 0x0800)
284#define THUMB_INSN_IMM_RD   ((UINT16) 0x0700)
285#define THUMB_INSN_IMM_S   ((UINT16) 0x0080)
286#define THUMB_INSN_IMM      ((UINT16) 0x00ff)
287#define THUMB_ADDSUB_TYPE   ((UINT16) 0x0600)
288#define THUMB_HIREG_OP      ((UINT16) 0x0300)
289#define THUMB_HIREG_H      ((UINT16) 0x00c0)
290#define THUMB_HIREG_RS      ((UINT16) 0x0038)
291#define THUMB_HIREG_RD      ((UINT16) 0x0007)
292#define THUMB_STACKOP_TYPE   ((UINT16) 0x0f00)
293#define THUMB_STACKOP_L      ((UINT16) 0x0800)
294#define THUMB_STACKOP_RD   ((UINT16) 0x0700)
295#define THUMB_ALUOP_TYPE   ((UINT16) 0x03c0)
296#define THUMB_BLOP_LO      ((UINT16) 0x0800)
297#define THUMB_BLOP_OFFS      ((UINT16) 0x07ff)
298#define THUMB_SHIFT_R      ((UINT16) 0x0800)
299#define THUMB_SHIFT_AMT      ((UINT16) 0x07c0)
300#define THUMB_HALFOP_L      ((UINT16) 0x0800)
301#define THUMB_HALFOP_OFFS   ((UINT16) 0x07c0)
302#define THUMB_BRANCH_OFFS   ((UINT16) 0x07ff)
303#define THUMB_LSOP_L      ((UINT16) 0x0800)
304#define THUMB_LSOP_OFFS      ((UINT16) 0x07c0)
305#define THUMB_MULTLS      ((UINT16) 0x0800)
306#define THUMB_MULTLS_BASE   ((UINT16) 0x0700)
307#define THUMB_RELADDR_SP   ((UINT16) 0x0800)
308#define THUMB_RELADDR_RD   ((UINT16) 0x0700)
309#define THUMB_INSN_TYPE_SHIFT      12
310#define THUMB_COND_TYPE_SHIFT      8
311#define THUMB_GROUP4_TYPE_SHIFT      10
312#define THUMB_GROUP5_TYPE_SHIFT      9
313#define THUMB_ADDSUB_TYPE_SHIFT      9
314#define THUMB_INSN_IMM_RD_SHIFT      8
315#define THUMB_STACKOP_TYPE_SHIFT   8
316#define THUMB_HIREG_OP_SHIFT      8
317#define THUMB_STACKOP_RD_SHIFT      8
318#define THUMB_MULTLS_BASE_SHIFT      8
319#define THUMB_RELADDR_RD_SHIFT      8
320#define THUMB_HIREG_H_SHIFT         6
321#define THUMB_HIREG_RS_SHIFT      3
322#define THUMB_ALUOP_TYPE_SHIFT      6
323#define THUMB_SHIFT_AMT_SHIFT      6
324#define THUMB_HALFOP_OFFS_SHIFT      6
325#define THUMB_LSOP_OFFS_SHIFT      6
326#define THUMB_GROUP5_RM_SHIFT      6
327#define THUMB_GROUP5_RN_SHIFT      3
328#define THUMB_GROUP5_RD_SHIFT      0
329#define THUMB_ADDSUB_RNIMM_SHIFT   6
330#define THUMB_ADDSUB_RS_SHIFT      3
331#define THUMB_ADDSUB_RD_SHIFT      0
269#define THUMB_INSN_TYPE     ((UINT16)0xf000)
270#define THUMB_COND_TYPE     ((UINT16)0x0f00)
271#define THUMB_GROUP4_TYPE   ((UINT16)0x0c00)
272#define THUMB_GROUP5_TYPE   ((UINT16)0x0e00)
273#define THUMB_GROUP5_RM     ((UINT16)0x01c0)
274#define THUMB_GROUP5_RN     ((UINT16)0x0038)
275#define THUMB_GROUP5_RD     ((UINT16)0x0007)
276#define THUMB_ADDSUB_RNIMM  ((UINT16)0x01c0)
277#define THUMB_ADDSUB_RS     ((UINT16)0x0038)
278#define THUMB_ADDSUB_RD     ((UINT16)0x0007)
279#define THUMB_INSN_ADDSUB   ((UINT16)0x0800)
280#define THUMB_INSN_CMP      ((UINT16)0x0800)
281#define THUMB_INSN_SUB      ((UINT16)0x0800)
282#define THUMB_INSN_IMM_RD   ((UINT16)0x0700)
283#define THUMB_INSN_IMM_S    ((UINT16)0x0080)
284#define THUMB_INSN_IMM      ((UINT16)0x00ff)
285#define THUMB_ADDSUB_TYPE   ((UINT16)0x0600)
286#define THUMB_HIREG_OP      ((UINT16)0x0300)
287#define THUMB_HIREG_H       ((UINT16)0x00c0)
288#define THUMB_HIREG_RS      ((UINT16)0x0038)
289#define THUMB_HIREG_RD      ((UINT16)0x0007)
290#define THUMB_STACKOP_TYPE  ((UINT16)0x0f00)
291#define THUMB_STACKOP_L     ((UINT16)0x0800)
292#define THUMB_STACKOP_RD    ((UINT16)0x0700)
293#define THUMB_ALUOP_TYPE    ((UINT16)0x03c0)
294#define THUMB_BLOP_LO       ((UINT16)0x0800)
295#define THUMB_BLOP_OFFS     ((UINT16)0x07ff)
296#define THUMB_SHIFT_R       ((UINT16)0x0800)
297#define THUMB_SHIFT_AMT     ((UINT16)0x07c0)
298#define THUMB_HALFOP_L      ((UINT16)0x0800)
299#define THUMB_HALFOP_OFFS   ((UINT16)0x07c0)
300#define THUMB_BRANCH_OFFS   ((UINT16)0x07ff)
301#define THUMB_LSOP_L        ((UINT16)0x0800)
302#define THUMB_LSOP_OFFS     ((UINT16)0x07c0)
303#define THUMB_MULTLS        ((UINT16)0x0800)
304#define THUMB_MULTLS_BASE   ((UINT16)0x0700)
305#define THUMB_RELADDR_SP    ((UINT16)0x0800)
306#define THUMB_RELADDR_RD    ((UINT16)0x0700)
307#define THUMB_INSN_TYPE_SHIFT       12
308#define THUMB_COND_TYPE_SHIFT       8
309#define THUMB_GROUP4_TYPE_SHIFT     10
310#define THUMB_GROUP5_TYPE_SHIFT     9
311#define THUMB_ADDSUB_TYPE_SHIFT     9
312#define THUMB_INSN_IMM_RD_SHIFT     8
313#define THUMB_STACKOP_TYPE_SHIFT    8
314#define THUMB_HIREG_OP_SHIFT        8
315#define THUMB_STACKOP_RD_SHIFT      8
316#define THUMB_MULTLS_BASE_SHIFT     8
317#define THUMB_RELADDR_RD_SHIFT      8
318#define THUMB_HIREG_H_SHIFT         6
319#define THUMB_HIREG_RS_SHIFT        3
320#define THUMB_ALUOP_TYPE_SHIFT      6
321#define THUMB_SHIFT_AMT_SHIFT       6
322#define THUMB_HALFOP_OFFS_SHIFT     6
323#define THUMB_LSOP_OFFS_SHIFT       6
324#define THUMB_GROUP5_RM_SHIFT       6
325#define THUMB_GROUP5_RN_SHIFT       3
326#define THUMB_GROUP5_RD_SHIFT       0
327#define THUMB_ADDSUB_RNIMM_SHIFT    6
328#define THUMB_ADDSUB_RS_SHIFT       3
329#define THUMB_ADDSUB_RD_SHIFT       0
332330
333331enum
334332{
r1686r1687
352350
353351enum
354352{
355    COND_EQ = 0,    /* Z: equal */
356    COND_NE,        /* ~Z: not equal */
357    COND_CS, COND_HS = 2,   /* C: unsigned higher or same */
358    COND_CC, COND_LO = 3,   /* ~C: unsigned lower */
359    COND_MI,        /* N: negative */
360    COND_PL,        /* ~N: positive or zero */
361    COND_VS,        /* V: overflow */
362    COND_VC,        /* ~V: no overflow */
363    COND_HI,        /* C && ~Z: unsigned higher */
364    COND_LS,        /* ~C || Z: unsigned lower or same */
365    COND_GE,        /* N == V: greater or equal */
366    COND_LT,        /* N != V: less than */
367    COND_GT,        /* ~Z && (N == V): greater than */
368    COND_LE,        /* Z || (N != V): less than or equal */
369    COND_AL,        /* always */
370    COND_NV         /* never */
353    COND_EQ = 0,          /*  Z           equal                   */
354    COND_NE,              /* ~Z           not equal               */
355    COND_CS, COND_HS = 2, /*  C           unsigned higher or same */
356    COND_CC, COND_LO = 3, /* ~C           unsigned lower          */
357    COND_MI,              /*  N           negative                */
358    COND_PL,              /* ~N           positive or zero        */
359    COND_VS,              /*  V           overflow                */
360    COND_VC,              /* ~V           no overflow             */
361    COND_HI,              /*  C && ~Z     unsigned higher         */
362    COND_LS,              /* ~C ||  Z     unsigned lower or same  */
363    COND_GE,              /*  N == V      greater or equal        */
364    COND_LT,              /*  N != V      less than               */
365    COND_GT,              /* ~Z && N == V greater than            */
366    COND_LE,              /*  Z || N != V less than or equal      */
367    COND_AL,              /*  1           always                  */
368    COND_NV               /*  0           never                   */
371369};
372370
373#define LSL(v,s) ((v) << (s))
374#define LSR(v,s) ((v) >> (s))
375#define ROL(v,s) (LSL((v),(s)) | (LSR((v),32u - (s))))
376#define ROR(v,s) (LSR((v),(s)) | (LSL((v),32u - (s))))
371#define LSL(v, s) ((v) << (s))
372#define LSR(v, s) ((v) >> (s))
373#define ROL(v, s) (LSL((v), (s)) | (LSR((v), 32u - (s))))
374#define ROR(v, s) (LSR((v), (s)) | (LSL((v), 32u - (s))))
377375
378376/* Convenience Macros */
379377#define R15                     ARM7REG(eR15)
380#define SPSR                    17                                  //SPSR is always the 18th register in our 0 based array sRegisterTable[][18]
378#define SPSR                    17                     // SPSR is always the 18th register in our 0 based array sRegisterTable[][18]
381379#define GET_CPSR                ARM7REG(eCPSR)
382380#define SET_CPSR(v)             (GET_CPSR = (v))
383#define MODE_FLAG               0xF                                 //Mode bits are 4:0 of CPSR, but we ignore bit 4.
381#define MODE_FLAG               0xF                    // Mode bits are 4:0 of CPSR, but we ignore bit 4.
384382#define GET_MODE                (GET_CPSR & MODE_FLAG)
385#define SIGN_BIT                ((UINT32)(1<<31))
386#define SIGN_BITS_DIFFER(a,b)   (((a)^(b)) >> 31)
383#define SIGN_BIT                ((UINT32)(1 << 31))
384#define SIGN_BITS_DIFFER(a, b)  (((a) ^ (b)) >> 31)
387385/* I really don't know why these were set to 16-bit, the thumb registers are still 32-bit ... */
388#define THUMB_SIGN_BIT            ((UINT32)(1<<31))
389#define THUMB_SIGN_BITS_DIFFER(a,b)   (((a)^(b)) >> 31)
386#define THUMB_SIGN_BIT               ((UINT32)(1 << 31))
387#define THUMB_SIGN_BITS_DIFFER(a, b) (((a)^(b)) >> 31)
390388
391389/* At one point I thought these needed to be cpu implementation specific, but they don't.. */
392390#define GET_REGISTER(reg)       GetRegister(reg)
393#define SET_REGISTER(reg,val)  SetRegister(reg,val)
391#define SET_REGISTER(reg, val)  SetRegister(reg, val)
394392#define ARM7_CHECKIRQ           arm7_check_irq_state()
395393
396394extern WRITE32_HANDLER((*arm7_coproc_do_callback));
397395extern READ32_HANDLER((*arm7_coproc_rt_r_callback));
398396extern WRITE32_HANDLER((*arm7_coproc_rt_w_callback));
399extern void (*arm7_coproc_dt_r_callback)(UINT32 insn, UINT32* prn, UINT32 (*read32)(int addr));
400extern void (*arm7_coproc_dt_w_callback)(UINT32 insn, UINT32* prn, void (*write32)(int addr, UINT32 data));
397extern void (*arm7_coproc_dt_r_callback)(UINT32 insn, UINT32* prn, UINT32 (*read32)(UINT32 addr));
398extern void (*arm7_coproc_dt_w_callback)(UINT32 insn, UINT32* prn, void (*write32)(UINT32 addr, UINT32 data));
401399
402400#ifdef ENABLE_DEBUGGER
403extern UINT32 arm7_disasm( char *pBuf, UINT32 pc, UINT32 opcode );
404extern UINT32 thumb_disasm( char *pBuf, UINT32 pc, UINT16 opcode );
401extern UINT32 arm7_disasm(char *pBuf, UINT32 pc, UINT32 opcode);
402extern UINT32 thumb_disasm(char *pBuf, UINT32 pc, UINT16 opcode);
405403
406extern char *(*arm7_dasm_cop_dt_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
407extern char *(*arm7_dasm_cop_rt_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
408extern char *(*arm7_dasm_cop_do_callback)( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0 );
404extern char *(*arm7_dasm_cop_dt_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
405extern char *(*arm7_dasm_cop_rt_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
406extern char *(*arm7_dasm_cop_do_callback)(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
409407#endif
410408
411409#endif /* ARM7CORE_H */
412
src/emu/cpu/arm7/arm7.c
r1686r1687
4242/* Example for showing how Co-Proc functions work */
4343#define TEST_COPROC_FUNCS 1
4444
45/*prototypes*/
45/* prototypes */
4646#if TEST_COPROC_FUNCS
4747static WRITE32_HANDLER(test_do_callback);
4848static READ32_HANDLER(test_rt_r_callback);
4949static WRITE32_HANDLER(test_rt_w_callback);
50static void test_dt_r_callback (UINT32 insn, UINT32* prn, UINT32 (*read32)(int addr));
51static void test_dt_w_callback (UINT32 insn, UINT32* prn, void (*write32)(int addr, UINT32 data));
50static void test_dt_r_callback(UINT32 insn, UINT32 *prn, UINT32 (*read32)(UINT32 addr));
51static void test_dt_w_callback(UINT32 insn, UINT32 *prn, void (*write32)(UINT32 addr, UINT32 data));
5252#ifdef ENABLE_DEBUGGER
53static char *Spec_RT( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
54static char *Spec_DT( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
55static char *Spec_DO( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
53static char *Spec_RT(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
54static char *Spec_DT(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
55static char *Spec_DO(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0);
5656#endif
5757#endif
5858
r1686r1687
7676/* CPU Registers */
7777typedef struct
7878{
79   ARM7CORE_REGS               //these must be included in your cpu specific register implementation
79    ARM7CORE_REGS               // these must be included in your cpu specific register implementation
8080} ARM7_REGS;
8181
8282static ARM7_REGS arm7;
r1686r1687
9090 **************************************************************************/
9191static void arm7_init(int index, int clock, const void *config, int (*irqcallback)(int))
9292{
93   //must call core
94   arm7_core_init("arm7", index);
93    // must call core
94    arm7_core_init("arm7", index);
9595
96   ARM7.irq_callback = irqcallback;
96    ARM7.irq_callback = irqcallback;
9797
9898#if TEST_COPROC_FUNCS
99   //setup co-proc callbacks example
100   arm7_coproc_do_callback = test_do_callback;
101   arm7_coproc_rt_r_callback = test_rt_r_callback;
102   arm7_coproc_rt_w_callback = test_rt_w_callback;
103   arm7_coproc_dt_r_callback = test_dt_r_callback;
104   arm7_coproc_dt_w_callback = test_dt_w_callback;
99    // setup co-proc callbacks example
100    arm7_coproc_do_callback = test_do_callback;
101    arm7_coproc_rt_r_callback = test_rt_r_callback;
102    arm7_coproc_rt_w_callback = test_rt_w_callback;
103    arm7_coproc_dt_r_callback = test_dt_r_callback;
104    arm7_coproc_dt_w_callback = test_dt_w_callback;
105105#ifdef ENABLE_DEBUGGER
106   //setup dasm callbacks - direct method example
107   arm7_dasm_cop_dt_callback = Spec_DT;
108   arm7_dasm_cop_rt_callback = Spec_RT;
109   arm7_dasm_cop_do_callback = Spec_DO;
106    // setup dasm callbacks - direct method example
107    arm7_dasm_cop_dt_callback = Spec_DT;
108    arm7_dasm_cop_rt_callback = Spec_RT;
109    arm7_dasm_cop_do_callback = Spec_DO;
110110#endif
111111#endif
112
113   return;
114112}
115113
116114static void arm7_reset(void)
117115{
118   //must call core reset
119   arm7_core_reset();
116    // must call core reset
117    arm7_core_reset();
120118}
121119
122120static void arm7_exit(void)
123121{
124   /* nothing to do here */
122    /* nothing to do here */
125123}
126124
127static int arm7_execute( int cycles )
125static int arm7_execute(int cycles)
128126{
129/*include the arm7 core execute code*/
127/* include the arm7 core execute code */
130128#include "arm7exec.c"
131} /* arm7_execute */
129}
132130
133131
134132static void set_irq_line(int irqline, int state)
135133{
136   //must call core
137   arm7_core_set_irq_line(irqline,state);
134    // must call core
135    arm7_core_set_irq_line(irqline,state);
138136}
139137
140138static void arm7_get_context(void *dst)
141139{
142   if( dst )
143   {
144      memcpy( dst, &ARM7, sizeof(ARM7) );
145   }
140    if (dst)
141    {
142        memcpy(dst, &ARM7, sizeof(ARM7));
143    }
146144}
147145
148146static void arm7_set_context(void *src)
149147{
150   if (src)
151   {
152      memcpy( &ARM7, src, sizeof(ARM7) );
153   }
148    if (src)
149    {
150        memcpy(&ARM7, src, sizeof(ARM7));
151    }
154152}
155153
156154#ifdef ENABLE_DEBUGGER
157155static offs_t arm7_dasm(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram)
158156{
159   if( T_IS_SET(GET_CPSR) )
160   {
161      return thumb_disasm( buffer, pc, oprom[0] | (oprom[1] << 8)) | 2;
162   }
163   else
164   {
165      return arm7_disasm( buffer, pc, oprom[0] | (oprom[1] << 8) | (oprom[2] << 16) | (oprom[3] << 24)) | 4;
166   }
157    if (T_IS_SET(GET_CPSR))
158    {
159        return thumb_disasm(buffer, pc, oprom[0] | (oprom[1] << 8)) | 2;
160    }
161    else
162    {
163        return arm7_disasm(buffer, pc, oprom[0] | (oprom[1] << 8) | (oprom[2] << 16) | (oprom[3] << 24)) | 4;
164    }
167165}
168166#endif /* ENABLE_DEBUGGER */
169167
r1686r1687
174172
175173static void arm7_set_info(UINT32 state, cpuinfo *info)
176174{
177   switch (state)
178   {
179      /* --- the following bits of info are set as 64-bit signed integers --- */
175    switch (state)
176    {
177        /* --- the following bits of info are set as 64-bit signed integers --- */
180178
181      /* interrupt lines/exceptions */
182      case CPUINFO_INT_INPUT_STATE + ARM7_IRQ_LINE:               set_irq_line(ARM7_IRQ_LINE, info->i); break;
183      case CPUINFO_INT_INPUT_STATE + ARM7_FIRQ_LINE:               set_irq_line(ARM7_FIRQ_LINE, info->i); break;
184      case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_EXCEPTION:         set_irq_line(ARM7_ABORT_EXCEPTION, info->i); break;
185      case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_PREFETCH_EXCEPTION:   set_irq_line(ARM7_ABORT_PREFETCH_EXCEPTION, info->i); break;
186      case CPUINFO_INT_INPUT_STATE + ARM7_UNDEFINE_EXCEPTION:         set_irq_line(ARM7_UNDEFINE_EXCEPTION, info->i); break;
179        /* interrupt lines/exceptions */
180        case CPUINFO_INT_INPUT_STATE + ARM7_IRQ_LINE:                   set_irq_line(ARM7_IRQ_LINE, info->i); break;
181        case CPUINFO_INT_INPUT_STATE + ARM7_FIRQ_LINE:                  set_irq_line(ARM7_FIRQ_LINE, info->i); break;
182        case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_EXCEPTION:            set_irq_line(ARM7_ABORT_EXCEPTION, info->i); break;
183        case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_PREFETCH_EXCEPTION:   set_irq_line(ARM7_ABORT_PREFETCH_EXCEPTION, info->i); break;
184        case CPUINFO_INT_INPUT_STATE + ARM7_UNDEFINE_EXCEPTION:         set_irq_line(ARM7_UNDEFINE_EXCEPTION, info->i); break;
187185
188      /* registers shared by all operating modes */
189      case CPUINFO_INT_REGISTER + ARM7_R0:         ARM7REG( 0) = info->i;               break;
190      case CPUINFO_INT_REGISTER + ARM7_R1:         ARM7REG( 1) = info->i;               break;
191      case CPUINFO_INT_REGISTER + ARM7_R2:         ARM7REG( 2) = info->i;               break;
192      case CPUINFO_INT_REGISTER + ARM7_R3:         ARM7REG( 3) = info->i;               break;
193      case CPUINFO_INT_REGISTER + ARM7_R4:         ARM7REG( 4) = info->i;               break;
194      case CPUINFO_INT_REGISTER + ARM7_R5:         ARM7REG( 5) = info->i;               break;
195      case CPUINFO_INT_REGISTER + ARM7_R6:         ARM7REG( 6) = info->i;               break;
196      case CPUINFO_INT_REGISTER + ARM7_R7:         ARM7REG( 7) = info->i;               break;
197      case CPUINFO_INT_REGISTER + ARM7_R8:         ARM7REG( 8) = info->i;               break;
198      case CPUINFO_INT_REGISTER + ARM7_R9:         ARM7REG( 9) = info->i;               break;
199      case CPUINFO_INT_REGISTER + ARM7_R10:         ARM7REG(10) = info->i;               break;
200      case CPUINFO_INT_REGISTER + ARM7_R11:         ARM7REG(11) = info->i;               break;
201      case CPUINFO_INT_REGISTER + ARM7_R12:         ARM7REG(12) = info->i;               break;
202      case CPUINFO_INT_REGISTER + ARM7_R13:         ARM7REG(13) = info->i;               break;
203      case CPUINFO_INT_REGISTER + ARM7_R14:         ARM7REG(14) = info->i;               break;
204      case CPUINFO_INT_REGISTER + ARM7_R15:         ARM7REG(15) = info->i;               break;
205      case CPUINFO_INT_REGISTER + ARM7_CPSR:         SET_CPSR(info->i);                  break;
186        /* registers shared by all operating modes */
187        case CPUINFO_INT_REGISTER + ARM7_R0:            ARM7REG( 0) = info->i;                  break;
188        case CPUINFO_INT_REGISTER + ARM7_R1:            ARM7REG( 1) = info->i;                  break;
189        case CPUINFO_INT_REGISTER + ARM7_R2:            ARM7REG( 2) = info->i;                  break;
190        case CPUINFO_INT_REGISTER + ARM7_R3:            ARM7REG( 3) = info->i;                  break;
191        case CPUINFO_INT_REGISTER + ARM7_R4:            ARM7REG( 4) = info->i;                  break;
192        case CPUINFO_INT_REGISTER + ARM7_R5:            ARM7REG( 5) = info->i;                  break;
193        case CPUINFO_INT_REGISTER + ARM7_R6:            ARM7REG( 6) = info->i;                  break;
194        case CPUINFO_INT_REGISTER + ARM7_R7:            ARM7REG( 7) = info->i;                  break;
195        case CPUINFO_INT_REGISTER + ARM7_R8:            ARM7REG( 8) = info->i;                  break;
196        case CPUINFO_INT_REGISTER + ARM7_R9:            ARM7REG( 9) = info->i;                  break;
197        case CPUINFO_INT_REGISTER + ARM7_R10:           ARM7REG(10) = info->i;                  break;
198        case CPUINFO_INT_REGISTER + ARM7_R11:           ARM7REG(11) = info->i;                  break;
199        case CPUINFO_INT_REGISTER + ARM7_R12:           ARM7REG(12) = info->i;                  break;
200        case CPUINFO_INT_REGISTER + ARM7_R13:           ARM7REG(13) = info->i;                  break;
201        case CPUINFO_INT_REGISTER + ARM7_R14:           ARM7REG(14) = info->i;                  break;
202        case CPUINFO_INT_REGISTER + ARM7_R15:           ARM7REG(15) = info->i;                  break;
203        case CPUINFO_INT_REGISTER + ARM7_CPSR:          SET_CPSR(info->i);                      break;
206204
207      case CPUINFO_INT_PC:
208      case CPUINFO_INT_REGISTER + ARM7_PC:         R15 = info->i;                     break;
209      case CPUINFO_INT_SP:                     SetRegister(13,info->i);            break;
205        case CPUINFO_INT_PC:
206        case CPUINFO_INT_REGISTER + ARM7_PC:            R15 = info->i;                          break;
207        case CPUINFO_INT_SP:                            SetRegister(13,info->i);                break;
210208
211      /* FIRQ Mode Shadowed Registers */
212      case CPUINFO_INT_REGISTER + ARM7_FR8:         ARM7REG(eR8_FIQ)  = info->i;         break;
213      case CPUINFO_INT_REGISTER + ARM7_FR9:         ARM7REG(eR9_FIQ)  = info->i;         break;
214      case CPUINFO_INT_REGISTER + ARM7_FR10:         ARM7REG(eR10_FIQ) = info->i;         break;
215      case CPUINFO_INT_REGISTER + ARM7_FR11:         ARM7REG(eR11_FIQ) = info->i;         break;
216      case CPUINFO_INT_REGISTER + ARM7_FR12:         ARM7REG(eR12_FIQ) = info->i;         break;
217      case CPUINFO_INT_REGISTER + ARM7_FR13:         ARM7REG(eR13_FIQ) = info->i;         break;
218      case CPUINFO_INT_REGISTER + ARM7_FR14:         ARM7REG(eR14_FIQ) = info->i;         break;
219      case CPUINFO_INT_REGISTER + ARM7_FSPSR:         ARM7REG(eSPSR_FIQ) = info->i;         break;
209        /* FIRQ Mode Shadowed Registers */
210        case CPUINFO_INT_REGISTER + ARM7_FR8:           ARM7REG(eR8_FIQ)  = info->i;            break;
211        case CPUINFO_INT_REGISTER + ARM7_FR9:           ARM7REG(eR9_FIQ)  = info->i;            break;
212        case CPUINFO_INT_REGISTER + ARM7_FR10:          ARM7REG(eR10_FIQ) = info->i;            break;
213        case CPUINFO_INT_REGISTER + ARM7_FR11:          ARM7REG(eR11_FIQ) = info->i;            break;
214        case CPUINFO_INT_REGISTER + ARM7_FR12:          ARM7REG(eR12_FIQ) = info->i;            break;
215        case CPUINFO_INT_REGISTER + ARM7_FR13:          ARM7REG(eR13_FIQ) = info->i;            break;
216        case CPUINFO_INT_REGISTER + ARM7_FR14:          ARM7REG(eR14_FIQ) = info->i;            break;
217        case CPUINFO_INT_REGISTER + ARM7_FSPSR:         ARM7REG(eSPSR_FIQ) = info->i;           break;
220218
221      /* IRQ Mode Shadowed Registers */
222      case CPUINFO_INT_REGISTER + ARM7_IR13:         ARM7REG(eR13_IRQ) = info->i;         break;
223      case CPUINFO_INT_REGISTER + ARM7_IR14:         ARM7REG(eR14_IRQ) = info->i;         break;
224      case CPUINFO_INT_REGISTER + ARM7_ISPSR:         ARM7REG(eSPSR_IRQ) = info->i;         break;
219        /* IRQ Mode Shadowed Registers */
220        case CPUINFO_INT_REGISTER + ARM7_IR13:          ARM7REG(eR13_IRQ) = info->i;            break;
221        case CPUINFO_INT_REGISTER + ARM7_IR14:          ARM7REG(eR14_IRQ) = info->i;            break;
222        case CPUINFO_INT_REGISTER + ARM7_ISPSR:         ARM7REG(eSPSR_IRQ) = info->i;           break;
225223
226      /* Supervisor Mode Shadowed Registers */
227      case CPUINFO_INT_REGISTER + ARM7_SR13:         ARM7REG(eR13_SVC) = info->i;         break;
228      case CPUINFO_INT_REGISTER + ARM7_SR14:         ARM7REG(eR14_SVC) = info->i;         break;
229      case CPUINFO_INT_REGISTER + ARM7_SSPSR:         ARM7REG(eSPSR_SVC) = info->i;         break;
224        /* Supervisor Mode Shadowed Registers */
225        case CPUINFO_INT_REGISTER + ARM7_SR13:          ARM7REG(eR13_SVC) = info->i;            break;
226        case CPUINFO_INT_REGISTER + ARM7_SR14:          ARM7REG(eR14_SVC) = info->i;            break;
227        case CPUINFO_INT_REGISTER + ARM7_SSPSR:         ARM7REG(eSPSR_SVC) = info->i;           break;
230228
231      /* Abort Mode Shadowed Registers */
232      case CPUINFO_INT_REGISTER + ARM7_AR13:         ARM7REG(eR13_ABT) = info->i;         break;
233      case CPUINFO_INT_REGISTER + ARM7_AR14:         ARM7REG(eR14_ABT) = info->i;         break;
234      case CPUINFO_INT_REGISTER + ARM7_ASPSR:         ARM7REG(eSPSR_ABT) = info->i;         break;
229        /* Abort Mode Shadowed Registers */
230        case CPUINFO_INT_REGISTER + ARM7_AR13:          ARM7REG(eR13_ABT) = info->i;            break;
231        case CPUINFO_INT_REGISTER + ARM7_AR14:          ARM7REG(eR14_ABT) = info->i;            break;
232        case CPUINFO_INT_REGISTER + ARM7_ASPSR:         ARM7REG(eSPSR_ABT) = info->i;           break;
235233
236      /* Undefined Mode Shadowed Registers */
237      case CPUINFO_INT_REGISTER + ARM7_UR13:         ARM7REG(eR13_UND) = info->i;         break;
238      case CPUINFO_INT_REGISTER + ARM7_UR14:         ARM7REG(eR14_UND) = info->i;         break;
239      case CPUINFO_INT_REGISTER + ARM7_USPSR:         ARM7REG(eSPSR_UND) = info->i;         break;
240   }
234        /* Undefined Mode Shadowed Registers */
235        case CPUINFO_INT_REGISTER + ARM7_UR13:          ARM7REG(eR13_UND) = info->i;            break;
236        case CPUINFO_INT_REGISTER + ARM7_UR14:          ARM7REG(eR14_UND) = info->i;            break;
237        case CPUINFO_INT_REGISTER + ARM7_USPSR:         ARM7REG(eSPSR_UND) = info->i;           break;
238    }
241239}
242240
243241
r1686r1687
248246
249247void arm7_get_info(UINT32 state, cpuinfo *info)
250248{
251   switch (state)
252   {
253      /* --- the following bits of info are returned as 64-bit signed integers --- */
249    switch (state)
250    {
251        /* --- the following bits of info are returned as 64-bit signed integers --- */
254252
255      /* cpu implementation data */
256      case CPUINFO_INT_CONTEXT_SIZE:               info->i = sizeof(ARM7);               break;
257      case CPUINFO_INT_INPUT_LINES:               info->i = ARM7_NUM_LINES;            break;
258      case CPUINFO_INT_DEFAULT_IRQ_VECTOR:         info->i = 0;                     break;
259      case CPUINFO_INT_ENDIANNESS:               info->i = CPU_IS_LE;               break;
260      case CPUINFO_INT_CLOCK_MULTIPLIER:            info->i = 1;                     break;
261      case CPUINFO_INT_CLOCK_DIVIDER:               info->i = 1;                     break;
262      case CPUINFO_INT_MIN_INSTRUCTION_BYTES:         info->i = 2;                     break;
263      case CPUINFO_INT_MAX_INSTRUCTION_BYTES:         info->i = 4;                     break;
264      case CPUINFO_INT_MIN_CYCLES:               info->i = 3;                     break;
265      case CPUINFO_INT_MAX_CYCLES:               info->i = 4;                     break;
253        /* cpu implementation data */
254        case CPUINFO_INT_CONTEXT_SIZE:                  info->i = sizeof(ARM7);                 break;
255        case CPUINFO_INT_INPUT_LINES:                   info->i = ARM7_NUM_LINES;               break;
256        case CPUINFO_INT_DEFAULT_IRQ_VECTOR:            info->i = 0;                            break;
257        case CPUINFO_INT_ENDIANNESS:                    info->i = CPU_IS_LE;                    break;
258        case CPUINFO_INT_CLOCK_MULTIPLIER:              info->i = 1;                            break;
259        case CPUINFO_INT_CLOCK_DIVIDER:                 info->i = 1;                            break;
260        case CPUINFO_INT_MIN_INSTRUCTION_BYTES:         info->i = 2;                            break;
261        case CPUINFO_INT_MAX_INSTRUCTION_BYTES:         info->i = 4;                            break;
262        case CPUINFO_INT_MIN_CYCLES:                    info->i = 3;                            break;
263        case CPUINFO_INT_MAX_CYCLES:                    info->i = 4;                            break;
266264
267      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:   info->i = 32;               break;
268      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM:   info->i = 32;               break;
269      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM:   info->i = 0;               break;
270      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:   info->i = 0;               break;
271      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA:   info->i = 0;               break;
272      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA:   info->i = 0;               break;
273      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:      info->i = 0;               break;
274      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO:      info->i = 0;               break;
275      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO:      info->i = 0;               break;
265        case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 32;                   break;
266        case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 32;                   break;
267        case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;                    break;
268        case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:    info->i = 0;                    break;
269        case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA:    info->i = 0;                    break;
270        case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA:    info->i = 0;                    break;
271        case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:      info->i = 0;                    break;
272        case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO:      info->i = 0;                    break;
273        case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO:      info->i = 0;                    break;
276274
277      /* interrupt lines/exceptions */
278      case CPUINFO_INT_INPUT_STATE + ARM7_IRQ_LINE:               info->i = ARM7.pendingIrq; break;
279      case CPUINFO_INT_INPUT_STATE + ARM7_FIRQ_LINE:               info->i = ARM7.pendingFiq; break;
280      case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_EXCEPTION:         info->i = ARM7.pendingAbtD; break;
281      case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_PREFETCH_EXCEPTION:   info->i = ARM7.pendingAbtP; break;
282      case CPUINFO_INT_INPUT_STATE + ARM7_UNDEFINE_EXCEPTION:         info->i = ARM7.pendingUnd; break;
275        /* interrupt lines/exceptions */
276        case CPUINFO_INT_INPUT_STATE + ARM7_IRQ_LINE:                   info->i = ARM7.pendingIrq; break;
277        case CPUINFO_INT_INPUT_STATE + ARM7_FIRQ_LINE:                  info->i = ARM7.pendingFiq; break;
278        case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_EXCEPTION:            info->i = ARM7.pendingAbtD; break;
279        case CPUINFO_INT_INPUT_STATE + ARM7_ABORT_PREFETCH_EXCEPTION:   info->i = ARM7.pendingAbtP; break;
280        case CPUINFO_INT_INPUT_STATE + ARM7_UNDEFINE_EXCEPTION:         info->i = ARM7.pendingUnd; break;
283281
284      /* registers shared by all operating modes */
285      case CPUINFO_INT_REGISTER + ARM7_R0:   info->i = ARM7REG( 0);                     break;
286      case CPUINFO_INT_REGISTER + ARM7_R1:   info->i = ARM7REG( 1);                     break;
287      case CPUINFO_INT_REGISTER + ARM7_R2:   info->i = ARM7REG( 2);                     break;
288      case CPUINFO_INT_REGISTER + ARM7_R3:   info->i = ARM7REG( 3);                     break;
289      case CPUINFO_INT_REGISTER + ARM7_R4:   info->i = ARM7REG( 4);                     break;
290      case CPUINFO_INT_REGISTER + ARM7_R5:   info->i = ARM7REG( 5);                     break;
291      case CPUINFO_INT_REGISTER + ARM7_R6:   info->i = ARM7REG( 6);                     break;
292      case CPUINFO_INT_REGISTER + ARM7_R7:   info->i = ARM7REG( 7);                     break;
293      case CPUINFO_INT_REGISTER + ARM7_R8:   info->i = ARM7REG( 8);                     break;
294      case CPUINFO_INT_REGISTER + ARM7_R9:   info->i = ARM7REG( 9);                     break;
295      case CPUINFO_INT_REGISTER + ARM7_R10:   info->i = ARM7REG(10);                     break;
296      case CPUINFO_INT_REGISTER + ARM7_R11:   info->i = ARM7REG(11);                     break;
297      case CPUINFO_INT_REGISTER + ARM7_R12:   info->i = ARM7REG(12);                     break;
298      case CPUINFO_INT_REGISTER + ARM7_R13:   info->i = ARM7REG(13);                     break;
299      case CPUINFO_INT_REGISTER + ARM7_R14:   info->i = ARM7REG(14);                     break;
300      case CPUINFO_INT_REGISTER + ARM7_R15:   info->i = ARM7REG(15);                     break;
282        /* registers shared by all operating modes */
283        case CPUINFO_INT_REGISTER + ARM7_R0:    info->i = ARM7REG( 0);                          break;
284        case CPUINFO_INT_REGISTER + ARM7_R1:    info->i = ARM7REG( 1);                          break;
285        case CPUINFO_INT_REGISTER + ARM7_R2:    info->i = ARM7REG( 2);                          break;
286        case CPUINFO_INT_REGISTER + ARM7_R3:    info->i = ARM7REG( 3);                          break;
287        case CPUINFO_INT_REGISTER + ARM7_R4:    info->i = ARM7REG( 4);                          break;
288        case CPUINFO_INT_REGISTER + ARM7_R5:    info->i = ARM7REG( 5);                          break;
289        case CPUINFO_INT_REGISTER + ARM7_R6:    info->i = ARM7REG( 6);                          break;
290        case CPUINFO_INT_REGISTER + ARM7_R7:    info->i = ARM7REG( 7);                          break;
291        case CPUINFO_INT_REGISTER + ARM7_R8:    info->i = ARM7REG( 8);                          break;
292        case CPUINFO_INT_REGISTER + ARM7_R9:    info->i = ARM7REG( 9);                          break;
293        case CPUINFO_INT_REGISTER + ARM7_R10:   info->i = ARM7REG(10);                          break;
294        case CPUINFO_INT_REGISTER + ARM7_R11:   info->i = ARM7REG(11);                          break;
295        case CPUINFO_INT_REGISTER + ARM7_R12:   info->i = ARM7REG(12);                          break;
296        case CPUINFO_INT_REGISTER + ARM7_R13:   info->i = ARM7REG(13);                          break;
297        case CPUINFO_INT_REGISTER + ARM7_R14:   info->i = ARM7REG(14);                          break;
298        case CPUINFO_INT_REGISTER + ARM7_R15:   info->i = ARM7REG(15);                          break;
301299
302      case CPUINFO_INT_PREVIOUSPC:         info->i = 0;    /* not implemented */         break;
303      case CPUINFO_INT_PC:
304      case CPUINFO_INT_REGISTER + ARM7_PC:   info->i = R15;                           break;
305      case CPUINFO_INT_SP:               info->i = GetRegister(13);                  break;
300        case CPUINFO_INT_PREVIOUSPC:            info->i = 0;    /* not implemented */           break;
301        case CPUINFO_INT_PC:
302        case CPUINFO_INT_REGISTER + ARM7_PC:    info->i = R15;                                  break;
303        case CPUINFO_INT_SP:                    info->i = GetRegister(13);                      break;
306304
307      /* FIRQ Mode Shadowed Registers */
308      case CPUINFO_INT_REGISTER + ARM7_FR8:   info->i = ARM7REG(eR8_FIQ);                  break;
309      case CPUINFO_INT_REGISTER + ARM7_FR9:   info->i = ARM7REG(eR9_FIQ);                  break;
310      case CPUINFO_INT_REGISTER + ARM7_FR10:   info->i = ARM7REG(eR10_FIQ);               break;
311      case CPUINFO_INT_REGISTER + ARM7_FR11:   info->i = ARM7REG(eR11_FIQ);               break;
312      case CPUINFO_INT_REGISTER + ARM7_FR12:   info->i = ARM7REG(eR12_FIQ);               break;
313      case CPUINFO_INT_REGISTER + ARM7_FR13:   info->i = ARM7REG(eR13_FIQ);               break;
314      case CPUINFO_INT_REGISTER + ARM7_FR14:   info->i = ARM7REG(eR14_FIQ);               break;
315      case CPUINFO_INT_REGISTER + ARM7_FSPSR:   info->i = ARM7REG(eSPSR_FIQ);               break;
305        /* FIRQ Mode Shadowed Registers */
306        case CPUINFO_INT_REGISTER + ARM7_FR8:   info->i = ARM7REG(eR8_FIQ);                     break;
307        case CPUINFO_INT_REGISTER + ARM7_FR9:   info->i = ARM7REG(eR9_FIQ);                     break;
308        case CPUINFO_INT_REGISTER + ARM7_FR10:  info->i = ARM7REG(eR10_FIQ);                    break;
309        case CPUINFO_INT_REGISTER + ARM7_FR11:  info->i = ARM7REG(eR11_FIQ);                    break;
310        case CPUINFO_INT_REGISTER + ARM7_FR12:  info->i = ARM7REG(eR12_FIQ);                    break;
311        case CPUINFO_INT_REGISTER + ARM7_FR13:  info->i = ARM7REG(eR13_FIQ);                    break;
312        case CPUINFO_INT_REGISTER + ARM7_FR14:  info->i = ARM7REG(eR14_FIQ);                    break;
313        case CPUINFO_INT_REGISTER + ARM7_FSPSR: info->i = ARM7REG(eSPSR_FIQ);                   break;
316314
317      /* IRQ Mode Shadowed Registers */
318      case CPUINFO_INT_REGISTER + ARM7_IR13:   info->i = ARM7REG(eR13_IRQ);               break;
319      case CPUINFO_INT_REGISTER + ARM7_IR14:   info->i = ARM7REG(eR14_IRQ);               break;
320      case CPUINFO_INT_REGISTER + ARM7_ISPSR:   info->i = ARM7REG(eSPSR_IRQ);               break;
315        /* IRQ Mode Shadowed Registers */
316        case CPUINFO_INT_REGISTER + ARM7_IR13:  info->i = ARM7REG(eR13_IRQ);                    break;
317        case CPUINFO_INT_REGISTER + ARM7_IR14:  info->i = ARM7REG(eR14_IRQ);                    break;
318        case CPUINFO_INT_REGISTER + ARM7_ISPSR: info->i = ARM7REG(eSPSR_IRQ);                   break;
321319
322      /* Supervisor Mode Shadowed Registers */
323      case CPUINFO_INT_REGISTER + ARM7_SR13:   info->i = ARM7REG(eR13_SVC);               break;
324      case CPUINFO_INT_REGISTER + ARM7_SR14:   info->i = ARM7REG(eR14_SVC);               break;
325      case CPUINFO_INT_REGISTER + ARM7_SSPSR:   info->i = ARM7REG(eSPSR_SVC);               break;
320        /* Supervisor Mode Shadowed Registers */
321        case CPUINFO_INT_REGISTER + ARM7_SR13:  info->i = ARM7REG(eR13_SVC);                    break;
322        case CPUINFO_INT_REGISTER + ARM7_SR14:  info->i = ARM7REG(eR14_SVC);                    break;
323        case CPUINFO_INT_REGISTER + ARM7_SSPSR: info->i = ARM7REG(eSPSR_SVC);                   break;
326324
327      /* Abort Mode Shadowed Registers */
328      case CPUINFO_INT_REGISTER + ARM7_AR13:   info->i = ARM7REG(eR13_ABT);               break;
329      case CPUINFO_INT_REGISTER + ARM7_AR14:   info->i = ARM7REG(eR14_ABT);               break;
330      case CPUINFO_INT_REGISTER + ARM7_ASPSR:   info->i = ARM7REG(eSPSR_ABT);               break;
325        /* Abort Mode Shadowed Registers */
326        case CPUINFO_INT_REGISTER + ARM7_AR13:  info->i = ARM7REG(eR13_ABT);                    break;
327        case CPUINFO_INT_REGISTER + ARM7_AR14:  info->i = ARM7REG(eR14_ABT);                    break;
328        case CPUINFO_INT_REGISTER + ARM7_ASPSR: info->i = ARM7REG(eSPSR_ABT);                   break;
331329
332      /* Undefined Mode Shadowed Registers */
333      case CPUINFO_INT_REGISTER + ARM7_UR13:   info->i = ARM7REG(eR13_UND);               break;
334      case CPUINFO_INT_REGISTER + ARM7_UR14:   info->i = ARM7REG(eR14_UND);               break;
335      case CPUINFO_INT_REGISTER + ARM7_USPSR:   info->i = ARM7REG(eSPSR_UND);               break;
330        /* Undefined Mode Shadowed Registers */
331        case CPUINFO_INT_REGISTER + ARM7_UR13:  info->i = ARM7REG(eR13_UND);                    break;
332        case CPUINFO_INT_REGISTER + ARM7_UR14:  info->i = ARM7REG(eR14_UND);                    break;
333        case CPUINFO_INT_REGISTER + ARM7_USPSR: info->i = ARM7REG(eSPSR_UND);                   break;
336334
337      /* --- the following bits of info are returned as pointers to data or functions --- */
338      case CPUINFO_PTR_SET_INFO:            info->setinfo = arm7_set_info;               break;
339      case CPUINFO_PTR_GET_CONTEXT:         info->getcontext = arm7_get_context;         break;
340      case CPUINFO_PTR_SET_CONTEXT:         info->setcontext = arm7_set_context;         break;
341      case CPUINFO_PTR_INIT:               info->init = arm7_init;                     break;
342      case CPUINFO_PTR_RESET:               info->reset = arm7_reset;                  break;
343      case CPUINFO_PTR_EXIT:               info->exit = arm7_exit;                     break;
344      case CPUINFO_PTR_EXECUTE:            info->execute = arm7_execute;               break;
345      case CPUINFO_PTR_BURN:               info->burn = NULL;                        break;
335        /* --- the following bits of info are returned as pointers to data or functions --- */
336        case CPUINFO_PTR_SET_INFO:              info->setinfo = arm7_set_info;                  break;
337        case CPUINFO_PTR_GET_CONTEXT:           info->getcontext = arm7_get_context;            break;
338        case CPUINFO_PTR_SET_CONTEXT:           info->setcontext = arm7_set_context;            break;
339        case CPUINFO_PTR_INIT:                  info->init = arm7_init;                         break;
340        case CPUINFO_PTR_RESET:                 info->reset = arm7_reset;                       break;
341        case CPUINFO_PTR_EXIT:                  info->exit = arm7_exit;                         break;
342        case CPUINFO_PTR_EXECUTE:               info->execute = arm7_execute;                   break;
343        case CPUINFO_PTR_BURN:                  info->burn = NULL;                              break;
346344#ifdef ENABLE_DEBUGGER
347      case CPUINFO_PTR_DISASSEMBLE:         info->disassemble = arm7_dasm;                break;
345        case CPUINFO_PTR_DISASSEMBLE:           info->disassemble = arm7_dasm;                  break;
348346#endif /* ENABLE_DEBUGGER */
349      case CPUINFO_PTR_INSTRUCTION_COUNTER:   info->icount = &ARM7_ICOUNT;               break;
347        case CPUINFO_PTR_INSTRUCTION_COUNTER:   info->icount = &ARM7_ICOUNT;                    break;
350348
351      /* --- the following bits of info are returned as NULL-terminated strings --- */
352      case CPUINFO_STR_NAME:               strcpy(info->s, "ARM7");                  break;
353      case CPUINFO_STR_CORE_FAMILY:         strcpy(info->s, "Acorn Risc Machine");         break;
354      case CPUINFO_STR_CORE_VERSION:         strcpy(info->s, "1.3");                     break;
355      case CPUINFO_STR_CORE_FILE:            strcpy(info->s, __FILE__);                  break;
356      case CPUINFO_STR_CORE_CREDITS:         strcpy(info->s, "Copyright Steve Ellenoff, sellenoff@hotmail.com"); break;
349        /* --- the following bits of info are returned as NULL-terminated strings --- */
350        case CPUINFO_STR_NAME:                  strcpy(info->s, "ARM7");                        break;
351        case CPUINFO_STR_CORE_FAMILY:           strcpy(info->s, "Acorn Risc Machine");          break;
352        case CPUINFO_STR_CORE_VERSION:          strcpy(info->s, "1.3");                         break;
353        case CPUINFO_STR_CORE_FILE:             strcpy(info->s, __FILE__);                      break;
354        case CPUINFO_STR_CORE_CREDITS:          strcpy(info->s, "Copyright Steve Ellenoff, sellenoff@hotmail.com"); break;
357355
358      case CPUINFO_STR_FLAGS:
359         sprintf(info->s, "%c%c%c%c%c%c%c %s",
360            (ARM7REG(eCPSR) & N_MASK) ? 'N' : '-',
361            (ARM7REG(eCPSR) & Z_MASK) ? 'Z' : '-',
362            (ARM7REG(eCPSR) & C_MASK) ? 'C' : '-',
363            (ARM7REG(eCPSR) & V_MASK) ? 'V' : '-',
364            (ARM7REG(eCPSR) & I_MASK) ? 'I' : '-',
365            (ARM7REG(eCPSR) & F_MASK) ? 'F' : '-',
366            (ARM7REG(eCPSR) & T_MASK) ? 'T' : '-',
367            GetModeText(ARM7REG(eCPSR)));
368      break;
356        case CPUINFO_STR_FLAGS:
357            sprintf(info->s, "%c%c%c%c%c%c%c %s",
358                    (ARM7REG(eCPSR) & N_MASK) ? 'N' : '-',
359                    (ARM7REG(eCPSR) & Z_MASK) ? 'Z' : '-',
360                    (ARM7REG(eCPSR) & C_MASK) ? 'C' : '-',
361                    (ARM7REG(eCPSR) & V_MASK) ? 'V' : '-',
362                    (ARM7REG(eCPSR) & I_MASK) ? 'I' : '-',
363                    (ARM7REG(eCPSR) & F_MASK) ? 'F' : '-',
364                    (ARM7REG(eCPSR) & T_MASK) ? 'T' : '-',
365                    GetModeText(ARM7REG(eCPSR)));
366        break;
369367
370      /* registers shared by all operating modes */
371      case CPUINFO_STR_REGISTER + ARM7_PC:   sprintf(info->s, "PC  :%08x", R15 );         break;
372      case CPUINFO_STR_REGISTER + ARM7_R0:   sprintf(info->s, "R0  :%08x", ARM7REG( 0) );   break;
373      case CPUINFO_STR_REGISTER + ARM7_R1:   sprintf(info->s, "R1  :%08x", ARM7REG( 1) );   break;
374      case CPUINFO_STR_REGISTER + ARM7_R2:   sprintf(info->s, "R2  :%08x", ARM7REG( 2) );   break;
375      case CPUINFO_STR_REGISTER + ARM7_R3:   sprintf(info->s, "R3  :%08x", ARM7REG( 3) );   break;
376      case CPUINFO_STR_REGISTER + ARM7_R4:   sprintf(info->s, "R4  :%08x", ARM7REG( 4) );   break;
377      case CPUINFO_STR_REGISTER + ARM7_R5:   sprintf(info->s, "R5  :%08x", ARM7REG( 5) );   break;
378      case CPUINFO_STR_REGISTER + ARM7_R6:   sprintf(info->s, "R6  :%08x", ARM7REG( 6) );   break;
379      case CPUINFO_STR_REGISTER + ARM7_R7:   sprintf(info->s, "R7  :%08x", ARM7REG( 7) );   break;
380      case CPUINFO_STR_REGISTER + ARM7_R8:   sprintf(info->s, "R8  :%08x", ARM7REG( 8) );   break;
381      case CPUINFO_STR_REGISTER + ARM7_R9:   sprintf(info->s, "R9  :%08x", ARM7REG( 9) );   break;
382      case CPUINFO_STR_REGISTER + ARM7_R10:   sprintf(info->s, "R10 :%08x", ARM7REG(10) );   break;
383      case CPUINFO_STR_REGISTER + ARM7_R11:   sprintf(info->s, "R11 :%08x", ARM7REG(11) );   break;
384      case CPUINFO_STR_REGISTER + ARM7_R12:   sprintf(info->s, "R12 :%08x", ARM7REG(12) );   break;
385      case CPUINFO_STR_REGISTER + ARM7_R13:   sprintf(info->s, "R13 :%08x", ARM7REG(13) );   break;
386      case CPUINFO_STR_REGISTER + ARM7_R14:   sprintf(info->s, "R14 :%08x", ARM7REG(14) );   break;
387      case CPUINFO_STR_REGISTER + ARM7_R15:   sprintf(info->s, "R15 :%08x", ARM7REG(15) );   break;
368        /* registers shared by all operating modes */
369        case CPUINFO_STR_REGISTER + ARM7_PC:    sprintf(info->s, "PC  :%08x", R15);            break;
370        case CPUINFO_STR_REGISTER + ARM7_R0:    sprintf(info->s, "R0  :%08x", ARM7REG( 0));    break;
371        case CPUINFO_STR_REGISTER + ARM7_R1:    sprintf(info->s, "R1  :%08x", ARM7REG( 1));    break;
372        case CPUINFO_STR_REGISTER + ARM7_R2:    sprintf(info->s, "R2  :%08x", ARM7REG( 2));    break;
373        case CPUINFO_STR_REGISTER + ARM7_R3:    sprintf(info->s, "R3  :%08x", ARM7REG( 3));    break;
374        case CPUINFO_STR_REGISTER + ARM7_R4:    sprintf(info->s, "R4  :%08x", ARM7REG( 4));    break;
375        case CPUINFO_STR_REGISTER + ARM7_R5:    sprintf(info->s, "R5  :%08x", ARM7REG( 5));    break;
376        case CPUINFO_STR_REGISTER + ARM7_R6:    sprintf(info->s, "R6  :%08x", ARM7REG( 6));    break;
377        case CPUINFO_STR_REGISTER + ARM7_R7:    sprintf(info->s, "R7  :%08x", ARM7REG( 7));    break;
378        case CPUINFO_STR_REGISTER + ARM7_R8:    sprintf(info->s, "R8  :%08x", ARM7REG( 8));    break;
379        case CPUINFO_STR_REGISTER + ARM7_R9:    sprintf(info->s, "R9  :%08x", ARM7REG( 9));    break;
380        case CPUINFO_STR_REGISTER + ARM7_R10:   sprintf(info->s, "R10 :%08x", ARM7REG(10));    break;
381        case CPUINFO_STR_REGISTER + ARM7_R11:   sprintf(info->s, "R11 :%08x", ARM7REG(11));    break;
382        case CPUINFO_STR_REGISTER + ARM7_R12:   sprintf(info->s, "R12 :%08x", ARM7REG(12));    break;
383        case CPUINFO_STR_REGISTER + ARM7_R13:   sprintf(info->s, "R13 :%08x", ARM7REG(13));    break;
384        case CPUINFO_STR_REGISTER + ARM7_R14:   sprintf(info->s, "R14 :%08x", ARM7REG(14));    break;
385        case CPUINFO_STR_REGISTER + ARM7_R15:   sprintf(info->s, "R15 :%08x", ARM7REG(15));    break;
388386
389      /* FIRQ Mode Shadowed Registers */
390      case CPUINFO_STR_REGISTER + ARM7_FR8:   sprintf(info->s, "FR8 :%08x", ARM7REG(eR8_FIQ)  ); break;
391      case CPUINFO_STR_REGISTER + ARM7_FR9:   sprintf(info->s, "FR9 :%08x", ARM7REG(eR9_FIQ)  ); break;
392      case CPUINFO_STR_REGISTER + ARM7_FR10:   sprintf(info->s, "FR10:%08x", ARM7REG(eR10_FIQ) ); break;
393      case CPUINFO_STR_REGISTER + ARM7_FR11:   sprintf(info->s, "FR11:%08x", ARM7REG(eR11_FIQ) ); break;
394      case CPUINFO_STR_REGISTER + ARM7_FR12:   sprintf(info->s, "FR12:%08x", ARM7REG(eR12_FIQ) ); break;
395      case CPUINFO_STR_REGISTER + ARM7_FR13:   sprintf(info->s, "FR13:%08x", ARM7REG(eR13_FIQ) ); break;
396      case CPUINFO_STR_REGISTER + ARM7_FR14:   sprintf(info->s, "FR14:%08x", ARM7REG(eR14_FIQ) ); break;
397      case CPUINFO_STR_REGISTER + ARM7_FSPSR:   sprintf(info->s, "FR16:%08x", ARM7REG(eSPSR_FIQ)); break;
387        /* FIRQ Mode Shadowed Registers */
388        case CPUINFO_STR_REGISTER + ARM7_FR8:   sprintf(info->s, "FR8 :%08x", ARM7REG(eR8_FIQ)  ); break;
389        case CPUINFO_STR_REGISTER + ARM7_FR9:   sprintf(info->s, "FR9 :%08x", ARM7REG(eR9_FIQ)  ); break;
390        case CPUINFO_STR_REGISTER + ARM7_FR10:  sprintf(info->s, "FR10:%08x", ARM7REG(eR10_FIQ) ); break;
391        case CPUINFO_STR_REGISTER + ARM7_FR11:  sprintf(info->s, "FR11:%08x", ARM7REG(eR11_FIQ) ); break;
392        case CPUINFO_STR_REGISTER + ARM7_FR12:  sprintf(info->s, "FR12:%08x", ARM7REG(eR12_FIQ) ); break;
393        case CPUINFO_STR_REGISTER + ARM7_FR13:  sprintf(info->s, "FR13:%08x", ARM7REG(eR13_FIQ) ); break;
394        case CPUINFO_STR_REGISTER + ARM7_FR14:  sprintf(info->s, "FR14:%08x", ARM7REG(eR14_FIQ) ); break;
395        case CPUINFO_STR_REGISTER + ARM7_FSPSR: sprintf(info->s, "FR16:%08x", ARM7REG(eSPSR_FIQ)); break;
398396
399      /* IRQ Mode Shadowed Registers */
400      case CPUINFO_STR_REGISTER + ARM7_IR13:   sprintf(info->s, "IR13:%08x", ARM7REG(eR13_IRQ) ); break;
401      case CPUINFO_STR_REGISTER + ARM7_IR14:   sprintf(info->s, "IR14:%08x", ARM7REG(eR14_IRQ) ); break;
402      case CPUINFO_STR_REGISTER + ARM7_ISPSR:   sprintf(info->s, "IR16:%08x", ARM7REG(eSPSR_IRQ)); break;
397        /* IRQ Mode Shadowed Registers */
398        case CPUINFO_STR_REGISTER + ARM7_IR13:  sprintf(info->s, "IR13:%08x", ARM7REG(eR13_IRQ) ); break;
399        case CPUINFO_STR_REGISTER + ARM7_IR14:  sprintf(info->s, "IR14:%08x", ARM7REG(eR14_IRQ) ); break;
400        case CPUINFO_STR_REGISTER + ARM7_ISPSR: sprintf(info->s, "IR16:%08x", ARM7REG(eSPSR_IRQ)); break;
403401
404      /* Supervisor Mode Shadowed Registers */
405      case CPUINFO_STR_REGISTER + ARM7_SR13:   sprintf(info->s, "SR13:%08x", ARM7REG(eR13_SVC) ); break;
406      case CPUINFO_STR_REGISTER + ARM7_SR14:   sprintf(info->s, "SR14:%08x", ARM7REG(eR14_SVC) ); break;
407      case CPUINFO_STR_REGISTER + ARM7_SSPSR:   sprintf(info->s, "SR16:%08x", ARM7REG(eSPSR_SVC)); break;
402        /* Supervisor Mode Shadowed Registers */
403        case CPUINFO_STR_REGISTER + ARM7_SR13:  sprintf(info->s, "SR13:%08x", ARM7REG(eR13_SVC) ); break;
404        case CPUINFO_STR_REGISTER + ARM7_SR14:  sprintf(info->s, "SR14:%08x", ARM7REG(eR14_SVC) ); break;
405        case CPUINFO_STR_REGISTER + ARM7_SSPSR: sprintf(info->s, "SR16:%08x", ARM7REG(eSPSR_SVC)); break;
408406
409      /* Abort Mode Shadowed Registers */
410      case CPUINFO_STR_REGISTER + ARM7_AR13:   sprintf(info->s, "AR13:%08x", ARM7REG(eR13_ABT) ); break;
411      case CPUINFO_STR_REGISTER + ARM7_AR14:   sprintf(info->s, "AR14:%08x", ARM7REG(eR14_ABT) ); break;
412      case CPUINFO_STR_REGISTER + ARM7_ASPSR:   sprintf(info->s, "AR16:%08x", ARM7REG(eSPSR_ABT)); break;
407        /* Abort Mode Shadowed Registers */
408        case CPUINFO_STR_REGISTER + ARM7_AR13:  sprintf(info->s, "AR13:%08x", ARM7REG(eR13_ABT) ); break;
409        case CPUINFO_STR_REGISTER + ARM7_AR14:  sprintf(info->s, "AR14:%08x", ARM7REG(eR14_ABT) ); break;
410        case CPUINFO_STR_REGISTER + ARM7_ASPSR: sprintf(info->s, "AR16:%08x", ARM7REG(eSPSR_ABT)); break;
413411
414      /* Undefined Mode Shadowed Registers */
415      case CPUINFO_STR_REGISTER + ARM7_UR13:   sprintf(info->s, "UR13:%08x", ARM7REG(eR13_UND) ); break;
416      case CPUINFO_STR_REGISTER + ARM7_UR14:   sprintf(info->s, "UR14:%08x", ARM7REG(eR14_UND) ); break;
417      case CPUINFO_STR_REGISTER + ARM7_USPSR:   sprintf(info->s, "UR16:%08x", ARM7REG(eSPSR_UND)); break;
418   }
412        /* Undefined Mode Shadowed Registers */
413        case CPUINFO_STR_REGISTER + ARM7_UR13:  sprintf(info->s, "UR13:%08x", ARM7REG(eR13_UND) ); break;
414        case CPUINFO_STR_REGISTER + ARM7_UR14:  sprintf(info->s, "UR14:%08x", ARM7REG(eR14_UND) ); break;
415        case CPUINFO_STR_REGISTER + ARM7_USPSR: sprintf(info->s, "UR16:%08x", ARM7REG(eSPSR_UND)); break;
416    }
419417}
420418
421419/* TEST COPROC CALLBACK HANDLERS - Used for example on how to implement only */
r1686r1687
423421
424422static WRITE32_HANDLER(test_do_callback)
425423{
426   LOG(("test_do_callback opcode=%x, =%x\n",offset,data));
424    LOG(("test_do_callback opcode=%x, =%x\n", offset, data));
427425}
428426static READ32_HANDLER(test_rt_r_callback)
429427{
430   UINT32 data=0;
431   LOG(("test_rt_r_callback opcode=%x\n",offset));
432   return data;
428    UINT32 data=0;
429    LOG(("test_rt_r_callback opcode=%x\n", offset));
430    return data;
433431}
434432static WRITE32_HANDLER(test_rt_w_callback)
435433{
436   LOG(("test_rt_w_callback opcode=%x, data from ARM7 register=%x\n",offset,data));
434    LOG(("test_rt_w_callback opcode=%x, data from ARM7 register=%x\n", offset, data));
437435}
438static void test_dt_r_callback (UINT32 insn, UINT32* prn, UINT32 (*read32)(int addr))
436static void test_dt_r_callback(UINT32 insn, UINT32 *prn, UINT32 (*read32)(UINT32 addr))
439437{
440   LOG(("test_dt_r_callback: insn = %x\n",insn));
438    LOG(("test_dt_r_callback: insn = %x\n", insn));
441439}
442static void test_dt_w_callback (UINT32 insn, UINT32* prn, void (*write32)(int addr, UINT32 data))
440static void test_dt_w_callback(UINT32 insn, UINT32 *prn, void (*write32)(UINT32 addr, UINT32 data))
443441{
444   LOG(("test_dt_w_callback: opcode = %x\n",insn));
442    LOG(("test_dt_w_callback: opcode = %x\n", insn));
445443}
446444
447445/* Custom Co-proc DASM handlers */
448446#ifdef ENABLE_DEBUGGER
449static char *Spec_RT( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
447static char *Spec_RT(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
450448{
451   pBuf += sprintf( pBuf, "SPECRT");
452   return pBuf;
449    pBuf += sprintf(pBuf, "SPECRT");
450    return pBuf;
453451}
454static char *Spec_DT( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
452static char *Spec_DT(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
455453{
456   pBuf += sprintf( pBuf, "SPECDT");
457   return pBuf;
454    pBuf += sprintf(pBuf, "SPECDT");
455    return pBuf;
458456}
459static char *Spec_DO( char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
457static char *Spec_DO(char *pBuf, UINT32 opcode, char *pConditionCode, char *pBuf0)
460458{
461   pBuf += sprintf( pBuf, "SPECDO");
462   return pBuf;
459    pBuf += sprintf(pBuf, "SPECDO");
460    return pBuf;
463461}
464462#endif
465463#endif
466
src/emu/cpu/arm7/arm7exec.c
r1686r1687
3131 *
3232 *         It should be included as follows in your cpu core:
3333 *
34 *         int arm7_execute( int cycles )
34 *         int arm7_execute(int cycles)
3535 *         {
3636 *         #include "arm7exec.c"
3737 *         }
r1686r1687
4747    ARM7_ICOUNT = cycles;
4848    do
4949    {
50       CALL_DEBUGGER(R15);
50        CALL_DEBUGGER(R15);
5151
52   /* handle Thumb instructions if active */
53   if( T_IS_SET(GET_CPSR) )
54   {
55      UINT32 readword;
56      UINT32 addr;
57      UINT32 rm, rn, rs, rd, op2, imm, rrs, rrd;
58      INT32 offs;
59      pc = R15;
52        /* handle Thumb instructions if active */
53        if (T_IS_SET(GET_CPSR))
54        {
55            UINT32 readword;
56            UINT32 addr;
57            UINT32 rm, rn, rs, rd, op2, imm, rrs, rrd;
58            INT32 offs;
6059
61      insn = cpu_readop16(pc & (~1));
62      ARM7_ICOUNT -= (3 - thumbCycles[insn >> 8]);
63      switch( ( insn & THUMB_INSN_TYPE ) >> THUMB_INSN_TYPE_SHIFT )
64      {
65         case 0x0: /* Logical shifting */
66            SET_CPSR(GET_CPSR &~ (N_MASK | Z_MASK));
67            if( insn & THUMB_SHIFT_R ) /* Shift right */
68            {
69               rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
70               rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
71               rrs = GET_REGISTER(rs);
72               offs = ( insn & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
73               if( offs != 0 )
74               {
75                  SET_REGISTER( rd, rrs >> offs );
76               if( rrs & ( 1 << (offs-1) ) )
77               {
78                  SET_CPSR(GET_CPSR | C_MASK);
79               }
80               else
81               {
82                  SET_CPSR(GET_CPSR &~ C_MASK);
83               }
84               }
85               else
86               {
87                  SET_REGISTER( rd, 0 );
88                  if( rrs & 0x80000000 )
89                  {
90                          SET_CPSR( GET_CPSR | C_MASK );
91                  }
92                  else
93                  {
94                          SET_CPSR( GET_CPSR &~ C_MASK );
95                  }
96               }
97               SET_CPSR(GET_CPSR &~ ( Z_MASK | N_MASK ) );
98               SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
99                    R15 += 2;
100            }
101            else /* Shift left */
102            {
103               rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
104               rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
105               rrs = GET_REGISTER(rs);
106               offs = ( insn & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
107               if( offs != 0 )
108               {
109                  SET_REGISTER( rd, rrs << offs );
110                  if( rrs & ( 1 << ( 31 - ( offs - 1 ) ) ) )
111                  {
112                     SET_CPSR(GET_CPSR | C_MASK);
113                  }
114                  else
115                  {
116                     SET_CPSR(GET_CPSR &~ C_MASK);
117                  }
118               }
119               else
120               {
121                  SET_REGISTER( rd, rrs );
122               }
123               SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
124               SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
125               R15 += 2;
126            }
127            break;
128         case 0x1: /* Arithmetic */
129            if( insn & THUMB_INSN_ADDSUB )
130            {
131               switch( ( insn & THUMB_ADDSUB_TYPE ) >> THUMB_ADDSUB_TYPE_SHIFT )
132               {
133                  case 0x0: /* ADD Rd, Rs, Rn */
134                     rn = GET_REGISTER( ( insn & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT );
135                     rs = GET_REGISTER( ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT );
136                     rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
137                     SET_REGISTER( rd, rs + rn );
138                     HandleThumbALUAddFlags( GET_REGISTER(rd), rs, rn );
139                     break;
140                  case 0x1: /* SUB Rd, Rs, Rn */
141                     rn = GET_REGISTER( ( insn & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT );
142                     rs = GET_REGISTER( ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT );
143                     rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
144                     SET_REGISTER( rd, rs - rn );
145                     HandleThumbALUSubFlags( GET_REGISTER(rd), rs, rn );
146                     break;
147                  case 0x2: /* ADD Rd, Rs, #imm */
148                     imm = ( insn & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
149                     rs = GET_REGISTER( ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT );
150                     rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
151                     SET_REGISTER( rd, rs + imm );
152                     HandleThumbALUAddFlags( GET_REGISTER(rd), rs, imm );
153                     break;
154                  case 0x3: /* SUB Rd, Rs, #imm */
155                     imm = ( insn & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
156                     rs = GET_REGISTER( ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT );
157                     rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
158                     SET_REGISTER( rd, rs - imm );
159                     HandleThumbALUSubFlags( GET_REGISTER(rd), rs,imm );
160                     break;
161                  default:
162                     fatalerror("%08x: G1 Undefined Thumb instruction: %04x\n", pc, insn);
163                     R15 += 2;
164                     break;
165               }
166            }
167            else
168            {
169               /* ASR.. */
170               //if( insn & THUMB_SHIFT_R ) /* Shift right */
171               {
172                  rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
173                  rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
174                  rrs = GET_REGISTER(rs);
175                  offs = ( insn & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
176                  if( offs == 0 )
177                  {
178                     offs = 32;
179                  }
180
181                  if( offs >= 32 )
182                  {
183                          if( rrs >> 31 )
184                  {
185                     SET_CPSR(GET_CPSR | C_MASK);
186                  }
187                  else
188                  {
189                     SET_CPSR(GET_CPSR &~ C_MASK);
190                  }
191                          SET_REGISTER( rd, ( rrs & 0x80000000 ) ? 0xFFFFFFFF : 0x00000000 );
192                  }
193                  else
194                  {
195                          if( ( rrs >> ( offs - 1 ) ) & 1 )
196                          {
197                          SET_CPSR( GET_CPSR | C_MASK );
198                          }
199                          else
200                          {
201                          SET_CPSR( GET_CPSR &~ C_MASK );
202                          }
203                          SET_REGISTER( rd, ( rrs & 0x80000000 ) ? ( ( 0xFFFFFFFF << ( 32 - offs ) ) | ( rrs >> offs ) ) : ( rrs >> offs ) );
204                  }
205                  SET_CPSR(GET_CPSR &~ (N_MASK | Z_MASK));
206                  SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
207                  R15 += 2;
208               }
209
210            }
211            break;
212         case 0x2: /* CMP / MOV */
213            if( insn & THUMB_INSN_CMP )
214            {
215               rn = GET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT );
216               op2 = ( insn & THUMB_INSN_IMM );
217               rd = rn - op2;
218               HandleThumbALUSubFlags( rd, rn, op2 );
219               //mame_printf_debug("%08x: xxx Thumb instruction: CMP R%d (%08x), %02x (N=%d, Z=%d, C=%d, V=%d)\n", pc, ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, GET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT ), op2, N_IS_SET(GET_CPSR) ? 1 : 0, Z_IS_SET(GET_CPSR) ? 1 : 0, C_IS_SET(GET_CPSR) ? 1 : 0, V_IS_SET(GET_CPSR) ? 1 : 0);
220            }
221            else
222            {
223               rd = ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT;
224               op2 = ( insn & THUMB_INSN_IMM );
225               SET_REGISTER( rd, op2 );
226               SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
227               SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
228               R15 += 2;
229            }
230            break;
231         case 0x3: /* ADD/SUB immediate */
232            if( insn & THUMB_INSN_SUB ) /* SUB Rd, #Offset8 */
233            {
234               rn = GET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT );
235               op2 = ( insn & THUMB_INSN_IMM );
236               //mame_printf_debug("%08x:  Thumb instruction: SUB R%d, %02x\n", pc, ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, op2);
237               rd = rn - op2;
238               SET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, rd );
239               HandleThumbALUSubFlags( rd, rn, op2 );
240            }
241            else /* ADD Rd, #Offset8 */
242            {
243               rn = GET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT );
244               op2 = insn & THUMB_INSN_IMM;
245               rd = rn + op2;
246               //mame_printf_debug("%08x:  Thumb instruction: ADD R%d, %02x\n", pc, ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, op2);
247               SET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, rd );
248               HandleThumbALUAddFlags( rd, rn, op2 );
249            }
250            break;
251         case 0x4: /* Rd & Rm instructions */
252            switch( ( insn & THUMB_GROUP4_TYPE ) >> THUMB_GROUP4_TYPE_SHIFT )
253            {
254               case 0x0:
255                  switch( ( insn & THUMB_ALUOP_TYPE ) >> THUMB_ALUOP_TYPE_SHIFT )
256                  {
257                     case 0x0: /* AND Rd, Rs */
258                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
259                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
260                        SET_REGISTER( rd, GET_REGISTER(rd) & GET_REGISTER(rs) );
261                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
262                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
263                        R15 += 2;
264                        break;
265                     case 0x1: /* EOR Rd, Rs */
266                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
267                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
268                        SET_REGISTER( rd, GET_REGISTER(rd) ^ GET_REGISTER(rs) );
269                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
270                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
271                        R15 += 2;
272                        break;
273                     case 0x2: /* LSL Rd, Rs */
274                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
275                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
276                        rrd = GET_REGISTER(rd);
277                        offs = GET_REGISTER(rs) & 0x000000ff;
278                        if (offs > 0)
279                        {
280                           if ( offs < 32 )
281                           {
282                                   SET_REGISTER( rd, rrd << offs );
283                                   if( rrd & ( 1 << ( 31 - ( offs - 1 ) ) ) )
284                                   {
285                                      SET_CPSR( GET_CPSR | C_MASK );
286                                   }
287                                   else
288                                   {
289                                      SET_CPSR( GET_CPSR &~ C_MASK );
290                                   }
291                           }
292                           else if( offs == 32 )
293                           {
294                                   SET_REGISTER( rd, 0 );
295                                   if( rrd & 1 )
296                                   {
297                                      SET_CPSR( GET_CPSR | C_MASK );
298                                   }
299                                   else
300                                   {
301                                      SET_CPSR( GET_CPSR &~ C_MASK );
302                                   }
303                           }
304                           else
305                           {
306                                   SET_REGISTER( rd, 0 );
307                                   SET_CPSR( GET_CPSR &~ C_MASK );
308                           }
309                        }
310                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
311                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
312                        R15 += 2;
313                        break;
314                     case 0x3: /* LSR Rd, Rs */
315                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
316                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
317                        rrd = GET_REGISTER(rd);
318                        offs = GET_REGISTER(rs) & 0x000000ff;
319                        if (offs >  0)
320                        {
321                           if( offs < 32 )
322                           {
323                                   SET_REGISTER( rd, rrd >> offs );
324                                   if( rrd & ( 1 << ( offs - 1 ) ) )
325                                   {
326                                      SET_CPSR( GET_CPSR | C_MASK );
327                                   }
328                                   else
329                                   {
330                                      SET_CPSR( GET_CPSR &~ C_MASK );
331                                   }
332                           }
333                           else if( offs == 32 )
334                           {
335                                   SET_REGISTER( rd, 0 );
336                                   if( rrd & 0x80000000 )
337                                   {
338                                      SET_CPSR( GET_CPSR | C_MASK );
339                                   }
340                                   else
341                                   {
342                                      SET_CPSR( GET_CPSR &~ C_MASK );
343                                   }
344                           }
345                           else
346                           {
347                                   SET_REGISTER( rd, 0 );
348                                   SET_CPSR( GET_CPSR &~ C_MASK );
349                           }
350                        }
351                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
352                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
353                        R15 += 2;
354                        break;
355                     case 0x4: /* ASR Rd, Rs */
356                        {
357                           rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
358                           rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
359                           rrs = GET_REGISTER(rs)&0xff;
360                           rrd = GET_REGISTER(rd);
361                           if (rrs != 0)
362                           {
363                              if (rrs >= 32)
364                              {
365                                 if (rrd>>31)
366                                 {
367                                    SET_CPSR(GET_CPSR | C_MASK);
368                                 }
369                                 else
370                                 {
371                                    SET_CPSR(GET_CPSR &~ C_MASK);
372                                 }
373                                 SET_REGISTER( rd, (GET_REGISTER(rd) & 0x80000000) ? 0xFFFFFFFF : 0x00000000 );
374                              }
375                              else
376                              {
377                                 if ((rrd>>(rrs-1))&1)
378                                 {
379                                    SET_CPSR(GET_CPSR | C_MASK);
380                                 }
381                                 else
382                                 {
383                                    SET_CPSR(GET_CPSR &~ C_MASK);
384                                 }
385                                 SET_REGISTER( rd, (rrd & 0x80000000) ? ((0xFFFFFFFF<<(32-rrs)) | (rrd>>rrs)) : (rrd>>rrs));
386                              }
387                           }
388                           SET_CPSR(GET_CPSR &~ (N_MASK | Z_MASK));
389                           SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
390                           R15 += 2;
391                        }
392                        break;
393                     case 0x5: /* ADC Rd, Rs */
394                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
395                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
396                        op2=(GET_CPSR & C_MASK) ? 1 : 0;
397                        rn=GET_REGISTER(rd) + GET_REGISTER(rs) + op2;
398                        HandleThumbALUAddFlags( rn, GET_REGISTER(rd), ( GET_REGISTER(rs) ) ); //?
399                        SET_REGISTER( rd, rn);
400                        break;
401                     case 0x6: /* SBC Rd, Rs */
402                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
403                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
404                        op2=(GET_CPSR & C_MASK) ? 0 : 1;
405                        rn=GET_REGISTER(rd) - GET_REGISTER(rs) - op2;
406                        HandleThumbALUSubFlags( rn, GET_REGISTER(rd), ( GET_REGISTER(rs) ) ); //?
407                        SET_REGISTER( rd, rn);
408                        break;
409                     case 0x7: /* ROR Rd, Rs */
410                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
411                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
412                                            rrd = GET_REGISTER(rd);
413                        imm = GET_REGISTER(rs) & 0x0000001f;
414                        SET_REGISTER( rd, ( rrd >> imm ) | ( rrd << ( 32 - imm ) ) );
415                        if( rrd & ( 1 << ( imm - 1 ) ) )
416                        {
417                                SET_CPSR( GET_CPSR | C_MASK );
418                        }
419                        else
420                        {
421                                SET_CPSR( GET_CPSR &~ C_MASK );
422                        }
423                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
424                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
425                        R15 += 2;
426                        break;
427                     case 0x8: /* TST Rd, Rs */
428                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
429                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
430                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
431                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) & GET_REGISTER(rs) ) );
432                        R15 += 2;
433                        break;
434                     case 0x9: /* NEG Rd, Rs - todo: check me */
435                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
436                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
437                                rrs = GET_REGISTER(rs);
438                        rn = 0 - rrs;
439                        SET_REGISTER( rd, rn );
440                        HandleThumbALUSubFlags( GET_REGISTER(rd), 0, rrs );
441                        break;
442                     case 0xa: /* CMP Rd, Rs */
443                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
444                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
445                        rn = GET_REGISTER(rd) - GET_REGISTER(rs);
446                        HandleThumbALUSubFlags( rn, GET_REGISTER(rd), GET_REGISTER(rs) );
447                        break;
448                     case 0xb: /* CMN Rd, Rs - check flags, add dasm */
449                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
450                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
451                        rn = GET_REGISTER(rd) + GET_REGISTER(rs);
452                        HandleThumbALUAddFlags( rn, GET_REGISTER(rd), GET_REGISTER(rs) );
453                        break;
454                     case 0xc: /* ORR Rd, Rs */
455                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
456                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
457                        SET_REGISTER( rd, GET_REGISTER(rd) | GET_REGISTER(rs) );
458                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
459                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
460                        R15 += 2;
461                        break;
462                     case 0xd: /* MUL Rd, Rs */
463                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
464                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
465                        rn = GET_REGISTER(rd) * GET_REGISTER(rs);
466                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
467                        SET_REGISTER( rd, rn );
468                        SET_CPSR( GET_CPSR | HandleALUNZFlags( rn ) );
469                        R15 += 2;
470                        break;
471                     case 0xe: /* BIC Rd, Rs */
472                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
473                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
474                        SET_REGISTER( rd, GET_REGISTER(rd) & (~GET_REGISTER(rs)) );
475                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
476                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
477                        R15 += 2;
478                        break;
479                     case 0xf: /* MVN Rd, Rs */
480                        rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
481                        rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
482                        op2 = GET_REGISTER(rs);
483                        SET_REGISTER( rd, ~op2 );
484                        SET_CPSR( GET_CPSR &~ ( Z_MASK | N_MASK ) );
485                        SET_CPSR( GET_CPSR | HandleALUNZFlags( GET_REGISTER(rd) ) );
486                        R15 += 2;
487                        break;
488                     default:
489                        fatalerror("%08x: G4-0 Undefined Thumb instruction: %04x %x\n", pc, insn, ( insn & THUMB_ALUOP_TYPE ) >> THUMB_ALUOP_TYPE_SHIFT);
490                        R15 += 2;
491                        break;
492                  }
493                  break;
494               case 0x1:
495                  switch( ( insn & THUMB_HIREG_OP ) >> THUMB_HIREG_OP_SHIFT )
496                  {
497                     case 0x0: /* ADD Rd, Rs */
498                        rs = ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
499                        rd = insn & THUMB_HIREG_RD;
500                        switch( ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
501                        {
502                           case 0x1: /* ADD Rd, HRs */
503                                    SET_REGISTER( rd, GET_REGISTER(rd) + GET_REGISTER(rs+8) );
504                              // emulate the effects of pre-fetch
505                              if (rs == 7)
506                              {
507                                 SET_REGISTER(rd, GET_REGISTER(rd) + 4);
508                              }
509                              break;
510                           case 0x2: /* ADD HRd, Rs */
511                              SET_REGISTER( rd+8, GET_REGISTER(rd+8) + GET_REGISTER(rs) );
512                              if (rd == 7)
513                              {
514                                 R15 += 2;
515                                 change_pc(R15);
516                              }
517                              break;
518                           case 0x3: /* Add HRd, HRs */
519                              SET_REGISTER( rd+8, GET_REGISTER(rd+8) + GET_REGISTER(rs+8) );
520                              // emulate the effects of pre-fetch
521                              if (rs == 7)
522                              {
523                                 SET_REGISTER(rd+8, GET_REGISTER(rd+8) + 4);
524                              }
525                              if (rd == 7)
526                              {
527                                 R15 += 2;
528                                 change_pc(R15);
529                              }
530                              break;
531                           default:
532                              fatalerror("%08x: G4-1-0 Undefined Thumb instruction: %04x %x\n", pc, insn, ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT );
533                              break;
534                        }
535                        R15 += 2;
536                        break;
537                     case 0x1: /* CMP */
538                        switch( ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
539                        {
540                           case 0x0: /* CMP Rd, Rs */
541                              rs = GET_REGISTER( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) );
542                              rd = GET_REGISTER( insn & THUMB_HIREG_RD );
543                              rn = rd - rs;
544                              HandleThumbALUSubFlags( rn, rd, rs );
545                              break;
546                           case 0x1: /* CMP Rd, Hs */
547                              rs = GET_REGISTER( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) + 8 );
548                              rd = GET_REGISTER( insn & THUMB_HIREG_RD );
549                              rn = rd - rs;
550                              HandleThumbALUSubFlags( rn, rd, rs );
551                              break;
552                           case 0x2: /* CMP Hd, Rs */
553                              rs = GET_REGISTER( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) );
554                              rd = GET_REGISTER( (insn & THUMB_HIREG_RD) + 8 );
555                              rn = rd - rs;
556                              HandleThumbALUSubFlags( rn, rd, rs );
557                              break;
558                           case 0x3: /* CMP Hd, Hs */
559                              rs = GET_REGISTER( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) + 8 );
560                              rd = GET_REGISTER( (insn & THUMB_HIREG_RD) + 8 );
561                              rn = rd - rs;
562                              HandleThumbALUSubFlags( rn, rd, rs );
563                              break;
564                           default:
565                              fatalerror("%08x: G4-1 Undefined Thumb instruction: %04x %x\n", pc, insn, ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT);
566                              R15 += 2;
567                              break;
568                        }
569                        break;
570                     case 0x2: /* MOV */
571                        switch( ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
572                        {
573                           case 0x1:   // MOV Rd, Hs
574                              rs = ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
575                              rd = insn & THUMB_HIREG_RD;
576                              if( rs == 7 )
577                              {
578                                 SET_REGISTER( rd, GET_REGISTER(rs + 8) + 4 );
579                              }
580                              else
581                              {
582                                 SET_REGISTER( rd, GET_REGISTER(rs + 8) );
583                              }
584                              R15 += 2;
585                              break;
586                           case 0x2:   // MOV Hd, Rs
587                              rs = ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
588                              rd = insn & THUMB_HIREG_RD;
589                              SET_REGISTER( rd + 8, GET_REGISTER(rs) );
590                              if( rd != 7 )
591                              {
592                                 R15 += 2;
593                              }
594                              else
595                                                       {
596                                                              R15 &= ~1;
597                                 change_pc(R15);
598                                                       }
599                              break;
600                           case 0x3:   // MOV Hd, Hs
601                              rs = ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
602                              rd = insn & THUMB_HIREG_RD;
603                              if (rs == 7)
604                              {
605                                 SET_REGISTER( rd + 8, GET_REGISTER(rs+8)+4 );
606                              }
607                              else
608                              {
609                              SET_REGISTER( rd + 8, GET_REGISTER(rs+8) );
610                              }
611                              if( rd != 7 )
612                              {
613                                 R15 += 2;
614                              }
615
616                                   if( rd == 7 )
617                                                       {
618                                                              R15 &= ~1;
619                                 change_pc(R15);
620                                                       }
621                              break;
622                           default:
623                              fatalerror("%08x: G4-2 Undefined Thumb instruction: %04x (%x)\n", pc, insn, ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT);
624                              R15 += 2;
625                              break;
626                        }
627                        break;
628                     case 0x3:
629                        switch( ( insn & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
630                        {
631                           case 0x0:
632                              rd = ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
633                              addr = GET_REGISTER(rd);
634                              if( addr & 1 )
635                              {
636                                 addr &= ~1;
637                              }
638                              else
639                              {
640                                 SET_CPSR(GET_CPSR &~ T_MASK);
641                                 if( addr & 2 )
642                                 {
643                                    addr += 2;
644                                 }
645                              }
646                              R15 = addr;
647                              break;
648                           case 0x1:
649                              addr = GET_REGISTER( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) + 8 );
650                              if( ( ( ( insn & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT ) + 8 ) == 15 )
651                              {
652                                 addr += 2;
653                              }
654                              if( addr & 1 )
655                              {
656                                 addr &= ~1;
657                              }
658                              else
659                              {
660                                 SET_CPSR(GET_CPSR &~ T_MASK);
661                                 if( addr & 2 )
662                                 {
663                                    addr += 2;
664                                 }
665                              }
666                              R15 = addr;
667                              break;
668                           default:
669                              fatalerror("%08x: G4-3 Undefined Thumb instruction: %04x\n", pc, insn);
670                              R15 += 2;
671                              break;
672                        }
673                        break;
674                     default:
675                        fatalerror("%08x: G4-x Undefined Thumb instruction: %04x\n", pc, insn);
676                        R15 += 2;
677                        break;
678                  }
679                  break;
680               case 0x2:
681               case 0x3:
682                  readword = READ32( ( R15 & ~2 ) + 4 + ( ( insn & THUMB_INSN_IMM ) << 2 ) );
683                  SET_REGISTER( ( insn & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT, readword );
684                  R15 += 2;
685                  break;
686               default:
687                  fatalerror("%08x: G4-y Undefined Thumb instruction: %04x\n", pc, insn);
688                  R15 += 2;
689                  break;
690            }
691            break;
692         case 0x5: /* LDR* STR* */
693            switch( ( insn & THUMB_GROUP5_TYPE ) >> THUMB_GROUP5_TYPE_SHIFT )
694            {
695               case 0x0: /* STR Rd, [Rn, Rm] */
696                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
697                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
698                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
699                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
700                  WRITE32( addr, GET_REGISTER(rd) );
701                  R15 += 2;
702                  break;
703               case 0x1: /* STRH Rd, [Rn, Rm] */
704                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
705                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
706                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
707                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
708                  WRITE16( addr, GET_REGISTER(rd) );
709                  R15 += 2;
710                  break;
711               case 0x2: /* STRB Rd, [Rn, Rm] */
712                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
713                   rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
714                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
715                   addr = GET_REGISTER(rn) + GET_REGISTER(rm);
716                   WRITE8( addr, GET_REGISTER(rd) );
717                  R15 += 2;
718                  break;
719               case 0x3: /* LDSB Rd, [Rn, Rm] todo, add dasm */
720                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
721                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
722                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
723                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
724                  op2 = READ8( addr );
725                  if( op2 & 0x00000080 )
726                  {
727                     op2 |= 0xffffff00;
728                  }
729                  SET_REGISTER( rd, op2 );
730                  R15 += 2;
731                  break;
732               case 0x4: /* LDR Rd, [Rn, Rm] */
733                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
734                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
735                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
736                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
737                  op2 = READ32( addr );
738                  SET_REGISTER( rd, op2 );
739                  R15 += 2;
740                  break;
741               case 0x5: /* LDRH Rd, [Rn, Rm] */
742                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
743                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
744                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
745                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
746                  op2 = READ16( addr );
747                  SET_REGISTER( rd, op2 );
748                  R15 += 2;
749                  break;
750
751               case 0x6: /* LDRB Rd, [Rn, Rm] */
752                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
753                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
754                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
755                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
756                  op2 = READ8( addr );
757                  SET_REGISTER( rd, op2 );
758                  R15 += 2;
759                  break;
760               case 0x7: /* LDSH Rd, [Rn, Rm] */
761                  rm = ( insn & THUMB_GROUP5_RM ) >> THUMB_GROUP5_RM_SHIFT;
762                  rn = ( insn & THUMB_GROUP5_RN ) >> THUMB_GROUP5_RN_SHIFT;
763                  rd = ( insn & THUMB_GROUP5_RD ) >> THUMB_GROUP5_RD_SHIFT;
764                  addr = GET_REGISTER(rn) + GET_REGISTER(rm);
765                  op2 = READ16( addr );
766                  if( op2 & 0x00008000 )
767                  {
768                     op2 |= 0xffff0000;
769                  }
770                  SET_REGISTER( rd, op2 );
771                  R15 += 2;
772                  break;
773               default:
774                  fatalerror("%08x: G5 Undefined Thumb instruction: %04x\n", pc, insn);
775                  R15 += 2;
776                  break;
777            }
778            break;
779         case 0x6: /* Word Store w/ Immediate Offset */
780            if( insn & THUMB_LSOP_L ) /* Load */
781            {
782               rn = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
783               rd = insn & THUMB_ADDSUB_RD;
784               offs = ( ( insn & THUMB_LSOP_OFFS ) >> THUMB_LSOP_OFFS_SHIFT ) << 2;
785               SET_REGISTER( rd, READ32(GET_REGISTER(rn) + offs) ); // fix
786               R15 += 2;
787            }
788            else /* Store */
789            {
790               rn = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
791               rd = insn & THUMB_ADDSUB_RD;
792               offs = ( ( insn & THUMB_LSOP_OFFS ) >> THUMB_LSOP_OFFS_SHIFT ) << 2;
793               WRITE32( GET_REGISTER(rn) + offs, GET_REGISTER(rd) );
794               R15 += 2;
795            }
796            break;
797         case 0x7: /* Byte Store w/ Immeidate Offset */
798            if( insn & THUMB_LSOP_L ) /* Load */
799            {
800               rn = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
801               rd = insn & THUMB_ADDSUB_RD;
802               offs = ( insn & THUMB_LSOP_OFFS ) >> THUMB_LSOP_OFFS_SHIFT;
803               SET_REGISTER( rd, READ8( GET_REGISTER(rn) + offs ) );
804               R15 += 2;
805            }
806            else /* Store */
807            {
808               rn = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
809               rd = insn & THUMB_ADDSUB_RD;
810               offs = ( insn & THUMB_LSOP_OFFS ) >> THUMB_LSOP_OFFS_SHIFT;
811               WRITE8( GET_REGISTER(rn) + offs, GET_REGISTER(rd) );
812               R15 += 2;
813            }
814            break;
815         case 0x8: /* Load/Store Halfword */
816            if( insn & THUMB_HALFOP_L ) /* Load */
817            {
818               imm = ( insn & THUMB_HALFOP_OFFS ) >> THUMB_HALFOP_OFFS_SHIFT;
819               rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
820               rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
821               SET_REGISTER( rd, READ16( GET_REGISTER(rs) + ( imm << 1 ) ) );
822               R15 += 2;
823            }
824            else /* Store */
825            {
826               imm = ( insn & THUMB_HALFOP_OFFS ) >> THUMB_HALFOP_OFFS_SHIFT;
827               rs = ( insn & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
828               rd = ( insn & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
829               WRITE16( GET_REGISTER(rs) + ( imm << 1 ), GET_REGISTER(rd) );
830               R15 += 2;
831            }
832            break;
833         case 0x9: /* Stack-Relative Load/Store */
834            if( insn & THUMB_STACKOP_L )
835            {
836               rd = ( insn & THUMB_STACKOP_RD ) >> THUMB_STACKOP_RD_SHIFT;
837               offs = (UINT8)( insn & THUMB_INSN_IMM );
838               readword = READ32( GET_REGISTER(13) + ( (UINT32)offs << 2 ) );
839               SET_REGISTER( rd, readword );
840               R15 += 2;
841            }
842            else
843            {
844               rd = ( insn & THUMB_STACKOP_RD ) >> THUMB_STACKOP_RD_SHIFT;
845               offs = (UINT8)( insn & THUMB_INSN_IMM );
846               WRITE32( GET_REGISTER(13) + ( (UINT32)offs << 2 ), GET_REGISTER(rd) );
847               R15 += 2;
848            }
849            break;
850         case 0xa: /* Get relative address */
851            if( insn & THUMB_RELADDR_SP ) /* ADD Rd, SP, #nn */
852            {
853               rd = ( insn & THUMB_RELADDR_RD ) >> THUMB_RELADDR_RD_SHIFT;
854               offs = (UINT8)( insn & THUMB_INSN_IMM ) << 2;
855               SET_REGISTER( rd, GET_REGISTER(13) + offs );
856               R15 += 2;
857            }
858            else /* ADD Rd, PC, #nn */
859            {
860               rd = ( insn & THUMB_RELADDR_RD ) >> THUMB_RELADDR_RD_SHIFT;
861               offs = (UINT8)( insn & THUMB_INSN_IMM ) << 2;
862               SET_REGISTER( rd, ( ( R15 + 4 ) & ~2 ) + offs );
863               R15 += 2;
864            }
865            break;
866         case 0xb: /* Stack-Related Opcodes */
867            switch( ( insn & THUMB_STACKOP_TYPE ) >> THUMB_STACKOP_TYPE_SHIFT )
868            {
869               case 0x0: /* ADD SP, #imm */
870                  addr = ( insn & THUMB_INSN_IMM );
871                  addr &= ~THUMB_INSN_IMM_S;
872                  SET_REGISTER( 13, GET_REGISTER(13) + ( ( insn & THUMB_INSN_IMM_S ) ? -( addr << 2 ) : ( addr << 2 ) ) );
873                  R15 += 2;
874                  break;
875               case 0x4: /* PUSH {Rlist} */
876                  for( offs = 7; offs >= 0; offs-- )
877                  {
878                     if( insn & ( 1 << offs ) )
879                     {
880                        SET_REGISTER( 13, GET_REGISTER(13) - 4 );
881                        WRITE32( GET_REGISTER(13), GET_REGISTER(offs) );
882                     }
883                  }
884                  R15 += 2;
885                  break;
886               case 0x5: /* PUSH {Rlist}{LR} */
887                  SET_REGISTER( 13, GET_REGISTER(13) - 4 );
888                  WRITE32( GET_REGISTER(13), GET_REGISTER(14) );
889                  for( offs = 7; offs >= 0; offs-- )
890                  {
891                     if( insn & ( 1 << offs ) )
892                     {
893                        SET_REGISTER( 13, GET_REGISTER(13) - 4 );
894                        WRITE32( GET_REGISTER(13), GET_REGISTER(offs) );
895                     }
896                  }
897                  R15 += 2;
898                  break;
899               case 0xc: /* POP {Rlist} */
900                  for( offs = 0; offs < 8; offs++ )
901                  {
902                     if( insn & ( 1 << offs ) )
903                     {
904                        SET_REGISTER( offs, READ32( GET_REGISTER(13) ) );
905                        SET_REGISTER( 13, GET_REGISTER(13) + 4 );
906                     }
907                  }
908                  R15 += 2;
909                  break;
910               case 0xd: /* POP {Rlist}{PC} */
911                  for( offs = 0; offs < 8; offs++ )
912                  {
913                     if( insn & ( 1 << offs ) )
914                     {
915                        SET_REGISTER( offs, READ32( GET_REGISTER(13) ) );
916                        SET_REGISTER( 13, GET_REGISTER(13) + 4 );
917                     }
918                  }
919                  R15 = READ32( GET_REGISTER(13) ) & ~1;
920                  SET_REGISTER( 13, GET_REGISTER(13) + 4 );
921                  break;
922               default:
923                  fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
924                  R15 += 2;
925                  break;
926            }
927            break;
928         case 0xc: /* Multiple Load/Store */
929            {
930               UINT32 ld_st_address;
931
932               rd = ( insn & THUMB_MULTLS_BASE ) >> THUMB_MULTLS_BASE_SHIFT;
933               ld_st_address = GET_REGISTER(rd) & 0xfffffffc;
934
935               if( insn & THUMB_MULTLS ) /* Load */
936               {
937                  int rd_in_list;
938
939                  rd_in_list = insn & (1 << rd);
940                  for( offs = 0; offs < 8; offs++ )
941                  {
942                     if( insn & ( 1 << offs ) )
943                     {
944                        SET_REGISTER(offs, READ32(ld_st_address));
945                        ld_st_address += 4;
946                     }
947                  }
948                  if (!rd_in_list)
949                     SET_REGISTER(rd, ld_st_address);
950                  R15 += 2;
951               }
952               else /* Store */
953               {
954                  for( offs = 0; offs < 8; offs++ )
955                  {
956                     if( insn & ( 1 << offs ) )
957                     {
958                        WRITE32(ld_st_address, GET_REGISTER(offs));
959                        ld_st_address += 4;
960                     }
961                  }
962                  SET_REGISTER(rd, ld_st_address);
963                  R15 += 2;
964               }
965            }
966            break;
967         case 0xd: /* Conditional Branch */
968            offs = (INT8)( insn & THUMB_INSN_IMM );
969            switch( ( insn & THUMB_COND_TYPE ) >> THUMB_COND_TYPE_SHIFT )
970            {
971               case COND_EQ:
972                     if( Z_IS_SET(GET_CPSR) )
973                     {
974                     R15 += 4 + (offs << 1);
975                  }
976                  else
977                  {
978                     R15 += 2;
979                  }
980                  break;
981               case COND_NE:
982                     if( Z_IS_CLEAR(GET_CPSR) )
983                     {
984                     R15 += 4 + (offs << 1);
985                  }
986                  else
987                  {
988                     R15 += 2;
989                  }
990                  break;
991               case COND_CS:
992                     if( C_IS_SET(GET_CPSR) )
993                     {
994                     R15 += 4 + (offs << 1);
995                  }
996                  else
997                  {
998                     R15 += 2;
999                  }
1000                  break;
1001               case COND_CC:
1002                     if( C_IS_CLEAR(GET_CPSR) )
1003                     {
1004                     R15 += 4 + (offs << 1);
1005                  }
1006                  else
1007                  {
1008                     R15 += 2;
1009                  }
1010                  break;
1011               case COND_MI:
1012                     if( N_IS_SET(GET_CPSR) )
1013                     {
1014                     R15 += 4 + (offs << 1);
1015                  }
1016                  else
1017                  {
1018                     R15 += 2;
1019                  }
1020                  break;
1021               case COND_PL:
1022                     if( N_IS_CLEAR(GET_CPSR) )
1023                     {
1024                     R15 += 4 + (offs << 1);
1025                  }
1026                  else
1027                  {
1028                     R15 += 2;
1029                  }
1030                  break;
1031               case COND_VS:
1032                     if( V_IS_SET(GET_CPSR) )
1033                     {
1034                     R15 += 4 + (offs << 1);
1035                  }
1036                  else
1037                  {
1038                     R15 += 2;
1039                  }
1040                  break;
1041               case COND_VC:
1042                     if( V_IS_CLEAR(GET_CPSR) )
1043                     {
1044                     R15 += 4 + (offs << 1);
1045                  }
1046                  else
1047                  {
1048                     R15 += 2;
1049                  }
1050                  break;
1051               case COND_HI:
1052                     if( C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR) )
1053                     {
1054                     R15 += 4 + (offs << 1);
1055                  }
1056                  else
1057                  {
1058                     R15 += 2;
1059                  }
1060                  break;
1061               case COND_LS:
1062                     if( C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR) )
1063                     {
1064                     R15 += 4 + (offs << 1);
1065                  }
1066                  else
1067                  {
1068                     R15 += 2;
1069                  }
1070                  break;
1071               case COND_GE:
1072                     if( !(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK) )
1073                     {
1074                     R15 += 4 + (offs << 1);
1075                  }
1076                  else
1077                  {
1078                     R15 += 2;
1079                  }
1080                  break;
1081               case COND_LT:
1082                     if( !(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK) )
1083                     {
1084                     R15 += 4 + (offs << 1);
1085                  }
1086                  else
1087                  {
1088                     R15 += 2;
1089                  }
1090                  break;
1091               case COND_GT:
1092                     if( Z_IS_CLEAR(GET_CPSR) && ( !(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK) ) )
1093                     {
1094                     R15 += 4 + (offs << 1);
1095                  }
1096                  else
1097                  {
1098                     R15 += 2;
1099                  }
1100                  break;
1101               case COND_LE:
1102                     if( Z_IS_SET(GET_CPSR) || ( !(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK) ) )
1103                     {
1104                     R15 += 4 + (offs << 1);
1105                  }
1106                  else
1107                  {
1108                     R15 += 2;
1109                  }
1110                  break;
1111               case COND_AL:
1112                  fatalerror("%08x: Undefined Thumb instruction: %04x (ARM9 reserved)\n", pc, insn);
1113                  R15 += 2;
1114                  break;
1115
1116               case COND_NV:   // SWI (this is sort of a "hole" in the opcode encoding)
1117                            ARM7.pendingSwi = 1;
1118                            ARM7_CHECKIRQ;
1119                  break;
1120            }
1121            break;
1122         case 0xe: /* B #offs */
1123                if( insn & THUMB_BLOP_LO )
1124                {
1125                    addr = GET_REGISTER(14);
1126                    addr += ( insn & THUMB_BLOP_OFFS ) << 1;
1127                    addr &= 0xfffffffc;
1128                    SET_REGISTER( 14, ( R15 + 4 ) | 1 );
1129                    R15 = addr;
1130                }
1131                else
1132                {
1133                    offs = ( insn & THUMB_BRANCH_OFFS ) << 1;
1134                    if( offs & 0x00000800 )
60            pc = R15;
61            insn = cpu_readop16(pc & (~1));
62            ARM7_ICOUNT -= (3 - thumbCycles[insn >> 8]);
63            switch ((insn & THUMB_INSN_TYPE) >> THUMB_INSN_TYPE_SHIFT)
64            {
65                case 0x0: /* Logical shifting */
66                    SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
67                    if (insn & THUMB_SHIFT_R) /* Shift right */
113568                    {
1136                        offs |= 0xfffff800;
69                        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
70                        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
71                        rrs = GET_REGISTER(rs);
72                        offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
73                        if (offs != 0)
74                        {
75                            SET_REGISTER(rd, rrs >> offs);
76                            if (rrs & (1 << (offs - 1)))
77                            {
78                                SET_CPSR(GET_CPSR | C_MASK);
79                            }
80                            else
81                            {
82                                SET_CPSR(GET_CPSR & ~C_MASK);
83                            }
84                        }
85                        else
86                        {
87                            SET_REGISTER(rd, 0);
88                            if (rrs & 0x80000000)
89                            {
90                                SET_CPSR(GET_CPSR | C_MASK);
91                            }
92                            else
93                            {
94                                SET_CPSR(GET_CPSR & ~C_MASK);
95                            }
96                        }
97                        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
98                        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
99                        R15 += 2;
1137100                    }
1138                    R15 += 4 + offs;
1139                }
1140            break;
1141         case 0xf: /* BL */
1142            if( insn & THUMB_BLOP_LO )
1143            {
1144               addr = GET_REGISTER(14);
1145               addr += ( insn & THUMB_BLOP_OFFS ) << 1;
1146               SET_REGISTER( 14, ( R15 + 2 ) | 1 );
1147               R15 = addr;
1148   //                      R15 += 2;
1149            }
1150            else
1151            {
1152               addr = ( insn & THUMB_BLOP_OFFS ) << 12;
1153               if( addr & ( 1 << 22 ) )
1154               {
1155                  addr |= 0xff800000;
1156               }
1157               addr += R15 + 4;
1158               SET_REGISTER( 14, addr );
1159               R15 += 2;
1160            }
1161            break;
1162         default:
1163            fatalerror("%08x: Undefined Thumb instruction: %04x\n", pc, insn);
1164            R15 += 2;
1165            break;
1166      }
1167   }
1168   else
1169   {
1170
1171        /* load 32 bit instruction */
1172        pc = R15;
1173        insn = cpu_readop32(pc);
1174
1175        /* process condition codes for this instruction */
1176        switch (insn >> INSN_COND_SHIFT)
1177        {
1178        case COND_EQ:
1179            if (Z_IS_CLEAR(GET_CPSR)) goto L_Next;
1180            break;
1181        case COND_NE:
1182            if (Z_IS_SET(GET_CPSR)) goto L_Next;
1183            break;
1184        case COND_CS:
1185            if (C_IS_CLEAR(GET_CPSR)) goto L_Next;
1186            break;
1187        case COND_CC:
1188            if (C_IS_SET(GET_CPSR)) goto L_Next;
1189            break;
1190        case COND_MI:
1191            if (N_IS_CLEAR(GET_CPSR)) goto L_Next;
1192            break;
1193        case COND_PL:
1194            if (N_IS_SET(GET_CPSR)) goto L_Next;
1195            break;
1196        case COND_VS:
1197            if (V_IS_CLEAR(GET_CPSR)) goto L_Next;
1198            break;
1199        case COND_VC:
1200            if (V_IS_SET(GET_CPSR)) goto L_Next;
1201            break;
1202        case COND_HI:
1203            if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR)) goto L_Next;
1204            break;
1205        case COND_LS:
1206            if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR)) goto L_Next;
1207            break;
1208        case COND_GE:
1209            if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK)) goto L_Next; /* Use x ^ (x >> ...) method */
1210            break;
1211        case COND_LT:
1212            if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK)) goto L_Next;
1213            break;
1214        case COND_GT:
1215            if (Z_IS_SET(GET_CPSR) || (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))) goto L_Next;
1216            break;
1217        case COND_LE:
1218            if (Z_IS_CLEAR(GET_CPSR) && (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))) goto L_Next;
1219            break;
1220        case COND_NV:
1221            goto L_Next;
1222        }
1223        /*******************************************************************/
1224        /* If we got here - condition satisfied, so decode the instruction */
1225        /*******************************************************************/
1226        switch( (insn & 0xF000000)>>24 )
1227        {
1228            case 0:
1229            case 1:
1230            case 2:
1231            case 3:
1232                /* Branch and Exchange (BX) */
1233                if( (insn&0x0ffffff0)==0x012fff10 )     //bits 27-4 == 000100101111111111110001
1234                {
1235                    R15 = GET_REGISTER(insn & 0x0f);
1236                    //If new PC address has A0 set, switch to Thumb mode
1237                    if(R15 & 1) {
1238                        SET_CPSR(GET_CPSR|T_MASK);
1239                       R15--;
101                    else /* Shift left */
102                    {
103                        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
104                        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
105                        rrs = GET_REGISTER(rs);
106                        offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
107                        if (offs != 0)
108                        {
109                            SET_REGISTER(rd, rrs << offs);
110                            if (rrs & (1 << (31 - (offs - 1))))
111                            {
112                                SET_CPSR(GET_CPSR | C_MASK);
113                            }
114                            else
115                            {
116                                SET_CPSR(GET_CPSR & ~C_MASK);
117                            }
118                        }
119                        else
120                        {
121                            SET_REGISTER(rd, rrs);
122                        }
123                        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
124                        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
125                        R15 += 2;
1240126                    }
1241                }
1242                else
1243                /* Multiply OR Swap OR Half Word Data Transfer */
1244                if( (insn & 0x0e000000)==0 && (insn & 0x80) && (insn & 0x10) )  //bits 27-25 == 000, bit 7=1, bit 4=1
1245                {
1246                    /* Half Word Data Transfer */
1247                    if(insn & 0x60)         //bits = 6-5 != 00
127                    break;
128                case 0x1: /* Arithmetic */
129                    if (insn & THUMB_INSN_ADDSUB)
1248130                    {
1249                        HandleHalfWordDT(insn);
131                        switch ((insn & THUMB_ADDSUB_TYPE) >> THUMB_ADDSUB_TYPE_SHIFT)
132                        {
133                            case 0x0: /* ADD Rd, Rs, Rn */
134                                rn = GET_REGISTER((insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
135                                rs = GET_REGISTER((insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
136                                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
137                                SET_REGISTER(rd, rs + rn);
138                                HandleThumbALUAddFlags(GET_REGISTER(rd), rs, rn);
139                                break;
140                            case 0x1: /* SUB Rd, Rs, Rn */
141                                rn = GET_REGISTER((insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
142                                rs = GET_REGISTER((insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
143                                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
144                                SET_REGISTER(rd, rs - rn);
145                                HandleThumbALUSubFlags(GET_REGISTER(rd), rs, rn);
146                                break;
147                            case 0x2: /* ADD Rd, Rs, #imm */
148                                imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
149                                rs = GET_REGISTER((insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
150                                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
151                                SET_REGISTER(rd, rs + imm);
152                                HandleThumbALUAddFlags(GET_REGISTER(rd), rs, imm);
153                                break;
154                            case 0x3: /* SUB Rd, Rs, #imm */
155                                imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
156                                rs = GET_REGISTER((insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
157                                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
158                                SET_REGISTER(rd, rs - imm);
159                                HandleThumbALUSubFlags(GET_REGISTER(rd), rs,imm);
160                                break;
161                            default:
162                                fatalerror("%08x: G1 Undefined Thumb instruction: %04x\n", pc, insn);
163                                R15 += 2;
164                                break;
165                        }
1250166                    }
1251167                    else
1252                    /* Swap */
1253                    if(insn & 0x01000000)   //bit 24 = 1
1254168                    {
1255                        HandleSwap(insn);
169                        /* ASR.. */
170                        //if (insn & THUMB_SHIFT_R) /* Shift right */
171                        {
172                            rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
173                            rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
174                            rrs = GET_REGISTER(rs);
175                            offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
176                            if (offs == 0)
177                            {
178                                offs = 32;
179                            }
180                            if (offs >= 32)
181                            {
182                                if (rrs >> 31)
183                                {
184                                    SET_CPSR(GET_CPSR | C_MASK);
185                                }
186                                else
187                                {
188                                    SET_CPSR(GET_CPSR & ~C_MASK);
189                                }
190                                SET_REGISTER(rd, (rrs & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
191                            }
192                            else
193                            {
194                                if ((rrs >> (offs - 1)) & 1)
195                                {
196                                    SET_CPSR(GET_CPSR | C_MASK);
197                                }
198                                else
199                                {
200                                    SET_CPSR(GET_CPSR & ~C_MASK);
201                                }
202                                SET_REGISTER(rd,
203                                             (rrs & 0x80000000)
204                                             ? ((0xFFFFFFFF << (32 - offs)) | (rrs >> offs))
205                                             : (rrs >> offs));
206                            }
207                            SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
208                            SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
209                            R15 += 2;
210                        }
1256211                    }
1257                    /* Multiply Or Multiply Long */
212                    break;
213                case 0x2: /* CMP / MOV */
214                    if (insn & THUMB_INSN_CMP)
215                    {
216                        rn = GET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
217                        op2 = insn & THUMB_INSN_IMM;
218                        rd = rn - op2;
219                        HandleThumbALUSubFlags(rd, rn, op2);
220                        //mame_printf_debug("%08x: xxx Thumb instruction: CMP R%d (%08x), %02x (N=%d, Z=%d, C=%d, V=%d)\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, GET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT), op2, N_IS_SET(GET_CPSR) ? 1 : 0, Z_IS_SET(GET_CPSR) ? 1 : 0, C_IS_SET(GET_CPSR) ? 1 : 0, V_IS_SET(GET_CPSR) ? 1 : 0);
221                    }
1258222                    else
1259223                    {
1260                        /* multiply long */
1261                        if( insn&0x800000 ) //Bit 23 = 1 for Multiply Long
224                        rd = (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT;
225                        op2 = (insn & THUMB_INSN_IMM);
226                        SET_REGISTER(rd, op2);
227                        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
228                        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
229                        R15 += 2;
230                    }
231                    break;
232                case 0x3: /* ADD/SUB immediate */
233                    if (insn & THUMB_INSN_SUB) /* SUB Rd, #Offset8 */
234                    {
235                        rn = GET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
236                        op2 = insn & THUMB_INSN_IMM;
237                        //mame_printf_debug("%08x:  Thumb instruction: SUB R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
238                        rd = rn - op2;
239                        SET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
240                        HandleThumbALUSubFlags(rd, rn, op2);
241                    }
242                    else /* ADD Rd, #Offset8 */
243                    {
244                        rn = GET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
245                        op2 = insn & THUMB_INSN_IMM;
246                        rd = rn + op2;
247                        //mame_printf_debug("%08x:  Thumb instruction: ADD R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
248                        SET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
249                        HandleThumbALUAddFlags(rd, rn, op2);
250                    }
251                    break;
252                case 0x4: /* Rd & Rm instructions */
253                    switch ((insn & THUMB_GROUP4_TYPE) >> THUMB_GROUP4_TYPE_SHIFT)
254                    {
255                        case 0x0:
256                            switch ((insn & THUMB_ALUOP_TYPE) >> THUMB_ALUOP_TYPE_SHIFT)
257                            {
258                                case 0x0: /* AND Rd, Rs */
259                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
260                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
261                                    SET_REGISTER(rd, GET_REGISTER(rd) & GET_REGISTER(rs));
262                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
263                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
264                                    R15 += 2;
265                                    break;
266                                case 0x1: /* EOR Rd, Rs */
267                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
268                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
269                                    SET_REGISTER(rd, GET_REGISTER(rd) ^ GET_REGISTER(rs));
270                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
271                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
272                                    R15 += 2;
273                                    break;
274                                case 0x2: /* LSL Rd, Rs */
275                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
276                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
277                                    rrd = GET_REGISTER(rd);
278                                    offs = GET_REGISTER(rs) & 0x000000ff;
279                                    if (offs > 0)
280                                    {
281                                        if (offs < 32)
282                                        {
283                                            SET_REGISTER(rd, rrd << offs);
284                                            if (rrd & (1 << (31 - (offs - 1))))
285                                            {
286                                                SET_CPSR(GET_CPSR | C_MASK);
287                                            }
288                                            else
289                                            {
290                                                SET_CPSR(GET_CPSR & ~C_MASK);
291                                            }
292                                        }
293                                        else if (offs == 32)
294                                        {
295                                            SET_REGISTER(rd, 0);
296                                            if (rrd & 1)
297                                            {
298                                                SET_CPSR(GET_CPSR | C_MASK);
299                                            }
300                                            else
301                                            {
302                                                SET_CPSR(GET_CPSR & ~C_MASK);
303                                            }
304                                        }
305                                        else
306                                        {
307                                            SET_REGISTER(rd, 0);
308                                            SET_CPSR(GET_CPSR & ~C_MASK);
309                                        }
310                                    }
311                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
312                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
313                                    R15 += 2;
314                                    break;
315                                case 0x3: /* LSR Rd, Rs */
316                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
317                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
318                                    rrd = GET_REGISTER(rd);
319                                    offs = GET_REGISTER(rs) & 0x000000ff;
320                                    if (offs >  0)
321                                    {
322                                        if (offs < 32)
323                                        {
324                                            SET_REGISTER(rd, rrd >> offs);
325                                            if (rrd & (1 << (offs - 1)))
326                                            {
327                                                SET_CPSR(GET_CPSR | C_MASK);
328                                            }
329                                            else
330                                            {
331                                                SET_CPSR(GET_CPSR & ~C_MASK);
332                                            }
333                                        }
334                                        else if (offs == 32)
335                                        {
336                                            SET_REGISTER(rd, 0);
337                                            if (rrd & 0x80000000)
338                                            {
339                                                SET_CPSR(GET_CPSR | C_MASK);
340                                            }
341                                            else
342                                            {
343                                                SET_CPSR(GET_CPSR & ~C_MASK);
344                                            }
345                                        }
346                                        else
347                                        {
348                                            SET_REGISTER(rd, 0);
349                                            SET_CPSR(GET_CPSR & ~C_MASK);
350                                        }
351                                    }
352                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
353                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
354                                    R15 += 2;
355                                    break;
356                                case 0x4: /* ASR Rd, Rs */
357                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
358                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
359                                    rrs = GET_REGISTER(rs)&0xff;
360                                    rrd = GET_REGISTER(rd);
361                                    if (rrs != 0)
362                                    {
363                                        if (rrs >= 32)
364                                        {
365                                            if (rrd >> 31)
366                                            {
367                                                SET_CPSR(GET_CPSR | C_MASK);
368                                            }
369                                            else
370                                            {
371                                                SET_CPSR(GET_CPSR & ~C_MASK);
372                                            }
373                                            SET_REGISTER(rd, (GET_REGISTER(rd) & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
374                                        }
375                                        else
376                                        {
377                                            if ((rrd >> (rrs-1)) & 1)
378                                            {
379                                                SET_CPSR(GET_CPSR | C_MASK);
380                                            }
381                                            else
382                                            {
383                                                SET_CPSR(GET_CPSR & ~C_MASK);
384                                            }
385                                            SET_REGISTER(rd, (rrd & 0x80000000)
386                                                         ? ((0xFFFFFFFF << (32 - rrs)) | (rrd >> rrs))
387                                                         : (rrd >> rrs));
388                                        }
389                                    }
390                                    SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
391                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
392                                    R15 += 2;
393                                    break;
394                                case 0x5: /* ADC Rd, Rs */
395                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
396                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
397                                    op2=(GET_CPSR & C_MASK) ? 1 : 0;
398                                    rn=GET_REGISTER(rd) + GET_REGISTER(rs) + op2;
399                                    HandleThumbALUAddFlags(rn, GET_REGISTER(rd), (GET_REGISTER(rs))); // ?
400                                    SET_REGISTER(rd, rn);
401                                    break;
402                                case 0x6: /* SBC Rd, Rs */
403                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
404                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
405                                    op2=(GET_CPSR & C_MASK) ? 0 : 1;
406                                    rn=GET_REGISTER(rd) - GET_REGISTER(rs) - op2;
407                                    HandleThumbALUSubFlags(rn, GET_REGISTER(rd), (GET_REGISTER(rs))); //?
408                                    SET_REGISTER(rd, rn);
409                                    break;
410                                case 0x7: /* ROR Rd, Rs */
411                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
412                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
413                                    rrd = GET_REGISTER(rd);
414                                    imm = GET_REGISTER(rs) & 0x0000001f;
415                                    SET_REGISTER(rd, (rrd >> imm) | (rrd << (32 - imm)));
416                                    if (rrd & (1 << (imm - 1)))
417                                    {
418                                        SET_CPSR(GET_CPSR | C_MASK);
419                                    }
420                                    else
421                                    {
422                                        SET_CPSR(GET_CPSR & ~C_MASK);
423                                    }
424                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
425                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
426                                    R15 += 2;
427                                    break;
428                                case 0x8: /* TST Rd, Rs */
429                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
430                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
431                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
432                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd) & GET_REGISTER(rs)));
433                                    R15 += 2;
434                                    break;
435                                case 0x9: /* NEG Rd, Rs - todo: check me */
436                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
437                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
438                                    rrs = GET_REGISTER(rs);
439                                    rn = 0 - rrs;
440                                    SET_REGISTER(rd, rn);
441                                    HandleThumbALUSubFlags(GET_REGISTER(rd), 0, rrs);
442                                    break;
443                                case 0xa: /* CMP Rd, Rs */
444                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
445                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
446                                    rn = GET_REGISTER(rd) - GET_REGISTER(rs);
447                                    HandleThumbALUSubFlags(rn, GET_REGISTER(rd), GET_REGISTER(rs));
448                                    break;
449                                case 0xb: /* CMN Rd, Rs - check flags, add dasm */
450                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
451                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
452                                    rn = GET_REGISTER(rd) + GET_REGISTER(rs);
453                                    HandleThumbALUAddFlags(rn, GET_REGISTER(rd), GET_REGISTER(rs));
454                                    break;
455                                case 0xc: /* ORR Rd, Rs */
456                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
457                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
458                                    SET_REGISTER(rd, GET_REGISTER(rd) | GET_REGISTER(rs));
459                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
460                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
461                                    R15 += 2;
462                                    break;
463                                case 0xd: /* MUL Rd, Rs */
464                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
465                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
466                                    rn = GET_REGISTER(rd) * GET_REGISTER(rs);
467                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
468                                    SET_REGISTER(rd, rn);
469                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(rn));
470                                    R15 += 2;
471                                    break;
472                                case 0xe: /* BIC Rd, Rs */
473                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
474                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
475                                    SET_REGISTER(rd, GET_REGISTER(rd) & (~GET_REGISTER(rs)));
476                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
477                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
478                                    R15 += 2;
479                                    break;
480                                case 0xf: /* MVN Rd, Rs */
481                                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
482                                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
483                                    op2 = GET_REGISTER(rs);
484                                    SET_REGISTER(rd, ~op2);
485                                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
486                                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(rd)));
487                                    R15 += 2;
488                                    break;
489                                default:
490                                    fatalerror("%08x: G4-0 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_ALUOP_TYPE) >> THUMB_ALUOP_TYPE_SHIFT);
491                                    R15 += 2;
492                                    break;
493                            }
494                            break;
495                        case 0x1:
496                            switch ((insn & THUMB_HIREG_OP) >> THUMB_HIREG_OP_SHIFT)
497                            {
498                                case 0x0: /* ADD Rd, Rs */
499                                    rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
500                                    rd = insn & THUMB_HIREG_RD;
501                                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
502                                    {
503                                        case 0x1: /* ADD Rd, HRs */
504                                            SET_REGISTER(rd, GET_REGISTER(rd) + GET_REGISTER(rs+8));
505                                            // emulate the effects of pre-fetch
506                                            if (rs == 7)
507                                            {
508                                                SET_REGISTER(rd, GET_REGISTER(rd) + 4);
509                                            }
510                                            break;
511                                        case 0x2: /* ADD HRd, Rs */
512                                            SET_REGISTER(rd+8, GET_REGISTER(rd+8) + GET_REGISTER(rs));
513                                            if (rd == 7)
514                                            {
515                                                R15 += 2;
516                                                change_pc(R15);
517                                            }
518                                            break;
519                                        case 0x3: /* Add HRd, HRs */
520                                            SET_REGISTER(rd+8, GET_REGISTER(rd+8) + GET_REGISTER(rs+8));
521                                            // emulate the effects of pre-fetch
522                                            if (rs == 7)
523                                            {
524                                                SET_REGISTER(rd+8, GET_REGISTER(rd+8) + 4);
525                                            }
526                                            if (rd == 7)
527                                            {
528                                                R15 += 2;
529                                                change_pc(R15);
530                                            }
531                                            break;
532                                        default:
533                                            fatalerror("%08x: G4-1-0 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
534                                            break;
535                                    }
536                                    R15 += 2;
537                                    break;
538                                case 0x1: /* CMP */
539                                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
540                                    {
541                                        case 0x0: /* CMP Rd, Rs */
542                                            rs = GET_REGISTER(((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
543                                            rd = GET_REGISTER(insn & THUMB_HIREG_RD);
544                                            rn = rd - rs;
545                                            HandleThumbALUSubFlags(rn, rd, rs);
546                                            break;
547                                        case 0x1: /* CMP Rd, Hs */
548                                            rs = GET_REGISTER(((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
549                                            rd = GET_REGISTER(insn & THUMB_HIREG_RD);
550                                            rn = rd - rs;
551                                            HandleThumbALUSubFlags(rn, rd, rs);
552                                            break;
553                                        case 0x2: /* CMP Hd, Rs */
554                                            rs = GET_REGISTER(((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
555                                            rd = GET_REGISTER((insn & THUMB_HIREG_RD) + 8);
556                                            rn = rd - rs;
557                                            HandleThumbALUSubFlags(rn, rd, rs);
558                                            break;
559                                        case 0x3: /* CMP Hd, Hs */
560                                            rs = GET_REGISTER(((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
561                                            rd = GET_REGISTER((insn & THUMB_HIREG_RD) + 8);
562                                            rn = rd - rs;
563                                            HandleThumbALUSubFlags(rn, rd, rs);
564                                            break;
565                                        default:
566                                            fatalerror("%08x: G4-1 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
567                                            R15 += 2;
568                                            break;
569                                    }
570                                    break;
571                                case 0x2: /* MOV */
572                                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
573                                    {
574                                        case 0x1:       // MOV Rd, Hs
575                                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
576                                            rd = insn & THUMB_HIREG_RD;
577                                            if (rs == 7)
578                                            {
579                                                SET_REGISTER(rd, GET_REGISTER(rs + 8) + 4);
580                                            }
581                                            else
582                                            {
583                                                SET_REGISTER(rd, GET_REGISTER(rs + 8));
584                                            }
585                                            R15 += 2;
586                                            break;
587                                        case 0x2:       // MOV Hd, Rs
588                                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
589                                            rd = insn & THUMB_HIREG_RD;
590                                            SET_REGISTER(rd + 8, GET_REGISTER(rs));
591                                            if (rd != 7)
592                                            {
593                                                R15 += 2;
594                                            }
595                                            else
596                                            {
597                                                R15 &= ~1;
598                                                change_pc(R15);
599                                            }
600                                            break;
601                                        case 0x3:       // MOV Hd, Hs
602                                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
603                                            rd = insn & THUMB_HIREG_RD;
604                                            if (rs == 7)
605                                            {
606                                                SET_REGISTER(rd + 8, GET_REGISTER(rs+8)+4);
607                                            }
608                                            else
609                                            {
610                                                SET_REGISTER(rd + 8, GET_REGISTER(rs+8));
611                                            }
612                                            if (rd != 7)
613                                            {
614                                                R15 += 2;
615                                            }
616                                            if (rd == 7)
617                                            {
618                                                R15 &= ~1;
619                                                change_pc(R15);
620                                            }
621                                            break;
622                                        default:
623                                            fatalerror("%08x: G4-2 Undefined Thumb instruction: %04x (%x)\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
624                                            R15 += 2;
625                                            break;
626                                    }
627                                    break;
628                                case 0x3:
629                                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
630                                    {
631                                        case 0x0:
632                                            rd = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
633                                            addr = GET_REGISTER(rd);
634                                            if (addr & 1)
635                                            {
636                                                addr &= ~1;
637                                            }
638                                            else
639                                            {
640                                                SET_CPSR(GET_CPSR & ~T_MASK);
641                                                if (addr & 2)
642                                                {
643                                                    addr += 2;
644                                                }
645                                            }
646                                            R15 = addr;
647                                            break;
648                                        case 0x1:
649                                            addr = GET_REGISTER(((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
650                                            if ((((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8) == 15)
651                                            {
652                                                addr += 2;
653                                            }
654                                            if (addr & 1)
655                                            {
656                                                addr &= ~1;
657                                            }
658                                            else
659                                            {
660                                                SET_CPSR(GET_CPSR & ~T_MASK);
661                                                if (addr & 2)
662                                                {
663                                                    addr += 2;
664                                                }
665                                            }
666                                            R15 = addr;
667                                            break;
668                                        default:
669                                            fatalerror("%08x: G4-3 Undefined Thumb instruction: %04x\n", pc, insn);
670                                            R15 += 2;
671                                            break;
672                                    }
673                                    break;
674                                default:
675                                    fatalerror("%08x: G4-x Undefined Thumb instruction: %04x\n", pc, insn);
676                                    R15 += 2;
677                                    break;
678                            }
679                            break;
680                        case 0x2:
681                        case 0x3:
682                            readword = READ32((R15 & ~2) + 4 + ((insn & THUMB_INSN_IMM) << 2));
683                            SET_REGISTER((insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, readword);
684                            R15 += 2;
685                            break;
686                        default:
687                            fatalerror("%08x: G4-y Undefined Thumb instruction: %04x\n", pc, insn);
688                            R15 += 2;
689                            break;
690                    }
691                    break;
692                case 0x5: /* LDR* STR* */
693                    switch ((insn & THUMB_GROUP5_TYPE) >> THUMB_GROUP5_TYPE_SHIFT)
694                    {
695                        case 0x0: /* STR Rd, [Rn, Rm] */
696                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
697                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
698                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
699                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
700                            WRITE32(addr, GET_REGISTER(rd));
701                            R15 += 2;
702                            break;
703                        case 0x1: /* STRH Rd, [Rn, Rm] */
704                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
705                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
706                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
707                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
708                            WRITE16(addr, GET_REGISTER(rd));
709                            R15 += 2;
710                            break;
711                        case 0x2: /* STRB Rd, [Rn, Rm] */
712                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
713                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
714                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
715                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
716                            WRITE8(addr, GET_REGISTER(rd));
717                            R15 += 2;
718                            break;
719                        case 0x3: /* LDSB Rd, [Rn, Rm] todo, add dasm */
720                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
721                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
722                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
723                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
724                            op2 = READ8(addr);
725                            if (op2 & 0x00000080)
726                            {
727                                op2 |= 0xffffff00;
728                            }
729                            SET_REGISTER(rd, op2);
730                            R15 += 2;
731                            break;
732                        case 0x4: /* LDR Rd, [Rn, Rm] */
733                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
734                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
735                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
736                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
737                            op2 = READ32(addr);
738                            SET_REGISTER(rd, op2);
739                            R15 += 2;
740                            break;
741                        case 0x5: /* LDRH Rd, [Rn, Rm] */
742                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
743                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
744                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
745                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
746                            op2 = READ16(addr);
747                            SET_REGISTER(rd, op2);
748                            R15 += 2;
749                            break;
750                        case 0x6: /* LDRB Rd, [Rn, Rm] */
751                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
752                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
753                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
754                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
755                            op2 = READ8(addr);
756                            SET_REGISTER(rd, op2);
757                            R15 += 2;
758                            break;
759                        case 0x7: /* LDSH Rd, [Rn, Rm] */
760                            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
761                            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
762                            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
763                            addr = GET_REGISTER(rn) + GET_REGISTER(rm);
764                            op2 = READ16(addr);
765                            if (op2 & 0x00008000)
766                            {
767                                op2 |= 0xffff0000;
768                            }
769                            SET_REGISTER(rd, op2);
770                            R15 += 2;
771                            break;
772                        default:
773                            fatalerror("%08x: G5 Undefined Thumb instruction: %04x\n", pc, insn);
774                            R15 += 2;
775                            break;
776                    }
777                    break;
778                case 0x6: /* Word Store w/ Immediate Offset */
779                    if (insn & THUMB_LSOP_L) /* Load */
780                    {
781                        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
782                        rd = insn & THUMB_ADDSUB_RD;
783                        offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
784                        SET_REGISTER(rd, READ32(GET_REGISTER(rn) + offs)); // fix
785                        R15 += 2;
786                    }
787                    else /* Store */
788                    {
789                        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
790                        rd = insn & THUMB_ADDSUB_RD;
791                        offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
792                        WRITE32(GET_REGISTER(rn) + offs, GET_REGISTER(rd));
793                        R15 += 2;
794                    }
795                    break;
796                case 0x7: /* Byte Store w/ Immeidate Offset */
797                    if (insn & THUMB_LSOP_L) /* Load */
798                    {
799                        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
800                        rd = insn & THUMB_ADDSUB_RD;
801                        offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
802                        SET_REGISTER(rd, READ8(GET_REGISTER(rn) + offs));
803                        R15 += 2;
804                    }
805                    else /* Store */
806                    {
807                        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
808                        rd = insn & THUMB_ADDSUB_RD;
809                        offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
810                        WRITE8(GET_REGISTER(rn) + offs, GET_REGISTER(rd));
811                        R15 += 2;
812                    }
813                    break;
814                case 0x8: /* Load/Store Halfword */
815                    if (insn & THUMB_HALFOP_L) /* Load */
816                    {
817                        imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
818                        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
819                        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
820                        SET_REGISTER(rd, READ16(GET_REGISTER(rs) + (imm << 1)));
821                        R15 += 2;
822                    }
823                    else /* Store */
824                    {
825                        imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
826                        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
827                        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
828                        WRITE16(GET_REGISTER(rs) + (imm << 1), GET_REGISTER(rd));
829                        R15 += 2;
830                    }
831                    break;
832                case 0x9: /* Stack-Relative Load/Store */
833                    if (insn & THUMB_STACKOP_L)
834                    {
835                        rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
836                        offs = (UINT8)(insn & THUMB_INSN_IMM);
837                        readword = READ32(GET_REGISTER(13) + ((UINT32)offs << 2));
838                        SET_REGISTER(rd, readword);
839                        R15 += 2;
840                    }
841                    else
842                    {
843                        rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
844                        offs = (UINT8)(insn & THUMB_INSN_IMM);
845                        WRITE32(GET_REGISTER(13) + ((UINT32)offs << 2), GET_REGISTER(rd));
846                        R15 += 2;
847                    }
848                    break;
849                case 0xa: /* Get relative address */
850                    if (insn & THUMB_RELADDR_SP) /* ADD Rd, SP, #nn */
851                    {
852                        rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
853                        offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
854                        SET_REGISTER(rd, GET_REGISTER(13) + offs);
855                        R15 += 2;
856                    }
857                    else /* ADD Rd, PC, #nn */
858                    {
859                        rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
860                        offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
861                        SET_REGISTER(rd, ((R15 + 4) & ~2) + offs);
862                        R15 += 2;
863                    }
864                    break;
865                case 0xb: /* Stack-Related Opcodes */
866                    switch ((insn & THUMB_STACKOP_TYPE) >> THUMB_STACKOP_TYPE_SHIFT)
867                    {
868                        case 0x0: /* ADD SP, #imm */
869                            addr = (insn & THUMB_INSN_IMM);
870                            addr &= ~THUMB_INSN_IMM_S;
871                            SET_REGISTER(13, GET_REGISTER(13) + ((insn & THUMB_INSN_IMM_S) ? -(addr << 2) : (addr << 2)));
872                            R15 += 2;
873                            break;
874                        case 0x4: /* PUSH {Rlist} */
875                            for (offs = 7; offs >= 0; offs--)
876                            {
877                                if (insn & (1 << offs))
878                                {
879                                    SET_REGISTER(13, GET_REGISTER(13) - 4);
880                                    WRITE32(GET_REGISTER(13), GET_REGISTER(offs));
881                                }
882                            }
883                            R15 += 2;
884                            break;
885                        case 0x5: /* PUSH {Rlist}{LR} */
886                            SET_REGISTER(13, GET_REGISTER(13) - 4);
887                            WRITE32(GET_REGISTER(13), GET_REGISTER(14));
888                            for (offs = 7; offs >= 0; offs--)
889                            {
890                                if (insn & (1 << offs))
891                                {
892                                    SET_REGISTER(13, GET_REGISTER(13) - 4);
893                                    WRITE32(GET_REGISTER(13), GET_REGISTER(offs));
894                                }
895                            }
896                            R15 += 2;
897                            break;
898                        case 0xc: /* POP {Rlist} */
899                            for (offs = 0; offs < 8; offs++)
900                            {
901                                if (insn & (1 << offs))
902                                {
903                                    SET_REGISTER(offs, READ32(GET_REGISTER(13)));
904                                    SET_REGISTER(13, GET_REGISTER(13) + 4);
905                                }
906                            }
907                            R15 += 2;
908                            break;
909                        case 0xd: /* POP {Rlist}{PC} */
910                            for (offs = 0; offs < 8; offs++)
911                            {
912                                if (insn & (1 << offs))
913                                {
914                                    SET_REGISTER(offs, READ32(GET_REGISTER(13)));
915                                    SET_REGISTER(13, GET_REGISTER(13) + 4);
916                                }
917                            }
918                            R15 = READ32(GET_REGISTER(13)) & ~1;
919                            SET_REGISTER(13, GET_REGISTER(13) + 4);
920                            break;
921                        default:
922                            fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
923                            R15 += 2;
924                            break;
925                    }
926                    break;
927                case 0xc: /* Multiple Load/Store */
928                    {
929                        UINT32 ld_st_address;
930
931                        rd = (insn & THUMB_MULTLS_BASE) >> THUMB_MULTLS_BASE_SHIFT;
932                        ld_st_address = GET_REGISTER(rd) & 0xfffffffc;
933
934                        if (insn & THUMB_MULTLS) /* Load */
1262935                        {
1263                            /* Signed? */
1264                            if( insn&0x00400000 )
1265                                HandleSMulLong(insn);
1266                            else
1267                                HandleUMulLong(insn);
936                            int rd_in_list;
937
938                            rd_in_list = insn & (1 << rd);
939                            for (offs = 0; offs < 8; offs++)
940                            {
941                                if (insn & (1 << offs))
942                                {
943                                    SET_REGISTER(offs, READ32(ld_st_address));
944                                    ld_st_address += 4;
945                                }
946                            }
947                            if (!rd_in_list)
948                                SET_REGISTER(rd, ld_st_address);
949                            R15 += 2;
1268950                        }
1269                        /* multiply */
1270                        else
951                        else /* Store */
1271952                        {
1272                            HandleMul(insn);
953                            for (offs = 0; offs < 8; offs++)
954                            {
955                                if (insn & (1 << offs))
956                                {
957                                    WRITE32(ld_st_address, GET_REGISTER(offs));
958                                    ld_st_address += 4;
959                                }
960                            }
961                            SET_REGISTER(rd, ld_st_address);
962                            R15 += 2;
1273963                        }
1274                        R15 += 4;
1275964                    }
1276                }
1277                else
1278                /* Data Processing OR PSR Transfer */
1279                if( (insn & 0x0c000000) ==0 )   //bits 27-26 == 00 - This check can only exist properly after Multiplication check above
1280                {
1281                    /* PSR Transfer (MRS & MSR) */
1282                    if( ((insn&0x00100000)==0) && ((insn&0x01800000)==0x01000000) ) //( S bit must be clear, and bit 24,23 = 10 )
965                    break;
966                case 0xd: /* Conditional Branch */
967                    offs = (INT8)(insn & THUMB_INSN_IMM);
968                    switch ((insn & THUMB_COND_TYPE) >> THUMB_COND_TYPE_SHIFT)
1283969                    {
1284                        HandlePSRTransfer(insn);
1285                        ARM7_ICOUNT += 2;       //PSR only takes 1 - S Cycle, so we add + 2, since at end, we -3..
1286                        R15 += 4;
970                        case COND_EQ:
971                            if (Z_IS_SET(GET_CPSR))
972                            {
973                                R15 += 4 + (offs << 1);
974                            }
975                            else
976                            {
977                                R15 += 2;
978                            }
979                            break;
980                        case COND_NE:
981                            if (Z_IS_CLEAR(GET_CPSR))
982                            {
983                                R15 += 4 + (offs << 1);
984                            }
985                            else
986                            {
987                                R15 += 2;
988                            }
989                            break;
990                        case COND_CS:
991                            if (C_IS_SET(GET_CPSR))
992                            {
993                                R15 += 4 + (offs << 1);
994                            }
995                            else
996                            {
997                                R15 += 2;
998                            }
999                            break;
1000                        case COND_CC:
1001                            if (C_IS_CLEAR(GET_CPSR))
1002                            {
1003                                R15 += 4 + (offs << 1);
1004                            }
1005                            else
1006                            {
1007                                R15 += 2;
1008                            }
1009                            break;
1010                        case COND_MI:
1011                            if (N_IS_SET(GET_CPSR))
1012                            {
1013                                R15 += 4 + (offs << 1);
1014                            }
1015                            else
1016                            {
1017                                R15 += 2;
1018                            }
1019                            break;
1020                        case COND_PL:
1021                            if (N_IS_CLEAR(GET_CPSR))
1022                            {
1023                                R15 += 4 + (offs << 1);
1024                            }
1025                            else
1026                            {
1027                                R15 += 2;
1028                            }
1029                            break;
1030                        case COND_VS:
1031                            if (V_IS_SET(GET_CPSR))
1032                            {
1033                                R15 += 4 + (offs << 1);
1034                            }
1035                            else
1036                            {
1037                                R15 += 2;
1038                            }
1039                            break;
1040                        case COND_VC:
1041                            if (V_IS_CLEAR(GET_CPSR))
1042                            {
1043                                R15 += 4 + (offs << 1);
1044                            }
1045                            else
1046                            {
1047                                R15 += 2;
1048                            }
1049                            break;
1050                        case COND_HI:
1051                            if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR))
1052                            {
1053                                R15 += 4 + (offs << 1);
1054                            }
1055                            else
1056                            {
1057                                R15 += 2;
1058                            }
1059                            break;
1060                        case COND_LS:
1061                            if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR))
1062                            {
1063                                R15 += 4 + (offs << 1);
1064                            }
1065                            else
1066                            {
1067                                R15 += 2;
1068                            }
1069                            break;
1070                        case COND_GE:
1071                            if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
1072                            {
1073                                R15 += 4 + (offs << 1);
1074                            }
1075                            else
1076                            {
1077                                R15 += 2;
1078                            }
1079                            break;
1080                        case COND_LT:
1081                            if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
1082                            {
1083                                R15 += 4 + (offs << 1);
1084                            }
1085                            else
1086                            {
1087                                R15 += 2;
1088                            }
1089                            break;
1090                        case COND_GT:
1091                            if (Z_IS_CLEAR(GET_CPSR) && !(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
1092                            {
1093                                R15 += 4 + (offs << 1);
1094                            }
1095                            else
1096                            {
1097                                R15 += 2;
1098                            }
1099                            break;
1100                        case COND_LE:
1101                            if (Z_IS_SET(GET_CPSR) || !(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
1102                            {
1103                                R15 += 4 + (offs << 1);
1104                            }
1105                            else
1106                            {
1107                                R15 += 2;
1108                            }
1109                            break;
1110                        case COND_AL:
1111                            fatalerror("%08x: Undefined Thumb instruction: %04x (ARM9 reserved)\n", pc, insn);
1112                            R15 += 2;
1113                            break;
1114                        case COND_NV:   // SWI (this is sort of a "hole" in the opcode encoding)
1115                            ARM7.pendingSwi = 1;
1116                            ARM7_CHECKIRQ;
1117                            break;
12871118                    }
1288                    /* Data Processing */
1119                    break;
1120                case 0xe: /* B #offs */
1121                    if (insn & THUMB_BLOP_LO)
1122                    {
1123                        addr = GET_REGISTER(14);
1124                        addr += (insn & THUMB_BLOP_OFFS) << 1;
1125                        addr &= 0xfffffffc;
1126                        SET_REGISTER(14, (R15 + 4) | 1);
1127                        R15 = addr;
1128                    }
12891129                    else
12901130                    {
1291                        HandleALU(insn);
1131                        offs = (insn & THUMB_BRANCH_OFFS) << 1;
1132                        if (offs & 0x00000800)
1133                        {
1134                            offs |= 0xfffff800;
1135                        }
1136                        R15 += 4 + offs;
12921137                    }
1293                }
1138                    break;
1139                case 0xf: /* BL */
1140                    if (insn & THUMB_BLOP_LO)
1141                    {
1142                        addr = GET_REGISTER(14);
1143                        addr += (insn & THUMB_BLOP_OFFS) << 1;
1144                        SET_REGISTER(14, (R15 + 2) | 1);
1145                        R15 = addr;
1146                        //R15 += 2;
1147                    }
1148                    else
1149                    {
1150                        addr = (insn & THUMB_BLOP_OFFS) << 12;
1151                        if (addr & (1 << 22))
1152                        {
1153                            addr |= 0xff800000;
1154                        }
1155                        addr += R15 + 4;
1156                        SET_REGISTER(14, addr);
1157                        R15 += 2;
1158                    }
1159                    break;
1160                default:
1161                    fatalerror("%08x: Undefined Thumb instruction: %04x\n", pc, insn);
1162                    R15 += 2;
1163                    break;
1164            }
1165        }
1166        else
1167        {
1168
1169            /* load 32 bit instruction */
1170            pc = R15;
1171            insn = cpu_readop32(pc);
1172
1173            /* process condition codes for this instruction */
1174            switch (insn >> INSN_COND_SHIFT)
1175            {
1176            case COND_EQ:
1177                if (Z_IS_CLEAR(GET_CPSR))
1178                    goto L_Next;
12941179                break;
1295            /* Data Transfer - Single Data Access */
1296            case 4:
1297            case 5:
1298            case 6:
1299            case 7:
1300                HandleMemSingle(insn);
1301                R15 += 4;
1180            case COND_NE:
1181                if (Z_IS_SET(GET_CPSR))
1182                    goto L_Next;
13021183                break;
1303            /* Block Data Transfer/Access */
1304            case 8:
1305            case 9:
1306                HandleMemBlock(insn);
1307                R15 += 4;
1184            case COND_CS:
1185                if (C_IS_CLEAR(GET_CPSR))
1186                    goto L_Next;
13081187                break;
1309            /* Branch or Branch & Link */
1310            case 0xa:
1311            case 0xb:
1312                HandleBranch(insn);
1188            case COND_CC:
1189                if (C_IS_SET(GET_CPSR))
1190                    goto L_Next;
13131191                break;
1314            /* Co-Processor Data Transfer */
1315            case 0xc:
1316            case 0xd:
1317                HandleCoProcDT(insn);
1318                R15 += 4;
1192            case COND_MI:
1193                if (N_IS_CLEAR(GET_CPSR))
1194                    goto L_Next;
13191195                break;
1320            /* Co-Processor Data Operation or Register Transfer */
1321            case 0xe:
1322                if(insn & 0x10)
1323                    HandleCoProcRT(insn);
1324                else
1325                    HandleCoProcDO(insn);
1326                R15 += 4;
1196            case COND_PL:
1197                if (N_IS_SET(GET_CPSR))
1198                    goto L_Next;
13271199                break;
1328            /* Software Interrupt */
1329            case 0x0f:
1330                ARM7.pendingSwi = 1;
1331                ARM7_CHECKIRQ;
1332                //couldn't find any cycle counts for SWI
1200            case COND_VS:
1201                if (V_IS_CLEAR(GET_CPSR))
1202                    goto L_Next;
13331203                break;
1334            /* Undefined */
1335            default:
1336                ARM7.pendingSwi = 1;
1337                ARM7_CHECKIRQ;
1338                ARM7_ICOUNT -= 1;               //undefined takes 4 cycles (page 77)
1339                LOG(("%08x:  Undefined instruction\n",pc-4));
1340                L_Next:
1204            case COND_VC:
1205                if (V_IS_SET(GET_CPSR))
1206                    goto L_Next;
1207                break;
1208            case COND_HI:
1209                if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR))
1210                    goto L_Next;
1211                break;
1212            case COND_LS:
1213                if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR))
1214                    goto L_Next;
1215                break;
1216            case COND_GE:
1217                if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK)) /* Use x ^ (x >> ...) method */
1218                    goto L_Next;
1219                break;
1220            case COND_LT:
1221                if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
1222                    goto L_Next;
1223                break;
1224            case COND_GT:
1225                if (Z_IS_SET(GET_CPSR) || (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK)))
1226                    goto L_Next;
1227                break;
1228            case COND_LE:
1229                if (Z_IS_CLEAR(GET_CPSR) && (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK)))
1230                  goto L_Next;
1231                break;
1232            case COND_NV:
1233                goto L_Next;
1234            }
1235            /*******************************************************************/
1236            /* If we got here - condition satisfied, so decode the instruction */
1237            /*******************************************************************/
1238            switch ((insn & 0xF000000) >> 24)
1239            {
1240                case 0:
1241                case 1:
1242                case 2:
1243                case 3:
1244                    /* Branch and Exchange (BX) */
1245                    if ((insn & 0x0ffffff0) == 0x012fff10)     // bits 27-4 == 000100101111111111110001
1246                    {
1247                        R15 = GET_REGISTER(insn & 0x0f);
1248                        // If new PC address has A0 set, switch to Thumb mode
1249                        if (R15 & 1) {
1250                            SET_CPSR(GET_CPSR|T_MASK);
1251                            R15--;
1252                        }
1253                    }
1254                    else
1255                    /* Multiply OR Swap OR Half Word Data Transfer */
1256                    if ((insn & 0x0e000000) == 0 && (insn & 0x80) && (insn & 0x10))  // bits 27-25=000 bit 7=1 bit 4=1
1257                    {
1258                        /* Half Word Data Transfer */
1259                        if (insn & 0x60)         // bits = 6-5 != 00
1260                        {
1261                            HandleHalfWordDT(insn);
1262                        }
1263                        else
1264                        /* Swap */
1265                        if (insn & 0x01000000)   // bit 24 = 1
1266                        {
1267                            HandleSwap(insn);
1268                        }
1269                        /* Multiply Or Multiply Long */
1270                        else
1271                        {
1272                            /* multiply long */
1273                            if (insn & 0x800000) // Bit 23 = 1 for Multiply Long
1274                            {
1275                                /* Signed? */
1276                                if (insn & 0x00400000)
1277                                    HandleSMulLong(insn);
1278                                else
1279                                    HandleUMulLong(insn);
1280                            }
1281                            /* multiply */
1282                            else
1283                            {
1284                                HandleMul(insn);
1285                            }
1286                            R15 += 4;
1287                        }
1288                    }
1289                    else
1290                    /* Data Processing OR PSR Transfer */
1291                    if ((insn & 0x0c000000) == 0)   // bits 27-26 == 00 - This check can only exist properly after Multiplication check above
1292                    {
1293                        /* PSR Transfer (MRS & MSR) */
1294                        if (((insn & 0x00100000) == 0) && ((insn & 0x01800000) == 0x01000000)) // S bit must be clear, and bit 24,23 = 10
1295                        {
1296                            HandlePSRTransfer(insn);
1297                            ARM7_ICOUNT += 2;       // PSR only takes 1 - S Cycle, so we add + 2, since at end, we -3..
1298                            R15 += 4;
1299                        }
1300                        /* Data Processing */
1301                        else
1302                        {
1303                            HandleALU(insn);
1304                        }
1305                    }
1306                    break;
1307                /* Data Transfer - Single Data Access */
1308                case 4:
1309                case 5:
1310                case 6:
1311                case 7:
1312                    HandleMemSingle(insn);
13411313                    R15 += 4;
1342                    ARM7_ICOUNT +=2;    //Any unexecuted instruction only takes 1 cycle (page 193)
1314                    break;
1315                /* Block Data Transfer/Access */
1316                case 8:
1317                case 9:
1318                    HandleMemBlock(insn);
1319                    R15 += 4;
1320                    break;
1321                /* Branch or Branch & Link */
1322                case 0xa:
1323                case 0xb:
1324                    HandleBranch(insn);
1325                    break;
1326                /* Co-Processor Data Transfer */
1327                case 0xc:
1328                case 0xd:
1329                    HandleCoProcDT(insn);
1330                    R15 += 4;
1331                    break;
1332                /* Co-Processor Data Operation or Register Transfer */
1333                case 0xe:
1334                    if (insn & 0x10)
1335                        HandleCoProcRT(insn);
1336                    else
1337                        HandleCoProcDO(insn);
1338                    R15 += 4;
1339                    break;
1340                /* Software Interrupt */
1341                case 0x0f:
1342                    ARM7.pendingSwi = 1;
1343                    ARM7_CHECKIRQ;
1344                    //couldn't find any cycle counts for SWI
1345                    break;
1346                /* Undefined */
1347                default:
1348                    ARM7.pendingSwi = 1;
1349                    ARM7_CHECKIRQ;
1350                    ARM7_ICOUNT -= 1;               //undefined takes 4 cycles (page 77)
1351                    LOG(("%08x:  Undefined instruction\n",pc-4));
1352                    L_Next:
1353                        R15 += 4;
1354                        ARM7_ICOUNT +=2;    //Any unexecuted instruction only takes 1 cycle (page 193)
1355            }
13431356        }
1344        }
13451357
1346      change_pc(R15);
1358        change_pc(R15);
13471359
13481360        ARM7_CHECKIRQ;
13491361
13501362        /* All instructions remove 3 cycles.. Others taking less / more will have adjusted this # prior to here */
13511363        ARM7_ICOUNT -= 3;
1352    } while( ARM7_ICOUNT > 0 );
1364    } while (ARM7_ICOUNT > 0);
13531365
13541366    return cycles - ARM7_ICOUNT;
13551367}
1356

Previous 15994 Revisions Next


© 1998-2018 The MESS Team