Hi,
os64dev wrote:suthers wrote:*snip*
It takes in a char that contains a packed bcd value and outputs it in standard binary (in a char again).
This was the most efficient way I could think of doing it, please tell me if you can think of anything more efficient.
(except for writing it in assembler
)
The fastest method is not to depack it anyway, just display it as hexidecimal and it will show the correct time.
That works, until you start doing calculations (like converting to/from UTC, or working out "seconds since 1970").
The next fastest way is probably to use a lookup table (especially if it's likely the table will be in the CPUs cache). You could probably even do 2 values at a time (use an array of 16-bit integers with 65536 entries).
os64dev wrote:I think there is also a bit somewhere in the CMOS that tells if the data in in BCD or in plain decimal.
There is, and you can change this bit so that the RTC operates in "binary mode", but that will mess up other OSs (e.g. Windows) and the BIOS. It might be a good idea to skip the BCD->binary conversion if the RTC is in binary mode though (in case some other OS changed it).
If you do change the RTC from "BCD mode" to "binary mode" then you need to update the rest of the RTC because changing the mode won't change the values reported (you'd just end up with an RTC using binary mode on BCD data, which will stuff things up). You can't switch to binary mode, then read the values, then switch back to BCD mode.
Also note that the location of the century value is *not* standard, and you shouldn't assume that it's in "CMOS location 0x32". Older computers didn't have any century value at all, and for newer computers you should find out where it is from ACPI tables (from the FADT?).
If there is no century value (or if you couldn't be bothered with ACPI) you could just assume "century = 20" and you'd be good for about 92 years. A better idea might be "#define CURRENT_YEAR 08" followed by "if (year >= CURRENT_YEAR) {century = 20} else {century = 21}". In this case you're good for almost 100 years after the OS was released, and you can update "CURRENT_YEAR" each time you release a new version of your OS.
Cheers,
Brendan