summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/mm58274c.cpp
blob: 491ace25a344eec06f6e0672529ee28707221188 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// license:BSD-3-Clause
// copyright-holders:Raphael Nabet
/***************************************************************************

    mm58274c.c

    mm58274c emulation

    Reference:
    * National Semiconductor MM58274C Microprocessor Compatible Real Time Clock
        <http://www.national.com/ds/MM/MM58274C.pdf>

    Todo:
    * Clock initialization will only work with the BwG: we need to provide
      a way to customize it.
    * Save the config to NVRAM?
    * Support interrupt pin output

    Raphael Nabet, 2002

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

#include "emu.h"
#include "machine/mm58274c.h"

enum
{
	st_dcf = 0x8,       /* data-changed flag */
	st_if = 0x1,        /* interrupt flag */

	ctl_test = 0x8,     /* test mode (0=normal, 1=test) (not emulated) */
	ctl_clkstop = 0x4,  /* clock start/stop (0=run, 1=stop) */
	ctl_intsel = 0x2,   /* interrupt select (0=clock setting register, 1=interrupt register) */
	ctl_intstop = 0x1,  /* interrupt start stop (0=interrupt run, 1=interrupt stop) */

	clk_set_leap = 0xc,     /* leap year counter (0 indicates a leap year) */
	clk_set_leap_inc = 0x4, /* leap year increment */
	clk_set_pm = 0x2,       /* am/pm indicator (0 = am, 1 = pm, 0 in 24-hour mode) */
	clk_set_24 = 0x1,       /* 12/24-hour select bit (1= 24-hour mode) */

	int_ctl_rpt = 0x8,      /* 1 for repeated interrupt */
	int_ctl_dly = 0x7       /* 0 no interrupt, 1 = .1 second, 2=.5, 3=1, 4=5, 5=10, 6=30, 7=60 */
};



const device_type MM58274C = device_creator<mm58274c_device>;


mm58274c_device::mm58274c_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
				: device_t(mconfig, MM58274C, "National Semiconductor MM58274C", tag, owner, clock, "mm58274c", __FILE__),
					m_mode24(0),
					m_day1(0)
{
}


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

void mm58274c_device::device_start()
{
	m_increment_rtc = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(mm58274c_device::rtc_increment_cb),this));
	m_increment_rtc->adjust(attotime::zero, 0, attotime::from_msec(100));
	m_interrupt_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(mm58274c_device::rtc_interrupt_cb),this));

	// register for state saving
	save_item(NAME(m_mode24));
	save_item(NAME(m_day1));

	save_item(NAME(m_status));
	save_item(NAME(m_control));
	save_item(NAME(m_clk_set));
	save_item(NAME(m_int_ctl));
	save_item(NAME(m_wday));
	save_item(NAME(m_years1));
	save_item(NAME(m_years2));
	save_item(NAME(m_months1));
	save_item(NAME(m_months2));
	save_item(NAME(m_days1));
	save_item(NAME(m_days2));
	save_item(NAME(m_hours1));
	save_item(NAME(m_hours2));
	save_item(NAME(m_minutes1));
	save_item(NAME(m_minutes2));
	save_item(NAME(m_seconds1));
	save_item(NAME(m_seconds2));
	save_item(NAME(m_tenths));
}

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

void mm58274c_device::device_reset()
{
	system_time systime;

	/* get the current date/time from the core */
	machine().current_datetime(systime);

	m_clk_set = systime.local_time.year & 3 << 2;
	if (m_mode24)
		m_clk_set |= clk_set_24;

	/* The clock count starts on 1st January 1900 */
	m_wday = 1 + ((systime.local_time.weekday - m_day1) % 7);
	m_years1 = (systime.local_time.year / 10) % 10;
	m_years2 = systime.local_time.year % 10;
	m_months1 = (systime.local_time.month + 1) / 10;
	m_months2 = (systime.local_time.month + 1) % 10;
	m_days1 = systime.local_time.mday / 10;
	m_days2 = systime.local_time.mday % 10;
	if (!m_mode24)
	{
		/* 12-hour mode */
		if (systime.local_time.hour > 12)
		{
			systime.local_time.hour -= 12;
			m_clk_set |= clk_set_pm;
		}
		if (systime.local_time.hour == 0)
			systime.local_time.hour = 12;
	}
	m_hours1 = systime.local_time.hour / 10;
	m_hours2 = systime.local_time.hour % 10;
	m_minutes1 = systime.local_time.minute / 10;
	m_minutes2 = systime.local_time.minute % 10;
	m_seconds1 = systime.local_time.second / 10;
	m_seconds2 = systime.local_time.second % 10;
	m_tenths = 0;
	m_status = 0;
	m_control = 0;
}



