Przeglądaj źródła

Display system clock

Johannes Zellner 8 miesięcy temu
rodzic
commit
2b8c6feafc
5 zmienionych plików z 112 dodań i 6 usunięć
  1. 6 3
      Makefile
  2. 3 0
      io.h
  3. 71 0
      rtc.c
  4. 7 0
      rtc.h
  5. 25 3
      terminal.c

+ 6 - 3
Makefile

@@ -24,7 +24,10 @@ kernel.o: kernel.c io.h terminal.h timer.h
 io.o: io.c io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-terminal.o: terminal.c terminal.h io.h timer.h
+terminal.o: terminal.c terminal.h io.h rtc.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 boot.elf: boot.o
@@ -33,7 +36,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o terminal.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -54,4 +57,4 @@ run: image.bin
 	qemu-system-i386 -M pc -drive file=image.bin,format=raw 2>/dev/null || qemu-system-x86_64 -M pc -drive file=image.bin,format=raw
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o terminal.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin

+ 3 - 0
io.h

@@ -6,6 +6,9 @@
 #define IO_VGA_ROWS      25
 #define IO_CONTENT_ROWS  IO_VGA_ROWS
 #define IO_CONTENT_SIZE  (IO_CONTENT_ROWS * IO_VGA_COLS)
+/* Top-right datetime: row 0, 19 chars "YYYY-MM-DD HH:MM:SS" */
+#define IO_DATETIME_LEN  19
+#define IO_DATETIME_POS  (IO_VGA_COLS - IO_DATETIME_LEN)
 
 void io_init(void);
 void io_clear_screen(void);

+ 71 - 0
rtc.c

@@ -0,0 +1,71 @@
+/* RTC (CMOS) date/time read. */
+
+#include "rtc.h"
+
+#define CMOS_ADDR  0x70
+#define CMOS_DATA  0x71
+
+static inline unsigned char cmos_read(unsigned char reg)
+{
+	unsigned char v;
+	/* Bit 7 on 0x70 disables NMI during access. */
+	__asm__ volatile ("outb %b1, %w0" : : "Nd"(CMOS_ADDR), "a"((unsigned char)(reg | 0x80)));
+	__asm__ volatile ("inb %w1, %0" : "=a"(v) : "Nd"(CMOS_DATA));
+	return v;
+}
+
+static unsigned int bcd_to_bin(unsigned char bcd)
+{
+	return (unsigned int)((bcd >> 4) * 10 + (bcd & 0x0F));
+}
+
+void rtc_read_datetime(char *buf)
+{
+	unsigned char sec, min, hour, day, month, year;
+	unsigned int s, m, h, d, mo, y;
+
+	/* Wait for RTC update to finish. Timeout to avoid hang. */
+	{
+		unsigned int n = 0;
+		while ((cmos_read(0x0A) & 0x80) && ++n < 10000)
+			;
+	}
+
+	sec   = cmos_read(0x00);
+	min   = cmos_read(0x02);
+	hour  = cmos_read(0x04);
+	day   = cmos_read(0x07);
+	month = cmos_read(0x08);
+	year  = cmos_read(0x09);
+
+	s  = bcd_to_bin(sec);
+	m  = bcd_to_bin(min);
+	h  = bcd_to_bin(hour);
+	d  = bcd_to_bin(day);
+	mo = bcd_to_bin(month);
+	y  = bcd_to_bin(year);
+	y += 2000;
+	if (y < 2000)
+		y += 100;
+
+	buf[0]  = (char)('0' + (y / 1000));
+	buf[1]  = (char)('0' + ((y / 100) % 10));
+	buf[2]  = (char)('0' + ((y / 10) % 10));
+	buf[3]  = (char)('0' + (y % 10));
+	buf[4]  = '-';
+	buf[5]  = (char)('0' + (mo / 10));
+	buf[6]  = (char)('0' + (mo % 10));
+	buf[7]  = '-';
+	buf[8]  = (char)('0' + (d / 10));
+	buf[9]  = (char)('0' + (d % 10));
+	buf[10] = ' ';
+	buf[11] = (char)('0' + (h / 10));
+	buf[12] = (char)('0' + (h % 10));
+	buf[13] = ':';
+	buf[14] = (char)('0' + (m / 10));
+	buf[15] = (char)('0' + (m % 10));
+	buf[16] = ':';
+	buf[17] = (char)('0' + (s / 10));
+	buf[18] = (char)('0' + (s % 10));
+	buf[19] = '\0';
+}

+ 7 - 0
rtc.h

@@ -0,0 +1,7 @@
+#ifndef RTC_H
+#define RTC_H
+
+/* Write formatted date/time into buf: "YYYY-MM-DD HH:MM:SS" (19 chars + NUL). */
+void rtc_read_datetime(char *buf);
+
+#endif

+ 25 - 3
terminal.c

@@ -1,12 +1,32 @@
-/* Terminal: prompt, blinking cursor, echo loop. */
+/* Terminal: prompt, datetime (top right), blinking cursor, echo loop. */
 
 #include "io.h"
+#include "rtc.h"
 #include "terminal.h"
 #include "timer.h"
 
+#define DATETIME_MS 1000
+static unsigned int last_datetime_tick;
+static char datetime_buf[IO_DATETIME_LEN + 1];
+
 static int cursor_visible;
 static unsigned int last_cursor_tick;
 
+static void update_datetime(void)
+{
+	unsigned int t = get_ticks_ms();
+	if (t - last_datetime_tick >= DATETIME_MS) {
+		last_datetime_tick = t;
+		rtc_read_datetime(datetime_buf);
+		{
+			unsigned int i;
+			for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
+				io_vga_write(IO_DATETIME_POS + i,
+					(unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
+		}
+	}
+}
+
 static void update_cursor_blink(void)
 {
 	unsigned int t = get_ticks_ms();
@@ -25,13 +45,15 @@ static const char prompt[] __attribute__((section(".text"))) = "\n> ";
 
 void terminal_run(void)
 {
-	last_cursor_tick = get_ticks_ms();
-	cursor_visible   = 1;
+	last_cursor_tick   = get_ticks_ms();
+	last_datetime_tick = get_ticks_ms();
+	cursor_visible     = 1;
 
 	io_putstr(msg);
 	io_putstr(prompt);
 
 	for (;;) {
+		update_datetime();
 		update_cursor_blink();
 		{
 			char c = io_poll_key();