summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/upd4701.cpp
blob: 37e5d5dadd99c6897b6b2e95d5836be94b81da4c (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// license:BSD-3-Clause
// copyright-holders:smf
/***************************************************************************

    NEC uPD4701

    Incremental Encoder Control

    2009-06 Converted to be a device

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

#include "emu.h"
#include "upd4701.h"

#define MASK_SWITCHES ( 7 )
#define MASK_COUNTER ( 0xfff )

const device_type UPD4701 = &device_creator<upd4701_device>;

upd4701_device::upd4701_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, UPD4701, "uPD4701 Encoder", tag, owner, clock, "upd4701", __FILE__), m_cs(0), m_xy(0), m_ul(0), m_resetx(0), m_resety(0), m_latchx(0), m_latchy(0),
	m_startx(0), m_starty(0), m_x(0), m_y(0), m_switches(0), m_latchswitches(0), m_cf(0)
{
}

//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void upd4701_device::device_config_complete()
{
}

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

void upd4701_device::device_start()
{
	save_item(NAME(m_cs));
	save_item(NAME(m_xy));
	save_item(NAME(m_ul));
	save_item(NAME(m_resetx));
	save_item(NAME(m_resety));
	save_item(NAME(m_latchx));
	save_item(NAME(m_latchy));
	save_item(NAME(m_startx));
	save_item(NAME(m_starty));
	save_item(NAME(m_x));
	save_item(NAME(m_y));
	save_item(NAME(m_switches));
	save_item(NAME(m_latchswitches));
	save_item(NAME(m_cf));
}

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

void upd4701_device::device_reset()
{
	m_cs = 1;
	m_xy = 0;
	m_ul = 0;
	m_resetx = 0;
	m_resety = 0;
	m_latchx = 0;
	m_latchy = 0;
	m_startx = 0;
	m_starty = 0;
	m_x = 0;
	m_y = 0;
	m_switches = 0;
	m_latchswitches = 0;
	m_cf = 1;
}

/* x,y increments can be 12bit (see MASK_COUNTER), hence we need a couple of
16bit handlers in the following  */

/*-------------------------------------------------
    ul_w
-------------------------------------------------*/

WRITE_LINE_MEMBER( upd4701_device::ul_w )
{
	m_ul = state;
}

/*-------------------------------------------------
    xy_w
-------------------------------------------------*/

WRITE_LINE_MEMBER( upd4701_device::xy_w )
{
	m_xy = state;
}

/*-------------------------------------------------
    cs_w
-------------------------------------------------*/

WRITE_LINE_MEMBER( upd4701_device::cs_w )
{
	if (m_cs != state)
	{
		m_cs = state;

		if (!m_cs)
		{
			m_latchx = (m_x - m_startx) & MASK_COUNTER;
			m_latchy = (m_y - m_starty) & MASK_COUNTER;

			m_latchswitches = (~m_switches) & MASK_SWITCHES;
			if (m_latchswitches != 0)
			{
				m_latchswitches |= 8;
			}

			m_cf = 1;
		}
	}
}

/*-------------------------------------------------
    resetx_w
-------------------------------------------------*/

WRITE_LINE_MEMBER( upd4701_device::resetx_w )
{
	if (m_resetx != state)
	{
		m_resetx = state;

		if (m_resetx)
		{
			m_startx = m_x;
		}
	}
}

/*-------------------------------------------------
    resety_w
-------------------------------------------------*/

WRITE_LINE_MEMBER( upd4701_device::resety_w )
{
	if (m_resety != state)
	{
		m_resety = state;

		if (m_resety)
		{
			m_starty = m_y;
		}
	}
}

/*-------------------------------------------------
    x_add
-------------------------------------------------*/

void upd4701_device::x_add( INT16 data )
{
	if (!m_resetx && data != 0)
	{
		m_x += data;

		if (m_cs)
		{
			m_cf = 0;
		}
	}
}

/*-------------------------------------------------
    y_add
-------------------------------------------------*/

void upd4701_device::y_add( INT16 data )
{
	if (!m_resety && data != 0)
	{
		m_y += data;

		if (m_cs)
		{
			m_cf = 0;
		}
	}
}

/*-------------------------------------------------
    switches_set
-------------------------------------------------*/

void upd4701_device::switches_set( UINT8 data )
{
	m_switches = data;
}

/*-------------------------------------------------
    d_r
-------------------------------------------------*/

READ16_MEMBER( upd4701_device::d_r )
{
	int data;

	if (m_cs)
	{
		return 0xff;
	}

	if (m_xy)
	{
		data = m_latchy;
	}
	else
	{
		data = m_latchx;
	}

	data |= m_latchswitches << 12;

	if (m_ul)
	{
		return data >> 8;
	}
	else
	{
		return data & 0xff;
	}
}

/*-------------------------------------------------
    sf_r
-------------------------------------------------*/

READ_LINE_MEMBER( upd4701_device::sf_r )
{
	if ((m_switches & MASK_SWITCHES) != MASK_SWITCHES)
	{
		return 0;
	}

	return 1;
}

/*-------------------------------------------------
    cf_r
-------------------------------------------------*/

READ_LINE_MEMBER( upd4701_device::cf_r )
{
	return m_cf;
}