Search
lxdream.org :: lxdream/src/sh4/sh4.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4.c
changeset 591:7b9612fd2395
prev586:2a3ba82cf243
next597:87cbdf62aa35
author nkeynes
date Thu Jan 17 10:11:37 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Add flag to skip breakpoints when it's the very first instruction of a run
(ie, so executing dreamcast_run() when the current pc is a breakpoint doesn't
just return immediately)
file annotate diff log raw
nkeynes@378
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@378
     3
 * 
nkeynes@378
     4
 * SH4 parent module for all CPU modes and SH4 peripheral
nkeynes@378
     5
 * modules.
nkeynes@378
     6
 *
nkeynes@378
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@378
     8
 *
nkeynes@378
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@378
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@378
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@378
    12
 * (at your option) any later version.
nkeynes@378
    13
 *
nkeynes@378
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@378
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@378
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@378
    17
 * GNU General Public License for more details.
nkeynes@378
    18
 */
nkeynes@378
    19
nkeynes@378
    20
#define MODULE sh4_module
nkeynes@378
    21
#include <math.h>
nkeynes@378
    22
#include "dream.h"
nkeynes@422
    23
#include "dreamcast.h"
nkeynes@378
    24
#include "sh4/sh4core.h"
nkeynes@378
    25
#include "sh4/sh4mmio.h"
nkeynes@378
    26
#include "sh4/intc.h"
nkeynes@422
    27
#include "sh4/xltcache.h"
nkeynes@422
    28
#include "sh4/sh4stat.h"
nkeynes@378
    29
#include "mem.h"
nkeynes@378
    30
#include "clock.h"
nkeynes@378
    31
#include "syscall.h"
nkeynes@378
    32
nkeynes@378
    33
void sh4_init( void );
nkeynes@526
    34
void sh4_xlat_init( void );
nkeynes@378
    35
void sh4_reset( void );
nkeynes@378
    36
void sh4_start( void );
nkeynes@378
    37
void sh4_stop( void );
nkeynes@378
    38
void sh4_save_state( FILE *f );
nkeynes@378
    39
int sh4_load_state( FILE *f );
nkeynes@378
    40
nkeynes@378
    41
uint32_t sh4_run_slice( uint32_t );
nkeynes@378
    42
uint32_t sh4_xlat_run_slice( uint32_t );
nkeynes@378
    43
nkeynes@378
    44
struct dreamcast_module sh4_module = { "SH4", sh4_init, sh4_reset, 
nkeynes@591
    45
				       sh4_start, sh4_run_slice, sh4_stop,
nkeynes@378
    46
				       sh4_save_state, sh4_load_state };
nkeynes@378
    47
nkeynes@378
    48
struct sh4_registers sh4r;
nkeynes@378
    49
struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
nkeynes@378
    50
int sh4_breakpoint_count = 0;
nkeynes@586
    51
sh4ptr_t sh4_main_ram;
nkeynes@591
    52
gboolean sh4_starting = FALSE;
nkeynes@526
    53
static gboolean sh4_use_translator = FALSE;
nkeynes@586
    54
struct sh4_icache_struct sh4_icache = { NULL, -1, -1, 0 };
nkeynes@378
    55
nkeynes@378
    56
void sh4_set_use_xlat( gboolean use )
nkeynes@378
    57
{
nkeynes@526
    58
// No-op if the translator was not built
nkeynes@526
    59
#ifdef SH4_TRANSLATOR
nkeynes@378
    60
    if( use ) {
nkeynes@378
    61
	xlat_cache_init();
nkeynes@378
    62
	sh4_x86_init();
nkeynes@378
    63
	sh4_module.run_time_slice = sh4_xlat_run_slice;
nkeynes@378
    64
    } else {
nkeynes@378
    65
	sh4_module.run_time_slice = sh4_run_slice;
nkeynes@378
    66
    }
nkeynes@526
    67
    sh4_use_translator = use;
nkeynes@526
    68
#endif
nkeynes@378
    69
}
nkeynes@378
    70
nkeynes@586
    71
gboolean sh4_is_using_xlat()
nkeynes@586
    72
{
nkeynes@586
    73
    return sh4_use_translator;
nkeynes@586
    74
}
nkeynes@586
    75
nkeynes@378
    76
