Skip to content

Commit

Permalink
- Always update SRAM on exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Jun 3, 2024
1 parent fc2cd66 commit 83f2a12
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 67 deletions.
3 changes: 0 additions & 3 deletions cube/swiss/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ extern char *dvdDiscTypeStr;
extern int dvdDiscTypeInt;
extern int drive_status;

extern u32 __SYS_SyncSram();
extern u32 __SYS_CheckSram();
extern void __SYS_ReadROM(void *buf,u32 len,u32 offset);
extern void populateDeviceAvailability();
#endif

8 changes: 5 additions & 3 deletions cube/swiss/include/swiss.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ extern dvddrvinfo driveInfo;
extern DiskHeader GCMDisk;

extern void udelay(int s);
extern void __SYS_ReadROM(void *buf,u32 len,u32 offset);
extern s32 DVD_LowGetCoverStatus(void);
extern u32 sdgecko_getAddressingType(s32 drv_no);
extern u32 sdgecko_getDevice(s32 drv_no);
Expand All @@ -57,10 +56,13 @@ extern s32 sdgecko_readCSD(s32 drv_no);
extern s32 sdgecko_readStatus(s32 drv_no);
extern s32 sdgecko_setHS(s32 drv_no);

extern syssram* __SYS_LockSram();
extern syssram* __SYS_LockSram(void);
extern syssramex* __SYS_LockSramEx(void);
extern u32 __SYS_UnlockSram(u32 write);
extern syssramex* __SYS_LockSramEx();
extern u32 __SYS_UnlockSramEx(u32 write);
extern u32 __SYS_SyncSram(void);
extern u32 __SYS_CheckSram(void);
extern void __SYS_ReadROM(void *buf,u32 len,u32 offset);

extern uiDrawObj_t * renderFileBrowser(file_handle** directory, int num_files, uiDrawObj_t *container);

Expand Down
92 changes: 92 additions & 0 deletions cube/swiss/source/config/sram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2024, Extrems <[email protected]>
*
* This file is part of Swiss.
*
* Swiss is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Swiss is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* with Swiss. If not, see <https://www.gnu.org/licenses/>.
*/

#include <ogc/system.h>
#include <stdbool.h>
#include "deviceHandler.h"
#include "sram.h"
#include "swiss.h"

static s32 onreset(s32 final)
{
if (!final) updateSRAM(&swissSettings, false);
return TRUE;
}

static sys_resetinfo resetinfo = {
{NULL, NULL}, onreset, 0
};

__attribute((constructor))
static void initSRAM(void)
{
bool writeSram = false;
syssram *sram = __SYS_LockSram();

if (!__SYS_CheckSram()) {
memset(sram, 0, sizeof(syssram));
sram->flags |= 0x10;
sram->flags |= 0x04;
writeSram = true;
}

__SYS_UnlockSram(writeSram);
while (!__SYS_SyncSram());

SYS_RegisterResetFunc(&resetinfo);
}

void refreshSRAM(SwissSettings *settings)
{
settings->sramHOffset = SYS_GetDisplayOffsetH();
settings->sram60Hz = SYS_GetEuRGB60();
settings->sramLanguage = SYS_GetLanguage();
settings->sramProgressive = SYS_GetProgressiveScan();
settings->sramStereo = SYS_GetSoundMode();
settings->sramVideo = SYS_GetVideoMode();

syssramex *sramex = __SYS_LockSramEx();

settings->configDeviceId = sramex->__padding0;
if (settings->configDeviceId > DEVICE_ID_MAX || !(getDeviceByUniqueId(settings->configDeviceId)->features & FEAT_CONFIG_DEVICE))
settings->configDeviceId = DEVICE_ID_UNK;

__SYS_UnlockSramEx(FALSE);
}

