Search
lxdream.org :: lxdream/src/sh4/sh4core.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4core.h
changeset 570:d2893980fbf5
prev569:a1c49e1e8776
next571:9bc09948d0f2
author nkeynes
date Sun Jan 06 12:24:18 2008 +0000 (14 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Change to generate different code for mmu on/off cases
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@564
    27
#include "sh4/sh4.h"
nkeynes@1
    28
nkeynes@1
    29
#ifdef __cplusplus
nkeynes@1
    30
extern "C" {
nkeynes@1
    31
#endif
nkeynes@1
    32
nkeynes@564
    33
/* Breakpoint data structure */
nkeynes@378
    34
extern struct breakpoint_struct sh4_breakpoints[MAX_BREAKPOINTS];
nkeynes@378
    35
extern int sh4_breakpoint_count;
nkeynes@569
    36
extern sh4ptr_t sh4_main_ram;
nkeynes@569
    37
nkeynes@569
    38
/**
nkeynes@569
    39
 * Cached direct pointer to the current instruction page. If AT is on, this
nkeynes@569
    40
 * is derived from the ITLB, otherwise this will be the entire memory region.
nkeynes@569
    41
 * This is actually a fairly useful optimization, as we can make a lot of
nkeynes@569
    42
 * assumptions about the "current page" that we can't make in general for
nkeynes@569
    43
 * arbitrary virtual addresses.
nkeynes@569
    44
 */
nkeynes@569
    45
struct sh4_icache_struct {
nkeynes@569
    46
    sh4ptr_t page; // Page pointer (NULL if no page)
nkeynes@569
    47
    sh4vma_t page_vma; // virtual address of the page.
nkeynes@569
    48
    sh4addr_t page_ppa; // physical address of the page
nkeynes@569
    49
    uint32_t mask;  // page mask 
nkeynes@569
    50
};
nkeynes@569
    51
extern struct sh4_icache_struct sh4_icache;
nkeynes@569
    52
nkeynes@569
    53
/**
nkeynes@569
    54
 * Test if a given address is contained in the current icache entry
nkeynes@569
    55
 */
nkeynes@569
    56
#define IS_IN_ICACHE(addr) (sh4_icache.page_vma == ((addr) & sh4_icache.mask))
nkeynes@569
    57
/**
nkeynes@569
    58
 * Return a pointer for the given vma, under the assumption that it is
nkeynes@569
    59
 * actually contained in the current icache entry.
nkeynes@569
    60
 */
nkeynes@569
    61
#define GET_ICACHE_PTR(addr) (sh4_icache.page + ((addr)-sh4_icache.page_vma))
nkeynes@569
    62
/**
nkeynes@569
    63
 * Return the physical (external) address for the given vma, assuming that it is
nkeynes@569
    64
 * actually contained in the current icache entry.
nkeynes@569
    65
 */
nkeynes@569
    66
#define GET_ICACHE_PHYS(addr) (sh4_icache.page_ppa + ((addr)-sh4_icache.page_vma))
nkeynes@378
    67
nkeynes@564
    68
/* SH4 module functions */
nkeynes@1
    69
void sh4_init( void );
nkeynes@1
    70
void sh4_reset( void );
nkeynes@1
    71
void sh4_run( void );
nkeynes@1
    72
void sh4_stop( void );
nkeynes@564
    73
nkeynes@564
    74
/* SH4 peripheral module functions */
nkeynes@564
    75
void CPG_reset( void );
nkeynes@564
    76
void DMAC_reset( void );
nkeynes@564
    77
void DMAC_run_slice( uint32_t );
nkeynes@564
    78
void DMAC_save_state( FILE * );
nkeynes@564
    79
int DMAC_load_state( FILE * );
nkeynes@564
    80
void INTC_reset( void );
nkeynes@564
    81
void INTC_save_state( FILE *f );
nkeynes@564
    82
int INTC_load_state( FILE *f );
nkeynes@564
    83
void MMU_init( void );
nkeynes@564
    84
void MMU_reset( void );
nkeynes@564
    85
void MMU_save_state( FILE *f );
nkeynes@564
    86
int MMU_load_state( FILE *f );
nkeynes@564
    87
void MMU_ldtlb();
nkeynes@564
    88
void SCIF_reset( void );
nkeynes@564
    89
void SCIF_run_slice( uint32_t );
nkeynes@564
    90
void SCIF_save_state( FILE *f );
nkeynes@564
    91
int SCIF_load_state( FILE *f );
nkeynes@564
    92
void SCIF_update_line_speed(void);
nkeynes@564
    93
void TMU_reset( void );
nkeynes@564
    94
void TMU_run_slice( uint32_t );
nkeynes@564
    95
void TMU_save_state( FILE * );
nkeynes@564
    96
int TMU_load_state( FILE * );
nkeynes@564
    97
void TMU_update_clocks( void );
nkeynes@564
    98
nkeynes@564
    99
/* SH4 instruction support methods */
nkeynes@401
   100
void sh4_sleep( void );
nkeynes@401
   101
void sh4_fsca( uint32_t angle, float *fr );
nkeynes@401
   102
void sh4_ftrv( float *fv, float *xmtrx );
nkeynes@564
   103
uint32_t sh4_read_sr(void);
nkeynes@564
   104
void sh4_write_sr(uint32_t val);
nkeynes@401
   105
void signsat48(void);
nkeynes@378
   106
nkeynes@10
   107
/* SH4 Memory */
nkeynes@570
   108
#define MMU_VMA_ERROR 0x8000000
nkeynes@570
   109
/**
nkeynes@570
   110
 * Update the sh4_icache structure to contain the specified vma. If the vma
nkeynes@570
   111
 * cannot be resolved, an MMU exception is raised and the function returns
nkeynes@570
   112
 * FALSE. Otherwise, returns TRUE and updates sh4_icache accordingly.
nkeynes@570
   113
 * Note: If the vma resolves to a non-memory area, sh4_icache will be 
nkeynes@570
   114
 * invalidated, but the function will still return TRUE.
nkeynes@570
   115
 * @return FALSE if an MMU exception was raised, otherwise TRUE.
nkeynes@570
   116
 */
nkeynes@569
   117
gboolean mmu_update_icache( sh4vma_t addr );
nkeynes@570
   118
nkeynes@570
   119
/**
nkeynes@570
   120
 * Resolve a virtual address through the TLB for a read operation, returning 
nkeynes@570
   121
 * the resultant P4 or external address. If the resolution fails, the 
nkeynes@570
   122
 * appropriate MMU exception is raised and the value MMU_VMA_ERROR is returned.
nkeynes@570
   123
 * @return An external address (0x00000000-0x1FFFFFFF), a P4 address
nkeynes@570
   124
 * (0xE0000000 - 0xFFFFFFFF), or MMU_VMA_ERROR.
nkeynes@570
   125
 */
nkeynes@570
   126
sh4addr_t mmu_vma_to_phys_read( sh4vma_t addr );
nkeynes@570
   127
sh4addr_t mmu_vma_to_phys_write( sh4vma_t addr );
nkeynes@559
   128
nkeynes@527
   129
int64_t sh4_read_quad( sh4addr_t addr );
nkeynes@570
   130
int32_t sh4_read_long( sh4addr_t addr );
nkeynes@570
   131
int32_t sh4_read_word( sh4addr_t addr );
nkeynes@570
   132
int32_t sh4_read_byte( sh4addr_t addr );
nkeynes@527
   133
void sh4_write_quad( sh4addr_t addr, uint64_t val );
nkeynes@570
   134
void sh4_write_long( sh4addr_t addr, uint32_t val );
nkeynes@570
   135
void sh4_write_word( sh4addr_t addr, uint32_t val );
nkeynes@570
   136
void sh4_write_byte( sh4addr_t addr, uint32_t val );
nkeynes@527
   137
int32_t sh4_read_phys_word( sh4addr_t addr );
nkeynes@527
   138
void sh4_flush_store_queue( sh4addr_t addr );
nkeynes@10
   139
nkeynes@564
   140
/* SH4 Exceptions */
nkeynes@564
   141
#define EXC_POWER_RESET     0x000 /* reset vector */
nkeynes@564
   142
#define EXC_MANUAL_RESET    0x020 /* reset vector */
nkeynes@564
   143
#define EXC_TLB_MISS_READ   0x040 /* TLB vector */
nkeynes@564
   144
#define EXC_TLB_MISS_WRITE  0x060 /* TLB vector */
nkeynes@564
   145
#define EXC_INIT_PAGE_WRITE 0x080
nkeynes@564
   146
#define EXC_TLB_PROT_READ   0x0A0
nkeynes@564
   147
#define EXC_TLB_PROT_WRITE  0x0C0
nkeynes@564
   148
#define EXC_DATA_ADDR_READ  0x0E0
nkeynes@564
   149
#define EXC_DATA_ADDR_WRITE 0x100
nkeynes@564
   150
#define EXC_TLB_MULTI_HIT   0x140
nkeynes@564
   151
#define EXC_SLOT_ILLEGAL    0x1A0
nkeynes@564
   152
#define EXC_ILLEGAL         0x180
nkeynes@564
   153
#define EXC_TRAP            0x160
nkeynes@564
   154
#define EXC_FPU_DISABLED    0x800
nkeynes@564
   155
#define EXC_SLOT_FPU_DISABLED 0x820
nkeynes@374
   156
nkeynes@564
   157
#define EXV_EXCEPTION    0x100  /* General exception vector */
nkeynes@564
   158
#define EXV_TLBMISS      0x400  /* TLB-miss exception vector */
nkeynes@564
   159
#define EXV_INTERRUPT    0x600  /* External interrupt vector */
nkeynes@564
   160
nkeynes@564
   161
gboolean sh4_raise_exception( int );
nkeynes@564
   162
gboolean sh4_raise_reset( int );
nkeynes@564
   163
gboolean sh4_raise_trap( int );
nkeynes@564
   164
gboolean sh4_raise_slot_exception( int, int );
nkeynes@564
   165
gboolean sh4_raise_tlb_exception( int );
nkeynes@564
   166
void sh4_accept_interrupt( void );
nkeynes@1
   167
nkeynes@1
   168
#define SIGNEXT4(n) ((((int32_t)(n))<<28)>>28)
nkeynes@1
   169
#define SIGNEXT8(n) ((int32_t)((int8_t)(n)))
nkeynes@1
   170
#define SIGNEXT12(n) ((((int32_t)(n))<<20)>>20)
nkeynes@1
   171
#define SIGNEXT16(n) ((int32_t)((int16_t)(n)))
nkeynes@1
   172
#define SIGNEXT32(n) ((int64_t)((int32_t)(n)))
nkeynes@1
   173
#define SIGNEXT48(n) ((((int64_t)(n))<<16)>>16)
nkeynes@559
   174
#define ZEROEXT32(n) ((int64_t)((uint64_t)((uint32_t)(n))))
nkeynes@1
   175
nkeynes@1
   176
/* Status Register (SR) bits */
nkeynes@1
   177
#define SR_MD    0x40000000 /* Processor mode ( User=0, Privileged=1 ) */ 
nkeynes@1
   178
#define SR_RB    0x20000000 /* Register bank (priviledged mode only) */
nkeynes@1
   179
#define SR_BL    0x10000000 /* Exception/interupt block (1 = masked) */
nkeynes@1
   180
#define SR_FD    0x00008000 /* FPU disable */
nkeynes@1
   181
#define SR_M     0x00000200
nkeynes@1
   182
#define SR_Q     0x00000100
nkeynes@1
   183
#define SR_IMASK 0x000000F0 /* Interrupt mask level */
nkeynes@1
   184
#define SR_S     0x00000002 /* Saturation operation for MAC instructions */
nkeynes@1
   185
#define SR_T     0x00000001 /* True/false or carry/borrow */
nkeynes@1
   186
#define SR_MASK  0x700083F3
nkeynes@1
   187
#define SR_MQSTMASK 0xFFFFFCFC /* Mask to clear the flags we're keeping separately */
nkeynes@1
   188
nkeynes@1
   189
#define IS_SH4_PRIVMODE() (sh4r.sr&SR_MD)
nkeynes@1
   190
#define SH4_INTMASK() ((sh4r.sr&SR_IMASK)>>4)
nkeynes@265
   191
#define SH4_EVENT_PENDING() (sh4r.event_pending <= sh4r.slice_cycle && !sh4r.in_delay_slot)
nkeynes@1
   192
nkeynes@1
   193
#define FPSCR_FR     0x00200000 /* FPU register bank */
nkeynes@1
   194
#define FPSCR_SZ     0x00100000 /* FPU transfer size (0=32 bits, 1=64 bits) */
nkeynes@1
   195
#define FPSCR_PR     0x00080000 /* Precision (0=32 bites, 1=64 bits) */
nkeynes@1
   196
#define FPSCR_DN     0x00040000 /* Denormalization mode (1 = treat as 0) */
nkeynes@1
   197
#define FPSCR_CAUSE  0x0003F000
nkeynes@1
   198
#define FPSCR_ENABLE 0x00000F80
nkeynes@1
   199
#define FPSCR_FLAG   0x0000007C
nkeynes@1
   200
#define FPSCR_RM     0x00000003 /* Rounding mode (0=nearest, 1=to zero) */
nkeynes@1
   201
nkeynes@1
   202
#define IS_FPU_DOUBLEPREC() (sh4r.fpscr&FPSCR_PR)
nkeynes@1
   203
#define IS_FPU_DOUBLESIZE() (sh4r.fpscr&FPSCR_SZ)
nkeynes@1
   204
#define IS_FPU_ENABLED() ((sh4r.sr&SR_FD)==0)
nkeynes@1
   205
nkeynes@374
   206
#define FR(x) sh4r.fr_bank[(x)^1]
nkeynes@374
   207
#define DRF(x) ((double *)sh4r.fr_bank)[x]
nkeynes@84
   208
#define XF(x) sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21][(x)^1]
nkeynes@95
   209
#define XDR(x) ((double *)(sh4r.fr[((~sh4r.fpscr)&FPSCR_FR)>>21]))[x]
nkeynes@95
   210
#define DRb(x,b) ((double *)(sh4r.fr[((b ? (~sh4r.fpscr) : sh4r.fpscr)&FPSCR_FR)>>21]))[x]
nkeynes@359
   211
#define DR(x) DRb((x>>1), (x&1))
nkeynes@359
   212
#define FPULf   *((float *)&sh4r.fpul)
nkeynes@359
   213
#define FPULi    (sh4r.fpul)
nkeynes@359
   214
nkeynes@2
   215
#define SH4_WRITE_STORE_QUEUE(addr,val) sh4r.store_queue[(addr>>2)&0xF] = val;
nkeynes@1
   216
nkeynes@1
   217
#ifdef __cplusplus
nkeynes@1
   218
}
nkeynes@1
   219
#endif
nkeynes@1
   220
#endif
nkeynes@359
   221
.