Search
lxdream.org :: lxdream/src/sh4/sh4core.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4core.h
changeset 561:533f6b478071
prev559:06714bc64271
next564:dc7b5ffb0535
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@10
     3
 * 
nkeynes@54
     4
 * This file defines the internal functions exported/used by the SH4 core, 
nkeynes@54
     5
 * except for disassembly functions defined in sh4dasm.h
nkeynes@10
     6
 *
nkeynes@10
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@10
     8
 *
nkeynes@10
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@10
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@10
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@10
    12
 * (at your option) any later version.
nkeynes@10
    13
 *
nkeynes@10
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@10
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@10
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@10
    17
 * GNU General Public License for more details.
nkeynes@1
    18
 */
nkeynes@30
    19
nkeynes@1
    20
#ifndef sh4core_H
nkeynes@1
    21
#define sh4core_H 1
nkeynes@1
    22
nkeynes@27
    23
#include <glib/gtypes.h>
nkeynes@1
    24
#include <stdint.h>
nkeynes@23
    25
#include <stdio.h>
nkeynes@378
    26
#include "mem.h"
nkeynes@1
    27
nkeynes@1
    28
#ifdef __cplusplus
nkeynes@1
    29
extern "C" {
nkeynes@1
    30
#if 0
nkeynes@1
    31
}
nkeynes@1
    32
#endif
nkeynes@1
    33
#endif
nkeynes@1
    34
nkeynes@27
    35
nkeynes@27
    36
/**
nkeynes@27
    37
 * SH4 is running normally 
nkeynes@27
    38
 */
nkeynes@27
    39
#define SH4_STATE_RUNNING 1
nkeynes@27
    40
/**
nkeynes@27
    41
 * SH4 is not executing instructions but all peripheral modules are still
nkeynes@27
    42
 * running
nkeynes@27
    43
 */
nkeynes@27
    44
#define SH4_STATE_SLEEP 2
nkeynes@27
    45
/**
nkeynes@27
    46
 * SH4 is not executing instructions, DMAC is halted, but all other peripheral
nkeynes@27
    47
 * modules are still running
nkeynes@27
    48
 */
nkeynes@27
    49
#define SH4_STATE_DEEP_SLEEP 3
nkeynes@27
    50
/**
nkeynes@27
    51
 * SH4 is not executing instructions and all peripheral modules are also
nkeynes@27
    52
 * stopped. As close as you can get to powered-off without actually being
nkeynes@27
    53
 * off.
nkeynes@27
    54
 */
nkeynes@27
    55
#define SH4_STATE_STANDBY 4
nkeynes@27
    56
nkeynes@265
    57
#define PENDING_IRQ 1
nkeynes@265
    58
#define PENDING_EVENT 2
nkeynes@265
    59
nkeynes@1
    60
struct sh4_registers {
nkeynes@1
    61
    uint32_t r[16];
nkeynes@374
    62
    uint32_t sr, pr, pc, fpscr;
nkeynes@374
    63
    uint32_t t, m, q, s; /* really boolean - 0 or 1 */
nkeynes@374
    64
    int32_t fpul;
nkeynes@374
    65
    float *fr_bank;
nkeynes@374
    66
    float fr[2][16];
nkeynes@374
    67
    uint64_t mac;
nkeynes@374
    68
    uint32_t gbr, ssr, spc, sgr, dbr, vbr;
nkeynes@374
    69
nkeynes@1
    70
    uint32_t r_bank[8]; /* hidden banked registers */
nkeynes@2
    71
    int32_t store_queue[16]; /* technically 2 banks of 32 bytes */
nkeynes@2
    72
    
nkeynes@1
    73
    uint32_t new_pc; /* Not a real register, but used to handle delay slots */
nkeynes@265
    74
    uint32_t event_pending; /* slice cycle time of the next pending event, or FFFFFFFF
nkeynes@265
    75
                             when no events are pending */
nkeynes@265
    76
    uint32_t event_types; /* bit 0 = IRQ pending, bit 1 = general event pending */
nkeynes@2
    77
    int in_delay_slot; /* flag to indicate the current instruction is in
nkeynes@2
    78
                             * a delay slot (certain rules apply) */
nkeynes@302
    79
    uint32_t slice_cycle; /* Current nanosecond within the timeslice */
nkeynes@27
    80
    int sh4_state; /* Current power-on state (one of the SH4_STATE_* values ) */
nkeynes@1
    81
};
nkeynes@1
    82
nkeynes@1
    83
