summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/timehelp.h
blob: 62626b3e25bbd26e093c712d051f1f39bacebc72 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles,smf
/***************************************************************************

    timehelp.h

    Assorted shared functionality between timekeeping chips and RTCs.

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

#pragma once

#ifndef SRC_DEVICES_MACHINE_TIMEHELP_H
#define SRC_DEVICES_MACHINE_TIMEHELP_H

class time_helper
{
public:
	static inline uint8_t make_bcd(uint8_t data)
	{
		return (((data / 10) % 10) << 4) + (data % 10);
	}

	static inline uint8_t from_bcd(uint8_t data)
	{
		return (((data >> 4) & 15) * 10) + (data & 15);
	}

	static int inc_bcd(uint8_t *data, int mask, int min, int max)
	{
		int bcd = (*data + 1) & mask;
		int carry = 0;

		if ((bcd & 0x0f) > 9)
		{
			bcd &= 0xf0;
			bcd += 0x10;
			if (bcd > max)
			{
				bcd = min;
				carry = 1;
			}
		}

		*data = (*data & ~mask) | (bcd & mask);
		return carry;
	}

};

#endif // SRC_DEVICES_MACHINE_TIMEHELP_H