2 * RGB2R-C128-Kassenprogramm
3 * © 2007-2009 phil_fry, sECuRE, sur5r
4 * See LICENSE for license information
17 void update_time(void) {
18 uint8_t bcd_hour, hour, min, sec, tenth;
20 /* Read the hour register first to stop the clock from updating the external
21 * registers from the internal (still ticking!) CIA registers. */
23 bcd_hour = CIA1.tod_hour;
25 /* if high bit is set, it is pm */
26 if (bcd_hour & 0x80) {
27 hour = bcd2dec(bcd_hour ^ 0x80);
28 /* adjust for 24h clock, 12:??pm is still 12:?? */
33 hour = bcd2dec(bcd_hour);
36 sec = bcd2dec(CIA1.tod_sec);
37 min = bcd2dec(CIA1.tod_min);
39 /* MUST read tod_10 to enable the clock latch again */
42 if (daytime.hour > hour) {
51 char *get_time(void) {
52 static char buffer[9];
54 sprintf(buffer, "%02d:%02d:%02d", daytime.hour, daytime.min, daytime.sec);
58 /* divide by 10; put quotient in high nibble, reminder in low nibble */
59 uint8_t dec2bcd(uint8_t dec) { return (((dec / 10) << 4) | (dec % 10)); }
61 void set_time(uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
64 /* CIA TOD will always flip the pm bit
65 * when either 0 or 12 is written to the hour register */
67 /* bcd 12 with high bit (pm) set */
69 } else if (hour > 12) {
70 /* convert 24h clock to 12h with pm bit set */
71 bcd_hour = dec2bcd(hour - 12);
72 bcd_hour = bcd_hour ^ 0x80;
74 /* includes 12pm since the bit gets automatically flipped */
75 bcd_hour = dec2bcd(hour);
83 CIA1.tod_hour = bcd_hour;
84 CIA1.tod_min = dec2bcd(min);
85 CIA1.tod_sec = dec2bcd(sec);
87 /* set CIA1.tod_10 and program "Control Timer A" */
88 __asm__("jsr initsystime");