extern struct sh4_registers sh4r;
nkeynes@378
    84
extern struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
nkeynes@378
    85
extern int sh4_breakpoint_count;
nkeynes@378
    86
nkeynes@1
    87
nkeynes@1
    88
/* Public functions */
nkeynes@378
    89
void sh4_set_use_xlat( gboolean use );
nkeynes@1
    90
void sh4_init( void );
nkeynes@1
    91
void sh4_reset( void );
nkeynes@1
    92
void sh4_run( void );
nkeynes@1
    93
void sh4_runto( uint32_t pc, uint32_t count );
nkeynes@1
    94
void sh4_runfor( uint32_t count );
nkeynes@1
    95
int sh4_isrunning( void );
nkeynes@1
    96
void sh4_stop( void );
nkeynes@1
    97
void sh4_set_pc( int );
nkeynes@401
    98
void sh4_sleep( void );
nkeynes@401
    99
void sh4_fsca( uint32_t angle, float *fr );
nkeynes@401
   100
void sh4_ftrv( float *fv, float *xmtrx );
nkeynes@401
   101
void signsat48(void);
nkeynes@378
   102
nkeynes@27
   103
gboolean sh4_execute_instruction( void );
nkeynes@246
   104
gboolean sh4_raise_exception( int );
nkeynes@559
   105
gboolean sh4_raise_reset( int );
nkeynes@391
   106
gboolean sh4_raise_trap( int );
nkeynes@246
   107
gboolean sh4_raise_slot_exception( int, int );
nkeynes@246
   108
gboolean sh4_raise_tlb_exception( int );
nkeynes@23
   109
void sh4_set_breakpoint( uint32_t pc, int type );
nkeynes@43
   110
gboolean sh4_clear_breakpoint( uint32_t pc, int type );
nkeynes@43
   111
int sh4_get_breakpoint( uint32_t pc );
nkeynes@378
   112
void sh4_accept_interrupt( void );
nkeynes@23
   113
nkeynes@23
   114
#define BREAK_ONESHOT 1
nkeynes@23
   115
#define BREAK_PERM 2
nkeynes@1
   116
nkeynes@10
   117
/* SH4 Memory */
nkeynes@559
   118
uint64_t mmu_vma_to_phys_read( sh4addr_t addr );
nkeynes@559
   119
uint64_t mmu_vma_to_phys_write( sh4addr_t addr );
nkeynes@559
   120
uint64_t mmu_vma_to_phys_exec( sh4addr_t addr );
nkeynes@559
   121
nkeynes@527
   122
int64_t sh4_read_quad( sh4addr_t addr );
nkeynes@559
   123
int64_t sh4_read_long( sh4addr_t addr );
nkeynes@559
   124
int64_t sh4_read_word( sh4addr_t addr );
nkeynes@559
   125
int64_t sh4_read_byte( sh4addr_t addr );
nkeynes@527
   126
void sh4_write_quad( sh4addr_t addr, uint64_t val );
nkeynes@559
   127
int32_t sh4_write_long( sh4addr_t addr, uint32_t val );
nkeynes@559
   128
int32_t sh4_write_word( sh4addr_t addr, uint32_t val );
nkeynes@559
   129
int32_t sh4_write_byte( sh4addr_t addr, uint32_t val );
nkeynes@527
   130
int32_t sh4_read_phys_word( sh4addr_t addr );
nkeynes@527
   131
void sh4_flush_store_queue( sh4addr_t addr );
nkeynes@559
   132
sh4ptr_t sh4_get_region_by_vma( sh4addr_t addr );
nkeynes@10
   133
nkeynes@374
   134
/* SH4 Support methods */
nkeynes@374
   135
uint32_t sh4_read_sr(void);
nkeynes@374
   136
void sh4_write_sr(uint32_t val);
nkeynes@374
   137
nkeynes@23
   138
/* Peripheral functions */
nkeynes@260
   139
void CPG_reset( void );
nkeynes@30
   140
void TMU_run_slice( uint32_t );
nkeynes@53
   141
void TMU_update_clocks( void );
nkeynes@53
   142
void TMU_reset( void );
nkeynes@53
   143
void TMU_save_state( FILE * );
nkeynes@53
   144
int TMU_load_state( FILE * );
nkeynes@54
   145
void DMAC_reset( void );
nkeynes@54
   146
void DMAC_run_slice( uint32_t );
nkeynes@54
   147
void DMAC_save_state( FILE * );
nkeynes@54
   148
