2023-03-11 00:44:57 +00:00
|
|
|
#ifndef VB_H_
|
|
|
|
#define VB_H_
|
2021-08-30 02:14:06 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef VBAPI
|
|
|
|
#define VBAPI extern
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Header includes */
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/**************************** 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
/********************************* Constants *********************************/
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* 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
|
2021-08-30 02:14:06 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* System registers */
|
2021-08-30 02:14:06 +00:00
|
|
|
#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
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Other registers */
|
|
|
|
#define VB_PC 0
|
2021-09-02 00:16:22 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Callbacks */
|
2021-09-30 17:33:55 +00:00
|
|
|
#define VB_ONEXCEPTION 0
|
|
|
|
#define VB_ONEXECUTE 1
|
|
|
|
#define VB_ONFETCH 2
|
|
|
|
#define VB_ONREAD 3
|
|
|
|
#define VB_ONWRITE 4
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************** Types ***********************************/
|
|
|
|
|
2021-09-19 01:31:40 +00:00
|
|
|
/* Forward references */
|
2021-08-30 02:14:06 +00:00
|
|
|
typedef struct VB VB;
|
2021-09-19 01:31:40 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Memory access descriptor */
|
2021-09-19 01:31:40 +00:00
|
|
|
typedef struct {
|
2023-03-11 00:44:57 +00:00
|
|
|
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;
|
2021-09-19 01:31:40 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Exception descriptor */
|
2021-09-19 01:31:40 +00:00
|
|
|
typedef struct {
|
2023-03-11 00:44:57 +00:00
|
|
|
uint32_t address; /* Memory address of handler routine */
|
|
|
|
uint16_t code; /* Cause code */
|
|
|
|
uint8_t cancel; /* Set to cancel the exception */
|
|
|
|
} VBException;
|
2021-09-19 01:31:40 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Instruction descriptor */
|
|
|
|
typedef struct {
|
|
|
|
uint32_t address; /* Memory address */
|
|
|
|
uint16_t code[2]; /* Fetched code units */
|
|
|
|
uint8_t size; /* Size in halfwords */
|
|
|
|
} VBInstruction;
|
2021-09-19 01:31:40 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Breakpoint handlers */
|
|
|
|
typedef int (*VBExceptionProc)(VB *, VBException *);
|
|
|
|
typedef int (*VBExecuteProc )(VB *, VBInstruction *);
|
|
|
|
typedef int (*VBFetchProc )(VB *, int, VBAccess *);
|
|
|
|
typedef int (*VBMemoryProc )(VB *, VBAccess *);
|
2021-09-19 01:31:40 +00:00
|
|
|
|
|
|
|
/* Simulation state */
|
2021-08-30 02:14:06 +00:00
|
|
|
struct VB {
|
|
|
|
|
|
|
|
/* Game pak */
|
|
|
|
struct {
|
2023-03-11 00:44:57 +00:00
|
|
|
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 */
|
2021-08-30 02:14:06 +00:00
|
|
|
} cart;
|
|
|
|
|
|
|
|
/* CPU */
|
|
|
|
struct {
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Main registers */
|
|
|
|
int32_t program[32]; /* Program registers */
|
|
|
|
uint32_t pc; /* Program counter */
|
2021-08-30 02:14:06 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* 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 */
|
2021-08-30 02:14:06 +00:00
|
|
|
|
|
|
|
/* Exception cause register */
|
|
|
|
struct {
|
|
|
|
uint16_t eicc; /* Exception/interrupt cause code */
|
2023-03-11 00:44:57 +00:00
|
|
|
uint16_t fecc; /* Duplexed exception cause code */
|
2021-08-30 02:14:06 +00:00
|
|
|
} ecr;
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Cache control word */
|
|
|
|
struct {
|
|
|
|
uint8_t ice; /* Instruction cache enable */
|
|
|
|
} chcw;
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
/* Program status word */
|
|
|
|
struct {
|
2023-03-11 00:44:57 +00:00
|
|
|
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 */
|
2021-08-30 02:14:06 +00:00
|
|
|
} psw;
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* 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 fp_flags; /* 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 */
|
2021-08-30 02:14:06 +00:00
|
|
|
} cpu;
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Breakpoint hooks */
|
|
|
|
VBExceptionProc onException; /* Exception raised */
|
|
|
|
VBExecuteProc onExecute; /* Instruction execute */
|
|
|
|
VBFetchProc onFetch; /* Bus read (fetch) */
|
|
|
|
VBMemoryProc onRead; /* Bus read (execute) */
|
|
|
|
VBMemoryProc onWrite; /* Bus write */
|
2021-09-19 01:31:40 +00:00
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/* Other state */
|
|
|
|
uint8_t wram[0x10000]; /* System memory */
|
2021-08-30 02:14:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
/************************************ API ************************************/
|
2022-04-15 01:51:09 +00:00
|
|
|
|
2023-03-11 01:00:45 +00:00
|
|
|
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);
|
2021-08-30 02:14:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-11 00:44:57 +00:00
|
|
|
#endif /* VB_H_ */
|