root / hw / i8254.c @ 8f40c388
History | View | Annotate | Download (12.8 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU 8253/8254 interval timer emulation
|
3 | 80cabfad | bellard | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 80cabfad | bellard | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 80cabfad | bellard | #include "vl.h" |
25 | 80cabfad | bellard | |
26 | b0a21b53 | bellard | //#define DEBUG_PIT
|
27 | b0a21b53 | bellard | |
28 | ec844b96 | bellard | #define RW_STATE_LSB 1 |
29 | ec844b96 | bellard | #define RW_STATE_MSB 2 |
30 | ec844b96 | bellard | #define RW_STATE_WORD0 3 |
31 | ec844b96 | bellard | #define RW_STATE_WORD1 4 |
32 | 80cabfad | bellard | |
33 | ec844b96 | bellard | typedef struct PITChannelState { |
34 | ec844b96 | bellard | int count; /* can be 65536 */ |
35 | ec844b96 | bellard | uint16_t latched_count; |
36 | ec844b96 | bellard | uint8_t count_latched; |
37 | ec844b96 | bellard | uint8_t status_latched; |
38 | ec844b96 | bellard | uint8_t status; |
39 | ec844b96 | bellard | uint8_t read_state; |
40 | ec844b96 | bellard | uint8_t write_state; |
41 | ec844b96 | bellard | uint8_t write_latch; |
42 | ec844b96 | bellard | uint8_t rw_mode; |
43 | ec844b96 | bellard | uint8_t mode; |
44 | ec844b96 | bellard | uint8_t bcd; /* not supported */
|
45 | ec844b96 | bellard | uint8_t gate; /* timer start */
|
46 | ec844b96 | bellard | int64_t count_load_time; |
47 | ec844b96 | bellard | /* irq handling */
|
48 | ec844b96 | bellard | int64_t next_transition_time; |
49 | ec844b96 | bellard | QEMUTimer *irq_timer; |
50 | ec844b96 | bellard | int irq;
|
51 | ec844b96 | bellard | } PITChannelState; |
52 | ec844b96 | bellard | |
53 | ec844b96 | bellard | struct PITState {
|
54 | ec844b96 | bellard | PITChannelState channels[3];
|
55 | ec844b96 | bellard | }; |
56 | ec844b96 | bellard | |
57 | ec844b96 | bellard | static PITState pit_state;
|
58 | 80cabfad | bellard | |
59 | b0a21b53 | bellard | static void pit_irq_timer_update(PITChannelState *s, int64_t current_time); |
60 | b0a21b53 | bellard | |
61 | 80cabfad | bellard | static int pit_get_count(PITChannelState *s) |
62 | 80cabfad | bellard | { |
63 | 80cabfad | bellard | uint64_t d; |
64 | 80cabfad | bellard | int counter;
|
65 | 80cabfad | bellard | |
66 | b0a21b53 | bellard | d = muldiv64(qemu_get_clock(vm_clock) - s->count_load_time, PIT_FREQ, ticks_per_sec); |
67 | 80cabfad | bellard | switch(s->mode) {
|
68 | 80cabfad | bellard | case 0: |
69 | 80cabfad | bellard | case 1: |
70 | 80cabfad | bellard | case 4: |
71 | 80cabfad | bellard | case 5: |
72 | 80cabfad | bellard | counter = (s->count - d) & 0xffff;
|
73 | 80cabfad | bellard | break;
|
74 | 80cabfad | bellard | case 3: |
75 | 80cabfad | bellard | /* XXX: may be incorrect for odd counts */
|
76 | 80cabfad | bellard | counter = s->count - ((2 * d) % s->count);
|
77 | 80cabfad | bellard | break;
|
78 | 80cabfad | bellard | default:
|
79 | 80cabfad | bellard | counter = s->count - (d % s->count); |
80 | 80cabfad | bellard | break;
|
81 | 80cabfad | bellard | } |
82 | 80cabfad | bellard | return counter;
|
83 | 80cabfad | bellard | } |
84 | 80cabfad | bellard | |
85 | 80cabfad | bellard | /* get pit output bit */
|
86 | ec844b96 | bellard | static int pit_get_out1(PITChannelState *s, int64_t current_time) |
87 | 80cabfad | bellard | { |
88 | 80cabfad | bellard | uint64_t d; |
89 | 80cabfad | bellard | int out;
|
90 | 80cabfad | bellard | |
91 | b0a21b53 | bellard | d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec); |
92 | 80cabfad | bellard | switch(s->mode) {
|
93 | 80cabfad | bellard | default:
|
94 | 80cabfad | bellard | case 0: |
95 | 80cabfad | bellard | out = (d >= s->count); |
96 | 80cabfad | bellard | break;
|
97 | 80cabfad | bellard | case 1: |
98 | 80cabfad | bellard | out = (d < s->count); |
99 | 80cabfad | bellard | break;
|
100 | 80cabfad | bellard | case 2: |
101 | 80cabfad | bellard | if ((d % s->count) == 0 && d != 0) |
102 | 80cabfad | bellard | out = 1;
|
103 | 80cabfad | bellard | else
|
104 | 80cabfad | bellard | out = 0;
|
105 | 80cabfad | bellard | break;
|
106 | 80cabfad | bellard | case 3: |
107 | 80cabfad | bellard | out = (d % s->count) < ((s->count + 1) >> 1); |
108 | 80cabfad | bellard | break;
|
109 | 80cabfad | bellard | case 4: |
110 | 80cabfad | bellard | case 5: |
111 | 80cabfad | bellard | out = (d == s->count); |
112 | 80cabfad | bellard | break;
|
113 | 80cabfad | bellard | } |
114 | 80cabfad | bellard | return out;
|
115 | 80cabfad | bellard | } |
116 | 80cabfad | bellard | |
117 | ec844b96 | bellard | int pit_get_out(PITState *pit, int channel, int64_t current_time) |
118 | ec844b96 | bellard | { |
119 | ec844b96 | bellard | PITChannelState *s = &pit->channels[channel]; |
120 | ec844b96 | bellard | return pit_get_out1(s, current_time);
|
121 | ec844b96 | bellard | } |
122 | ec844b96 | bellard | |
123 | b0a21b53 | bellard | /* return -1 if no transition will occur. */
|
124 | b0a21b53 | bellard | static int64_t pit_get_next_transition_time(PITChannelState *s,
|
125 | b0a21b53 | bellard | int64_t current_time) |
126 | 80cabfad | bellard | { |
127 | b0a21b53 | bellard | uint64_t d, next_time, base; |
128 | b0a21b53 | bellard | int period2;
|
129 | 80cabfad | bellard | |
130 | b0a21b53 | bellard | d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec); |
131 | 80cabfad | bellard | switch(s->mode) {
|
132 | 80cabfad | bellard | default:
|
133 | 80cabfad | bellard | case 0: |
134 | 80cabfad | bellard | case 1: |
135 | b0a21b53 | bellard | if (d < s->count)
|
136 | b0a21b53 | bellard | next_time = s->count; |
137 | b0a21b53 | bellard | else
|
138 | b0a21b53 | bellard | return -1; |
139 | 80cabfad | bellard | break;
|
140 | 80cabfad | bellard | case 2: |
141 | b0a21b53 | bellard | base = (d / s->count) * s->count; |
142 | b0a21b53 | bellard | if ((d - base) == 0 && d != 0) |
143 | b0a21b53 | bellard | next_time = base + s->count; |
144 | b0a21b53 | bellard | else
|
145 | b0a21b53 | bellard | next_time = base + s->count + 1;
|
146 | 80cabfad | bellard | break;
|
147 | 80cabfad | bellard | case 3: |
148 | b0a21b53 | bellard | base = (d / s->count) * s->count; |
149 | b0a21b53 | bellard | period2 = ((s->count + 1) >> 1); |
150 | b0a21b53 | bellard | if ((d - base) < period2)
|
151 | b0a21b53 | bellard | next_time = base + period2; |
152 | b0a21b53 | bellard | else
|
153 | b0a21b53 | bellard | next_time = base + s->count; |
154 | 80cabfad | bellard | break;
|
155 | 80cabfad | bellard | case 4: |
156 | 80cabfad | bellard | case 5: |
157 | b0a21b53 | bellard | if (d < s->count)
|
158 | b0a21b53 | bellard | next_time = s->count; |
159 | b0a21b53 | bellard | else if (d == s->count) |
160 | b0a21b53 | bellard | next_time = s->count + 1;
|
161 | 80cabfad | bellard | else
|
162 | b0a21b53 | bellard | return -1; |
163 | 80cabfad | bellard | break;
|
164 | 80cabfad | bellard | } |
165 | b0a21b53 | bellard | /* convert to timer units */
|
166 | b0a21b53 | bellard | next_time = s->count_load_time + muldiv64(next_time, ticks_per_sec, PIT_FREQ); |
167 | 1154e441 | bellard | /* fix potential rounding problems */
|
168 | 1154e441 | bellard | /* XXX: better solution: use a clock at PIT_FREQ Hz */
|
169 | 1154e441 | bellard | if (next_time <= current_time)
|
170 | 1154e441 | bellard | next_time = current_time + 1;
|
171 | b0a21b53 | bellard | return next_time;
|
172 | 80cabfad | bellard | } |
173 | 80cabfad | bellard | |
174 | 80cabfad | bellard | /* val must be 0 or 1 */
|
175 | ec844b96 | bellard | void pit_set_gate(PITState *pit, int channel, int val) |
176 | 80cabfad | bellard | { |
177 | ec844b96 | bellard | PITChannelState *s = &pit->channels[channel]; |
178 | ec844b96 | bellard | |
179 | 80cabfad | bellard | switch(s->mode) {
|
180 | 80cabfad | bellard | default:
|
181 | 80cabfad | bellard | case 0: |
182 | 80cabfad | bellard | case 4: |
183 | 80cabfad | bellard | /* XXX: just disable/enable counting */
|
184 | 80cabfad | bellard | break;
|
185 | 80cabfad | bellard | case 1: |
186 | 80cabfad | bellard | case 5: |
187 | 80cabfad | bellard | if (s->gate < val) {
|
188 | 80cabfad | bellard | /* restart counting on rising edge */
|
189 | b0a21b53 | bellard | s->count_load_time = qemu_get_clock(vm_clock); |
190 | b0a21b53 | bellard | pit_irq_timer_update(s, s->count_load_time); |
191 | 80cabfad | bellard | } |
192 | 80cabfad | bellard | break;
|
193 | 80cabfad | bellard | case 2: |
194 | 80cabfad | bellard | case 3: |
195 | 80cabfad | bellard | if (s->gate < val) {
|
196 | 80cabfad | bellard | /* restart counting on rising edge */
|
197 | b0a21b53 | bellard | s->count_load_time = qemu_get_clock(vm_clock); |
198 | b0a21b53 | bellard | pit_irq_timer_update(s, s->count_load_time); |
199 | 80cabfad | bellard | } |
200 | 80cabfad | bellard | /* XXX: disable/enable counting */
|
201 | 80cabfad | bellard | break;
|
202 | 80cabfad | bellard | } |
203 | 80cabfad | bellard | s->gate = val; |
204 | 80cabfad | bellard | } |
205 | 80cabfad | bellard | |
206 | ec844b96 | bellard | int pit_get_gate(PITState *pit, int channel) |
207 | ec844b96 | bellard | { |
208 | ec844b96 | bellard | PITChannelState *s = &pit->channels[channel]; |
209 | ec844b96 | bellard | return s->gate;
|
210 | ec844b96 | bellard | } |
211 | ec844b96 | bellard | |
212 | fd06c375 | bellard | int pit_get_initial_count(PITState *pit, int channel) |
213 | fd06c375 | bellard | { |
214 | fd06c375 | bellard | PITChannelState *s = &pit->channels[channel]; |
215 | fd06c375 | bellard | return s->count;
|
216 | fd06c375 | bellard | } |
217 | fd06c375 | bellard | |
218 | fd06c375 | bellard | int pit_get_mode(PITState *pit, int channel) |
219 | fd06c375 | bellard | { |
220 | fd06c375 | bellard | PITChannelState *s = &pit->channels[channel]; |
221 | fd06c375 | bellard | return s->mode;
|
222 | fd06c375 | bellard | } |
223 | fd06c375 | bellard | |
224 | 80cabfad | bellard | static inline void pit_load_count(PITChannelState *s, int val) |
225 | 80cabfad | bellard | { |
226 | 80cabfad | bellard | if (val == 0) |
227 | 80cabfad | bellard | val = 0x10000;
|
228 | b0a21b53 | bellard | s->count_load_time = qemu_get_clock(vm_clock); |
229 | 80cabfad | bellard | s->count = val; |
230 | b0a21b53 | bellard | pit_irq_timer_update(s, s->count_load_time); |
231 | 80cabfad | bellard | } |
232 | 80cabfad | bellard | |
233 | ec844b96 | bellard | /* if already latched, do not latch again */
|
234 | ec844b96 | bellard | static void pit_latch_count(PITChannelState *s) |
235 | ec844b96 | bellard | { |
236 | ec844b96 | bellard | if (!s->count_latched) {
|
237 | ec844b96 | bellard | s->latched_count = pit_get_count(s); |
238 | ec844b96 | bellard | s->count_latched = s->rw_mode; |
239 | ec844b96 | bellard | } |
240 | ec844b96 | bellard | } |
241 | ec844b96 | bellard | |
242 | b41a2cd1 | bellard | static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
243 | 80cabfad | bellard | { |
244 | ec844b96 | bellard | PITState *pit = opaque; |
245 | 80cabfad | bellard | int channel, access;
|
246 | 80cabfad | bellard | PITChannelState *s; |
247 | 80cabfad | bellard | |
248 | 80cabfad | bellard | addr &= 3;
|
249 | 80cabfad | bellard | if (addr == 3) { |
250 | 80cabfad | bellard | channel = val >> 6;
|
251 | ec844b96 | bellard | if (channel == 3) { |
252 | ec844b96 | bellard | /* read back command */
|
253 | ec844b96 | bellard | for(channel = 0; channel < 3; channel++) { |
254 | ec844b96 | bellard | s = &pit->channels[channel]; |
255 | ec844b96 | bellard | if (val & (2 << channel)) { |
256 | ec844b96 | bellard | if (!(val & 0x20)) { |
257 | ec844b96 | bellard | pit_latch_count(s); |
258 | ec844b96 | bellard | } |
259 | ec844b96 | bellard | if (!(val & 0x10) && !s->status_latched) { |
260 | ec844b96 | bellard | /* status latch */
|
261 | ec844b96 | bellard | /* XXX: add BCD and null count */
|
262 | ec844b96 | bellard | s->status = (pit_get_out1(s, qemu_get_clock(vm_clock)) << 7) |
|
263 | ec844b96 | bellard | (s->rw_mode << 4) |
|
264 | ec844b96 | bellard | (s->mode << 1) |
|
265 | ec844b96 | bellard | s->bcd; |
266 | ec844b96 | bellard | s->status_latched = 1;
|
267 | ec844b96 | bellard | } |
268 | ec844b96 | bellard | } |
269 | ec844b96 | bellard | } |
270 | ec844b96 | bellard | } else {
|
271 | ec844b96 | bellard | s = &pit->channels[channel]; |
272 | ec844b96 | bellard | access = (val >> 4) & 3; |
273 | ec844b96 | bellard | if (access == 0) { |
274 | ec844b96 | bellard | pit_latch_count(s); |
275 | ec844b96 | bellard | } else {
|
276 | ec844b96 | bellard | s->rw_mode = access; |
277 | ec844b96 | bellard | s->read_state = access; |
278 | ec844b96 | bellard | s->write_state = access; |
279 | ec844b96 | bellard | |
280 | ec844b96 | bellard | s->mode = (val >> 1) & 7; |
281 | ec844b96 | bellard | s->bcd = val & 1;
|
282 | ec844b96 | bellard | /* XXX: update irq timer ? */
|
283 | ec844b96 | bellard | } |
284 | 80cabfad | bellard | } |
285 | 80cabfad | bellard | } else {
|
286 | ec844b96 | bellard | s = &pit->channels[addr]; |
287 | ec844b96 | bellard | switch(s->write_state) {
|
288 | ec844b96 | bellard | default:
|
289 | 80cabfad | bellard | case RW_STATE_LSB:
|
290 | 80cabfad | bellard | pit_load_count(s, val); |
291 | 80cabfad | bellard | break;
|
292 | 80cabfad | bellard | case RW_STATE_MSB:
|
293 | 80cabfad | bellard | pit_load_count(s, val << 8);
|
294 | 80cabfad | bellard | break;
|
295 | 80cabfad | bellard | case RW_STATE_WORD0:
|
296 | ec844b96 | bellard | s->write_latch = val; |
297 | ec844b96 | bellard | s->write_state = RW_STATE_WORD1; |
298 | ec844b96 | bellard | break;
|
299 | 80cabfad | bellard | case RW_STATE_WORD1:
|
300 | ec844b96 | bellard | pit_load_count(s, s->write_latch | (val << 8));
|
301 | ec844b96 | bellard | s->write_state = RW_STATE_WORD0; |
302 | 80cabfad | bellard | break;
|
303 | 80cabfad | bellard | } |
304 | 80cabfad | bellard | } |
305 | 80cabfad | bellard | } |
306 | 80cabfad | bellard | |
307 | b41a2cd1 | bellard | static uint32_t pit_ioport_read(void *opaque, uint32_t addr) |
308 | 80cabfad | bellard | { |
309 | ec844b96 | bellard | PITState *pit = opaque; |
310 | 80cabfad | bellard | int ret, count;
|
311 | 80cabfad | bellard | PITChannelState *s; |
312 | 80cabfad | bellard | |
313 | 80cabfad | bellard | addr &= 3;
|
314 | ec844b96 | bellard | s = &pit->channels[addr]; |
315 | ec844b96 | bellard | if (s->status_latched) {
|
316 | ec844b96 | bellard | s->status_latched = 0;
|
317 | ec844b96 | bellard | ret = s->status; |
318 | ec844b96 | bellard | } else if (s->count_latched) { |
319 | ec844b96 | bellard | switch(s->count_latched) {
|
320 | ec844b96 | bellard | default:
|
321 | ec844b96 | bellard | case RW_STATE_LSB:
|
322 | ec844b96 | bellard | ret = s->latched_count & 0xff;
|
323 | ec844b96 | bellard | s->count_latched = 0;
|
324 | ec844b96 | bellard | break;
|
325 | ec844b96 | bellard | case RW_STATE_MSB:
|
326 | 80cabfad | bellard | ret = s->latched_count >> 8;
|
327 | ec844b96 | bellard | s->count_latched = 0;
|
328 | ec844b96 | bellard | break;
|
329 | ec844b96 | bellard | case RW_STATE_WORD0:
|
330 | 80cabfad | bellard | ret = s->latched_count & 0xff;
|
331 | ec844b96 | bellard | s->count_latched = RW_STATE_MSB; |
332 | ec844b96 | bellard | break;
|
333 | ec844b96 | bellard | } |
334 | ec844b96 | bellard | } else {
|
335 | ec844b96 | bellard | switch(s->read_state) {
|
336 | ec844b96 | bellard | default:
|
337 | ec844b96 | bellard | case RW_STATE_LSB:
|
338 | ec844b96 | bellard | count = pit_get_count(s); |
339 | ec844b96 | bellard | ret = count & 0xff;
|
340 | ec844b96 | bellard | break;
|
341 | ec844b96 | bellard | case RW_STATE_MSB:
|
342 | ec844b96 | bellard | count = pit_get_count(s); |
343 | ec844b96 | bellard | ret = (count >> 8) & 0xff; |
344 | ec844b96 | bellard | break;
|
345 | ec844b96 | bellard | case RW_STATE_WORD0:
|
346 | ec844b96 | bellard | count = pit_get_count(s); |
347 | ec844b96 | bellard | ret = count & 0xff;
|
348 | ec844b96 | bellard | s->read_state = RW_STATE_WORD1; |
349 | ec844b96 | bellard | break;
|
350 | ec844b96 | bellard | case RW_STATE_WORD1:
|
351 | ec844b96 | bellard | count = pit_get_count(s); |
352 | ec844b96 | bellard | ret = (count >> 8) & 0xff; |
353 | ec844b96 | bellard | s->read_state = RW_STATE_WORD0; |
354 | ec844b96 | bellard | break;
|
355 | ec844b96 | bellard | } |
356 | 80cabfad | bellard | } |
357 | 80cabfad | bellard | return ret;
|
358 | 80cabfad | bellard | } |
359 | 80cabfad | bellard | |
360 | b0a21b53 | bellard | static void pit_irq_timer_update(PITChannelState *s, int64_t current_time) |
361 | b0a21b53 | bellard | { |
362 | b0a21b53 | bellard | int64_t expire_time; |
363 | b0a21b53 | bellard | int irq_level;
|
364 | b0a21b53 | bellard | |
365 | b0a21b53 | bellard | if (!s->irq_timer)
|
366 | b0a21b53 | bellard | return;
|
367 | b0a21b53 | bellard | expire_time = pit_get_next_transition_time(s, current_time); |
368 | ec844b96 | bellard | irq_level = pit_get_out1(s, current_time); |
369 | b0a21b53 | bellard | pic_set_irq(s->irq, irq_level); |
370 | b0a21b53 | bellard | #ifdef DEBUG_PIT
|
371 | b0a21b53 | bellard | printf("irq_level=%d next_delay=%f\n",
|
372 | b0a21b53 | bellard | irq_level, |
373 | b0a21b53 | bellard | (double)(expire_time - current_time) / ticks_per_sec);
|
374 | b0a21b53 | bellard | #endif
|
375 | b0a21b53 | bellard | s->next_transition_time = expire_time; |
376 | b0a21b53 | bellard | if (expire_time != -1) |
377 | b0a21b53 | bellard | qemu_mod_timer(s->irq_timer, expire_time); |
378 | b0a21b53 | bellard | else
|
379 | b0a21b53 | bellard | qemu_del_timer(s->irq_timer); |
380 | b0a21b53 | bellard | } |
381 | b0a21b53 | bellard | |
382 | b0a21b53 | bellard | static void pit_irq_timer(void *opaque) |
383 | b0a21b53 | bellard | { |
384 | b0a21b53 | bellard | PITChannelState *s = opaque; |
385 | b0a21b53 | bellard | |
386 | b0a21b53 | bellard | pit_irq_timer_update(s, s->next_transition_time); |
387 | b0a21b53 | bellard | } |
388 | b0a21b53 | bellard | |
389 | b0a21b53 | bellard | static void pit_save(QEMUFile *f, void *opaque) |
390 | b0a21b53 | bellard | { |
391 | ec844b96 | bellard | PITState *pit = opaque; |
392 | b0a21b53 | bellard | PITChannelState *s; |
393 | b0a21b53 | bellard | int i;
|
394 | b0a21b53 | bellard | |
395 | b0a21b53 | bellard | for(i = 0; i < 3; i++) { |
396 | ec844b96 | bellard | s = &pit->channels[i]; |
397 | b0a21b53 | bellard | qemu_put_be32s(f, &s->count); |
398 | b0a21b53 | bellard | qemu_put_be16s(f, &s->latched_count); |
399 | ec844b96 | bellard | qemu_put_8s(f, &s->count_latched); |
400 | ec844b96 | bellard | qemu_put_8s(f, &s->status_latched); |
401 | ec844b96 | bellard | qemu_put_8s(f, &s->status); |
402 | ec844b96 | bellard | qemu_put_8s(f, &s->read_state); |
403 | ec844b96 | bellard | qemu_put_8s(f, &s->write_state); |
404 | ec844b96 | bellard | qemu_put_8s(f, &s->write_latch); |
405 | ec844b96 | bellard | qemu_put_8s(f, &s->rw_mode); |
406 | b0a21b53 | bellard | qemu_put_8s(f, &s->mode); |
407 | b0a21b53 | bellard | qemu_put_8s(f, &s->bcd); |
408 | b0a21b53 | bellard | qemu_put_8s(f, &s->gate); |
409 | b0a21b53 | bellard | qemu_put_be64s(f, &s->count_load_time); |
410 | b0a21b53 | bellard | if (s->irq_timer) {
|
411 | b0a21b53 | bellard | qemu_put_be64s(f, &s->next_transition_time); |
412 | b0a21b53 | bellard | qemu_put_timer(f, s->irq_timer); |
413 | b0a21b53 | bellard | } |
414 | b0a21b53 | bellard | } |
415 | b0a21b53 | bellard | } |
416 | b0a21b53 | bellard | |
417 | b0a21b53 | bellard | static int pit_load(QEMUFile *f, void *opaque, int version_id) |
418 | b0a21b53 | bellard | { |
419 | ec844b96 | bellard | PITState *pit = opaque; |
420 | b0a21b53 | bellard | PITChannelState *s; |
421 | b0a21b53 | bellard | int i;
|
422 | b0a21b53 | bellard | |
423 | b0a21b53 | bellard | if (version_id != 1) |
424 | b0a21b53 | bellard | return -EINVAL;
|
425 | b0a21b53 | bellard | |
426 | b0a21b53 | bellard | for(i = 0; i < 3; i++) { |
427 | ec844b96 | bellard | s = &pit->channels[i]; |
428 | b0a21b53 | bellard | qemu_get_be32s(f, &s->count); |
429 | b0a21b53 | bellard | qemu_get_be16s(f, &s->latched_count); |
430 | ec844b96 | bellard | qemu_get_8s(f, &s->count_latched); |
431 | ec844b96 | bellard | qemu_get_8s(f, &s->status_latched); |
432 | ec844b96 | bellard | qemu_get_8s(f, &s->status); |
433 | ec844b96 | bellard | qemu_get_8s(f, &s->read_state); |
434 | ec844b96 | bellard | qemu_get_8s(f, &s->write_state); |
435 | ec844b96 | bellard | qemu_get_8s(f, &s->write_latch); |
436 | ec844b96 | bellard | qemu_get_8s(f, &s->rw_mode); |
437 | b0a21b53 | bellard | qemu_get_8s(f, &s->mode); |
438 | b0a21b53 | bellard | qemu_get_8s(f, &s->bcd); |
439 | b0a21b53 | bellard | qemu_get_8s(f, &s->gate); |
440 | b0a21b53 | bellard | qemu_get_be64s(f, &s->count_load_time); |
441 | b0a21b53 | bellard | if (s->irq_timer) {
|
442 | b0a21b53 | bellard | qemu_get_be64s(f, &s->next_transition_time); |
443 | b0a21b53 | bellard | qemu_get_timer(f, s->irq_timer); |
444 | b0a21b53 | bellard | } |
445 | b0a21b53 | bellard | } |
446 | b0a21b53 | bellard | return 0; |
447 | b0a21b53 | bellard | } |
448 | b0a21b53 | bellard | |
449 | d7d02e3c | bellard | static void pit_reset(void *opaque) |
450 | 80cabfad | bellard | { |
451 | d7d02e3c | bellard | PITState *pit = opaque; |
452 | 80cabfad | bellard | PITChannelState *s; |
453 | 80cabfad | bellard | int i;
|
454 | 80cabfad | bellard | |
455 | 80cabfad | bellard | for(i = 0;i < 3; i++) { |
456 | ec844b96 | bellard | s = &pit->channels[i]; |
457 | 80cabfad | bellard | s->mode = 3;
|
458 | 80cabfad | bellard | s->gate = (i != 2);
|
459 | 80cabfad | bellard | pit_load_count(s, 0);
|
460 | 80cabfad | bellard | } |
461 | d7d02e3c | bellard | } |
462 | d7d02e3c | bellard | |
463 | d7d02e3c | bellard | PITState *pit_init(int base, int irq) |
464 | d7d02e3c | bellard | { |
465 | d7d02e3c | bellard | PITState *pit = &pit_state; |
466 | d7d02e3c | bellard | PITChannelState *s; |
467 | d7d02e3c | bellard | |
468 | d7d02e3c | bellard | s = &pit->channels[0];
|
469 | d7d02e3c | bellard | /* the timer 0 is connected to an IRQ */
|
470 | d7d02e3c | bellard | s->irq_timer = qemu_new_timer(vm_clock, pit_irq_timer, s); |
471 | d7d02e3c | bellard | s->irq = irq; |
472 | 80cabfad | bellard | |
473 | ec844b96 | bellard | register_savevm("i8254", base, 1, pit_save, pit_load, pit); |
474 | b0a21b53 | bellard | |
475 | d7d02e3c | bellard | qemu_register_reset(pit_reset, pit); |
476 | ec844b96 | bellard | register_ioport_write(base, 4, 1, pit_ioport_write, pit); |
477 | ec844b96 | bellard | register_ioport_read(base, 3, 1, pit_ioport_read, pit); |
478 | d7d02e3c | bellard | |
479 | d7d02e3c | bellard | pit_reset(pit); |
480 | d7d02e3c | bellard | |
481 | ec844b96 | bellard | return pit;
|
482 | 80cabfad | bellard | } |