int DMAC_load_state( FILE * );
nkeynes@32
   149
void SCIF_reset( void );
nkeynes@30
   150
void SCIF_run_slice( uint32_t );
nkeynes@23
   151
void SCIF_save_state( FILE *f );
nkeynes@23
   152
int SCIF_load_state( FILE *f );
nkeynes@157
   153
void INTC_reset( void );
nkeynes@157
   154
void INTC_save_state( FILE *f );
nkeynes@157
   155
int INTC_load_state( FILE *f );
nkeynes@312
   156
void MMU_init( void );
nkeynes@312
   157
void MMU_reset( void );
nkeynes@312
   158
void MMU_save_state( FILE *f );
nkeynes@312
   159
int MMU_load_state( FILE *f );
nkeynes@550
   160
void MMU_ldtlb();
nkeynes@422
   161
void SCIF_update_line_speed(void);
nkeynes@1
   162
nkeynes@1
   163
#define SIGNEXT4(n) ((((int32_t)(n))<<28)>>28)
nkeynes@1
   164
#define SIGNEXT8(n) ((int32_t)((int8_t)(n)))
nkeynes@1
   165
#define SIGNEXT12(n) ((((int32_t)(n))<<20)>>20)
nkeynes@1
   166
#define SIGNEXT16(n) ((int32_t)((int16_t)(n)))
nkeynes@1
   167
#define SIGNEXT32(n) ((int64_t)((int32_t)(n)))
nkeynes@1
   168
#define SIGNEXT48(n) ((((int64_t)(n))<<16)>>16)
nkeynes@559
   169
#define ZEROEXT32(n) ((int64_t)((uint64_t)((uint32_t)(n))))
nkeynes@1
   170
nkeynes@1
   171
/* Status Register (SR) bits */
nkeynes@1
   172
#define SR_MD    0x40000000 /* Processor mode ( User=0, Privileged=1 ) */ 
nkeynes@1
   173
#define SR_RB    0x20000000 /* Register bank (priviledged mode only) */
nkeynes@1
   174
#define SR_BL    0x10000000 /* Exception/interupt block (1 = masked) */
nkeynes@1
   175
#define SR_FD    0x00008000 /* FPU disable */
nkeynes@1
   176
#define SR_M     0x00000200
nkeynes@1
   177
#define SR_Q     0x00000100
nkeynes@1
   178
#define SR_IMASK 0x000000F0 /* Interrupt mask level */
nkeynes@1
   179
#define SR_S     0x00000002 /* Saturation operation for MAC instructions */
nkeynes@1
   180
#define SR_T     0x00000001 /* True/false or carry/borrow */
nkeynes@1
   181
#define SR_MASK  0x700083F3
nkeynes@1
   182
#define SR_MQSTMASK 0xFFFFFCFC /* Mask to clear the flags we're keeping separately */
nkeynes@1
   183
nkeynes@1
   184
#define IS_SH4_PRIVMODE() (sh4r.sr&SR_MD)
nkeynes@1
   185
#define SH4_INTMASK() ((sh4r.sr&SR_IMASK)>>4)
nkeynes@265
   186
#define SH4_EVENT_PENDING() (sh4r.event_pending <= sh4r.slice_cycle && !sh4r.in_delay_slot)
nkeynes@1
   187
nkeynes@1
   188
#define FPSCR_FR     0x00200000 /* FPU register bank */
nkeynes@1
   189
#define FPSCR_SZ     0x00100000 /* FPU transfer size (0=32 bits, 1=64 bits) */
nkeynes@1
   190
#define FPSCR_PR     0x00080000 /* Precision (0=32 bites, 1=64 bits) */
nkeynes@1
   191
#define FPSCR_DN     0x00040000 /* Denormalization mode (1 = treat as 0) */
nkeynes@1
   192
#define FPSCR_CAUSE  0x0003F000
nkeynes@1
   193
#define FPSCR_ENABLE 0x00000F80
nkeynes@1
   194
#define FPSCR_FLAG   0x0000007C
nkeynes@1
   195
#define FPSCR_RM     0x00000003 /* Rounding mode (0=nearest, 1=to zero) */
nkeynes@1
   196
nkeynes@1
   197
#define IS_FPU_DOUBLEPREC() (sh4r.fpscr&FPSCR_PR)
nkeynes@1
   198
#define IS_FPU_DOUBLESIZE() (sh4r.fpscr&FPSCR_SZ)
nkeynes@1
   199
