From aa1d23e08bd2e7611061ce8c42d0ba6b8540d235 Mon Sep 17 00:00:00 2001 From: dingusdev <52434309+dingusdev@users.noreply.github.com> Date: Sat, 22 Oct 2022 11:41:52 -0700 Subject: [PATCH] Fixed hard disk support Compiles, still unfinished --- devices/CMakeLists.txt | 1 + devices/common/adb/adb.cpp | 2 +- devices/common/hwcomponent.h | 1 + devices/common/ide/heath_ide.cpp | 77 ------------ devices/common/ide/ide_hd.cpp | 117 +++++++++++++++++++ devices/common/ide/{heath_ide.h => ide_hd.h} | 38 ++++-- devices/common/scsi/scsi.h | 16 ++- devices/common/scsi/scsi_hd.cpp | 29 +++-- devices/common/scsi/scsi_hd.h | 14 ++- devices/ioctrl/heathrow.cpp | 6 +- devices/ioctrl/macio.h | 6 +- 11 files changed, 195 insertions(+), 112 deletions(-) delete mode 100644 devices/common/ide/heath_ide.cpp create mode 100644 devices/common/ide/ide_hd.cpp rename devices/common/ide/{heath_ide.h => ide_hd.h} (72%) diff --git a/devices/CMakeLists.txt b/devices/CMakeLists.txt index 36a654d..a4b05f8 100644 --- a/devices/CMakeLists.txt +++ b/devices/CMakeLists.txt @@ -6,6 +6,7 @@ file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/common/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/common/adb/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/common/i2c/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/common/ide/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/common/pci/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/common/scsi/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/ethernet/*.cpp" diff --git a/devices/common/adb/adb.cpp b/devices/common/adb/adb.cpp index 858da59..1f4aefa 100644 --- a/devices/common/adb/adb.cpp +++ b/devices/common/adb/adb.cpp @@ -402,7 +402,7 @@ bool ADB_Bus::adb_keybd_listen(int reg) { } bool ADB_Bus::adb_mouse_listen(int reg) { - if ((reg != 0) | (reg != 3)) { + if ((reg != 0) || (reg != 3)) { return false; } diff --git a/devices/common/hwcomponent.h b/devices/common/hwcomponent.h index 6543b1b..b1b11b8 100644 --- a/devices/common/hwcomponent.h +++ b/devices/common/hwcomponent.h @@ -43,6 +43,7 @@ enum HWCompType { SCSI_BUS = 1ULL << 20, /* SCSI bus */ SCSI_HOST = 1ULL << 21, /* SCSI host adapter */ SCSI_DEV = 1ULL << 22, /* SCSI device */ + IDE_DEV = 1ULL << 25, /* IDE device */ SND_SERVER = 1ULL << 31, /* host sound server */ FLOPPY_CTRL = 1ULL << 32, /* floppy disk controller */ FLOPPY_DRV = 1ULL << 33, /* floppy disk drive */ diff --git a/devices/common/ide/heath_ide.cpp b/devices/common/ide/heath_ide.cpp deleted file mode 100644 index 74286fc..0000000 --- a/devices/common/ide/heath_ide.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* -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 . -*/ - -/** @file Heathrow hard drive controller */ - -#include "heath_ide.h" -#include -#include -#include -#include - -#define sector_size 512 - -using namespace std; - -HeathIDE::HeathIDE(std::string filename) { - this->hdd_img; - - // Taken from: - // https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file/22986486 - this->hdd_img.ignore(std::numeric_limits::max()); - this->img_size = this->hdd_img.gcount(); - this->hdd_img.clear(); // Since ignore will have set eof. - this->hdd_img.seekg(0, std::ios_base::beg); -} - - -uint32_t HeathIDE::read(int reg) { - uint32_t res = 0; - switch (reg) { - case HEATH_DATA: - res = heath_regs[HEATH_DATA]; - case HEATH_ERROR: - res = heath_regs[HEATH_ERROR]; - case HEATH_SEC_COUNT: - res = heath_regs[HEATH_SEC_COUNT]; - case HEATH_SEC_NUM: - res = heath_regs[HEATH_SEC_NUM]; - default: - LOG_F(WARNING, "Attempted to read unknown IDE register: %x", reg); - } - - return res; -} - - -void HeathIDE::write(int reg, uint32_t value) { - switch (reg) { - case HEATH_DATA: - res = heath_regs[HEATH_DATA]; - case HEATH_CMD: - perform_command(value); - default: - LOG_F(WARNING, "Attempted to write unknown IDE register: %x", reg); - } - -void HeathIDE::perform_command(uint32_t command) { - LOG_F(WARNING, "Attempted to execute IDE command: %x", command); -} \ No newline at end of file diff --git a/devices/common/ide/ide_hd.cpp b/devices/common/ide/ide_hd.cpp new file mode 100644 index 0000000..3a7d97b --- /dev/null +++ b/devices/common/ide/ide_hd.cpp @@ -0,0 +1,117 @@ +/* +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 . +*/ + +/** @file Heathrow hard drive controller */ + +#include +#include +#include +#include +#include + +#define sector_size 512 + +using namespace std; + +IdeHardDisk::IdeHardDisk() { + this->name = "IDE0"; + supports_types(HWCompType::IDE_DEV); +} + +void IdeHardDisk::insert_image(std::string filename) { + hdd_img.open(filename); + + // Taken from: + // https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file/22986486 + hdd_img.ignore(std::numeric_limits::max()); + img_size = this->hdd_img.gcount(); + hdd_img.clear(); // Since ignore will have set eof. + hdd_img.seekg(0, std::ios_base::beg); +} + +uint32_t IdeHardDisk::read(int reg) { + switch (reg) { + case IDE_Reg::IDE_DATA: + LOG_F(0, "Retrieving DATA from IDE: %x", regs[IDE_Reg::IDE_DATA]); + return regs[IDE_Reg::IDE_DATA]; + case IDE_Reg::ERROR: + return regs[IDE_Reg::ERROR]; + case IDE_Reg::SEC_COUNT: + return regs[IDE_Reg::SEC_COUNT]; + case IDE_Reg::SEC_NUM: + return regs[IDE_Reg::SEC_NUM]; + case IDE_Reg::CYL_LOW: + return regs[IDE_Reg::CYL_LOW]; + case IDE_Reg::CYL_HIGH: + return regs[IDE_Reg::CYL_HIGH]; + case IDE_Reg::DRIVE_HEAD: + return regs[IDE_Reg::DRIVE_HEAD]; + case IDE_Reg::STATUS: + return regs[IDE_Reg::STATUS]; + case IDE_Reg::ALT_STATUS: + return regs[IDE_Reg::ALT_STATUS]; + case IDE_Reg::TIME_CONFIG: + return regs[IDE_Reg::TIME_CONFIG]; + default: + LOG_F(WARNING, "Attempted to read unknown IDE register: %x", reg); + return 0x0; + } + +} + +void IdeHardDisk::write(int reg, uint32_t value) { + switch (reg) { + case IDE_Reg::IDE_DATA: + regs[IDE_Reg::IDE_DATA] = value; + case IDE_Reg::FEATURES: + regs[IDE_Reg::FEATURES] = value; + case IDE_Reg::SEC_COUNT: + regs[IDE_Reg::SEC_COUNT] = value; + case IDE_Reg::SEC_NUM: + regs[IDE_Reg::SEC_NUM] = value; + case IDE_Reg::CYL_LOW: + regs[IDE_Reg::CYL_LOW] = value; + case IDE_Reg::CYL_HIGH: + regs[IDE_Reg::CYL_HIGH] = value; + case IDE_Reg::DRIVE_HEAD: + regs[IDE_Reg::DRIVE_HEAD] = value; + case IDE_Reg::COMMAND: + LOG_F(0, "Executing COMMAND for IDE: %x", value); + perform_command(value); + case IDE_Reg::DEV_CTRL: + regs[IDE_Reg::DEV_CTRL] = value; + case IDE_Reg::TIME_CONFIG: + regs[IDE_Reg::TIME_CONFIG] = value; + default: + LOG_F(WARNING, "Attempted to write unknown IDE register: %x", reg); + } +} + +void IdeHardDisk::perform_command(uint32_t command) { + switch (command) { + case IDE_Cmd::READ_SECTOR: + LOG_F(WARNING, "Trying to read sector with: %x", command); + case IDE_Cmd::WRITE_SECTOR: + LOG_F(WARNING, "Trying to write sector with: %x", command); + default: + LOG_F(WARNING, "Attempted to execute IDE command: %x", command); + } +} \ No newline at end of file diff --git a/devices/common/ide/heath_ide.h b/devices/common/ide/ide_hd.h similarity index 72% rename from devices/common/ide/heath_ide.h rename to devices/common/ide/ide_hd.h index e0d65cd..2120909 100644 --- a/devices/common/ide/heath_ide.h +++ b/devices/common/ide/ide_hd.h @@ -21,16 +21,21 @@ along with this program. If not, see . /** @file ATA hard drive support */ -#ifndef ATA_HD_H -#define ATA_HD_H +#ifndef IDE_HD_H +#define IDE_HD_H +#include #include +#include +#include #include #include #define SEC_SIZE 512 -/** Heath IDE register offsets. */ +using namespace std; + +/** IDE register offsets. */ enum IDE_Reg : int { IDE_DATA = 0x0, ERROR = 0x1, // error (read) @@ -41,13 +46,13 @@ enum IDE_Reg : int { CYL_HIGH = 0x5, // cylinder high DRIVE_HEAD = 0x6, // drive/head STATUS = 0x7, // status (read) - CMD = 0x7, // command (write) + COMMAND = 0x7, // command (write) ALT_STATUS = 0x16, // alt status (read) DEV_CTRL = 0x16, // device control (write) - TIME_CONFIG = 0x20, + TIME_CONFIG = 0x20 }; -enum IDE_Status { +enum IDE_Status : int { ERR = 0x1, IDX = 0x2, CORR = 0x4, @@ -59,24 +64,39 @@ enum IDE_Status { }; /** Heath IDE commands. */ -enum IDE_Cmd { +enum IDE_Cmd : int { + IDE_NOP = 0x00, RESET_ATAPI = 0x08, RECALIBRATE = 0x10, READ_SECTOR = 0x20, + READ_LONG = 0x22, WRITE_SECTOR = 0x30, + WRITE_LONG = 0x32, + WRITE_VERIFY = 0x40, FORMAT_TRACKS = 0x50, DIAGNOSTICS = 0x90, + READ_DMA = 0xC8, + WRITE_DMA = 0xCA, }; -class HeathIDE : public HWComponent { +class IdeHardDisk : public HWComponent { public: - HeathIDE(std::string filename); + IdeHardDisk(); + ~IdeHardDisk() = default; + + static std::unique_ptr create() { + return std::unique_ptr(new IdeHardDisk()); + } + + void insert_image(std::string filename); uint32_t read(int reg); void write(int reg, uint32_t value); void perform_command(uint32_t command); private: + std::fstream hdd_img; + uint64_t img_size; uint32_t regs[33]; uint8_t buffer[SEC_SIZE]; }; diff --git a/devices/common/scsi/scsi.h b/devices/common/scsi/scsi.h index 9198483..1bff2b9 100644 --- a/devices/common/scsi/scsi.h +++ b/devices/common/scsi/scsi.h @@ -58,7 +58,7 @@ enum ScsiMsg : int { BUS_PHASE_CHANGE, }; -enum ScsiCommamd : int { +enum ScsiCommand : int { TEST_UNITY_READY = 0x0, REWIND = 0x1, REQ_SENSE = 0x3, @@ -94,8 +94,19 @@ enum ScsiSense : int { BLANK_CHECK = 0x8, VOL_OVERFLOW = 0xD, MISCOMPARE = 0xE, - COMPLETED = 0xF, + COMPLETED = 0xF +}; +enum ScsiError : int { + NO_ERROR = 0x0, + NO_SECTOR = 0x1, + WRITE_FAULT = 0x3, + DEV_NOT_READY = 0x4, + INVALID_CMD = 0x20, + INVALID_LBA = 0x21, + INVALID_CDB = 0x24, + INVALID_LUN = 0x25, + WRITE_PROTECT = 0x27 }; /** Standard SCSI bus timing values measured in ns. */ @@ -114,7 +125,6 @@ public: ~ScsiDevice() = default; virtual void notify(ScsiMsg msg_type, int param) = 0; - virtual void select() = 0; private: int scsi_id; diff --git a/devices/common/scsi/scsi_hd.cpp b/devices/common/scsi/scsi_hd.cpp index ac7b8db..79c4374 100644 --- a/devices/common/scsi/scsi_hd.cpp +++ b/devices/common/scsi/scsi_hd.cpp @@ -34,7 +34,7 @@ along with this program. If not, see . using namespace std; -SCSI_HD::SCSI_HD() { +ScsiHardDisk::ScsiHardDisk() { supports_types(HWCompType::SCSI_DEV); std::string hd_image_path = GET_STR_PROP("hdd_img"); @@ -51,32 +51,37 @@ SCSI_HD::SCSI_HD() { this->hdd_img.seekg(0, std::ios_base::beg); } -int SCSI_HD::test_unit_ready() { +int ScsiHardDisk::test_unit_ready() { return 0x0; } -int SCSI_HD::req_sense(uint8_t alloc_len) { +int ScsiHardDisk::req_sense(uint8_t alloc_len) { if (alloc_len != 252) { LOG_F(WARNING, "Inappropriate Allocation Length: %%d", alloc_len); } - return 0x0; //placeholder - no sense + return ScsiError::NO_ERROR; // placeholder - no sense } -int SCSI_HD::inquiry() { +int ScsiHardDisk::inquiry() { return 0x1000000F; } -int SCSI_HD::send_diagnostic() { +int ScsiHardDisk::send_diagnostic() { return 0x0; } -int SCSI_HD::read_capacity_10() { +int ScsiHardDisk::mode_select() { + return 0x0; +} + +uint64_t ScsiHardDisk::read_capacity_10() { return this->img_size; } -void SCSI_HD::format() {} +void ScsiHardDisk::format() { +} -void SCSI_HD::read(uint32_t lba, uint16_t transfer_len) { +void ScsiHardDisk::read(uint32_t lba, uint16_t transfer_len) { uint32_t transfer_size = (transfer_len == 0) ? 256 : transfer_len; transfer_size *= sector_size; uint32_t device_offset = lba * sector_size; @@ -85,7 +90,7 @@ void SCSI_HD::read(uint32_t lba, uint16_t transfer_len) { this->hdd_img.read(img_buffer, transfer_size); } -void SCSI_HD::write(uint32_t lba, uint16_t transfer_len) { +void ScsiHardDisk::write(uint32_t lba, uint16_t transfer_len) { uint32_t transfer_size = (transfer_len == 0) ? 256 : transfer_len; transfer_size *= sector_size; uint32_t device_offset = lba * sector_size; @@ -95,11 +100,11 @@ void SCSI_HD::write(uint32_t lba, uint16_t transfer_len) { } -void SCSI_HD::seek(uint32_t lba) { +void ScsiHardDisk::seek(uint32_t lba) { this->hdd_img.seekg(0, this->hdd_img.beg); } -void SCSI_HD::rewind() { +void ScsiHardDisk::rewind() { this->hdd_img.seekg(0, this->hdd_img.beg); } diff --git a/devices/common/scsi/scsi_hd.h b/devices/common/scsi/scsi_hd.h index 0f0a5b8..8288c6f 100644 --- a/devices/common/scsi/scsi_hd.h +++ b/devices/common/scsi/scsi_hd.h @@ -30,18 +30,20 @@ along with this program. If not, see . #include #include -class SCSI_HD : public ScsiDevice { +class ScsiHardDisk : public ScsiDevice { public: - SCSI_HD(); - ~SCSI_HD() = default; + ScsiHardDisk(); + ~ScsiHardDisk() = default; virtual void notify(ScsiMsg msg_type, int param) = 0; int test_unit_ready(); int req_sense(uint8_t alloc_len); - int read_capacity_10(); int inquiry(); int send_diagnostic(); + int mode_select(); + + uint64_t read_capacity_10(); void format(); void read(uint32_t lba, uint16_t transfer_len); @@ -57,6 +59,10 @@ protected: uint8_t scsi_command[12]; uint64_t file_offset = 0; uint8_t status; + + //inquiry info + char vendor_info[8] = {'D', 'i', 'n', 'g', 'u', 's', 'D', '\0'}; + char prod_info[16] = {'E', 'm', 'u', 'l', 'a', 't', 'e', 'd', ' ', 'D', 'i', 's', 'k', '\0'}; }; #endif \ No newline at end of file diff --git a/devices/ioctrl/heathrow.cpp b/devices/ioctrl/heathrow.cpp index 5b88d0c..2e6ceb0 100644 --- a/devices/ioctrl/heathrow.cpp +++ b/devices/ioctrl/heathrow.cpp @@ -24,7 +24,7 @@ along with this program. If not, see . #include #include #include -#include +#include #include #include #include @@ -80,7 +80,7 @@ HeathrowIC::HeathrowIC() : PCIDevice("mac-io/heathrow"), InterruptCtrl() this->mesh = dynamic_cast(gMachineObj->get_comp_by_name("Mesh")); // connect IDE HW - this->ide_0 = dynamic_cast(gMachineObj->get_comp_by_name("IDE0")); + this->ide_0 = dynamic_cast(gMachineObj->get_comp_by_name("IDE0")); // connect serial HW this->escc = dynamic_cast(gMachineObj->get_comp_by_name("Escc")); @@ -214,7 +214,7 @@ void HeathrowIC::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int this->viacuda->write((offset - 0x16000) >> 9, value); break; case 0x20: - this->ide_0->write((offset - 0x20000) >> 9, value); + this->ide_0->write((offset - 0x20000) >> 4, value); break; default: if (sub_addr >= 0x60) { diff --git a/devices/ioctrl/macio.h b/devices/ioctrl/macio.h index 3dd3962..786f418 100644 --- a/devices/ioctrl/macio.h +++ b/devices/ioctrl/macio.h @@ -54,7 +54,7 @@ along with this program. If not, see . #include #include #include -#include +#include #include #include #include @@ -138,7 +138,7 @@ private: MaceController* mace; ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it EsccController* escc; // ESCC serial controller - HeathIDE* ide_0; // Internal ATA + IdeHardDisk* ide_0; // Internal ATA Sc53C94* scsi_0; // external SCSI Swim3::Swim3Ctrl* swim3; // floppy disk controller @@ -231,7 +231,7 @@ private: ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it MESHController* mesh; // MESH SCSI cell instance EsccController* escc; // ESCC serial controller - HeathIDE* ide_0; // Internal ATA + IdeHardDisk* ide_0; // Internal ATA Swim3::Swim3Ctrl* swim3; // floppy disk controller std::unique_ptr snd_out_dma;