diff --git a/src/Config.cpp b/src/Config.cpp index 9c5103a5..67b2f242 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -35,6 +35,14 @@ char FirmwarePath[1024]; int DLDIEnable; char DLDISDPath[1024]; +char FirmwareUsername[64]; +int FirmwareLanguage; +bool FirmwareOverrideSettings; +int FirmwareBirthdayMonth; +int FirmwareBirthdayDay; +int FirmwareFavouriteColour; +char FirmwareMessage[1024]; + char DSiBIOS9Path[1024]; char DSiBIOS7Path[1024]; char DSiFirmwarePath[1024]; @@ -63,6 +71,14 @@ ConfigEntry ConfigFile[] = {"DLDIEnable", 0, &DLDIEnable, 0, NULL, 0}, {"DLDISDPath", 1, DLDISDPath, 0, "", 1023}, + {"FirmwareUsername", 1, FirmwareUsername, 0, "melonDS", 63}, + {"FirmwareLanguage", 0, &FirmwareLanguage, 1, NULL, 0}, + {"FirmwareOverrideSettings", 0, &FirmwareOverrideSettings, false, NULL, 0}, + {"FirmwareBirthdayMonth", 0, &FirmwareBirthdayMonth, 0, NULL, 0}, + {"FirmwareBirthdayDay", 0, &FirmwareBirthdayDay, 0, NULL, 0}, + {"FirmwareFavouriteColour", 0, &FirmwareFavouriteColour, 0, NULL, 0}, + {"FirmwareMessage", 1, FirmwareMessage, 0, "", 1023}, + {"DSiBIOS9Path", 1, DSiBIOS9Path, 0, "", 1023}, {"DSiBIOS7Path", 1, DSiBIOS7Path, 0, "", 1023}, {"DSiFirmwarePath", 1, DSiFirmwarePath, 0, "", 1023}, diff --git a/src/Config.h b/src/Config.h index 4dd0b068..bf7214e2 100644 --- a/src/Config.h +++ b/src/Config.h @@ -48,6 +48,14 @@ extern char FirmwarePath[1024]; extern int DLDIEnable; extern char DLDISDPath[1024]; +extern char FirmwareUsername[64]; +extern int FirmwareLanguage; +extern bool FirmwareOverrideSettings; +extern int FirmwareBirthdayMonth; +extern int FirmwareBirthdayDay; +extern int FirmwareFavouriteColour; +extern char FirmwareMessage[1024]; + extern char DSiBIOS9Path[1024]; extern char DSiBIOS7Path[1024]; extern char DSiFirmwarePath[1024]; diff --git a/src/SPI.cpp b/src/SPI.cpp index 54444d94..2f990c0d 100644 --- a/src/SPI.cpp +++ b/src/SPI.cpp @@ -19,6 +19,10 @@ #include #include #include +#include +#include +#include +#include #include "Config.h" #include "NDS.h" #include "DSi.h" @@ -112,25 +116,25 @@ u32 FixFirmwareLength(u32 originalLength) return originalLength; } -void Reset() +void LoadDefaultFirmware() { - if (Firmware) delete[] Firmware; - Firmware = NULL; + FirmwareLength = 0x20000; + Firmware = new u8[FirmwareLength]; + memset(Firmware, 0xFF, FirmwareLength); + FirmwareMask = FirmwareLength - 1; - if (NDS::ConsoleType == 1) - strncpy(FirmwarePath, Config::DSiFirmwarePath, 1023); - else - strncpy(FirmwarePath, Config::FirmwarePath, 1023); + u32 userdata = 0x7FE00 & FirmwareMask; - FILE* f = Platform::OpenLocalFile(FirmwarePath, "rb"); - if (!f) - { - printf("Firmware not found\n"); + memset(Firmware + userdata, 0, 0x74); - // TODO: generate default firmware - return; - } + // user settings offset + *(u16*)&Firmware[0x20] = (FirmwareLength - 0x200) >> 3; + Firmware[userdata+0x00] = 5; // version +} + +void LoadFirmwareFromFile(FILE* f) +{ fseek(f, 0, SEEK_END); FirmwareLength = FixFirmwareLength((u32)ftell(f)); @@ -143,19 +147,76 @@ void Reset() fclose(f); // take a backup - char firmbkp[1028]; + char fwBackupPath[sizeof(FirmwarePath) + 4]; int fplen = strlen(FirmwarePath); - strncpy(&firmbkp[0], FirmwarePath, fplen); - strncpy(&firmbkp[fplen], ".bak", 1028-fplen); - firmbkp[fplen+4] = '\0'; - f = Platform::OpenLocalFile(firmbkp, "rb"); - if (f) fclose(f); + strcpy(&fwBackupPath[0], FirmwarePath); + strncpy(&fwBackupPath[fplen], ".bak", sizeof(fwBackupPath) - fplen); + fwBackupPath[fplen+4] = '\0'; + f = Platform::OpenLocalFile(fwBackupPath, "rb"); + if (!f) + { + f = Platform::OpenLocalFile(fwBackupPath, "wb"); + if (f) + { + fwrite(Firmware, 1, FirmwareLength, f); + fclose(f); + } + else + { + printf("Could not write firmware backup!\n"); + } + } else { - f = Platform::OpenLocalFile(firmbkp, "wb"); - fwrite(Firmware, 1, FirmwareLength, f); fclose(f); } +} + +void LoadUserSettingsFromConfig() +{ + // setting up username + std::u16string username = std::wstring_convert, char16_t>{}.from_bytes(Config::FirmwareUsername); + size_t usernameLength = std::min(username.length(), (size_t) 10); + memcpy(Firmware + UserSettings + 0x06, username.data(), usernameLength * sizeof(char16_t)); + Firmware[UserSettings+0x1A] = usernameLength; + + // setting language + Firmware[UserSettings+0x64] = Config::FirmwareLanguage; + + // setting up color + Firmware[UserSettings+0x02] = Config::FirmwareFavouriteColour; + + // setting up birthday + Firmware[UserSettings+0x03] = Config::FirmwareBirthdayMonth; + Firmware[UserSettings+0x04] = Config::FirmwareBirthdayDay; + + // setup message + std::u16string message = std::wstring_convert, char16_t>{}.from_bytes(Config::FirmwareMessage); + size_t messageLength = std::min(message.length(), (size_t) 26); + memcpy(Firmware + UserSettings + 0x1C, message.data(), messageLength * sizeof(char16_t)); + Firmware[UserSettings+0x50] = messageLength; +} + +void Reset() +{ + if (Firmware) delete[] Firmware; + Firmware = NULL; + + if (NDS::ConsoleType == 1) + strncpy(FirmwarePath, Config::DSiFirmwarePath, sizeof(FirmwarePath) - 1); + else + strncpy(FirmwarePath, Config::FirmwarePath, sizeof(FirmwarePath) - 1); + + FILE* f = Platform::OpenLocalFile(FirmwarePath, "rb"); + if (!f) + { + printf("Firmware not found! Generating default firmware.\n"); + LoadDefaultFirmware(); + } + else + { + LoadFirmwareFromFile(f); + } FirmwareMask = FirmwareLength - 1; @@ -168,6 +229,9 @@ void Reset() UserSettings = userdata; + if (!f || Config::FirmwareOverrideSettings) + LoadUserSettingsFromConfig(); + // fix touchscreen coords *(u16*)&Firmware[userdata+0x58] = 0; *(u16*)&Firmware[userdata+0x5A] = 0; diff --git a/src/frontend/Util_ROM.cpp b/src/frontend/Util_ROM.cpp index 4d55299a..29247e44 100644 --- a/src/frontend/Util_ROM.cpp +++ b/src/frontend/Util_ROM.cpp @@ -165,7 +165,7 @@ int VerifyDSFirmware() long len; f = Platform::OpenLocalFile(Config::FirmwarePath, "rb"); - if (!f) return Load_FirmwareMissing; + if (!f) return Load_FirmwareNotBootable; fseek(f, 0, SEEK_END); len = ftell(f); diff --git a/src/frontend/qt_sdl/CMakeLists.txt b/src/frontend/qt_sdl/CMakeLists.txt index 61a2d205..cb810496 100644 --- a/src/frontend/qt_sdl/CMakeLists.txt +++ b/src/frontend/qt_sdl/CMakeLists.txt @@ -10,6 +10,7 @@ SET(SOURCES_QT_SDL InputConfig/resources/ds.qrc VideoSettingsDialog.cpp AudioSettingsDialog.cpp + FirmwareSettingsDialog.cpp WifiSettingsDialog.cpp InterfaceSettingsDialog.cpp ROMInfoDialog.cpp diff --git a/src/frontend/qt_sdl/EmuSettingsDialog.cpp b/src/frontend/qt_sdl/EmuSettingsDialog.cpp index b7de274d..31e2e528 100644 --- a/src/frontend/qt_sdl/EmuSettingsDialog.cpp +++ b/src/frontend/qt_sdl/EmuSettingsDialog.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "types.h" #include "Platform.h" diff --git a/src/frontend/qt_sdl/FirmwareSettingsDialog.cpp b/src/frontend/qt_sdl/FirmwareSettingsDialog.cpp new file mode 100644 index 00000000..7857cdc4 --- /dev/null +++ b/src/frontend/qt_sdl/FirmwareSettingsDialog.cpp @@ -0,0 +1,72 @@ +/* + Copyright 2016-2020 Arisotura + + This file is part of melonDS. + + melonDS 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 3 of the License, or (at your option) + any later version. + + melonDS 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 along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#include "Config.h" +#include "FirmwareSettingsDialog.h" +#include "ui_FirmwareSettingsDialog.h" + +FirmwareSettingsDialog* FirmwareSettingsDialog::currentDlg = nullptr; + +FirmwareSettingsDialog::FirmwareSettingsDialog(QWidget* parent) : QDialog(parent), ui(new Ui::FirmwareSettingsDialog) +{ + ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + + ui->usernameEdit->setText(Config::FirmwareUsername); + + ui->languageBox->addItems(languages); + ui->languageBox->setCurrentIndex(Config::FirmwareLanguage); + + QDate birthDate = QDate(QDate::currentDate().year(), Config::FirmwareBirthdayMonth, Config::FirmwareBirthdayDay); + ui->birthdayEdit->setDate(birthDate); + + ui->colorsEdit->addItems(colours); + ui->colorsEdit->setCurrentIndex(Config::FirmwareFavouriteColour); + + ui->messageEdit->setText(Config::FirmwareMessage); + + ui->overrideFirmwareBox->setChecked(Config::FirmwareOverrideSettings); +} + +FirmwareSettingsDialog::~FirmwareSettingsDialog() +{ + delete ui; +} + +void FirmwareSettingsDialog::on_dialogButtons_accepted() +{ + std::string newName = ui->usernameEdit->text().toStdString(); + strncpy(Config::FirmwareUsername, newName.c_str(), 63); Config::FirmwareUsername[63] = '\0'; + + Config::FirmwareLanguage = ui->languageBox->currentIndex(); + Config::FirmwareFavouriteColour = ui->colorsEdit->currentIndex(); + Config::FirmwareBirthdayDay = ui->birthdayEdit->date().day(); + Config::FirmwareBirthdayMonth = ui->birthdayEdit->date().month(); + Config::FirmwareOverrideSettings = ui->overrideFirmwareBox->isChecked(); + + std::string newMessage = ui->messageEdit->text().toStdString(); + strncpy(Config::FirmwareMessage, newMessage.c_str(), 1023); Config::FirmwareMessage[1023] = '\0'; + Config::Save(); + + closeDlg(); +} + +void FirmwareSettingsDialog::on_dialogButtons_rejected() +{ + closeDlg(); +} diff --git a/src/frontend/qt_sdl/FirmwareSettingsDialog.h b/src/frontend/qt_sdl/FirmwareSettingsDialog.h new file mode 100644 index 00000000..5f8e5ff9 --- /dev/null +++ b/src/frontend/qt_sdl/FirmwareSettingsDialog.h @@ -0,0 +1,92 @@ +/* + Copyright 2016-2020 Arisotura + + This file is part of melonDS. + + melonDS 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 3 of the License, or (at your option) + any later version. + + melonDS 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 along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#ifndef FIRMWARESETTINGSDIALOG_H +#define FIRMWARESETTINGSDIALOG_H + +#include +#include + +namespace Ui { class FirmwareSettingsDialog; } +class FirmwareSettingsDialog; + +class FirmwareSettingsDialog : public QDialog +{ + Q_OBJECT + +public: + const QStringList colours + { + "Greyish Blue", + "Brown", + "Red", + "Light Pink", + "Orange", + "Yellow", + "Lime", + "Light Green", + "Dark Green", + "Turqoise", + "Light Blue", + "Blue", + "Dark Blue", + "Dark Purple", + "Light Purple", + "Dark Pink" + }; + + const QStringList languages + { + "Japanese", + "English", + "French", + "German", + "Italian", + "Spanish" + }; + + explicit FirmwareSettingsDialog(QWidget* parent); + ~FirmwareSettingsDialog(); + + static FirmwareSettingsDialog* currentDlg; + static FirmwareSettingsDialog* openDlg(QWidget* parent) + { + if (currentDlg) + { + currentDlg->activateWindow(); + return currentDlg; + } + + currentDlg = new FirmwareSettingsDialog(parent); + currentDlg->show(); + return currentDlg; + } + static void closeDlg() + { + currentDlg = nullptr; + } + +private slots: + void on_dialogButtons_accepted(); + void on_dialogButtons_rejected(); + +private: + Ui::FirmwareSettingsDialog* ui; +}; + +#endif // FIRMWARESETTINGSDIALOG_H diff --git a/src/frontend/qt_sdl/FirmwareSettingsDialog.ui b/src/frontend/qt_sdl/FirmwareSettingsDialog.ui new file mode 100644 index 00000000..2e279469 --- /dev/null +++ b/src/frontend/qt_sdl/FirmwareSettingsDialog.ui @@ -0,0 +1,144 @@ + + + FirmwareSettingsDialog + + + + 0 + 0 + 511 + 272 + + + + + 0 + 0 + + + + Firmware settings - melonDS + + + + QLayout::SetFixedSize + + + + + + + Username + + + + + + + MelonDS + + + 10 + + + false + + + + + + + Language + + + + + + + + + + Birthday + + + + + + + + + + Color + + + + + + + + + + Message + + + + + + + + + + Override firmware settings + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + dialogButtons + accepted() + FirmwareSettingsDialog + accept() + + + 255 + 250 + + + 255 + 135 + + + + + dialogButtons + rejected() + FirmwareSettingsDialog + reject() + + + 255 + 250 + + + 255 + 135 + + + + + diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index d6acc919..fb1caea5 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -55,6 +55,7 @@ #include "InputConfig/InputConfigDialog.h" #include "VideoSettingsDialog.h" #include "AudioSettingsDialog.h" +#include "FirmwareSettingsDialog.h" #include "WifiSettingsDialog.h" #include "InterfaceSettingsDialog.h" #include "ROMInfoDialog.h" @@ -1411,6 +1412,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) actInterfaceSettings = menu->addAction("Interface settings"); connect(actInterfaceSettings, &QAction::triggered, this, &MainWindow::onOpenInterfaceSettings); + actFirmwareSettings = menu->addAction("Firmware settings"); + connect(actFirmwareSettings, &QAction::triggered, this, &MainWindow::onOpenFirmwareSettings); + { QMenu* submenu = menu->addMenu("Savestate settings"); @@ -2446,6 +2450,14 @@ void MainWindow::onOpenAudioSettings() connect(dlg, &AudioSettingsDialog::finished, this, &MainWindow::onAudioSettingsFinished); } +void MainWindow::onOpenFirmwareSettings() +{ + FirmwareSettingsDialog* dlg = FirmwareSettingsDialog::openDlg(this); + connect(dlg, &FirmwareSettingsDialog::finished, this, &MainWindow::onFirmwareSettingsFinished); +} + +void MainWindow::onFirmwareSettingsFinished(int res) {} + void MainWindow::onUpdateAudioSettings() { SPU::SetInterpolation(Config::AudioInterp); diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h index 13784170..0f9034fe 100644 --- a/src/frontend/qt_sdl/main.h +++ b/src/frontend/qt_sdl/main.h @@ -256,10 +256,12 @@ private slots: void onInputConfigFinished(int res); void onOpenVideoSettings(); void onOpenAudioSettings(); + void onOpenFirmwareSettings(); void onUpdateAudioSettings(); void onAudioSettingsFinished(int res); void onOpenWifiSettings(); void onWifiSettingsFinished(int res); + void onFirmwareSettingsFinished(int res); void onOpenInterfaceSettings(); void onInterfaceSettingsFinished(int res); void onUpdateMouseTimer(); @@ -331,6 +333,7 @@ public: QAction* actVideoSettings; QAction* actAudioSettings; QAction* actWifiSettings; + QAction* actFirmwareSettings; QAction* actInterfaceSettings; QAction* actSavestateSRAMReloc; QAction* actScreenSize[4];