]> git.sur5r.net Git - c128-kasse/blob - src/c128time.c
update the clock on the main menu via an irq handler
[c128-kasse] / src / c128time.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #include <peekpoke.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <c128.h>
11 #include <stdint.h>
12 #include <6502.h>
13 #include <conio.h>
14
15 #include "bcd2dec.h"
16 #include "general.h"
17 #include "globals.h"
18
19 /* This file uses the CIA TOD (Complex Interface Adapter, Time of Day)
20  * for timekeeping, see https://www.c64-wiki.com/wiki/CIA and its Links section
21  * for a bit more information */
22
23 /* the Time of Day PM bit is set for hours >= 12 */
24 #define TOD_PM 0x80
25
26 #define DAYTIME_IRQ_STACK_SIZE 32
27 uint8_t daytime_irq_stack[DAYTIME_IRQ_STACK_SIZE];
28
29 void update_time(void) {
30   volatile static uint8_t bcd_hour, hour, min, sec, tenth;
31
32   /* Read the hour register first to stop the clock from updating the external
33    * registers from the internal (still ticking!) CIA registers. */
34
35   bcd_hour = CIA1.tod_hour;
36
37   /* hour is >= 12 if TOD_PM is set */
38   if (bcd_hour & TOD_PM) {
39     hour = bcd2dec(bcd_hour ^ TOD_PM);
40     /* adjust for 24h clock, 12:??pm should still be 12:?? */
41     if (hour != 12) {
42       hour += 12;
43     }
44   } else {
45     hour = bcd2dec(bcd_hour);
46   }
47
48   sec = bcd2dec(CIA1.tod_sec);
49   min = bcd2dec(CIA1.tod_min);
50
51   /* MUST read tod_10 to enable the clock latch again */
52   tenth = CIA1.tod_10;
53
54   /* it's a new day when hour wraps */
55   if (daytime.hour > hour) {
56     daytime.day++;
57   }
58
59   daytime.hour = hour;
60   daytime.min = min;
61   daytime.sec = sec;
62 }
63
64 char *get_time(void) {
65   static char buffer[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
66   update_time();
67   sprintf(buffer, "%02d:%02d:%02d", daytime.hour, daytime.min, daytime.sec);
68   return buffer;
69 }
70
71 /* divide by 10; put quotient in high nibble, remainder in low nibble */
72 uint8_t dec2bcd(const uint8_t dec) { return (((dec / 10) << 4) | (dec % 10)); }
73
74 void set_time(uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
75   uint8_t bcd_hour;
76
77   /* CIA TOD will always flip the PM bit
78    * when we either want to write the 0th or 12th hour.
79    * So we need to write the hour with the PM bit inverted,
80    * for the CIA to flip it again */
81   if (hour == 0) {
82     /* the 0th hour is 12am in 12h clock format, 0x12 is BCD for 12 */
83     bcd_hour = 0x12 ^ TOD_PM;
84   } else if (hour > 12) {
85     /* convert 24h clock to 12h with PM bit set */
86     bcd_hour = dec2bcd(hour - 12);
87     bcd_hour = bcd_hour ^ TOD_PM;
88   } else {
89     /* includes 12pm since the PM bit gets automatically flipped */
90     bcd_hour = dec2bcd(hour);
91   }
92
93   daytime.day = day;
94   daytime.hour = hour;
95   daytime.min = min;
96   daytime.sec = sec;
97
98   CIA1.tod_hour = bcd_hour;
99   CIA1.tod_min = dec2bcd(min);
100   CIA1.tod_sec = dec2bcd(sec);
101
102   /* set CIA1.tod_10 and program "Control Timer A" */
103   __asm__("jsr initsystime");
104 }
105
106 uint8_t _daytime_irq(void) {
107   static char *t;
108   static uint8_t x, y;
109   /* We are called 60 times a second. We only want to draw a clock
110    * when we are a) on the mainscreen and b) the seconds changed */
111   if (kasse_menu == MENU_MAIN && CIA1.tod_sec != daytime.sec) {
112     t = get_time();
113     x = wherex();
114     y = wherey();
115     cputsxy(70, 3, t);
116     gotoxy(x, y);
117   }
118   /* always call additional handlers */
119   return (IRQ_NOT_HANDLED);
120 }
121
122 void install_daytime_irq(void) {
123   SEI();
124   set_irq(&_daytime_irq, daytime_irq_stack, DAYTIME_IRQ_STACK_SIZE);
125   CLI();
126 }