dingusppc/devices/common/ata/atabasedevice.cpp

124 lines
3.6 KiB
C++
Raw Normal View History

/*
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/>.
*/
/** @file Heathrow hard drive controller */
2022-12-07 21:36:25 +00:00
#include <devices/common/ata/atabasedevice.h>
#include <devices/common/ata/atadefs.h>
2022-10-30 22:38:09 +00:00
#include <devices/deviceregistry.h>
#include <loguru.hpp>
2022-12-07 21:36:25 +00:00
#include <cinttypes>
2022-12-08 07:04:09 +00:00
using namespace ata_interface;
2022-12-07 21:36:25 +00:00
AtaBaseDevice::AtaBaseDevice(const std::string name)
{
this->set_name(name);
supports_types(HWCompType::IDE_DEV);
2022-12-05 15:42:51 +00:00
device_reset();
}
void AtaBaseDevice::device_reset()
{
this->r_error = 1; // Device 0 passed, Device 1 passed or not present
this->r_sect_count = 1;
this->r_sect_num = 1;
2022-12-08 07:04:09 +00:00
this->r_dev_ctrl = 0;
// set ATA protocol signature
this->r_cylinder_lo = 0;
this->r_cylinder_hi = 0;
2022-12-08 07:04:09 +00:00
this->r_dev_head = 0; // TODO: ATAPI devices shouldn't change this reg!
2022-12-11 23:08:43 +00:00
this->r_status = DRDY | DSC;
}
2022-12-07 21:36:25 +00:00
uint16_t AtaBaseDevice::read(const uint8_t reg_addr) {
switch (reg_addr) {
2022-12-11 23:08:43 +00:00
case ATA_Reg::DATA:
LOG_F(WARNING, "Retrieving data from %s", this->name.c_str());
return 0xFFFFU;
2022-12-11 23:08:43 +00:00
case ATA_Reg::ERROR:
return this->r_error;
2022-12-11 23:08:43 +00:00
case ATA_Reg::SEC_COUNT:
return this->r_sect_count;
2022-12-11 23:08:43 +00:00
case ATA_Reg::SEC_NUM:
return this->r_sect_num;
2022-12-11 23:08:43 +00:00
case ATA_Reg::CYL_LOW:
return this->r_cylinder_lo;
2022-12-11 23:08:43 +00:00
case ATA_Reg::CYL_HIGH:
return this->r_cylinder_hi;
2022-12-11 23:08:43 +00:00
case ATA_Reg::DEVICE_HEAD:
return this->r_dev_head;
2022-12-11 23:08:43 +00:00
case ATA_Reg::STATUS:
// TODO: clear pending interrupt
return this->r_status;
2022-12-11 23:08:43 +00:00
case ATA_Reg::ALT_STATUS:
return this->r_status;
default:
2022-12-07 21:36:25 +00:00
LOG_F(WARNING, "Attempted to read unknown IDE register: %x", reg_addr);
return 0;
}
}
2022-12-07 21:36:25 +00:00
void AtaBaseDevice::write(const uint8_t reg_addr, const uint16_t value) {
switch (reg_addr) {
2022-12-11 23:08:43 +00:00
case ATA_Reg::DATA:
LOG_F(WARNING, "Pushing data to %s", this->name.c_str());
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::FEATURES:
this->r_features = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::SEC_COUNT:
this->r_sect_count = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::SEC_NUM:
this->r_sect_num = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::CYL_LOW:
this->r_cylinder_lo = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::CYL_HIGH:
this->r_cylinder_hi = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::DEVICE_HEAD:
this->r_dev_head = value;
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::COMMAND:
this->r_command = value;
2022-12-11 23:08:43 +00:00
if (is_selected() || this->r_command == DIAGNOSTICS) {
perform_command();
}
2022-11-27 04:34:54 +00:00
break;
2022-12-11 23:08:43 +00:00
case ATA_Reg::DEV_CTRL:
if (!(this->r_dev_ctrl & SRST) && (value & SRST)) {
2022-12-08 23:52:39 +00:00
LOG_F(INFO, "%s: soft reset triggered", this->name.c_str());
2022-12-11 23:08:43 +00:00
this->r_status |= BSY;
2022-12-08 23:52:39 +00:00
}
this->r_dev_ctrl = value;
2022-11-27 04:34:54 +00:00
break;
default:
2022-12-07 21:36:25 +00:00
LOG_F(WARNING, "Attempted to write unknown IDE register: %x", reg_addr);
}
2022-10-30 22:38:09 +00:00
}