summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/e0516.cpp
blob: fd09cf6813712d754b597b1df08baebd5e0ca0d1 (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
// license:BSD-3-Clause
// copyright-holders:Curt Coder
/**********************************************************************

    Microelectronic-Marin E050-16 Real Time Clock emulation

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

#include "emu.h"
#include "e0516.h"



//**************************************************************************
//  MACROS / CONSTANTS
//**************************************************************************

#define LOG 0


// states
enum
{
	STATE_ADDRESS = 0,
	STATE_DATA
};



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

// device type definition
const device_type E0516 = &device_creator<e0516_device>;

//-------------------------------------------------
//  e0516_device - constructor
//-------------------------------------------------

e0516_device::e0516_device(const machine_config &mconfig, std::string tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, E0516, "E05-16", tag, owner, clock, "e0516", __FILE__),
		device_rtc_interface(mconfig, *this), m_cs(0), m_clk(0), m_data_latch(0), m_reg_latch(0), m_read_write(0), m_state(0), m_bits(0), m_dio(0), m_timer(nullptr)
{
}


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

void e0516_device::device_start()
{
	// allocate timers
	m_timer = timer_alloc();
	m_timer->adjust(attotime::from_hz(clock() / 32768), 0, attotime::from_hz(clock() / 32768));

	// state saving
	save_item(NAME(m_cs));
	save_item(NAME(m_clk));
	save_item(NAME(m_data_latch));
	save_item(NAME(m_reg_latch));
	save_item(NAME(m_read_write));
	save_item(NAME(m_state));
	save_item(NAME(m_bits));
	save_item(NAME(m_dio));
}


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

void e0516_device::device_reset()
{
	set_current_time(machine());
}


//-------------------------------------------------
//  device_timer - handler timer events
//-------------------------------------------------

void e0516_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	advance_seconds();
}


//-------------------------------------------------
//  cs_w - chip select input
//-------------------------------------------------

WRITE_LINE_MEMBER( e0516_device::cs_w )
{
	if (LOG) logerror("E05-16 '%s' CS %u\n", tag().c_str(), state);

	m_cs = state;

	if (m_cs)
	{
		m_data_latch = 0;
		m_reg_latch = 0;
		m_bits = 0;
		m_state = STATE_ADDRESS;
	}
}


//-------------------------------------------------
//  clk_w - serial clock input
//-------------------------------------------------

WRITE_LINE_MEMBER( e0516_device::clk_w )
{
	if (LOG) logerror("E05-16 '%s' CLK %u\n", tag().c_str(), state);

	m_clk = state;

	if (m_cs || m_clk) return;

	m_bits++;

	if (m_state == STATE_ADDRESS)
	{
		if (LOG) logerror("E05-16 '%s' Command Bit %u\n", tag().c_str(), m_dio);

		// command
		m_reg_latch |= m_dio << 3;
		m_reg_latch >>= 1;

		if (m_bits == 4)
		{
			m_state = STATE_DATA;
			m_bits = 0;

			if (BIT(m_reg_latch, 0))
			{
				// load register value to data latch
				m_data_latch = convert_to_bcd(get_clock_register(m_reg_latch >> 1));
			}
		}
	}
	else
	{
		// data
		if (BIT(m_reg_latch, 0))
		{
			// read
			if (LOG) logerror("E05-16 '%s' Data Bit OUT %u\n", tag().c_str(), m_dio);

			m_dio = BIT(m_data_latch, 0);
			m_data_latch >>= 1;
		}
		else
		{
			// write
			if (LOG) logerror("E05-16 '%s' Data Bit IN %u\n", tag().c_str(), m_dio);

			m_data_latch |= m_dio << 7;
			m_data_latch >>= 1;
		}

		if (m_bits == 8)
		{
			m_state = STATE_ADDRESS;
			m_bits = 0;

			if (!BIT(m_reg_latch, 0))
			{
				// write latched data to register
				set_clock_register(m_reg_latch >> 1, bcd_to_integer(m_data_latch));
			}
		}
	}
}


//-------------------------------------------------
//  dio_w - serial data input
//-------------------------------------------------

WRITE_LINE_MEMBER( e0516_device::dio_w )
{
	if (LOG) logerror("E05-16 '%s' DIO %u\n", tag().c_str(), state);

	m_dio = state;
}


//-------------------------------------------------
//  do_r - serial data output
//-------------------------------------------------

READ_LINE_MEMBER( e0516_device::dio_r )
{
	return m_dio;
}