2016-11-21 12:47:16 +00:00
|
|
|
//
|
|
|
|
// OricMFMDSK.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 21/11/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-11-21 12:47:16 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "OricMFMDSK.hpp"
|
2017-09-23 02:39:23 +00:00
|
|
|
|
2017-09-25 01:40:43 +00:00
|
|
|
#include "../../Encodings/MFM/Constants.hpp"
|
2017-09-28 02:14:50 +00:00
|
|
|
#include "../../Encodings/MFM/Shifter.hpp"
|
2017-09-25 01:40:43 +00:00
|
|
|
#include "../../Encodings/MFM/Encoder.hpp"
|
2018-01-10 03:12:34 +00:00
|
|
|
#include "../../Track/PCMTrack.hpp"
|
2017-09-28 02:14:50 +00:00
|
|
|
#include "../../Track/TrackSerialiser.hpp"
|
2016-11-21 12:47:16 +00:00
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
OricMFMDSK::OricMFMDSK(const std::string &file_name) :
|
2017-11-03 02:32:00 +00:00
|
|
|
file_(file_name) {
|
|
|
|
if(!file_.check_signature("MFM_DISK"))
|
2018-04-28 03:18:45 +00:00
|
|
|
throw Error::InvalidFormat;
|
2016-11-21 12:47:16 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
head_count_ = file_.get32le();
|
|
|
|
track_count_ = file_.get32le();
|
|
|
|
geometry_type_ = file_.get32le();
|
2016-11-21 12:47:16 +00:00
|
|
|
|
2016-11-26 02:13:12 +00:00
|
|
|
if(geometry_type_ < 1 || geometry_type_ > 2)
|
2018-04-28 03:18:45 +00:00
|
|
|
throw Error::InvalidFormat;
|
2016-11-21 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
HeadPosition OricMFMDSK::get_maximum_head_position() {
|
|
|
|
return HeadPosition(static_cast<int>(track_count_));
|
2016-11-21 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
int OricMFMDSK::get_head_count() {
|
|
|
|
return static_cast<int>(head_count_);
|
2016-11-21 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
long OricMFMDSK::get_file_offset_for_position(Track::Address address) {
|
|
|
|
int seek_offset = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(geometry_type_) {
|
2016-11-26 02:13:12 +00:00
|
|
|
case 1:
|
2018-05-07 03:17:36 +00:00
|
|
|
seek_offset = address.head * static_cast<int>(track_count_) + address.position.as_int();
|
2016-11-21 12:47:16 +00:00
|
|
|
break;
|
2016-11-26 02:13:12 +00:00
|
|
|
case 2:
|
2018-05-07 03:17:36 +00:00
|
|
|
seek_offset = address.position.as_int() * static_cast<int>(track_count_ * head_count_) + address.head;
|
2016-11-21 12:47:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-07 01:45:12 +00:00
|
|
|
return static_cast<long>(seek_offset) * 6400 + 256;
|
2016-12-31 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
std::shared_ptr<Track> OricMFMDSK::get_track_at_position(Track::Address address) {
|
2016-11-21 12:47:16 +00:00
|
|
|
PCMSegment segment;
|
2017-09-23 00:28:11 +00:00
|
|
|
{
|
2017-11-03 02:32:00 +00:00
|
|
|
std::lock_guard<std::mutex> lock_guard(file_.get_file_access_mutex());
|
|
|
|
file_.seek(get_file_offset_for_position(address), SEEK_SET);
|
2017-09-23 00:28:11 +00:00
|
|
|
|
|
|
|
// The file format omits clock bits. So it's not a genuine MFM capture.
|
|
|
|
// A consumer must contextually guess when an FB, FC, etc is meant to be a control mark.
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t track_offset = 0;
|
2017-09-23 00:28:11 +00:00
|
|
|
uint8_t last_header[6] = {0, 0, 0, 0, 0, 0};
|
|
|
|
std::unique_ptr<Encodings::MFM::Encoder> encoder = Encodings::MFM::GetMFMEncoder(segment.data);
|
|
|
|
bool did_sync = false;
|
|
|
|
while(track_offset < 6250) {
|
2017-11-03 02:32:00 +00:00
|
|
|
uint8_t next_byte = file_.get8();
|
2017-09-23 00:28:11 +00:00
|
|
|
track_offset++;
|
|
|
|
|
|
|
|
switch(next_byte) {
|
|
|
|
default: {
|
|
|
|
encoder->add_byte(next_byte);
|
|
|
|
if(did_sync) {
|
|
|
|
switch(next_byte) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
case 0xfe:
|
|
|
|
for(int byte = 0; byte < 6; byte++) {
|
2017-11-03 02:32:00 +00:00
|
|
|
last_header[byte] = file_.get8();
|
2017-09-23 00:28:11 +00:00
|
|
|
encoder->add_byte(last_header[byte]);
|
2020-01-15 04:16:06 +00:00
|
|
|
++track_offset;
|
2017-09-23 00:28:11 +00:00
|
|
|
if(track_offset == 6250) break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xfb:
|
|
|
|
for(int byte = 0; byte < (128 << last_header[3]) + 2; byte++) {
|
2017-11-03 02:32:00 +00:00
|
|
|
encoder->add_byte(file_.get8());
|
2020-01-15 04:16:06 +00:00
|
|
|
++track_offset;
|
|
|
|
// Special exception: don't interrupt a sector body if it seems to
|
|
|
|
// be about to run over the end of the track. It seems like BD-500
|
|
|
|
// disks break the usual 6250-byte rule, pushing out to just less
|
|
|
|
// than 6400 bytes total.
|
|
|
|
if(track_offset == 6400) break;
|
2017-09-23 00:28:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-12-28 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 00:28:11 +00:00
|
|
|
did_sync = false;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-26 05:40:10 +00:00
|
|
|
|
2017-09-23 00:28:11 +00:00
|
|
|
case 0xa1: // a synchronisation mark that implies a sector or header coming
|
|
|
|
encoder->output_short(Storage::Encodings::MFM::MFMSync);
|
|
|
|
did_sync = true;
|
|
|
|
break;
|
2016-11-26 05:40:10 +00:00
|
|
|
|
2017-09-23 00:28:11 +00:00
|
|
|
case 0xc2: // an 'ordinary' synchronisation mark
|
|
|
|
encoder->output_short(Storage::Encodings::MFM::MFMIndexSync);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-26 05:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:12:34 +00:00
|
|
|
return std::make_shared<PCMTrack>(segment);
|
2016-11-21 12:47:16 +00:00
|
|
|
}
|
2016-12-31 03:51:48 +00:00
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
void OricMFMDSK::set_tracks(const std::map<Track::Address, std::shared_ptr<Track>> &tracks) {
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto &track : tracks) {
|
2017-10-07 23:37:36 +00:00
|
|
|
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;
|
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
for(const auto bit : segment.data) {
|
|
|
|
shifter.add_input_bit(bit ? 1 : 0);
|
2017-10-07 23:37:36 +00:00
|
|
|
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(capture_size && offset == 2) {
|
|
|
|
size = parsed_track.back();
|
|
|
|
capture_size = false;
|
|
|
|
}
|
2017-09-28 02:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-09-28 02:14:50 +00:00
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
if(shifter.get_token() == Storage::Encodings::MFM::Shifter::Token::ID) {
|
|
|
|
offset = 6;
|
|
|
|
shifter.set_should_obey_syncs(false);
|
|
|
|
capture_size = true;
|
|
|
|
}
|
2017-09-28 02:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:37:36 +00:00
|
|
|
long file_offset = get_file_offset_for_position(track.first);
|
2016-12-31 16:48:46 +00:00
|
|
|
|
2017-11-03 02:32:00 +00:00
|
|
|
std::lock_guard<std::mutex> lock_guard(file_.get_file_access_mutex());
|
|
|
|
file_.seek(file_offset, SEEK_SET);
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t track_size = std::min(static_cast<std::size_t>(6400), parsed_track.size());
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.write(parsed_track.data(), track_size);
|
2017-10-07 23:37:36 +00:00
|
|
|
}
|
2016-12-31 03:51:48 +00:00
|
|
|
}
|
2017-11-03 02:32:00 +00:00
|
|
|
|
|
|
|
bool OricMFMDSK::get_is_read_only() {
|
|
|
|
return file_.get_is_known_read_only();
|
|
|
|
}
|