#define IS_FPU_ENABLED() ((sh4r.sr&SR_FD)==0)
nkeynes@1
   200
nkeynes@374
   201
#define FR(x) sh4r.fr_bank[(x)^1]
nkeynes@374
   202
#define DRF(x) ((double *)sh4r.fr_bank)[x]
nkeynes@84
   203
#define XF(x) sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21][(x)^1]
nkeynes@95
   204
#define XDR(x) ((double *)(sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21]))[x]
nkeynes@95
   205
#define DRb(x,b) ((double *)(sh4r.fr[((b ? (~sh4r.fpscr) : sh4r.fpscr)&FPSCR_FR)>>21]))[x]
nkeynes@359
   206
#define DR(x) DRb((x>>1), (x&1))
nkeynes@359
   207
#define FPULf   *((float *)&sh4r.fpul)
nkeynes@359
   208
#define FPULi    (sh4r.fpul)
nkeynes@359
   209
nkeynes@367
   210
/* CPU-generated exception code/vector pairs */
nkeynes@559
   211
#define EXC_POWER_RESET     0x000 /* vector special */
nkeynes@559
   212
#define EXC_MANUAL_RESET    0x020
nkeynes@559
   213
#define EXC_TLB_MISS_READ   0x040
nkeynes@559
   214
#define EXC_TLB_MISS_WRITE  0x060
nkeynes@559
   215
#define EXC_INIT_PAGE_WRITE 0x080
nkeynes@559
   216
#define EXC_TLB_PROT_READ   0x0A0
nkeynes@559
   217
#define EXC_TLB_PROT_WRITE  0x0C0
nkeynes@559
   218
#define EXC_DATA_ADDR_READ  0x0E0
nkeynes@367
   219
#define EXC_DATA_ADDR_WRITE 0x100
nkeynes@559
   220
#define EXC_TLB_MULTI_HIT   0x140
nkeynes@559
   221
#define EXC_SLOT_ILLEGAL    0x1A0
nkeynes@559
   222
#define EXC_ILLEGAL         0x180
nkeynes@559
   223
#define EXC_TRAP            0x160
nkeynes@559
   224
#define EXC_FPU_DISABLED    0x800
nkeynes@367
   225
#define EXC_SLOT_FPU_DISABLED 0x820
nkeynes@367
   226
nkeynes@559
   227
#define EXV_EXCEPTION    0x100  /* General exception vector */
nkeynes@559
   228
#define EXV_TLBMISS      0x400  /* TLB-miss exception vector */
nkeynes@559
   229
#define EXV_INTERRUPT    0x600  /* External interrupt vector */
nkeynes@559
   230
nkeynes@1
   231
/* Exceptions (for use with sh4_raise_exception) */
nkeynes@1
   232
nkeynes@1
   233
#define EX_ILLEGAL_INSTRUCTION 0x180, 0x100
nkeynes@1
   234
#define EX_SLOT_ILLEGAL        0x1A0, 0x100
nkeynes@1
   235
#define EX_TLB_MISS_READ       0x040, 0x400
nkeynes@1
   236
#define EX_TLB_MISS_WRITE      0x060, 0x400
nkeynes@1
   237
#define EX_INIT_PAGE_WRITE     0x080, 0x100
nkeynes@1
   238
#define EX_TLB_PROT_READ       0x0A0, 0x100
nkeynes@1
   239
#define EX_TLB_PROT_WRITE      0x0C0, 0x100
nkeynes@1
   240
#define EX_DATA_ADDR_READ      0x0E0, 0x100
nkeynes@1
   241
#define EX_DATA_ADDR_WRITE     0x100, 0x100
nkeynes@1
   242
#define EX_FPU_EXCEPTION       0x120, 0x100
nkeynes@1
   243
#define EX_TRAPA               0x160, 0x100
nkeynes@1
   244
#define EX_BREAKPOINT          0x1E0, 0x100
nkeynes@1
   245
#define EX_FPU_DISABLED        0x800, 0x100
nkeynes@1
   246
#define EX_SLOT_FPU_DISABLED   0x820, 0x100
nkeynes@1
   247
nkeynes@2
   248
#define SH4_WRITE_STORE_QUEUE(addr,val) sh4r.store_queue[(addr>>2)&0xF] = val;
nkeynes@1
   249
nkeynes@1
   250
#ifdef __cplusplus
nkeynes@1
   251
}
nkeynes@1
   252
#endif
nkeynes@1
   253
#endif
nkeynes@359
   254
.