| 1 |
#include "IDEHD.hh" |
| 2 |
|
| 3 |
#include "DeviceConfig.hh" |
| 4 |
#include "MSXException.hh" |
| 5 |
#include "MSXMotherBoard.hh" |
| 6 |
#include "serialize.hh" |
| 7 |
|
| 8 |
#include "endian.hh" |
| 9 |
#include "narrow.hh" |
| 10 |
#include "xrange.hh" |
| 11 |
|
| 12 |
#include <cassert> |
| 13 |
|
| 14 |
namespace openmsx { |
| 15 |
|
| 16 |
IDEHD::IDEHD(const DeviceConfig& config) |
| 17 |
: HD(config) |
| 18 |
, AbstractIDEDevice(config.getMotherBoard()) |
| 19 |
, devName(config.getChildData("name", "openMSX hard disk")) |
| 20 |
{ |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
bool IDEHD::isPacketDevice() |
| 25 |
{ |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
std::string_view IDEHD::getDeviceName() |
| 30 |
{ |
| 31 |
return devName; |
| 32 |
} |
| 33 |
|
| 34 |
void IDEHD::fillIdentifyBlock(AlignedBuffer& buf) |
| 35 |
{ |
| 36 |
auto totalSectors = getNbSectors(); |
| 37 |
const uint16_t heads = 16; |
| 38 |
const uint16_t sectors = 32; |
| 39 |
auto cylinders = narrow<uint16_t>(totalSectors / size_t(heads * sectors)); |
| 40 |
Endian::writeL16(&buf[1 * 2], cylinders); |
| 41 |
Endian::writeL16(&buf[3 * 2], heads); |
| 42 |
Endian::writeL16(&buf[6 * 2], sectors); |
| 43 |
|
| 44 |
buf[47 * 2 + 0] = 16; |
| 45 |
buf[47 * 2 + 1] = 0x80; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
buf[49 * 2 + 1] = 0x0A; |
| 50 |
|
| 51 |
|
| 52 |
Endian::writeL32(&buf[60 * 2], unsigned(totalSectors)); |
| 53 |
} |
| 54 |
|
| 55 |
unsigned IDEHD::readBlockStart(AlignedBuffer& buf, unsigned count) |
| 56 |
{ |
| 57 |
try { |
| 58 |
assert(count >= 512); |
| 59 |
(void)count; |
| 60 |
readSector(transferSectorNumber, |
| 61 |
*aligned_cast<SectorBuffer*>(buf)); |
| 62 |
++transferSectorNumber; |
| 63 |
return 512; |
| 64 |
} catch (MSXException&) { |
| 65 |
abortReadTransfer(UNC); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void IDEHD::writeBlockComplete(AlignedBuffer& buf, unsigned count) |
| 71 |
{ |
| 72 |
try { |
| 73 |
assert((count % 512) == 0); |
| 74 |
size_t num = count / 512; |
| 75 |
for (auto i : xrange(num)) { |
| 76 |
writeSector(transferSectorNumber++, |
| 77 |
*aligned_cast<SectorBuffer*>(buf + 512 * i)); |
| 78 |
} |
| 79 |
} catch (MSXException&) { |
| 80 |
abortWriteTransfer(UNC); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void IDEHD::executeCommand(uint8_t cmd) |
| 85 |
{ |
| 86 |
if (0x10 <= cmd && cmd < 0x20) { |
| 87 |
|
| 88 |
setError(0); |
| 89 |
setByteCount(0); |
| 90 |
return; |
| 91 |
} |
| 92 |
switch (cmd) { |
| 93 |
case 0x20: |
| 94 |
case 0x21: |
| 95 |
case 0x30: |
| 96 |
case 0x31: { |
| 97 |
unsigned sectorNumber = getSectorNumber(); |
| 98 |
unsigned numSectors = getNumSectors(); |
| 99 |
if ((sectorNumber + numSectors) > getNbSectors()) { |
| 100 |
|
| 101 |
|
| 102 |
setError(IDNF); |
| 103 |
break; |
| 104 |
} |
| 105 |
transferSectorNumber = sectorNumber; |
| 106 |
if (cmd < 0x30) { |
| 107 |
startLongReadTransfer(numSectors * 512); |
| 108 |
} else { |
| 109 |
startWriteTransfer(numSectors * 512); |
| 110 |
} |
| 111 |
break; |
| 112 |
} |
| 113 |
|
| 114 |
case 0xF8: |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
setSectorNumber(unsigned(getNbSectors())); |
| 119 |
break; |
| 120 |
|
| 121 |
default: |
| 122 |
AbstractIDEDevice::executeCommand(cmd); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
template<typename Archive> |
| 128 |
void IDEHD::serialize(Archive& ar, unsigned ) |
| 129 |
{ |
| 130 |
|
| 131 |
ar.template serializeBase<HD>(*this); |
| 132 |
ar.template serializeBase<AbstractIDEDevice>(*this); |
| 133 |
ar.serialize("transferSectorNumber", transferSectorNumber); |
| 134 |
} |
| 135 |
INSTANTIATE_SERIALIZE_METHODS(IDEHD); |
| 136 |
REGISTER_POLYMORPHIC_INITIALIZER(IDEDevice, IDEHD, "IDEHD"); |
| 137 |
|
| 138 |
} |