CTAOS v2
This commit is contained in:
25
kernel/clock/cmos.h
Normal file
25
kernel/clock/cmos.h
Normal file
@ -0,0 +1,25 @@
|
||||
volatile static byte cmos_data[128];
|
||||
|
||||
void cmos_write ()
|
||||
{
|
||||
byte i;
|
||||
for (i = 0; i < 128; i++) {
|
||||
//asm volatile ("cli");
|
||||
outportb(0x70, i);
|
||||
iowait();
|
||||
outportb(0x71, cmos_data[i]);
|
||||
//asm volatile ("sti");
|
||||
}
|
||||
}
|
||||
|
||||
void cmos_read ()
|
||||
{
|
||||
byte i;
|
||||
for (i = 0; i < 128; i++) {
|
||||
//asm volatile ("cli");
|
||||
outportb(0x70, i);
|
||||
iowait();
|
||||
cmos_data[i] = inportb(0x71);
|
||||
//asm volatile ("sti");
|
||||
}
|
||||
}
|
76
kernel/clock/pictimer.c
Normal file
76
kernel/clock/pictimer.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <system.h>
|
||||
#include "time.c"
|
||||
int timer_ticks = 0;
|
||||
int timer_hz;
|
||||
|
||||
void timer_phase(int hz)
|
||||
{
|
||||
int divisor = 1193180/hz; // Calculate the divisor
|
||||
outportb(0x43, 0x36); // Set our command byte 0x36
|
||||
outportb(0x40, divisor&0xFF); // Set low byte
|
||||
outportb(0x40, divisor>>8); // Set high byte
|
||||
timer_hz = hz;
|
||||
}
|
||||
|
||||
void clock_show (int uptime_secs)
|
||||
{
|
||||
int i;
|
||||
for (i=0;i<80;i++) putc_pos_font (i, 0, ' ', 0x02, 0x0F);
|
||||
puts_pos_font (64, 0, "Uptime:", 0x02, 0x0E);
|
||||
|
||||
unsigned int uptime;
|
||||
|
||||
uptime = uptime_secs%60; // Seconds
|
||||
uptime += 100* ((uptime_secs/60)%60); // Minutes
|
||||
uptime += 10000*(uptime_secs/3600); // Hours
|
||||
|
||||
for (i=79;i>71;i--) {
|
||||
if (i==77 || i==74) {
|
||||
if (uptime_secs%2==0) putc_pos_font(i, 0, ':', 0x02, 0x0F);
|
||||
else putc_pos_font(i, 0, ' ', 0x02, 0x0F);
|
||||
}
|
||||
else {
|
||||
putc_pos_font(i, 0, (uptime%10)+'0', 0x02, 0x0F);
|
||||
uptime/=10;
|
||||
}
|
||||
}
|
||||
|
||||
// PRINT CURRENT TIME
|
||||
uptime = clock.seconds; // Seconds
|
||||
uptime += 100* clock.minutes; // Minutes
|
||||
uptime += 10000*clock.hours; // Hours
|
||||
|
||||
for (i=9;i>1;i--) {
|
||||
if (i==7 || i==4) {
|
||||
if (uptime_secs%2==0) putc_pos_font(i, 0, ':', 0x02, 0x0F);
|
||||
else putc_pos_font(i, 0, ' ', 0x02, 0x0F);
|
||||
}
|
||||
else {
|
||||
putc_pos_font(i,0, (uptime%10)+'0', 0x02, 0x0F);
|
||||
uptime/=10;
|
||||
}
|
||||
}
|
||||
if (clock.am_pm==0) puts_pos_font(10, 0, "am", 0x02, 0x0F);
|
||||
else puts_pos_font(10, 0, "pm", 0x02, 0x0F);
|
||||
|
||||
// PRINT DATE
|
||||
putc_pos_font(32, 0, (clock.day/10)+'0', 0x02, 0x0E);
|
||||
putc_pos_font(33, 0, (clock.day%10)+'0', 0x02, 0x0E);
|
||||
puts_pos_font(35, 0, (char*)clock_months[clock.month], 0x02, 0x0F);
|
||||
putc_pos_font(35+strlen(clock_months[clock.month])+1, 0, (clock.century/10)+'0', 0x02, 0x0E);
|
||||
putc_pos_font(35+strlen(clock_months[clock.month])+2, 0, (clock.century%10)+'0', 0x02, 0x0E);
|
||||
putc_pos_font(35+strlen(clock_months[clock.month])+3, 0, (clock.year/10)+'0', 0x02, 0x0E);
|
||||
putc_pos_font(35+strlen(clock_months[clock.month])+4, 0, (clock.year%10)+'0', 0x02, 0x0E);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void timer_handler(regs *r)
|
||||
{
|
||||
timer_ticks++;
|
||||
|
||||
if (timer_ticks % timer_hz == 0) {
|
||||
clock_show (timer_ticks / timer_hz);
|
||||
clock_inc();
|
||||
}
|
||||
}
|
143
kernel/clock/time.c
Normal file
143
kernel/clock/time.c
Normal file
@ -0,0 +1,143 @@
|
||||
#ifndef __TIME_C
|
||||
#define __TIME_C
|
||||
|
||||
#include "cmos.h"
|
||||
|
||||
static const char* clock_months[] = {0,
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"};
|
||||
static const char* clock_weekdays[] = {0,
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
|
||||
|
||||
byte clock_months_len[] = {
|
||||
0,
|
||||
31, // January
|
||||
28, // February
|
||||
31, // March
|
||||
30, // April
|
||||
31, // May
|
||||
30, // June
|
||||
31, // July
|
||||
31, // August
|
||||
30, // September
|
||||
31, // October
|
||||
30, // November
|
||||
31 // December
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
byte seconds;
|
||||
byte minutes;
|
||||
byte hours;
|
||||
byte weekday;
|
||||
byte day;
|
||||
byte month;
|
||||
byte year;
|
||||
byte century;
|
||||
byte am_pm;
|
||||
|
||||
} TIME;
|
||||
volatile static TIME clock;
|
||||
|
||||
void RTC_get_time()
|
||||
{
|
||||
cmos_read();
|
||||
|
||||
if ((cmos_data[0x0b]&4)==0) // BCD = true;
|
||||
{
|
||||
clock.seconds = (cmos_data[0x00]%16) + 10*(cmos_data[0x00]/16);
|
||||
clock.minutes = (cmos_data[0x02]%16) + 10*(cmos_data[0x02]/16);
|
||||
if ((cmos_data[0x0b]&2)==0) { // AM/PM
|
||||
if (cmos_data[0x04]&80) { // pm
|
||||
clock.hours = ((cmos_data[0x04]-0x80)%16) + 10*((cmos_data[0x04]-0x80)/16);
|
||||
clock.am_pm = 1;
|
||||
}
|
||||
else { // am
|
||||
clock.hours = (cmos_data[0x04]%16) + 10*(cmos_data[0x04]/16);
|
||||
clock.am_pm = 0;
|
||||
}
|
||||
}
|
||||
else { // 24 hours
|
||||
clock.hours = (cmos_data[0x04]%16) + 10*(cmos_data[0x04]/16);
|
||||
if (clock.hours > 12) {
|
||||
clock.am_pm = 1;
|
||||
clock.hours -= 12;
|
||||
}
|
||||
else clock.am_pm = 0;
|
||||
}
|
||||
|
||||
clock.weekday = (cmos_data[0x06]%16) + 10*(cmos_data[0x06]/16);
|
||||
clock.day = (cmos_data[0x07]%16) + 10*(cmos_data[0x07]/16);
|
||||
clock.month = (cmos_data[0x08]%16) + 10*(cmos_data[0x08]/16);
|
||||
clock.year = (cmos_data[0x09]%16) + 10*(cmos_data[0x09]/16);
|
||||
clock.century = (cmos_data[0x32]%16) + 10*(cmos_data[0x32]/16);
|
||||
}
|
||||
|
||||
else {//BCD = false;
|
||||
clock.seconds = cmos_data[0x00];
|
||||
clock.minutes = cmos_data[0x02];
|
||||
if ((cmos_data[0x0b]&2)==0) { // AM/PM
|
||||
if (cmos_data[0x04]&80) { // pm
|
||||
clock.hours = cmos_data[0x04]-0x80;
|
||||
clock.am_pm = 1;
|
||||
}
|
||||
else { // am
|
||||
clock.hours = cmos_data[0x04];
|
||||
clock.am_pm = 0;
|
||||
}
|
||||
}
|
||||
else { // 24 hours
|
||||
clock.hours = cmos_data[0x02];
|
||||
if (clock.hours > 12) {
|
||||
clock.am_pm = 1;
|
||||
clock.hours -= 12;
|
||||
}
|
||||
else clock.am_pm = 0;
|
||||
}
|
||||
clock.weekday = cmos_data[0x06];
|
||||
clock.day = cmos_data[0x07];
|
||||
clock.month = cmos_data[0x08];
|
||||
clock.year = cmos_data[0x09];
|
||||
clock.century = cmos_data[0x32];
|
||||
}
|
||||
// Leap years
|
||||
if (clock.year % 4 == 0) clock_months_len[2]=29;
|
||||
|
||||
}
|
||||
|
||||
void clock_inc()
|
||||
{
|
||||
// New minute
|
||||
if (++clock.seconds > 59) {
|
||||
clock.seconds = 0;
|
||||
// New hour
|
||||
if (++clock.minutes > 59) {
|
||||
clock.minutes = 0;
|
||||
clock.hours++;
|
||||
if (clock.hours == 12 && clock.am_pm == 1) { // 11:59pm -> 0:00am
|
||||
clock.hours = 0; clock.am_pm = 0;
|
||||
// New day
|
||||
clock.weekday = 1+(clock.weekday%7);
|
||||
// New month
|
||||
if (++clock.day > clock_months_len[clock.month]) {
|
||||
clock.day = 1;
|
||||
// New year
|
||||
if (++clock.month>12) {
|
||||
clock.month = 1;
|
||||
// New century
|
||||
if (++clock.year > 99) {
|
||||
clock.year = 0;
|
||||
clock.century++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (clock.hours == 12 && clock.am_pm == 0) // 11:59am -> 12:00pm
|
||||
clock.am_pm = 1;
|
||||
else if (clock.hours == 13 && clock.am_pm == 1) // 12:59pm -> 1:59pm
|
||||
clock.hours = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user