2017-10-01 00:30:15 +00:00
|
|
|
//
|
|
|
|
// MFMSectorDump.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 30/09/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-10-01 00:30:15 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "MFMSectorDump.hpp"
|
|
|
|
|
|
|
|
#include "Utility/ImplicitSectors.hpp"
|
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
MFMSectorDump::MFMSectorDump(const std::string &file_name) : file_(file_name) {}
|
2017-10-01 00:30:15 +00:00
|
|
|
|
2018-01-08 02:59:18 +00:00
|
|
|
void MFMSectorDump::set_geometry(int sectors_per_track, uint8_t sector_size, uint8_t first_sector, bool is_double_density) {
|
2017-10-01 00:30:15 +00:00
|
|
|
sectors_per_track_ = sectors_per_track;
|
|
|
|
sector_size_ = sector_size;
|
|
|
|
is_double_density_ = is_double_density;
|
2018-01-08 02:59:18 +00:00
|
|
|
first_sector_ = first_sector;
|
2017-10-01 00:30:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
std::shared_ptr<Track> MFMSectorDump::get_track_at_position(Track::Address address) {
|
2019-11-13 04:22:25 +00:00
|
|
|
if(address.head >= get_head_count()) return nullptr;
|
|
|
|
if(address.position.as_largest() >= get_maximum_head_position().as_largest()) return nullptr;
|
2017-10-01 00:30:15 +00:00
|
|
|
|
2019-11-13 04:22:25 +00:00
|
|
|
uint8_t sectors[(128 << sector_size_)*sectors_per_track_];
|
|
|
|
const long file_offset = get_file_offset_for_position(address);
|
2017-10-01 00:30:15 +00:00
|
|
|
|
|
|
|
{
|
2020-06-15 04:24:10 +00:00
|
|
|
std::lock_guard lock_guard(file_.get_file_access_mutex());
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(file_offset, SEEK_SET);
|
|
|
|
file_.read(sectors, sizeof(sectors));
|
2017-10-01 00:30:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 03:00:39 +00:00
|
|
|
return track_for_sectors(sectors, sectors_per_track_, uint8_t(address.position.as_int()), uint8_t(address.head), first_sector_, sector_size_, is_double_density_);
|
2017-10-01 00:30:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
void MFMSectorDump::set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {
|
2017-10-01 00:30:15 +00:00
|
|
|
uint8_t parsed_track[(128 << sector_size_)*sectors_per_track_];
|
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto &track : tracks) {
|
2017-10-07 23:37:36 +00:00
|
|
|
// Assumption here: sector IDs will run from 0.
|
2020-05-10 03:00:39 +00:00
|
|
|
decode_sectors(*track.second, parsed_track, first_sector_, first_sector_ + uint8_t(sectors_per_track_-1), sector_size_, is_double_density_);
|
2019-11-13 04:22:25 +00:00
|
|
|
const long file_offset = get_file_offset_for_position(track.first);
|
2017-10-01 00:30:15 +00:00
|
|
|
|
2020-06-15 04:24:10 +00:00
|
|
|
std::lock_guard lock_guard(file_.get_file_access_mutex());
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.ensure_is_at_least_length(file_offset);
|
|
|
|
file_.seek(file_offset, SEEK_SET);
|
|
|
|
file_.write(parsed_track, sizeof(parsed_track));
|
2017-10-07 23:37:36 +00:00
|
|
|
}
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MFMSectorDump::get_is_read_only() {
|
|
|
|
return file_.get_is_known_read_only();
|
2017-10-01 00:30:15 +00:00
|
|
|
}
|