2021-10-26 17:00:04 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-11-01 15:02:13 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2021-10-26 17:00:04 +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/>.
|
|
|
|
*/
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
/** @file NCR53C94/Am53CF94 SCSI controller definitions. */
|
2021-10-26 17:00:04 +00:00
|
|
|
|
|
|
|
/* NOTE: Power Macintosh computers don't have a real NCR 53C94 chip.
|
2022-01-22 03:36:31 +00:00
|
|
|
The corresponding functionality is provided by the 53CF94 compatible
|
2021-10-26 17:00:04 +00:00
|
|
|
cell in the custom Curio IC (Am79C950).
|
|
|
|
*/
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
#ifndef SC_53C94_H
|
|
|
|
#define SC_53C94_H
|
2021-10-26 17:00:04 +00:00
|
|
|
|
2022-07-17 03:37:15 +00:00
|
|
|
#include <devices/common/scsi/scsi.h>
|
2022-02-05 17:17:17 +00:00
|
|
|
|
2021-10-26 17:00:04 +00:00
|
|
|
#include <cinttypes>
|
2023-11-01 15:13:12 +00:00
|
|
|
#include <functional>
|
2022-07-17 03:37:15 +00:00
|
|
|
#include <memory>
|
2021-10-26 17:00:04 +00:00
|
|
|
|
2023-11-03 07:21:33 +00:00
|
|
|
class DmaBidirChannel;
|
|
|
|
class InterruptCtrl;
|
|
|
|
|
2022-10-31 22:10:22 +00:00
|
|
|
#define DATA_FIFO_MAX 16
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
/** 53C94 read registers */
|
2021-10-26 17:00:04 +00:00
|
|
|
namespace Read {
|
|
|
|
enum Reg53C94 : uint8_t {
|
|
|
|
Xfer_Cnt_LSB = 0,
|
|
|
|
Xfer_Cnt_MSB = 1,
|
|
|
|
FIFO = 2,
|
|
|
|
Command = 3,
|
|
|
|
Status = 4,
|
2022-01-24 21:55:33 +00:00
|
|
|
Int_Status = 5,
|
2021-10-26 17:00:04 +00:00
|
|
|
Seq_Step = 6,
|
|
|
|
FIFO_Flags = 7,
|
|
|
|
Config_1 = 8,
|
|
|
|
Config_2 = 0xB,
|
|
|
|
Config_3 = 0xC,
|
2022-01-22 03:36:31 +00:00
|
|
|
Config_4 = 0xD, // Am53CF94 extension
|
|
|
|
Xfer_Cnt_Hi = 0xE, // Am53CF94 extension
|
2021-10-26 17:00:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
/** 53C94 write registers */
|
2021-10-26 17:00:04 +00:00
|
|
|
namespace Write {
|
|
|
|
enum Reg53C94 : uint8_t {
|
|
|
|
Xfer_Cnt_LSB = 0,
|
|
|
|
Xfer_Cnt_MSB = 1,
|
|
|
|
FIFO = 2,
|
|
|
|
Command = 3,
|
|
|
|
Dest_Bus_ID = 4,
|
|
|
|
Sel_Timeout = 5,
|
2022-02-05 17:17:17 +00:00
|
|
|
Sync_Period = 6,
|
|
|
|
Sync_Offset = 7,
|
2021-10-26 17:00:04 +00:00
|
|
|
Config_1 = 8,
|
2022-01-22 03:36:31 +00:00
|
|
|
Clock_Factor = 9,
|
2021-10-26 17:00:04 +00:00
|
|
|
Test_Mode = 0xA,
|
|
|
|
Config_2 = 0xB,
|
|
|
|
Config_3 = 0xC,
|
2022-01-22 03:36:31 +00:00
|
|
|
Config_4 = 0xD, // Am53CF94 extension
|
|
|
|
Xfer_Cnt_Hi = 0xE, // Am53CF94 extension
|
|
|
|
Data_Align = 0xF
|
2021-10-26 17:00:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-24 21:55:33 +00:00
|
|
|
/** NCR53C94/Am53CF94 commands. */
|
2022-01-22 03:36:31 +00:00
|
|
|
enum {
|
2022-11-02 21:28:43 +00:00
|
|
|
CMD_NOP = 0,
|
|
|
|
CMD_CLEAR_FIFO = 1,
|
|
|
|
CMD_RESET_DEVICE = 2,
|
|
|
|
CMD_RESET_BUS = 3,
|
|
|
|
CMD_DMA_STOP = 4,
|
|
|
|
CMD_XFER = 0x10,
|
|
|
|
CMD_COMPLETE_STEPS = 0x11,
|
|
|
|
CMD_MSG_ACCEPTED = 0x12,
|
|
|
|
CMD_SELECT_NO_ATN = 0x41,
|
2022-11-07 11:28:30 +00:00
|
|
|
CMD_SELECT_WITH_ATN = 0x42,
|
2022-11-02 21:28:43 +00:00
|
|
|
CMD_ENA_SEL_RESEL = 0x44,
|
2022-02-05 17:17:17 +00:00
|
|
|
};
|
|
|
|
|
2022-11-01 01:11:06 +00:00
|
|
|
/** Status register bits. **/
|
|
|
|
enum {
|
|
|
|
STAT_TC = 1 << 4, // Terminal count (NCR) / count to zero (AMD)
|
|
|
|
STAT_GE = 1 << 6, // Gross Error (NCR) / Illegal Operation Error (AMD)
|
|
|
|
};
|
|
|
|
|
2022-02-05 17:17:17 +00:00
|
|
|
/** Interrupt status register bits. */
|
|
|
|
enum {
|
|
|
|
INTSTAT_SRST = 0x80, // bus reset
|
|
|
|
INTSTAT_ICMD = 0x40, // invalid command
|
|
|
|
INTSTAT_DIS = 0x20, // disconnected
|
|
|
|
INTSTAT_SR = 0x10, // service request
|
|
|
|
INTSTAT_SO = 0x08, // successful operation
|
|
|
|
INTSTAT_RESEL = 0x04, // reselected
|
|
|
|
INTSTAT_SELA = 0x02, // selected as a target with attention
|
|
|
|
INTSTAT_SEL = 0x01, // selected as a target without attention
|
2022-01-22 03:36:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2022-02-05 17:17:17 +00:00
|
|
|
CFG2_ENF = 0x40, // Am53CF94: enable features (ENF) bit
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Sequencer states. */
|
|
|
|
namespace SeqState {
|
|
|
|
enum {
|
|
|
|
IDLE = 0,
|
|
|
|
BUS_FREE,
|
|
|
|
ARB_BEGIN,
|
|
|
|
ARB_END,
|
|
|
|
SEL_BEGIN,
|
|
|
|
SEL_END,
|
2022-11-07 11:28:30 +00:00
|
|
|
SEND_MSG,
|
|
|
|
SEND_CMD,
|
2022-02-05 17:17:17 +00:00
|
|
|
CMD_COMPLETE,
|
2022-10-31 22:18:47 +00:00
|
|
|
XFER_BEGIN,
|
|
|
|
XFER_END,
|
|
|
|
SEND_DATA,
|
|
|
|
RCV_DATA,
|
2022-11-02 21:28:43 +00:00
|
|
|
RCV_STATUS,
|
|
|
|
RCV_MESSAGE,
|
2022-02-05 17:17:17 +00:00
|
|
|
};
|
2022-01-22 03:36:31 +00:00
|
|
|
};
|
|
|
|
|
2022-02-05 17:17:17 +00:00
|
|
|
/** Sequence descriptor for sequencer commands. */
|
|
|
|
typedef struct {
|
|
|
|
int next_step;
|
|
|
|
int step_num;
|
|
|
|
int status;
|
|
|
|
} SeqDesc;
|
|
|
|
|
2023-11-01 15:13:12 +00:00
|
|
|
typedef std::function<void(const uint8_t drq_state)> DrqCb;
|
|
|
|
|
2022-02-05 17:17:17 +00:00
|
|
|
class Sc53C94 : public ScsiDevice {
|
2021-10-26 17:00:04 +00:00
|
|
|
public:
|
2022-02-05 17:17:17 +00:00
|
|
|
Sc53C94(uint8_t chip_id=12, uint8_t my_id=7);
|
2022-01-22 03:36:31 +00:00
|
|
|
~Sc53C94() = default;
|
2021-10-26 17:00:04 +00:00
|
|
|
|
2022-07-17 03:37:15 +00:00
|
|
|
static std::unique_ptr<HWComponent> create() {
|
|
|
|
return std::unique_ptr<Sc53C94>(new Sc53C94());
|
|
|
|
}
|
|
|
|
|
2022-02-05 17:17:17 +00:00
|
|
|
// HWComponent methods
|
|
|
|
int device_postinit();
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
// 53C94 registers access
|
2022-10-31 22:20:39 +00:00
|
|
|
uint8_t read(uint8_t reg_offset);
|
|
|
|
void write(uint8_t reg_offset, uint8_t value);
|
|
|
|
uint16_t pseudo_dma_read();
|
2023-11-01 15:16:58 +00:00
|
|
|
void pseudo_dma_write(uint16_t data);
|
2021-10-26 17:00:04 +00:00
|
|
|
|
2022-11-07 11:31:29 +00:00
|
|
|
// real DMA control
|
|
|
|
void real_dma_xfer(int direction);
|
|
|
|
|
|
|
|
void set_dma_channel(DmaBidirChannel *dma_ch) {
|
|
|
|
this->dma_ch = dma_ch;
|
|
|
|
};
|
|
|
|
|
2023-11-01 15:13:12 +00:00
|
|
|
void set_drq_callback(DrqCb cb) {
|
|
|
|
this->drq_cb = cb;
|
|
|
|
}
|
|
|
|
|
2022-02-05 17:17:17 +00:00
|
|
|
// ScsiDevice methods
|
2023-11-01 15:07:29 +00:00
|
|
|
void notify(ScsiMsg msg_type, int param);
|
2022-11-02 20:27:18 +00:00
|
|
|
bool prepare_data() { return false; };
|
2023-12-08 10:05:03 +00:00
|
|
|
bool get_more_data() { return false; };
|
2022-10-31 22:17:08 +00:00
|
|
|
bool has_data() { return this->data_fifo_pos != 0; };
|
2022-11-07 11:28:30 +00:00
|
|
|
int send_data(uint8_t* dst_ptr, int count);
|
2022-10-27 12:07:20 +00:00
|
|
|
void process_command() {};
|
2022-02-05 17:17:17 +00:00
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
protected:
|
|
|
|
void reset_device();
|
2022-01-24 21:55:33 +00:00
|
|
|
void update_command_reg(uint8_t cmd);
|
|
|
|
void exec_command();
|
|
|
|
void exec_next_command();
|
2022-02-05 17:17:17 +00:00
|
|
|
void fifo_push(const uint8_t data);
|
2022-11-02 20:19:31 +00:00
|
|
|
uint8_t fifo_pop();
|
2022-02-05 17:17:17 +00:00
|
|
|
|
|
|
|
void sequencer();
|
|
|
|
void seq_defer_state(uint64_t delay_ns);
|
2022-01-22 03:36:31 +00:00
|
|
|
|
2022-10-31 22:18:47 +00:00
|
|
|
bool rcv_data();
|
|
|
|
|
2022-02-06 00:50:19 +00:00
|
|
|
void update_irq();
|
|
|
|
|
2021-10-26 17:00:04 +00:00
|
|
|
private:
|
2022-01-22 03:36:31 +00:00
|
|
|
uint8_t chip_id;
|
2022-02-05 17:17:17 +00:00
|
|
|
uint8_t my_bus_id;
|
|
|
|
ScsiBus* bus_obj;
|
|
|
|
uint32_t my_timer_id;
|
|
|
|
|
2022-01-24 21:55:33 +00:00
|
|
|
uint8_t cmd_fifo[2];
|
|
|
|
uint8_t data_fifo[16];
|
|
|
|
int cmd_fifo_pos;
|
|
|
|
int data_fifo_pos;
|
2022-11-07 11:28:30 +00:00
|
|
|
int bytes_out;
|
2022-01-22 03:36:31 +00:00
|
|
|
bool on_reset = false;
|
|
|
|
uint32_t xfer_count;
|
|
|
|
uint32_t set_xfer_count;
|
2022-01-24 21:55:33 +00:00
|
|
|
uint8_t status;
|
2022-02-05 17:17:17 +00:00
|
|
|
uint8_t target_id;
|
2022-01-24 21:55:33 +00:00
|
|
|
uint8_t int_status;
|
2022-02-05 17:17:17 +00:00
|
|
|
uint8_t seq_step;
|
2022-01-22 03:36:31 +00:00
|
|
|
uint8_t sel_timeout;
|
2022-02-05 17:17:17 +00:00
|
|
|
uint8_t sync_offset;
|
2022-01-22 03:36:31 +00:00
|
|
|
uint8_t clk_factor;
|
|
|
|
uint8_t config1;
|
|
|
|
uint8_t config2;
|
|
|
|
uint8_t config3;
|
2022-02-05 17:17:17 +00:00
|
|
|
|
|
|
|
// sequencer state
|
|
|
|
uint32_t seq_timer_id;
|
|
|
|
uint32_t cur_state;
|
|
|
|
uint32_t next_state;
|
|
|
|
SeqDesc* cmd_steps;
|
|
|
|
bool is_initiator;
|
|
|
|
uint8_t cur_cmd;
|
2023-11-01 15:02:13 +00:00
|
|
|
bool is_dma_cmd = false;
|
2022-10-31 22:18:47 +00:00
|
|
|
int cur_bus_phase;
|
2022-02-06 00:50:19 +00:00
|
|
|
|
|
|
|
// interrupt related stuff
|
|
|
|
InterruptCtrl* int_ctrl = nullptr;
|
|
|
|
uint32_t irq_id = 0;
|
|
|
|
uint8_t irq = 0;
|
2022-11-07 11:31:29 +00:00
|
|
|
|
|
|
|
// DMA related stuff
|
|
|
|
DmaBidirChannel* dma_ch;
|
2023-11-01 15:13:12 +00:00
|
|
|
DrqCb drq_cb = nullptr;
|
2021-10-26 17:00:04 +00:00
|
|
|
};
|
|
|
|
|
2022-01-22 03:36:31 +00:00
|
|
|
#endif // SC_53C94_H
|