2017-08-05 14:02:10 +00:00
|
|
|
//
|
|
|
|
// CPCDSK.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/08/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CPCDSK.hpp"
|
|
|
|
|
2017-08-05 15:44:53 +00:00
|
|
|
#include "../Encodings/MFM.hpp"
|
|
|
|
|
2017-08-05 14:02:10 +00:00
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
|
|
|
CPCDSK::CPCDSK(const char *file_name) :
|
|
|
|
Storage::FileHolder(file_name), is_extended_(false) {
|
|
|
|
if(!check_signature("MV - CPC", 8)) {
|
|
|
|
is_extended_ = true;
|
2017-08-06 23:47:10 +00:00
|
|
|
fseek(file_, 0, SEEK_SET);
|
2017-08-05 14:02:10 +00:00
|
|
|
if(!check_signature("EXTENDED", 8))
|
|
|
|
throw ErrorNotCPCDSK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't really care about about the creator; skip.
|
|
|
|
fseek(file_, 0x30, SEEK_SET);
|
|
|
|
head_position_count_ = (unsigned int)fgetc(file_);
|
|
|
|
head_count_ = (unsigned int)fgetc(file_);
|
2017-08-05 15:44:53 +00:00
|
|
|
|
|
|
|
if(is_extended_) {
|
2017-08-07 15:38:19 +00:00
|
|
|
// Skip two unused bytes and grab the track size table.
|
|
|
|
fseek(file_, 2, SEEK_CUR);
|
2017-08-05 15:44:53 +00:00
|
|
|
for(unsigned int c = 0; c < head_position_count_ * head_count_; c++) {
|
|
|
|
track_sizes_.push_back((size_t)(fgetc(file_) << 8));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_of_a_track_ = fgetc16le();
|
|
|
|
}
|
2017-08-05 14:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CPCDSK::get_head_position_count() {
|
|
|
|
return head_position_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CPCDSK::get_head_count() {
|
|
|
|
return head_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPCDSK::get_is_read_only() {
|
|
|
|
// TODO: allow writing.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Track> CPCDSK::get_uncached_track_at_position(unsigned int head, unsigned int position) {
|
2017-08-05 15:44:53 +00:00
|
|
|
// Given that thesea are interleaved images, determine which track, chronologically, is being requested.
|
|
|
|
unsigned int chronological_track = (position * head_count_) + head;
|
|
|
|
|
|
|
|
// All DSK images reserve 0x100 bytes for their headers.
|
|
|
|
long file_offset = 0x100;
|
|
|
|
if(is_extended_) {
|
|
|
|
// Tracks are a variable size in the original DSK file format; sum the lengths
|
|
|
|
// of all tracks prior to the interesting one to get a file offset.
|
|
|
|
unsigned int t = 0;
|
|
|
|
while(t < chronological_track && t < track_sizes_.size()) {
|
|
|
|
file_offset += track_sizes_[t];
|
2017-08-07 15:36:29 +00:00
|
|
|
t++;
|
2017-08-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Tracks are a fixed size in the original DSK file format.
|
|
|
|
file_offset += size_of_a_track_ * chronological_track;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the track, and skip the unused part of track information.
|
|
|
|
fseek(file_, file_offset + 16, SEEK_SET);
|
|
|
|
|
|
|
|
// Grab the track information.
|
|
|
|
fseek(file_, 5, SEEK_CUR); // skip track number, side number, sector size — each is given per sector
|
|
|
|
int number_of_sectors = fgetc(file_);
|
2017-08-11 18:24:50 +00:00
|
|
|
uint8_t gap3_length = (uint8_t)fgetc(file_);
|
|
|
|
uint8_t filler_byte = (uint8_t)fgetc(file_);
|
2017-08-05 15:44:53 +00:00
|
|
|
|
|
|
|
// Grab the sector information
|
|
|
|
struct SectorInfo {
|
|
|
|
uint8_t track;
|
|
|
|
uint8_t side;
|
|
|
|
uint8_t sector;
|
2017-08-07 16:12:04 +00:00
|
|
|
uint8_t length;
|
2017-08-05 15:44:53 +00:00
|
|
|
uint8_t status1;
|
|
|
|
uint8_t status2;
|
2017-08-07 15:26:15 +00:00
|
|
|
size_t actual_length;
|
2017-08-05 15:44:53 +00:00
|
|
|
};
|
|
|
|
std::vector<SectorInfo> sector_infos;
|
|
|
|
while(number_of_sectors--) {
|
2017-08-07 16:12:04 +00:00
|
|
|
SectorInfo sector_info;
|
|
|
|
|
|
|
|
sector_info.track = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.side = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.sector = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.length = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.status1 = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.status2 = (uint8_t)fgetc(file_);
|
|
|
|
sector_info.actual_length = fgetc16le();
|
|
|
|
|
|
|
|
sector_infos.push_back(sector_info);
|
2017-08-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the sectors.
|
|
|
|
fseek(file_, file_offset + 0x100, SEEK_SET);
|
2017-08-07 15:26:15 +00:00
|
|
|
std::vector<Storage::Encodings::MFM::Sector> sectors;
|
|
|
|
for(auto §or_info : sector_infos) {
|
|
|
|
Storage::Encodings::MFM::Sector new_sector;
|
|
|
|
new_sector.track = sector_info.track;
|
|
|
|
new_sector.side = sector_info.side;
|
|
|
|
new_sector.sector = sector_info.sector;
|
2017-08-11 18:24:50 +00:00
|
|
|
new_sector.size = sector_info.length;
|
2017-08-05 15:44:53 +00:00
|
|
|
|
2017-08-07 16:12:04 +00:00
|
|
|
size_t data_size;
|
|
|
|
if(is_extended_) {
|
|
|
|
data_size = sector_info.actual_length;
|
|
|
|
} else {
|
|
|
|
data_size = (size_t)(128 << sector_info.length);
|
|
|
|
if(data_size == 0x2000) data_size = 0x1800;
|
|
|
|
}
|
2017-08-07 15:26:15 +00:00
|
|
|
new_sector.data.resize(data_size);
|
|
|
|
fread(new_sector.data.data(), sizeof(uint8_t), data_size, file_);
|
|
|
|
|
2017-08-11 18:33:34 +00:00
|
|
|
if(sector_info.status2 & 0x20) {
|
|
|
|
// The CRC failed in the data field.
|
|
|
|
new_sector.has_data_crc_error = true;
|
2017-08-12 20:39:32 +00:00
|
|
|
} else {
|
|
|
|
if(sector_info.status1 & 0x20) {
|
|
|
|
// The CRC failed in the ID field.
|
|
|
|
new_sector.has_header_crc_error = true;
|
|
|
|
}
|
2017-08-11 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(sector_info.status2 & 0x40) {
|
|
|
|
// This sector is marked as deleted.
|
|
|
|
new_sector.is_deleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sector_info.status2 & 0x01) {
|
|
|
|
// Data field wasn't found.
|
|
|
|
new_sector.data.clear();
|
2017-08-07 16:12:04 +00:00
|
|
|
}
|
2017-08-07 15:26:15 +00:00
|
|
|
|
|
|
|
sectors.push_back(std::move(new_sector));
|
2017-08-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:33:34 +00:00
|
|
|
// TODO: extensions to the extended format; John Elliot's addition of single-density support,
|
|
|
|
// and Simon Owen's weak/random sectors, subject to adding some logic to pick a potential
|
|
|
|
// FM/MFM encoding that can produce specified weak values.
|
|
|
|
|
2017-08-11 18:24:50 +00:00
|
|
|
if(sectors.size()) return Storage::Encodings::MFM::GetMFMTrackWithSectors(sectors, gap3_length, filler_byte);
|
2017-08-07 15:26:15 +00:00
|
|
|
|
2017-08-05 14:02:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|