openMSX
RawTrack.cc
Go to the documentation of this file.
1 #include "RawTrack.hh"
2 #include "CRC16.hh"
3 #include "one_of.hh"
4 #include "ranges.hh"
5 #include "serialize.hh"
6 #include "serialize_stl.hh"
7 #include <cassert>
8 
9 using std::vector;
10 
11 namespace openmsx {
12 
14 {
15  clear(size);
16 }
17 
18 void RawTrack::clear(unsigned size)
19 {
20  idam.clear();
21  data.assign(size, 0x4e);
22 }
23 
24 void RawTrack::addIdam(unsigned idx)
25 {
26  assert(idx < data.size());
27  assert(idam.empty() || (idx > idam.back()));
28  idam.push_back(idx);
29 }
30 
31 void RawTrack::write(int idx, byte val, bool setIdam)
32 {
33  unsigned i2 = wrapIndex(idx);
34  auto it = ranges::lower_bound(idam, i2);
35  if (setIdam) {
36  // add idam (if not already present)
37  if ((it == end(idam)) || (*it != i2)) {
38  idam.insert(it, i2);
39  }
40  } else {
41  // remove idam (if present)
42  if ((it != end(idam)) && (*it == i2)) {
43  idam.erase(it);
44  }
45  }
46  data[i2] = val;
47 }
48 
49 bool RawTrack::decodeSectorImpl(int idx, Sector& sector) const
50 {
51  // read (and check) address mark
52  // assume addr mark starts with three A1 bytes (should be
53  // located right before the current 'idx' position)
54  if (read(idx) != 0xFE) return false; // 'sector'-data not filled in
55 
56  ++idx;
57  sector.addrIdx = idx;
58  CRC16 addrCrc;
59  addrCrc.init({0xA1, 0xA1, 0xA1, 0xFE});
60  updateCrc(addrCrc, sector.addrIdx, 4);
61  sector.track = read(idx++);
62  sector.head = read(idx++);
63  sector.sector = read(idx++);
64  sector.sizeCode = read(idx++);
65  byte addrCrc1 = read(idx++);
66  byte addrCrc2 = read(idx++);
67  sector.addrCrcErr = (256 * addrCrc1 + addrCrc2) != addrCrc.getValue();
68 
69  sector.dataIdx = -1; // (for now) no data block found
70  sector.deleted = false; // dummy
71  sector.dataCrcErr = true; // dummy
72 
73  if (!sector.addrCrcErr) {
74  // Locate data mark, should starts within 43 bytes from current
75  // position (that's what the WD2793 does).
76  for (int i = 0; i < 43; ++i) {
77  int idx2 = idx + i;
78  int j = 0;
79  for (; j < 3; ++j) {
80  if (read(idx2 + j) != 0xA1) break;
81  }
82  if (j != 3) continue; // didn't find 3 x 0xA1
83 
84  byte type = read(idx2 + 3);
85  if (type != one_of(0xfb, 0xf8)) continue;
86 
87  CRC16 dataCrc;
88  dataCrc.init({0xA1, 0xA1, 0xA1});
89  dataCrc.update(type);
90 
91  // OK, found start of data, calculate CRC.
92  int dataIdx = idx2 + 4;
93  unsigned sectorSize = 128 << (sector.sizeCode & 7);
94  updateCrc(dataCrc, dataIdx, sectorSize);
95  byte dataCrc1 = read(dataIdx + sectorSize + 0);
96  byte dataCrc2 = read(dataIdx + sectorSize + 1);
97  bool dataCrcErr = (256 * dataCrc1 + dataCrc2) != dataCrc.getValue();
98 
99  // store result
100  sector.dataIdx = dataIdx;
101  sector.deleted = type == 0xf8;
102  sector.dataCrcErr = dataCrcErr;
103  break;
104  }
105  }
106  return true;
107 }
108 
109 vector<RawTrack::Sector> RawTrack::decodeAll() const
110 {
111  // only complete sectors (header + data block)
112  vector<Sector> result;
113  for (auto& i : idam) {
114  Sector sector;
115  if (decodeSectorImpl(i, sector) &&
116  (sector.dataIdx != -1)) {
117  result.push_back(sector);
118  }
119  }
120  return result;
121 }
122 
123 static vector<unsigned> rotateIdam(vector<unsigned> idam, unsigned startIdx)
124 {
125  // find first element that is equal or bigger
126  auto it = ranges::lower_bound(idam, startIdx);
127  // rotate range so that we start at that element
128  std::rotate(begin(idam), it, end(idam));
129  return idam;
130 }
131 
132 bool RawTrack::decodeNextSector(unsigned startIdx, Sector& sector) const
133 {
134  // get first valid sector-header
135  for (auto& i : rotateIdam(idam, startIdx)) {
136  if (decodeSectorImpl(i, sector)) {
137  return true;
138  }
139  }
140  return false;
141 }
142 
143 bool RawTrack::decodeSector(byte sectorNum, Sector& sector) const
144 {
145  // only complete sectors (header + data block)
146  for (auto& i : idam) {
147  if (decodeSectorImpl(i, sector) &&
148  (sector.sector == sectorNum) &&
149  (sector.dataIdx != -1)) {
150  return true;
151  }
152  }
153  return false;
154 }
155 
156 void RawTrack::readBlock(int idx, unsigned size, byte* destination) const
157 {
158  for (unsigned i = 0; i < size; ++i) {
159  destination[i] = read(idx + i);
160  }
161 }
162 void RawTrack::writeBlock(int idx, unsigned size, const byte* source)
163 {
164  for (unsigned i = 0; i < size; ++i) {
165  write(idx + i, source[i]);
166  }
167 }
168 
169 void RawTrack::updateCrc(CRC16& crc, int idx, int size) const
170 {
171  unsigned start = wrapIndex(idx);
172  unsigned end = start + size;
173  if (end <= data.size()) {
174  crc.update(&data[start], size);
175  } else {
176  unsigned part = unsigned(data.size()) - start;
177  crc.update(&data[start], part);
178  crc.update(&data[ 0], size - part);
179  }
180 }
181 
182 word RawTrack::calcCrc(int idx, int size) const
183 {
184  CRC16 crc;
185  updateCrc(crc, idx, size);
186  return crc.getValue();
187 }
188 
190 {
191  // Tests on a real WD2793 have shown that the first 'A1' byte in a
192  // sequence of 3 'special' A1 bytes gets systematically read as a
193  // different value by the read-track command.
194  //
195  // Very roughly in my tests I got:
196  // - about 1/2 of the times the value 0x14 is returned
197  // - about 1/3 of the times the value 0xC2 is returned
198  // - about 1/6 of the times a different value is returned (I've seen
199  // 00, 02, F8, FC)
200  // When re-reading the same track again, the values 14 and C2 remain,
201  // but often the values 00 or 02 change to 14, and F8 or FC change to
202  // C2. I've never seen the 'real' value 'A1'. Note again that this
203  // transformation only applies to the 1st of the three A1 bytes, the
204  // 2nd and 3rd are correctly read as A1.
205  //
206  // I've no good explanation for why this is happening, except for the
207  // following observation which might be the start of a partial
208  // explanation, or it might just be a coincidence:
209  //
210  // The 'special' value A1 is encoded as: 0x4489, or in binary
211  // 01 00 01 00 10 00 10 01
212  // If you only look at the even bits (the data bits) you get the value
213  // A1 back. The odd bits (the clock bits) are generated by the MFM
214  // encoding rules and ensure that there are never 2 consecutive 1-bits
215  // and never more than 3 consecutive 0-bits. Though this is a 'special'
216  // sequence and the clock bit of the 6th bit (counting from left to
217  // right starting at 1) is 0, it should be 1 according to the MFM
218  // rules. The normal encoding for A1 is 0x44A9. An FDC uses this
219  // special bit-pattern to synchronize the bitstream on, e.g. to know
220  // where the byte-boundaries are located in the stream.
221  //
222  // Now if you shift the pattern 0x4489 1 bit to the left, you get
223  // 0x8912. If you then decode the data bits you get the value 0x14. And
224  // this _might_ be a partial explanation for why read-track often
225  // returns 0x14 instead of 0xA1.
226  //
227  // The implementation below always changes the first 0xA1 byte to 0x14.
228  // It does not also return the value 0xC2, because I've no clue yet
229  // when which value should be returned.
230  //
231  // The motivation for this (crude) implementation is the following:
232  // There's a cracked disk version of Nemesis that comes with a
233  // copy-protection. The disk has a track which contains sector-headers
234  // with CRC errors. The software verifies the protection via the
235  // read-track command. In the resulting data block it scans for the
236  // first A1 byte and then sums the next 8 bytes. Initially openMSX
237  // returned A1 'too early', and then the sum of the 8 following bytes
238  // doesn't match. But if the first A1 is turned into something
239  // (anything) else, so the sum starts one byte later, it does work
240  // fine.
241 
242  for (int i : idam) {
243  if ((read(i - 3) != 0xA1) ||
244  (read(i - 2) != 0xA1) ||
245  (read(i - 1) != 0xA1) ||
246  (read(i - 0) != 0xFE)) continue;
247  write(i - 3, 0x14);
248 
249  Sector sector;
250  if (decodeSectorImpl(i, sector) &&
251  (sector.dataIdx != -1)) {
252  int d = sector.dataIdx;
253  if ((read(d - 4) != 0xA1) ||
254  (read(d - 3) != 0xA1) ||
255  (read(d - 2) != 0xA1)) continue;
256  write(d - 4, 0x14);
257  }
258  }
259 }
260 
261 // version 1: initial version (fixed track length of 6250)
262 // version 2: variable track length
263 template<typename Archive>
264 void RawTrack::serialize(Archive& ar, unsigned version)
265 {
266  ar.serialize("idam", idam);
267  auto len = unsigned(data.size());
268  if (ar.versionAtLeast(version, 2)) {
269  ar.serialize("trackLength", len);
270  } else {
271  assert(ar.isLoader());
272  len = 6250;
273  }
274  if (ar.isLoader()) {
275  data.resize(len);
276  }
277  ar.serialize_blob("data", data.data(), data.size());
278 }
280 
281 template<typename Archive>
282 void RawTrack::Sector::serialize(Archive& ar, unsigned /*version*/)
283 {
284  ar.serialize("addrIdx", addrIdx,
285  "dataIdx", dataIdx,
286  "track", track,
287  "head", head,
288  "sector", sector,
289  "sizeCode", sizeCode,
290  "deleted", deleted,
291  "addrCrcErr", addrCrcErr,
292  "dataCrcErr", dataCrcErr);
293 }
295 
296 } // namespace openmsx
openmsx::RawTrack::decodeNextSector
bool decodeNextSector(unsigned startIdx, Sector &sector) const
Get the next sector (starting from a certain index).
Definition: RawTrack.cc:132
openmsx::RawTrack::decodeAll
std::vector< Sector > decodeAll() const
Get info on all sectors in this track.
Definition: RawTrack.cc:109
one_of.hh
gl::rotate
mat4 rotate(float angle, const vec3 &axis)
Definition: gl_transform.hh:58
serialize.hh
openmsx::CRC16::init
constexpr void init(uint16_t initialCRC)
(Re)initialize the current value
Definition: CRC16.hh:47
CRC16.hh
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:227
openmsx::RawTrack
Definition: RawTrack.hh:62
ranges::lower_bound
auto lower_bound(ForwardRange &&range, const T &value)
Definition: ranges.hh:71
openmsx::RawTrack::serialize
void serialize(Archive &ar, unsigned version)
Definition: RawTrack.cc:264
ranges.hh
openmsx::RawTrack::Sector::sector
byte sector
Definition: RawTrack.hh:80
openmsx::RawTrack::readBlock
void readBlock(int idx, unsigned size, byte *destination) const
Like memcpy() but copy from/to circular buffer.
Definition: RawTrack.cc:156
openmsx::RawTrack::decodeSector
bool decodeSector(byte sectorNum, Sector &sector) const
Get a sector with a specific number.
Definition: RawTrack.cc:143
openmsx::CRC16::update
constexpr void update(uint8_t value)
Update CRC with one byte.
Definition: CRC16.hh:62
openmsx::RawTrack::Sector::head
byte head
Definition: RawTrack.hh:79
openmsx::RawTrack::Sector::sizeCode
byte sizeCode
Definition: RawTrack.hh:81
openmsx::RawTrack::write
void write(int idx, byte val, bool setIdam=false)
Definition: RawTrack.cc:31
openmsx::RawTrack::applyWd2793ReadTrackQuirk
void applyWd2793ReadTrackQuirk()
Definition: RawTrack.cc:189
openmsx::RawTrack::updateCrc
void updateCrc(CRC16 &crc, int idx, int size) const
Definition: RawTrack.cc:169
openmsx::CRC16::getValue
constexpr uint16_t getValue() const
Get current CRC value.
Definition: CRC16.hh:106
openmsx::CRC16
This class calculates CRC numbers for the polygon x^16 + x^12 + x^5 + 1.
Definition: CRC16.hh:35
openmsx::RawTrack::writeBlock
void writeBlock(int idx, unsigned size, const byte *source)
Definition: RawTrack.cc:162
openmsx::RawTrack::calcCrc
word calcCrc(int idx, int size) const
Convenience method to calculate CRC for part of this track.
Definition: RawTrack.cc:182
one_of
Definition: one_of.hh:7
openmsx::RawTrack::Sector::dataCrcErr
bool dataCrcErr
Definition: RawTrack.hh:84
openmsx::RawTrack::read
byte read(int idx) const
Definition: RawTrack.hh:104
openmsx::RawTrack::Sector::track
byte track
Definition: RawTrack.hh:78
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::RawTrack::Sector::addrIdx
int addrIdx
Definition: RawTrack.hh:76
serialize_stl.hh
openmsx::RawTrack::addIdam
void addIdam(unsigned idx)
Definition: RawTrack.cc:24
openmsx::RawTrack::wrapIndex
int wrapIndex(int idx) const
Definition: RawTrack.hh:106
RawTrack.hh
openmsx::RawTrack::Sector::deleted
bool deleted
Definition: RawTrack.hh:82
openmsx::RawTrack::Sector
Definition: RawTrack.hh:73
openmsx::RawTrack::Sector::addrCrcErr
bool addrCrcErr
Definition: RawTrack.hh:83
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::RawTrack::RawTrack
RawTrack(unsigned size=STANDARD_SIZE)
Definition: RawTrack.cc:13
openmsx::RawTrack::Sector::dataIdx
int dataIdx
Definition: RawTrack.hh:77
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::RawTrack::Sector::serialize
void serialize(Archive &ar, unsigned version)
Definition: RawTrack.cc:282
openmsx::RawTrack::clear
void clear(unsigned size)
Clear track data.
Definition: RawTrack.cc:18