Skip to content

Instantly share code, notes, and snippets.

@LemonBoy
Created November 28, 2011 21:27
Show Gist options
  • Save LemonBoy/1402153 to your computer and use it in GitHub Desktop.
Save LemonBoy/1402153 to your computer and use it in GitHub Desktop.
CMOS fun for boring days
#include <stdio.h>
#include <sys/io.h>
int convert_bcd (unsigned char bcd) {
return (bcd >> 4) * 10 + (bcd & 0xF);
}
unsigned char cmos_read (int offset) {
outb(0x80 | (offset & 0x7F), 0x70);
return inb(0x71);
}
int main (int argc, char **argv) {
/* int i, offset;*/
/* if (argc < 2)*/
/* return 1; */
ioperm(0x70, 2, 1);
/* for (i = 1; i < argc; i++) {*/
/* printf("%02x\n", cmos_read(atoi(argv[i])));*/
/* }*/
if (cmos_read(0xA) & 0x80) {
printf("Could not fetch the time, the RTC is in update stage\n");
return 1;
}
printf("%02i:%02i:%02i %02i/%02i/%04i\n",
convert_bcd(cmos_read(4)),
convert_bcd(cmos_read(2)),
convert_bcd(cmos_read(0)),
convert_bcd(cmos_read(7)),
convert_bcd(cmos_read(8)),
convert_bcd(cmos_read(9)) + 2000
);
ioperm(0x70, 2, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment