Full front-end rewrite

This commit is contained in:
2022-04-14 20:51:09 -05:00
parent fccb799a9c
commit 3cf006ba13
59 changed files with 9424 additions and 7228 deletions
+16 -16
View File
@@ -64,7 +64,7 @@ static void busWriteMemory(uint8_t *mem, int type, int32_t value) {
/***************************** Module Functions ******************************/
/* Read a data unit from the bus */
static int32_t busRead(VB *emu, uint32_t address, int type, int debug) {
static int32_t busRead(VB *sim, uint32_t address, int type, int debug) {
/* Force address alignment */
address &= ~((uint32_t) TYPE_SIZES[type] - 1);
@@ -77,20 +77,20 @@ static int32_t busRead(VB *emu, uint32_t address, int type, int debug) {
case 3 : return 0; /* Unmapped */
case 4 : return 0; /* Game pak expansion */
case 5 : return /* WRAM */
busReadMemory(&emu->wram[address & 0xFFFF], type);
case 6 : return emu->cart.sram == NULL ? 0 : /* Game pak RAM */
busReadMemory(&emu->cart.sram
[address & (emu->cart.sramSize - 1)], type);
default: return emu->cart.rom == NULL ? 0 : /* Game pak ROM */
busReadMemory(&emu->cart.rom
[address & (emu->cart.romSize - 1)], type);
busReadMemory(&sim->wram[address & 0xFFFF], type);
case 6 : return sim->cart.sram == NULL ? 0 : /* Game pak RAM */
busReadMemory(&sim->cart.sram
[address & (sim->cart.sramSize - 1)], type);
default: return sim->cart.rom == NULL ? 0 : /* Game pak ROM */
busReadMemory(&sim->cart.rom
[address & (sim->cart.romSize - 1)], type);
}
}
/* Write a data unit to the bus */
static void busWrite(
VB *emu, uint32_t address, int type, uint32_t value, int debug) {
VB *sim, uint32_t address, int type, uint32_t value, int debug) {
/* Force address alignment */
address &= ~((uint32_t) TYPE_SIZES[type] - 1);
@@ -103,17 +103,17 @@ static void busWrite(
case 3 : return; /* Unmapped */
case 4 : return; /* Game pak expansion */
case 5 : /* WRAM */
busWriteMemory(&emu->wram[address & 0xFFFF], type, value);
busWriteMemory(&sim->wram[address & 0xFFFF], type, value);
return;
case 6 : /* Cartridge RAM */
if (emu->cart.sram != NULL)
busWriteMemory(&emu->cart.sram
[address & (emu->cart.sramSize - 1)], type, value);
if (sim->cart.sram != NULL)
busWriteMemory(&sim->cart.sram
[address & (sim->cart.sramSize - 1)], type, value);
return;
default: /* Cartridge ROM */
if (debug && emu->cart.rom != NULL)
busWriteMemory(&emu->cart.rom
[address & (emu->cart.romSize - 1)], type, value);
if (debug && sim->cart.rom != NULL)
busWriteMemory(&sim->cart.rom
[address & (sim->cart.romSize - 1)], type, value);
}
}
+667 -705
View File
File diff suppressed because it is too large Load Diff
+138 -127
View File
@@ -1,4 +1,4 @@
#define VBAPI
#define VBAPI VB_EXPORT
/* Header includes */
#include <float.h>
@@ -33,15 +33,15 @@ static const uint8_t TYPE_SIZES[] = { 1, 1, 2, 2, 4 };
/***************************** Module Functions ******************************/
/* Process a simulation for some number of clocks */
static int sysEmulate(VB *emu, uint32_t clocks) {
static int sysEmulate(VB *sim, uint32_t clocks) {
int broke;
broke = cpuEmulate(emu, clocks);
broke = cpuEmulate(sim, clocks);
return broke;
}
/* Determine the number of clocks before a break condititon could occur */
static uint32_t sysUntil(VB *emu, uint32_t clocks) {
clocks = cpuUntil(emu, clocks);
static uint32_t sysUntil(VB *sim, uint32_t clocks) {
clocks = cpuUntil(sim, clocks);
return clocks;
}
@@ -49,242 +49,253 @@ static uint32_t sysUntil(VB *emu, uint32_t clocks) {
/******************************* API Functions *******************************/
/* Associate two simulations as peers */
void vbConnect(VB *emu1, VB *emu2) {
/* Associate two simulations as peers, or remove an association */
void vbConnect(VB *a, VB *b) {
/* Disconnect */
if (b == NULL) {
if (a->peer != NULL)
a->peer->peer = NULL;
a->peer = NULL;
return;
}
/* The simulations are already linked */
if (a->peer == b && b->peer == a)
return;
/* Disconnect any existing link associations */
if (emu1->peer != NULL && emu1->peer != emu2)
emu1->peer->peer = NULL;
if (emu2->peer != NULL && emu2->peer != emu1)
emu2->peer->peer = NULL;
if (a->peer != NULL && a->peer != b)
a->peer->peer = NULL;
if (b->peer != NULL && b->peer != a)
b->peer->peer = NULL;
/* Link the two simulations */
emu1->peer = emu2;
emu2->peer = emu1;
a->peer = b;
b->peer = a;
}
/* Disassociate linked peers */
void vbDisconnect(VB *emu) {
if (emu->peer != NULL)
emu->peer->peer = NULL;
emu->peer = NULL;
}
/* Process one or two simulations */
int vbEmulate(VB *emu1, VB *emu2, uint32_t *clocks) {
/* Process one simulation */
int vbEmulate(VB *sim, uint32_t *clocks) {
int broke; /* The simulation requested an application break */
uint32_t until; /* Maximum clocks before a break could happen */
int x; /* Iterator */
/* Processing one simulaiton */
if (emu2 == NULL) {
do {
until = sysUntil (emu1, *clocks);
broke = sysEmulate(emu1, until );
*clocks -= until;
} while (!broke && *clocks > 0);
}
/* Process the simulation until a break condition occurs */
do {
until = *clocks;
until = sysUntil (sim, until);
broke = sysEmulate(sim, until);
*clocks -= until;
} while (!broke && *clocks > 0);
/* Processing two simulations */
else {
do {
until = sysUntil (emu1, *clocks);
until = sysUntil (emu2, until );
broke = sysEmulate(emu1, until );
broke |= sysEmulate(emu2, until );
*clocks -= until;
} while (!broke && *clocks > 0);
}
return broke;
}
/* Process multiple simulations */
int vbEmulateMulti(VB **sims, int count, uint32_t *clocks) {
int broke; /* The simulation requested an application break */
uint32_t until; /* Maximum clocks before a break could happen */
int x; /* Iterator */
/* Process simulations until a break condition occurs */
do {
broke = 0;
until = *clocks;
for (x = 0; x < count; x++)
until = sysUntil (sims[x], until);
for (x = 0; x < count; x++)
broke |= sysEmulate(sims[x], until);
*clocks -= until;
} while (!broke && *clocks > 0);
return broke;
}
/* Retrieve a current breakpoint callback */
void* vbGetCallback(VB *emu, int type) {
void* vbGetCallback(VB *sim, int type) {
/* -Wpedantic ignored for pointer conversion because no alternative */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
switch (type) {
case VB_ONEXCEPTION: return emu->onException;
case VB_ONEXECUTE : return emu->onExecute;
case VB_ONFETCH : return emu->onFetch;
case VB_ONREAD : return emu->onRead;
case VB_ONWRITE : return emu->onWrite;
case VB_ONEXCEPTION: return sim->onException;
case VB_ONEXECUTE : return sim->onExecute;
case VB_ONFETCH : return sim->onFetch;
case VB_ONREAD : return sim->onRead;
case VB_ONWRITE : return sim->onWrite;
}
#pragma GCC diagnostic pop
return NULL;
}
/* Retrieve the value of PC */
uint32_t vbGetProgramCounter(VB *emu, int type) {
switch (type) {
case VB_PC : return emu->cpu.pc;
case VB_PC_FROM: return emu->cpu.pcFrom;
case VB_PC_TO : return emu->cpu.pcTo;
}
return 0;
uint32_t vbGetProgramCounter(VB *sim) {
return sim->cpu.pc;
}
/* Retrieve the value of a program register */
int32_t vbGetProgramRegister(VB *emu, int id) {
return id < 1 || id > 31 ? 0 : emu->cpu.program[id];
int32_t vbGetProgramRegister(VB *sim, int id) {
return id < 1 || id > 31 ? 0 : sim->cpu.program[id];
}
/* Retrieve the ROM buffer */
void* vbGetROM(VB *emu, uint32_t *size) {
void* vbGetROM(VB *sim, uint32_t *size) {
if (size != NULL)
*size = emu->cart.romSize;
return emu->cart.rom;
*size = sim->cart.romSize;
return sim->cart.rom;
}
/* Retrieve the SRAM buffer */
void* vbGetSRAM(VB *emu, uint32_t *size) {
void* vbGetSRAM(VB *sim, uint32_t *size) {
if (size != NULL)
*size = emu->cart.sramSize;
return emu->cart.sram;
*size = sim->cart.sramSize;
return sim->cart.sram;
}
/* Retrieve the value of a system register */
uint32_t vbGetSystemRegister(VB *emu, int id) {
uint32_t vbGetSystemRegister(VB *sim, int id) {
switch (id) {
case VB_ADTRE: return emu->cpu.adtre;
case VB_CHCW : return emu->cpu.chcw.ice << 1;
case VB_EIPC : return emu->cpu.eipc;
case VB_EIPSW: return emu->cpu.eipsw;
case VB_FEPC : return emu->cpu.fepc;
case VB_FEPSW: return emu->cpu.fepsw;
case VB_ADTRE: return sim->cpu.adtre;
case VB_CHCW : return sim->cpu.chcw.ice << 1;
case VB_EIPC : return sim->cpu.eipc;
case VB_EIPSW: return sim->cpu.eipsw;
case VB_FEPC : return sim->cpu.fepc;
case VB_FEPSW: return sim->cpu.fepsw;
case VB_PIR : return 0x00005346;
case VB_TKCW : return 0x000000E0;
case 29 : return emu->cpu.sr29;
case 29 : return sim->cpu.sr29;
case 30 : return 0x00000004;
case 31 : return emu->cpu.sr31;
case 31 : return sim->cpu.sr31;
case VB_ECR : return
(uint32_t) emu->cpu.ecr.fecc << 16 | emu->cpu.ecr.eicc;
(uint32_t) sim->cpu.ecr.fecc << 16 | sim->cpu.ecr.eicc;
case VB_PSW : return
(uint32_t) emu->cpu.psw.i << 16 |
(uint32_t) emu->cpu.psw.np << 15 |
(uint32_t) emu->cpu.psw.ep << 14 |
(uint32_t) emu->cpu.psw.ae << 13 |
(uint32_t) emu->cpu.psw.id << 12 |
(uint32_t) emu->cpu.psw.fro << 9 |
(uint32_t) emu->cpu.psw.fiv << 8 |
(uint32_t) emu->cpu.psw.fzd << 7 |
(uint32_t) emu->cpu.psw.fov << 6 |
(uint32_t) emu->cpu.psw.fud << 5 |
(uint32_t) emu->cpu.psw.fpr << 4 |
(uint32_t) emu->cpu.psw.cy << 3 |
(uint32_t) emu->cpu.psw.ov << 2 |
(uint32_t) emu->cpu.psw.s << 1 |
(uint32_t) emu->cpu.psw.z
(uint32_t) sim->cpu.psw.i << 16 |
(uint32_t) sim->cpu.psw.np << 15 |
(uint32_t) sim->cpu.psw.ep << 14 |
(uint32_t) sim->cpu.psw.ae << 13 |
(uint32_t) sim->cpu.psw.id << 12 |
(uint32_t) sim->cpu.psw.fro << 9 |
(uint32_t) sim->cpu.psw.fiv << 8 |
(uint32_t) sim->cpu.psw.fzd << 7 |
(uint32_t) sim->cpu.psw.fov << 6 |
(uint32_t) sim->cpu.psw.fud << 5 |
(uint32_t) sim->cpu.psw.fpr << 4 |
(uint32_t) sim->cpu.psw.cy << 3 |
(uint32_t) sim->cpu.psw.ov << 2 |
(uint32_t) sim->cpu.psw.s << 1 |
(uint32_t) sim->cpu.psw.z
;
}
return 0;
}
/* Prepare a simulation state instance for use */
void vbInit(VB *emu) {
void vbInit(VB *sim) {
/* Breakpoint callbacks */
emu->onException = NULL;
emu->onExecute = NULL;
emu->onFetch = NULL;
emu->onRead = NULL;
emu->onWrite = NULL;
sim->onException = NULL;
sim->onExecute = NULL;
sim->onFetch = NULL;
sim->onRead = NULL;
sim->onWrite = NULL;
/* System */
emu->peer = NULL;
sim->peer = NULL;
/* Cartridge */
emu->cart.rom = NULL;
emu->cart.romSize = 0;
emu->cart.sram = NULL;
emu->cart.sramSize = 0;
sim->cart.rom = NULL;
sim->cart.romSize = 0;
sim->cart.sram = NULL;
sim->cart.sramSize = 0;
/* Everything else */
vbReset(emu);
vbReset(sim);
}
/* Read a data unit from the bus */
int32_t vbRead(VB *emu, uint32_t address, int type, int debug) {
int32_t vbRead(VB *sim, uint32_t address, int type, int debug) {
return type < 0 || type >= (int) sizeof TYPE_SIZES ? 0 :
busRead(emu, address, type, debug);
busRead(sim, address, type, debug);
}
/* Simulate a hardware reset */
void vbReset(VB *emu) {
void vbReset(VB *sim) {
uint32_t x; /* Iterator */
/* Subsystem components */
cpuReset(emu);
cpuReset(sim);
/* WRAM (the hardware does not do this) */
for (x = 0; x < 0x10000; x++)
emu->wram[x] = 0x00;
sim->wram[x] = 0x00;
}
/* Specify a breakpoint callback */
void vbSetCallback(VB *emu, int type, void *callback) {
void vbSetCallback(VB *sim, int type, void *callback) {
/* -Wpedantic ignored for pointer conversion because no alternative */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
switch (type) {
case VB_ONEXCEPTION: emu->onException=(VB_EXCEPTIONPROC)callback;break;
case VB_ONEXECUTE : emu->onExecute =(VB_EXECUTEPROC )callback;break;
case VB_ONFETCH : emu->onFetch =(VB_FETCHPROC )callback;break;
case VB_ONREAD : emu->onRead =(VB_READPROC )callback;break;
case VB_ONWRITE : emu->onWrite =(VB_WRITEPROC )callback;break;
case VB_ONEXCEPTION: sim->onException=(VB_EXCEPTIONPROC)callback;break;
case VB_ONEXECUTE : sim->onExecute =(VB_EXECUTEPROC )callback;break;
case VB_ONFETCH : sim->onFetch =(VB_FETCHPROC )callback;break;
case VB_ONREAD : sim->onRead =(VB_READPROC )callback;break;
case VB_ONWRITE : sim->onWrite =(VB_WRITEPROC )callback;break;
}
#pragma GCC diagnostic pop
}
/* Specify a new value for PC */
uint32_t vbSetProgramCounter(VB *emu, uint32_t value) {
uint32_t vbSetProgramCounter(VB *sim, uint32_t value) {
value &= 0xFFFFFFFE;
emu->cpu.causeCode = 0;
emu->cpu.fetch = 0;
emu->cpu.pc = value;
emu->cpu.state = CPU_FETCH;
emu->cpu.substring = 0;
sim->cpu.busWait = 0;
sim->cpu.causeCode = 0;
sim->cpu.clocks = 0;
sim->cpu.fetch = 0;
sim->cpu.pc = value;
sim->cpu.state = CPU_FETCH;
sim->cpu.substring = 0;
return value;
}
/* Specify a new value for a program register */
int32_t vbSetProgramRegister(VB *emu, int id, int32_t value) {
return id < 1 || id > 31 ? 0 : (emu->cpu.program[id] = value);
int32_t vbSetProgramRegister(VB *sim, int id, int32_t value) {
return id < 1 || id > 31 ? 0 : (sim->cpu.program[id] = value);
}
/* Supply a ROM buffer */
int vbSetROM(VB *emu, void *rom, uint32_t size) {
int vbSetROM(VB *sim, void *rom, uint32_t size) {
/* Check the buffer size */
if (size < 1024 || size > 0x1000000 || ((size - 1) & size) != 0)
return 0;
/* Configure the ROM buffer */
emu->cart.rom = (uint8_t *) rom;
emu->cart.romSize = size;
sim->cart.rom = (uint8_t *) rom;
sim->cart.romSize = size;
return 1;
}
/* Supply an SRAM buffer */
int vbSetSRAM(VB *emu, void *sram, uint32_t size) {
int vbSetSRAM(VB *sim, void *sram, uint32_t size) {
/* Check the buffer size */
if (size == 0 || ((size - 1) & size) != 0)
return 0;
/* Configure the SRAM buffer */
emu->cart.sram = (uint8_t *) sram;
emu->cart.sramSize = size;
sim->cart.sram = (uint8_t *) sram;
sim->cart.sramSize = size;
return 1;
}
/* Specify a new value for a system register */
uint32_t vbSetSystemRegister(VB *emu, int id, uint32_t value) {
return cpuSetSystemRegister(emu, id, value, 1);
uint32_t vbSetSystemRegister(VB *sim, int id, uint32_t value) {
return cpuSetSystemRegister(sim, id, value, 1);
}
/* Write a data unit to the bus */
void vbWrite(VB *emu, uint32_t address, int type, int32_t value, int debug) {
void vbWrite(VB *sim, uint32_t address, int type, int32_t value, int debug) {
if (type >= 0 && type < (int32_t) sizeof TYPE_SIZES)
busWrite(emu, address, type, value, debug);
busWrite(sim, address, type, value, debug);
}
+20 -28
View File
@@ -141,14 +141,6 @@ struct VB {
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 */
@@ -175,27 +167,27 @@ struct VB {
/**************************** Function Prototypes ****************************/
/******************************* API Commands ********************************/
VBAPI void vbConnect (VB *emu1, VB *emu2);
VBAPI void vbDisconnect (VB *emu);
VBAPI int vbEmulate (VB *emu1, VB *emu2, uint32_t *clocks);
VBAPI void* vbGetCallback (VB *emu, int type);
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 void vbSetCallback (VB *emu, int type, void *callback);
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);
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);