pvbemu/core/vb.h

198 lines
6.3 KiB
C

#ifndef __VB_H__
#define __VB_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifndef VBAPI
#define VBAPI extern
#endif
/* Header includes */
#include <stddef.h>
#include <stdint.h>
/********************************* Constants *********************************/
/* Memory access types */
#define VB_CANCEL -1
#define VB_S8 0
#define VB_U8 1
#define VB_S16 2
#define VB_U16 3
#define VB_S32 4
/* System register IDs */
#define VB_ADTRE 25
#define VB_CHCW 24
#define VB_ECR 4
#define VB_EIPC 0
#define VB_EIPSW 1
#define VB_FEPC 2
#define VB_FEPSW 3
#define VB_PIR 6
#define VB_PSW 5
#define VB_TKCW 7
/* PC types */
#define VB_PC 0
#define VB_PC_FROM 1
#define VB_PC_TO 2
/*********************************** Types ***********************************/
/* Forward references */
typedef struct VB VB;
/* Memory access */
typedef struct {
uint32_t address; /* Bus address being accessed */
uint32_t clocks; /* Number of clocks required to complete */
int32_t value; /* Value read (callback's responsibility) or to write */
int8_t type; /* Data type of value */
} VB_ACCESS;
/* CPU instruction */
typedef struct {
/* Public fields */
uint32_t address; /* Bus address */
uint16_t bits[2]; /* Binary instruction code */
uint8_t size; /* Size in bytes of the instruction */
/* Implementation fields */
int32_t aux[2]; /* Auxiliary storage for CAXI and bit strings */
uint8_t id; /* Internal operation ID */
} VB_INSTRUCTION;
/* Breakpoint callbacks */
typedef int (*VB_EXCEPTIONPROC)(VB *, uint16_t);
typedef int (*VB_EXECUTEPROC )(VB *, VB_INSTRUCTION *);
typedef int (*VB_FETCHPROC )(VB *, int, VB_ACCESS *);
typedef int (*VB_READPROC )(VB *, VB_ACCESS *);
typedef int (*VB_WRITEPROC )(VB *, VB_ACCESS *);
/* Simulation state */
struct VB {
/* Game pak */
struct {
uint8_t *rom; /* Active ROM buffer */
uint32_t romSize; /* Size of ROM data */
uint8_t *sram; /* Active SRAM buffer */
uint32_t sramSize; /* Size of SRAM data */
} cart;
/* CPU */
struct {
/* System registers */
uint32_t adtre; /* Address trap register for execution */
uint32_t eipc; /* Exception/Interrupt PC */
uint32_t eipsw; /* Exception/Interrupt PSW */
uint32_t fepc; /* Fatal error PC */
uint32_t fepsw; /* Fatal error PSW */
uint32_t sr29; /* Unknown system register */
uint32_t sr31; /* Unknown system register */
/* Cache control word */
struct {
int8_t ice; /* Instruction cache enable */
} chcw;
/* Exception cause register */
struct {
uint16_t eicc; /* Exception/interrupt cause code */
uint16_t fecc; /* Fatal error cause code */
} ecr;
/* Program status word */
struct {
int8_t ae; /* Address trap enable */
int8_t cy; /* Carry */
int8_t ep; /* Exception pending */
int8_t fiv; /* Floating invalid */
int8_t fov; /* Floating overflow */
int8_t fpr; /* Floating precision */
int8_t fro; /* Floating reserved operand */
int8_t fud; /* Floating underflow */
int8_t fzd; /* Floating zero divide */
int8_t i; /* Interrupt level */
int8_t id; /* Interrupt disable */
int8_t np; /* NMI pending */
int8_t ov; /* Overflow */
int8_t s; /* Sign */
int8_t z; /* Zero */
} psw;
/* Other registers */
uint32_t pc; /* Program counter */
int32_t program[32]; /* program registers */
/* History tracking */
uint32_t eipcFrom; /* Source of most recent jump */
uint32_t eipcTo; /* Destination of most recent jump */
uint32_t fepcFrom; /* Source of most recent jump */
uint32_t fepcTo; /* Destination of most recent jump */
uint32_t pcFrom; /* Source of most recent jump */
uint32_t pcTo; /* Destination of most recent jump */
/* Other fields */
VB_ACCESS access; /* Memory access descriptor */
VB_INSTRUCTION inst; /* Instruction descriptor */
uint8_t irq[5]; /* Interrupt request lines */
uint8_t busWait; /* Waiting on a memory access */
uint16_t causeCode; /* Exception cause code */
uint32_t clocks; /* Clocks until next action */
int16_t fetch; /* Index of fetch unit */
uint8_t state; /* Operations state */
uint8_t substring; /* A bit string operation is in progress */
} cpu;
/* Breakpoint callbacks */
VB_EXCEPTIONPROC onException; /* CPU exception */
VB_EXECUTEPROC onExecute; /* Instruction execute */
VB_FETCHPROC onFetch; /* Instruction fetch */
VB_READPROC onRead; /* Memory read */
VB_WRITEPROC onWrite; /* Memory write */
/* Other fields */
VB *peer; /* Communications peer */
uint8_t wram[0x10000]; /* Main memory */
};
/**************************** Function Prototypes ****************************/
VBAPI void vbConnect (VB *emu1, VB *emu2);
VBAPI void vbDisconnect (VB *emu);
VBAPI int vbEmulate (VB *emu1, VB *emu2, uint32_t *clocks);
VBAPI uint32_t vbGetProgramCounter (VB *emu, int type);
VBAPI int32_t vbGetProgramRegister (VB *emu, int id);
VBAPI void* vbGetROM (VB *emu, uint32_t *size);
VBAPI void* vbGetSRAM (VB *emu, uint32_t *size);
VBAPI uint32_t vbGetSystemRegister (VB *emu, int id);
VBAPI void vbInit (VB *emu);
VBAPI int32_t vbRead (VB *emu, uint32_t address, int type, int debug);
VBAPI void vbReset (VB *emu);
VBAPI uint32_t vbSetProgramCounter (VB *emu, uint32_t value);
VBAPI int32_t vbSetProgramRegister (VB *emu, int id, int32_t value);
VBAPI int vbSetROM (VB *emu, void *rom, uint32_t size);
VBAPI int vbSetSRAM (VB *emu, void *sram, uint32_t size);
VBAPI uint32_t vbSetSystemRegister (VB *emu, int id, uint32_t value);
VBAPI void vbWrite (VB *emu, uint32_t address, int type, int32_t value, int debug);
#ifdef __cplusplus
}
#endif
#endif /* __VB_H__ */