199 lines
7.2 KiB
C
199 lines
7.2 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
|
||
|
||
/* Breakpoint callback types */
|
||
#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 */
|
||
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 */
|
||
|
||
/* Other fields */
|
||
VB_ACCESS access; /* Memory access descriptor */
|
||
VB_INSTRUCTION inst; /* Instruction descriptor */
|
||
uint8_t irq[5]; /* Interrupt request lines */
|
||
uint8_t busWait; /* Memory access counter */
|
||
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 */
|
||
};
|
||
|
||
|
||
|
||
/******************************* API Commands ********************************/
|
||
|
||
VBAPI void vbConnect (VB *sim1, VB *sim2);
|
||
VBAPI int vbEmulate (VB *sim, uint32_t *clocks);
|
||
VBAPI int vbEmulateMulti (VB **sims, int count, uint32_t *clocks);
|
||
VBAPI void* vbGetCallback (VB *sim, int type);
|
||
VBAPI uint32_t vbGetProgramCounter (VB *sim);
|
||
VBAPI int32_t vbGetProgramRegister (VB *sim, int id);
|
||
VBAPI void* vbGetROM (VB *sim, uint32_t *size);
|
||
VBAPI void* vbGetSRAM (VB *sim, uint32_t *size);
|
||
VBAPI uint32_t vbGetSystemRegister (VB *sim, int id);
|
||
VBAPI void vbInit (VB *sim);
|
||
VBAPI int32_t vbRead (VB *sim, uint32_t address, int type, int debug);
|
||
VBAPI void vbReset (VB *sim);
|
||
VBAPI void* vbSetCallback (VB *sim, int type, void *callback);
|
||
VBAPI uint32_t vbSetProgramCounter (VB *sim, uint32_t value);
|
||
VBAPI int32_t vbSetProgramRegister (VB *sim, int id, int32_t value);
|
||
VBAPI int vbSetROM (VB *sim, void *rom, uint32_t size);
|
||
VBAPI int vbSetSRAM (VB *sim, void *sram, uint32_t size);
|
||
VBAPI uint32_t vbSetSystemRegister (VB *sim, int id, uint32_t value);
|
||
VBAPI void vbWrite (VB *sim, uint32_t address, int type, int32_t value, int debug);
|
||
|
||
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif /* __VB_H__ */
|
||
|