summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/rp5h01.c
blob: 35e3f3fae5bdc93194c66615236af5726eca5cdb (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
/***************************************************************************

    RP5H01

	TODO:
	- convert to modern and follow the datasheet better (all dumps
	  presumably needs to be redone from scratch?)

    2009-06 Converted to be a device

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

#include "emu.h"
#include "machine/rp5h01.h"


/***************************************************************************
    PARAMETERS
***************************************************************************/

/* these also work as the address masks */
enum {
	COUNTER_MODE_6_BITS = 0x3f,
	COUNTER_MODE_7_BITS = 0x7f
};


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

struct rp5h01_state
{
	int counter;
	int counter_mode;	/* test pin */
	int enabled;		/* chip enable */
	int old_reset;		/* reset pin state (level-triggered) */
	int old_clock;		/* clock pin state (level-triggered) */
	UINT8 *data;
};

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

INLINE rp5h01_state *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert((device->type() == RP5H01));
	return (rp5h01_state *)downcast<rp5h01_device *>(device)->token();
}

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

/*-------------------------------------------------
    rp5h01_enable_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( rp5h01_enable_w )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	/* process the /CE signal and enable/disable the IC */
	rp5h01->enabled = (data == 0) ? 1 : 0;
}

/*-------------------------------------------------
    rp5h01_reset_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( rp5h01_reset_w )
{
	rp5h01_state *rp5h01 = get_safe_token(device);
	int newstate = (data == 0) ? 0 : 1;

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return;

	/* now look for a 0->1 transition */
	if (rp5h01->old_reset == 0 && newstate == 1)
	{
		/* reset the counter */
		rp5h01->counter = 0;
	}

	/* update the pin */
	rp5h01->old_reset = newstate;
}

/*-------------------------------------------------
    rp5h01_cs_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( rp5h01_cs_w )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return;

	if (data == 1)
	{
		/* reset the counter */
		rp5h01->counter = 0;
	}
}

/*-------------------------------------------------
    rp5h01_clock_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( rp5h01_clock_w )
{
	rp5h01_state *rp5h01 = get_safe_token(device);
	int newstate = (data == 0) ? 0 : 1;

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return;

	/* now look for a 1->0 transition */
	if (rp5h01->old_clock == 1 && newstate == 0)
	{
		/* increment the counter, and mask it with the mode */
		rp5h01->counter++;
	}

	/* update the pin */
	rp5h01->old_clock = newstate;
}

/*-------------------------------------------------
    rp5h01_test_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( rp5h01_test_w )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return;

	/* process the test signal and change the counter mode */
	rp5h01->counter_mode = (data == 0) ? COUNTER_MODE_6_BITS : COUNTER_MODE_7_BITS;
}

/*-------------------------------------------------
    rp5h01_counter_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER( rp5h01_counter_r )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return 0; /* ? (should be high impedance) */

	/* return A5 */
	return (rp5h01->counter >> 5) & 1;
}

/*-------------------------------------------------
    rp5h01_data_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER( rp5h01_data_r )
{
	rp5h01_state *rp5h01 = get_safe_token(device);
	int byte, bit;

	/* if it's not enabled, ignore */
	if (!rp5h01->enabled)
		return 0; /* ? (should be high impedance) */

	/* get the byte offset and bit offset */
	byte = (rp5h01->counter & rp5h01->counter_mode) >> 3;
	bit = 7 - (rp5h01->counter & 7);

	/* return the data */
	return (rp5h01->data[byte] >> bit) & 1;
}

/*-------------------------------------------------
    DEVICE_START( rp5h01 )
-------------------------------------------------*/

static DEVICE_START( rp5h01 )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	assert(device->static_config() == NULL);

	rp5h01->data = *device->region();

	/* register for state saving */
	device->save_item(NAME(rp5h01->counter));
	device->save_item(NAME(rp5h01->counter_mode));
	device->save_item(NAME(rp5h01->enabled));
	device->save_item(NAME(rp5h01->old_reset));
	device->save_item(NAME(rp5h01->old_clock));
}

/*-------------------------------------------------
    DEVICE_RESET( rp5h01 )
-------------------------------------------------*/

static DEVICE_RESET( rp5h01 )
{
	rp5h01_state *rp5h01 = get_safe_token(device);

	rp5h01->counter = 0;
	rp5h01->counter_mode = COUNTER_MODE_6_BITS;
	rp5h01->enabled = 0;
	rp5h01->old_reset = -1;
	rp5h01->old_clock = -1;
}

const device_type RP5H01 = &device_creator<rp5h01_device>;

rp5h01_device::rp5h01_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, RP5H01, "RP5H01", tag, owner, clock)
{
	m_token = global_alloc_array_clear(UINT8, sizeof(rp5h01_state));
}

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

void rp5h01_device::device_config_complete()
{
}

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

void rp5h01_device::device_start()
{
	DEVICE_START_NAME( rp5h01 )(this);
}

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

void rp5h01_device::device_reset()
{
	DEVICE_RESET_NAME( rp5h01 )(this);
}