summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/bus/snes_ctrl/mouse.c
blob: 1d6cb5b52f60dd96bec57f3e9c12777567d7a916 (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
234
235
236
237
238
239
240
241
/**********************************************************************

    Nintendo Super Famicom & SNES Mouse
 
    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

**********************************************************************/

#include "mouse.h"

//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

const device_type SNES_MOUSE = &device_creator<snes_mouse_device>;


static INPUT_PORTS_START( snes_mouse )
	PORT_START("BUTTONS")
	PORT_BIT( 0x00ff, IP_ACTIVE_HIGH, IPT_SPECIAL ) // these must be 0!
	PORT_BIT( 0x0100, IP_ACTIVE_HIGH, IPT_BUTTON2 ) PORT_NAME("Button Right")
	PORT_BIT( 0x0200, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_NAME("Button Left")
	PORT_BIT( 0x0c00, IP_ACTIVE_HIGH, IPT_UNUSED ) // mouse speed: 0 = slow, 1 = normal, 2 = fast, 3 = unused
	PORT_BIT( 0x1000, IP_ACTIVE_HIGH, IPT_UNUSED )
	PORT_BIT( 0x2000, IP_ACTIVE_HIGH, IPT_UNUSED )
	PORT_BIT( 0x4000, IP_ACTIVE_HIGH, IPT_UNUSED )
	PORT_BIT( 0x8000, IP_ACTIVE_LOW, IPT_UNUSED )

	// we use IPT_LIGHTGUN instead of IPT_MOUSE to avoid input to wrap when you reach the screen border
	// due to the relative nature of movement detection in SNES mouse, when we wrap the system would
	// detect a sudden jump in the wrong direction, making the usage unfriendly...
	PORT_START("MOUSE_X")
	PORT_BIT( 0x1ff, 0x100, IPT_LIGHTGUN_X ) PORT_NAME("Superscope X Axis") PORT_SENSITIVITY(30) PORT_KEYDELTA(5)
//	PORT_BIT( 0xff, 0x00, IPT_MOUSE_X) PORT_SENSITIVITY(30) PORT_KEYDELTA(5)

	PORT_START("MOUSE_Y")
	PORT_BIT( 0x1ff, 0x100, IPT_LIGHTGUN_Y) PORT_NAME("Superscope Y Axis") PORT_SENSITIVITY(30) PORT_KEYDELTA(5)
//	PORT_BIT( 0xff, 0x00, IPT_MOUSE_Y) PORT_SENSITIVITY(30) PORT_KEYDELTA(5)
INPUT_PORTS_END


//-------------------------------------------------
//  input_ports - device-specific input ports
//-------------------------------------------------

ioport_constructor snes_mouse_device::device_input_ports() const
{
	return INPUT_PORTS_NAME( snes_mouse );
}



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  snes_mouse_device - constructor
//-------------------------------------------------

snes_mouse_device::snes_mouse_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
					device_t(mconfig, SNES_MOUSE, "Nintendo SNES / SFC Mouse Controller", tag, owner, clock, "snes_mouse", __FILE__),
					device_snes_control_port_interface(mconfig, *this),
					m_buttons(*this, "BUTTONS"),
					m_xaxis(*this, "MOUSE_X"),
					m_yaxis(*this, "MOUSE_Y")
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void snes_mouse_device::device_start()
{
	save_item(NAME(m_strobe));
	save_item(NAME(m_idx));
	save_item(NAME(m_latch));
	save_item(NAME(m_x));
	save_item(NAME(m_y));
	save_item(NAME(m_oldx));
	save_item(NAME(m_oldy));
	save_item(NAME(m_deltax));
	save_item(NAME(m_deltay));
	save_item(NAME(m_speed));
	save_item(NAME(m_dirx));
	save_item(NAME(m_diry));
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void snes_mouse_device::device_reset()
{
	m_strobe = 0;
	m_idx = 0;
	m_latch = 0;
	m_x = 0;
	m_y = 0;
	m_oldx = 0;
	m_oldy = 0;
	m_deltax = 0;
	m_deltay = 0;
	m_speed = 0;
	m_dirx = -1;
	m_diry = -1;
}


//-------------------------------------------------
//  poll
//-------------------------------------------------

void snes_mouse_device::port_poll()
{
	INT16 var;
	int new_dir;
	m_idx = 0;
	m_latch = m_buttons->read();

	m_oldx = m_x;
	m_oldy = m_y;
	m_x = m_xaxis->read();
	m_y = m_yaxis->read();

	var = m_x - m_oldx;
	if (var)
	{
		new_dir = (var < 0) ? 1 : 0;
		if (m_dirx != new_dir)
			m_dirx = new_dir;
	}

	if (var < -127)
	{
		m_deltax = 0x7f;
		m_oldx -= 127;
	}
	else if (var < 0)
	{
		m_deltax = -var;
		m_oldx = m_x;
	}
	else if (var > 127)
	{
		m_deltax = 0x7f;
		m_oldx += 127;
	}
	else
	{
		m_deltax = var;
		m_oldx = m_x;
	}
	
	var = m_y - m_oldy;
	if (var)
	{
		new_dir = (var < 0) ? 1 : 0;
		if (m_diry != new_dir)
			m_diry = new_dir;
	}
	
	if (var < -127)
	{
		m_deltay = 0x7f;
		m_oldy -= 127;
	}
	else if (var < 0)
	{
		m_deltay = -var;
		m_oldy = m_y;
	}
	else if (var > 127)
	{
		m_deltay = 0x7f;
		m_oldy += 127;
	}
	else
	{
		m_deltay = var;
		m_oldy = m_y;
	}

	m_deltax |= (m_dirx << 7);
	m_deltay |= (m_diry << 7);
}

//-------------------------------------------------
//  read
//-------------------------------------------------

UINT8 snes_mouse_device::read_pin4()
{
	UINT8 res = 0;
	
	if (m_strobe == 1)
	{
		// reading with strobe 1, changes mouse speed
		m_speed = (m_speed + 1) % 3;
		return res;
	}
	
	if (m_idx >= 32)
		res |= 0x01;
	else if (m_idx >= 24)
		res |= BIT(m_deltax, (31 - m_idx++));
	else if (m_idx >= 16)
		res |= BIT(m_deltay, (23 - m_idx++));
	else if (m_idx == 11)
	{
		res |= BIT(m_speed, 0);
		m_idx++;
	}
	else if (m_idx == 10)
	{
		res |= BIT(m_speed, 1);
		m_idx++;
	}
	else
		res |= BIT(m_latch, m_idx++);
	
	return res;
}

//-------------------------------------------------
//  write
//-------------------------------------------------

void snes_mouse_device::write_strobe(UINT8 data)
{
	int old = m_strobe;
	m_strobe = data & 0x01;

	if (m_strobe < old)	// 1 -> 0 transition
		port_poll();
}