Skip to content

Commit bc706fa

Browse files
committed
rtc-test: cleanup register_b_set_flag test
Introduce set_datetime_bcd/assert_datetime_bcd, and handle UIP correctly. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent fafeb41 commit bc706fa

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

tests/rtc-test.c

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "qemu/timer.h"
1818
#include "hw/timer/mc146818rtc_regs.h"
1919

20+
#define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
21+
2022
static uint8_t base = 0x70;
2123

2224
static int bcd2dec(int value)
@@ -297,16 +299,30 @@ static void alarm_time(void)
297299
g_assert(cmos_read(RTC_REG_C) == 0);
298300
}
299301

302+
static void set_time_regs(int h, int m, int s)
303+
{
304+
cmos_write(RTC_HOURS, h);
305+
cmos_write(RTC_MINUTES, m);
306+
cmos_write(RTC_SECONDS, s);
307+
}
308+
300309
static void set_time(int mode, int h, int m, int s)
301310
{
302-
/* set BCD 12 hour mode */
303311
cmos_write(RTC_REG_B, mode);
304-
305312
cmos_write(RTC_REG_A, 0x76);
313+
set_time_regs(h, m, s);
314+
cmos_write(RTC_REG_A, 0x26);
315+
}
316+
317+
static void set_datetime_bcd(int h, int min, int s, int d, int m, int y)
318+
{
306319
cmos_write(RTC_HOURS, h);
307-
cmos_write(RTC_MINUTES, m);
320+
cmos_write(RTC_MINUTES, min);
308321
cmos_write(RTC_SECONDS, s);
309-
cmos_write(RTC_REG_A, 0x26);
322+
cmos_write(RTC_YEAR, y & 0xFF);
323+
cmos_write(RTC_CENTURY, y >> 8);
324+
cmos_write(RTC_MONTH, m);
325+
cmos_write(RTC_DAY_OF_MONTH, d);
310326
}
311327

312328
#define assert_time(h, m, s) \
@@ -316,6 +332,17 @@ static void set_time(int mode, int h, int m, int s)
316332
g_assert_cmpint(cmos_read(RTC_SECONDS), ==, s); \
317333
} while(0)
318334

335+
#define assert_datetime_bcd(h, min, s, d, m, y) \
336+
do { \
337+
g_assert_cmpint(cmos_read(RTC_HOURS), ==, h); \
338+
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, min); \
339+
g_assert_cmpint(cmos_read(RTC_SECONDS), ==, s); \
340+
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, d); \
341+
g_assert_cmpint(cmos_read(RTC_MONTH), ==, m); \
342+
g_assert_cmpint(cmos_read(RTC_YEAR), ==, (y & 0xFF)); \
343+
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, (y >> 8)); \
344+
} while(0)
345+
319346
static void basic_12h_bcd(void)
320347
{
321348
/* set BCD 12 hour mode */
@@ -506,41 +533,30 @@ static void fuzz_registers(void)
506533

507534
static void register_b_set_flag(void)
508535
{
536+
if (cmos_read(RTC_REG_A) & REG_A_UIP) {
537+
clock_step(UIP_HOLD_LENGTH + NANOSECONDS_PER_SECOND / 5);
538+
}
539+
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
540+
509541
/* Enable binary-coded decimal (BCD) mode and SET flag in Register B*/
510542
cmos_write(RTC_REG_B, REG_B_24H | REG_B_SET);
511543

512-
cmos_write(RTC_REG_A, 0x76);
513-
cmos_write(RTC_YEAR, 0x11);
514-
cmos_write(RTC_CENTURY, 0x20);
515-
cmos_write(RTC_MONTH, 0x02);
516-
cmos_write(RTC_DAY_OF_MONTH, 0x02);
517-
cmos_write(RTC_HOURS, 0x02);
518-
cmos_write(RTC_MINUTES, 0x04);
519-
cmos_write(RTC_SECONDS, 0x58);
520-
cmos_write(RTC_REG_A, 0x26);
544+
set_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
521545

522-
/* Since SET flag is still enabled, these are equality checks. */
523-
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
524-
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
525-
g_assert_cmpint(cmos_read(RTC_SECONDS), ==, 0x58);
526-
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
527-
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
528-
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
529-
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
546+
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
547+
548+
/* Since SET flag is still enabled, time does not advance. */
549+
clock_step(1000000000LL);
550+
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
530551

531552
/* Disable SET flag in Register B */
532553
cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_SET);
533554

534-
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
535-
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
555+
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
536556

537-
/* Since SET flag is disabled, this is an inequality check.
538-
* We (reasonably) assume that no (sexagesimal) overflow occurs. */
539-
g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
540-
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
541-
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
542-
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
543-
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
557+
/* Since SET flag is disabled, the clock now advances. */
558+
clock_step(1000000000LL);
559+
assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
544560
}
545561

546562
#define RTC_PERIOD_CODE1 13 /* 8 Hz */

0 commit comments

Comments
 (0)