summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/ds1302.c
blob: 9d02b1b97d3757c523bf814e68b1ff3f8fd91c30 (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
/************************************************************

    DALLAS DS1302

    RTC + BACKUP RAM



    Emulation by ElSemi


    Missing Features:
      - Burst Mode
      - Clock programming (useless)



    2009-05 Converted to be a device

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


#include "emu.h"
#include "ds1302.h"
#include "devhelpr.h"


/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

INLINE UINT8 convert_to_bcd(int val)
{
	return ((val / 10) << 4) | (val % 10);
}


/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

//**************************************************************************
//  DEVICE CONFIGURATION
//**************************************************************************

//-------------------------------------------------
//  ds1302_device_config - constructor
//-------------------------------------------------

ds1302_device_config::ds1302_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
    : device_config(mconfig, static_alloc_device_config, "Dallas DS1302 RTC", tag, owner, clock)
{
}


//-------------------------------------------------
//  static_alloc_device_config - allocate a new
//  configuration object
//-------------------------------------------------

device_config *ds1302_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
{
    return global_alloc(ds1302_device_config(mconfig, tag, owner, clock));
}


//-------------------------------------------------
//  alloc_device - allocate a new device object
//-------------------------------------------------

device_t *ds1302_device_config::alloc_device(running_machine &machine) const
{
    return auto_alloc(&machine, ds1302_device(machine, *this));
}


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

const device_type DS1302 = ds1302_device_config::static_alloc_device_config;

//-------------------------------------------------
//  ds1302_device - constructor
//-------------------------------------------------

ds1302_device::ds1302_device(running_machine &_machine, const ds1302_device_config &config)
    : device_t(_machine, config),
      m_config(config)
{

}

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

void ds1302_device::device_start()
{
	state_save_register_device_item(this, 0, m_shift_in);
	state_save_register_device_item(this, 0, m_shift_out);
	state_save_register_device_item(this, 0, m_icount);
	state_save_register_device_item(this, 0, m_last_clk);
	state_save_register_device_item(this, 0, m_last_cmd);
	state_save_register_device_item_array(this, 0, m_sram);
}


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

void ds1302_device::device_reset()
{
	m_shift_in  = 0;
	m_shift_out = 0;
	m_icount    = 0;
	m_last_clk  = 0;
	m_last_cmd  = 0;
}


/*-------------------------------------------------
    ds1302_dat_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER_TRAMPOLINE(ds1302, ds1302_dat_w)
{
	if (data)
	{
		m_shift_in |= (1 << m_icount);
	}
	else
	{
		m_shift_in &= ~(1 << m_icount);
	}
}


/*-------------------------------------------------
    ds1302_clk_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER_TRAMPOLINE(ds1302, ds1302_clk_w)
{
	if (data != m_last_clk)
	{
		if (data)	//Rising, shift in command
		{
			m_icount++;
			if(m_icount == 8)	//Command start
			{
				system_time systime;
				m_machine.base_datetime(systime);

				switch(m_shift_in)
				{
					case 0x81:	//Sec
						m_shift_out = convert_to_bcd(systime.local_time.second);
						break;

					case 0x83:	//Min
						m_shift_out = convert_to_bcd(systime.local_time.minute);
						break;

					case 0x85:	//Hour
						m_shift_out = convert_to_bcd(systime.local_time.hour);
						break;

					case 0x87:	//Day
						m_shift_out = convert_to_bcd(systime.local_time.mday);
						break;

					case 0x89:	//Month
						m_shift_out = convert_to_bcd(systime.local_time.month + 1);
						break;

					case 0x8b:	//weekday
						m_shift_out = convert_to_bcd(systime.local_time.weekday);
						break;

					case 0x8d:	//Year
						m_shift_out = convert_to_bcd(systime.local_time.year % 100);
						break;

					default:
						m_shift_out = 0x0;
				}

				if(m_shift_in > 0xc0)
				{
					m_shift_out = m_sram[(m_shift_in >> 1) & 0x1f];
				}
				m_last_cmd = m_shift_in & 0xff;
				m_icount++;
			}

			if(m_icount == 17 && !(m_last_cmd & 1))
			{
				UINT8 val = (m_shift_in >> 9) & 0xff;

				switch(m_last_cmd)
				{
					case 0x80:	//Sec
						break;

					case 0x82:	//Min
						break;

					case 0x84:	//Hour
						break;

					case 0x86:	//Day
						break;

					case 0x88:	//Month
						break;

					case 0x8a:	//weekday
						break;

					case 0x8c:	//Year
						break;

					default:
						m_shift_out = 0x0;
				}

				if(m_last_cmd > 0xc0)
				{
					m_sram[(m_last_cmd >> 1) & 0x1f] = val;
				}



			}
		}
	}
	m_last_clk = data;
}


/*-------------------------------------------------
    ds1302_read
-------------------------------------------------*/

READ8_DEVICE_HANDLER_TRAMPOLINE(ds1302, ds1302_read)
{
	return (m_shift_out & (1 << (m_icount - 9))) ? 1 : 0;
}