Enhancements for RTC

This commit is contained in:
Adrien Bourmault 2019-04-25 16:31:06 +02:00
parent 7de1bcb27a
commit 93cce54081
4 changed files with 129 additions and 71 deletions

View File

@ -174,7 +174,7 @@ $(KOBJDIR)/%.o: %.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
test: all test: all
@qemu-system-x86_64 -m 4G -hda $(BUILDDIR)/bin/disk.img \ @qemu-system-x86_64 -rtc base=localtime -m 4G -hda $(BUILDDIR)/bin/disk.img \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
test32: all test32: all

View File

@ -32,13 +32,13 @@ typedef struct
uchar month; uchar month;
uchar year; uchar year;
uchar century; uchar century;
} Time_t; } __attribute__((packed)) Time_t;
extern void IoSetupRtc(void); extern void IoSetupRtc(void);
extern void IoEnableRtc(void); extern void IoEnableRtc(void);
extern ulong IoRtcTicks;
extern void IoPrintRtcTime(void); extern void IoPrintRtcTime(void);
extern ulong IoGetRtcTicks(void); extern ulong IoGetRtcTicks(void);
extern Time_t* IoGetRtcTime(void); extern Time_t* IoGetRtcTime(void);
static char* WeekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; extern char* IoGetRtcTimeChar(void);
//static char* WeekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

View File

@ -93,12 +93,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
IoEnableKeyb(); IoEnableKeyb();
IoPrintRtcTime(); IoPrintRtcTime();
while (1) {
KernLog("There was %d ticks\n", IoGetRtcTicks());
for (uint i = 1; i < 10 ; i++) {
while (IoGetRtcTicks() < i * 10000) {
}
IoPrintRtcTime();
} }
KernLog("Goodbye after %d ticks\n", IoGetRtcTicks());
KernLog("\nGoodbye after %d ticks", IoRtcTicks);
// End this machine's suffering // End this machine's suffering
BFlushBuf(BStdOut); BFlushBuf(BStdOut);
KeCrashSystem(); KeCrashSystem();

View File

