summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/rtc4543.cpp
blob: 9e883ab7fabeca84415a37fc068adf19716f9f5d (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
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/**********************************************************************

    rtc4543.c - Epson R4543 real-time clock chip emulation
    by R. Belmont

    JRC 6355E / NJU6355E is basically similar, but order of registers
    is reversed and readouts happen on falling CLK edge.

    The clock's seven registers are read out as 52 consecutive bits of
    data, with the middle register being only 4 bits wide. The bits
    are numbered 0-27 and 32-55 here for implementation convenience.

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

#include "emu.h"
#include "rtc4543.h"

//#define VERBOSE 1
#include "logmacro.h"


//**************************************************************************
//  MACROS / CONSTANTS
//**************************************************************************

char const *const rtc4543_device::s_reg_names[7] =
{
	"second",
	"minute",
	"hour",
	"day of the week",
	"day",
	"month",
	"year"
};


//**************************************************************************
//  RTC4543 DEVICE
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(RTC4543, rtc4543_device, "rtc4543", "Epson R4543 RTC")


//-------------------------------------------------
//  rtc4543_device - constructor
//-------------------------------------------------

rtc4543_device::rtc4543_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: rtc4543_device(mconfig, RTC4543, tag, owner, clock)
{
}

rtc4543_device::rtc4543_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, type, tag, owner, clock)
	, device_rtc_interface(mconfig, *this)
	, data_cb(*this)
	, m_ce(0), m_clk(0), m_wr(0), m_data(0), m_curbit(0)
	, m_clock_timer(nullptr)
{
}


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

void rtc4543_device::device_start()
{
	data_cb.resolve_safe();

	// allocate timers
	m_clock_timer = timer_alloc();
	m_clock_timer->adjust(attotime::from_hz(clock() / 32768), 0, attotime::from_hz(clock() / 32768));

	// state saving
	save_item(NAME(m_ce));
	save_item(NAME(m_clk));
	save_item(NAME(m_wr));
	save_item(NAME(m_data));
	save_item(NAME(m_regs));
	save_item(NAME(m_curbit));
}


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

void rtc4543_device::device_reset()
{
	m_ce = 0;
	m_wr = 0;
	m_clk = 0;
	m_data = 0;
	m_curbit = 0;
}


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

void rtc4543_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	advance_seconds();
}


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

void rtc4543_device::rtc_clock_updated(int year, int month, int day, int day_of_week, int hour, int minute, int second)
{
	static int const weekday[7] = { 7, 1, 2, 3, 4, 5, 6 };

	m_regs[0] = convert_to_bcd(second);                     // seconds (BCD, 0-59) in bits 0-6, bit 7 = battery low
	m_regs[1] = convert_to_bcd(minute);                     // minutes (BCD, 0-59)
	m_regs[2] = convert_to_bcd(hour);                       // hour (BCD, 0-23)
	m_regs[3] = convert_to_bcd(weekday[day_of_week - 1]);   // day of the week (1-7)
	m_regs[4] = convert_to_bcd(day);                        // day (BCD, 1-31)
	m_regs[5] = convert_to_bcd(month);                      // month (BCD, 1-12)
	m_regs[6] = convert_to_bcd(year % 100);                 // year (BCD, 0-99)
}


//-------------------------------------------------
//  ce_w - chip enable write
//-------------------------------------------------

WRITE_LINE_MEMBER( rtc4543_device::ce_w )
{
	if (!state && m_ce) // complete transfer
	{
		LOG("CE falling edge\n");
		ce_falling();
	}
	else if (state && !m_ce) // start new data transfer
	{
		LOG("CE rising edge\n");
		ce_rising();
	}

	m_ce = state;

	// timer disabled during writes
	m_clock_timer->enable(!m_ce || !m_wr);
}


//-------------------------------------------------
//  ce_rising - CE rising edge trigger
//-------------------------------------------------

void rtc4543_device::ce_rising()
{
	m_curbit = 0; // force immediate reload of output data
}


//-------------------------------------------------
//  ce_falling - CE falling edge trigger
//-------------------------------------------------

void rtc4543_device::ce_falling()
{
}


//-------------------------------------------------
//  wr_w - data direction line write
//-------------------------------------------------

WRITE_LINE_MEMBER( rtc4543_device::wr_w )
{
	if (state != m_wr)
		LOG("WR: %u\n", state);

	m_wr = state;
}


//-------------------------------------------------
//  clk_w - serial clock write
//-------------------------------------------------

WRITE_LINE_MEMBER( rtc4543_device::clk_w )
{
	if (m_ce)
	{
		int bit = m_curbit;
		if (!m_clk && state)
		{
			clk_rising();
			LOG("CLK rising edge (I/O: %u, bit %d)\n", m_data, bit);
		}
		else if (m_clk && !state)
		{
			clk_falling();
			LOG("CLK falling edge (I/O: %u, bit %d)\n", m_data, bit);
		}
	}

	m_clk = state;
}


//-------------------------------------------------
//  clk_rising - CLK rising edge trigger
//-------------------------------------------------

void rtc4543_device::clk_rising()
{
	// note: output data does not change when clk at final bit
	if (m_curbit == 56)
		return;

	// rising edge - read/write data becomes valid here
	if (!m_wr)
		load_bit(m_curbit / 8);
	else
		store_bit(m_curbit / 8);

	advance_bit();

	// update only occurs when a write goes all the way through
	if (m_wr && m_curbit == 56)
		update_effective();
}


//-------------------------------------------------
//  clk_falling - CLK falling edge trigger
//-------------------------------------------------

void rtc4543_device::clk_falling()
{
}


//-------------------------------------------------
//  data_w - I/O write
//-------------------------------------------------

WRITE_LINE_MEMBER( rtc4543_device::data_w )
{
	m_data = state & 1;
}


//-------------------------------------------------
//  data_r - I/O read
//-------------------------------------------------

READ_LINE_MEMBER( rtc4543_device::data_r )
{
	return m_data;
}


//-------------------------------------------------
//  load_bit - serial read from register
//-------------------------------------------------

void rtc4543_device::load_bit(int reg)
{
	assert(reg < ARRAY_LENGTH(m_regs));
	int bit = m_curbit & 7;

	// reload data?
	if (bit == 0)
		LOG("RTC sending low digit of %s: %x\n", s_reg_names[reg], m_regs[reg] & 0xf);
	else if (bit == 4)
		LOG("RTC sending high digit of %s: %x\n", s_reg_names[reg], (m_regs[reg] >> 4) & 0xf);

	// shift data bit
	m_data = (m_regs[reg] >> bit) & 1;
	data_cb(m_data);
}


//-------------------------------------------------
//  store_bit - serial write
//-------------------------------------------------

void rtc4543_device::store_bit(int reg)
{
	assert(reg < ARRAY_LENGTH(m_regs));
	int bit = m_curbit & 7;

	m_regs[reg] &= ~(1 << bit);
	m_regs[reg] |= m_data << bit;

	if (bit == 7)
		LOG("RTC received high digit of %s: %X\n", s_reg_names[reg], (m_regs[reg] >> 4) & 0xf);
	else if (bit == 3)
		LOG("RTC received low digit of %s: %X\n", s_reg_names[reg], m_regs[reg] & 0xf);
}


//-------------------------------------------------
//  advance_bit - increment the bit counter
//-------------------------------------------------

void rtc4543_device::advance_bit()
{
	m_curbit++;

	// day-of-week register only takes 4 bits
	if (m_curbit == 28)
	{
		// skip 4 bits, Brother Maynard
		m_curbit += 4;
	}
}


//-------------------------------------------------
//  update_effective - update the RTC
//-------------------------------------------------

void rtc4543_device::update_effective()
{
	LOG("RTC updated: %02x.%02x.%02x (%01x) %02x:%02x:%02x\n", m_regs[6], m_regs[5], m_regs[4], m_regs[3], m_regs[2], m_regs[1], m_regs[0]);
	set_time(
			false,
			bcd_to_integer(m_regs[6]),      // year
			bcd_to_integer(m_regs[5]),      // month
			bcd_to_integer(m_regs[4]),      // day
			(m_regs[3] % 7) + 1,            // day of week
			bcd_to_integer(m_regs[2]),      // hour
			bcd_to_integer(m_regs[1]),      // minute
			bcd_to_integer(m_regs[0]));     // second
}


//**************************************************************************
//  JRC 6355E DEVICE
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(JRC6355E, jrc6355e_device, "jrc6355e", "JRC 6355E RTC")


//-------------------------------------------------
//  jrc6355e_device - constructor
//-------------------------------------------------

jrc6355e_device::jrc6355e_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: rtc4543_device(mconfig, JRC6355E, tag, owner, clock)
{
}


//-------------------------------------------------
//  ce_rising - CE rising edge trigger
//-------------------------------------------------

void jrc6355e_device::ce_rising()
{
	m_curbit = 0; // force immediate reload of output data
	load_bit(6);
}



//-------------------------------------------------
//  ce_falling - CE falling edge trigger
//-------------------------------------------------

void jrc6355e_device::ce_falling()
{
	// update occurs on falling edge of CE after minutes are written
	if (m_wr && m_curbit >= 48)
	{
		// seconds are zeroed
		m_regs[0] = 0;
		update_effective();
	}
}


//-------------------------------------------------
//  clk_rising - CLK rising edge trigger
//-------------------------------------------------

void jrc6355e_device::clk_rising()
{
	if (m_curbit == 56)
		return;

	if (m_wr)
		store_bit(6 - (m_curbit / 8));
}


//-------------------------------------------------
//  clk_falling - CLK falling edge trigger
//-------------------------------------------------

void jrc6355e_device::clk_falling()
{
	if (m_curbit == 56)
		return;

	advance_bit();

	if (!m_wr && m_curbit != 56)
		load_bit(6 - (m_curbit / 8));
}