@@ -210,43 +210,48 @@ DiskEncoder::decodeAmigaTrack(ByteView track, TrackNr t, MutableByteView dst)
210210 if (ADF_DEBUG) fprintf (stderr, " Decoding track %ld\n " , t);
211211 assert (dst.size () % bsize == 0 );
212212
213- // Seek all sync marks
214- std::vector<isize> sectorStart (count);
215- isize nr = 0 ; isize index = 0 ;
213+ // Find all sync marks
216214 auto it = track.cyclic_begin ();
215+ std::vector<isize> syncMarks;
216+ syncMarks.reserve (count);
217217
218- while (index < track.size () + 8 && nr < count) {
218+ constexpr isize syncMarkLen = 4 ;
219+ for (isize i = 0 ; i < track.size () + syncMarkLen; ++i, ++it) {
219220
220221 // Scan MFM stream for $4489 $4489
221- if (it[index++ ] != 0x44 ) continue ;
222- if (it[index++ ] != 0x89 ) continue ;
223- if (it[index++ ] != 0x44 ) continue ;
224- if (it[index++ ] != 0x89 ) continue ;
222+ if (it[0 ] != 0x44 ) continue ;
223+ if (it[1 ] != 0x89 ) continue ;
224+ if (it[2 ] != 0x44 ) continue ;
225+ if (it[3 ] != 0x89 ) continue ;
225226
226227 // Make sure it's not a DOS track
227- if (it[index+ 1 ] == 0x89 ) continue ;
228+ if (it[5 ] == 0x89 ) continue ;
228229
229- sectorStart[nr++] = index ;
230+ syncMarks. push_back (it. offset ()) ;
230231 }
231232
232- if (ADF_DEBUG) fprintf (stderr, " Found %ld sectors (expected %ld)\n " , nr , count);
233+ if (ADF_DEBUG) fprintf (stderr, " Found %zd sectors (expected %ld)\n " , syncMarks. size () , count);
233234
234- if (nr != count) {
235+ // Check that the track contains the proper amount of sync marks
236+ if (isize (syncMarks.size ()) != count) {
235237
236- warn (" Found %ld sectors, expected %ld. Aborting.\n " , nr , count);
238+ warn (" Found %zd sectors, expected %ld. Aborting.\n " , syncMarks. size () , count);
237239 throw DeviceError (DeviceError::DSK_WRONG_SECTOR_COUNT);
238240 }
239241
240242 // Decode all sectors
241243 for (SectorNr s = 0 ; s < count; s++)
242- decodeAmigaSector (track, sectorStart [s], dst);
244+ decodeAmigaSector (track, syncMarks [s], dst);
243245}
244246
245247void
246248DiskEncoder::decodeAmigaSector (ByteView track, isize offset, MutableByteView dst)
247249{
248250 const isize bsize = 512 ;
249251
252+ // Skip sync mark
253+ offset += 4 ;
254+
250255 // Decode sector info
251256 u8 info[4 ]; decodeOddEven (info, &track[offset], 4 );
252257
0 commit comments