2 * $Id: sh4core.c,v 1.19 2006-02-05 04:02:57 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
23 #include "sh4/sh4core.h"
24 #include "sh4/sh4mmio.h"
30 /* CPU-generated exception code/vector pairs */
31 #define EXC_POWER_RESET 0x000 /* vector special */
32 #define EXC_MANUAL_RESET 0x020
33 #define EXC_SLOT_ILLEGAL 0x1A0
34 #define EXC_ILLEGAL 0x180
35 #define EXV_ILLEGAL 0x100
36 #define EXC_TRAP 0x160
37 #define EXV_TRAP 0x100
38 #define EXC_FPDISABLE 0x800
39 #define EXV_FPDISABLE 0x100
41 /********************** SH4 Module Definition ****************************/
43 void sh4_init( void );
44 void sh4_reset( void );
45 uint32_t sh4_run_slice( uint32_t );
46 void sh4_start( void );
47 void sh4_stop( void );
48 void sh4_save_state( FILE *f );
49 int sh4_load_state( FILE *f );
51 struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset,
52 NULL, sh4_run_slice, sh4_stop,
53 sh4_save_state, sh4_load_state };
55 struct sh4_registers sh4r;
59 register_io_regions( mmio_list_sh4mmio );
66 /* zero everything out, for the sake of having a consistent state. */
67 memset( &sh4r, 0, sizeof(sh4r) );
69 /* Resume running if we were halted */
70 sh4r.sh4_state = SH4_STATE_RUNNING;
73 sh4r.new_pc= 0xA0000002;
74 sh4r.vbr = 0x00000000;
75 sh4r.fpscr = 0x00040001;
78 /* Mem reset will do this, but if we want to reset _just_ the SH4... */
79 MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
81 /* Peripheral modules */
86 static struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
87 static int sh4_breakpoint_count = 0;
89 void sh4_set_breakpoint( uint32_t pc, int type )
91 sh4_breakpoints[sh4_breakpoint_count].address = pc;
92 sh4_breakpoints[sh4_breakpoint_count].type = type;
93 sh4_breakpoint_count++;
96 gboolean sh4_clear_breakpoint( uint32_t pc, int type )
100 for( i=0; i<sh4_breakpoint_count; i++ ) {
101 if( sh4_breakpoints[i].address == pc &&
102 sh4_breakpoints[i].type == type ) {
103 while( ++i < sh4_breakpoint_count ) {
104 sh4_breakpoints[i-1].address = sh4_breakpoints[i].address;
105 sh4_breakpoints[i-1].type = sh4_breakpoints[i].type;
107 sh4_breakpoint_count--;
114 int sh4_get_breakpoint( uint32_t pc )
117 for( i=0; i<sh4_breakpoint_count; i++ ) {
118 if( sh4_breakpoints[i].address == pc )
119 return sh4_breakpoints[i].type;
124 uint32_t sh4_run_slice( uint32_t nanosecs )
126 int target = sh4r.icount + nanosecs / sh4_cpu_period;
127 int start = sh4r.icount;
130 if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
131 if( sh4r.int_pending != 0 )
132 sh4r.sh4_state = SH4_STATE_RUNNING;;
135 for( sh4r.slice_cycle = 0; sh4r.slice_cycle < nanosecs; sh4r.slice_cycle += sh4_cpu_period ) {
136 if( !sh4_execute_instruction() )
138 #ifdef ENABLE_DEBUG_MODE
139 for( i=0; i<sh4_breakpoint_count; i++ ) {
140 if( sh4_breakpoints[i].address == sh4r.pc ) {
144 if( i != sh4_breakpoint_count ) {
146 if( sh4_breakpoints[i].type == BREAK_ONESHOT )
147 sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
153 /* If we aborted early, but the cpu is still technically running,
154 * we're doing a hard abort - cut the timeslice back to what we
157 if( sh4r.slice_cycle != nanosecs && sh4r.sh4_state == SH4_STATE_RUNNING ) {
158 nanosecs = sh4r.slice_cycle;
160 if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
161 TMU_run_slice( nanosecs );
162 SCIF_run_slice( nanosecs );
164 sh4r.icount += sh4r.slice_cycle / sh4_cpu_period;
173 void sh4_save_state( FILE *f )
175 fwrite( &sh4r, sizeof(sh4r), 1, f );
177 SCIF_save_state( f );
180 int sh4_load_state( FILE * f )
182 fread( &sh4r, sizeof(sh4r), 1, f );
184 return SCIF_load_state( f );
187 /********************** SH4 emulation core ****************************/
189 void sh4_set_pc( int pc )
195 #define UNDEF(ir) do{ ERROR( "Raising exception on undefined instruction at %08x, opcode = %04x", sh4r.pc, ir ); RAISE( EXC_ILLEGAL, EXV_ILLEGAL ); }while(0)
196 #define UNIMP(ir) do{ ERROR( "Halted on unimplemented instruction at %08x, opcode = %04x", sh4r.pc, ir ); dreamcast_stop(); return FALSE; }while(0)
198 #define RAISE( x, v ) do{ \
199 if( sh4r.vbr == 0 ) { \
200 ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
203 sh4r.spc = sh4r.pc + 2; \
204 sh4r.ssr = sh4_read_sr(); \
205 sh4r.sgr = sh4r.r[15]; \
206 MMIO_WRITE(MMU,EXPEVT,x); \
207 sh4r.pc = sh4r.vbr + v; \
208 sh4r.new_pc = sh4r.pc + 2; \
209 sh4_load_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
211 return TRUE; } while(0)
213 #define MEM_READ_BYTE( addr ) sh4_read_byte(addr)
214 #define MEM_READ_WORD( addr ) sh4_read_word(addr)
215 #define MEM_READ_LONG( addr ) sh4_read_long(addr)
216 #define MEM_WRITE_BYTE( addr, val ) sh4_write_byte(addr, val)
217 #define MEM_WRITE_WORD( addr, val ) sh4_write_word(addr, val)
218 #define MEM_WRITE_LONG( addr, val ) sh4_write_long(addr, val)
220 #define MEM_FR_READ( addr, reg ) *((uint32_t *)&FR(reg)) = sh4_read_long(addr)
222 #define MEM_DR_READ( addr, reg ) do { \
223 *((uint32_t *)&FR((reg) & 0x0E)) = sh4_read_long(addr); \
224 *((uint32_t *)&FR((reg) | 0x01)) = sh4_read_long(addr+4); } while(0)
226 #define MEM_FR_WRITE( addr, reg ) sh4_write_long( addr, *((uint32_t *)&FR((reg))) )
228 #define MEM_DR_WRITE( addr, reg ) do { \
229 sh4_write_long( addr, *((uint32_t *)&FR((reg)&0x0E)) ); \
230 sh4_write_long( addr+4, *((uint32_t *)&FR((reg)|0x01)) ); } while(0)
232 #define FP_WIDTH (IS_FPU_DOUBLESIZE() ? 8 : 4)
234 #define MEM_FP_READ( addr, reg ) if( IS_FPU_DOUBLESIZE() ) MEM_DR_READ(addr, reg ); else MEM_FR_READ( addr, reg )
236 #define MEM_FP_WRITE( addr, reg ) if( IS_FPU_DOUBLESIZE() ) MEM_DR_WRITE(addr, reg ); else MEM_FR_WRITE( addr, reg )
238 #define CHECK( x, c, v ) if( !x ) RAISE( c, v )
239 #define CHECKPRIV() CHECK( IS_SH4_PRIVMODE(), EXC_ILLEGAL, EXV_ILLEGAL )
240 #define CHECKFPUEN() CHECK( IS_FPU_ENABLED(), EXC_FPDISABLE, EXV_FPDISABLE )
241 #define CHECKDEST(p) if( (p) == 0 ) { ERROR( "%08X: Branch/jump to NULL, CPU halted", sh4r.pc ); dreamcast_stop(); return FALSE; }
242 #define CHECKSLOTILLEGAL() if(sh4r.in_delay_slot) { RAISE(EXC_SLOT_ILLEGAL,EXV_ILLEGAL); }
244 static void sh4_switch_banks( )
248 memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
249 memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
250 memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
253 static void sh4_load_sr( uint32_t newval )
255 if( (newval ^ sh4r.sr) & SR_RB )
258 sh4r.t = (newval&SR_T) ? 1 : 0;
259 sh4r.s = (newval&SR_S) ? 1 : 0;
260 sh4r.m = (newval&SR_M) ? 1 : 0;
261 sh4r.q = (newval&SR_Q) ? 1 : 0;
265 static uint32_t sh4_read_sr( void )
267 /* synchronize sh4r.sr with the various bitflags */
268 sh4r.sr &= SR_MQSTMASK;
269 if( sh4r.t ) sh4r.sr |= SR_T;
270 if( sh4r.s ) sh4r.sr |= SR_S;
271 if( sh4r.m ) sh4r.sr |= SR_M;
272 if( sh4r.q ) sh4r.sr |= SR_Q;
275 /* function for external use */
276 void sh4_raise_exception( int code, int vector )
281 static void sh4_accept_interrupt( void )
283 uint32_t code = intc_accept_interrupt();
284 sh4r.ssr = sh4_read_sr();
286 sh4r.sgr = sh4r.r[15];
287 sh4_load_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
288 MMIO_WRITE( MMU, INTEVT, code );
289 sh4r.pc = sh4r.vbr + 0x600;
290 sh4r.new_pc = sh4r.pc + 2;
291 // WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
294 gboolean sh4_execute_instruction( void )
304 #define RN(ir) sh4r.r[(ir&0x0F00)>>8]
305 #define RN_BANK(ir) sh4r.r_bank[(ir&0x0070)>>4]
306 #define RM(ir) sh4r.r[(ir&0x00F0)>>4]
307 #define DISP4(ir) (ir&0x000F) /* 4-bit displacements are *NOT* sign-extended */
308 #define DISP8(ir) (ir&0x00FF)
309 #define PCDISP8(ir) SIGNEXT8(ir&0x00FF)
310 #define IMM8(ir) SIGNEXT8(ir&0x00FF)
311 #define UIMM8(ir) (ir&0x00FF) /* Unsigned immmediate */
312 #define DISP12(ir) SIGNEXT12(ir&0x0FFF)
313 #define FRNn(ir) ((ir&0x0F00)>>8)
314 #define FRMn(ir) ((ir&0x00F0)>>4)
315 #define DRNn(ir) ((ir&0x0E00)>>9)
316 #define DRMn(ir) ((ir&0x00E0)>>5)
317 #define FVN(ir) ((ir&0x0C00)>>8)
318 #define FVM(ir) ((ir&0x0300)>>6)
319 #define FRN(ir) FR(FRNn(ir))
320 #define FRM(ir) FR(FRMn(ir))
321 #define FRNi(ir) (*((uint32_t *)&FR(FRNn(ir))))
322 #define FRMi(ir) (*((uint32_t *)&FR(FRMn(ir))))
323 #define DRN(ir) DR(DRNn(ir))
324 #define DRM(ir) DR(DRMn(ir))
325 #define DRNi(ir) (*((uint64_t *)&DR(FRNn(ir))))
326 #define DRMi(ir) (*((uint64_t *)&DR(FRMn(ir))))
327 #define FPULf *((float *)&sh4r.fpul)
328 #define FPULi (sh4r.fpul)
330 if( SH4_INT_PENDING() )
331 sh4_accept_interrupt();
334 if( pc > 0xFFFFFF00 ) {
336 bios_syscall( pc & 0xFF );
337 sh4r.in_delay_slot = 1;
338 pc = sh4r.pc = sh4r.pr;
339 sh4r.new_pc = sh4r.pc + 2;
341 ir = MEM_READ_WORD(pc);
344 switch( (ir&0xF000)>>12 ) {
345 case 0: /* 0000nnnnmmmmxxxx */
346 switch( ir&0x000F ) {
348 switch( (ir&0x00F0)>>4 ) {
349 case 0: /* STC SR, Rn */
351 RN(ir) = sh4_read_sr();
353 case 1: /* STC GBR, Rn */
356 case 2: /* STC VBR, Rn */
360 case 3: /* STC SSR, Rn */
364 case 4: /* STC SPC, Rn */
368 case 8: case 9: case 10: case 11: case 12: case 13:
369 case 14: case 15:/* STC Rm_bank, Rn */
371 RN(ir) = RN_BANK(ir);
377 switch( (ir&0x00F0)>>4 ) {
378 case 0: /* BSRF Rn */
379 CHECKDEST( pc + 4 + RN(ir) );
381 sh4r.in_delay_slot = 1;
382 sh4r.pr = sh4r.pc + 4;
383 sh4r.pc = sh4r.new_pc;
384 sh4r.new_pc = pc + 4 + RN(ir);
386 case 2: /* BRAF Rn */
387 CHECKDEST( pc + 4 + RN(ir) );
389 sh4r.in_delay_slot = 1;
390 sh4r.pc = sh4r.new_pc;
391 sh4r.new_pc = pc + 4 + RN(ir);
393 case 8: /* PREF [Rn] */
395 if( (tmp & 0xFC000000) == 0xE0000000 ) {
396 /* Store queue operation */
397 int queue = (tmp&0x20)>>2;
398 int32_t *src = &sh4r.store_queue[queue];
399 uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
400 uint32_t target = tmp&0x03FFFFE0 | hi;
401 mem_copy_to_sh4( target, src, 32 );
402 //if( (target &0xFF000000) != 0x04000000 )
403 // WARN( "Executed SQ%c => %08X",
404 // (queue == 0 ? '0' : '1'), target );
407 case 9: /* OCBI [Rn] */
408 case 10:/* OCBP [Rn] */
409 case 11:/* OCBWB [Rn] */
412 case 12:/* MOVCA.L R0, [Rn] */
417 case 4: /* MOV.B Rm, [R0 + Rn] */
418 MEM_WRITE_BYTE( R0 + RN(ir), RM(ir) );
420 case 5: /* MOV.W Rm, [R0 + Rn] */
421 MEM_WRITE_WORD( R0 + RN(ir), RM(ir) );
423 case 6: /* MOV.L Rm, [R0 + Rn] */
424 MEM_WRITE_LONG( R0 + RN(ir), RM(ir) );
426 case 7: /* MUL.L Rm, Rn */
427 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
431 switch( (ir&0x0FF0)>>4 ) {
453 if( (ir&0x00F0) == 0x20 ) /* MOVT Rn */
455 else if( ir == 0x0019 ) /* DIV0U */
456 sh4r.m = sh4r.q = sh4r.t = 0;
457 else if( ir == 0x0009 )
462 switch( (ir&0x00F0) >> 4 ) {
463 case 0: /* STS MACH, Rn */
464 RN(ir) = sh4r.mac >> 32;
466 case 1: /* STS MACL, Rn */
467 RN(ir) = (uint32_t)sh4r.mac;
469 case 2: /* STS PR, Rn */
472 case 3: /* STC SGR, Rn */
476 case 5:/* STS FPUL, Rn */
479 case 6: /* STS FPSCR, Rn */
482 case 15:/* STC DBR, Rn */
490 switch( (ir&0x0FF0)>>4 ) {
492 CHECKDEST( sh4r.pr );
494 sh4r.in_delay_slot = 1;
495 sh4r.pc = sh4r.new_pc;
496 sh4r.new_pc = sh4r.pr;
499 if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
500 sh4r.sh4_state = SH4_STATE_STANDBY;
502 sh4r.sh4_state = SH4_STATE_SLEEP;
504 return FALSE; /* Halt CPU */
507 CHECKDEST( sh4r.spc );
509 sh4r.in_delay_slot = 1;
510 sh4r.pc = sh4r.new_pc;
511 sh4r.new_pc = sh4r.spc;
512 sh4_load_sr( sh4r.ssr );
517 case 12:/* MOV.B [R0+R%d], R%d */
518 RN(ir) = MEM_READ_BYTE( R0 + RM(ir) );
520 case 13:/* MOV.W [R0+R%d], R%d */
521 RN(ir) = MEM_READ_WORD( R0 + RM(ir) );
523 case 14:/* MOV.L [R0+R%d], R%d */
524 RN(ir) = MEM_READ_LONG( R0 + RM(ir) );
526 case 15:/* MAC.L [Rm++], [Rn++] */
527 tmpl = ( SIGNEXT32(MEM_READ_LONG(RM(ir))) *
528 SIGNEXT32(MEM_READ_LONG(RN(ir))) );
530 /* 48-bit Saturation. Yuch */
531 tmpl += SIGNEXT48(sh4r.mac);
532 if( tmpl < 0xFFFF800000000000LL )
533 tmpl = 0xFFFF800000000000LL;
534 else if( tmpl > 0x00007FFFFFFFFFFFLL )
535 tmpl = 0x00007FFFFFFFFFFFLL;
536 sh4r.mac = (sh4r.mac&0xFFFF000000000000LL) |
537 (tmpl&0x0000FFFFFFFFFFFFLL);
538 } else sh4r.mac = tmpl;
547 case 1: /* 0001nnnnmmmmdddd */
548 /* MOV.L Rm, [Rn + disp4*4] */
549 MEM_WRITE_LONG( RN(ir) + (DISP4(ir)<<2), RM(ir) );
551 case 2: /* 0010nnnnmmmmxxxx */
552 switch( ir&0x000F ) {
553 case 0: /* MOV.B Rm, [Rn] */
554 MEM_WRITE_BYTE( RN(ir), RM(ir) );
556 case 1: /* MOV.W Rm, [Rn] */
557 MEM_WRITE_WORD( RN(ir), RM(ir) );
559 case 2: /* MOV.L Rm, [Rn] */
560 MEM_WRITE_LONG( RN(ir), RM(ir) );
564 case 4: /* MOV.B Rm, [--Rn] */
566 MEM_WRITE_BYTE( RN(ir), RM(ir) );
568 case 5: /* MOV.W Rm, [--Rn] */
570 MEM_WRITE_WORD( RN(ir), RM(ir) );
572 case 6: /* MOV.L Rm, [--Rn] */
574 MEM_WRITE_LONG( RN(ir), RM(ir) );
576 case 7: /* DIV0S Rm, Rn */
579 sh4r.t = sh4r.q ^ sh4r.m;
581 case 8: /* TST Rm, Rn */
582 sh4r.t = (RN(ir)&RM(ir) ? 0 : 1);
584 case 9: /* AND Rm, Rn */
587 case 10:/* XOR Rm, Rn */
590 case 11:/* OR Rm, Rn */
593 case 12:/* CMP/STR Rm, Rn */
594 /* set T = 1 if any byte in RM & RN is the same */
595 tmp = RM(ir) ^ RN(ir);
596 sh4r.t = ((tmp&0x000000FF)==0 || (tmp&0x0000FF00)==0 ||
597 (tmp&0x00FF0000)==0 || (tmp&0xFF000000)==0)?1:0;
599 case 13:/* XTRCT Rm, Rn */
600 RN(ir) = (RN(ir)>>16) | (RM(ir)<<16);
602 case 14:/* MULU.W Rm, Rn */
603 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
604 (uint32_t)((RM(ir)&0xFFFF) * (RN(ir)&0xFFFF));
606 case 15:/* MULS.W Rm, Rn */
607 sh4r.mac = (sh4r.mac&0xFFFFFFFF00000000LL) |
608 (uint32_t)(SIGNEXT32(RM(ir)&0xFFFF) * SIGNEXT32(RN(ir)&0xFFFF));
612 case 3: /* 0011nnnnmmmmxxxx */
613 switch( ir&0x000F ) {
614 case 0: /* CMP/EQ Rm, Rn */
615 sh4r.t = ( RM(ir) == RN(ir) ? 1 : 0 );
617 case 2: /* CMP/HS Rm, Rn */
618 sh4r.t = ( RN(ir) >= RM(ir) ? 1 : 0 );
620 case 3: /* CMP/GE Rm, Rn */
621 sh4r.t = ( ((int32_t)RN(ir)) >= ((int32_t)RM(ir)) ? 1 : 0 );
623 case 4: { /* DIV1 Rm, Rn */
624 /* This is just from the sh4p manual with some
625 * simplifications (someone want to check it's correct? :)
626 * Why they couldn't just provide a real DIV instruction...
627 * Please oh please let the translator batch these things
628 * up into a single DIV... */
629 uint32_t tmp0, tmp1, tmp2, dir;
631 dir = sh4r.q ^ sh4r.m;
632 sh4r.q = (RN(ir) >> 31);
634 RN(ir) = (RN(ir) << 1) | sh4r.t;
638 tmp1 = (RN(ir)<tmp0 ? 1 : 0 );
641 tmp1 = (RN(ir)>tmp0 ? 1 : 0 );
643 sh4r.q ^= sh4r.m ^ tmp1;
644 sh4r.t = ( sh4r.q == sh4r.m ? 1 : 0 );
646 case 5: /* DMULU.L Rm, Rn */
647 sh4r.mac = ((uint64_t)RM(ir)) * ((uint64_t)RN(ir));
649 case 6: /* CMP/HI Rm, Rn */
650 sh4r.t = ( RN(ir) > RM(ir) ? 1 : 0 );
652 case 7: /* CMP/GT Rm, Rn */
653 sh4r.t = ( ((int32_t)RN(ir)) > ((int32_t)RM(ir)) ? 1 : 0 );
655 case 8: /* SUB Rm, Rn */
658 case 10:/* SUBC Rm, Rn */
660 RN(ir) = RN(ir) - RM(ir) - sh4r.t;
661 sh4r.t = (RN(ir) > tmp || (RN(ir) == tmp && sh4r.t == 1));
663 case 11:/* SUBV Rm, Rn */
666 case 12:/* ADD Rm, Rn */
669 case 13:/* DMULS.L Rm, Rn */
670 sh4r.mac = SIGNEXT32(RM(ir)) * SIGNEXT32(RN(ir));
672 case 14:/* ADDC Rm, Rn */
674 RN(ir) += RM(ir) + sh4r.t;
675 sh4r.t = ( RN(ir) < tmp || (RN(ir) == tmp && sh4r.t != 0) ? 1 : 0 );
677 case 15:/* ADDV Rm, Rn */
683 case 4: /* 0100nnnnxxxxxxxx */
684 switch( ir&0x00FF ) {
685 case 0x00: /* SHLL Rn */
686 sh4r.t = RN(ir) >> 31;
689 case 0x01: /* SHLR Rn */
690 sh4r.t = RN(ir) & 0x00000001;
693 case 0x02: /* STS.L MACH, [--Rn] */
695 MEM_WRITE_LONG( RN(ir), (sh4r.mac>>32) );
697 case 0x03: /* STC.L SR, [--Rn] */
700 MEM_WRITE_LONG( RN(ir), sh4_read_sr() );
702 case 0x04: /* ROTL Rn */
703 sh4r.t = RN(ir) >> 31;
707 case 0x05: /* ROTR Rn */
708 sh4r.t = RN(ir) & 0x00000001;
710 RN(ir) |= (sh4r.t << 31);
712 case 0x06: /* LDS.L [Rn++], MACH */
713 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
714 (((uint64_t)MEM_READ_LONG(RN(ir)))<<32);
717 case 0x07: /* LDC.L [Rn++], SR */
719 sh4_load_sr( MEM_READ_LONG(RN(ir)) );
722 case 0x08: /* SHLL2 Rn */
725 case 0x09: /* SHLR2 Rn */
728 case 0x0A: /* LDS Rn, MACH */
729 sh4r.mac = (sh4r.mac & 0x00000000FFFFFFFF) |
730 (((uint64_t)RN(ir))<<32);
732 case 0x0B: /* JSR [Rn] */
735 sh4r.in_delay_slot = 1;
736 sh4r.pc = sh4r.new_pc;
737 sh4r.new_pc = RN(ir);
740 case 0x0E: /* LDC Rn, SR */
742 sh4_load_sr( RN(ir) );
744 case 0x10: /* DT Rn */
746 sh4r.t = ( RN(ir) == 0 ? 1 : 0 );
748 case 0x11: /* CMP/PZ Rn */
749 sh4r.t = ( ((int32_t)RN(ir)) >= 0 ? 1 : 0 );
751 case 0x12: /* STS.L MACL, [--Rn] */
753 MEM_WRITE_LONG( RN(ir), (uint32_t)sh4r.mac );
755 case 0x13: /* STC.L GBR, [--Rn] */
757 MEM_WRITE_LONG( RN(ir), sh4r.gbr );
759 case 0x15: /* CMP/PL Rn */
760 sh4r.t = ( ((int32_t)RN(ir)) > 0 ? 1 : 0 );
762 case 0x16: /* LDS.L [Rn++], MACL */
763 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
764 (uint64_t)((uint32_t)MEM_READ_LONG(RN(ir)));
767 case 0x17: /* LDC.L [Rn++], GBR */
768 sh4r.gbr = MEM_READ_LONG(RN(ir));
771 case 0x18: /* SHLL8 Rn */
774 case 0x19: /* SHLR8 Rn */
777 case 0x1A: /* LDS Rn, MACL */
778 sh4r.mac = (sh4r.mac & 0xFFFFFFFF00000000LL) |
779 (uint64_t)((uint32_t)(RN(ir)));
781 case 0x1B: /* TAS.B [Rn] */
782 tmp = MEM_READ_BYTE( RN(ir) );
783 sh4r.t = ( tmp == 0 ? 1 : 0 );
784 MEM_WRITE_BYTE( RN(ir), tmp | 0x80 );
786 case 0x1E: /* LDC Rn, GBR */
789 case 0x20: /* SHAL Rn */
790 sh4r.t = RN(ir) >> 31;
793 case 0x21: /* SHAR Rn */
794 sh4r.t = RN(ir) & 0x00000001;
795 RN(ir) = ((int32_t)RN(ir)) >> 1;
797 case 0x22: /* STS.L PR, [--Rn] */
799 MEM_WRITE_LONG( RN(ir), sh4r.pr );
801 case 0x23: /* STC.L VBR, [--Rn] */
804 MEM_WRITE_LONG( RN(ir), sh4r.vbr );
806 case 0x24: /* ROTCL Rn */
812 case 0x25: /* ROTCR Rn */
813 tmp = RN(ir) & 0x00000001;
815 RN(ir) |= (sh4r.t << 31 );
818 case 0x26: /* LDS.L [Rn++], PR */
819 sh4r.pr = MEM_READ_LONG( RN(ir) );
822 case 0x27: /* LDC.L [Rn++], VBR */
824 sh4r.vbr = MEM_READ_LONG(RN(ir));
827 case 0x28: /* SHLL16 Rn */
830 case 0x29: /* SHLR16 Rn */
833 case 0x2A: /* LDS Rn, PR */
836 case 0x2B: /* JMP [Rn] */
839 sh4r.in_delay_slot = 1;
840 sh4r.pc = sh4r.new_pc;
841 sh4r.new_pc = RN(ir);
843 case 0x2E: /* LDC Rn, VBR */
847 case 0x32: /* STC.L SGR, [--Rn] */
850 MEM_WRITE_LONG( RN(ir), sh4r.sgr );
852 case 0x33: /* STC.L SSR, [--Rn] */
855 MEM_WRITE_LONG( RN(ir), sh4r.ssr );
857 case 0x37: /* LDC.L [Rn++], SSR */
859 sh4r.ssr = MEM_READ_LONG(RN(ir));
862 case 0x3E: /* LDC Rn, SSR */
866 case 0x43: /* STC.L SPC, [--Rn] */
869 MEM_WRITE_LONG( RN(ir), sh4r.spc );
871 case 0x47: /* LDC.L [Rn++], SPC */
873 sh4r.spc = MEM_READ_LONG(RN(ir));
876 case 0x4E: /* LDC Rn, SPC */
880 case 0x52: /* STS.L FPUL, [--Rn] */
882 MEM_WRITE_LONG( RN(ir), sh4r.fpul );
884 case 0x56: /* LDS.L [Rn++], FPUL */
885 sh4r.fpul = MEM_READ_LONG(RN(ir));
888 case 0x5A: /* LDS Rn, FPUL */
891 case 0x62: /* STS.L FPSCR, [--Rn] */
893 MEM_WRITE_LONG( RN(ir), sh4r.fpscr );
895 case 0x66: /* LDS.L [Rn++], FPSCR */
896 sh4r.fpscr = MEM_READ_LONG(RN(ir));
899 case 0x6A: /* LDS Rn, FPSCR */
902 case 0xF2: /* STC.L DBR, [--Rn] */
905 MEM_WRITE_LONG( RN(ir), sh4r.dbr );
907 case 0xF6: /* LDC.L [Rn++], DBR */
909 sh4r.dbr = MEM_READ_LONG(RN(ir));
912 case 0xFA: /* LDC Rn, DBR */
916 case 0x83: case 0x93: case 0xA3: case 0xB3: case 0xC3:
917 case 0xD3: case 0xE3: case 0xF3: /* STC.L Rn_BANK, [--Rn] */
920 MEM_WRITE_LONG( RN(ir), RN_BANK(ir) );
922 case 0x87: case 0x97: case 0xA7: case 0xB7: case 0xC7:
923 case 0xD7: case 0xE7: case 0xF7: /* LDC.L [Rn++], Rn_BANK */
925 RN_BANK(ir) = MEM_READ_LONG( RN(ir) );
928 case 0x8E: case 0x9E: case 0xAE: case 0xBE: case 0xCE:
929 case 0xDE: case 0xEE: case 0xFE: /* LDC Rm, Rn_BANK */
931 RN_BANK(ir) = RM(ir);
934 if( (ir&0x000F) == 0x0F ) {
935 /* MAC.W [Rm++], [Rn++] */
936 tmp = SIGNEXT16(MEM_READ_WORD(RM(ir))) *
937 SIGNEXT16(MEM_READ_WORD(RN(ir)));
941 } else sh4r.mac += SIGNEXT32(tmp);
944 } else if( (ir&0x000F) == 0x0C ) {
947 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
948 else if( (tmp & 0x1F) == 0 )
949 RN(ir) = ((int32_t)RN(ir)) >> 31;
951 RN(ir) = ((int32_t)RN(ir)) >> (((~RM(ir)) & 0x1F)+1);
952 } else if( (ir&0x000F) == 0x0D ) {
955 if( (tmp & 0x80000000) == 0 ) RN(ir) <<= (tmp&0x1f);
956 else if( (tmp & 0x1F) == 0 ) RN(ir) = 0;
957 else RN(ir) >>= (((~tmp) & 0x1F)+1);
961 case 5: /* 0101nnnnmmmmdddd */
962 /* MOV.L [Rm + disp4*4], Rn */
963 RN(ir) = MEM_READ_LONG( RM(ir) + (DISP4(ir)<<2) );
965 case 6: /* 0110xxxxxxxxxxxx */
966 switch( ir&0x000f ) {
967 case 0: /* MOV.B [Rm], Rn */
968 RN(ir) = MEM_READ_BYTE( RM(ir) );
970 case 1: /* MOV.W [Rm], Rn */
971 RN(ir) = MEM_READ_WORD( RM(ir) );
973 case 2: /* MOV.L [Rm], Rn */
974 RN(ir) = MEM_READ_LONG( RM(ir) );
976 case 3: /* MOV Rm, Rn */
979 case 4: /* MOV.B [Rm++], Rn */
980 RN(ir) = MEM_READ_BYTE( RM(ir) );
983 case 5: /* MOV.W [Rm++], Rn */
984 RN(ir) = MEM_READ_WORD( RM(ir) );
987 case 6: /* MOV.L [Rm++], Rn */
988 RN(ir) = MEM_READ_LONG( RM(ir) );
991 case 7: /* NOT Rm, Rn */
994 case 8: /* SWAP.B Rm, Rn */
995 RN(ir) = (RM(ir)&0xFFFF0000) | ((RM(ir)&0x0000FF00)>>8) |
996 ((RM(ir)&0x000000FF)<<8);
998 case 9: /* SWAP.W Rm, Rn */
999 RN(ir) = (RM(ir)>>16) | (RM(ir)<<16);
1001 case 10:/* NEGC Rm, Rn */
1003 RN(ir) = tmp - sh4r.t;
1004 sh4r.t = ( 0<tmp || tmp<RN(ir) ? 1 : 0 );
1006 case 11:/* NEG Rm, Rn */
1007 RN(ir) = 0 - RM(ir);
1009 case 12:/* EXTU.B Rm, Rn */
1010 RN(ir) = RM(ir)&0x000000FF;
1012 case 13:/* EXTU.W Rm, Rn */
1013 RN(ir) = RM(ir)&0x0000FFFF;
1015 case 14:/* EXTS.B Rm, Rn */
1016 RN(ir) = SIGNEXT8( RM(ir)&0x000000FF );
1018 case 15:/* EXTS.W Rm, Rn */
1019 RN(ir) = SIGNEXT16( RM(ir)&0x0000FFFF );
1023 case 7: /* 0111nnnniiiiiiii */
1027 case 8: /* 1000xxxxxxxxxxxx */
1028 switch( (ir&0x0F00) >> 8 ) {
1029 case 0: /* MOV.B R0, [Rm + disp4] */
1030 MEM_WRITE_BYTE( RM(ir) + DISP4(ir), R0 );
1032 case 1: /* MOV.W R0, [Rm + disp4*2] */
1033 MEM_WRITE_WORD( RM(ir) + (DISP4(ir)<<1), R0 );
1035 case 4: /* MOV.B [Rm + disp4], R0 */
1036 R0 = MEM_READ_BYTE( RM(ir) + DISP4(ir) );
1038 case 5: /* MOV.W [Rm + disp4*2], R0 */
1039 R0 = MEM_READ_WORD( RM(ir) + (DISP4(ir)<<1) );
1041 case 8: /* CMP/EQ imm, R0 */
1042 sh4r.t = ( R0 == IMM8(ir) ? 1 : 0 );
1044 case 9: /* BT disp8 */
1047 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1048 sh4r.pc += (PCDISP8(ir)<<1) + 4;
1049 sh4r.new_pc = sh4r.pc + 2;
1053 case 11:/* BF disp8 */
1056 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1057 sh4r.pc += (PCDISP8(ir)<<1) + 4;
1058 sh4r.new_pc = sh4r.pc + 2;
1062 case 13:/* BT/S disp8 */
1065 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1066 sh4r.in_delay_slot = 1;
1067 sh4r.pc = sh4r.new_pc;
1068 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1069 sh4r.in_delay_slot = 1;
1073 case 15:/* BF/S disp8 */
1076 CHECKDEST( sh4r.pc + (PCDISP8(ir)<<1) + 4 )
1077 sh4r.in_delay_slot = 1;
1078 sh4r.pc = sh4r.new_pc;
1079 sh4r.new_pc = pc + (PCDISP8(ir)<<1) + 4;
1086 case 9: /* 1001xxxxxxxxxxxx */
1087 /* MOV.W [disp8*2 + pc + 4], Rn */
1088 RN(ir) = MEM_READ_WORD( pc + 4 + (DISP8(ir)<<1) );
1090 case 10:/* 1010dddddddddddd */
1092 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1094 sh4r.in_delay_slot = 1;
1095 sh4r.pc = sh4r.new_pc;
1096 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1098 case 11:/* 1011dddddddddddd */
1100 CHECKDEST( sh4r.pc + (DISP12(ir)<<1) + 4 )
1102 sh4r.in_delay_slot = 1;
1104 sh4r.pc = sh4r.new_pc;
1105 sh4r.new_pc = pc + 4 + (DISP12(ir)<<1);
1107 case 12:/* 1100xxxxdddddddd */
1108 switch( (ir&0x0F00)>>8 ) {
1109 case 0: /* MOV.B R0, [GBR + disp8] */
1110 MEM_WRITE_BYTE( sh4r.gbr + DISP8(ir), R0 );
1112 case 1: /* MOV.W R0, [GBR + disp8*2] */
1113 MEM_WRITE_WORD( sh4r.gbr + (DISP8(ir)<<1), R0 );
1115 case 2: /*MOV.L R0, [GBR + disp8*4] */
1116 MEM_WRITE_LONG( sh4r.gbr + (DISP8(ir)<<2), R0 );
1118 case 3: /* TRAPA imm8 */
1120 sh4r.in_delay_slot = 1;
1121 MMIO_WRITE( MMU, TRA, UIMM8(ir) );
1122 sh4r.pc = sh4r.new_pc; /* RAISE ends the instruction */
1124 RAISE( EXC_TRAP, EXV_TRAP );
1126 case 4: /* MOV.B [GBR + disp8], R0 */
1127 R0 = MEM_READ_BYTE( sh4r.gbr + DISP8(ir) );
1129 case 5: /* MOV.W [GBR + disp8*2], R0 */
1130 R0 = MEM_READ_WORD( sh4r.gbr + (DISP8(ir)<<1) );
1132 case 6: /* MOV.L [GBR + disp8*4], R0 */
1133 R0 = MEM_READ_LONG( sh4r.gbr + (DISP8(ir)<<2) );
1135 case 7: /* MOVA disp8 + pc&~3 + 4, R0 */
1136 R0 = (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4;
1138 case 8: /* TST imm8, R0 */
1139 sh4r.t = (R0 & UIMM8(ir) ? 0 : 1);
1141 case 9: /* AND imm8, R0 */
1144 case 10:/* XOR imm8, R0 */
1147 case 11:/* OR imm8, R0 */
1150 case 12:/* TST.B imm8, [R0+GBR] */
1151 sh4r.t = ( MEM_READ_BYTE(R0 + sh4r.gbr) & UIMM8(ir) ? 0 : 1 );
1153 case 13:/* AND.B imm8, [R0+GBR] */
1154 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1155 UIMM8(ir) & MEM_READ_BYTE(R0 + sh4r.gbr) );
1157 case 14:/* XOR.B imm8, [R0+GBR] */
1158 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1159 UIMM8(ir) ^ MEM_READ_BYTE(R0 + sh4r.gbr) );
1161 case 15:/* OR.B imm8, [R0+GBR] */
1162 MEM_WRITE_BYTE( R0 + sh4r.gbr,
1163 UIMM8(ir) | MEM_READ_BYTE(R0 + sh4r.gbr) );
1167 case 13:/* 1101nnnndddddddd */
1168 /* MOV.L [disp8*4 + pc&~3 + 4], Rn */
1169 RN(ir) = MEM_READ_LONG( (pc&0xFFFFFFFC) + (DISP8(ir)<<2) + 4 );
1171 case 14:/* 1110nnnniiiiiiii */
1175 case 15:/* 1111xxxxxxxxxxxx */
1177 if( IS_FPU_DOUBLEPREC() ) {
1178 switch( ir&0x000F ) {
1179 case 0: /* FADD FRm, FRn */
1182 case 1: /* FSUB FRm, FRn */
1185 case 2: /* FMUL FRm, FRn */
1186 DRN(ir) = DRN(ir) * DRM(ir);
1188 case 3: /* FDIV FRm, FRn */
1189 DRN(ir) = DRN(ir) / DRM(ir);
1191 case 4: /* FCMP/EQ FRm, FRn */
1192 sh4r.t = ( DRN(ir) == DRM(ir) ? 1 : 0 );
1194 case 5: /* FCMP/GT FRm, FRn */
1195 sh4r.t = ( DRN(ir) > DRM(ir) ? 1 : 0 );
1197 case 6: /* FMOV.S [Rm+R0], FRn */
1198 MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
1200 case 7: /* FMOV.S FRm, [Rn+R0] */
1201 MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
1203 case 8: /* FMOV.S [Rm], FRn */
1204 MEM_FP_READ( RM(ir), FRNn(ir) );
1206 case 9: /* FMOV.S [Rm++], FRn */
1207 MEM_FP_READ( RM(ir), FRNn(ir) );
1210 case 10:/* FMOV.S FRm, [Rn] */
1211 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1213 case 11:/* FMOV.S FRm, [--Rn] */
1215 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1217 case 12:/* FMOV FRm, FRn */
1218 if( IS_FPU_DOUBLESIZE() )
1224 switch( (ir&0x00F0) >> 4 ) {
1225 case 0: /* FSTS FPUL, FRn */
1228 case 1: /* FLDS FRn,FPUL */
1231 case 2: /* FLOAT FPUL, FRn */
1232 DRN(ir) = (float)FPULi;
1234 case 3: /* FTRC FRn, FPUL */
1235 FPULi = (uint32_t)DRN(ir);
1236 /* FIXME: is this sufficient? */
1238 case 4: /* FNEG FRn */
1241 case 5: /* FABS FRn */
1242 DRN(ir) = fabs(DRN(ir));
1244 case 6: /* FSQRT FRn */
1245 DRN(ir) = sqrt(DRN(ir));
1247 case 7: /* FSRRA FRn */
1248 DRN(ir) = 1.0/sqrt(DRN(ir));
1250 case 8: /* FLDI0 FRn */
1253 case 9: /* FLDI1 FRn */
1256 case 10: /* FCNVSD FPUL, DRn */
1257 DRN(ir) = (double)FPULf;
1259 case 11: /* FCNVDS DRn, FPUL */
1260 FPULf = (float)DRN(ir);
1262 case 14:/* FIPR FVm, FVn */
1266 if( (ir&0x0300) == 0x0100 ) { /* FTRV XMTRX,FVn */
1269 else if( (ir&0x0100) == 0 ) { /* FSCA FPUL, DRn */
1270 float angle = (((float)(short)(FPULi>>16)) +
1271 ((float)(FPULi&16)/65536.0)) *
1274 DR(reg) = sinf(angle);
1275 DR(reg+1) = cosf(angle);
1278 else if( ir == 0xFBFD ) {
1280 sh4r.fpscr ^= FPSCR_FR;
1283 else if( ir == 0xF3FD ) {
1285 sh4r.fpscr ^= FPSCR_SZ;
1291 case 14:/* FMAC FR0, FRm, FRn */
1292 DRN(ir) += DRM(ir)*DR0;
1297 switch( ir&0x000F ) {
1298 case 0: /* FADD FRm, FRn */
1301 case 1: /* FSUB FRm, FRn */
1304 case 2: /* FMUL FRm, FRn */
1305 FRN(ir) = FRN(ir) * FRM(ir);
1307 case 3: /* FDIV FRm, FRn */
1308 FRN(ir) = FRN(ir) / FRM(ir);
1310 case 4: /* FCMP/EQ FRm, FRn */
1311 sh4r.t = ( FRN(ir) == FRM(ir) ? 1 : 0 );
1313 case 5: /* FCMP/GT FRm, FRn */
1314 sh4r.t = ( FRN(ir) > FRM(ir) ? 1 : 0 );
1316 case 6: /* FMOV.S [Rm+R0], FRn */
1317 MEM_FP_READ( RM(ir) + R0, FRNn(ir) );
1319 case 7: /* FMOV.S FRm, [Rn+R0] */
1320 MEM_FP_WRITE( RN(ir) + R0, FRMn(ir) );
1322 case 8: /* FMOV.S [Rm], FRn */
1323 MEM_FP_READ( RM(ir), FRNn(ir) );
1325 case 9: /* FMOV.S [Rm++], FRn */
1326 MEM_FP_READ( RM(ir), FRNn(ir) );
1329 case 10:/* FMOV.S FRm, [Rn] */
1330 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1332 case 11:/* FMOV.S FRm, [--Rn] */
1334 MEM_FP_WRITE( RN(ir), FRMn(ir) );
1336 case 12:/* FMOV FRm, FRn */
1337 if( IS_FPU_DOUBLESIZE() )
1343 switch( (ir&0x00F0) >> 4 ) {
1344 case 0: /* FSTS FPUL, FRn */
1347 case 1: /* FLDS FRn,FPUL */
1350 case 2: /* FLOAT FPUL, FRn */
1351 FRN(ir) = (float)FPULi;
1353 case 3: /* FTRC FRn, FPUL */
1354 FPULi = (uint32_t)FRN(ir);
1355 /* FIXME: is this sufficient? */
1357 case 4: /* FNEG FRn */
1360 case 5: /* FABS FRn */
1361 FRN(ir) = fabsf(FRN(ir));
1363 case 6: /* FSQRT FRn */
1364 FRN(ir) = sqrtf(FRN(ir));
1366 case 7: /* FSRRA FRn */
1367 FRN(ir) = 1.0/sqrtf(FRN(ir));
1369 case 8: /* FLDI0 FRn */
1372 case 9: /* FLDI1 FRn */
1375 case 10: /* FCNVSD FPUL, DRn */
1378 case 11: /* FCNVDS DRn, FPUL */
1381 case 14:/* FIPR FVm, FVn */
1382 /* FIXME: This is not going to be entirely accurate
1383 * as the SH4 instruction is less precise. Also
1384 * need to check for 0s and infinities.
1389 FR(tmp2+3) = FR(tmp)*FR(tmp2) +
1390 FR(tmp+1)*FR(tmp2+1) +
1391 FR(tmp+2)*FR(tmp2+2) +
1392 FR(tmp+3)*FR(tmp2+3);
1396 if( (ir&0x0300) == 0x0100 ) { /* FTRV XMTRX,FVn */
1398 float fv[4] = { FR(tmp), FR(tmp+1), FR(tmp+2), FR(tmp+3) };
1399 FR(tmp) = XF(0) * fv[0] + XF(4)*fv[1] +
1400 XF(8)*fv[2] + XF(12)*fv[3];
1401 FR(tmp+1) = XF(1) * fv[0] + XF(5)*fv[1] +
1402 XF(9)*fv[2] + XF(13)*fv[3];
1403 FR(tmp+2) = XF(2) * fv[0] + XF(6)*fv[1] +
1404 XF(10)*fv[2] + XF(14)*fv[3];
1405 FR(tmp+3) = XF(3) * fv[0] + XF(7)*fv[1] +
1406 XF(11)*fv[2] + XF(15)*fv[3];
1409 else if( (ir&0x0100) == 0 ) { /* FSCA FPUL, DRn */
1410 float angle = (((float)(short)(FPULi>>16)) +
1411 ((float)(FPULi&16)/65536.0)) *
1414 FR(reg) = sinf(angle);
1415 FR(reg+1) = cosf(angle);
1418 else if( ir == 0xFBFD ) {
1420 sh4r.fpscr ^= FPSCR_FR;
1423 else if( ir == 0xF3FD ) {
1425 sh4r.fpscr ^= FPSCR_SZ;
1431 case 14:/* FMAC FR0, FRm, FRn */
1432 FRN(ir) += FRM(ir)*FR0;
1439 sh4r.pc = sh4r.new_pc;
1441 sh4r.in_delay_slot = 0;
.