2019-08-14 03:09:11 +00:00
|
|
|
//
|
|
|
|
// SCSI.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/08/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SCSI.hpp"
|
|
|
|
|
|
|
|
using namespace SCSI;
|
|
|
|
|
2019-09-02 17:00:01 +00:00
|
|
|
Bus::Bus(HalfCycles clock_rate) {
|
2019-10-30 02:36:29 +00:00
|
|
|
cycles_to_time_ = 1.0 / double(clock_rate.as_integral());
|
2019-09-02 17:00:01 +00:00
|
|
|
|
|
|
|
// NB: note that the dispatch times below are **ORDERED**
|
|
|
|
// from least to greatest. Each box should contain the number
|
|
|
|
// of whole clock periods it will take to get the the first
|
|
|
|
// discrete moment after the required delay interval has been met.
|
2019-10-30 02:36:29 +00:00
|
|
|
using IntType = Cycles::IntType;
|
|
|
|
dispatch_times_[0] = 1 + IntType(CableSkew / cycles_to_time_);
|
|
|
|
dispatch_times_[1] = 1 + IntType(DeskewDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[2] = 1 + IntType(BusFreeDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[3] = 1 + IntType(BusSettleDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[4] = 1 + IntType(BusClearDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[5] = 1 + IntType(BusSetDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[6] = 1 + IntType(ArbitrationDelay / cycles_to_time_);
|
|
|
|
dispatch_times_[7] = 1 + IntType(ResetHoldTime / cycles_to_time_);
|
2019-09-02 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 03:09:11 +00:00
|
|
|
size_t Bus::add_device() {
|
|
|
|
const auto slot = device_states_.size();
|
|
|
|
device_states_.push_back(DefaultBusState);
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::set_device_output(size_t device, BusState output) {
|
2019-08-18 03:43:42 +00:00
|
|
|
if(device_states_[device] == output) return;
|
2019-08-14 03:09:11 +00:00
|
|
|
device_states_[device] = output;
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
const auto previous_state = state_;
|
2019-08-14 03:09:11 +00:00
|
|
|
state_ = DefaultBusState;
|
|
|
|
for(auto state: device_states_) {
|
2019-08-16 03:14:40 +00:00
|
|
|
state_ |= state;
|
2019-08-14 03:09:11 +00:00
|
|
|
}
|
2019-08-18 03:43:42 +00:00
|
|
|
if(state_ == previous_state) return;
|
|
|
|
|
2019-09-19 23:31:22 +00:00
|
|
|
if(activity_observer_ && (state_^previous_state)&SCSI::Line::Busy) {
|
|
|
|
activity_observer_->set_led_status("SCSI", state_&SCSI::Line::Busy);
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:30:04 +00:00
|
|
|
// printf("SCSI bus: %02x %c%c%c%c%c%c%c%c%c%c\n",
|
|
|
|
// state_ & 0xff,
|
|
|
|
// (state_ & Line::Parity) ? 'p' : '-',
|
|
|
|
// (state_ & Line::SelectTarget) ? 's' : '-',
|
|
|
|
// (state_ & Line::Attention) ? 't' : '-',
|
|
|
|
// (state_ & Line::Control) ? 'c' : '-',
|
|
|
|
// (state_ & Line::Busy) ? 'b' : '-',
|
|
|
|
// (state_ & Line::Acknowledge) ? 'a' : '-',
|
|
|
|
// (state_ & Line::Reset) ? 'r' : '-',
|
|
|
|
// (state_ & Line::Input) ? 'i' : '-',
|
|
|
|
// (state_ & Line::Message) ? 'm' : '-',
|
|
|
|
// (state_ & Line::Request) ? 'q' : '-'
|
|
|
|
// );
|
2019-08-18 03:43:42 +00:00
|
|
|
|
2019-09-02 17:00:01 +00:00
|
|
|
bool was_asleep = preferred_clocking() == ClockingHint::Preference::None;
|
|
|
|
dispatch_index_ = 0;
|
|
|
|
time_in_state_ = HalfCycles(0);
|
|
|
|
if(was_asleep) update_clocking_observer();
|
2019-08-18 03:43:42 +00:00
|
|
|
}
|
2019-09-19 23:31:22 +00:00
|
|
|
|
|
|
|
void Bus::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
activity_observer_ = observer;
|
|
|
|
activity_observer_->register_led("SCSI");
|
|
|
|
}
|
2019-08-14 03:09:11 +00:00
|
|
|
|
2022-09-15 20:13:54 +00:00
|
|
|
BusState Bus::get_state() const {
|
2019-08-14 03:09:11 +00:00
|
|
|
return state_;
|
|
|
|
}
|
2019-08-18 03:43:42 +00:00
|
|
|
|
|
|
|
void Bus::add_observer(Observer *observer) {
|
|
|
|
observers_.push_back(observer);
|
|
|
|
}
|
2019-09-02 17:00:01 +00:00
|
|
|
|
2020-05-10 01:22:51 +00:00
|
|
|
ClockingHint::Preference Bus::preferred_clocking() const {
|
2019-09-02 20:03:33 +00:00
|
|
|
return (dispatch_index_ < dispatch_times_.size()) ? ClockingHint::Preference::RealTime : ClockingHint::Preference::None;
|
2019-09-02 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 00:17:47 +00:00
|
|
|
void Bus::update_observers() {
|
2019-10-30 02:36:29 +00:00
|
|
|
const auto time_elapsed = double(time_in_state_.as_integral()) * cycles_to_time_;
|
2019-09-19 00:17:47 +00:00
|
|
|
for(auto &observer: observers_) {
|
|
|
|
observer->scsi_bus_did_change(this, state_, time_elapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:00:01 +00:00
|
|
|
void Bus::run_for(HalfCycles time) {
|
2019-09-02 20:03:33 +00:00
|
|
|
if(dispatch_index_ < dispatch_times_.size()) {
|
2019-09-02 17:00:01 +00:00
|
|
|
time_in_state_ += time;
|
|
|
|
|
|
|
|
const auto old_index = dispatch_index_;
|
2019-10-30 02:36:29 +00:00
|
|
|
const auto time_as_int = time_in_state_.as_integral();
|
2019-09-02 20:03:33 +00:00
|
|
|
while(time_as_int >= dispatch_times_[dispatch_index_] && dispatch_index_ < dispatch_times_.size()) {
|
2019-09-02 17:00:01 +00:00
|
|
|
++dispatch_index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dispatch_index_ != old_index) {
|
2019-09-19 00:17:47 +00:00
|
|
|
update_observers();
|
2019-09-02 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(preferred_clocking() == ClockingHint::Preference::None) {
|
|
|
|
update_clocking_observer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|