2018-04-28 03:18:45 +00:00
|
|
|
//
|
|
|
|
// AppleDSK.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 27/04/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AppleDSK.hpp"
|
|
|
|
|
2018-04-28 19:18:48 +00:00
|
|
|
#include "../../Track/PCMTrack.hpp"
|
2018-05-04 02:40:45 +00:00
|
|
|
#include "../../Encodings/AppleGCR/Encoder.hpp"
|
2018-04-28 19:18:48 +00:00
|
|
|
|
2018-04-28 03:18:45 +00:00
|
|
|
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;
|
2018-04-29 20:34:10 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2018-04-28 03:18:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
HeadPosition AppleDSK::get_maximum_head_position() {
|
|
|
|
return HeadPosition(number_of_tracks);
|
2018-04-28 03:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Track> AppleDSK::get_track_at_position(Track::Address address) {
|
2018-05-07 03:17:36 +00:00
|
|
|
const long file_offset = address.position.as_int() * bytes_per_sector * sectors_per_track_;
|
2018-04-28 03:18:45 +00:00
|
|
|
file_.seek(file_offset, SEEK_SET);
|
2018-04-28 19:18:48 +00:00
|
|
|
const std::vector<uint8_t> track_data = file_.read(static_cast<size_t>(bytes_per_sector * sectors_per_track_));
|
|
|
|
|
2018-05-02 00:31:42 +00:00
|
|
|
Storage::Disk::PCMSegment segment;
|
2018-05-07 03:17:36 +00:00
|
|
|
const uint8_t track = static_cast<uint8_t>(address.position.as_int());
|
2018-04-28 19:18:48 +00:00
|
|
|
|
|
|
|
// In either case below, the code aims for exactly 50,000 bits per track.
|
|
|
|
if(sectors_per_track_ == 16) {
|
|
|
|
// Write the sectors.
|
2018-05-03 01:28:18 +00:00
|
|
|
std::size_t sector_number_ = 0;
|
|
|
|
for(uint8_t c = 0; c < 16; ++c) {
|
2018-05-02 00:31:42 +00:00
|
|
|
segment += Encodings::AppleGCR::six_and_two_sync(10);
|
2018-05-10 00:28:58 +00:00
|
|
|
segment += Encodings::AppleGCR::header(254, track, c);
|
2018-05-02 00:31:42 +00:00
|
|
|
segment += Encodings::AppleGCR::six_and_two_sync(10);
|
2018-05-03 01:28:18 +00:00
|
|
|
segment += Encodings::AppleGCR::six_and_two_data(&track_data[sector_number_ * 256]);
|
2018-04-29 20:18:14 +00:00
|
|
|
|
|
|
|
// 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.
|
2018-04-29 20:34:10 +00:00
|
|
|
sector_number_ += is_prodos_ ? 8 : 7;
|
|
|
|
if(sector_number_ > 0xf) sector_number_ %= 15;
|
2018-04-28 19:18:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 19:47:50 +00:00
|
|
|
// Pad if necessary.
|
2018-05-02 00:31:42 +00:00
|
|
|
if(segment.number_of_bits < 50000) {
|
|
|
|
segment += Encodings::AppleGCR::six_and_two_sync((50000 - segment.number_of_bits) >> 3);
|
2018-04-28 19:47:50 +00:00
|
|
|
}
|
2018-04-28 19:18:48 +00:00
|
|
|
} else {
|
2018-04-28 03:18:45 +00:00
|
|
|
|
2018-04-28 19:18:48 +00:00
|
|
|
}
|
2018-04-28 03:18:45 +00:00
|
|
|
|
2018-05-02 00:31:42 +00:00
|
|
|
return std::make_shared<PCMTrack>(segment);
|
2018-04-28 03:18:45 +00:00
|
|
|
}
|