From: Luiz Capitulino <lcapitulino@redhat.com>
To: Anthony Liguori <aliguori@us.ibm.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [RFC v2 5/6] Add RTC test case
Date: Mon, 5 Dec 2011 12:32:45 -0200 [thread overview]
Message-ID: <20111205123245.698fbfde@doriath> (raw)
In-Reply-To: <1322765012-3164-6-git-send-email-aliguori@us.ibm.com>
On Thu, 1 Dec 2011 12:43:31 -0600
Anthony Liguori <aliguori@us.ibm.com> wrote:
I think it's a good idea to use python's unittest module and write tests as
unit-tests (eg. using asserts instead of writing results as strings).
> ---
> rtc-test.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 105 insertions(+), 0 deletions(-)
> create mode 100644 rtc-test.py
>
> diff --git a/rtc-test.py b/rtc-test.py
> new file mode 100644
> index 0000000..3159795
> --- /dev/null
> +++ b/rtc-test.py
> @@ -0,0 +1,105 @@
> +from qtest import inb, outb
> +import qtest, time, calendar
> +
> +base = 0x70
> +
> +def bcd2dec(value):
> + return (((value >> 4) & 0x0F) * 10) + (value & 0x0F)
> +
> +def dec2bcd(value):
> + return ((value / 10) << 4) | (value % 10)
> +
> +def cmos_read(reg):
> + outb(base + 0, reg)
> + return inb(base + 1)
> +
> +def cmos_write(reg, val):
> + outb(base + 0, reg)
> + outb(base + 1, val)
> +
> +def cmos_get_date_time():
> + base_year = 2000
> +
> + sec = cmos_read(0x00)
> + min = cmos_read(0x02)
> + hour = cmos_read(0x04)
> + mday = cmos_read(0x07)
> + mon = cmos_read(0x08)
> + year = cmos_read(0x09)
> +
> + if (cmos_read(0x0B) & 4) == 0:
> + sec = bcd2dec(sec)
> + min = bcd2dec(min)
> + hour = bcd2dec(hour)
> + mday = bcd2dec(mday)
> + mon = bcd2dec(mon)
> + year = bcd2dec(year)
> + hour_offset = 80
> + else:
> + hour_offset = 0x80
> +
> + if (cmos_read(0x0B) & 2) == 0:
> + if hour >= hour_offset:
> + hour -= hour_offset
> + hour += 12
> +
> + return time.gmtime(calendar.timegm(((base_year + year), mon, mday, hour, min, sec)))
> +
> +def check_time():
> + # This check assumes a few things. First, we cannot guarantee that we get
> + # a consistent reading from the wall clock because we may hit an edge of
> + # the clock while reading. To work around this, we read four clock readings
> + # such that at least two of them should match. We need to assume that one
> + # reading is corrupt so we need four readings to ensure that we have at
> + # least two consecutive identical readings
> + #
> + # It's also possible that we'll cross an edge reading the host clock so
> + # simply check to make sure that the clock reading is within the period of
> + # when we expect it to be.
> +
> + start = time.gmtime()
> + date1 = cmos_get_date_time()
> + date2 = cmos_get_date_time()
> + date3 = cmos_get_date_time()
> + date4 = cmos_get_date_time()
> + end = time.gmtime()
> +
> + if date1 == date2:
> + date = date1
> + elif date2 == date3:
> + date = date2
> + elif date3 == date4:
> + date = date4
> + else:
> + print 'Could not read RTC fast enough'
> + return False
> +
> + if not start <= date <= end:
> + t = calendar.timegm(date)
> + s = calendar.timegm(start)
> + if t < s:
> + print 'RTC is %d second(s) behind wall-clock' % (s - t)
> + else:
> + print 'RTC is %d second(s) ahead of wall-clock' % (t - s)
> + return False
> +
> + return True
> +
> +def main(args):
> + qtest.init(args[0])
> +
> + # Set BCD mode
> + cmos_write(0x0B, cmos_read(0x0B) | 0x02)
> + if not check_time():
> + return 1
> +
> + # Set DEC mode
> + cmos_write(0x0B, cmos_read(0x0B) & ~0x02)
> + if not check_time():
> + return 1
> +
> + return 0
> +
> +if __name__ == '__main__':
> + import sys
> + sys.exit(main(sys.argv[1:]))
next prev parent reply other threads:[~2011-12-05 14:32 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-01 18:43 [Qemu-devel] [RFC v2 0/6] qtest unit test framework Anthony Liguori
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 1/6] qtest: add " Anthony Liguori
2011-12-05 14:27 ` Luiz Capitulino
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 2/6] qtest: add support for target-i386 -M pc Anthony Liguori
2011-12-02 7:52 ` Paolo Bonzini
2011-12-29 17:40 ` Peter Maydell
2011-12-29 18:47 ` Anthony Liguori
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 3/6] Add core python test framework Anthony Liguori
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 4/6] Add uart test case Anthony Liguori
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 5/6] Add RTC " Anthony Liguori
2011-12-05 14:32 ` Luiz Capitulino [this message]
2011-12-01 18:43 ` [Qemu-devel] [RFC v2 6/6] Add C version of rtc-test Anthony Liguori
2011-12-02 17:25 ` Kevin Wolf
2011-12-02 17:26 ` Anthony Liguori
2011-12-02 17:45 ` Kevin Wolf
2011-12-02 18:20 ` Luiz Capitulino
2011-12-02 18:43 ` Anthony Liguori
2011-12-05 8:51 ` Kevin Wolf
2011-12-04 10:03 ` [Qemu-devel] [RFC v2 0/6] qtest unit test framework Dor Laor
2011-12-05 15:29 ` Anthony Liguori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111205123245.698fbfde@doriath \
--to=lcapitulino@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).