Преглед на файлове

Add a clock demo which also introduces proper vga mode

Johannes Zellner преди 8 месеца
родител
ревизия
71bca761f4
променени са 9 файла, в които са добавени 447 реда и са изтрити 2 реда
  1. 8 2
      Makefile
  2. 182 0
      clock.c
  3. 1 0
      io.c
  4. 14 0
      reboot.c
  5. 11 0
      rtc.c
  6. 3 0
      rtc.h
  7. 21 0
      terminal.c
  8. 193 0
      vga.c
  9. 14 0
      vga.h

+ 8 - 2
Makefile

@@ -50,13 +50,19 @@ cat.o: cat.c fat.h io.h
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+vga.o: vga.c vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+clock.o: clock.c io.h rtc.h timer.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o clock.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -95,4 +101,4 @@ run: image.bin root.img
 	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o clock.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img

+ 182 - 0
clock.c

@@ -0,0 +1,182 @@
+/* clock command: analog clock in VGA mode 13h. Press ESC to return. */
+
+#include "io.h"
+#include "rtc.h"
+#include "timer.h"
+#include "vga.h"
+
+#define CX          (VGA_GFX_WIDTH / 2)
+#define CY          (VGA_GFX_HEIGHT / 2)
+#define RADIUS      80
+#define RAD_HOUR    40
+#define RAD_MIN     56
+#define RAD_SEC     72
+#define TICK_LEN    6
+
+#define COL_BG      0
+#define COL_FACE    15   /* white */
+#define COL_TICK    7    /* gray */
+#define COL_HOUR    15
+#define COL_MIN     15
+#define COL_SEC     12   /* light red */
+#define COL_CENTER  15
+
+/*
+ * Precomputed unit-vector table at radius 64 for 60 clock positions.
+ * Index 0 = 12 o'clock (up), 15 = 3 o'clock, 30 = 6 o'clock, etc.
+ * x = 64*sin(2*pi*i/60), y = -64*cos(2*pi*i/60)
+ */
+static const signed char tab_x[60] = {
+	  0,  7, 13, 20, 26, 32, 37, 42, 47, 51,
+	 55, 59, 62, 63, 64, 63, 62, 59, 55, 51,
+	 47, 42, 37, 32, 26, 20, 13,  7,  0, -7,
+	-13,-20,-26,-32,-37,-42,-47,-51,-55,-59,
+	-62,-63,-64,-63,-62,-59,-55,-51,-47,-42,
+	-37,-32,-26,-20,-13, -7
+};
+static const signed char tab_y[60] = {
+	-64,-63,-62,-59,-55,-51,-47,-42,-37,-32,
+	-26,-20,-13, -7,  0,  7, 13, 20, 26, 32,
+	 37, 42, 47, 51, 55, 59, 62, 63, 64, 63,
+	 62, 59, 55, 51, 47, 42, 37, 32, 26, 20,
+	 13,  7,  0, -7,-13,-20,-26,-32,-37,-42,
+	-47,-51,-55,-59,-62,-63
+};
+
+/* ---- drawing primitives ---- */
+
+static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
+{
+	int dx = x1 - x0;
+	int dy = y1 - y0;
+	int ax = (dx < 0) ? -dx : dx;
+	int ay = (dy < 0) ? -dy : dy;
+	int steps = (ax > ay) ? ax : ay;
+	int i;
+
+	if (steps == 0) {
+		vga_put_pixel(x0, y0, color);
+		return;
+	}
+	for (i = 0; i <= steps; i++) {
+		int x = x0 + dx * i / steps;
+		int y = y0 + dy * i / steps;
+		vga_put_pixel(x, y, color);
+	}
+}
+
+static void draw_circle(int cx, int cy, int r, unsigned char color)
+{
+	int x = r, y = 0;
+	int d = 1 - r;
+
+	while (y <= x) {
+		vga_put_pixel(cx + x, cy + y, color);
+		vga_put_pixel(cx - x, cy + y, color);
+		vga_put_pixel(cx + x, cy - y, color);
+		vga_put_pixel(cx - x, cy - y, color);
+		vga_put_pixel(cx + y, cy + x, color);
+		vga_put_pixel(cx - y, cy + x, color);
+		vga_put_pixel(cx + y, cy - x, color);
+		vga_put_pixel(cx - y, cy - x, color);
+		if (d <= 0) {
+			d += 2 * y + 3;
+		} else {
+			d += 2 * (y - x) + 5;
+			x--;
+		}
+		y++;
+	}
+}
+
+static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
+{
+	draw_line(x0, y0, x1, y1, color);
+	draw_line(x0 + 1, y0, x1 + 1, y1, color);
+	draw_line(x0, y0 + 1, x1, y1 + 1, color);
+}
+
+/* ---- clock rendering ---- */
+
+static void draw_face(void)
+{
+	int i;
+
+	draw_circle(CX, CY, RADIUS, COL_FACE);
+	draw_circle(CX, CY, RADIUS - 1, COL_FACE);
+
+	/* Hour tick marks */
+	for (i = 0; i < 60; i += 5) {
+		int inner = RADIUS - TICK_LEN;
+		int ox = CX + tab_x[i] * RADIUS / 64;
+		int oy = CY + tab_y[i] * RADIUS / 64;
+		int ix = CX + tab_x[i] * inner / 64;
+		int iy = CY + tab_y[i] * inner / 64;
+		draw_line(ix, iy, ox, oy, COL_TICK);
+	}
+}
+
+static void draw_hand(int idx60, int length, unsigned char color, int thick)
+{
+	int ex = CX + tab_x[idx60] * length / 64;
+	int ey = CY + tab_y[idx60] * length / 64;
+
+	if (thick)
+		draw_thick_line(CX, CY, ex, ey, color);
+	else
+		draw_line(CX, CY, ex, ey, color);
+}
+
+static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
+{
+	int hour_idx;
+
+	/* Hour hand: 5 positions per hour + minute offset */
+	hour_idx = (int)((h % 12) * 5 + m / 12);
+	if (hour_idx >= 60)
+		hour_idx = 59;
+
+	draw_hand(hour_idx, RAD_HOUR, COL_HOUR, 1);
+	draw_hand((int)m, RAD_MIN, COL_MIN, 1);
+	draw_hand((int)s, RAD_SEC, COL_SEC, 0);
+
+	/* Center dot */
+	vga_put_pixel(CX, CY, COL_CENTER);
+	vga_put_pixel(CX + 1, CY, COL_CENTER);
+	vga_put_pixel(CX, CY + 1, COL_CENTER);
+	vga_put_pixel(CX + 1, CY + 1, COL_CENTER);
+}
+
+/* ---- main entry point ---- */
+
+void program_clock_main(void)
+{
+	unsigned int last_sec = 99;
+	unsigned int h, m, s;
+	unsigned int tick;
+
+	vga_set_mode_13();
+	vga_clear(COL_BG);
+	io_drain_keyboard();
+
+	for (;;) {
+		int k = io_poll_key();
+		if (k == 0x1B)
+			break;
+
+		rtc_read_hms(&h, &m, &s);
+		if (s != last_sec) {
+			vga_clear(COL_BG);
+			draw_face();
+			draw_hands(h, m, s);
+			last_sec = s;
+		}
+
+		tick = get_ticks_ms();
+		while (get_ticks_ms() - tick < 50)
+			__asm__ volatile ("hlt");
+	}
+
+	vga_set_mode_text();
+	io_clear_screen();
+}

