6 static unsigned char rtc_mystery_reg[3];
7 unsigned char ricoh_memory[0x0F];
8 unsigned char ricoh_alarm[0x0F];
10 void put_rtc_byte(uint32_t address_, uint8_t value_, uint8_t rtc_type) {
11 uint32_t address = address_ & 0x3F;
12 uint8_t value = (value_ & 0x0F);
16 //printf("Wrote byte %.2X.\n", address);
18 if (rtc_type == RTC_TYPE_MSM) {
21 rtc_mystery_reg[address - 0x0D] = value & (0x01 | 0x08);
25 rtc_mystery_reg[address - 0x0D] = value;
32 int rtc_bank = (rtc_mystery_reg[0] & 0x03);
33 if ((rtc_bank & 0x02) && address < 0x0D) {
34 if (rtc_bank & 0x01) {
35 // Low nibble of value -> high nibble in RTC memory
36 ricoh_memory[address] &= 0x0F;
37 ricoh_memory[address] |= ((value & 0x0F) << 4);
40 // Low nibble of value -> low nibble in RTC memory
41 ricoh_memory[address] &= 0xF0;
42 ricoh_memory[address] |= (value & 0x0F);
46 else if ((rtc_bank & 0x01) && address < 0x0D) {
47 // RTC alarm stuff, no idea what this is supposed to be for.
53 ricoh_alarm[address] = 0;
57 ricoh_alarm[address] &= (value & (0x08 ^ 0xFF));
62 ricoh_alarm[address] = (value & (0x0C ^ 0xFF));
65 ricoh_alarm[address] = (value & (0x0E ^ 0xFF));
68 ricoh_alarm[address] = value;
71 //printf("Write to Ricoh alarm @%.2X: %.2X -> %.2X\n", address, value, ricoh_alarm[address]);
74 else if (address >= 0x0D) {
75 rtc_mystery_reg[address - 0x0D] = value;
81 uint8_t get_rtc_byte(uint32_t address_, uint8_t rtc_type) {
82 uint32_t address = address_ & 0x3F;
84 if ((address & 3) == 2 || (address & 3) == 0) {
85 //printf("Garbage byte read.\n");
92 struct tm *rtc_time = localtime(&t);
94 if (rtc_type == RTC_TYPE_RICOH) {
95 int rtc_bank = (rtc_mystery_reg[0] & 0x03);
96 if ((rtc_bank & 0x02) && address < 0x0D) {
97 // Get low/high nibble from memory (bank 2/3)
98 return ((ricoh_memory[address] >> (rtc_bank & 0x01) ? 4 : 0) & 0x0F);
100 else if ((rtc_bank & 0x01) && address < 0x0D) {
101 // Get byte from alarm
102 return ricoh_alarm[address];
106 //printf("Read byte %.2X.\n", address);
109 case 0x00: // Seconds low?
110 return rtc_time->tm_sec % 10;
111 case 0x01: // Seconds high?
112 return rtc_time->tm_sec / 10;
113 case 0x02: // Minutes low?
114 return rtc_time->tm_min % 10;
115 case 0x03: // Minutes high?
116 return rtc_time->tm_min / 10;
117 case 0x04: // Hours low?
118 return rtc_time->tm_hour % 10;
119 case 0x05: // Hours high?
120 if (rtc_type == RTC_TYPE_MSM) {
121 if (rtc_mystery_reg[2] & 4) {
122 return (((rtc_time->tm_hour % 12) / 10) | (rtc_time->tm_hour >= 12) ? 0x04 : 0x00);
125 return rtc_time->tm_hour / 10;
128 if (ricoh_alarm[10] & 0x01) {
129 return rtc_time->tm_hour / 10;
132 return (((rtc_time->tm_hour % 12) / 10) | (rtc_time->tm_hour >= 12) ? 0x02 : 0x00);
136 case 0x06: // Day low?
137 if (rtc_type == RTC_TYPE_MSM)
138 return rtc_time->tm_mday % 10;
140 return rtc_time->tm_wday;
141 case 0x07: // Day high?
142 if (rtc_type == RTC_TYPE_MSM)
143 return rtc_time->tm_mday / 10;
145 return rtc_time->tm_mday % 10;
146 case 0x08: // Month low?
147 if (rtc_type == RTC_TYPE_MSM)
148 return (rtc_time->tm_mon + 1) % 10;
150 return rtc_time->tm_mday / 10;
151 case 0x09: // Month high?
152 if (rtc_type == RTC_TYPE_MSM)
153 return (rtc_time->tm_mon + 1) / 10;
155 return (rtc_time->tm_mon + 1) % 10;
156 case 0x0A: // Year low?
157 if (rtc_type == RTC_TYPE_MSM)
158 return rtc_time->tm_year % 10;
160 return (rtc_time->tm_mon + 1) / 10;
161 case 0x0B: // Year high?
162 if (rtc_type == RTC_TYPE_MSM)
163 return rtc_time->tm_year / 10;
165 return rtc_time->tm_year % 10;
166 case 0x0C: // Day of week?
167 if (rtc_type == RTC_TYPE_MSM)
168 return rtc_time->tm_wday;
170 return rtc_time->tm_year / 10;
171 case 0x0D: // Mystery register D-F?
174 if (rtc_type == RTC_TYPE_MSM) {
175 return rtc_mystery_reg[address - 0x0D];
178 if (address == 0x0D) return rtc_mystery_reg[address - 0x0D];