minor data storage improvements

This commit is contained in:
Lephenixnoir 2021-03-16 13:19:41 +01:00
parent 4c9b738247
commit c20db6a8ca
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 43 additions and 42 deletions

View File

@ -6,6 +6,7 @@
#define LIBFXOS_MEMORY_H
#include <string>
#include <array>
#include <cstdint>
namespace FxOS {
@ -14,7 +15,7 @@ namespace FxOS {
class MemoryArea
{
public:
enum MemoryAreaName {
enum MemoryAreaName: int8_t {
/* Userspace seen from user and privileged mode */
U0, P0,
/* Second half of memory, only for privileged mode */
@ -50,14 +51,17 @@ struct MemoryRegion
{
/* Address space regions that correspond to standard (ie. contiguous
multi-addressable) memory */
static MemoryRegion const &ROM;
static MemoryRegion const &RAM;
static MemoryRegion const &ROM_P2;
static MemoryRegion const &RAM_P2;
static MemoryRegion const &RS;
static MemoryRegion const &ILRAM;
static MemoryRegion const &XRAM;
static MemoryRegion const &YRAM;
static MemoryRegion const ROM;
static MemoryRegion const RAM;
static MemoryRegion const ROM_P2;
static MemoryRegion const RAM_P2;
static MemoryRegion const RS;
static MemoryRegion const ILRAM;
static MemoryRegion const XRAM;
static MemoryRegion const YRAM;
/* All standard regions. */
static std::array<MemoryRegion const *, 8> const all();
/* Determine if an address falls into one of the standard regions.
Throws std::out_of_range if none. */
@ -93,6 +97,9 @@ struct MemoryRegion
contained in one, which should always be the case) */
MemoryArea area() const noexcept;
private:
static std::array<MemoryRegion const *, 8> const m_all;
/* Automatically guess the cacheable and mappable flags */
void guess_flags() noexcept;
};

View File

@ -91,7 +91,7 @@ public:
/* Buffer initialized from file by looking in one of the specified
directories only. */
Buffer(std::string filepath, std::vector<std::string> &folders,
Buffer(std::string filepath, std::vector<std::string> const &folders,
ssize_t size=-1, int fill=0x00);
/* Create a buffer by copying and resizing another buffer */

View File

@ -67,7 +67,7 @@ uint32_t MemoryRegion::size() const noexcept
MemoryArea MemoryRegion::area() const noexcept
{
using Area = MemoryArea;
Area areas[5] = { Area::P4, Area::P3, Area::P2, Area::P1, Area::P0 };
static Area areas[5]={ Area::P4, Area::P3, Area::P2, Area::P1, Area::P0 };
for(int i = 0; i < 5; i++)
{
@ -101,50 +101,44 @@ void MemoryRegion::guess_flags() noexcept
}
}
using R = MemoryRegion;
using R = MemoryRegion;
/* Basic memory regions */
R const &R::ROM = MemoryRegion("ROM", 0x80000000, 0x81ffffff, false);
R const &R::RAM = MemoryRegion("RAM", 0x88000000, 0x88040000, true);
R const &R::ROM_P2 = MemoryRegion("ROM_P2", 0xa0000000, 0xa07fffff, false);
R const &R::RAM_P2 = MemoryRegion("RAM_P2", 0xa8000000, 0xa8040000, true);
R const &R::RS = MemoryRegion("RS", 0xfd800000, 0xfd8007ff, true);
R const &R::ILRAM = MemoryRegion("ILRAM", 0xe5200000, 0xe5203fff, true);
R const &R::XRAM = MemoryRegion("XRAM", 0xe5007000, 0xe5008fff, true);
R const &R::YRAM = MemoryRegion("YRAM", 0xe5017000, 0xe5018fff, true);
R const R::ROM = MemoryRegion("ROM", 0x80000000, 0x81ffffff, false);
R const R::RAM = MemoryRegion("RAM", 0x88000000, 0x88040000, true);
R const R::ROM_P2 = MemoryRegion("ROM_P2", 0xa0000000, 0xa07fffff, false);
R const R::RAM_P2 = MemoryRegion("RAM_P2", 0xa8000000, 0xa8040000, true);
R const R::RS = MemoryRegion("RS", 0xfd800000, 0xfd8007ff, true);
R const R::ILRAM = MemoryRegion("ILRAM", 0xe5200000, 0xe5203fff, true);
R const R::XRAM = MemoryRegion("XRAM", 0xe5007000, 0xe5008fff, true);
R const R::YRAM = MemoryRegion("YRAM", 0xe5017000, 0xe5018fff, true);
std::array<MemoryRegion const *, 8> const MemoryRegion::m_all = {
&R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2,
&R::RS, &R::ILRAM, &R::XRAM, &R::YRAM,
};
std::array<MemoryRegion const *, 8> const MemoryRegion::all()
{
return m_all;
}
MemoryRegion const *MemoryRegion::region_for(uint32_t address)
{
MemoryRegion const *regs[8] = {
&R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2,
&R::RS, &R::ILRAM, &R::XRAM, &R::YRAM
};
for(int i = 0; i < 8; i++)
{
if(regs[i]->start <= address && address < regs[i]->end)
return regs[i];
for(auto r: MemoryRegion::all()) {
if(r->start <= address && address < r->end) return r;
}
return nullptr;
}
MemoryRegion::MemoryRegion(std::string name)
{
MemoryRegion const *regs[8] = {
&R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2,
&R::RS, &R::ILRAM, &R::XRAM, &R::YRAM
};
for(int i = 0; i < 8; i++)
{
if(regs[i]->name == name)
{
*this = *regs[i];
for(auto r: MemoryRegion::all()) {
if(r->name == name) {
*this = *r;
return;
}
}
throw std::runtime_error("No standard region named '" + name + "'");
}

View File

@ -86,7 +86,7 @@ Buffer::Buffer(std::string filepath, ssize_t bufsize, int fill)
this->path = filepath;
}
Buffer::Buffer(std::string filepath, std::vector<std::string> &folders,
Buffer::Buffer(std::string filepath, std::vector<std::string> const &folders,
ssize_t size, int fill):
Buffer()
{