| 1 | /* Cygne |
| 2 | * |
| 3 | * Copyright notice for this file: |
| 4 | * Copyright (C) 2002 Dox [email protected] |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <time.h> |
| 22 | |
| 23 | #include "wswan.h" |
| 24 | |
| 25 | #include "../state.h" |
| 26 | |
| 27 | static uint64 CurrentTime; |
| 28 | static uint32 ClockCycleCounter; |
| 29 | static uint8 wsCA15; |
| 30 | static uint8 Command, Data; |
| 31 | |
| 32 | void WSwan_RTCWrite(uint32 A, uint8 V) |
| 33 | { |
| 34 | switch(A) |
| 35 | { |
| 36 | case 0xca: |
| 37 | if(V==0x15) |
| 38 | wsCA15=0; |
| 39 | Command = V; |
| 40 | break; |
| 41 | case 0xcb: |
| 42 | Data = V; |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | uint8 WSwan_RTCRead(uint32 A) |
| 48 | { |
| 49 | time_t long_time; |
| 50 | struct tm *newtime; |
| 51 | |
| 52 | switch(A) |
| 53 | { |
| 54 | case 0xca: |
| 55 | return (Command|0x80); |
| 56 | case 0xcb: |
| 57 | if(Command != 0x15) |
| 58 | return Data | 0x80; |
| 59 | |
| 60 | long_time = CurrentTime; |
| 61 | newtime = gmtime( &long_time ); |
| 62 | |
| 63 | switch(wsCA15) |
| 64 | { |
| 65 | case 0: |
| 66 | wsCA15++; |
| 67 | return mBCD(newtime->tm_year-100); |
| 68 | case 1: |
| 69 | wsCA15++; |
| 70 | return mBCD(newtime->tm_mon); |
| 71 | case 2: |
| 72 | wsCA15++; |
| 73 | return mBCD(newtime->tm_mday); |
| 74 | case 3: |
| 75 | wsCA15++; |
| 76 | return mBCD(newtime->tm_wday); |
| 77 | case 4: |
| 78 | wsCA15++; |
| 79 | return mBCD(newtime->tm_hour); |
| 80 | case 5: |
| 81 | wsCA15++; |
| 82 | return mBCD(newtime->tm_min); |
| 83 | case 6: |
| 84 | wsCA15=0; |
| 85 | return mBCD(newtime->tm_sec); |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | void WSwan_RTCReset(void) |
| 94 | { |
| 95 | time_t happy_time = time(NULL); |
| 96 | |
| 97 | CurrentTime = mktime(localtime(&happy_time)); |
| 98 | ClockCycleCounter = 0; |
| 99 | wsCA15 = 0; |
| 100 | } |
| 101 | |
| 102 | void WSwan_RTCClock(uint32 cycles) |
| 103 | { |
| 104 | ClockCycleCounter += cycles; |
| 105 | while(ClockCycleCounter >= 3072000) |
| 106 | { |
| 107 | ClockCycleCounter -= 3072000; |
| 108 | CurrentTime++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int WSwan_RTCStateAction(StateMem *sm, int load, int data_only) |
| 113 | { |
| 114 | SFORMAT StateRegs[] = |
| 115 | { |
| 116 | { &(CurrentTime), (uint32_t)sizeof(CurrentTime), MDFNSTATE_RLSB, "CurrentTime" }, |
| 117 | { &(ClockCycleCounter), (uint32_t)sizeof(ClockCycleCounter), MDFNSTATE_RLSB, "ClockCycleCounter" }, |
| 118 | { &(wsCA15), (uint32_t)sizeof(wsCA15), MDFNSTATE_RLSB, "wsCA15" }, |
| 119 | { &(Command), (uint32_t)sizeof(Command), MDFNSTATE_RLSB, "Command" }, |
| 120 | { &(Data), (uint32_t)sizeof(Data), MDFNSTATE_RLSB, "Data" }, |
| 121 | { 0, 0, 0, 0 } |
| 122 | }; |
| 123 | |
| 124 | if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "RTC", false)) |
| 125 | return 0; |
| 126 | return 1; |
| 127 | } |