shrooms-vb-native/emulation.c

75 lines
1.9 KiB
C
Raw Permalink Normal View History

#include "emulation.h"
#include <stdlib.h>
static int onFrame(VB *sim) {
SimContext *ctx = vbGetUserData(sim);
ctx->hasFrame = true;
return 1;
}
int emuInit(EmulationContext *emu) {
emu->sim = malloc(vbSizeOf());
vbInit(emu->sim);
emu->ctx = malloc(sizeof(SimContext));
2024-11-01 01:47:48 +00:00
emu->ctx->hasFrame = false;
emu->ctx->currentSample = 0;
vbSetSamples(emu->sim, emu->ctx->samples[emu->ctx->currentSample], VB_S16, 834);
vbSetUserData(emu->sim, emu->ctx);
vbSetFrameCallback(emu->sim, &onFrame);
return 0;
}
void emuDestroy(EmulationContext *emu) {
uint8_t *rom = vbGetCartROM(emu->sim, NULL);
SimContext *ctx = vbGetUserData(emu->sim);
if (rom) free(rom);
if (ctx) free(ctx);
free(emu->sim);
}
void emuLoadGame(EmulationContext *emu, uint8_t *rom, uint32_t romSize) {
2024-10-30 03:25:26 +00:00
uint8_t *oldRom = vbGetCartROM(emu->sim, NULL);
if (oldRom) free(oldRom);
vbSetCartROM(emu->sim, rom, romSize);
2024-10-30 03:25:26 +00:00
vbReset(emu->sim);
}
2024-10-30 02:33:48 +00:00
void emuReset(EmulationContext *emu) {
vbReset(emu->sim);
}
2024-11-01 01:56:58 +00:00
bool emuIsGameLoaded(EmulationContext *emu) {
return vbGetCartROM(emu->sim, NULL) != NULL;
}
#define MAX_STEP_CLOCKS 20000000
void emuTick(EmulationContext *emu) {
uint32_t clocks = MAX_STEP_CLOCKS;
vbEmulate(emu->sim, &clocks);
}
bool emuReadPixels(EmulationContext *emu, uint8_t *left, uint8_t *right) {
if (!emu->ctx->hasFrame) {
return false;
}
emu->ctx->hasFrame = false;
vbGetPixels(emu->sim, left, 1, 384, right, 1, 384);
return true;
}
void emuReadSamples(EmulationContext *emu, void **data, uint32_t *bytes) {
uint32_t samplePairs;
2024-11-01 01:47:48 +00:00
*data = vbGetSamples(emu->sim, NULL, NULL, &samplePairs);
*bytes = samplePairs * 4;
emu->ctx->currentSample += 1;
emu->ctx->currentSample %= 2;
2024-11-01 01:47:48 +00:00
vbSetSamples(emu->sim, emu->ctx->samples[emu->ctx->currentSample], VB_S16, 834);
}
void emuSetKeys(EmulationContext *emu, uint16_t keys) {
vbSetKeys(emu->sim, keys);
}