shithub: riscv

Download patch

ref: d5368b0799a2a335f4accac2027617c433db4fef
parent: 638b82129e750940b4d886c17c7c0ba9d8dbceac
author: Michael Forney <mforney@mforney.org>
date: Wed Feb 10 04:00:05 EST 2021

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.

--- a/sys/src/games/gb/mem.c
+++ b/sys/src/games/gb/mem.c
@@ -329,7 +329,7 @@
 timerforward(MBC3Timer *t)
 {
 	vlong n, nd;
-	int x;
+	uint x;
 	
 	n = nsec();
 	nd = n - t->ns;
@@ -340,7 +340,7 @@
 		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 @@
 	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){