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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
// license:BSD-3-Clause
// copyright-holders:AJR
/***************************************************************************
Kawasaki Steel (Kawatetsu) KP63(A) Timer/Counter
These macro cells provide 4 independent 16-bit down counters (reduced
to 3 in some versions) driven by an 8-bit prescaler attached to the
system clock. This prescaler is not fully emulated here, since its
operations are mostly transparent, though a divide-by-4 clock output
(SYNC) may be selected to appear on a port pin.
Each counter has a single and optional external input (GATEn), which
on the KP63 can only be used to gate a divide-by-4 count but can also
be configured as an input clock on the KP63A.
Two outputs are generated for each counter. The pulse or toggle output
(OUTPn) has configurable polarity and can be used for 8-bit PWM. The
strobe output (OUTSn) goes active high for 4 clock cycles when the
counter underflows and is connected to the interrupt controller.
Writing the initial count register (CR) and reading the current count
are two-step processes, effective at the second write or first read.
These must not be overlapped with each other since they share a
temporary register.
***************************************************************************/
#include "emu.h"
#include "kp63.h"
#define VERBOSE 0
#include "logmacro.h"
//**************************************************************************
// GLOBAL VARIABLES
//**************************************************************************
// device type definitions
DEFINE_DEVICE_TYPE(KP63_3CHANNEL, kp63_3channel_device, "kp63_3channel", "Kawasaki Steel KP63 Timer/Counter (3 channels)")
DEFINE_DEVICE_TYPE(KP63A, kp63a_device, "kp63a", "Kawasaki Steel KP63A Timer/Counter")
const char *const kp63_device::s_count_modes[4] =
{
"one-shot",
"continuous count",
"WDT",
"PWM"
};
//**************************************************************************
// KP63 DEVICE
//**************************************************************************
//-------------------------------------------------
// kp63_device - constructor
//-------------------------------------------------
kp63_device::kp63_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, const XTAL &clock, u8 num_counters, u8 mode_mask)
: device_t(mconfig, type, tag, owner, clock)
, m_out_pulse_callback(*this)
, m_out_strobe_callback(*this)
, c_num_counters(num_counters)
, c_mode_mask(mode_mask)
, m_timer{0}
, m_strobe_timer{0}
, m_pwm_timer{0}
, m_cr{0}
, m_last_count{0}
, m_count_tmp{0}
, m_status{0}
, m_rw_seq(0)
, m_timer_started(0)
, m_gate_input(0xf)
{
}
//-------------------------------------------------
// kp63_3channel_device - constructor
//-------------------------------------------------
kp63_3channel_device::kp63_3channel_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock)
: kp63_device(mconfig, KP63_3CHANNEL, tag, owner, clock, 3, 0x1f)
{
}
//-------------------------------------------------
// kp63a_device - constructor
//-------------------------------------------------
kp63a_device::kp63a_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock)
: kp63_device(mconfig, KP63A, tag, owner, clock, 4, 0x3f)
{
}
//-------------------------------------------------
// device_resolve_objects - resolve objects that
// may be needed for other devices to set
// initial conditions at start time
//-------------------------------------------------
void kp63_device::device_resolve_objects()
{
// Resolve output callbacks
m_out_pulse_callback.resolve_all_safe();
m_out_strobe_callback.resolve_all_safe();
}
//-------------------------------------------------
// timer_expired - handle timed count underflow
//-------------------------------------------------
template <int N>
TIMER_CALLBACK_MEMBER(kp63_device::timer_expired)
{
timer_pulse(N);
}
//-------------------------------------------------
// strobe_off - handle end of strobe output
//-------------------------------------------------
template <int N>
TIMER_CALLBACK_MEMBER(kp63_device::strobe_off)
{
m_out_strobe_callback[N](0);
}
//-------------------------------------------------
// pwm_off - handle PWM phase change
//-------------------------------------------------
template <int N>
TIMER_CALLBACK_MEMBER(kp63_device::pwm_off)
{
m_status[N] &= 0x7f;
m_out_pulse_callback[N](BIT(m_status[N], 4) ? 1 : 0);
}
//-------------------------------------------------
// device_start - device-specific startup
//-------------------------------------------------
void kp63_device::device_start()
{
// Setup timers
m_timer[0] = timer_alloc(FUNC(kp63_device::timer_expired<0>), this);
m_strobe_timer[0] = timer_alloc(FUNC(kp63_device::strobe_off<0>), this);
m_pwm_timer[0] = timer_alloc(FUNC(kp63_device::pwm_off<0>), this);
m_timer[1] = timer_alloc(FUNC(kp63_device::timer_expired<1>), this);
m_strobe_timer[1] = timer_alloc(FUNC(kp63_device::strobe_off<1>), this);
m_pwm_timer[1] = timer_alloc(FUNC(kp63_device::pwm_off<1>), this);
m_timer[2] = timer_alloc(FUNC(kp63_device::timer_expired<2>), this);
m_strobe_timer[2] = timer_alloc(FUNC(kp63_device::strobe_off<2>), this);
m_pwm_timer[2] = timer_alloc(FUNC(kp63_device::pwm_off<2>), this);
if (c_num_counters > 3)
{
m_timer[3] = timer_alloc(FUNC(kp63_device::timer_expired<3>), this);
m_strobe_timer[3] = timer_alloc(FUNC(kp63_device::strobe_off<3>), this);
m_pwm_timer[3] = timer_alloc(FUNC(kp63_device::pwm_off<3>), this);
}
// Save state
save_item(NAME(m_cr));
save_item(NAME(m_last_count));
save_item(NAME(m_count_tmp));
save_item(NAME(m_status));
save_item(NAME(m_rw_seq));
save_item(NAME(m_timer_started));
save_item(NAME(m_gate_input));
}
//-------------------------------------------------
// device_reset - device-specific reset
//-------------------------------------------------
void kp63_device::device_reset()
{
for (unsigned n = 0; n < c_num_counters; n++)
{
// Turn off timers
m_timer[n]->adjust(attotime::never);
m_strobe_timer[n]->adjust(attotime::never);
m_pwm_timer[n]->adjust(attotime::never);
// Reset status and count
m_status[n] = 0;
m_cr[n] = 0xffff;
m_last_count[n] = 0xffff;
// Clear outputs
m_out_pulse_callback[n](0);
m_out_strobe_callback[n](0);
}
// Clear read/write sequence for all counters
m_rw_seq = 0;
m_timer_started = 0;
}
//-------------------------------------------------
// timer_pulse - change outputs and stop or
// reload timer as count underflows
//-------------------------------------------------
void kp63_device::timer_pulse(unsigned n)
{
// Toggle pulse output
m_status[n] ^= 0x80;
m_out_pulse_callback[n](BIT(m_status[n], 7) != BIT(m_status[n], 4) ? 1 : 0);
// Begin strobe output
m_out_strobe_callback[n](1);
m_strobe_timer[n]->adjust(clocks_to_attotime(4));
// Reload timer in continuous count and PWM modes
if (BIT(m_status[n], 2))
timer_reload(n);
else
{
// Stop count at FFFF in one-shot and WDT modes
m_last_count[n] = 0xffff;
m_timer_started &= ~(1 << n);
}
}
//-------------------------------------------------
// timer_reload - reload timer from CR
//-------------------------------------------------
void kp63_device::timer_reload(unsigned n)
{
m_timer_started |= 1 << n;
if (BIT(m_status[n], 5) || ((m_status[n] & 0x03) == 0x03 && !BIT(m_gate_input, n)))
m_last_count[n] = m_cr[n];
else
{
unsigned prescale = BIT(m_status[n], 1) ? 4 : BIT(m_status[n], 0) ? 16 : 256;
if ((m_status[n] & 0x0c) == 0x0c)
{
// PWM
m_timer[n]->adjust(clocks_to_attotime(prescale * ((m_cr[n] & 0x00ff) + 1)));
m_pwm_timer[n]->adjust(clocks_to_attotime(prescale * ((m_cr[n] >> 8) + 1)));
}
else
m_timer[n]->adjust(clocks_to_attotime(prescale * (u32(m_cr[n]) + 1)));
}
}
//-------------------------------------------------
// timer_resume_count - start counting again
//-------------------------------------------------
void kp63_device::timer_resume_count(unsigned n)
{
if (!BIT(m_status[n], 5) || ((m_status[n] & 0x03) != 0x03 || BIT(m_gate_input, n)))
{
unsigned prescale = BIT(m_status[n], 1) ? 4 : BIT(m_status[n], 0) ? 16 : 256;
if ((m_status[n] & 0x0c) == 0x0c)
{
// PWM
m_timer[n]->adjust(clocks_to_attotime(prescale * ((m_last_count[n] & 0x00ff) + 1)));
m_pwm_timer[n]->adjust(clocks_to_attotime(prescale * ((m_last_count[n] >> 8) + 1)));
}
else
m_timer[n]->adjust(clocks_to_attotime(prescale * (u32(m_last_count[n]) + 1)));
}
}
//-------------------------------------------------
// timer_get_count - obtain the instant count in
// case of a readout or pause
//-------------------------------------------------
u16 kp63_device::timer_get_count(unsigned n) const
{
if (!BIT(m_timer_started, n) || BIT(m_status[n], 5) || ((m_status[n] & 0x03) == 0x03 && !BIT(m_gate_input, n)))
return m_last_count[n];
else
{
unsigned prescale = BIT(m_status[n], 1) ? 4 : BIT(m_status[n], 0) ? 16 : 256;
if ((m_status[n] & 0x0c) == 0x0c)
{
// PWM
u8 ticks = attotime_to_clocks(m_timer[n]->remaining()) / prescale;
return ticks | ((m_cr[n] - (u16(ticks) << 8)) & 0xff00);
}
else
return attotime_to_clocks(m_timer[n]->remaining()) / prescale;
}
}
//-------------------------------------------------
// read - read count or status register
//-------------------------------------------------
u8 kp63_device::read(offs_t offset)
{
const unsigned n = offset >> 1;
assert(n < c_num_counters);
if (BIT(offset, 0))
{
// Status read clears read/write sequence
if (!machine().side_effects_disabled())
m_rw_seq &= ~(1 << n);
return m_status[n];
}
else if (BIT(m_rw_seq, n))
{
// Second step of counter readout
if (!machine().side_effects_disabled())
m_rw_seq &= ~(1 << n);
return m_count_tmp[n];
}
else
{
// First step of counter readout
u16 count = timer_get_count(n);
if (!machine().side_effects_disabled())
{
// Latch high byte into TMP register
m_rw_seq |= 1 << n;
m_count_tmp[n] = count >> 8;
}
return count & 0x00ff;
}
}
//-------------------------------------------------
// write - set CR or mode register
//-------------------------------------------------
void kp63_device::write(offs_t offset, u8 data)
{
const unsigned n = offset >> 1;
assert(n < c_num_counters);
if (BIT(offset, 0))
{
bool old_outp = BIT(m_status[n], 7) != BIT(m_status[n], 4);
// Stop count before setting mode
if (BIT(m_timer_started, n))
{
if (!BIT(m_status[n], 5) || ((m_status[n] & 0x03) != 0x03 || BIT(m_gate_input, n)))
{
m_last_count[n] = timer_get_count(n);
m_timer[n]->adjust(attotime::never);
m_pwm_timer[n]->adjust(attotime::never);
}
m_timer_started &= ~(1 << n);
}
if (BIT(data & c_mode_mask, 5))
LOG("%s: Timer #%d configured for %s mode, %s edges of GATE, initial output %c\n",
machine().describe_context(),
n,
s_count_modes[BIT(data, 2, 2)],
BIT(data, 1) ? "???" : BIT(data, 0) ? "falling" : "rising",
BIT(data, 4) ? 'H' : 'L');
else
LOG("%s: Timer #%d configured for %s mode, 1/%d system clock (GATE %s), initial output %c\n",
machine().describe_context(),
n,
s_count_modes[BIT(data, 2, 2)],
BIT(data, 1) ? 4 : BIT(data, 0) ? 16 : 256,
(data & 0x03) == 0x03 ? "effective" : "ignored",
BIT(data, 4) ? 'H' : 'L');
m_status[n] = data & c_mode_mask;
// Update OUTP
if (old_outp != BIT(data, 4))
m_out_pulse_callback[n](BIT(data, 4) ? 1 : 0);
}
else if ((m_status[n] & 0x0c) == 0x08)
{
// WDT retrigger (data ignored; initial count must be written using a different mode)
timer_reload(n);
}
else if (BIT(m_rw_seq, n))
{
// Second step of initial count write
m_rw_seq &= ~(1 << n);
m_cr[n] = u16(data) << 8 | m_count_tmp[n];
LOG("%s: Timer #%d initial count = %d\n", machine().describe_context(), n, (m_status[n] == 0x0c) ? m_cr[n] & 0x00ff : m_cr[n]);
// Automatic retrigger in one-shot and continuous modes
if (!BIT(m_status[n], 3) || !BIT(m_timer_started, n))
{
if (!BIT(m_status[n], 7))
{
// Toggle OUTP
m_status[n] |= 0x80;
m_out_pulse_callback[n](BIT(m_status[n], 4) ? 0 : 1);
}
timer_reload(n);
}
}
else
{
// First step of initial count write (held in TMP register)
m_rw_seq |= 1 << n;
m_count_tmp[n] = data;
}
}
//-------------------------------------------------
// write_gate - handle gate inputs
//-------------------------------------------------
void kp63_device::write_gate(unsigned n, bool state)
{
assert(n < c_num_counters);
if (BIT(m_gate_input, n) != state)
return;
if (state)
m_gate_input |= 1 << n;
else
m_gate_input &= ~(1 << n);
if (BIT(m_timer_started, n))
{
if ((m_status[n] & 0x23) == 0x03)
{
// Timer gated on or off
if (state)
timer_resume_count(n);
else
{
m_last_count[n] = timer_get_count(n);
m_timer[n]->adjust(attotime::never);
}
}
else if ((m_status[n] & 0x23) == (state ? 0x21 : 0x20))
{
// Count edges of gate input
if ((m_status[n] & 0x0c) == 0x0c)
{
// PWM: count is in lower 8 bits
if ((m_last_count[n] & 0x00ff) == 0)
timer_pulse(n);
else
{
// Decrement both halves and check for underflow in upper half
m_last_count[n] -= 0x0101;
if (m_last_count[n] >= 0xff00)
{
m_status[n] &= 0x7f;
m_out_pulse_callback[n](BIT(m_status[n], 4) ? 1 : 0);
}
}
}
else if (m_last_count[n]-- == 0)
timer_pulse(n);
}
}
}
|