2022-01-16 21:47:04 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
|
|
|
Copyright (C) 2018-22 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Bandit ARBus-to-PCI Bridge emulation. */
|
|
|
|
|
|
|
|
#include "bandit.h"
|
|
|
|
#include <devices/memctrl/memctrlbase.h>
|
|
|
|
#include <endianswap.h>
|
|
|
|
#include <loguru.hpp>
|
|
|
|
#include <machines/machinebase.h>
|
|
|
|
#include <memaccess.h>
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
|
2022-03-13 19:55:29 +00:00
|
|
|
Bandit::Bandit(int bridge_num, std::string name) : PCIHost(), PCIDevice(name)
|
2022-01-16 21:47:04 +00:00
|
|
|
{
|
|
|
|
this->base_addr = 0xF0000000 + ((bridge_num & 3) << 25);
|
|
|
|
|
|
|
|
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
|
|
|
|
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
|
|
|
|
|
|
|
|
// add memory mapped I/O region for Bandit control registers
|
2022-03-13 19:55:29 +00:00
|
|
|
// This region has the following layout:
|
2022-01-16 21:47:04 +00:00
|
|
|
// base_addr + 0x000000 --> I/O space
|
|
|
|
// base_addr + 0x800000 --> CONFIG_ADDR
|
|
|
|
// base_addr + 0xC00000 --> CONFIG_DATA
|
2022-01-17 12:57:01 +00:00
|
|
|
// base_addr + 0x1000000 --> pass-through memory space (not included below)
|
2022-01-16 21:47:04 +00:00
|
|
|
mem_ctrl->add_mmio_region(base_addr, 0x01000000, this);
|
|
|
|
|
|
|
|
// prepare the PCI config header
|
2022-03-13 19:55:29 +00:00
|
|
|
this->vendor_id = PCI_VENDOR_APPLE;
|
|
|
|
this->device_id = 0x0001;
|
|
|
|
this->class_rev = 0x06000003;
|
|
|
|
this->cache_ln_sz = 8;
|
|
|
|
this->command = 0x16;
|
|
|
|
|
|
|
|
// make several PCI config space registers read-only
|
|
|
|
this->pci_wr_cmd = [](uint16_t cmd) {}; // command register
|
|
|
|
this->pci_wr_cache_lnsz = [](uint8_t val) {}; // cache line size register
|
2022-01-16 21:47:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:38:51 +00:00
|
|
|
uint32_t Bandit::pci_cfg_read(uint32_t reg_offs, uint32_t size)
|
|
|
|
{
|
|
|
|
if (reg_offs < 64) {
|
|
|
|
return PCIDevice::pci_cfg_read(reg_offs, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (reg_offs) {
|
|
|
|
case BANDIT_ADDR_MASK:
|
|
|
|
return BYTESWAP_32(this->addr_mask);
|
|
|
|
default:
|
|
|
|
LOG_F(WARNING, "%s: reading from unimplemented config register at 0x%X",
|
|
|
|
this->pci_name.c_str(), reg_offs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bandit::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size)
|
|
|
|
{
|
|
|
|
if (reg_offs < 64) {
|
|
|
|
PCIDevice::pci_cfg_write(reg_offs, value, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (reg_offs) {
|
|
|
|
case BANDIT_ADDR_MASK:
|
|
|
|
this->addr_mask = BYTESWAP_32(value);
|
|
|
|
this->verbose_address_space();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_F(WARNING, "%s: writing to unimplemented config register at 0x%X",
|
|
|
|
this->pci_name.c_str(), reg_offs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:47:04 +00:00
|
|
|
uint32_t Bandit::read(uint32_t reg_start, uint32_t offset, int size)
|
|
|
|
{
|
2022-01-17 00:28:35 +00:00
|
|
|
int fun_num;
|
|
|
|
uint8_t reg_offs;
|
|
|
|
uint32_t result, idsel;
|
2022-01-16 21:47:04 +00:00
|
|
|
|
|
|
|
if (offset & BANDIT_CONFIG_SPACE) {
|
|
|
|
if (offset & 0x00400000) {
|
|
|
|
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
|
2022-01-17 00:28:35 +00:00
|
|
|
if (this->config_addr & BANDIT_CAR_TYPE) {
|
|
|
|
LOG_F(WARNING, "%s: config cycle type 1 not supported yet", this->name.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
|
|
|
|
fun_num = (this->config_addr >> 8) & 7;
|
|
|
|
reg_offs = this->config_addr & 0xFCU;
|
2022-01-16 21:47:04 +00:00
|
|
|
|
2022-01-17 00:28:35 +00:00
|
|
|
if (!SINGLE_BIT_SET(idsel)) {
|
|
|
|
LOG_F(ERROR, "%s: invalid IDSEL=0x%X passed", this->name.c_str(), idsel);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idsel == BANDIT_ID_SEL) { // access to myself
|
|
|
|
result = this->pci_cfg_read(reg_offs, size);
|
|
|
|
} else {
|
|
|
|
if (this->dev_map.count(idsel)) {
|
|
|
|
result = this->dev_map[idsel]->pci_cfg_read(reg_offs, size);
|
2022-01-16 21:47:04 +00:00
|
|
|
} else {
|
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
|
|
|
"%s err: read attempt from non-existing PCI device %d \n",
|
|
|
|
this->name.c_str(),
|
2022-01-17 00:28:35 +00:00
|
|
|
idsel);
|
2022-01-16 21:47:04 +00:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = this->config_addr;
|
|
|
|
}
|
|
|
|
} else { // I/O space access
|
|
|
|
LOG_F(WARNING, "%s: I/O space write not implemented yet", this->name.c_str());
|
|
|
|
}
|
2022-03-13 19:55:29 +00:00
|
|
|
return result;
|
2022-01-16 21:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bandit::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
|
|
|
|
{
|
2022-01-17 00:28:35 +00:00
|
|
|
int fun_num;
|
|
|
|
uint8_t reg_offs;
|
|
|
|
uint32_t idsel;
|
2022-01-16 21:47:04 +00:00
|
|
|
|
|
|
|
if (offset & BANDIT_CONFIG_SPACE) {
|
|
|
|
if (offset & 0x00400000) {
|
|
|
|
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
|
2022-01-17 00:28:35 +00:00
|
|
|
if (this->config_addr & BANDIT_CAR_TYPE) {
|
|
|
|
LOG_F(WARNING, "%s: config cycle type 1 not supported yet", this->name.c_str());
|
2022-01-16 21:47:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:28:35 +00:00
|
|
|
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
|
|
|
|
fun_num = (this->config_addr >> 8) & 7;
|
|
|
|
reg_offs = this->config_addr & 0xFCU;
|
|
|
|
|
|
|
|
if (!SINGLE_BIT_SET(idsel)) {
|
|
|
|
LOG_F(ERROR, "%s: invalid IDSEL=0x%X passed", this->name.c_str(), idsel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idsel == BANDIT_ID_SEL) { // access to myself
|
|
|
|
this->pci_cfg_write(reg_offs, value, size);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 21:47:04 +00:00
|
|
|
|
2022-01-17 00:28:35 +00:00
|
|
|
if (this->dev_map.count(idsel)) {
|
|
|
|
this->dev_map[idsel]->pci_cfg_write(reg_offs, value, size);
|
2022-01-16 21:47:04 +00:00
|
|
|
} else {
|
|
|
|
LOG_F(
|
|
|
|
ERROR,
|
|
|
|
"%s err: write attempt to non-existing PCI device %d \n",
|
|
|
|
this->name.c_str(),
|
2022-01-17 00:28:35 +00:00
|
|
|
idsel);
|
2022-01-16 21:47:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-17 00:28:35 +00:00
|
|
|
this->config_addr = BYTESWAP_32(value);
|
2022-01-16 21:47:04 +00:00
|
|
|
}
|
|
|
|
} else { // I/O space access
|
|
|
|
LOG_F(WARNING, "%s: I/O space write not implemented yet", this->name.c_str());
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 20:38:51 +00:00
|
|
|
|
|
|
|
void Bandit::verbose_address_space()
|
|
|
|
{
|
|
|
|
uint32_t mask;
|
|
|
|
int bit_pos;
|
|
|
|
|
|
|
|
LOG_F(INFO, "%s address spaces:", this->pci_name.c_str());
|
|
|
|
|
|
|
|
// verbose coarse aka 256MB memory regions
|
|
|
|
for (mask = 0x10000, bit_pos = 0; mask != 0x80000000UL; mask <<= 1, bit_pos++) {
|
|
|
|
if (this->addr_mask & mask) {
|
|
|
|
uint32_t start_addr = bit_pos << 28;
|
|
|
|
LOG_F(INFO, "- 0x%X ... 0x%X", start_addr, start_addr + 0x0FFFFFFFU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verbose fine aka 16MB memory regions
|
|
|
|
for (mask = 0x1, bit_pos = 0; mask != 0x10000UL; mask <<= 1, bit_pos++) {
|
|
|
|
if (this->addr_mask & mask) {
|
|
|
|
uint32_t start_addr = (bit_pos << 24) + 0xF0000000UL;
|
|
|
|
LOG_F(INFO, "- 0x%X ... 0x%X", start_addr, start_addr + 0x00FFFFFFU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|