summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/rtc65271.cpp
blob: 9ccc83997bf2ccd4bcf7fe4e148b416f54041213 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// license:BSD-3-Clause
// copyright-holders:Raphael Nabet, R. Belmont
/*
    rtc65271 emulation

    This chip is an RTC for computer built by Epson and Spezial-Electronic (I
    think SE is the second source here).

    Reference:
    * Realtime Clock Module RTC-65271 Application Manual
        <http://www.bgmicro.com/pdf/rtc65271.pdf>

    Todo:
    * Support square wave pin output?
    * Support DSE mode?

    Raphael Nabet, 2003-2004
    R. Belmont, 2012
*/

#include "emu.h"
#include "rtc65271.h"

/* Delay between the beginning (UIP asserted) and the end (UIP cleared and
update interrupt asserted) of the update cycle */
#define UPDATE_CYCLE_TIME attotime::from_usec(1984)
/* Delay between the assertion of UIP and the effective start of the update
cycle */
/*#define UPDATE_CYCLE_DELAY attotime::from_usec(244)*/

enum
{
	reg_second = 0,
	reg_alarm_second,
	reg_minute,
	reg_alarm_minute,
	reg_hour,
	reg_alarm_hour,
	reg_weekday,
	reg_monthday,
	reg_month,
	reg_year,
	reg_A,
	reg_B,
	reg_C,
	reg_D
};

enum
{
	reg_A_UIP   = 0x80,
	reg_A_DV    = 0x70,
	reg_A_RS    = 0x0F,

	reg_B_SET   = 0x80,
	reg_B_PIE   = 0x40,
	reg_B_AIE   = 0x20,
	reg_B_UIE   = 0x10,
	reg_B_SQW   = 0x08,
	reg_B_DM    = 0x04,
	reg_B_24h   = 0x02,
	reg_B_DSE   = 0x01,

	reg_C_IRQF  = 0x80,
	reg_C_PF    = 0x40,
	reg_C_AF    = 0x20,
	reg_C_UF    = 0x10,

	reg_D_VRT   = 0x80
};

static const int SQW_freq_table[16] =
{
	0,
	256,
	128,
	8192,
	4096,
	2048,
	1024,
	512,
	256,
	128,
	64,
	32,
	16,
	8,
	4,
	2,
};


/*
    BCD utilities
*/

/*
    Increment a binary-encoded uint8_t
*/
static uint8_t increment_binary(uint8_t data)
{
	return data+1;
}


/*
    Increment a BCD-encoded uint8_t
*/
static uint8_t increment_BCD(uint8_t data)
{
	if ((data & 0x0f) < 0x09)
	{
		if ((data & 0xf0) < 0xa0)
			data++;
		else
			data = data + 0x01 - 0xa0;
	}
	else
	{
		if ((data & 0xf0) < 0xa0)
			data = data - 0x09 + 0x10;
		else
			data = data - 0x09 - 0x90;
	}
	return data;
}


/*
    Convert a binary-encoded uint8_t to BCD
*/
static uint8_t binary_to_BCD(uint8_t data)
{
	data %= 100;

	return ((data / 10) << 4) | (data %10);
}


/*
    Convert a BCD-encoded uint8_t to binary
*/
static uint8_t BCD_to_binary(uint8_t data)
{
	if ((data & 0x0f) >= 0x0a)
		data = data - 0x0a + 0x10;
	if ((data & 0xf0) >= 0xa0)
		data = data - 0xa0;

	return (data & 0x0f) + (((data & 0xf0) >> 4) * 10);
}


/*
    Public functions
*/

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

void rtc65271_device::nvram_default()
{
	memset(m_regs,0, sizeof(m_regs));
	memset(m_xram,0, sizeof(m_xram));

	m_regs[reg_B] |= reg_B_DM;  // Firebeat assumes the chip factory defaults to non-BCD mode (or maybe Konami programs it that way?)
}

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

