Skip to content

Commit 721862f

Browse files
Initial CMD646U2 Ultra ATA controller emulation.
1 parent 1f05865 commit 721862f

File tree

3 files changed

+359
-5
lines changed

3 files changed

+359
-5
lines changed

devices/common/ata/cmd646.cpp

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*
2+
DingusPPC - The Experimental PowerPC Macintosh emulator
3+
Copyright (C) 2018-24 divingkatae and maximum
4+
(theweirdo) spatium
5+
6+
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
/** @file CMD646U2 PCI Ultra ATA controller emulation. */
23+
24+
#include <devices/common/ata/cmd646.h>
25+
#include <devices/deviceregistry.h>
26+
#include <loguru.hpp>
27+
#include <machines/machinebase.h>
28+
29+
CmdIdeCtrl::CmdIdeCtrl() : PCIDevice("cmd-ide") {
30+
this->supports_types(HWCompType::PCI_DEV | HWCompType::IDE_HOST);
31+
32+
// set up PCI configuration space header
33+
this->vendor_id = this->subsys_vndr = PCI_VENDOR_SILICON_IMAGE;
34+
this->device_id = this->subsys_id = DEV_ID_CMD646;
35+
this->class_rev = ((MY_DEV_CLASS | this->prog_if) << 8) | MY_REV_ID;
36+
this->max_lat = 4;
37+
this->min_gnt = 2;
38+
this->irq_pin = 1;
39+
40+
this->bars_cfg[0] = 0xFFFFFFF9; // Command block I/O space, primary channel
41+
this->bars_cfg[1] = 0xFFFFFFFD; // Control block I/O space, primary channel
42+
this->bars_cfg[2] = 0xFFFFFFF9; // Command block I/O space, secondary channel
43+
this->bars_cfg[3] = 0xFFFFFFFD; // Control block I/O space, secondary channel
44+
this->bars_cfg[4] = 0xFFFFFFF1; // Bus master I/O space, both channels
45+
46+
this->finish_config_bars();
47+
48+
this->pci_notify_bar_change = [this](int bar_num) {
49+
this->notify_bar_change(bar_num);
50+
};
51+
52+
gMachineObj->add_device("CmdAta0", std::unique_ptr<IdeChannel>(new IdeChannel("CmdAta0")));
53+
gMachineObj->add_device("CmdAta1", std::unique_ptr<IdeChannel>(new IdeChannel("CmdAta1")));
54+
55+
this->ch0 = dynamic_cast<IdeChannel*>(gMachineObj->get_comp_by_name("CmdAta0"));
56+
this->ch1 = dynamic_cast<IdeChannel*>(gMachineObj->get_comp_by_name("CmdAta1"));
57+
58+
this->ch0->set_irq_callback([this](const uint8_t intrq_state) {
59+
LOG_F(INFO, "CmdAta0 INTRQ updated to %d", intrq_state);
60+
this->update_irq(0, intrq_state);
61+
});
62+
63+
this->ch1->set_irq_callback([this](const uint8_t intrq_state) {
64+
LOG_F(INFO, "CmdAta1 INTRQ updated to %d", intrq_state);
65+
this->update_irq(1, intrq_state);
66+
});
67+
}
68+
69+
uint32_t CmdIdeCtrl::pci_cfg_read(uint32_t reg_offs, AccessDetails &details) {
70+
if (reg_offs < 64)
71+
return PCIDevice::pci_cfg_read(reg_offs, details);
72+
73+
if (details.size != 1)
74+
ABORT_F("%s: non-byte read from PCI config reg 0x%X", this->name.c_str(),
75+
reg_offs + details.offset);
76+
77+
if (reg_offs < 112)
78+
return this->read_config_reg(reg_offs + details.offset);
79+
80+
LOG_F(WARNING, "%s: reading config reg at 0x%X", this->name.c_str(), reg_offs);
81+
82+
return 0;
83+
}
84+
85+
void CmdIdeCtrl::pci_cfg_write(uint32_t reg_offs, uint32_t value, AccessDetails &details) {
86+
if (reg_offs < 64) {
87+
PCIDevice::pci_cfg_write(reg_offs, value, details);
88+
return;
89+
}
90+
91+
if (details.size != 1)
92+
ABORT_F("%s: non-byte write to PCI config reg 0x%X", this->name.c_str(),
93+
reg_offs + details.offset);
94+
95+
if (reg_offs < 112)
96+
this->write_config_reg(reg_offs + details.offset, value);
97+
else
98+
LOG_F(WARNING, "%s: writing config reg at 0x%X", this->name.c_str(), reg_offs);
99+
}
100+
101+
void CmdIdeCtrl::notify_bar_change(int bar_num) {
102+
if (bar_num >= 0 && bar_num <= 4)
103+
this->io_bases[bar_num] = this->bars[bar_num] & ~3;
104+
}
105+
106+
bool CmdIdeCtrl::pci_io_read(uint32_t offset, uint32_t size, uint32_t* res) {
107+
*res = 0;
108+
109+
if ((offset & ~7) == this->io_bases[0]) {
110+
*res = this->ch0->read(offset & 7, size);
111+
} else if ((offset & ~7) == this->io_bases[2]) {
112+
*res = this->ch1->read(offset & 7, size);
113+
} else if ((offset & ~3) == this->io_bases[1]) {
114+
*res = this->ch0->read((offset & 3) + DEV_CTRL_BLK_OFFSET, size);
115+
} else if ((offset & ~3) == this->io_bases[3]) {
116+
*res = this->ch1->read((offset & 3) + DEV_CTRL_BLK_OFFSET, size);
117+
} else if ((offset & ~0xF) == this->io_bases[4]) {
118+
*res = this->read_bus_master_reg(offset & 0xF);
119+
} else {
120+
*res = 0xFFFFFFFFUL;
121+
return false;
122+
}
123+
124+
return true;
125+
}
126+
127+
bool CmdIdeCtrl::pci_io_write(uint32_t offset, uint32_t value, uint32_t size) {
128+
if ((offset & ~7) == this->io_bases[0]) {
129+
this->ch0->write(offset & 7, value, size);
130+
} else if ((offset & ~7) == this->io_bases[2]) {
131+
this->ch1->write(offset & 7, value, size);
132+
} else if ((offset & ~3) == this->io_bases[1]) {
133+
this->ch0->write((offset & 3) + DEV_CTRL_BLK_OFFSET, value, size);
134+
} else if ((offset & ~3) == this->io_bases[3]) {
135+
this->ch1->write((offset & 3) + DEV_CTRL_BLK_OFFSET, value, size);
136+
} else if ((offset & ~0xF) == this->io_bases[4]) {
137+
this->write_bus_master_reg(offset & 0xF, value & 0xFFU);
138+
} else
139+
return false;
140+
141+
return true;
142+
};
143+
144+
uint8_t CmdIdeCtrl::read_config_reg(uint32_t reg_offset) {
145+
switch(reg_offset) {
146+
case ARTTIM0:
147+
return this->addr_setup_time_0;
148+
case DRWTIM0:
149+
return this->data_rw_time_0;
150+
case ARTTIM1:
151+
return this->addr_setup_time_1;
152+
case DRWTIM1:
153+
return this->data_rw_time_1;
154+
default:
155+
LOG_F(ERROR, "%s: unimplemented config reg at 0x%X", this->name.c_str(),
156+
reg_offset);
157+
}
158+
159+
return 0;
160+
}
161+
162+
void CmdIdeCtrl::write_config_reg(uint32_t reg_offset, uint8_t val) {
163+
switch(reg_offset) {
164+
case ARTTIM0:
165+
this->addr_setup_time_0 = val;
166+
LOG_F(9, "%s: ARTTIM0 set to 0x%X", this->name.c_str(), val);
167+
break;
168+
case DRWTIM0:
169+
this->data_rw_time_0 = val;
170+
LOG_F(9, "%s: DRWTIM0 set to 0x%X", this->name.c_str(), val);
171+
break;
172+
case ARTTIM1:
173+
this->addr_setup_time_1 = val;
174+
LOG_F(9, "%s: ARTTIM1 set to 0x%X", this->name.c_str(), val);
175+
break;
176+
case DRWTIM1:
177+
this->data_rw_time_1 = val;
178+
LOG_F(9, "%s: DRWTIM1 set to 0x%X", this->name.c_str(), val);
179+
break;
180+
default:
181+
LOG_F(ERROR, "%s: unimplemented config reg at 0x%X", this->name.c_str(),
182+
reg_offset);
183+
}
184+
}
185+
186+
uint8_t CmdIdeCtrl::read_bus_master_reg(const uint8_t reg_offset) {
187+
switch(reg_offset) {
188+
case MRDMODE:
189+
return this->mrdmode;
190+
default:
191+
LOG_F(ERROR, "%s: unimplemented bus master reg at 0x%X", this->name.c_str(),
192+
reg_offset);
193+
}
194+
195+
return 0;
196+
}
197+
198+
void CmdIdeCtrl::write_bus_master_reg(const uint8_t reg_offset, const uint8_t val) {
199+
switch(reg_offset) {
200+
case MRDMODE:
201+
if (val & BM_CH0_INT)
202+
this->mrdmode &= ~BM_CH0_INT;
203+
if (val & BM_CH1_INT)
204+
this->mrdmode &= ~BM_CH1_INT;
205+
this->mrdmode = (this->mrdmode & ~(BM_BLOCK_CH0_INT | BM_BLOCK_CH1_INT)) |
206+
(val & (BM_BLOCK_CH0_INT | BM_BLOCK_CH1_INT));
207+
LOG_F(INFO, "%s: MRDMODE set to 0x%X", this->name.c_str(), val);
208+
if ((this->mrdmode & BM_CH0_INT) && !(this->mrdmode & BM_BLOCK_CH0_INT))
209+
this->update_irq(0, 1);
210+
if ((this->mrdmode & BM_CH1_INT) && !(this->mrdmode & BM_BLOCK_CH1_INT))
211+
this->update_irq(1, 1);
212+
break;
213+
case UDIDETCR0:
214+
this->udma_time_cr = val;
215+
break;
216+
default:
217+
LOG_F(ERROR, "%s: unimplemented bus master reg at 0x%X", this->name.c_str(),
218+
reg_offset);
219+
}
220+
}
221+
222+
void CmdIdeCtrl::update_irq(const int ch_num, const uint8_t irq_level) {
223+
bool forward_irq = !(this->mrdmode & (ch_num ? BM_BLOCK_CH1_INT : BM_BLOCK_CH0_INT));
224+
225+
if (irq_level)
226+
this->mrdmode |= ch_num ? BM_CH1_INT : BM_CH0_INT;
227+
else
228+
this->mrdmode &= ~(ch_num ? BM_CH1_INT : BM_CH0_INT);
229+
230+
if (!irq_level || forward_irq)
231+
this->irq_info.int_ctrl_obj->ack_int(this->irq_info.irq_id, irq_level);
232+
}
233+
234+
static const DeviceDescription CmdIde_Descriptor = {
235+
CmdIdeCtrl::create, {}, {}
236+
};
237+
238+
REGISTER_DEVICE(CmdAta, CmdIde_Descriptor);

