2 * QTest testcase for the MC146818 real-time clock
4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "hw/mc146818rtc_regs.h"
22 static uint8_t base
= 0x70;
24 static int bcd2dec(int value
)
26 return (((value
>> 4) & 0x0F) * 10) + (value
& 0x0F);
29 static uint8_t cmos_read(uint8_t reg
)
35 static void cmos_write(uint8_t reg
, uint8_t val
)
41 static int tm_cmp(struct tm
*lhs
, struct tm
*rhs
)
46 memcpy(&d1
, lhs
, sizeof(d1
));
47 memcpy(&d2
, rhs
, sizeof(d2
));
62 static void print_tm(struct tm
*tm
)
64 printf("%04d-%02d-%02d %02d:%02d:%02d\n",
65 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
66 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tm
->tm_gmtoff
);
70 static void cmos_get_date_time(struct tm
*date
)
72 int base_year
= 2000, hour_offset
;
73 int sec
, min
, hour
, mday
, mon
, year
;
77 sec
= cmos_read(RTC_SECONDS
);
78 min
= cmos_read(RTC_MINUTES
);
79 hour
= cmos_read(RTC_HOURS
);
80 mday
= cmos_read(RTC_DAY_OF_MONTH
);
81 mon
= cmos_read(RTC_MONTH
);
82 year
= cmos_read(RTC_YEAR
);
84 if ((cmos_read(RTC_REG_B
) & REG_B_DM
) == 0) {
96 if ((cmos_read(0x0B) & REG_B_24H
) == 0) {
97 if (hour
>= hour_offset
) {
104 localtime_r(&ts
, &dummy
);
106 date
->tm_isdst
= dummy
.tm_isdst
;
109 date
->tm_hour
= hour
;
110 date
->tm_mday
= mday
;
111 date
->tm_mon
= mon
- 1;
112 date
->tm_year
= base_year
+ year
- 1900;
120 static void check_time(int wiggle
)
122 struct tm start
, date
[4], end
;
127 * This check assumes a few things. First, we cannot guarantee that we get
128 * a consistent reading from the wall clock because we may hit an edge of
129 * the clock while reading. To work around this, we read four clock readings
130 * such that at least two of them should match. We need to assume that one
131 * reading is corrupt so we need four readings to ensure that we have at
132 * least two consecutive identical readings
134 * It's also possible that we'll cross an edge reading the host clock so
135 * simply check to make sure that the clock reading is within the period of
136 * when we expect it to be.
140 gmtime_r(&ts
, &start
);
142 cmos_get_date_time(&date
[0]);
143 cmos_get_date_time(&date
[1]);
144 cmos_get_date_time(&date
[2]);
145 cmos_get_date_time(&date
[3]);
150 if (tm_cmp(&date
[0], &date
[1]) == 0) {
152 } else if (tm_cmp(&date
[1], &date
[2]) == 0) {
154 } else if (tm_cmp(&date
[2], &date
[3]) == 0) {
157 g_assert_not_reached();
160 if (!(tm_cmp(&start
, datep
) <= 0 && tm_cmp(datep
, &end
) <= 0)) {
163 start
.tm_isdst
= datep
->tm_isdst
;
165 t
= (long)mktime(datep
);
166 s
= (long)mktime(&start
);
168 g_test_message("RTC is %ld second(s) behind wall-clock\n", (s
- t
));
170 g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t
- s
));
173 g_assert_cmpint(ABS(t
- s
), <=, wiggle
);
177 static int wiggle
= 2;
179 static void set_year_20xx(void)
182 cmos_write(RTC_REG_B
, REG_B_24H
);
183 cmos_write(RTC_REG_A
, 0x76);
184 cmos_write(RTC_YEAR
, 0x11);
185 cmos_write(RTC_CENTURY
, 0x20);
186 cmos_write(RTC_MONTH
, 0x02);
187 cmos_write(RTC_DAY_OF_MONTH
, 0x02);
188 cmos_write(RTC_HOURS
, 0x02);
189 cmos_write(RTC_MINUTES
, 0x04);
190 cmos_write(RTC_SECONDS
, 0x58);
191 cmos_write(RTC_REG_A
, 0x26);
193 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
194 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
195 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
196 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
197 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
198 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
199 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
201 if (sizeof(time_t) == 4) {
205 /* Set a date in 2080 to ensure there is no year-2038 overflow. */
206 cmos_write(RTC_REG_A
, 0x76);
207 cmos_write(RTC_YEAR
, 0x80);
208 cmos_write(RTC_REG_A
, 0x26);
210 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
211 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
212 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
213 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
214 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
215 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x80);
216 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
218 cmos_write(RTC_REG_A
, 0x76);
219 cmos_write(RTC_YEAR
, 0x11);
220 cmos_write(RTC_REG_A
, 0x26);
222 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
223 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
224 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
225 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
226 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
227 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
228 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
231 static void set_year_1980(void)
234 cmos_write(RTC_REG_B
, REG_B_24H
);
235 cmos_write(RTC_REG_A
, 0x76);
236 cmos_write(RTC_YEAR
, 0x80);
237 cmos_write(RTC_CENTURY
, 0x19);
238 cmos_write(RTC_MONTH
, 0x02);
239 cmos_write(RTC_DAY_OF_MONTH
, 0x02);
240 cmos_write(RTC_HOURS
, 0x02);
241 cmos_write(RTC_MINUTES
, 0x04);
242 cmos_write(RTC_SECONDS
, 0x58);
243 cmos_write(RTC_REG_A
, 0x26);
245 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
246 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
247 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
248 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
249 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
250 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x80);
251 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x19);
254 static void bcd_check_time(void)
257 cmos_write(RTC_REG_B
, REG_B_24H
);
261 static void dec_check_time(void)
264 cmos_write(RTC_REG_B
, REG_B_24H
| REG_B_DM
);
268 static void alarm_time(void)
278 cmos_write(RTC_REG_B
, REG_B_24H
| REG_B_DM
);
280 g_assert(!get_irq(RTC_ISA_IRQ
));
281 cmos_read(RTC_REG_C
);
283 now
.tm_sec
= (now
.tm_sec
+ 2) % 60;
284 cmos_write(RTC_SECONDS_ALARM
, now
.tm_sec
);
285 cmos_write(RTC_MINUTES_ALARM
, RTC_ALARM_DONT_CARE
);
286 cmos_write(RTC_HOURS_ALARM
, RTC_ALARM_DONT_CARE
);
287 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) | REG_B_AIE
);
289 for (i
= 0; i
< 2 + wiggle
; i
++) {
290 if (get_irq(RTC_ISA_IRQ
)) {
294 clock_step(1000000000);
297 g_assert(get_irq(RTC_ISA_IRQ
));
298 g_assert((cmos_read(RTC_REG_C
) & REG_C_AF
) != 0);
299 g_assert(cmos_read(RTC_REG_C
) == 0);
302 static void set_time(int mode
, int h
, int m
, int s
)
304 /* set BCD 12 hour mode */
305 cmos_write(RTC_REG_B
, mode
);
307 cmos_write(RTC_REG_A
, 0x76);
308 cmos_write(RTC_HOURS
, h
);
309 cmos_write(RTC_MINUTES
, m
);
310 cmos_write(RTC_SECONDS
, s
);
311 cmos_write(RTC_REG_A
, 0x26);
314 #define assert_time(h, m, s) \
316 g_assert_cmpint(cmos_read(RTC_HOURS), ==, h); \
317 g_assert_cmpint(cmos_read(RTC_MINUTES), ==, m); \
318 g_assert_cmpint(cmos_read(RTC_SECONDS), ==, s); \
321 static void basic_12h_bcd(void)
323 /* set BCD 12 hour mode */
324 set_time(0, 0x81, 0x59, 0x00);
325 clock_step(1000000000LL);
326 assert_time(0x81, 0x59, 0x01);
327 clock_step(59000000000LL);
328 assert_time(0x82, 0x00, 0x00);
330 /* test BCD wraparound */
331 set_time(0, 0x09, 0x59, 0x59);
332 clock_step(60000000000LL);
333 assert_time(0x10, 0x00, 0x59);
336 set_time(0, 0x12, 0x59, 0x59);
337 clock_step(1000000000LL);
338 assert_time(0x01, 0x00, 0x00);
341 set_time(0, 0x92, 0x59, 0x59);
342 clock_step(1000000000LL);
343 assert_time(0x81, 0x00, 0x00);
346 set_time(0, 0x11, 0x59, 0x59);
347 clock_step(1000000000LL);
348 assert_time(0x92, 0x00, 0x00);
349 /* TODO: test day wraparound */
352 set_time(0, 0x91, 0x59, 0x59);
353 clock_step(1000000000LL);
354 assert_time(0x12, 0x00, 0x00);
355 /* TODO: test day wraparound */
358 static void basic_12h_dec(void)
360 /* set decimal 12 hour mode */
361 set_time(REG_B_DM
, 0x81, 59, 0);
362 clock_step(1000000000LL);
363 assert_time(0x81, 59, 1);
364 clock_step(59000000000LL);
365 assert_time(0x82, 0, 0);
368 set_time(REG_B_DM
, 0x8c, 59, 59);
369 clock_step(1000000000LL);
370 assert_time(0x81, 0, 0);
373 set_time(REG_B_DM
, 0x0c, 59, 59);
374 clock_step(1000000000LL);
375 assert_time(0x01, 0, 0);
378 set_time(REG_B_DM
, 0x0b, 59, 59);
379 clock_step(1000000000LL);
380 assert_time(0x8c, 0, 0);
383 set_time(REG_B_DM
, 0x8b, 59, 59);
384 clock_step(1000000000LL);
385 assert_time(0x0c, 0, 0);
386 /* TODO: test day wraparound */
389 static void basic_24h_bcd(void)
391 /* set BCD 24 hour mode */
392 set_time(REG_B_24H
, 0x09, 0x59, 0x00);
393 clock_step(1000000000LL);
394 assert_time(0x09, 0x59, 0x01);
395 clock_step(59000000000LL);
396 assert_time(0x10, 0x00, 0x00);
398 /* test BCD wraparound */
399 set_time(REG_B_24H
, 0x09, 0x59, 0x00);
400 clock_step(60000000000LL);
401 assert_time(0x10, 0x00, 0x00);
403 /* TODO: test day wraparound */
404 set_time(REG_B_24H
, 0x23, 0x59, 0x00);
405 clock_step(60000000000LL);
406 assert_time(0x00, 0x00, 0x00);
409 static void basic_24h_dec(void)
411 /* set decimal 24 hour mode */
412 set_time(REG_B_24H
| REG_B_DM
, 9, 59, 0);
413 clock_step(1000000000LL);
414 assert_time(9, 59, 1);
415 clock_step(59000000000LL);
416 assert_time(10, 0, 0);
418 /* test BCD wraparound */
419 set_time(REG_B_24H
| REG_B_DM
, 9, 59, 0);
420 clock_step(60000000000LL);
421 assert_time(10, 0, 0);
423 /* TODO: test day wraparound */
424 set_time(REG_B_24H
| REG_B_DM
, 23, 59, 0);
425 clock_step(60000000000LL);
426 assert_time(0, 0, 0);
429 static void am_pm_alarm(void)
431 cmos_write(RTC_MINUTES_ALARM
, 0xC0);
432 cmos_write(RTC_SECONDS_ALARM
, 0xC0);
434 /* set BCD 12 hour mode */
435 cmos_write(RTC_REG_B
, 0);
437 /* Set time and alarm hour. */
438 cmos_write(RTC_REG_A
, 0x76);
439 cmos_write(RTC_HOURS_ALARM
, 0x82);
440 cmos_write(RTC_HOURS
, 0x81);
441 cmos_write(RTC_MINUTES
, 0x59);
442 cmos_write(RTC_SECONDS
, 0x00);
443 cmos_read(RTC_REG_C
);
444 cmos_write(RTC_REG_A
, 0x26);
446 /* Check that alarm triggers when AM/PM is set. */
447 clock_step(60000000000LL);
448 g_assert(cmos_read(RTC_HOURS
) == 0x82);
449 g_assert((cmos_read(RTC_REG_C
) & REG_C_AF
) != 0);
452 * Each of the following two tests takes over 60 seconds due to the time
453 * needed to report the PIT interrupts. Unfortunately, our PIT device
454 * model keeps counting even when GATE=0, so we cannot simply disable
457 if (g_test_quick()) {
461 /* set DEC 12 hour mode */
462 cmos_write(RTC_REG_B
, REG_B_DM
);
464 /* Set time and alarm hour. */
465 cmos_write(RTC_REG_A
, 0x76);
466 cmos_write(RTC_HOURS_ALARM
, 0x82);
467 cmos_write(RTC_HOURS
, 3);
468 cmos_write(RTC_MINUTES
, 0);
469 cmos_write(RTC_SECONDS
, 0);
470 cmos_read(RTC_REG_C
);
471 cmos_write(RTC_REG_A
, 0x26);
473 /* Check that alarm triggers. */
474 clock_step(3600 * 11 * 1000000000LL);
475 g_assert(cmos_read(RTC_HOURS
) == 0x82);
476 g_assert((cmos_read(RTC_REG_C
) & REG_C_AF
) != 0);
478 /* Same as above, with inverted HOURS and HOURS_ALARM. */
479 cmos_write(RTC_REG_A
, 0x76);
480 cmos_write(RTC_HOURS_ALARM
, 2);
481 cmos_write(RTC_HOURS
, 3);
482 cmos_write(RTC_MINUTES
, 0);
483 cmos_write(RTC_SECONDS
, 0);
484 cmos_read(RTC_REG_C
);
485 cmos_write(RTC_REG_A
, 0x26);
487 /* Check that alarm does not trigger if hours differ only by AM/PM. */
488 clock_step(3600 * 11 * 1000000000LL);
489 g_assert(cmos_read(RTC_HOURS
) == 0x82);
490 g_assert((cmos_read(RTC_REG_C
) & REG_C_AF
) == 0);
493 /* success if no crash or abort */
494 static void fuzz_registers(void)
498 for (i
= 0; i
< 1000; i
++) {
501 reg
= (uint8_t)g_test_rand_int_range(0, 16);
502 val
= (uint8_t)g_test_rand_int_range(0, 256);
504 cmos_write(reg
, val
);
509 static void register_b_set_flag(void)
511 /* Enable binary-coded decimal (BCD) mode and SET flag in Register B*/
512 cmos_write(RTC_REG_B
, REG_B_24H
| REG_B_SET
);
514 cmos_write(RTC_REG_A
, 0x76);
515 cmos_write(RTC_YEAR
, 0x11);
516 cmos_write(RTC_CENTURY
, 0x20);
517 cmos_write(RTC_MONTH
, 0x02);
518 cmos_write(RTC_DAY_OF_MONTH
, 0x02);
519 cmos_write(RTC_HOURS
, 0x02);
520 cmos_write(RTC_MINUTES
, 0x04);
521 cmos_write(RTC_SECONDS
, 0x58);
522 cmos_write(RTC_REG_A
, 0x26);
524 /* Since SET flag is still enabled, these are equality checks. */
525 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
526 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
527 g_assert_cmpint(cmos_read(RTC_SECONDS
), ==, 0x58);
528 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
529 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
530 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
531 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
533 /* Disable SET flag in Register B */
534 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) & ~REG_B_SET
);
536 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
537 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
539 /* Since SET flag is disabled, this is an inequality check.
540 * We (reasonably) assume that no (sexagesimal) overflow occurs. */
541 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
542 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
543 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
544 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
545 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
548 int main(int argc
, char **argv
)
550 QTestState
*s
= NULL
;
553 g_test_init(&argc
, &argv
, NULL
);
555 s
= qtest_start("-display none -rtc clock=vm");
556 qtest_irq_intercept_in(s
, "ioapic");
558 qtest_add_func("/rtc/check-time/bcd", bcd_check_time
);
559 qtest_add_func("/rtc/check-time/dec", dec_check_time
);
560 qtest_add_func("/rtc/alarm/interrupt", alarm_time
);
561 qtest_add_func("/rtc/alarm/am-pm", am_pm_alarm
);
562 qtest_add_func("/rtc/basic/dec-24h", basic_24h_dec
);
563 qtest_add_func("/rtc/basic/bcd-24h", basic_24h_bcd
);
564 qtest_add_func("/rtc/basic/dec-12h", basic_12h_dec
);
565 qtest_add_func("/rtc/basic/bcd-12h", basic_12h_bcd
);
566 qtest_add_func("/rtc/set-year/20xx", set_year_20xx
);
567 qtest_add_func("/rtc/set-year/1980", set_year_1980
);
568 qtest_add_func("/rtc/misc/register_b_set_flag", register_b_set_flag
);
569 qtest_add_func("/rtc/misc/fuzz-registers", fuzz_registers
);