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
|
// license:BSD-3-Clause
// copyright-holders:smf
/***************************************************************************
Fujitsu Micro F2MC-16 series Programmable Pulse Generator
***************************************************************************/
#include "emu.h"
#include "f2mc16_ppg.h"
namespace {
struct PPGC { enum : uint8_t
{
PEN = 1 << 7,
PCS = 1 << 6, // channel 1
POE = 1 << 5,
PIE = 1 << 4,
PUF = 1 << 3,
PCM = 3 << 1, // channel 0
PCM_DIV1 = 0 << 1,
PCM_DIV4 = 1 << 1,
PCM_DIV16 = 2 << 1,
PCM_TIMEBASE = 3 << 1,
MD = 3 << 1, // channel 1
MD_2_CHANNEL = 0 << 1,
MD_SINGLE_CHANNEL_8BIT_PRESCALER = 1 << 1,
MD_SINGLE_CHANNEL_16BIT = 3 << 1,
RESERVED = 1 << 0
}; };
} // anonymous namespace
DEFINE_DEVICE_TYPE(F2MC16_PPG, f2mc16_ppg_device, "f2mc16_ppg", "F2MC16 Programmable Pulse Generator")
f2mc16_ppg_device::f2mc16_ppg_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, required_device<f2mc16_intc_device> &intc, uint8_t ppg0_vector, uint8_t ppg1_vector) :
f2mc16_ppg_device(mconfig, tag, owner, clock)
{
m_cpu = downcast<f2mc16_device *>(owner);
m_intc.set_tag(intc);
m_vector[0] = ppg0_vector;
m_vector[1] = ppg1_vector;
}
f2mc16_ppg_device::f2mc16_ppg_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
device_t(mconfig, F2MC16_PPG, tag, owner, clock),
m_cpu(nullptr),
m_intc(*this, finder_base::DUMMY_TAG),
m_vector{ 0,0 },
m_output_cb(*this),
m_peripheral_clock_hz(0),
m_peripheral_clock_changed(attotime::zero),
m_timebase_hz(0),
m_timebase_changed(attotime::zero),
m_clocksel(),
m_hz(),
m_duty(),
m_pcnt(),
m_pcntrl(),
m_lh(),
m_ppgc(),
m_prll(),
m_prlh()
{
}
void f2mc16_ppg_device::device_start()
{
m_timer[0] = timer_alloc(FUNC(f2mc16_ppg_device::timer_callback<0>), this);
m_timer[1] = timer_alloc(FUNC(f2mc16_ppg_device::timer_callback<1>), this);
m_start_time[0] = attotime::never;
m_start_time[1] = attotime::never;
save_item(NAME(m_peripheral_clock_hz));
save_item(NAME(m_timebase_hz));
save_item(NAME(m_start_time));
save_item(NAME(m_underflow_time));
save_item(NAME(m_clocksel));
save_item(NAME(m_hz));
save_item(NAME(m_duty));
save_item(NAME(m_pcnt));
save_item(NAME(m_pcntrl));
save_item(NAME(m_lh));
save_item(NAME(m_ppgc));
save_item(NAME(m_prll));
save_item(NAME(m_prlh));
}
void f2mc16_ppg_device::device_clock_changed()
{
if (machine().scheduler().currently_executing())
machine().scheduler().synchronize(timer_expired_delegate(FUNC(f2mc16_ppg_device::update_peripheral_clock), this), clock());
else
update_peripheral_clock(clock());
}
void f2mc16_ppg_device::device_reset()
{
update_pcnt();
for (int i = 0; i < 2; i++)
m_ppgc[i] = PPGC::PUF | PPGC::RESERVED;
update();
}
void f2mc16_ppg_device::timebase_hz(uint32_t hz)
{
if (started() && machine().scheduler().currently_executing())
machine().scheduler().synchronize(timer_expired_delegate(FUNC(f2mc16_ppg_device::update_timebase_hz), this), hz);
else
update_timebase_hz(hz);
}
uint8_t f2mc16_ppg_device::ppgc_r(offs_t offset)
{
if (!m_cpu->rmw() && m_underflow_time[offset] >= machine().time())
return m_ppgc[offset] & ~PPGC::PUF;
return m_ppgc[offset];
}
void f2mc16_ppg_device::ppgc_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
uint16_t ppgc = (m_ppgc[1] << 8) | m_ppgc[0];
uint16_t prev = ppgc;
COMBINE_DATA(&ppgc);
if (ppgc != prev)
{
update_pcnt();
m_ppgc[0] = ppgc >> 0;
m_ppgc[1] = ppgc >> 8;
if (ACCESSING_BITS_0_7 && !(m_ppgc[0] & PPGC::PUF) && m_underflow_time[0] <= machine().time())
{
m_ppgc[0] |= PPGC::PUF;
m_underflow_time[0] = attotime::never;
}
if (ACCESSING_BITS_8_15 && !(m_ppgc[1] & PPGC::PUF) && m_underflow_time[1] <= machine().time())
{
m_ppgc[1] |= PPGC::PUF;
m_underflow_time[1] = attotime::never;
}
update();
}
}
template<unsigned N>
uint16_t f2mc16_ppg_device::prl_r()
{
return (m_prlh[N] << 8) | m_prll[N];
}
template<unsigned N>
void f2mc16_ppg_device::prl_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
uint16_t prl = (m_prlh[N] << 8) | m_prll[N];
uint16_t prev = prl;
COMBINE_DATA(&prl);
if (prl != prev)
{
update_pcnt();
m_prll[N] = prl >> 0;
m_prlh[N] = prl >> 8;
update();
}
}
void f2mc16_ppg_device::update()
{
attotime now = machine().time();
for (int i = 0; i < 2; i++)
{
bool start = false;
if (!(m_ppgc[i] & PPGC::PEN))
m_start_time[i] = attotime::never;
else if (m_start_time[i].is_never())
{
m_start_time[i] = now;
start = true;
}
uint8_t pcm = (i == 0 || (m_ppgc[1] & PPGC::MD) != PPGC::MD_2_CHANNEL) ?
(m_ppgc[0] & PPGC::PCM) :
(m_ppgc[1] & PPGC::PCS) ? PPGC::PCM_TIMEBASE : PPGC::PCM_DIV1;
m_clocksel[i] = m_start_time[i].is_never() ? 0 :
(pcm == PPGC::PCM_DIV1) ? m_peripheral_clock_hz :
(pcm == PPGC::PCM_DIV4) ? m_peripheral_clock_hz / 4 :
(pcm == PPGC::PCM_DIV16) ? m_peripheral_clock_hz / 16 :
(pcm == PPGC::PCM_TIMEBASE) ? m_timebase_hz :
0;
if ((m_ppgc[1] & PPGC::MD) == PPGC::MD_2_CHANNEL)
{
m_pcntrl[i][0] = m_prll[i] + 1;
m_pcntrl[i][1] = m_prlh[i] + 1;
}
else if ((m_ppgc[1] & PPGC::MD) == PPGC::MD_SINGLE_CHANNEL_8BIT_PRESCALER)
{
if (i == 0)
{
m_pcntrl[i][0] = m_prll[i] + 1;
m_pcntrl[i][1] = m_prlh[i] + 1;
}
else
{
uint16_t prescale = (m_prll[0] + 1) + (m_prlh[0] + 1);
m_pcntrl[i][0] = (m_prll[i] + 1) * prescale;
m_pcntrl[i][1] = (m_prlh[i] + 1) * prescale;
}
}
else if ((m_ppgc[1] & PPGC::MD) == PPGC::MD_SINGLE_CHANNEL_16BIT)
{
m_pcntrl[i][0] = ((m_prll[1] << 8) | m_prll[0]) + 1;
m_pcntrl[i][1] = ((m_prlh[1] << 8) | m_prlh[0]) + 1;
}
else
{
m_pcntrl[i][0] = 0;
m_pcntrl[i][1] = 0;
}
if (start)
m_pcnt[i][m_lh[i]] = m_pcntrl[i][m_lh[i]];
m_pcnt[i][!m_lh[i]] = m_pcntrl[i][!m_lh[i]];
attotime event_time = attotime::never;
attotime puf = m_start_time[i].is_never() || !m_clocksel[i] ? attotime::never :
m_start_time[i] + attotime::from_ticks(m_pcnt[i][m_lh[i]], m_clocksel[i]);
if (m_underflow_time[i] > now)
{
m_underflow_time[i] = puf;
if ((m_ppgc[i] & PPGC::PIE) && event_time > puf)
event_time = puf;
}
uint32_t total = (m_ppgc[i] & PPGC::POE) ? m_pcntrl[i][0] + m_pcntrl[i][1] : 0;
uint32_t hz = total ? m_clocksel[i] / total : 0;
uint32_t duty = hz ? (0x100000000ULL * m_pcntrl[i][1]) / total : 0;
if (m_hz[i] != hz || m_duty[i] != duty)
{
if ((m_lh[i] || m_pcnt[i][0] != m_pcntrl[i][0] || m_pcnt[i][1] != m_pcntrl[i][1]) && hz)
{
if (event_time > puf)
event_time = puf;
}
else
{
if (!m_output_cb[i].isunset())
m_output_cb[i](0, hz, duty);
else if ((m_ppgc[1] & PPGC::MD) != PPGC::MD_SINGLE_CHANNEL_8BIT_PRESCALER || (m_output_cb[0].isunset() && m_output_cb[1].isunset()))
logerror("%s PPG%d unmapped write %d, %d, %08x\n", machine().describe_context(), i, 0, hz, duty);
m_hz[i] = hz;
m_duty[i] = duty;
}
}
m_timer[i]->adjust(event_time.is_never() ? event_time : event_time - now);
m_intc->set_irq(m_vector[i], ((m_ppgc[i] & PPGC::PIE) && m_underflow_time[i] <= now) ? 1 : 0);
}
}
void f2mc16_ppg_device::update_pcnt()
{
for (int i = 0; i < 2; i++)
{
if (m_start_time[i] != attotime::never)
{
attotime now = machine().time();
int64_t ticks = (now - m_start_time[i]).as_ticks(m_clocksel[i]);
for (int p = 0; p < 4 && ticks; p++)
{
if (m_pcnt[i][m_lh[i]] > ticks)
{
m_pcnt[i][m_lh[i]] -= ticks;
ticks = 0;
}
else
{
ticks -= m_pcnt[i][m_lh[i]];
m_pcnt[i][m_lh[i]] = m_pcntrl[i][m_lh[i]];
m_lh[i] = !m_lh[i];
}
if (p == 1 && (m_pcntrl[i][0] + m_pcntrl[i][1]))
ticks %= (m_pcntrl[i][0] + m_pcntrl[i][1]);
}
m_start_time[i] = now;
}
}
}
TIMER_CALLBACK_MEMBER(f2mc16_ppg_device::update_timebase_hz)
{
update_pcnt();
m_timebase_hz = param;
if (started())
{
m_timebase_changed = machine().time();
update();
}
}
TIMER_CALLBACK_MEMBER(f2mc16_ppg_device::update_peripheral_clock)
{
update_pcnt();
m_peripheral_clock_hz = param;
m_peripheral_clock_changed = machine().time();
update();
}
template <unsigned N>
TIMER_CALLBACK_MEMBER(f2mc16_ppg_device::timer_callback)
{
m_lh[N] = !m_lh[N];
m_pcnt[N][m_lh[N]] = m_pcntrl[N][m_lh[N]];
m_start_time[N] = machine().time();
update();
}
template uint16_t f2mc16_ppg_device::prl_r<0>();
template uint16_t f2mc16_ppg_device::prl_r<1>();
template void f2mc16_ppg_device::prl_w<0>(offs_t offset, uint16_t data, uint16_t mem_mask);
template void f2mc16_ppg_device::prl_w<1>(offs_t offset, uint16_t data, uint16_t mem_mask);
|