dingusppc/devices/common/nvram.cpp

96 lines
2.6 KiB
C++
Raw Normal View History

/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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
#include <devices/common/hwcomponent.h>
#include <devices/common/nvram.h>
2020-05-12 18:55:45 +00:00
#include <cinttypes>
#include <cstring>
#include <fstream>
#include <iostream>
2021-09-15 22:46:38 +00:00
#include <loguru.hpp>
2020-01-07 10:52:38 +00:00
/** @file Non-volatile RAM implementation.
*/
using namespace std;
2020-01-07 10:52:38 +00:00
/** the signature for NVRAM backing file identification. */
static char NVRAM_FILE_ID[] = "DINGUSPPCNVRAM";
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;
this->storage = new uint8_t[ram_size];
this->init();
}
2020-05-12 18:55:45 +00:00
NVram::~NVram() {
2020-01-07 10:52:38 +00:00
this->save();
if (this->storage)
delete this->storage;
}
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-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-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-07 10:52:38 +00:00
ifstream f(this->file_name, ios::in | ios::binary);
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 ||
!f.read((char*)this->storage, this->ram_size)) {
2020-02-24 05:36:31 +00:00
LOG_F(WARNING, "Could not restore NVRAM content from the given file. \n");
2020-01-07 10:52:38 +00:00
memset(this->storage, 0, sizeof(this->ram_size));
}
2020-01-07 10:52:38 +00:00
f.close();
}
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 */
2020-05-12 18:55:45 +00:00
f.write((char*)this->storage, this->ram_size);
2020-01-07 10:52:38 +00:00
f.close();
}