Stick the conversion of python to unix time with all of

the other param code so that other functions can use it
as well.

--HG--
extra : convert_revision : a8becdeadc70af0b64bff5b0770788dfba6e1857
This commit is contained in:
Nathan Binkert 2007-01-28 10:26:59 -08:00
parent f9a341f8e7
commit 37795b104d
6 changed files with 52 additions and 63 deletions

View file

@ -65,69 +65,32 @@ TsunamiIO::RTC::RTC(const string &n, Tsunami* tsunami, const vector<int> &t,
stat_regA = RTCA_32768HZ | RTCA_1024HZ; stat_regA = RTCA_32768HZ | RTCA_1024HZ;
stat_regB = RTCB_PRDC_IE |RTCB_BIN | RTCB_24HR; stat_regB = RTCB_PRDC_IE |RTCB_BIN | RTCB_24HR;
if (year_is_bcd) {
// The RTC uses BCD for the last two digits in the year.
// They python year is a full year.
int _year = t[0] % 100;
int tens = _year / 10;
int ones = _year % 10;
year = (tens << 4) + ones;
} else {
// Even though the datasheet says that the year field should be
// interpreted as BCD, we just enter the number of years since
// 1900 since linux seems to be happy with that (and I believe
// that Tru64 was as well)
year = t[0] - 1900;
}
mon = t[1];
mday = t[2];
hour = t[3];
min = t[4];
sec = t[5];
// wday is defined to be in the range from 1 - 7 with 1 being Sunday.
// the value coming from python is in the range from 0 - 6 with 0 being
// Monday. Fix that here.
wday = t[6] + 2;
if (wday > 7)
wday -= 7;
DPRINTFN("Real-time clock set to %s", getDateString());
}
std::string
TsunamiIO::RTC::getDateString()
{
struct tm tm; struct tm tm;
parseTime(t, &tm);
memset(&tm, 0, sizeof(tm)); year = tm.tm_year;
if (year_is_bcd) { if (year_is_bcd) {
// undo the BCD and conver to years since 1900 guessing that // The datasheet says that the year field can be either BCD or
// anything before 1970 is actually after 2000 // years since 1900. Linux seems to be happy with years since
int _year = (year >> 4) * 10 + (year & 0xf); // 1900.
if (_year < 70) year = year % 100;
_year += 100; int tens = year / 10;
int ones = year % 10;
tm.tm_year = _year; year = (tens << 4) + ones;
} else {
// number of years since 1900
tm.tm_year = year;
} }
// unix is 0-11 for month // Unix is 0-11 for month, data seet says start at 1
tm.tm_mon = mon - 1; mon = tm.tm_mon + 1;
tm.tm_mday = mday; mday = tm.tm_mday;
tm.tm_hour = hour; hour = tm.tm_hour;
tm.tm_min = min; min = tm.tm_min;
tm.tm_sec = sec; sec = tm.tm_sec;
// to add more annoyance unix is 0 - 6 with 0 as sunday // Datasheet says 1 is sunday
tm.tm_wday = wday - 1; wday = tm.tm_wday + 1;
return asctime(&tm); DPRINTFN("Real-time clock set to %s", asctime(&tm));
} }
void void

View file