void sh4_init(void)
nkeynes@378
    77
{
nkeynes@378
    78
    register_io_regions( mmio_list_sh4mmio );
nkeynes@418
    79
    sh4_main_ram = mem_get_region_by_name(MEM_REGION_MAIN);
nkeynes@378
    80
    MMU_init();
nkeynes@378
    81
    sh4_reset();
nkeynes@378
    82
}
nkeynes@378
    83
nkeynes@591
    84
void sh4_start(void)
nkeynes@591
    85
{
nkeynes@591
    86
    sh4_starting = TRUE;
nkeynes@591
    87
}
nkeynes@591
    88
nkeynes@378
    89
void sh4_reset(void)
nkeynes@378
    90
{
nkeynes@526
    91
    if(	sh4_use_translator ) {
nkeynes@472
    92
	xlat_flush_cache();
nkeynes@472
    93
    }
nkeynes@472
    94
nkeynes@378
    95
    /* zero everything out, for the sake of having a consistent state. */
nkeynes@378
    96
    memset( &sh4r, 0, sizeof(sh4r) );
nkeynes@378
    97
nkeynes@378
    98
    /* Resume running if we were halted */
nkeynes@378
    99
    sh4r.sh4_state = SH4_STATE_RUNNING;
nkeynes@378
   100
nkeynes@378
   101
    sh4r.pc    = 0xA0000000;
nkeynes@378
   102
    sh4r.new_pc= 0xA0000002;
nkeynes@378
   103
    sh4r.vbr   = 0x00000000;
nkeynes@378
   104
    sh4r.fpscr = 0x00040001;
nkeynes@378
   105
    sh4r.sr    = 0x700000F0;
nkeynes@378
   106
    sh4r.fr_bank = &sh4r.fr[0][0];
nkeynes@378
   107
nkeynes@378
   108
    /* Mem reset will do this, but if we want to reset _just_ the SH4... */
nkeynes@378
   109
    MMIO_WRITE( MMU, EXPEVT, EXC_POWER_RESET );
nkeynes@378
   110
nkeynes@378
   111
    /* Peripheral modules */
nkeynes@378
   112
    CPG_reset();
nkeynes@378
   113
    INTC_reset();
nkeynes@378
   114
    MMU_reset();
nkeynes@378
   115
    TMU_reset();
nkeynes@378
   116
    SCIF_reset();
nkeynes@401
   117
    sh4_stats_reset();
nkeynes@378
   118
}
nkeynes@378
   119
nkeynes@378
   120
void sh4_stop(void)
nkeynes@378
   121
{
nkeynes@526
   122
    if(	sh4_use_translator ) {
nkeynes@502
   123
	/* If we were running with the translator, update new_pc and in_delay_slot */
nkeynes@502
   124
	sh4r.new_pc = sh4r.pc+2;
nkeynes@502
   125
	sh4r.in_delay_slot = FALSE;
nkeynes@502
   126
    }
nkeynes@378
   127
nkeynes@378
   128
}
nkeynes@378
   129
nkeynes@378
   130
void sh4_save_state( FILE *f )
nkeynes@378
   131
{
nkeynes@526
   132
    if(	sh4_use_translator ) {
nkeynes@401
   133
	/* If we were running with the translator, update new_pc and in_delay_slot */
nkeynes@401
   134
	sh4r.new_pc = sh4r.pc+2;
nkeynes@401
   135
	sh4r.in_delay_slot = FALSE;
nkeynes@401
   136
    }
nkeynes@401
   137
nkeynes@378
   138
    fwrite( &sh4r, sizeof(sh4r), 1, f );
nkeynes@378
   139
    MMU_save_state( f );
nkeynes@378
   140
    INTC_save_state( f );
nkeynes@378
   141
    TMU_save_state( f );
nkeynes@378
   142
    SCIF_save_state( f );
nkeynes@378
   143
}
nkeynes@378
   144
nkeynes@378
   145
int sh4_load_state( FILE * f )
nkeynes@378
   146
{
nkeynes@526
   147
    if(	sh4_use_translator ) {
nkeynes@472
   148
	xlat_flush_cache();
nkeynes@472
   149
    }
nkeynes@378
   150
    fread( &sh4r, sizeof(sh4r), 1, f );
nkeynes@412
   151
    sh4r.fr_bank = &sh4r.fr[(sh4r.fpscr&FPSCR_FR)>>21][0]; // Fixup internal FR pointer
nkeynes@378
   152
    MMU_load_state( f );
nkeynes@378
   153
    INTC_load_state( f );
nkeynes@378
   154
    TMU_load_state( f );
nkeynes@378
   155
    return SCIF_load_state( f );
nkeynes@378
   156
}
nkeynes@378
   157
