1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-29 16:29:08 +00:00

Threw a sector cache into my MFM parser, in an attempt to cut down analysis costs. Also made it aware of multiple heads.

This commit is contained in:
Thomas Harte 2017-08-11 10:29:13 -04:00
parent 6be5851484
commit 734099a956
6 changed files with 45 additions and 21 deletions

View File

@ -19,8 +19,8 @@ std::unique_ptr<Catalogue> StaticAnalyser::Acorn::GetDFSCatalogue(const std::sha
std::unique_ptr<Catalogue> catalogue(new Catalogue); std::unique_ptr<Catalogue> catalogue(new Catalogue);
Storage::Encodings::MFM::Parser parser(false, disk); Storage::Encodings::MFM::Parser parser(false, disk);
std::shared_ptr<Storage::Encodings::MFM::Sector> names = parser.get_sector(0, 0); std::shared_ptr<Storage::Encodings::MFM::Sector> names = parser.get_sector(0, 0, 0);
std::shared_ptr<Storage::Encodings::MFM::Sector> details = parser.get_sector(0, 1); std::shared_ptr<Storage::Encodings::MFM::Sector> details = parser.get_sector(0, 0, 1);
if(!names || !details) return nullptr; if(!names || !details) return nullptr;
if(names->data.size() != 256 || details->data.size() != 256) return nullptr; if(names->data.size() != 256 || details->data.size() != 256) return nullptr;
@ -61,7 +61,7 @@ std::unique_ptr<Catalogue> StaticAnalyser::Acorn::GetDFSCatalogue(const std::sha
uint8_t track = (uint8_t)(start_sector / 10); uint8_t track = (uint8_t)(start_sector / 10);
start_sector++; start_sector++;
std::shared_ptr<Storage::Encodings::MFM::Sector> next_sector = parser.get_sector(track, sector); std::shared_ptr<Storage::Encodings::MFM::Sector> next_sector = parser.get_sector(0, track, sector);
if(!next_sector) break; if(!next_sector) break;
long length_from_sector = std::min(data_length, 256l); long length_from_sector = std::min(data_length, 256l);
@ -77,13 +77,13 @@ std::unique_ptr<Catalogue> StaticAnalyser::Acorn::GetADFSCatalogue(const std::sh
std::unique_ptr<Catalogue> catalogue(new Catalogue); std::unique_ptr<Catalogue> catalogue(new Catalogue);
Storage::Encodings::MFM::Parser parser(true, disk); Storage::Encodings::MFM::Parser parser(true, disk);
std::shared_ptr<Storage::Encodings::MFM::Sector> free_space_map_second_half = parser.get_sector(0, 1); std::shared_ptr<Storage::Encodings::MFM::Sector> free_space_map_second_half = parser.get_sector(0, 0, 1);
if(!free_space_map_second_half) return nullptr; if(!free_space_map_second_half) return nullptr;
std::vector<uint8_t> root_directory; std::vector<uint8_t> root_directory;
root_directory.reserve(5 * 256); root_directory.reserve(5 * 256);
for(uint8_t c = 2; c < 7; c++) { for(uint8_t c = 2; c < 7; c++) {
std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector(0, c); std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector(0, 0, c);
if(!sector) return nullptr; if(!sector) return nullptr;
root_directory.insert(root_directory.end(), sector->data.begin(), sector->data.end()); root_directory.insert(root_directory.end(), sector->data.begin(), sector->data.end());
} }

View File

@ -228,25 +228,26 @@ std::unique_ptr<Encoder> Storage::Encodings::MFM::GetFMEncoder(std::vector<uint8
Parser::Parser(bool is_mfm) : Parser::Parser(bool is_mfm) :
Storage::Disk::Controller(4000000, 1, 300), Storage::Disk::Controller(4000000, 1, 300),
crc_generator_(0x1021, 0xffff), crc_generator_(0x1021, 0xffff),
shift_register_(0), track_(0), is_mfm_(is_mfm) { shift_register_(0), is_mfm_(is_mfm),
track_(0), head_(0) {
Storage::Time bit_length; Storage::Time bit_length;
bit_length.length = 1; bit_length.length = 1;
bit_length.clock_rate = is_mfm ? 500000 : 250000; // i.e. 250 kbps (including clocks) bit_length.clock_rate = is_mfm ? 500000 : 250000; // i.e. 250 kbps (including clocks)
set_expected_bit_length(bit_length); set_expected_bit_length(bit_length);
drive.reset(new Storage::Disk::Drive); drive_.reset(new Storage::Disk::Drive);
set_drive(drive); set_drive(drive_);
set_motor_on(true); set_motor_on(true);
} }
Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Disk> &disk) : Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Disk> &disk) :
Parser(is_mfm) { Parser(is_mfm) {
drive->set_disk(disk); drive_->set_disk(disk);
} }
Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Track> &track) : Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Track> &track) :
Parser(is_mfm) { Parser(is_mfm) {
drive->set_disk_with_track(track); drive_->set_disk_with_track(track);
} }
void Parser::seek_to_track(uint8_t track) { void Parser::seek_to_track(uint8_t track) {
@ -261,7 +262,20 @@ void Parser::seek_to_track(uint8_t track) {
} }
} }
std::shared_ptr<Sector> Parser::get_sector(uint8_t track, uint8_t sector) { std::shared_ptr<Sector> Parser::get_sector(uint8_t head, uint8_t track, uint8_t sector) {
// 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;
}
// Failing that, set the proper head and track, and search for the sector. get_sector automatically
// inserts everything found into sectors_by_index_.
if(head_ != head) {
drive_->set_head(head);
invalidate_track();
}
seek_to_track(track); seek_to_track(track);
return get_sector(sector); return get_sector(sector);
} }
@ -384,8 +398,7 @@ std::vector<uint8_t> Parser::get_track() {
} }
std::shared_ptr<Sector> Parser::get_next_sector() std::shared_ptr<Sector> Parser::get_next_sector() {
{
std::shared_ptr<Sector> sector(new Sector); std::shared_ptr<Sector> sector(new Sector);
index_count_ = 0; index_count_ = 0;
@ -455,6 +468,10 @@ std::shared_ptr<Sector> Parser::get_next_sector()
if((data_crc >> 8) != get_next_byte()) continue; if((data_crc >> 8) != get_next_byte()) continue;
if((data_crc & 0xff) != get_next_byte()) continue; if((data_crc & 0xff) != get_next_byte()) continue;
// Put this sector into the cache.
int index = get_index(head_, track_, sector->sector);
sectors_by_index_[index] = sector;
return sector; return sector;
} }
@ -465,7 +482,7 @@ std::shared_ptr<Sector> Parser::get_sector(uint8_t sector) {
std::shared_ptr<Sector> first_sector; std::shared_ptr<Sector> first_sector;
index_count_ = 0; index_count_ = 0;
while(!first_sector && index_count_ < 2) first_sector = get_next_sector(); while(!first_sector && index_count_ < 2) first_sector = get_next_sector();
if(!first_sector) return first_sector; if(!first_sector) return nullptr;
if(first_sector->sector == sector) return first_sector; if(first_sector->sector == sector) return first_sector;
while(1) { while(1) {
@ -475,3 +492,7 @@ std::shared_ptr<Sector> Parser::get_sector(uint8_t sector) {
if(next_sector->sector == sector) return next_sector; if(next_sector->sector == sector) return next_sector;
} }
} }
int Parser::get_index(uint8_t head, uint8_t track, uint8_t sector) {
return head | (track << 8) | (sector << 16);
}

