Răsfoiți Sursa

Add slow shadow buffering

Johannes Zellner 5 luni în urmă
părinte
comite
db56220e16
9 a modificat fișierele cu 223 adăugiri și 27 ștergeri
  1. 13 2
      Makefile
  2. 3 3
      colors.c
  3. 3 0
      display.c
  4. 2 0
      kernel.c
  5. 17 0
      switch.S
  6. 2 0
      terminal.c
  7. 137 20
      vga.c
  8. 23 2
      vga.h
  9. 23 0
      vga_copy.S

+ 13 - 2
Makefile

@@ -68,6 +68,9 @@ serial.o: serial.c serial.h pci.h
 vga.o: vga.c vga.h pci.h gpu.h ati.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+vga_copy.o: vga_copy.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
 gpu.o: gpu.c gpu.h pci.h ati.h nvidia.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -113,7 +116,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -193,5 +196,13 @@ run-usb: usb.img root.img
 	qemu-system-i386 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
+scp-final: final.bin
+	scp final.bin root@192.168.0.136:
+	ssh root@192.168.0.136 dd if=final.bin of=/dev/fd0 bs=512
+
+scp-root: root.img
+	scp root.img root@192.168.0.136:
+	ssh root@192.168.0.136 dd if=root.img of=/dev/fd0 bs=512
+
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 3 - 3
colors.c

@@ -33,7 +33,7 @@ static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsig
 	*b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
 }
 
