pvbemu/core/vb.h

222 lines
6.5 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>
/**************************** Compilation Control ****************************/
/*
VB_LITTLE_ENDIAN
Accelerates simulated memory reads and writes on hosts with little-endian
byte ordering. If left undefined, a generic implementation is used instead.
*/
/*
VB_SIGNED_PROPAGATE
Accelerates sign extension by assuming the >> operator propagates the sign
bit for signed types and does not for unsigned types. If left undefined, a
generic implementation is used instead.
*/
/********************************* Constants *********************************/
/* Data types */
#define VB_S8 0
#define VB_U8 1
#define VB_S16 2
#define VB_U16 3
#define VB_S32 4
/* Register types */
#define VB_PROGRAM 0
#define VB_SYSTEM 1
#define VB_OTHER 2
/* System registers */
#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
/* Other registers */
#define VB_PC 0
/* Callbacks */
#define VB_ONEXCEPTION 0
#define VB_ONEXECUTE 1
#define VB_ONFETCH 2
#define VB_ONREAD 3
#define VB_ONWRITE 4
/*********************************** Types ***********************************/
/* Forward references */
typedef struct VB VB;
/* Memory access descriptor */
typedef struct {
uint32_t address; /* Memory address */
uint32_t clocks; /* Number of clocks taken */
int32_t value; /* Data loaded/to store */
uint8_t type; /* Data type */
} VBAccess;
/* Exception descriptor */
typedef struct {
uint32_t address; /* Memory address of handler routine */
uint16_t code; /* Cause code */
uint8_t cancel; /* Set to cancel the exception */
} VBException;
/* Instruction descriptor */
typedef struct {
uint32_t address; /* Memory address */
uint16_t code[2]; /* Fetched code units */
uint8_t size; /* Size in halfwords */
} VBInstruction;
/* Breakpoint handlers */
typedef int (*VBExceptionProc)(VB *, VBException *);
typedef int (*VBExecuteProc )(VB *, VBInstruction *);
typedef int (*VBFetchProc )(VB *, int, VBAccess *);
typedef int (*VBMemoryProc )(VB *, VBAccess *);
/* Simulation state */
struct VB {
/* Game pak */
struct {
uint8_t *ram; /* (S)RAM data */
uint8_t *rom; /* ROM data */
uint32_t ramSize; /* Size of RAM in bytes */
uint32_t romSize; /* Size of ROM in bytes */
} cart;
/* CPU */
struct {
/* Main registers */
int32_t program[32]; /* Program registers */
uint32_t pc; /* Program counter */
/* System registers */
uint32_t adtre; /* Address trap target address */
uint32_t eipc; /* Exception return PC */
uint32_t eipsw; /* Exception return PSW */
uint32_t fepc; /* Duplexed exception return PC */
uint32_t fepsw; /* Duplexed exception return PSW */
uint32_t sr29; /* System register 29 */
uint32_t sr31; /* System register 31 */
/* Exception cause register */
struct {
uint16_t eicc; /* Exception/interrupt cause code */
uint16_t fecc; /* Duplexed exception cause code */
} ecr;
/* Cache control word */
struct {
uint8_t ice; /* Instruction cache enable */
} chcw;
/* Program status word */
struct {
uint8_t ae; /* Address trap enable */
uint8_t cy; /* Carry */
uint8_t ep; /* Exception pending */
uint8_t fiv; /* Floating-point invalid operation */
uint8_t fov; /* Floating-point overflow */
uint8_t fpr; /* Floating point precision degradation */
uint8_t fro; /* Floating-point reserved operand */
uint8_t fud; /* Floating-point underflow */
uint8_t fzd; /* Floating-point zero division */
uint8_t i; /* Interrupt level */
uint8_t id; /* Interrupt disable */
uint8_t np; /* Duplexed exception pending */
uint8_t ov; /* Overflow */
uint8_t s; /* Sign */
uint8_t z; /* Zero */
} psw;
/* Instruction */
struct {
int32_t aux[9]; /* Auxiliary storage */
void *def; /* Operation descriptor */
uint16_t code[2]; /* Fetched code units */
uint8_t size; /* Size in bytes */
} inst;
/* Other state */
uint32_t clocks; /* Clocks until next activity */
uint32_t fpFlags; /* Floating-point exception flags */
uint16_t exception; /* Current exception cause code */
uint8_t irq[5]; /* Interrupt requests */
uint8_t bitstring; /* Processing a bit string instruction */
uint8_t stage; /* Pipeline stage handler */
uint8_t step; /* General processing step index */
} cpu;
/* Breakpoint hooks */
VBExceptionProc onException; /* Exception raised */
VBExecuteProc onExecute; /* Instruction execute */
VBFetchProc onFetch; /* Bus read (fetch) */
VBMemoryProc onRead; /* Bus read (execute) */
VBMemoryProc onWrite; /* Bus write */
/* Other state */
uint8_t wram[0x10000]; /* System memory */
};
/************************************ API ************************************/
VBAPI int vbEmulate (VB *sim, uint32_t *clocks);
VBAPI int vbEmulateEx (VB **sims, int count, uint32_t *clocks);
VBAPI void* vbGetCallback(VB *sim, int id);
VBAPI int32_t vbGetRegister(VB *sim, int type, int id);
VBAPI uint8_t* vbGetROM (VB *sim, uint32_t *size);
VBAPI uint8_t* vbGetSRAM (VB *sim, uint32_t *size);
VBAPI void vbInit (VB *sim);
VBAPI int32_t vbRead (VB *sim, uint32_t address, int type);
VBAPI void vbReadEx (VB *sim, uint32_t address, uint8_t *buffer, uint32_t length);
VBAPI void vbReset (VB *sim);
VBAPI void* vbSetCallback(VB *sim, int id, void *proc);
VBAPI int32_t vbSetRegister(VB *sim, int type, int id, int32_t value);
VBAPI int vbSetROM (VB *sim, uint8_t *data, uint32_t size);
VBAPI int vbSetSRAM (VB *sim, uint8_t *data, uint32_t size);
VBAPI void vbWrite (VB *sim, uint32_t address, int type, int32_t value);
VBAPI void vbWriteEx (VB *sim, uint32_t address, uint8_t *buffer, uint32_t length);
#ifdef __cplusplus
}
#endif
#endif /* VB_H_ */