2 * RGB2R-C128-Kassenprogramm
3 * © 2007-2009 phil_fry, sECuRE, sur5r
4 * See LICENSE for license information
17 char *get_time(void) {
18 static char buffer[9];
19 uint8_t bcd_hour, hour, bcd_min, bcd_sec, tenth;
21 /* Read the hour register first to stop the clock from updating the external
22 * registers from the internal (still ticking!) CIA registers. */
24 bcd_hour = CIA1.tod_hour;
26 /* if high bit is set, it is pm */
27 if (bcd_hour & 0x80) {
28 hour = bcd2dec(bcd_hour ^ 0x80);
29 /* adjust for 24h clock, 12:??pm is still 12:?? */
34 hour = bcd2dec(bcd_hour);
37 bcd_sec = CIA1.tod_sec;
38 bcd_min = CIA1.tod_min;
40 /* MUST read tod_10 to enable the clock latch again */
43 sprintf(buffer, "%02d:%02x:%02x", hour, bcd_min, bcd_sec);
47 /* divide by 10; put quotient in high nibble, reminder in low nibble */
48 uint8_t dec2bcd(uint8_t dec) { return (((dec / 10) << 4) | (dec % 10)); }
50 void set_time(uint8_t hour, uint8_t min, uint8_t sec) {
53 /* CIA TOD will always flip the pm bit
54 * when either 0 or 12 is written to the hour register */
56 /* bcd 12 with high bit (pm) set */
58 } else if (hour > 12) {
59 /* convert 24h clock to 12h with pm bit set */
60 bcd_hour = dec2bcd(hour - 12);
61 bcd_hour = bcd_hour ^ 0x80;
63 /* includes 12pm since the bit gets automatically flipped */
64 bcd_hour = dec2bcd(hour);
67 CIA1.tod_hour = bcd_hour;
68 CIA1.tod_min = dec2bcd(min);
69 CIA1.tod_sec = dec2bcd(sec);
71 /* set CIA1.tod_10 and program "Control Timer A" */
72 __asm__("jsr initsystime");