2020-02-28 16:04:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2022-01-16 20:30:43 +00:00
|
|
|
Copyright (C) 2018-22 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/>.
|
|
|
|
*/
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2022-01-16 20:30:43 +00:00
|
|
|
/** MPC106 (Grackle) emulation. */
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2021-10-23 18:17:47 +00:00
|
|
|
#include <devices/common/hwcomponent.h>
|
|
|
|
#include <devices/common/mmiodevice.h>
|
2022-07-17 03:33:06 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
#include <devices/memctrl/memctrlbase.h>
|
|
|
|
#include <devices/memctrl/mpc106.h>
|
|
|
|
#include <memaccess.h>
|
|
|
|
|
2019-07-02 02:15:33 +00:00
|
|
|
#include <cinttypes>
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2021-09-15 22:46:38 +00:00
|
|
|
#include <loguru.hpp>
|
2022-08-19 18:07:22 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-07-15 00:05:10 +00:00
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
MPC106::MPC106() : MemCtrlBase(), PCIDevice("Grackle"), PCIHost()
|
2022-01-16 20:30:43 +00:00
|
|
|
{
|
2020-03-14 13:23:46 +00:00
|
|
|
this->name = "Grackle";
|
|
|
|
|
2022-01-26 15:45:21 +00:00
|
|
|
supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV |
|
|
|
|
HWCompType::PCI_HOST | HWCompType::PCI_DEV);
|
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
// populate PCI config header
|
|
|
|
this->vendor_id = PCI_VENDOR_MOTOROLA;
|
|
|
|
this->device_id = 0x0002;
|
|
|
|
this->class_rev = 0x06000040;
|
|
|
|
this->cache_ln_sz = 8;
|
|
|
|
this->command = 6;
|
|
|
|
this->status = 0x80;
|
|
|
|
|
|
|
|
// add PCI/ISA I/O space, 64K for now
|
2020-03-31 19:12:06 +00:00
|
|
|
add_mmio_region(0xFE000000, 0x10000, this);
|
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
// add memory mapped I/O region for MPC106 registers
|
2019-08-21 06:33:01 +00:00
|
|
|
add_mmio_region(0xFEC00000, 0x300000, this);
|
|
|
|
}
|
2019-07-12 05:27:14 +00:00
|
|
|
|
2022-08-19 18:07:22 +00:00
|
|
|
int MPC106::device_postinit()
|
|
|
|
{
|
|
|
|
std::string pci_dev_name;
|
|
|
|
|
|
|
|
static const std::map<std::string, int> pci_slots = {
|
|
|
|
{"pci_A1", 0xD}, {"pci_B1", 0xE}, {"pci_C1", 0xF}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& slot : pci_slots) {
|
|
|
|
pci_dev_name = GET_STR_PROP(slot.first);
|
|
|
|
if (!pci_dev_name.empty()) {
|
|
|
|
this->attach_pci_device(pci_dev_name, slot.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:16:31 +00:00
|
|
|
uint32_t MPC106::read(uint32_t rgn_start, uint32_t offset, int size) {
|
2020-03-31 19:12:06 +00:00
|
|
|
uint32_t result;
|
|
|
|
|
2022-08-22 10:16:31 +00:00
|
|
|
if (rgn_start == 0xFE000000) {
|
2022-03-13 21:22:53 +00:00
|
|
|
// broadcast I/O request to devices that support I/O space
|
|
|
|
// until a device returns true that means "request accepted"
|
2020-03-31 19:12:06 +00:00
|
|
|
for (auto& dev : this->io_space_devs) {
|
|
|
|
if (dev->pci_io_read(offset, size, &result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_F(ERROR, "Attempt to read from unmapped PCI I/O space, offset=0x%X", offset);
|
2020-05-12 18:55:45 +00:00
|
|
|
} else {
|
2020-03-31 19:12:06 +00:00
|
|
|
if (offset >= 0x200000) {
|
2020-05-12 18:55:45 +00:00
|
|
|
if (this->config_addr & 0x80) // process only if bit E (enable) is set
|
2020-03-31 19:12:06 +00:00
|
|
|
return pci_read(size);
|
|
|
|
}
|
2019-07-07 06:10:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
// FIXME: reading from CONFIG_ADDR is ignored for now
|
2019-08-21 06:33:01 +00:00
|
|
|
|
2019-07-07 06:10:32 +00:00
|
|
|
return 0;
|
2019-07-02 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 10:16:31 +00:00
|
|
|
void MPC106::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) {
|
|
|
|
if (rgn_start == 0xFE000000) {
|
2022-03-13 21:22:53 +00:00
|
|
|
// broadcast I/O request to devices that support I/O space
|
|
|
|
// until a device returns true that means "request accepted"
|
2020-03-31 19:12:06 +00:00
|
|
|
for (auto& dev : this->io_space_devs) {
|
|
|
|
if (dev->pci_io_write(offset, value, size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_F(ERROR, "Attempt to write to unmapped PCI I/O space, offset=0x%X", offset);
|
2020-05-12 18:55:45 +00:00
|
|
|
} else {
|
2020-03-31 19:12:06 +00:00
|
|
|
if (offset < 0x200000) {
|
|
|
|
this->config_addr = value;
|
2020-05-12 18:55:45 +00:00
|
|
|
} else {
|
|
|
|
if (this->config_addr & 0x80) // process only if bit E (enable) is set
|
2020-03-31 19:12:06 +00:00
|
|
|
return pci_write(value, size);
|
|
|
|
}
|
2019-07-07 06:10:32 +00:00
|
|
|
}
|
2019-07-15 00:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint32_t MPC106::pci_read(uint32_t size) {
|
2019-08-23 19:30:30 +00:00
|
|
|
int bus_num, dev_num, fun_num, reg_offs;
|
2019-07-15 00:05:10 +00:00
|
|
|
|
2019-08-21 06:33:01 +00:00
|
|
|
bus_num = (this->config_addr >> 8) & 0xFF;
|
2019-08-23 19:30:30 +00:00
|
|
|
dev_num = (this->config_addr >> 19) & 0x1F;
|
|
|
|
fun_num = (this->config_addr >> 16) & 0x07;
|
|
|
|
reg_offs = (this->config_addr >> 24) & 0xFC;
|
2019-07-15 00:05:10 +00:00
|
|
|
|
2022-08-24 14:12:35 +00:00
|
|
|
if (bus_num) {
|
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
|
|
|
"%s err: read attempt from non-local PCI bus, config_addr = %x %02x:%02x.%x @%02x.%c",
|
|
|
|
this->name.c_str(), this->config_addr, bus_num, dev_num, fun_num, reg_offs,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
|
|
|
|
);
|
|
|
|
return 0xFFFFFFFFUL; // PCI spec §6.1
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
if (dev_num == 0 && fun_num == 0) { // dev_num 0 is assigned to myself
|
2019-08-23 19:30:30 +00:00
|
|
|
return this->pci_cfg_read(reg_offs, size);
|
2019-08-21 06:33:01 +00:00
|
|
|
} else {
|
2022-01-16 20:30:43 +00:00
|
|
|
if (this->dev_map.count(dev_num)) {
|
|
|
|
return this->dev_map[dev_num]->pci_cfg_read(reg_offs, size);
|
2019-08-23 19:30:30 +00:00
|
|
|
} else {
|
2020-05-12 18:55:45 +00:00
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
2022-08-22 09:29:09 +00:00
|
|
|
"%s err: read attempt from non-existing PCI device %02x:%02x.%x @%02x.%c",
|
|
|
|
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
|
|
|
|
);
|
2022-08-24 14:12:35 +00:00
|
|
|
return 0xFFFFFFFFUL; // PCI spec §6.1
|
2019-08-23 19:30:30 +00:00
|
|
|
}
|
2019-08-21 06:33:01 +00:00
|
|
|
}
|
2019-07-15 00:05:10 +00:00
|
|
|
|
2019-08-21 06:33:01 +00:00
|
|
|
return 0;
|
2019-07-15 00:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void MPC106::pci_write(uint32_t value, uint32_t size) {
|
2019-08-23 19:30:30 +00:00
|
|
|
int bus_num, dev_num, fun_num, reg_offs;
|
2019-07-15 00:05:10 +00:00
|
|
|
|
2019-08-21 06:33:01 +00:00
|
|
|
bus_num = (this->config_addr >> 8) & 0xFF;
|
2022-08-24 14:12:35 +00:00
|
|
|
dev_num = (this->config_addr >> 19) & 0x1F;
|
|
|
|
fun_num = (this->config_addr >> 16) & 0x07;
|
|
|
|
reg_offs = (this->config_addr >> 24) & 0xFC;
|
|
|
|
|
2019-08-21 06:33:01 +00:00
|
|
|
if (bus_num) {
|
2020-05-12 18:55:45 +00:00
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
2022-08-24 14:12:35 +00:00
|
|
|
"%s err: write attempt to non-local PCI bus, config_addr = %x %02x:%02x.%x @%02x.%c = %0*x",
|
|
|
|
this->name.c_str(), this->config_addr, bus_num, dev_num, fun_num, reg_offs,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size,
|
|
|
|
size * 2, value
|
|
|
|
);
|
2019-08-21 06:33:01 +00:00
|
|
|
return;
|
2019-07-15 00:05:10 +00:00
|
|
|
}
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
if (dev_num == 0 && fun_num == 0) { // dev_num 0 is assigned to myself
|
2019-08-23 19:30:30 +00:00
|
|
|
this->pci_cfg_write(reg_offs, value, size);
|
2019-08-21 06:33:01 +00:00
|
|
|
} else {
|
2022-01-16 20:30:43 +00:00
|
|
|
if (this->dev_map.count(dev_num)) {
|
|
|
|
this->dev_map[dev_num]->pci_cfg_write(reg_offs, value, size);
|
2019-08-23 19:30:30 +00:00
|
|
|
} else {
|
2020-05-12 18:55:45 +00:00
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
2022-08-22 09:29:09 +00:00
|
|
|
"%s err: write attempt to non-existing PCI device %02x:%02x.%x @%02x.%c = %0*x",
|
|
|
|
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size,
|
|
|
|
size * 2, value
|
|
|
|
);
|
2019-08-23 19:30:30 +00:00
|
|
|
}
|
2019-08-21 06:33:01 +00:00
|
|
|
}
|
2019-07-02 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint32_t MPC106::pci_cfg_read(uint32_t reg_offs, uint32_t size) {
|
2019-08-21 06:33:01 +00:00
|
|
|
#ifdef MPC106_DEBUG
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(9, "read from Grackle register %08X", reg_offs);
|
2019-08-21 06:33:01 +00:00
|
|
|
#endif
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
if (reg_offs < 64) {
|
|
|
|
return PCIDevice::pci_cfg_read(reg_offs, size);
|
|
|
|
}
|
|
|
|
|
2021-02-03 22:39:19 +00:00
|
|
|
return read_mem(&this->my_pci_cfg_hdr[reg_offs], size);
|
2019-07-15 00:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void MPC106::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
|
2019-08-21 06:33:01 +00:00
|
|
|
#ifdef MPC106_DEBUG
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(9, "write %08X to Grackle register %08X", value, reg_offs);
|
2019-08-21 06:33:01 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-13 21:22:53 +00:00
|
|
|
if (reg_offs < 64) {
|
|
|
|
PCIDevice::pci_cfg_write(reg_offs, value, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:33:01 +00:00
|
|
|
// FIXME: implement write-protection for read-only registers
|
2021-02-03 22:39:19 +00:00
|
|
|
|
|
|
|
write_mem(&this->my_pci_cfg_hdr[reg_offs], value, size);
|
2019-10-07 01:21:01 +00:00
|
|
|
|
|
|
|
if (this->my_pci_cfg_hdr[0xF2] & 8) {
|
|
|
|
#ifdef MPC106_DEBUG
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(9, "MPC106: MCCR1[MEMGO] was set!");
|
2019-10-07 01:21:01 +00:00
|
|
|
#endif
|
|
|
|
setup_ram();
|
|
|
|
}
|
2019-07-02 02:15:33 +00:00
|
|
|
}
|
2019-08-23 19:30:30 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void MPC106::setup_ram() {
|
2019-10-07 01:21:01 +00:00
|
|
|
uint32_t mem_start, mem_end, ext_mem_start, ext_mem_end, bank_start, bank_end;
|
|
|
|
uint32_t ram_size = 0;
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint8_t bank_en = this->my_pci_cfg_hdr[0xA0];
|
2019-10-07 01:21:01 +00:00
|
|
|
|
|
|
|
for (int bank = 0; bank < 8; bank++) {
|
|
|
|
if (bank_en & (1 << bank)) {
|
|
|
|
if (bank < 4) {
|
2020-05-12 18:55:45 +00:00
|
|
|
mem_start = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x80]);
|
2020-01-13 02:04:06 +00:00
|
|
|
ext_mem_start = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x88]);
|
2020-05-12 18:55:45 +00:00
|
|
|
mem_end = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x90]);
|
|
|
|
ext_mem_end = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x98]);
|
2019-10-07 01:21:01 +00:00
|
|
|
} else {
|
2020-05-12 18:55:45 +00:00
|
|
|
mem_start = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x84]);
|
2020-01-13 02:04:06 +00:00
|
|
|
ext_mem_start = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x8C]);
|
2020-05-12 18:55:45 +00:00
|
|
|
mem_end = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x94]);
|
|
|
|
ext_mem_end = READ_DWORD_LE_A(&this->my_pci_cfg_hdr[0x9C]);
|
2019-10-07 01:21:01 +00:00
|
|
|
}
|
|
|
|
bank_start = (((ext_mem_start >> bank * 8) & 3) << 30) |
|
|
|
|
(((mem_start >> bank * 8) & 0xFF) << 20);
|
|
|
|
bank_end = (((ext_mem_end >> bank * 8) & 3) << 30) |
|
|
|
|
(((mem_end >> bank * 8) & 0xFF) << 20) | 0xFFFFFUL;
|
|
|
|
if (bank && bank_start != ram_size)
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(WARNING, "MPC106: RAM not contiguous!");
|
2021-02-03 22:29:48 +00:00
|
|
|
ram_size += bank_end - bank_start + 1;
|
2019-10-07 01:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->add_ram_region(0, ram_size)) {
|
2022-08-14 12:26:56 +00:00
|
|
|
LOG_F(ERROR, "MPC106 RAM allocation failed!");
|
2019-10-07 01:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-17 03:33:06 +00:00
|
|
|
|
2022-08-19 18:07:22 +00:00
|
|
|
static const PropMap Grackle_Properties = {
|
|
|
|
{"pci_A1",
|
|
|
|
new StrProperty("")},
|
|
|
|
{"pci_B1",
|
|
|
|
new StrProperty("")},
|
|
|
|
{"pci_C1",
|
|
|
|
new StrProperty("")},
|
|
|
|
};
|
|
|
|
|
2022-07-17 03:33:06 +00:00
|
|
|
static const DeviceDescription Grackle_Descriptor = {
|
2022-08-19 18:07:22 +00:00
|
|
|
MPC106::create, {}, Grackle_Properties
|
2022-07-17 03:33:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_DEVICE(Grackle, Grackle_Descriptor);
|