2 * QEMU 8253/8254 interval timer emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu-timer.h"
31 #define RW_STATE_LSB 1
32 #define RW_STATE_MSB 2
33 #define RW_STATE_WORD0 3
34 #define RW_STATE_WORD1 4
36 typedef struct PITChannelState {
37 int count; /* can be 65536 */
38 uint16_t latched_count;
39 uint8_t count_latched;
40 uint8_t status_latched;
47 uint8_t bcd; /* not supported */
48 uint8_t gate; /* timer start */
49 int64_t count_load_time;
51 int64_t next_transition_time;
57 PITChannelState channels[3];
60 static PITState pit_state;
62 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
64 static int pit_get_count(PITChannelState *s)
69 d = muldiv64(qemu_get_clock(vm_clock) - s->count_load_time, PIT_FREQ, ticks_per_sec);
75 counter = (s->count - d) & 0xffff;
78 /* XXX: may be incorrect for odd counts */
79 counter = s->count - ((2 * d) % s->count);
82 counter = s->count - (d % s->count);
88 /* get pit output bit */
89 static int pit_get_out1(PITChannelState *s, int64_t current_time)
94 d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec);
98 out = (d >= s->count);
101 out = (d < s->count);
104 if ((d % s->count) == 0 && d != 0)
110 out = (d % s->count) < ((s->count + 1) >> 1);
114 out = (d == s->count);
120 int pit_get_out(PITState *pit, int channel, int64_t current_time)
122 PITChannelState *s = &pit->channels[channel];
123 return pit_get_out1(s, current_time);
126 /* return -1 if no transition will occur. */
127 static int64_t pit_get_next_transition_time(PITChannelState *s,
128 int64_t current_time)
130 uint64_t d, next_time, base;
133 d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec);
139 next_time = s->count;
144 base = (d / s->count) * s->count;
145 if ((d - base) == 0 && d != 0)
146 next_time = base + s->count;
148 next_time = base + s->count + 1;
151 base = (d / s->count) * s->count;
152 period2 = ((s->count + 1) >> 1);
153 if ((d - base) < period2)
154 next_time = base + period2;
156 next_time = base + s->count;
161 next_time = s->count;
162 else if (d == s->count)
163 next_time = s->count + 1;
168 /* convert to timer units */
169 next_time = s->count_load_time + muldiv64(next_time, ticks_per_sec, PIT_FREQ);
170 /* fix potential rounding problems */
171 /* XXX: better solution: use a clock at PIT_FREQ Hz */
172 if (next_time <= current_time)
173 next_time = current_time + 1;
177 /* val must be 0 or 1 */
178 void pit_set_gate(PITState *pit, int channel, int val)
180 PITChannelState *s = &pit->channels[channel];
186 /* XXX: just disable/enable counting */
191 /* restart counting on rising edge */
192 s->count_load_time = qemu_get_clock(vm_clock);
193 pit_irq_timer_update(s, s->count_load_time);
199 /* restart counting on rising edge */
200 s->count_load_time = qemu_get_clock(vm_clock);
201 pit_irq_timer_update(s, s->count_load_time);
203 /* XXX: disable/enable counting */
209 int pit_get_gate(PITState *pit, int channel)
211 PITChannelState *s = &pit->channels[channel];
215 int pit_get_initial_count(PITState *pit, int channel)
217 PITChannelState *s = &pit->channels[channel];
221 int pit_get_mode(PITState *pit, int channel)
223 PITChannelState *s = &pit->channels[channel];
227 static inline void pit_load_count(PITChannelState *s, int val)
231 s->count_load_time = qemu_get_clock(vm_clock);
233 pit_irq_timer_update(s, s->count_load_time);
236 /* if already latched, do not latch again */
237 static void pit_latch_count(PITChannelState *s)
239 if (!s->count_latched) {
240 s->latched_count = pit_get_count(s);
241 s->count_latched = s->rw_mode;
245 static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val)
247 PITState *pit = opaque;
255 /* read back command */
256 for(channel = 0; channel < 3; channel++) {
257 s = &pit->channels[channel];
258 if (val & (2 << channel)) {
262 if (!(val & 0x10) && !s->status_latched) {
264 /* XXX: add BCD and null count */
265 s->status = (pit_get_out1(s, qemu_get_clock(vm_clock)) << 7) |
269 s->status_latched = 1;
274 s = &pit->channels[channel];
275 access = (val >> 4) & 3;
280 s->read_state = access;
281 s->write_state = access;
283 s->mode = (val >> 1) & 7;
285 /* XXX: update irq timer ? */
289 s = &pit->channels[addr];
290 switch(s->write_state) {
293 pit_load_count(s, val);
296 pit_load_count(s, val << 8);
299 s->write_latch = val;
300 s->write_state = RW_STATE_WORD1;
303 pit_load_count(s, s->write_latch | (val << 8));
304 s->write_state = RW_STATE_WORD0;
310 static uint32_t pit_ioport_read(void *opaque, uint32_t addr)
312 PITState *pit = opaque;
317 s = &pit->channels[addr];
318 if (s->status_latched) {
319 s->status_latched = 0;
321 } else if (s->count_latched) {
322 switch(s->count_latched) {
325 ret = s->latched_count & 0xff;
326 s->count_latched = 0;
329 ret = s->latched_count >> 8;
330 s->count_latched = 0;
333 ret = s->latched_count & 0xff;
334 s->count_latched = RW_STATE_MSB;
338 switch(s->read_state) {
341 count = pit_get_count(s);
345 count = pit_get_count(s);
346 ret = (count >> 8) & 0xff;
349 count = pit_get_count(s);
351 s->read_state = RW_STATE_WORD1;
354 count = pit_get_count(s);
355 ret = (count >> 8) & 0xff;
356 s->read_state = RW_STATE_WORD0;
363 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
370 expire_time = pit_get_next_transition_time(s, current_time);
371 irq_level = pit_get_out1(s, current_time);
372 qemu_set_irq(s->irq, irq_level);
374 printf("irq_level=%d next_delay=%f\n",
376 (double)(expire_time - current_time) / ticks_per_sec);
378 s->next_transition_time = expire_time;
379 if (expire_time != -1)
380 qemu_mod_timer(s->irq_timer, expire_time);
382 qemu_del_timer(s->irq_timer);
385 static void pit_irq_timer(void *opaque)
387 PITChannelState *s = opaque;
389 pit_irq_timer_update(s, s->next_transition_time);
392 static void pit_save(QEMUFile *f, void *opaque)
394 PITState *pit = opaque;
398 for(i = 0; i < 3; i++) {
399 s = &pit->channels[i];
400 qemu_put_be32(f, s->count);
401 qemu_put_be16s(f, &s->latched_count);
402 qemu_put_8s(f, &s->count_latched);
403 qemu_put_8s(f, &s->status_latched);
404 qemu_put_8s(f, &s->status);
405 qemu_put_8s(f, &s->read_state);
406 qemu_put_8s(f, &s->write_state);
407 qemu_put_8s(f, &s->write_latch);
408 qemu_put_8s(f, &s->rw_mode);
409 qemu_put_8s(f, &s->mode);
410 qemu_put_8s(f, &s->bcd);
411 qemu_put_8s(f, &s->gate);
412 qemu_put_be64(f, s->count_load_time);
414 qemu_put_be64(f, s->next_transition_time);
415 qemu_put_timer(f, s->irq_timer);
420 static int pit_load(QEMUFile *f, void *opaque, int version_id)
422 PITState *pit = opaque;
429 for(i = 0; i < 3; i++) {
430 s = &pit->channels[i];
431 s->count=qemu_get_be32(f);
432 qemu_get_be16s(f, &s->latched_count);
433 qemu_get_8s(f, &s->count_latched);
434 qemu_get_8s(f, &s->status_latched);
435 qemu_get_8s(f, &s->status);
436 qemu_get_8s(f, &s->read_state);
437 qemu_get_8s(f, &s->write_state);
438 qemu_get_8s(f, &s->write_latch);
439 qemu_get_8s(f, &s->rw_mode);
440 qemu_get_8s(f, &s->mode);
441 qemu_get_8s(f, &s->bcd);
442 qemu_get_8s(f, &s->gate);
443 s->count_load_time=qemu_get_be64(f);
445 s->next_transition_time=qemu_get_be64(f);
446 qemu_get_timer(f, s->irq_timer);
452 static void pit_reset(void *opaque)
454 PITState *pit = opaque;
458 for(i = 0;i < 3; i++) {
459 s = &pit->channels[i];
462 pit_load_count(s, 0);
466 PITState *pit_init(int base, qemu_irq irq)
468 PITState *pit = &pit_state;
471 s = &pit->channels[0];
472 /* the timer 0 is connected to an IRQ */
473 s->irq_timer = qemu_new_timer(vm_clock, pit_irq_timer, s);
476 register_savevm("i8254", base, 1, pit_save, pit_load, pit);
478 qemu_register_reset(pit_reset, pit);
479 register_ioport_write(base, 4, 1, pit_ioport_write, pit);
480 register_ioport_read(base, 3, 1, pit_ioport_read, pit);