void rtc65271_device::nvram_read(emu_file &file)
{
	uint8_t buf;

	/* version flag */
	if (file.read(&buf, 1) != 1)
		return;
	if (buf != 0)
		return;

	/* control registers */
	if (file.read(&buf, 1) != 1)
		return;
	m_regs[reg_A] = buf & (reg_A_DV /*| reg_A_RS*/);
	if (file.read(&buf, 1) != 1)
		return;
	m_regs[reg_B] = buf & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE);

	/* alarm registers */
	if (file.read(&m_regs[reg_alarm_second], 1) != 1)
		return;
	if (file.read(&m_regs[reg_alarm_minute], 1) != 1)
		return;
	if (file.read(&m_regs[reg_alarm_hour], 1) != 1)
		return;

	/* user RAM */
	if (file.read(m_regs+14, 50) != 50)
		return;

	/* extended RAM */
	if (file.read(m_xram, 4096) != 4096)
		return;

	m_regs[reg_D] |= reg_D_VRT; /* the data was backed up successfully */
	/*m_dirty = false;*/

	{
		system_time systime;

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

		/* set clock registers */
		m_regs[reg_second] = systime.local_time.second;
		m_regs[reg_minute] = systime.local_time.minute;
		if (m_regs[reg_B] & reg_B_24h)
			/* 24-hour mode */
			m_regs[reg_hour] = systime.local_time.hour;
		else
		{   /* 12-hour mode */
			if (systime.local_time.hour >= 12)
			{
				m_regs[reg_hour] = 0x80;
				systime.local_time.hour -= 12;
			}
			else
			{
				m_regs[reg_hour] = 0;
			}

			// Firebeat indicates non-BCD 12-hour mode has 0-based hour, so 12 AM is 0x00 and 12 PM is 0x80
			m_regs[reg_hour] |= systime.local_time.hour; // ? systime.local_time.hour : 12;
		}
		m_regs[reg_weekday] = systime.local_time.weekday + 1;
		m_regs[reg_monthday] = systime.local_time.mday;
		m_regs[reg_month] = systime.local_time.month + 1;
		m_regs[reg_year] = systime.local_time.year % 100;
		if (! (m_regs[reg_B] & reg_B_DM))
		{   /* BCD mode */
			m_regs[reg_second] = binary_to_BCD(m_regs[reg_second]);
			m_regs[reg_minute] = binary_to_BCD(m_regs[reg_minute]);
			m_regs[reg_hour] = (m_regs[reg_hour] & 0x80) | binary_to_BCD(m_regs[reg_hour] & 0x7f);
			/*m_regs[reg_weekday] = binary_to_BCD(m_regs[reg_weekday]);*/
			m_regs[reg_monthday] = binary_to_BCD(m_regs[reg_monthday]);
			m_regs[reg_month] = binary_to_BCD(m_regs[reg_month]);
			m_regs[reg_year] = binary_to_BCD(m_regs[reg_year]);
		}
	}
}

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

void rtc65271_device::nvram_write(emu_file &file)
{
	uint8_t buf;


	/* version flag */
	buf = 0;
	if (file.write(& buf, 1) != 1)
		return;

	/* control registers */
	buf = m_regs[reg_A] & (reg_A_DV | reg_A_RS);
	if (file.write(&buf, 1) != 1)
		return;
	buf = m_regs[reg_B] & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE);
	if (file.write(&buf, 1) != 1)
		return;

	/* alarm registers */
	if (file.write(&m_regs[reg_alarm_second], 1) != 1)
		return;
	if (file.write(&m_regs[reg_alarm_minute], 1) != 1)
		return;
	if (file.write(&m_regs[reg_alarm_hour], 1) != 1)
		return;

	/* user RAM */
	if (file.write(m_regs+14, 50) != 50)
		return;

	/* extended RAM */
	if (file.write(m_xram, 4096) != 4096)
		return;
}

/*
    Read a byte from clock

    xramsel: select RTC register if 0, XRAM if 1
    offset: address (A0-A5 pins)
*/
uint8_t rtc65271_device::read(int xramsel, offs_t offset)
{
	int reply;

	if (xramsel)
	{
		if (offset & 0x20)
			/* XRAM page register */
			reply = m_cur_xram_page;
		else
			/* XRAM data */
			reply = m_xram[(offset & 0x1f) + 0x0020*m_cur_xram_page];
	}
	else
	{
		if (offset & 0x01)
			/* data register */
			switch (m_cur_reg)
			{
			case reg_A:
				reply = m_regs[m_cur_reg] & ~reg_A_DV;
				reply |= 0x20;  // indicate normal RTC operation
				break;

			case reg_C:
				reply = m_regs[m_cur_reg];
				m_regs[m_cur_reg] = 0;
				field_interrupts();
				break;
			case reg_D:
				reply = m_regs[m_cur_reg];
				m_regs[m_cur_reg] = /*0*/reg_D_VRT; /* set VRT flag so that the computer does not complain that the battery is low */
				break;

			default:
				reply = m_regs[m_cur_reg];
				break;
			}
		else
			/* indirect address register */
			reply = m_cur_reg;
	}

	return reply;
}

uint8_t rtc65271_device::rtc_r(offs_t offset)
{
	return read(0, offset );
}

uint8_t rtc65271_device::xram_r(offs_t offset)
{
	return read(1, offset );
}

