2022-12-07 21:36:25 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-06-18 21:24:19 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2022-12-07 21:36:25 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** IDE Channel (aka IDE port) emulation.
|
|
|
|
|
|
|
|
One IDE channel is capable of controlling up to two IDE devices.
|
|
|
|
This class handles device registration and passing of messages
|
|
|
|
from and to the host.
|
|
|
|
*/
|
|
|
|
|
2023-06-18 21:24:19 +00:00
|
|
|
#include <devices/common/ata/atabasedevice.h>
|
2022-12-07 21:36:25 +00:00
|
|
|
#include <devices/common/ata/atadefs.h>
|
|
|
|
#include <devices/common/ata/idechannel.h>
|
|
|
|
#include <devices/common/hwcomponent.h>
|
|
|
|
#include <devices/deviceregistry.h>
|
2023-06-18 21:24:19 +00:00
|
|
|
#include <machines/machinebase.h>
|
2023-11-03 07:21:33 +00:00
|
|
|
#include <loguru.hpp>
|
2022-12-07 21:36:25 +00:00
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2022-12-08 07:04:09 +00:00
|
|
|
using namespace ata_interface;
|
|
|
|
|
2022-12-07 21:36:25 +00:00
|
|
|
IdeChannel::IdeChannel(const std::string name)
|
|
|
|
{
|
|
|
|
this->set_name(name);
|
|
|
|
this->supports_types(HWCompType::IDE_BUS);
|
|
|
|
|
2023-06-18 21:24:19 +00:00
|
|
|
this->device_stub = std::unique_ptr<AtaNullDevice>(new AtaNullDevice());
|
|
|
|
|
|
|
|
this->devices[0] = this->device_stub.get();
|
|
|
|
this->devices[1] = this->device_stub.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
int IdeChannel::device_postinit() {
|
|
|
|
this->int_ctrl = dynamic_cast<InterruptCtrl*>(
|
|
|
|
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
|
|
|
|
this->irq_id = this->int_ctrl->register_dev_int(
|
|
|
|
this->name == "IDE0" ? IntSrc::IDE0 : IntSrc::IDE1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdeChannel::register_device(int id, AtaInterface* dev_obj) {
|
|
|
|
if (id < 0 || id >= 2)
|
|
|
|
ABORT_F("%s: invalid device ID", this->name.c_str());
|
|
|
|
|
|
|
|
this->devices[id] = dev_obj;
|
|
|
|
|
|
|
|
((AtaBaseDevice*)dev_obj)->set_host(this);
|
2022-12-07 21:36:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 23:52:39 +00:00
|
|
|
uint32_t IdeChannel::read(const uint8_t reg_addr, const int size)
|
2022-12-07 21:36:25 +00:00
|
|
|
{
|
2022-12-11 23:08:43 +00:00
|
|
|
if (reg_addr == TIME_CONFIG) {
|
2022-12-08 23:52:39 +00:00
|
|
|
if (size != 4) {
|
|
|
|
LOG_F(WARNING, "%s: non-DWORD read from the channel config", this->name.c_str());
|
|
|
|
}
|
|
|
|
return this->ch_config;
|
|
|
|
} else {
|
|
|
|
return this->devices[this->cur_dev]->read(reg_addr);
|
|
|
|
}
|
2022-12-07 21:36:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 23:52:39 +00:00
|
|
|
void IdeChannel::write(const uint8_t reg_addr, const uint32_t val, const int size)
|
2022-12-07 21:36:25 +00:00
|
|
|
{
|
2022-12-11 23:08:43 +00:00
|
|
|
if (reg_addr == TIME_CONFIG) {
|
2022-12-08 23:52:39 +00:00
|
|
|
if (size != 4) {
|
|
|
|
LOG_F(WARNING, "%s: non-DWORD write to the channel config", this->name.c_str());
|
|
|
|
}
|
|
|
|
this->ch_config = val;
|
|
|
|
} else {
|
|
|
|
// keep track of the currently selected device
|
2022-12-11 23:08:43 +00:00
|
|
|
if (reg_addr == DEVICE_HEAD) {
|
2022-12-08 23:52:39 +00:00
|
|
|
this->cur_dev = (val >> 4) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// redirect register writes to both devices
|
|
|
|
for (auto& dev : this->devices) {
|
|
|
|
dev->write(reg_addr, val);
|
|
|
|
}
|
2022-12-07 21:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const DeviceDescription Ide0_Descriptor = {
|
|
|
|
IdeChannel::create_first, {}, {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const DeviceDescription Ide1_Descriptor = {
|
|
|
|
IdeChannel::create_second, {}, {}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_DEVICE(Ide0, Ide0_Descriptor);
|
|
|
|
REGISTER_DEVICE(Ide1, Ide1_Descriptor);
|