void updateSRAM(SwissSettings *settings, bool syncSram)
{
SYS_SetDisplayOffsetH(settings->sramHOffset);
SYS_SetEuRGB60(settings->sram60Hz);
SYS_SetLanguage(settings->sramLanguage);
SYS_SetProgressiveScan(settings->sramProgressive);
SYS_SetSoundMode(settings->sramStereo);
SYS_SetVideoMode(settings->sramVideo);

bool writeSram = false;
syssramex *sramex = __SYS_LockSramEx();

if (sramex->__padding0 != settings->configDeviceId) {
sramex->__padding0 = settings->configDeviceId;
writeSram = true;
}

__SYS_UnlockSramEx(writeSram);
while (syncSram && !__SYS_SyncSram());
}
29 changes: 29 additions & 0 deletions cube/swiss/source/config/sram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Extrems <[email protected]>
*
* This file is part of Swiss.
*
* Swiss is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Swiss is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* with Swiss. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef __SRAM_H__
#define __SRAM_H__

#include <stdbool.h>
#include "swiss.h"

void refreshSRAM(SwissSettings *settings);
void updateSRAM(SwissSettings *settings, bool syncSram);

#endif /* __SRAM_H__ */
1 change: 1 addition & 0 deletions cube/swiss/source/devices/filelock.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdbool.h>
#include <stdint.h>
#include "deviceHandler.h"
#include "filelock.h"

static lwpq_t queue = LWP_TQUEUE_NULL;

Expand Down
44 changes: 2 additions & 42 deletions cube/swiss/source/gui/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "settings.h"
#include "exi.h"
#include "bba.h"
#include "sram.h"

#define page_x_ofs_key (30)
#define page_x_ofs_val (410)
Expand Down Expand Up @@ -102,38 +103,9 @@ static char *tooltips_game[PAGE_GAME_MAX+1] = {
"Prefer Clean Boot:\n\nWhen enabled, the GameCube will be reset and the game\nbooted through normal processes with no changes applied.\nRegion restrictions may be applicable.\n\nOnly available to devices attached to the DVD Interface."
};

syssram* sram;
syssramex* sramex;

// Number of settings (including Back, Next, Save, Exit buttons) per page
int settings_count_pp[5] = {PAGE_GLOBAL_MAX, PAGE_NETWORK_MAX, PAGE_GAME_GLOBAL_MAX, PAGE_GAME_DEFAULTS_MAX, PAGE_GAME_MAX};

void refreshSRAM(SwissSettings *settings) {
bool writeSram = false;
sram = __SYS_LockSram();
if(!__SYS_CheckSram()) {
memset(sram, 0, sizeof(syssram));
sram->flags |= 0x10;
sram->flags |= 0x04;
writeSram = true;
}
settings->sramHOffset = sram->display_offsetH;
settings->sram60Hz = (sram->ntd >> 6) & 1;
settings->sramLanguage = sram->lang;
settings->sramProgressive = (sram->flags >> 7) & 1;
settings->sramStereo = (sram->flags >> 2) & 1;
settings->sramVideo = sram->flags & 3;
__SYS_UnlockSram(writeSram);
if(writeSram)
while(!__SYS_SyncSram());
sramex = __SYS_LockSramEx();
settings->configDeviceId = sramex->__padding0;
if(settings->configDeviceId > DEVICE_ID_MAX || !(getDeviceByUniqueId(settings->configDeviceId)->features & FEAT_CONFIG_DEVICE)) {
settings->configDeviceId = DEVICE_ID_UNK;
}
__SYS_UnlockSramEx(0);
}

