Gameboy Development Forum

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.

Ads

#1 2025-02-09 06:25:49

alieneight
New member
Registered: 2025-02-09
Posts: 3

Set RTC values, Gameboy classic, GBDK-2020

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!) :

Code:

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:

Code:

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

 

#2 2025-02-09 15:55:51

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 250

Re: Set RTC values, Gameboy classic, GBDK-2020

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.

Code:

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;
}

Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2025-02-18 15:22:38

alieneight
New member
Registered: 2025-02-09
Posts: 3

Re: Set RTC values, Gameboy classic, GBDK-2020

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

 

#4 2025-02-19 09:05:56

toxa
Member
Registered: 2020-02-13
Posts: 310

Re: Set RTC values, Gameboy classic, GBDK-2020

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

 

#5 2025-02-20 02:49:02

alieneight
New member
Registered: 2025-02-09
Posts: 3

Re: Set RTC values, Gameboy classic, GBDK-2020

Ha, excellent. This looks exactly like what I'm trying to (re)invent. Thanks!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson