summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/latch8.cpp
blob: 7b10bed2df6d54db47a7ea7d224f849fec99d77f (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
// 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)
{
	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].isnull())
				m_write_cb[i](BIT(m_value, i));
	}
}

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

	update( new_val, mask);
}

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

READ8_MEMBER( latch8_device::read )
{
	uint8_t res;

	assert(offset == 0);

	res = m_value;
	if (m_has_read)
	{
		for (int i = 0; i < 8; i++)
		{
			if (!m_read_cb[i].isnull())
				res = (res & ~(1 << i)) | (m_read_cb[i]() << 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;
}

/* 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_t data)
{
	uint8_t mask = (1<<offset);
	uint8_t 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); }

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_value(0)
	, m_has_write(0)
	, m_has_read(0)
	, m_maskout(0)
	, m_xorvalue(0)
	, m_nosync(0)
	, m_write_cb{{*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this}}
	, m_read_cb{{*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this}}
{
}


//-------------------------------------------------
//  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].isnull() && !m_write_cb[i].isnull())
			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.isnull()) m_has_write = 1;
		cb.resolve();
	}

	/* setup device read handlers */
	for (auto &cb : m_read_cb)
	{
		if (!cb.isnull()) m_has_read = 1;
		cb.resolve();
	}

	save_item(NAME(m_value));
}

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

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