2019-07-17 22:14:13 -04:00
|
|
|
#pragma once
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "NotificationManager.h"
|
|
|
|
#include "../Utilities/FolderUtilities.h"
|
|
|
|
|
|
|
|
struct MissingFirmwareMessage
|
|
|
|
{
|
|
|
|
const char* Filename;
|
2020-05-26 00:05:35 -04:00
|
|
|
FirmwareType Firmware;
|
2019-07-17 22:14:13 -04:00
|
|
|
uint32_t Size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FirmwareHelper
|
|
|
|
{
|
|
|
|
private:
|
2021-03-10 11:13:28 -05:00
|
|
|
static bool AttemptLoadDspFirmware(string combinedFilename, string splitFilenameProgram, string splitFilenameData, vector<uint8_t> &programRom, vector<uint8_t> &dataRom, uint32_t programSize, uint32_t dataSize)
|
2019-07-17 22:14:13 -04:00
|
|
|
{
|
2021-03-10 11:13:28 -05:00
|
|
|
VirtualFile combinedFirmware(FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), combinedFilename));
|
|
|
|
if(combinedFirmware.GetSize() == programSize + dataSize) {
|
2019-07-17 22:14:13 -04:00
|
|
|
vector<uint8_t> firmwareData;
|
|
|
|
combinedFirmware.ReadFile(firmwareData);
|
|
|
|
programRom.insert(programRom.end(), firmwareData.begin(), firmwareData.begin() + programSize);
|
|
|
|
dataRom.insert(dataRom.end(), firmwareData.begin() + programSize, firmwareData.end());
|
|
|
|
return true;
|
2021-03-10 11:13:28 -05:00
|
|
|
} else {
|
|
|
|
VirtualFile splitFirmwareProg(FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), splitFilenameProgram));
|
|
|
|
VirtualFile splitFirmwareData(FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), splitFilenameData));
|
|
|
|
|
|
|
|
if(splitFirmwareProg.GetSize() == programSize && splitFirmwareData.GetSize() == dataSize) {
|
2019-07-17 22:14:13 -04:00
|
|
|
splitFirmwareProg.ReadFile(programRom);
|
|
|
|
splitFirmwareData.ReadFile(dataRom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-10 11:13:28 -05:00
|
|
|
|
2020-02-19 23:53:34 -05:00
|
|
|
static bool AttemptLoadBsxFirmware(uint8_t** prgRom, uint32_t& prgSize)
|
|
|
|
{
|
2020-06-22 20:18:45 -04:00
|
|
|
VirtualFile firmware(FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), "BS-X.bin"));
|
2021-03-10 11:13:28 -05:00
|
|
|
if(firmware.IsValid() && firmware.GetSize() >= 0x8000) {
|
2020-02-19 23:53:34 -05:00
|
|
|
*prgRom = new uint8_t[firmware.GetSize()];
|
|
|
|
prgSize = (uint32_t)firmware.GetSize();
|
|
|
|
firmware.ReadFile(*prgRom, (uint32_t)firmware.GetSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:59:12 -04:00
|
|
|
static bool AttemptLoadFirmware(uint8_t** out, string filename, uint32_t size, string altFilename = "")
|
2020-05-26 00:05:35 -04:00
|
|
|
{
|
2020-06-23 14:59:12 -04:00
|
|
|
string path = FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), filename);
|
|
|
|
VirtualFile firmware(path);
|
2021-03-10 11:13:28 -05:00
|
|
|
if((!firmware.IsValid() || firmware.GetSize() != size) && !altFilename.empty()) {
|
2020-06-23 14:59:12 -04:00
|
|
|
string altPath = FolderUtilities::CombinePath(FolderUtilities::GetFirmwareFolder(), altFilename);
|
|
|
|
firmware = VirtualFile(altPath);
|
|
|
|
}
|
|
|
|
|
2021-03-10 11:13:28 -05:00
|
|
|
if(firmware.IsValid() && firmware.GetSize() == size) {
|
2020-05-26 00:05:35 -04:00
|
|
|
*out = new uint8_t[firmware.GetSize()];
|
|
|
|
firmware.ReadFile(*out, (uint32_t)firmware.GetSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:14:13 -04:00
|
|
|
public:
|
2021-03-10 11:13:28 -05:00
|
|
|
static bool LoadDspFirmware(Console *console, FirmwareType type, string combinedFilename, string splitFilenameProgram, string splitFilenameData, vector<uint8_t> &programRom, vector<uint8_t> &dataRom, vector<uint8_t> &embeddedFirware, uint32_t programSize = 0x1800, uint32_t dataSize = 0x800)
|
2019-07-17 22:14:13 -04:00
|
|
|
{
|
2021-03-10 11:13:28 -05:00
|
|
|
if(embeddedFirware.size() == programSize + dataSize) {
|
2019-10-26 17:47:57 -04:00
|
|
|
programRom.insert(programRom.end(), embeddedFirware.begin(), embeddedFirware.begin() + programSize);
|
|
|
|
dataRom.insert(dataRom.end(), embeddedFirware.begin() + programSize, embeddedFirware.end());
|
|
|
|
return true;
|
2021-03-10 11:13:28 -05:00
|
|
|
} else if(AttemptLoadDspFirmware(combinedFilename, splitFilenameProgram, splitFilenameData, programRom, dataRom, programSize, dataSize)) {
|
2019-07-17 22:14:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MissingFirmwareMessage msg;
|
|
|
|
msg.Filename = combinedFilename.c_str();
|
2020-05-26 00:05:35 -04:00
|
|
|
msg.Firmware = type;
|
2019-07-17 22:14:13 -04:00
|
|
|
msg.Size = programSize + dataSize;
|
|
|
|
console->GetNotificationManager()->SendNotification(ConsoleNotificationType::MissingFirmware, &msg);
|
|
|
|
|
|
|
|
//Try again in case the user selected a valid firmware file
|
2021-03-10 11:13:28 -05:00
|
|
|
if(AttemptLoadDspFirmware(combinedFilename, splitFilenameProgram, splitFilenameData, programRom, dataRom, programSize, dataSize)) {
|
2019-07-17 22:14:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageManager::DisplayMessage("Error", "Could not find firmware file for DSP: " + combinedFilename);
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-19 23:53:34 -05:00
|
|
|
|
|
|
|
static bool LoadBsxFirmware(Console* console, uint8_t** prgRom, uint32_t& prgSize)
|
|
|
|
{
|
2021-03-10 11:13:28 -05:00
|
|
|
if(AttemptLoadBsxFirmware(prgRom, prgSize)) {
|
2020-02-19 23:53:34 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MissingFirmwareMessage msg;
|
2020-06-22 20:18:45 -04:00
|
|
|
msg.Filename = "BS-X.bin";
|
2020-05-26 00:05:35 -04:00
|
|
|
msg.Firmware = FirmwareType::Satellaview;
|
2021-03-10 11:13:28 -05:00
|
|
|
msg.Size = 1024*1024;
|
2020-02-19 23:53:34 -05:00
|
|
|
console->GetNotificationManager()->SendNotification(ConsoleNotificationType::MissingFirmware, &msg);
|
2021-03-10 11:13:28 -05:00
|
|
|
|
|
|
|
if(AttemptLoadBsxFirmware(prgRom, prgSize)) {
|
2020-02-19 23:53:34 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageManager::DisplayMessage("Error", "Could not find firmware file for BS-X");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-26 00:05:35 -04:00
|
|
|
|
2020-06-18 22:22:46 -04:00
|
|
|
static bool LoadSgbFirmware(Console* console, uint8_t** prgRom, uint32_t& prgSize, bool useSgb2)
|
2020-06-18 00:58:22 -04:00
|
|
|
{
|
2020-06-18 22:22:46 -04:00
|
|
|
string filename = useSgb2 ? "SGB2.sfc" : "SGB1.sfc";
|
|
|
|
prgSize = useSgb2 ? 0x80000 : 0x40000;
|
2021-03-10 11:13:28 -05:00
|
|
|
if(AttemptLoadFirmware(prgRom, filename, prgSize)) {
|
2020-06-18 00:58:22 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MissingFirmwareMessage msg;
|
2020-06-18 22:22:46 -04:00
|
|
|
msg.Filename = filename.c_str();
|
|
|
|
msg.Firmware = useSgb2 ? FirmwareType::SGB2 : FirmwareType::SGB1;
|
2020-06-18 00:58:22 -04:00
|
|
|
msg.Size = prgSize;
|
|
|
|
console->GetNotificationManager()->SendNotification(ConsoleNotificationType::MissingFirmware, &msg);
|
|
|
|
|
2021-03-10 11:13:28 -05:00
|
|
|
if(AttemptLoadFirmware(prgRom, filename, prgSize)) {
|
2020-06-18 00:58:22 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageManager::DisplayMessage("Error", "Could not find firmware file for Super Game Boy");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:05:35 -04:00
|
|
|
static bool LoadGbBootRom(Console* console, uint8_t** bootRom, FirmwareType type)
|
|
|
|
{
|
2020-06-18 00:58:22 -04:00
|
|
|
string filename;
|
2020-06-23 14:59:12 -04:00
|
|
|
string altFilename;
|
2021-03-10 11:13:28 -05:00
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
case FirmwareType::Gameboy: filename = "dmg_boot.bin"; altFilename = "gb_bios.bin"; break;
|
|
|
|
case FirmwareType::GameboyColor: filename = "cgb_boot.bin"; altFilename = "gbc_bios.bin"; break;
|
|
|
|
case FirmwareType::Sgb1GameboyCpu: filename = "sgb_boot.bin"; altFilename = "sgb_bios.bin"; break;
|
|
|
|
case FirmwareType::Sgb2GameboyCpu: filename = "sgb2_boot.bin"; altFilename = "sgb_bios.bin"; break;
|
2020-06-18 00:58:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t size = type == FirmwareType::GameboyColor ? 2304 : 256;
|
2021-03-10 11:13:28 -05:00
|
|
|
if(AttemptLoadFirmware(bootRom, filename, size, altFilename)) {
|
2020-05-26 00:05:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-18 00:58:22 -04:00
|
|
|
/*MissingFirmwareMessage msg;
|
2020-05-26 00:05:35 -04:00
|
|
|
msg.Filename = filename.c_str();
|
|
|
|
msg.Firmware = type;
|
|
|
|
msg.Size = size;
|
|
|
|
console->GetNotificationManager()->SendNotification(ConsoleNotificationType::MissingFirmware, &msg);
|
|
|
|
|
|
|
|
if(AttemptLoadFirmware(bootRom, filename, size)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-18 00:58:22 -04:00
|
|
|
MessageManager::DisplayMessage("Error", "Could not find boot rom: " + filename);*/
|
2020-05-26 00:05:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-10 11:13:28 -05:00
|
|
|
};
|