2021-08-22 22:20:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-09-30 19:34:47 +00:00
|
|
|
Copyright (C) 2018-24 divingkatae and maximum
|
2021-08-22 22:20:28 +00:00
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file MESH (Macintosh Enhanced SCSI Hardware) controller emulation. */
|
|
|
|
|
2023-01-25 23:34:17 +00:00
|
|
|
#include <core/timermanager.h>
|
|
|
|
#include <devices/common/hwinterrupt.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
#include <devices/common/scsi/mesh.h>
|
2023-11-03 07:21:33 +00:00
|
|
|
#include <devices/common/scsi/scsi.h>
|
2022-07-17 10:53:44 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2023-01-25 19:58:30 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
#include <machines/machinebase.h>
|
2021-10-23 18:17:47 +00:00
|
|
|
|
2021-08-22 22:20:28 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
|
2022-03-09 15:56:52 +00:00
|
|
|
using namespace MeshScsi;
|
|
|
|
|
2023-01-25 19:58:30 +00:00
|
|
|
int MeshController::device_postinit()
|
|
|
|
{
|
2023-09-30 19:34:47 +00:00
|
|
|
this->bus_obj = dynamic_cast<ScsiBus*>(gMachineObj->get_comp_by_name("ScsiMesh"));
|
2023-01-25 19:58:30 +00:00
|
|
|
|
2023-01-25 23:34:17 +00:00
|
|
|
this->int_ctrl = dynamic_cast<InterruptCtrl*>(
|
|
|
|
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
|
2024-02-11 23:47:53 +00:00
|
|
|
this->irq_id = this->int_ctrl->register_dev_int(IntSrc::SCSI_MESH);
|
2023-01-25 23:34:17 +00:00
|
|
|
|
2023-01-25 19:58:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshController::reset(bool is_hard_reset)
|
|
|
|
{
|
|
|
|
this->cur_cmd = SeqCmd::NoOperation;
|
2023-08-08 23:39:53 +00:00
|
|
|
this->fifo_cnt = 0;
|
2023-01-25 19:58:30 +00:00
|
|
|
this->int_mask = 0;
|
2023-08-08 23:39:53 +00:00
|
|
|
this->xfer_count = 0;
|
2023-10-18 09:21:38 +00:00
|
|
|
this->src_id = 7;
|
2023-01-25 19:58:30 +00:00
|
|
|
|
|
|
|
if (is_hard_reset) {
|
|
|
|
this->bus_stat = 0;
|
2023-08-08 23:39:53 +00:00
|
|
|
this->sync_params = (0 << 16) | 2; // fast async operation (guessed)
|
2023-01-25 19:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MeshController::read(uint8_t reg_offset)
|
2021-08-22 22:20:28 +00:00
|
|
|
{
|
|
|
|
switch(reg_offset) {
|
2023-08-08 23:39:53 +00:00
|
|
|
case MeshReg::XferCount0:
|
|
|
|
return this->xfer_count & 0xFFU;
|
|
|
|
case MeshReg::XferCount1:
|
|
|
|
return (this->xfer_count >> 8) & 0xFFU;
|
2023-01-25 23:34:17 +00:00
|
|
|
case MeshReg::Sequence:
|
|
|
|
return this->cur_cmd;
|
2021-08-22 22:20:28 +00:00
|
|
|
case MeshReg::BusStatus0:
|
2023-01-25 19:58:30 +00:00
|
|
|
return this->bus_obj->test_ctrl_lines(0xFFU);
|
|
|
|
case MeshReg::BusStatus1:
|
|
|
|
return this->bus_obj->test_ctrl_lines(0xE000U) >> 8;
|
2023-08-08 23:39:53 +00:00
|
|
|
case MeshReg::FIFOCount:
|
|
|
|
return this->fifo_cnt;
|
2023-01-25 23:34:17 +00:00
|
|
|
case MeshReg::Exception:
|
|
|
|
return 0;
|
|
|
|
case MeshReg::Error:
|
|
|
|
return 0;
|
2023-01-25 19:58:30 +00:00
|
|
|
case MeshReg::IntMask:
|
|
|
|
return this->int_mask;
|
2023-01-25 23:34:17 +00:00
|
|
|
case MeshReg::Interrupt:
|
|
|
|
return this->int_stat;
|
2023-08-08 23:39:53 +00:00
|
|
|
case MeshReg::DestID:
|
|
|
|
return this->dst_id;
|
|
|
|
case MeshReg::SyncParms:
|
|
|
|
return this->sync_params;
|
2021-08-22 22:20:28 +00:00
|
|
|
case MeshReg::MeshID:
|
|
|
|
return this->chip_id; // tell them who we are
|
|
|
|
default:
|
2022-08-22 10:09:52 +00:00
|
|
|
LOG_F(WARNING, "MESH: read from unimplemented register at offset 0x%x", reg_offset);
|
2021-08-22 22:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-25 19:58:30 +00:00
|
|
|
void MeshController::write(uint8_t reg_offset, uint8_t value)
|
2021-08-22 22:20:28 +00:00
|
|
|
{
|
2023-01-25 19:58:30 +00:00
|
|
|
uint16_t new_stat;
|
|
|
|
|
2021-08-22 22:20:28 +00:00
|
|
|
switch(reg_offset) {
|
|
|
|
case MeshReg::Sequence:
|
2023-01-25 19:58:30 +00:00
|
|
|
perform_command(value);
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
|
|
|
case MeshReg::BusStatus1:
|
2023-01-25 19:58:30 +00:00
|
|
|
new_stat = value << 8;
|
|
|
|
if (new_stat != this->bus_stat) {
|
|
|
|
for (uint16_t mask = SCSI_CTRL_RST; mask >= SCSI_CTRL_SEL; mask >>= 1) {
|
|
|
|
if ((new_stat ^ this->bus_stat) & mask) {
|
|
|
|
if (new_stat & mask)
|
2023-10-18 09:21:38 +00:00
|
|
|
this->bus_obj->assert_ctrl_line(this->src_id, mask);
|
2023-01-25 19:58:30 +00:00
|
|
|
else
|
2023-10-18 09:21:38 +00:00
|
|
|
this->bus_obj->release_ctrl_line(this->src_id, mask);
|
2023-01-25 19:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this->bus_stat = new_stat;
|
|
|
|
}
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
|
|
|
case MeshReg::IntMask:
|
2023-01-25 19:58:30 +00:00
|
|
|
this->int_mask = value;
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
|
|
|
case MeshReg::Interrupt:
|
2023-01-25 19:58:30 +00:00
|
|
|
this->int_stat &= ~(value & INT_MASK); // clear requested interrupt bits
|
2023-01-25 23:34:17 +00:00
|
|
|
update_irq();
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
|
|
|
case MeshReg::SourceID:
|
2023-01-25 19:58:30 +00:00
|
|
|
this->src_id = value;
|
|
|
|
break;
|
|
|
|
case MeshReg::DestID:
|
|
|
|
this->dst_id = value;
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
|
|
|
case MeshReg::SyncParms:
|
2023-01-25 19:58:30 +00:00
|
|
|
this->sync_params = value;
|
|
|
|
break;
|
2023-01-25 23:34:17 +00:00
|
|
|
case MeshReg::SelTimeOut:
|
|
|
|
LOG_F(9, "MESH: selection timeout set to 0x%x", value);
|
|
|
|
break;
|
2023-01-25 19:58:30 +00:00
|
|
|
default:
|
|
|
|
LOG_F(WARNING, "MESH: write to unimplemented register at offset 0x%x",
|
|
|
|
reg_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshController::perform_command(const uint8_t cmd)
|
|
|
|
{
|
2023-01-25 23:34:17 +00:00
|
|
|
this->cur_cmd = cmd;
|
2023-01-25 19:58:30 +00:00
|
|
|
|
|
|
|
this->int_stat &= ~INT_CMD_DONE;
|
|
|
|
|
2023-01-25 23:34:17 +00:00
|
|
|
switch (this->cur_cmd & 0xF) {
|
|
|
|
case SeqCmd::Arbitrate:
|
2023-09-24 20:36:44 +00:00
|
|
|
this->bus_obj->release_ctrl_lines(this->src_id);
|
2023-01-25 23:34:17 +00:00
|
|
|
this->cur_state = SeqState::BUS_FREE;
|
|
|
|
this->sequencer();
|
|
|
|
break;
|
|
|
|
case SeqCmd::Select:
|
|
|
|
this->cur_state = SeqState::SEL_BEGIN;
|
|
|
|
this->sequencer();
|
|
|
|
break;
|
2023-10-02 13:01:27 +00:00
|
|
|
case SeqCmd::BusFree:
|
|
|
|
LOG_F(INFO, "MESH: BusFree stub invoked");
|
|
|
|
this->int_stat |= INT_CMD_DONE;
|
|
|
|
break;
|
|
|
|
case SeqCmd::EnaReselect:
|
|
|
|
LOG_F(INFO, "MESH: EnaReselect stub invoked");
|
|
|
|
this->int_stat |= INT_CMD_DONE;
|
|
|
|
break;
|
2023-01-25 23:34:17 +00:00
|
|
|
case SeqCmd::DisReselect:
|
2023-10-02 13:01:27 +00:00
|
|
|
LOG_F(9, "MESH: DisReselect stub invoked");
|
2023-09-24 20:36:44 +00:00
|
|
|
this->int_stat |= INT_CMD_DONE;
|
2023-01-25 23:34:17 +00:00
|
|
|
break;
|
2023-01-25 19:58:30 +00:00
|
|
|
case SeqCmd::ResetMesh:
|
|
|
|
this->reset(false);
|
|
|
|
this->int_stat |= INT_CMD_DONE;
|
2021-08-22 22:20:28 +00:00
|
|
|
break;
|
2023-01-25 23:34:17 +00:00
|
|
|
case SeqCmd::FlushFIFO:
|
2023-10-02 13:01:27 +00:00
|
|
|
LOG_F(INFO, "MESH: FlushFIFO stub invoked");
|
2023-12-19 13:53:43 +00:00
|
|
|
this->int_stat |= INT_CMD_DONE;
|
2023-01-25 23:34:17 +00:00
|
|
|
break;
|
2021-08-22 22:20:28 +00:00
|
|
|
default:
|
2023-01-25 19:58:30 +00:00
|
|
|
LOG_F(ERROR, "MESH: unsupported sequencer command 0x%X", this->cur_cmd);
|
2021-08-22 22:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-17 10:53:44 +00:00
|
|
|
|
2023-01-25 23:34:17 +00:00
|
|
|
void MeshController::seq_defer_state(uint64_t delay_ns)
|
|
|
|
{
|
|
|
|
seq_timer_id = TimerManager::get_instance()->add_oneshot_timer(
|
|
|
|
delay_ns,
|
|
|
|
[this]() {
|
|
|
|
// re-enter the sequencer with the state specified in next_state
|
|
|
|
this->cur_state = this->next_state;
|
|
|
|
this->sequencer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshController::sequencer()
|
|
|
|
{
|
|
|
|
switch (this->cur_state) {
|
|
|
|
case SeqState::IDLE:
|
|
|
|
break;
|
|
|
|
case SeqState::BUS_FREE:
|
|
|
|
if (this->bus_obj->current_phase() == ScsiPhase::BUS_FREE) {
|
|
|
|
this->next_state = SeqState::ARB_BEGIN;
|
|
|
|
this->seq_defer_state(BUS_FREE_DELAY + BUS_SETTLE_DELAY);
|
|
|
|
} else { // continue waiting
|
|
|
|
this->next_state = SeqState::BUS_FREE;
|
|
|
|
this->seq_defer_state(BUS_FREE_DELAY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SeqState::ARB_BEGIN:
|
|
|
|
if (!this->bus_obj->begin_arbitration(this->src_id)) {
|
|
|
|
LOG_F(ERROR, "MESH: arbitration error, bus not free!");
|
|
|
|
this->bus_obj->release_ctrl_lines(this->src_id);
|
|
|
|
this->next_state = SeqState::BUS_FREE;
|
|
|
|
this->seq_defer_state(BUS_CLEAR_DELAY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->next_state = SeqState::ARB_END;
|
|
|
|
this->seq_defer_state(ARB_DELAY);
|
|
|
|
break;
|
|
|
|
case SeqState::ARB_END:
|
|
|
|
if (this->bus_obj->end_arbitration(this->src_id) &&
|
|
|
|
!this->bus_obj->test_ctrl_lines(SCSI_CTRL_SEL)) { // arbitration won
|
|
|
|
this->bus_obj->assert_ctrl_line(this->src_id, SCSI_CTRL_SEL);
|
|
|
|
} else { // arbitration lost
|
|
|
|
LOG_F(INFO, "MESH: arbitration lost!");
|
|
|
|
this->bus_obj->release_ctrl_lines(this->src_id);
|
|
|
|
this->exception |= EXC_ARB_LOST;
|
|
|
|
this->int_stat |= INT_EXCEPTION;
|
|
|
|
}
|
|
|
|
this->int_stat |= INT_CMD_DONE;
|
|
|
|
update_irq();
|
|
|
|
break;
|
|
|
|
case SeqState::SEL_BEGIN:
|
|
|
|
this->bus_obj->begin_selection(this->src_id, this->dst_id, this->cur_cmd & 0x20);
|
|
|
|
this->next_state = SeqState::SEL_END;
|
|
|
|
this->seq_defer_state(SEL_TIME_OUT);
|
|
|
|
break;
|
|
|
|
case SeqState::SEL_END:
|
|
|
|
if (this->bus_obj->end_selection(this->src_id, this->dst_id)) {
|
|
|
|
this->bus_obj->release_ctrl_line(this->src_id, SCSI_CTRL_SEL);
|
|
|
|
LOG_F(9, "MESH: selection completed");
|
|
|
|
} else { // selection timeout
|
|
|
|
this->bus_obj->disconnect(this->src_id);
|
|
|
|
this->cur_state = SeqState::IDLE;
|
|
|
|
this->exception |= EXC_SEL_TIMEOUT;
|
|
|
|
this->int_stat |= INT_EXCEPTION;
|
|
|
|
}
|
|
|
|
this->int_stat |= INT_CMD_DONE;
|
|
|
|
update_irq();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ABORT_F("MESH: unimplemented sequencer state %d", this->cur_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshController::update_irq()
|
|
|
|
{
|
|
|
|
uint8_t new_irq = !!(this->int_stat & this->int_mask);
|
|
|
|
if (new_irq != this->irq) {
|
|
|
|
this->irq = new_irq;
|
|
|
|
this->int_ctrl->ack_int(this->irq_id, new_irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 07:05:16 +00:00
|
|
|
static const DeviceDescription Mesh_Tnt_Descriptor = {
|
|
|
|
MeshController::create_for_tnt, {}, {}
|
2022-07-17 10:53:44 +00:00
|
|
|
};
|
|
|
|
|
2023-12-11 07:05:16 +00:00
|
|
|
static const DeviceDescription Mesh_Heathrow_Descriptor = {
|
|
|
|
MeshController::create_for_heathrow, {}, {}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_DEVICE(MeshTnt, Mesh_Tnt_Descriptor);
|
|
|
|
REGISTER_DEVICE(MeshHeathrow, Mesh_Heathrow_Descriptor);
|