summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/aicartc.cpp
blob: c6d5beb5a45099b6c1b40ee107092e5c5fd71ee2 (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
// license:BSD-3-Clause
// copyright-holders:Angelo Salese
/***************************************************************************

    AICA-RTC sub-device

    TODO:
    - move this inside AICA sound core once that'll get modernized

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

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



//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(AICARTC, aicartc_device, "aicartc", "AICA RTC")


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

//-------------------------------------------------
//  aicartc_device - constructor
//-------------------------------------------------

aicartc_device::aicartc_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, AICARTC, tag, owner, clock)
	, device_rtc_interface(mconfig, *this)
	, m_rtc_reg_lo(0), m_rtc_reg_hi(0), m_rtc_tick(0), m_we(0)
	, m_clock_timer(nullptr)
{
}


//-------------------------------------------------
//  device_validity_check - perform validity checks
//  on this device
//-------------------------------------------------

void aicartc_device::device_validity_check(validity_checker &valid) const
{
}

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

void aicartc_device::device_start()
{
	m_clock_timer = timer_alloc();
	m_clock_timer->adjust(attotime::from_hz(clock()), 0, attotime::from_hz(clock()));
}


//-------------------------------------------------
//  rtc_clock_updated -
//-------------------------------------------------

void aicartc_device::rtc_clock_updated(int year, int month, int day, int day_of_week, int hour, int minute, int second)
{
	const int month_to_day_conversion[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

	// put the seconds
	uint32_t current_time = second;

	// put the minutes
	current_time += minute * 60;

	// put the hours
	current_time += hour * 60 * 60;

	// put the days (note -1) */
	current_time += (day - 1) * 60 * 60 * 24;

	// take the months - despite popular beliefs, leap years aren't just evenly divisible by 4 */
	if (((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) && month > 2)
		current_time += (month_to_day_conversion[month - 1] + 1) * 60 * 60 * 24;
	else
		current_time += (month_to_day_conversion[month - 1]) * 60 * 60 * 24;

	// put the years
	int year_count = (year - 1949);

	for (int i = 0; i < year_count - 1; i++)
		current_time += (((((i+1950) % 4) == 0) && (((i+1950) % 100) != 0)) || (((i+1950) % 400) == 0)) ? 60*60*24*366 : 60*60*24*365;

	m_rtc_reg_lo = current_time & 0x0000ffff;
	m_rtc_reg_hi = (current_time & 0xffff0000) >> 16;
}


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

void aicartc_device::device_reset()
{
	m_rtc_tick = 0;
}


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


void aicartc_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	m_rtc_tick++;
	if(m_rtc_tick & 0x8000)
	{
		m_rtc_tick = 0;
		m_rtc_reg_lo++;
		if(m_rtc_reg_lo == 0)
			m_rtc_reg_hi++;
	}
}


//**************************************************************************
//  READ/WRITE HANDLERS
//**************************************************************************

READ16_MEMBER( aicartc_device::read )
{
	uint16_t res;

	res = 0;
	switch(offset)
	{
		case 0:
			res = m_rtc_reg_hi; break;
		case 1:
			res = m_rtc_reg_lo; break;
	}

	return res;
}

WRITE16_MEMBER( aicartc_device::write )
{
	switch(offset)
	{
		case 0:
			if(m_we)
			{
				COMBINE_DATA(&m_rtc_reg_hi);
				// clear write enable here?
			}

			break;

		case 1:
			if(m_we)
			{
				COMBINE_DATA(&m_rtc_reg_lo);
				m_rtc_tick = 0; // low register also clears tick count
			}

			break;

		case 2:
			m_we = data & 1;
			break;
	}

}