diff --git a/StaticAnalyser/Acorn/Disk.cpp b/StaticAnalyser/Acorn/Disk.cpp index 26942cfe0..7e5c6f663 100644 --- a/StaticAnalyser/Acorn/Disk.cpp +++ b/StaticAnalyser/Acorn/Disk.cpp @@ -19,8 +19,8 @@ std::unique_ptr StaticAnalyser::Acorn::GetDFSCatalogue(const std::sha std::unique_ptr catalogue(new Catalogue); Storage::Encodings::MFM::Parser parser(false, disk); - std::shared_ptr names = parser.get_sector(0, 0, 0); - std::shared_ptr details = parser.get_sector(0, 0, 1); + Storage::Encodings::MFM::Sector *names = parser.get_sector(0, 0, 0); + Storage::Encodings::MFM::Sector *details = parser.get_sector(0, 0, 1); if(!names || !details) return nullptr; if(names->data.size() != 256 || details->data.size() != 256) return nullptr; @@ -61,7 +61,7 @@ std::unique_ptr StaticAnalyser::Acorn::GetDFSCatalogue(const std::sha uint8_t track = (uint8_t)(start_sector / 10); start_sector++; - std::shared_ptr next_sector = parser.get_sector(0, track, sector); + Storage::Encodings::MFM::Sector *next_sector = parser.get_sector(0, track, sector); if(!next_sector) break; long length_from_sector = std::min(data_length, 256l); @@ -77,13 +77,13 @@ std::unique_ptr StaticAnalyser::Acorn::GetADFSCatalogue(const std::sh std::unique_ptr catalogue(new Catalogue); Storage::Encodings::MFM::Parser parser(true, disk); - std::shared_ptr free_space_map_second_half = parser.get_sector(0, 0, 1); + Storage::Encodings::MFM::Sector *free_space_map_second_half = parser.get_sector(0, 0, 1); if(!free_space_map_second_half) return nullptr; std::vector root_directory; root_directory.reserve(5 * 256); for(uint8_t c = 2; c < 7; c++) { - std::shared_ptr sector = parser.get_sector(0, 0, c); + Storage::Encodings::MFM::Sector *sector = parser.get_sector(0, 0, c); if(!sector) return nullptr; root_directory.insert(root_directory.end(), sector->data.begin(), sector->data.end()); } diff --git a/StaticAnalyser/AmstradCPC/StaticAnalyser.cpp b/StaticAnalyser/AmstradCPC/StaticAnalyser.cpp index 2926ec9a6..70d066e9b 100644 --- a/StaticAnalyser/AmstradCPC/StaticAnalyser.cpp +++ b/StaticAnalyser/AmstradCPC/StaticAnalyser.cpp @@ -152,7 +152,7 @@ static void InspectCatalogue( static bool CheckBootSector(const std::shared_ptr &disk, StaticAnalyser::Target &target) { Storage::Encodings::MFM::Parser parser(true, disk); - std::shared_ptr boot_sector = parser.get_sector(0, 0, 0x41); + Storage::Encodings::MFM::Sector *boot_sector = parser.get_sector(0, 0, 0x41); if(boot_sector != nullptr) { // Check that the first 64 bytes of the sector aren't identical; if they are then probably // this disk was formatted and the filler byte never replaced. diff --git a/Storage/Disk/DiskImage/Formats/SSD.cpp b/Storage/Disk/DiskImage/Formats/SSD.cpp index 0a32dd77b..f09e2f742 100644 --- a/Storage/Disk/DiskImage/Formats/SSD.cpp +++ b/Storage/Disk/DiskImage/Formats/SSD.cpp @@ -80,10 +80,10 @@ std::shared_ptr SSD::get_track_at_position(unsigned int head, unsigned in } void SSD::set_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr &track) { - std::vector data; +/* std::vector data; Storage::Encodings::MFM::Parser parser(false, track); for(unsigned int c = 0; c < 10; c++) { - std::shared_ptr sector = parser.get_sector(0, (uint8_t)position, (uint8_t)c); + Storage::Encodings::MFM::Sector *sector = parser.get_sector(0, (uint8_t)position, (uint8_t)c); if(sector) { data.insert(data.end(), sector->data.begin(), sector->data.end()); } else { @@ -98,5 +98,5 @@ void SSD::set_track_at_position(unsigned int head, unsigned int position, const std::lock_guard lock_guard(file_access_mutex_); ensure_file_is_at_least_length(file_offset); fseek(file_, file_offset, SEEK_SET); - fwrite(data.data(), 1, data.size(), file_); + fwrite(data.data(), 1, data.size(), file_);*/ } diff --git a/Storage/Disk/Encodings/MFM/Parser.cpp b/Storage/Disk/Encodings/MFM/Parser.cpp index 74195c3fb..2ac296ee9 100644 --- a/Storage/Disk/Encodings/MFM/Parser.cpp +++ b/Storage/Disk/Encodings/MFM/Parser.cpp @@ -9,218 +9,50 @@ #include "Parser.hpp" #include "Constants.hpp" -#include "../../DiskImage/DiskImage.hpp" -#include "../../SingleTrackDisk/SingleTrackDisk.hpp" +#include "../../Track/TrackSerialiser.hpp" +#include "SegmentParser.hpp" using namespace Storage::Encodings::MFM; -Parser::Parser(bool is_mfm) : - Storage::Disk::Controller(4000000), - crc_generator_(0x1021, 0xffff), - shift_register_(0), is_mfm_(is_mfm), - track_(0), head_(0) { - Storage::Time bit_length; - bit_length.length = 1; - bit_length.clock_rate = is_mfm ? 500000 : 250000; // i.e. 250 kbps (including clocks) - set_expected_bit_length(bit_length); - - drive_.reset(new Storage::Disk::Drive(4000000, 300, 2)); - set_drive(drive_); - drive_->set_motor_on(true); -} - Parser::Parser(bool is_mfm, const std::shared_ptr &disk) : - Parser(is_mfm) { - drive_->set_disk(disk); -} + is_mfm_(is_mfm), disk_(disk) {} -Parser::Parser(bool is_mfm, const std::shared_ptr &track) : - Parser(is_mfm) { - drive_->set_disk(std::make_shared>(track)); -} - -void Parser::seek_to_track(uint8_t track) { - int difference = (int)track - (int)track_; - track_ = track; - - if(difference) { - int direction = difference < 0 ? -1 : 1; - difference *= direction; - - for(int c = 0; c < difference; c++) drive_->step(direction); - } -} - -std::shared_ptr Parser::get_sector(uint8_t head, uint8_t track, uint8_t sector) { - // Switch head and track if necessary. - if(head_ != head) { - drive_->set_head(head); - } - seek_to_track(track); - int track_index = get_index(head, track, 0); - - // Populate the sector cache if it's not already populated by asking for sectors unless and until - // one is returned that has already been seen. - if(decoded_tracks_.find(track_index) == decoded_tracks_.end()) { - std::shared_ptr first_sector = get_next_sector(); - std::set visited_sectors; - if(first_sector) { - while(1) { - std::shared_ptr next_sector = get_next_sector(); - if(next_sector) { - if(visited_sectors.find(next_sector->address.sector) != visited_sectors.end()) { - break; - } - visited_sectors.insert(next_sector->address.sector); - } - } - } - decoded_tracks_.insert(track_index); +void Parser::install_sectors_from_track(const Storage::Disk::Track::Address &address) { + if(sectors_by_address_by_track_.find(address) != sectors_by_address_by_track_.end()) { + return; } - // Check cache for sector. - int index = get_index(head, track, sector); - auto cached_sector = sectors_by_index_.find(index); - if(cached_sector != sectors_by_index_.end()) { - return cached_sector->second; + std::shared_ptr track = disk_->get_track_at_position((unsigned int)address.head, (unsigned int)address.position); + if(!track) { + return; } - // If it wasn't found, it doesn't exist. - return nullptr; + std::map sectors = sectors_from_segment( + Storage::Disk::track_serialisation(*track, is_mfm_ ? MFMBitLength : FMBitLength), + is_mfm_); + + std::map sectors_by_id; + for(auto §or : sectors) { + sectors_by_id.insert(std::make_pair(sector.second.address.sector, std::move(sector.second))); + } + sectors_by_address_by_track_.insert(std::make_pair(address, std::move(sectors_by_id))); } -void Parser::process_input_bit(int value) { - shift_register_ = ((shift_register_ << 1) | (unsigned int)value) & 0xffff; - bit_count_++; -} +Sector *Parser::get_sector(int head, int track, uint8_t sector) { + Disk::Track::Address address; + address.position = track; + address.head = head; + install_sectors_from_track(address); -void Parser::process_index_hole() { - index_count_++; -} - -uint8_t Parser::get_byte_for_shift_value(uint16_t value) { - return (uint8_t)( - ((value&0x0001) >> 0) | - ((value&0x0004) >> 1) | - ((value&0x0010) >> 2) | - ((value&0x0040) >> 3) | - ((value&0x0100) >> 4) | - ((value&0x0400) >> 5) | - ((value&0x1000) >> 6) | - ((value&0x4000) >> 7)); -} - -uint8_t Parser::get_next_byte() { - bit_count_ = 0; - // Archetypal MFM is 500,000 bps given that the drive has an RPM of 300. Clock rate was - // specified at 4,000,000. So that's an idealised 8 cycles per bit, Jump ahead 14 - // times that... - run_for(Cycles(14 * 8)); - - // ... and proceed at half-idealised-bit intervals to get the next bit. Then proceed very gingerly indeed. - while(bit_count_ < 15) run_for(Cycles(4)); - while(bit_count_ < 16) run_for(Cycles(2)); - - uint8_t byte = get_byte_for_shift_value((uint16_t)shift_register_); - crc_generator_.add(byte); - return byte; -} - -std::shared_ptr Parser::get_next_sector() { - std::shared_ptr sector(new Sector); - index_count_ = 0; - - while(index_count_ < 2) { - // look for an ID address mark - bool id_found = false; - while(!id_found) { - run_for(Cycles(1)); - if(is_mfm_) { - while(shift_register_ == MFMSync) { - uint8_t mark = get_next_byte(); - if(mark == IDAddressByte) { - crc_generator_.set_value(MFMPostSyncCRCValue); - id_found = true; - break; - } - } - } else { - if(shift_register_ == FMIDAddressMark) { - crc_generator_.reset(); - id_found = true; - } - } - if(index_count_ >= 2) return nullptr; - } - - crc_generator_.add(IDAddressByte); - sector->address.track = get_next_byte(); - sector->address.side = get_next_byte(); - sector->address.sector = get_next_byte(); - sector->size = get_next_byte(); - uint16_t header_crc = crc_generator_.get_value(); - if((header_crc >> 8) != get_next_byte()) sector->has_header_crc_error = true; - if((header_crc & 0xff) != get_next_byte()) sector->has_header_crc_error = true; - - // look for data mark - bool data_found = false; - while(!data_found) { - run_for(Cycles(1)); - if(is_mfm_) { - while(shift_register_ == MFMSync) { - uint8_t mark = get_next_byte(); - if(mark == DataAddressByte) { - crc_generator_.set_value(MFMPostSyncCRCValue); - data_found = true; - break; - } - if(mark == IDAddressByte) return nullptr; - } - } else { - if(shift_register_ == FMDataAddressMark) { - crc_generator_.reset(); - data_found = true; - } - if(shift_register_ == FMIDAddressMark) return nullptr; - } - if(index_count_ >= 2) return nullptr; - } - crc_generator_.add(DataAddressByte); - - size_t data_size = (size_t)(128 << sector->size); - sector->data.reserve(data_size); - for(size_t c = 0; c < data_size; c++) { - sector->data.push_back(get_next_byte()); - } - uint16_t data_crc = crc_generator_.get_value(); - if((data_crc >> 8) != get_next_byte()) sector->has_data_crc_error = true; - if((data_crc & 0xff) != get_next_byte()) sector->has_data_crc_error = true; - - // Put this sector into the cache. - int index = get_index(head_, track_, sector->address.sector); - sectors_by_index_[index] = sector; - - return sector; + auto sectors = sectors_by_address_by_track_.find(address); + if(sectors == sectors_by_address_by_track_.end()) { + return nullptr; } - return nullptr; -} - -std::shared_ptr Parser::get_sector(uint8_t sector) { - std::shared_ptr first_sector; - index_count_ = 0; - while(!first_sector && index_count_ < 2) first_sector = get_next_sector(); - if(!first_sector) return nullptr; - if(first_sector->address.sector == sector) return first_sector; - - while(1) { - std::shared_ptr next_sector = get_next_sector(); - if(!next_sector) continue; - if(next_sector->address.sector == first_sector->address.sector) return nullptr; - if(next_sector->address.sector == sector) return next_sector; + auto stored_sector = sectors->second.find(sector); + if(stored_sector == sectors->second.end()) { + return nullptr; } -} -int Parser::get_index(uint8_t head, uint8_t track, uint8_t sector) { - return head | (track << 8) | (sector << 16); + return &stored_sector->second; } diff --git a/Storage/Disk/Encodings/MFM/Parser.hpp b/Storage/Disk/Encodings/MFM/Parser.hpp index a34648c4b..a749ab407 100644 --- a/Storage/Disk/Encodings/MFM/Parser.hpp +++ b/Storage/Disk/Encodings/MFM/Parser.hpp @@ -10,49 +10,34 @@ #define Parser_hpp #include "Sector.hpp" -#include "../../Controller/DiskController.hpp" -#include "../../../../NumberTheory/CRC.hpp" +#include "../../Track/Track.hpp" +#include "../../Drive.hpp" namespace Storage { namespace Encodings { namespace MFM { -class Parser: public Storage::Disk::Controller { +/*! + Provides a mechanism for collecting sectors from a disk. +*/ +class Parser { public: Parser(bool is_mfm, const std::shared_ptr &disk); - Parser(bool is_mfm, const std::shared_ptr &track); /*! - Attempts to read the sector located at @c track and @c sector. + Seeks to the physical track at @c head and @c track. Searches on it for a sector + with logical address @c sector. @returns a sector if one was found; @c nullptr otherwise. */ - std::shared_ptr get_sector(uint8_t head, uint8_t track, uint8_t sector); + Storage::Encodings::MFM::Sector *get_sector(int head, int track, uint8_t sector); private: - Parser(bool is_mfm); + std::shared_ptr disk_; + bool is_mfm_ = true; - std::shared_ptr drive_; - unsigned int shift_register_; - int index_count_; - uint8_t track_, head_; - int bit_count_; - NumberTheory::CRC16 crc_generator_; - bool is_mfm_; - - void seek_to_track(uint8_t track); - void process_input_bit(int value); - void process_index_hole(); - uint8_t get_next_byte(); - - uint8_t get_byte_for_shift_value(uint16_t value); - - std::shared_ptr get_next_sector(); - std::shared_ptr get_sector(uint8_t sector); - - std::map> sectors_by_index_; - std::set decoded_tracks_; - int get_index(uint8_t head, uint8_t track, uint8_t sector); + void install_sectors_from_track(const Storage::Disk::Track::Address &address); + std::map> sectors_by_address_by_track_; }; } diff --git a/Storage/Disk/Parsers/CPM.cpp b/Storage/Disk/Parsers/CPM.cpp index ceaffb8a8..b75d55e28 100644 --- a/Storage/Disk/Parsers/CPM.cpp +++ b/Storage/Disk/Parsers/CPM.cpp @@ -26,7 +26,7 @@ std::unique_ptr Storage::Disk::CPM::GetCatalogue( if(catalogue_allocation_bitmap & 0x8000) { size_t size_read = 0; do { - std::shared_ptr sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector)); + Storage::Encodings::MFM::Sector *sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector)); if(!sector_contents) { return nullptr; } @@ -105,7 +105,7 @@ std::unique_ptr Storage::Disk::CPM::GetCatalogue( track = first_sector / parameters.sectors_per_track; for(int s = 0; s < sectors_per_block && record < number_of_records; s++) { - std::shared_ptr sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector)); + Storage::Encodings::MFM::Sector *sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector)); if(!sector_contents) break; sector++; if(sector == parameters.sectors_per_track) { diff --git a/Storage/Disk/Track/Track.hpp b/Storage/Disk/Track/Track.hpp index 9e6b15071..54d0194f7 100644 --- a/Storage/Disk/Track/Track.hpp +++ b/Storage/Disk/Track/Track.hpp @@ -31,6 +31,12 @@ class Track { bool operator < (const Address &rhs) const { return (head < rhs.head) || (position < rhs.position); } +// Address(const Address &rhs) noexcept : head(rhs.head), position(rhs.position) {} +// const Address &operator =(const Address &rhs) { +// head = rhs.head; +// position = rhs.position; +// return *this; +// } }; /*!