Base class for ATAPI devices.

This commit is contained in:
Maxim Poliakovski 2023-05-15 17:56:14 +02:00
parent e36150a5ca
commit 586828b375
4 changed files with 253 additions and 16 deletions

View File

@ -19,7 +19,7 @@ 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 */
/** @file Basic ATA device emulation. */
#include <devices/common/ata/atabasedevice.h>
#include <devices/common/ata/atadefs.h>
@ -53,17 +53,10 @@ void AtaBaseDevice::device_set_signature() {
this->r_sect_num = 1;
this->r_dev_head = 0;
// set protocol signature
if (this->device_type == DEVICE_TYPE_ATAPI) {
this->r_cylinder_lo = 0x14;
this->r_cylinder_hi = 0xEB;
this->r_status_save = this->r_status; // for restoring on the first command
this->r_status = 0;
} else { // assume ATA by default
this->r_cylinder_lo = 0;
this->r_cylinder_hi = 0;
this->r_status = DRDY | DSC; // DSC=1 is required for ATA devices
}
// set ATA protocol signature
this->r_cylinder_lo = 0;
this->r_cylinder_hi = 0;
this->r_status = DRDY | DSC; // DSC=1 is required for ATA devices
}
uint16_t AtaBaseDevice::read(const uint8_t reg_addr) {

View File

@ -29,6 +29,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <devices/common/hwcomponent.h>
#include <cinttypes>
#include <string>
class AtaBaseDevice : public HWComponent, public AtaInterface
{
@ -49,14 +50,13 @@ public:
this->r_error &= 0x7F;
};
void device_reset(bool is_soft_reset);
void device_set_signature();
virtual void device_reset(bool is_soft_reset);
virtual void device_set_signature();
void device_control(const uint8_t new_ctrl);
private:
protected:
bool is_selected() { return ((this->r_dev_head >> 4) & 1) == this->my_dev_id; };
protected:
uint8_t my_dev_id = 0; // my IDE device ID configured by the host
uint8_t device_type = ata_interface::DEVICE_TYPE_UNKNOWN;
@ -74,6 +74,11 @@ protected:
uint8_t r_status;
uint8_t r_status_save;
uint8_t r_dev_ctrl = 0x08;
uint16_t *data_ptr = nullptr;
uint8_t data_buf[512] = {};
int data_pos = 0;
int xfer_cnt = 0;
};
#endif // ATA_BASE_DEVICE_H

View File

