time: move get_local_time out to platform-specific dir

Patched DD code to make use of this code. Untested on Windows.
This commit is contained in:
Mike Ryan 2016-01-31 09:22:09 -08:00 committed by Tyler J. Stachecki
parent 4e0620c637
commit cfd2336443
5 changed files with 100 additions and 28 deletions

View file

@ -303,6 +303,7 @@ set(OS_SOURCES
set(OS_POSIX_SOURCES
${PROJECT_SOURCE_DIR}/os/posix/alloc.c
${PROJECT_SOURCE_DIR}/os/posix/local_time.c
${PROJECT_SOURCE_DIR}/os/posix/main.c
${PROJECT_SOURCE_DIR}/os/posix/rom_file.c
${PROJECT_SOURCE_DIR}/os/posix/save_file.c
@ -313,6 +314,7 @@ set(OS_WINAPI_SOURCES
${PROJECT_SOURCE_DIR}/os/winapi/alloc.c
${PROJECT_SOURCE_DIR}/os/winapi/gl_config.c
${PROJECT_SOURCE_DIR}/os/winapi/gl_window.c
${PROJECT_SOURCE_DIR}/os/winapi/local_time.c
${PROJECT_SOURCE_DIR}/os/winapi/main.c
${PROJECT_SOURCE_DIR}/os/winapi/rom_file.c
${PROJECT_SOURCE_DIR}/os/winapi/save_file.c

View file

@ -18,18 +18,13 @@
//
#include "common.h"
#include "local_time.h"
#include "bus/address.h"
#include "bus/controller.h"
#include "dd/controller.h"
#include "ri/controller.h"
#include "vr4300/interface.h"
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef DEBUG_MMIO_REGISTER_ACCESS
#define debug_mmio_read(what, mnemonic, val) printf(#what": READ [%s]: 0x%.8X\n", mnemonic, val)
#define debug_mmio_write(what, mnemonic, val, dqm) printf(#what": WRITE [%s]: 0x%.8X/0x%.8X\n", mnemonic, val, dqm)
@ -589,28 +584,14 @@ void set_offset(struct dd_controller *dd) {
}
void get_dd_time(uint8_t *out) {
time_t now = time(NULL);
struct tm time = { 0, };
struct time_stamp now;
get_local_time(&now);
#ifdef _WIN32
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
out[0] = byte2bcd(now.year);
out[1] = byte2bcd(now.month);
out[2] = byte2bcd(now.day);
out[3] = byte2bcd(now.hour);
out[4] = byte2bcd(now.min);
out[5] = byte2bcd(now.sec);
out[0] = (uint8_t)(((sysTime.wYear / 10) << 4) | (sysTime.wYear % 10));
out[1] = (uint8_t)(((sysTime.wMonth / 10) << 4) | (sysTime.wMonth % 10));
out[2] = (uint8_t)(((sysTime.wDay / 10) << 4) | (sysTime.wDay % 10));
out[3] = (uint8_t)(((sysTime.wHour / 10) << 4) | (sysTime.wHour % 10));
out[4] = (uint8_t)(((sysTime.wMinute / 10) << 4) | (sysTime.wMinute % 10));
out[5] = (uint8_t)(((sysTime.wSecond / 10) << 4) | (sysTime.wSecond % 10));
#else
localtime_r(&now, &time);
time.tm_mon += 1; // month is zero-indexed in this struct
out[0] = (uint8_t)(((time.tm_year / 10) << 4) | (time.tm_year % 10));
out[1] = (uint8_t)(((time.tm_mon / 10) << 4) | (time.tm_mon % 10));
out[2] = (uint8_t)(((time.tm_mday / 10) << 4) | (time.tm_mday % 10));
out[3] = (uint8_t)(((time.tm_hour / 10) << 4) | (time.tm_hour % 10));
out[4] = (uint8_t)(((time.tm_min / 10) << 4) | (time.tm_min % 10));
out[5] = (uint8_t)(((time.tm_sec / 10) << 4) | (time.tm_sec % 10));
#endif
}

38
os/common/local_time.h Normal file
View file

@ -0,0 +1,38 @@
//
// os/common/local_time.h
//
// Functions for getting current time.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#ifndef __os_local_time_h__
#define __os_local_time_h__
#include "common.h"
#include <stddef.h>
#ifdef _WIN32
#include <windows.h>
#endif
struct time_stamp {
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned min;
unsigned sec;
unsigned week_day;
};
void get_local_time(struct time_stamp *ts);
static inline uint8_t byte2bcd(unsigned byte) {
byte %= 100;
return ((byte / 10) << 4) | (byte % 10);
}
#endif

25
os/posix/local_time.c Normal file
View file

@ -0,0 +1,25 @@
//
// os/unix/rom_file.c
//
// Functions for mapping ROM images into the address space.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#include "local_time.h"
#include <time.h>
void get_local_time(struct time_stamp *ts) {
time_t now = time(NULL);
struct tm time = { 0, };
localtime_r(&now, &time);
ts->year = time.tm_year;
ts->month = time.tm_mon + 1; // month is zero-indexed in this struct
ts->day = time.tm_mday;
ts->hour = time.tm_hour;
ts->min = time.tm_min;
ts->sec = time.tm_sec;
ts->week_day = time.tm_wday;
}

26
os/winapi/local_time.c Normal file
View file

@ -0,0 +1,26 @@
//
// os/winapi/local_time.c: Time functions for Windows.
//
// CEN64: Cycle-Accurate Nintendo 64 Emulator.
// Copyright (C) 2015, Tyler J. Stachecki.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#include "local_time.h"
#include <time.h>
#include <windows.h>
void get_local_time(struct time_stamp *ts) {
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
ts->year = sysTime.wYear;
ts->month = sysTime.wMonth;
ts->day = sysTime.wDay;
ts->hour = sysTime.wHour;
ts->min = sysTime.wMinute;
ts->sec = sysTime.wSecond;
ts->week_day = sysTime.wDayOfWeek - 1;
}