2016-09-13 11:26:51 +00:00
|
|
|
//
|
|
|
|
// Disk.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 13/09/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-09-13 11:26:51 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Disk.hpp"
|
2018-01-25 02:48:44 +00:00
|
|
|
#include "../../../Storage/Disk/Controller/DiskController.hpp"
|
|
|
|
#include "../../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
|
|
|
#include "../../../Storage/Data/Commodore.hpp"
|
2016-09-14 01:53:36 +00:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
#include <array>
|
2016-09-13 11:26:51 +00:00
|
|
|
|
2018-01-25 02:48:44 +00:00
|
|
|
using namespace Analyser::Static::Commodore;
|
2016-09-13 11:26:51 +00:00
|
|
|
|
2016-09-26 00:05:56 +00:00
|
|
|
class CommodoreGCRParser: public Storage::Disk::Controller {
|
2016-09-14 01:53:36 +00:00
|
|
|
public:
|
2016-09-26 01:24:16 +00:00
|
|
|
std::shared_ptr<Storage::Disk::Drive> drive;
|
|
|
|
|
2017-09-11 02:56:05 +00:00
|
|
|
CommodoreGCRParser() : Storage::Disk::Controller(4000000), shift_register_(0), track_(1) {
|
2017-09-16 01:18:36 +00:00
|
|
|
drive.reset(new Storage::Disk::Drive(4000000, 300, 2));
|
2016-09-26 01:24:16 +00:00
|
|
|
set_drive(drive);
|
2017-09-11 02:56:05 +00:00
|
|
|
drive->set_motor_on(true);
|
2016-09-15 11:54:42 +00:00
|
|
|
}
|
2016-09-14 01:53:36 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
struct Sector {
|
2016-09-14 01:53:36 +00:00
|
|
|
uint8_t sector, track;
|
|
|
|
std::array<uint8_t, 256> data;
|
|
|
|
bool header_checksum_matched;
|
|
|
|
bool data_checksum_matched;
|
|
|
|
};
|
|
|
|
|
2016-09-15 11:58:32 +00:00
|
|
|
/*!
|
|
|
|
Attempts to read the sector located at @c track and @c sector.
|
|
|
|
|
|
|
|
@returns a sector if one was found; @c nullptr otherwise.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Sector> get_sector(uint8_t track, uint8_t sector) {
|
2017-10-22 01:00:40 +00:00
|
|
|
int difference = static_cast<int>(track) - static_cast<int>(track_);
|
2016-09-15 11:54:42 +00:00
|
|
|
track_ = track;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(difference) {
|
2016-09-15 11:54:42 +00:00
|
|
|
int direction = difference < 0 ? -1 : 1;
|
2018-05-07 03:17:36 +00:00
|
|
|
difference *= direction;
|
2016-09-15 11:54:42 +00:00
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
for(int c = 0; c < difference; c++) {
|
|
|
|
get_drive().step(Storage::Disk::HeadPosition(direction));
|
|
|
|
}
|
2016-09-15 11:54:42 +00:00
|
|
|
|
|
|
|
unsigned int zone = 3;
|
|
|
|
if(track >= 18) zone = 2;
|
|
|
|
else if(track >= 25) zone = 1;
|
|
|
|
else if(track >= 31) zone = 0;
|
|
|
|
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(zone));
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_sector(sector);
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 11:58:32 +00:00
|
|
|
private:
|
|
|
|
unsigned int shift_register_;
|
|
|
|
int index_count_;
|
|
|
|
int bit_count_;
|
|
|
|
uint8_t track_;
|
2016-09-18 02:01:54 +00:00
|
|
|
std::shared_ptr<Sector> sector_cache_[65536];
|
2016-09-15 11:58:32 +00:00
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void process_input_bit(int value) {
|
2017-10-21 23:49:04 +00:00
|
|
|
shift_register_ = ((shift_register_ << 1) | static_cast<unsigned int>(value)) & 0x3ff;
|
2016-09-15 11:58:32 +00:00
|
|
|
bit_count_++;
|
|
|
|
}
|
|
|
|
|
2018-03-23 00:23:21 +00:00
|
|
|
unsigned int proceed_to_next_block(int max_index_count) {
|
2016-09-15 11:58:32 +00:00
|
|
|
// find GCR lead-in
|
|
|
|
proceed_to_shift_value(0x3ff);
|
|
|
|
if(shift_register_ != 0x3ff) return 0xff;
|
|
|
|
|
|
|
|
// find end of lead-in
|
2018-03-23 00:23:21 +00:00
|
|
|
while(shift_register_ == 0x3ff && index_count_ < max_index_count) {
|
2017-07-25 01:19:05 +00:00
|
|
|
run_for(Cycles(1));
|
2016-09-15 11:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// continue for a further nine bits
|
|
|
|
bit_count_ = 0;
|
2018-03-23 00:23:21 +00:00
|
|
|
while(bit_count_ < 9 && index_count_ < max_index_count) {
|
2017-07-25 01:19:05 +00:00
|
|
|
run_for(Cycles(1));
|
2016-09-15 11:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Storage::Encodings::CommodoreGCR::decoding_from_dectet(shift_register_);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int get_next_byte() {
|
2016-09-15 11:58:32 +00:00
|
|
|
bit_count_ = 0;
|
2017-07-25 01:19:05 +00:00
|
|
|
while(bit_count_ < 10) run_for(Cycles(1));
|
2016-09-15 11:58:32 +00:00
|
|
|
return Storage::Encodings::CommodoreGCR::decoding_from_dectet(shift_register_);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void proceed_to_shift_value(unsigned int shift_value) {
|
2018-03-23 00:23:21 +00:00
|
|
|
const int max_index_count = index_count_ + 2;
|
|
|
|
while(shift_register_ != shift_value && index_count_ < max_index_count) {
|
2017-07-25 01:19:05 +00:00
|
|
|
run_for(Cycles(1));
|
2016-09-15 11:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void process_index_hole() {
|
2016-09-15 11:58:32 +00:00
|
|
|
index_count_++;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Sector> get_sector(uint8_t sector) {
|
2017-10-22 01:50:53 +00:00
|
|
|
uint16_t sector_address = static_cast<uint16_t>((track_ << 8) | sector);
|
2016-09-18 02:01:54 +00:00
|
|
|
if(sector_cache_[sector_address]) return sector_cache_[sector_address];
|
|
|
|
|
|
|
|
std::shared_ptr<Sector> first_sector = get_next_sector();
|
2016-09-14 01:53:36 +00:00
|
|
|
if(!first_sector) return first_sector;
|
|
|
|
if(first_sector->sector == sector) return first_sector;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2016-09-18 02:01:54 +00:00
|
|
|
std::shared_ptr<Sector> next_sector = get_next_sector();
|
2016-09-14 01:53:36 +00:00
|
|
|
if(next_sector->sector == first_sector->sector) return nullptr;
|
|
|
|
if(next_sector->sector == sector) return next_sector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Sector> get_next_sector() {
|
2016-09-18 02:01:54 +00:00
|
|
|
std::shared_ptr<Sector> sector(new Sector);
|
2018-03-23 00:23:21 +00:00
|
|
|
const int max_index_count = index_count_ + 2;
|
2016-09-14 01:53:36 +00:00
|
|
|
|
2018-03-23 00:23:21 +00:00
|
|
|
while(index_count_ < max_index_count) {
|
2016-09-14 01:53:36 +00:00
|
|
|
// look for a sector header
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2018-03-23 00:23:21 +00:00
|
|
|
if(proceed_to_next_block(max_index_count) == 0x08) break;
|
|
|
|
if(index_count_ >= max_index_count) return nullptr;
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get sector details, skip if this looks malformed
|
2017-10-22 01:50:53 +00:00
|
|
|
uint8_t checksum = static_cast<uint8_t>(get_next_byte());
|
|
|
|
sector->sector = static_cast<uint8_t>(get_next_byte());
|
|
|
|
sector->track = static_cast<uint8_t>(get_next_byte());
|
2016-09-14 01:53:36 +00:00
|
|
|
uint8_t disk_id[2];
|
2017-10-22 01:50:53 +00:00
|
|
|
disk_id[0] = static_cast<uint8_t>(get_next_byte());
|
|
|
|
disk_id[1] = static_cast<uint8_t>(get_next_byte());
|
2016-09-14 01:53:36 +00:00
|
|
|
if(checksum != (sector->sector ^ sector->track ^ disk_id[0] ^ disk_id[1])) continue;
|
|
|
|
|
|
|
|
// look for the following data
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2018-03-23 00:23:21 +00:00
|
|
|
if(proceed_to_next_block(max_index_count) == 0x07) break;
|
|
|
|
if(index_count_ >= max_index_count) return nullptr;
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checksum = 0;
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t c = 0; c < 256; c++) {
|
2017-10-22 01:50:53 +00:00
|
|
|
sector->data[c] = static_cast<uint8_t>(get_next_byte());
|
2016-09-14 01:53:36 +00:00
|
|
|
checksum ^= sector->data[c];
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(checksum == get_next_byte()) {
|
2017-10-22 01:50:53 +00:00
|
|
|
uint16_t sector_address = static_cast<uint16_t>((sector->track << 8) | sector->sector);
|
2016-09-18 02:01:54 +00:00
|
|
|
sector_cache_[sector_address] = sector;
|
|
|
|
return sector;
|
|
|
|
}
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 02:48:44 +00:00
|
|
|
std::vector<File> Analyser::Static::Commodore::GetFiles(const std::shared_ptr<Storage::Disk::Disk> &disk) {
|
2018-01-24 03:18:16 +00:00
|
|
|
std::vector<File> files;
|
2016-09-14 01:53:36 +00:00
|
|
|
CommodoreGCRParser parser;
|
2016-09-26 01:24:16 +00:00
|
|
|
parser.drive->set_disk(disk);
|
2016-09-14 01:53:36 +00:00
|
|
|
|
|
|
|
// find any sector whatsoever to establish the current track
|
2016-09-18 02:01:54 +00:00
|
|
|
std::shared_ptr<CommodoreGCRParser::Sector> sector;
|
2016-09-14 01:53:36 +00:00
|
|
|
|
2016-09-15 11:54:42 +00:00
|
|
|
// assemble directory
|
2016-09-14 01:53:36 +00:00
|
|
|
std::vector<uint8_t> directory;
|
2016-09-15 11:54:42 +00:00
|
|
|
uint8_t next_track = 18;
|
|
|
|
uint8_t next_sector = 1;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2016-09-15 11:54:42 +00:00
|
|
|
sector = parser.get_sector(next_track, next_sector);
|
|
|
|
if(!sector) break;
|
2016-09-14 01:53:36 +00:00
|
|
|
directory.insert(directory.end(), sector->data.begin(), sector->data.end());
|
2016-09-15 11:54:42 +00:00
|
|
|
next_track = sector->data[0];
|
|
|
|
next_sector = sector->data[1];
|
2016-09-14 01:53:36 +00:00
|
|
|
|
|
|
|
if(!next_track) break;
|
2016-09-15 11:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse directory
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t header_pointer = static_cast<std::size_t>(-32);
|
2017-03-26 18:34:47 +00:00
|
|
|
while(header_pointer+32+31 < directory.size()) {
|
2016-09-15 11:54:42 +00:00
|
|
|
header_pointer += 32;
|
|
|
|
|
|
|
|
File new_file;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(directory[header_pointer + 2] & 7) {
|
2016-09-15 11:54:42 +00:00
|
|
|
case 0: // DEL files
|
|
|
|
default: continue; // Unknown file types
|
|
|
|
|
|
|
|
case 1: new_file.type = File::DataSequence; break;
|
|
|
|
case 2: new_file.type = File::RelocatableProgram; break; // TODO: need a "don't know about relocatable" program?
|
|
|
|
case 3: new_file.type = File::User; break;
|
|
|
|
// case 4: new_file.type = File::Relative; break; // Can't handle REL files yet
|
|
|
|
}
|
|
|
|
|
|
|
|
next_track = directory[header_pointer + 3];
|
|
|
|
next_sector = directory[header_pointer + 4];
|
|
|
|
|
|
|
|
new_file.raw_name.reserve(16);
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t c = 0; c < 16; c++) {
|
2016-09-15 11:54:42 +00:00
|
|
|
new_file.raw_name.push_back(directory[header_pointer + 5 + c]);
|
|
|
|
}
|
2016-11-06 23:43:51 +00:00
|
|
|
new_file.name = Storage::Data::Commodore::petscii_from_bytes(&new_file.raw_name[0], 16, false);
|
2016-09-15 11:54:42 +00:00
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t number_of_sectors = static_cast<std::size_t>(directory[header_pointer + 0x1e]) + (static_cast<std::size_t>(directory[header_pointer + 0x1f]) << 8);
|
2016-09-15 11:54:42 +00:00
|
|
|
new_file.data.reserve((number_of_sectors - 1) * 254 + 252);
|
|
|
|
|
|
|
|
bool is_first_sector = true;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(next_track) {
|
2016-09-15 11:54:42 +00:00
|
|
|
sector = parser.get_sector(next_track, next_sector);
|
|
|
|
if(!sector) break;
|
|
|
|
|
|
|
|
next_track = sector->data[0];
|
|
|
|
next_sector = sector->data[1];
|
|
|
|
|
2017-10-22 01:50:53 +00:00
|
|
|
if(is_first_sector) new_file.starting_address = static_cast<uint16_t>(sector->data[2]) | static_cast<uint16_t>(sector->data[3] << 8);
|
2016-09-15 11:54:42 +00:00
|
|
|
if(next_track)
|
|
|
|
new_file.data.insert(new_file.data.end(), sector->data.begin() + (is_first_sector ? 4 : 2), sector->data.end());
|
|
|
|
else
|
|
|
|
new_file.data.insert(new_file.data.end(), sector->data.begin() + 2, sector->data.begin() + next_sector);
|
|
|
|
|
|
|
|
is_first_sector = false;
|
|
|
|
}
|
2016-09-14 01:53:36 +00:00
|
|
|
|
2016-09-15 11:54:42 +00:00
|
|
|
if(!next_track) files.push_back(new_file);
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
2016-09-13 11:26:51 +00:00
|
|
|
|
2016-09-14 01:53:36 +00:00
|
|
|
return files;
|
2016-09-13 11:26:51 +00:00
|
|
|
}
|