2016-07-10 14:17:53 +00:00
|
|
|
//
|
|
|
|
// G64.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-10 14:17:53 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "G64.hpp"
|
|
|
|
|
2017-11-10 03:04:49 +00:00
|
|
|
#include <cstring>
|
2016-07-10 22:36:52 +00:00
|
|
|
#include <vector>
|
2017-11-10 03:04:49 +00:00
|
|
|
|
2017-09-23 02:39:23 +00:00
|
|
|
#include "../../Track/PCMTrack.hpp"
|
|
|
|
#include "../../Encodings/CommodoreGCR.hpp"
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2016-08-27 21:18:12 +00:00
|
|
|
using namespace Storage::Disk;
|
2016-07-10 14:17:53 +00:00
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
G64::G64(const std::string &file_name) :
|
2017-11-03 02:32:00 +00:00
|
|
|
file_(file_name) {
|
2016-07-10 14:17:53 +00:00
|
|
|
// read and check the file signature
|
2018-04-28 03:18:45 +00:00
|
|
|
if(!file_.check_signature("GCR-1541")) throw Error::InvalidFormat;
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// check the version number
|
2017-11-03 02:32:00 +00:00
|
|
|
int version = file_.get8();
|
2018-04-28 03:18:45 +00:00
|
|
|
if(version != 0) throw Error::UnknownVersion;
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// get the number of tracks and track size
|
2017-11-03 02:32:00 +00:00
|
|
|
number_of_tracks_ = file_.get8();
|
|
|
|
maximum_track_size_ = file_.get16le();
|
2016-07-10 14:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
HeadPosition G64::get_maximum_head_position() {
|
2016-07-10 14:17:53 +00:00
|
|
|
// give at least 84 tracks, to yield the normal geometry but,
|
|
|
|
// if there are more, shove them in
|
2018-05-07 03:17:36 +00:00
|
|
|
return HeadPosition(number_of_tracks_ > 84 ? number_of_tracks_ : 84, 2);
|
2016-07-10 14:17:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
std::shared_ptr<Track> G64::get_track_at_position(Track::Address address) {
|
2016-07-10 14:17:53 +00:00
|
|
|
std::shared_ptr<Track> resulting_track;
|
|
|
|
|
|
|
|
// seek to this track's entry in the track table
|
2018-05-07 03:17:36 +00:00
|
|
|
file_.seek(static_cast<long>((address.position.as_half() * 4) + 0xc), SEEK_SET);
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// read the track offset
|
2018-07-01 21:59:43 +00:00
|
|
|
const uint32_t track_offset = file_.get32le();
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// if the track offset is zero, this track doesn't exist, so...
|
2018-07-01 21:59:43 +00:00
|
|
|
if(!track_offset) return nullptr;
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// seek to the track start
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(static_cast<long>(track_offset), SEEK_SET);
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// get the real track length
|
2018-07-01 21:59:43 +00:00
|
|
|
const uint16_t track_length = file_.get16le();
|
2016-07-10 14:17:53 +00:00
|
|
|
|
|
|
|
// grab the byte contents of this track
|
2018-07-01 21:59:43 +00:00
|
|
|
const std::vector<uint8_t> track_contents = file_.read(track_length);
|
2016-07-10 14:17:53 +00:00
|
|
|
|
2016-07-10 17:42:45 +00:00
|
|
|
// seek to this track's entry in the speed zone table
|
2018-05-07 03:17:36 +00:00
|
|
|
file_.seek(static_cast<long>((address.position.as_half() * 4) + 0x15c), SEEK_SET);
|
2016-07-10 14:17:53 +00:00
|
|
|
|
2016-07-10 17:42:45 +00:00
|
|
|
// read the speed zone offsrt
|
2018-07-01 21:59:43 +00:00
|
|
|
const uint32_t speed_zone_offset = file_.get32le();
|
2016-07-10 17:42:45 +00:00
|
|
|
|
|
|
|
// if the speed zone is not constant, create a track based on the whole table; otherwise create one that's constant
|
2017-03-26 18:34:47 +00:00
|
|
|
if(speed_zone_offset > 3) {
|
2016-07-10 17:42:45 +00:00
|
|
|
// seek to start of speed zone
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.seek(static_cast<long>(speed_zone_offset), SEEK_SET);
|
2016-07-10 17:42:45 +00:00
|
|
|
|
|
|
|
// read the speed zone bytes
|
2018-07-01 21:59:43 +00:00
|
|
|
const uint16_t speed_zone_length = (track_length + 3) >> 2;
|
2016-07-10 17:42:45 +00:00
|
|
|
uint8_t speed_zone_contents[speed_zone_length];
|
2017-11-03 02:32:00 +00:00
|
|
|
file_.read(speed_zone_contents, speed_zone_length);
|
2016-07-10 20:10:05 +00:00
|
|
|
|
2016-07-10 22:07:53 +00:00
|
|
|
// divide track into appropriately timed PCMSegments
|
|
|
|
std::vector<PCMSegment> segments;
|
|
|
|
unsigned int current_speed = speed_zone_contents[0] >> 6;
|
|
|
|
unsigned int start_byte_in_current_speed = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
for(unsigned int byte = 0; byte < track_length; byte ++) {
|
2016-07-29 15:03:09 +00:00
|
|
|
unsigned int byte_speed = speed_zone_contents[byte >> 2] >> (6 - (byte&3)*2);
|
2017-11-12 22:48:11 +00:00
|
|
|
if(byte_speed != current_speed || byte == static_cast<uint16_t>(track_length-1)) {
|
2016-07-10 22:07:53 +00:00
|
|
|
unsigned int number_of_bytes = byte - start_byte_in_current_speed;
|
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
PCMSegment segment(
|
|
|
|
Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(current_speed),
|
|
|
|
number_of_bytes * 8,
|
|
|
|
&track_contents[start_byte_in_current_speed]);
|
2016-07-10 22:07:53 +00:00
|
|
|
segments.push_back(std::move(segment));
|
|
|
|
|
|
|
|
current_speed = byte_speed;
|
|
|
|
start_byte_in_current_speed = byte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resulting_track.reset(new PCMTrack(std::move(segments)));
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2018-07-01 16:05:41 +00:00
|
|
|
PCMSegment segment(
|
|
|
|
Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(static_cast<unsigned int>(speed_zone_offset)),
|
|
|
|
track_length * 8,
|
|
|
|
track_contents
|
|
|
|
);
|
2016-07-10 20:10:05 +00:00
|
|
|
|
|
|
|
resulting_track.reset(new PCMTrack(std::move(segment)));
|
2016-07-10 17:42:45 +00:00
|
|
|
}
|
2016-07-10 14:17:53 +00:00
|
|
|
|
2016-07-10 22:24:12 +00:00
|
|
|
// TODO: find out whether it's possible for a G64 to supply only a partial track. I don't think it is, which would make the
|
|
|
|
// above correct but supposing I'm wrong, the above would produce some incorrectly clocked tracks
|
|
|
|
|
2016-07-10 14:17:53 +00:00
|
|
|
return resulting_track;
|
|
|
|
}
|