Skip to content

Commit

Permalink
Merge pull request #4 from ShutOstrich/master
Browse files Browse the repository at this point in the history
Update to mGBA 0.10.2
  • Loading branch information
clobber authored Apr 30, 2023
2 parents 7faa943 + 575687c commit 8e956c9
Show file tree
Hide file tree
Showing 27 changed files with 329 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>0.10.0</string>
<string>0.10.2</string>
<key>NSPrincipalClass</key>
<string>OEGameCoreController</string>
<key>OEGameCoreClass</key>
Expand Down
3 changes: 3 additions & 0 deletions src/core/cache-set.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ void mCacheSetDeinit(struct mCacheSet* cache) {
for (i = 0; i < mMapCacheSetSize(&cache->maps); ++i) {
mMapCacheDeinit(mMapCacheSetGetPointer(&cache->maps, i));
}
mMapCacheSetDeinit(&cache->maps);
for (i = 0; i < mBitmapCacheSetSize(&cache->bitmaps); ++i) {
mBitmapCacheDeinit(mBitmapCacheSetGetPointer(&cache->bitmaps, i));
}
mBitmapCacheSetDeinit(&cache->bitmaps);
for (i = 0; i < mTileCacheSetSize(&cache->tiles); ++i) {
mTileCacheDeinit(mTileCacheSetGetPointer(&cache->tiles, i));
}
mTileCacheSetDeinit(&cache->tiles);
}

void mCacheSetAssignVRAM(struct mCacheSet* cache, void* vram) {
Expand Down
11 changes: 8 additions & 3 deletions src/core/cheats.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,10 @@ bool mCheatParseEZFChtFile(struct mCheatDevice* device, struct VFile* vf) {
return false;
}
char* name = gbkToUtf8(&cheat[1], end - cheat - 1);
strncpy(cheatName, name, sizeof(cheatName) - 1);
free(name);
if (name) {
strncpy(cheatName, name, sizeof(cheatName) - 1);
free(name);
}
cheatNameLength = strlen(cheatName);
continue;
}
Expand All @@ -501,7 +503,10 @@ bool mCheatParseEZFChtFile(struct mCheatDevice* device, struct VFile* vf) {
}
if (strncmp(cheat, "ON", eq - cheat) != 0) {
char* subname = gbkToUtf8(cheat, eq - cheat);
snprintf(&cheatName[cheatNameLength], sizeof(cheatName) - cheatNameLength - 1, ": %s", subname);
if (subname) {
snprintf(&cheatName[cheatNameLength], sizeof(cheatName) - cheatNameLength - 1, ": %s", subname);
free(subname);
}
}
set = device->createSet(device, cheatName);
set->enabled = false;
Expand Down
6 changes: 3 additions & 3 deletions src/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ void mCoreConfigDeinit(struct mCoreConfig* config) {

#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
bool mCoreConfigLoad(struct mCoreConfig* config) {
char path[PATH_MAX];
char path[PATH_MAX + 1];
mCoreConfigDirectory(path, PATH_MAX);
strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
return mCoreConfigLoadPath(config, path);
}

bool mCoreConfigSave(const struct mCoreConfig* config) {
char path[PATH_MAX];
char path[PATH_MAX + 1];
mCoreConfigDirectory(path, PATH_MAX);
strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
return mCoreConfigSavePath(config, path);
Expand Down Expand Up @@ -304,7 +304,7 @@ void mCoreConfigPortablePath(char* out, size_t outLength) {
CFRelease(suburl);
}
#endif
strncat(out, PATH_SEP "portable.ini", outLength - strlen(out));
strncat(out, PATH_SEP "portable.ini", outLength - strlen(out) - 1);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const char* mLogCategoryName(int category) {
}

const char* mLogCategoryId(int category) {
if (category < MAX_CATEGORY) {
if (category >= 0 && category < MAX_CATEGORY) {
return _categoryIds[category];
}
return NULL;
Expand Down Expand Up @@ -88,6 +88,7 @@ void mLogExplicit(struct mLogger* context, int category, enum mLogLevel level, c
if (!context->filter || mLogFilterTest(context->filter, category, level)) {
context->log(context, category, level, format, args);
}
va_end(args);
}

void mLogFilterInit(struct mLogFilter* filter) {
Expand Down
13 changes: 12 additions & 1 deletion src/core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@ static void _waitOnInterrupt(struct mCoreThreadInternal* threadContext) {
}

static void _pokeRequest(struct mCoreThreadInternal* threadContext) {
if (threadContext->state == mTHREAD_RUNNING || threadContext->state == mTHREAD_PAUSED) {
switch (threadContext->state) {
case mTHREAD_RUNNING:
case mTHREAD_PAUSED:
case mTHREAD_CRASHED:
threadContext->state = mTHREAD_REQUEST;
break;
case mTHREAD_INITIALIZED:
case mTHREAD_REQUEST:
case mTHREAD_INTERRUPTED:
case mTHREAD_INTERRUPTING:
case mTHREAD_EXITING:
case mTHREAD_SHUTDOWN:
break;
}
}

Expand Down
42 changes: 35 additions & 7 deletions src/gb/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ void GBAudioReset(struct GBAudio* audio) {
}

void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
if (samples > BLIP_BUFFER_SIZE / 2) {
samples = BLIP_BUFFER_SIZE / 2;
}
mCoreSyncLockAudio(audio->p->sync);
audio->samples = samples;
blip_clear(audio->left);
Expand Down Expand Up @@ -269,6 +272,29 @@ void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
GBAudioRun(audio, mTimingCurrentTime(audio->timing), 0x4);
audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);

audio->ch3.sample = audio->ch3.wavedata8[audio->ch3.window >> 1];
if (!(audio->ch3.window & 1)) {
audio->ch3.sample >>= 4;
}
audio->ch3.sample &= 0xF;
int volume;
switch (audio->ch3.volume) {
case 0:
volume = 4;
break;
case 1:
volume = 0;
break;
case 2:
volume = 1;
break;
default:
case 3:
volume = 2;
break;
}
audio->ch3.sample >>= volume;
}

void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
Expand Down Expand Up @@ -377,13 +403,13 @@ void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
}

void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
GBAudioRun(audio, mTimingCurrentTime(audio->timing), 0x2);
GBAudioRun(audio, mTimingCurrentTime(audio->timing), 0xF);
audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
}

void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
GBAudioRun(audio, mTimingCurrentTime(audio->timing), 0x2);
GBAudioRun(audio, mTimingCurrentTime(audio->timing), 0xF);
audio->ch1Right = GBRegisterNR51GetCh1Right(value);
audio->ch2Right = GBRegisterNR51GetCh2Right(value);
audio->ch3Right = GBRegisterNR51GetCh3Right(value);
Expand Down Expand Up @@ -476,11 +502,11 @@ void GBAudioRun(struct GBAudio* audio, int32_t timestamp, int channels) {
if (!audio->enable) {
return;
}
if (audio->p && channels != 0xF && timestamp - audio->lastSample > (int) (SAMPLE_INTERVAL * audio->timingFactor)) {
if (audio->p && channels != 0x1F && timestamp - audio->lastSample > (int) (SAMPLE_INTERVAL * audio->timingFactor)) {
GBAudioSample(audio, timestamp);
}

if (audio->playingCh1 && (channels & 0x1)) {
if (audio->playingCh1 && (channels & 0x1) && audio->ch1.envelope.dead != 2) {
int period = 4 * (2048 - audio->ch1.control.frequency) * audio->timingFactor;
int32_t diff = timestamp - audio->ch1.lastUpdate;
if (diff >= period) {
Expand All @@ -490,7 +516,7 @@ void GBAudioRun(struct GBAudio* audio, int32_t timestamp, int channels) {
_updateSquareSample(&audio->ch1);
}
}
if (audio->playingCh2 && (channels & 0x2)) {
if (audio->playingCh2 && (channels & 0x2) && audio->ch2.envelope.dead != 2) {
int period = 4 * (2048 - audio->ch2.control.frequency) * audio->timingFactor;
int32_t diff = timestamp - audio->ch2.lastUpdate;
if (diff >= period) {
Expand Down Expand Up @@ -756,7 +782,7 @@ void GBAudioSample(struct GBAudio* audio, int32_t timestamp) {
for (sample = audio->sampleIndex; timestamp >= interval && sample < GB_MAX_SAMPLES; ++sample, timestamp -= interval) {
int16_t sampleLeft = 0;
int16_t sampleRight = 0;
GBAudioRun(audio, sample * interval + audio->lastSample, 0xF);
GBAudioRun(audio, sample * interval + audio->lastSample, 0x1F);
GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
sampleLeft = (sampleLeft * audio->masterVolume * 6) >> 7;
sampleRight = (sampleRight * audio->masterVolume * 6) >> 7;
Expand Down Expand Up @@ -863,7 +889,7 @@ bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudi
envelope->currentVolume &= 0xF;
}
_updateEnvelopeDead(envelope);
return (envelope->initialVolume || envelope->direction) && envelope->dead != 2;
return envelope->initialVolume || envelope->direction;
}

static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
Expand Down Expand Up @@ -1066,6 +1092,8 @@ void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGSt
audio->ch4.lastEvent = currentTime + (when & (cycles - 1)) - cycles;
}
}
audio->ch4.nSamples = 0;
audio->ch4.samples = 0;
}

void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state) {
Expand Down
6 changes: 3 additions & 3 deletions src/gb/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,16 @@ static void _GBCoreReset(struct mCore* core) {
switch (gb->model) {
case GB_MODEL_DMG:
case GB_MODEL_MGB: // TODO
strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path) - 1);
break;
case GB_MODEL_SGB:
case GB_MODEL_SGB2: // TODO
strncat(path, PATH_SEP "sgb_bios.bin", PATH_MAX - strlen(path));
strncat(path, PATH_SEP "sgb_bios.bin", PATH_MAX - strlen(path) - 1);
break;
case GB_MODEL_CGB:
case GB_MODEL_AGB:
case GB_MODEL_SCGB:
strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path) - 1);
break;
default:
break;
Expand Down
25 changes: 23 additions & 2 deletions src/gb/gb.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ void GBUnloadROM(struct GB* gb) {

if (gb->romVf) {
#ifndef FIXED_ROM_BUFFER
gb->romVf->unmap(gb->romVf, gb->memory.rom, gb->pristineRomSize);
if (gb->isPristine && gb->memory.rom) {
gb->romVf->unmap(gb->romVf, gb->memory.rom, gb->pristineRomSize);
}
#endif
gb->romVf->close(gb->romVf);
gb->romVf = NULL;
Expand Down Expand Up @@ -596,13 +598,13 @@ void GBReset(struct SM83Core* cpu) {
GBVideoReset(&gb->video);
GBTimerReset(&gb->timer);
GBIOReset(gb);
GBAudioReset(&gb->audio);
if (!gb->biosVf && gb->memory.rom) {
GBSkipBIOS(gb);
} else {
mTimingSchedule(&gb->timing, &gb->timer.event, 0);
}

GBAudioReset(&gb->audio);
GBSIOReset(&gb->sio);

cpu->memory.setActiveRegion(cpu, cpu->pc);
Expand Down Expand Up @@ -744,6 +746,25 @@ void GBSkipBIOS(struct GB* gb) {
GBUnmapBIOS(gb);
}

GBIOWrite(gb, GB_REG_NR52, 0xF1);
GBIOWrite(gb, GB_REG_NR14, 0x3F);
GBIOWrite(gb, GB_REG_NR10, 0x80);
GBIOWrite(gb, GB_REG_NR11, 0xBF);
GBIOWrite(gb, GB_REG_NR12, 0xF3);
GBIOWrite(gb, GB_REG_NR13, 0xF3);
GBIOWrite(gb, GB_REG_NR24, 0x3F);
GBIOWrite(gb, GB_REG_NR21, 0x3F);
GBIOWrite(gb, GB_REG_NR22, 0x00);
GBIOWrite(gb, GB_REG_NR34, 0x3F);
GBIOWrite(gb, GB_REG_NR30, 0x7F);
GBIOWrite(gb, GB_REG_NR31, 0xFF);
GBIOWrite(gb, GB_REG_NR32, 0x9F);
GBIOWrite(gb, GB_REG_NR44, 0x3F);
GBIOWrite(gb, GB_REG_NR41, 0xFF);
GBIOWrite(gb, GB_REG_NR42, 0x00);
GBIOWrite(gb, GB_REG_NR43, 0x00);
GBIOWrite(gb, GB_REG_NR50, 0x77);
GBIOWrite(gb, GB_REG_NR51, 0xF3);
GBIOWrite(gb, GB_REG_LCDC, 0x91);
gb->memory.io[GB_REG_BANK] = 0x1;
GBVideoSkipBIOS(&gb->video);
Expand Down
34 changes: 3 additions & 31 deletions src/gb/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,7 @@ void GBIOReset(struct GB* gb) {
GBIOWrite(gb, GB_REG_TMA, 0);
GBIOWrite(gb, GB_REG_TAC, 0);
GBIOWrite(gb, GB_REG_IF, 1);
gb->audio.playingCh1 = false;
gb->audio.playingCh2 = false;
gb->audio.playingCh3 = false;
gb->audio.playingCh4 = false;
GBIOWrite(gb, GB_REG_NR52, 0xF1);
GBIOWrite(gb, GB_REG_NR14, 0x3F);
GBIOWrite(gb, GB_REG_NR10, 0x80);
GBIOWrite(gb, GB_REG_NR11, 0xBF);
GBIOWrite(gb, GB_REG_NR12, 0xF3);
GBIOWrite(gb, GB_REG_NR13, 0xF3);
GBIOWrite(gb, GB_REG_NR24, 0x3F);
GBIOWrite(gb, GB_REG_NR21, 0x3F);
GBIOWrite(gb, GB_REG_NR22, 0x00);
GBIOWrite(gb, GB_REG_NR34, 0x3F);
GBIOWrite(gb, GB_REG_NR30, 0x7F);
GBIOWrite(gb, GB_REG_NR31, 0xFF);
GBIOWrite(gb, GB_REG_NR32, 0x9F);
GBIOWrite(gb, GB_REG_NR44, 0x3F);
GBIOWrite(gb, GB_REG_NR41, 0xFF);
GBIOWrite(gb, GB_REG_NR42, 0x00);
GBIOWrite(gb, GB_REG_NR43, 0x00);
GBIOWrite(gb, GB_REG_NR50, 0x77);
GBIOWrite(gb, GB_REG_NR51, 0xF3);
if (!gb->biosVf) {
GBIOWrite(gb, GB_REG_LCDC, 0x91);
gb->memory.io[GB_REG_BANK] = 1;
} else {
GBIOWrite(gb, GB_REG_LCDC, 0x00);
gb->memory.io[GB_REG_BANK] = 0xFF;
}
GBIOWrite(gb, GB_REG_LCDC, 0x00);
GBIOWrite(gb, GB_REG_SCY, 0x00);
GBIOWrite(gb, GB_REG_SCX, 0x00);
GBIOWrite(gb, GB_REG_LYC, 0x00);
Expand All @@ -203,6 +174,7 @@ void GBIOReset(struct GB* gb) {
}
GBIOWrite(gb, GB_REG_WY, 0x00);
GBIOWrite(gb, GB_REG_WX, 0x00);
gb->memory.io[GB_REG_BANK] = 0xFF;
if (gb->model & GB_MODEL_CGB) {
GBIOWrite(gb, GB_REG_KEY0, 0);
GBIOWrite(gb, GB_REG_JOYP, 0xFF);
Expand Down Expand Up @@ -754,7 +726,7 @@ void GBIODeserialize(struct GB* gb, const struct GBSerializedState* state) {
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_SCX, state->io[GB_REG_SCX]);
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_WY, state->io[GB_REG_WY]);
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_WX, state->io[GB_REG_WX]);
if (gb->model & GB_MODEL_SGB) {
if (gb->model == GB_MODEL_SGB) {
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_BGP, state->io[GB_REG_BGP]);
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_OBP0, state->io[GB_REG_OBP0]);
gb->video.renderer->writeVideoRegister(gb->video.renderer, GB_REG_OBP1, state->io[GB_REG_OBP1]);
Expand Down
Loading

0 comments on commit 8e956c9

Please sign in to comment.