Просмотр исходного кода

Add color command to test video performance

Johannes Zellner 8 месяцев назад
Родитель
Сommit
dcd588b70d
5 измененных файлов с 203 добавлено и 24 удалено
  1. 5 2
      Makefile
  2. 133 0
      colors.c
  3. 38 22
      fbtext.c
  4. 4 0
      fbtext.h
  5. 23 0
      terminal.c

+ 5 - 2
Makefile

@@ -65,6 +65,9 @@ ati.o: ati.c ati.h pci.h
 clock.o: clock.c io.h rtc.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+colors.o: colors.c fbtext.h io.h timer.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 display.o: display.c io.h vga.h ati.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -74,7 +77,7 @@ boot.elf: boot.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 vga.o ati.o font_8x8.o fbtext.o clock.o display.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 ati.o font_8x8.o fbtext.o clock.o colors.o display.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -113,4 +116,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 vga.o ati.o font_8x8.o fbtext.o clock.o display.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 ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img

+ 133 - 0
colors.c

@@ -0,0 +1,133 @@
+/* colors command: full-screen animated rainbow in VESA LFB. Target 60fps. Press ESC to return. */
+
+#include "fbtext.h"
+#include "io.h"
+#include "timer.h"
+#include "vga.h"
+
+#define RAINBOW_SEG   256
+#define RAINBOW_FULL  (6 * RAINBOW_SEG)
+#define FRAME_MS      16
+#define PHASE_STEP    32
+/* FPS overlay: 7 chars × 8 px wide, 8 px tall. Rainbow must not draw here. */
+#define FPS_RECT_W    (7 * 8)
+#define FPS_RECT_H    8
+
+static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsigned int *b)
+{
+	unsigned int seg = hue / RAINBOW_SEG;
+	unsigned int f   = hue % RAINBOW_SEG;
+	unsigned int r1, g1, b1, r2, g2, b2;
+	if (seg >= 6)
+		seg = 0;
+	switch (seg) {
+		case 0: r1=255; g1=0;   b1=0;   r2=255; g2=255; b2=0;   break; /* R → Y */
+		case 1: r1=255; g1=255; b1=0;   r2=0;   g2=255; b2=0;   break; /* Y → G */
+		case 2: r1=0;   g1=255; b1=0;   r2=0;   g2=255; b2=255; break; /* G → C */
+		case 3: r1=0;   g1=255; b1=255; r2=0;   g2=0;   b2=255; break; /* C → B */
+		case 4: r1=0;   g1=0;   b1=255; r2=255; g2=0;   b2=255; break; /* B → M */
+		default: r1=255; g1=0; b1=255; r2=255; g2=0;   b2=0;   break; /* M → R */
+	}
+	*r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
+	*g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
+	*b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
+}
+
+static void draw_frame(volatile unsigned char *fb, unsigned int w, unsigned int h,
+		unsigned int pitch, unsigned int bpp, unsigned int phase)
+{
+	/* One full rainbow across width and height (stretched) */
+	unsigned int scale_x = RAINBOW_FULL / (w ? w : 1);
+	unsigned int scale_y = RAINBOW_FULL / (h ? h : 1);
+	unsigned int y, x;
+
+	for (y = 0; y < h; y++) {
+		for (x = 0; x < w; x++) {
+			/* Leave FPS rect untouched so the overlay is never overwritten */
+			if (y < FPS_RECT_H && x < FPS_RECT_W)
+				continue;
+			unsigned int hue = (phase + x * scale_x + y * scale_y) % RAINBOW_FULL;
+			unsigned int r, g, b;
+			hue_to_rgb(hue, &r, &g, &b);
+
+			if (bpp == 16) {
+				unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+				unsigned int off = (unsigned long)y * pitch + x * 2;
+				fb[off]     = (unsigned char)(rgb16 & 0xFF);
+				fb[off + 1] = (unsigned char)(rgb16 >> 8);
+			} else if (bpp == 24) {
+				unsigned int off = (unsigned long)y * pitch + x * 3;
+				fb[off]     = (unsigned char)b;
+				fb[off + 1] = (unsigned char)g;
+				fb[off + 2] = (unsigned char)r;
+			} else if (bpp == 32) {
+				volatile unsigned int *row = (volatile unsigned int *)(fb + (unsigned long)y * pitch);
+				row[x] = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
+			}
+		}
+	}
+}
+
+#define FPS_BUF_SIZE  8
+
+static void format_fps(unsigned int fps, char *buf)
+{
+	unsigned int f = fps;
+	if (f > 999)
+		f = 999;
+	buf[0] = (char)('0' + (f / 100));
+	buf[1] = (char)('0' + ((f / 10) % 10));
+	buf[2] = (char)('0' + (f % 10));
+	buf[3] = ' ';
+	buf[4] = 'f';
+	buf[5] = 'p';
+	buf[6] = 's';
+	buf[7] = '\0';
+}
+
+void program_colors_main(void)
+{
+	volatile unsigned char *fb;
+	unsigned int w, h, pitch, bpp;
+	unsigned int phase = 0;
+	unsigned int frame_start;
+	unsigned int frame_count = 0;
+	unsigned int last_fps_ticks;
+	unsigned int displayed_fps = 0;
+	char fps_buf[FPS_BUF_SIZE];
+
+	if (!vga_has_vesa())
+		return;
+	fb = (volatile unsigned char *)vga_fb_ptr();
+	if (!fb)
+		return;
+	w = vga_fb_width();
+	h = vga_fb_height();
+	pitch = vga_fb_pitch();
+	bpp = vga_fb_bpp();
+	if (bpp != 16 && bpp != 24 && bpp != 32)
+		return;
+
+	io_drain_keyboard();
+	last_fps_ticks = get_ticks_ms();
+
+	for (;;) {
+		frame_start = get_ticks_ms();
+		if (io_poll_key() == 0x1B)
+			break;
+		draw_frame(fb, w, h, pitch, bpp, phase);
+		phase = (phase + PHASE_STEP) % RAINBOW_FULL;
+
+		frame_count++;
+		if (get_ticks_ms() - last_fps_ticks >= 1000) {
+			displayed_fps = frame_count;
+			frame_count = 0;
+			last_fps_ticks = get_ticks_ms();
+		}
+		format_fps(displayed_fps, fps_buf);
+		fbtext_draw_string_xy(0, 0, fps_buf, 255, 255, 255, 0);
+
+		while (get_ticks_ms() - frame_start < FRAME_MS)
+			__asm__ volatile ("hlt");
+	}
+}

