Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
Hi,
Env - Classic Gameboy, EZ-Flash Jr, GBDK-2020
Goal - To display the time on the Gameboy screen, values taken from the RTC on the EZ-Flash cart. Also be able to set the time from the code.
The following code works great for getting the hours, minutes and seconds from the RTC (thanks dgc1980 on github!) :
void get_time(void) { SWITCH_RAM_MBC1(0x08); rtc_s = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0x09); rtc_m = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0x0A); rtc_h = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0); }
But I'm struggling with how to set the values. Just writing to the same registers doesn't seem to work:
void put_time(void) { rtc_s = 0 rtc_m = 0; rtc_h = 13; SWITCH_RAM_MBC1(0x08); *(UBYTE *)0xA000 = rtc_s; SWITCH_RAM_MBC1(0x09); *(UBYTE *)0xA000 = rtc_m; SWITCH_RAM_MBC1(0x0A); *(UBYTE *)0xA000 = rtc_h; SWITCH_RAM_MBC1(0); }
Any pointers/help much appreciated.
Thanks,
Alien Eight.
Offline
The most obvious apparent issue is that you should enable access to the SRAM region before writing to it, or reading from it.
For reading, you should ideally also latch the clock data, which copies the time data into a temporary register which is frozen until latched again. This prevents the issue that you may read for example the seconds, and then before you read the minutes, the clock ticks forward so you get the wrong time. Latching happens when the value of the register at 0x6000 changes from 0 to 1. Ideally you should first write 0 then 1 to make sure that you have a change from 0 to 1, in case the register was already set to 1 by mistake.
void get_time(void) { DISABLE_RAM; SWITCH_RAM_MBC1(0x03); // Latch RTC data *(UBYTE *)0x6000 = 0; *(UBYTE *)0x6000 = 1; rtc_s = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0x09); rtc_m = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0x0A); rtc_h = *(UBYTE *)0xA000; SWITCH_RAM_MBC1(0); *(UBYTE *)0x6000 = 0; DISABLE_RAM; } void put_time(void) { rtc_s = 0; rtc_m = 0; rtc_h = 13; ENABLE_RAM; SWITCH_RAM_MBC1(0x08); *(UBYTE *)0xA000 = rtc_s; SWITCH_RAM_MBC1(0x09); *(UBYTE *)0xA000 = rtc_m; SWITCH_RAM_MBC1(0x0A); *(UBYTE *)0xA000 = rtc_h; SWITCH_RAM_MBC1(0); DISABLE_RAM; }
Offline
Thanks for the reply.
The values have indeed changed, but I'm getting odd readings when I get them. I'll spend a bit of time on it and no doubt get back to you if I don't get anywhere.
Cheers.
Offline
there is an rtc.h header in the GBStudio core project: https://github.com/chrismaltby/gbvm/blo … lude/rtc.h which provide the RTC definitions and convenience functions.
Offline
Ha, excellent. This looks exactly like what I'm trying to (re)invent. Thanks!
Offline