qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 1/4] hw/ds1338.c: Fix handling of HOURS register.
@ 2012-12-02 17:14 Antoine Mathys
  2012-12-04 17:42 ` Peter Maydell
  0 siblings, 1 reply; 5+ messages in thread
From: Antoine Mathys @ 2012-12-02 17:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: peter.maydell, paul

Per the datasheet, the mapping between 12 and 24 hours modes is:
     0      <->  12   PM
     1-12   <->  1-12 AM
     13-23  <->  1-11 PM

Signed-off-by: Antoine Mathys <barsamin@gmail.com>
---
  hw/ds1338.c |   23 +++++++++++++++--------
  1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/hw/ds1338.c b/hw/ds1338.c
index b576d56..1274b22 100644
--- a/hw/ds1338.c
+++ b/hw/ds1338.c
@@ -50,9 +50,14 @@ static void capture_current_time(DS1338State *s)
      s->nvram[0] = to_bcd(now.tm_sec);
      s->nvram[1] = to_bcd(now.tm_min);
      if (s->nvram[2] & 0x40) {
-        s->nvram[2] = (to_bcd((now.tm_hour % 12)) + 1) | 0x40;
-        if (now.tm_hour >= 12) {
-            s->nvram[2] |= 0x20;
+        int tmp = now.tm_hour;
+        if (tmp == 0) {
+            tmp = 24;
+        }
+        if (tmp <= 12) {
+            s->nvram[2] = 0x40 | to_bcd(tmp);
+        } else {
+            s->nvram[2] = 0x60 | to_bcd(tmp - 12);
          }
      } else {
          s->nvram[2] = to_bcd(now.tm_hour);
@@ -127,15 +132,17 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data)
              break;
          case 2:
              if (data & 0x40) {
+                int tmp = from_bcd(data & 0x1f);
                  if (data & 0x20) {
-                    data = from_bcd(data & 0x4f) + 11;
-                } else {
-                    data = from_bcd(data & 0x1f) - 1;
+                    tmp += 12;
+                }
+                if (tmp == 24) {
+                    tmp = 0;
                  }
+                now.tm_hour = tmp;
              } else {
-                data = from_bcd(data);
+                now.tm_hour = from_bcd(data & 0x3f);
              }
-            now.tm_hour = data;
              break;
          case 3:
              now.tm_wday = from_bcd(data & 7) - 1;
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-12-04 21:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-02 17:14 [Qemu-devel] [PATCH 1/4] hw/ds1338.c: Fix handling of HOURS register Antoine Mathys
2012-12-04 17:42 ` Peter Maydell
2012-12-04 18:53   ` Andreas Färber
2012-12-04 20:57   ` Antoine Mathys
2012-12-04 21:10     ` Peter Maydell

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).