nkeynes@378
   158
nkeynes@586
   159
void sh4_set_breakpoint( uint32_t pc, breakpoint_type_t type )
nkeynes@378
   160
{
nkeynes@378
   161
    sh4_breakpoints[sh4_breakpoint_count].address = pc;
nkeynes@378
   162
    sh4_breakpoints[sh4_breakpoint_count].type = type;
nkeynes@586
   163
    if( sh4_use_translator ) {
nkeynes@586
   164
	xlat_invalidate_word( pc );
nkeynes@586
   165
    }
nkeynes@378
   166
    sh4_breakpoint_count++;
nkeynes@378
   167
}
nkeynes@378
   168
nkeynes@586
   169
gboolean sh4_clear_breakpoint( uint32_t pc, breakpoint_type_t type )
nkeynes@378
   170
{
nkeynes@378
   171
    int i;
nkeynes@378
   172
nkeynes@378
   173
    for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@378
   174
	if( sh4_breakpoints[i].address == pc && 
nkeynes@378
   175
	    sh4_breakpoints[i].type == type ) {
nkeynes@378
   176
	    while( ++i < sh4_breakpoint_count ) {
nkeynes@378
   177
		sh4_breakpoints[i-1].address = sh4_breakpoints[i].address;
nkeynes@378
   178
		sh4_breakpoints[i-1].type = sh4_breakpoints[i].type;
nkeynes@378
   179
	    }
nkeynes@586
   180
	    if( sh4_use_translator ) {
nkeynes@586
   181
		xlat_invalidate_word( pc );
nkeynes@586
   182
	    }
nkeynes@378
   183
	    sh4_breakpoint_count--;
nkeynes@378
   184
	    return TRUE;
nkeynes@378
   185
	}
nkeynes@378
   186
    }
nkeynes@378
   187
    return FALSE;
nkeynes@378
   188
}
nkeynes@378
   189
nkeynes@378
   190
int sh4_get_breakpoint( uint32_t pc )
nkeynes@378
   191
{
nkeynes@378
   192
    int i;
nkeynes@378
   193
    for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@378
   194
	if( sh4_breakpoints[i].address == pc )
nkeynes@378
   195
	    return sh4_breakpoints[i].type;
nkeynes@378
   196
    }
nkeynes@378
   197
    return 0;
nkeynes@378
   198
}
nkeynes@378
   199
nkeynes@401
   200
void sh4_set_pc( int pc )
nkeynes@401
   201
{
nkeynes@401
   202
    sh4r.pc = pc;
nkeynes@401
   203
    sh4r.new_pc = pc+2;
nkeynes@401
   204
}
nkeynes@401
   205
nkeynes@401
   206
nkeynes@401
   207
/******************************* Support methods ***************************/
nkeynes@401
   208
nkeynes@401
   209
static void sh4_switch_banks( )
nkeynes@401
   210
{
nkeynes@401
   211
    uint32_t tmp[8];
nkeynes@401
   212
nkeynes@401
   213
    memcpy( tmp, sh4r.r, sizeof(uint32_t)*8 );
nkeynes@401
   214
    memcpy( sh4r.r, sh4r.r_bank, sizeof(uint32_t)*8 );
nkeynes@401
   215
    memcpy( sh4r.r_bank, tmp, sizeof(uint32_t)*8 );
nkeynes@401
   216
}
nkeynes@401
   217
nkeynes@401
   218
void sh4_write_sr( uint32_t newval )
nkeynes@401
   219
{
nkeynes@586
   220
    int oldbank = (sh4r.sr&SR_MDRB) == SR_MDRB;
nkeynes@586
   221
    int newbank = (newval&SR_MDRB) == SR_MDRB;
nkeynes@586
   222
    if( oldbank != newbank )
nkeynes@401
   223
        sh4_switch_banks();
nkeynes@401
   224
    sh4r.sr = newval;
nkeynes@401
   225
    sh4r.t = (newval&SR_T) ? 1 : 0;
nkeynes@401
   226
    sh4r.s = (newval&SR_S) ? 1 : 0;
nkeynes@401
   227
    sh4r.m = (newval&SR_M) ? 1 : 0;
nkeynes@401
   228
    sh4r.q = (newval&SR_Q) ? 1 : 0;
nkeynes@401
   229
    intc_mask_changed();
nkeynes@401
   230
}
nkeynes@401
   231
nkeynes@401
   232
uint32_t sh4_read_sr( void )
nkeynes@401
   233
{
nkeynes@401
   234
    /* synchronize sh4r.sr with the various bitflags */
nkeynes@401
   235
    sh4r.sr &= SR_MQSTMASK;
nkeynes@401
   236
    if( sh4r.t ) sh4r.sr |= SR_T;
nkeynes@401
   237
    if( sh4r.s ) sh4r.sr |= SR_S;
nkeynes@401
   238
    if( sh4r.m ) sh4r.sr |= SR_M;
nkeynes@401
   239
    if( sh4r.q ) sh4r.sr |= SR_Q;
nkeynes@401
   240
    return sh4r.sr;
nkeynes@401
   241
}
nkeynes@401
   242
nkeynes@401
   243
nkeynes@401
   244
nkeynes@401
   245
#define RAISE( x, v ) do{			\
nkeynes@401
   246
    if( sh4r.vbr == 0 ) { \
nkeynes@401
   247
        ERROR( "%08X: VBR not initialized while raising exception %03X, halting", sh4r.pc, x ); \
nkeynes@401
   248
        dreamcast_stop(); return FALSE;	\
nkeynes@401
   249
    } else { \
nkeynes@401
   250
        sh4r.spc = sh4r.pc;	\
nkeynes@401
   251
        sh4r.ssr = sh4_read_sr(); \
nkeynes@401
   252
        sh4r.sgr = sh4r.r[15]; \
nkeynes@401
   253
        MMIO_WRITE(MMU,EXPEVT,x); \
nkeynes@401
   254
        sh4r.pc = sh4r.vbr + v; \
nkeynes@401
   255
        sh4r.new_pc = sh4r.pc + 2; \
nkeynes@401
   256
        sh4_write_sr( sh4r.ssr |SR_MD|SR_BL|SR_RB ); \
nkeynes@401
   257
	if( sh4r.in_delay_slot ) { \
nkeynes@401
   258
	    sh4r.in_delay_slot = 0; \
nkeynes@401
   259
	    sh4r.spc -= 2; \
nkeynes@401
   260
	} \
nkeynes@401
   261
    } \
nkeynes@401
   262
    return TRUE; } while(0)
nkeynes@401
   263
nkeynes@401
   264
/**
nkeynes@401
   265
 * Raise a general CPU exception for the specified exception code.
nkeynes@401
   266
 * (NOT for TRAPA or TLB exceptions)
nkeynes@401
   267
 */
nkeynes@401
   268
gboolean sh4_raise_exception( int code )
nkeynes@401
   269
{
nkeynes@401
   270
    RAISE( code, EXV_EXCEPTION );
nkeynes@401
   271
}
nkeynes@401
   272
nkeynes@586
   273
/**
nkeynes@586
   274
 * Raise a CPU reset exception with the specified exception code.
nkeynes@586
   275
 */
nkeynes@586
   276
gboolean sh4_raise_reset( int code )
nkeynes@586
   277
{
nkeynes@586
   278
    // FIXME: reset modules as per "manual reset"
nkeynes@586
   279
    sh4_reset();
nkeynes@586
   280
    MMIO_WRITE(MMU,EXPEVT,code);
nkeynes@586
   281
    sh4r.vbr = 0;
nkeynes@586
   282
    sh4r.pc = 0xA0000000;
nkeynes@586
   283
    sh4r.new_pc = sh4r.pc + 2;
nkeynes@586
   284
    sh4_write_sr( (sh4r.sr|SR_MD|SR_BL|SR_RB|SR_IMASK)
nkeynes@586
   285
		  &(~SR_FD) );
nkeynes@586
   286
}
nkeynes@586
   287
nkeynes@401
   288
gboolean sh4_raise_trap( int trap )
nkeynes@401
   289
{
nkeynes@401
   290
    MMIO_WRITE( MMU, TRA, trap<<2 );
nkeynes@586
   291
    RAISE( EXC_TRAP, EXV_EXCEPTION );
nkeynes@401
   292
}
nkeynes@401
   293
nkeynes@401
   294