View File

@ -75,7 +75,7 @@ class Parser: public Storage::Disk::Controller {
@returns a sector if one was found; @c nullptr otherwise. @returns a sector if one was found; @c nullptr otherwise.
*/ */
std::shared_ptr<Storage::Encodings::MFM::Sector> get_sector(uint8_t track, uint8_t sector); std::shared_ptr<Storage::Encodings::MFM::Sector> get_sector(uint8_t head, uint8_t track, uint8_t sector);
/*! /*!
Attempts to read the track at @c track, starting from the index hole. Attempts to read the track at @c track, starting from the index hole.
@ -92,10 +92,10 @@ class Parser: public Storage::Disk::Controller {
private: private:
Parser(bool is_mfm); Parser(bool is_mfm);
std::shared_ptr<Storage::Disk::Drive> drive; std::shared_ptr<Storage::Disk::Drive> drive_;
unsigned int shift_register_; unsigned int shift_register_;
int index_count_; int index_count_;
uint8_t track_; uint8_t track_, head_;
int bit_count_; int bit_count_;
NumberTheory::CRC16 crc_generator_; NumberTheory::CRC16 crc_generator_;
bool is_mfm_; bool is_mfm_;
@ -110,6 +110,9 @@ class Parser: public Storage::Disk::Controller {
std::shared_ptr<Storage::Encodings::MFM::Sector> get_next_sector(); std::shared_ptr<Storage::Encodings::MFM::Sector> get_next_sector();
std::shared_ptr<Storage::Encodings::MFM::Sector> get_sector(uint8_t sector); std::shared_ptr<Storage::Encodings::MFM::Sector> get_sector(uint8_t sector);
std::vector<uint8_t> get_track(); std::vector<uint8_t> get_track();
std::map<int, std::shared_ptr<Storage::Encodings::MFM::Sector>> sectors_by_index_;
int get_index(uint8_t head, uint8_t track, uint8_t sector);
}; };

View File

@ -87,7 +87,7 @@ void AcornADF::store_updated_track_at_position(unsigned int head, unsigned int p
std::vector<uint8_t> parsed_track; std::vector<uint8_t> parsed_track;
Storage::Encodings::MFM::Parser parser(true, track); Storage::Encodings::MFM::Parser parser(true, track);
for(unsigned int c = 0; c < sectors_per_track; c++) { for(unsigned int c = 0; c < sectors_per_track; c++) {
std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector((uint8_t)position, (uint8_t)c); std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector(0, (uint8_t)position, (uint8_t)c);
if(sector) { if(sector) {
parsed_track.insert(parsed_track.end(), sector->data.begin(), sector->data.end()); parsed_track.insert(parsed_track.end(), sector->data.begin(), sector->data.end());
} else { } else {

View File

@ -81,7 +81,7 @@ void SSD::store_updated_track_at_position(unsigned int head, unsigned int positi
std::vector<uint8_t> parsed_track; std::vector<uint8_t> parsed_track;
Storage::Encodings::MFM::Parser parser(false, track); Storage::Encodings::MFM::Parser parser(false, track);
for(unsigned int c = 0; c < 10; c++) { for(unsigned int c = 0; c < 10; c++) {
std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector((uint8_t)position, (uint8_t)c); std::shared_ptr<Storage::Encodings::MFM::Sector> sector = parser.get_sector(0, (uint8_t)position, (uint8_t)c);
if(sector) { if(sector) {
parsed_track.insert(parsed_track.end(), sector->data.begin(), sector->data.end()); parsed_track.insert(parsed_track.end(), sector->data.begin(), sector->data.end());
} else { } else {

View File

@ -24,7 +24,7 @@ std::unique_ptr<Storage::Disk::CPM::Catalogue> Storage::Disk::CPM::GetCatalogue(
int track = parameters.reserved_tracks; int track = parameters.reserved_tracks;
while(catalogue_allocation_bitmap) { while(catalogue_allocation_bitmap) {
if(catalogue_allocation_bitmap & 0x8000) { if(catalogue_allocation_bitmap & 0x8000) {
std::shared_ptr<Storage::Encodings::MFM::Sector> sector_contents = parser.get_sector((uint8_t)track, (uint8_t)(parameters.first_sector + sector)); std::shared_ptr<Storage::Encodings::MFM::Sector> sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector));
if(!sector_contents) { if(!sector_contents) {
return nullptr; return nullptr;
} }
@ -91,7 +91,7 @@ std::unique_ptr<Storage::Disk::CPM::Catalogue> Storage::Disk::CPM::GetCatalogue(
track = first_sector / parameters.sectors_per_track; track = first_sector / parameters.sectors_per_track;
for(int s = 0; s < sectors_per_block && record < number_of_records; s++) { for(int s = 0; s < sectors_per_block && record < number_of_records; s++) {
std::shared_ptr<Storage::Encodings::MFM::Sector> sector_contents = parser.get_sector((uint8_t)track, (uint8_t)(parameters.first_sector + sector)); std::shared_ptr<Storage::Encodings::MFM::Sector> sector_contents = parser.get_sector(0, (uint8_t)track, (uint8_t)(parameters.first_sector + sector));
if(!sector_contents) break; if(!sector_contents) break;
sector++; sector++;
if(sector == parameters.sectors_per_track) { if(sector == parameters.sectors_per_track) {