From 9781460c413c9f3aa216beaf364053c6eb52f4cd Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 26 Feb 2021 21:22:35 -0500 Subject: [PATCH 1/3] Thanks to a hint from the MAME guys: finally completes Macintosh 128kb and 512kb emulation (!) --- Analyser/Static/Macintosh/StaticAnalyser.cpp | 13 +++++ .../Apple/Macintosh/DriveSpeedAccumulator.cpp | 57 +++++++++++++------ .../Apple/Macintosh/DriveSpeedAccumulator.hpp | 5 +- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Analyser/Static/Macintosh/StaticAnalyser.cpp b/Analyser/Static/Macintosh/StaticAnalyser.cpp index 9c9b203d8..94356ef98 100644 --- a/Analyser/Static/Macintosh/StaticAnalyser.cpp +++ b/Analyser/Static/Macintosh/StaticAnalyser.cpp @@ -19,6 +19,19 @@ Analyser::Static::TargetList Analyser::Static::Macintosh::GetTargets(const Media using Target = Analyser::Static::Macintosh::Target; auto *const target = new Target; target->media = media; + + // If this is a single-sided floppy disk, guess the Macintosh 512kb. + if(media.mass_storage_devices.empty()) { + bool has_800kb_disks = false; + for(const auto &disk: media.disks) { + has_800kb_disks |= disk->get_head_count() > 1; + } + + if(!has_800kb_disks) { + target->model = Target::Model::Mac512k; + } + } + targets.push_back(std::unique_ptr(target)); return targets; diff --git a/Machines/Apple/Macintosh/DriveSpeedAccumulator.cpp b/Machines/Apple/Macintosh/DriveSpeedAccumulator.cpp index b4277def4..7a6a3ec1b 100644 --- a/Machines/Apple/Macintosh/DriveSpeedAccumulator.cpp +++ b/Machines/Apple/Macintosh/DriveSpeedAccumulator.cpp @@ -8,6 +8,36 @@ #include "DriveSpeedAccumulator.hpp" +namespace { + +/* + For knowledge encapsulate below, all credit goes to the MAME team. No original research here. + + Per their investigation, the bytes collected for PWM output feed a 6-bit LFSR, which then keeps + output high until it eventually reaches a state of 0x20. The LFSR shifts rightward and taps bits + 0 and 1 as the new input into bit 5. + + I've therefore implemented the LFSR as below, feeding into a lookup table to calculate actual + pulse widths from the values stored into the PWM buffer. +*/ +template constexpr uint8_t lfsr() { + if constexpr (value == 0x20 || !value) return 0; + return 1+lfsr<(((value ^ (value >> 1))&1) << 5) | (value >> 1)>(); +} + +constexpr uint8_t pwm_lookup[] = { + lfsr<0>(), lfsr<1>(), lfsr<2>(), lfsr<3>(), lfsr<4>(), lfsr<5>(), lfsr<6>(), lfsr<7>(), + lfsr<8>(), lfsr<9>(), lfsr<10>(), lfsr<11>(), lfsr<12>(), lfsr<13>(), lfsr<14>(), lfsr<15>(), + lfsr<16>(), lfsr<17>(), lfsr<18>(), lfsr<19>(), lfsr<20>(), lfsr<21>(), lfsr<22>(), lfsr<23>(), + lfsr<24>(), lfsr<25>(), lfsr<26>(), lfsr<27>(), lfsr<28>(), lfsr<29>(), lfsr<30>(), lfsr<31>(), + lfsr<32>(), lfsr<33>(), lfsr<34>(), lfsr<35>(), lfsr<36>(), lfsr<37>(), lfsr<38>(), lfsr<39>(), + lfsr<40>(), lfsr<41>(), lfsr<42>(), lfsr<43>(), lfsr<44>(), lfsr<45>(), lfsr<46>(), lfsr<47>(), + lfsr<48>(), lfsr<49>(), lfsr<50>(), lfsr<51>(), lfsr<52>(), lfsr<53>(), lfsr<54>(), lfsr<55>(), + lfsr<56>(), lfsr<57>(), lfsr<58>(), lfsr<59>(), lfsr<60>(), lfsr<61>(), lfsr<62>(), lfsr<63>(), +}; + +} + using namespace Apple::Macintosh; void DriveSpeedAccumulator::post_sample(uint8_t sample) { @@ -17,40 +47,33 @@ void DriveSpeedAccumulator::post_sample(uint8_t sample) { // the samples until there is a certain small quantity of them, // then produce a new estimate of rotation speed and start the // buffer afresh. - samples_[sample_pointer_] = sample; - ++sample_pointer_; - - if(sample_pointer_ == samples_.size()) { - sample_pointer_ = 0; + // + // Note the table lookup here; see text above. + sample_total_ += pwm_lookup[sample & 0x3f]; + ++sample_count_; + if(sample_count_ == samples_per_bucket) { // The below fits for a function like `a + bc`; it encapsultes the following // beliefs: // // (i) motor speed is proportional to voltage supplied; // (ii) with pulse-width modulation it's therefore proportional to the duty cycle; - // (iii) the Mac pulse-width modulates whatever it reads from the disk speed buffer; + // (iii) the Mac pulse-width modulates whatever it reads from the disk speed buffer, as per the LFSR rules above; // (iv) ... subject to software pulse-width modulation of that pulse-width modulation. // // So, I believe current motor speed is proportional to a low-pass filtering of // the speed buffer. Which I've implemented very coarsely via 'large' bucketed-averages, // noting also that exact disk motor speed is always a little approximate. - // Sum all samples. - // TODO: if the above is the correct test, do it on sample receipt rather than - // bothering with an intermediate buffer. - int sum = 0; - for(auto s: samples_) { - sum += s; - } - // The formula below was derived from observing values the Mac wrote into its // disk-speed buffer. Given that it runs a calibration loop before doing so, // I cannot guarantee the accuracy of these numbers beyond being within the // range that the computer would accept. - const float normalised_sum = float(sum) / float(samples_.size()); - const float rotation_speed = (normalised_sum * 27.08f) - 259.0f; + const float normalised_sum = float(sample_total_) / float(samples_per_bucket); + const float rotation_speed = (normalised_sum - 3.7f) * 17.6f; delegate_->drive_speed_accumulator_set_drive_speed(this, rotation_speed); + sample_count_ = 0; + sample_total_ = 0; } } - diff --git a/Machines/Apple/Macintosh/DriveSpeedAccumulator.hpp b/Machines/Apple/Macintosh/DriveSpeedAccumulator.hpp index f626217d1..45568e006 100644 --- a/Machines/Apple/Macintosh/DriveSpeedAccumulator.hpp +++ b/Machines/Apple/Macintosh/DriveSpeedAccumulator.hpp @@ -34,8 +34,9 @@ class DriveSpeedAccumulator { } private: - std::array samples_; - std::size_t sample_pointer_ = 0; + static constexpr int samples_per_bucket = 20; + int sample_count_ = 0; + int sample_total_ = 0; Delegate *delegate_ = nullptr; }; From a1df8452cea56dc3c2739cac61d52c1c13789d83 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 26 Feb 2021 21:22:54 -0500 Subject: [PATCH 2/3] Add the 128kb and 512kb Macintoshes as selectable options in macOS. --- .../MachinePicker/Base.lproj/MachinePicker.xib | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/MachinePicker/Base.lproj/MachinePicker.xib b/OSBindings/Mac/Clock Signal/MachinePicker/Base.lproj/MachinePicker.xib index caab21366..423168470 100644 --- a/OSBindings/Mac/Clock Signal/MachinePicker/Base.lproj/MachinePicker.xib +++ b/OSBindings/Mac/Clock Signal/MachinePicker/Base.lproj/MachinePicker.xib @@ -238,11 +238,11 @@ Gw - + - + @@ -250,13 +250,15 @@ Gw - - + + - + + + From 4f5eb4d71b684a35f48dea70f4445eb5dd1b3537 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 26 Feb 2021 21:25:11 -0500 Subject: [PATCH 3/3] Adds the Mac 128k & 512k as Qt options. --- OSBindings/Qt/mainwindow.cpp | 6 ++++-- OSBindings/Qt/mainwindow.ui | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/OSBindings/Qt/mainwindow.cpp b/OSBindings/Qt/mainwindow.cpp index 86afb4691..add228524 100644 --- a/OSBindings/Qt/mainwindow.cpp +++ b/OSBindings/Qt/mainwindow.cpp @@ -1175,8 +1175,10 @@ void MainWindow::start_macintosh() { auto target = std::make_unique(); switch(ui->macintoshModelComboBox->currentIndex()) { - default: target->model = Target::Model::Mac512ke; break; - case 1: target->model = Target::Model::MacPlus; break; + default: target->model = Target::Model::Mac128k; break; + case 1: target->model = Target::Model::Mac512k; break; + case 2: target->model = Target::Model::Mac512ke; break; + case 3: target->model = Target::Model::MacPlus; break; } launchTarget(std::move(target)); diff --git a/OSBindings/Qt/mainwindow.ui b/OSBindings/Qt/mainwindow.ui index e7d29c142..88acf3f0e 100644 --- a/OSBindings/Qt/mainwindow.ui +++ b/OSBindings/Qt/mainwindow.ui @@ -248,6 +248,16 @@ + + + 128k + + + + + 512k + + 512ke