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) {
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
uint16_t or_bits = (uint16_t)((spread_value << 1) | (spread_value >> 1) | (output_ << 15));
|
|
|
|
output_ = spread_value | ((~or_bits) & 0xaaaa);
|
2016-11-26 05:39:20 +00:00
|
|
|
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-11-26 05:39:20 +00:00
|
|
|
output_short(output_ = MFMIndexAddressMark);
|
2016-09-25 21:46:11 +00:00
|
|
|
add_byte(MFMIndexAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_ID_address_mark() {
|
2016-11-26 05:39:20 +00:00
|
|
|
output_short(output_ = MFMAddressMark);
|
2016-09-25 21:46:11 +00:00
|
|
|
add_byte(MFMIDAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_data_address_mark() {
|
2016-11-26 05:39:20 +00:00
|
|
|
output_short(output_ = MFMAddressMark);
|
2016-09-25 21:46:11 +00:00
|
|
|
add_byte(MFMDataAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_deleted_data_address_mark() {
|
2016-11-26 05:39:20 +00:00
|
|
|
output_short(output_ = MFMAddressMark);
|
2016-09-25 21:46:11 +00:00
|
|
|
add_byte(MFMDeletedDataAddressByte);
|
2016-09-18 21:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint16_t output_;
|
|
|
|
};
|
|
|
|
|
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-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
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-11-26 05:39:20 +00:00
|
|
|
void add_index_address_mark() { output_short(FMIndexAddressMark); }
|
|
|
|
void add_ID_address_mark() { output_short(FMIDAddressMark); }
|
|
|
|
void add_data_address_mark() { output_short(FMDataAddressMark); }
|
|
|
|
void add_deleted_data_address_mark() { output_short(FMDeletedDataAddressMark); }
|
2016-09-18 21:16:20 +00:00
|
|
|
};
|
|
|
|
|
2016-09-18 22:33:26 +00:00
|
|
|
static uint8_t logarithmic_size_for_size(size_t size)
|
|
|
|
{
|
|
|
|
switch(size)
|
|
|
|
{
|
|
|
|
default: return 0;
|
|
|
|
case 256: return 1;
|
|
|
|
case 512: return 2;
|
|
|
|
case 1024: return 3;
|
|
|
|
case 2048: return 4;
|
|
|
|
case 4196: return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:20 +00:00
|
|
|
template<class T> std::shared_ptr<Storage::Disk::Track>
|
|
|
|
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,
|
2016-09-18 22:56:35 +00:00
|
|
|
size_t inter_sector_gap,
|
|
|
|
size_t expected_track_bytes)
|
2016-09-18 21:16:20 +00:00
|
|
|
{
|
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 22:33:26 +00:00
|
|
|
NumberTheory::CRC16 crc_generator(0x1021, 0xffff);
|
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
|
|
|
|
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);
|
|
|
|
|
|
|
|
// header CRC
|
|
|
|
crc_generator.reset();
|
|
|
|
crc_generator.add(sector.track);
|
|
|
|
crc_generator.add(sector.side);
|
|
|
|
crc_generator.add(sector.sector);
|
|
|
|
crc_generator.add(size);
|
|
|
|
uint16_t crc_value = crc_generator.get_value();
|
|
|
|
shifter.add_byte(crc_value >> 8);
|
2016-09-18 22:46:58 +00:00
|
|
|
shifter.add_byte(crc_value & 0xff);
|
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
|
|
|
crc_generator.reset();
|
|
|
|
for(size_t c = 0; c < sector.data.size(); c++)
|
|
|
|
{
|
|
|
|
shifter.add_byte(sector.data[c]);
|
|
|
|
crc_generator.add(sector.data[c]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// data CRC
|
|
|
|
crc_value = crc_generator.get_value();
|
|
|
|
shifter.add_byte(crc_value >> 8);
|
2016-09-18 22:46:58 +00:00
|
|
|
shifter.add_byte(crc_value & 0xff);
|
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) :
|
|
|
|
target_(target)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Encoder::output_short(uint16_t value)
|
|
|
|
{
|
|
|
|
target_.push_back(value >> 8);
|
|
|
|
target_.push_back(value & 0xff);
|
|
|
|
}
|
2016-09-18 21:16:20 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
std::unique_ptr<Encoder> Storage::Encodings::MFM::GetMFMEncoder(std::vector<uint8_t> &target)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<Encoder>(new MFMEncoder(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Encoder> Storage::Encodings::MFM::GetFMEncoder(std::vector<uint8_t> &target)
|
|
|
|
{
|
|
|
|
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) :
|
2016-12-26 01:00:57 +00:00
|
|
|
Storage::Disk::Controller(4000000, 1, 300),
|
|
|
|
crc_generator_(0x1021, 0xffff),
|
|
|
|
shift_register_(0), track_(0), is_mfm_(is_mfm)
|
|
|
|
{
|
|
|
|
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) :
|
|
|
|
Parser(is_mfm)
|
|
|
|
{
|
|
|
|
drive->set_disk(disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser::Parser(bool is_mfm, const std::shared_ptr<Storage::Disk::Track> &track) :
|
|
|
|
Parser(is_mfm)
|
|
|
|
{
|
|
|
|
drive->set_disk_with_track(track);
|
|
|
|
}
|
|
|
|
|
2016-12-26 01:00:57 +00:00
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> Parser::get_sector(uint8_t track, uint8_t sector)
|
|
|
|
{
|
|
|
|
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++) step(direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_sector(sector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::process_input_bit(int value, unsigned int cycles_since_index_hole)
|
|
|
|
{
|
|
|
|
shift_register_ = ((shift_register_ << 1) | (unsigned int)value) & 0xffff;
|
|
|
|
bit_count_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::process_index_hole()
|
|
|
|
{
|
|
|
|
index_count_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Parser::get_next_byte()
|
|
|
|
{
|
|
|
|
bit_count_ = 0;
|
|
|
|
while(bit_count_ < 16) run_for_cycles(1);
|
|
|
|
uint8_t byte = (uint8_t)(
|
|
|
|
((shift_register_&0x0001) >> 0) |
|
|
|
|
((shift_register_&0x0004) >> 1) |
|
|
|
|
((shift_register_&0x0010) >> 2) |
|
|
|
|
((shift_register_&0x0040) >> 3) |
|
|
|
|
((shift_register_&0x0100) >> 4) |
|
|
|
|
((shift_register_&0x0400) >> 5) |
|
|
|
|
((shift_register_&0x1000) >> 6) |
|
|
|
|
((shift_register_&0x4000) >> 7));
|
|
|
|
crc_generator_.add(byte);
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> Parser::get_next_sector()
|
|
|
|
{
|
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> sector(new Storage::Encodings::MFM::Sector);
|
|
|
|
index_count_ = 0;
|
|
|
|
|
|
|
|
while(index_count_ < 2)
|
|
|
|
{
|
|
|
|
// look for an ID address mark
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
run_for_cycles(1);
|
|
|
|
if(is_mfm_)
|
|
|
|
{
|
|
|
|
if(shift_register_ == Storage::Encodings::MFM::MFMAddressMark)
|
|
|
|
{
|
|
|
|
uint8_t mark = get_next_byte();
|
|
|
|
if(mark == Storage::Encodings::MFM::MFMIDAddressByte) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(shift_register_ == Storage::Encodings::MFM::FMIDAddressMark) break;
|
|
|
|
}
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
crc_generator_.reset();
|
|
|
|
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
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
run_for_cycles(1);
|
|
|
|
if(is_mfm_)
|
|
|
|
{
|
|
|
|
if(shift_register_ == Storage::Encodings::MFM::MFMAddressMark)
|
|
|
|
{
|
|
|
|
uint8_t mark = get_next_byte();
|
|
|
|
if(mark == Storage::Encodings::MFM::MFMDataAddressByte) break;
|
|
|
|
if(mark == Storage::Encodings::MFM::MFMIDAddressByte) return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(shift_register_ == Storage::Encodings::MFM::FMDataAddressMark) break;
|
|
|
|
if(shift_register_ == Storage::Encodings::MFM::FMIDAddressMark) return nullptr;
|
|
|
|
}
|
|
|
|
if(index_count_ >= 2) return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t data_size = (size_t)(128 << size);
|
|
|
|
sector->data.reserve(data_size);
|
|
|
|
crc_generator_.reset();
|
|
|
|
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()) continue;
|
|
|
|
if((data_crc & 0xff) != get_next_byte()) continue;
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> Parser::get_sector(uint8_t sector)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> first_sector = get_next_sector();
|
|
|
|
if(!first_sector) return first_sector;
|
|
|
|
if(first_sector->sector == sector) return first_sector;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Storage::Encodings::MFM::Sector> next_sector = get_next_sector();
|
|
|
|
if(next_sector->sector == first_sector->sector) return nullptr;
|
|
|
|
if(next_sector->sector == sector) return next_sector;
|
|
|
|
}
|
|
|
|
}
|