2019-08-11 03:53:52 +00:00
|
|
|
//
|
|
|
|
// ncr5380.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/08/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ncr5380.hpp"
|
|
|
|
|
2019-08-12 00:55:20 +00:00
|
|
|
#include "../../Outputs/Log.hpp"
|
|
|
|
|
2019-08-11 03:53:52 +00:00
|
|
|
using namespace NCR::NCR5380;
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
NCR5380::NCR5380(int clock_rate) :
|
|
|
|
device_(bus_, 6),
|
|
|
|
clock_rate_(clock_rate) {
|
2019-08-14 03:09:11 +00:00
|
|
|
device_id_ = bus_.add_device();
|
|
|
|
}
|
|
|
|
|
2019-08-11 03:53:52 +00:00
|
|
|
void NCR5380::write(int address, uint8_t value) {
|
2019-08-19 03:15:54 +00:00
|
|
|
using SCSI::Line;
|
2019-08-12 00:55:20 +00:00
|
|
|
switch(address & 7) {
|
|
|
|
case 0:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 0] Set current SCSI bus state to " << PADHEX(2) << int(value));
|
2019-08-14 03:09:11 +00:00
|
|
|
data_bus_ = value;
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
|
2019-08-14 03:09:11 +00:00
|
|
|
case 1: {
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 1] Initiator command register set: " << PADHEX(2) << int(value));
|
2019-08-14 03:09:11 +00:00
|
|
|
initiator_command_ = value;
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
bus_output_ &= ~(Line::Reset | Line::Acknowledge | Line::Busy | Line::SelectTarget | Line::Attention);
|
|
|
|
if(value & 0x80) bus_output_ |= Line::Reset;
|
|
|
|
if(value & 0x10) bus_output_ |= Line::Acknowledge;
|
|
|
|
if(value & 0x08) bus_output_ |= Line::Busy;
|
|
|
|
if(value & 0x04) bus_output_ |= Line::SelectTarget;
|
|
|
|
if(value & 0x02) bus_output_ |= Line::Attention;
|
|
|
|
|
2019-08-14 03:09:11 +00:00
|
|
|
/* bit 5 = differential enable if this were a 5381 */
|
2019-08-18 03:43:42 +00:00
|
|
|
|
|
|
|
test_mode_ = value & 0x40;
|
2019-08-16 03:14:40 +00:00
|
|
|
assert_data_bus_ = value & 0x01;
|
2019-08-14 03:09:11 +00:00
|
|
|
} break;
|
2019-08-12 00:55:20 +00:00
|
|
|
|
|
|
|
case 2:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 2] Set mode: " << PADHEX(2) << int(value));
|
2019-08-12 00:55:20 +00:00
|
|
|
mode_ = value;
|
2019-08-14 03:09:11 +00:00
|
|
|
|
|
|
|
// bit 7: 1 = use block mode DMA mode (if DMA mode is also enabled)
|
|
|
|
// bit 6: 1 = be a SCSI target; 0 = be an initiator
|
|
|
|
// bit 5: 1 = check parity
|
|
|
|
// bit 4: 1 = generate an interrupt if parity checking is enabled and an error is found
|
|
|
|
// bit 3: 1 = generate an interrupt when an EOP is received from the DMA controller
|
|
|
|
// bit 2: 1 = generate an interrupt and reset low 6 bits of register 1 if an unexpected loss of Line::Busy occurs
|
|
|
|
// bit 1: 1 = use DMA mode
|
|
|
|
// bit 0: 1 = begin arbitration mode (device ID should be in register 0)
|
|
|
|
|
2019-08-16 03:14:40 +00:00
|
|
|
if(mode_ & 1) {
|
2019-08-19 02:39:27 +00:00
|
|
|
arbitration_in_progress_ = true;
|
2019-08-16 03:14:40 +00:00
|
|
|
if(state_ == ExecutionState::None) {
|
|
|
|
set_execution_state(ExecutionState::WatchingBusy);
|
|
|
|
lost_arbitration_ = false;
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-19 02:39:27 +00:00
|
|
|
arbitration_in_progress_ = false;
|
|
|
|
bus_output_ &= ~SCSI::Line::Busy;
|
2019-08-16 03:14:40 +00:00
|
|
|
set_execution_state(ExecutionState::None);
|
|
|
|
}
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
case 3: {
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 3] Set target command: " << PADHEX(2) << int(value));
|
2019-08-18 03:43:42 +00:00
|
|
|
bus_output_ &= ~(Line::Request | Line::Message | Line::Control | Line::Input);
|
|
|
|
if(value & 0x08) bus_output_ |= Line::Request;
|
|
|
|
if(value & 0x04) bus_output_ |= Line::Message;
|
|
|
|
if(value & 0x02) bus_output_ |= Line::Control;
|
|
|
|
if(value & 0x01) bus_output_ |= Line::Input;
|
|
|
|
} break;
|
2019-08-12 00:55:20 +00:00
|
|
|
|
|
|
|
case 4:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 4] Set select enabled: " << PADHEX(2) << int(value));
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 5] Start DMA send: " << PADHEX(2) << int(value));
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 6] Start DMA target receive: " << PADHEX(2) << int(value));
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 7] Start DMA initiator receive: " << PADHEX(2) << int(value));
|
2019-08-12 00:55:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-08-14 03:09:11 +00:00
|
|
|
|
|
|
|
// Data is output only if the data bus is asserted.
|
|
|
|
if(assert_data_bus_) {
|
2019-08-18 01:30:59 +00:00
|
|
|
bus_output_ |= data_bus_;
|
2019-08-14 03:09:11 +00:00
|
|
|
} else {
|
2019-08-18 01:30:59 +00:00
|
|
|
bus_output_ &= ~SCSI::Line::Data;
|
2019-08-14 03:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In test mode, still nothing is output. Otherwise throw out
|
|
|
|
// the current value of bus_output_.
|
|
|
|
if(test_mode_) {
|
|
|
|
bus_.set_device_output(device_id_, SCSI::DefaultBusState);
|
|
|
|
} else {
|
|
|
|
bus_.set_device_output(device_id_, bus_output_);
|
|
|
|
}
|
2019-08-11 03:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t NCR5380::read(int address) {
|
2019-08-19 03:15:54 +00:00
|
|
|
using SCSI::Line;
|
2019-08-12 00:55:20 +00:00
|
|
|
switch(address & 7) {
|
|
|
|
case 0:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 0] Get current SCSI bus state");
|
2019-08-14 03:09:11 +00:00
|
|
|
return uint8_t(bus_.get_state());
|
2019-08-12 00:55:20 +00:00
|
|
|
|
|
|
|
case 1:
|
2019-08-16 03:28:30 +00:00
|
|
|
LOG("[SCSI 1] Initiator command register get: " << (arbitration_in_progress_ ? 'p' : '-') << (lost_arbitration_ ? 'l' : '-'));
|
2019-08-16 03:14:40 +00:00
|
|
|
return
|
|
|
|
// Bits repeated as they were set.
|
|
|
|
(initiator_command_ & ~0x60) |
|
|
|
|
|
|
|
|
// Arbitration in progress.
|
|
|
|
(arbitration_in_progress_ ? 0x40 : 0x00) |
|
|
|
|
|
|
|
|
// Lost arbitration.
|
|
|
|
(lost_arbitration_ ? 0x20 : 0x00);
|
2019-08-12 00:55:20 +00:00
|
|
|
|
|
|
|
case 2:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 2] Get mode");
|
2019-08-12 00:55:20 +00:00
|
|
|
return mode_;
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
case 3: {
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 3] Get target command");
|
2019-08-18 03:43:42 +00:00
|
|
|
const auto bus_state = bus_.get_state();
|
|
|
|
return
|
|
|
|
((bus_state & SCSI::Line::Request) ? 0x08 : 0x00) |
|
|
|
|
((bus_state & SCSI::Line::Message) ? 0x04 : 0x00) |
|
|
|
|
((bus_state & SCSI::Line::Control) ? 0x02 : 0x00) |
|
|
|
|
((bus_state & SCSI::Line::Input) ? 0x01 : 0x00);
|
|
|
|
}
|
2019-08-12 00:55:20 +00:00
|
|
|
|
2019-08-16 03:14:40 +00:00
|
|
|
case 4: {
|
|
|
|
const auto bus_state = bus_.get_state();
|
2019-08-19 02:39:27 +00:00
|
|
|
const uint8_t result =
|
2019-08-19 03:15:54 +00:00
|
|
|
((bus_state & Line::Reset) ? 0x80 : 0x00) |
|
|
|
|
((bus_state & Line::Busy) ? 0x40 : 0x00) |
|
|
|
|
((bus_state & Line::Request) ? 0x20 : 0x00) |
|
|
|
|
((bus_state & Line::Message) ? 0x10 : 0x00) |
|
|
|
|
((bus_state & Line::Control) ? 0x08 : 0x00) |
|
|
|
|
((bus_state & Line::Input) ? 0x04 : 0x00) |
|
|
|
|
((bus_state & Line::SelectTarget) ? 0x02 : 0x00) |
|
|
|
|
((bus_state & Line::Parity) ? 0x01 : 0x00);
|
2019-08-19 02:39:27 +00:00
|
|
|
LOG("[SCSI 4] Get current bus state: " << PADHEX(2) << int(result));
|
|
|
|
return result;
|
2019-08-16 03:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 5: {
|
|
|
|
const auto bus_state = bus_.get_state();
|
2019-08-19 03:15:54 +00:00
|
|
|
const bool phase_matches =
|
|
|
|
(bus_output_ & (Line::Message | Line::Control | Line::Input)) ==
|
|
|
|
(bus_state & (Line::Message | Line::Control | Line::Input));
|
|
|
|
|
2019-08-19 02:39:27 +00:00
|
|
|
const uint8_t result =
|
2019-08-19 03:15:54 +00:00
|
|
|
(phase_matches ? 0x08 : 0x00) |
|
|
|
|
((bus_state & Line::Attention) ? 0x02 : 0x00) |
|
|
|
|
((bus_state & Line::Acknowledge) ? 0x01 : 0x00);
|
2019-08-19 02:39:27 +00:00
|
|
|
LOG("[SCSI 5] Get bus and status: " << PADHEX(2) << int(result));
|
|
|
|
return result;
|
2019-08-16 03:14:40 +00:00
|
|
|
}
|
2019-08-12 00:55:20 +00:00
|
|
|
|
|
|
|
case 6:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 6] Get input data");
|
2019-08-12 00:55:20 +00:00
|
|
|
return 0xff;
|
|
|
|
|
|
|
|
case 7:
|
2019-08-12 02:43:25 +00:00
|
|
|
LOG("[SCSI 7] Reset parity/interrupt");
|
2019-08-12 00:55:20 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
2019-08-11 03:53:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-16 03:14:40 +00:00
|
|
|
|
|
|
|
void NCR5380::run_for(Cycles cycles) {
|
|
|
|
if(state_ == ExecutionState::None) return;
|
|
|
|
|
|
|
|
++time_in_state_;
|
|
|
|
switch(state_) {
|
|
|
|
default: break;
|
|
|
|
|
2019-08-19 02:39:27 +00:00
|
|
|
/*
|
|
|
|
Official documentation:
|
|
|
|
|
2019-08-16 03:14:40 +00:00
|
|
|
Arbitration is accomplished using a bus-free filter to continuously monitor BSY.
|
|
|
|
If BSY remains inactive for at least 400 nsec then the SCSI bus is considered free
|
|
|
|
and arbitration may begin. Arbitration will begin if the bus is free, SEL is inactive
|
|
|
|
and the ARBITRATION bit (port 2, bit 0) is active. Once arbitration has begun
|
2019-08-19 02:39:27 +00:00
|
|
|
(BSY asserted), an arbitration delay of 2.2 /Lsec must elapse before the data bus
|
|
|
|
can be examined to deter- mine if arbitration has been won. This delay must be
|
|
|
|
implemented in the controlling software driver.
|
|
|
|
|
|
|
|
Personal notes:
|
|
|
|
|
|
|
|
I'm discounting that "arbitratation is accomplished" opening, and assuming that what needs
|
|
|
|
to happen is:
|
|
|
|
|
|
|
|
(i) wait for BSY to be inactive;
|
|
|
|
(ii) count 400 nsec;
|
|
|
|
(iii) check that BSY and SEL are inactive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case ExecutionState::WatchingBusy:
|
2019-08-16 03:14:40 +00:00
|
|
|
if(bus_.get_state() & SCSI::Line::Busy) {
|
2019-08-19 02:39:27 +00:00
|
|
|
// Arbitration is lost only if a non-busy state had previously been observed.
|
|
|
|
if(time_in_state_ > 1) {
|
|
|
|
lost_arbitration_ = true;
|
|
|
|
set_execution_state(ExecutionState::None);
|
|
|
|
} else {
|
|
|
|
time_in_state_ = 0;
|
|
|
|
}
|
|
|
|
} /* else {
|
|
|
|
arbitration_in_progress_ = true;
|
|
|
|
}*/
|
2019-08-16 03:14:40 +00:00
|
|
|
|
2019-08-19 02:39:27 +00:00
|
|
|
// Check for having hit 400ns (more or less) since BSY was inactive.
|
|
|
|
if(!lost_arbitration_ && time_in_state_ == int(int64_t(400) * int64_t(clock_rate_) / int64_t(1000000000))) {
|
|
|
|
// arbitration_in_progress_ = false;
|
2019-08-16 03:14:40 +00:00
|
|
|
if(bus_.get_state() & SCSI::Line::SelectTarget) {
|
|
|
|
lost_arbitration_ = true;
|
|
|
|
set_execution_state(ExecutionState::None);
|
|
|
|
} else {
|
2019-08-19 02:39:27 +00:00
|
|
|
bus_output_ &= ~SCSI::Line::Busy;
|
2019-08-16 03:14:40 +00:00
|
|
|
set_execution_state(ExecutionState::None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NCR5380::set_execution_state(ExecutionState state) {
|
|
|
|
time_in_state_ = 0;
|
2019-08-19 02:39:27 +00:00
|
|
|
state_ = state;
|
2019-08-16 03:14:40 +00:00
|
|
|
update_clocking_observer();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClockingHint::Preference NCR5380::preferred_clocking() {
|
|
|
|
// Request real-time clocking if any sort of timed bus watching is ongoing,
|
|
|
|
// given that there's no knowledge in here as to what clocking other devices
|
|
|
|
// on the SCSI bus might be enjoying.
|
2019-08-19 02:39:27 +00:00
|
|
|
return (state_ == ExecutionState::None) ? ClockingHint::Preference::None : ClockingHint::Preference::RealTime;
|
2019-08-16 03:14:40 +00:00
|
|
|
}
|