-static void draw_frame(volatile unsigned char *fb, unsigned int w, unsigned int h,
+static void draw_frame(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) */
@@ -87,7 +87,7 @@ static void format_fps(unsigned int fps, char *buf)
 
 void program_colors_main(void)
 {
-	volatile unsigned char *fb;
+	unsigned char *fb;
 	unsigned int w, h, pitch, bpp;
 	unsigned int phase = 0;
 	unsigned int frame_start;
@@ -98,7 +98,7 @@ void program_colors_main(void)
 
 	if (!vga_has_vesa())
 		return;
-	fb = (volatile unsigned char *)vga_fb_ptr();
+	fb = (unsigned char *)vga_fb_ptr();
 	if (!fb)
 		return;
 	w = vga_fb_width();

+ 3 - 0
display.c

@@ -63,6 +63,9 @@ void program_display_main(void)
 	io_putstr("LFB ");
 	put_hex((unsigned int)(unsigned long)vga_fb_ptr());
 	io_putchar('\n');
+	io_putstr("rendering: ");
+	io_putstr(vga_shadow_is_active() ? "shadow buffer" : "direct");
+	io_putchar('\n');
 	{
 		unsigned int pci_id = vga_fb_pci_id();
 		if (pci_id) {

+ 2 - 0
kernel.c

@@ -134,5 +134,7 @@ void kernel_main(void)
 	timer_init();
 	mouse_init();
 	process_init();
+	vga_shadow_init();
+	vga_fb_flush();
 	terminal_run();
 }

+ 17 - 0
switch.S

@@ -5,6 +5,9 @@
  * current process's stack.  We save the rest, bump the tick counter,
  * and -- when the timeslice expires -- call schedule() and swap ESP
  * to the next process.
+ *
+ * Shadow buffer flush: every ~33 ticks (30Hz), copy the shadow buffer
+ * to the video framebuffer using USWC write-combining.
  */
 .code32
 .text
@@ -13,12 +16,15 @@
 .extern current_pid
 .extern procs
 .extern schedule
+.extern vga_shadow_dirty
+.extern vga_shadow_flush_pending
 
 /* struct process offsets (must match process.h / process.c) */
 .equ PCB_ESP,   0
 .equ PCB_SIZE,  24      /* sizeof(struct process): 4+4+4+12 = 24 */
 
 .equ TIMESLICE, 20
+.equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */
 
 .globl pit_irq_stub
 pit_irq_stub:
@@ -29,6 +35,15 @@ pit_irq_stub:
 	movb  $0x20, %al
 	outb  %al, $0x20
 
+	/* Shadow buffer flush at 30Hz: just set flag, main loop does the copy. */
+	decl  flush_counter
+	jnz   .no_flush
+	movl  $FLUSH_INTERVAL, flush_counter
+	cmpl  $0, vga_shadow_dirty
+	jz    .no_flush
+	movl  $1, vga_shadow_flush_pending
+.no_flush:
+
 	/* Decrement timeslice counter; switch only when it reaches 0. */
 	decl  slice_counter
 	jnz   .no_switch
@@ -59,3 +74,5 @@ pit_irq_stub:
 .align 4
 slice_counter:
 	.long TIMESLICE
+flush_counter:
+	.long FLUSH_INTERVAL

+ 2 - 0
terminal.c

@@ -9,6 +9,7 @@
 #include "rtc.h"
 #include "terminal.h"
 #include "timer.h"
+#include "vga.h"
 
 extern void program_hello_main(void);
 extern void program_reboot_main(void);
@@ -510,6 +511,7 @@ void terminal_run(void)
 	line_start = io_get_cursor();
 
 	for (;;) {
+		vga_fb_flush_pending_check();
 		mouse_poll_update();
 		update_datetime();
 		update_cursor_blink();

+ 137 - 20
vga.c

@@ -1,9 +1,10 @@
-/* VGA: VESA LFB (from boot). */
+/* VGA: VESA LFB (from boot) with shadow buffer and 30Hz USWC flush. */
 
 #include "vga.h"
 #include "pci.h"
 #include "gpu.h"
 #include "ati.h"
+#include "serial.h"
 
 /* Cached boot framebuffer info (set once when vga_fb_* first used). */
 static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
@@ -11,6 +12,12 @@ static unsigned int vga_fb_vram_kb_val;
 static volatile unsigned char *vga_fb_lfb;
 static int vga_fb_cached;
 
+/* Shadow buffer state. */
+static unsigned char *vga_shadow_buf;
+static unsigned int   vga_shadow_pitch;
+static unsigned int   vga_shadow_size;
+int vga_shadow_dirty;
+
 /*
  * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
  * Fixes real hardware where VBE mode set leaves stale CRTC state,
@@ -139,6 +146,8 @@ unsigned int vga_fb_pci_id(void)
 void *vga_fb_ptr(void)
 {
 	vga_cache_boot_fb();
+	if (vga_shadow_buf)
+		return (void *)vga_shadow_buf;
 	return (void *)vga_fb_lfb;
 }
 
@@ -149,15 +158,15 @@ int vga_has_vesa(void)
 }
 
 /* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
-static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
+static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 {
-	unsigned int pitch = vga_fb_pitch_val;
+	unsigned int pitch = vga_shadow_pitch;
 	unsigned int bpp = vga_fb_bpp_val;
 	unsigned char *p;
 
-	if (x >= vga_fb_w || y >= vga_fb_h || !vga_fb_lfb)
+	if (x >= vga_fb_w || y >= vga_fb_h || !vga_shadow_buf)
 		return;
-	p = (unsigned char *)vga_fb_lfb + (unsigned long)y * pitch;
+	p = vga_shadow_buf + (unsigned long)y * pitch;
 	if (bpp == 16) {
 		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
 		unsigned int off = x * 2;
@@ -180,9 +189,10 @@ static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, un
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 {
 	vga_cache_boot_fb();
-	if (!vga_fb_lfb)
+	if (!vga_shadow_buf)
 		return;
-	vga_lfb_put_pixel(x, y, r, g, b);
+	shadow_put_pixel(x, y, r, g, b);
+	vga_shadow_dirty = 1;
 }
 
 static unsigned int vga_pixel_bytes(void)
@@ -199,14 +209,14 @@ void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned i
 {
 	unsigned int pitch, pb, row, col;
 	unsigned char *fb;
-	volatile unsigned char *src;
+	const unsigned char *src;
 
 	vga_cache_boot_fb();
-	if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
+	if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
 		return;
-	pitch = vga_fb_pitch_val;
+	pitch = vga_shadow_pitch;
 	pb = vga_pixel_bytes();
-	fb = (unsigned char *)vga_fb_lfb;
+	fb = vga_shadow_buf;
 	for (row = 0; row < h; row++) {
 		src = fb + (unsigned long)(y + row) * pitch + x * pb;
 		for (col = 0; col < w * pb; col++)
@@ -218,38 +228,39 @@ void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned
 		const unsigned char *buf)
 {
 	unsigned int pitch, pb, row, col;
-	volatile unsigned char *dst;
+	unsigned char *dst;
 
 	vga_cache_boot_fb();
-	if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
+	if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
 		return;
-	pitch = vga_fb_pitch_val;
+	pitch = vga_shadow_pitch;
 	pb = vga_pixel_bytes();
 	for (row = 0; row < h; row++) {
-		dst = vga_fb_lfb + (unsigned long)(y + row) * pitch + x * pb;
+		dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
 		for (col = 0; col < w * pb; col++)
 			dst[col] = buf[row * w * pb + col];
 	}
+	vga_shadow_dirty = 1;
 }
 
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 {
-	unsigned int pitch = vga_fb_pitch_val;
+	unsigned int pitch = vga_shadow_pitch;
 	unsigned int bpp = vga_fb_bpp_val;
 
 	vga_cache_boot_fb();
-	if (!vga_fb_lfb)
+	if (!vga_shadow_buf)
 		return;
 	if (bpp == 16) {
 		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
 		unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
-		volatile unsigned short *fb = (volatile unsigned short *)vga_fb_lfb;
+		unsigned short *fb = (unsigned short *)vga_shadow_buf;
 		unsigned int n = (pitch / 2) * vga_fb_h;
 		unsigned int i;
 		for (i = 0; i < n; i++)
 			fb[i] = val;
 	} else if (bpp == 24) {
-		volatile unsigned char *p = vga_fb_lfb;
+		unsigned char *p = vga_shadow_buf;
 		unsigned int row, x;
 		for (row = 0; row < vga_fb_h; row++) {
 			unsigned int row_off = row * pitch;
@@ -261,7 +272,7 @@ void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 			}
 		}
 	} else if (bpp == 32) {
-		volatile unsigned int *fb = (volatile unsigned int *)vga_fb_lfb;
+		unsigned int *fb = (unsigned int *)vga_shadow_buf;
 		unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
 		unsigned int stride = pitch / 4;
 		unsigned int row, x;
@@ -269,4 +280,110 @@ void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 			for (x = 0; x < vga_fb_w; x++)
 				fb[row * stride + x] = pixel;
 	}
+	vga_shadow_dirty = 1;
+}
+
+/* ---- Shadow buffer init and flush ---- */
+
+/* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
+extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
+
+int vga_shadow_flush_pending;
+
+void vga_shadow_init(void)
+{
+	vga_cache_boot_fb();
+	if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0) {
+		serial_puts("vga: no VESA mode, shadow buffer disabled\n");
+		return;
+	}
+
+	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
+	vga_shadow_pitch = vga_fb_pitch_val;
+	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
+	vga_shadow_dirty = 0;
+	vga_shadow_flush_pending = 0;
+	serial_puts("vga: shadow buffer ");
+	serial_puthex((unsigned int)VGA_SHADOW_BASE);
+	serial_puts(" size=");
+	serial_putdec(vga_shadow_size / 1024);
+	serial_puts("KB\n");
+}
+
+/* Single bulk copy: shadow pitch == VRAM pitch, so entire buffer is contiguous. */
+static void shadow_bulk_copy(void)
+{
+	unsigned int dwords = vga_shadow_size / 4;
+	vga_fb_copy_row((void *)vga_fb_lfb, (void *)vga_shadow_buf, dwords);
+}
+
+/* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
+   so it gets copied to VRAM along with everything else. Uses bright colors to be obvious. */
+static void draw_flush_debug_square(void)
+{
+	static unsigned int flush_color_counter;
+	unsigned int r, g, b;
+	unsigned int x, y;
+	unsigned int sx = vga_fb_w - 32;
+	unsigned int sy = vga_fb_h - 32;
+	unsigned int pitch = vga_shadow_pitch;
+	unsigned int bpp = vga_fb_bpp_val;
+	unsigned char *fb = vga_shadow_buf;
+
+	flush_color_counter++;
+	/* Cycle through bright primary/secondary colors */
+	switch (flush_color_counter % 6) {
+		case 0: r = 255; g = 0;   b = 0;   break; /* red */
+		case 1: r = 0;   g = 255; b = 0;   break; /* green */
+		case 2: r = 0;   g = 0;   b = 255; break; /* blue */
+		case 3: r = 255; g = 255; b = 0;   break; /* yellow */
+		case 4: r = 255; g = 0;   b = 255; break; /* magenta */
+		default: r = 0; g = 255; b = 255; break; /* cyan */
+	}
+
+	for (y = 0; y < 32; y++) {
+		unsigned char *row = fb + (sy + y) * pitch;
+		for (x = 0; x < 32; x++) {
+			if (bpp == 16) {
+				unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+				unsigned short *p = (unsigned short *)(row + (sx + x) * 2);
+				*p = (unsigned short)rgb16;
+			} else if (bpp == 24) {
+				row[(sx + x) * 3]     = (unsigned char)b;
+				row[(sx + x) * 3 + 1] = (unsigned char)g;
+				row[(sx + x) * 3 + 2] = (unsigned char)r;
+			} else if (bpp == 32) {
+				unsigned int *p = (unsigned int *)(row + (sx + x) * 4);
+				*p = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
+			}
+		}
+	}
+}
+
+void vga_fb_flush_pending_check(void)
+{
+	if (!vga_shadow_flush_pending)
+		return;
+	vga_shadow_flush_pending = 0;
+	if (!vga_shadow_dirty)
+		return;
+	serial_puts("vga: flush\n");
+	draw_flush_debug_square();
+	shadow_bulk_copy();
+	vga_shadow_dirty = 0;
+}
+
+void vga_fb_flush(void)
+{
+	vga_cache_boot_fb();
+	if (!vga_shadow_buf || !vga_fb_lfb || !vga_shadow_dirty)
+		return;
+	draw_flush_debug_square();
+	shadow_bulk_copy();
+	vga_shadow_dirty = 0;
+}
+
+int vga_shadow_is_active(void)
+{
+	return vga_shadow_buf != 0;
 }

+ 23 - 2
vga.h

@@ -1,4 +1,4 @@
-/* VGA: VESA LFB (from boot). */
+/* VGA: VESA LFB (from boot). Shadow buffer with 30Hz USWC flush. */
 #ifndef VGA_H
 #define VGA_H
 
@@ -16,7 +16,10 @@ struct vga_boot_fb_info {
 
 #define VGA_BOOT_FB ((volatile struct vga_boot_fb_info *)0x600)
 
-/* LFB API. RGB 0..255. */
+/* Shadow buffer lives at 1MB — safely above kernel, stacks, DMA, and program load area. */
+#define VGA_SHADOW_BASE 0x100000
+
+/* LFB API. RGB 0..255. All drawing goes into the shadow buffer; flush copies to VRAM at 30Hz. */
 unsigned int vga_fb_width(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_pitch(void);
@@ -35,4 +38,22 @@ void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned i
 void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		const unsigned char *buf);
 
+/* Initialize shadow buffer (called once from kernel_main after vga_cache_boot_fb). */
+void vga_shadow_init(void);
+
+/* Copy shadow buffer to VRAM. Called by 30Hz timer or manually after init. */
+void vga_fb_flush(void);
+
+/* Flag set by PIT IRQ when 30Hz flush is due. Main loop checks and clears. */
+extern int vga_shadow_flush_pending;
+
+/* Called from main loop: if pending, copies shadow to VRAM and draws debug square. */
+void vga_fb_flush_pending_check(void);
+
+/* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
+int vga_shadow_is_active(void);
+
+/* Dirty flag: set by any draw operation, checked by timer flush. */
+extern int vga_shadow_dirty;
+
 #endif

+ 23 - 0
vga_copy.S

@@ -0,0 +1,23 @@
+/* VGA framebuffer row copy: rep movsl optimized for P5 USWC write-combining.
+ *
+ * void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
+ *
+ * On Pentium (P5), rep movsl to UC memory (PCI framebuffer) uses the
+ * 64-byte USWC write-combining buffer. Writes are batched and flushed
+ * as burst transfers to VRAM, avoiding per-dword PCI bus stalls.
+ */
+.code32
+.text
+
+.globl vga_fb_copy_row
+vga_fb_copy_row:
+	pushl %esi
+	pushl %edi
+	movl  12(%esp), %edi	/* dst */
+	movl  16(%esp), %esi	/* src */
+	movl  20(%esp), %ecx	/* dword count */
+	cld
+	rep movsl
+	popl %edi
+	popl %esi
+	ret