summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/lh79524_timer.cpp
blob: d0656f010e5c3ec970fdcf41c41949495b930160 (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
// license:BSD-3-Clause
// copyright-holders:Myrtle Shah
/*****************************************************************************

        LH79524 Timer

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

#include "emu.h"
#include "lh79524_timer.h"


DEFINE_DEVICE_TYPE(LH79524_TIMER, lh79524_timer_device, "lh79524_timer", "LH79524 Timer")


lh79524_timer_device::lh79524_timer_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, LH79524_TIMER, tag, owner, clock)
	, m_irq_cb(*this) {
}

void lh79524_timer_device::set_timer_index(int index) {
	timer_index = index;
}


void lh79524_timer_device::device_start() {
	m_tick_timer = timer_alloc(FUNC(lh79524_timer_device::timer_update), this);

	save_item(NAME(m_control));
	save_item(NAME(m_cap_control));
	save_item(NAME(m_inten));
	save_item(NAME(m_status));
	save_item(NAME(m_cnt));
	save_item(NAME(m_cmp0));
	save_item(NAME(m_cmp1));
}

void lh79524_timer_device::device_reset() {
	m_control = 0;
	m_cap_control = 0;
	m_inten = 0;
	m_status = 0;
	m_cnt = 0;
	m_cmp0 = 0;
	m_cmp1 = 0;

	m_tick_timer->adjust(attotime::never);

	update_interrupt();
}

void lh79524_timer_device::update_interrupt() {
	if ((m_status & 0x7) & (m_inten & 0x7)) {
		m_irq_cb(ASSERT_LINE);
	} else {
		m_irq_cb(CLEAR_LINE);
	}

}

void lh79524_timer_device::device_clock_changed() {
	if (BIT(m_control, 1)) {
		uint32_t hclk_div_value = (m_control >> 2) & 0x7;
		m_tick_timer->adjust(attotime::from_hz(clock() / (2U << hclk_div_value)), 0, attotime::from_hz(clock() / (2U << hclk_div_value)));
	} else {
		m_tick_timer->adjust(attotime::never);
	}
}

TIMER_CALLBACK_MEMBER(lh79524_timer_device::timer_update) {
	if (!BIT(m_control, 1))
		return;

	bool tc = (timer_index == 0) ?  BIT(m_cap_control, 14) : BIT(m_control, 13);

	uint16_t limit = tc ? m_cmp1 : 0xffff;

	if (m_cnt == m_cmp0) {
		m_status |= 0x0002;
	}

	if (m_cnt == m_cmp1) {
		m_status |= 0x0004;
	}

	if (m_cnt == limit) {
		m_cnt = 0;
		m_status |= 0x0001;
	} else {
		m_cnt++;
	}

	update_interrupt();
}

uint32_t lh79524_timer_device::read(offs_t offset, uint32_t mem_mask) {
	offs_t addr = offset << 2;
	if (timer_index >= 1 && addr >= 0x04) {
		addr += 0x04;
	}
	switch (addr) {
	case 0x00:
		return m_control;
	case 0x04:
		return m_cap_control;
	case 0x08:
		return m_inten;
	case 0x0c:
		return m_status;
	case 0x10:
		return m_cnt;
	case 0x14:
		return m_cmp0;
	case 0x18:
		return m_cmp1;
	default:
		logerror("[%s] %s: unknown read %x\n", tag(), machine().describe_context(), offset << 2);
	}

	return 0;
}

void lh79524_timer_device::write(offs_t offset, uint32_t data, uint32_t mem_mask) {
	offs_t addr = offset << 2;
	if (timer_index >= 1 && addr >= 0x04) {
		addr += 0x04;
	}
	switch (addr) {
	case 0x00:
		if (data & 0x1) {
			m_cnt = 0;
			data &= ~0x1;
		}
		COMBINE_DATA(&m_control);
		lh79524_timer_device::device_clock_changed();
		break;
	case 0x04:
		COMBINE_DATA(&m_cap_control);
		break;
	case 0x08:
		COMBINE_DATA(&m_inten);
		update_interrupt();
		break;
	case 0x0c:
		if (BIT(mem_mask, 0) && BIT(data, 0)) {
			m_status &= ~0x1;
		}
		if (BIT(mem_mask, 1) && BIT(data, 1)) {
			m_status &= ~0x2;
		}
		if (BIT(mem_mask, 2) && BIT(data, 2)) {
			m_status &= ~0x4;
		}
		update_interrupt();
		break;
	case 0x10:
		COMBINE_DATA(&m_cnt);
		break;
	case 0x14:
		COMBINE_DATA(&m_cmp0);
		break;
	case 0x18:
		COMBINE_DATA(&m_cmp1);
		break;
	default:
		logerror("[%s] %s: unknown write %x = %x\n", tag(), machine().describe_context(), offset << 2, data);
	}
}