attotime mm58274c_device::interrupt_period_table(int val)
{
	switch(val)
	{
		case 0: return attotime::from_msec(0);
		case 1: return attotime::from_msec(100);
		case 2: return attotime::from_msec(500);
		case 3: return attotime::from_seconds(1);
		case 4: return attotime::from_seconds(5);
		case 5: return attotime::from_seconds(10);
		case 6: return attotime::from_seconds(30);
		case 7: return attotime::from_seconds(60);
		default: fatalerror("out of range\n");
	}
}

READ8_MEMBER( mm58274c_device::read )
{
	int reply;

	offset &= 0xf;

	switch (offset)
	{
		case 0x00:   /* Control Register */
			reply = m_status;
			m_status = 0;
			break;

		case 0x01:   /* Tenths of Seconds */
			reply = m_tenths;
			break;

		case 0x02:   /* Units Seconds */
			reply = m_seconds2;
			break;

		case 0x03:   /* Tens Seconds */
			reply = m_seconds1;
			break;

		case 0x04:  /* Units Minutes */
			reply = m_minutes2;
			break;

		case 0x05:   /* Tens Minutes */
			reply = m_minutes1;
			break;

		case 0x06:   /* Units Hours */
			reply = m_hours2;
			break;

		case 0x07:   /* Tens Hours */
			reply = m_hours1;
			break;

		case 0x08:   /* Units Days */
			reply = m_days2;
			break;

		case 0x09:   /* Tens Days */
			reply = m_days1;
			break;

		case 0x0a:   /* Units Months */
			reply = m_months2;
			break;

		case 0x0b:   /* Tens Months */
			reply = m_months1;
			break;

		case 0x0c:   /* Units Years */
			reply = m_years2;
			break;

		case 0x0d:   /* Tens Years */
			reply = m_years1;
			break;

		case 0x0e:   /* Day of Week */
			reply = m_wday;
			break;

		case 0x0f:   /* Clock Setting & Interrupt Registers */
			if (m_control & ctl_intsel) /* interrupt register */
				reply = m_int_ctl;
			else    /* clock setting register */
			{
				if (m_clk_set & clk_set_24) /* 24-hour mode */
					reply = m_clk_set & ~clk_set_pm;
				else    /* 12-hour mode */
					reply = m_clk_set;
			}
			break;

		default:
			reply = 0;
			break;
	}

	return reply;
}


WRITE8_MEMBER( mm58274c_device::write )
{
	offset &= 0xf;
	data &= 0xf;

	switch (offset)
	{
		case 0x00:   /* Control Register (test mode and interrupt not emulated) */
			if ((!(m_control & ctl_intstop)) && (data & ctl_intstop))   /* interrupt stop */
				m_interrupt_timer->enable(0);
			else if ((m_control & ctl_intstop) && (!(data & ctl_intstop)))  /* interrupt run */
			{
				attotime period = interrupt_period_table(m_int_ctl & int_ctl_dly);

				m_interrupt_timer->adjust(period, 0, m_int_ctl & int_ctl_rpt ? period : attotime::zero);
			}
			if (data & ctl_clkstop) /* stopping the clock clears the tenth counter */
				m_tenths = 0;
			m_control = data;
			break;

		case 0x01:   /* Tenths of Seconds: cannot be written */
			break;

		case 0x02:   /* Units Seconds */
			m_seconds2 = data;
			break;

		case 0x03:   /* Tens Seconds */
			m_seconds1 = data;
			break;

		case 0x04:   /* Units Minutes */
			m_minutes2 = data;
			break;

		case 0x05:   /* Tens Minutes */
			m_minutes1 = data;
			break;

		case 0x06:   /* Units Hours */
			m_hours2 = data;
			break;

		case 0x07:   /* Tens Hours */
			m_hours1 = data;
			break;

		case 0x08:   /* Units Days */
			m_days2 = data;
			break;

		case 0x09:   /* Tens Days */
			m_days1 = data;
			break;

		case 0x0a:   /* Units Months */
			m_months2 = data;
			break;

		case 0x0b:   /* Tens Months */
			m_months1 = data;
			break;

		case 0x0c:   /* Units Years */
			m_years2 = data;
			break;

		case 0x0d:   /* Tens Years */
			m_years1 = data;
			break;

		case 0x0e:   /* Day of Week */
			m_wday = data;
			break;

		case 0x0f:   /* Clock Setting & Interrupt Registers */
			if (m_control & ctl_intsel) /* interrupt register (not emulated) */
			{
				m_int_ctl = data;
				if (!(m_control & ctl_intstop)) /* interrupt run */
				{
					attotime period = interrupt_period_table(m_int_ctl & int_ctl_dly);

					m_interrupt_timer->adjust(period, 0, m_int_ctl & int_ctl_rpt ? period : attotime::zero);
				}
			}
			else    /* clock setting register */
			{
				m_clk_set = data;
#if 0
				if (m_clk_set & clk_set_24) /* 24-hour mode */
					m_clk_set &= ~clk_set_pm;
#endif
			}
			break;
	}
}