+ 1 - 0
io.c

@@ -27,6 +27,7 @@ static void disable_hw_cursor(void)
 
 /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
 static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
+	[0x01] = 0x1B,  /* Escape */
 	[0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
 	[0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
 	[0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',

+ 14 - 0
reboot.c

@@ -0,0 +1,14 @@
+/* Reboot command: trigger a CPU reset via the keyboard controller. */
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+void program_reboot_main(void)
+{
+	/* Pulse the CPU reset line via the keyboard controller (port 0x64). */
+	outb(0x64, 0xFE);
+	for (;;)
+		__asm__ volatile ("hlt");
+}

+ 11 - 0
rtc.c

@@ -69,3 +69,14 @@ void rtc_read_datetime(char *buf)
 	buf[18] = (char)('0' + (s % 10));
 	buf[19] = '\0';
 }
+
+void rtc_read_hms(unsigned int *hour, unsigned int *minute, unsigned int *second)
+{
+	unsigned int n = 0;
+
+	while ((cmos_read(0x0A) & 0x80) && ++n < 10000)
+		;
+	*second = bcd_to_bin(cmos_read(0x00));
+	*minute = bcd_to_bin(cmos_read(0x02));
+	*hour   = bcd_to_bin(cmos_read(0x04));
+}

+ 3 - 0
rtc.h

@@ -4,4 +4,7 @@
 /* Write formatted date/time into buf: "YYYY-MM-DD HH:MM:SS" (19 chars + NUL). */
 void rtc_read_datetime(char *buf);
 
+/* Read current hour (0-23), minute (0-59), second (0-59). */
+void rtc_read_hms(unsigned int *hour, unsigned int *minute, unsigned int *second);
+
 #endif

+ 21 - 0
terminal.c

@@ -9,6 +9,7 @@ extern void program_hello_main(void);
 extern void program_reboot_main(void);
 extern void program_ls_main(void);
 extern void program_cat_main(const char *filename);
+extern void program_clock_main(void);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
 
@@ -114,6 +115,22 @@ static int is_cmd_ls(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_clock(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
+	    line[i+3] != 'c' || line[i+4] != 'k')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 #define CAT_FNAME_MAX 64
 static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
 {
@@ -228,6 +245,10 @@ void terminal_run(void)
 						program_ls_main();
 						io_drain_keyboard();
 						draw_header_row();
+					} else if (is_cmd_clock(line, line_len)) {
+						program_clock_main();
+						io_drain_keyboard();
+						draw_header_row();
 					} else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
 						program_cat_main(cat_fname);
 						io_drain_keyboard();

+ 193 - 0
vga.c

@@ -0,0 +1,193 @@
+/* VGA mode 13h / mode 03h via direct register programming.
+   Saves and restores the 8x16 font from plane 2 so text mode survives. */
+
+#include "vga.h"
+
+#define VGA_FB ((volatile unsigned char *)0xA0000)
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static inline unsigned char inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+/* ---- low-level register helpers ---- */
+
+static void write_seq(unsigned char idx, unsigned char val)
+{
+	outb(0x3C4, idx);
+	outb(0x3C5, val);
+}
+
+static void write_crtc(unsigned char idx, unsigned char val)
+{
+	outb(0x3D4, idx);
+	outb(0x3D5, val);
+}
+
+static void write_gc(unsigned char idx, unsigned char val)
+{
+	outb(0x3CE, idx);
+	outb(0x3CF, val);
+}
+
+static void crtc_unlock(void)
+{
+	outb(0x3D4, 0x11);
+	outb(0x3D5, inb(0x3D5) & 0x7F);
+}
+
+/* ---- register tables ---- */
+
+/* Mode 13h: 320x200, 256 colors, chain-4, framebuffer at 0xA0000 */
+static const unsigned char misc_13  = 0x63;
+static const unsigned char seq_13[]  = { 0x03, 0x01, 0x0F, 0x00, 0x0E };
+static const unsigned char crtc_13[] = {
+	0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
+	0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3, 0xFF
+};
+static const unsigned char gc_13[]   = {
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 0xFF
+};
+static const unsigned char ac_13[]   = {
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+	0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+	0x41, 0x00, 0x0F, 0x00, 0x00
+};
+
+/* Mode 03h: 80x25, 16-color text, framebuffer at 0xB8000 */
+static const unsigned char misc_03  = 0x67;
+static const unsigned char seq_03[]  = { 0x03, 0x00, 0x03, 0x00, 0x02 };
+static const unsigned char crtc_03[] = {
+	0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
+	0x00, 0x4F, 0x20, 0x0E, 0x00, 0x00, 0x00, 0x00,
+	0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3, 0xFF
+};
+static const unsigned char gc_03[]   = {
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00, 0xFF
+};
+static const unsigned char ac_03[]   = {
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
+	0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
+	0x0C, 0x00, 0x0F, 0x08, 0x00
+};
+
+/* ---- font buffer: 256 chars x 32 bytes/char ---- */
+#define FONT_SIZE 8192
+static unsigned char font_buf[FONT_SIZE];
+
+/* ---- set all VGA registers for a mode ---- */
+
+static void set_regs(unsigned char misc,
+	const unsigned char *seq, const unsigned char *crtc,
+	const unsigned char *gc, const unsigned char *ac)
+{
+	unsigned int i;
+
+	/* Misc Output */
+	outb(0x3C2, misc);
+
+	/* Sequencer: sync reset, write 1-4, release */
+	write_seq(0, 0x01);
+	for (i = 1; i < 5; i++)
+		write_seq((unsigned char)i, seq[i]);
+	write_seq(0, seq[0]);
+
+	/* CRTC: unlock then write all 25 */
+	crtc_unlock();
+	for (i = 0; i < 25; i++)
+		write_crtc((unsigned char)i, crtc[i]);
+
+	/* Graphics Controller */
+	for (i = 0; i < 9; i++)
+		write_gc((unsigned char)i, gc[i]);
+
+	/* Attribute Controller */
+	(void)inb(0x3DA);  /* reset flip-flop */
+	for (i = 0; i < 21; i++) {
+		outb(0x3C0, (unsigned char)i);
+		outb(0x3C0, ac[i]);
+	}
+	/* Re-enable display (PAS bit) */
+	(void)inb(0x3DA);
+	outb(0x3C0, 0x20);
+}
+
+/* ---- font save / restore via plane 2 ---- */
+
+static void font_plane_access(void)
+{
+	write_seq(0, 0x01);       /* sync reset */
+	write_seq(2, 0x04);       /* map mask: plane 2 only */
+	write_seq(4, 0x06);       /* sequential access, extended memory */
+	write_seq(0, 0x03);       /* release reset */
+	write_gc(4, 0x02);        /* read map select: plane 2 */
+	write_gc(5, 0x00);        /* mode: read/write mode 0 */
+	write_gc(6, 0x00);        /* misc: A0000, 128K window */
+}
+
+static void text_plane_access(void)
+{
+	write_seq(0, 0x01);       /* sync reset */
+	write_seq(2, 0x03);       /* map mask: planes 0 + 1 */
+	write_seq(4, 0x02);       /* odd/even, extended memory */
+	write_seq(0, 0x03);       /* release reset */
+	write_gc(4, 0x00);        /* read map select: plane 0 */
+	write_gc(5, 0x10);        /* odd/even mode */
+	write_gc(6, 0x0E);        /* B8000, 32K window */
+}
+
+static void save_font(void)
+{
+	unsigned int i;
+
+	font_plane_access();
+	for (i = 0; i < FONT_SIZE; i++)
+		font_buf[i] = VGA_FB[i];
+}
+
+static void restore_font(void)
+{
+	unsigned int i;
+
+	font_plane_access();
+	for (i = 0; i < FONT_SIZE; i++)
+		VGA_FB[i] = font_buf[i];
+	text_plane_access();
+}
+
+/* ---- public API ---- */
+
+void vga_set_mode_13(void)
+{
+	save_font();
+	set_regs(misc_13, seq_13, crtc_13, gc_13, ac_13);
+}
+
+void vga_set_mode_text(void)
+{
+	set_regs(misc_03, seq_03, crtc_03, gc_03, ac_03);
+	restore_font();
+}
+
+void vga_put_pixel(int x, int y, unsigned char color)
+{
+	if (x >= 0 && x < VGA_GFX_WIDTH && y >= 0 && y < VGA_GFX_HEIGHT)
+		VGA_FB[y * VGA_GFX_WIDTH + x] = color;
+}
+
+void vga_clear(unsigned char color)
+{
+	unsigned int i;
+	unsigned int n = VGA_GFX_WIDTH * VGA_GFX_HEIGHT;
+
+	for (i = 0; i < n; i++)
+		VGA_FB[i] = color;
+}

+ 14 - 0
vga.h

@@ -0,0 +1,14 @@
+/* VGA graphics: mode 13h (320x200 256-color) and text mode 03h.
+   Works on real VGA hardware and QEMU alike. */
+#ifndef VGA_H
+#define VGA_H
+
+#define VGA_GFX_WIDTH   320
+#define VGA_GFX_HEIGHT  200
+
+void vga_set_mode_13(void);
+void vga_set_mode_text(void);
+void vga_put_pixel(int x, int y, unsigned char color);
+void vga_clear(unsigned char color);
+
+#endif