the fm presets list should wrap.. so make that work
[kugel-rb.git] / apps / plugins / rockboy / rtc.c
blob248ebc0714808d5d658ac2f33ca3cc456973d656
4 #include "rockmacros.h"
5 #include <stdio.h>
7 #include "defs.h"
8 #include "mem.h"
9 #include "rtc-gb.h"
11 struct rtc rtc;
13 void rtc_latch(byte b)
15 if ((rtc.latch ^ b) & b & 1)
17 rtc.regs[0] = rtc.s;
18 rtc.regs[1] = rtc.m;
19 rtc.regs[2] = rtc.h;
20 rtc.regs[3] = rtc.d;
21 rtc.regs[4] = (rtc.d>>9) | (rtc.stop<<6) | (rtc.carry<<7);
22 rtc.regs[5] = 0xff;
23 rtc.regs[6] = 0xff;
24 rtc.regs[7] = 0xff;
26 rtc.latch = b;
29 void rtc_write(byte b)
31 /* printf("write %02X: %02X (%d)\n", rtc.sel, b, b); */
32 if (!(rtc.sel & 8)) return;
33 switch (rtc.sel & 7)
35 case 0:
36 rtc.s = rtc.regs[0] = b;
37 while (rtc.s >= 60) rtc.s -= 60;
38 break;
39 case 1:
40 rtc.m = rtc.regs[1] = b;
41 while (rtc.m >= 60) rtc.m -= 60;
42 break;
43 case 2:
44 rtc.h = rtc.regs[2] = b;
45 while (rtc.h >= 24) rtc.h -= 24;
46 break;
47 case 3:
48 rtc.regs[3] = b;
49 rtc.d = (rtc.d & 0x100) | b;
50 break;
51 case 4:
52 rtc.regs[4] = b;
53 rtc.d = (rtc.d & 0xff) | ((b&1)<<9);
54 rtc.stop = (b>>6)&1;
55 rtc.carry = (b>>7)&1;
56 break;
60 void rtc_tick(void)
62 if (rtc.stop) return;
63 if (++rtc.t == 60)
65 if (++rtc.s == 60)
67 if (++rtc.m == 60)
69 if (++rtc.h == 24)
71 if (++rtc.d == 365)
73 rtc.d = 0;
74 rtc.carry = 1;
76 rtc.h = 0;
78 rtc.m = 0;
80 rtc.s = 0;
82 rtc.t = 0;
86 void rtc_save_internal(int fd)
88 int rt = 0;
90 #if CONFIG_RTC
91 rt = rb->mktime(rb->get_time());
92 #endif
93 fdprintf(fd, "%d %d %d %d %d %d %d %d\n", rtc.carry, rtc.stop, rtc.d, rtc.h,
94 rtc.m, rtc.s, rtc.t, rt);
97 void rtc_load_internal(int fd)
99 int rt = 0;
100 char buf[32];
102 rb->read_line(fd, buf, sizeof(buf));
103 sscanf(buf, "%d %d %d %d %d %d %d %d\n", &rtc.carry, &rtc.stop, &rtc.d,
104 &rtc.h, &rtc.m, &rtc.s, &rtc.t, &rt);
106 while (rtc.t >= 60) rtc.t -= 60;
107 while (rtc.s >= 60) rtc.s -= 60;
108 while (rtc.m >= 60) rtc.m -= 60;
109 while (rtc.h >= 24) rtc.h -= 24;
110 while (rtc.d >= 365) rtc.d -= 365;
111 rtc.stop &= 1;
112 rtc.carry &= 1;
114 #if CONFIG_RTC
115 if (rt) rt = (rb->mktime(rb->get_time()) - rt) * 60;
116 #endif
117 while (rt-- > 0) rtc_tick();