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
28 #define RW_STATE_LSB 1
29 #define RW_STATE_MSB 2
30 #define RW_STATE_WORD0 3
31 #define RW_STATE_WORD1 4
33 typedef struct PITChannelState
{
34 int count
; /* can be 65536 */
35 uint16_t latched_count
;
36 uint8_t count_latched
;
37 uint8_t status_latched
;
44 uint8_t bcd
; /* not supported */
45 uint8_t gate
; /* timer start */
46 int64_t count_load_time
;
48 int64_t next_transition_time
;
54 PITChannelState channels
[3];
57 static PITState pit_state
;
59 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
);
61 static int pit_get_count(PITChannelState
*s
)
66 d
= muldiv64(qemu_get_clock(vm_clock
) - s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
72 counter
= (s
->count
- d
) & 0xffff;
75 /* XXX: may be incorrect for odd counts */
76 counter
= s
->count
- ((2 * d
) % s
->count
);
79 counter
= s
->count
- (d
% s
->count
);
85 /* get pit output bit */
86 static int pit_get_out1(PITChannelState
*s
, int64_t current_time
)
91 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
95 out
= (d
>= s
->count
);
101 if ((d
% s
->count
) == 0 && d
!= 0)
107 out
= (d
% s
->count
) < ((s
->count
+ 1) >> 1);
111 out
= (d
== s
->count
);
117 int pit_get_out(PITState
*pit
, int channel
, int64_t current_time
)
119 PITChannelState
*s
= &pit
->channels
[channel
];
120 return pit_get_out1(s
, current_time
);
123 /* return -1 if no transition will occur. */
124 static int64_t pit_get_next_transition_time(PITChannelState
*s
,
125 int64_t current_time
)
127 uint64_t d
, next_time
, base
;
130 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
136 next_time
= s
->count
;
141 base
= (d
/ s
->count
) * s
->count
;
142 if ((d
- base
) == 0 && d
!= 0)
143 next_time
= base
+ s
->count
;
145 next_time
= base
+ s
->count
+ 1;
148 base
= (d
/ s
->count
) * s
->count
;
149 period2
= ((s
->count
+ 1) >> 1);
150 if ((d
- base
) < period2
)
151 next_time
= base
+ period2
;
153 next_time
= base
+ s
->count
;
158 next_time
= s
->count
;
159 else if (d
== s
->count
)
160 next_time
= s
->count
+ 1;
165 /* convert to timer units */
166 next_time
= s
->count_load_time
+ muldiv64(next_time
, ticks_per_sec
, PIT_FREQ
);
167 /* fix potential rounding problems */
168 /* XXX: better solution: use a clock at PIT_FREQ Hz */
169 if (next_time
<= current_time
)
170 next_time
= current_time
+ 1;
174 /* val must be 0 or 1 */
175 void pit_set_gate(PITState
*pit
, int channel
, int val
)
177 PITChannelState
*s
= &pit
->channels
[channel
];
183 /* XXX: just disable/enable counting */
188 /* restart counting on rising edge */
189 s
->count_load_time
= qemu_get_clock(vm_clock
);
190 pit_irq_timer_update(s
, s
->count_load_time
);
196 /* restart counting on rising edge */
197 s
->count_load_time
= qemu_get_clock(vm_clock
);
198 pit_irq_timer_update(s
, s
->count_load_time
);
200 /* XXX: disable/enable counting */
206 int pit_get_gate(PITState
*pit
, int channel
)
208 PITChannelState
*s
= &pit
->channels
[channel
];
212 static inline void pit_load_count(PITChannelState
*s
, int val
)
216 s
->count_load_time
= qemu_get_clock(vm_clock
);
218 pit_irq_timer_update(s
, s
->count_load_time
);
221 /* if already latched, do not latch again */
222 static void pit_latch_count(PITChannelState
*s
)
224 if (!s
->count_latched
) {
225 s
->latched_count
= pit_get_count(s
);
226 s
->count_latched
= s
->rw_mode
;
230 static void pit_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
232 PITState
*pit
= opaque
;
240 /* read back command */
241 for(channel
= 0; channel
< 3; channel
++) {
242 s
= &pit
->channels
[channel
];
243 if (val
& (2 << channel
)) {
247 if (!(val
& 0x10) && !s
->status_latched
) {
249 /* XXX: add BCD and null count */
250 s
->status
= (pit_get_out1(s
, qemu_get_clock(vm_clock
)) << 7) |
254 s
->status_latched
= 1;
259 s
= &pit
->channels
[channel
];
260 access
= (val
>> 4) & 3;
265 s
->read_state
= access
;
266 s
->write_state
= access
;
268 s
->mode
= (val
>> 1) & 7;
270 /* XXX: update irq timer ? */
274 s
= &pit
->channels
[addr
];
275 switch(s
->write_state
) {
278 pit_load_count(s
, val
);
281 pit_load_count(s
, val
<< 8);
284 s
->write_latch
= val
;
285 s
->write_state
= RW_STATE_WORD1
;
288 pit_load_count(s
, s
->write_latch
| (val
<< 8));
289 s
->write_state
= RW_STATE_WORD0
;
295 static uint32_t pit_ioport_read(void *opaque
, uint32_t addr
)
297 PITState
*pit
= opaque
;
302 s
= &pit
->channels
[addr
];
303 if (s
->status_latched
) {
304 s
->status_latched
= 0;
306 } else if (s
->count_latched
) {
307 switch(s
->count_latched
) {
310 ret
= s
->latched_count
& 0xff;
311 s
->count_latched
= 0;
314 ret
= s
->latched_count
>> 8;
315 s
->count_latched
= 0;
318 ret
= s
->latched_count
& 0xff;
319 s
->count_latched
= RW_STATE_MSB
;
323 switch(s
->read_state
) {
326 count
= pit_get_count(s
);
330 count
= pit_get_count(s
);
331 ret
= (count
>> 8) & 0xff;
334 count
= pit_get_count(s
);
336 s
->read_state
= RW_STATE_WORD1
;
339 count
= pit_get_count(s
);
340 ret
= (count
>> 8) & 0xff;
341 s
->read_state
= RW_STATE_WORD0
;
348 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
)
355 expire_time
= pit_get_next_transition_time(s
, current_time
);
356 irq_level
= pit_get_out1(s
, current_time
);
357 pic_set_irq(s
->irq
, irq_level
);
359 printf("irq_level=%d next_delay=%f\n",
361 (double)(expire_time
- current_time
) / ticks_per_sec
);
363 s
->next_transition_time
= expire_time
;
364 if (expire_time
!= -1)
365 qemu_mod_timer(s
->irq_timer
, expire_time
);
367 qemu_del_timer(s
->irq_timer
);
370 static void pit_irq_timer(void *opaque
)
372 PITChannelState
*s
= opaque
;
374 pit_irq_timer_update(s
, s
->next_transition_time
);
377 static void pit_save(QEMUFile
*f
, void *opaque
)
379 PITState
*pit
= opaque
;
383 for(i
= 0; i
< 3; i
++) {
384 s
= &pit
->channels
[i
];
385 qemu_put_be32s(f
, &s
->count
);
386 qemu_put_be16s(f
, &s
->latched_count
);
387 qemu_put_8s(f
, &s
->count_latched
);
388 qemu_put_8s(f
, &s
->status_latched
);
389 qemu_put_8s(f
, &s
->status
);
390 qemu_put_8s(f
, &s
->read_state
);
391 qemu_put_8s(f
, &s
->write_state
);
392 qemu_put_8s(f
, &s
->write_latch
);
393 qemu_put_8s(f
, &s
->rw_mode
);
394 qemu_put_8s(f
, &s
->mode
);
395 qemu_put_8s(f
, &s
->bcd
);
396 qemu_put_8s(f
, &s
->gate
);
397 qemu_put_be64s(f
, &s
->count_load_time
);
399 qemu_put_be64s(f
, &s
->next_transition_time
);
400 qemu_put_timer(f
, s
->irq_timer
);
405 static int pit_load(QEMUFile
*f
, void *opaque
, int version_id
)
407 PITState
*pit
= opaque
;
414 for(i
= 0; i
< 3; i
++) {
415 s
= &pit
->channels
[i
];
416 qemu_get_be32s(f
, &s
->count
);
417 qemu_get_be16s(f
, &s
->latched_count
);
418 qemu_get_8s(f
, &s
->count_latched
);
419 qemu_get_8s(f
, &s
->status_latched
);
420 qemu_get_8s(f
, &s
->status
);
421 qemu_get_8s(f
, &s
->read_state
);
422 qemu_get_8s(f
, &s
->write_state
);
423 qemu_get_8s(f
, &s
->write_latch
);
424 qemu_get_8s(f
, &s
->rw_mode
);
425 qemu_get_8s(f
, &s
->mode
);
426 qemu_get_8s(f
, &s
->bcd
);
427 qemu_get_8s(f
, &s
->gate
);
428 qemu_get_be64s(f
, &s
->count_load_time
);
430 qemu_get_be64s(f
, &s
->next_transition_time
);
431 qemu_get_timer(f
, s
->irq_timer
);
437 static void pit_reset(void *opaque
)
439 PITState
*pit
= opaque
;
443 for(i
= 0;i
< 3; i
++) {
444 s
= &pit
->channels
[i
];
447 pit_load_count(s
, 0);
451 PITState
*pit_init(int base
, int irq
)
453 PITState
*pit
= &pit_state
;
456 s
= &pit
->channels
[0];
457 /* the timer 0 is connected to an IRQ */
458 s
->irq_timer
= qemu_new_timer(vm_clock
, pit_irq_timer
, s
);
461 register_savevm("i8254", base
, 1, pit_save
, pit_load
, pit
);
463 qemu_register_reset(pit_reset
, pit
);
464 register_ioport_write(base
, 4, 1, pit_ioport_write
, pit
);
465 register_ioport_read(base
, 3, 1, pit_ioport_read
, pit
);