2016-09-18 23:21:02 +00:00
|
|
|
//
|
|
|
|
// SSD.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/09/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SSD.hpp"
|
|
|
|
|
2017-09-25 00:07:56 +00:00
|
|
|
#include "../../Encodings/MFM/MFM.hpp"
|
2017-09-25 00:31:19 +00:00
|
|
|
#include "../../Encodings/MFM/Parser.hpp"
|
2016-09-18 23:21:02 +00:00
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
SSD::SSD(const char *file_name) :
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::FileHolder(file_name) {
|
2016-09-18 23:21:02 +00:00
|
|
|
// very loose validation: the file needs to be a multiple of 256 bytes
|
|
|
|
// and not ungainly large
|
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
if(file_stats_.st_size & 255) throw ErrorNotSSD;
|
|
|
|
if(file_stats_.st_size < 512) throw ErrorNotSSD;
|
|
|
|
if(file_stats_.st_size > 800*256) throw ErrorNotSSD;
|
2016-09-18 23:32:08 +00:00
|
|
|
|
|
|
|
// this has two heads if the suffix is .dsd, one if it's .ssd
|
2016-11-21 12:14:09 +00:00
|
|
|
head_count_ = (tolower(file_name[strlen(file_name) - 3]) == 'd') ? 2 : 1;
|
|
|
|
track_count_ = (unsigned int)(file_stats_.st_size / (256 * 10));
|
|
|
|
if(track_count_ < 40) track_count_ = 40;
|
|
|
|
else if(track_count_ < 80) track_count_ = 80;
|
2016-09-18 23:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int SSD::get_head_position_count() {
|
2016-11-21 12:14:09 +00:00
|
|
|
return track_count_;
|
2016-09-18 23:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int SSD::get_head_count() {
|
2016-11-21 12:14:09 +00:00
|
|
|
return head_count_;
|
2016-09-18 23:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool SSD::get_is_read_only() {
|
2016-12-29 01:09:14 +00:00
|
|
|
return is_read_only_;
|
2016-12-25 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
long SSD::get_file_offset_for_position(unsigned int head, unsigned int position) {
|
2016-12-30 03:15:58 +00:00
|
|
|
return (position * head_count_ + head) * 256 * 10;
|
|
|
|
}
|
|
|
|
|
2017-09-23 00:28:11 +00:00
|
|
|
std::shared_ptr<Track> SSD::get_track_at_position(unsigned int head, unsigned int position) {
|
2016-09-18 23:21:02 +00:00
|
|
|
std::shared_ptr<Track> track;
|
2016-09-18 23:32:08 +00:00
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
if(head >= head_count_) return track;
|
2016-09-18 23:32:08 +00:00
|
|
|
|
2016-09-19 01:09:32 +00:00
|
|
|
std::vector<Storage::Encodings::MFM::Sector> sectors;
|
2017-09-23 00:28:11 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
|
|
|
|
fseek(file_, get_file_offset_for_position(head, position), SEEK_SET);
|
|
|
|
|
|
|
|
for(int sector = 0; sector < 10; sector++) {
|
|
|
|
Storage::Encodings::MFM::Sector new_sector;
|
|
|
|
new_sector.track = (uint8_t)position;
|
|
|
|
new_sector.side = 0;
|
|
|
|
new_sector.sector = (uint8_t)sector;
|
|
|
|
new_sector.size = 1;
|
|
|
|
|
|
|
|
new_sector.data.resize(256);
|
|
|
|
fread(new_sector.data.data(), 1, 256, file_);
|
|
|
|
|
|
|
|
// zero out if this wasn't present in the disk image; it's still appropriate to put a sector
|
|
|
|
// on disk because one will have been placed during formatting, but there's no reason to leak
|
|
|
|
// information from outside the emulated machine's world
|
|
|
|
if(feof(file_)) memset(new_sector.data.data(), 0, 256);
|
|
|
|
|
|
|
|
sectors.push_back(std::move(new_sector));
|
|
|
|
}
|
2016-09-18 23:32:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 01:09:32 +00:00
|
|
|
if(sectors.size()) return Storage::Encodings::MFM::GetFMTrackWithSectors(sectors);
|
|
|
|
|
2016-09-18 23:21:02 +00:00
|
|
|
return track;
|
|
|
|
}
|
2016-12-30 03:15:58 +00:00
|
|
|
|
2017-09-23 00:28:11 +00:00
|
|
|
void SSD::set_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr<Track> &track) {
|
|
|
|
std::vector<uint8_t> data;
|
2016-12-30 03:15:58 +00:00
|
|
|
Storage::Encodings::MFM::Parser parser(false, track);
|
2017-03-26 18:34:47 +00:00
|
|
|
for(unsigned int c = 0; c < 10; c++) {
|
2017-08-11 14:29:13 +00:00
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector(0, (uint8_t)position, (uint8_t)c);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(sector) {
|
2017-09-23 00:28:11 +00:00
|
|
|
data.insert(data.end(), sector->data.begin(), sector->data.end());
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-30 23:03:30 +00:00
|
|
|
// TODO: what's correct here? Warn the user that whatever has been written to the disk,
|
|
|
|
// it can no longer be stored as an SSD? If so, warn them by what route?
|
2017-09-23 00:28:11 +00:00
|
|
|
data.resize(data.size() + 256);
|
2016-12-30 03:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 23:08:12 +00:00
|
|
|
long file_offset = get_file_offset_for_position(head, position);
|
2017-09-23 00:28:11 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
|
2016-12-30 23:08:12 +00:00
|
|
|
ensure_file_is_at_least_length(file_offset);
|
|
|
|
fseek(file_, file_offset, SEEK_SET);
|
2017-09-23 00:28:11 +00:00
|
|
|
fwrite(data.data(), 1, data.size(), file_);
|
2016-12-30 03:15:58 +00:00
|
|
|
}
|