2020-02-28 16:04:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-04-16 22:03:36 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2020-02-28 16:04:28 +00:00
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 17:38:32 +00:00
|
|
|
|
2022-03-28 23:45:00 +00:00
|
|
|
#include <devices/common/hwcomponent.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
#include <devices/common/nvram.h>
|
2022-07-17 10:52:12 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2021-09-15 22:46:38 +00:00
|
|
|
#include <loguru.hpp>
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
/** @file Non-volatile RAM implementation.
|
|
|
|
*/
|
|
|
|
|
2020-01-05 16:52:46 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
/** the signature for NVRAM backing file identification. */
|
|
|
|
static char NVRAM_FILE_ID[] = "DINGUSPPCNVRAM";
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2022-03-28 23:45:00 +00:00
|
|
|
NVram::NVram(std::string file_name, uint32_t ram_size)
|
|
|
|
{
|
|
|
|
this->name = "NVRAM";
|
|
|
|
|
|
|
|
supports_types(HWCompType::NVRAM);
|
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
this->file_name = file_name;
|
|
|
|
this->ram_size = ram_size;
|
|
|
|
|
2023-04-16 22:03:36 +00:00
|
|
|
// allocate memory storage and fill it with zeroes
|
|
|
|
this->storage = std::unique_ptr<uint8_t[]>(new uint8_t[ram_size] ());
|
2020-01-07 10:52:38 +00:00
|
|
|
|
|
|
|
this->init();
|
2020-01-05 16:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
NVram::~NVram() {
|
2020-01-07 10:52:38 +00:00
|
|
|
this->save();
|
2020-01-05 16:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint8_t NVram::read_byte(uint32_t offset) {
|
2020-01-07 10:52:38 +00:00
|
|
|
return (this->storage[offset]);
|
|
|
|
}
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void NVram::write_byte(uint32_t offset, uint8_t val) {
|
2020-01-07 10:52:38 +00:00
|
|
|
this->storage[offset] = val;
|
|
|
|
}
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
void NVram::init() {
|
2020-05-12 18:55:45 +00:00
|
|
|
char sig[sizeof(NVRAM_FILE_ID)];
|
2020-01-07 10:52:38 +00:00
|
|
|
uint16_t data_size;
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
ifstream f(this->file_name, ios::in | ios::binary);
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
if (f.fail() || !f.read(sig, sizeof(NVRAM_FILE_ID)) ||
|
|
|
|
!f.read((char*)&data_size, sizeof(data_size)) ||
|
|
|
|
memcmp(sig, NVRAM_FILE_ID, sizeof(NVRAM_FILE_ID)) || data_size != this->ram_size ||
|
2023-04-16 22:03:36 +00:00
|
|
|
!f.read((char*)this->storage.get(), this->ram_size)) {
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(WARNING, "Could not restore NVRAM content from the given file.");
|
2020-01-05 16:52:46 +00:00
|
|
|
}
|
2020-01-07 10:52:38 +00:00
|
|
|
|
|
|
|
f.close();
|
2020-01-05 16:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void NVram::save() {
|
2020-01-07 10:52:38 +00:00
|
|
|
ofstream f(this->file_name, ios::out | ios::binary);
|
2020-01-05 17:38:32 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
/* write file identification */
|
|
|
|
f.write(NVRAM_FILE_ID, sizeof(NVRAM_FILE_ID));
|
2020-05-12 18:55:45 +00:00
|
|
|
f.write((char*)&this->ram_size, sizeof(this->ram_size));
|
2020-01-05 17:38:32 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
/* write NVRAM content */
|
2023-04-16 22:03:36 +00:00
|
|
|
f.write((char*)this->storage.get(), this->ram_size);
|
2020-01-05 16:52:46 +00:00
|
|
|
|
2020-01-07 10:52:38 +00:00
|
|
|
f.close();
|
2020-01-05 16:52:46 +00:00
|
|
|
}
|
2022-07-17 10:52:12 +00:00
|
|
|
|
|
|
|
static const DeviceDescription Nvram_Descriptor = {
|
|
|
|
NVram::create, {}, {}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_DEVICE(NVRAM, Nvram_Descriptor);
|