+ 38 - 22
fbtext.c

@@ -21,6 +21,29 @@ static inline unsigned int row_to_y0(unsigned int row)
 	return row == 0 ? HEADER_TOP : (CONTENT_TOP + (row - 1) * ROW_HEIGHT);
 }
 
+/* Draw one 8x8 glyph at pixel (x0, y0). height 8 = glyph only; 10 = glyph + 2 rows bg (terminal row).
+   skip_bg: 1 = only draw foreground (transparent background for overlays). */
+static void draw_glyph_at(unsigned int x0, unsigned int y0, unsigned char c,
+		unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
+		unsigned int r_bg, unsigned int g_bg, unsigned int b_bg,
+		unsigned int height, int skip_bg)
+{
+	unsigned int x, y;
+	const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
+
+	for (y = 0; y < height; y++) {
+		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
+		for (x = 0; x < 8; x++) {
+			unsigned int px = x0 + x;
+			unsigned int py = y0 + y;
+			if (row_bits & (0x80u >> x))
+				vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
+			else if (!skip_bg)
+				vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
+		}
+	}
+}
+
 /* VGA attribute (0-15) to RGB. Standard VGA palette. */
 static void attr_to_rgb(unsigned char attr, int fg, unsigned int *r, unsigned int *g, unsigned int *b)
 {
@@ -85,27 +108,11 @@ void fbtext_clear(void)
 
 static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
 {
-	unsigned int x0 = col * 8;
-	unsigned int y0 = row_to_y0(row);
 	unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
-	unsigned int x, y;
-	const unsigned char *glyph;
 
 	attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
 	attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
-
-	glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
-	for (y = 0; y < ROW_HEIGHT; y++) {
-		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
-		for (x = 0; x < 8; x++) {
-			unsigned int px = x0 + x;
-			unsigned int py = y0 + y;
-			if (row_bits & (0x80u >> x))
-				vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
-			else
-				vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
-		}
-	}
+	draw_glyph_at(col * 8, row_to_y0(row), c, r_fg, g_fg, b_fg, r_bg, g_bg, b_bg, ROW_HEIGHT, 0);
 }
 
 void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
@@ -169,16 +176,14 @@ void fbtext_show_cursor(void)
 /* Redraw only the cursor region (CURSOR_W x CURSOR_H) so the line is restored. */
 static void draw_glyph_cursor_region(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
 {
-	unsigned int x0 = col * 8;
-	unsigned int y0 = row_to_y0(row);
 	unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
 	unsigned int x, y;
-	const unsigned char *glyph;
+	const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
+	unsigned int x0 = col * 8;
+	unsigned int y0 = row_to_y0(row);
 
 	attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
 	attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
-
-	glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
 	for (y = 0; y < ROW_HEIGHT && y < CURSOR_H; y++) {
 		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
 		for (x = 0; x < CURSOR_W; x++) {
@@ -224,6 +229,17 @@ static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsig
 	*b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
 }
 
+/* Draw string at pixel (x0, y0) with fg RGB. transparent_bg 1 = only fg pixels (for overlays). */
+void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
+		unsigned int r_fg, unsigned int g_fg, unsigned int b_fg, int transparent_bg)
+{
+	unsigned int i = 0;
+	while (str[i]) {
+		draw_glyph_at(x0 + i * 8, y0, (unsigned char)str[i], r_fg, g_fg, b_fg, 0, 0, 0, FONT_8X8_ROWS, transparent_bg);
+		i++;
+	}
+}
+
 #define RAINBOW_LINE_HEIGHT  2   /* 2-pixel tall line for visibility */
 
 void fbtext_draw_rainbow_separator(void)

+ 4 - 0
fbtext.h

@@ -16,6 +16,10 @@ void fbtext_show_cursor(void);
 /* Hide cursor and redraw cell with (c, attr) so the character is restored. */
 void fbtext_hide_cursor(unsigned char c, unsigned char attr);
 
+/* Draw string at pixel (x0, y0) with fg RGB. transparent_bg 1 = only fg pixels (overlays). */
+void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
+		unsigned int r_fg, unsigned int g_fg, unsigned int b_fg, int transparent_bg);
+
 /* Draw a rainbow gradient line just below the header row (between header and content). No-op if not LFB. */
 void fbtext_draw_rainbow_separator(void);
 

+ 23 - 0
terminal.c

@@ -11,6 +11,7 @@ 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 program_colors_main(void);
 extern void program_display_main(void);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
@@ -134,6 +135,22 @@ static int is_cmd_clock(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_colors(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 6 > line_len)
+		return 0;
+	if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
+	    line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_display(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -301,6 +318,11 @@ void terminal_run(void)
 						io_drain_keyboard();
 						io_clear_screen();
 						draw_header_row();
+					} else if (is_cmd_colors(line, line_len)) {
+						program_colors_main();
+						io_drain_keyboard();
+						io_clear_screen();
+						draw_header_row();
 					} else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
 						program_cat_main(cat_fname);
 						io_drain_keyboard();
@@ -317,6 +339,7 @@ void terminal_run(void)
 						io_putstr("reboot\n");
 						io_putstr("ls\n");
 						io_putstr("clock\n");
+						io_putstr("colors\n");
 						io_putstr("cat <file>\n");
 						io_putstr("display\n");
 						io_putstr("drive <0|1>\n");