| 1 |
#include "SectorBasedDisk.hh" |
| 2 |
|
| 3 |
#include "MSXException.hh" |
| 4 |
|
| 5 |
#include "narrow.hh" |
| 6 |
#include "xrange.hh" |
| 7 |
|
| 8 |
#include <cassert> |
| 9 |
|
| 10 |
namespace openmsx { |
| 11 |
|
| 12 |
SectorBasedDisk::SectorBasedDisk(DiskName name_) |
| 13 |
: Disk(std::move(name_)) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
void SectorBasedDisk::writeTrackImpl(uint8_t track, uint8_t side, const RawTrack& input) |
| 18 |
{ |
| 19 |
for (const auto& s : input.decodeAll()) { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if ((s.sector < 1) || (s.sector > getSectorsPerTrack())) continue; |
| 26 |
SectorBuffer buf; |
| 27 |
input.readBlock(s.dataIdx, buf.raw); |
| 28 |
auto logicalSector = physToLog(track, side, s.sector); |
| 29 |
writeSector(logicalSector, buf); |
| 30 |
|
| 31 |
|
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
void SectorBasedDisk::readTrack(uint8_t track, uint8_t side, RawTrack& output) |
| 36 |
{ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
checkCaches(); |
| 46 |
int num = track | (side << 8); |
| 47 |
if (num == cachedTrackNum) { |
| 48 |
output = cachedTrackData; |
| 49 |
return; |
| 50 |
} |
| 51 |
cachedTrackNum = num; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
try { |
| 85 |
output.clear(RawTrack::STANDARD_SIZE); |
| 86 |
|
| 87 |
int idx = 0; |
| 88 |
auto write = [&](unsigned n, uint8_t value) { |
| 89 |
repeat(n, [&] { output.write(idx++, value); }); |
| 90 |
}; |
| 91 |
|
| 92 |
write(80, 0x4E); |
| 93 |
write(12, 0x00); |
| 94 |
write( 3, 0xC2); |
| 95 |
write( 1, 0xFC); |
| 96 |
write(50, 0x4E); |
| 97 |
|
| 98 |
for (auto j : xrange(9)) { |
| 99 |
write(12, 0x00); |
| 100 |
|
| 101 |
write( 3, 0xA1); |
| 102 |
output.write(idx++, 0xFE, true); |
| 103 |
output.write(idx++, track); |
| 104 |
output.write(idx++, side); |
| 105 |
output.write(idx++, narrow<uint8_t>(j + 1)); |
| 106 |
output.write(idx++, 0x02); |
| 107 |
uint16_t addrCrc = output.calcCrc(idx - 8, 8); |
| 108 |
output.write(idx++, narrow_cast<uint8_t>(addrCrc >> 8)); |
| 109 |
output.write(idx++, narrow_cast<uint8_t>(addrCrc & 0xff)); |
| 110 |
|
| 111 |
write(22, 0x4E); |
| 112 |
write(12, 0x00); |
| 113 |
|
| 114 |
write( 3, 0xA1); |
| 115 |
write( 1, 0xFB); |
| 116 |
|
| 117 |
auto logicalSector = physToLog(track, side, narrow<uint8_t>(j + 1)); |
| 118 |
SectorBuffer buf; |
| 119 |
readSector(logicalSector, buf); |
| 120 |
for (auto r : buf.raw) output.write(idx++, r); |
| 121 |
|
| 122 |
uint16_t dataCrc = output.calcCrc(idx - (512 + 4), 512 + 4); |
| 123 |
output.write(idx++, narrow_cast<uint8_t>(dataCrc >> 8)); |
| 124 |
output.write(idx++, narrow_cast<uint8_t>(dataCrc & 0xff)); |
| 125 |
|
| 126 |
write(84, 0x4E); |
| 127 |
} |
| 128 |
|
| 129 |
write(182, 0x4E); |
| 130 |
assert(idx == RawTrack::STANDARD_SIZE); |
| 131 |
} catch (MSXException& ) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
output.clear(RawTrack::STANDARD_SIZE); |
| 138 |
cachedTrackNum = -1; |
| 139 |
} |
| 140 |
cachedTrackData = output; |
| 141 |
} |
| 142 |
|
| 143 |
void SectorBasedDisk::flushCaches() |
| 144 |
{ |
| 145 |
Disk::flushCaches(); |
| 146 |
cachedTrackNum = -1; |
| 147 |
} |
| 148 |
|
| 149 |
size_t SectorBasedDisk::getNbSectorsImpl() |
| 150 |
{ |
| 151 |
assert(nbSectors != size_t(-1)); |
| 152 |
return nbSectors; |
| 153 |
} |
| 154 |
void SectorBasedDisk::setNbSectors(size_t num) |
| 155 |
{ |
| 156 |
assert(nbSectors == size_t(-1)); |
| 157 |
nbSectors = num; |
| 158 |
} |
| 159 |
|
| 160 |
void SectorBasedDisk::detectGeometry() |
| 161 |
{ |
| 162 |
|
| 163 |
|
| 164 |
if (getNbSectors() == 1440) { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
setSectorsPerTrack(9); |
| 178 |
setNbSides(2); |
| 179 |
|
| 180 |
} else { |
| 181 |
|
| 182 |
|
| 183 |
Disk::detectGeometry(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
} |