2 * $Id: sh4core.c,v 1.17 2005-12-29 12:52:29 nkeynes Exp $
4 * SH4 emulation core, and parent module for all the SH4 peripheral
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #define MODULE sh4_module
29 /* CPU-generated exception code/vector pairs */
30 #define EXC_POWER_RESET 0x000 /* vector special */
31 #define EXC_MANUAL_RESET 0x020
32 #define EXC_SLOT_ILLEGAL 0x1A0
33 #define EXC_ILLEGAL 0x180
34 #define EXV_ILLEGAL 0x100
35 #define EXC_TRAP 0x160
36 #define EXV_TRAP 0x100
37 #define EXC_FPDISABLE 0x800
38 #define EXV_FPDISABLE 0x100
40 /********************** SH4 Module Definition ****************************/
42 void sh4_init( void );
43 void sh4_reset( void );
44 uint32_t sh4_run_slice( uint32_t );
45 void sh4_start( void );
46 void sh4_stop( void );
47 void sh4_save_state( FILE *f );
48 int sh4_load_state( FILE *f );
50 struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset,
51 NULL, sh4_run_slice, sh4_stop,
52 sh4_save_state, sh4_load_state };
54 struct sh4_registers sh4r;
58 register_io_regions( mmio_list_sh4mmio );
65 /* zero everything out, for the sake of having a consistent state. */
66 memset( &sh4r, 0, sizeof(sh4r) );
68 /* Resume running if we were halted */
69 sh4r.sh4_state = SH4_STATE_RUNNING;
72 sh4r.new_pc= 0xA0000002;
73 sh4r.vbr = 0x00000000;
74 sh4r.fpscr = 0x00040001;
77 /* Mem reset will do this, but if we want to reset _just_ the SH4... */
78 MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
80 /* Peripheral modules */
85 static struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
86 static int sh4_breakpoint_count = 0;
88 void sh4_set_breakpoint( uint32_t pc, int type )
90 sh4_breakpoints[sh4_breakpoint_count].address = pc;
91 sh4_breakpoints[sh4_breakpoint_count].type = type;
92 sh4_breakpoint_count++;
95 gboolean sh4_clear_breakpoint( uint32_t pc, int type )
99 for( i=0; i<sh4_breakpoint_count; i++ ) {
100 if( sh4_breakpoints[i].address == pc &&
101 sh4_breakpoints[i].type == type ) {
102 while( ++i < sh4_breakpoint_count ) {
103 sh4_breakpoints[i-1].address = sh4_breakpoints[i].address;
104 sh4_breakpoints[i-1].type = sh4_breakpoints[i].type;
106 sh4_breakpoint_count--;
113 int sh4_get_breakpoint( uint32_t pc )
116 for( i=0; i<sh4_breakpoint_count; i++ ) {
117 if( sh4_breakpoints[i].address == pc )
118 return sh4_breakpoints[i].type;
123 uint32_t sh4_run_slice( uint32_t nanosecs )
125 int target = sh4r.icount + nanosecs / sh4_cpu_period;
126 int start = sh4r.icount;
129 if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
130 if( sh4r.int_pending != 0 )
131 sh4r.sh4_state = SH4_STATE_RUNNING;;
134 for( sh4r.slice_cycle = 0; sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
135 if( !sh4_execute_instruction() )
137 #ifdef ENABLE_DEBUG_MODE
138 for( i=0; i<sh4_breakpoint_count; i++ ) {
139 if( sh4_breakpoints[i].address == sh4r.pc ) {
143 if( i != sh4_breakpoint_count ) {
145 if( sh4_breakpoints[i].type == BREAK_ONESHOT )
146 sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
152 /* If we aborted early, but the cpu is still technically running,
153 * we're doing a hard abort - cut the timeslice back to what we
156 if( sh4r.slice_cycle != nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
157 nanosecs = sh4r.slice_cycle;
159 if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
160 TMU_run_slice( nanosecs );
161 SCIF_run_slice( nanosecs );
163 sh4r.icount += sh4r.slice_cycle / sh4_cpu_period;
172 void sh4_save_state( FILE *f )
174 fwrite( &sh4r, sizeof(sh4r), 1, f );
176 SCIF_save_state( f );
179 int sh4_load_state( FILE * f )
181 fread( &sh4r, sizeof(sh4r), 1, f );
183 return SCIF_load_state( f );
186 /********************** SH4 emulation core ****************************/
188 void sh4_set_pc( int pc )
194 #define UNDEF(ir) do{ ERROR( "Raising exception on undefined instruction at %08x, opcode = %04x", sh4r.pc, ir ); RAISE( EXC_ILLEGAL, EXV_ILLEGAL ); }while(0)
195 #define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop(); return FALSE; }while(0)
197 #define RAISE( x, v ) do{ \
198 if( sh4r.vbr == 0 ) { \
199 ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
202 sh4r.spc = sh4r.pc + 2; \
203 sh4r.ssr = sh4_read_sr(); \
204 sh4r.sgr = sh4r.r[15]; \
205 MMIO_WRITE(MMU,EXPEVT,x); \
206 sh4r.pc = sh4r.vbr + v; \
207 sh4r.new_pc = sh4r.pc + 2; \
208 sh4_load_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
210 return TRUE; } while(0)
212 #define MEM_READ_BYTE( addr ) sh4_read_byte(addr)
213 #define MEM_READ_WORD( addr ) sh4_read_word(addr)
214 #define MEM_READ_LONG( addr ) sh4_read_long(addr)
215 #define MEM_WRITE_BYTE( addr, val ) sh4_write_byte(addr, val)
216 #define MEM_WRITE_WORD( addr, val ) sh4_write_word(addr, val)
217 #define MEM_WRITE_LONG( addr, val ) sh4_write_long(addr, val)
219 #define MEM_FP_READ( addr, reg ) if( IS_FPU_DOUBLESIZE() ) { \
220 ((uint32_t *)FR)[(reg)&0xE0] = sh4_read_long(addr); \
221 ((uint32_t *)FR)[(reg)|1] = sh4_read_long(addr+4); \
222 } else ((uint32_t *)FR)[reg] = sh4_read_long(addr)
224 #define MEM_FP_WRITE( addr, reg ) if( IS_FPU_DOUBLESIZE() ) { \
225 sh4_write_long( addr, ((uint32_t *)FR)[(reg)&0xE0] ); \
226 sh4_write_long( addr+4, ((uint32_t *)FR)[(reg)|1] ); \
227 } else sh4_write_long( addr, ((uint32_t *)FR)[reg] )
229 #define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
231 #define CHECK( x, c, v ) if( !x ) RAISE( c, v )
232 #define CHECKPRIV() CHECK( IS_SH4_PRIVMODE(), EXC_ILLEGAL, EXV_ILLEGAL )
233 #define CHECKFPUEN() CHECK( IS_FPU_ENABLED(), EXC_FPDISABLE, EXV_FPDISABLE )
234 #define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); sh4_stop(); return; }
235 #define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) { RAISE(EXC_SLOT_ILLEGAL,EXV_ILLEGAL); }
237 static void sh4_switch_banks( )
241 memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
242 memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
243 memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
246 static void sh4_load_sr( uint32_t newval )
248 if( (newval ^ sh4r.sr) & SR_RB )
251 sh4r.t = (newval&SR_T) ? 1 : 0;
252 sh4r.s = (newval&SR_S) ? 1 : 0;
253 sh4r.m = (newval&SR_M) ? 1 : 0;
254 sh4r.q = (newval&SR_Q) ? 1 : 0;
258 static uint32_t sh4_read_sr( void )
260 /* synchronize sh4r.sr with the various bitflags */
261 sh4r.sr &= SR_MQSTMASK;
262 if( sh4r.t ) sh4r.sr |= SR_T;
263 if( sh4r.s ) sh4r.sr |= SR_S;
264 if( sh4r.m ) sh4r.sr |= SR_M;
265 if( sh4r.q ) sh4r.sr |= SR_Q;
268 /* function for external use */
269 void sh4_raise_exception( int code, int vector )
274 static void sh4_accept_interrupt( void )
276 uint32_t code = intc_accept_interrupt();
277 sh4r.ssr = sh4_read_sr();
279 sh4r.sgr = sh4r.r[15];
280 sh4_load_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
281 MMIO_WRITE( MMU, INTEVT, code );
282 sh4r.pc = sh4r.vbr + 0x600;
283 sh4r.new_pc = sh4r.pc + 2;
284 WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
287 gboolean sh4_execute_instruction( void )
296 #define RN(ir) sh4r.r[(ir&0x0F00)>>8]
297 #define RN_BANK(ir) sh4r.r_bank[(ir&0x0070)>>4]
298 #define RM(ir) sh4r.r[(ir&0x00F0)>>4]
299 #define DISP4(ir) (ir&0x000F) /* 4-bit displacements are *NOT* sign-extended */
300 #define DISP8(ir) (ir&0x00FF)
301 #define PCDISP8(ir) SIGNEXT8(ir&0x00FF)
302 #define IMM8(ir) SIGNEXT8(ir&0x00FF)
303 #define UIMM8(ir) (ir&0x00FF) /* Unsigned immmediate */
304 #define DISP12(ir) SIGNEXT12(ir&0x0FFF)
305 #define FVN(ir) ((ir&0x0C00)>>8)
306 #define FVM(ir) ((ir&0x0300)>>6)
307 #define FRN(ir) (FR[(ir&0x0F00)>>8])
308 #define FRM(ir) (FR[(ir&0x00F0)>>4])
309 #define FRNi(ir) (((uint32_t *)FR)[(ir&0x0F00)>>8])
310 #define FRMi(ir) (((uint32_t *)FR)[(ir&0x00F0)>>4])
311 #define DRN(ir) (((double *)FR)[(ir&0x0E00)>>9])
312 #define DRM(ir) (((double *)FR)[(ir&0x00E0)>>5])
313 #define DRNi(ir) (((uint64_t *)FR)[(ir&0x0E00)>>9])
314 #define DRMi(ir) (((uint64_t *)FR)[(ir&0x00E0)>>5])
315 #define FRNn(ir) ((ir&0x0F00)>>8)
316 #define FRMn(ir) ((ir&0x00F0)>>4)
317 #define FPULf *((float *)&sh4r.fpul)
318 #define FPULi (sh4r.fpul)
320 if( SH4_INT_PENDING() )
321 sh4_accept_interrupt();
324 ir = MEM_READ_WORD(pc);
327 switch( (ir&0xF000)>>12 ) {
328 case 0: /* 0000nnnnmmmmxxxx */
329 switch( ir&0x000F ) {
331 switch( (ir&0x00F0)>>4 ) {
332 case 0: /* STC SR, Rn */
334 RN(ir) = sh4_read_sr();
336 case 1: /* STC GBR, Rn */
339 case 2: /* STC VBR, Rn */
343 case 3: /* STC SSR, Rn */
347 case 4: /* STC SPC, Rn */
351 case 8: case 9: case 10: case 11: case 12: case 13:
352 case 14: case 15:/* STC Rm_bank, Rn */
354 RN(ir) = RN_BANK(ir);
360 switch( (ir&0x00F0)>>4 ) {
361 case 0: /* BSRF Rn */
362 CHECKDEST( pc + 4 + RN(ir) );
364 sh4r.in_delay_slot = 1;
365 sh4r.pr = sh4r.pc + 4;
366 sh4r.pc = sh4r.new_pc;
367 sh4r.new_pc = pc + 4 + RN(ir);
369 case 2: /* BRAF Rn */
370 CHECKDEST( pc + 4 + RN(ir) );
372 sh4r.in_delay_slot = 1;
373 sh4r.pc = sh4r.new_pc;
374 sh4r.new_pc = pc + 4 + RN(ir);
376 case 8: /* PREF [Rn] */
378 if( (tmp & 0xFC000000) == 0xE0000000 ) {
379 /* Store queue operation */
380 int queue = (tmp&0x20)>>2;
381 int32_t *src = &sh4r.store_queue[queue];
382 uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
383 uint32_t target = tmp&0x03FFFFE0 | hi;
384 mem_copy_to_sh4( target, src, 32 );
385 // WARN( "Executed SQ%c => %08X",
386 // (queue == 0 ? '0' : '1'), target );
389 case 9: /* OCBI [Rn] */
390 case 10:/* OCBP [Rn] */
391 case 11:/* OCBWB [Rn] */
394 case 12:/* MOVCA.L R0, [Rn] */
399 case 4: /* MOV.B Rm, [R0 + Rn] */
400 MEM_WRITE_BYTE( R0 + RN(ir), RM(ir) );
402 case 5: /* MOV.W Rm, [R0 + Rn] */
403 MEM_WRITE_WORD( R0 + RN(ir), RM(ir) );
405 case 6: /* MOV.L Rm, [R0 + Rn] */
406 MEM_WRITE_LONG( R0 + RN(ir), RM(ir) );
408 case 7: /* MUL.L Rm, Rn */
409 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
413 switch( (ir&0x0FF0)>>4 ) {
435 if( (ir&0x00F0) == 0x20 ) /* MOVT Rn */
437 else if( ir == 0x0019 ) /* DIV0U */
438 sh4r.m = sh4r.q = sh4r.t = 0;
439 else if( ir == 0x0009 )
444 switch( (ir&0x00F0) >> 4 ) {
445 case 0: /* STS MACH, Rn */
446 RN(ir) = sh4r.mac >> 32;
448 case 1: /* STS MACL, Rn */
449 RN(ir) = (uint32_t)sh4r.mac;
451 case 2: /* STS PR, Rn */
454 case 3: /* STC SGR, Rn */
458 case 5:/* STS FPUL, Rn */
461 case 6: /* STS FPSCR, Rn */
464 case 15:/* STC DBR, Rn */
472 switch( (ir&0x0FF0)>>4 ) {
474 CHECKDEST( sh4r.pr );
476 sh4r.in_delay_slot = 1;
477 sh4r.pc = sh4r.new_pc;
478 sh4r.new_pc = sh4r.pr;
481 if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
482 sh4r.sh4_state = SH4_STATE_STANDBY;
484 sh4r.sh4_state = SH4_STATE_SLEEP;
486 return FALSE; /* Halt CPU */
489 CHECKDEST( sh4r.spc );
491 sh4r.in_delay_slot = 1;
492 sh4r.pc = sh4r.new_pc;
493 sh4r.new_pc = sh4r.spc;
494 sh4_load_sr( sh4r.ssr );
499 case 12:/* MOV.B [R0+R%d], R%d */
500 RN(ir) = MEM_READ_BYTE( R0 + RM(ir) );
502 case 13:/* MOV.W [R0+R%d], R%d */
503 RN(ir) = MEM_READ_WORD( R0 + RM(ir) );
505 case 14:/* MOV.L [R0+R%d], R%d */
506 RN(ir) = MEM_READ_LONG( R0 + RM(ir) );
508 case 15:/* MAC.L [Rm++], [Rn++] */
509 tmpl = ( SIGNEXT32(MEM_READ_LONG(RM(ir))) *
510 SIGNEXT32(MEM_READ_LONG(RN(ir))) );
512 /* 48-bit Saturation. Yuch */
513 tmpl += SIGNEXT48(sh4r.mac);
514 if( tmpl < 0xFFFF800000000000LL )
515 tmpl = 0xFFFF800000000000LL;
516 else if( tmpl > 0x00007FFFFFFFFFFFLL )
517 tmpl = 0x00007FFFFFFFFFFFLL;
518 sh4r.mac = (sh4r.mac&0xFFFF000000000000LL) |
519 (tmpl&0x0000FFFFFFFFFFFFLL);
520 } else sh4r.mac = tmpl;
529 case 1: /* 0001nnnnmmmmdddd */
530 /* MOV.L Rm, [Rn + disp4*4] */
531 MEM_WRITE_LONG( RN(ir) + (DISP4(ir)<<2), RM(ir) );
533 case 2: /* 0010nnnnmmmmxxxx */
534 switch( ir&0x000F ) {
535 case 0: /* MOV.B Rm, [Rn] */
536 MEM_WRITE_BYTE( RN(ir), RM(ir) );
538 case 1: /* MOV.W Rm, [Rn] */
539 MEM_WRITE_WORD( RN(ir), RM(ir) );
541 case 2: /* MOV.L Rm, [Rn] */
542 MEM_WRITE_LONG( RN(ir), RM(ir) );
546 case 4: /* MOV.B Rm, [--Rn] */
548 MEM_WRITE_BYTE( RN(ir), RM(ir) );
550 case 5: /* MOV.W Rm, [--Rn] */
552 MEM_WRITE_WORD( RN(ir), RM(ir) );
554 case 6: /* MOV.L Rm, [--Rn] */
556 MEM_WRITE_LONG( RN(ir), RM(ir) );
558 case 7: /* DIV0S Rm, Rn */
561 sh4r.t = sh4r.q ^ sh4r.m;
563 case 8: /* TST Rm, Rn */
564 sh4r.t = (RN(ir)&RM(ir) ? 0 : 1);
566 case 9: /* AND Rm, Rn */
569 case 10:/* XOR Rm, Rn */
572 case 11:/* OR Rm, Rn */
575 case 12:/* CMP/STR Rm, Rn */
576 /* set T = 1 if any byte in RM & RN is the same */
577 tmp = RM(ir) ^ RN(ir);
578 sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
579 (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
581 case 13:/* XTRCT Rm, Rn */
582 RN(ir) = (RN(ir)>>16) | (RM(ir)<<16);
584 case 14:/* MULU.W Rm, Rn */
585 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
586 (uint32_t)((RM(ir)&0xFFFF) * (RN(ir)&0xFFFF));
588 case 15:/* MULS.W Rm, Rn */
589 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
590 (uint32_t)(SIGNEXT32(RM(ir)&0xFFFF) * SIGNEXT32(RN(ir)&0xFFFF));
594 case 3: /* 0011nnnnmmmmxxxx */
595 switch( ir&0x000F ) {
596 case 0: /* CMP/EQ Rm, Rn */
597 sh4r.t = ( RM(ir) == RN(ir) ? 1 : 0 );
599 case 2: /* CMP/HS Rm, Rn */
600 sh4r.t = ( RN(ir) >= RM(ir) ? 1 : 0 );
602 case 3: /* CMP/GE Rm, Rn */
603 sh4r.t = ( ((int32_t)RN(ir)) >= ((int32_t)RM(ir)) ? 1 : 0 );
605 case 4: { /* DIV1 Rm, Rn */
606 /* This is just from the sh4p manual with some
607 * simplifications (someone want to check it's correct? :)
608 * Why they couldn't just provide a real DIV instruction...
609 * Please oh please let the translator batch these things
610 * up into a single DIV... */
611 uint32_t tmp0, tmp1, tmp2, dir;
613 dir = sh4r.q ^ sh4r.m;
614 sh4r.q = (RN(ir) >> 31);
616 RN(ir) = (RN(ir) << 1) | sh4r.t;
620 tmp1 = (RN(ir)<tmp0 ? 1 : 0 );
623 tmp1 = (RN(ir)>tmp0 ? 1 : 0 );
625 sh4r.q ^= sh4r.m ^ tmp1;
626 sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
628 case 5: /* DMULU.L Rm, Rn */
629 sh4r.mac = ((uint64_t)RM(ir)) * ((uint64_t)RN(ir));
631 case 6: /* CMP/HI Rm, Rn */
632 sh4r.t = ( RN(ir) > RM(ir) ? 1 : 0 );
634 case 7: /* CMP/GT Rm, Rn */
635 sh4r.t = ( ((int32_t)RN(ir)) > ((int32_t)RM(ir)) ? 1 : 0 );
637 case 8: /* SUB Rm, Rn */
640 case 10:/* SUBC Rm, Rn */
642 RN(ir) = RN(ir) - RM(ir) - sh4r.t;
643 sh4r.t = (RN(ir) > tmp || (RN(ir) == tmp && sh4r.t == 1));
645 case 11:/* SUBV Rm, Rn */
648 case 12:/* ADD Rm, Rn */
651 case 13:/* DMULS.L Rm, Rn */
652 sh4r.mac = SIGNEXT32(RM(ir)) * SIGNEXT32(RN(ir));
654 case 14:/* ADDC Rm, Rn */
656 RN(ir) += RM(ir) + sh4r.t;
657 sh4r.t = ( RN(ir) < tmp || (RN(ir) == tmp && sh4r.t != 0) ? 1 : 0 );
659 case 15:/* ADDV Rm, Rn */
665 case 4: /* 0100nnnnxxxxxxxx */
666 switch( ir&0x00FF ) {
667 case 0x00: /* SHLL Rn */
668 sh4r.t = RN(ir) >> 31;
671 case 0x01: /* SHLR Rn */
672 sh4r.t = RN(ir) & 0x00000001;
675 case 0x02: /* STS.L MACH, [--Rn] */
677 MEM_WRITE_LONG( RN(ir), (sh4r.mac>>32) );
679 case 0x03: /* STC.L SR, [--Rn] */
682 MEM_WRITE_LONG( RN(ir), sh4_read_sr() );
684 case 0x04: /* ROTL Rn */
685 sh4r.t = RN(ir) >> 31;
689 case 0x05: /* ROTR Rn */
690 sh4r.t = RN(ir) & 0x00000001;
692 RN(ir) |= (sh4r.t << 31);
694 case 0x06: /* LDS.L [Rn++], MACH */
695 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
696 (((uint64_t)MEM_READ_LONG(RN(ir)))<<32);
699 case 0x07: /* LDC.L [Rn++], SR */
701 sh4_load_sr( MEM_READ_LONG(RN(ir)) );
704 case 0x08: /* SHLL2 Rn */
707 case 0x09: /* SHLR2 Rn */
710 case 0x0A: /* LDS Rn, MACH */
711 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
712 (((uint64_t)RN(ir))<<32);
714 case 0x0B: /* JSR [Rn] */
717 sh4r.in_delay_slot = 1;
718 sh4r.pc = sh4r.new_pc;
719 sh4r.new_pc = RN(ir);
722 case 0x0E: /* LDC Rn, SR */
724 sh4_load_sr( RN(ir) );
726 case 0x10: /* DT Rn */
728 sh4r.t = ( RN(ir) == 0 ? 1 : 0 );
730 case 0x11: /* CMP/PZ Rn */
731 sh4r.t = ( ((int32_t)RN(ir)) >= 0 ? 1 : 0 );
733 case 0x12: /* STS.L MACL, [--Rn] */
735 MEM_WRITE_LONG( RN(ir), (uint32_t)sh4r.mac );
737 case 0x13: /* STC.L GBR, [--Rn] */
739 MEM_WRITE_LONG( RN(ir), sh4r.gbr );
741 case 0x15: /* CMP/PL Rn */
742 sh4r.t = ( ((int32_t)RN(ir)) > 0 ? 1 : 0 );
744 case 0x16: /* LDS.L [Rn++], MACL */
745 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
746 (uint64_t)((uint32_t)MEM_READ_LONG(RN(ir)));
749 case 0x17: /* LDC.L [Rn++], GBR */
750 sh4r.gbr = MEM_READ_LONG(RN(ir));
753 case 0x18: /* SHLL8 Rn */
756 case 0x19: /* SHLR8 Rn */
759 case 0x1A: /* LDS Rn, MACL */
760 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
761 (uint64_t)((uint32_t)(RN(ir)));
763 case 0x1B: /* TAS.B [Rn] */
764 tmp = MEM_READ_BYTE( RN(ir) );
765 sh4r.t = ( tmp == 0 ? 1 : 0 );
766 MEM_WRITE_BYTE( RN(ir), tmp | 0x80 );
768 case 0x1E: /* LDC Rn, GBR */
771 case 0x20: /* SHAL Rn */
772 sh4r.t = RN(ir) >> 31;
775 case 0x21: /* SHAR Rn */
776 sh4r.t = RN(ir) & 0x00000001;
777 RN(ir) = ((int32_t)RN(ir)) >> 1;
779 case 0x22: /* STS.L PR, [--Rn] */
781 MEM_WRITE_LONG( RN(ir), sh4r.pr );
783 case 0x23: /* STC.L VBR, [--Rn] */
786 MEM_WRITE_LONG( RN(ir), sh4r.vbr );
788 case 0x24: /* ROTCL Rn */
794 case 0x25: /* ROTCR Rn */
795 tmp = RN(ir) & 0x00000001;
797 RN(ir) |= (sh4r.t << 31 );
800 case 0x26: /* LDS.L [Rn++], PR */
801 sh4r.pr = MEM_READ_LONG( RN(ir) );
804 case 0x27: /* LDC.L [Rn++], VBR */
806 sh4r.vbr = MEM_READ_LONG(RN(ir));
809 case 0x28: /* SHLL16 Rn */
812 case 0x29: /* SHLR16 Rn */
815 case 0x2A: /* LDS Rn, PR */
818 case 0x2B: /* JMP [Rn] */
821 sh4r.in_delay_slot = 1;
822 sh4r.pc = sh4r.new_pc;
823 sh4r.new_pc = RN(ir);
825 case 0x2E: /* LDC Rn, VBR */
829 case 0x32: /* STC.L SGR, [--Rn] */
832 MEM_WRITE_LONG( RN(ir), sh4r.sgr );
834 case 0x33: /* STC.L SSR, [--Rn] */
837 MEM_WRITE_LONG( RN(ir), sh4r.ssr );
839 case 0x37: /* LDC.L [Rn++], SSR */
841 sh4r.ssr = MEM_READ_LONG(RN(ir));
844 case 0x3E: /* LDC Rn, SSR */
848 case 0x43: /* STC.L SPC, [--Rn] */
851 MEM_WRITE_LONG( RN(ir), sh4r.spc );
853 case 0x47: /* LDC.L [Rn++], SPC */
855 sh4r.spc = MEM_READ_LONG(RN(ir));
858 case 0x4E: /* LDC Rn, SPC */
862 case 0x52: /* STS.L FPUL, [--Rn] */
864 MEM_WRITE_LONG( RN(ir), sh4r.fpul );
866 case 0x56: /* LDS.L [Rn++], FPUL */
867 sh4r.fpul = MEM_READ_LONG(RN(ir));
870 case 0x5A: /* LDS Rn, FPUL */
873 case 0x62: /* STS.L FPSCR, [--Rn] */
875 MEM_WRITE_LONG( RN(ir), sh4r.fpscr );
877 case 0x66: /* LDS.L [Rn++], FPSCR */
878 sh4r.fpscr = MEM_READ_LONG(RN(ir));
881 case 0x6A: /* LDS Rn, FPSCR */
884 case 0xF2: /* STC.L DBR, [--Rn] */
887 MEM_WRITE_LONG( RN(ir), sh4r.dbr );
889 case 0xF6: /* LDC.L [Rn++], DBR */
891 sh4r.dbr = MEM_READ_LONG(RN(ir));
894 case 0xFA: /* LDC Rn, DBR */
898 case 0x83: case 0x93: case 0xA3: case 0xB3: case 0xC3:
899 case 0xD3: case 0xE3: case 0xF3: /* STC.L Rn_BANK, [--Rn] */
902 MEM_WRITE_LONG( RN(ir), RN_BANK(ir) );
904 case 0x87: case 0x97: case 0xA7: case 0xB7: case 0xC7:
905 case 0xD7: case 0xE7: case 0xF7: /* LDC.L [Rn++], Rn_BANK */
907 RN_BANK(ir) = MEM_READ_LONG( RN(ir) );
910 case 0x8E: case 0x9E: case 0xAE: case 0xBE: case 0xCE:
911 case 0xDE: case 0xEE: case 0xFE: /* LDC Rm, Rn_BANK */
913 RN_BANK(ir) = RM(ir);
916 if( (ir&0x000F) == 0x0F ) {
917 /* MAC.W [Rm++], [Rn++] */
918 tmp = SIGNEXT16(MEM_READ_WORD(RM(ir))) *
919 SIGNEXT16(MEM_READ_WORD(RN(ir)));
923 } else sh4r.mac += SIGNEXT32(tmp);
926 } else if( (ir&0x000F) == 0x0C ) {
929 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
930 else if( (tmp & 0x1F) == 0 )
931 RN(ir) = ((int32_t)RN(ir)) >> 31;
933 RN(ir) = ((int32_t)RN(ir)) >> (((~RM(ir)) & 0x1F)+1);
934 } else if( (ir&0x000F) == 0x0D ) {
937 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
938 else if( (tmp & 0x1F) == 0 ) RN(ir) = 0;
939 else RN(ir) >>= (((~tmp) & 0x1F)+1);
943 case 5: /* 0101nnnnmmmmdddd */
944 /* MOV.L [Rm + disp4*4], Rn */
945 RN(ir) = MEM_READ_LONG( RM(ir) + (DISP4(ir)<<2) );
947 case 6: /* 0110xxxxxxxxxxxx */
948 switch( ir&0x000f ) {
949 case 0: /* MOV.B [Rm], Rn */
950 RN(ir) = MEM_READ_BYTE( RM(ir) );
952 case 1: /* MOV.W [Rm], Rn */
953 RN(ir) = MEM_READ_WORD( RM(ir) );
955 case 2: /* MOV.L [Rm], Rn */
956 RN(ir) = MEM_READ_LONG( RM(ir) );
958 case 3: /* MOV Rm, Rn */
961 case 4: /* MOV.B [Rm++], Rn */
962 RN(ir) = MEM_READ_BYTE( RM(ir) );
965 case 5: /* MOV.W [Rm++], Rn */
966 RN(ir) = MEM_READ_WORD( RM(ir) );
969 case 6: /* MOV.L [Rm++], Rn */
970 RN(ir) = MEM_READ_LONG( RM(ir) );
973 case 7: /* NOT Rm, Rn */
976 case 8: /* SWAP.B Rm, Rn */
977 RN(ir) = (RM(ir)&0xFFFF0000) | ((RM(ir)&0x0000FF00)>>8) |
978 ((RM(ir)&0x000000FF)<<8);
980 case 9: /* SWAP.W Rm, Rn */
981 RN(ir) = (RM(ir)>>16) | (RM(ir)<<16);
983 case 10:/* NEGC Rm, Rn */
985 RN(ir) = tmp - sh4r.t;
986 sh4r.t = ( 0<tmp || tmp<RN(ir) ? 1 : 0 );
988 case 11:/* NEG Rm, Rn */
991 case 12:/* EXTU.B Rm, Rn */
992 RN(ir) = RM(ir)&0x000000FF;
994 case 13:/* EXTU.W Rm, Rn */
995 RN(ir) = RM(ir)&0x0000FFFF;
997 case 14:/* EXTS.B Rm, Rn */
998 RN(ir) = SIGNEXT8( RM(ir)&0x000000FF );
1000 case 15:/* EXTS.W Rm, Rn */
1001 RN(ir) = SIGNEXT16( RM(ir)&0x0000FFFF );
1005 case 7: /* 0111nnnniiiiiiii */
1009 case 8: /* 1000xxxxxxxxxxxx */
1010 switch( (ir&0x0F00) >> 8 ) {
1011 case 0: /* MOV.B R0, [Rm + disp4] */
1012 MEM_WRITE_BYTE( RM(ir) + DISP4(ir), R0 );
1014 case 1: /* MOV.W R0, [Rm + disp4*2] */
1015 MEM_WRITE_WORD( RM(ir) + (DISP4(ir)<<1), R0 );
1017 case 4: /* MOV.B [Rm + disp4], R0 */
1018 R0 = MEM_READ_BYTE( RM(ir) + DISP4(ir) );
1020 case 5: /* MOV.W [Rm + disp4*2], R0 */
1021 R0 = MEM_READ_WORD( RM(ir) + (DISP4(ir)<<1) );
1023 case 8: /* CMP/EQ imm, R0 */
1024 sh4r.t = ( R0 == IMM8(ir) ? 1 : 0 );
1026 case 9: /* BT disp8 */
1029 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1030 sh4r.pc += (PCDISP8(ir)<<1) + 4;
1031 sh4r.new_pc = sh4r.pc + 2;
1035 case 11:/* BF disp8 */
1038 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1039 sh4r.pc += (PCDISP8(ir)<<1) + 4;
1040 sh4r.new_pc = sh4r.pc + 2;
1044 case 13:/* BT/S disp8 */
1047 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1048 sh4r.in_delay_slot = 1;
1049 sh4r.pc = sh4r.new_pc;
1050 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1051 sh4r.in_delay_slot = 1;
1055 case 15:/* BF/S disp8 */
1058 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1059 sh4r.in_delay_slot = 1;
1060 sh4r.pc = sh4r.new_pc;
1061 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1068 case 9: /* 1001xxxxxxxxxxxx */
1069 /* MOV.W [disp8*2 + pc + 4], Rn */
1070 RN(ir) = MEM_READ_WORD( pc + 4 + (DISP8(ir)<<1) );
1072 case 10:/* 1010dddddddddddd */
1074 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1076 sh4r.in_delay_slot = 1;
1077 sh4r.pc = sh4r.new_pc;
1078 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1080 case 11:/* 1011dddddddddddd */
1082 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1084 sh4r.in_delay_slot = 1;
1086 sh4r.pc = sh4r.new_pc;
1087 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1089 case 12:/* 1100xxxxdddddddd */
1090 switch( (ir&0x0F00)>>8 ) {
1091 case 0: /* MOV.B R0, [GBR + disp8] */
1092 MEM_WRITE_BYTE( sh4r.gbr + DISP8(ir), R0 );
1094 case 1: /* MOV.W R0, [GBR + disp8*2] */
1095 MEM_WRITE_WORD( sh4r.gbr + (DISP8(ir)<<1), R0 );
1097 case 2: /*MOV.L R0, [GBR + disp8*4] */
1098 MEM_WRITE_LONG( sh4r.gbr + (DISP8(ir)<<2), R0 );
1100 case 3: /* TRAPA imm8 */
1102 sh4r.in_delay_slot = 1;
1103 MMIO_WRITE( MMU, TRA, UIMM8(ir) );
1104 sh4r.pc = sh4r.new_pc; /* RAISE ends the instruction */
1106 RAISE( EXC_TRAP, EXV_TRAP );
1108 case 4: /* MOV.B [GBR + disp8], R0 */
1109 R0 = MEM_READ_BYTE( sh4r.gbr + DISP8(ir) );
1111 case 5: /* MOV.W [GBR + disp8*2], R0 */
1112 R0 = MEM_READ_WORD( sh4r.gbr + (DISP8(ir)<<1) );
1114 case 6: /* MOV.L [GBR + disp8*4], R0 */
1115 R0 = MEM_READ_LONG( sh4r.gbr + (DISP8(ir)<<2) );
1117 case 7: /* MOVA disp8 + pc&~3 + 4, R0 */
1118 R0 = (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4;
1120 case 8: /* TST imm8, R0 */
1121 sh4r.t = (R0 & UIMM8(ir) ? 0 : 1);
1123 case 9: /* AND imm8, R0 */
1126 case 10:/* XOR imm8, R0 */
1129 case 11:/* OR imm8, R0 */
1132 case 12:/* TST.B imm8, [R0+GBR] */
1133 sh4r.t = ( MEM_READ_BYTE(R0 + sh4r.gbr) & UIMM8(ir) ? 0 : 1 );
1135 case 13:/* AND.B imm8, [R0+GBR] */
1136 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1137 UIMM8(ir) & MEM_READ_BYTE(R0 + sh4r.gbr) );
1139 case 14:/* XOR.B imm8, [R0+GBR] */
1140 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1141 UIMM8(ir) ^ MEM_READ_BYTE(R0 + sh4r.gbr) );
1143 case 15:/* OR.B imm8, [R0+GBR] */
1144 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1145 UIMM8(ir) | MEM_READ_BYTE(R0 + sh4r.gbr) );
1149 case 13:/* 1101nnnndddddddd */
1150 /* MOV.L [disp8*4 + pc&~3 + 4], Rn */
1151 RN(ir) = MEM_READ_LONG( (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4 );
1153 case 14:/* 1110nnnniiiiiiii */
1157 case 15:/* 1111xxxxxxxxxxxx */
1159 switch( ir&0x000F ) {
1160 case 0: /* FADD FRm, FRn */
1163 case 1: /* FSUB FRm, FRn */
1166 case 2: /* FMUL FRm, FRn */
1167 FRN(ir) = FRN(ir) * FRM(ir);
1169 case 3: /* FDIV FRm, FRn */
1170 FRN(ir) = FRN(ir) / FRM(ir);
1172 case 4: /* FCMP/EQ FRm, FRn */
1173 sh4r.t = ( FRN(ir) == FRM(ir) ? 1 : 0 );
1175 case 5: /* FCMP/GT FRm, FRn */
1176 sh4r.t = ( FRN(ir) > FRM(ir) ? 1 : 0 );
1178 case 6: /* FMOV.S [Rm+R0], FRn */
1179 MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
1181 case 7: /* FMOV.S FRm, [Rn+R0] */
1182 MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
1184 case 8: /* FMOV.S [Rm], FRn */
1185 MEM_FP_READ( RM(ir), FRNn(ir) );
1187 case 9: /* FMOV.S [Rm++], FRn */
1188 MEM_FP_READ( RM(ir), FRNn(ir) );
1191 case 10:/* FMOV.S FRm, [Rn] */
1192 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1194 case 11:/* FMOV.S FRm, [--Rn] */
1196 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1198 case 12:/* FMOV FRm, FRn */
1199 if( IS_FPU_DOUBLESIZE() ) {
1206 switch( (ir&0x00F0) >> 4 ) {
1207 case 0: /* FSTS FPUL, FRn */
1210 case 1: /* FLDS FRn, FPUL */
1213 case 2: /* FLOAT FPUL, FRn */
1214 FRN(ir) = (float)FPULi;
1216 case 3: /* FTRC FRn, FPUL */
1217 FPULi = (uint32_t)FRN(ir);
1218 /* FIXME: is this sufficient? */
1220 case 4: /* FNEG FRn */
1223 case 5: /* FABS FRn */
1224 FRN(ir) = fabsf(FRN(ir));
1226 case 6: /* FSQRT FRn */
1227 FRN(ir) = sqrtf(FRN(ir));
1229 case 7: /* FSRRA FRn */
1230 FRN(ir) = 1.0/sqrtf(FRN(ir));
1232 case 8: /* FLDI0 FRn */
1235 case 9: /* FLDI1 FRn */
1238 case 10: /* FCNVSD FPUL, DRn */
1239 if( IS_FPU_DOUBLEPREC() )
1240 DRN(ir) = (double)FPULf;
1243 case 11: /* FCNVDS DRn, FPUL */
1244 if( IS_FPU_DOUBLEPREC() )
1245 FPULf = (float)DRN(ir);
1248 case 14:/* FIPR FVm, FVn */
1249 /* FIXME: This is not going to be entirely accurate
1250 * as the SH4 instruction is less precise. Also
1251 * need to check for 0s and infinities.
1254 float *fr_bank = FR;
1257 fr_bank[tmp2+3] = fr_bank[tmp]*fr_bank[tmp2] +
1258 fr_bank[tmp+1]*fr_bank[tmp2+1] +
1259 fr_bank[tmp+2]*fr_bank[tmp2+2] +
1260 fr_bank[tmp+3]*fr_bank[tmp2+3];
1264 if( (ir&0x0300) == 0x0100 ) { /* FTRV XMTRX,FVn */
1265 float *fvout = FR+FVN(ir);
1267 float fv[4] = { fvout[0], fvout[1], fvout[2], fvout[3] };
1268 fvout[0] = xm[0] * fv[0] + xm[4]*fv[1] +
1269 xm[8]*fv[2] + xm[12]*fv[3];
1270 fvout[1] = xm[1] * fv[0] + xm[5]*fv[1] +
1271 xm[9]*fv[2] + xm[13]*fv[3];
1272 fvout[2] = xm[2] * fv[0] + xm[6]*fv[1] +
1273 xm[10]*fv[2] + xm[14]*fv[3];
1274 fvout[3] = xm[3] * fv[0] + xm[7]*fv[1] +
1275 xm[11]*fv[2] + xm[15]*fv[3];
1278 else if( (ir&0x0100) == 0 ) { /* FSCA FPUL, DRn */
1279 float angle = (((float)(short)(FPULi>>16)) +
1280 ((float)(FPULi&16)/65536.0)) *
1283 FR[reg] = sinf(angle);
1284 FR[reg+1] = cosf(angle);
1287 else if( ir == 0xFBFD ) {
1289 sh4r.fpscr ^= FPSCR_FR;
1292 else if( ir == 0xF3FD ) {
1294 sh4r.fpscr ^= FPSCR_SZ;
1300 case 14:/* FMAC FR0, FRm, FRn */
1301 FRN(ir) += FRM(ir)*FR0;
1307 sh4r.pc = sh4r.new_pc;
1309 sh4r.in_delay_slot = 0;
.