1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 08:49:37 +00:00

Switches to providing a full record of changes to disk images, rather than feeding them a track at a time.

Gets explicit about `override`s while doing so, to ensure full adaptation.
This commit is contained in:
Thomas Harte 2017-10-07 19:37:36 -04:00
parent b37787a414
commit 44cdc124af
13 changed files with 96 additions and 90 deletions

View File

@ -49,9 +49,9 @@ class DiskImage {
virtual std::shared_ptr<Track> get_track_at_position(Track::Address address) = 0;
/*!
Replaces the Track at position @c position underneath @c head with @c track. Ignored if this disk is read-only.
Replaces the Tracks indicated by the map, that maps from physical address to track content.
*/
virtual void set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track) {}
virtual void set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {}
/*!
Communicates that it is likely to be a while before any more tracks are written.

View File

@ -30,10 +30,7 @@ template <typename T> void DiskImageHolder<T>::flush_tracks() {
unwritten_tracks_.clear();
update_queue_->enqueue([this, track_copies]() {
// TODO: communicate these as a batch, not one by one.
for(auto &pair : *track_copies) {
disk_image_.set_track_at_position(pair.first, pair.second);
}
disk_image_.set_tracks(*track_copies);
});
}
}

View File

@ -31,11 +31,11 @@ class AcornADF: public MFMSectorDump {
ErrorNotAcornADF,
};
int get_head_position_count();
int get_head_count();
int get_head_position_count() override;
int get_head_count() override;
private:
long get_file_offset_for_position(Track::Address address);
long get_file_offset_for_position(Track::Address address) override;
};
}

View File

@ -35,10 +35,10 @@ class CPCDSK: public DiskImage, public Storage::FileHolder {
};
// implemented to satisfy @c Disk
int get_head_position_count();
int get_head_count();
int get_head_position_count() override;
int get_head_count() override;
using DiskImage::get_is_read_only;
std::shared_ptr<Track> get_track_at_position(Track::Address address);
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
private:
int head_count_;

View File

@ -33,9 +33,9 @@ class D64: public DiskImage, public Storage::FileHolder {
};
// implemented to satisfy @c Disk
int get_head_position_count();
int get_head_position_count() override;
using DiskImage::get_is_read_only;
std::shared_ptr<Track> get_track_at_position(Track::Address address);
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
private:
int number_of_tracks_;

View File

@ -36,8 +36,8 @@ class G64: public DiskImage, public Storage::FileHolder {
};
// implemented to satisfy @c Disk
int get_head_position_count();
std::shared_ptr<Track> get_track_at_position(Track::Address address);
int get_head_position_count() override;
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
using DiskImage::get_is_read_only;
private:

View File

@ -84,24 +84,26 @@ std::shared_ptr<Track> HFE::get_track_at_position(Track::Address address) {
return track;
}
void HFE::set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track) {
std::unique_lock<std::mutex> lock_guard(file_access_mutex_);
uint16_t track_length = seek_track(address);
lock_guard.unlock();
void HFE::set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {
for(auto &track : tracks) {
std::unique_lock<std::mutex> lock_guard(file_access_mutex_);
uint16_t track_length = seek_track(track.first);
lock_guard.unlock();
PCMSegment segment = Storage::Disk::track_serialisation(*track, Storage::Time(1, track_length * 8));
Storage::Data::BitReverse::reverse(segment.data);
uint16_t data_length = std::min(static_cast<uint16_t>(segment.data.size()), track_length);
PCMSegment segment = Storage::Disk::track_serialisation(*track.second, Storage::Time(1, track_length * 8));
Storage::Data::BitReverse::reverse(segment.data);
uint16_t data_length = std::min(static_cast<uint16_t>(segment.data.size()), track_length);
lock_guard.lock();
seek_track(address);
lock_guard.lock();
seek_track(track.first);
uint16_t c = 0;
while(c < data_length) {
uint16_t length = (uint16_t)std::min(256, data_length - c);
fwrite(&segment.data[c], 1, length, file_);
c += length;
fseek(file_, 256, SEEK_CUR);
uint16_t c = 0;
while(c < data_length) {
uint16_t length = (uint16_t)std::min(256, data_length - c);
fwrite(&segment.data[c], 1, length, file_);
c += length;
fseek(file_, 256, SEEK_CUR);
}
lock_guard.unlock();
}
lock_guard.unlock();
}

View File

@ -34,11 +34,11 @@ class HFE: public DiskImage, public Storage::FileHolder {
};
// implemented to satisfy @c Disk
int get_head_position_count();
int get_head_count();
int get_head_position_count() override;
int get_head_count() override;
using Storage::FileHolder::get_is_read_only;
void set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track);
std::shared_ptr<Track> get_track_at_position(Track::Address address);
void set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) override;
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
private:
uint16_t seek_track(Track::Address address);

View File

@ -35,16 +35,21 @@ std::shared_ptr<Track> MFMSectorDump::get_track_at_position(Track::Address addre
return track_for_sectors(sectors, static_cast<uint8_t>(address.position), static_cast<uint8_t>(address.head), 0, sector_size_, is_double_density_);
}
void MFMSectorDump::set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track) {
void MFMSectorDump::set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {
uint8_t parsed_track[(128 << sector_size_)*sectors_per_track_];
// Assumption here: sector IDs will run from 0.
decode_sectors(*track, parsed_track, 0, static_cast<uint8_t>(sectors_per_track_-1), sector_size_, is_double_density_);
long file_offset = get_file_offset_for_position(address);
// TODO: it would be more efficient from a file access and locking point of view to parse the sectors
// in one loop, then write in another.
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
ensure_file_is_at_least_length(file_offset);
fseek(file_, file_offset, SEEK_SET);
fwrite(parsed_track, 1, sizeof(parsed_track), file_);
for(auto &track : tracks) {
// Assumption here: sector IDs will run from 0.
decode_sectors(*track.second, parsed_track, 0, static_cast<uint8_t>(sectors_per_track_-1), sector_size_, is_double_density_);
long file_offset = get_file_offset_for_position(track.first);
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
ensure_file_is_at_least_length(file_offset);
fseek(file_, file_offset, SEEK_SET);
fwrite(parsed_track, 1, sizeof(parsed_track), file_);
}
fflush(file_);
}

View File

@ -24,8 +24,8 @@ class MFMSectorDump: public DiskImage, public Storage::FileHolder {
void set_geometry(int sectors_per_track, uint8_t sector_size, bool is_double_density);
using Storage::FileHolder::get_is_read_only;
void set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track);
std::shared_ptr<Track> get_track_at_position(Track::Address address);
void set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) override;
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
private:
std::mutex file_access_mutex_;

View File

@ -114,49 +114,51 @@ std::shared_ptr<Track> OricMFMDSK::get_track_at_position(Track::Address address)
return track;
}
void OricMFMDSK::set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track) {
PCMSegment segment = Storage::Disk::track_serialisation(*track, Storage::Encodings::MFM::MFMBitLength);
Storage::Encodings::MFM::Shifter shifter;
shifter.set_is_double_density(true);
shifter.set_should_obey_syncs(true);
std::vector<uint8_t> parsed_track;
int size = 0;
int offset = 0;
bool capture_size = false;
void OricMFMDSK::set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {
for(auto &track : tracks) {
PCMSegment segment = Storage::Disk::track_serialisation(*track.second, Storage::Encodings::MFM::MFMBitLength);
Storage::Encodings::MFM::Shifter shifter;
shifter.set_is_double_density(true);
shifter.set_should_obey_syncs(true);
std::vector<uint8_t> parsed_track;
int size = 0;
int offset = 0;
bool capture_size = false;
for(unsigned int bit = 0; bit < segment.number_of_bits; ++bit) {
shifter.add_input_bit(segment.bit(bit));
if(shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::None) continue;
parsed_track.push_back(shifter.get_byte());
for(unsigned int bit = 0; bit < segment.number_of_bits; ++bit) {
shifter.add_input_bit(segment.bit(bit));
if(shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::None) continue;
parsed_track.push_back(shifter.get_byte());
if(offset) {
offset--;
if(!offset) {
shifter.set_should_obey_syncs(true);
if(offset) {
offset--;
if(!offset) {
shifter.set_should_obey_syncs(true);
}
if(capture_size && offset == 2) {
size = parsed_track.back();
capture_size = false;
}
}
if(capture_size && offset == 2) {
size = parsed_track.back();
capture_size = false;
if( shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::Data ||
shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::DeletedData) {
offset = 128 << size;
shifter.set_should_obey_syncs(false);
}
if(shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::ID) {
offset = 6;
shifter.set_should_obey_syncs(false);
capture_size = true;
}
}
if( shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::Data ||
shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::DeletedData) {
offset = 128 << size;
shifter.set_should_obey_syncs(false);
}
long file_offset = get_file_offset_for_position(track.first);
if(shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::ID) {
offset = 6;
shifter.set_should_obey_syncs(false);
capture_size = true;
}
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
fseek(file_, file_offset, SEEK_SET);
size_t track_size = std::min((size_t)6400, parsed_track.size());
fwrite(parsed_track.data(), 1, track_size, file_);
}
long file_offset = get_file_offset_for_position(address);
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
fseek(file_, file_offset, SEEK_SET);
size_t track_size = std::min((size_t)6400, parsed_track.size());
fwrite(parsed_track.data(), 1, track_size, file_);
}

View File

@ -32,11 +32,11 @@ class OricMFMDSK: public DiskImage, public Storage::FileHolder {
};
// implemented to satisfy @c Disk
int get_head_position_count();
int get_head_count();
int get_head_position_count() override;
int get_head_count() override;
using Storage::FileHolder::get_is_read_only;
void set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track);
std::shared_ptr<Track> get_track_at_position(Track::Address address);
void set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) override;
std::shared_ptr<Track> get_track_at_position(Track::Address address) override;
private:
std::mutex file_access_mutex_;

View File

@ -31,11 +31,11 @@ class SSD: public MFMSectorDump {
ErrorNotSSD,
};
int get_head_position_count();
int get_head_count();
int get_head_position_count() override;
int get_head_count() override;
private:
long get_file_offset_for_position(Track::Address address);
long get_file_offset_for_position(Track::Address address) override;
int head_count_;
int track_count_;