| 1 |
#include "RawTrack.hh" |
| 2 |
|
| 3 |
#include "CRC16.hh" |
| 4 |
#include "enumerate.hh" |
| 5 |
#include "one_of.hh" |
| 6 |
#include "ranges.hh" |
| 7 |
#include "serialize.hh" |
| 8 |
#include "serialize_stl.hh" |
| 9 |
#include "small_buffer.hh" |
| 10 |
#include "xrange.hh" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cassert> |
| 14 |
|
| 15 |
namespace openmsx { |
| 16 |
|
| 17 |
RawTrack::RawTrack(unsigned size) |
| 18 |
{ |
| 19 |
clear(size); |
| 20 |
} |
| 21 |
|
| 22 |
void RawTrack::clear(unsigned size) |
| 23 |
{ |
| 24 |
idam.clear(); |
| 25 |
data.assign(size, 0x4e); |
| 26 |
} |
| 27 |
|
| 28 |
void RawTrack::addIdam(unsigned idx) |
| 29 |
{ |
| 30 |
assert(idx < data.size()); |
| 31 |
assert(idam.empty() || (idx > idam.back())); |
| 32 |
idam.push_back(idx); |
| 33 |
} |
| 34 |
|
| 35 |
void RawTrack::write(int idx, uint8_t val, bool setIdam) |
| 36 |
{ |
| 37 |
unsigned i2 = wrapIndex(idx); |
| 38 |
auto it = std::ranges::lower_bound(idam, i2); |
| 39 |
if (setIdam) { |
| 40 |
|
| 41 |
if ((it == end(idam)) || (*it != i2)) { |
| 42 |
idam.insert(it, i2); |
| 43 |
} |
| 44 |
} else { |
| 45 |
|
| 46 |
if ((it != end(idam)) && (*it == i2)) { |
| 47 |
idam.erase(it); |
| 48 |
} |
| 49 |
} |
| 50 |
data[i2] = val; |
| 51 |
} |
| 52 |
|
| 53 |
std::optional<RawTrack::Sector> RawTrack::decodeSectorImpl(int idx) const |
| 54 |
{ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if (read(idx) != 0xFE) return {}; |
| 59 |
++idx; |
| 60 |
|
| 61 |
Sector sector; |
| 62 |
sector.addrIdx = idx; |
| 63 |
CRC16 addrCrc; |
| 64 |
addrCrc.init({0xA1, 0xA1, 0xA1, 0xFE}); |
| 65 |
updateCrc(addrCrc, sector.addrIdx, 4); |
| 66 |
sector.track = read(idx++); |
| 67 |
sector.head = read(idx++); |
| 68 |
sector.sector = read(idx++); |
| 69 |
sector.sizeCode = read(idx++); |
| 70 |
uint8_t addrCrc1 = read(idx++); |
| 71 |
uint8_t addrCrc2 = read(idx++); |
| 72 |
sector.addrCrcErr = (256 * addrCrc1 + addrCrc2) != addrCrc.getValue(); |
| 73 |
|
| 74 |
sector.dataIdx = -1; |
| 75 |
sector.deleted = false; |
| 76 |
sector.dataCrcErr = true; |
| 77 |
|
| 78 |
if (!sector.addrCrcErr) { |
| 79 |
|
| 80 |
|
| 81 |
for (auto i : xrange(43)) { |
| 82 |
int idx2 = idx + i; |
| 83 |
int j = 0; |
| 84 |
for (; j < 3; ++j) { |
| 85 |
if (read(idx2 + j) != 0xA1) break; |
| 86 |
} |
| 87 |
if (j != 3) continue; |
| 88 |
|
| 89 |
uint8_t type = read(idx2 + 3); |
| 90 |
if (type != one_of(0xfb, 0xf8)) continue; |
| 91 |
|
| 92 |
CRC16 dataCrc; |
| 93 |
dataCrc.init({0xA1, 0xA1, 0xA1}); |
| 94 |
dataCrc.update(type); |
| 95 |
|
| 96 |
|
| 97 |
int dataIdx = idx2 + 4; |
| 98 |
int sectorSize = 128 << (sector.sizeCode & 7); |
| 99 |
updateCrc(dataCrc, dataIdx, sectorSize); |
| 100 |
uint8_t dataCrc1 = read(dataIdx + sectorSize + 0); |
| 101 |
uint8_t dataCrc2 = read(dataIdx + sectorSize + 1); |
| 102 |
bool dataCrcErr = (256 * dataCrc1 + dataCrc2) != dataCrc.getValue(); |
| 103 |
|
| 104 |
|
| 105 |
sector.dataIdx = dataIdx; |
| 106 |
sector.deleted = type == 0xf8; |
| 107 |
sector.dataCrcErr = dataCrcErr; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
return sector; |
| 112 |
} |
| 113 |
|
| 114 |
std::vector<RawTrack::Sector> RawTrack::decodeAll() const |
| 115 |
{ |
| 116 |
|
| 117 |
std::vector<Sector> result; |
| 118 |
for (const auto& i : idam) { |
| 119 |
if (auto sector = decodeSectorImpl(narrow<int>(i)); |
| 120 |
sector && (sector->dataIdx != -1)) { |
| 121 |
result.push_back(*sector); |
| 122 |
} |
| 123 |
} |
| 124 |
return result; |
| 125 |
} |
| 126 |
|
| 127 |
static small_buffer<int, 16> rotateIdam(std::span<const unsigned> idam, unsigned startIdx) |
| 128 |
{ |
| 129 |
|
| 130 |
small_buffer<int, 16> result(uninitialized_tag{}, idam.size()); |
| 131 |
|
| 132 |
|
| 133 |
auto it = std::ranges::lower_bound(idam, startIdx); |
| 134 |
|
| 135 |
std::ranges::rotate_copy(idam, it, result.begin()); |
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
std::optional<RawTrack::Sector> RawTrack::decodeNextSector(unsigned startIdx) const |
| 140 |
{ |
| 141 |
|
| 142 |
for (const auto& i : rotateIdam(idam, startIdx)) { |
| 143 |
if (auto sector = decodeSectorImpl(narrow<int>(i))) { |
| 144 |
return sector; |
| 145 |
} |
| 146 |
} |
| 147 |
return {}; |
| 148 |
} |
| 149 |
|
| 150 |
std::optional<RawTrack::Sector> RawTrack::decodeSector(uint8_t sectorNum) const |
| 151 |
{ |
| 152 |
|
| 153 |
for (const auto& i : idam) { |
| 154 |
if (auto sector = decodeSectorImpl(narrow<int>(i)); |
| 155 |
sector && |
| 156 |
(sector->sector == sectorNum) && |
| 157 |
(sector->dataIdx != -1)) { |
| 158 |
return sector; |
| 159 |
} |
| 160 |
} |
| 161 |
return {}; |
| 162 |
} |
| 163 |
|
| 164 |
void RawTrack::readBlock(int idx, std::span<uint8_t> destination) const |
| 165 |
{ |
| 166 |
for (auto [i, d] : enumerate(destination)) { |
| 167 |
d = read(idx + int(i)); |
| 168 |
} |
| 169 |
} |
| 170 |
void RawTrack::writeBlock(int idx, std::span<const uint8_t> source) |
| 171 |
{ |
| 172 |
for (auto [i, s] : enumerate(source)) { |
| 173 |
write(idx + int(i), s); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void RawTrack::updateCrc(CRC16& crc, int idx, int size) const |
| 178 |
{ |
| 179 |
unsigned start = wrapIndex(idx); |
| 180 |
unsigned end = start + size; |
| 181 |
if (end <= data.size()) { |
| 182 |
crc.update(subspan(data, start, size)); |
| 183 |
} else { |
| 184 |
unsigned part = unsigned(data.size()) - start; |
| 185 |
crc.update(subspan(data, start, part)); |
| 186 |
crc.update(subspan(data, 0, size - part)); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
uint16_t RawTrack::calcCrc(int idx, int size) const |
| 191 |
{ |
| 192 |
CRC16 crc; |
| 193 |
updateCrc(crc, idx, size); |
| 194 |
return crc.getValue(); |
| 195 |
} |
| 196 |
|
| 197 |
void RawTrack::applyWd2793ReadTrackQuirk() |
| 198 |
{ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
for (unsigned ii : idam) { |
| 251 |
auto i = narrow<int>(ii); |
| 252 |
if ((read(i - 3) != 0xA1) || |
| 253 |
(read(i - 2) != 0xA1) || |
| 254 |
(read(i - 1) != 0xA1) || |
| 255 |
(read(i - 0) != 0xFE)) continue; |
| 256 |
write(i - 3, 0x14); |
| 257 |
|
| 258 |
if (auto sector = decodeSectorImpl(i); |
| 259 |
sector && (sector->dataIdx != -1)) { |
| 260 |
int d = sector->dataIdx; |
| 261 |
if ((read(d - 4) != 0xA1) || |
| 262 |
(read(d - 3) != 0xA1) || |
| 263 |
(read(d - 2) != 0xA1)) continue; |
| 264 |
write(d - 4, 0x14); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
template<typename Archive> |
| 272 |
void RawTrack::serialize(Archive& ar, unsigned version) |
| 273 |
{ |
| 274 |
ar.serialize("idam", idam); |
| 275 |
auto len = unsigned(data.size()); |
| 276 |
if (ar.versionAtLeast(version, 2)) { |
| 277 |
ar.serialize("trackLength", len); |
| 278 |
} else { |
| 279 |
assert(Archive::IS_LOADER); |
| 280 |
len = 6250; |
| 281 |
} |
| 282 |
if constexpr (Archive::IS_LOADER) { |
| 283 |
data.resize(len); |
| 284 |
} |
| 285 |
ar.serialize_blob("data", data); |
| 286 |
} |
| 287 |
INSTANTIATE_SERIALIZE_METHODS(RawTrack); |
| 288 |
|
| 289 |
template<typename Archive> |
| 290 |
void RawTrack::Sector::serialize(Archive& ar, unsigned ) |
| 291 |
{ |
| 292 |
ar.serialize("addrIdx", addrIdx, |
| 293 |
"dataIdx", dataIdx, |
| 294 |
"track", track, |
| 295 |
"head", head, |
| 296 |
"sector", sector, |
| 297 |
"sizeCode", sizeCode, |
| 298 |
"deleted", deleted, |
| 299 |
"addrCrcErr", addrCrcErr, |
| 300 |
"dataCrcErr", dataCrcErr); |
| 301 |
} |
| 302 |
INSTANTIATE_SERIALIZE_METHODS(RawTrack::Sector); |
| 303 |
|
| 304 |
} |