2020-02-28 16:04:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2021-10-23 19:00:31 +00:00
|
|
|
Copyright (C) 2018-21 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
|
|
|
|
2021-10-23 18:17:47 +00:00
|
|
|
#include <devices/memctrl/memctrlbase.h>
|
2023-11-03 07:21:33 +00:00
|
|
|
#include <devices/common/mmiodevice.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <algorithm> // to shut up MSVC errors (:
|
2020-01-07 23:24:31 +00:00
|
|
|
#include <cstring>
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <string>
|
2019-08-18 23:34:24 +00:00
|
|
|
#include <vector>
|
2022-12-12 08:13:49 +00:00
|
|
|
#include <loguru.hpp>
|
2019-08-18 23:34:24 +00:00
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
static inline bool match_mem_entry(const AddressMapEntry* entry,
|
|
|
|
const uint32_t start, const uint32_t end,
|
|
|
|
MMIODevice* dev_instance)
|
|
|
|
{
|
|
|
|
return start == entry->start && end == entry->end &&
|
|
|
|
(!dev_instance || dev_instance == entry->devobj);
|
|
|
|
}
|
2019-08-18 23:34:24 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
MemCtrlBase::~MemCtrlBase() {
|
2021-05-15 22:50:44 +00:00
|
|
|
for (auto& entry : address_map) {
|
|
|
|
if (entry)
|
|
|
|
delete(entry);
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
for (auto& reg : mem_regions) {
|
2019-08-18 23:34:24 +00:00
|
|
|
if (reg)
|
2020-05-12 18:55:45 +00:00
|
|
|
delete (reg);
|
2019-08-18 23:34:24 +00:00
|
|
|
}
|
|
|
|
this->mem_regions.clear();
|
|
|
|
this->address_map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
AddressMapEntry* MemCtrlBase::find_range(uint32_t addr) {
|
|
|
|
for (auto& entry : address_map) {
|
2021-05-15 22:50:44 +00:00
|
|
|
if (addr >= entry->start && addr <= entry->end)
|
|
|
|
return entry;
|
2019-08-18 23:34:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:46:39 +00:00
|
|
|
return nullptr;
|
2019-08-18 23:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
AddressMapEntry* MemCtrlBase::find_range_exact(uint32_t addr, uint32_t size,
|
|
|
|
MMIODevice* dev_instance)
|
|
|
|
{
|
2022-12-12 08:13:49 +00:00
|
|
|
if (size) {
|
2023-01-11 22:35:54 +00:00
|
|
|
const uint32_t end = addr + size - 1;
|
2022-12-12 08:13:49 +00:00
|
|
|
for (auto& entry : address_map) {
|
2023-01-11 22:35:54 +00:00
|
|
|
if (match_mem_entry(entry, addr, end, dev_instance))
|
2022-12-12 08:13:49 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AddressMapEntry* MemCtrlBase::find_range_contains(uint32_t addr, uint32_t size) {
|
|
|
|
if (size) {
|
|
|
|
uint32_t end = addr + size - 1;
|
|
|
|
for (auto& entry : address_map) {
|
|
|
|
if (addr >= entry->start && end <= entry->end)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AddressMapEntry* MemCtrlBase::find_range_overlaps(uint32_t addr, uint32_t size) {
|
|
|
|
if (size) {
|
|
|
|
uint32_t end = addr + size - 1;
|
|
|
|
for (auto& entry : address_map) {
|
|
|
|
if (end >= entry->start && addr <= entry->end)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MemCtrlBase::is_range_free(uint32_t addr, uint32_t size) {
|
|
|
|
bool result = true;
|
|
|
|
if (size) {
|
|
|
|
uint32_t end = addr + size - 1;
|
|
|
|
for (auto& entry : address_map) {
|
|
|
|
if (addr == entry->start && end == entry->end) {
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(WARNING, "memory region 0x%X..0x%X%s%s%s already exists",
|
|
|
|
addr, end,
|
|
|
|
entry->devobj ? " (" : "",
|
|
|
|
entry->devobj ? entry->devobj->get_name().c_str() : "",
|
|
|
|
entry->devobj ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:13:49 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else if (addr >= entry->start && end <= entry->end) {
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(WARNING, "0x%X..0x%X already exists in memory region 0x%X..0x%X%s%s%s",
|
|
|
|
addr, end, entry->start, entry->end,
|
|
|
|
entry->devobj ? " (" : "",
|
|
|
|
entry->devobj ? entry->devobj->get_name().c_str() : "",
|
|
|
|
entry->devobj ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:13:49 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else if (end >= entry->start && addr <= entry->end) {
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(ERROR, "0x%X..0x%X overlaps existing memory region 0x%X..0x%X%s%s%s",
|
|
|
|
addr, end, entry->start, entry->end,
|
|
|
|
entry->devobj ? " (" : "",
|
|
|
|
entry->devobj ? entry->devobj->get_name().c_str() : "",
|
|
|
|
entry->devobj ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:13:49 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
bool MemCtrlBase::add_mem_region(uint32_t start_addr, uint32_t size,
|
|
|
|
uint32_t dest_addr, uint32_t type,
|
|
|
|
uint8_t init_val = 0)
|
|
|
|
{
|
2021-05-15 22:50:44 +00:00
|
|
|
AddressMapEntry *entry;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
/* error if a memory region for the given range already exists */
|
2022-12-12 08:13:49 +00:00
|
|
|
if (!is_range_free(start_addr, size))
|
2019-08-18 23:34:24 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint8_t* reg_content = new uint8_t[size];
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
this->mem_regions.push_back(reg_content);
|
|
|
|
|
2021-05-15 22:50:44 +00:00
|
|
|
entry = new AddressMapEntry;
|
|
|
|
|
2022-12-12 08:13:49 +00:00
|
|
|
uint32_t end = start_addr + size - 1;
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->start = start_addr;
|
2022-12-12 08:13:49 +00:00
|
|
|
entry->end = end;
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->mirror = dest_addr;
|
|
|
|
entry->type = type;
|
|
|
|
entry->devobj = 0;
|
|
|
|
entry->mem_ptr = reg_content;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
this->address_map.push_back(entry);
|
|
|
|
|
2022-12-12 08:13:49 +00:00
|
|
|
LOG_F(INFO, "Added mem region 0x%X..0x%X (%s%s%s%s) -> 0x%X", start_addr, end,
|
|
|
|
entry->type & RT_ROM ? "ROM," : "",
|
|
|
|
entry->type & RT_RAM ? "RAM," : "",
|
|
|
|
entry->type & RT_MMIO ? "MMIO," : "",
|
|
|
|
entry->type & RT_MIRROR ? "MIRROR," : "",
|
|
|
|
dest_addr
|
|
|
|
);
|
|
|
|
|
2019-08-18 23:34:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
bool MemCtrlBase::add_rom_region(uint32_t start_addr, uint32_t size) {
|
2019-08-18 23:34:24 +00:00
|
|
|
return add_mem_region(start_addr, size, 0, RT_ROM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
bool MemCtrlBase::add_ram_region(uint32_t start_addr, uint32_t size) {
|
2019-08-18 23:34:24 +00:00
|
|
|
return add_mem_region(start_addr, size, 0, RT_RAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
bool MemCtrlBase::add_mem_mirror(uint32_t start_addr, uint32_t dest_addr) {
|
2021-05-15 22:50:44 +00:00
|
|
|
AddressMapEntry *entry, *ref_entry;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
ref_entry = find_range(dest_addr);
|
|
|
|
if (!ref_entry)
|
|
|
|
return false;
|
|
|
|
|
2021-05-15 22:50:44 +00:00
|
|
|
entry = new AddressMapEntry;
|
|
|
|
|
2022-12-12 08:13:49 +00:00
|
|
|
uint32_t end = start_addr + (ref_entry->end - ref_entry->start);
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->start = start_addr;
|
2022-12-12 08:13:49 +00:00
|
|
|
entry->end = end;
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->mirror = dest_addr;
|
|
|
|
entry->type = ref_entry->type | RT_MIRROR;
|
|
|
|
entry->devobj = 0;
|
|
|
|
entry->mem_ptr = ref_entry->mem_ptr;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
this->address_map.push_back(entry);
|
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(INFO, "Added mem region mirror 0x%X..0x%X (%s%s%s%s) -> 0x%X : 0x%X..0x%X%s%s%s",
|
|
|
|
start_addr, end,
|
2022-12-12 08:13:49 +00:00
|
|
|
entry->type & RT_ROM ? "ROM," : "",
|
|
|
|
entry->type & RT_RAM ? "RAM," : "",
|
|
|
|
entry->type & RT_MMIO ? "MMIO," : "",
|
|
|
|
entry->type & RT_MIRROR ? "MIRROR," : "",
|
|
|
|
dest_addr,
|
|
|
|
ref_entry->start, ref_entry->end,
|
2023-01-11 22:35:54 +00:00
|
|
|
ref_entry->devobj ? " (" : "",
|
|
|
|
ref_entry->devobj ? ref_entry->devobj->get_name().c_str() : "",
|
|
|
|
ref_entry->devobj ? ")"
|
|
|
|
: ""
|
2022-12-12 08:13:49 +00:00
|
|
|
);
|
|
|
|
|
2019-08-18 23:34:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 21:47:52 +00:00
|
|
|
bool MemCtrlBase::set_data(uint32_t load_addr, const uint8_t* data, uint32_t size) {
|
2020-05-12 18:55:45 +00:00
|
|
|
AddressMapEntry* ref_entry;
|
2019-08-18 23:34:24 +00:00
|
|
|
uint32_t cpy_size;
|
|
|
|
|
2023-02-02 21:47:52 +00:00
|
|
|
ref_entry = find_range(load_addr);
|
2019-08-18 23:34:24 +00:00
|
|
|
if (!ref_entry)
|
|
|
|
return false;
|
|
|
|
|
2023-02-02 21:47:52 +00:00
|
|
|
uint32_t load_offset = load_addr - ref_entry->start;
|
|
|
|
|
2019-08-18 23:34:24 +00:00
|
|
|
cpy_size = std::min(ref_entry->end - ref_entry->start + 1, size);
|
2023-02-02 21:47:52 +00:00
|
|
|
memcpy(ref_entry->mem_ptr + load_offset, data, cpy_size);
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
bool MemCtrlBase::add_mmio_region(uint32_t start_addr, uint32_t size, MMIODevice* dev_instance)
|
|
|
|
{
|
2021-05-15 22:50:44 +00:00
|
|
|
AddressMapEntry *entry;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
2022-12-12 08:13:49 +00:00
|
|
|
/* error if a memory region for the given range already exists */
|
|
|
|
if (!is_range_free(start_addr, size))
|
2019-08-18 23:34:24 +00:00
|
|
|
return false;
|
|
|
|
|
2021-05-15 22:50:44 +00:00
|
|
|
entry = new AddressMapEntry;
|
|
|
|
|
2022-12-12 08:13:49 +00:00
|
|
|
uint32_t end = start_addr + size - 1;
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->start = start_addr;
|
2022-12-12 08:13:49 +00:00
|
|
|
entry->end = end;
|
2021-05-15 22:50:44 +00:00
|
|
|
entry->mirror = 0;
|
|
|
|
entry->type = RT_MMIO;
|
|
|
|
entry->devobj = dev_instance;
|
|
|
|
entry->mem_ptr = 0;
|
2019-08-18 23:34:24 +00:00
|
|
|
|
|
|
|
this->address_map.push_back(entry);
|
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(INFO, "Added mmio region 0x%X..0x%X%s%s%s",
|
|
|
|
start_addr, end,
|
|
|
|
dev_instance ? " (" : "",
|
|
|
|
dev_instance ? dev_instance->get_name().c_str() : "",
|
|
|
|
dev_instance ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:13:49 +00:00
|
|
|
|
2019-08-18 23:34:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-06 20:46:39 +00:00
|
|
|
|
2023-01-11 22:35:54 +00:00
|
|
|
bool MemCtrlBase::remove_mmio_region(uint32_t start_addr, uint32_t size, MMIODevice* dev_instance)
|
|
|
|
{
|
2022-12-12 08:16:59 +00:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
uint32_t end = start_addr + size - 1;
|
|
|
|
address_map.erase(std::remove_if(address_map.begin(), address_map.end(),
|
|
|
|
[start_addr, end, dev_instance, &found](const AddressMapEntry *entry) {
|
2023-01-11 22:35:54 +00:00
|
|
|
bool result = match_mem_entry(entry, start_addr, end, dev_instance);
|
2022-12-12 08:16:59 +00:00
|
|
|
found += result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
), address_map.end());
|
|
|
|
|
|
|
|
if (found == 0)
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(ERROR, "Cannot find mmio region 0x%X..0x%X%s%s%s to remove",
|
|
|
|
start_addr, end,
|
|
|
|
dev_instance ? " (" : "",
|
|
|
|
dev_instance ? dev_instance->get_name().c_str() : "",
|
|
|
|
dev_instance ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:16:59 +00:00
|
|
|
else if (found > 1)
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(ERROR, "Removed %d occurrences of mmio region 0x%X..0x%X%s%s%s",
|
|
|
|
found, start_addr, end,
|
|
|
|
dev_instance ? " (" : "",
|
|
|
|
dev_instance ? dev_instance->get_name().c_str() : "",
|
|
|
|
dev_instance ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
2022-12-12 08:16:59 +00:00
|
|
|
else
|
2023-01-11 22:35:54 +00:00
|
|
|
LOG_F(INFO, "Removed mmio region 0x%X..0x%X%s%s%s",
|
|
|
|
start_addr, end,
|
|
|
|
dev_instance ? " (" : "",
|
|
|
|
dev_instance ? dev_instance->get_name().c_str() : "",
|
|
|
|
dev_instance ? ")"
|
|
|
|
: ""
|
|
|
|
);
|
|
|
|
|
2022-12-12 08:16:59 +00:00
|
|
|
return (found > 0);
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:46:39 +00:00
|
|
|
AddressMapEntry* MemCtrlBase::find_rom_region()
|
|
|
|
{
|
|
|
|
for (auto& entry : address_map) {
|
|
|
|
if (entry->type == RT_ROM) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|