@ -0,0 +1,185 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 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 Basic ATAPI interface emulation. */
#include <devices/common/ata/atabasedevice.h>
#include <devices/common/ata/atapibasedevice.h>
#include <devices/common/ata/atadefs.h>
#include <devices/common/scsi/scsi.h> // ATAPI reuses SCSI commands (sic!)
#include <endianswap.h>
#include <loguru.hpp>
#include <memaccess.h>
#include <cinttypes>
using namespace ata_interface;
AtapiBaseDevice::AtapiBaseDevice(const std::string name)
: AtaBaseDevice(name, DEVICE_TYPE_ATAPI) {
}
void AtapiBaseDevice::device_set_signature() {
this->r_sect_count = 1;
this->r_sect_num = 1;
this->r_dev_head = 0;
// set ATAPI protocol signature
this->r_byte_count = 0xEB14;
this->r_status_save = this->r_status; // for restoring on the first command
this->r_status = 0;
}
uint16_t AtapiBaseDevice::read(const uint8_t reg_addr) {
switch (reg_addr) {
case ATA_Reg::DATA:
if (this->data_ptr && this->xfer_cnt) {
this->xfer_cnt -= 2;
if (this->xfer_cnt <= 0) {
this->r_status &= ~DRQ;
}
return *this->data_ptr++;
} else {
return 0xFFFFU;
}
case ATA_Reg::ERROR:
return this->r_error;
case ATAPI_Reg::INT_REASON:
return this->r_int_reason;
case ATAPI_Reg::BYTE_COUNT_LO:
return this->r_byte_count & 0xFFU;
case ATAPI_Reg::BYTE_COUNT_HI:
return (this->r_byte_count >> 8) & 0xFFU;
case ATA_Reg::DEVICE_HEAD:
return this->r_dev_head;
case ATA_Reg::STATUS:
// TODO: clear pending interrupt
return this->r_status;
case ATA_Reg::ALT_STATUS:
return this->r_status;
default:
LOG_F(WARNING, "Attempted to read unknown register: %x", reg_addr);
return 0;
}
}
void AtapiBaseDevice::write(const uint8_t reg_addr, const uint16_t value) {
switch (reg_addr) {
case ATA_Reg::DATA:
if (this->data_ptr && this->xfer_cnt) {
*this->data_ptr++ = BYTESWAP_16(value);
this->xfer_cnt -= 2;
if (this->xfer_cnt <= 0) {
this->r_status &= ~DRQ;
if (this->r_int_reason & CoD) {
this->perform_packet_command();
}
}
}
break;
case ATA_Reg::FEATURES:
this->r_features = value;
break;
case ATA_Reg::SEC_COUNT:
case ATA_Reg::SEC_NUM:
break; // unused in ATAPI, NoOp them here for compatibility
case ATAPI_Reg::BYTE_COUNT_LO:
this->r_byte_count = (this->r_byte_count & 0xFF00U) | (value & 0xFFU);
break;
case ATAPI_Reg::BYTE_COUNT_HI:
this->r_byte_count = (this->r_byte_count & 0xFFU) | ((value & 0xFFU) << 8);
break;
case ATA_Reg::DEVICE_HEAD:
this->r_dev_head = value;
break;
case ATA_Reg::COMMAND:
this->r_command = value;
if (is_selected() || this->r_command == DIAGNOSTICS) {
perform_command();
}
break;
case ATA_Reg::DEV_CTRL:
this->device_control(value);
break;
default:
LOG_F(WARNING, "Attempted to write unknown IDE register: %x", reg_addr);
}
}
int AtapiBaseDevice::perform_command() {
this->r_status |= BSY;
switch (this->r_command) {
case ATAPI_PACKET:
this->data_ptr = (uint16_t *)this->cmd_pkt;
this->xfer_cnt = sizeof(this->cmd_pkt);
this->r_int_reason &= ~ATAPI_Int_Reason::IO; // host->device
this->r_int_reason |= ATAPI_Int_Reason::CoD; // command packet
this->r_status |= DRQ;
this->r_status &= ~BSY;
break;
case ATAPI_IDFY_DEV:
LOG_F(INFO, "ATAPI_IDENTIFY issued");
this->data_buf[0] = 0x85;
this->data_buf[1] = 0xC0;
this->data_ptr = (uint16_t *)this->data_buf;
this->data_pos = 0;
this->xfer_cnt = 512;
this->r_status |= DRQ;
this->r_status &= ~BSY;
break;
case SET_FEATURES:
LOG_F(INFO, "%s: SET_FEATURES #%d", this->name.c_str(), this->r_features);
this->r_status &= ~BSY;
break;
default:
LOG_F(ERROR, "%s: unsupported command 0x%X", this->name.c_str(), this->r_command);
return -1;
}
return 0;
}
void AtapiBaseDevice::perform_packet_command() {
uint32_t lba, xfer_len;
this->r_status |= BSY;
switch (this->cmd_pkt[0]) {
case ScsiCommand::TEST_UNIT_READY:
this->r_status &= ~BSY;
break;
case ScsiCommand::START_STOP_UNIT:
this->r_status &= ~BSY;
break;
case ScsiCommand::READ_12:
lba = READ_DWORD_BE_U(&this->cmd_pkt[2]);
xfer_len = READ_DWORD_BE_U(&this->cmd_pkt[6]);
this->r_int_reason |= ~ATAPI_Int_Reason::IO; // device->host
this->r_int_reason &= ATAPI_Int_Reason::CoD; // data
this->xfer_cnt = xfer_len * 2048;
this->r_status |= DRQ;
this->r_status &= ~BSY;
break;
default:
LOG_F(INFO, "%s: unsupported ATAPI command 0x%X", this->name.c_str(),
this->cmd_pkt[0]);
}
}

View File

@ -0,0 +1,54 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 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 Base class for ATAPI devices. */
#ifndef ATAPI_BASE_DEVICE_H
#define ATAPI_BASE_DEVICE_H
#include <devices/common/ata/atabasedevice.h>
#include <devices/common/ata/atadefs.h>
#include <cinttypes>
#include <string>
class AtapiBaseDevice : public AtaBaseDevice
{
public:
AtapiBaseDevice(const std::string name);
~AtapiBaseDevice() = default;
void device_set_signature() override;
uint16_t read(const uint8_t reg_addr) override;
void write(const uint8_t reg_addr, const uint16_t value) override;
int perform_command() override;
void perform_packet_command();
private:
uint8_t r_int_reason;
uint16_t r_byte_count;
uint8_t cmd_pkt[12] = {};
};
#endif // ATAPI_BASE_DEVICE_H