2018-04-21 21:33:42 +00:00
|
|
|
//
|
|
|
|
// DiskII.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 20/04/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-04-21 21:33:42 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "DiskII.hpp"
|
2018-04-24 05:11:31 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
2018-05-12 21:54:13 +00:00
|
|
|
#include <cstring>
|
2018-04-24 05:11:31 +00:00
|
|
|
|
|
|
|
using namespace Apple;
|
|
|
|
|
2018-04-25 02:44:45 +00:00
|
|
|
namespace {
|
2018-05-10 02:03:59 +00:00
|
|
|
const uint8_t input_command = 0x4; // i.e. Q6
|
|
|
|
const uint8_t input_mode = 0x8; // i.e. Q7
|
|
|
|
const uint8_t input_flux = 0x1;
|
2018-04-25 02:44:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 21:19:29 +00:00
|
|
|
DiskII::DiskII(int clock_rate) :
|
|
|
|
clock_rate_(clock_rate),
|
2018-04-29 21:51:10 +00:00
|
|
|
inputs_(input_command),
|
2019-07-10 20:05:59 +00:00
|
|
|
drives_{{clock_rate, 300, 1}, {clock_rate, 300, 1}}
|
2018-04-25 02:44:45 +00:00
|
|
|
{
|
2018-05-28 03:17:06 +00:00
|
|
|
drives_[0].set_clocking_hint_observer(this);
|
|
|
|
drives_[1].set_clocking_hint_observer(this);
|
2018-05-17 02:07:54 +00:00
|
|
|
drives_[active_drive_].set_event_delegate(this);
|
2018-04-25 02:44:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 05:11:31 +00:00
|
|
|
void DiskII::set_control(Control control, bool on) {
|
2018-04-25 02:44:45 +00:00
|
|
|
int previous_stepper_mask = stepper_mask_;
|
|
|
|
switch(control) {
|
|
|
|
case Control::P0: stepper_mask_ = (stepper_mask_ & 0xe) | (on ? 0x1 : 0x0); break;
|
|
|
|
case Control::P1: stepper_mask_ = (stepper_mask_ & 0xd) | (on ? 0x2 : 0x0); break;
|
|
|
|
case Control::P2: stepper_mask_ = (stepper_mask_ & 0xb) | (on ? 0x4 : 0x0); break;
|
|
|
|
case Control::P3: stepper_mask_ = (stepper_mask_ & 0x7) | (on ? 0x8 : 0x0); break;
|
|
|
|
|
|
|
|
case Control::Motor:
|
2018-05-17 01:44:09 +00:00
|
|
|
motor_is_enabled_ = on;
|
|
|
|
drives_[active_drive_].set_motor_on(on);
|
2018-05-18 01:39:11 +00:00
|
|
|
return;
|
2018-04-25 02:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the stepper magnet selections have changed, and any is on, see how
|
|
|
|
// that moves the head.
|
|
|
|
if(previous_stepper_mask ^ stepper_mask_ && stepper_mask_) {
|
|
|
|
// Convert from a representation of bits set to the centre of pull.
|
2018-04-26 01:59:18 +00:00
|
|
|
int direction = 0;
|
|
|
|
if(stepper_mask_&1) direction += (((stepper_position_ - 0) + 4)&7) - 4;
|
|
|
|
if(stepper_mask_&2) direction += (((stepper_position_ - 2) + 4)&7) - 4;
|
|
|
|
if(stepper_mask_&4) direction += (((stepper_position_ - 4) + 4)&7) - 4;
|
|
|
|
if(stepper_mask_&8) direction += (((stepper_position_ - 6) + 4)&7) - 4;
|
2018-04-25 02:44:45 +00:00
|
|
|
const int bits_set = (stepper_mask_&1) + ((stepper_mask_ >> 1)&1) + ((stepper_mask_ >> 2)&1) + ((stepper_mask_ >> 3)&1);
|
2018-04-26 01:59:18 +00:00
|
|
|
direction /= bits_set;
|
2018-04-25 02:44:45 +00:00
|
|
|
|
|
|
|
// Compare to the stepper position to decide whether that pulls in the current cog notch,
|
|
|
|
// or grabs a later one.
|
2018-05-07 03:17:36 +00:00
|
|
|
drives_[active_drive_].step(Storage::Disk::HeadPosition(-direction, 4));
|
2018-04-26 01:59:18 +00:00
|
|
|
stepper_position_ = (stepper_position_ - direction + 8) & 7;
|
2018-04-25 02:44:45 +00:00
|
|
|
}
|
2018-04-24 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskII::select_drive(int drive) {
|
2018-05-17 01:44:09 +00:00
|
|
|
if((drive&1) == active_drive_) return;
|
|
|
|
|
2018-04-25 02:44:45 +00:00
|
|
|
drives_[active_drive_].set_event_delegate(this);
|
|
|
|
drives_[active_drive_^1].set_event_delegate(nullptr);
|
2018-05-17 01:44:09 +00:00
|
|
|
|
|
|
|
drives_[active_drive_].set_motor_on(false);
|
|
|
|
active_drive_ = drive & 1;
|
|
|
|
drives_[active_drive_].set_motor_on(motor_is_enabled_);
|
2018-04-24 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 03:01:11 +00:00
|
|
|
// The read pulse is controlled by a special IC that outputs a 1us pulse for every field reversal on the disk.
|
|
|
|
|
2018-04-24 05:11:31 +00:00
|
|
|
void DiskII::run_for(const Cycles cycles) {
|
2018-05-28 03:17:06 +00:00
|
|
|
if(preferred_clocking() == ClockingHint::Preference::None) return;
|
2018-04-24 05:29:36 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
auto integer_cycles = cycles.as_integral();
|
2018-05-28 21:19:29 +00:00
|
|
|
while(integer_cycles--) {
|
|
|
|
const int address = (state_ & 0xf0) | inputs_ | ((shift_register_&0x80) >> 6);
|
2018-08-01 03:01:11 +00:00
|
|
|
if(flux_duration_) {
|
|
|
|
--flux_duration_;
|
|
|
|
if(!flux_duration_) inputs_ |= input_flux;
|
|
|
|
}
|
2018-05-28 21:19:29 +00:00
|
|
|
state_ = state_machine_[static_cast<std::size_t>(address)];
|
|
|
|
switch(state_ & 0xf) {
|
|
|
|
default: shift_register_ = 0; break; // clear
|
|
|
|
case 0x8: break; // nop
|
|
|
|
|
|
|
|
case 0x9: shift_register_ = static_cast<uint8_t>(shift_register_ << 1); break; // shift left, bringing in a zero
|
|
|
|
case 0xd: shift_register_ = static_cast<uint8_t>((shift_register_ << 1) | 1); break; // shift left, bringing in a one
|
|
|
|
|
|
|
|
case 0xa: // shift right, bringing in write protected status
|
|
|
|
shift_register_ = (shift_register_ >> 1) | (is_write_protected() ? 0x80 : 0x00);
|
|
|
|
|
|
|
|
// If the controller is in the sense write protect loop but the register will never change,
|
|
|
|
// short circuit further work and return now.
|
2018-05-28 21:48:35 +00:00
|
|
|
if(shift_register_ == (is_write_protected() ? 0xff : 0x00)) {
|
2018-05-28 21:19:29 +00:00
|
|
|
if(!drive_is_sleeping_[0]) drives_[0].run_for(Cycles(integer_cycles));
|
|
|
|
if(!drive_is_sleeping_[1]) drives_[1].run_for(Cycles(integer_cycles));
|
|
|
|
decide_clocking_preference();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xb: shift_register_ = data_input_; break; // load data register from data bus
|
2018-04-24 15:29:05 +00:00
|
|
|
}
|
2018-05-28 21:19:29 +00:00
|
|
|
|
|
|
|
// Currently writing?
|
|
|
|
if(inputs_&input_mode) {
|
|
|
|
// state_ & 0x80 should be the current level sent to the disk;
|
|
|
|
// therefore transitions in that bit should become flux transitions
|
|
|
|
drives_[active_drive_].write_bit(!!((state_ ^ address) & 0x80));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: surely there's a less heavyweight solution than inline updates?
|
|
|
|
if(!drive_is_sleeping_[0]) drives_[0].run_for(Cycles(1));
|
|
|
|
if(!drive_is_sleeping_[1]) drives_[1].run_for(Cycles(1));
|
2018-04-29 21:51:10 +00:00
|
|
|
}
|
2018-04-24 16:03:03 +00:00
|
|
|
|
2018-08-04 01:13:18 +00:00
|
|
|
// Per comp.sys.apple2.programmer there is a delay between the controller
|
|
|
|
// motor switch being flipped and the drive motor actually switching off.
|
|
|
|
// This models that, accepting overrun as a risk.
|
|
|
|
if(motor_off_time_ >= 0) {
|
2019-10-30 02:36:29 +00:00
|
|
|
motor_off_time_ -= cycles.as_integral();
|
2018-08-04 01:13:18 +00:00
|
|
|
if(motor_off_time_ < 0) {
|
|
|
|
set_control(Control::Motor, false);
|
|
|
|
}
|
|
|
|
}
|
2018-05-28 21:19:29 +00:00
|
|
|
decide_clocking_preference();
|
2018-04-29 21:51:10 +00:00
|
|
|
}
|
2018-04-25 02:44:45 +00:00
|
|
|
|
2018-05-28 21:19:29 +00:00
|
|
|
void DiskII::decide_clocking_preference() {
|
|
|
|
ClockingHint::Preference prior_preference = clocking_preference_;
|
|
|
|
|
|
|
|
// If in read mode, clocking is either:
|
|
|
|
//
|
|
|
|
// just-in-time, if drives are running or the shift register has any 1s in it or a flux event hasn't yet passed; or
|
2018-06-03 12:11:17 +00:00
|
|
|
// none, given that drives are not running, the shift register has already emptied and there's no flux about to be received.
|
2018-05-28 21:19:29 +00:00
|
|
|
if(!(inputs_ & ~input_flux)) {
|
2018-06-03 12:11:17 +00:00
|
|
|
clocking_preference_ = (!motor_is_enabled_ && !shift_register_ && (inputs_&input_flux)) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime;
|
2018-05-28 21:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If in writing mode, clocking is real time.
|
|
|
|
if(inputs_ & input_mode) {
|
|
|
|
clocking_preference_ = ClockingHint::Preference::RealTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If in sense-write-protect mode, clocking is just-in-time if the shift register hasn't yet filled with the value that
|
|
|
|
// corresponds to the current write protect status. Otherwise it is none.
|
|
|
|
if((inputs_ & ~input_flux) == input_command) {
|
|
|
|
clocking_preference_ = (shift_register_ == (is_write_protected() ? 0xff : 0x00)) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Announce a change if there was one.
|
|
|
|
if(prior_preference != clocking_preference_)
|
2018-05-28 03:17:06 +00:00
|
|
|
update_clocking_observer();
|
2018-04-24 05:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskII::is_write_protected() {
|
2018-05-17 01:44:09 +00:00
|
|
|
return !!(stepper_mask_ & 2) | drives_[active_drive_].get_is_read_only();
|
2018-04-24 05:11:31 +00:00
|
|
|
}
|
2018-04-24 15:29:05 +00:00
|
|
|
|
|
|
|
void DiskII::set_state_machine(const std::vector<uint8_t> &state_machine) {
|
2018-05-10 02:03:59 +00:00
|
|
|
/*
|
|
|
|
An unadulterated P6 ROM read returns values with an address formed as:
|
|
|
|
|
|
|
|
state b0, state b2, state b3, pulse, Q7, Q6, shift, state b1
|
|
|
|
|
2018-05-19 02:07:58 +00:00
|
|
|
... and has the top nibble of each value stored in the ROM reflected.
|
|
|
|
Beneath Apple Pro-DOS uses a different order and several of the
|
|
|
|
online copies are reformatted into that order.
|
2018-05-10 02:03:59 +00:00
|
|
|
|
|
|
|
So the code below remaps into Beneath Apple Pro-DOS order if the
|
|
|
|
supplied state machine isn't already in that order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(state_machine[0] != 0x18) {
|
|
|
|
for(size_t source_address = 0; source_address < 256; ++source_address) {
|
|
|
|
// Remap into Beneath Apple Pro-DOS address form.
|
|
|
|
size_t destination_address =
|
|
|
|
((source_address&0x80) ? 0x10 : 0x00) |
|
|
|
|
((source_address&0x01) ? 0x20 : 0x00) |
|
|
|
|
((source_address&0x40) ? 0x40 : 0x00) |
|
|
|
|
((source_address&0x20) ? 0x80 : 0x00) |
|
|
|
|
((source_address&0x10) ? 0x01 : 0x00) |
|
|
|
|
((source_address&0x08) ? 0x08 : 0x00) |
|
|
|
|
((source_address&0x04) ? 0x04 : 0x00) |
|
|
|
|
((source_address&0x02) ? 0x02 : 0x00);
|
|
|
|
uint8_t source_value = state_machine[source_address];
|
|
|
|
|
|
|
|
// Remap into Beneath Apple Pro-DOS value form.
|
|
|
|
source_value =
|
|
|
|
((source_value & 0x80) ? 0x10 : 0x0) |
|
|
|
|
((source_value & 0x40) ? 0x20 : 0x0) |
|
|
|
|
((source_value & 0x20) ? 0x40 : 0x0) |
|
|
|
|
((source_value & 0x10) ? 0x80 : 0x0) |
|
|
|
|
(source_value & 0x0f);
|
|
|
|
|
|
|
|
// Store.
|
|
|
|
state_machine_[destination_address] = source_value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(&state_machine_[0], &state_machine[0], 128);
|
|
|
|
}
|
2018-04-24 15:29:05 +00:00
|
|
|
}
|
2018-04-25 02:44:45 +00:00
|
|
|
|
|
|
|
void DiskII::set_disk(const std::shared_ptr<Storage::Disk::Disk> &disk, int drive) {
|
|
|
|
drives_[drive].set_disk(disk);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
void DiskII::process_event(const Storage::Disk::Drive::Event &event) {
|
2018-04-25 02:44:45 +00:00
|
|
|
if(event.type == Storage::Disk::Track::Event::FluxTransition) {
|
|
|
|
inputs_ &= ~input_flux;
|
2018-08-01 03:01:11 +00:00
|
|
|
flux_duration_ = 2; // Upon detection of a flux transition, the flux flag should stay set for 1us. Emulate that as two cycles.
|
2018-05-28 21:19:29 +00:00
|
|
|
decide_clocking_preference();
|
2018-04-25 02:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 21:51:10 +00:00
|
|
|
|
2018-05-28 03:17:06 +00:00
|
|
|
void DiskII::set_component_prefers_clocking(ClockingHint::Source *component, ClockingHint::Preference preference) {
|
|
|
|
drive_is_sleeping_[0] = drives_[0].preferred_clocking() == ClockingHint::Preference::None;
|
|
|
|
drive_is_sleeping_[1] = drives_[1].preferred_clocking() == ClockingHint::Preference::None;
|
2018-05-28 21:19:29 +00:00
|
|
|
decide_clocking_preference();
|
2018-04-29 21:51:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 03:17:06 +00:00
|
|
|
ClockingHint::Preference DiskII::preferred_clocking() {
|
2018-05-28 21:19:29 +00:00
|
|
|
return clocking_preference_;
|
2018-04-29 21:51:10 +00:00
|
|
|
}
|
2018-05-09 02:05:43 +00:00
|
|
|
|
2018-05-18 01:39:11 +00:00
|
|
|
void DiskII::set_data_input(uint8_t input) {
|
|
|
|
data_input_ = input;
|
2018-05-09 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 01:39:11 +00:00
|
|
|
int DiskII::read_address(int address) {
|
2018-05-09 02:05:43 +00:00
|
|
|
switch(address & 0xf) {
|
|
|
|
default:
|
|
|
|
case 0x0: set_control(Control::P0, false); break;
|
|
|
|
case 0x1: set_control(Control::P0, true); break;
|
|
|
|
case 0x2: set_control(Control::P1, false); break;
|
|
|
|
case 0x3: set_control(Control::P1, true); break;
|
|
|
|
case 0x4: set_control(Control::P2, false); break;
|
|
|
|
case 0x5: set_control(Control::P2, true); break;
|
|
|
|
case 0x6: set_control(Control::P3, false); break;
|
|
|
|
case 0x7: set_control(Control::P3, true); break;
|
|
|
|
|
2018-05-18 01:39:11 +00:00
|
|
|
case 0x8:
|
|
|
|
shift_register_ = 0;
|
2018-08-04 01:13:18 +00:00
|
|
|
motor_off_time_ = clock_rate_;
|
|
|
|
break;
|
|
|
|
case 0x9:
|
|
|
|
set_control(Control::Motor, true);
|
|
|
|
motor_off_time_ = -1;
|
2018-05-18 01:39:11 +00:00
|
|
|
break;
|
2018-05-09 02:05:43 +00:00
|
|
|
|
|
|
|
case 0xa: select_drive(0); break;
|
|
|
|
case 0xb: select_drive(1); break;
|
|
|
|
|
2018-05-18 01:39:11 +00:00
|
|
|
case 0xc: inputs_ &= ~input_command; break;
|
|
|
|
case 0xd: inputs_ |= input_command; break;
|
2018-05-19 02:07:58 +00:00
|
|
|
case 0xe:
|
|
|
|
if(inputs_ & input_mode)
|
|
|
|
drives_[active_drive_].end_writing();
|
|
|
|
inputs_ &= ~input_mode;
|
|
|
|
break;
|
|
|
|
case 0xf:
|
|
|
|
if(!(inputs_ & input_mode))
|
2019-10-30 02:36:29 +00:00
|
|
|
drives_[active_drive_].begin_writing(Storage::Time(1, int(clock_rate_)), false);
|
2018-05-19 02:07:58 +00:00
|
|
|
inputs_ |= input_mode;
|
|
|
|
break;
|
2018-05-09 02:05:43 +00:00
|
|
|
}
|
2018-05-28 21:19:29 +00:00
|
|
|
decide_clocking_preference();
|
2018-05-18 00:18:34 +00:00
|
|
|
return (address & 1) ? 0xff : shift_register_;
|
2018-05-09 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 02:17:13 +00:00
|
|
|
void DiskII::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
drives_[0].set_activity_observer(observer, "Drive 1", true);
|
|
|
|
drives_[1].set_activity_observer(observer, "Drive 2", true);
|
|
|
|
}
|
2018-06-09 16:51:53 +00:00
|
|
|
|
|
|
|
Storage::Disk::Drive &DiskII::get_drive(int index) {
|
|
|
|
return drives_[index];
|
|
|
|
}
|