refresh.h: fix build with asm core enabled
[rofl0r-gnuboy.git] / rtc.c
blobc3785ec21dcb2170b5086dcf6e8bc1f2e27c5d4c
4 #include <stdio.h>
5 #include <time.h>
7 #include "defs.h"
8 #include "mem.h"
9 #include "rtc.h"
10 #include "rc.h"
12 struct rtc rtc;
14 static int syncrtc = 1;
16 rcvar_t rtc_exports[] =
18 RCV_BOOL("syncrtc", &syncrtc, "resync RTC for elapsed time on load"),
19 RCV_END
23 void rtc_latch(byte b)
25 if ((rtc.latch ^ b) & b & 1)
27 rtc.regs[0] = rtc.s;
28 rtc.regs[1] = rtc.m;
29 rtc.regs[2] = rtc.h;
30 rtc.regs[3] = rtc.d;
31 rtc.regs[4] = (rtc.d>>9) | (rtc.stop<<6) | (rtc.carry<<7);
32 rtc.regs[5] = 0xff;
33 rtc.regs[6] = 0xff;
34 rtc.regs[7] = 0xff;
36 rtc.latch = b;
39 void rtc_write(byte b)
41 /* printf("write %02X: %02X (%d)\n", rtc.sel, b, b); */
42 if (!(rtc.sel & 8)) return;
43 switch (rtc.sel & 7)
45 case 0:
46 rtc.s = rtc.regs[0] = b;
47 while (rtc.s >= 60) rtc.s -= 60;
48 break;
49 case 1:
50 rtc.m = rtc.regs[1] = b;
51 while (rtc.m >= 60) rtc.m -= 60;
52 break;
53 case 2:
54 rtc.h = rtc.regs[2] = b;
55 while (rtc.h >= 24) rtc.h -= 24;
56 break;
57 case 3:
58 rtc.regs[3] = b;
59 rtc.d = (rtc.d & 0x100) | b;
60 break;
61 case 4:
62 rtc.regs[4] = b;
63 rtc.d = (rtc.d & 0xff) | ((b&1)<<9);
64 rtc.stop = (b>>6)&1;
65 rtc.carry = (b>>7)&1;
66 break;
70 void rtc_tick()
72 if (rtc.stop) return;
73 if (++rtc.t == 60)
75 if (++rtc.s == 60)
77 if (++rtc.m == 60)
79 if (++rtc.h == 24)
81 if (++rtc.d == 365)
83 rtc.d = 0;
84 rtc.carry = 1;
86 rtc.h = 0;
88 rtc.m = 0;
90 rtc.s = 0;
92 rtc.t = 0;
96 void rtc_save_internal(FILE *f)
98 fprintf(f, "%d %d %d %02d %02d %02d %02d\n%ld\n",
99 rtc.carry, rtc.stop, rtc.d, rtc.h, rtc.m, rtc.s, rtc.t,
100 (long) time(0));
103 void rtc_load_internal(FILE *f)
105 int rt = 0;
106 fscanf(
107 f, "%d %d %d %02d %02d %02d %02d\n%d\n",
108 &rtc.carry, &rtc.stop, &rtc.d,
109 &rtc.h, &rtc.m, &rtc.s, &rtc.t, &rt);
110 while (rtc.t >= 60) rtc.t -= 60;
111 while (rtc.s >= 60) rtc.s -= 60;
112 while (rtc.m >= 60) rtc.m -= 60;
113 while (rtc.h >= 24) rtc.h -= 24;
114 while (rtc.d >= 365) rtc.d -= 365;
115 rtc.stop &= 1;
116 rtc.carry &= 1;
117 if (rt) rt = (time(0) - rt) * 60;
118 if (syncrtc) while (rt-- > 0) rtc_tick();