summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/latch8.cpp
blob: 2bb8e1ddbb092cae1b7fee78a3a00fb24212854a (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
// 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_t new_val, uint8_t mask)
{
	const uint8_t old_val = m_value;

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

	if (m_has_write)
	{
		uint8_t changed = old_val ^ m_value;
		for (int i = 0; i < 8; i++)
			if (BIT(changed, i))
				m_write_cb[i](BIT(m_value, i));
	}
}

TIMER_CALLBACK_MEMBER( latch8_device::timerproc )
{
	const uint8_t new_val = param & 0xff;
	const uint8_t mask = param >> 8;

	update(new_val, mask);
}

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

uint8_t latch8_device::read(offs_t offset)
{
	assert(offset == 0);

	uint8_t res = m_value;
	if (m_has_read)
	{
		for (int i = 0; i < 8; i++)
		{
			if (!m_read_cb[i].isunset())
				res = (res & ~(1 << i)) | (m_read_cb[i]() << i);
		}
	}
	return (res & ~m_maskout) ^ m_xorvalue;
}


void latch8_device::write(offs_t offset, uint8_t data)
{
	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);
}


void latch8_device::reset_w(offs_t offset, uint8_t data)
{
	assert(offset == 0);

	m_value = 0;
}

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

template <int Bit> inline void latch8_device::bitx_w(offs_t offset, uint8_t data)
{
	const uint8_t mask = 1 << offset;
	const uint8_t masked_data = BIT(data, Bit) << 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);
}

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

DEFINE_DEVICE_TYPE(LATCH8, latch8_device, "latch8", "8-bit latch")

latch8_device::latch8_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, LATCH8, tag, owner, clock)
	, m_write_cb(*this)
	, m_read_cb(*this, 0)
	, m_value(0)
	, m_has_write(false)
	, m_has_read(false)
	, m_maskout(0)
	, m_xorvalue(0)
	, m_nosync(0)
{
}


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

void latch8_device::device_validity_check(validity_checker &valid) const
{
	for (int i = 0; i < 8; i++)
		if (!m_read_cb[i].isunset() && !m_write_cb[i].isunset())
			osd_printf_error("Device %s: Bit %d already has a handler.\n", tag(), i);
}

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

void latch8_device::device_start()
{
	// setup nodemap
	for (auto &cb : m_write_cb)
	{
		if (!cb.isunset())
			m_has_write = true;
	}

	// setup device read handlers
	for (auto &cb : m_read_cb)
	{
		if (!cb.isunset())
			m_has_read = true;
	}

	save_item(NAME(m_value));
}

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

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