// Set RTC interrupt flag
TIMER_CALLBACK_MEMBER(mm58274c_device::rtc_interrupt_cb)
{
	m_status |= st_if;
}


// Increment RTC clock (timed interrupt every 1/10s)
TIMER_CALLBACK_MEMBER(mm58274c_device::rtc_increment_cb)
{
	if (!(m_control & ctl_clkstop))
	{
		m_status |= st_dcf;

		if ((++m_tenths) == 10)
		{
			m_tenths = 0;

			if ((++m_seconds2) == 10)
			{
				m_seconds2 = 0;

				if ((++m_seconds1) == 6)
				{
					m_seconds1 = 0;

					if ((++m_minutes2) == 10)
					{
						m_minutes2 = 0;

						if ((++m_minutes1) == 6)
						{
							m_minutes1 = 0;

							if ((++m_hours2) == 10)
							{
								m_hours2 = 0;

								m_hours1++;
							}

							/* handle wrap-around */
							if ((!(m_clk_set & clk_set_24))
									&& ((m_hours1*10 + m_hours2) == 12))
							{
								m_clk_set ^= clk_set_pm;
							}
							if ((!(m_clk_set & clk_set_24))
									&& ((m_hours1*10 + m_hours2) == 13))
							{
								m_hours1 = 0;
								m_hours2 = 1;
							}

							if ((m_clk_set & clk_set_24)
								&& ((m_hours1*10 + m_hours2) == 24))
							{
								m_hours1 = m_hours2 = 0;
							}

							/* increment day if needed */
							if ((m_clk_set & clk_set_24)
								? ((m_hours1*10 + m_hours2) == 0)
								: (((m_hours1*10 + m_hours2) == 12)
									&& (!(m_clk_set & clk_set_pm))))
							{
								int days_in_month;

								if ((++m_days2) == 10)
								{
									m_days2 = 0;

									m_days1++;
								}

								if ((++m_wday) == 8)
									m_wday = 1;

								{
									static const int days_in_month_array[] =
									{
										31,28,31, 30,31,30,
										31,31,30, 31,30,31
									};

									if (((m_months1*10 + m_months2) != 2) || (m_clk_set & clk_set_leap))
										days_in_month = days_in_month_array[m_months1*10 + m_months2 - 1];
									else
										days_in_month = 29;
								}


								if ((m_days1*10 + m_days2) == days_in_month+1)
								{
									m_days1 = 0;
									m_days2 = 1;

									if ((++m_months2) == 10)
									{
										m_months2 = 0;

										m_months1++;
									}

									if ((m_months1*10 + m_months2) == 13)
									{
										m_months1 = 0;
										m_months2 = 1;

										m_clk_set = (m_clk_set & ~clk_set_leap)
															| ((m_clk_set + clk_set_leap_inc) & clk_set_leap);

										if ((++m_years2) == 10)
										{
											m_years2 = 0;

											if ((++m_years1) == 10)
												m_years1 = 0;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}