2016-08-01 11:09:15 +00:00
|
|
|
//
|
|
|
|
// D64.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 01/08/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "D64.hpp"
|
|
|
|
|
2016-08-01 12:41:16 +00:00
|
|
|
#include <algorithm>
|
2017-09-23 02:39:23 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "../../Track/PCMTrack.hpp"
|
|
|
|
#include "../../Encodings/CommodoreGCR.hpp"
|
2016-08-01 12:41:16 +00:00
|
|
|
|
2016-08-27 21:15:09 +00:00
|
|
|
using namespace Storage::Disk;
|
2016-08-01 11:09:15 +00:00
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
D64::D64(const char *file_name) :
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::FileHolder(file_name) {
|
2016-08-01 12:41:16 +00:00
|
|
|
// in D64, this is it for validation without imposing potential false-negative tests — check that
|
|
|
|
// the file size appears to be correct. Stone-age stuff.
|
2016-11-21 12:14:09 +00:00
|
|
|
if(file_stats_.st_size != 174848 && file_stats_.st_size != 196608)
|
2016-08-01 12:41:16 +00:00
|
|
|
throw ErrorNotD64;
|
|
|
|
|
2016-11-21 12:14:09 +00:00
|
|
|
number_of_tracks_ = (file_stats_.st_size == 174848) ? 35 : 40;
|
2016-08-01 12:41:16 +00:00
|
|
|
|
2016-08-01 13:43:08 +00:00
|
|
|
// then, ostensibly, this is a valid file. Hmmm. Pick a disk ID as a function of the file_name,
|
|
|
|
// being the most stable thing available
|
2016-11-21 12:14:09 +00:00
|
|
|
disk_id_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(*file_name) {
|
2016-11-21 12:14:09 +00:00
|
|
|
disk_id_ ^= file_name[0];
|
|
|
|
disk_id_ = (uint16_t)((disk_id_ << 2) ^ (disk_id_ >> 13));
|
2016-08-01 13:43:08 +00:00
|
|
|
file_name++;
|
|
|
|
}
|
2016-08-01 11:09:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
int D64::get_head_position_count() {
|
2016-11-21 12:14:09 +00:00
|
|
|
return number_of_tracks_*2;
|
2016-08-01 11:09:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
std::shared_ptr<Track> D64::get_track_at_position(Track::Address address) {
|
2016-09-18 23:21:02 +00:00
|
|
|
// every other track is missing, as is any head above 0
|
2017-10-07 01:45:12 +00:00
|
|
|
if(address.position&1 || address.head)
|
2016-08-01 13:43:08 +00:00
|
|
|
return std::shared_ptr<Track>();
|
2016-08-01 12:41:16 +00:00
|
|
|
|
|
|
|
// figure out where this track starts on the disk
|
|
|
|
int offset_to_track = 0;
|
2017-10-07 01:45:12 +00:00
|
|
|
int tracks_to_traverse = address.position >> 1;
|
2016-08-01 12:41:16 +00:00
|
|
|
|
|
|
|
int zone_sizes[] = {17, 7, 6, 10};
|
|
|
|
int sectors_by_zone[] = {21, 19, 18, 17};
|
|
|
|
int zone = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
for(int current_zone = 0; current_zone < 4; current_zone++) {
|
2016-08-01 12:41:16 +00:00
|
|
|
int tracks_in_this_zone = std::min(tracks_to_traverse, zone_sizes[current_zone]);
|
|
|
|
offset_to_track += tracks_in_this_zone * sectors_by_zone[current_zone];
|
|
|
|
tracks_to_traverse -= tracks_in_this_zone;
|
|
|
|
if(tracks_in_this_zone == zone_sizes[current_zone]) zone++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// seek to start of data
|
2016-11-21 12:14:09 +00:00
|
|
|
fseek(file_, offset_to_track * 256, SEEK_SET);
|
2016-08-01 12:41:16 +00:00
|
|
|
|
|
|
|
// build up a PCM sampling of the GCR version of this track
|
|
|
|
|
|
|
|
// format per sector:
|
|
|
|
//
|
|
|
|
// syncronisation: three $FFs directly in GCR
|
|
|
|
// value $08 to announce a header
|
|
|
|
// a checksum made of XORing the following four bytes
|
|
|
|
// sector number (1 byte)
|
|
|
|
// track number (1 byte)
|
|
|
|
// disk ID (2 bytes)
|
|
|
|
// five GCR bytes of value $55
|
|
|
|
// = [6 bytes -> 7.5 GCR bytes] + ... = 21 GCR bytes
|
|
|
|
//
|
|
|
|
// syncronisation: three $FFs directly in GCR
|
|
|
|
// value $07 to announce data
|
|
|
|
// 256 data bytes
|
|
|
|
// a checksum: the XOR of the previous 256 bytes
|
|
|
|
// two bytes of vaue $00
|
|
|
|
// = [260 bytes -> 325 GCR bytes] + 3 GCR bytes = 328 GCR bytes
|
|
|
|
//
|
|
|
|
// = 349 GCR bytes per sector
|
|
|
|
|
|
|
|
PCMSegment track;
|
|
|
|
size_t track_bytes = 349 * (size_t)sectors_by_zone[zone];
|
|
|
|
track.number_of_bits = (unsigned int)track_bytes * 8;
|
2016-09-18 22:56:35 +00:00
|
|
|
track.data.resize(track_bytes);
|
|
|
|
uint8_t *data = &track.data[0];
|
2016-08-01 12:41:16 +00:00
|
|
|
|
2016-08-01 13:43:08 +00:00
|
|
|
memset(data, 0, track_bytes);
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
for(int sector = 0; sector < sectors_by_zone[zone]; sector++) {
|
2016-08-01 12:41:16 +00:00
|
|
|
uint8_t *sector_data = &data[sector * 349];
|
|
|
|
sector_data[0] = sector_data[1] = sector_data[2] = 0xff;
|
|
|
|
|
2016-08-01 14:08:38 +00:00
|
|
|
uint8_t sector_number = (uint8_t)(sector); // sectors count from 0
|
2017-10-07 01:45:12 +00:00
|
|
|
uint8_t track_number = (uint8_t)((address.position >> 1) + 1); // tracks count from 1
|
2016-11-21 12:14:09 +00:00
|
|
|
uint8_t checksum = (uint8_t)(sector_number ^ track_number ^ disk_id_ ^ (disk_id_ >> 8));
|
2016-08-01 12:41:16 +00:00
|
|
|
uint8_t header_start[4] = {
|
|
|
|
0x08, checksum, sector_number, track_number
|
|
|
|
};
|
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[3], header_start);
|
|
|
|
|
|
|
|
uint8_t header_end[4] = {
|
2016-11-21 12:14:09 +00:00
|
|
|
(uint8_t)(disk_id_ & 0xff), (uint8_t)(disk_id_ >> 8), 0, 0
|
2016-08-01 12:41:16 +00:00
|
|
|
};
|
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[8], header_end);
|
2016-08-01 13:43:08 +00:00
|
|
|
|
|
|
|
// pad out post-header parts
|
|
|
|
uint8_t zeros[4] = {0, 0, 0, 0};
|
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[13], zeros);
|
2016-08-01 12:41:16 +00:00
|
|
|
sector_data[18] = 0x52;
|
|
|
|
sector_data[19] = 0x94;
|
|
|
|
sector_data[20] = 0xaf;
|
|
|
|
|
|
|
|
// get the actual contents
|
|
|
|
uint8_t source_data[256];
|
2016-11-21 12:14:09 +00:00
|
|
|
fread(source_data, 1, 256, file_);
|
2016-08-01 12:41:16 +00:00
|
|
|
|
|
|
|
// compute the latest checksum
|
|
|
|
checksum = 0;
|
|
|
|
for(int c = 0; c < 256; c++)
|
|
|
|
checksum ^= source_data[c];
|
|
|
|
|
|
|
|
// put in another sync
|
|
|
|
sector_data[21] = sector_data[22] = sector_data[23] = 0xff;
|
|
|
|
|
|
|
|
// now start writing in the actual data
|
|
|
|
uint8_t start_of_data[4] = {
|
|
|
|
0x07, source_data[0], source_data[1], source_data[2]
|
|
|
|
};
|
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[24], start_of_data);
|
|
|
|
int source_data_offset = 3;
|
|
|
|
int target_data_offset = 29;
|
2017-03-26 18:34:47 +00:00
|
|
|
while((source_data_offset+4) < 256) {
|
2016-08-01 12:41:16 +00:00
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[target_data_offset], &source_data[source_data_offset]);
|
|
|
|
target_data_offset += 5;
|
|
|
|
source_data_offset += 4;
|
|
|
|
}
|
|
|
|
uint8_t end_of_data[4] = {
|
|
|
|
source_data[255], checksum, 0, 0
|
|
|
|
};
|
|
|
|
Encodings::CommodoreGCR::encode_block(§or_data[target_data_offset], end_of_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::shared_ptr<Track>(new PCMTrack(std::move(track)));
|
2016-08-01 11:09:15 +00:00
|
|
|
}
|