From e1147b87abef7687055c9127437e6ce1353c6113 Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Mon, 30 Jul 2007 05:19:05 +0000 Subject: [PATCH] RTC (real time clock) support for Rockboy. The time in games like Pokemon Gold/Silver should now stay synced on RTC-capable targets. Other targets will lose track of time when the player is turned off. User's can edit the .rtc file in an attempt to resync the RTC. The clock should stay synced when entering and leaving the menu. I've tested this over the past two weeks and it seems to work well. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14071 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugin.c | 3 ++ apps/plugin.h | 7 +++-- apps/plugins/rockboy/Makefile | 3 +- apps/plugins/rockboy/emu.c | 10 +----- apps/plugins/rockboy/menu.c | 12 ++++++++ apps/plugins/rockboy/rtc.c | 49 +++++++++++++++++------------- apps/plugins/rockboy/sys_rockbox.c | 20 ------------ 7 files changed, 51 insertions(+), 53 deletions(-) diff --git a/apps/plugin.c b/apps/plugin.c index dbd7d7b4fb..fee3a51851 100644 --- a/apps/plugin.c +++ b/apps/plugin.c @@ -439,6 +439,9 @@ static const struct plugin_api rockbox_api = { kbd_input, get_time, set_time, +#if CONFIG_RTC + mktime, +#endif plugin_get_buffer, plugin_get_audio_buffer, plugin_tsr, diff --git a/apps/plugin.h b/apps/plugin.h index 5ec0174c52..5146e18139 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -112,12 +112,12 @@ #define PLUGIN_MAGIC 0x526F634B /* RocK */ /* increase this every time the api struct changes */ -#define PLUGIN_API_VERSION 67 +#define PLUGIN_API_VERSION 68 /* update this to latest version if a change to the api struct breaks backwards compatibility (and please take the opportunity to sort in any new function which are "waiting" at the end of the function table) */ -#define PLUGIN_MIN_API_VERSION 65 +#define PLUGIN_MIN_API_VERSION 68 /* plugin return codes */ enum plugin_status { @@ -549,6 +549,9 @@ struct plugin_api { int (*kbd_input)(char* buffer, int buflen); struct tm* (*get_time)(void); int (*set_time)(const struct tm *tm); +#if CONFIG_RTC + time_t (*mktime)(struct tm *t); +#endif void* (*plugin_get_buffer)(size_t *buffer_size); void* (*plugin_get_audio_buffer)(size_t *buffer_size); void (*plugin_tsr)(bool (*exit_callback)(bool reenter)); diff --git a/apps/plugins/rockboy/Makefile b/apps/plugins/rockboy/Makefile index 774be303fd..65170bb6fd 100644 --- a/apps/plugins/rockboy/Makefile +++ b/apps/plugins/rockboy/Makefile @@ -19,7 +19,8 @@ endif LINKFILE := $(OBJDIR)/link.lds DEPFILE = $(OBJDIR)/dep-rockboy SRC = cpu.c emu.c events.c fastmem.c hw.c lcd.c lcdc.c loader.c main.c \ - mem.c menu.c rbsound.c rockboy.c rtc.c save.c sound.c sys_rockbox.c + mem.c menu.c rbsound.c rockboy.c rtc.c save.c sound.c sys_rockbox.c \ + ../../../firmware/common/sscanf.c #CFLAGS += -DDYNAREC #SRC += dynarec.c diff --git a/apps/plugins/rockboy/emu.c b/apps/plugins/rockboy/emu.c index 02e5fa0b1c..d04fa9a0fb 100644 --- a/apps/plugins/rockboy/emu.c +++ b/apps/plugins/rockboy/emu.c @@ -32,7 +32,6 @@ void emu_step(void) * make things work in the mean time. */ void emu_run(void) { - /*void *timer = sys_timer();*/ int framesin=0,frames=0,timeten=*rb->current_tick, timehun=*rb->current_tick; setvidmode(); @@ -48,20 +47,13 @@ void emu_run(void) while (R_LY > 0 && R_LY < 144) emu_step(); - rtc_tick(); /* RTC support not implemented */ + rtc_tick(); if (options.sound || !plugbuf) { sound_mix(); pcm_submit(); } - else - { -/* delay = framelen - sys_elapsed(timer); - sys_sleep(delay); - sys_elapsed(timer); -*/ - } doevents(); vid_begin(); diff --git a/apps/plugins/rockboy/menu.c b/apps/plugins/rockboy/menu.c index 803310f70c..0c9e46bbdf 100644 --- a/apps/plugins/rockboy/menu.c +++ b/apps/plugins/rockboy/menu.c @@ -8,6 +8,7 @@ #include "rockmacros.h" #include "mem.h" #include "lib/oldmenuapi.h" +#include "rtc-gb.h" #if (CONFIG_KEYPAD == IPOD_4G_PAD) #define MENU_BUTTON_UP BUTTON_SCROLL_BACK @@ -84,6 +85,11 @@ int do_user_menu(void) { bool done=false; int m, ret=0; int result; + int time = 0; + +#if CONFIG_RTC + time = rb->mktime(rb->get_time()); +#endif /* Clean out the button Queue */ while (rb->button_get(false) != BUTTON_NONE) @@ -129,6 +135,12 @@ int do_user_menu(void) { rb->lcd_setfont(0); /* Reset the font */ rb->lcd_clear_display(); /* Clear display for screen size changes */ + + /* Keep the RTC in sync */ +#if CONFIG_RTC + time = (rb->mktime(rb->get_time()) - time) * 60; +#endif + while (time-- > 0) rtc_tick(); return ret; } diff --git a/apps/plugins/rockboy/rtc.c b/apps/plugins/rockboy/rtc.c index 0b011a828c..17b2853fce 100644 --- a/apps/plugins/rockboy/rtc.c +++ b/apps/plugins/rockboy/rtc.c @@ -7,6 +7,7 @@ #include "defs.h" #include "mem.h" #include "rtc-gb.h" +#include "sscanf.h" struct rtc rtc; @@ -85,31 +86,37 @@ void rtc_tick() void rtc_save_internal(int fd) { - (void)fd; /* stop compiler complaining */ - /* TODO */ - /* fprintf(f, "%d %d %d %02d %02d %02d %02d\n%d\n", - rtc.carry, rtc.stop, rtc.d, rtc.h, rtc.m, rtc.s, rtc.t, - time(0)); */ + int rt = 0; + +#if CONFIG_RTC + rt = rb->mktime(rb->get_time()); +#endif + fdprintf(fd, "%d %d %d %d %d %d %d %d\n", rtc.carry, rtc.stop, rtc.d, rtc.h, + rtc.m, rtc.s, rtc.t, rt); } void rtc_load_internal(int fd) { - /* int rt = 0; */ - (void)fd; /* stop compiler complaining */ - /* TODO */ - /* fscanf( - f, "%d %d %d %02d %02d %02d %02d\n%d\n", - &rtc.carry, &rtc.stop, &rtc.d, - &rtc.h, &rtc.m, &rtc.s, &rtc.t, &rt); - while (rtc.t >= 60) rtc.t -= 60; - while (rtc.s >= 60) rtc.s -= 60; - while (rtc.m >= 60) rtc.m -= 60; - while (rtc.h >= 24) rtc.h -= 24; - while (rtc.d >= 365) rtc.d -= 365; - rtc.stop &= 1; - rtc.carry &= 1; - if (rt) rt = (time(0) - rt) * 60; - if (syncrtc) while (rt-- > 0) rtc_tick(); */ + int rt = 0; + char buf[32]; + + rb->read_line(fd, buf, sizeof(buf)); + sscanf(buf, "%d %d %d %d %d %d %d %d\n", &rtc.carry, &rtc.stop, &rtc.d, + &rtc.h, &rtc.m, &rtc.s, &rtc.t, &rt); + + while (rtc.t >= 60) rtc.t -= 60; + while (rtc.s >= 60) rtc.s -= 60; + while (rtc.m >= 60) rtc.m -= 60; + while (rtc.h >= 24) rtc.h -= 24; + while (rtc.d >= 365) rtc.d -= 365; + rtc.stop &= 1; + rtc.carry &= 1; + +#if CONFIG_RTC + if (rt) rt = (rb->mktime(rb->get_time()) - rt) * 60; +#endif + while (rt-- > 0) rtc_tick(); + } diff --git a/apps/plugins/rockboy/sys_rockbox.c b/apps/plugins/rockboy/sys_rockbox.c index 60cac0a88d..fe3326737d 100644 --- a/apps/plugins/rockboy/sys_rockbox.c +++ b/apps/plugins/rockboy/sys_rockbox.c @@ -328,23 +328,3 @@ void vid_update(int scanline) } #endif -long timerresult; - -void *sys_timer(void) -{ - /*timerresult=*rb->current_tick; - return &timerresult;*/ - return 0; -} - -/* returns microseconds passed since sys_timer */ -int sys_elapsed(long *oldtick) -{ - /* int elap,mytime=microtick; - - elap=mytime-*oldtick; - *oldtick=mytime; - return elap; */ - /* return ((*rb->current_tick-(*oldtick))*1000000)/HZ; */ - return *oldtick; -}