summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/rp5h01.c
blob: 3abed1220da0e4dabd6ca5d02af6627fe2f08832 (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
#include "driver.h"
#include "machine/rp5h01.h"

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

/* local copy of the interface pointer */
static const struct RP5H01_interface *intf;

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

typedef struct _RP5H01 {
	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;
} RP5H01;

static RP5H01 RP5H01_state[MAX_RP5H01];

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

int RP5H01_init( const struct RP5H01_interface *interface ) {
	int i;

	/* setup our local copy of the interface */
	intf = interface;

	if ( intf->num > MAX_RP5H01 ) {
		logerror( "Requested number of RP5H01's is bigger than the supported amount\n" );
		return -1;
	}

	/* initialize the state */
	for( i = 0; i < intf->num; i++ ) {
		RP5H01_state[i].counter = 0;
		RP5H01_state[i].counter_mode = COUNTER_MODE_6_BITS;
		RP5H01_state[i].data = &( memory_region( intf->region[i] )[ intf->offset[i] ] );
		RP5H01_state[i].enabled = 0;
		RP5H01_state[i].old_reset = -1;
		RP5H01_state[i].old_clock = -1;
	}

	return 0;
}

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

void RP5H01_enable_w( int which, int data ) {
	RP5H01	*chip;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

void RP5H01_reset_w( int which, int data ) {
	RP5H01	*chip;
	int		newstate = ( data == 0 ) ? 0 : 1;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

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

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

void RP5H01_clock_w( int which, int data ) {
	RP5H01	*chip;
	int		newstate = ( data == 0 ) ? 0 : 1;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

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

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

void RP5H01_test_w( int which, int data ) {
	RP5H01	*chip;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

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

int RP5H01_counter_r( int which ) {
	RP5H01	*chip;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return 0;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

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

int RP5H01_data_r( int which ) {
	RP5H01	*chip;
	int		byte, bit;

	if ( which >= intf->num ) {
		logerror( "RP5H01_enable: trying to access an unmapped chip\n" );
		return 0;
	}

	/* get the chip */
	chip = &RP5H01_state[which];

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

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

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

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

WRITE8_HANDLER( RP5H01_0_enable_w ) {
	RP5H01_enable_w( 0, data );
}

WRITE8_HANDLER( RP5H01_0_reset_w ) {
	RP5H01_reset_w( 0, data );
}

WRITE8_HANDLER( RP5H01_0_clock_w ) {
	RP5H01_clock_w( 0, data );
}

WRITE8_HANDLER( RP5H01_0_test_w ) {
	RP5H01_test_w( 0, data );
}

READ8_HANDLER( RP5H01_0_counter_r ) {
	return RP5H01_counter_r( 0 );
}

READ8_HANDLER( RP5H01_0_data_r ) {
	return RP5H01_data_r( 0 );
}