Skip to content

Commit

Permalink
Tiny RTC refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
valtyr committed Jun 15, 2021
1 parent 5df9241 commit 4378c98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
40 changes: 15 additions & 25 deletions src/impl/kernel/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@ typedef enum
CMOS_R_STATUS_B = 0x0B,
} CMOSRegister;

static RTCTime currentTime;

void tiny_delay()
int read_cmos(CMOSRegister r)
{
for (int i = 10; i > 0; i--)
__asm__ volatile("nop");
outb(0x70, r);
return inb(0x71);
}

int read_cmos(CMOSRegister r)
int update_in_progress()
{
outb(0x70, r);
int value = inb(0x71);
tiny_delay(); // Supposedly this is good practice hmm
return value;
return read_cmos(CMOS_R_STATUS_A) & 0x80;
}

RTCTime initial_blocking_read()
RTCTime RTCNow()
{
int updateInProgress = 0;
while (!updateInProgress)
{
updateInProgress = read_cmos(CMOS_R_STATUS_A) >> 7 & 0x1;
}
while (updateInProgress)

while (update_in_progress())
{
updateInProgress = read_cmos(CMOS_R_STATUS_A) >> 7 & 0x1;
// Chill for a bit

// We could get bad state here...
// might want to read the registers
// twice until they match.
}

int seconds = read_cmos(CMOS_R_SECONDS);
Expand Down Expand Up @@ -98,18 +93,13 @@ void RTCInit()

sti();

currentTime = initial_blocking_read();
BIOSPrintf("RTC initialized!\n");
RTCTime currentTime = RTCNow();
BIOSPrintf(
"Current time: %d:%2d:%2d %d/%d/%d\n",
"RTC initialized at %d:%02d:%02d %02d/%02d/%d\n",
currentTime.hours,
currentTime.minutes,
currentTime.seconds,
currentTime.day,
currentTime.month + 1,
currentTime.year);
}

RTCTime RTCGetTime()
{
}
3 changes: 1 addition & 2 deletions src/intf/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ typedef struct RTCTime
} RTCTime;

void RTCInit();

RTCTime RTCGetTime();
RTCTime RTCNow();

0 comments on commit 4378c98

Please sign in to comment.