From 8f78e5039ef89c098a4464b32c9d24d4729b74b0 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 24 May 2018 18:45:00 -0400 Subject: [PATCH] Factors out track seeking. --- Storage/Disk/DiskImage/Formats/WOZ.cpp | 27 +++++++++++++++++--------- Storage/Disk/DiskImage/Formats/WOZ.hpp | 9 +++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/WOZ.cpp b/Storage/Disk/DiskImage/Formats/WOZ.cpp index 3100f1a76..eae1a84e9 100644 --- a/Storage/Disk/DiskImage/Formats/WOZ.cpp +++ b/Storage/Disk/DiskImage/Formats/WOZ.cpp @@ -90,22 +90,31 @@ int WOZ::get_head_count() { return is_3_5_disk_ ? 2 : 1; } -std::shared_ptr WOZ::get_track_at_position(Track::Address address) { +long WOZ::file_offset(Track::Address address) { // Calculate table position; if this track is defined to be unformatted, return no track. const int table_position = address.head * (is_3_5_disk_ ? 80 : 160) + (is_3_5_disk_ ? address.position.as_int() : address.position.as_quarter()); - if(track_map_[table_position] == 0xff) return nullptr; + if(track_map_[table_position] == 0xff) return NoSuchTrack; // Seek to the real track. - file_.seek(tracks_offset_ + track_map_[table_position] * 6656, SEEK_SET); + return tracks_offset_ + track_map_[table_position] * 6656; +} +std::shared_ptr WOZ::get_track_at_position(Track::Address address) { + long offset = file_offset(address); + if(offset == NoSuchTrack) return nullptr; + + // Seek to the real track. PCMSegment track_contents; - track_contents.data = file_.read(6646); - track_contents.data.resize(file_.get16le()); - track_contents.number_of_bits = file_.get16le(); + { + std::lock_guard lock_guard(file_.get_file_access_mutex()); + file_.seek(offset, SEEK_SET); - const uint16_t splice_point = file_.get16le(); - if(splice_point != 0xffff) { - // TODO: expand track from splice_point? + // In WOZ a track is up to 6646 bytes of data, followed by a two-byte record of the + // number of bytes that actually had data in them, then a two-byte count of the number + // of bits that were used. Other information follows but is not intended for emulation. + track_contents.data = file_.read(6646); + track_contents.data.resize(file_.get16le()); + track_contents.number_of_bits = file_.get16le(); } return std::shared_ptr(new PCMTrack(track_contents)); diff --git a/Storage/Disk/DiskImage/Formats/WOZ.hpp b/Storage/Disk/DiskImage/Formats/WOZ.hpp index 762ee3dce..03f642d35 100644 --- a/Storage/Disk/DiskImage/Formats/WOZ.hpp +++ b/Storage/Disk/DiskImage/Formats/WOZ.hpp @@ -34,6 +34,15 @@ class WOZ: public DiskImage { bool is_3_5_disk_ = false; uint8_t track_map_[160]; long tracks_offset_ = -1; + + /*! + Gets the in-file offset of a track. + + @returns The offset within the file of the track at @c address or @c NoSuchTrack if + the track does not exit. + */ + long file_offset(Track::Address address); + constexpr static long NoSuchTrack = 0; // This is definitely an offset a track can't lie at. }; }