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"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2016-09-18 23:32:08 +00:00
|
|
|
#include "../Encodings/MFM.hpp"
|
2016-09-18 23:21:02 +00:00
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
|
|
|
SSD::SSD(const char *file_name) : _file(nullptr)
|
|
|
|
{
|
|
|
|
struct stat file_stats;
|
|
|
|
stat(file_name, &file_stats);
|
|
|
|
|
|
|
|
// very loose validation: the file needs to be a multiple of 256 bytes
|
|
|
|
// and not ungainly large
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
_file = fopen(file_name, "rb");
|
|
|
|
|
|
|
|
if(!_file) throw ErrorCantOpen;
|
2016-09-18 23:32:08 +00:00
|
|
|
|
|
|
|
// this has two heads if the suffix is .dsd, one if it's .ssd
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
|
|
|
SSD::~SSD()
|
|
|
|
{
|
|
|
|
if(_file) fclose(_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int SSD::get_head_position_count()
|
|
|
|
{
|
2016-09-18 23:32:08 +00:00
|
|
|
return _track_count;
|
2016-09-18 23:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int SSD::get_head_count()
|
|
|
|
{
|
2016-09-18 23:32:08 +00:00
|
|
|
return _head_count;
|
2016-09-18 23:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Track> SSD::get_track_at_position(unsigned int head, unsigned int position)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Track> track;
|
2016-09-18 23:32:08 +00:00
|
|
|
|
|
|
|
if(head >= _head_count) return track;
|
2016-09-19 11:34:10 +00:00
|
|
|
long file_offset = (position * _head_count + head) * 256 * 10;
|
2016-09-18 23:32:08 +00:00
|
|
|
fseek(_file, file_offset, SEEK_SET);
|
|
|
|
|
2016-09-19 01:09:32 +00:00
|
|
|
std::vector<Storage::Encodings::MFM::Sector> sectors;
|
2016-09-18 23:32:08 +00:00
|
|
|
for(int sector = 0; sector < 10; sector++)
|
|
|
|
{
|
2016-09-19 01:09:32 +00:00
|
|
|
Storage::Encodings::MFM::Sector new_sector;
|
|
|
|
new_sector.track = (uint8_t)position;
|
|
|
|
new_sector.side = 0;
|
|
|
|
new_sector.sector = (uint8_t)sector;
|
|
|
|
|
|
|
|
new_sector.data.resize(256);
|
|
|
|
fread(&new_sector.data[0], 1, 256, _file);
|
2016-09-25 21:46:11 +00:00
|
|
|
if(feof(_file))
|
|
|
|
break;
|
2016-09-19 01:09:32 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|