src/mess/machine/rtc65271.c
| r1119 | r1120 | |
| 1 | | /* |
| 2 | | rtc65271 emulation |
| 3 | | |
| 4 | | This chip is an RTC for computer built by Epson and Spezial-Electronic (I |
| 5 | | think SE is the second source here). |
| 6 | | |
| 7 | | Reference: |
| 8 | | * Realtime Clock Module RTC-65271 Application Manual |
| 9 | | <http://www.bgmicro.com/pdf/rtc65271.pdf> |
| 10 | | |
| 11 | | Todo: |
| 12 | | * Support square wave pin output? |
| 13 | | * Support DSE mode? |
| 14 | | |
| 15 | | Raphael Nabet, 2003-2004 |
| 16 | | */ |
| 17 | | |
| 18 | | #include <math.h> |
| 19 | | #include "mame.h" |
| 20 | | #include "rtc65271.h" |
| 21 | | |
| 22 | | static void field_interrupts(void); |
| 23 | | static TIMER_CALLBACK(rtc_SQW_callback); |
| 24 | | static TIMER_CALLBACK(rtc_begin_update_callback); |
| 25 | | static TIMER_CALLBACK(rtc_end_update_callback); |
| 26 | | |
| 27 | | /* Delay between the beginning (UIP asserted) and the end (UIP cleared and |
| 28 | | update interrupt asserted) of the update cycle */ |
| 29 | | #define UPDATE_CYCLE_TIME MAME_TIME_IN_USEC(1984) |
| 30 | | /* Delay between the assertion of UIP and the effective start of the update |
| 31 | | cycle */ |
| 32 | | /*#define UPDATE_CYCLE_DELAY MAME_TIME_IN_USEC(244)*/ |
| 33 | | |
| 34 | | static struct |
| 35 | | { |
| 36 | | /* 64 8-bit registers (10 clock registers, 4 control/status registers, and |
| 37 | | 50 bytes of user RAM) */ |
| 38 | | UINT8 regs[64]; |
| 39 | | int cur_reg; |
| 40 | | |
| 41 | | /* extended RAM: 4kbytes of battery-backed RAM (in pages of 32 bytes) */ |
| 42 | | UINT8 *xram; |
| 43 | | int cur_xram_page; |
| 44 | | |
| 45 | | /* update timer: called every second */ |
| 46 | | void *update_timer; |
| 47 | | |
| 48 | | /* SQW timer: called every periodic clock half-period */ |
| 49 | | void *SQW_timer; |
| 50 | | int SQW_internal_state; |
| 51 | | |
| 52 | | /* callback called when interrupt pin state changes (may be NULL) */ |
| 53 | | void (*interrupt_callback)(int state); |
| 54 | | } rtc; |
| 55 | | |
| 56 | | enum |
| 57 | | { |
| 58 | | reg_second = 0, |
| 59 | | reg_alarm_second, |
| 60 | | reg_minute, |
| 61 | | reg_alarm_minute, |
| 62 | | reg_hour, |
| 63 | | reg_alarm_hour, |
| 64 | | reg_weekday, |
| 65 | | reg_monthday, |
| 66 | | reg_month, |
| 67 | | reg_year, |
| 68 | | reg_A, |
| 69 | | reg_B, |
| 70 | | reg_C, |
| 71 | | reg_D |
| 72 | | }; |
| 73 | | |
| 74 | | enum |
| 75 | | { |
| 76 | | reg_A_UIP = 0x80, |
| 77 | | reg_A_DV = 0x70, |
| 78 | | reg_A_RS = 0x0F, |
| 79 | | |
| 80 | | reg_B_SET = 0x80, |
| 81 | | reg_B_PIE = 0x40, |
| 82 | | reg_B_AIE = 0x20, |
| 83 | | reg_B_UIE = 0x10, |
| 84 | | reg_B_SQW = 0x08, |
| 85 | | reg_B_DM = 0x04, |
| 86 | | reg_B_24h = 0x02, |
| 87 | | reg_B_DSE = 0x01, |
| 88 | | |
| 89 | | reg_C_IRQF = 0x80, |
| 90 | | reg_C_PF = 0x40, |
| 91 | | reg_C_AF = 0x20, |
| 92 | | reg_C_UF = 0x10, |
| 93 | | |
| 94 | | reg_D_VRT = 0x80 |
| 95 | | }; |
| 96 | | |
| 97 | | static mame_time SQW_period_table(int val) |
| 98 | | { |
| 99 | | switch(val) |
| 100 | | { |
| 101 | | case 0: return time_zero; |
| 102 | | case 1: return MAME_TIME_IN_HZ(256); |
| 103 | | case 2: return MAME_TIME_IN_HZ(128); |
| 104 | | case 3: return MAME_TIME_IN_HZ(8192); |
| 105 | | case 4: return MAME_TIME_IN_HZ(4096); |
| 106 | | case 5: return MAME_TIME_IN_HZ(2048); |
| 107 | | case 6: return MAME_TIME_IN_HZ(1024); |
| 108 | | case 7: return MAME_TIME_IN_HZ(512); |
| 109 | | case 8: return MAME_TIME_IN_HZ(256); |
| 110 | | case 9: return MAME_TIME_IN_HZ(128); |
| 111 | | case 10: return MAME_TIME_IN_HZ(64); |
| 112 | | case 11: return MAME_TIME_IN_HZ(32); |
| 113 | | case 12: return MAME_TIME_IN_HZ(16); |
| 114 | | case 13: return MAME_TIME_IN_HZ(8); |
| 115 | | case 14: return MAME_TIME_IN_HZ(4); |
| 116 | | case 15: return MAME_TIME_IN_HZ(2); |
| 117 | | default: fatalerror("out of range"); |
| 118 | | } |
| 119 | | return time_zero; |
| 120 | | } |
| 121 | | |
| 122 | | |
| 123 | | /* |
| 124 | | BCD utilities |
| 125 | | */ |
| 126 | | |
| 127 | | /* |
| 128 | | Increment a binary-encoded UINT8 |
| 129 | | */ |
| 130 | | static UINT8 increment_binary(UINT8 data) |
| 131 | | { |
| 132 | | return data+1; |
| 133 | | } |
| 134 | | |
| 135 | | |
| 136 | | /* |
| 137 | | Increment a BCD-encoded UINT8 |
| 138 | | */ |
| 139 | | static UINT8 increment_BCD(UINT8 data) |
| 140 | | { |
| 141 | | if ((data & 0x0f) < 0x09) |
| 142 | | { |
| 143 | | if ((data & 0xf0) < 0xa0) |
| 144 | | data++; |
| 145 | | else |
| 146 | | data = data + 0x01 - 0xa0; |
| 147 | | } |
| 148 | | else |
| 149 | | { |
| 150 | | if ((data & 0xf0) < 0xa0) |
| 151 | | data = data - 0x09 + 0x10; |
| 152 | | else |
| 153 | | data = data - 0x09 - 0x90; |
| 154 | | } |
| 155 | | return data; |
| 156 | | } |
| 157 | | |
| 158 | | |
| 159 | | /* |
| 160 | | Convert a binary-encoded UINT8 to BCD |
| 161 | | */ |
| 162 | | static UINT8 binary_to_BCD(UINT8 data) |
| 163 | | { |
| 164 | | data %= 100; |
| 165 | | |
| 166 | | return ((data / 10) << 4) | (data %10); |
| 167 | | } |
| 168 | | |
| 169 | | |
| 170 | | /* |
| 171 | | Convert a BCD-encoded UINT8 to binary |
| 172 | | */ |
| 173 | | static UINT8 BCD_to_binary(UINT8 data) |
| 174 | | { |
| 175 | | if ((data & 0x0f) >= 0x0a) |
| 176 | | data = data - 0x0a + 0x10; |
| 177 | | if ((data & 0xf0) >= 0xa0) |
| 178 | | data = data - 0xa0; |
| 179 | | |
| 180 | | return (data & 0x0f) + (((data & 0xf0) >> 4) * 10); |
| 181 | | } |
| 182 | | |
| 183 | | |
| 184 | | /* |
| 185 | | Public functions |
| 186 | | */ |
| 187 | | |
| 188 | | |
| 189 | | /* |
| 190 | | load the SRAM and register contents from file |
| 191 | | */ |
| 192 | | int rtc65271_file_load(mame_file *file) |
| 193 | | { |
| 194 | | UINT8 buf; |
| 195 | | |
| 196 | | |
| 197 | | /* version flag */ |
| 198 | | if (mame_fread(file, & buf, 1) != 1) |
| 199 | | return 1; |
| 200 | | if (buf != 0) |
| 201 | | return 1; |
| 202 | | |
| 203 | | /* control registers */ |
| 204 | | if (mame_fread(file, &buf, 1) != 1) |
| 205 | | return 1; |
| 206 | | rtc.regs[reg_A] = buf & (reg_A_DV /*| reg_A_RS*/); |
| 207 | | if (mame_fread(file, &buf, 1) != 1) |
| 208 | | return 1; |
| 209 | | rtc.regs[reg_B] = buf & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE); |
| 210 | | |
| 211 | | /* alarm registers */ |
| 212 | | if (mame_fread(file, &rtc.regs[reg_alarm_second], 1) != 1) |
| 213 | | return 1; |
| 214 | | if (mame_fread(file, &rtc.regs[reg_alarm_minute], 1) != 1) |
| 215 | | return 1; |
| 216 | | if (mame_fread(file, &rtc.regs[reg_alarm_hour], 1) != 1) |
| 217 | | return 1; |
| 218 | | |
| 219 | | /* user RAM */ |
| 220 | | if (mame_fread(file, rtc.regs+14, 50) != 50) |
| 221 | | return 1; |
| 222 | | |
| 223 | | /* extended RAM */ |
| 224 | | if (mame_fread(file, rtc.xram, 4096) != 4096) |
| 225 | | return 1; |
| 226 | | |
| 227 | | rtc.regs[reg_D] |= reg_D_VRT; /* the data was backed up successfully */ |
| 228 | | /*rtc.dirty = FALSE;*/ |
| 229 | | |
| 230 | | { |
| 231 | | mame_system_time systime; |
| 232 | | |
| 233 | | /* get the current date/time from the core */ |
| 234 | | mame_get_current_datetime(Machine, &systime); |
| 235 | | |
| 236 | | /* set clock registers */ |
| 237 | | rtc.regs[reg_second] = systime.local_time.second; |
| 238 | | rtc.regs[reg_minute] = systime.local_time.minute; |
| 239 | | if (rtc.regs[reg_B] & reg_B_24h) |
| 240 | | /* 24-hour mode */ |
| 241 | | rtc.regs[reg_hour] = systime.local_time.hour; |
| 242 | | else |
| 243 | | { /* 12-hour mode */ |
| 244 | | if (systime.local_time.hour >= 12) |
| 245 | | { |
| 246 | | rtc.regs[reg_hour] = 0x80; |
| 247 | | systime.local_time.hour -= 12; |
| 248 | | } |
| 249 | | else |
| 250 | | rtc.regs[reg_hour] = 0; |
| 251 | | rtc.regs[reg_hour] |= systime.local_time.hour ? systime.local_time.hour : 12; |
| 252 | | } |
| 253 | | rtc.regs[reg_weekday] = systime.local_time.weekday + 1; |
| 254 | | rtc.regs[reg_monthday] = systime.local_time.mday; |
| 255 | | rtc.regs[reg_month] = systime.local_time.month + 1; |
| 256 | | rtc.regs[reg_year] = systime.local_time.year % 100; |
| 257 | | if (! (rtc.regs[reg_B] & reg_B_DM)) |
| 258 | | { /* BCD mode */ |
| 259 | | rtc.regs[reg_second] = binary_to_BCD(rtc.regs[reg_second]); |
| 260 | | rtc.regs[reg_minute] = binary_to_BCD(rtc.regs[reg_minute]); |
| 261 | | rtc.regs[reg_hour] = (rtc.regs[reg_hour] & 0x80) | binary_to_BCD(rtc.regs[reg_hour] & 0x7f); |
| 262 | | /*rtc.regs[reg_weekday] = binary_to_BCD(rtc.regs[reg_weekday]);*/ |
| 263 | | rtc.regs[reg_monthday] = binary_to_BCD(rtc.regs[reg_monthday]); |
| 264 | | rtc.regs[reg_month] = binary_to_BCD(rtc.regs[reg_month]); |
| 265 | | rtc.regs[reg_year] = binary_to_BCD(rtc.regs[reg_year]); |
| 266 | | } |
| 267 | | } |
| 268 | | |
| 269 | | return 0; |
| 270 | | } |
| 271 | | |
| 272 | | /* |
| 273 | | save the SRAM and register contents to file |
| 274 | | */ |
| 275 | | int rtc65271_file_save(mame_file *file) |
| 276 | | { |
| 277 | | UINT8 buf; |
| 278 | | |
| 279 | | |
| 280 | | /* version flag */ |
| 281 | | buf = 0; |
| 282 | | if (mame_fwrite(file, & buf, 1) != 1) |
| 283 | | return 1; |
| 284 | | |
| 285 | | /* control registers */ |
| 286 | | buf = rtc.regs[reg_A] & (reg_A_DV | reg_A_RS); |
| 287 | | if (mame_fwrite(file, &buf, 1) != 1) |
| 288 | | return 1; |
| 289 | | buf = rtc.regs[reg_B] & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE); |
| 290 | | if (mame_fwrite(file, &buf, 1) != 1) |
| 291 | | return 1; |
| 292 | | |
| 293 | | /* alarm registers */ |
| 294 | | if (mame_fwrite(file, &rtc.regs[reg_alarm_second], 1) != 1) |
| 295 | | return 1; |
| 296 | | if (mame_fwrite(file, &rtc.regs[reg_alarm_minute], 1) != 1) |
| 297 | | return 1; |
| 298 | | if (mame_fwrite(file, &rtc.regs[reg_alarm_hour], 1) != 1) |
| 299 | | return 1; |
| 300 | | |
| 301 | | /* user RAM */ |
| 302 | | if (mame_fwrite(file, rtc.regs+14, 50) != 50) |
| 303 | | return 1; |
| 304 | | |
| 305 | | /* extended RAM */ |
| 306 | | if (mame_fwrite(file, rtc.xram, 4096) != 4096) |
| 307 | | return 1; |
| 308 | | |
| 309 | | return 0; |
| 310 | | } |
| 311 | | |
| 312 | | /* |
| 313 | | Initialize clock |
| 314 | | |
| 315 | | xram: pointer to 4kb RAM area |
| 316 | | interrupt_callback: callback called when interrupt pin state changes (may |
| 317 | | be NULL) |
| 318 | | */ |
| 319 | | void rtc65271_init(UINT8 *xram, void (*interrupt_callback)(int state)) |
| 320 | | { |
| 321 | | memset(&rtc, 0, sizeof(rtc)); |
| 322 | | |
| 323 | | rtc.xram = xram; |
| 324 | | |
| 325 | | rtc.update_timer = mame_timer_alloc(rtc_begin_update_callback); |
| 326 | | mame_timer_adjust(rtc.update_timer, MAME_TIME_IN_SEC(1), 0, MAME_TIME_IN_SEC(1)); |
| 327 | | rtc.SQW_timer = mame_timer_alloc(rtc_SQW_callback); |
| 328 | | rtc.interrupt_callback = interrupt_callback; |
| 329 | | } |
| 330 | | |
| 331 | | /* |
| 332 | | Read a byte from clock |
| 333 | | |
| 334 | | xramsel: select RTC register if 0, XRAM if 1 |
| 335 | | offset: address (A0-A5 pins) |
| 336 | | */ |
| 337 | | UINT8 rtc65271_r(int xramsel, offs_t offset) |
| 338 | | { |
| 339 | | int reply; |
| 340 | | |
| 341 | | if (xramsel) |
| 342 | | { |
| 343 | | if (offset & 0x20) |
| 344 | | /* XRAM page register */ |
| 345 | | reply = rtc.cur_xram_page; |
| 346 | | else |
| 347 | | /* XRAM data */ |
| 348 | | reply = rtc.xram[(offset & 0x1f) + 0x0020*rtc.cur_xram_page]; |
| 349 | | } |
| 350 | | else |
| 351 | | { |
| 352 | | if (offset & 0x01) |
| 353 | | /* data register */ |
| 354 | | switch (rtc.cur_reg) |
| 355 | | { |
| 356 | | case reg_C: |
| 357 | | reply = rtc.regs[rtc.cur_reg]; |
| 358 | | rtc.regs[rtc.cur_reg] = 0; |
| 359 | | field_interrupts(); |
| 360 | | break; |
| 361 | | case reg_D: |
| 362 | | reply = rtc.regs[rtc.cur_reg]; |
| 363 | | rtc.regs[rtc.cur_reg] = /*0*/reg_D_VRT; /* set VRT flag so that the computer does not complain that the battery is low */ |
| 364 | | break; |
| 365 | | |
| 366 | | default: |
| 367 | | reply = rtc.regs[rtc.cur_reg]; |
| 368 | | break; |
| 369 | | } |
| 370 | | else |
| 371 | | /* indirect address register */ |
| 372 | | reply = rtc.cur_reg; |
| 373 | | } |
| 374 | | |
| 375 | | return reply; |
| 376 | | } |
| 377 | | |
| 378 | | |
| 379 | | /* |
| 380 | | Read a byte from clock |
| 381 | | |
| 382 | | xramsel: select RTC register if 0, XRAM if 1 |
| 383 | | offset: address (A0-A5 pins) |
| 384 | | */ |
| 385 | | void rtc65271_w(int xramsel, offs_t offset, UINT8 data) |
| 386 | | { |
| 387 | | if (xramsel) |
| 388 | | { |
| 389 | | if (offset & 0x20) |
| 390 | | /* XRAM page register */ |
| 391 | | rtc.cur_xram_page = data & 0x7f; |
| 392 | | else |
| 393 | | /* XRAM data */ |
| 394 | | rtc.xram[(offset & 0x1f) + 0x0020*rtc.cur_xram_page] = data; |
| 395 | | } |
| 396 | | else |
| 397 | | { |
| 398 | | if (offset & 0x01) |
| 399 | | /* data register */ |
| 400 | | switch (rtc.cur_reg) |
| 401 | | { |
| 402 | | case reg_second: |
| 403 | | /* the data sheet says bit 7 is read-only. (I have no idea of |
| 404 | | the reason why it is.) */ |
| 405 | | rtc.regs[reg_second] = data & 0x7f; |
| 406 | | break; |
| 407 | | |
| 408 | | case reg_A: |
| 409 | | if ((data & reg_A_RS) != (rtc.regs[rtc.cur_reg] & reg_A_RS)) |
| 410 | | { |
| 411 | | if (data & reg_A_RS) |
| 412 | | { |
| 413 | | double period = mame_time_to_double(SQW_period_table(data & reg_A_RS)); |
| 414 | | double time_to_half_period; |
| 415 | | |
| 416 | | time_to_half_period = period*.5 - fmod(mame_time_to_double(mame_timer_timeelapsed(rtc.update_timer)), period); |
| 417 | | if (time_to_half_period > 0) |
| 418 | | { |
| 419 | | /* first half period */ |
| 420 | | rtc.SQW_internal_state = 1; /* or is it 0??? */ |
| 421 | | mame_timer_adjust(rtc.SQW_timer, double_to_mame_time(time_to_half_period), 0, double_to_mame_time(period*.5)); |
| 422 | | } |
| 423 | | else |
| 424 | | { |
| 425 | | /* second half period */ |
| 426 | | rtc.SQW_internal_state = 0; /* or is it 1??? */ |
| 427 | | mame_timer_adjust(rtc.SQW_timer, double_to_mame_time(time_to_half_period+period*.5), 0, double_to_mame_time(period*.5)); |
| 428 | | } |
| 429 | | } |
| 430 | | else |
| 431 | | { |
| 432 | | /*if (rtc.SQW_internal_state)*/ |
| 433 | | rtc.SQW_internal_state = 0; /* right??? */ |
| 434 | | |
| 435 | | /* Stop the divider used for SQW and periodic interrupts. */ |
| 436 | | mame_timer_adjust(rtc.SQW_timer, time_never, 0, time_zero); |
| 437 | | } |
| 438 | | } |
| 439 | | /* The UIP bit is read-only */ |
| 440 | | rtc.regs[reg_A] = (data & ~reg_A_UIP) | (rtc.regs[reg_A] & reg_A_UIP); |
| 441 | | break; |
| 442 | | |
| 443 | | case reg_B: |
| 444 | | rtc.regs[rtc.cur_reg] = data; |
| 445 | | if (data & reg_B_SET) |
| 446 | | { |
| 447 | | /* if we are in SET mode, clear update cycle */ |
| 448 | | rtc.regs[reg_A] &= ~reg_A_UIP; |
| 449 | | rtc.regs[reg_B] &= ~reg_B_UIE; /* the data sheet tells this, but I wonder how much sense it makes */ |
| 450 | | field_interrupts(); |
| 451 | | } |
| 452 | | break; |
| 453 | | |
| 454 | | case reg_C: |
| 455 | | case reg_D: |
| 456 | | break; |
| 457 | | |
| 458 | | default: |
| 459 | | rtc.regs[rtc.cur_reg] = data; |
| 460 | | break; |
| 461 | | } |
| 462 | | else |
| 463 | | /* indirect address register */ |
| 464 | | rtc.cur_reg = data & 0x3f; |
| 465 | | } |
| 466 | | } |
| 467 | | |
| 468 | | static void field_interrupts(void) |
| 469 | | { |
| 470 | | if (rtc.regs[reg_C] & rtc.regs[reg_B] & (reg_C_PF | reg_C_AF | reg_C_UF)) |
| 471 | | { |
| 472 | | rtc.regs[reg_C] |= reg_C_IRQF; |
| 473 | | if (rtc.interrupt_callback) |
| 474 | | rtc.interrupt_callback(1); |
| 475 | | } |
| 476 | | else |
| 477 | | { |
| 478 | | rtc.regs[reg_C] &= ~reg_C_IRQF; |
| 479 | | if (rtc.interrupt_callback) |
| 480 | | rtc.interrupt_callback(0); |
| 481 | | } |
| 482 | | } |
| 483 | | |
| 484 | | |
| 485 | | /* |
| 486 | | Timer handlers |
| 487 | | */ |
| 488 | | |
| 489 | | /* |
| 490 | | Update SQW output state each half-period and assert periodic interrupt each |
| 491 | | period. |
| 492 | | */ |
| 493 | | static TIMER_CALLBACK(rtc_SQW_callback) |
| 494 | | { |
| 495 | | rtc.SQW_internal_state = ! rtc.SQW_internal_state; |
| 496 | | if (! rtc.SQW_internal_state) |
| 497 | | { |
| 498 | | /* high-to-low??? transition -> interrupt (or should it be low-to-high?) */ |
| 499 | | rtc.regs[reg_C] |= reg_C_PF; |
| 500 | | field_interrupts(); |
| 501 | | } |
| 502 | | } |
| 503 | | |
| 504 | | /* |
| 505 | | Begin update cycle (called every second) |
| 506 | | */ |
| 507 | | static TIMER_CALLBACK(rtc_begin_update_callback) |
| 508 | | { |
| 509 | | if (((rtc.regs[reg_A] & reg_A_DV) == 0x20) && ! (rtc.regs[reg_B] & reg_B_SET)) |
| 510 | | { |
| 511 | | rtc.regs[reg_A] |= reg_A_UIP; |
| 512 | | |
| 513 | | /* schedule end of update cycle */ |
| 514 | | mame_timer_set(UPDATE_CYCLE_TIME, 0, rtc_end_update_callback); |
| 515 | | } |
| 516 | | } |
| 517 | | |
| 518 | | /* |
| 519 | | End update cycle (called UPDATE_CYCLE_TIME = 1948us after start of update |
| 520 | | cycle) |
| 521 | | */ |
| 522 | | static TIMER_CALLBACK(rtc_end_update_callback) |
| 523 | | { |
| 524 | | static const int days_in_month_table[12] = |
| 525 | | { |
| 526 | | 31,28,31, 30,31,30, |
| 527 | | 31,31,30, 31,30,31 |
| 528 | | }; |
| 529 | | UINT8 (*increment)(UINT8 data); |
| 530 | | int c59, c23, c12, c11, c29; |
| 531 | | |
| 532 | | if (! (rtc.regs[reg_A] & reg_A_UIP)) |
| 533 | | /* abort if update cycle has been canceled */ |
| 534 | | return; |
| 535 | | |
| 536 | | if (rtc.regs[reg_B] & reg_B_DM) |
| 537 | | { |
| 538 | | /* binary mode */ |
| 539 | | increment = increment_binary; |
| 540 | | c59 = 59; |
| 541 | | c23 = 23; |
| 542 | | c12 = 12; |
| 543 | | c11 = 11; |
| 544 | | c29 = 29; |
| 545 | | } |
| 546 | | else |
| 547 | | { |
| 548 | | /* BCD mode */ |
| 549 | | increment = increment_BCD; |
| 550 | | c59 = 0x59; |
| 551 | | c23 = 0x23; |
| 552 | | c12 = 0x12; |
| 553 | | c11 = 0x11; |
| 554 | | c29 = 0x29; |
| 555 | | } |
| 556 | | |
| 557 | | /* increment second */ |
| 558 | | if (rtc.regs[reg_second] < c59) |
| 559 | | rtc.regs[reg_second] = (*increment)(rtc.regs[reg_second]); |
| 560 | | else |
| 561 | | { |
| 562 | | rtc.regs[reg_second] = 0; |
| 563 | | |
| 564 | | /* increment minute */ |
| 565 | | if (rtc.regs[reg_minute] < c59) |
| 566 | | rtc.regs[reg_minute] = (*increment)(rtc.regs[reg_minute]); |
| 567 | | else |
| 568 | | { |
| 569 | | rtc.regs[reg_minute] = 0; |
| 570 | | |
| 571 | | /* increment hour */ |
| 572 | | if (rtc.regs[reg_B] & reg_B_24h) |
| 573 | | { |
| 574 | | /* 24 hour mode */ |
| 575 | | if (rtc.regs[reg_hour] < c23) |
| 576 | | rtc.regs[reg_hour] = (*increment)(rtc.regs[reg_hour]); |
| 577 | | else |
| 578 | | rtc.regs[reg_hour] = 0; |
| 579 | | } |
| 580 | | else |
| 581 | | { |
| 582 | | /* 12 hour mode */ |
| 583 | | if (rtc.regs[reg_hour] < c12) |
| 584 | | { |
| 585 | | if ((rtc.regs[reg_hour] & 0x7f) == c11) |
| 586 | | rtc.regs[reg_hour] ^= 0x80; |
| 587 | | rtc.regs[reg_hour] = ((*increment)(rtc.regs[reg_hour] & 0x7f) & 0x7f) |
| 588 | | | (rtc.regs[reg_hour] & 0x80); |
| 589 | | } |
| 590 | | else |
| 591 | | rtc.regs[reg_hour] = 1 | (rtc.regs[reg_hour] & 0x80); |
| 592 | | } |
| 593 | | |
| 594 | | /* increment day if needed */ |
| 595 | | if (rtc.regs[reg_hour] == ((rtc.regs[reg_B] & reg_B_24h) ? 0 : c12)) |
| 596 | | { |
| 597 | | /* increment day */ |
| 598 | | int days_in_month; |
| 599 | | |
| 600 | | if (rtc.regs[reg_weekday] < 7) |
| 601 | | rtc.regs[reg_weekday]++; |
| 602 | | else |
| 603 | | rtc.regs[reg_weekday] = 1; |
| 604 | | |
| 605 | | if ((rtc.regs[reg_month] != 2) || (rtc.regs[reg_year] & 0x03)) |
| 606 | | { |
| 607 | | if (rtc.regs[reg_B] & reg_B_DM) |
| 608 | | { |
| 609 | | /* binary mode */ |
| 610 | | days_in_month = days_in_month_table[rtc.regs[reg_month] - 1]; |
| 611 | | } |
| 612 | | else |
| 613 | | { |
| 614 | | /* BCD mode */ |
| 615 | | days_in_month = binary_to_BCD(days_in_month_table[BCD_to_binary(rtc.regs[reg_month]) - 1]); |
| 616 | | } |
| 617 | | } |
| 618 | | else |
| 619 | | days_in_month = c29; |
| 620 | | |
| 621 | | if (rtc.regs[reg_monthday] < days_in_month) |
| 622 | | rtc.regs[reg_monthday] = (*increment)(rtc.regs[reg_monthday]); |
| 623 | | else |
| 624 | | { |
| 625 | | /* increment month */ |
| 626 | | rtc.regs[reg_monthday] = 1; |
| 627 | | |
| 628 | | if (rtc.regs[reg_month] < c12) |
| 629 | | rtc.regs[reg_month] = (*increment)(rtc.regs[reg_month]); |
| 630 | | else |
| 631 | | { |
| 632 | | /* increment year */ |
| 633 | | rtc.regs[reg_month] = 1; |
| 634 | | |
| 635 | | if (rtc.regs[reg_B] & reg_B_DM) |
| 636 | | { |
| 637 | | /* binary mode */ |
| 638 | | if (rtc.regs[reg_year] < 99) |
| 639 | | rtc.regs[reg_year]++; |
| 640 | | else |
| 641 | | rtc.regs[reg_year] = 0; |
| 642 | | } |
| 643 | | else |
| 644 | | { |
| 645 | | /* BCD mode */ |
| 646 | | rtc.regs[reg_year] = increment_BCD(rtc.regs[reg_year]); |
| 647 | | } |
| 648 | | } |
| 649 | | } |
| 650 | | } |
| 651 | | } |
| 652 | | } |
| 653 | | |
| 654 | | rtc.regs[reg_A] &= ~reg_A_UIP; |
| 655 | | rtc.regs[reg_C] |= reg_C_UF; |
| 656 | | |
| 657 | | /* test for alarm (values in range 0xc0-0xff mean "don't care") */ |
| 658 | | if ((((rtc.regs[reg_alarm_second] & 0xc0) == 0xc0) || (rtc.regs[reg_alarm_second] == rtc.regs[reg_second])) |
| 659 | | && (((rtc.regs[reg_alarm_minute] & 0xc0) == 0xc0) || (rtc.regs[reg_alarm_minute] == rtc.regs[reg_minute])) |
| 660 | | && (((rtc.regs[reg_alarm_hour] & 0xc0) == 0xc0) || (rtc.regs[reg_alarm_hour] == rtc.regs[reg_hour]))) |
| 661 | | rtc.regs[reg_C] |= reg_C_AF; |
| 662 | | |
| 663 | | field_interrupts(); |
| 664 | | } |