1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-19 19:16:34 +00:00

Use <algorithm>, and otherwise reduce.

This commit is contained in:
Thomas Harte
2025-07-19 21:37:03 -04:00
parent fd4a91ba72
commit 88e776ad5b
3 changed files with 28 additions and 25 deletions
+2
View File
@@ -63,6 +63,8 @@ uint8_t IWM::read(const int address) {
if(data_register_ & 0x80) {
data_register_ = 0;
// logger.info().append("Reading data: %02x", result);
} else {
// logger.info().append("Spurious read?");
}
// logger.info().append("Reading data register: %02x", result);
+15 -14
View File
@@ -13,6 +13,7 @@
#include "Storage/Disk/Encodings/AppleGCR/Encoder.hpp"
#include "Storage/Disk/Encodings/AppleGCR/SegmentParser.hpp"
#include <algorithm>
#include <bit>
#include <cstring>
@@ -204,11 +205,12 @@ std::unique_ptr<Track> MacintoshIMG::track_at_position(const Track::Address addr
const std::lock_guard buffer_lock(buffer_mutex_);
if(encoding_ == Encoding::GCR400 || encoding_ == Encoding::GCR800) {
// Perform a GCR encoding.
const auto included_sectors = Storage::Encodings::AppleGCR::Macintosh::sectors_in_track(address.position.as_int());
const size_t start_sector = size_t(included_sectors.start * head_count() + included_sectors.length * address.head);
const auto included_sectors =
Storage::Encodings::AppleGCR::Macintosh::sectors_in_track(address.position.as_int());
const size_t start_sector =
size_t(included_sectors.start * head_count() + included_sectors.length * address.head);
if(start_sector*512 >= data_.size()) return nullptr;
const uint8_t *const sector = &data_[512 * start_sector];
const uint8_t *const tags = tags_.size() ? &tags_[12 * start_sector] : nullptr;
@@ -228,36 +230,35 @@ std::unique_ptr<Track> MacintoshIMG::track_at_position(const Track::Address addr
for(int c = 0; c < included_sectors.length; ++c) {
const uint8_t sector_id = source_sectors[c];
uint8_t sector_plus_tags[524];
auto target = std::begin(sector_plus_tags);
// Copy in the tags, if provided; otherwise generate them.
if(tags) {
memcpy(sector_plus_tags, &tags[sector_id * 12], 12);
std::copy(&tags[sector_id * 12], &tags[(sector_id + 1) * 12], target);
} else {
// TODO: fill in tags properly.
memset(sector_plus_tags, 0, 12);
std::fill(target, target + 12, 0);
}
target += 12;
// Copy in the sector body.
memcpy(&sector_plus_tags[12], &sector[sector_id * 512], 512);
// NB: sync lengths below are identical to those for
// the Apple II, as I have no idea whatsoever what they
// should be.
std::copy(&sector[sector_id * 512], &sector[(sector_id + 1) * 512], target);
// NB: sync lengths below are probably not identical to any
// specific Mac.
segment += Encodings::AppleGCR::six_and_two_sync(28);
segment += Encodings::AppleGCR::Macintosh::header(
format_,
uint8_t(address.position.as_int()),
sector_id,
!!address.head
address.head > 0
);
segment += Encodings::AppleGCR::six_and_two_sync(7);
segment += Encodings::AppleGCR::six_and_two_sync(4);
segment += Encodings::AppleGCR::Macintosh::data(sector_id, sector_plus_tags);
segment += Encodings::AppleGCR::six_and_two_sync(20);
}
// TODO: it seems some tracks are skewed respective to others; investigate further.
// segment.rotate_right(3000); // Just a test, yo.
return std::make_unique<PCMTrack>(segment);
}
+11 -11
View File
@@ -8,6 +8,9 @@
#include "Encoder.hpp"
#include <cassert>
#include <bit>
namespace {
const uint8_t five_and_three_mapping[] = {
@@ -199,7 +202,7 @@ AppleGCR::Macintosh::SectorSpan AppleGCR::Macintosh::sectors_in_track(int track)
}
Storage::Disk::PCMSegment AppleGCR::Macintosh::header(uint8_t type, uint8_t track, uint8_t sector, bool side_two) {
std::vector<uint8_t> data(11);
std::vector<uint8_t> data(10);
// The standard prologue.
data[0] = header_prologue[0];
@@ -215,8 +218,8 @@ Storage::Disk::PCMSegment AppleGCR::Macintosh::header(uint8_t type, uint8_t trac
// 5) the XOR of all those fields.
//
// (all two-and-six encoded).
data[3] = track&0x3f;
data[4] = sector;
data[3] = track & 0x3f;
data[4] = sector & 0x3f;
data[5] = (side_two ? 0x20 : 0x00) | ((track >> 6) & 0x1f);
data[6] = type;
data[7] = data[3] ^ data[4] ^ data[5] ^ data[6];
@@ -225,16 +228,15 @@ Storage::Disk::PCMSegment AppleGCR::Macintosh::header(uint8_t type, uint8_t trac
data[c] = six_and_two_mapping[data[c]];
}
// Then the standard epilogue.
// Then the standard epilogue, as far as the Mac cares.
data[8] = epilogue[0];
data[9] = epilogue[1];
data[10] = epilogue[2];
return Storage::Disk::PCMSegment(data);
}
Storage::Disk::PCMSegment AppleGCR::Macintosh::data(uint8_t sector, const uint8_t *source) {
std::vector<uint8_t> output(710);
std::vector<uint8_t> output(709);
int checksum[3] = {0, 0, 0};
// Write prologue.
@@ -256,12 +258,12 @@ Storage::Disk::PCMSegment AppleGCR::Macintosh::data(uint8_t sector, const uint8_
uint8_t values[3];
// The low byte of the checksum is rotated left one position; Cf. 18FA4.
checksum[0] = (checksum[0] << 1) | (checksum[0] >> 7);
checksum[0] = std::rotl(uint8_t(checksum[0]), 1);
// See 18FBA and 18FBC: an ADDX (with the carry left over from the roll)
// and an EOR act to update the checksum and generate the next output.
values[0] = uint8_t(*source ^ checksum[0]);
checksum[2] += *source + (checksum[0] >> 8);
checksum[2] += *source + (checksum[0] & 1);
++source;
// As above, but now 18FD0 and 18FD2.
@@ -281,7 +283,6 @@ Storage::Disk::PCMSegment AppleGCR::Macintosh::data(uint8_t sector, const uint8_
// Throw away the top bits of checksum[1] and checksum[2]; the original
// routine is byte centric, the longer ints here are just to retain the
// carry after each add transientliy.
checksum[0] &= 0xff;
checksum[1] &= 0xff;
checksum[2] &= 0xff;
@@ -313,10 +314,9 @@ Storage::Disk::PCMSegment AppleGCR::Macintosh::data(uint8_t sector, const uint8_
((checksum[0] >> 6) & 0x03)
];
// Write epilogue.
// Write epilogue; the Mac needs only the first two bytes,
output[707] = epilogue[0];
output[708] = epilogue[1];
output[709] = epilogue[2];
return Storage::Disk::PCMSegment(output);
}