From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:47385) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TiYlN-0002GG-E5 for qemu-devel@nongnu.org; Tue, 11 Dec 2012 17:57:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TiYlG-0000va-4t for qemu-devel@nongnu.org; Tue, 11 Dec 2012 17:57:21 -0500 Received: from mail-we0-f173.google.com ([74.125.82.173]:39520) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TiYlF-0000vT-Uz for qemu-devel@nongnu.org; Tue, 11 Dec 2012 17:57:14 -0500 Received: by mail-we0-f173.google.com with SMTP id z2so4398wey.4 for ; Tue, 11 Dec 2012 14:57:13 -0800 (PST) Message-ID: <50C7BA46.6000009@gmail.com> Date: Tue, 11 Dec 2012 23:57:10 +0100 From: Antoine Mathys MIME-Version: 1.0 References: <50C7B767.8020406@gmail.com> In-Reply-To: <50C7B767.8020406@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH v2 3/6] hw/ds1338.c: Fix handling of HOURS register. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , paul@codesourcery.com 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 --- hw/ds1338.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/hw/ds1338.c b/hw/ds1338.c index 69018bc..0f88720 100644 --- a/hw/ds1338.c +++ b/hw/ds1338.c @@ -55,10 +55,15 @@ static void capture_current_time(DS1338State *s) qemu_get_timedate(&now, s->offset); 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; + if (s->nvram[2] & HOURS_12) { + int tmp = now.tm_hour; + if (tmp == 0) { + tmp = 24; + } + if (tmp <= 12) { + s->nvram[2] = HOURS_12 | to_bcd(tmp); + } else { + s->nvram[2] = HOURS_12 | HOURS_PM | to_bcd(tmp - 12); } } else { s->nvram[2] = to_bcd(now.tm_hour); @@ -132,16 +137,18 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data) now.tm_min = from_bcd(data & 0x7f); break; case 2: - if (data & 0x40) { - if (data & 0x20) { - data = from_bcd(data & 0x4f) + 11; - } else { - data = from_bcd(data & 0x1f) - 1; + if (data & HOURS_12) { + int tmp = from_bcd(data & (HOURS_PM - 1)); + if (data & HOURS_PM) { + tmp += 12; + } + if (tmp == 24) { + tmp = 0; } + now.tm_hour = tmp; } else { - data = from_bcd(data); + now.tm_hour = from_bcd(data & (HOURS_12 - 1)); } - now.tm_hour = data; break; case 3: now.tm_wday = from_bcd(data & 7) - 1; -- 1.7.10.4