devices/common/ata/cmd646.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
DingusPPC - The Experimental PowerPC Macintosh emulator
3+
Copyright (C) 2018-24 divingkatae and maximum
4+
(theweirdo) spatium
5+
6+
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
/** @file CMD646U2 PCI Ultra ATA controller definitions. */
23+
24+
#ifndef CMD646_IDE_H
25+
#define CMD646_IDE_H
26+
27+
#include <devices/common/hwcomponent.h>
28+
#include <devices/common/hwinterrupt.h>
29+
#include <devices/common/ata/idechannel.h>
30+
#include <devices/common/pci/pcidevice.h>
31+
32+
#include <memory>
33+
34+
#define DEV_ID_CMD646 0x646
35+
#define MY_DEV_CLASS 0x010180 // mass storage | IDE controller | IDE master
36+
#define MY_REV_ID 7
37+
38+
// Offset for converting addresses of the device control block registers
39+
// defined in the PCI IDE Controller specification, rev. 1.0 3/4/94
40+
// to the addresses used in IdeChannel
41+
#define DEV_CTRL_BLK_OFFSET 0x14
42+
43+
/** CMD646 control/status registers. */
44+
enum {
45+
ARTTIM0 = 0x53,
46+
DRWTIM0 = 0x54,
47+
ARTTIM1 = 0x55,
48+
DRWTIM1 = 0x56,
49+
};
50+
51+
/** CMD646 bus master registers. */
52+
enum {
53+
MRDMODE = 1, // misnomer, contains interrupt control/status bits (CMD646U2 specific)
54+
UDIDETCR0 = 3, // Ultra DMA timing control register (CMD646U2 specific)
55+
};
56+
57+
/** Bit definitions for the MRDMODE register. */
58+
enum {
59+
BM_CH0_INT = 1 << 2,
60+
BM_CH1_INT = 1 << 3,
61+
BM_BLOCK_CH0_INT = 1 << 4,
62+
BM_BLOCK_CH1_INT = 1 << 5,
63+
};
64+
65+
class CmdIdeCtrl : public PCIDevice {
66+
public:
67+
CmdIdeCtrl();
68+
~CmdIdeCtrl() = default;
69+
70+
static std::unique_ptr<HWComponent> create() {
71+
return std::unique_ptr<CmdIdeCtrl>(new CmdIdeCtrl());
72+
}
73+
74+
// PCIDevice methods
75+
uint32_t pci_cfg_read(uint32_t reg_offs, AccessDetails &details) override;
76+
void pci_cfg_write(uint32_t reg_offs, uint32_t value, AccessDetails &details) override;
77+
78+
bool pci_io_read(uint32_t offset, uint32_t size, uint32_t* res) override;
79+
bool pci_io_write(uint32_t offset, uint32_t value, uint32_t size) override;
80+
81+
int device_postinit() override {
82+
this->irq_info = this->host_instance->register_pci_int(this);
83+
return 0;
84+
};
85+
86+
private:
87+
void notify_bar_change(int bar_num);
88+
uint8_t read_config_reg(uint32_t reg_offset);
89+
void write_config_reg(uint32_t reg_offset, uint8_t val);
90+
uint8_t read_bus_master_reg(const uint8_t reg_offset);
91+
void write_bus_master_reg(const uint8_t reg_offset, const uint8_t val);
92+
void update_irq(const int ch_num, const uint8_t irq_level);
93+
94+
// on reset, programming interface defaults to
95+
// "both channels operating in native mode"
96+
uint8_t prog_if = 0x0F;
97+
98+
uint32_t io_bases[5] = {};
99+
100+
IdeChannel *ch0 = nullptr;
101+
IdeChannel *ch1 = nullptr;
102+
103+
// unknown default, set it to 2 clocks (60 ns)
104+
uint8_t addr_setup_time_0 = 0x40; // address setup time for drive 0
105+
uint8_t addr_setup_time_1 = 0x40; // address setup time for drive 1
106+
uint8_t data_rw_time_0 = 0x00; // data read/write time for drive 0
107+
uint8_t data_rw_time_1 = 0x00; // data read/write time for drive 1
108+
109+
uint8_t mrdmode = 0;
110+
uint8_t udma_time_cr = 0;
111+
112+
IntDetails irq_info = {};
113+
};
114+
115+
#endif // CMD646_IDE_H

devices/common/pci/pcibase.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ enum {
5252

5353
/** PCI Vendor IDs for devices used in Power Macintosh computers. */
5454
enum {
55-
PCI_VENDOR_ATI = 0x1002,
56-
PCI_VENDOR_DEC = 0x1011,
57-
PCI_VENDOR_MOTOROLA = 0x1057,
58-
PCI_VENDOR_APPLE = 0x106B,
59-
PCI_VENDOR_NVIDIA = 0x10DE,
55+
PCI_VENDOR_ATI = 0x1002,
56+
PCI_VENDOR_DEC = 0x1011,
57+
PCI_VENDOR_MOTOROLA = 0x1057,
58+
PCI_VENDOR_APPLE = 0x106B,
59+
PCI_VENDOR_SILICON_IMAGE = 0x1095,
60+
PCI_VENDOR_NVIDIA = 0x10DE,
6061
};
6162

6263
/** PCI BAR types */

0 commit comments

Comments
 (0)