summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/upd4701.c
blob: 5d120bc66006099208d2f6d881654b7873099dd3 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/***************************************************************************

    NEC uPD4701

    Incremental Encoder Control

    2009-06 Converted to be a device

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

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

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _upd4701_state upd4701_state;
struct _upd4701_state
{
	int cs;
	int xy;
	int ul;
	int resetx;
	int resety;
	int latchx;
	int latchy;
	int startx;
	int starty;
	int x;
	int y;
	int switches;
	int latchswitches;
	int cf;
};

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

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


/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

INLINE upd4701_state *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert((device->type() == UPD4701));
	return (upd4701_state *)downcast<upd4701_device *>(device)->token();
}


/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    upd4701_ul_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_ul_w )
{
	upd4701_state *upd4701 = get_safe_token(device);
	upd4701->ul = data;
}

/*-------------------------------------------------
    upd4701_xy_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_xy_w )
{
	upd4701_state *upd4701 = get_safe_token(device);
	upd4701->xy = data;
}

/*-------------------------------------------------
    upd4701_cs_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_cs_w )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if (data != upd4701->cs)
	{
		upd4701->cs = data;

		if (!upd4701->cs)
		{
			upd4701->latchx = (upd4701->x - upd4701->startx) & MASK_COUNTER;
			upd4701->latchy = (upd4701->y - upd4701->starty) & MASK_COUNTER;

			upd4701->latchswitches = (~upd4701->switches) & MASK_SWITCHES;
			if (upd4701->latchswitches != 0)
			{
				upd4701->latchswitches |= 8;
			}

			upd4701->cf = 1;
		}
	}
}

/*-------------------------------------------------
    upd4701_resetx_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_resetx_w )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if (upd4701->resetx != data)
	{
		upd4701->resetx = data;

		if (upd4701->resetx)
		{
			upd4701->startx = upd4701->x;
		}
	}
}

/*-------------------------------------------------
    upd4701_resety_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_resety_w )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if (upd4701->resety != data)
	{
		upd4701->resety = data;

		if (upd4701->resety)
		{
			upd4701->starty = upd4701->y;
		}
	}
}

/*-------------------------------------------------
    upd4701_x_add
-------------------------------------------------*/

WRITE16_DEVICE_HANDLER( upd4701_x_add )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if (!upd4701->resetx && data != 0)
	{
		upd4701->x += data;

		if (upd4701->cs)
		{
			upd4701->cf = 0;
		}
	}
}

/*-------------------------------------------------
    upd4701_y_add
-------------------------------------------------*/

WRITE16_DEVICE_HANDLER( upd4701_y_add )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if (!upd4701->resety && data != 0)
	{
		upd4701->y += data;

		if (upd4701->cs)
		{
			upd4701->cf = 0;
		}
	}
}

/*-------------------------------------------------
    upd4701_switches_set
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( upd4701_switches_set )
{
	upd4701_state *upd4701 = get_safe_token(device);
	upd4701->switches = data;
}

/*-------------------------------------------------
    upd4701_d_r
-------------------------------------------------*/

READ16_DEVICE_HANDLER( upd4701_d_r )
{
	upd4701_state *upd4701 = get_safe_token(device);
	int data;

	if (upd4701->cs)
	{
		return 0xff;
	}

	if (upd4701->xy)
	{
		data = upd4701->latchy;
	}
	else
	{
		data = upd4701->latchx;
	}

	data |= upd4701->latchswitches << 12;

	if (upd4701->ul)
	{
		return data >> 8;
	}
	else
	{
		return data & 0xff;
	}
}

/*-------------------------------------------------
    upd4701_sf_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER( upd4701_sf_r )
{
	upd4701_state *upd4701 = get_safe_token(device);

	if ((upd4701->switches & MASK_SWITCHES) != MASK_SWITCHES)
	{
		return 0;
	}

	return 1;
}

/*-------------------------------------------------
    upd4701_cf_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER( upd4701_cf_r )
{
	upd4701_state *upd4701 = get_safe_token(device);
	return upd4701->cf;
}

/*-------------------------------------------------
    DEVICE_START( upd4701 )
-------------------------------------------------*/

static DEVICE_START( upd4701 )
{
	upd4701_state *upd4701 = get_safe_token(device);

	/* register for state saving */
	device->save_item(NAME(upd4701->cs));
	device->save_item(NAME(upd4701->xy));
	device->save_item(NAME(upd4701->ul));
	device->save_item(NAME(upd4701->resetx));
	device->save_item(NAME(upd4701->resety));
	device->save_item(NAME(upd4701->latchx));
	device->save_item(NAME(upd4701->latchy));
	device->save_item(NAME(upd4701->startx));
	device->save_item(NAME(upd4701->starty));
	device->save_item(NAME(upd4701->x));
	device->save_item(NAME(upd4701->y));
	device->save_item(NAME(upd4701->switches));
	device->save_item(NAME(upd4701->latchswitches));
	device->save_item(NAME(upd4701->cf));
}

/*-------------------------------------------------
    DEVICE_RESET( upd4701 )
-------------------------------------------------*/

static DEVICE_RESET( upd4701 )
{
	upd4701_state *upd4701 = get_safe_token(device);

	upd4701->cs = 1;
	upd4701->xy = 0;
	upd4701->ul = 0;
	upd4701->resetx = 0;
	upd4701->resety = 0;
	upd4701->latchx = 0;
	upd4701->latchy = 0;
	upd4701->startx = 0;
	upd4701->starty = 0;
	upd4701->x = 0;
	upd4701->y = 0;
	upd4701->switches = 0;
	upd4701->latchswitches = 0;
	upd4701->cf = 1;
}

/*-------------------------------------------------
    device definition
-------------------------------------------------*/

DEVICE_GET_INFO(upd4701)
{
 switch (state)
 {
  case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(upd4701_state); break;

  case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(upd4701); break;

  case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME(upd4701); break;

  case DEVINFO_STR_NAME: strcpy(info->s, "NEC uPD4701 Encoder"); break;
 }
}

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, "NEC uPD4701 Encoder", tag, owner, clock)
{
	m_token = global_alloc_array_clear(UINT8, sizeof(upd4701_state));
}

//-------------------------------------------------
//  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()
{
	DEVICE_START_NAME( upd4701 )(this);
}

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

void upd4701_device::device_reset()
{
	DEVICE_RESET_NAME( upd4701 )(this);
}