From d5368b0799a2a335f4accac2027617c433db4fef Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Wed, 10 Feb 2021 09:00:05 +0000 Subject: [PATCH] games/gb: various RTC fixes MBC3 write switches on a>>13, so the RTC register is 5 (0xA000-0xBFFF). Mask off upper bits of DH register when updating the timer. Only the lowest bit is part of the day counter. Use uint for x in timerforward() so that we don't set negative values for timer registers if it happens to overflow. Update timer and then latch rather than the other way around. Otherwise, timer remains static and will overflow after 512 days. --- sys/src/games/gb/mem.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sys/src/games/gb/mem.c b/sys/src/games/gb/mem.c index 05e4754ea..c299c66f5 100644 --- a/sys/src/games/gb/mem.c +++ b/sys/src/games/gb/mem.c @@ -329,7 +329,7 @@ void timerforward(MBC3Timer *t) { vlong n, nd; - int x; + uint x; n = nsec(); nd = n - t->ns; @@ -340,7 +340,7 @@ timerforward(MBC3Timer *t) return; } t->ns = n - nd % BILLION; - x = t->sec + t->min * 60 + t->hr * 3600 + t->dl * 86400 + t->dh * (256 * 86400); + x = t->sec + t->min * 60 + t->hr * 3600 + ((t->dh & 1) << 8 | t->dl) * 86400; x += nd / BILLION; t->sec = x % 60; x /= 60; @@ -398,12 +398,12 @@ mbc3(int a, int v) case 2: b1 = v & 15; break; case 3: if(latch == 0 && v == 1){ + timerforward(&timer); timerl = timer; - timerforward(&timerl); } latch = v; break; - case 0xa: + case 5: if(!ramen) return 0; switch(b1){