4 * SH4 parent module for all CPU modes and 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
24 #include "dreamcast.h"
25 #include "sh4/sh4core.h"
26 #include "sh4/sh4mmio.h"
28 #include "sh4/xltcache.h"
29 #include "sh4/sh4stat.h"
30 #include "sh4/sh4trans.h"
35 void sh4_init( void );
36 void sh4_xlat_init( void );
37 void sh4_reset( void );
38 void sh4_start( void );
39 void sh4_stop( void );
40 void sh4_save_state( FILE *f );
41 int sh4_load_state( FILE *f );
43 uint32_t sh4_run_slice( uint32_t );
44 uint32_t sh4_xlat_run_slice( uint32_t );
46 struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset,
47 sh4_start, sh4_run_slice, sh4_stop,
48 sh4_save_state, sh4_load_state };
50 struct sh4_registers sh4r;
51 struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
52 int sh4_breakpoint_count = 0;
53 sh4ptr_t sh4_main_ram;
54 gboolean sh4_starting = FALSE;
55 static gboolean sh4_use_translator = FALSE;
56 struct sh4_icache_struct sh4_icache = { NULL, -1, -1, 0 };
58 void sh4_set_use_xlat( gboolean use )
60 // No-op if the translator was not built
65 sh4_module.run_time_slice = sh4_xlat_run_slice;
67 sh4_module.run_time_slice = sh4_run_slice;
69 sh4_use_translator = use;
73 gboolean sh4_is_using_xlat()
75 return sh4_use_translator;
80 register_io_regions( mmio_list_sh4mmio );
81 sh4_main_ram = mem_get_region_by_name(MEM_REGION_MAIN);
94 if( sh4_use_translator ) {
98 /* zero everything out, for the sake of having a consistent state. */
99 memset( &sh4r, 0, sizeof(sh4r) );
101 /* Resume running if we were halted */
102 sh4r.sh4_state = SH4_STATE_RUNNING;
104 sh4r.pc = 0xA0000000;
105 sh4r.new_pc= 0xA0000002;
106 sh4r.vbr = 0x00000000;
107 sh4r.fpscr = 0x00040001;
108 sh4r.sr = 0x700000F0;
109 sh4r.fr_bank = &sh4r.fr[0][0];
111 /* Mem reset will do this, but if we want to reset _just_ the SH4... */
112 MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
114 /* Peripheral modules */
125 if( sh4_use_translator ) {
126 /* If we were running with the translator, update new_pc and in_delay_slot */
127 sh4r.new_pc = sh4r.pc+2;
128 sh4r.in_delay_slot = FALSE;
133 void sh4_save_state( FILE *f )
135 if( sh4_use_translator ) {
136 /* If we were running with the translator, update new_pc and in_delay_slot */
137 sh4r.new_pc = sh4r.pc+2;
138 sh4r.in_delay_slot = FALSE;
141 fwrite( &sh4r, sizeof(sh4r), 1, f );
143 INTC_save_state( f );
145 SCIF_save_state( f );
148 int sh4_load_state( FILE * f )
150 if( sh4_use_translator ) {
153 fread( &sh4r, sizeof(sh4r), 1, f );
154 sh4r.fr_bank = &sh4r.fr[(sh4r.fpscr&FPSCR_FR)>>21][0]; // Fixup internal FR pointer
156 INTC_load_state( f );
158 return SCIF_load_state( f );
162 void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type )
164 sh4_breakpoints[sh4_breakpoint_count].address = pc;
165 sh4_breakpoints[sh4_breakpoint_count].type = type;
166 if( sh4_use_translator ) {
167 xlat_invalidate_word( pc );
169 sh4_breakpoint_count++;
172 gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type )
176 for( i=0; i<sh4_breakpoint_count; i++ ) {
177 if( sh4_breakpoints[i].address == pc &&
178 sh4_breakpoints[i].type == type ) {
179 while( ++i < sh4_breakpoint_count ) {
180 sh4_breakpoints[i-1].address = sh4_breakpoints[i].address;
181 sh4_breakpoints[i-1].type = sh4_breakpoints[i].type;
183 if( sh4_use_translator ) {
184 xlat_invalidate_word( pc );
186 sh4_breakpoint_count--;
193 int sh4_get_breakpoint( uint32_t pc )
196 for( i=0; i<sh4_breakpoint_count; i++ ) {
197 if( sh4_breakpoints[i].address == pc )
198 return sh4_breakpoints[i].type;
203 void sh4_set_pc( int pc )
210 /******************************* Support methods ***************************/
212 static void sh4_switch_banks( )
216 memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
217 memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
218 memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
221 void sh4_write_sr( uint32_t newval )
223 int oldbank = (sh4r.sr&SR_MDRB) == SR_MDRB;
224 int newbank = (newval&SR_MDRB) == SR_MDRB;
225 if( oldbank != newbank )
228 sh4r.t = (newval&SR_T) ? 1 : 0;
229 sh4r.s = (newval&SR_S) ? 1 : 0;
230 sh4r.m = (newval&SR_M) ? 1 : 0;
231 sh4r.q = (newval&SR_Q) ? 1 : 0;
235 uint32_t sh4_read_sr( void )
237 /* synchronize sh4r.sr with the various bitflags */
238 sh4r.sr &= SR_MQSTMASK;
239 if( sh4r.t ) sh4r.sr |= SR_T;
240 if( sh4r.s ) sh4r.sr |= SR_S;
241 if( sh4r.m ) sh4r.sr |= SR_M;
242 if( sh4r.q ) sh4r.sr |= SR_Q;
248 #define RAISE( x, v ) do{ \
249 if( sh4r.vbr == 0 ) { \
250 ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
251 dreamcast_stop(); return FALSE; \
253 sh4r.spc = sh4r.pc; \
254 sh4r.ssr = sh4_read_sr(); \
255 sh4r.sgr = sh4r.r[15]; \
256 MMIO_WRITE(MMU,EXPEVT,x); \
257 sh4r.pc = sh4r.vbr + v; \
258 sh4r.new_pc = sh4r.pc + 2; \
259 sh4_write_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
260 if( sh4r.in_delay_slot ) { \
261 sh4r.in_delay_slot = 0; \
265 return TRUE; } while(0)
268 * Raise a general CPU exception for the specified exception code.
269 * (NOT for TRAPA or TLB exceptions)
271 gboolean sh4_raise_exception( int code )
273 RAISE( code, EXV_EXCEPTION );
277 * Raise a CPU reset exception with the specified exception code.
279 gboolean sh4_raise_reset( int code )
281 // FIXME: reset modules as per "manual reset"
283 MMIO_WRITE(MMU,EXPEVT,code);
285 sh4r.pc = 0xA0000000;
286 sh4r.new_pc = sh4r.pc + 2;
287 sh4_write_sr( (sh4r.sr|SR_MD|SR_BL|SR_RB|SR_IMASK)
291 gboolean sh4_raise_trap( int trap )
293 MMIO_WRITE( MMU, TRA, trap<<2 );
294 RAISE( EXC_TRAP, EXV_EXCEPTION );
297 gboolean sh4_raise_slot_exception( int normal_code, int slot_code ) {
298 if( sh4r.in_delay_slot ) {
299 return sh4_raise_exception(slot_code);
301 return sh4_raise_exception(normal_code);
305 gboolean sh4_raise_tlb_exception( int code )
307 RAISE( code, EXV_TLBMISS );
310 void sh4_accept_interrupt( void )
312 uint32_t code = intc_accept_interrupt();
313 sh4r.ssr = sh4_read_sr();
315 sh4r.sgr = sh4r.r[15];
316 sh4_write_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
317 MMIO_WRITE( MMU, INTEVT, code );
318 sh4r.pc = sh4r.vbr + 0x600;
319 sh4r.new_pc = sh4r.pc + 2;
320 // WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
323 void signsat48( void )
325 if( ((int64_t)sh4r.mac) < (int64_t)0xFFFF800000000000LL )
326 sh4r.mac = 0xFFFF800000000000LL;
327 else if( ((int64_t)sh4r.mac) > (int64_t)0x00007FFFFFFFFFFFLL )
328 sh4r.mac = 0x00007FFFFFFFFFFFLL;
331 void sh4_fsca( uint32_t anglei, float *fr )
333 float angle = (((float)(anglei&0xFFFF))/65536.0) * 2 * M_PI;
339 * Enter sleep mode (eg by executing a SLEEP instruction).
340 * Sets sh4_state appropriately and ensures any stopping peripheral modules
345 if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
346 sh4r.sh4_state = SH4_STATE_STANDBY;
347 /* Bring all running peripheral modules up to date, and then halt them. */
348 TMU_run_slice( sh4r.slice_cycle );
349 SCIF_run_slice( sh4r.slice_cycle );
351 if( MMIO_READ( CPG, STBCR2 ) & 0x80 ) {
352 sh4r.sh4_state = SH4_STATE_DEEP_SLEEP;
353 /* Halt DMAC but other peripherals still running */
356 sh4r.sh4_state = SH4_STATE_SLEEP;
359 if( sh4_xlat_is_running() ) {
360 sh4_translate_exit( XLAT_EXIT_SLEEP );
365 * Wakeup following sleep mode (IRQ or reset). Sets state back to running,
366 * and restarts any peripheral devices that were stopped.
368 void sh4_wakeup(void)
370 switch( sh4r.sh4_state ) {
371 case SH4_STATE_STANDBY:
373 case SH4_STATE_DEEP_SLEEP:
375 case SH4_STATE_SLEEP:
378 sh4r.sh4_state = SH4_STATE_RUNNING;
382 * Run a time slice (or portion of a timeslice) while the SH4 is sleeping.
383 * Returns when either the SH4 wakes up (interrupt received) or the end of
384 * the slice is reached. Updates sh4.slice_cycle with the exit time and
385 * returns the same value.
387 uint32_t sh4_sleep_run_slice( uint32_t nanosecs )
389 int sleep_state = sh4r.sh4_state;
390 assert( sleep_state != SH4_STATE_RUNNING );
392 while( sh4r.event_pending < nanosecs ) {
393 sh4r.slice_cycle = sh4r.event_pending;
394 if( sh4r.event_types & PENDING_EVENT ) {
397 if( sh4r.event_types & PENDING_IRQ ) {
399 return sh4r.slice_cycle;
402 sh4r.slice_cycle = nanosecs;
403 return sh4r.slice_cycle;
408 * Compute the matrix tranform of fv given the matrix xf.
409 * Both fv and xf are word-swapped as per the sh4r.fr banks
411 void sh4_ftrv( float *target, float *xf )
413 float fv[4] = { target[1], target[0], target[3], target[2] };
414 target[1] = xf[1] * fv[0] + xf[5]*fv[1] +
415 xf[9]*fv[2] + xf[13]*fv[3];
416 target[0] = xf[0] * fv[0] + xf[4]*fv[1] +
417 xf[8]*fv[2] + xf[12]*fv[3];
418 target[3] = xf[3] * fv[0] + xf[7]*fv[1] +
419 xf[11]*fv[2] + xf[15]*fv[3];
420 target[2] = xf[2] * fv[0] + xf[6]*fv[1] +
421 xf[10]*fv[2] + xf[14]*fv[3];
424 gboolean sh4_has_page( sh4vma_t vma )
426 sh4addr_t addr = mmu_vma_to_phys_disasm(vma);
427 return addr != MMU_VMA_ERROR && mem_has_page(addr);
.