1 /*************************************************************************** 2 * Copyright (C) 2007 by Sindre Aamås * 3 * aamas@stud.ntnu.no * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License version 2 as * 7 * published by the Free Software Foundation. * 8 * * 9 * This program is distributed in the hope that it will be useful, * 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 * GNU General Public License version 2 for more details. * 13 * * 14 * You should have received a copy of the GNU General Public License * 15 * version 2 along with this program; if not, write to the * 16 * Free Software Foundation, Inc., * 17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 18 ***************************************************************************/ 19 #ifndef RTC_H 20 #define RTC_H 21 22 #include <ctime> 23 #include <stdint.h> 24 25 namespace gambatte 26 { 27 28 struct SaveState; 29 30 class Rtc 31 { 32 public: 33 Rtc(); 34 getActive()35 const unsigned char* getActive() const 36 { 37 return activeData_; 38 } 39 getBaseTime()40 uint64_t& getBaseTime() 41 { 42 return baseTime_; 43 } 44 setBaseTime(const uint64_t baseTime)45 void setBaseTime(const uint64_t baseTime) 46 { 47 baseTime_ = baseTime; 48 } 49 latch(const unsigned data)50 void latch(const unsigned data) 51 { 52 if (!lastLatchData_ && data == 1) 53 doLatch(); 54 55 lastLatchData_ = data; 56 } 57 58 void saveState(SaveState &state) const; 59 void loadState(const SaveState &state); 60 set(const bool enabled,unsigned bank)61 void set(const bool enabled, unsigned bank) 62 { 63 bank &= 0xF; 64 bank -= 8; 65 66 this->enabled_ = enabled; 67 this->index_ = bank; 68 69 doSwapActive(); 70 } 71 write(const unsigned data)72 void write(const unsigned data) 73 { 74 (this->*activeSet_)(data); 75 *activeData_ = data; 76 } 77 78 private: 79 unsigned char *activeData_; 80 void (Rtc::*activeSet_)(unsigned); 81 uint64_t baseTime_; 82 uint64_t haltTime_; 83 unsigned char index_; 84 unsigned char dataDh_; 85 unsigned char dataDl_; 86 unsigned char dataH_; 87 unsigned char dataM_; 88 unsigned char dataS_; 89 bool enabled_; 90 bool lastLatchData_; 91 92 void doLatch(); 93 void doSwapActive(); 94 void setDh(unsigned new_dh); 95 void setDl(unsigned new_lowdays); 96 void setH(unsigned new_hours); 97 void setM(unsigned new_minutes); 98 void setS(unsigned new_seconds); 99 100 }; 101 102 } 103 104 #endif 105