summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/latch8.cpp
blob: b454de88535999f266098d432d47a6ad8ab77868 (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
// license:BSD-3-Clause
// copyright-holders:Couriersud
/**********************************************************************

    8 bit latch interface and emulation

    2008/08     couriersud

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

#include "emu.h"
#include "latch8.h"

void latch8_device::update(UINT8 new_val, UINT8 mask)
{
	UINT8 old_val = m_value;

	m_value = (m_value & ~mask) | (new_val & mask);

	if (m_has_write)
	{
		int i;
		UINT8 changed = old_val ^ m_value;
		for (i=0; i<8; i++)
			if (((changed & (1<<i)) != 0)) {
				if (i==0 && !m_write_0.isnull()) m_write_0(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==1 && !m_write_1.isnull()) m_write_1(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==2 && !m_write_2.isnull()) m_write_2(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==3 && !m_write_3.isnull()) m_write_3(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==4 && !m_write_4.isnull()) m_write_4(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==5 && !m_write_5.isnull()) m_write_5(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==6 && !m_write_6.isnull()) m_write_6(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
				if (i==7 && !m_write_7.isnull()) m_write_7(machine().driver_data()->generic_space(), m_offset[i] , (m_value >> i) & 1);
			}
	}
}

TIMER_CALLBACK_MEMBER( latch8_device::timerproc )
{
	UINT8 new_val = param & 0xFF;
	UINT8 mask = param >> 8;

	update( new_val, mask);
}

/* ----------------------------------------------------------------------- */

READ8_MEMBER( latch8_device::read )
{
	UINT8 res;

	assert(offset == 0);

	res = m_value;
	if (m_has_read)
	{
		int i;
		for (i=0; i<8; i++)
		{
			if (i==0 && !m_read_0.isnull()) { res &= ~( 1 << i); res |= ((m_read_0(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==1 && !m_read_1.isnull()) { res &= ~( 1 << i); res |= ((m_read_1(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==2 && !m_read_2.isnull()) { res &= ~( 1 << i); res |= ((m_read_2(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==3 && !m_read_3.isnull()) { res &= ~( 1 << i); res |= ((m_read_3(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==4 && !m_read_4.isnull()) { res &= ~( 1 << i); res |= ((m_read_4(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==5 && !m_read_5.isnull()) { res &= ~( 1 << i); res |= ((m_read_5(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==6 && !m_read_6.isnull()) { res &= ~( 1 << i); res |= ((m_read_6(space, 0, 0xff) >> m_offset[i]) & 0x01) << i; }
			if (i==7 && !m_read_7.isnull()) { res &= ~( 1 << i); res |= ((m_read_7(space, 0, 0xff) >> m_offset[i]) & 0x01) << i;}
		}
	}
	return (res & ~m_maskout) ^ m_xorvalue;
}


WRITE8_MEMBER( latch8_device::write )
{
	assert(offset == 0);

	if (m_nosync != 0xff)
		machine().scheduler().synchronize(timer_expired_delegate(FUNC(latch8_device::timerproc),this), (0xFF << 8) | data);
	else
		update(data, 0xFF);
}


WRITE8_MEMBER( latch8_device::reset_w )
{
	assert(offset == 0);

	m_value = 0;
}

/* read bit x                 */
/* return (latch >> x) & 0x01 */

UINT8 latch8_device::bitx_r( offs_t offset, int bit)
{
	assert( offset == 0);

	return (m_value >> bit) & 0x01;
}

READ8_MEMBER( latch8_device::bit0_r) { return bitx_r(offset, 0); }
READ8_MEMBER( latch8_device::bit1_r) { return bitx_r(offset, 1); }
READ8_MEMBER( latch8_device::bit2_r) { return bitx_r(offset, 2); }
READ8_MEMBER( latch8_device::bit3_r) { return bitx_r(offset, 3); }
READ8_MEMBER( latch8_device::bit4_r) { return bitx_r(offset, 4); }
READ8_MEMBER( latch8_device::bit5_r) { return bitx_r(offset, 5); }
READ8_MEMBER( latch8_device::bit6_r) { return bitx_r(offset, 6); }
READ8_MEMBER( latch8_device::bit7_r) { return bitx_r(offset, 7); }

READ8_MEMBER( latch8_device::bit0_q_r) { return bitx_r(offset, 0) ^ 1; }
READ8_MEMBER( latch8_device::bit1_q_r) { return bitx_r(offset, 1) ^ 1; }
READ8_MEMBER( latch8_device::bit2_q_r) { return bitx_r(offset, 2) ^ 1; }
READ8_MEMBER( latch8_device::bit3_q_r) { return bitx_r(offset, 3) ^ 1; }
READ8_MEMBER( latch8_device::bit4_q_r) { return bitx_r(offset, 4) ^ 1; }
READ8_MEMBER( latch8_device::bit5_q_r) { return bitx_r(offset, 5) ^ 1; }
READ8_MEMBER( latch8_device::bit6_q_r) { return bitx_r(offset, 6) ^ 1; }
READ8_MEMBER( latch8_device::bit7_q_r) { return bitx_r(offset, 7) ^ 1; }

/* write bit x from data into bit determined by offset */
/* latch = (latch & ~(1<<offset)) | (((data >> x) & 0x01) << offset) */

void latch8_device::bitx_w(int bit, offs_t offset, UINT8 data)
{
	UINT8 mask = (1<<offset);
	UINT8 masked_data = (((data >> bit) & 0x01) << offset);

	assert( offset < 8);

	/* No need to synchronize ? */
	if (m_nosync & mask)
		update(masked_data, mask);
	else
		machine().scheduler().synchronize(timer_expired_delegate(FUNC(latch8_device::timerproc),this), (mask << 8) | masked_data);
}

WRITE8_MEMBER( latch8_device::bit0_w ) { bitx_w(0, offset, data); }
WRITE8_MEMBER( latch8_device::bit1_w ) { bitx_w(1, offset, data); }
WRITE8_MEMBER( latch8_device::bit2_w ) { bitx_w(2, offset, data); }
WRITE8_MEMBER( latch8_device::bit3_w ) { bitx_w(3, offset, data); }
WRITE8_MEMBER( latch8_device::bit4_w ) { bitx_w(4, offset, data); }
WRITE8_MEMBER( latch8_device::bit5_w ) { bitx_w(5, offset, data); }
WRITE8_MEMBER( latch8_device::bit6_w ) { bitx_w(6, offset, data); }
WRITE8_MEMBER( latch8_device::bit7_w ) { bitx_w(7, offset, data); }

const device_type LATCH8 = &device_creator<latch8_device>;

latch8_device::latch8_device(const machine_config &mconfig, std::string tag, device_t *owner, UINT32 clock)
		: device_t(mconfig, LATCH8, "8 bit latch", tag, owner, clock, "latch8", __FILE__),
		m_value(0),
		m_has_write(0),
		m_has_read(0),
		m_maskout(0),
		m_xorvalue(0),
		m_nosync(0),
		m_write_0(*this),
		m_write_1(*this),
		m_write_2(*this),
		m_write_3(*this),
		m_write_4(*this),
		m_write_5(*this),
		m_write_6(*this),
		m_write_7(*this),
		m_read_0(*this),
		m_read_1(*this),
		m_read_2(*this),
		m_read_3(*this),
		m_read_4(*this),
		m_read_5(*this),
		m_read_6(*this),
		m_read_7(*this)
{
	memset(m_offset, 0, sizeof(m_offset));
}


//-------------------------------------------------
//  device_validity_check - validate device
//  configuration
//-------------------------------------------------

void latch8_device::device_validity_check(validity_checker &valid) const
{
	if (!m_read_0.isnull() && !m_write_0.isnull()) osd_printf_error("Device %s: Bit 0 already has a handler.\n", tag().c_str());
	if (!m_read_1.isnull() && !m_write_1.isnull()) osd_printf_error("Device %s: Bit 1 already has a handler.\n", tag().c_str());
	if (!m_read_2.isnull() && !m_write_2.isnull()) osd_printf_error("Device %s: Bit 2 already has a handler.\n", tag().c_str());
	if (!m_read_3.isnull() && !m_write_3.isnull()) osd_printf_error("Device %s: Bit 3 already has a handler.\n", tag().c_str());
	if (!m_read_4.isnull() && !m_write_4.isnull()) osd_printf_error("Device %s: Bit 4 already has a handler.\n", tag().c_str());
	if (!m_read_5.isnull() && !m_write_5.isnull()) osd_printf_error("Device %s: Bit 5 already has a handler.\n", tag().c_str());
	if (!m_read_6.isnull() && !m_write_6.isnull()) osd_printf_error("Device %s: Bit 6 already has a handler.\n", tag().c_str());
	if (!m_read_7.isnull() && !m_write_7.isnull()) osd_printf_error("Device %s: Bit 7 already has a handler.\n", tag().c_str());
}
//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void latch8_device::device_start()
{
	m_write_0.resolve();
	m_write_1.resolve();
	m_write_2.resolve();
	m_write_3.resolve();
	m_write_4.resolve();
	m_write_5.resolve();
	m_write_6.resolve();
	m_write_7.resolve();

	m_read_0.resolve();
	m_read_1.resolve();
	m_read_2.resolve();
	m_read_3.resolve();
	m_read_4.resolve();
	m_read_5.resolve();
	m_read_6.resolve();
	m_read_7.resolve();

	/* setup nodemap */
	if (!m_write_0.isnull()) m_has_write = 1;
	if (!m_write_1.isnull()) m_has_write = 1;
	if (!m_write_2.isnull()) m_has_write = 1;
	if (!m_write_3.isnull()) m_has_write = 1;
	if (!m_write_4.isnull()) m_has_write = 1;
	if (!m_write_5.isnull()) m_has_write = 1;
	if (!m_write_6.isnull()) m_has_write = 1;
	if (!m_write_7.isnull()) m_has_write = 1;

	/* setup device read handlers */
	if (!m_read_0.isnull()) m_has_read = 1;
	if (!m_read_1.isnull()) m_has_read = 1;
	if (!m_read_2.isnull()) m_has_read = 1;
	if (!m_read_3.isnull()) m_has_read = 1;
	if (!m_read_4.isnull()) m_has_read = 1;
	if (!m_read_5.isnull()) m_has_read = 1;
	if (!m_read_6.isnull()) m_has_read = 1;
	if (!m_read_7.isnull()) m_has_read = 1;

	save_item(NAME(m_value));
}

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

void latch8_device::device_reset()
{
	m_value = 0;
}