summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/ds1386.cpp
blob: 91a6e2f9c06c9dae2ca45becc7f128cafb3473b4 (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
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
/**********************************************************************

    Dallas DS1386/DS1386P RAMified Watchdog Timekeeper

	Note: Largely untested.

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

#include "ds1386.h"
#include "machine/timehelp.h"

#define DISABLE_OSC		(0x80)
#define	DISABLE_SQW		(0x40)

#define COMMAND_TE		(0x80)
#define COMMAND_IPSW	(0x40)
#define COMMAND_IBH_LO	(0x20)
#define COMMAND_PU_LVL	(0x10)
#define COMMAND_WAM		(0x08)
#define COMMAND_TDM		(0x04)
#define COMMAND_WAF		(0x02)
#define COMMAND_TDF		(0x01)

#define HOURS_12_24		(0x40)
#define HOURS_AM_PM		(0x20)

const device_type DS1386_8K = &device_creator<ds1386_8k_device>;
const device_type DS1386_32K = &device_creator<ds1386_32k_device>;

ds1386_device::ds1386_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, const char *shortname, size_t size)
	: device_t(mconfig, type, name, tag, owner, clock, shortname, __FILE__)
	, device_nvram_interface(mconfig, *this)
	, m_tod_alarm(0)
	, m_watchdog_alarm(0)
	, m_square(1)
	, m_inta_cb(*this)
	, m_intb_cb(*this)
	, m_sqw_cb(*this)
	, m_clock_timer(nullptr)
	, m_square_timer(nullptr)
	, m_watchdog_timer(nullptr)
	, m_inta_timer(nullptr)
	, m_intb_timer(nullptr)
	, m_default_data(*this, DEVICE_SELF, size)
	, m_hundredths(0)
	, m_seconds(0)
	, m_minutes(0)
	, m_minutes_alarm(0)
	, m_hours(0)
	, m_hours_alarm(0)
	, m_days(0)
	, m_days_alarm(0)
	, m_date(0)
	, m_months_enables(0)
	, m_years(0)
	, m_ram_size(size)
{
}

ds1386_8k_device::ds1386_8k_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: ds1386_device(mconfig, DS1386_8K, "DS1386-8K", tag, owner, clock, "ds1386_8k", 8192)
{
}

ds1386_32k_device::ds1386_32k_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: ds1386_device(mconfig, DS1386_32K, "DS1386-32K", tag, owner, clock, "ds1386_32k", 32768)
{
}

void ds1386_device::safe_inta_cb(int state)
{
	if (!m_inta_cb.isnull())
		m_inta_cb(state);
}

void ds1386_device::safe_intb_cb(int state)
{
	if (!m_intb_cb.isnull())
		m_intb_cb(state);
}

void ds1386_device::safe_sqw_cb(int state)
{
	if (!m_sqw_cb.isnull())
		m_sqw_cb(state);
}

void ds1386_device::device_start()
{
	m_inta_cb.resolve();
	m_intb_cb.resolve();
	m_sqw_cb.resolve();

	m_tod_alarm = 0;
	m_watchdog_alarm = 0;
	m_square = 1;

	safe_inta_cb(0);
	safe_intb_cb(0);
	safe_sqw_cb(1);

	// allocate timers
	m_clock_timer = timer_alloc(CLOCK_TIMER);
	m_clock_timer->adjust(attotime::from_hz(100), 0, attotime::from_hz(100));
	m_square_timer = timer_alloc(SQUAREWAVE_TIMER);
	m_square_timer->adjust(attotime::never);
	m_watchdog_timer = timer_alloc(WATCHDOG_TIMER);
	m_watchdog_timer->adjust(attotime::never);
	m_inta_timer= timer_alloc(INTA_TIMER);
	m_inta_timer->adjust(attotime::never);
	m_intb_timer= timer_alloc(INTB_TIMER);
	m_intb_timer->adjust(attotime::never);

	set_current_time();

	// state saving
	save_item(NAME(m_tod_alarm));
	save_item(NAME(m_watchdog_alarm));
	save_item(NAME(m_square));

	m_ram = std::make_unique<UINT8[]>(m_ram_size);
	memset(&m_ram[0], 0, m_ram_size);
}

void ds1386_device::device_reset()
{
	m_tod_alarm = 0;
	m_watchdog_alarm = 0;
	m_square = 1;

	safe_inta_cb(0);
	safe_intb_cb(0);
	safe_sqw_cb(1);

	m_clock_timer->adjust(attotime::from_hz(100), 0, attotime::from_hz(100));
	m_square_timer->adjust(attotime::never);
	m_watchdog_timer->adjust(attotime::never);

	set_current_time();
}


void ds1386_device::set_current_time()
{
	system_time systime;
	machine().base_datetime(systime);

	m_hundredths = 0;
	m_seconds = time_helper::make_bcd(systime.local_time.second);
	m_minutes = time_helper::make_bcd(systime.local_time.minute);
	m_hours = time_helper::make_bcd(systime.local_time.hour);
	m_days = time_helper::make_bcd(systime.local_time.weekday + 1);
	m_date = time_helper::make_bcd(systime.local_time.mday);
	m_months_enables = time_helper::make_bcd(systime.local_time.month + 1);
	m_years = time_helper::make_bcd(systime.local_time.year % 100);
}

void ds1386_device::time_of_day_alarm()
{
	m_ram[REGISTER_COMMAND] |= COMMAND_TDF;
	m_tod_alarm = 1;

	if (m_ram[REGISTER_COMMAND] & COMMAND_IPSW)
	{
		safe_inta_cb(m_tod_alarm);
		if (m_ram[REGISTER_COMMAND] & COMMAND_PU_LVL)
		{
			m_inta_timer->adjust(attotime::from_msec(3));
		}
	}
	else
	{
		safe_intb_cb(m_tod_alarm);
		if (m_ram[REGISTER_COMMAND] & COMMAND_PU_LVL)
		{
			m_intb_timer->adjust(attotime::from_msec(3));
		}
	}
}

void ds1386_device::watchdog_alarm()
{
	m_ram[REGISTER_COMMAND] |= COMMAND_WAF;
	m_watchdog_alarm = 1;

	if (m_ram[REGISTER_COMMAND] & COMMAND_IPSW)
	{
		safe_intb_cb(m_watchdog_alarm);
		if (m_ram[REGISTER_COMMAND] & COMMAND_PU_LVL)
		{
			m_intb_timer->adjust(attotime::from_msec(3));
		}
	}
	else
	{
		safe_inta_cb(m_watchdog_alarm);
		if (m_ram[REGISTER_COMMAND] & COMMAND_PU_LVL)
		{
			m_inta_timer->adjust(attotime::from_msec(3));
		}
	}
}

void ds1386_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
		case CLOCK_TIMER:
			advance_hundredths();
			break;

		case SQUAREWAVE_TIMER:
			m_square = ((m_square == 0) ? 1 : 0);
			safe_sqw_cb(m_square);
			break;

		case WATCHDOG_TIMER:
			if ((m_ram[REGISTER_COMMAND] & COMMAND_WAF) == 0)
				watchdog_alarm();
			break;

		case INTA_TIMER:
			if (m_ram[REGISTER_COMMAND] & COMMAND_IPSW)
			{
				m_tod_alarm = 0;
			}
			else
			{
				m_watchdog_alarm = 0;
			}
			safe_inta_cb(0);
			break;

		case INTB_TIMER:
			if (m_ram[REGISTER_COMMAND] & COMMAND_IPSW)
			{
				m_watchdog_alarm = 0;
			}
			else
			{
				m_tod_alarm = 0;
			}
			safe_intb_cb(0);
			break;
	}
}

void ds1386_device::advance_hundredths()
{
	if ((m_ram[REGISTER_COMMAND] & COMMAND_TE) != 0)
	{
		copy_ram_to_registers();
	}

	int carry = time_helper::inc_bcd(&m_hundredths, 0xff, 0x00, 0x99);
	if (carry)
	{
		carry = time_helper::inc_bcd(&m_seconds, 0x7f, 0x00, 0x59);
	}
	if (carry)
	{
		carry = time_helper::inc_bcd(&m_minutes, 0x7f, 0x00, 0x59);
	}
	if (carry)
	{
		UINT8 value = 0;
		UINT8 min_value = 0;
		UINT8 max_value = 0;
		if (m_hours & HOURS_12_24)
		{
			value = m_hours & 0x1f;
			min_value = 0x01;
			max_value = 0x12;
		}
		else
		{
			value = m_hours & 0x3f;
			min_value = 0x00;
			max_value = 0x23;
		}
		carry = time_helper::inc_bcd(&value, 0x1f, min_value, max_value);

		m_hours &= ~0x1f;
		if (carry)
		{
			if (m_hours & HOURS_12_24)
			{
				if (m_hours & HOURS_AM_PM)
					carry = 1;
				else
					carry = 0;

				m_hours = m_hours ^ HOURS_AM_PM;
			}
			else
			{
				m_hours &= ~0x3f;
			}
		}
		else
		{
			if ((m_hours & HOURS_12_24) == 0)
				m_hours &= ~0x3f;
		}
		m_hours |= value;
	}

	if (carry)
	{
		UINT8 maxdays;
		static const UINT8 daysinmonth[] = { 0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31 };

		time_helper::inc_bcd(&m_days, 0x07, 0x01, 0x07);

		UINT8 month = time_helper::from_bcd(m_months_enables);
		UINT8 year = time_helper::from_bcd(m_years);

		if (month == 2 && (year % 4) == 0)
		{
			maxdays = 0x29;
		}
		else if (month >= 1 && month <= 12)
		{
			maxdays = daysinmonth[month - 1];
		}
		else
		{
			maxdays = 0x31;
		}

		carry = time_helper::inc_bcd(&m_date, 0x3f, 0x01, maxdays);
	}
	if (carry)
	{
		UINT8 enables = m_months_enables & 0xc0;
		carry = time_helper::inc_bcd(&m_months_enables, 0x1f, 0x01, 0x12);
		m_months_enables &= 0x3f;
		m_months_enables |= enables;
	}
	if (carry)
	{
		carry = time_helper::inc_bcd(&m_years, 0xff, 0x00, 0x99);
	}

	if ((m_ram[REGISTER_COMMAND] & COMMAND_TE) != 0)
	{
		copy_registers_to_ram();
	}

	check_tod_alarm();
}

void ds1386_device::copy_ram_to_registers()
{
	m_hundredths = m_ram[REGISTER_HUNDREDTHS];
	m_seconds = m_ram[REGISTER_SECONDS];
	m_minutes = m_ram[REGISTER_MINUTES];
	m_hours = m_ram[REGISTER_HOURS];
	m_days = m_ram[REGISTER_DAYS];
	m_date = m_ram[REGISTER_DATE];
	m_months_enables = m_ram[REGISTER_MONTHS];
	m_years = m_ram[REGISTER_YEARS];
}

void ds1386_device::copy_registers_to_ram()
{
	m_ram[REGISTER_HUNDREDTHS] = m_hundredths;
	m_ram[REGISTER_SECONDS] = m_seconds;
	m_ram[REGISTER_MINUTES] = m_minutes;
	m_ram[REGISTER_HOURS] = m_hours;
	m_ram[REGISTER_DAYS] = m_days;
	m_ram[REGISTER_DATE] = m_date;
	m_ram[REGISTER_MONTHS] = m_months_enables;
	m_ram[REGISTER_YEARS] = m_years;
}

void ds1386_device::check_tod_alarm()
{
	UINT8 mode = BIT(m_days_alarm, 7) | (BIT(m_hours_alarm, 5) << 1) | (BIT(m_minutes_alarm, 3) << 2);
	bool zeroes = (m_hundredths == 0 && m_seconds == 0);
	if (zeroes && (m_ram[REGISTER_COMMAND] & COMMAND_TDF) == 0)
	{
		bool minutes_match = (m_minutes & 0x7f) == (m_minutes_alarm & 0x7f);
		bool hours_match = (m_hours & 0x7f) == (m_hours_alarm & 0x7f);
		bool days_match = (m_days & 0x7) == (m_days_alarm & 0x07);
		bool alarm_match = false;
		switch (mode)
		{
			case ALARM_PER_MINUTE:
				alarm_match = true;
				break;
			case ALARM_MINUTES_MATCH:
				alarm_match = minutes_match;
				break;
			case ALARM_HOURS_MATCH:
				alarm_match = hours_match;
				break;
			case ALARM_DAYS_MATCH:
				alarm_match = days_match;
				break;
			default:
				break;
		}
		if (alarm_match)
		{
			time_of_day_alarm();
		}
	}
}

void ds1386_device::nvram_default()
{
	memset(&m_ram[0], 0, m_ram_size);
}

void ds1386_device::nvram_read(emu_file &file)
{
	file.read(&m_ram[0], m_ram_size);
}

void ds1386_device::nvram_write(emu_file &file)
{
	file.write(&m_ram[0], m_ram_size);
}

WRITE8_MEMBER( ds1386_device::data_w )
{
	if (offset >= m_ram_size)
		return;

	if (offset >= 0xe)
	{
		m_ram[offset] = data;
	}
	else
	{
		switch (offset)
		{
			case 0x00: // hundredths
			case 0x03: // minutes alarm
			case 0x05: // horus alarm
			case 0x0a: // years
				m_ram[offset] = data;
				break;

			case 0x01: // seconds
			case 0x02: // minutes
			case 0x04: // hours
				m_ram[offset] = data & 0x7f;
				break;

			case 0x06: // days
				m_ram[offset] = data & 0x07;
				break;

			case 0x07: // days alarm
				m_ram[offset] = data & 0x87;
				break;

			case 0x08: // date
				m_ram[offset] = data & 0x3f;
				break;

			case 0x09: // months
			{
				UINT8 old_value = m_ram[offset];
				m_ram[offset] = data & 0xdf;
				UINT8 changed = old_value ^ data;
				if (changed & DISABLE_SQW)
				{
					if (m_ram[offset] & DISABLE_SQW)
					{
						m_square_timer->adjust(attotime::never);
					}
					else
					{
						m_square_timer->adjust(attotime::from_hz(2048));
					}
				}
				if (changed & DISABLE_OSC)
				{
					if (m_ram[offset] & DISABLE_OSC)
					{
						m_clock_timer->adjust(attotime::never);
					}
					else
					{
						m_clock_timer->adjust(attotime::from_hz(100));
					}
				}
				break;
			}

			case 0x0c: // watchdog hundredths
			case 0x0d: // watchdog seconds
			{
				if ((m_ram[REGISTER_COMMAND] & COMMAND_WAF) != 0 && (m_ram[REGISTER_COMMAND] & COMMAND_WAM) == 0)
				{
					m_ram[REGISTER_COMMAND] &= ~COMMAND_WAF;
					m_watchdog_alarm = 0;
					if (m_ram[REGISTER_COMMAND] & COMMAND_IPSW)
						safe_intb_cb(0);
					else
						safe_inta_cb(0);
				}
				m_ram[offset] = data;
				UINT8 wd_hundredths = m_ram[REGISTER_WATCHDOG_HUNDREDTHS];
				UINT8 wd_seconds = m_ram[REGISTER_WATCHDOG_SECONDS];
				int total_hundredths = (wd_hundredths & 0xf) + ((wd_hundredths >> 4) & 0xf) * 10 + (wd_seconds & 0xf) * 100 + ((wd_seconds >> 4) & 0xf) * 1000;
				m_watchdog_timer->adjust(attotime::from_msec(total_hundredths * 10));
				break;
			}

			case 0x0b: // command
			{
				UINT8 old = m_ram[offset];
				m_ram[offset] = data & 0xfc;
				UINT8 changed = old ^ m_ram[offset];
				if (changed & COMMAND_IPSW)
				{
					int old_a = (old & COMMAND_IPSW) ? m_tod_alarm : m_watchdog_alarm;
					int old_b = (old & COMMAND_IPSW) ? m_watchdog_alarm : m_tod_alarm;
					int new_a = old_b;
					int new_b = old_a;

					bool a_changed = old_a != new_a;
					bool b_changed = old_b != new_b;

					std::swap(m_tod_alarm, m_watchdog_alarm);

					if (a_changed)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_inta_cb(m_tod_alarm);
						else
							safe_inta_cb(m_watchdog_alarm);
					}
					if (b_changed)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_intb_cb(m_watchdog_alarm);
						else
							safe_intb_cb(m_tod_alarm);
					}
				}
				if (changed & COMMAND_WAM)
				{
					if (m_ram[offset] & COMMAND_WAF)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_intb_cb(0);
						else
							safe_inta_cb(0);
					}
					else if (m_watchdog_alarm)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_intb_cb(m_watchdog_alarm);
						else
							safe_inta_cb(m_watchdog_alarm);
					}
				}
				if (changed & COMMAND_TDM)
				{
					if (m_ram[offset] & COMMAND_TDF)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_inta_cb(0);
						else
							safe_intb_cb(0);
					}
					else if (m_tod_alarm)
					{
						if (m_ram[offset] & COMMAND_IPSW)
							safe_inta_cb(m_tod_alarm);
						else
							safe_intb_cb(m_tod_alarm);
					}
				}
				break;
			}
		}
	}
}

READ8_MEMBER( ds1386_device::data_r )
{
	if (offset >= m_ram_size)
		return 0;

	return m_ram[offset];
}