summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/icm7170.cpp
blob: 97be23ee0d2ce3497efa30743a45d343efc6d7dd (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/**********************************************************************

    Intersil/Renesas ICM7170 Real Time Clock

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

#include "emu.h"
#include "icm7170.h"

#include "coreutil.h"

#define VERBOSE (0)
#include "logmacro.h"

DEFINE_DEVICE_TYPE(ICM7170, icm7170_device, "icm7170", "Intersil/Renesas ICM7170 Real Time Clock")

// internal registers
enum
{
	REG_CNT_100TH_SEC = 0,
	REG_CNT_HOURS,
	REG_CNT_MINUTES,
	REG_CNT_SECONDS,
	REG_CNT_MONTH,
	REG_CNT_DAY,
	REG_CNT_YEAR,
	REG_CNT_DAY_OF_WEEK,
	REG_RAM_100TH_SEC,
	REG_RAM_HOURS,
	REG_RAM_MINUTES,
	REG_RAM_SECONDS,
	REG_RAM_MONTH,
	REG_RAM_DAY,
	REG_RAM_YEAR,
	REG_RAM_DAY_OF_WEEK,
	REG_INT_STATUS_AND_MASK,
	REG_COMMAND
};

enum
{
	CMD_REG_TEST_MODE = 0x20,
	CMD_REG_IRQ_ENABLE = 0x10,
	CMD_REG_RUN = 0x08,
	CMD_REG_24_HOUR = 0x04,
	CMD_REG_FREQ_MASK = 0x03
};

enum
{
	IRQ_BIT_GLOBAL = 0x80,
	IRQ_BIT_DAY = 0x40,
	IRQ_BIT_HOUR = 0x20,
	IRQ_BIT_MINUTE = 0x10,
	IRQ_BIT_SECOND = 0x08,
	IRQ_BIT_10TH_SECOND = 0x04,
	IRQ_BIT_100TH_SECOND = 0x02,
	IRQ_BIT_ALARM = 0x01
};

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

//-------------------------------------------------
//  icm7170_device - constructor
//-------------------------------------------------

icm7170_device::icm7170_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, ICM7170, tag, owner, clock),
		device_rtc_interface(mconfig, *this),
		device_nvram_interface(mconfig, *this),
		m_out_irq_cb(*this),
		m_out_irq_state(false)
{
}

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

void icm7170_device::device_start()
{
	m_timer = timer_alloc(FUNC(icm7170_device::clock_tick), this);

	// TODO: frequency should be based on input clock and divisor
	m_timer->adjust(attotime::from_hz(100), 0, attotime::from_hz(100));
	m_timer->enable(false);

	save_item(NAME(m_regs));
	save_item(NAME(m_out_irq_state));
}

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

void icm7170_device::device_reset()
{
	m_regs[REG_COMMAND] &= ~CMD_REG_RUN;
	m_irq_status = 0;
	recalc_irqs();
}

//-------------------------------------------------
//  clock_tick - advance the RTC's counters
//-------------------------------------------------

TIMER_CALLBACK_MEMBER(icm7170_device::clock_tick)
{
	// advance hundredths
	m_irq_status |= IRQ_BIT_100TH_SECOND;
	if (m_regs[REG_CNT_100TH_SEC]++ == 99)
	{
		m_regs[REG_CNT_100TH_SEC] = 0;

		if (m_regs[REG_COMMAND] & CMD_REG_24_HOUR)
			LOG("clock_tick %02d-%02d-%02d %02d:%02d:%02d\n",
				m_regs[REG_CNT_YEAR], m_regs[REG_CNT_MONTH], m_regs[REG_CNT_DAY],
				m_regs[REG_CNT_HOURS], m_regs[REG_CNT_MINUTES], m_regs[REG_CNT_SECONDS]);
		else
			LOG("clock_tick %02d-%02d-%02d %02d:%02d:%02d %s\n",
				m_regs[REG_CNT_YEAR], m_regs[REG_CNT_MONTH], m_regs[REG_CNT_DAY],
				m_regs[REG_CNT_HOURS] & 0xf, m_regs[REG_CNT_MINUTES], m_regs[REG_CNT_SECONDS],
				(m_regs[REG_CNT_HOURS] & 0x80) ? "pm" : "am");

		// advance seconds
		m_irq_status |= IRQ_BIT_SECOND;
		if (m_regs[REG_CNT_SECONDS]++ == 59)
		{
			m_regs[REG_CNT_SECONDS] = 0;

			// advance minutes
			m_irq_status |= IRQ_BIT_MINUTE;
			if (m_regs[REG_CNT_MINUTES]++ == 59)
			{
				m_regs[REG_CNT_MINUTES] = 0;

				// invert am/pm bit at 12:00
				if (!(m_regs[REG_COMMAND] & CMD_REG_24_HOUR) && (m_regs[REG_CNT_HOURS] & 0xf) == 11)
					m_regs[REG_CNT_HOURS] ^= 0x80;

				// advance hours
				m_irq_status |= IRQ_BIT_HOUR;
				if (((m_regs[REG_COMMAND] & CMD_REG_24_HOUR) && (m_regs[REG_CNT_HOURS] == 23))
				|| (!(m_regs[REG_COMMAND] & CMD_REG_24_HOUR) && (m_regs[REG_CNT_HOURS] & 0xf) == 12))
				{
					if (m_regs[REG_COMMAND] & CMD_REG_24_HOUR)
						m_regs[REG_CNT_HOURS] = 0;
					else
						m_regs[REG_CNT_HOURS] = (m_regs[REG_CNT_HOURS] & 0x80) | 1;

					// advance days
					m_irq_status |= IRQ_BIT_DAY;
					if (m_regs[REG_CNT_DAY]++ == gregorian_days_in_month(m_regs[REG_CNT_MONTH] & 0xf, (m_regs[REG_CNT_YEAR] & 0x7f) + 2000))
					{
						m_regs[REG_CNT_DAY] = 1;

						// advance months
						if (m_regs[REG_CNT_MONTH]++ == 12)
						{
							m_regs[REG_CNT_MONTH] = 1;

							// advance years
							if (m_regs[REG_CNT_YEAR]++ == 99)
								m_regs[REG_CNT_YEAR] = 0;
						}
					}

					// advance day of week
					if (m_regs[REG_CNT_DAY_OF_WEEK]++ == 6)
						m_regs[REG_CNT_DAY_OF_WEEK] = 0;
				}
				else
					m_regs[REG_CNT_HOURS]++;
			}
		}
	}

	// check tenths interrupt
	if (!(m_regs[REG_CNT_100TH_SEC] % 10))
		m_irq_status |= IRQ_BIT_10TH_SECOND;

	// check alarm
	if (m_irq_mask & IRQ_BIT_ALARM)
		if (BIT(m_regs[REG_RAM_YEAR], 7) || !((m_regs[REG_RAM_YEAR] ^ m_regs[REG_CNT_YEAR]) & 0x7f))
			if (BIT(m_regs[REG_RAM_MONTH], 7) || !((m_regs[REG_RAM_MONTH] ^ m_regs[REG_CNT_MONTH]) & 0xf))
				if (BIT(m_regs[REG_RAM_DAY], 7) || !((m_regs[REG_RAM_DAY] ^ m_regs[REG_CNT_DAY]) & 0x1f))
					if (BIT(m_regs[REG_RAM_HOURS], 6)
					|| ((m_regs[REG_COMMAND] & CMD_REG_24_HOUR) && !((m_regs[REG_RAM_HOURS] ^ m_regs[REG_CNT_HOURS]) & 0x1f))
					|| (!(m_regs[REG_COMMAND] & CMD_REG_24_HOUR) && !((m_regs[REG_RAM_HOURS] ^ m_regs[REG_CNT_HOURS]) & 0x8f)))
						if (BIT(m_regs[REG_RAM_MINUTES], 7) || !((m_regs[REG_RAM_MINUTES] ^ m_regs[REG_CNT_MINUTES]) & 0x3f))
							if (BIT(m_regs[REG_RAM_SECONDS], 7) || !((m_regs[REG_RAM_SECONDS] ^ m_regs[REG_CNT_SECONDS]) & 0x3f))
								if (BIT(m_regs[REG_RAM_100TH_SEC], 7) || !((m_regs[REG_RAM_100TH_SEC] ^ m_regs[REG_CNT_100TH_SEC]) & 0x7f))
									m_irq_status |= IRQ_BIT_ALARM;

	recalc_irqs();
}


//-------------------------------------------------
//  rtc_clock_updated - called when the host time changes
//-------------------------------------------------

void icm7170_device::rtc_clock_updated(int year, int month, int day, int day_of_week, int hour, int minute, int second)
{
	if (m_regs[REG_COMMAND] & CMD_REG_RUN)
	{
		m_regs[REG_CNT_YEAR] = year % 99;
		m_regs[REG_CNT_MONTH] = month;
		m_regs[REG_CNT_DAY] = day;
		m_regs[REG_CNT_DAY_OF_WEEK] = day_of_week;
		if (!(m_regs[REG_COMMAND] & CMD_REG_24_HOUR))
		{
			// transform hours from 0..23 to 1..12
			m_regs[REG_CNT_HOURS] = (hour > 12) ? hour - 12 : hour ? hour : 1;

			// set am/pm bit
			if (hour > 11)
				m_regs[REG_CNT_HOURS] |= 0x80;
		}
		else
			m_regs[REG_CNT_HOURS] = hour;
		m_regs[REG_CNT_MINUTES] = minute;
		m_regs[REG_CNT_SECONDS] = second;

		if (m_regs[REG_COMMAND] & CMD_REG_24_HOUR)
			LOG("rtc_clock_updated %02d-%02d-%02d %02d:%02d:%02d\n",
				m_regs[REG_CNT_YEAR], m_regs[REG_CNT_MONTH], m_regs[REG_CNT_DAY],
				m_regs[REG_CNT_HOURS], m_regs[REG_CNT_MINUTES], m_regs[REG_CNT_SECONDS]);
		else
			LOG("rtc_clock_updated %02d-%02d-%02d %02d:%02d:%02d %s\n",
				m_regs[REG_CNT_YEAR], m_regs[REG_CNT_MONTH], m_regs[REG_CNT_DAY],
				m_regs[REG_CNT_HOURS] & 0xf, m_regs[REG_CNT_MINUTES], m_regs[REG_CNT_SECONDS],
				(m_regs[REG_CNT_HOURS] & 0x80) ? "pm" : "am");
	}
}


//-------------------------------------------------
//  nvram_default - called to initialize NVRAM to
//  its default state
//-------------------------------------------------

void icm7170_device::nvram_default()
{
	memset(m_regs, 0, 0x20);
}


//-------------------------------------------------
//  nvram_read - called to read NVRAM from the
//  .nv file
//-------------------------------------------------

bool icm7170_device::nvram_read(util::read_stream &file)
{
	auto const [err, actual] = util::read(file, m_regs, 0x20);
	return !err && (actual == 0x20);
}


//-------------------------------------------------
//  nvram_write - called to write NVRAM to the
//  .nv file
//-------------------------------------------------

bool icm7170_device::nvram_write(util::write_stream &file)
{
	auto const [err, actual] = util::write(file, m_regs, 0x20);
	return !err;
}

// non-inherited device functions
uint8_t icm7170_device::read(offs_t offset)
{
	uint8_t data = m_regs[offset & 0x1f];

	if ((offset & 0x1f) == REG_INT_STATUS_AND_MASK)
	{
		data = m_irq_status;
		m_irq_status = 0;
		recalc_irqs();
	}

	LOG("ICM7170 Register %d Read %02x\n", offset, data);

	return data;
}

void icm7170_device::write(offs_t offset, uint8_t data)
{
	// TODO: unused register bits should be masked
	switch (offset & 0x1f)
	{
		case REG_INT_STATUS_AND_MASK:
			m_irq_mask = data;
			LOG("ICM7170 IRQ Mask Write %02x\n", data);
			recalc_irqs();
			break;

		case REG_COMMAND:
			m_timer->enable(data & CMD_REG_RUN);
			[[fallthrough]];

		default:
			m_regs[offset & 0x1f] = data;
			LOG("ICM7170 Register %d Write %02x\n", offset & 0x1f, data);
			break;
	}
}

void icm7170_device::recalc_irqs()
{
	if (m_irq_status & m_irq_mask & ~IRQ_BIT_GLOBAL)
		m_irq_status |= IRQ_BIT_GLOBAL;
	else
		m_irq_status &= ~IRQ_BIT_GLOBAL;

	bool const irq_state = (m_regs[REG_COMMAND] & CMD_REG_IRQ_ENABLE) && (m_irq_status & IRQ_BIT_GLOBAL);
	if (m_out_irq_state != irq_state)
	{
		m_out_irq_state = irq_state;
		m_out_irq_cb(m_out_irq_state);
	}
}