summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/psx/ctlrport.cpp
blob: 5f84e482b4d49bb4af7d30498df0b0aaf443da83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// license:BSD-3-Clause
// copyright-holders:Carl
/* PAD emulation */

#include "ctlrport.h"
#include "analogue.h"
#include "multitap.h"

const device_type PSX_CONTROLLER_PORT = &device_creator<psx_controller_port_device>;

psx_controller_port_device::psx_controller_port_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
		device_t(mconfig, PSX_CONTROLLER_PORT, "Playstation Controller Port", tag, owner, clock, "psx_controller_port", __FILE__),
		device_slot_interface(mconfig, *this),
		m_tx(false),
		m_dev(nullptr),
		m_card(*this, "card")
{
}

void psx_controller_port_device::device_config_complete()
{
	m_dev = dynamic_cast<device_psx_controller_interface *>(get_card_device());
}

static MACHINE_CONFIG_FRAGMENT( psx_memory_card )
	MCFG_PSXCARD_ADD("card")
MACHINE_CONFIG_END

machine_config_constructor psx_controller_port_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( psx_memory_card );
}

void psx_controller_port_device::disable_card(bool state)
{
	if(state)
		popmessage("Memory card port %s is disabled\n", m_card->brief_instance_name());

	m_card->disable(state);
}

const device_type PSXCONTROLLERPORTS = &device_creator<psxcontrollerports_device>;

psxcontrollerports_device::psxcontrollerports_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, PSXCONTROLLERPORTS, "PSXCONTROLLERPORTS", tag, owner, clock, "psxcontrollerports", __FILE__), m_port0(nullptr), m_port1(nullptr),
	m_dsr_handler(*this),
	m_rxd_handler(*this)
{
}

void psxcontrollerports_device::device_start()
{
	m_dsr_handler.resolve_safe();
	m_rxd_handler.resolve_safe();

	m_port0 = machine().device<psx_controller_port_device>("port1");
	m_port1 = machine().device<psx_controller_port_device>("port2");
	m_port0->setup_ack_cb(psx_controller_port_device::void_cb(FUNC(psxcontrollerports_device::ack), this));
	m_port1->setup_ack_cb(psx_controller_port_device::void_cb(FUNC(psxcontrollerports_device::ack), this));
}

// add controllers to define so they can be connected to the multitap
#define PSX_CONTROLLERS \
		SLOT_INTERFACE("digital_pad", PSX_STANDARD_CONTROLLER) \
		SLOT_INTERFACE("dualshock_pad", PSX_DUALSHOCK) \
		SLOT_INTERFACE("analog_joystick", PSX_ANALOG_JOYSTICK)

SLOT_INTERFACE_START(psx_controllers)
	PSX_CONTROLLERS
	SLOT_INTERFACE("multitap", PSX_MULTITAP)
SLOT_INTERFACE_END

SLOT_INTERFACE_START(psx_controllers_nomulti)
	PSX_CONTROLLERS
SLOT_INTERFACE_END

WRITE_LINE_MEMBER(psxcontrollerports_device::write_dtr)
{
	m_port0->sel_w(!state);
	m_port1->sel_w(state);
}

WRITE_LINE_MEMBER(psxcontrollerports_device::write_sck)
{
	m_port0->clock_w(state);
	m_port1->clock_w(state);
	m_rxd_handler(m_port0->rx_r() && m_port1->rx_r());
}

WRITE_LINE_MEMBER(psxcontrollerports_device::write_txd)
{
	m_port0->tx_w(state);
	m_port1->tx_w(state);
}

void psxcontrollerports_device::ack()
{
	m_dsr_handler(m_port0->ack_r() && m_port1->ack_r());
}

device_psx_controller_interface::device_psx_controller_interface(const machine_config &mconfig, device_t &device) :
		device_slot_card_interface(mconfig, device), m_odata(0), m_idata(0), m_bit(0), m_count(0), m_memcard(false), m_clock(false), m_sel(false),
		m_ack(true), m_rx(false), m_ack_timer(nullptr), m_owner(nullptr)
{
}

device_psx_controller_interface::~device_psx_controller_interface()
{
}

void device_psx_controller_interface::interface_pre_reset()
{
	m_bit = 0;
	m_count = 0;
	m_idata = 0;
	m_memcard = false;

	m_clock = true;
	m_sel = true;
	m_rx = true;
	m_ack = true;
	m_owner->ack();
}

void device_psx_controller_interface::interface_pre_start()
{
	m_owner = dynamic_cast<psx_controller_port_device *>(device().owner());
	m_ack_timer = device().machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(device_psx_controller_interface::ack_timer), this));
}

void device_psx_controller_interface::ack_timer(void *ptr, int param)
{
	m_ack = param;
	m_owner->ack();

	if(!param)
		m_ack_timer->adjust(attotime::from_usec(2), 1);
}

void device_psx_controller_interface::do_pad()
{
	if(!m_bit)
	{
		if(!m_count)
			m_odata = 0xff;
		m_idata = 0;
	}

	m_rx = (m_odata & (1 << m_bit)) ? true : false;
	m_idata |= (m_owner->tx_r()?1:0) << m_bit;
	m_bit = (m_bit + 1) % 8;

	if(!m_bit)
	{
		if((!m_count) && (m_idata & 0xf0))
		{
				m_memcard = true;
				return;
		}

		if(get_pad(m_count++, &m_odata, m_idata))
			m_ack_timer->adjust(attotime::from_usec(10), 0);
		else
			m_count = 0;
	}
}

void device_psx_controller_interface::sel_w(bool state) {
	if(state && !m_sel)
		interface_pre_reset(); // don't reset the controller, just the interface
	m_sel = state;
}

const device_type PSX_STANDARD_CONTROLLER = &device_creator<psx_standard_controller_device>;

psx_standard_controller_device::psx_standard_controller_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
		device_t(mconfig, PSX_STANDARD_CONTROLLER, "Playstation Standard Controller", tag, owner, clock, "psx_standard_controller", __FILE__),
		device_psx_controller_interface(mconfig, *this),
		m_pad0(*this,"PSXPAD0"),
		m_pad1(*this,"PSXPAD1")
{
}

bool psx_standard_controller_device::get_pad(int count, UINT8 *odata, UINT8 idata)
{
	switch(count)
	{
		case 0:
			*odata = 0x41;
			break;
		case 1:
			if(idata != QUERY_PAD_STATE)
				return false;
			*odata = 0x5a;
			break;
		case 2:
			*odata = m_pad0->read();
			break;
		case 3:
			*odata = m_pad1->read();
			break;
		case 4:
			return false;
	}
	return true;
}

static INPUT_PORTS_START( psx_standard_controller )
	PORT_START("PSXPAD0")
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START )
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNUSED )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNUSED )
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SELECT )

	PORT_START("PSXPAD1")
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_NAME("Square")
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_NAME("Cross")
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Circle")
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_NAME("Triangle")
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_NAME("R1")
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_NAME("L1")
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_NAME("R2")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_NAME("L2")
INPUT_PORTS_END

ioport_constructor psx_standard_controller_device::device_input_ports() const
{
	return INPUT_PORTS_NAME(psx_standard_controller);
}