summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/msm5001n.cpp
blob: 4fd30735314e52f296929fa9a14502ec7bfbf1ad (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
// license:BSD-3-Clause
// copyright-holders:hap
/*

OKI MSM5001N CMOS LCD Watch IC

Decap shows it's not an MCU. No known documentation exists, but there are datasheets
available for equivalent Samsung chips: KS5198, KS5199A, KS5114.

These kind of chips were used a lot for cheap 2-button digital wristwatches.

TODO:
- add D/S inputs (other display modes, setup mode)
- datasheets mention a 4-year calendar, does it mean it supports leap years somehow?
- one of the Samsung datasheets mention 24hr mode, does the MSM5001N support that?

*/

#include "emu.h"
#include "msm5001n.h"


DEFINE_DEVICE_TYPE(MSM5001N, msm5001n_device, "msm5001n", "OKI MSM5001N LCD Watch")

//-------------------------------------------------
//  constructor
//-------------------------------------------------

msm5001n_device::msm5001n_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock) :
	device_t(mconfig, MSM5001N, tag, owner, clock),
	device_rtc_interface(mconfig, *this),
	device_nvram_interface(mconfig, *this),
	m_write_segs(*this)
{ }


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

// allow save_item on a non-fundamental type
ALLOW_SAVE_TYPE(msm5001n_device::mode);

void msm5001n_device::device_start()
{
	m_timer = timer_alloc(FUNC(msm5001n_device::clock_tick), this);

	m_power = true;
	initialize();

	// register for savestates
	save_item(NAME(m_power));
	save_item(NAME(m_mode));
	save_item(NAME(m_counter));
}

void msm5001n_device::initialize()
{
	m_mode = MODE_NORMAL_HRMIN;
	m_counter = 0;

	// 1 January, 1AM
	set_time(true, 0, 1, 1, 1, 1, 0, 0);
}


//-------------------------------------------------
//  input pins
//-------------------------------------------------

void msm5001n_device::d_w(int state)
{
}

void msm5001n_device::s_w(int state)
{
}

void msm5001n_device::power_w(int state)
{
	if (m_power && !state)
	{
		// reset chip when power goes off
		initialize();
		write_lcd(nullptr, false);
	}

	m_power = bool(state);
}

void msm5001n_device::device_clock_changed()
{
	// smallest interval (at default frequency of 32768Hz) is LCD refresh at 32Hz
	attotime period = attotime::from_ticks(1024, clock());
	m_timer->adjust(period, 0, period);

	// clear LCD if clock stopped
	if (clock() == 0)
		write_lcd(nullptr, false);
}


//-------------------------------------------------
//  process
//-------------------------------------------------

void msm5001n_device::write_lcd(u8 *digits, bool colon)
{
	u32 segs = 0;

	if (digits)
	{
		// 0-9, A, P, none
		static const u8 lut_segs[0x10] =
			{ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x73,0x00,0x00,0x00,0x00 };

		for (int i = 0; i < 4; i++)
			segs |= lut_segs[digits[i]] << (8 * i);
	}
	segs |= colon ? 0x80 : 0;

	// COM1: BC1,F2,A2,B2,COLON,F3,AD3,B3,F4,A4,B4
	m_write_segs(0, bitswap<11>(segs,25,21,16,17,7,13,8,9,5,0,1));

	// COM2: D2,E2,G2,C2,D4,E3,G3,C3,E4,G4,C4
	m_write_segs(1, bitswap<11>(segs,19,20,22,18,3,12,14,10,4,6,2));
}

TIMER_CALLBACK_MEMBER(msm5001n_device::clock_tick)
{
	if (!m_power)
		return;

	m_counter++;
	if ((m_counter & 0x1f) == 0)
		advance_seconds();

	u8 digits[4];
	for (int i = 0; i < 4; i++)
		digits[i] = 0xf;
	bool colon = false;

	// convert current time to BCD
	u8 minute = convert_to_bcd(get_clock_register(RTC_MINUTE));
	u8 hour = get_clock_register(RTC_HOUR) % 12;
	hour = convert_to_bcd((hour == 0) ? 12 : hour);

	switch (m_mode)
	{
		case MODE_NORMAL_HRMIN:
			digits[0] = minute & 0xf;
			digits[1] = minute >> 4;
			digits[2] = hour & 0xf;
			if (hour & 0xf0)
				digits[3] = hour >> 4;

			colon = !BIT(m_counter, 4);
			break;

		default:
			break;
	}

	write_lcd(digits, colon);
}


//-------------------------------------------------
//  nvram
//-------------------------------------------------

bool msm5001n_device::nvram_write(util::write_stream &file)
{
	u8 buf[5];

	// current time
	for (int i = 0; i < 5; i++)
		buf[i] = get_clock_register(i);

	auto const [err, actual] = write(file, &buf, sizeof(buf));
	if (err)
		return false;

	return true;
}

bool msm5001n_device::nvram_read(util::read_stream &file)
{
	u8 buf[5];
	auto const [err, actual] = read(file, &buf, sizeof(buf));
	if (err || (sizeof(buf) != actual))
		return false;

	// current time
	for (int i = 0; i < 5; i++)
		set_clock_register(i, buf[i]);

	return true;
}