2019-08-20 02:47:01 +00:00
|
|
|
//
|
|
|
|
// TargetImplementation.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/08/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
template <typename Executor> Target<Executor>::Target(Bus &bus, int scsi_id) :
|
|
|
|
bus_(bus),
|
|
|
|
scsi_id_mask_(BusState(1 << scsi_id)),
|
|
|
|
scsi_bus_device_id_(bus.add_device()) {
|
|
|
|
bus.add_observer(this);
|
|
|
|
}
|
|
|
|
|
2019-09-01 01:44:22 +00:00
|
|
|
template <typename Executor> void Target<Executor>::scsi_bus_did_change(Bus *, BusState new_state, double time_since_change) {
|
2019-08-20 02:47:01 +00:00
|
|
|
/*
|
|
|
|
"The target determines that it is selected when the SEL# signal
|
|
|
|
and its SCSI ID bit are active and the BSY# and I#/O signals
|
2021-01-31 23:36:29 +00:00
|
|
|
are false. It then asserts the signal within a selection
|
|
|
|
abort time."
|
2019-08-20 02:47:01 +00:00
|
|
|
*/
|
|
|
|
|
2019-09-16 02:06:45 +00:00
|
|
|
// Wait for deskew, at the very least.
|
|
|
|
if(time_since_change < SCSI::DeskewDelay) return;
|
|
|
|
|
2019-08-20 02:47:01 +00:00
|
|
|
// A reset always takes precedence over anything else ongoing.
|
|
|
|
if(new_state & Line::Reset) {
|
|
|
|
phase_ = Phase::AwaitingSelection;
|
|
|
|
bus_state_ = DefaultBusState;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-20 02:47:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(phase_) {
|
2019-08-25 21:46:05 +00:00
|
|
|
/*
|
|
|
|
While awaiting selection the SCSI target is passively watching the bus waiting for its ID
|
|
|
|
to be set during a target selection. It will segue automatically from there to the command
|
|
|
|
phase regardless of its executor.
|
|
|
|
*/
|
2019-08-20 02:47:01 +00:00
|
|
|
case Phase::AwaitingSelection:
|
|
|
|
if(
|
|
|
|
(new_state & scsi_id_mask_) &&
|
|
|
|
((new_state & (Line::SelectTarget | Line::Busy | Line::Input)) == Line::SelectTarget)
|
|
|
|
) {
|
|
|
|
phase_ = Phase::Command;
|
2019-08-25 21:43:14 +00:00
|
|
|
command_.resize(0);
|
|
|
|
command_pointer_ = 0;
|
2019-08-20 02:47:01 +00:00
|
|
|
bus_state_ |= Line::Busy; // Initiate the command phase: request a command byte.
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-20 02:47:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-08-25 21:46:05 +00:00
|
|
|
/*
|
|
|
|
In the command phase, the target will stream an appropriate number of bytes for the command
|
|
|
|
it is being offered, before giving the executor a chance to handle the command. If the target
|
|
|
|
supports this command, it becomes responsible for the appropriate next phase transition. If it
|
|
|
|
reports that it doesn't support that command, a suitable response is automatically dispatched.
|
|
|
|
*/
|
2019-08-20 02:47:01 +00:00
|
|
|
case Phase::Command:
|
|
|
|
// Wait for select to be disabled before beginning the control phase proper.
|
|
|
|
if((new_state & Line::SelectTarget)) return;
|
|
|
|
|
|
|
|
bus_state_ |= Line::Control;
|
|
|
|
|
|
|
|
switch(new_state & (Line::Request | Line::Acknowledge)) {
|
|
|
|
// If request and acknowledge are both enabled, grab a byte and cancel the request.
|
|
|
|
case Line::Request | Line::Acknowledge:
|
|
|
|
bus_state_ &= ~Line::Request;
|
|
|
|
|
|
|
|
if(command_.empty()) {
|
|
|
|
begin_command(uint8_t(new_state));
|
|
|
|
|
|
|
|
// TODO: if(command_.empty()) signal_error_somehow();
|
|
|
|
} else {
|
|
|
|
command_[command_pointer_] = uint8_t(new_state);
|
|
|
|
++command_pointer_;
|
|
|
|
if(command_pointer_ == command_.size()) {
|
2019-09-08 02:04:44 +00:00
|
|
|
if(!dispatch_command()) {
|
|
|
|
// This is just a guess for now; I don't know how SCSI
|
|
|
|
// devices are supposed to respond if they don't support
|
|
|
|
// a command.
|
|
|
|
terminate_command(Responder::Status::TaskAborted);
|
|
|
|
}
|
2019-08-20 02:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The reset of request has caused the initiator to reset acknowledge, so it is now
|
|
|
|
// safe to request the next byte.
|
|
|
|
case 0:
|
|
|
|
bus_state_ |= Line::Request;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-20 02:47:01 +00:00
|
|
|
break;
|
2019-08-23 03:16:58 +00:00
|
|
|
|
|
|
|
case Phase::ReceivingData:
|
|
|
|
switch(new_state & (Line::Request | Line::Acknowledge)) {
|
|
|
|
case Line::Request | Line::Acknowledge:
|
|
|
|
bus_state_ &= ~Line::Request;
|
|
|
|
|
|
|
|
data_[data_pointer_] = uint8_t(new_state);
|
|
|
|
++data_pointer_;
|
2019-09-07 17:17:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2019-08-23 03:16:58 +00:00
|
|
|
if(data_pointer_ == data_.size()) {
|
2019-09-13 01:58:09 +00:00
|
|
|
next_function_(CommandState(command_, data_), *this);
|
2019-09-08 02:04:44 +00:00
|
|
|
} else {
|
|
|
|
bus_state_ |= Line::Request;
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Phase::SendingData:
|
2019-09-04 01:15:30 +00:00
|
|
|
case Phase::SendingStatus:
|
|
|
|
case Phase::SendingMessage:
|
2019-08-23 03:16:58 +00:00
|
|
|
switch(new_state & (Line::Request | Line::Acknowledge)) {
|
|
|
|
case Line::Request | Line::Acknowledge:
|
|
|
|
bus_state_ &= ~(Line::Request | 0xff);
|
|
|
|
|
|
|
|
++data_pointer_;
|
2019-09-07 17:17:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2019-09-04 01:15:30 +00:00
|
|
|
if(
|
2019-09-07 17:17:34 +00:00
|
|
|
(phase_ == Phase::SendingMessage && data_pointer_ == 1) ||
|
|
|
|
(phase_ == Phase::SendingStatus && data_pointer_ == 1) ||
|
2019-09-04 01:15:30 +00:00
|
|
|
(phase_ == Phase::SendingData && data_pointer_ == data_.size())
|
|
|
|
) {
|
2019-09-13 01:58:09 +00:00
|
|
|
next_function_(CommandState(command_, data_), *this);
|
2019-09-08 02:04:44 +00:00
|
|
|
} else {
|
|
|
|
bus_state_ |= Line::Request;
|
|
|
|
bus_state_ &= ~0xff;
|
|
|
|
|
|
|
|
switch(phase_) {
|
|
|
|
case Phase::SendingData: bus_state_ |= data_[data_pointer_]; break;
|
|
|
|
case Phase::SendingStatus: bus_state_ |= BusState(status_); break;
|
|
|
|
default:
|
|
|
|
case Phase::SendingMessage: bus_state_ |= BusState(message_); break;
|
|
|
|
}
|
2019-09-04 01:15:30 +00:00
|
|
|
}
|
2019-08-23 03:16:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
break;
|
2019-08-20 02:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Executor> void Target<Executor>::begin_command(uint8_t first_byte) {
|
|
|
|
// The logic below is valid for SCSI-1. TODO: other SCSIs.
|
|
|
|
switch(first_byte >> 5) {
|
|
|
|
default: break;
|
|
|
|
case 0: command_.resize(6); break; // Group 0 commands: 6 bytes long.
|
|
|
|
case 1: command_.resize(10); break; // Group 1 commands: 10 bytes long.
|
|
|
|
case 5: command_.resize(12); break; // Group 5 commands: 12 bytes long.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the first byte if it was recognised.
|
|
|
|
if(!command_.empty()) {
|
|
|
|
command_[0] = first_byte;
|
|
|
|
command_pointer_ = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Executor> bool Target<Executor>::dispatch_command() {
|
|
|
|
|
2019-09-13 01:58:09 +00:00
|
|
|
CommandState arguments(command_, data_);
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
#define G0(x) x
|
|
|
|
#define G1(x) (0x20|x)
|
|
|
|
#define G5(x) (0xa0|x)
|
|
|
|
|
2019-09-19 01:41:29 +00:00
|
|
|
LOG("---Command " << PADHEX(2) << int(command_[0]) << "---");
|
2019-09-08 02:04:44 +00:00
|
|
|
|
2019-08-20 02:47:01 +00:00
|
|
|
switch(command_[0]) {
|
|
|
|
default: return false;
|
|
|
|
|
2019-08-25 21:03:41 +00:00
|
|
|
case G0(0x00): return executor_.test_unit_ready(arguments, *this);
|
|
|
|
case G0(0x01): return executor_.rezero_unit(arguments, *this);
|
|
|
|
case G0(0x03): return executor_.request_sense(arguments, *this);
|
|
|
|
case G0(0x04): return executor_.format_unit(arguments, *this);
|
|
|
|
case G0(0x08): return executor_.read(arguments, *this);
|
|
|
|
case G0(0x0a): return executor_.write(arguments, *this);
|
|
|
|
case G0(0x0b): return executor_.seek(arguments, *this);
|
2019-09-12 01:52:02 +00:00
|
|
|
case G0(0x12): return executor_.inquiry(arguments, *this);
|
2019-09-13 01:58:09 +00:00
|
|
|
case G0(0x15): return executor_.mode_select(arguments, *this);
|
2019-08-25 21:03:41 +00:00
|
|
|
case G0(0x16): return executor_.reserve_unit(arguments, *this);
|
|
|
|
case G0(0x17): return executor_.release_unit(arguments, *this);
|
2019-09-12 01:52:02 +00:00
|
|
|
case G0(0x1a): return executor_.mode_sense(arguments, *this);
|
2019-08-25 21:03:41 +00:00
|
|
|
case G0(0x1c): return executor_.read_diagnostic(arguments, *this);
|
|
|
|
case G0(0x1d): return executor_.write_diagnostic(arguments, *this);
|
|
|
|
|
|
|
|
case G1(0x05): return executor_.read_capacity(arguments, *this);
|
|
|
|
case G1(0x08): return executor_.read(arguments, *this);
|
|
|
|
case G1(0x0a): return executor_.write(arguments, *this);
|
|
|
|
case G1(0x0e): return executor_.write_and_verify(arguments, *this);
|
|
|
|
case G1(0x0f): return executor_.verify(arguments, *this);
|
|
|
|
case G1(0x11): return executor_.search_data_equal(arguments, *this);
|
|
|
|
case G1(0x10): return executor_.search_data_high(arguments, *this);
|
|
|
|
case G1(0x12): return executor_.search_data_low(arguments, *this);
|
2019-09-12 01:52:02 +00:00
|
|
|
case G1(0x1c): return executor_.read_buffer(arguments, *this);
|
2019-09-13 01:58:09 +00:00
|
|
|
case G1(0x15): return executor_.mode_select(arguments, *this);
|
|
|
|
|
2019-08-25 21:03:41 +00:00
|
|
|
|
|
|
|
case G5(0x09): return executor_.set_block_limits(arguments, *this);
|
2019-08-20 02:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef G0
|
|
|
|
#undef G1
|
|
|
|
#undef G5
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-22 02:37:39 +00:00
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
template <typename Executor> void Target<Executor>::send_data(std::vector<uint8_t> &&data, continuation next) {
|
|
|
|
// Data out phase: control and message all reset, input set.
|
|
|
|
bus_state_ &= ~(Line::Control | Line::Input | Line::Message);
|
|
|
|
bus_state_ |= Line::Input;
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
phase_ = Phase::SendingData;
|
2019-09-03 03:14:37 +00:00
|
|
|
next_function_ = next;
|
2019-08-23 03:16:58 +00:00
|
|
|
data_ = std::move(data);
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
data_pointer_ = 0;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Executor> void Target<Executor>::receive_data(size_t length, continuation next) {
|
|
|
|
// Data out phase: control, input and message all reset.
|
|
|
|
bus_state_ &= ~(Line::Control | Line::Input | Line::Message);
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
phase_ = Phase::ReceivingData;
|
2019-09-03 03:14:37 +00:00
|
|
|
next_function_ = next;
|
2019-08-23 03:16:58 +00:00
|
|
|
data_.resize(length);
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
data_pointer_ = 0;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 01:15:30 +00:00
|
|
|
template <typename Executor> void Target<Executor>::send_status(Status status, continuation next) {
|
2019-08-23 03:16:58 +00:00
|
|
|
// Status phase: message reset, control and input set.
|
|
|
|
bus_state_ &= ~(Line::Control | Line::Input | Line::Message);
|
|
|
|
bus_state_ |= Line::Input | Line::Control;
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-09-04 01:15:30 +00:00
|
|
|
status_ = status;
|
2019-08-23 03:16:58 +00:00
|
|
|
phase_ = Phase::SendingStatus;
|
2019-09-03 03:14:37 +00:00
|
|
|
next_function_ = next;
|
|
|
|
|
2019-09-07 17:17:34 +00:00
|
|
|
data_pointer_ = 0;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 01:15:30 +00:00
|
|
|
template <typename Executor> void Target<Executor>::send_message(Message message, continuation next) {
|
2019-09-07 17:35:38 +00:00
|
|
|
// Message in phase: message, control and input set.
|
|
|
|
bus_state_ |= Line::Message | Line::Control | Line::Input;
|
2019-09-03 03:14:37 +00:00
|
|
|
|
2019-09-04 01:15:30 +00:00
|
|
|
message_ = message;
|
2019-08-23 03:16:58 +00:00
|
|
|
phase_ = Phase::SendingMessage;
|
2019-09-03 03:14:37 +00:00
|
|
|
next_function_ = next;
|
|
|
|
|
2019-09-07 17:17:34 +00:00
|
|
|
data_pointer_ = 0;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Executor> void Target<Executor>::end_command() {
|
|
|
|
// TODO: was this a linked command?
|
|
|
|
|
|
|
|
// Release all bus lines and return to awaiting selection.
|
|
|
|
phase_ = Phase::AwaitingSelection;
|
|
|
|
bus_state_ = DefaultBusState;
|
2019-09-01 01:44:22 +00:00
|
|
|
set_device_output(bus_state_);
|
2019-09-04 01:15:30 +00:00
|
|
|
|
2019-09-19 01:41:29 +00:00
|
|
|
LOG("---Done---");
|
2019-08-23 03:16:58 +00:00
|
|
|
}
|