/*
    Write a byte to clock

    xramsel: select RTC register if 0, XRAM if 1
    offset: address (A0-A5 pins)
*/
void rtc65271_device::write(int xramsel, offs_t offset, uint8_t data)
{
	if (xramsel)
	{
		if (offset & 0x20)
			/* XRAM page register */
			m_cur_xram_page = data & 0x7f;
		else
			/* XRAM data */
			m_xram[(offset & 0x1f) + 0x0020*m_cur_xram_page] = data;
	}
	else
	{
		if (offset & 0x01)
			/* data register */
			switch (m_cur_reg)
			{
			case reg_second:
				/* the data sheet says bit 7 is read-only.  (I have no idea of
				the reason why it is.) */
				m_regs[reg_second] = data & 0x7f;
				break;

			case reg_A:
				if ((data & reg_A_RS) != (m_regs[m_cur_reg] & reg_A_RS))
				{
					if (data & reg_A_RS)
					{
						attotime period = attotime::from_hz(SQW_freq_table[data & reg_A_RS]);
						attotime half_period = period / 2;
						attotime elapsed = m_update_timer->elapsed();

						if (half_period > elapsed)
							m_SQW_timer->adjust(half_period - elapsed);
						else
							m_SQW_timer->adjust(half_period);
					}
					else
					{
						m_SQW_internal_state = 0;   /* right??? */

						/* Stop the divider used for SQW and periodic interrupts. */
						m_SQW_timer->adjust(attotime::never);
					}
				}
				/* The UIP bit is read-only */
				m_regs[reg_A] = (data & ~reg_A_UIP) | (m_regs[reg_A] & reg_A_UIP);
				break;

			case reg_B:
				m_regs[m_cur_reg] = data;
				if (data & reg_B_SET)
				{
					/* if we are in SET mode, clear update cycle */
					m_regs[reg_A] &= ~reg_A_UIP;
					m_regs[reg_B] &= ~reg_B_UIE;    /* the data sheet tells this, but I wonder how much sense it makes */
					field_interrupts();
				}
				break;

			case reg_C:
			case reg_D:
				break;

			default:
				m_regs[m_cur_reg] = data;
				break;
			}
		else
			/* indirect address register */
			m_cur_reg = data & 0x3f;
	}
}

void rtc65271_device::rtc_w(offs_t offset, uint8_t data)
{
	write(0, offset, data );
}

void rtc65271_device::xram_w(offs_t offset, uint8_t data)
{
	write(1, offset, data );
}

void rtc65271_device::field_interrupts()
{
	if (m_regs[reg_C] & m_regs[reg_B] & (reg_C_PF | reg_C_AF | reg_C_UF))
	{
		m_regs[reg_C] |= reg_C_IRQF;
		if (!m_interrupt_cb.isnull())
			m_interrupt_cb(1);
	}
	else
	{
		m_regs[reg_C] &= ~reg_C_IRQF;
		if (!m_interrupt_cb.isnull())
			m_interrupt_cb(0);
	}
}


/*
    Update SQW output state each half-period and assert periodic interrupt each
    period.
*/
TIMER_CALLBACK_MEMBER(rtc65271_device::rtc_SQW_cb)
{
	attotime half_period;

	m_SQW_internal_state = ! m_SQW_internal_state;
	if (! m_SQW_internal_state)
	{
		/* high-to-low??? transition -> interrupt (or should it be low-to-high?) */
		m_regs[reg_C] |= reg_C_PF;
		field_interrupts();
	}

	half_period = attotime::from_hz(SQW_freq_table[m_regs[reg_A] & reg_A_RS]) / 2;
	m_SQW_timer->adjust(half_period);
}

/*
    Begin update cycle (called every second)
*/
TIMER_CALLBACK_MEMBER(rtc65271_device::rtc_begin_update_cb)
{
	if (((m_regs[reg_A] & reg_A_DV) == 0x20) && ! (m_regs[reg_B] & reg_B_SET))
	{
		m_regs[reg_A] |= reg_A_UIP;

		/* schedule end of update cycle */
		machine().scheduler().timer_set(UPDATE_CYCLE_TIME, timer_expired_delegate(FUNC(rtc65271_device::rtc_end_update_cb), this));
	}
}