@ -29,18 +29,18 @@
extern void RtcIsr(void); extern void RtcIsr(void);
ulong IoRtcTicks = 0; static ulong IoRtcTicks = 0;
static uchar RtcRate = 0x05; //2048Hz
Time_t IoRtcTime; static Time_t IoRtcOriginTime;
static Time_t IoRtcTime;
static char time24or12Mode;
void IoSetupRtc(void) void IoSetupRtc(void)
{ {
IdtRegisterIrq(RtcIsr, 0x28, 0x8E); IdtRegisterIrq(RtcIsr, 0x28, 0x8E);
} }
void IoGetTimeFromRtc(void) static void GetTimeFromRtc(void)
{ {
char time24or12Mode;
Time_t lastTime; Time_t lastTime;
char updateInProgress = 1; char updateInProgress = 1;
@ -51,32 +51,32 @@ void IoGetTimeFromRtc(void)
} }
IoWriteByteOnPort(0x70, 0x0); IoWriteByteOnPort(0x70, 0x0);
IoRtcTime.sec = IoReadByteFromPort(0x71); IoRtcOriginTime.sec = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x02); IoWriteByteOnPort(0x70, 0x02);
IoRtcTime.min = IoReadByteFromPort(0x71); IoRtcOriginTime.min = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x04); IoWriteByteOnPort(0x70, 0x04);
IoRtcTime.hour = IoReadByteFromPort(0x71); IoRtcOriginTime.hour = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x06); IoWriteByteOnPort(0x70, 0x06);
IoRtcTime.weekday = IoReadByteFromPort(0x71); IoRtcOriginTime.weekday = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x07); IoWriteByteOnPort(0x70, 0x07);
IoRtcTime.day = IoReadByteFromPort(0x71); IoRtcOriginTime.day = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x08); IoWriteByteOnPort(0x70, 0x08);
IoRtcTime.month = IoReadByteFromPort(0x71); IoRtcOriginTime.month = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x09); IoWriteByteOnPort(0x70, 0x09);
IoRtcTime.year = IoReadByteFromPort(0x71); IoRtcOriginTime.year = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x32); IoWriteByteOnPort(0x70, 0x32);
IoRtcTime.century = IoReadByteFromPort(0x71); IoRtcOriginTime.century = IoReadByteFromPort(0x71);
// Now while we don't get the same value, read the registers (ensure data are valid) // Now while we don't get the same value, read the registers (ensure data are valid)
do { do {
lastTime.sec = IoRtcTime.sec; lastTime.sec = IoRtcOriginTime.sec;
lastTime.min = IoRtcTime.min; lastTime.min = IoRtcOriginTime.min;
lastTime.hour = IoRtcTime.hour; lastTime.hour = IoRtcOriginTime.hour;
lastTime.weekday = IoRtcTime.weekday; lastTime.weekday = IoRtcOriginTime.weekday;
lastTime.day = IoRtcTime.day; lastTime.day = IoRtcOriginTime.day;
lastTime.month = IoRtcTime.month; lastTime.month = IoRtcOriginTime.month;
lastTime.year = IoRtcTime.year; lastTime.year = IoRtcOriginTime.year;
lastTime.century = IoRtcTime.century; lastTime.century = IoRtcOriginTime.century;
while(updateInProgress) { // wait while the RTC updates its value while(updateInProgress) { // wait while the RTC updates its value
IoWriteByteOnPort(0x70, 0x0A); IoWriteByteOnPort(0x70, 0x0A);
@ -84,53 +84,63 @@ void IoGetTimeFromRtc(void)
} }
IoWriteByteOnPort(0x70, 0x0); IoWriteByteOnPort(0x70, 0x0);
IoRtcTime.sec = IoReadByteFromPort(0x71); IoRtcOriginTime.sec = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x02); IoWriteByteOnPort(0x70, 0x02);
IoRtcTime.min = IoReadByteFromPort(0x71); IoRtcOriginTime.min = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x04); IoWriteByteOnPort(0x70, 0x04);
IoRtcTime.hour = IoReadByteFromPort(0x71); IoRtcOriginTime.hour = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x06); IoWriteByteOnPort(0x70, 0x06);
IoRtcTime.weekday = IoReadByteFromPort(0x71); IoRtcOriginTime.weekday = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x07); IoWriteByteOnPort(0x70, 0x07);
IoRtcTime.day = IoReadByteFromPort(0x71); IoRtcOriginTime.day = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x08); IoWriteByteOnPort(0x70, 0x08);
IoRtcTime.month = IoReadByteFromPort(0x71); IoRtcOriginTime.month = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x09); IoWriteByteOnPort(0x70, 0x09);
IoRtcTime.year = IoReadByteFromPort(0x71); IoRtcOriginTime.year = IoReadByteFromPort(0x71);
IoWriteByteOnPort(0x70, 0x32); IoWriteByteOnPort(0x70, 0x32);
IoRtcTime.century = IoReadByteFromPort(0x71); IoRtcOriginTime.century = IoReadByteFromPort(0x71);
} while ( (lastTime.sec != IoRtcTime.sec) || (lastTime.min != IoRtcTime.min) || } while ( (lastTime.sec != IoRtcOriginTime.sec) || (lastTime.min != IoRtcOriginTime.min) ||
(lastTime.hour != IoRtcTime.hour) || (lastTime.weekday != IoRtcTime.weekday) || (lastTime.hour != IoRtcOriginTime.hour) || (lastTime.weekday != IoRtcOriginTime.weekday) ||
(lastTime.day != IoRtcTime.day) || (lastTime.month != IoRtcTime.month) || (lastTime.day != IoRtcOriginTime.day) || (lastTime.month != IoRtcOriginTime.month) ||
(lastTime.year != IoRtcTime.year) || (lastTime.century != IoRtcTime.century) (lastTime.year != IoRtcOriginTime.year) || (lastTime.century != IoRtcOriginTime.century)
); );
IoWriteByteOnPort(0x70, 0x32); IoWriteByteOnPort(0x70, 0x0B);
time24or12Mode = IoReadByteFromPort(0x71); time24or12Mode = IoReadByteFromPort(0x71);
// Convert to binary if it is necessary // Convert to binary if it is necessary
if (!(time24or12Mode & 0x04)) { if (!(time24or12Mode & 0x04)) {
IoRtcTime.sec = (IoRtcTime.sec & 0x0F) IoRtcOriginTime.sec = (IoRtcOriginTime.sec & 0x0F)
+ ((IoRtcTime.sec / 16) * 10); + ((IoRtcOriginTime.sec / 16) * 10);
IoRtcTime.min = (IoRtcTime.min & 0x0F) IoRtcOriginTime.min = (IoRtcOriginTime.min & 0x0F)
+ ((IoRtcTime.min / 16) * 10); + ((IoRtcOriginTime.min / 16) * 10);
IoRtcTime.hour = ( (IoRtcTime.hour & 0x0F) IoRtcOriginTime.hour = ( (IoRtcOriginTime.hour & 0x0F)
+ (((IoRtcTime.hour & 0x70) / 16) * 10) ) + (((IoRtcOriginTime.hour & 0x70) / 16) * 10) )
| (IoRtcTime.hour & 0x80); | (IoRtcOriginTime.hour & 0x80);
IoRtcTime.day = (IoRtcTime.day & 0x0F) + ((IoRtcTime.day / 16) * 10); IoRtcOriginTime.day = (IoRtcOriginTime.day & 0x0F) + ((IoRtcOriginTime.day / 16) * 10);
IoRtcTime.month = (IoRtcTime.month & 0x0F) IoRtcOriginTime.month = (IoRtcOriginTime.month & 0x0F)
+ ((IoRtcTime.month / 16) * 10); + ((IoRtcOriginTime.month / 16) * 10);
IoRtcTime.year = (IoRtcTime.year & 0x0F) IoRtcOriginTime.year = (IoRtcOriginTime.year & 0x0F)
+ ((IoRtcTime.year / 16) * 10); + ((IoRtcOriginTime.year / 16) * 10);
IoRtcTime.century = (IoRtcTime.century & 0x0F) IoRtcOriginTime.century = (IoRtcOriginTime.century & 0x0F)
+ ((IoRtcTime.century / 16) * 10); + ((IoRtcOriginTime.century / 16) * 10);
IoRtcOriginTime.weekday = (IoRtcOriginTime.weekday & 0x0F)
+ ((IoRtcOriginTime.weekday / 16) * 10);
} }
// Convert 12 to 24 hour if necessary // Convert 12 to 24 hour if necessary
if (!(time24or12Mode & 0x02) && (IoRtcTime.hour & 0x80)) { if (!(time24or12Mode & 0x02) && (IoRtcOriginTime.hour & 0x80)) {
IoRtcTime.hour = ((IoRtcTime.hour & 0x7F) + 12) % 24; IoRtcOriginTime.hour = ((IoRtcOriginTime.hour & 0x7)+ 10) % 24;
} }
IoRtcTime.sec = IoRtcOriginTime.sec;
IoRtcTime.min = IoRtcOriginTime.min;
IoRtcTime.hour = IoRtcOriginTime.hour;
IoRtcTime.weekday = IoRtcOriginTime.weekday;
IoRtcTime.day = IoRtcOriginTime.day;
IoRtcTime.month = IoRtcOriginTime.month;
IoRtcTime.year = IoRtcOriginTime.year;
IoRtcTime.century = IoRtcOriginTime.century;
} }
void IoEnableRtc(void) void IoEnableRtc(void)
@ -139,10 +149,10 @@ void IoEnableRtc(void)
char readedInterruptConfig; char readedInterruptConfig;
char readedRegister; char readedRegister;
char readedIrqs; char readedIrqs;
uchar RtcRate;
// Setting up the register control and interrupt rates // Setting up the register control and interrupt rates
RtcRate = 0x05; DebugLog("[RTC Time] Interrupt frequency set to %d Hz\n",
32768 >> (RtcRate-1));
IoWriteByteOnPort(0x70, 0x8B); IoWriteByteOnPort(0x70, 0x8B);
readedRegister = IoReadByteFromPort(0x71); readedRegister = IoReadByteFromPort(0x71);
@ -162,11 +172,11 @@ void IoEnableRtc(void)
readedIrqs = IoReadByteFromPort(0x21); readedIrqs = IoReadByteFromPort(0x21);
IoWriteByteOnPort(0x21, 0xFB & readedIrqs); // Enables IRQ on PIC 1 IoWriteByteOnPort(0x21, 0xFB & readedIrqs); // Enables IRQ on PIC 1
// clean-up // Clean-up
IoWriteByteOnPort(0x70, 0x0C); // Select status reg C IoWriteByteOnPort(0x70, 0x0C); // Select status reg C
IoReadByteFromPort(0x71); // Flush IoReadByteFromPort(0x71); // Flush
IoGetTimeFromRtc(); GetTimeFromRtc();
KeRestoreIRQs(flags); KeRestoreIRQs(flags);
IoEnableNMI(); IoEnableNMI();
} }
@ -183,19 +193,63 @@ void RtcHandler(void)
void IoPrintRtcTime(void) void IoPrintRtcTime(void)
{ {
KernLog("[RTC Time] %s %d/%d/%d ; %d:%d:%d\n", Time_t* RtcTime = IoGetRtcTime();
WeekDays[IoRtcTime.weekday], KernLog("[RTC Time] %02d/%02d/%04d ; %02d:%02d:%02d\n",
IoRtcTime.day, RtcTime->day,
IoRtcTime.month, RtcTime->month,
IoRtcTime.year + IoRtcTime.century*100, RtcTime->year + RtcTime->century*100,
IoRtcTime.hour, RtcTime->hour,
IoRtcTime.min, RtcTime->min,
IoRtcTime.sec RtcTime->sec
); );
} }
// FIXME XXX FIXME
char* IoGetRtcTimeChar(void)
{
Time_t* RtcTime = IoGetRtcTime();
char* timeChar = "";
sprintf(timeChar, "[RTC Time] %d/%d/%d ; %d:%d:%d\n",
RtcTime->day,
RtcTime->month,
RtcTime->year + RtcTime->century*100,
RtcTime->hour,
RtcTime->min,
RtcTime->sec
);
return timeChar;
}
// END OF FIXME XXX FIXME
static void UpdateRtcTime(void)
{
ulong frequency = 32768 >> (RtcRate-1);
uchar minRemain, hourRemain, dayRemain;
IoRtcTime.sec =
(uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency) + 1) % 60);
minRemain =
(uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency) + 1) / 60);
IoRtcTime.min =
(uchar)(((ulong)IoRtcOriginTime.min + minRemain) % 60);
hourRemain =
(uchar)(((ulong)IoRtcOriginTime.min + minRemain) / 60);
IoRtcTime.hour =
(uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) % 24);
dayRemain =
(uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) / 24);
if (dayRemain) {
KeStartPanic("[RTC Time] We must shutdown this computer for your safety.\n");
}
}
Time_t* IoGetRtcTime(void) Time_t* IoGetRtcTime(void)
{ {
UpdateRtcTime();
return &IoRtcTime; return &IoRtcTime;
} }
@ -204,3 +258,4 @@ ulong IoGetRtcTicks(void)
return IoRtcTicks; return IoRtcTicks;
} }