8 Commits
Author SHA1 Message Date
GuyPerfect 08fb65cb77 Fix FRO detection 2024-10-13 21:35:48 -05:00
GuyPerfect 1d6f62e319 Passing the pizza gauntlet 2024-10-13 20:13:20 -05:00
GuyPerfect 2e1cd12829 Implement bit string instructions 2024-10-13 19:14:08 -05:00
GuyPerfect 2273761222 Pre-bit-string adjustments 2024-10-13 16:31:18 -05:00
GuyPerfect bfc2254b9e Implement halt, exception, fatal 2024-10-13 13:06:49 -05:00
GuyPerfect 7c25a4ac93 CPU bug fixes 2024-10-13 10:29:54 -05:00
GuyPerfect 5f563e6cac Implement floating-point instructions 2024-10-11 19:15:27 -05:00
GuyPerfect 2e0af6b109 Implement most CPU instructions 2024-10-11 11:55:24 -05:00
4 changed files with 1654 additions and 405 deletions
+1511 -315
View File
File diff suppressed because it is too large Load Diff
+89 -43
View File
@@ -1,6 +1,7 @@
#ifndef VBAPI
#define VBAPI
#endif
#include <float.h>
#include <vb.h>
@@ -64,15 +65,21 @@ struct VB {
/* Working data */
union {
struct {
uint32_t dest;
uint64_t src;
} bs; /* Arithmetic bit strings */
struct {
uint32_t address;
int32_t value;
} data;
} data; /* Data accesses */
} aux;
/* Other state */
uint32_t clocks; /* Master clocks to wait */
uint16_t code[2]; /* Instruction code units */
uint16_t exception; /* Exception cause code */
int halt; /* CPU is halting */
uint16_t irq; /* Interrupt request lines */
int length; /* Instruction code length */
uint32_t nextPC; /* Address of next instruction */
@@ -83,27 +90,28 @@ struct VB {
/* Other system state */
uint8_t wram[0x10000]; /* System RAM */
/* Application callbacks */
/* Application data */
vbOnException onException; /* CPU exception */
vbOnExecute onExecute; /* CPU instruction execute */
vbOnFetch onFetch; /* CPU instruction fetch */
vbOnRead onRead; /* CPU instruction read */
vbOnWrite onWrite; /* CPU instruction write */
void *tag; /* User data */
};
/***************************** Library Functions *****************************/
/* Determine the lesser of two clocks figures */
static uint32_t MinClocks(uint32_t a, uint32_t b) {
return a < b ? a : b;
}
/* Sign-extend an integer of variable width */
static int32_t SignExtend(int32_t value, int32_t bits) {
#ifndef VB_SIGNED_PROPAGATE
value &= ~((uint32_t) 0xFFFFFFFF << bits);
bits = (int32_t) 1 << (bits - (int32_t) 1);
return (value ^ bits) - bits;
#else
return value << (32 - bits) >> (32 - bits);
#endif
}
@@ -169,19 +177,6 @@ VBAPI int vbEmulateEx(VB **sims, int count, uint32_t *clocks) {
return 0;
}
/* Retrieve a callback handler */
VBAPI void* vbGetCallback(VB *sim, int id) {
switch (id) {
/*case VB_EXCEPTION: return *(void **) &sim->onException;*/
case VB_EXECUTE : return *(void **) &sim->onExecute;
case VB_FETCH : return *(void **) &sim->onFetch;
/*case VB_FRAME : return *(void **) &sim->onFrame;*/
case VB_READ : return *(void **) &sim->onRead;
case VB_WRITE : return *(void **) &sim->onWrite;
}
return NULL;
}
/* Retrieve the game pack RAM buffer */
VBAPI void* vbGetCartRAM(VB *sim, uint32_t *size) {
if (size != NULL)
@@ -196,6 +191,21 @@ VBAPI void* vbGetCartROM(VB *sim, uint32_t *size) {
return sim->cart.rom;
}
/* Retrieve the exception callback handle */
VBAPI vbOnException vbGetExceptionCallback(VB *sim) {
return sim->onException;
}
/* Retrieve the execute callback handle */
VBAPI vbOnExecute vbGetExecuteCallback(VB *sim) {
return sim->onExecute;
}
/* Retrieve the fetch callback handle */
VBAPI vbOnFetch vbGetFetchCallback(VB *sim) {
return sim->onFetch;
}
/* Retrieve the value of the program counter */
VBAPI uint32_t vbGetProgramCounter(VB *sim) {
return sim->cpu.pc;
@@ -206,11 +216,26 @@ VBAPI int32_t vbGetProgramRegister(VB *sim, int index) {
return index < 1 || index > 31 ? 0 : sim->cpu.program[index];
}
/* Retrieve the read callback handle */
VBAPI vbOnRead vbGetReadCallback(VB *sim) {
return sim->onRead;
}
/* Retrieve the value in a system register */
VBAPI uint32_t vbGetSystemRegister(VB *sim, int index) {
return index < 0 || index > 31 ? 0 : cpuGetSystemRegister(sim, index);
}
/* Retrieve a simulation's userdata pointer */
VBAPI void* vbGetUserData(VB *sim) {
return sim->tag;
}
/* Retrieve the write callback handle */
VBAPI vbOnWrite vbGetWriteCallback(VB *sim) {
return sim->onWrite;
}
/* Initialize a simulation instance */
VBAPI VB* vbInit(VB *sim) {
sim->cart.ram = NULL;
@@ -241,6 +266,8 @@ VBAPI VB* vbReset(VB *sim) {
sim->wram[x] = 0x00;
/* CPU (normal) */
sim->cpu.exception = 0;
sim->cpu.halt = 0;
sim->cpu.irq = 0;
sim->cpu.pc = 0xFFFFFFF0;
cpuSetSystemRegister(sim, VB_ECR, 0x0000FFF0, 1);
@@ -267,29 +294,6 @@ VBAPI VB* vbReset(VB *sim) {
return sim;
}
/* Specify a new callback handler */
VBAPI void* vbSetCallback(VB *sim, int id, void *callback) {
void *prev = NULL;
void **target = NULL;
/* Select callback by ID */
switch (id) {
/*case VB_EXCEPTION: target = (void **) &sim->onException; break;*/
case VB_EXECUTE : target = (void **) &sim->onExecute ; break;
case VB_FETCH : target = (void **) &sim->onFetch ; break;
/*case VB_FRAME : target = (void **) &sim->onFrame ; break;*/
case VB_READ : target = (void **) &sim->onRead ; break;
case VB_WRITE : target = (void **) &sim->onWrite ; break;
}
/* Retrieve current state and update new state */
if (target != NULL) {
prev = *target;
*target = callback;
}
return prev;
}
/* Specify a game pak RAM buffer */
VBAPI int vbSetCartRAM(VB *sim, void *sram, uint32_t size) {
if (sram != NULL) {
@@ -312,6 +316,27 @@ VBAPI int vbSetCartROM(VB *sim, void *rom, uint32_t size) {
return 0;
}
/* Specify a new exception callback handle */
VBAPI vbOnException vbSetExceptionCallback(VB *sim, vbOnException callback) {
vbOnException prev = sim->onException;
sim->onException = callback;
return prev;
}
/* Specify a new execute callback handle */
VBAPI vbOnExecute vbSetExecuteCallback(VB *sim, vbOnExecute callback) {
vbOnExecute prev = sim->onExecute;
sim->onExecute = callback;
return prev;
}
/* Specify a new fetch callback handle */
VBAPI vbOnFetch vbSetFetchCallback(VB *sim, vbOnFetch callback) {
vbOnFetch prev = sim->onFetch;
sim->onFetch = callback;
return prev;
}
/* Specify a new value for the program counter */
VBAPI uint32_t vbSetProgramCounter(VB *sim, uint32_t value) {
sim->cpu.operation = CPU_FETCH;
@@ -325,17 +350,38 @@ VBAPI int32_t vbSetProgramRegister(VB *sim, int index, int32_t value) {
return index < 1 || index > 31 ? 0 : (sim->cpu.program[index] = value);
}
/* Specify a new read callback handle */
VBAPI vbOnRead vbSetReadCallback(VB *sim, vbOnRead callback) {
vbOnRead prev = sim->onRead;
sim->onRead = callback;
return prev;
}
/* Specify a new value for a system register */
VBAPI uint32_t vbSetSystemRegister(VB *sim, int index, uint32_t value) {
return index < 0 || index > 31 ? 0 :
cpuSetSystemRegister(sim, index, value, 1);
}
/* Specify a new write callback handle */
VBAPI vbOnWrite vbSetWriteCallback(VB *sim, vbOnWrite callback) {
vbOnWrite prev = sim->onWrite;
sim->onWrite = callback;
return prev;
}
/* Determine the size of a simulation instance */
VBAPI size_t vbSizeOf() {
return sizeof (VB);
}
/* Specify a simulation's userdata pointer */
VBAPI void* vbSetUserData(VB *sim, void *tag) {
void *prev = sim->tag;
sim->tag = tag;
return prev;
}
/* Write a value to the memory bus */
VBAPI int32_t vbWrite(VB *sim, uint32_t address, int type, int32_t value) {
if (type < 0 || type > 4)
+16 -9
View File
@@ -44,11 +44,6 @@ extern "C" {
#define VB_U16 3
#define VB_S32 4
/* CPU modes */
#define VB_ACCURACY 0
#define VB_DEBUG 1
#define VB_WHOLE 2
/*********************************** Types ***********************************/
@@ -57,7 +52,8 @@ extern "C" {
typedef struct VB VB;
/* Callbacks */
typedef int (*vbOnExecute)(VB *sim, uint32_t address, const uint16_t *code, int length);
typedef int (*vbOnException)(VB *sim, uint16_t *cause);
typedef int (*vbOnExecute )(VB *sim, uint32_t address, const uint16_t *code, int length);
typedef int (*vbOnFetch )(VB *sim, int fetch, uint32_t address, int32_t *value, uint32_t *cycles);
typedef int (*vbOnRead )(VB *sim, uint32_t address, int type, int32_t *value, uint32_t *cycles);
typedef int (*vbOnWrite )(VB *sim, uint32_t address, int type, int32_t *value, uint32_t *cycles, int *cancel);
@@ -72,18 +68,29 @@ VBAPI int vbEmulateEx (VB **sims, int count, uint32_t *clocks);
VBAPI void* vbGetCallback (VB *sim, int id);
VBAPI void* vbGetCartRAM (VB *sim, uint32_t *size);
VBAPI void* vbGetCartROM (VB *sim, uint32_t *size);
VBAPI vbOnException vbGetExceptionCallback(VB *sim);
VBAPI vbOnExecute vbGetExecuteCallback (VB *sim);
VBAPI vbOnFetch vbGetFetchCallback (VB *sim);
VBAPI uint32_t vbGetProgramCounter (VB *sim);
VBAPI int32_t vbGetProgramRegister(VB *sim, int index);
VBAPI int32_t vbGetProgramRegister (VB *sim, int index);
VBAPI vbOnRead vbGetReadCallback (VB *sim);
VBAPI uint32_t vbGetSystemRegister (VB *sim, int index);
VBAPI void* vbGetUserData (VB *sim);
VBAPI vbOnWrite vbGetWriteCallback (VB *sim);
VBAPI VB* vbInit (VB *sim);
VBAPI int32_t vbRead (VB *sim, uint32_t address, int type);
VBAPI VB* vbReset (VB *sim);
VBAPI void* vbSetCallback (VB *sim, int index, void *callback);
VBAPI int vbSetCartRAM (VB *sim, void *sram, uint32_t size);
VBAPI int vbSetCartROM (VB *sim, void *rom, uint32_t size);
VBAPI vbOnException vbSetExceptionCallback(VB *sim, vbOnException callback);
VBAPI vbOnExecute vbSetExecuteCallback (VB *sim, vbOnExecute callback);
VBAPI vbOnFetch vbSetFetchCallback (VB *sim, vbOnFetch callback);
VBAPI uint32_t vbSetProgramCounter (VB *sim, uint32_t value);
VBAPI int32_t vbSetProgramRegister(VB *sim, int index, int32_t value);
VBAPI int32_t vbSetProgramRegister (VB *sim, int index, int32_t value);
VBAPI vbOnRead vbSetReadCallback (VB *sim, vbOnRead callback);
VBAPI uint32_t vbSetSystemRegister (VB *sim, int index, uint32_t value);
VBAPI void* vbSetUserData (VB *sim, void *tag);
VBAPI vbOnWrite vbSetWriteCallback (VB *sim, vbOnWrite callback);
VBAPI size_t vbSizeOf ();
VBAPI int32_t vbWrite (VB *sim, uint32_t address, int type, int32_t value);
+2 -2
View File
@@ -41,14 +41,14 @@ core:
# GCC compilation control
@gcc core/vb.c -I core -c -o /dev/null \
-Werror -std=c90 -Wall -Wextra -Wpedantic \
-D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE
-D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE -D VB_DIV_GENERIC
# Clang generic
@emcc core/vb.c -I core -c -o /dev/null \
-Werror -std=c90 -Wall -Wextra -Wpedantic
# Clang compilation control
@emcc core/vb.c -I core -c -o /dev/null \
-Werror -std=c90 -Wall -Wextra -Wpedantic \
-D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE
-D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE -D VB_DIV_GENERIC
.PHONY: wasm
wasm: