2017-09-25 02:41:16 +00:00
|
|
|
//
|
|
|
|
// SegmentParser.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 24/09/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SegmentParser.hpp"
|
|
|
|
#include "Shifter.hpp"
|
|
|
|
|
|
|
|
using namespace Storage::Encodings::MFM;
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
std::map<std::size_t, Storage::Encodings::MFM::Sector> Storage::Encodings::MFM::sectors_from_segment(const Storage::Disk::PCMSegment &&segment, bool is_double_density) {
|
|
|
|
std::map<std::size_t, Sector> result;
|
2017-09-25 02:41:16 +00:00
|
|
|
Shifter shifter;
|
2017-09-25 23:57:11 +00:00
|
|
|
shifter.set_is_double_density(is_double_density);
|
|
|
|
shifter.set_should_obey_syncs(true);
|
2017-09-25 02:41:16 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Storage::Encodings::MFM::Sector> new_sector;
|
|
|
|
bool is_reading = false;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t position = 0;
|
|
|
|
std::size_t size = 0;
|
|
|
|
std::size_t start_location = 0;
|
2017-09-25 02:41:16 +00:00
|
|
|
|
|
|
|
for(unsigned int bit = 0; bit < segment.number_of_bits; ++bit) {
|
|
|
|
shifter.add_input_bit(segment.bit(bit));
|
|
|
|
switch(shifter.get_token()) {
|
|
|
|
case Shifter::Token::None:
|
|
|
|
case Shifter::Token::Sync:
|
|
|
|
case Shifter::Token::Index:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Shifter::Token::ID:
|
|
|
|
new_sector.reset(new Storage::Encodings::MFM::Sector);
|
|
|
|
is_reading = true;
|
2017-09-25 23:57:11 +00:00
|
|
|
start_location = bit;
|
2017-09-25 02:41:16 +00:00
|
|
|
position = 0;
|
2017-09-25 23:57:11 +00:00
|
|
|
shifter.set_should_obey_syncs(false);
|
2017-09-25 02:41:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Shifter::Token::Data:
|
|
|
|
case Shifter::Token::DeletedData:
|
|
|
|
if(new_sector) {
|
|
|
|
is_reading = true;
|
2017-09-25 23:57:11 +00:00
|
|
|
shifter.set_should_obey_syncs(false);
|
2017-09-25 02:41:16 +00:00
|
|
|
new_sector->is_deleted = (shifter.get_token() == Shifter::Token::DeletedData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Shifter::Token::Byte:
|
|
|
|
if(is_reading) {
|
|
|
|
switch(position) {
|
|
|
|
case 0: new_sector->address.track = shifter.get_byte(); ++position; break;
|
|
|
|
case 1: new_sector->address.side = shifter.get_byte(); ++position; break;
|
|
|
|
case 2: new_sector->address.sector = shifter.get_byte(); ++position; break;
|
|
|
|
case 3:
|
|
|
|
new_sector->size = shifter.get_byte();
|
2017-11-11 20:28:40 +00:00
|
|
|
size = static_cast<std::size_t>(128 << new_sector->size);
|
2017-09-25 02:41:16 +00:00
|
|
|
++position;
|
|
|
|
is_reading = false;
|
2017-09-25 23:57:11 +00:00
|
|
|
shifter.set_should_obey_syncs(true);
|
2017-09-25 02:41:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-11-01 01:32:28 +00:00
|
|
|
if(new_sector->samples.empty()) new_sector->samples.emplace_back();
|
|
|
|
new_sector->samples[0].push_back(shifter.get_byte());
|
2017-09-25 02:41:16 +00:00
|
|
|
++position;
|
|
|
|
if(position == size + 4) {
|
2017-09-25 23:57:11 +00:00
|
|
|
result.insert(std::make_pair(start_location, std::move(*new_sector)));
|
2017-09-25 02:41:16 +00:00
|
|
|
is_reading = false;
|
2017-09-25 23:57:11 +00:00
|
|
|
shifter.set_should_obey_syncs(true);
|
2017-09-25 02:41:16 +00:00
|
|
|
new_sector.reset();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|