@ -125,9 +125,6 @@ class TsunamiIO : public BasicPioDevice
/** RTC read data */ /** RTC read data */
uint8_t readData(); uint8_t readData();
/** RTC get the date */
std::string getDateString();
/** /**
* Serialize this object to the given output stream. * Serialize this object to the given output stream.
* @param base The base name of the counter object. * @param base The base name of the counter object.

View file

@ -49,12 +49,14 @@ using namespace std;
using namespace TheISA; using namespace TheISA;
DumbTOD::DumbTOD(Params *p) DumbTOD::DumbTOD(Params *p)
: BasicPioDevice(p), todTime(p->init_time) : BasicPioDevice(p)
{ {
pioSize = 0x08; pioSize = 0x08;
struct tm tm; struct tm tm;
gmtime_r((time_t*)&todTime, &tm); parseTime(p->init_time, &tm);
todTime = timegm(&tm);
DPRINTFN("Real-time clock set to %s\n", asctime(&tm)); DPRINTFN("Real-time clock set to %s\n", asctime(&tm));
DPRINTFN("Real-time clock set to %d\n", todTime); DPRINTFN("Real-time clock set to %d\n", todTime);
} }
@ -86,7 +88,7 @@ BEGIN_DECLARE_SIM_OBJECT_PARAMS(DumbTOD)
Param<Tick> pio_latency; Param<Tick> pio_latency;
SimObjectParam<Platform *> platform; SimObjectParam<Platform *> platform;
SimObjectParam<System *> system; SimObjectParam<System *> system;
Param<time_t> time; VectorParam<int> time;
END_DECLARE_SIM_OBJECT_PARAMS(DumbTOD) END_DECLARE_SIM_OBJECT_PARAMS(DumbTOD)
@ -96,7 +98,7 @@ BEGIN_INIT_SIM_OBJECT_PARAMS(DumbTOD)
INIT_PARAM(pio_latency, "Programmed IO latency"), INIT_PARAM(pio_latency, "Programmed IO latency"),
INIT_PARAM(platform, "platform"), INIT_PARAM(platform, "platform"),
INIT_PARAM(system, "system object"), INIT_PARAM(system, "system object"),
INIT_PARAM(time, "System time to use (0 for actual time") INIT_PARAM(time, "")
END_INIT_SIM_OBJECT_PARAMS(DumbTOD) END_INIT_SIM_OBJECT_PARAMS(DumbTOD)

View file

@ -36,6 +36,8 @@
#ifndef __DEV_SPARC_DTOD_HH__ #ifndef __DEV_SPARC_DTOD_HH__
#define __DEV_SPARC_DTOD_HH__ #define __DEV_SPARC_DTOD_HH__
#include <vector>
#include "base/range.hh" #include "base/range.hh"
#include "dev/io_device.hh" #include "dev/io_device.hh"
@ -52,7 +54,7 @@ class DumbTOD : public BasicPioDevice
public: public:
struct Params : public BasicPioDevice::Params struct Params : public BasicPioDevice::Params
{ {
time_t init_time; std::vector<int> init_time;
}; };
protected: protected:
const Params *params() const { return (const Params *)_params; } const Params *params() const { return (const Params *)_params; }

View file

@ -777,3 +777,27 @@ ParamContext::describeAllContexts(ostream &os)
os << endl; os << endl;
} }
} }
void
parseTime(const std::vector<int> &time, struct tm *tm)
{
memset(tm, 0, sizeof(struct tm));
// UNIX is years since 1900
tm->tm_year = time[0] - 1900;
// Python starts at 1, UNIX starts at 0
tm->tm_mon = time[1] - 1;
tm->tm_mday = time[2];
tm->tm_hour = time[3];
tm->tm_min = time[4];
tm->tm_sec = time[5];
// Python has 0 as Monday, UNIX is 0 as sunday
tm->tm_wday = time[6] + 1;
if (tm->tm_wday > 6)
tm->tm_wday -= 7;
// Python starts at 1, Unix starts at 0
tm->tm_yday = time[7] - 1;
}

View file

@ -781,4 +781,5 @@ SimObjectVectorParam<OBJ_CLASS *>::showType(std::ostream &os) const \
template <class T> bool parseParam(const std::string &str, T &data); template <class T> bool parseParam(const std::string &str, T &data);
template <class T> void showParam(std::ostream &os, const T &data); template <class T> void showParam(std::ostream &os, const T &data);
void parseTime(const std::vector<int> &time, struct tm *tm);
#endif // _SIM_PARAM_HH_ #endif // _SIM_PARAM_HH_