2016-09-18 17:35:54 +00:00
|
|
|
//
|
|
|
|
// MFM.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/09/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MFM.hpp"
|
|
|
|
|
2016-09-19 12:16:06 +00:00
|
|
|
#include "../PCMTrack.hpp"
|
|
|
|
#include "../../../NumberTheory/CRC.hpp"
|
2016-09-18 20:53:21 +00:00
|
|
|
|
|
|
|
using namespace Storage::Encodings::MFM;
|
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
class MFMEncoder: public Encoder {
|
2016-09-18 21:16:20 +00:00
|
|
|
public:
|
2016-11-26 05:39:20 +00:00
|
|
|
MFMEncoder(std::vector<uint8_t> &target) : Encoder(target) {}
|
2016-09-18 21:16:20 +00:00
|
|
|
|
|
|
|
void add_byte(uint8_t input) {
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.add(input);
|
2016-09-18 21:16:20 +00:00
|
|
|
uint16_t spread_value =
|
|
|
|
(uint16_t)(
|
|
|
|
((input & 0x01) << 0) |
|
|
|
|
((input & 0x02) << 1) |
|
|
|
|
((input & 0x04) << 2) |
|
|
|
|
((input & 0x08) << 3) |
|
|
|
|
((input & 0x10) << 4) |
|
|
|
|
((input & 0x20) << 5) |
|
|
|
|
((input & 0x40) << 6) |
|
|
|
|
((input & 0x80) << 7)
|
|
|
|
);
|
2016-12-28 23:29:37 +00:00
|
|
|
uint16_t or_bits = (uint16_t)((spread_value << 1) | (spread_value >> 1) | (last_output_ << 15));
|
|
|
|
uint16_t output = spread_value | ((~or_bits) & 0xaaaa);
|
|
|
|
output_short(output);
|
2016-09-18 20:53:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:20 +00:00
|
|
|
void add_index_address_mark() {
|
2016-12-28 23:29:37 +00:00
|
|
|
for(int c = 0; c < 3; c++) output_short(MFMIndexSync);
|
2016-12-31 20:25:11 +00:00
|
|
|
add_byte(IndexAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_ID_address_mark() {
|
2016-12-28 23:29:37 +00:00
|
|
|
output_sync();
|
2016-12-31 20:25:11 +00:00
|
|
|
add_byte(IDAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_data_address_mark() {
|
2016-12-28 23:29:37 +00:00
|
|
|
output_sync();
|
2016-12-31 20:25:11 +00:00
|
|
|
add_byte(DataAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_deleted_data_address_mark() {
|
2016-12-28 23:29:37 +00:00
|
|
|
output_sync();
|
2016-12-31 20:25:11 +00:00
|
|
|
add_byte(DeletedDataAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-28 23:29:37 +00:00
|
|
|
uint16_t last_output_;
|
|
|
|
void output_short(uint16_t value) {
|
|
|
|
last_output_ = value;
|
|
|
|
Encoder::output_short(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_sync() {
|
|
|
|
for(int c = 0; c < 3; c++) output_short(MFMSync);
|
|
|
|
crc_generator_.set_value(MFMPostSyncCRCValue);
|
|
|
|
}
|
2016-09-18 21:16:20 +00:00
|
|
|
};
|
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
class FMEncoder: public Encoder {
|
2016-09-18 22:46:58 +00:00
|
|
|
// encodes each 16-bit part as clock, data, clock, data [...]
|
2016-09-18 21:16:20 +00:00
|
|
|
public:
|
2016-11-26 05:39:20 +00:00
|
|
|
FMEncoder(std::vector<uint8_t> &target) : Encoder(target) {}
|
|
|
|
|
2016-09-18 21:16:20 +00:00
|
|
|
void add_byte(uint8_t input) {
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.add(input);
|
2016-11-26 05:39:20 +00:00
|
|
|
output_short(
|
2016-09-18 21:16:20 +00:00
|
|
|
(uint16_t)(
|
2016-09-18 22:46:58 +00:00
|
|
|
((input & 0x01) << 0) |
|
|
|
|
((input & 0x02) << 1) |
|
|
|
|
((input & 0x04) << 2) |
|
|
|
|
((input & 0x08) << 3) |
|
|
|
|
((input & 0x10) << 4) |
|
|
|
|
((input & 0x20) << 5) |
|
|
|
|
((input & 0x40) << 6) |
|
|
|
|
((input & 0x80) << 7) |
|
|
|
|
0xaaaa
|
2016-09-18 21:16:20 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void add_index_address_mark() {
|
2016-12-28 23:56:53 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(IndexAddressByte);
|
2016-12-28 23:56:53 +00:00
|
|
|
output_short(FMIndexAddressMark);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void add_ID_address_mark() {
|
2016-12-28 23:56:53 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(IDAddressByte);
|
2016-12-28 23:56:53 +00:00
|
|
|
output_short(FMIDAddressMark);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void add_data_address_mark() {
|
2016-12-28 23:56:53 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(DataAddressByte);
|
2016-12-28 23:56:53 +00:00
|
|
|
output_short(FMDataAddressMark);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void add_deleted_data_address_mark() {
|
2016-12-28 23:56:53 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(DeletedDataAddressByte);
|
2016-12-28 23:56:53 +00:00
|
|
|
output_short(FMDeletedDataAddressMark);
|
|
|
|
}
|
2016-09-18 21:16:20 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
static uint8_t logarithmic_size_for_size(size_t size) {
|
|
|
|
switch(size) {
|
2016-09-18 22:33:26 +00:00
|
|
|
default: return 0;
|
|
|
|
case 256: return 1;
|
|
|
|
case 512: return 2;
|
|
|
|
case 1024: return 3;
|
2017-03-26 18:34:47 +00:00
|
|
|
case 2048: return 4;
|
2016-09-18 22:33:26 +00:00
|
|
|
case 4196: return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:20 +00:00
|
|
|
template<class T> std::shared_ptr<Storage::Disk::Track>
|
2017-03-26 18:34:47 +00:00
|
|
|
GetTrackWithSectors(
|
|
|
|
const std::vector<Sector> §ors,
|
|
|
|
size_t post_index_address_mark_bytes, uint8_t post_index_address_mark_value,
|
|
|
|
size_t pre_address_mark_bytes, size_t post_address_mark_bytes,
|
|
|
|
size_t pre_data_mark_bytes, size_t post_data_bytes,
|
|
|
|
size_t inter_sector_gap,
|
|
|
|
size_t expected_track_bytes) {
|
2016-11-26 05:39:20 +00:00
|
|
|
Storage::Disk::PCMSegment segment;
|
|
|
|
segment.data.reserve(expected_track_bytes);
|
|
|
|
T shifter(segment.data);
|
2016-09-18 20:53:21 +00:00
|
|
|
|
|
|
|
// output the index mark
|
|
|
|
shifter.add_index_address_mark();
|
|
|
|
|
|
|
|
// add the post-index mark
|
2016-09-18 21:16:20 +00:00
|
|
|
for(int c = 0; c < post_index_address_mark_bytes; c++) shifter.add_byte(post_index_address_mark_value);
|
2016-09-18 20:53:21 +00:00
|
|
|
|
|
|
|
// add sectors
|
2017-03-26 18:34:47 +00:00
|
|
|
for(const Sector §or : sectors) {
|
2016-09-18 22:33:26 +00:00
|
|
|
// gap
|
2016-09-18 21:16:20 +00:00
|
|
|
for(int c = 0; c < pre_address_mark_bytes; c++) shifter.add_byte(0x00);
|
2016-09-18 20:53:21 +00:00
|
|
|
|
2016-09-18 22:33:26 +00:00
|
|
|
// sector header
|
2016-09-18 20:53:21 +00:00
|
|
|
shifter.add_ID_address_mark();
|
|
|
|
shifter.add_byte(sector.track);
|
|
|
|
shifter.add_byte(sector.side);
|
|
|
|
shifter.add_byte(sector.sector);
|
2016-09-18 22:33:26 +00:00
|
|
|
uint8_t size = logarithmic_size_for_size(sector.data.size());
|
|
|
|
shifter.add_byte(size);
|
2016-12-28 23:29:37 +00:00
|
|
|
shifter.add_crc();
|
2016-09-18 22:33:26 +00:00
|
|
|
|
|
|
|
// gap
|
2016-09-18 21:16:20 +00:00
|
|
|
for(int c = 0; c < post_address_mark_bytes; c++) shifter.add_byte(0x4e);
|
|
|
|
for(int c = 0; c < pre_data_mark_bytes; c++) shifter.add_byte(0x00);
|
2016-09-18 20:53:21 +00:00
|
|
|
|
2016-09-18 22:33:26 +00:00
|
|
|
// data
|
2016-09-18 20:53:21 +00:00
|
|
|
shifter.add_data_address_mark();
|
2016-09-18 22:33:26 +00:00
|
|
|
for(size_t c = 0; c < sector.data.size(); c++)
|
|
|
|
{
|
|
|
|
shifter.add_byte(sector.data[c]);
|
|
|
|
}
|
2016-12-28 23:29:37 +00:00
|
|
|
shifter.add_crc();
|
2016-09-18 20:53:21 +00:00
|
|
|
|
2016-09-18 22:33:26 +00:00
|
|
|
// gap
|
2016-09-18 21:16:20 +00:00
|
|
|
for(int c = 0; c < post_data_bytes; c++) shifter.add_byte(0x00);
|
|
|
|
for(int c = 0; c < inter_sector_gap; c++) shifter.add_byte(0x4e);
|
2016-09-18 20:53:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
while(segment.data.size() < expected_track_bytes) shifter.add_byte(0x00);
|
2016-09-18 20:53:21 +00:00
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
segment.number_of_bits = (unsigned int)(segment.data.size() * 8);
|
|
|
|
return std::shared_ptr<Storage::Disk::Track>(new Storage::Disk::PCMTrack(std::move(segment)));
|
2016-09-18 20:53:21 +00:00
|
|
|
}
|
2016-09-18 21:16:20 +00:00
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
Encoder::Encoder(std::vector<uint8_t> &target) :
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_(0x1021, 0xffff),
|
2017-03-26 18:34:47 +00:00
|
|
|
target_(target) {}
|
2016-11-26 05:39:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Encoder::output_short(uint16_t value) {
|
2016-11-26 05:39:20 +00:00
|
|
|
target_.push_back(value >> 8);
|
|
|
|
target_.push_back(value & 0xff);
|
|
|
|
}
|
2016-09-18 21:16:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Encoder::add_crc() {
|
2016-12-29 00:48:46 +00:00
|
|
|
uint16_t crc_value = crc_generator_.get_value();
|
|
|
|
add_byte(crc_value >> 8);
|
|
|
|
add_byte(crc_value & 0xff);
|
2016-12-28 23:29:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Storage::Disk::Track> Storage::Encodings::MFM::GetFMTrackWithSectors(const std::vector<Sector> §ors) {
|
2016-11-26 05:39:20 +00:00
|
|
|
return GetTrackWithSectors<FMEncoder>(
|
2016-09-18 21:16:20 +00:00
|
|
|
sectors,
|
|
|
|
16, 0x00,
|
|
|
|
6, 0,
|
|
|
|
17, 14,
|
2016-09-18 22:56:35 +00:00
|
|
|
0,
|
2016-09-20 02:06:56 +00:00
|
|
|
6250); // i.e. 250kbps (including clocks) * 60 = 15000kpm, at 300 rpm => 50 kbits/rotation => 6250 bytes/rotation
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Storage::Disk::Track> Storage::Encodings::MFM::GetMFMTrackWithSectors(const std::vector<Sector> §ors) {
|
2016-11-26 05:39:20 +00:00
|
|
|
return GetTrackWithSectors<MFMEncoder>(
|
2016-09-18 21:16:20 +00:00
|
|
|
sectors,
|
|
|
|
50, 0x4e,
|
|
|
|
12, 22,
|
|
|
|
12, 18,
|
2016-09-18 22:56:35 +00:00
|
|
|
32,
|
2016-09-20 02:06:56 +00:00
|
|
|
12500); // unintelligently: double the single-density bytes/rotation (or: 500kps @ 300 rpm)
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
2016-11-26 05:39:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<Encoder> Storage::Encodings::MFM::GetMFMEncoder(std::vector<uint8_t> &target) {
|
2016-11-26 05:39:20 +00:00
|
|
|
return std::unique_ptr<Encoder>(new MFMEncoder(target));
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<Encoder> Storage::Encodings::MFM::GetFMEncoder(std::vector<uint8_t> &target) {
|
2016-11-26 05:39:20 +00:00
|
|
|
return std::unique_ptr<Encoder>(new FMEncoder(target));
|
|
|
|
}
|
2016-12-26 01:00:57 +00:00
|
|
|
|
|
|
|
#pragma mark - Parser
|
|
|
|
|
2016-12-26 19:24:33 +00:00
|
|
|
Parser::Parser(bool is_mfm) :
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Disk::Controller(4000000, 1, 300),
|
|
|
|
crc_generator_(0x1021, 0xffff),
|
|
|
|
shift_register_(0), track_(0), is_mfm_(is_mfm) {
|
2016-12-26 01:00:57 +00:00
|
|
|
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);
|
|
|
|
set_drive(drive);
|
|
|
|
set_motor_on(true);
|
|
|
|
}
|
|
|
|
|
2016-12-26 19:24:33 +00:00
|
|
|
Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Disk> &disk) :
|
2017-03-26 18:34:47 +00:00
|
|
|
Parser(is_mfm) {
|
2016-12-26 19:24:33 +00:00
|
|
|
drive->set_disk(disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Track> &track) :
|
2017-03-26 18:34:47 +00:00
|
|
|
Parser(is_mfm) {
|
2016-12-26 19:24:33 +00:00
|
|
|
drive->set_disk_with_track(track);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Parser::seek_to_track(uint8_t track) {
|
2016-12-26 01:00:57 +00:00
|
|
|
int difference = (int)track - (int)track_;
|
|
|
|
track_ = track;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(difference) {
|
2016-12-26 01:00:57 +00:00
|
|
|
int direction = difference < 0 ? -1 : 1;
|
|
|
|
difference *= direction;
|
|
|
|
|
|
|
|
for(int c = 0; c < difference; c++) step(direction);
|
|
|
|
}
|
2016-12-31 00:59:23 +00:00
|
|
|
}
|
2016-12-26 01:00:57 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Sector> Parser::get_sector(uint8_t track, uint8_t sector) {
|
2016-12-31 00:59:23 +00:00
|
|
|
seek_to_track(track);
|
2016-12-26 01:00:57 +00:00
|
|
|
return get_sector(sector);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::vector<uint8_t> Parser::get_track(uint8_t track) {
|
2016-12-31 00:59:23 +00:00
|
|
|
seek_to_track(track);
|
2016-12-31 04:10:52 +00:00
|
|
|
return get_track();
|
2016-12-31 00:59:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Parser::process_input_bit(int value, unsigned int cycles_since_index_hole) {
|
2016-12-26 01:00:57 +00:00
|
|
|
shift_register_ = ((shift_register_ << 1) | (unsigned int)value) & 0xffff;
|
|
|
|
bit_count_++;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Parser::process_index_hole() {
|
2016-12-26 01:00:57 +00:00
|
|
|
index_count_++;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t Parser::get_byte_for_shift_value(uint16_t value) {
|
2016-12-31 05:11:31 +00:00
|
|
|
return (uint8_t)(
|
|
|
|
((value&0x0001) >> 0) |
|
|
|
|
((value&0x0004) >> 1) |
|
|
|
|
((value&0x0010) >> 2) |
|
|
|
|
((value&0x0040) >> 3) |
|
|
|
|
((value&0x0100) >> 4) |
|
|
|
|
((value&0x0400) >> 5) |
|
|
|
|
((value&0x1000) >> 6) |
|
2017-03-26 18:34:47 +00:00
|
|
|
((value&0x4000) >> 7));
|
|
|
|
}
|
2016-12-31 05:11:31 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t Parser::get_next_byte() {
|
2016-12-26 01:00:57 +00:00
|
|
|
bit_count_ = 0;
|
|
|
|
while(bit_count_ < 16) run_for_cycles(1);
|
2016-12-31 05:11:31 +00:00
|
|
|
uint8_t byte = get_byte_for_shift_value((uint16_t)shift_register_);
|
2016-12-26 01:00:57 +00:00
|
|
|
crc_generator_.add(byte);
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::vector<uint8_t> Parser::get_track() {
|
2016-12-31 00:59:23 +00:00
|
|
|
std::vector<uint8_t> result;
|
2016-12-31 17:23:08 +00:00
|
|
|
int distance_until_permissible_sync = 0;
|
|
|
|
uint8_t last_id[6];
|
|
|
|
int last_id_pointer = 0;
|
|
|
|
bool next_is_type = false;
|
2016-12-31 00:59:23 +00:00
|
|
|
|
2016-12-31 05:11:31 +00:00
|
|
|
// align to the next index hole
|
2016-12-31 00:59:23 +00:00
|
|
|
index_count_ = 0;
|
|
|
|
while(!index_count_) run_for_cycles(1);
|
|
|
|
|
2016-12-31 04:10:52 +00:00
|
|
|
// capture every other bit until the next index hole
|
2016-12-31 00:59:23 +00:00
|
|
|
index_count_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(1) {
|
2016-12-31 00:59:23 +00:00
|
|
|
// wait until either another bit or the index hole arrives
|
|
|
|
bit_count_ = 0;
|
2016-12-31 17:23:08 +00:00
|
|
|
bool found_sync = false;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(!index_count_ && !found_sync && bit_count_ < 16) {
|
2016-12-31 17:23:08 +00:00
|
|
|
int previous_bit_count = bit_count_;
|
|
|
|
run_for_cycles(1);
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!distance_until_permissible_sync && bit_count_ != previous_bit_count) {
|
2016-12-31 17:23:08 +00:00
|
|
|
uint16_t low_shift_register = (shift_register_&0xffff);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_mfm_) {
|
2016-12-31 17:23:08 +00:00
|
|
|
found_sync = (low_shift_register == MFMIndexSync) || (low_shift_register == MFMSync);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-31 17:23:08 +00:00
|
|
|
found_sync =
|
|
|
|
(low_shift_register == FMIndexAddressMark) ||
|
|
|
|
(low_shift_register == FMIDAddressMark) ||
|
|
|
|
(low_shift_register == FMDataAddressMark) ||
|
|
|
|
(low_shift_register == FMDeletedDataAddressMark);
|
|
|
|
}
|
2016-12-31 05:11:31 +00:00
|
|
|
}
|
2016-12-31 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 17:23:08 +00:00
|
|
|
// if that was the index hole then finish
|
2017-03-26 18:34:47 +00:00
|
|
|
if(index_count_) {
|
2016-12-31 17:23:08 +00:00
|
|
|
if(bit_count_) result.push_back(get_byte_for_shift_value((uint16_t)(shift_register_ << (16 - bit_count_))));
|
|
|
|
break;
|
2016-12-31 04:10:52 +00:00
|
|
|
}
|
2016-12-31 17:23:08 +00:00
|
|
|
|
|
|
|
// store whatever the current byte is
|
|
|
|
uint8_t byte_value = get_byte_for_shift_value((uint16_t)shift_register_);
|
|
|
|
result.push_back(byte_value);
|
|
|
|
if(last_id_pointer < 6) last_id[last_id_pointer++] = byte_value;
|
|
|
|
|
|
|
|
// if no syncs are permissible here, decrement the waiting period and perform no further contemplation
|
|
|
|
bool found_id = false, found_data = false;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(distance_until_permissible_sync) {
|
2016-12-31 17:23:08 +00:00
|
|
|
distance_until_permissible_sync--;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(found_sync) {
|
|
|
|
if(is_mfm_) {
|
2016-12-31 17:23:08 +00:00
|
|
|
next_is_type = true;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
switch(shift_register_&0xffff) {
|
2016-12-31 17:23:08 +00:00
|
|
|
case FMIDAddressMark: found_id = true; break;
|
|
|
|
case FMDataAddressMark:
|
|
|
|
case FMDeletedDataAddressMark: found_data = true; break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else if(next_is_type) {
|
|
|
|
switch(byte_value) {
|
2016-12-31 20:25:11 +00:00
|
|
|
case IDAddressByte: found_id = true; break;
|
|
|
|
case DataAddressByte:
|
|
|
|
case DeletedDataAddressByte: found_data = true; break;
|
2016-12-31 17:23:08 +00:00
|
|
|
}
|
2016-12-31 05:20:00 +00:00
|
|
|
}
|
2016-12-31 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(found_id) {
|
2016-12-31 17:23:08 +00:00
|
|
|
distance_until_permissible_sync = 6;
|
|
|
|
last_id_pointer = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(found_data) {
|
2016-12-31 17:23:08 +00:00
|
|
|
distance_until_permissible_sync = 128 << last_id[3];
|
2016-12-31 04:10:52 +00:00
|
|
|
}
|
2016-12-31 00:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-31 20:25:11 +00:00
|
|
|
std::shared_ptr<Sector> Parser::get_next_sector()
|
2016-12-26 01:00:57 +00:00
|
|
|
{
|
2016-12-31 20:25:11 +00:00
|
|
|
std::shared_ptr<Sector> sector(new Sector);
|
2016-12-26 01:00:57 +00:00
|
|
|
index_count_ = 0;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
while(index_count_ < 2) {
|
2016-12-26 01:00:57 +00:00
|
|
|
// look for an ID address mark
|
2016-12-29 03:34:22 +00:00
|
|
|
bool id_found = false;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(!id_found) {
|
2016-12-26 01:00:57 +00:00
|
|
|
run_for_cycles(1);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_mfm_) {
|
|
|
|
while(shift_register_ == MFMSync) {
|
2016-12-26 01:00:57 +00:00
|
|
|
uint8_t mark = get_next_byte();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(mark == IDAddressByte) {
|
2016-12-29 00:48:46 +00:00
|
|
|
crc_generator_.set_value(MFMPostSyncCRCValue);
|
2016-12-29 03:34:22 +00:00
|
|
|
id_found = true;
|
2016-12-29 00:48:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-12-26 01:00:57 +00:00
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(shift_register_ == FMIDAddressMark) {
|
2016-12-29 00:48:46 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-29 03:34:22 +00:00
|
|
|
id_found = true;
|
2016-12-29 00:48:46 +00:00
|
|
|
}
|
2016-12-26 01:00:57 +00:00
|
|
|
}
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(IDAddressByte);
|
2016-12-26 01:00:57 +00:00
|
|
|
sector->track = get_next_byte();
|
|
|
|
sector->side = get_next_byte();
|
|
|
|
sector->sector = get_next_byte();
|
|
|
|
uint8_t size = get_next_byte();
|
|
|
|
uint16_t header_crc = crc_generator_.get_value();
|
|
|
|
if((header_crc >> 8) != get_next_byte()) continue;
|
|
|
|
if((header_crc & 0xff) != get_next_byte()) continue;
|
|
|
|
|
|
|
|
// look for data mark
|
2016-12-29 03:34:22 +00:00
|
|
|
bool data_found = false;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(!data_found) {
|
2016-12-26 01:00:57 +00:00
|
|
|
run_for_cycles(1);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_mfm_) {
|
|
|
|
while(shift_register_ == MFMSync) {
|
2016-12-26 01:00:57 +00:00
|
|
|
uint8_t mark = get_next_byte();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(mark == DataAddressByte) {
|
2016-12-29 00:48:46 +00:00
|
|
|
crc_generator_.set_value(MFMPostSyncCRCValue);
|
2016-12-29 03:34:22 +00:00
|
|
|
data_found = true;
|
2016-12-29 00:48:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-12-31 20:25:11 +00:00
|
|
|
if(mark == IDAddressByte) return nullptr;
|
2016-12-26 01:00:57 +00:00
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(shift_register_ == FMDataAddressMark) {
|
2016-12-29 00:48:46 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-29 03:34:22 +00:00
|
|
|
data_found = true;
|
2016-12-29 00:48:46 +00:00
|
|
|
}
|
2016-12-31 20:25:11 +00:00
|
|
|
if(shift_register_ == FMIDAddressMark) return nullptr;
|
2016-12-26 01:00:57 +00:00
|
|
|
}
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add(DataAddressByte);
|
2016-12-26 01:00:57 +00:00
|
|
|
|
|
|
|
size_t data_size = (size_t)(128 << size);
|
|
|
|
sector->data.reserve(data_size);
|
2017-03-26 18:34:47 +00:00
|
|
|
for(size_t c = 0; c < data_size; c++) {
|
2016-12-26 01:00:57 +00:00
|
|
|
sector->data.push_back(get_next_byte());
|
|
|
|
}
|
|
|
|
uint16_t data_crc = crc_generator_.get_value();
|
|
|
|
if((data_crc >> 8) != get_next_byte()) continue;
|
|
|
|
if((data_crc & 0xff) != get_next_byte()) continue;
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Sector> Parser::get_sector(uint8_t sector) {
|
2016-12-31 20:25:11 +00:00
|
|
|
std::shared_ptr<Sector> first_sector;
|
2016-12-30 22:55:06 +00:00
|
|
|
index_count_ = 0;
|
2016-12-30 22:44:35 +00:00
|
|
|
while(!first_sector && index_count_ < 2) first_sector = get_next_sector();
|
2016-12-26 01:00:57 +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-12-31 20:25:11 +00:00
|
|
|
std::shared_ptr<Sector> next_sector = get_next_sector();
|
2016-12-30 19:21:14 +00:00
|
|
|
if(!next_sector) continue;
|
2016-12-26 01:00:57 +00:00
|
|
|
if(next_sector->sector == first_sector->sector) return nullptr;
|
|
|
|
if(next_sector->sector == sector) return next_sector;
|
|
|
|
}
|
|
|
|
}
|