2021-07-16 22:11:07 +00:00
|
|
|
//
|
|
|
|
// AmigaADF.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AmigaADF.hpp"
|
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
#include "../../Encodings/MFM/Constants.hpp"
|
|
|
|
#include "../../Encodings/MFM/Encoder.hpp"
|
2021-10-12 21:49:01 +00:00
|
|
|
#include "../../../../Numeric/BitSpread.hpp"
|
2021-07-17 00:07:17 +00:00
|
|
|
#include "../../Track/PCMTrack.hpp"
|
2021-07-16 22:11:07 +00:00
|
|
|
|
2021-10-10 19:58:10 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2021-07-16 22:11:07 +00:00
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-10-10 23:06:51 +00:00
|
|
|
/// Builds a buffer containing the bytes between @c begin and @c end split up so that the nibbles in the first half of the buffer
|
|
|
|
/// consist of the odd bits of the source bytes — b1, b3, b5 and b7 — ordered so that most-significant nibbles come before
|
|
|
|
/// least-significant ones, and the second half of the buffer contains the even bits.
|
|
|
|
///
|
|
|
|
/// Nibbles are written to @c first; it is assumed that an even number of source bytes have been supplied.
|
2021-10-10 18:47:48 +00:00
|
|
|
template <typename IteratorT, class OutputIt> void encode_block(IteratorT begin, IteratorT end, OutputIt first) {
|
|
|
|
// Parse 1: combine odd bits.
|
2021-07-17 00:07:17 +00:00
|
|
|
auto cursor = begin;
|
|
|
|
while(cursor != end) {
|
2021-10-09 20:45:19 +00:00
|
|
|
auto source = uint8_t(
|
|
|
|
((*cursor & 0x02) << 3) |
|
|
|
|
((*cursor & 0x08) << 2) |
|
|
|
|
((*cursor & 0x20) << 1) |
|
|
|
|
((*cursor & 0x80) << 0)
|
|
|
|
);
|
2021-07-17 00:07:17 +00:00
|
|
|
++cursor;
|
|
|
|
source |=
|
2021-10-09 20:45:19 +00:00
|
|
|
((*cursor & 0x02) >> 1) |
|
|
|
|
((*cursor & 0x08) >> 2) |
|
|
|
|
((*cursor & 0x20) >> 3) |
|
|
|
|
((*cursor & 0x80) >> 4);
|
2021-07-17 00:07:17 +00:00
|
|
|
++cursor;
|
|
|
|
|
2021-10-10 18:47:48 +00:00
|
|
|
*first = source;
|
|
|
|
++first;
|
2021-07-17 00:07:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 18:47:48 +00:00
|
|
|
// Parse 2: combine even bits.
|
2021-07-17 00:07:17 +00:00
|
|
|
cursor = begin;
|
|
|
|
while(cursor != end) {
|
2021-10-09 20:45:19 +00:00
|
|
|
auto source = uint8_t(
|
|
|
|
((*cursor & 0x01) << 4) |
|
|
|
|
((*cursor & 0x04) << 3) |
|
|
|
|
((*cursor & 0x10) << 2) |
|
|
|
|
((*cursor & 0x40) << 1)
|
|
|
|
);
|
2021-07-17 00:07:17 +00:00
|
|
|
++cursor;
|
|
|
|
source |=
|
2021-10-09 20:45:19 +00:00
|
|
|
((*cursor & 0x01) >> 0) |
|
|
|
|
((*cursor & 0x04) >> 1) |
|
|
|
|
((*cursor & 0x10) >> 2) |
|
|
|
|
((*cursor & 0x40) >> 3);
|
2021-07-17 00:07:17 +00:00
|
|
|
++cursor;
|
|
|
|
|
2021-10-10 18:47:48 +00:00
|
|
|
*first = source;
|
|
|
|
++first;
|
2021-07-17 00:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 23:06:51 +00:00
|
|
|
/// Construsts the Amiga-style checksum of the bytes between @c begin and @c end, which is a 32-bit exclusive OR of the source data
|
|
|
|
/// with each byte converted into a 16-bit word by inserting a 0 bit between every data bit, and then combined into 32-bit words in
|
|
|
|
/// big endian order.
|
2021-10-10 19:58:10 +00:00
|
|
|
template <typename IteratorT> auto checksum(IteratorT begin, IteratorT end) {
|
|
|
|
uint16_t checksum[2]{};
|
|
|
|
int offset = 0;
|
2021-10-10 18:47:48 +00:00
|
|
|
while(begin != end) {
|
|
|
|
const uint8_t value = *begin;
|
|
|
|
++begin;
|
|
|
|
|
|
|
|
// Do a clockless MFM encode.
|
2021-10-12 21:49:01 +00:00
|
|
|
const auto spread = Numeric::spread_bits(value);
|
2021-10-10 19:58:10 +00:00
|
|
|
checksum[offset] ^= spread;
|
|
|
|
offset ^= 1;
|
2021-10-10 18:47:48 +00:00
|
|
|
}
|
2021-07-17 00:07:17 +00:00
|
|
|
|
2021-10-10 19:58:10 +00:00
|
|
|
return std::array<uint8_t, 4>{
|
|
|
|
uint8_t(checksum[0] >> 8),
|
|
|
|
uint8_t(checksum[0]),
|
|
|
|
uint8_t(checksum[1] >> 8),
|
|
|
|
uint8_t(checksum[1]),
|
2021-07-17 00:07:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-10 23:06:51 +00:00
|
|
|
/// Obtains the Amiga-style checksum of the data between @c begin and @c end, then odd-even encodes it and writes
|
|
|
|
/// it out to @c encoder.
|
2021-10-10 18:47:48 +00:00
|
|
|
template <typename IteratorT> void write_checksum(IteratorT begin, IteratorT end, std::unique_ptr<Storage::Encodings::MFM::Encoder> &encoder) {
|
2021-10-10 19:58:10 +00:00
|
|
|
// Believe it or not, this appears to be the actual checksum algorithm on the Amiga:
|
|
|
|
//
|
|
|
|
// (1) calculate the XOR checksum of the MFM-encoded data, read as 32-bit words;
|
|
|
|
// (2) throw away the clock bits;
|
|
|
|
// (3) Take the resulting 32-bit value and perform an odd-even MFM encoding on those.
|
2021-10-10 18:47:48 +00:00
|
|
|
const auto raw_checksum = checksum(begin, end);
|
|
|
|
|
2021-10-10 19:58:10 +00:00
|
|
|
std::decay_t<decltype(raw_checksum)> encoded_checksum{};
|
2021-10-10 18:47:48 +00:00
|
|
|
encode_block(raw_checksum.begin(), raw_checksum.end(), encoded_checksum.begin());
|
|
|
|
|
2021-10-10 23:06:51 +00:00
|
|
|
encoder->add_bytes(encoded_checksum.begin(), encoded_checksum.end());
|
2021-10-10 18:47:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:11:07 +00:00
|
|
|
AmigaADF::AmigaADF(const std::string &file_name) :
|
|
|
|
file_(file_name) {
|
|
|
|
// Dumb validation only for now: a size check.
|
|
|
|
if(file_.stats().st_size != 901120) throw Error::InvalidFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeadPosition AmigaADF::get_maximum_head_position() {
|
|
|
|
return HeadPosition(80);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AmigaADF::get_head_count() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Track> AmigaADF::get_track_at_position(Track::Address address) {
|
2021-07-17 00:07:17 +00:00
|
|
|
using namespace Storage::Encodings;
|
|
|
|
|
|
|
|
// Create an MFM encoder.
|
|
|
|
Storage::Disk::PCMSegment encoded_segment;
|
|
|
|
encoded_segment.data.reserve(102'400); // i.e. 0x1900 bytes.
|
|
|
|
auto encoder = MFM::GetMFMEncoder(encoded_segment.data);
|
|
|
|
|
|
|
|
// Grab the unencoded track.
|
2021-07-16 22:11:07 +00:00
|
|
|
file_.seek(get_file_offset_for_position(address), SEEK_SET);
|
2021-07-17 00:07:17 +00:00
|
|
|
const std::vector<uint8_t> track_data = file_.read(512 * 11);
|
|
|
|
|
|
|
|
// Eleven sectors are then encoded.
|
|
|
|
for(size_t s = 0; s < 11; s++) {
|
|
|
|
// Two bytes of 0x00 act as an inter-sector gap.
|
|
|
|
encoder->add_byte(0);
|
|
|
|
encoder->add_byte(0);
|
|
|
|
|
|
|
|
// Add additional sync.
|
|
|
|
encoder->output_short(MFM::MFMSync);
|
|
|
|
encoder->output_short(MFM::MFMSync);
|
|
|
|
|
2021-10-10 18:47:48 +00:00
|
|
|
// Encode and write the header.
|
2021-07-17 00:07:17 +00:00
|
|
|
const uint8_t header[4] = {
|
2021-10-10 19:58:10 +00:00
|
|
|
0xff, // Amiga v1.0 format.
|
|
|
|
uint8_t(address.position.as_int() * 2 + address.head), // Track.
|
|
|
|
uint8_t(s), // Sector.
|
|
|
|
uint8_t(11 - s), // Sectors remaining.
|
2021-07-17 00:07:17 +00:00
|
|
|
};
|
2021-10-10 18:47:48 +00:00
|
|
|
std::array<uint8_t, 4> encoded_header;
|
|
|
|
encode_block(std::begin(header), std::end(header), std::begin(encoded_header));
|
2021-10-10 23:06:51 +00:00
|
|
|
encoder->add_bytes(std::begin(encoded_header), std::end(encoded_header));
|
2021-10-10 18:47:48 +00:00
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
// Write the sector label.
|
|
|
|
const std::array<uint8_t, 16> os_recovery{};
|
2021-10-10 23:06:51 +00:00
|
|
|
encoder->add_bytes(os_recovery.begin(), os_recovery.end());
|
2021-07-17 00:07:17 +00:00
|
|
|
|
2021-10-10 19:58:10 +00:00
|
|
|
// Encode the data.
|
|
|
|
std::array<uint8_t, 512> encoded_data;
|
|
|
|
encode_block(&track_data[s * 512], &track_data[(s + 1) * 512], std::begin(encoded_data));
|
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
// Write checksums.
|
2021-10-10 18:47:48 +00:00
|
|
|
write_checksum(std::begin(encoded_header), std::end(encoded_header), encoder);
|
|
|
|
write_checksum(std::begin(encoded_data), std::end(encoded_data), encoder);
|
2021-07-16 22:11:07 +00:00
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
// Write data.
|
2021-10-10 23:06:51 +00:00
|
|
|
encoder->add_bytes(std::begin(encoded_data), std::end(encoded_data));
|
2021-07-17 00:07:17 +00:00
|
|
|
}
|
2021-07-16 22:11:07 +00:00
|
|
|
|
2021-10-12 01:05:37 +00:00
|
|
|
// Throw in an '830-byte' gap (that's in MFM, I think — 830 bytes prior to decoding).
|
|
|
|
// Cf. https://www.techtravels.org/2007/01/syncing-to-the-0x4489-0x4489/#comment-295
|
|
|
|
for(int c = 0; c < 415; c++) {
|
|
|
|
encoder->add_byte(0xff);
|
|
|
|
}
|
|
|
|
|
2021-07-17 00:07:17 +00:00
|
|
|
return std::make_shared<Storage::Disk::PCMTrack>(std::move(encoded_segment));
|
2021-07-16 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long AmigaADF::get_file_offset_for_position(Track::Address address) {
|
|
|
|
return (address.position.as_int() * 2 + address.head) * 512 * 11;
|
|
|
|
}
|