Initial prototyping for hard disks

This commit is contained in:
dingusdev 2022-09-01 22:10:52 -07:00
parent 5b08f283e5
commit a61dd5701a
5 changed files with 206 additions and 0 deletions

View File

@ -58,6 +58,42 @@ enum ScsiMsg : int {
BUS_PHASE_CHANGE, BUS_PHASE_CHANGE,
}; };
enum ScsiCommamd : int {
SCSI_TEST_UNITY_READY = 0x0,
SCSI_REWIND = 0x1,
SCSI_REQ_SENSE = 0x3,
SCSI_FORMAT = 0x4,
SCSI_READ_BLK_LIMITS = 0x5,
SCSI_READ_6 = 0x8,
SCSI_WRITE_6 = 0xA,
SCSI_SEEK_6 = 0xB,
SCSI_VERIFY_6 = 0x13,
SCSI_MODE_SELECT_6 = 0x15,
SCSI_ERASE_6 = 0x19,
SCSI_MODE_SENSE_6 = 0x1A,
SCSI_DIAG_RESULTS = 0x1C,
SCSI_READ_CAPAC_10 = 0x25,
SCSI_READ_10 = 0x28,
SCSI_WRITE_10 = 0x2A,
SCSI_VERIFY_10 = 0x2F,
SCSI_READ_LONG_10 = 0x35,
};
enum ScsiSense : int {
SCSI_NO_SENSE = 0x0,
SCSI_RECOVERED = 0x1,
SCSI_NOT_READY = 0x2,
SCSI_MEDIA_ERR = 0x3,
SCSI_HW_ERROR = 0x4,
SCSI_ILLEGAL_REQ = 0x5,
SCSI_UNIT_ATTENTION = 0x6,
SCSI_DATA_PROTECT = 0x7,
SCSI_BLANK_CHECK = 0x8,
SCSI_MISCOMPARE = 0xE,
SCSI_COMPLETED = 0xF,
};
/** Standard SCSI bus timing values measured in ns. */ /** Standard SCSI bus timing values measured in ns. */
#define BUS_SETTLE_DELAY 400 #define BUS_SETTLE_DELAY 400
#define BUS_FREE_DELAY 800 #define BUS_FREE_DELAY 800

View File

@ -0,0 +1,19 @@
#include "ata_hd.h"
#include <fstream>
#include <limits>
#include <stdio.h>
#define sector_size 512
using namespace std;
HardDrive::HardDrive(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<std::streamsize>::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);
}

View File

@ -0,0 +1,17 @@
/** @file ATA hard drive support */
#ifndef SCSI_HD_H
#define SCSI_HD_H
#include <cinttypes>
#include <stdio.h>
#include <string>
class ATAHardDrive {
public:
ATAHardDrive(std::string filename);
}
#endif

View File

@ -0,0 +1,72 @@
#include "scsi_hd.h"
#include <fstream>
#include <limits>
#include <stdio.h>
#define sector_size 512
using namespace std;
HardDrive::HardDrive(std::string filename) {
supports_types(HWCompType::SCSI_DEV);
this->hdd_img.open(filename, ios::out | ios::in | ios::binary);
// Taken from:
// https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file/22986486
this->hdd_img.ignore(std::numeric_limits<std::streamsize>::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);
}
int HardDrive::test_unit_ready(){
return 0x0;
}
int HardDrive::req_sense(uint8_t alloc_len) {
if (alloc_len != 252) {
LOG_F(WARNING, "Inappropriate Allocation Length: %%d", alloc_len);
}
return 0x0; //placeholder - no sense
}
int HardDrive::inquiry() {
return 0x1000000F;
}
int HardDrive::send_diagnostic() {
return 0x0;
}
int HardDrive::read_capacity_10() {
return this->img_size;
}
void HardDrive::format() {
}
void HardDrive::read(uint32_t lba, uint16_t transfer_len) {
uint32_t final_transfer = (transfer_len == 0) ? 256 : transfer_len;
final_transfer = transfer_len * sector_size;
this->hdd_img.seekg(lba * sector_size);
img_buffer = this->hdd_img.read(memblock, size);
}
void HardDrive::write(uint32_t lba, uint16_t transfer_len) {
uint32_t final_transfer = (transfer_len == 0) ? 256 : transfer_len;
final_transfer = transfer_len * sector_size;
this->hdd_img.seekg(device_offset, this->hdd_img.beg);
this->hdd_img.write((char*)&read_buffer, sector_size);
}
void HardDrive::seek(uint32_t lba) {
}
void HardDrive::rewind() {
this->hdd_img.seekg(0, disk_image.beg);
}

View File

@ -0,0 +1,62 @@
/*
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 SCSI hard drive support */
#ifndef SCSI_HD_H
#define SCSI_HD_H
#include <devices/common/scsi/scsi.h>
#include <cinttypes>
#include <stdio.h>
#include <string>
class HardDrive : public ScsiDevice {
public:
HardDrive(std::string filename);
~HardDrive() = default;
virtual void notify(ScsiMsg msg_type, int param){
return param;
};
int test_unit_ready();
int req_sense(uint8_t alloc_len);
int read_capacity_10();
int inquiry();
int send_diagnostic();
void format();
void read(uint32_t lba, uint16_t transfer_len);
void write(uint32_t lba, uint16_t transfer_len);
void seek(uint32_t lba);
void rewind();
protected:
std::string img_path;
fstream hdd_img;
uint64_t img_size;
char img_buffer[1 << 17];
uint8_t scsi_command[12];
uint64_t file_offset = 0;
}
#endif