2016-09-13 11:26:51 +00:00
|
|
|
//
|
|
|
|
// Disk.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 13/09/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Disk.hpp"
|
2016-09-26 00:05:56 +00:00
|
|
|
#include "../../Storage/Disk/DiskController.hpp"
|
2016-09-14 01:53:36 +00:00
|
|
|
#include "../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
2016-11-06 23:43:51 +00:00
|
|
|
#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
|
|
|
|
|
|
|
using namespace StaticAnalyser::Commodore;
|
|
|
|
|
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-03-26 18:34:47 +00:00
|
|
|
CommodoreGCRParser() : Storage::Disk::Controller(4000000, 1, 300), shift_register_(0), track_(1) {
|
2016-09-26 01:24:16 +00:00
|
|
|
drive.reset(new Storage::Disk::Drive);
|
|
|
|
set_drive(drive);
|
2017-05-17 00:53:06 +00:00
|
|
|
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) {
|
2016-09-15 11:54:42 +00:00
|
|
|
int difference = (int)track - (int)track_;
|
|
|
|
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;
|
|
|
|
difference *= 2 * direction;
|
|
|
|
|
|
|
|
for(int c = 0; c < difference; c++) step(direction);
|
|
|
|
|
|
|
|
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-03-26 18:34:47 +00:00
|
|
|
void process_input_bit(int value, unsigned int cycles_since_index_hole) {
|
2016-09-15 11:58:32 +00:00
|
|
|
shift_register_ = ((shift_register_ << 1) | (unsigned int)value) & 0x3ff;
|
|
|
|
bit_count_++;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int proceed_to_next_block() {
|
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
|
2017-03-26 18:34:47 +00:00
|
|
|
while(shift_register_ == 0x3ff && index_count_ < 2) {
|
2016-09-15 11:58:32 +00:00
|
|
|
run_for_cycles(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue for a further nine bits
|
|
|
|
bit_count_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(bit_count_ < 9 && index_count_ < 2) {
|
2016-09-15 11:58:32 +00:00
|
|
|
run_for_cycles(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
while(bit_count_ < 10) run_for_cycles(1);
|
|
|
|
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) {
|
2016-09-15 11:58:32 +00:00
|
|
|
index_count_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(shift_register_ != shift_value && index_count_ < 2) {
|
2016-09-15 11:58:32 +00:00
|
|
|
run_for_cycles(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-09-18 02:01:54 +00:00
|
|
|
uint16_t sector_address = (uint16_t)((track_ << 8) | sector);
|
|
|
|
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);
|
2016-09-14 01:53:36 +00:00
|
|
|
index_count_ = 0;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
while(index_count_ < 2) {
|
2016-09-14 01:53:36 +00:00
|
|
|
// look for a sector header
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2016-09-14 01:53:36 +00:00
|
|
|
if(proceed_to_next_block() == 0x08) break;
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get sector details, skip if this looks malformed
|
|
|
|
uint8_t checksum = (uint8_t)get_next_byte();
|
|
|
|
sector->sector = (uint8_t)get_next_byte();
|
|
|
|
sector->track = (uint8_t)get_next_byte();
|
|
|
|
uint8_t disk_id[2];
|
|
|
|
disk_id[0] = (uint8_t)get_next_byte();
|
|
|
|
disk_id[1] = (uint8_t)get_next_byte();
|
|
|
|
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) {
|
2016-09-14 01:53:36 +00:00
|
|
|
if(proceed_to_next_block() == 0x07) break;
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
for(size_t c = 0; c < 256; c++) {
|
2016-09-14 01:53:36 +00:00
|
|
|
sector->data[c] = (uint8_t)get_next_byte();
|
|
|
|
checksum ^= sector->data[c];
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(checksum == get_next_byte()) {
|
2016-09-18 02:01:54 +00:00
|
|
|
uint16_t sector_address = (uint16_t)((sector->track << 8) | sector->sector);
|
|
|
|
sector_cache_[sector_address] = sector;
|
|
|
|
return sector;
|
|
|
|
}
|
2016-09-14 01:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::list<File> StaticAnalyser::Commodore::GetFiles(const std::shared_ptr<Storage::Disk::Disk> &disk) {
|
2016-09-14 01:53:36 +00:00
|
|
|
std::list<File> files;
|
|
|
|
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
|
|
|
|
size_t header_pointer = (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-03-26 18:34:47 +00:00
|
|
|
for(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
|
|
|
|
|
|
|
size_t number_of_sectors = (size_t)directory[header_pointer + 0x1e] + ((size_t)directory[header_pointer + 0x1f] << 8);
|
|
|
|
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];
|
|
|
|
|
2016-09-15 23:21:09 +00:00
|
|
|
if(is_first_sector) new_file.starting_address = (uint16_t)sector->data[2] | (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
|
|
|
}
|