gboolean sh4_raise_slot_exception( int normal_code, int slot_code ) {
nkeynes@401
   295
    if( sh4r.in_delay_slot ) {
nkeynes@401
   296
	return sh4_raise_exception(slot_code);
nkeynes@401
   297
    } else {
nkeynes@401
   298
	return sh4_raise_exception(normal_code);
nkeynes@401
   299
    }
nkeynes@401
   300
}
nkeynes@401
   301
nkeynes@401
   302
gboolean sh4_raise_tlb_exception( int code )
nkeynes@401
   303
{
nkeynes@401
   304
    RAISE( code, EXV_TLBMISS );
nkeynes@401
   305
}
nkeynes@401
   306
nkeynes@401
   307
void sh4_accept_interrupt( void )
nkeynes@401
   308
{
nkeynes@401
   309
    uint32_t code = intc_accept_interrupt();
nkeynes@401
   310
    sh4r.ssr = sh4_read_sr();
nkeynes@401
   311
    sh4r.spc = sh4r.pc;
nkeynes@401
   312
    sh4r.sgr = sh4r.r[15];
nkeynes@401
   313
    sh4_write_sr( sh4r.ssr|SR_BL|SR_MD|SR_RB );
nkeynes@401
   314
    MMIO_WRITE( MMU, INTEVT, code );
nkeynes@401
   315
    sh4r.pc = sh4r.vbr + 0x600;
nkeynes@401
   316
    sh4r.new_pc = sh4r.pc + 2;
nkeynes@401
   317
    //    WARN( "Accepting interrupt %03X, from %08X => %08X", code, sh4r.spc, sh4r.pc );
nkeynes@401
   318
}
nkeynes@401
   319
nkeynes@401
   320
void signsat48( void )
nkeynes@401
   321
{
nkeynes@401
   322
    if( ((int64_t)sh4r.mac) < (int64_t)0xFFFF800000000000LL )
nkeynes@401
   323
	sh4r.mac = 0xFFFF800000000000LL;
nkeynes@401
   324
    else if( ((int64_t)sh4r.mac) > (int64_t)0x00007FFFFFFFFFFFLL )
nkeynes@401
   325
	sh4r.mac = 0x00007FFFFFFFFFFFLL;
nkeynes@401
   326
}
nkeynes@401
   327
nkeynes@401
   328
void sh4_fsca( uint32_t anglei, float *fr )
nkeynes@401
   329
{
nkeynes@401
   330
    float angle = (((float)(anglei&0xFFFF))/65536.0) * 2 * M_PI;
nkeynes@401
   331
    *fr++ = cosf(angle);
nkeynes@401
   332
    *fr = sinf(angle);
nkeynes@401
   333
}
nkeynes@401
   334
nkeynes@401
   335
void sh4_sleep(void)
nkeynes@401
   336
{
nkeynes@401
   337
    if( MMIO_READ( CPG, STBCR ) & 0x80 ) {
nkeynes@401
   338
	sh4r.sh4_state = SH4_STATE_STANDBY;
nkeynes@401
   339
    } else {
nkeynes@401
   340
	sh4r.sh4_state = SH4_STATE_SLEEP;
nkeynes@401
   341
    }
nkeynes@401
   342
}
nkeynes@401
   343
nkeynes@401
   344
/**
nkeynes@401
   345
 * Compute the matrix tranform of fv given the matrix xf.
nkeynes@401
   346
 * Both fv and xf are word-swapped as per the sh4r.fr banks
nkeynes@401
   347
 */
nkeynes@401
   348
void sh4_ftrv( float *target, float *xf )
nkeynes@401
   349
{
nkeynes@401
   350
    float fv[4] = { target[1], target[0], target[3], target[2] };
nkeynes@401
   351
    target[1] = xf[1] * fv[0] + xf[5]*fv[1] +
nkeynes@401
   352
	xf[9]*fv[2] + xf[13]*fv[3];
nkeynes@401
   353
    target[0] = xf[0] * fv[0] + xf[4]*fv[1] +
nkeynes@401
   354
	xf[8]*fv[2] + xf[12]*fv[3];
nkeynes@401
   355
    target[3] = xf[3] * fv[0] + xf[7]*fv[1] +
nkeynes@401
   356
	xf[11]*fv[2] + xf[15]*fv[3];
nkeynes@401
   357
    target[2] = xf[2] * fv[0] + xf[6]*fv[1] +
nkeynes@401
   358
	xf[10]*fv[2] + xf[14]*fv[3];
nkeynes@401
   359
}
nkeynes@401
   360
.