2005-03-02 23:49:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "rockmacros.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "mem.h"
|
2005-07-03 14:05:12 +00:00
|
|
|
#include "rtc-gb.h"
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
struct rtc rtc;
|
2005-03-02 23:49:38 +00:00
|
|
|
|
|
|
|
void rtc_latch(byte b)
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
if ((rtc.latch ^ b) & b & 1)
|
|
|
|
{
|
|
|
|
rtc.regs[0] = rtc.s;
|
|
|
|
rtc.regs[1] = rtc.m;
|
|
|
|
rtc.regs[2] = rtc.h;
|
|
|
|
rtc.regs[3] = rtc.d;
|
|
|
|
rtc.regs[4] = (rtc.d>>9) | (rtc.stop<<6) | (rtc.carry<<7);
|
|
|
|
rtc.regs[5] = 0xff;
|
|
|
|
rtc.regs[6] = 0xff;
|
|
|
|
rtc.regs[7] = 0xff;
|
|
|
|
}
|
|
|
|
rtc.latch = b;
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_write(byte b)
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
/* printf("write %02X: %02X (%d)\n", rtc.sel, b, b); */
|
|
|
|
if (!(rtc.sel & 8)) return;
|
|
|
|
switch (rtc.sel & 7)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
rtc.s = rtc.regs[0] = b;
|
|
|
|
while (rtc.s >= 60) rtc.s -= 60;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rtc.m = rtc.regs[1] = b;
|
|
|
|
while (rtc.m >= 60) rtc.m -= 60;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rtc.h = rtc.regs[2] = b;
|
|
|
|
while (rtc.h >= 24) rtc.h -= 24;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
rtc.regs[3] = b;
|
|
|
|
rtc.d = (rtc.d & 0x100) | b;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
rtc.regs[4] = b;
|
|
|
|
rtc.d = (rtc.d & 0xff) | ((b&1)<<9);
|
|
|
|
rtc.stop = (b>>6)&1;
|
|
|
|
rtc.carry = (b>>7)&1;
|
|
|
|
break;
|
|
|
|
}
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
void rtc_tick(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
if (rtc.stop) return;
|
|
|
|
if (++rtc.t == 60)
|
|
|
|
{
|
|
|
|
if (++rtc.s == 60)
|
|
|
|
{
|
|
|
|
if (++rtc.m == 60)
|
|
|
|
{
|
|
|
|
if (++rtc.h == 24)
|
|
|
|
{
|
|
|
|
if (++rtc.d == 365)
|
|
|
|
{
|
|
|
|
rtc.d = 0;
|
|
|
|
rtc.carry = 1;
|
|
|
|
}
|
|
|
|
rtc.h = 0;
|
|
|
|
}
|
|
|
|
rtc.m = 0;
|
|
|
|
}
|
|
|
|
rtc.s = 0;
|
|
|
|
}
|
|
|
|
rtc.t = 0;
|
|
|
|
}
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_save_internal(int fd)
|
|
|
|
{
|
2007-07-30 05:19:05 +00:00
|
|
|
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);
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_load_internal(int fd)
|
|
|
|
{
|
2007-07-30 05:19:05 +00:00
|
|
|
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();
|
|
|
|
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|