char* getConfigDeviceName(SwissSettings *settings) {
DEVICEHANDLER_INTERFACE *configDevice = getDeviceByUniqueId(settings->configDeviceId);
return configDevice != NULL ? (char*)(configDevice->deviceName) : "None";
Expand Down Expand Up @@ -909,19 +881,7 @@ int show_settings(int page, int option, ConfigEntry *config) {
swissSettings.sramHOffset &= ~1;
}
VIDEO_SetAdjustingValues(swissSettings.sramHOffset, 0);
sram = __SYS_LockSram();
sram->display_offsetH = swissSettings.sramHOffset;
sram->ntd = swissSettings.sram60Hz ? (sram->ntd|0x40):(sram->ntd&~0x40);
sram->lang = swissSettings.sramLanguage;
sram->flags = swissSettings.sramProgressive ? (sram->flags|0x80):(sram->flags&~0x80);
sram->flags = swissSettings.sramStereo ? (sram->flags|0x04):(sram->flags&~0x04);
sram->flags = (swissSettings.sramVideo&0x03)|(sram->flags&~0x03);
__SYS_UnlockSram(1);
while(!__SYS_SyncSram());
sramex = __SYS_LockSramEx();
sramex->__padding0 = swissSettings.configDeviceId;
__SYS_UnlockSramEx(1);
while(!__SYS_SyncSram());
updateSRAM(&swissSettings, true);
// Update our .ini (in memory)
if(config != NULL) {
config_update_game(config, true);
Expand Down
1 change: 0 additions & 1 deletion cube/swiss/source/gui/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,5 @@ extern char *recentListLevelStr[];
#define SRAM_LANG_MAX 7
extern char *sramLang[];
int show_settings(int page, int option, ConfigEntry *config);
void refreshSRAM();

#endif
1 change: 1 addition & 0 deletions cube/swiss/source/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "bba.h"
#include "dvd.h"
#include "main.h"
#include "swiss.h"
#include "util.h"
#include "devices/dvd/deviceHandler-DVD.h"

Expand Down
4 changes: 4 additions & 0 deletions cube/swiss/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "exi.h"
#include "httpd.h"
#include "config.h"
#include "sram.h"
#include "gui/FrameBufferMagic.h"
#include "gui/IPLFontWrite.h"
#include "devices/deviceHandler.h"
Expand Down Expand Up @@ -209,6 +210,9 @@ int main(int argc, char *argv[])
GXRModeObj *forcedMode = getVideoModeFromSwissSetting(swissSettings.uiVMode);
DrawVideoMode(forcedMode);

swissSettings.sram60Hz = getTVFormat() != VI_PAL;
swissSettings.sramProgressive = getScanMode() == VI_PROGRESSIVE;

swissSettings.initNetworkAtStart |= !!bba_exists(LOC_MEMCARD_SLOT_A | LOC_MEMCARD_SLOT_B | LOC_SERIAL_PORT_2);
if(swissSettings.initNetworkAtStart) {
// Start up the BBA if it exists
Expand Down
25 changes: 7 additions & 18 deletions cube/swiss/source/swiss.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,18 @@ void ogc_video__reset()
swissSettings.gameVMode = 0;
if(swissSettings.sramVideo == SYS_VIDEO_PAL && !swissSettings.sram60Hz)
swissSettings.sramProgressive = 0;
} else {
swissSettings.sram60Hz = getTVFormat() != VI_PAL;
swissSettings.sramProgressive = getScanMode() == VI_PROGRESSIVE;

if(swissSettings.sramProgressive) {
if(swissSettings.sramVideo == SYS_VIDEO_PAL) {
swissSettings.sramProgressive = 0;
swissSettings.gameVMode = -2;
} else
swissSettings.gameVMode = -1;
} else if(swissSettings.sramProgressive) {
if(swissSettings.sramVideo == SYS_VIDEO_PAL) {
swissSettings.sramProgressive = 0;
swissSettings.gameVMode = -2;
} else
swissSettings.gameVMode = 0;
}
swissSettings.gameVMode = -1;
} else
swissSettings.gameVMode = 0;

if(!strncmp(gameID, "GB3E51", 6) || (!strncmp(gameID, "G2OE41", 6) && swissSettings.sramLanguage == SYS_LANG_SPANISH))
swissSettings.sramProgressive = 0;

syssram* sram = __SYS_LockSram();
sram->ntd = swissSettings.sram60Hz ? (sram->ntd|0x40):(sram->ntd&~0x40);
sram->flags = swissSettings.sramProgressive ? (sram->flags|0x80):(sram->flags&~0x80);
__SYS_UnlockSram(1);
while(!__SYS_SyncSram());

/* set TV mode for current game */
switch(swissSettings.gameVMode) {
case -2:
Expand Down

0 comments on commit 83f2a12

Please sign in to comment.