diff --git a/Makefile b/Makefile index ab68262..3d4de79 100644 --- a/Makefile +++ b/Makefile @@ -119,7 +119,7 @@ KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources)) KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources)) all : - make OS/K -j 8 + @make OS/K -j 8 ## MISC MAKEFILE ------------------------------------------------------------- # diff --git a/ProjectTree b/ProjectTree deleted file mode 100644 index df9289b..0000000 --- a/ProjectTree +++ /dev/null @@ -1,157 +0,0 @@ -#=----------------------------------------------------------------------------=# -# GNU GPL OS/K # -# # -# Desc: # -# # -# # -# Copyright © 2018-2019 The OS/K Team # -# # -# This file is part of OS/K. # -# # -# OS/K is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# any later version. # -# # -# OS/K is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY# without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with OS/K. If not, see . # -#=----------------------------------------------------------------------------=# - - -. -├── boot -│   ├── grub -│   │   ├── grub.cfg -│   │   └── multiboot.pdf -│   ├── loader -│   │   ├── cpu -│   │   │   ├── cpu32.inc -│   │   │   └── cpu.inc -│   │   ├── io -│   │   │   └── terminal.inc -│   │   ├── mem -│   │   │   ├── management.inc -│   │   │   └── structures.inc -│   │   ├── multiboot -│   │   │   ├── check.inc -│   │   │   └── header.inc -│   │   └── loader.asm -│   └── folder.desc -├── build -│   ├── create_disk.sh -│   ├── install-os-k.sh -│   └── kernel.ld -├── include -│   ├── ex -│   │   ├── lock.h -│   │   └── malloc.h -│   ├── init -│   │   ├── boot.h -│   │   └── mboot.h -│   ├── io -│   │   ├── ata.h -│   │   ├── cursor.h -│   │   ├── keyb.h -│   │   ├── spkr.h -│   │   └── vga.h -│   ├── ke -│   │   ├── cpuid.h -│   │   ├── idt.h -│   │   ├── proc.h -│   │   ├── sched.h -│   │   ├── spinlock.h -│   │   └── time.h -│   ├── lib -│   │   ├── buf.h -│   │   └── list.h -│   ├── mm -│   │   ├── heap.h -│   │   ├── malloc.h -│   │   └── mm.h -│   ├── po -│   │   └── shtdwn.h -│   ├── sh -│   │   ├── argv.h -│   │   └── shell.h -│   ├── asm.h -│   ├── errno.h -│   ├── kernel.h -│   ├── libc.h -│   └── vers.h -├── kaleid -│   ├── kernel -│   │   ├── init -│   │   │   ├── info.c -│   │   │   ├── init.c -│   │   │   ├── ssp.c -│   │   │   └── table.c -│   │   ├── io -│   │   │   ├── ata.asm -│   │   │   ├── ata.c -│   │   │   ├── cursor.c -│   │   │   ├── keyb.c -│   │   │   ├── spkr.c -│   │   │   └── vga.c -│   │   ├── ke -│   │   │   ├── cpuf.asm -│   │   │   ├── cpuf.inc -│   │   │   ├── cpuid.c -│   │   │   ├── idt.c -│   │   │   ├── isr.asm -│   │   │   ├── log.c -│   │   │   ├── panic.c -│   │   │   ├── pit.c -│   │   │   └── rtc.c -│   │   ├── mm -│   │   │   ├── gdt.asm -│   │   │   ├── gdt.c -│   │   │   ├── heap.c -│   │   │   ├── malloc.c -│   │   │   ├── map.c -│   │   │   ├── paging.asm -│   │   │   └── paging.c -│   │   ├── po -│   │   │   └── shtdwn.c -│   │   ├── ps -│   │   │   └── sched.c -│   │   └── sh -│   │   ├── argv.c -│   │   ├── musage.c -│   │   ├── shcmds.c -│   │   ├── shell.c -│   │   └── shell.h -│   ├── libbuf -│   │   ├── bclose.c -│   │   ├── bflush.c -│   │   ├── bgetc.c -│   │   ├── bmisc.c -│   │   ├── bopen.c -│   │   ├── bprint.c -│   │   ├── bputc.c -│   │   ├── bread.c -│   │   ├── bscan.c -│   │   ├── bscroll.c -│   │   └── bwrite.c -│   └── libc -│   ├── atoi.c -│   ├── ctype.c -│   ├── errno.c -│   ├── itoa.c -│   ├── mem.c -│   ├── rand.c -│   ├── sprintf.c -│   ├── string.c -│   └── strtol.c -├── AUTHORS -├── ChangeLog -├── COPYING -├── Makefile -├── ProjectTree -└── README.md - -28 directories, 101 files diff --git a/include/ke/time.h b/include/ke/time.h index c96c9fc..432239c 100644 --- a/include/ke/time.h +++ b/include/ke/time.h @@ -45,7 +45,7 @@ struct Time_t struct TimerFilo_t { uint countDown; - ulong sema; // semaphore, don't know how it is for instance + ulong sema; } __attribute__((packed)); void KeSetupRTC(void); @@ -53,13 +53,15 @@ void KeEnableRTC(void); ulong KeGetTimeStamp(void); ulong KeGetClockTicks(void); +Time_t *KeGetOriginTime(void); void KeDelayExecution(uint); Time_t *KeGetCurTime(void); char *KeFormatCurTime(void); +void KeSetCurTime(Time_t); -void KeEnablePIT(void); -void KeSleep(uint); +void KeEnablePIT(void); +void KeSleep(uint); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index 3e11afa..182ce28 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -27,27 +27,26 @@ #include #define COUNTDONE 1 -#define PIT_FREQUENCY 100 // Hz = 10ms +#define PIT_FREQUENCY 1000 // Hz = 10ms static TimerFilo_t timerFilo[20]; //20 concurrent sleep max static ulong Ticks = 0; +static Time_t CurTime; +static char TimeFmtBuf[22] = { 0 }; // // ISR handler for the Programmable Interval Timer // static void HandlePIT(ISRFrame_t *regs) { + Ticks++; for (uchar i = 0; i < 20; i++) { - // debug - //DebugLog("Hello world of PIT ticks !\n"); - // - if (timerFilo[i].countDown > 0) { timerFilo[i].countDown--; if (timerFilo[i].countDown == 0) - i; //XXX SendMessage a message to timerFifo[i].sema that COUNTDONE; + timerFilo[i].sema = 1; } KeSendEOItoPIC(0x28); @@ -72,15 +71,17 @@ void KeSleep(uint delay) return; timerBlock->countDown = delay; - // wait for a message on timerFifo[i].sema -} + while (!timerBlock->sema) { + KeDelayExecution(1); + } + timerBlock->sema = 0; +} void KeEnablePIT(void) { ulong flags = KePauseIRQs(); - char readIrqs; - uint divisor = 1193180 / PIT_FREQUENCY; + uint divisor = 1193180 / PIT_FREQUENCY; // PIT base frequency / Needed frequency KeRegisterISR(HandlePIT, 0x20); @@ -92,8 +93,97 @@ void KeEnablePIT(void) // Setting up the IRQs KeUnmaskIRQ(0); - DebugLog("\tPIT activated with rate generator mode 10ms\n"); + DebugLog("\tPIT activated with rate generator mode %d ms\n", 1000/PIT_FREQUENCY); KeRestoreIRQs(flags); KeEnableNMI(); } + +char *KeFormatCurTime(void) +{ + Time_t *RtcTime = KeGetCurTime(); + snprintf(TimeFmtBuf, sizeof(TimeFmtBuf), + "%02d/%02d/%04d - %02d:%02d:%02d", + RtcTime->day, + RtcTime->month, + RtcTime->year + RtcTime->century*100, + RtcTime->hour, + RtcTime->min, + RtcTime->sec + ); + return TimeFmtBuf; +} + + +static void UpdateCurTime(void) +{ + ulong frequency = PIT_FREQUENCY; + uchar minRemain, hourRemain, dayRemain; + + CurTime.sec = + (uchar)(((ulong)KeGetOriginTime()->sec + (Ticks / frequency)) % 60); + + minRemain = + (uchar)(((ulong)KeGetOriginTime()->sec + (Ticks / frequency)) / 60); + + CurTime.min = + (uchar)(((ulong)KeGetOriginTime()->min + minRemain) % 60); + + hourRemain = + (uchar)(((ulong)KeGetOriginTime()->min + minRemain) / 60); + + CurTime.hour = + (uchar)(((ulong)KeGetOriginTime()->hour + hourRemain) % 24); + + dayRemain = + (uchar)(((ulong)KeGetOriginTime()->hour + hourRemain) / 24); + + CurTime.day = + (uchar)(((ulong)KeGetOriginTime()->day + dayRemain) % 30); + +} + +Time_t* KeGetCurTime(void) +{ + UpdateCurTime(); + return &CurTime; +} + +void KeSetCurTime(Time_t time) +{ + CurTime = time; +} + +static uint IsLeapYear(uint year) +{ + if (!(year % 4)) { + return 0; + } + + return year % 100 == 0 + ? (year % 400 == 0) + : 1; +} + +static uint DaysInMonth(uint month, uint year) +{ + return (month == 2) + ? (28 + IsLeapYear(year)) + : 31 - (month - 1) % 7 % 2; +} + +ulong KeGetTimeStamp(void) +{ + Time_t *time = KeGetCurTime(); + + uint dpy = 365 + IsLeapYear(time->year); + uint dim = DaysInMonth(time->month, time->year + time->century * 100); + + return time->sec + + time->min * 60 + + time->hour * 60 * 60 + + time->day * 24 * 60 * 60 + + time->month * dim * 24 * 60 * 60 + + (time->year + time->century * 100) + * dpy * 24 * 60 * 60; +} diff --git a/kaleid/kernel/ke/rtc.c b/kaleid/kernel/ke/rtc.c index a6f1bdf..3702eef 100644 --- a/kaleid/kernel/ke/rtc.c +++ b/kaleid/kernel/ke/rtc.c @@ -28,7 +28,6 @@ static ulong Ticks = 0; static Time_t OriginTime; -static Time_t CurTime; // TODO asnprintf() static char TimeFmtBuf[22] = { 0 }; @@ -158,14 +157,7 @@ static void GetTimeFromRTC(void) OriginTime.hour = ((OriginTime.hour & 0x7)+ 10) % 24; } - CurTime.sec = OriginTime.sec; - CurTime.min = OriginTime.min; - CurTime.hour = OriginTime.hour; - CurTime.weekday = OriginTime.weekday; - CurTime.day = OriginTime.day; - CurTime.month = OriginTime.month; - CurTime.year = OriginTime.year; - CurTime.century = OriginTime.century; + KeSetCurTime(OriginTime); } // @@ -179,95 +171,16 @@ static void HandleRTC(ISRFrame_t *regs) KeSendEOItoPIC(0x28); } -char *KeFormatCurTime(void) -{ - Time_t *RtcTime = KeGetCurTime(); - snprintf(TimeFmtBuf, sizeof(TimeFmtBuf), - "%02d/%02d/%04d - %02d:%02d:%02d", - RtcTime->day, - RtcTime->month, - RtcTime->year + RtcTime->century*100, - RtcTime->hour, - RtcTime->min, - RtcTime->sec - ); - return TimeFmtBuf; -} - - -static void UpdateCurTime(void) -{ - ulong frequency = 32768 >> (RTC_RATE - 1); - uchar minRemain, hourRemain, dayRemain; - - CurTime.sec = - (uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) % 60); - - minRemain = - (uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) / 60); - - CurTime.min = - (uchar)(((ulong)OriginTime.min + minRemain) % 60); - - hourRemain = - (uchar)(((ulong)OriginTime.min + minRemain) / 60); - - CurTime.hour = - (uchar)(((ulong)OriginTime.hour + hourRemain) % 24); - - dayRemain = - (uchar)(((ulong)OriginTime.hour + hourRemain) / 24); - - CurTime.day = - (uchar)(((ulong)OriginTime.day + dayRemain) % 30); - -} - -Time_t* KeGetCurTime(void) -{ - UpdateCurTime(); - return &CurTime; -} - -static uint IsLeapYear(uint year) -{ - if (!(year % 4)) { - return 0; - } - - return year % 100 == 0 - ? (year % 400 == 0) - : 1; -} - -static uint DaysInMonth(uint month, uint year) -{ - return (month == 2) - ? (28 + IsLeapYear(year)) - : 31 - (month - 1) % 7 % 2; -} - -ulong KeGetTimeStamp(void) -{ - Time_t *time = KeGetCurTime(); - - uint dpy = 365 + IsLeapYear(time->year); - uint dim = DaysInMonth(time->month, time->year + time->century * 100); - - return time->sec - + time->min * 60 - + time->hour * 60 * 60 - + time->day * 24 * 60 * 60 - + time->month * dim * 24 * 60 * 60 - + (time->year + time->century * 100) - * dpy * 24 * 60 * 60; -} - ulong KeGetClockTicks(void) { return Ticks; } +Time_t* KeGetOriginTime(void) +{ + return &OriginTime; +} + void KeEnableRTC(void) { ulong flags = KePauseIRQs(); diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 3530d2f..a284fd6 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -132,6 +132,14 @@ error_t CmdDie(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdSleep(int argc, char **argv, char *cmdline) +{ + int delay = ShAtoi(argv[1]); + + KeSleep(delay); + return EOK; +} + error_t CmdDumpATASect(int argc, char **argv, char *cmdline) { char sector[512] = {0}; @@ -317,6 +325,7 @@ Command_t shcmdtable[] = { "march", CmdStarWars, "Play the Imperial March" }, { "time", CmdTime, "Print time" }, { "ver", CmdVersion, "Version and legal infos" }, + { "sleep", CmdSleep, "Sleep x ms" }, { NULL, NULL, NULL } };