Page 1 of 1

Time Code?

Posted: Thu Jul 06, 2006 6:17 am
by jakelstr
Hi
I am working on an OS.
Could anybody tell me the C Code that can access the system time clock?
Thanks

try that one

Posted: Thu Jul 06, 2006 10:49 am
by digo_rp
#include <defines>
#include <x86>
#include <cmos>
#include <main>
#include <screen>

struct time time1;
struct time time2;
struct tm *tm;

byte *DayOfTheWeek[7] = {
"Segunda", "Terca", "Quarta",
"Quinta", "Sexta", "Sabado",
"Domingo"
};

byte *MonthOfTheWeek[12] = {
"Janeiro", "Fevereiro", "Marco", "Abril",
"Maio", "Junho", "Julho", "Agosto",
"Setembro", "Outubro", "Novembro", "Dezembro"
};

char getdatetime() {
int i;
int utc = 0, setclock = 0;
time_t now;
int cpu_type, cmos_state;

cmos_state = read_register(HEALTH);

if (cmos_state & (DIAG_BADBATT | DIAG_MEMDIRT | DIAG_BADTIME)) {
puts("\nCMOS RAM error(s) found...");
sprintf(buf, "CMOS state = 0x%02x\n", cmos_state);
puts(buf);
if (cmos_state & DIAG_BADBATT)
puts("RTC lost power. Reset CMOS RAM with SETUP.\n");
if (cmos_state & DIAG_MEMDIRT)
puts("CMOS RAM checksum is bad. Run SETUP.\n");
if (cmos_state & DIAG_BADTIME)
puts("Time invalid in CMOS RAM. Reset clock with setclock.\n");
if(!setclock)
puts("-q\n");
return -1;
}

if(setclock) {

}

for (i = 0; i <10>year = read_register(YEAR);
t->month = read_register(MONTH);
t->mday = read_register(MDAY);
t->wday = read_register(WDAY);
t->hour = read_register(HOUR);
t->minute = read_register(MINUTE);
t->second = read_register(SECOND);

if ((read_register(STATUS) & 0x04) == 0) {
/* Convert BCD to binary (default RTC mode) */
t->year = BCD_TO_DEC(t->year);
t->month = BCD_TO_DEC(t->month);
t->mday = BCD_TO_DEC(t->mday);
t->wday = BCD_TO_DEC(t->wday);
t->hour = BCD_TO_DEC(t->hour);
t->minute = BCD_TO_DEC(t->minute);
t->second = BCD_TO_DEC(t->second);
}
}

/************************************************************************/
/* set_time( time ) */
/* Update the hardware real-time clock of the AT. */
/* Set the clock mode to BCD format if mode is binary. */
/************************************************************************/
void set_time(struct tm *t) {
int status;
t->tm_year = (t->tm_year > 99) ? t->tm_year - 1900 : t->tm_year;
t->tm_mon++;
if (((status = read_register(STATUS)) & 0x04) == 0) {
/* Convert binary to BCD if necessary */
t->tm_year = DEC_TO_BCD(t->tm_year);
t->tm_mon = DEC_TO_BCD(t->tm_mon);
t->tm_wday = DEC_TO_BCD(t->tm_wday);
t->tm_mday = DEC_TO_BCD(t->tm_mday);
t->tm_hour = DEC_TO_BCD(t->tm_hour);
t->tm_min = DEC_TO_BCD(t->tm_min);
t->tm_sec = DEC_TO_BCD(t->tm_sec);
}
write_register(STATUS, status | 0x80); /* update clock */
write_register(YEAR, t->tm_year);
write_register(MONTH, t->tm_mon);
write_register(MDAY, t->tm_mday);
write_register(WDAY, t->tm_wday);
write_register(HOUR, t->tm_hour);
write_register(MINUTE, t->tm_min);
write_register(SECOND, t->tm_sec);
write_register(STATUS, status); /* update finished */
}

int read_register(char reg_addr) {
byte result;
cli();
outportb(CLK_ELE, reg_addr);
result = inportb(CLK_IO);
sti();
return result;
}

void write_register(char reg_addr, int reg_value) {
cli();
outportb(CLK_ELE, reg_addr);
outportb(CLK_IO, reg_value);
sti();
}

Posted: Thu Jul 06, 2006 8:04 pm
by carbonBased
This code would be infinitely more useful if you could post the register values, as well (although, I believe everybody should have a copy of "The Indispensable PC Hardware Book" :)

--Jeff