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

    RP5H01


    2009-06 Converted to be a device

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

#include "driver.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
***************************************************************************/

typedef struct _rp5h01_state rp5h01_state;
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(const device_config *device)
{
	assert(device != NULL);
	assert(device->token != NULL);
	assert((device->type == RP5H01));
	return (rp5h01_state *)device->token;
}

INLINE const rp5h01_interface *get_interface(const device_config *device)
{
	assert(device != NULL);
	assert((device->type == RP5H01));
	return (const rp5h01_interface *) device->static_config;
}

/***************************************************************************
    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_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);
	const rp5h01_interface *intf = get_interface(device);

	rp5h01->data = &(memory_region(device->machine, intf->region)[intf->offset]);

	/* register for state saving */
	state_save_register_device_item(device, 0, rp5h01->counter);
	state_save_register_device_item(device, 0, rp5h01->counter_mode);
	state_save_register_device_item(device, 0, rp5h01->enabled);
	state_save_register_device_item(device, 0, rp5h01->old_reset);
	state_save_register_device_item(device, 0, 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;
}

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

static const char *DEVTEMPLATE_SOURCE = __FILE__;

#define DEVTEMPLATE_ID(p,s)		p##rp5h01##s
#define DEVTEMPLATE_FEATURES	DT_HAS_START | DT_HAS_RESET
#define DEVTEMPLATE_NAME		"RP5H01"
#define DEVTEMPLATE_FAMILY		"RP5H01"
#define DEVTEMPLATE_CLASS		DEVICE_CLASS_PERIPHERAL
#include "devtempl.h"