1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-17 13:29:02 +00:00
CLK/Storage/Disk/DiskImage/Formats/AppleDSK.cpp
Thomas Harte a9d4fe0b41 Introduces filetype wiring for DO and PO files.
Also corrects sector numbering logic to ensure there is a sector 15.
2018-04-29 16:34:10 -04:00

81 lines
2.6 KiB
C++

//
// AppleDSK.cpp
// Clock Signal
//
// Created by Thomas Harte on 27/04/2018.
// Copyright © 2018 Thomas Harte. All rights reserved.
//
#include "AppleDSK.hpp"
#include "../../Track/PCMTrack.hpp"
#include "../../Encodings/AppleGCR.hpp"
using namespace Storage::Disk;
namespace {
const int number_of_tracks = 35;
const int bytes_per_sector = 256;
}
AppleDSK::AppleDSK(const std::string &file_name) :
file_(file_name) {
if(file_.stats().st_size % number_of_tracks*bytes_per_sector) throw Error::InvalidFormat;
sectors_per_track_ = static_cast<int>(file_.stats().st_size / (number_of_tracks*bytes_per_sector));
if(sectors_per_track_ != 13 && sectors_per_track_ != 16) throw Error::InvalidFormat;
// Check whether this is a Pro DOS disk by inspecting the filename.
if(sectors_per_track_ == 16) {
size_t string_index = file_name.size()-1;
while(file_name[string_index] != '.') {
if(file_name[string_index] == 'p') {
is_prodos_ = true;
break;
}
--string_index;
}
}
}
int AppleDSK::get_head_position_count() {
return number_of_tracks * 4;
}
std::shared_ptr<Track> AppleDSK::get_track_at_position(Track::Address address) {
const long file_offset = (address.position >> 2) * bytes_per_sector * sectors_per_track_;
file_.seek(file_offset, SEEK_SET);
const std::vector<uint8_t> track_data = file_.read(static_cast<size_t>(bytes_per_sector * sectors_per_track_));
std::vector<Storage::Disk::PCMSegment> segments;
const uint8_t track = static_cast<uint8_t>(address.position >> 2);
// In either case below, the code aims for exactly 50,000 bits per track.
if(sectors_per_track_ == 16) {
// Write the sectors.
uint8_t sector_number_ = 0;
for(std::size_t c = 0; c < 16; ++c) {
segments.push_back(Encodings::AppleGCR::six_and_two_sync(10));
segments.push_back(Encodings::AppleGCR::header(0, track, sector_number_));
segments.push_back(Encodings::AppleGCR::six_and_two_sync(10));
segments.push_back(Encodings::AppleGCR::six_and_two_data(&track_data[c * 256]));
segments.push_back(Encodings::AppleGCR::six_and_two_sync(10));
// DOS and Pro DOS interleave sectors on disk, and they're represented in a disk
// image in physical order rather than logical. So that skew needs to be applied here.
sector_number_ += is_prodos_ ? 8 : 7;
if(sector_number_ > 0xf) sector_number_ %= 15;
}
// Pad if necessary.
int encoded_length = (80 + 112 + 80 + 2848 + 80) * sectors_per_track_;
if(encoded_length < 50000) {
segments.push_back(Encodings::AppleGCR::six_and_two_sync((50000 - encoded_length) >> 3));
}
} else {
}
return std::shared_ptr<PCMTrack>(new PCMTrack(segments));
}