/*
    End update cycle (called UPDATE_CYCLE_TIME = 1948us after start of update
    cycle)
*/
TIMER_CALLBACK_MEMBER(rtc65271_device::rtc_end_update_cb)
{
	static const int days_in_month_table[12] =
	{
		31,28,31, 30,31,30,
		31,31,30, 31,30,31
	};
	uint8_t (*increment)(uint8_t data);
	int c59, c23, c12, c11, c29;

	if (! (m_regs[reg_A] & reg_A_UIP))
		/* abort if update cycle has been canceled */
		return;

	if (m_regs[reg_B] & reg_B_DM)
	{
		/* binary mode */
		increment = increment_binary;
		c59 = 59;
		c23 = 23;
		c12 = 12;
		c11 = 11;
		c29 = 29;
	}
	else
	{
		/* BCD mode */
		increment = increment_BCD;
		c59 = 0x59;
		c23 = 0x23;
		c12 = 0x12;
		c11 = 0x11;
		c29 = 0x29;
	}

	/* increment second */
	if (m_regs[reg_second] < c59)
		m_regs[reg_second] = (*increment)(m_regs[reg_second]);
	else
	{
		m_regs[reg_second] = 0;

		/* increment minute */
		if (m_regs[reg_minute] < c59)
			m_regs[reg_minute] = (*increment)(m_regs[reg_minute]);
		else
		{
			m_regs[reg_minute] = 0;

			/* increment hour */
			if (m_regs[reg_B] & reg_B_24h)
			{
				/* 24 hour mode */
				if (m_regs[reg_hour] < c23)
					m_regs[reg_hour] = (*increment)(m_regs[reg_hour]);
				else
					m_regs[reg_hour] = 0;
			}
			else
			{
				/* 12 hour mode */
				if (m_regs[reg_hour] < c12)
				{
					if ((m_regs[reg_hour] & 0x7f) == c11)
						m_regs[reg_hour] ^= 0x80;
					m_regs[reg_hour] = ((*increment)(m_regs[reg_hour] & 0x7f) & 0x7f)
											| (m_regs[reg_hour] & 0x80);
				}
				else
					m_regs[reg_hour] = 1 | (m_regs[reg_hour] & 0x80);
			}

			/* increment day if needed */
			if (m_regs[reg_hour] == ((m_regs[reg_B] & reg_B_24h) ? 0 : c12))
			{
				/* increment day */
				int days_in_month;

				if (m_regs[reg_weekday] < 7)
					m_regs[reg_weekday]++;
				else
					m_regs[reg_weekday] = 1;

				if ((m_regs[reg_month] != 2) || (m_regs[reg_year] & 0x03))
				{
					if (m_regs[reg_B] & reg_B_DM)
					{
						/* binary mode */
						days_in_month = days_in_month_table[m_regs[reg_month] - 1];
					}
					else
					{
						/* BCD mode */
						days_in_month = binary_to_BCD(days_in_month_table[BCD_to_binary(m_regs[reg_month]) - 1]);
					}
				}
				else
					days_in_month = c29;

				if (m_regs[reg_monthday] < days_in_month)
					m_regs[reg_monthday] = (*increment)(m_regs[reg_monthday]);
				else
				{
					/* increment month */
					m_regs[reg_monthday] = 1;

					if (m_regs[reg_month] < c12)
						m_regs[reg_month] = (*increment)(m_regs[reg_month]);
					else
					{
						/* increment year */
						m_regs[reg_month] = 1;

						if (m_regs[reg_B] & reg_B_DM)
						{
							/* binary mode */
							if (m_regs[reg_year] < 99)
								m_regs[reg_year]++;
							else
								m_regs[reg_year] = 0;
						}
						else
						{
							/* BCD mode */
							m_regs[reg_year] = increment_BCD(m_regs[reg_year]);
						}
					}
				}
			}
		}
	}

	m_regs[reg_A] &= ~reg_A_UIP;
	m_regs[reg_C] |= reg_C_UF;

	/* test for alarm (values in range 0xc0-0xff mean "don't care") */
	if ((((m_regs[reg_alarm_second] & 0xc0) == 0xc0) || (m_regs[reg_alarm_second] == m_regs[reg_second]))
			&& (((m_regs[reg_alarm_minute] & 0xc0) == 0xc0) || (m_regs[reg_alarm_minute] == m_regs[reg_minute]))
			&& (((m_regs[reg_alarm_hour] & 0xc0) == 0xc0) || (m_regs[reg_alarm_hour] == m_regs[reg_hour])))
		m_regs[reg_C] |= reg_C_AF;

	field_interrupts();
}

// device type definition
DEFINE_DEVICE_TYPE(RTC65271, rtc65271_device, "rtc65271", "Epson RTC-65271 RTC")

//-------------------------------------------------
//  rtc65271_device - constructor
//-------------------------------------------------

rtc65271_device::rtc65271_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, RTC65271, tag, owner, clock)
	, device_nvram_interface(mconfig, *this)
	, m_interrupt_cb(*this)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------
void rtc65271_device::device_start()
{
	m_update_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(rtc65271_device::rtc_begin_update_cb), this));
	m_update_timer->adjust(attotime::from_seconds(1), 0, attotime::from_seconds(1));
	m_SQW_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(rtc65271_device::rtc_SQW_cb), this));
	m_interrupt_cb.resolve();

	save_item(NAME(m_regs));
	save_item(NAME(m_cur_reg));
	save_item(NAME(m_xram));
	save_item(NAME(m_cur_xram_page));
	save_item(NAME(m_SQW_internal_state));
}