Quellcode durchsuchen

9.106s-init: get the current time from cmos

rain vor 5 Jahren
Ursprung
Commit
83a9db0e33
5 geänderte Dateien mit 123 neuen und 2 gelöschten Zeilen
  1. 1 0
      LINES
  2. 6 2
      README.md
  3. 23 0
      include/kernel/init.h
  4. 1 0
      include/kernel/types.h
  5. 92 0
      init/main.c

+ 1 - 0
LINES

@@ -37,4 +37,5 @@
 2021-8-14 8.275: 8229 lines, 55 files, rain
 2021-8-15 8.413: 8413 lines, 57 files, rain
 2021-8-17 8.990: 8990 lines, 59 files, rain
+2021-8-18 9.106: 9106 lines, 59 files, rain
 

+ 6 - 2
README.md

@@ -141,13 +141,17 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we can run in the user-mode. 
 
 8.229u-user:  
-&nbsp;&nbsp;&nbsp;&nbsp;add syscall `sys_print` (will remove it later), and now user can call it 
+&nbsp;&nbsp;&nbsp;&nbsp;add syscall `sys_print` (will remove it later), and now user can call it <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;to put a message on the screen. 
 
 8.463u-tty:  
-&nbsp;&nbsp;&nbsp;&nbsp;move the `tty.c` from module, to the main kernel because we need it, and 
+&nbsp;&nbsp;&nbsp;&nbsp;move the `tty.c` from module, to the main kernel because we need it, and <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;it's not a device. 
 
 8.990u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working. drunk, do it later. 
 
+9.106s-init:  
+&nbsp;&nbsp;&nbsp;&nbsp;get the time from cmos, and make it to unix time-stamp. 
+&nbsp;&nbsp;&nbsp;&Nbsp;and this version looks work. 
+

+ 23 - 0
include/kernel/init.h

@@ -177,5 +177,28 @@ struct tty {
 };
 
 
+struct time {
+	__uint16_t year;
+	__uint8_t month;
+	__uint8_t day;
+
+	__uint8_t hour;
+	__uint8_t min;
+	__uint8_t sec;
+};
+
+
+/* seconds per minutes */
+#define MIN 	60
+#define HOUR 	(MIN * 60)
+#define DAY 	(HOUR * 24)
+
+#define YEAR 	(DAY * 365)
+
+#define isleapyear(year) (			\
+	(((year) % 4) && (!(year) % 100)) || 	\
+	((!(year) % 100) && (!(year) % 400)))
+
+
 #endif
 

+ 1 - 0
include/kernel/types.h

@@ -48,6 +48,7 @@ typedef __uint_t 	__uint32_t;
 typedef __ulong_t 	__uint64_t;
 
 typedef __ulong_t 	size_t;
+typedef __ulong_t 	time_t;
 
 #endif
 

+ 92 - 0
init/main.c

@@ -51,6 +51,22 @@
 
 /* we save boot-time arguments at that address where boot was. */
 struct bootarg *bootarg = (struct bootarg *) 0x7c00;
+void time_init(void);
+
+
+/* well, it looks like linux */
+
+#define CMOS_READ(addr) ({		\
+	outb(addr, 0x70);	\
+	inb(0x71);			\
+})					\
+
+/* only for byte */
+
+#define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
+
+void time_init(void);
+time_t mktime(struct time *);
 
 
 /* 
@@ -87,6 +103,82 @@ int init(void) {
 	else 
 		printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
 
+	time_init();
+
 	return 0;
 }
 
+void time_init(void) {
+	/* get current time, and save it to `startup_time` */
+	struct time tm;
+
+	/* it will execute when 'sec' changed for the second time */
+
+	do {
+		tm.sec = CMOS_READ(0);
+		tm.min = CMOS_READ(2);
+		tm.hour = CMOS_READ(4);
+		tm.day = CMOS_READ(7);
+		/* mouth is 1 - 12 */
+		tm.month = CMOS_READ(8) - 1;
+		/* this is the year only 2 bits (1970 -> 70) */
+
+		tm.year = CMOS_READ(9);
+	} while (tm.sec != CMOS_READ(0));
+
+	BCD2BIN(tm.sec);
+	BCD2BIN(tm.min);
+	BCD2BIN(tm.hour);
+	BCD2BIN(tm.day);
+	BCD2BIN(tm.year);
+
+	tm.year += tm.year >= 70 ? 1900 : 2000;
+
+	printk(white, "current time: %d %d %d  %d:%d:%d. \n", 
+			tm.year, tm.month + 1, tm.day, tm.hour, tm.min, tm.sec);
+
+	printk(white, "current unix time-stamp: %d. \n", mktime(&tm));
+
+	return;
+}
+
+
+static int month[] = {
+	0, 
+	31, 
+	31 + 29, 
+	31 + 29 + 31, 
+	31 + 29 + 31 + 30, 
+	31 + 29 + 31 + 30 + 31, 
+	31 + 29 + 31 + 30 + 31 + 30, 
+	31 + 29 + 31 + 30 + 31 + 30 + 31, 
+	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, 
+	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 
+	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 
+	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
+};
+
+
+time_t mktime(struct time *tm) {
+	time_t res;
+	int year;
+
+	year = tm->year - 1970;
+
+	/* this not includes this year */
+	res = year * YEAR + ((year + 1) >> 2) * DAY;
+	
+	/* if this year isn't a leap-year, and current-month > 2 */
+
+	/* month starts at 0 */
+	if (!isleapyear(tm->year) && tm->month > 1) 
+		res -= DAY;
+
+	res += month[tm->month] * DAY;
+	res += (tm->day - 1) * DAY;
+	res += tm->hour * HOUR;
+	res += tm->min * MIN;
+	res += tm->sec;
+
+	return res;
+}