2 * $Id: sh4core.c,v 1.12 2005-12-26 03:10:23 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.
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 uint32_t sh4_freq = SH4_BASE_RATE;
41 uint32_t sh4_bus_freq = SH4_BASE_RATE;
42 uint32_t sh4_peripheral_freq = SH4_BASE_RATE / 2;
44 uint32_t sh4_cpu_period = 1000 / SH4_BASE_RATE; /* in nanoseconds */
45 uint32_t sh4_bus_period = 1000 / SH4_BASE_RATE;
46 uint32_t sh4_peripheral_period = 2000 / SH4_BASE_RATE;
48 /********************** SH4 Module Definition ****************************/
50 void sh4_init( void );
51 void sh4_reset( void );
52 uint32_t sh4_run_slice( uint32_t );
53 void sh4_start( void );
54 void sh4_stop( void );
55 void sh4_save_state( FILE *f );
56 int sh4_load_state( FILE *f );
58 struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset,
59 NULL, sh4_run_slice, sh4_stop,
60 sh4_save_state, sh4_load_state };
62 struct sh4_registers sh4r;
66 register_io_regions( mmio_list_sh4mmio );
73 /* zero everything out, for the sake of having a consistent state. */
74 memset( &sh4r, 0, sizeof(sh4r) );
76 /* Resume running if we were halted */
77 sh4r.sh4_state = SH4_STATE_RUNNING;
80 sh4r.new_pc= 0xA0000002;
81 sh4r.vbr = 0x00000000;
82 sh4r.fpscr = 0x00040001;
85 /* Mem reset will do this, but if we want to reset _just_ the SH4... */
86 MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
88 /* Peripheral modules */
93 uint32_t sh4_run_slice( uint32_t nanosecs )
95 int target = sh4r.icount + nanosecs / sh4_cpu_period;
96 int start = sh4r.icount;
99 if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
100 if( sh4r.int_pending != 0 )
101 sh4r.sh4_state = SH4_STATE_RUNNING;;
104 while( sh4r.icount < target && sh4r.sh4_state == SH4_STATE_RUNNING ) {
106 if( !sh4_execute_instruction() )
110 /* If we aborted early, but the cpu is still technically running,
111 * we're doing a hard abort - cut the timeslice back to what we
114 if( target != sh4r.icount && sh4r.sh4_state == SH4_STATE_RUNNING ) {
115 /* Halted - compute time actually executed */
116 nanosecs = (sh4r.icount - start) * sh4_cpu_period;
118 if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
119 TMU_run_slice( nanosecs );
120 SCIF_run_slice( nanosecs );
130 void sh4_save_state( FILE *f )
132 fwrite( &sh4r, sizeof(sh4r), 1, f );
133 SCIF_save_state( f );
136 int sh4_load_state( FILE * f )
138 fread( &sh4r, sizeof(sh4r), 1, f );
139 return SCIF_load_state( f );
142 /********************** SH4 emulation core ****************************/
144 void sh4_set_pc( int pc )
150 void sh4_set_breakpoint( uint32_t pc, int type )
155 #define UNDEF(ir) do{ ERROR( "Raising exception on undefined instruction at %08x, opcode = %04x", sh4r.pc, ir ); RAISE( EXC_ILLEGAL, EXV_ILLEGAL ); }while(0)
156 #define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop(); return FALSE; }while(0)
158 #define RAISE( x, v ) do{ \
159 if( sh4r.vbr == 0 ) { \
160 ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
163 sh4r.spc = sh4r.pc + 2; \
164 sh4r.ssr = sh4_read_sr(); \
165 sh4r.sgr = sh4r.r[15]; \
166 MMIO_WRITE(MMU,EXPEVT,x); \
167 sh4r.pc = sh4r.vbr + v; \
168 sh4r.new_pc = sh4r.pc + 2; \
169 sh4_load_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
171 return TRUE; } while(0)
173 #define MEM_READ_BYTE( addr ) sh4_read_byte(addr)
174 #define MEM_READ_WORD( addr ) sh4_read_word(addr)
175 #define MEM_READ_LONG( addr ) sh4_read_long(addr)
176 #define MEM_WRITE_BYTE( addr, val ) sh4_write_byte(addr, val)
177 #define MEM_WRITE_WORD( addr, val ) sh4_write_word(addr, val)
178 #define MEM_WRITE_LONG( addr, val ) sh4_write_long(addr, val)
180 #define MEM_FP_READ( addr, reg ) if( IS_FPU_DOUBLESIZE() ) { \
181 ((uint32_t *)FR)[(reg)&0xE0] = sh4_read_long(addr); \
182 ((uint32_t *)FR)[(reg)|1] = sh4_read_long(addr+4); \
183 } else ((uint32_t *)FR)[reg] = sh4_read_long(addr)
185 #define MEM_FP_WRITE( addr, reg ) if( IS_FPU_DOUBLESIZE() ) { \
186 sh4_write_long( addr, ((uint32_t *)FR)[(reg)&0xE0] ); \
187 sh4_write_long( addr+4, ((uint32_t *)FR)[(reg)|1] ); \
188 } else sh4_write_long( addr, ((uint32_t *)FR)[reg] )
190 #define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
192 #define CHECK( x, c, v ) if( !x ) RAISE( c, v )
193 #define CHECKPRIV() CHECK( IS_SH4_PRIVMODE(), EXC_ILLEGAL, EXV_ILLEGAL )
194 #define CHECKFPUEN() CHECK( IS_FPU_ENABLED(), EXC_FPDISABLE, EXV_FPDISABLE )
195 #define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); sh4_stop(); return; }
196 #define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) { RAISE(EXC_SLOT_ILLEGAL,EXV_ILLEGAL); }
198 static void sh4_switch_banks( )
202 memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
203 memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
204 memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
207 static void sh4_load_sr( uint32_t newval )
209 if( (newval ^ sh4r.sr) & SR_RB )
212 sh4r.t = (newval&SR_T) ? 1 : 0;
213 sh4r.s = (newval&SR_S) ? 1 : 0;
214 sh4r.m = (newval&SR_M) ? 1 : 0;
215 sh4r.q = (newval&SR_Q) ? 1 : 0;
219 static uint32_t sh4_read_sr( void )
221 /* synchronize sh4r.sr with the various bitflags */
222 sh4r.sr &= SR_MQSTMASK;
223 if( sh4r.t ) sh4r.sr |= SR_T;
224 if( sh4r.s ) sh4r.sr |= SR_S;
225 if( sh4r.m ) sh4r.sr |= SR_M;
226 if( sh4r.q ) sh4r.sr |= SR_Q;
229 /* function for external use */
230 void sh4_raise_exception( int code, int vector )
235 static void sh4_accept_interrupt( void )
237 uint32_t code = intc_accept_interrupt();
238 sh4r.ssr = sh4_read_sr();
240 sh4r.sgr = sh4r.r[15];
241 sh4_load_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
242 MMIO_WRITE( MMU, INTEVT, code );
243 sh4r.pc = sh4r.vbr + 0x600;
244 sh4r.new_pc = sh4r.pc + 2;
245 WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
248 gboolean sh4_execute_instruction( void )
257 #define RN(ir) sh4r.r[(ir&0x0F00)>>8]
258 #define RN_BANK(ir) sh4r.r_bank[(ir&0x0070)>>4]
259 #define RM(ir) sh4r.r[(ir&0x00F0)>>4]
260 #define DISP4(ir) (ir&0x000F) /* 4-bit displacements are *NOT* sign-extended */
261 #define DISP8(ir) (ir&0x00FF)
262 #define PCDISP8(ir) SIGNEXT8(ir&0x00FF)
263 #define IMM8(ir) SIGNEXT8(ir&0x00FF)
264 #define UIMM8(ir) (ir&0x00FF) /* Unsigned immmediate */
265 #define DISP12(ir) SIGNEXT12(ir&0x0FFF)
266 #define FVN(ir) ((ir&0x0C00)>>8)
267 #define FVM(ir) ((ir&0x0300)>>6)
268 #define FRN(ir) (FR[(ir&0x0F00)>>8])
269 #define FRM(ir) (FR[(ir&0x00F0)>>4])
270 #define FRNi(ir) (((uint32_t *)FR)[(ir&0x0F00)>>8])
271 #define FRMi(ir) (((uint32_t *)FR)[(ir&0x00F0)>>4])
272 #define DRN(ir) (((double *)FR)[(ir&0x0E00)>>9])
273 #define DRM(ir) (((double *)FR)[(ir&0x00E0)>>5])
274 #define DRNi(ir) (((uint64_t *)FR)[(ir&0x0E00)>>9])
275 #define DRMi(ir) (((uint64_t *)FR)[(ir&0x00E0)>>5])
276 #define FRNn(ir) ((ir&0x0F00)>>8)
277 #define FRMn(ir) ((ir&0x00F0)>>4)
278 #define FPULf *((float *)&sh4r.fpul)
279 #define FPULi (sh4r.fpul)
281 if( SH4_INT_PENDING() )
282 sh4_accept_interrupt();
285 ir = MEM_READ_WORD(pc);
288 switch( (ir&0xF000)>>12 ) {
289 case 0: /* 0000nnnnmmmmxxxx */
290 switch( ir&0x000F ) {
292 switch( (ir&0x00F0)>>4 ) {
293 case 0: /* STC SR, Rn */
295 RN(ir) = sh4_read_sr();
297 case 1: /* STC GBR, Rn */
300 case 2: /* STC VBR, Rn */
304 case 3: /* STC SSR, Rn */
308 case 4: /* STC SPC, Rn */
312 case 8: case 9: case 10: case 11: case 12: case 13:
313 case 14: case 15:/* STC Rm_bank, Rn */
315 RN(ir) = RN_BANK(ir);
321 switch( (ir&0x00F0)>>4 ) {
322 case 0: /* BSRF Rn */
323 CHECKDEST( pc + 4 + RN(ir) );
325 sh4r.in_delay_slot = 1;
326 sh4r.pr = sh4r.pc + 4;
327 sh4r.pc = sh4r.new_pc;
328 sh4r.new_pc = pc + 4 + RN(ir);
330 case 2: /* BRAF Rn */
331 CHECKDEST( pc + 4 + RN(ir) );
333 sh4r.in_delay_slot = 1;
334 sh4r.pc = sh4r.new_pc;
335 sh4r.new_pc = pc + 4 + RN(ir);
337 case 8: /* PREF [Rn] */
339 if( (tmp & 0xFC000000) == 0xE0000000 ) {
340 /* Store queue operation */
341 int queue = (tmp&0x20)>>2;
342 int32_t *src = &sh4r.store_queue[queue];
343 uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
344 uint32_t target = tmp&0x03FFFFE0 | hi;
345 mem_copy_to_sh4( target, src, 32 );
346 WARN( "Executed SQ%c => %08X",
347 (queue == 0 ? '0' : '1'), target );
350 case 9: /* OCBI [Rn] */
351 case 10:/* OCBP [Rn] */
352 case 11:/* OCBWB [Rn] */
355 case 12:/* MOVCA.L R0, [Rn] */
360 case 4: /* MOV.B Rm, [R0 + Rn] */
361 MEM_WRITE_BYTE( R0 + RN(ir), RM(ir) );
363 case 5: /* MOV.W Rm, [R0 + Rn] */
364 MEM_WRITE_WORD( R0 + RN(ir), RM(ir) );
366 case 6: /* MOV.L Rm, [R0 + Rn] */
367 MEM_WRITE_LONG( R0 + RN(ir), RM(ir) );
369 case 7: /* MUL.L Rm, Rn */
370 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
374 switch( (ir&0x0FF0)>>4 ) {
396 if( (ir&0x00F0) == 0x20 ) /* MOVT Rn */
398 else if( ir == 0x0019 ) /* DIV0U */
399 sh4r.m = sh4r.q = sh4r.t = 0;
400 else if( ir == 0x0009 )
405 switch( (ir&0x00F0) >> 4 ) {
406 case 0: /* STS MACH, Rn */
407 RN(ir) = sh4r.mac >> 32;
409 case 1: /* STS MACL, Rn */
410 RN(ir) = (uint32_t)sh4r.mac;
412 case 2: /* STS PR, Rn */
415 case 3: /* STC SGR, Rn */
419 case 5:/* STS FPUL, Rn */
422 case 6: /* STS FPSCR, Rn */
425 case 15:/* STC DBR, Rn */
433 switch( (ir&0x0FF0)>>4 ) {
435 CHECKDEST( sh4r.pr );
437 sh4r.in_delay_slot = 1;
438 sh4r.pc = sh4r.new_pc;
439 sh4r.new_pc = sh4r.pr;
442 if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
443 sh4r.sh4_state = SH4_STATE_STANDBY;
445 sh4r.sh4_state = SH4_STATE_SLEEP;
447 return FALSE; /* Halt CPU */
450 CHECKDEST( sh4r.spc );
452 sh4r.in_delay_slot = 1;
453 sh4r.pc = sh4r.new_pc;
454 sh4r.new_pc = sh4r.spc;
455 sh4_load_sr( sh4r.ssr );
456 WARN( "RTE => %08X", sh4r.new_pc );
461 case 12:/* MOV.B [R0+R%d], R%d */
462 RN(ir) = MEM_READ_BYTE( R0 + RM(ir) );
464 case 13:/* MOV.W [R0+R%d], R%d */
465 RN(ir) = MEM_READ_WORD( R0 + RM(ir) );
467 case 14:/* MOV.L [R0+R%d], R%d */
468 RN(ir) = MEM_READ_LONG( R0 + RM(ir) );
470 case 15:/* MAC.L [Rm++], [Rn++] */
471 tmpl = ( SIGNEXT32(MEM_READ_LONG(RM(ir))) *
472 SIGNEXT32(MEM_READ_LONG(RN(ir))) );
474 /* 48-bit Saturation. Yuch */
475 tmpl += SIGNEXT48(sh4r.mac);
476 if( tmpl < 0xFFFF800000000000LL )
477 tmpl = 0xFFFF800000000000LL;
478 else if( tmpl > 0x00007FFFFFFFFFFFLL )
479 tmpl = 0x00007FFFFFFFFFFFLL;
480 sh4r.mac = (sh4r.mac&0xFFFF000000000000LL) |
481 (tmpl&0x0000FFFFFFFFFFFFLL);
482 } else sh4r.mac = tmpl;
491 case 1: /* 0001nnnnmmmmdddd */
492 /* MOV.L Rm, [Rn + disp4*4] */
493 MEM_WRITE_LONG( RN(ir) + (DISP4(ir)<<2), RM(ir) );
495 case 2: /* 0010nnnnmmmmxxxx */
496 switch( ir&0x000F ) {
497 case 0: /* MOV.B Rm, [Rn] */
498 MEM_WRITE_BYTE( RN(ir), RM(ir) );
500 case 1: /* MOV.W Rm, [Rn] */
501 MEM_WRITE_WORD( RN(ir), RM(ir) );
503 case 2: /* MOV.L Rm, [Rn] */
504 MEM_WRITE_LONG( RN(ir), RM(ir) );
508 case 4: /* MOV.B Rm, [--Rn] */
510 MEM_WRITE_BYTE( RN(ir), RM(ir) );
512 case 5: /* MOV.W Rm, [--Rn] */
514 MEM_WRITE_WORD( RN(ir), RM(ir) );
516 case 6: /* MOV.L Rm, [--Rn] */
518 MEM_WRITE_LONG( RN(ir), RM(ir) );
520 case 7: /* DIV0S Rm, Rn */
523 sh4r.t = sh4r.q ^ sh4r.m;
525 case 8: /* TST Rm, Rn */
526 sh4r.t = (RN(ir)&RM(ir) ? 0 : 1);
528 case 9: /* AND Rm, Rn */
531 case 10:/* XOR Rm, Rn */
534 case 11:/* OR Rm, Rn */
537 case 12:/* CMP/STR Rm, Rn */
538 /* set T = 1 if any byte in RM & RN is the same */
539 tmp = RM(ir) ^ RN(ir);
540 sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
541 (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
543 case 13:/* XTRCT Rm, Rn */
544 RN(ir) = (RN(ir)>>16) | (RM(ir)<<16);
546 case 14:/* MULU.W Rm, Rn */
547 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
548 (uint32_t)((RM(ir)&0xFFFF) * (RN(ir)&0xFFFF));
550 case 15:/* MULS.W Rm, Rn */
551 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
552 (uint32_t)(SIGNEXT32(RM(ir)&0xFFFF) * SIGNEXT32(RN(ir)&0xFFFF));
556 case 3: /* 0011nnnnmmmmxxxx */
557 switch( ir&0x000F ) {
558 case 0: /* CMP/EQ Rm, Rn */
559 sh4r.t = ( RM(ir) == RN(ir) ? 1 : 0 );
561 case 2: /* CMP/HS Rm, Rn */
562 sh4r.t = ( RN(ir) >= RM(ir) ? 1 : 0 );
564 case 3: /* CMP/GE Rm, Rn */
565 sh4r.t = ( ((int32_t)RN(ir)) >= ((int32_t)RM(ir)) ? 1 : 0 );
567 case 4: { /* DIV1 Rm, Rn */
568 /* This is just from the sh4p manual with some
569 * simplifications (someone want to check it's correct? :)
570 * Why they couldn't just provide a real DIV instruction...
571 * Please oh please let the translator batch these things
572 * up into a single DIV... */
573 uint32_t tmp0, tmp1, tmp2, dir;
575 dir = sh4r.q ^ sh4r.m;
576 sh4r.q = (RN(ir) >> 31);
578 RN(ir) = (RN(ir) << 1) | sh4r.t;
582 tmp1 = (RN(ir)<tmp0 ? 1 : 0 );
585 tmp1 = (RN(ir)>tmp0 ? 1 : 0 );
587 sh4r.q ^= sh4r.m ^ tmp1;
588 sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
590 case 5: /* DMULU.L Rm, Rn */
591 sh4r.mac = ((uint64_t)RM(ir)) * ((uint64_t)RN(ir));
593 case 6: /* CMP/HI Rm, Rn */
594 sh4r.t = ( RN(ir) > RM(ir) ? 1 : 0 );
596 case 7: /* CMP/GT Rm, Rn */
597 sh4r.t = ( ((int32_t)RN(ir)) > ((int32_t)RM(ir)) ? 1 : 0 );
599 case 8: /* SUB Rm, Rn */
602 case 10:/* SUBC Rm, Rn */
604 RN(ir) = RN(ir) - RM(ir) - sh4r.t;
605 sh4r.t = (RN(ir) > tmp || (RN(ir) == tmp && sh4r.t == 1));
607 case 11:/* SUBV Rm, Rn */
610 case 12:/* ADD Rm, Rn */
613 case 13:/* DMULS.L Rm, Rn */
614 sh4r.mac = SIGNEXT32(RM(ir)) * SIGNEXT32(RN(ir));
616 case 14:/* ADDC Rm, Rn */
618 RN(ir) += RM(ir) + sh4r.t;
619 sh4r.t = ( RN(ir) < tmp || (RN(ir) == tmp && sh4r.t != 0) ? 1 : 0 );
621 case 15:/* ADDV Rm, Rn */
627 case 4: /* 0100nnnnxxxxxxxx */
628 switch( ir&0x00FF ) {
629 case 0x00: /* SHLL Rn */
630 sh4r.t = RN(ir) >> 31;
633 case 0x01: /* SHLR Rn */
634 sh4r.t = RN(ir) & 0x00000001;
637 case 0x02: /* STS.L MACH, [--Rn] */
639 MEM_WRITE_LONG( RN(ir), (sh4r.mac>>32) );
641 case 0x03: /* STC.L SR, [--Rn] */
644 MEM_WRITE_LONG( RN(ir), sh4_read_sr() );
646 case 0x04: /* ROTL Rn */
647 sh4r.t = RN(ir) >> 31;
651 case 0x05: /* ROTR Rn */
652 sh4r.t = RN(ir) & 0x00000001;
654 RN(ir) |= (sh4r.t << 31);
656 case 0x06: /* LDS.L [Rn++], MACH */
657 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
658 (((uint64_t)MEM_READ_LONG(RN(ir)))<<32);
661 case 0x07: /* LDC.L [Rn++], SR */
663 sh4_load_sr( MEM_READ_LONG(RN(ir)) );
666 case 0x08: /* SHLL2 Rn */
669 case 0x09: /* SHLR2 Rn */
672 case 0x0A: /* LDS Rn, MACH */
673 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
674 (((uint64_t)RN(ir))<<32);
676 case 0x0B: /* JSR [Rn] */
679 sh4r.in_delay_slot = 1;
680 sh4r.pc = sh4r.new_pc;
681 sh4r.new_pc = RN(ir);
684 case 0x0E: /* LDC Rn, SR */
686 sh4_load_sr( RN(ir) );
688 case 0x10: /* DT Rn */
690 sh4r.t = ( RN(ir) == 0 ? 1 : 0 );
692 case 0x11: /* CMP/PZ Rn */
693 sh4r.t = ( ((int32_t)RN(ir)) >= 0 ? 1 : 0 );
695 case 0x12: /* STS.L MACL, [--Rn] */
697 MEM_WRITE_LONG( RN(ir), (uint32_t)sh4r.mac );
699 case 0x13: /* STC.L GBR, [--Rn] */
701 MEM_WRITE_LONG( RN(ir), sh4r.gbr );
703 case 0x15: /* CMP/PL Rn */
704 sh4r.t = ( ((int32_t)RN(ir)) > 0 ? 1 : 0 );
706 case 0x16: /* LDS.L [Rn++], MACL */
707 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
708 (uint64_t)((uint32_t)MEM_READ_LONG(RN(ir)));
711 case 0x17: /* LDC.L [Rn++], GBR */
712 sh4r.gbr = MEM_READ_LONG(RN(ir));
715 case 0x18: /* SHLL8 Rn */
718 case 0x19: /* SHLR8 Rn */
721 case 0x1A: /* LDS Rn, MACL */
722 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
723 (uint64_t)((uint32_t)(RN(ir)));
725 case 0x1B: /* TAS.B [Rn] */
726 tmp = MEM_READ_BYTE( RN(ir) );
727 sh4r.t = ( tmp == 0 ? 1 : 0 );
728 MEM_WRITE_BYTE( RN(ir), tmp | 0x80 );
730 case 0x1E: /* LDC Rn, GBR */
733 case 0x20: /* SHAL Rn */
734 sh4r.t = RN(ir) >> 31;
737 case 0x21: /* SHAR Rn */
738 sh4r.t = RN(ir) & 0x00000001;
739 RN(ir) = ((int32_t)RN(ir)) >> 1;
741 case 0x22: /* STS.L PR, [--Rn] */
743 MEM_WRITE_LONG( RN(ir), sh4r.pr );
745 case 0x23: /* STC.L VBR, [--Rn] */
748 MEM_WRITE_LONG( RN(ir), sh4r.vbr );
750 case 0x24: /* ROTCL Rn */
756 case 0x25: /* ROTCR Rn */
757 tmp = RN(ir) & 0x00000001;
759 RN(ir) |= (sh4r.t << 31 );
762 case 0x26: /* LDS.L [Rn++], PR */
763 sh4r.pr = MEM_READ_LONG( RN(ir) );
766 case 0x27: /* LDC.L [Rn++], VBR */
768 sh4r.vbr = MEM_READ_LONG(RN(ir));
771 case 0x28: /* SHLL16 Rn */
774 case 0x29: /* SHLR16 Rn */
777 case 0x2A: /* LDS Rn, PR */
780 case 0x2B: /* JMP [Rn] */
783 sh4r.in_delay_slot = 1;
784 sh4r.pc = sh4r.new_pc;
785 sh4r.new_pc = RN(ir);
787 case 0x2E: /* LDC Rn, VBR */
791 case 0x32: /* STC.L SGR, [--Rn] */
794 MEM_WRITE_LONG( RN(ir), sh4r.sgr );
796 case 0x33: /* STC.L SSR, [--Rn] */
799 MEM_WRITE_LONG( RN(ir), sh4r.ssr );
801 case 0x37: /* LDC.L [Rn++], SSR */
803 sh4r.ssr = MEM_READ_LONG(RN(ir));
806 case 0x3E: /* LDC Rn, SSR */
810 case 0x43: /* STC.L SPC, [--Rn] */
813 MEM_WRITE_LONG( RN(ir), sh4r.spc );
815 case 0x47: /* LDC.L [Rn++], SPC */
817 sh4r.spc = MEM_READ_LONG(RN(ir));
820 case 0x4E: /* LDC Rn, SPC */
824 case 0x52: /* STS.L FPUL, [--Rn] */
826 MEM_WRITE_LONG( RN(ir), sh4r.fpul );
828 case 0x56: /* LDS.L [Rn++], FPUL */
829 sh4r.fpul = MEM_READ_LONG(RN(ir));
832 case 0x5A: /* LDS Rn, FPUL */
835 case 0x62: /* STS.L FPSCR, [--Rn] */
837 MEM_WRITE_LONG( RN(ir), sh4r.fpscr );
839 case 0x66: /* LDS.L [Rn++], FPSCR */
840 sh4r.fpscr = MEM_READ_LONG(RN(ir));
843 case 0x6A: /* LDS Rn, FPSCR */
846 case 0xF2: /* STC.L DBR, [--Rn] */
849 MEM_WRITE_LONG( RN(ir), sh4r.dbr );
851 case 0xF6: /* LDC.L [Rn++], DBR */
853 sh4r.dbr = MEM_READ_LONG(RN(ir));
856 case 0xFA: /* LDC Rn, DBR */
860 case 0x83: case 0x93: case 0xA3: case 0xB3: case 0xC3:
861 case 0xD3: case 0xE3: case 0xF3: /* STC.L Rn_BANK, [--Rn] */
864 MEM_WRITE_LONG( RN(ir), RN_BANK(ir) );
866 case 0x87: case 0x97: case 0xA7: case 0xB7: case 0xC7:
867 case 0xD7: case 0xE7: case 0xF7: /* LDC.L [Rn++], Rn_BANK */
869 RN_BANK(ir) = MEM_READ_LONG( RN(ir) );
872 case 0x8E: case 0x9E: case 0xAE: case 0xBE: case 0xCE:
873 case 0xDE: case 0xEE: case 0xFE: /* LDC Rm, Rn_BANK */
875 RN_BANK(ir) = RM(ir);
878 if( (ir&0x000F) == 0x0F ) {
879 /* MAC.W [Rm++], [Rn++] */
880 tmp = SIGNEXT16(MEM_READ_WORD(RM(ir))) *
881 SIGNEXT16(MEM_READ_WORD(RN(ir)));
885 } else sh4r.mac += SIGNEXT32(tmp);
888 } else if( (ir&0x000F) == 0x0C ) {
891 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
892 else if( (tmp & 0x1F) == 0 )
893 RN(ir) = ((int32_t)RN(ir)) >> 31;
895 RN(ir) = ((int32_t)RN(ir)) >> (((~RM(ir)) & 0x1F)+1);
896 } else if( (ir&0x000F) == 0x0D ) {
899 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
900 else if( (tmp & 0x1F) == 0 ) RN(ir) = 0;
901 else RN(ir) >>= (((~tmp) & 0x1F)+1);
905 case 5: /* 0101nnnnmmmmdddd */
906 /* MOV.L [Rm + disp4*4], Rn */
907 RN(ir) = MEM_READ_LONG( RM(ir) + (DISP4(ir)<<2) );
909 case 6: /* 0110xxxxxxxxxxxx */
910 switch( ir&0x000f ) {
911 case 0: /* MOV.B [Rm], Rn */
912 RN(ir) = MEM_READ_BYTE( RM(ir) );
914 case 1: /* MOV.W [Rm], Rn */
915 RN(ir) = MEM_READ_WORD( RM(ir) );
917 case 2: /* MOV.L [Rm], Rn */
918 RN(ir) = MEM_READ_LONG( RM(ir) );
920 case 3: /* MOV Rm, Rn */
923 case 4: /* MOV.B [Rm++], Rn */
924 RN(ir) = MEM_READ_BYTE( RM(ir) );
927 case 5: /* MOV.W [Rm++], Rn */
928 RN(ir) = MEM_READ_WORD( RM(ir) );
931 case 6: /* MOV.L [Rm++], Rn */
932 RN(ir) = MEM_READ_LONG( RM(ir) );
935 case 7: /* NOT Rm, Rn */
938 case 8: /* SWAP.B Rm, Rn */
939 RN(ir) = (RM(ir)&0xFFFF0000) | ((RM(ir)&0x0000FF00)>>8) |
940 ((RM(ir)&0x000000FF)<<8);
942 case 9: /* SWAP.W Rm, Rn */
943 RN(ir) = (RM(ir)>>16) | (RM(ir)<<16);
945 case 10:/* NEGC Rm, Rn */
947 RN(ir) = tmp - sh4r.t;
948 sh4r.t = ( 0<tmp || tmp<RN(ir) ? 1 : 0 );
950 case 11:/* NEG Rm, Rn */
953 case 12:/* EXTU.B Rm, Rn */
954 RN(ir) = RM(ir)&0x000000FF;
956 case 13:/* EXTU.W Rm, Rn */
957 RN(ir) = RM(ir)&0x0000FFFF;
959 case 14:/* EXTS.B Rm, Rn */
960 RN(ir) = SIGNEXT8( RM(ir)&0x000000FF );
962 case 15:/* EXTS.W Rm, Rn */
963 RN(ir) = SIGNEXT16( RM(ir)&0x0000FFFF );
967 case 7: /* 0111nnnniiiiiiii */
971 case 8: /* 1000xxxxxxxxxxxx */
972 switch( (ir&0x0F00) >> 8 ) {
973 case 0: /* MOV.B R0, [Rm + disp4] */
974 MEM_WRITE_BYTE( RM(ir) + DISP4(ir), R0 );
976 case 1: /* MOV.W R0, [Rm + disp4*2] */
977 MEM_WRITE_WORD( RM(ir) + (DISP4(ir)<<1), R0 );
979 case 4: /* MOV.B [Rm + disp4], R0 */
980 R0 = MEM_READ_BYTE( RM(ir) + DISP4(ir) );
982 case 5: /* MOV.W [Rm + disp4*2], R0 */
983 R0 = MEM_READ_WORD( RM(ir) + (DISP4(ir)<<1) );
985 case 8: /* CMP/EQ imm, R0 */
986 sh4r.t = ( R0 == IMM8(ir) ? 1 : 0 );
988 case 9: /* BT disp8 */
991 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
992 sh4r.pc += (PCDISP8(ir)<<1) + 4;
993 sh4r.new_pc = sh4r.pc + 2;
997 case 11:/* BF disp8 */
1000 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1001 sh4r.pc += (PCDISP8(ir)<<1) + 4;
1002 sh4r.new_pc = sh4r.pc + 2;
1006 case 13:/* BT/S disp8 */
1009 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1010 sh4r.in_delay_slot = 1;
1011 sh4r.pc = sh4r.new_pc;
1012 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1013 sh4r.in_delay_slot = 1;
1017 case 15:/* BF/S disp8 */
1020 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1021 sh4r.in_delay_slot = 1;
1022 sh4r.pc = sh4r.new_pc;
1023 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1030 case 9: /* 1001xxxxxxxxxxxx */
1031 /* MOV.W [disp8*2 + pc + 4], Rn */
1032 RN(ir) = MEM_READ_WORD( pc + 4 + (DISP8(ir)<<1) );
1034 case 10:/* 1010dddddddddddd */
1036 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1038 sh4r.in_delay_slot = 1;
1039 sh4r.pc = sh4r.new_pc;
1040 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1042 case 11:/* 1011dddddddddddd */
1044 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1046 sh4r.in_delay_slot = 1;
1048 sh4r.pc = sh4r.new_pc;
1049 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1051 case 12:/* 1100xxxxdddddddd */
1052 switch( (ir&0x0F00)>>8 ) {
1053 case 0: /* MOV.B R0, [GBR + disp8] */
1054 MEM_WRITE_BYTE( sh4r.gbr + DISP8(ir), R0 );
1056 case 1: /* MOV.W R0, [GBR + disp8*2] */
1057 MEM_WRITE_WORD( sh4r.gbr + (DISP8(ir)<<1), R0 );
1059 case 2: /*MOV.L R0, [GBR + disp8*4] */
1060 MEM_WRITE_LONG( sh4r.gbr + (DISP8(ir)<<2), R0 );
1062 case 3: /* TRAPA imm8 */
1064 sh4r.in_delay_slot = 1;
1065 MMIO_WRITE( MMU, TRA, UIMM8(ir) );
1066 sh4r.pc = sh4r.new_pc; /* RAISE ends the instruction */
1068 RAISE( EXC_TRAP, EXV_TRAP );
1070 case 4: /* MOV.B [GBR + disp8], R0 */
1071 R0 = MEM_READ_BYTE( sh4r.gbr + DISP8(ir) );
1073 case 5: /* MOV.W [GBR + disp8*2], R0 */
1074 R0 = MEM_READ_WORD( sh4r.gbr + (DISP8(ir)<<1) );
1076 case 6: /* MOV.L [GBR + disp8*4], R0 */
1077 R0 = MEM_READ_LONG( sh4r.gbr + (DISP8(ir)<<2) );
1079 case 7: /* MOVA disp8 + pc&~3 + 4, R0 */
1080 R0 = (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4;
1082 case 8: /* TST imm8, R0 */
1083 sh4r.t = (R0 & UIMM8(ir) ? 0 : 1);
1085 case 9: /* AND imm8, R0 */
1088 case 10:/* XOR imm8, R0 */
1091 case 11:/* OR imm8, R0 */
1094 case 12:/* TST.B imm8, [R0+GBR] */
1095 sh4r.t = ( MEM_READ_BYTE(R0 + sh4r.gbr) & UIMM8(ir) ? 0 : 1 );
1097 case 13:/* AND.B imm8, [R0+GBR] */
1098 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1099 UIMM8(ir) & MEM_READ_BYTE(R0 + sh4r.gbr) );
1101 case 14:/* XOR.B imm8, [R0+GBR] */
1102 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1103 UIMM8(ir) ^ MEM_READ_BYTE(R0 + sh4r.gbr) );
1105 case 15:/* OR.B imm8, [R0+GBR] */
1106 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1107 UIMM8(ir) | MEM_READ_BYTE(R0 + sh4r.gbr) );
1111 case 13:/* 1101nnnndddddddd */
1112 /* MOV.L [disp8*4 + pc&~3 + 4], Rn */
1113 RN(ir) = MEM_READ_LONG( (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4 );
1115 case 14:/* 1110nnnniiiiiiii */
1119 case 15:/* 1111xxxxxxxxxxxx */
1121 switch( ir&0x000F ) {
1122 case 0: /* FADD FRm, FRn */
1125 case 1: /* FSUB FRm, FRn */
1128 case 2: /* FMUL FRm, FRn */
1129 FRN(ir) = FRN(ir) * FRM(ir);
1131 case 3: /* FDIV FRm, FRn */
1132 FRN(ir) = FRN(ir) / FRM(ir);
1134 case 4: /* FCMP/EQ FRm, FRn */
1135 sh4r.t = ( FRN(ir) == FRM(ir) ? 1 : 0 );
1137 case 5: /* FCMP/GT FRm, FRn */
1138 sh4r.t = ( FRN(ir) > FRM(ir) ? 1 : 0 );
1140 case 6: /* FMOV.S [Rm+R0], FRn */
1141 MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
1143 case 7: /* FMOV.S FRm, [Rn+R0] */
1144 MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
1146 case 8: /* FMOV.S [Rm], FRn */
1147 MEM_FP_READ( RM(ir), FRNn(ir) );
1149 case 9: /* FMOV.S [Rm++], FRn */
1150 MEM_FP_READ( RM(ir), FRNn(ir) );
1153 case 10:/* FMOV.S FRm, [Rn] */
1154 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1156 case 11:/* FMOV.S FRm, [--Rn] */
1158 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1160 case 12:/* FMOV FRm, FRn */
1161 if( IS_FPU_DOUBLESIZE() ) {
1168 switch( (ir&0x00F0) >> 4 ) {
1169 case 0: /* FSTS FPUL, FRn */
1172 case 1: /* FLDS FRn, FPUL */
1175 case 2: /* FLOAT FPUL, FRn */
1176 FRN(ir) = (float)FPULi;
1178 case 3: /* FTRC FRn, FPUL */
1179 FPULi = (uint32_t)FRN(ir);
1180 /* FIXME: is this sufficient? */
1182 case 4: /* FNEG FRn */
1185 case 5: /* FABS FRn */
1186 FRN(ir) = fabsf(FRN(ir));
1188 case 6: /* FSQRT FRn */
1189 FRN(ir) = sqrtf(FRN(ir));
1191 case 7: /* FSRRA FRn */
1192 FRN(ir) = 1.0/sqrtf(FRN(ir));
1194 case 8: /* FLDI0 FRn */
1197 case 9: /* FLDI1 FRn */
1200 case 10: /* FCNVSD FPUL, DRn */
1201 if( IS_FPU_DOUBLEPREC() )
1202 DRN(ir) = (double)FPULf;
1205 case 11: /* FCNVDS DRn, FPUL */
1206 if( IS_FPU_DOUBLEPREC() )
1207 FPULf = (float)DRN(ir);
1210 case 14:/* FIPR FVm, FVn */
1211 /* FIXME: This is not going to be entirely accurate
1212 * as the SH4 instruction is less precise. Also
1213 * need to check for 0s and infinities.
1216 float *fr_bank = FR;
1219 fr_bank[tmp2+3] = fr_bank[tmp]*fr_bank[tmp2] +
1220 fr_bank[tmp+1]*fr_bank[tmp2+1] +
1221 fr_bank[tmp+2]*fr_bank[tmp2+2] +
1222 fr_bank[tmp+3]*fr_bank[tmp2+3];
1226 if( (ir&0x0300) == 0x0100 ) { /* FTRV XMTRX,FVn */
1227 float *fvout = FR+FVN(ir);
1229 float fv[4] = { fvout[0], fvout[1], fvout[2], fvout[3] };
1230 fvout[0] = xm[0] * fv[0] + xm[4]*fv[1] +
1231 xm[8]*fv[2] + xm[12]*fv[3];
1232 fvout[1] = xm[1] * fv[0] + xm[5]*fv[1] +
1233 xm[9]*fv[2] + xm[13]*fv[3];
1234 fvout[2] = xm[2] * fv[0] + xm[6]*fv[1] +
1235 xm[10]*fv[2] + xm[14]*fv[3];
1236 fvout[3] = xm[3] * fv[0] + xm[7]*fv[1] +
1237 xm[11]*fv[2] + xm[15]*fv[3];
1240 else if( (ir&0x0100) == 0 ) { /* FSCA FPUL, DRn */
1241 float angle = (((float)(short)(FPULi>>16)) +
1242 ((float)(FPULi&16)/65536.0)) *
1245 FR[reg] = sinf(angle);
1246 FR[reg+1] = cosf(angle);
1249 else if( ir == 0xFBFD ) {
1251 sh4r.fpscr ^= FPSCR_FR;
1254 else if( ir == 0xF3FD ) {
1256 sh4r.fpscr ^= FPSCR_SZ;
1262 case 14:/* FMAC FR0, FRm, FRn */
1263 FRN(ir) += FRM(ir)*FR0;
1269 sh4r.pc = sh4r.new_pc;
1271 sh4r.in_delay_slot = 0;
.