add time.c

This commit is contained in:
Hojun-Cho 2024-12-05 21:41:07 +09:00
parent 7acbe88026
commit dbb2d58280
3 changed files with 71 additions and 1 deletions

View File

@ -5,7 +5,7 @@ PROG = boot
SRCS = srt0.S gdt.S boot.c SRCS = srt0.S gdt.S boot.c
OBJS = srt0.o gdt.o boot.o OBJS = srt0.o gdt.o boot.o
ESRCS = console.c print.c a20.c ESRCS = console.c print.c a20.c time.c
OBJS += $(ESRCS:.c=.o) OBJS += $(ESRCS:.c=.o)
include $(SDIR)/Makefile.inc include $(SDIR)/Makefile.inc

View File

@ -11,6 +11,9 @@ void print(const char *fmt, ...);
// a20.c // a20.c
void a20up(void); void a20up(void);
// long.c
long getsecs(void);
static __inline void static __inline void
outb(int port, u8 data) outb(int port, u8 data)
{ {

67
boot/time.c Normal file
View File

@ -0,0 +1,67 @@
#include <u.h>
#include "dat.h"
#include "fn.h"
// https://stanislavs.org/helppc/int_1a.html
#define INT_TIME 2<<8
#define INT_DATE 4<<8
static int
bcd2int(u8 c)
{
return ((c & 0xf0)/8*5+(c & 0x0f));
}
static int
isleap(int y)
{
return ((y % 4 == 0 && y % 100) || (y % 400 == 0));
}
static long
date2day(int y, u8 m, u8 d)
{
long t;
static const u16 m2d[] = {(u16)-1,
0,31,59,90,120,151,181,212,243,273,304,334,365};
t = (y - 1970) * 365 + m2d[m] + d - 1;
y -= (m <= 2);
for(; y >= 1970; --y){
if(isleap(y))
t += 1;
}
return t;
}
// bios long data
static void
int1a(int f, u8 *b)
{
__asm volatile("int $0x3a \n\t"
"setc %b0\n\t"
"movb %%ch, 0(%2)\n\t"
"movb %%cl, 1(%2)\n\t"
"movb %%dh, 2(%2)\n\t"
"movb %%dl, 3(%2)\n\t"
: "=a" (f)
: "0" (f), "r" (b)
: "%ecx", "%edx", "cc");
b[0] = bcd2int(b[0]);
b[1] = bcd2int(b[1]);
b[2] = bcd2int(b[2]);
b[3] = bcd2int(b[3]);
}
long
getsecs(void)
{
u8 t[4], d[4];
long s;
int1a(INT_TIME, t);
int1a(INT_DATE, d);
s = (date2day(d[0]*100+d[1], d[2], d[3]) * 24 + t[0]) * 60
+ (t[1] * 60) + t[2];
return s;
}