Hello again,

2008/12/10 Kees Grinwis <[EMAIL PROTECTED]>:
> I suggest to have a look at the attached source of a simple test
> application related to the implementation of the retrieval of the
> current time from the operating system before continuing reading this
> posting.

Whoops, I'd forgotten to include the file itself. Corrected with this mail...


-- 
Greetingz
Kees
#include <time.h>
#include <stdio.h>

unsigned char cmosram[256];

#if defined linux || defined __linux
    #define RPCEMU_UNIX
    #define RPCEMU_LINUX
#endif
#if defined WIN32 || defined _WIN32
    #define RPCEMU_WIN32
#endif

struct rpcemu_cmostime {
    int tick; // centiseconds
    int second;
    int minute;
    int hour;
    int monthday;
    int month;
    int year;
};

/**
 * Function updates the given <code>struct rpcemu_cmostime</code> with the
 * current system time.
 *
 * The actual implementation is platform specific.
 *
 * @param *cmostime The structure that should be populated
 * 
 */
void cmosgetsystemtime(struct rpcemu_cmostime *cmostime) {
#if defined RPCEMU_UNIX
    time_t systemtime;
    struct tm *tm_systemtime;

    /* First get the current system time and convert it to a tm struct */
    systemtime = time(NULL);
    tm_systemtime = gmtime(&systemtime);

    /* Populate the rpcemu_cmostime structure */
    cmostime->year = tm_systemtime->tm_year; /* N.B. Year is not used in the CMOS clock... */
    cmostime->month = tm_systemtime->tm_mon;
    cmostime->monthday = tm_systemtime->tm_mday;
    cmostime->hour = tm_systemtime->tm_hour;
    cmostime->minute = tm_systemtime->tm_min;
    cmostime->second = tm_systemtime->tm_sec;
    cmostime->tick = 0; /* Initialise the tick counter */
#endif
#if defined RPCEMU_WIN32
    SYSTEMTIME systemtime;

    /* Get the system time from the Win32 API */
    GetLocalTime(&systemtime);

    /* Populate the rpcemu_cmostime structure */
    cmostime->year = systemtime.wYear; /* N.B. Year is not used in the CMOS clock... */
    cmostime->month = systemtime.wMonth;
    cmostime->monthday = systemtime.wDay;
    cmostime->hour = systemtime.wHour;
    cmostime->minute = systemtime.wMinute;
    cmostime->second = systemtime.wSecond;
    cmostime->tick = systemtime.wMillisendos / 10; /* Tick counter is in centiseconds */
#endif
#if !defined RPCEMU_UNIX && !defined RPCEMU_WIN32
    printf("No CMOS support\n");
    // [KG][TODO] Should this be an assert?
#endif
}


/**
 * Updates the global cmos RAM array with the contents of the given 
 * <code>struct rpcemu_cmostime</code>
 *
 * @param *cmostime The structure to be used
 * 
 */
void cmosupdatetimer(struct rpcemu_cmostime cmostime) {
    int c,d;

    d = cmostime.tick % 10;
    c = cmostime.tick / 10;
    cmosram[1] = d | (c << 4);

    d = cmostime.second % 10;
    c = cmostime.second / 10;
    cmosram[2] = d | (c << 4);

    d = cmostime.minute % 10;
    c = cmostime.minute / 10;
    cmosram[3] = d | (c << 4);

    d = cmostime.hour % 10;
    c = cmostime.hour / 10;
    cmosram[4] = d | (c << 4);

    d = cmostime.monthday % 10;
    c = cmostime.monthday / 10;
    cmosram[5] = d | (c << 4);

    d = cmostime.month % 10;
    c = cmostime.month / 10;
    cmosram[6] = d | (c << 4);

    /* The time is stored in BCD format in the CMOS clock, so when assuming 
     * hexadecimals the time should be printed correctly for us humans
     */
    printf("M%2x - D%2x\n", cmosram[6], cmosram[5]);
    printf("%2x:%2x:%2x\n", cmosram[4], cmosram[3], cmosram[2]);
}


int main(void) {
    struct rpcemu_cmostime cmostime;

    cmosgetsystemtime(&cmostime);
    cmosupdatetimer(cmostime);

    return 0;
}
_______________________________________________
Rpcemu mailing list
[email protected]
http://www.riscos.info/cgi-bin/mailman/listinfo/rpcemu

Reply via email to